1 /*
2 card-opti92x-ad1848.c - driver for OPTi 82c92x based soundcards.
3 Copyright (C) 1998-2000 by Massimo Piccioni <dafastidio@libero.it>
4
5 Part of this code was developed at the Italian Ministry of Air Defence,
6 Sixth Division (oh, che pace ...), Rome.
7
8 Thanks to Maria Grazia Pollarini, Salvatore Vassallo.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25
26 #include <linux/init.h>
27 #include <linux/err.h>
28 #include <linux/isa.h>
29 #include <linux/delay.h>
30 #include <linux/pnp.h>
31 #include <linux/module.h>
32 #include <linux/io.h>
33 #include <asm/dma.h>
34 #include <sound/core.h>
35 #include <sound/tlv.h>
36 #include <sound/wss.h>
37 #include <sound/mpu401.h>
38 #include <sound/opl3.h>
39 #ifndef OPTi93X
40 #include <sound/opl4.h>
41 #endif
42 #define SNDRV_LEGACY_FIND_FREE_IOPORT
43 #define SNDRV_LEGACY_FIND_FREE_IRQ
44 #define SNDRV_LEGACY_FIND_FREE_DMA
45 #include <sound/initval.h>
46
47 MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>");
48 MODULE_LICENSE("GPL");
49 #ifdef OPTi93X
50 MODULE_DESCRIPTION("OPTi93X");
51 MODULE_SUPPORTED_DEVICE("{{OPTi,82C931/3}}");
52 #else /* OPTi93X */
53 #ifdef CS4231
54 MODULE_DESCRIPTION("OPTi92X - CS4231");
55 MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (CS4231)},"
56 "{OPTi,82C925 (CS4231)}}");
57 #else /* CS4231 */
58 MODULE_DESCRIPTION("OPTi92X - AD1848");
59 MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (AD1848)},"
60 "{OPTi,82C925 (AD1848)},"
61 "{OAK,Mozart}}");
62 #endif /* CS4231 */
63 #endif /* OPTi93X */
64
65 static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
66 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
67 //static bool enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */
68 #ifdef CONFIG_PNP
69 static bool isapnp = true; /* Enable ISA PnP detection */
70 #endif
71 static long port = SNDRV_DEFAULT_PORT1; /* 0x530,0xe80,0xf40,0x604 */
72 static long mpu_port = SNDRV_DEFAULT_PORT1; /* 0x300,0x310,0x320,0x330 */
73 static long fm_port = SNDRV_DEFAULT_PORT1; /* 0x388 */
74 static int irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10,11 */
75 static int mpu_irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10 */
76 static int dma1 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
77 #if defined(CS4231) || defined(OPTi93X)
78 static int dma2 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
79 #endif /* CS4231 || OPTi93X */
80
81 module_param(index, int, 0444);
82 MODULE_PARM_DESC(index, "Index value for opti9xx based soundcard.");
83 module_param(id, charp, 0444);
84 MODULE_PARM_DESC(id, "ID string for opti9xx based soundcard.");
85 //module_param(enable, bool, 0444);
86 //MODULE_PARM_DESC(enable, "Enable opti9xx soundcard.");
87 #ifdef CONFIG_PNP
88 module_param(isapnp, bool, 0444);
89 MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard.");
90 #endif
91 module_param_hw(port, long, ioport, 0444);
92 MODULE_PARM_DESC(port, "WSS port # for opti9xx driver.");
93 module_param_hw(mpu_port, long, ioport, 0444);
94 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for opti9xx driver.");
95 module_param_hw(fm_port, long, ioport, 0444);
96 MODULE_PARM_DESC(fm_port, "FM port # for opti9xx driver.");
97 module_param_hw(irq, int, irq, 0444);
98 MODULE_PARM_DESC(irq, "WSS irq # for opti9xx driver.");
99 module_param_hw(mpu_irq, int, irq, 0444);
100 MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for opti9xx driver.");
101 module_param_hw(dma1, int, dma, 0444);
102 MODULE_PARM_DESC(dma1, "1st dma # for opti9xx driver.");
103 #if defined(CS4231) || defined(OPTi93X)
104 module_param_hw(dma2, int, dma, 0444);
105 MODULE_PARM_DESC(dma2, "2nd dma # for opti9xx driver.");
106 #endif /* CS4231 || OPTi93X */
107
108 #define OPTi9XX_HW_82C928 1
109 #define OPTi9XX_HW_82C929 2
110 #define OPTi9XX_HW_82C924 3
111 #define OPTi9XX_HW_82C925 4
112 #define OPTi9XX_HW_82C930 5
113 #define OPTi9XX_HW_82C931 6
114 #define OPTi9XX_HW_82C933 7
115 #define OPTi9XX_HW_LAST OPTi9XX_HW_82C933
116
117 #define OPTi9XX_MC_REG(n) n
118
119 #ifdef OPTi93X
120
121 #define OPTi93X_STATUS 0x02
122 #define OPTi93X_PORT(chip, r) ((chip)->port + OPTi93X_##r)
123
124 #define OPTi93X_IRQ_PLAYBACK 0x04
125 #define OPTi93X_IRQ_CAPTURE 0x08
126
127 #endif /* OPTi93X */
128
129 struct snd_opti9xx {
130 unsigned short hardware;
131 unsigned char password;
132 char name[7];
133
134 unsigned long mc_base;
135 struct resource *res_mc_base;
136 unsigned long mc_base_size;
137 #ifdef OPTi93X
138 unsigned long mc_indir_index;
139 struct resource *res_mc_indir;
140 #endif /* OPTi93X */
141 struct snd_wss *codec;
142 unsigned long pwd_reg;
143
144 spinlock_t lock;
145
146 long wss_base;
147 int irq;
148 };
149
150 static int snd_opti9xx_pnp_is_probed;
151
152 #ifdef CONFIG_PNP
153
154 static const struct pnp_card_device_id snd_opti9xx_pnpids[] = {
155 #ifndef OPTi93X
156 /* OPTi 82C924 */
157 { .id = "OPT0924",
158 .devs = { { "OPT0000" }, { "OPT0002" }, { "OPT0005" } },
159 .driver_data = 0x0924 },
160 /* OPTi 82C925 */
161 { .id = "OPT0925",
162 .devs = { { "OPT9250" }, { "OPT0002" }, { "OPT0005" } },
163 .driver_data = 0x0925 },
164 #else
165 /* OPTi 82C931/3 */
166 { .id = "OPT0931", .devs = { { "OPT9310" }, { "OPT0002" } },
167 .driver_data = 0x0931 },
168 #endif /* OPTi93X */
169 { .id = "" }
170 };
171
172 MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids);
173
174 #endif /* CONFIG_PNP */
175
176 #define DEV_NAME KBUILD_MODNAME
177
178 static char * snd_opti9xx_names[] = {
179 "unknown",
180 "82C928", "82C929",
181 "82C924", "82C925",
182 "82C930", "82C931", "82C933"
183 };
184
snd_opti9xx_init(struct snd_opti9xx * chip,unsigned short hardware)185 static int snd_opti9xx_init(struct snd_opti9xx *chip,
186 unsigned short hardware)
187 {
188 static int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
189
190 chip->hardware = hardware;
191 strcpy(chip->name, snd_opti9xx_names[hardware]);
192
193 spin_lock_init(&chip->lock);
194
195 chip->irq = -1;
196
197 #ifndef OPTi93X
198 #ifdef CONFIG_PNP
199 if (isapnp && chip->mc_base)
200 /* PnP resource gives the least 10 bits */
201 chip->mc_base |= 0xc00;
202 else
203 #endif /* CONFIG_PNP */
204 {
205 chip->mc_base = 0xf8c;
206 chip->mc_base_size = opti9xx_mc_size[hardware];
207 }
208 #else
209 chip->mc_base_size = opti9xx_mc_size[hardware];
210 #endif
211
212 switch (hardware) {
213 #ifndef OPTi93X
214 case OPTi9XX_HW_82C928:
215 case OPTi9XX_HW_82C929:
216 chip->password = (hardware == OPTi9XX_HW_82C928) ? 0xe2 : 0xe3;
217 chip->pwd_reg = 3;
218 break;
219
220 case OPTi9XX_HW_82C924:
221 case OPTi9XX_HW_82C925:
222 chip->password = 0xe5;
223 chip->pwd_reg = 3;
224 break;
225 #else /* OPTi93X */
226
227 case OPTi9XX_HW_82C930:
228 case OPTi9XX_HW_82C931:
229 case OPTi9XX_HW_82C933:
230 chip->mc_base = (hardware == OPTi9XX_HW_82C930) ? 0xf8f : 0xf8d;
231 if (!chip->mc_indir_index)
232 chip->mc_indir_index = 0xe0e;
233 chip->password = 0xe4;
234 chip->pwd_reg = 0;
235 break;
236 #endif /* OPTi93X */
237
238 default:
239 snd_printk(KERN_ERR "chip %d not supported\n", hardware);
240 return -ENODEV;
241 }
242 return 0;
243 }
244
snd_opti9xx_read(struct snd_opti9xx * chip,unsigned char reg)245 static unsigned char snd_opti9xx_read(struct snd_opti9xx *chip,
246 unsigned char reg)
247 {
248 unsigned long flags;
249 unsigned char retval = 0xff;
250
251 spin_lock_irqsave(&chip->lock, flags);
252 outb(chip->password, chip->mc_base + chip->pwd_reg);
253
254 switch (chip->hardware) {
255 #ifndef OPTi93X
256 case OPTi9XX_HW_82C924:
257 case OPTi9XX_HW_82C925:
258 if (reg > 7) {
259 outb(reg, chip->mc_base + 8);
260 outb(chip->password, chip->mc_base + chip->pwd_reg);
261 retval = inb(chip->mc_base + 9);
262 break;
263 }
264 /* Fall through */
265
266 case OPTi9XX_HW_82C928:
267 case OPTi9XX_HW_82C929:
268 retval = inb(chip->mc_base + reg);
269 break;
270 #else /* OPTi93X */
271
272 case OPTi9XX_HW_82C930:
273 case OPTi9XX_HW_82C931:
274 case OPTi9XX_HW_82C933:
275 outb(reg, chip->mc_indir_index);
276 outb(chip->password, chip->mc_base + chip->pwd_reg);
277 retval = inb(chip->mc_indir_index + 1);
278 break;
279 #endif /* OPTi93X */
280
281 default:
282 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
283 }
284
285 spin_unlock_irqrestore(&chip->lock, flags);
286 return retval;
287 }
288
snd_opti9xx_write(struct snd_opti9xx * chip,unsigned char reg,unsigned char value)289 static void snd_opti9xx_write(struct snd_opti9xx *chip, unsigned char reg,
290 unsigned char value)
291 {
292 unsigned long flags;
293
294 spin_lock_irqsave(&chip->lock, flags);
295 outb(chip->password, chip->mc_base + chip->pwd_reg);
296
297 switch (chip->hardware) {
298 #ifndef OPTi93X
299 case OPTi9XX_HW_82C924:
300 case OPTi9XX_HW_82C925:
301 if (reg > 7) {
302 outb(reg, chip->mc_base + 8);
303 outb(chip->password, chip->mc_base + chip->pwd_reg);
304 outb(value, chip->mc_base + 9);
305 break;
306 }
307 /* Fall through */
308
309 case OPTi9XX_HW_82C928:
310 case OPTi9XX_HW_82C929:
311 outb(value, chip->mc_base + reg);
312 break;
313 #else /* OPTi93X */
314
315 case OPTi9XX_HW_82C930:
316 case OPTi9XX_HW_82C931:
317 case OPTi9XX_HW_82C933:
318 outb(reg, chip->mc_indir_index);
319 outb(chip->password, chip->mc_base + chip->pwd_reg);
320 outb(value, chip->mc_indir_index + 1);
321 break;
322 #endif /* OPTi93X */
323
324 default:
325 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
326 }
327
328 spin_unlock_irqrestore(&chip->lock, flags);
329 }
330
331
snd_opti9xx_write_mask(struct snd_opti9xx * chip,unsigned char reg,unsigned char value,unsigned char mask)332 static inline void snd_opti9xx_write_mask(struct snd_opti9xx *chip,
333 unsigned char reg, unsigned char value, unsigned char mask)
334 {
335 unsigned char oldval = snd_opti9xx_read(chip, reg);
336
337 snd_opti9xx_write(chip, reg, (oldval & ~mask) | (value & mask));
338 }
339
snd_opti9xx_configure(struct snd_opti9xx * chip,long port,int irq,int dma1,int dma2,long mpu_port,int mpu_irq)340 static int snd_opti9xx_configure(struct snd_opti9xx *chip,
341 long port,
342 int irq, int dma1, int dma2,
343 long mpu_port, int mpu_irq)
344 {
345 unsigned char wss_base_bits;
346 unsigned char irq_bits;
347 unsigned char dma_bits;
348 unsigned char mpu_port_bits = 0;
349 unsigned char mpu_irq_bits;
350
351 switch (chip->hardware) {
352 #ifndef OPTi93X
353 case OPTi9XX_HW_82C924:
354 /* opti 929 mode (?), OPL3 clock output, audio enable */
355 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc);
356 /* enable wave audio */
357 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
358 /* Fall through */
359
360 case OPTi9XX_HW_82C925:
361 /* enable WSS mode */
362 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
363 /* OPL3 FM synthesis */
364 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
365 /* disable Sound Blaster IRQ and DMA */
366 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
367 #ifdef CS4231
368 /* cs4231/4248 fix enabled */
369 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
370 #else
371 /* cs4231/4248 fix disabled */
372 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
373 #endif /* CS4231 */
374 break;
375
376 case OPTi9XX_HW_82C928:
377 case OPTi9XX_HW_82C929:
378 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
379 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
380 /*
381 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae);
382 */
383 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
384 #ifdef CS4231
385 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
386 #else
387 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
388 #endif /* CS4231 */
389 break;
390
391 #else /* OPTi93X */
392 case OPTi9XX_HW_82C931:
393 /* disable 3D sound (set GPIO1 as output, low) */
394 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(20), 0x04, 0x0c);
395 case OPTi9XX_HW_82C933: /* FALL THROUGH */
396 /*
397 * The BTC 1817DW has QS1000 wavetable which is connected
398 * to the serial digital input of the OPTI931.
399 */
400 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(21), 0x82, 0xff);
401 /*
402 * This bit sets OPTI931 to automaticaly select FM
403 * or digital input signal.
404 */
405 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(26), 0x01, 0x01);
406 case OPTi9XX_HW_82C930: /* FALL THROUGH */
407 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03);
408 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff);
409 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 |
410 (chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04),
411 0x34);
412 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf);
413 break;
414 #endif /* OPTi93X */
415
416 default:
417 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
418 return -EINVAL;
419 }
420
421 /* PnP resource says it decodes only 10 bits of address */
422 switch (port & 0x3ff) {
423 case 0x130:
424 chip->wss_base = 0x530;
425 wss_base_bits = 0x00;
426 break;
427 case 0x204:
428 chip->wss_base = 0x604;
429 wss_base_bits = 0x03;
430 break;
431 case 0x280:
432 chip->wss_base = 0xe80;
433 wss_base_bits = 0x01;
434 break;
435 case 0x340:
436 chip->wss_base = 0xf40;
437 wss_base_bits = 0x02;
438 break;
439 default:
440 snd_printk(KERN_WARNING "WSS port 0x%lx not valid\n", port);
441 goto __skip_base;
442 }
443 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
444
445 __skip_base:
446 switch (irq) {
447 //#ifdef OPTi93X
448 case 5:
449 irq_bits = 0x05;
450 break;
451 //#endif /* OPTi93X */
452 case 7:
453 irq_bits = 0x01;
454 break;
455 case 9:
456 irq_bits = 0x02;
457 break;
458 case 10:
459 irq_bits = 0x03;
460 break;
461 case 11:
462 irq_bits = 0x04;
463 break;
464 default:
465 snd_printk(KERN_WARNING "WSS irq # %d not valid\n", irq);
466 goto __skip_resources;
467 }
468
469 switch (dma1) {
470 case 0:
471 dma_bits = 0x01;
472 break;
473 case 1:
474 dma_bits = 0x02;
475 break;
476 case 3:
477 dma_bits = 0x03;
478 break;
479 default:
480 snd_printk(KERN_WARNING "WSS dma1 # %d not valid\n", dma1);
481 goto __skip_resources;
482 }
483
484 #if defined(CS4231) || defined(OPTi93X)
485 if (dma1 == dma2) {
486 snd_printk(KERN_ERR "don't want to share dmas\n");
487 return -EBUSY;
488 }
489
490 switch (dma2) {
491 case 0:
492 case 1:
493 break;
494 default:
495 snd_printk(KERN_WARNING "WSS dma2 # %d not valid\n", dma2);
496 goto __skip_resources;
497 }
498 dma_bits |= 0x04;
499 #endif /* CS4231 || OPTi93X */
500
501 #ifndef OPTi93X
502 outb(irq_bits << 3 | dma_bits, chip->wss_base);
503 #else /* OPTi93X */
504 snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits));
505 #endif /* OPTi93X */
506
507 __skip_resources:
508 if (chip->hardware > OPTi9XX_HW_82C928) {
509 switch (mpu_port) {
510 case 0:
511 case -1:
512 break;
513 case 0x300:
514 mpu_port_bits = 0x03;
515 break;
516 case 0x310:
517 mpu_port_bits = 0x02;
518 break;
519 case 0x320:
520 mpu_port_bits = 0x01;
521 break;
522 case 0x330:
523 mpu_port_bits = 0x00;
524 break;
525 default:
526 snd_printk(KERN_WARNING
527 "MPU-401 port 0x%lx not valid\n", mpu_port);
528 goto __skip_mpu;
529 }
530
531 switch (mpu_irq) {
532 case 5:
533 mpu_irq_bits = 0x02;
534 break;
535 case 7:
536 mpu_irq_bits = 0x03;
537 break;
538 case 9:
539 mpu_irq_bits = 0x00;
540 break;
541 case 10:
542 mpu_irq_bits = 0x01;
543 break;
544 default:
545 snd_printk(KERN_WARNING "MPU-401 irq # %d not valid\n",
546 mpu_irq);
547 goto __skip_mpu;
548 }
549
550 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6),
551 (mpu_port <= 0) ? 0x00 :
552 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
553 0xf8);
554 }
555 __skip_mpu:
556
557 return 0;
558 }
559
560 #ifdef OPTi93X
561
562 static const DECLARE_TLV_DB_SCALE(db_scale_5bit_3db_step, -9300, 300, 0);
563 static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0);
564 static const DECLARE_TLV_DB_SCALE(db_scale_4bit_12db_max, -3300, 300, 0);
565
566 static struct snd_kcontrol_new snd_opti93x_controls[] = {
567 WSS_DOUBLE("Master Playback Switch", 0,
568 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
569 WSS_DOUBLE_TLV("Master Playback Volume", 0,
570 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1,
571 db_scale_5bit_3db_step),
572 WSS_DOUBLE_TLV("PCM Playback Volume", 0,
573 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1,
574 db_scale_5bit),
575 WSS_DOUBLE_TLV("FM Playback Volume", 0,
576 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1,
577 db_scale_4bit_12db_max),
578 WSS_DOUBLE("Line Playback Switch", 0,
579 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
580 WSS_DOUBLE_TLV("Line Playback Volume", 0,
581 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1,
582 db_scale_4bit_12db_max),
583 WSS_DOUBLE("Mic Playback Switch", 0,
584 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
585 WSS_DOUBLE_TLV("Mic Playback Volume", 0,
586 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1,
587 db_scale_4bit_12db_max),
588 WSS_DOUBLE_TLV("CD Playback Volume", 0,
589 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1,
590 db_scale_4bit_12db_max),
591 WSS_DOUBLE("Aux Playback Switch", 0,
592 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
593 WSS_DOUBLE_TLV("Aux Playback Volume", 0,
594 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1,
595 db_scale_4bit_12db_max),
596 };
597
snd_opti93x_mixer(struct snd_wss * chip)598 static int snd_opti93x_mixer(struct snd_wss *chip)
599 {
600 struct snd_card *card;
601 unsigned int idx;
602 struct snd_ctl_elem_id id1, id2;
603 int err;
604
605 if (snd_BUG_ON(!chip || !chip->pcm))
606 return -EINVAL;
607
608 card = chip->card;
609
610 strcpy(card->mixername, chip->pcm->name);
611
612 memset(&id1, 0, sizeof(id1));
613 memset(&id2, 0, sizeof(id2));
614 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
615 /* reassign AUX0 switch to CD */
616 strcpy(id1.name, "Aux Playback Switch");
617 strcpy(id2.name, "CD Playback Switch");
618 err = snd_ctl_rename_id(card, &id1, &id2);
619 if (err < 0) {
620 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
621 return err;
622 }
623 /* reassign AUX1 switch to FM */
624 strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
625 strcpy(id2.name, "FM Playback Switch");
626 err = snd_ctl_rename_id(card, &id1, &id2);
627 if (err < 0) {
628 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
629 return err;
630 }
631 /* remove AUX1 volume */
632 strcpy(id1.name, "Aux Playback Volume"); id1.index = 1;
633 snd_ctl_remove_id(card, &id1);
634
635 /* Replace WSS volume controls with OPTi93x volume controls */
636 id1.index = 0;
637 for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
638 strcpy(id1.name, snd_opti93x_controls[idx].name);
639 snd_ctl_remove_id(card, &id1);
640
641 err = snd_ctl_add(card,
642 snd_ctl_new1(&snd_opti93x_controls[idx], chip));
643 if (err < 0)
644 return err;
645 }
646 return 0;
647 }
648
snd_opti93x_interrupt(int irq,void * dev_id)649 static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id)
650 {
651 struct snd_opti9xx *chip = dev_id;
652 struct snd_wss *codec = chip->codec;
653 unsigned char status;
654
655 if (!codec)
656 return IRQ_HANDLED;
657
658 status = snd_opti9xx_read(chip, OPTi9XX_MC_REG(11));
659 if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
660 snd_pcm_period_elapsed(codec->playback_substream);
661 if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
662 snd_wss_overrange(codec);
663 snd_pcm_period_elapsed(codec->capture_substream);
664 }
665 outb(0x00, OPTi93X_PORT(codec, STATUS));
666 return IRQ_HANDLED;
667 }
668
669 #endif /* OPTi93X */
670
snd_opti9xx_read_check(struct snd_opti9xx * chip)671 static int snd_opti9xx_read_check(struct snd_opti9xx *chip)
672 {
673 unsigned char value;
674 #ifdef OPTi93X
675 unsigned long flags;
676 #endif
677
678 chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
679 "OPTi9xx MC");
680 if (chip->res_mc_base == NULL)
681 return -EBUSY;
682 #ifndef OPTi93X
683 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
684 if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1)))
685 if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
686 return 0;
687 #else /* OPTi93X */
688 chip->res_mc_indir = request_region(chip->mc_indir_index, 2,
689 "OPTi93x MC");
690 if (chip->res_mc_indir == NULL)
691 return -EBUSY;
692
693 spin_lock_irqsave(&chip->lock, flags);
694 outb(chip->password, chip->mc_base + chip->pwd_reg);
695 outb(((chip->mc_indir_index & 0x1f0) >> 4), chip->mc_base);
696 spin_unlock_irqrestore(&chip->lock, flags);
697
698 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
699 snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
700 if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
701 return 0;
702
703 release_and_free_resource(chip->res_mc_indir);
704 chip->res_mc_indir = NULL;
705 #endif /* OPTi93X */
706 release_and_free_resource(chip->res_mc_base);
707 chip->res_mc_base = NULL;
708
709 return -ENODEV;
710 }
711
snd_card_opti9xx_detect(struct snd_card * card,struct snd_opti9xx * chip)712 static int snd_card_opti9xx_detect(struct snd_card *card,
713 struct snd_opti9xx *chip)
714 {
715 int i, err;
716
717 #ifndef OPTi93X
718 for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
719 #else
720 for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
721 #endif
722 err = snd_opti9xx_init(chip, i);
723 if (err < 0)
724 return err;
725
726 err = snd_opti9xx_read_check(chip);
727 if (err == 0)
728 return 1;
729 #ifdef OPTi93X
730 chip->mc_indir_index = 0;
731 #endif
732 }
733 return -ENODEV;
734 }
735
736 #ifdef CONFIG_PNP
737 static int snd_card_opti9xx_pnp(struct snd_opti9xx *chip,
738 struct pnp_card_link *card,
739 const struct pnp_card_device_id *pid)
740 {
741 struct pnp_dev *pdev;
742 int err;
743 struct pnp_dev *devmpu;
744 #ifndef OPTi93X
745 struct pnp_dev *devmc;
746 #endif
747
748 pdev = pnp_request_card_device(card, pid->devs[0].id, NULL);
749 if (pdev == NULL)
750 return -EBUSY;
751
752 err = pnp_activate_dev(pdev);
753 if (err < 0) {
754 snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
755 return err;
756 }
757
758 #ifdef OPTi93X
759 port = pnp_port_start(pdev, 0) - 4;
760 fm_port = pnp_port_start(pdev, 1) + 8;
761 /* adjust mc_indir_index - some cards report it at 0xe?d,
762 other at 0xe?c but it really is always at 0xe?e */
763 chip->mc_indir_index = (pnp_port_start(pdev, 3) & ~0xf) | 0xe;
764 #else
765 devmc = pnp_request_card_device(card, pid->devs[2].id, NULL);
766 if (devmc == NULL)
767 return -EBUSY;
768
769 err = pnp_activate_dev(devmc);
770 if (err < 0) {
771 snd_printk(KERN_ERR "MC pnp configure failure: %d\n", err);
772 return err;
773 }
774
775 port = pnp_port_start(pdev, 1);
776 fm_port = pnp_port_start(pdev, 2) + 8;
777 /*
778 * The MC(0) is never accessed and card does not
779 * include it in the PnP resource range. OPTI93x include it.
780 */
781 chip->mc_base = pnp_port_start(devmc, 0) - 1;
782 chip->mc_base_size = pnp_port_len(devmc, 0) + 1;
783 #endif /* OPTi93X */
784 irq = pnp_irq(pdev, 0);
785 dma1 = pnp_dma(pdev, 0);
786 #if defined(CS4231) || defined(OPTi93X)
787 dma2 = pnp_dma(pdev, 1);
788 #endif /* CS4231 || OPTi93X */
789
790 devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
791
792 if (devmpu && mpu_port > 0) {
793 err = pnp_activate_dev(devmpu);
794 if (err < 0) {
795 snd_printk(KERN_ERR "MPU401 pnp configure failure\n");
796 mpu_port = -1;
797 } else {
798 mpu_port = pnp_port_start(devmpu, 0);
799 mpu_irq = pnp_irq(devmpu, 0);
800 }
801 }
802 return pid->driver_data;
803 }
804 #endif /* CONFIG_PNP */
805
806 static void snd_card_opti9xx_free(struct snd_card *card)
807 {
808 struct snd_opti9xx *chip = card->private_data;
809
810 if (chip) {
811 #ifdef OPTi93X
812 if (chip->irq > 0) {
813 disable_irq(chip->irq);
814 free_irq(chip->irq, chip);
815 }
816 release_and_free_resource(chip->res_mc_indir);
817 #endif
818 release_and_free_resource(chip->res_mc_base);
819 }
820 }
821
822 static int snd_opti9xx_probe(struct snd_card *card)
823 {
824 static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
825 int error;
826 int xdma2;
827 struct snd_opti9xx *chip = card->private_data;
828 struct snd_wss *codec;
829 struct snd_rawmidi *rmidi;
830 struct snd_hwdep *synth;
831
832 #if defined(CS4231) || defined(OPTi93X)
833 xdma2 = dma2;
834 #else
835 xdma2 = -1;
836 #endif
837
838 if (port == SNDRV_AUTO_PORT) {
839 port = snd_legacy_find_free_ioport(possible_ports, 4);
840 if (port < 0) {
841 snd_printk(KERN_ERR "unable to find a free WSS port\n");
842 return -EBUSY;
843 }
844 }
845 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
846 mpu_port, mpu_irq);
847 if (error)
848 return error;
849
850 error = snd_wss_create(card, chip->wss_base + 4, -1, irq, dma1, xdma2,
851 #ifdef OPTi93X
852 WSS_HW_OPTI93X, WSS_HWSHARE_IRQ,
853 #else
854 WSS_HW_DETECT, 0,
855 #endif
856 &codec);
857 if (error < 0)
858 return error;
859 chip->codec = codec;
860 error = snd_wss_pcm(codec, 0);
861 if (error < 0)
862 return error;
863 error = snd_wss_mixer(codec);
864 if (error < 0)
865 return error;
866 #ifdef OPTi93X
867 error = snd_opti93x_mixer(codec);
868 if (error < 0)
869 return error;
870 #endif
871 #ifdef CS4231
872 error = snd_wss_timer(codec, 0);
873 if (error < 0)
874 return error;
875 #endif
876 #ifdef OPTi93X
877 error = request_irq(irq, snd_opti93x_interrupt,
878 0, DEV_NAME" - WSS", chip);
879 if (error < 0) {
880 snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", irq);
881 return error;
882 }
883 #endif
884 chip->irq = irq;
885 strcpy(card->driver, chip->name);
886 sprintf(card->shortname, "OPTi %s", card->driver);
887 #if defined(CS4231) || defined(OPTi93X)
888 snprintf(card->longname, sizeof(card->longname),
889 "%s, %s at 0x%lx, irq %d, dma %d&%d",
890 card->shortname, codec->pcm->name,
891 chip->wss_base + 4, irq, dma1, xdma2);
892 #else
893 snprintf(card->longname, sizeof(card->longname),
894 "%s, %s at 0x%lx, irq %d, dma %d",
895 card->shortname, codec->pcm->name, chip->wss_base + 4, irq,
896 dma1);
897 #endif /* CS4231 || OPTi93X */
898
899 if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
900 rmidi = NULL;
901 else {
902 error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
903 mpu_port, 0, mpu_irq, &rmidi);
904 if (error)
905 snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
906 mpu_port);
907 }
908
909 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
910 struct snd_opl3 *opl3 = NULL;
911 #ifndef OPTi93X
912 if (chip->hardware == OPTi9XX_HW_82C928 ||
913 chip->hardware == OPTi9XX_HW_82C929 ||
914 chip->hardware == OPTi9XX_HW_82C924) {
915 struct snd_opl4 *opl4;
916 /* assume we have an OPL4 */
917 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
918 0x20, 0x20);
919 if (snd_opl4_create(card, fm_port, fm_port - 8,
920 2, &opl3, &opl4) < 0) {
921 /* no luck, use OPL3 instead */
922 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
923 0x00, 0x20);
924 }
925 }
926 #endif /* !OPTi93X */
927 if (!opl3 && snd_opl3_create(card, fm_port, fm_port + 2,
928 OPL3_HW_AUTO, 0, &opl3) < 0) {
929 snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n",
930 fm_port, fm_port + 4 - 1);
931 }
932 if (opl3) {
933 error = snd_opl3_hwdep_new(opl3, 0, 1, &synth);
934 if (error < 0)
935 return error;
936 }
937 }
938
939 return snd_card_register(card);
940 }
941
942 static int snd_opti9xx_card_new(struct device *pdev, struct snd_card **cardp)
943 {
944 struct snd_card *card;
945 int err;
946
947 err = snd_card_new(pdev, index, id, THIS_MODULE,
948 sizeof(struct snd_opti9xx), &card);
949 if (err < 0)
950 return err;
951 card->private_free = snd_card_opti9xx_free;
952 *cardp = card;
953 return 0;
954 }
955
956 static int snd_opti9xx_isa_match(struct device *devptr,
957 unsigned int dev)
958 {
959 #ifdef CONFIG_PNP
960 if (snd_opti9xx_pnp_is_probed)
961 return 0;
962 if (isapnp)
963 return 0;
964 #endif
965 return 1;
966 }
967
968 static int snd_opti9xx_isa_probe(struct device *devptr,
969 unsigned int dev)
970 {
971 struct snd_card *card;
972 int error;
973 static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
974 #ifdef OPTi93X
975 static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
976 #else
977 static int possible_irqs[] = {9, 10, 11, 7, -1};
978 #endif /* OPTi93X */
979 static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
980 static int possible_dma1s[] = {3, 1, 0, -1};
981 #if defined(CS4231) || defined(OPTi93X)
982 static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
983 #endif /* CS4231 || OPTi93X */
984
985 if (mpu_port == SNDRV_AUTO_PORT) {
986 if ((mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
987 snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
988 return -EBUSY;
989 }
990 }
991 if (irq == SNDRV_AUTO_IRQ) {
992 if ((irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
993 snd_printk(KERN_ERR "unable to find a free IRQ\n");
994 return -EBUSY;
995 }
996 }
997 if (mpu_irq == SNDRV_AUTO_IRQ) {
998 if ((mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
999 snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
1000 return -EBUSY;
1001 }
1002 }
1003 if (dma1 == SNDRV_AUTO_DMA) {
1004 if ((dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
1005 snd_printk(KERN_ERR "unable to find a free DMA1\n");
1006 return -EBUSY;
1007 }
1008 }
1009 #if defined(CS4231) || defined(OPTi93X)
1010 if (dma2 == SNDRV_AUTO_DMA) {
1011 if ((dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4])) < 0) {
1012 snd_printk(KERN_ERR "unable to find a free DMA2\n");
1013 return -EBUSY;
1014 }
1015 }
1016 #endif
1017
1018 error = snd_opti9xx_card_new(devptr, &card);
1019 if (error < 0)
1020 return error;
1021
1022 if ((error = snd_card_opti9xx_detect(card, card->private_data)) < 0) {
1023 snd_card_free(card);
1024 return error;
1025 }
1026 if ((error = snd_opti9xx_probe(card)) < 0) {
1027 snd_card_free(card);
1028 return error;
1029 }
1030 dev_set_drvdata(devptr, card);
1031 return 0;
1032 }
1033
1034 static int snd_opti9xx_isa_remove(struct device *devptr,
1035 unsigned int dev)
1036 {
1037 snd_card_free(dev_get_drvdata(devptr));
1038 return 0;
1039 }
1040
1041 #ifdef CONFIG_PM
1042 static int snd_opti9xx_suspend(struct snd_card *card)
1043 {
1044 struct snd_opti9xx *chip = card->private_data;
1045
1046 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1047 chip->codec->suspend(chip->codec);
1048 return 0;
1049 }
1050
1051 static int snd_opti9xx_resume(struct snd_card *card)
1052 {
1053 struct snd_opti9xx *chip = card->private_data;
1054 int error, xdma2;
1055 #if defined(CS4231) || defined(OPTi93X)
1056 xdma2 = dma2;
1057 #else
1058 xdma2 = -1;
1059 #endif
1060
1061 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
1062 mpu_port, mpu_irq);
1063 if (error)
1064 return error;
1065 chip->codec->resume(chip->codec);
1066 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1067 return 0;
1068 }
1069
1070 static int snd_opti9xx_isa_suspend(struct device *dev, unsigned int n,
1071 pm_message_t state)
1072 {
1073 return snd_opti9xx_suspend(dev_get_drvdata(dev));
1074 }
1075
1076 static int snd_opti9xx_isa_resume(struct device *dev, unsigned int n)
1077 {
1078 return snd_opti9xx_resume(dev_get_drvdata(dev));
1079 }
1080 #endif
1081
1082 static struct isa_driver snd_opti9xx_driver = {
1083 .match = snd_opti9xx_isa_match,
1084 .probe = snd_opti9xx_isa_probe,
1085 .remove = snd_opti9xx_isa_remove,
1086 #ifdef CONFIG_PM
1087 .suspend = snd_opti9xx_isa_suspend,
1088 .resume = snd_opti9xx_isa_resume,
1089 #endif
1090 .driver = {
1091 .name = DEV_NAME
1092 },
1093 };
1094
1095 #ifdef CONFIG_PNP
1096 static int snd_opti9xx_pnp_probe(struct pnp_card_link *pcard,
1097 const struct pnp_card_device_id *pid)
1098 {
1099 struct snd_card *card;
1100 int error, hw;
1101 struct snd_opti9xx *chip;
1102
1103 if (snd_opti9xx_pnp_is_probed)
1104 return -EBUSY;
1105 if (! isapnp)
1106 return -ENODEV;
1107 error = snd_opti9xx_card_new(&pcard->card->dev, &card);
1108 if (error < 0)
1109 return error;
1110 chip = card->private_data;
1111
1112 hw = snd_card_opti9xx_pnp(chip, pcard, pid);
1113 switch (hw) {
1114 case 0x0924:
1115 hw = OPTi9XX_HW_82C924;
1116 break;
1117 case 0x0925:
1118 hw = OPTi9XX_HW_82C925;
1119 break;
1120 case 0x0931:
1121 hw = OPTi9XX_HW_82C931;
1122 break;
1123 default:
1124 snd_card_free(card);
1125 return -ENODEV;
1126 }
1127
1128 if ((error = snd_opti9xx_init(chip, hw))) {
1129 snd_card_free(card);
1130 return error;
1131 }
1132 error = snd_opti9xx_read_check(chip);
1133 if (error) {
1134 snd_printk(KERN_ERR "OPTI chip not found\n");
1135 snd_card_free(card);
1136 return error;
1137 }
1138 if ((error = snd_opti9xx_probe(card)) < 0) {
1139 snd_card_free(card);
1140 return error;
1141 }
1142 pnp_set_card_drvdata(pcard, card);
1143 snd_opti9xx_pnp_is_probed = 1;
1144 return 0;
1145 }
1146
1147 static void snd_opti9xx_pnp_remove(struct pnp_card_link *pcard)
1148 {
1149 snd_card_free(pnp_get_card_drvdata(pcard));
1150 pnp_set_card_drvdata(pcard, NULL);
1151 snd_opti9xx_pnp_is_probed = 0;
1152 }
1153
1154 #ifdef CONFIG_PM
1155 static int snd_opti9xx_pnp_suspend(struct pnp_card_link *pcard,
1156 pm_message_t state)
1157 {
1158 return snd_opti9xx_suspend(pnp_get_card_drvdata(pcard));
1159 }
1160
1161 static int snd_opti9xx_pnp_resume(struct pnp_card_link *pcard)
1162 {
1163 return snd_opti9xx_resume(pnp_get_card_drvdata(pcard));
1164 }
1165 #endif
1166
1167 static struct pnp_card_driver opti9xx_pnpc_driver = {
1168 .flags = PNP_DRIVER_RES_DISABLE,
1169 .name = DEV_NAME,
1170 .id_table = snd_opti9xx_pnpids,
1171 .probe = snd_opti9xx_pnp_probe,
1172 .remove = snd_opti9xx_pnp_remove,
1173 #ifdef CONFIG_PM
1174 .suspend = snd_opti9xx_pnp_suspend,
1175 .resume = snd_opti9xx_pnp_resume,
1176 #endif
1177 };
1178 #endif
1179
1180 #ifdef OPTi93X
1181 #define CHIP_NAME "82C93x"
1182 #else
1183 #define CHIP_NAME "82C92x"
1184 #endif
1185
1186 static int __init alsa_card_opti9xx_init(void)
1187 {
1188 #ifdef CONFIG_PNP
1189 pnp_register_card_driver(&opti9xx_pnpc_driver);
1190 if (snd_opti9xx_pnp_is_probed)
1191 return 0;
1192 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
1193 #endif
1194 return isa_register_driver(&snd_opti9xx_driver, 1);
1195 }
1196
1197 static void __exit alsa_card_opti9xx_exit(void)
1198 {
1199 if (!snd_opti9xx_pnp_is_probed) {
1200 isa_unregister_driver(&snd_opti9xx_driver);
1201 return;
1202 }
1203 #ifdef CONFIG_PNP
1204 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
1205 #endif
1206 }
1207
1208 module_init(alsa_card_opti9xx_init)
1209 module_exit(alsa_card_opti9xx_exit)
1210