Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / sound / isa / opti9xx / opti92x-ad1848.c
blobf8fbe22515c99b627885fa3c05da815514777f4e
1 /*
2 card-opti92x-ad1848.c - driver for OPTi 82c92x based soundcards.
3 Copyright (C) 1998-2000 by Massimo Piccioni <dafastidio@libero.it>
5 Part of this code was developed at the Italian Ministry of Air Defence,
6 Sixth Division (oh, che pace ...), Rome.
8 Thanks to Maria Grazia Pollarini, Salvatore Vassallo.
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.
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.
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
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 <asm/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_IRQ
43 #define SNDRV_LEGACY_FIND_FREE_DMA
44 #include <sound/initval.h>
46 MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>");
47 MODULE_LICENSE("GPL");
48 #ifdef OPTi93X
49 MODULE_DESCRIPTION("OPTi93X");
50 MODULE_SUPPORTED_DEVICE("{{OPTi,82C931/3}}");
51 #else /* OPTi93X */
52 #ifdef CS4231
53 MODULE_DESCRIPTION("OPTi92X - CS4231");
54 MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (CS4231)},"
55 "{OPTi,82C925 (CS4231)}}");
56 #else /* CS4231 */
57 MODULE_DESCRIPTION("OPTi92X - AD1848");
58 MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (AD1848)},"
59 "{OPTi,82C925 (AD1848)},"
60 "{OAK,Mozart}}");
61 #endif /* CS4231 */
62 #endif /* OPTi93X */
64 static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
65 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
66 //static bool enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */
67 #ifdef CONFIG_PNP
68 static bool isapnp = true; /* Enable ISA PnP detection */
69 #endif
70 static long port = SNDRV_DEFAULT_PORT1; /* 0x530,0xe80,0xf40,0x604 */
71 static long mpu_port = SNDRV_DEFAULT_PORT1; /* 0x300,0x310,0x320,0x330 */
72 static long fm_port = SNDRV_DEFAULT_PORT1; /* 0x388 */
73 static int irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10,11 */
74 static int mpu_irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10 */
75 static int dma1 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
76 #if defined(CS4231) || defined(OPTi93X)
77 static int dma2 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
78 #endif /* CS4231 || OPTi93X */
80 module_param(index, int, 0444);
81 MODULE_PARM_DESC(index, "Index value for opti9xx based soundcard.");
82 module_param(id, charp, 0444);
83 MODULE_PARM_DESC(id, "ID string for opti9xx based soundcard.");
84 //module_param(enable, bool, 0444);
85 //MODULE_PARM_DESC(enable, "Enable opti9xx soundcard.");
86 #ifdef CONFIG_PNP
87 module_param(isapnp, bool, 0444);
88 MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard.");
89 #endif
90 module_param(port, long, 0444);
91 MODULE_PARM_DESC(port, "WSS port # for opti9xx driver.");
92 module_param(mpu_port, long, 0444);
93 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for opti9xx driver.");
94 module_param(fm_port, long, 0444);
95 MODULE_PARM_DESC(fm_port, "FM port # for opti9xx driver.");
96 module_param(irq, int, 0444);
97 MODULE_PARM_DESC(irq, "WSS irq # for opti9xx driver.");
98 module_param(mpu_irq, int, 0444);
99 MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for opti9xx driver.");
100 module_param(dma1, int, 0444);
101 MODULE_PARM_DESC(dma1, "1st dma # for opti9xx driver.");
102 #if defined(CS4231) || defined(OPTi93X)
103 module_param(dma2, int, 0444);
104 MODULE_PARM_DESC(dma2, "2nd dma # for opti9xx driver.");
105 #endif /* CS4231 || OPTi93X */
107 #define OPTi9XX_HW_82C928 1
108 #define OPTi9XX_HW_82C929 2
109 #define OPTi9XX_HW_82C924 3
110 #define OPTi9XX_HW_82C925 4
111 #define OPTi9XX_HW_82C930 5
112 #define OPTi9XX_HW_82C931 6
113 #define OPTi9XX_HW_82C933 7
114 #define OPTi9XX_HW_LAST OPTi9XX_HW_82C933
116 #define OPTi9XX_MC_REG(n) n
118 #ifdef OPTi93X
120 #define OPTi93X_STATUS 0x02
121 #define OPTi93X_PORT(chip, r) ((chip)->port + OPTi93X_##r)
123 #define OPTi93X_IRQ_PLAYBACK 0x04
124 #define OPTi93X_IRQ_CAPTURE 0x08
126 #endif /* OPTi93X */
128 struct snd_opti9xx {
129 unsigned short hardware;
130 unsigned char password;
131 char name[7];
133 unsigned long mc_base;
134 struct resource *res_mc_base;
135 unsigned long mc_base_size;
136 #ifdef OPTi93X
137 unsigned long mc_indir_index;
138 struct resource *res_mc_indir;
139 #endif /* OPTi93X */
140 struct snd_wss *codec;
141 unsigned long pwd_reg;
143 spinlock_t lock;
145 long wss_base;
146 int irq;
149 static int snd_opti9xx_pnp_is_probed;
151 #ifdef CONFIG_PNP
153 static struct pnp_card_device_id snd_opti9xx_pnpids[] = {
154 #ifndef OPTi93X
155 /* OPTi 82C924 */
156 { .id = "OPT0924",
157 .devs = { { "OPT0000" }, { "OPT0002" }, { "OPT0005" } },
158 .driver_data = 0x0924 },
159 /* OPTi 82C925 */
160 { .id = "OPT0925",
161 .devs = { { "OPT9250" }, { "OPT0002" }, { "OPT0005" } },
162 .driver_data = 0x0925 },
163 #else
164 /* OPTi 82C931/3 */
165 { .id = "OPT0931", .devs = { { "OPT9310" }, { "OPT0002" } },
166 .driver_data = 0x0931 },
167 #endif /* OPTi93X */
168 { .id = "" }
171 MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids);
173 #endif /* CONFIG_PNP */
175 #ifdef OPTi93X
176 #define DEV_NAME "opti93x"
177 #else
178 #define DEV_NAME "opti92x"
179 #endif
181 static char * snd_opti9xx_names[] = {
182 "unknown",
183 "82C928", "82C929",
184 "82C924", "82C925",
185 "82C930", "82C931", "82C933"
189 static long __devinit snd_legacy_find_free_ioport(long *port_table, long size)
191 while (*port_table != -1) {
192 if (request_region(*port_table, size, "ALSA test")) {
193 release_region(*port_table, size);
194 return *port_table;
196 port_table++;
198 return -1;
201 static int __devinit snd_opti9xx_init(struct snd_opti9xx *chip,
202 unsigned short hardware)
204 static int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
206 chip->hardware = hardware;
207 strcpy(chip->name, snd_opti9xx_names[hardware]);
209 spin_lock_init(&chip->lock);
211 chip->irq = -1;
213 #ifndef OPTi93X
214 #ifdef CONFIG_PNP
215 if (isapnp && chip->mc_base)
216 /* PnP resource gives the least 10 bits */
217 chip->mc_base |= 0xc00;
218 else
219 #endif /* CONFIG_PNP */
221 chip->mc_base = 0xf8c;
222 chip->mc_base_size = opti9xx_mc_size[hardware];
224 #else
225 chip->mc_base_size = opti9xx_mc_size[hardware];
226 #endif
228 switch (hardware) {
229 #ifndef OPTi93X
230 case OPTi9XX_HW_82C928:
231 case OPTi9XX_HW_82C929:
232 chip->password = (hardware == OPTi9XX_HW_82C928) ? 0xe2 : 0xe3;
233 chip->pwd_reg = 3;
234 break;
236 case OPTi9XX_HW_82C924:
237 case OPTi9XX_HW_82C925:
238 chip->password = 0xe5;
239 chip->pwd_reg = 3;
240 break;
241 #else /* OPTi93X */
243 case OPTi9XX_HW_82C930:
244 case OPTi9XX_HW_82C931:
245 case OPTi9XX_HW_82C933:
246 chip->mc_base = (hardware == OPTi9XX_HW_82C930) ? 0xf8f : 0xf8d;
247 if (!chip->mc_indir_index)
248 chip->mc_indir_index = 0xe0e;
249 chip->password = 0xe4;
250 chip->pwd_reg = 0;
251 break;
252 #endif /* OPTi93X */
254 default:
255 snd_printk(KERN_ERR "chip %d not supported\n", hardware);
256 return -ENODEV;
258 return 0;
261 static unsigned char snd_opti9xx_read(struct snd_opti9xx *chip,
262 unsigned char reg)
264 unsigned long flags;
265 unsigned char retval = 0xff;
267 spin_lock_irqsave(&chip->lock, flags);
268 outb(chip->password, chip->mc_base + chip->pwd_reg);
270 switch (chip->hardware) {
271 #ifndef OPTi93X
272 case OPTi9XX_HW_82C924:
273 case OPTi9XX_HW_82C925:
274 if (reg > 7) {
275 outb(reg, chip->mc_base + 8);
276 outb(chip->password, chip->mc_base + chip->pwd_reg);
277 retval = inb(chip->mc_base + 9);
278 break;
281 case OPTi9XX_HW_82C928:
282 case OPTi9XX_HW_82C929:
283 retval = inb(chip->mc_base + reg);
284 break;
285 #else /* OPTi93X */
287 case OPTi9XX_HW_82C930:
288 case OPTi9XX_HW_82C931:
289 case OPTi9XX_HW_82C933:
290 outb(reg, chip->mc_indir_index);
291 outb(chip->password, chip->mc_base + chip->pwd_reg);
292 retval = inb(chip->mc_indir_index + 1);
293 break;
294 #endif /* OPTi93X */
296 default:
297 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
300 spin_unlock_irqrestore(&chip->lock, flags);
301 return retval;
304 static void snd_opti9xx_write(struct snd_opti9xx *chip, unsigned char reg,
305 unsigned char value)
307 unsigned long flags;
309 spin_lock_irqsave(&chip->lock, flags);
310 outb(chip->password, chip->mc_base + chip->pwd_reg);
312 switch (chip->hardware) {
313 #ifndef OPTi93X
314 case OPTi9XX_HW_82C924:
315 case OPTi9XX_HW_82C925:
316 if (reg > 7) {
317 outb(reg, chip->mc_base + 8);
318 outb(chip->password, chip->mc_base + chip->pwd_reg);
319 outb(value, chip->mc_base + 9);
320 break;
323 case OPTi9XX_HW_82C928:
324 case OPTi9XX_HW_82C929:
325 outb(value, chip->mc_base + reg);
326 break;
327 #else /* OPTi93X */
329 case OPTi9XX_HW_82C930:
330 case OPTi9XX_HW_82C931:
331 case OPTi9XX_HW_82C933:
332 outb(reg, chip->mc_indir_index);
333 outb(chip->password, chip->mc_base + chip->pwd_reg);
334 outb(value, chip->mc_indir_index + 1);
335 break;
336 #endif /* OPTi93X */
338 default:
339 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
342 spin_unlock_irqrestore(&chip->lock, flags);
346 #define snd_opti9xx_write_mask(chip, reg, value, mask) \
347 snd_opti9xx_write(chip, reg, \
348 (snd_opti9xx_read(chip, reg) & ~(mask)) | ((value) & (mask)))
351 static int snd_opti9xx_configure(struct snd_opti9xx *chip,
352 long port,
353 int irq, int dma1, int dma2,
354 long mpu_port, int mpu_irq)
356 unsigned char wss_base_bits;
357 unsigned char irq_bits;
358 unsigned char dma_bits;
359 unsigned char mpu_port_bits = 0;
360 unsigned char mpu_irq_bits;
362 switch (chip->hardware) {
363 #ifndef OPTi93X
364 case OPTi9XX_HW_82C924:
365 /* opti 929 mode (?), OPL3 clock output, audio enable */
366 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc);
367 /* enable wave audio */
368 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
370 case OPTi9XX_HW_82C925:
371 /* enable WSS mode */
372 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
373 /* OPL3 FM synthesis */
374 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
375 /* disable Sound Blaster IRQ and DMA */
376 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
377 #ifdef CS4231
378 /* cs4231/4248 fix enabled */
379 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
380 #else
381 /* cs4231/4248 fix disabled */
382 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
383 #endif /* CS4231 */
384 break;
386 case OPTi9XX_HW_82C928:
387 case OPTi9XX_HW_82C929:
388 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
389 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
391 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae);
393 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
394 #ifdef CS4231
395 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
396 #else
397 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
398 #endif /* CS4231 */
399 break;
401 #else /* OPTi93X */
402 case OPTi9XX_HW_82C931:
403 /* disable 3D sound (set GPIO1 as output, low) */
404 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(20), 0x04, 0x0c);
405 case OPTi9XX_HW_82C933: /* FALL THROUGH */
407 * The BTC 1817DW has QS1000 wavetable which is connected
408 * to the serial digital input of the OPTI931.
410 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(21), 0x82, 0xff);
412 * This bit sets OPTI931 to automaticaly select FM
413 * or digital input signal.
415 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(26), 0x01, 0x01);
416 case OPTi9XX_HW_82C930: /* FALL THROUGH */
417 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03);
418 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff);
419 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 |
420 (chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04),
421 0x34);
422 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf);
423 break;
424 #endif /* OPTi93X */
426 default:
427 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
428 return -EINVAL;
431 /* PnP resource says it decodes only 10 bits of address */
432 switch (port & 0x3ff) {
433 case 0x130:
434 chip->wss_base = 0x530;
435 wss_base_bits = 0x00;
436 break;
437 case 0x204:
438 chip->wss_base = 0x604;
439 wss_base_bits = 0x03;
440 break;
441 case 0x280:
442 chip->wss_base = 0xe80;
443 wss_base_bits = 0x01;
444 break;
445 case 0x340:
446 chip->wss_base = 0xf40;
447 wss_base_bits = 0x02;
448 break;
449 default:
450 snd_printk(KERN_WARNING "WSS port 0x%lx not valid\n", port);
451 goto __skip_base;
453 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
455 __skip_base:
456 switch (irq) {
457 //#ifdef OPTi93X
458 case 5:
459 irq_bits = 0x05;
460 break;
461 //#endif /* OPTi93X */
462 case 7:
463 irq_bits = 0x01;
464 break;
465 case 9:
466 irq_bits = 0x02;
467 break;
468 case 10:
469 irq_bits = 0x03;
470 break;
471 case 11:
472 irq_bits = 0x04;
473 break;
474 default:
475 snd_printk(KERN_WARNING "WSS irq # %d not valid\n", irq);
476 goto __skip_resources;
479 switch (dma1) {
480 case 0:
481 dma_bits = 0x01;
482 break;
483 case 1:
484 dma_bits = 0x02;
485 break;
486 case 3:
487 dma_bits = 0x03;
488 break;
489 default:
490 snd_printk(KERN_WARNING "WSS dma1 # %d not valid\n", dma1);
491 goto __skip_resources;
494 #if defined(CS4231) || defined(OPTi93X)
495 if (dma1 == dma2) {
496 snd_printk(KERN_ERR "don't want to share dmas\n");
497 return -EBUSY;
500 switch (dma2) {
501 case 0:
502 case 1:
503 break;
504 default:
505 snd_printk(KERN_WARNING "WSS dma2 # %d not valid\n", dma2);
506 goto __skip_resources;
508 dma_bits |= 0x04;
509 #endif /* CS4231 || OPTi93X */
511 #ifndef OPTi93X
512 outb(irq_bits << 3 | dma_bits, chip->wss_base);
513 #else /* OPTi93X */
514 snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits));
515 #endif /* OPTi93X */
517 __skip_resources:
518 if (chip->hardware > OPTi9XX_HW_82C928) {
519 switch (mpu_port) {
520 case 0:
521 case -1:
522 break;
523 case 0x300:
524 mpu_port_bits = 0x03;
525 break;
526 case 0x310:
527 mpu_port_bits = 0x02;
528 break;
529 case 0x320:
530 mpu_port_bits = 0x01;
531 break;
532 case 0x330:
533 mpu_port_bits = 0x00;
534 break;
535 default:
536 snd_printk(KERN_WARNING
537 "MPU-401 port 0x%lx not valid\n", mpu_port);
538 goto __skip_mpu;
541 switch (mpu_irq) {
542 case 5:
543 mpu_irq_bits = 0x02;
544 break;
545 case 7:
546 mpu_irq_bits = 0x03;
547 break;
548 case 9:
549 mpu_irq_bits = 0x00;
550 break;
551 case 10:
552 mpu_irq_bits = 0x01;
553 break;
554 default:
555 snd_printk(KERN_WARNING "MPU-401 irq # %d not valid\n",
556 mpu_irq);
557 goto __skip_mpu;
560 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6),
561 (mpu_port <= 0) ? 0x00 :
562 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
563 0xf8);
565 __skip_mpu:
567 return 0;
570 #ifdef OPTi93X
572 static const DECLARE_TLV_DB_SCALE(db_scale_5bit_3db_step, -9300, 300, 0);
573 static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0);
574 static const DECLARE_TLV_DB_SCALE(db_scale_4bit_12db_max, -3300, 300, 0);
576 static struct snd_kcontrol_new snd_opti93x_controls[] = {
577 WSS_DOUBLE("Master Playback Switch", 0,
578 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
579 WSS_DOUBLE_TLV("Master Playback Volume", 0,
580 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1,
581 db_scale_5bit_3db_step),
582 WSS_DOUBLE_TLV("PCM Playback Volume", 0,
583 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1,
584 db_scale_5bit),
585 WSS_DOUBLE_TLV("FM Playback Volume", 0,
586 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1,
587 db_scale_4bit_12db_max),
588 WSS_DOUBLE("Line Playback Switch", 0,
589 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
590 WSS_DOUBLE_TLV("Line Playback Volume", 0,
591 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1,
592 db_scale_4bit_12db_max),
593 WSS_DOUBLE("Mic Playback Switch", 0,
594 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
595 WSS_DOUBLE_TLV("Mic Playback Volume", 0,
596 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1,
597 db_scale_4bit_12db_max),
598 WSS_DOUBLE_TLV("CD Playback Volume", 0,
599 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1,
600 db_scale_4bit_12db_max),
601 WSS_DOUBLE("Aux Playback Switch", 0,
602 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
603 WSS_DOUBLE_TLV("Aux Playback Volume", 0,
604 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1,
605 db_scale_4bit_12db_max),
608 static int __devinit snd_opti93x_mixer(struct snd_wss *chip)
610 struct snd_card *card;
611 unsigned int idx;
612 struct snd_ctl_elem_id id1, id2;
613 int err;
615 if (snd_BUG_ON(!chip || !chip->pcm))
616 return -EINVAL;
618 card = chip->card;
620 strcpy(card->mixername, chip->pcm->name);
622 memset(&id1, 0, sizeof(id1));
623 memset(&id2, 0, sizeof(id2));
624 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
625 /* reassign AUX0 switch to CD */
626 strcpy(id1.name, "Aux Playback Switch");
627 strcpy(id2.name, "CD Playback Switch");
628 err = snd_ctl_rename_id(card, &id1, &id2);
629 if (err < 0) {
630 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
631 return err;
633 /* reassign AUX1 switch to FM */
634 strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
635 strcpy(id2.name, "FM Playback Switch");
636 err = snd_ctl_rename_id(card, &id1, &id2);
637 if (err < 0) {
638 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
639 return err;
641 /* remove AUX1 volume */
642 strcpy(id1.name, "Aux Playback Volume"); id1.index = 1;
643 snd_ctl_remove_id(card, &id1);
645 /* Replace WSS volume controls with OPTi93x volume controls */
646 id1.index = 0;
647 for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
648 strcpy(id1.name, snd_opti93x_controls[idx].name);
649 snd_ctl_remove_id(card, &id1);
651 err = snd_ctl_add(card,
652 snd_ctl_new1(&snd_opti93x_controls[idx], chip));
653 if (err < 0)
654 return err;
656 return 0;
659 static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id)
661 struct snd_opti9xx *chip = dev_id;
662 struct snd_wss *codec = chip->codec;
663 unsigned char status;
665 if (!codec)
666 return IRQ_HANDLED;
668 status = snd_opti9xx_read(chip, OPTi9XX_MC_REG(11));
669 if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
670 snd_pcm_period_elapsed(codec->playback_substream);
671 if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
672 snd_wss_overrange(codec);
673 snd_pcm_period_elapsed(codec->capture_substream);
675 outb(0x00, OPTi93X_PORT(codec, STATUS));
676 return IRQ_HANDLED;
679 #endif /* OPTi93X */
681 static int __devinit snd_opti9xx_read_check(struct snd_opti9xx *chip)
683 unsigned char value;
684 #ifdef OPTi93X
685 unsigned long flags;
686 #endif
688 chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
689 "OPTi9xx MC");
690 if (chip->res_mc_base == NULL)
691 return -EBUSY;
692 #ifndef OPTi93X
693 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
694 if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1)))
695 if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
696 return 0;
697 #else /* OPTi93X */
698 chip->res_mc_indir = request_region(chip->mc_indir_index, 2,
699 "OPTi93x MC");
700 if (chip->res_mc_indir == NULL)
701 return -EBUSY;
703 spin_lock_irqsave(&chip->lock, flags);
704 outb(chip->password, chip->mc_base + chip->pwd_reg);
705 outb(((chip->mc_indir_index & 0x1f0) >> 4), chip->mc_base);
706 spin_unlock_irqrestore(&chip->lock, flags);
708 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
709 snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
710 if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
711 return 0;
713 release_and_free_resource(chip->res_mc_indir);
714 chip->res_mc_indir = NULL;
715 #endif /* OPTi93X */
716 release_and_free_resource(chip->res_mc_base);
717 chip->res_mc_base = NULL;
719 return -ENODEV;
722 static int __devinit snd_card_opti9xx_detect(struct snd_card *card,
723 struct snd_opti9xx *chip)
725 int i, err;
727 #ifndef OPTi93X
728 for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
729 #else
730 for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
731 #endif
732 err = snd_opti9xx_init(chip, i);
733 if (err < 0)
734 return err;
736 err = snd_opti9xx_read_check(chip);
737 if (err == 0)
738 return 1;
739 #ifdef OPTi93X
740 chip->mc_indir_index = 0;
741 #endif
743 return -ENODEV;
746 #ifdef CONFIG_PNP
747 static int __devinit snd_card_opti9xx_pnp(struct snd_opti9xx *chip,
748 struct pnp_card_link *card,
749 const struct pnp_card_device_id *pid)
751 struct pnp_dev *pdev;
752 int err;
753 struct pnp_dev *devmpu;
754 #ifndef OPTi93X
755 struct pnp_dev *devmc;
756 #endif
758 pdev = pnp_request_card_device(card, pid->devs[0].id, NULL);
759 if (pdev == NULL)
760 return -EBUSY;
762 err = pnp_activate_dev(pdev);
763 if (err < 0) {
764 snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
765 return err;
768 #ifdef OPTi93X
769 port = pnp_port_start(pdev, 0) - 4;
770 fm_port = pnp_port_start(pdev, 1) + 8;
771 /* adjust mc_indir_index - some cards report it at 0xe?d,
772 other at 0xe?c but it really is always at 0xe?e */
773 chip->mc_indir_index = (pnp_port_start(pdev, 3) & ~0xf) | 0xe;
774 #else
775 devmc = pnp_request_card_device(card, pid->devs[2].id, NULL);
776 if (devmc == NULL)
777 return -EBUSY;
779 err = pnp_activate_dev(devmc);
780 if (err < 0) {
781 snd_printk(KERN_ERR "MC pnp configure failure: %d\n", err);
782 return err;
785 port = pnp_port_start(pdev, 1);
786 fm_port = pnp_port_start(pdev, 2) + 8;
788 * The MC(0) is never accessed and card does not
789 * include it in the PnP resource range. OPTI93x include it.
791 chip->mc_base = pnp_port_start(devmc, 0) - 1;
792 chip->mc_base_size = pnp_port_len(devmc, 0) + 1;
793 #endif /* OPTi93X */
794 irq = pnp_irq(pdev, 0);
795 dma1 = pnp_dma(pdev, 0);
796 #if defined(CS4231) || defined(OPTi93X)
797 dma2 = pnp_dma(pdev, 1);
798 #endif /* CS4231 || OPTi93X */
800 devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
802 if (devmpu && mpu_port > 0) {
803 err = pnp_activate_dev(devmpu);
804 if (err < 0) {
805 snd_printk(KERN_ERR "MPU401 pnp configure failure\n");
806 mpu_port = -1;
807 } else {
808 mpu_port = pnp_port_start(devmpu, 0);
809 mpu_irq = pnp_irq(devmpu, 0);
812 return pid->driver_data;
814 #endif /* CONFIG_PNP */
816 static void snd_card_opti9xx_free(struct snd_card *card)
818 struct snd_opti9xx *chip = card->private_data;
820 if (chip) {
821 #ifdef OPTi93X
822 if (chip->irq > 0) {
823 disable_irq(chip->irq);
824 free_irq(chip->irq, chip);
826 release_and_free_resource(chip->res_mc_indir);
827 #endif
828 release_and_free_resource(chip->res_mc_base);
832 static int __devinit snd_opti9xx_probe(struct snd_card *card)
834 static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
835 int error;
836 int xdma2;
837 struct snd_opti9xx *chip = card->private_data;
838 struct snd_wss *codec;
839 #ifdef CS4231
840 struct snd_timer *timer;
841 #endif
842 struct snd_pcm *pcm;
843 struct snd_rawmidi *rmidi;
844 struct snd_hwdep *synth;
846 #if defined(CS4231) || defined(OPTi93X)
847 xdma2 = dma2;
848 #else
849 xdma2 = -1;
850 #endif
852 if (port == SNDRV_AUTO_PORT) {
853 port = snd_legacy_find_free_ioport(possible_ports, 4);
854 if (port < 0) {
855 snd_printk(KERN_ERR "unable to find a free WSS port\n");
856 return -EBUSY;
859 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
860 mpu_port, mpu_irq);
861 if (error)
862 return error;
864 error = snd_wss_create(card, chip->wss_base + 4, -1, irq, dma1, xdma2,
865 #ifdef OPTi93X
866 WSS_HW_OPTI93X, WSS_HWSHARE_IRQ,
867 #else
868 WSS_HW_DETECT, 0,
869 #endif
870 &codec);
871 if (error < 0)
872 return error;
873 chip->codec = codec;
874 error = snd_wss_pcm(codec, 0, &pcm);
875 if (error < 0)
876 return error;
877 error = snd_wss_mixer(codec);
878 if (error < 0)
879 return error;
880 #ifdef OPTi93X
881 error = snd_opti93x_mixer(codec);
882 if (error < 0)
883 return error;
884 #endif
885 #ifdef CS4231
886 error = snd_wss_timer(codec, 0, &timer);
887 if (error < 0)
888 return error;
889 #endif
890 #ifdef OPTi93X
891 error = request_irq(irq, snd_opti93x_interrupt,
892 0, DEV_NAME" - WSS", chip);
893 if (error < 0) {
894 snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", irq);
895 return error;
897 #endif
898 chip->irq = irq;
899 strcpy(card->driver, chip->name);
900 sprintf(card->shortname, "OPTi %s", card->driver);
901 #if defined(CS4231) || defined(OPTi93X)
902 sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d&%d",
903 card->shortname, pcm->name,
904 chip->wss_base + 4, irq, dma1, xdma2);
905 #else
906 sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d",
907 card->shortname, pcm->name, chip->wss_base + 4, irq, dma1);
908 #endif /* CS4231 || OPTi93X */
910 if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
911 rmidi = NULL;
912 else {
913 error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
914 mpu_port, 0, mpu_irq, &rmidi);
915 if (error)
916 snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
917 mpu_port);
920 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
921 struct snd_opl3 *opl3 = NULL;
922 #ifndef OPTi93X
923 if (chip->hardware == OPTi9XX_HW_82C928 ||
924 chip->hardware == OPTi9XX_HW_82C929 ||
925 chip->hardware == OPTi9XX_HW_82C924) {
926 struct snd_opl4 *opl4;
927 /* assume we have an OPL4 */
928 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
929 0x20, 0x20);
930 if (snd_opl4_create(card, fm_port, fm_port - 8,
931 2, &opl3, &opl4) < 0) {
932 /* no luck, use OPL3 instead */
933 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
934 0x00, 0x20);
937 #endif /* !OPTi93X */
938 if (!opl3 && snd_opl3_create(card, fm_port, fm_port + 2,
939 OPL3_HW_AUTO, 0, &opl3) < 0) {
940 snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n",
941 fm_port, fm_port + 4 - 1);
943 if (opl3) {
944 error = snd_opl3_hwdep_new(opl3, 0, 1, &synth);
945 if (error < 0)
946 return error;
950 return snd_card_register(card);
953 static int snd_opti9xx_card_new(struct snd_card **cardp)
955 struct snd_card *card;
956 int err;
958 err = snd_card_create(index, id, THIS_MODULE,
959 sizeof(struct snd_opti9xx), &card);
960 if (err < 0)
961 return err;
962 card->private_free = snd_card_opti9xx_free;
963 *cardp = card;
964 return 0;
967 static int __devinit snd_opti9xx_isa_match(struct device *devptr,
968 unsigned int dev)
970 #ifdef CONFIG_PNP
971 if (snd_opti9xx_pnp_is_probed)
972 return 0;
973 if (isapnp)
974 return 0;
975 #endif
976 return 1;
979 static int __devinit snd_opti9xx_isa_probe(struct device *devptr,
980 unsigned int dev)
982 struct snd_card *card;
983 int error;
984 static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
985 #ifdef OPTi93X
986 static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
987 #else
988 static int possible_irqs[] = {9, 10, 11, 7, -1};
989 #endif /* OPTi93X */
990 static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
991 static int possible_dma1s[] = {3, 1, 0, -1};
992 #if defined(CS4231) || defined(OPTi93X)
993 static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
994 #endif /* CS4231 || OPTi93X */
996 if (mpu_port == SNDRV_AUTO_PORT) {
997 if ((mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
998 snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
999 return -EBUSY;
1002 if (irq == SNDRV_AUTO_IRQ) {
1003 if ((irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
1004 snd_printk(KERN_ERR "unable to find a free IRQ\n");
1005 return -EBUSY;
1008 if (mpu_irq == SNDRV_AUTO_IRQ) {
1009 if ((mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
1010 snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
1011 return -EBUSY;
1014 if (dma1 == SNDRV_AUTO_DMA) {
1015 if ((dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
1016 snd_printk(KERN_ERR "unable to find a free DMA1\n");
1017 return -EBUSY;
1020 #if defined(CS4231) || defined(OPTi93X)
1021 if (dma2 == SNDRV_AUTO_DMA) {
1022 if ((dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4])) < 0) {
1023 snd_printk(KERN_ERR "unable to find a free DMA2\n");
1024 return -EBUSY;
1027 #endif
1029 error = snd_opti9xx_card_new(&card);
1030 if (error < 0)
1031 return error;
1033 if ((error = snd_card_opti9xx_detect(card, card->private_data)) < 0) {
1034 snd_card_free(card);
1035 return error;
1037 snd_card_set_dev(card, devptr);
1038 if ((error = snd_opti9xx_probe(card)) < 0) {
1039 snd_card_free(card);
1040 return error;
1042 dev_set_drvdata(devptr, card);
1043 return 0;
1046 static int __devexit snd_opti9xx_isa_remove(struct device *devptr,
1047 unsigned int dev)
1049 snd_card_free(dev_get_drvdata(devptr));
1050 dev_set_drvdata(devptr, NULL);
1051 return 0;
1054 #ifdef CONFIG_PM
1055 static int snd_opti9xx_suspend(struct snd_card *card)
1057 struct snd_opti9xx *chip = card->private_data;
1059 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1060 chip->codec->suspend(chip->codec);
1061 return 0;
1064 static int snd_opti9xx_resume(struct snd_card *card)
1066 struct snd_opti9xx *chip = card->private_data;
1067 int error, xdma2;
1068 #if defined(CS4231) || defined(OPTi93X)
1069 xdma2 = dma2;
1070 #else
1071 xdma2 = -1;
1072 #endif
1074 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
1075 mpu_port, mpu_irq);
1076 if (error)
1077 return error;
1078 chip->codec->resume(chip->codec);
1079 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1080 return 0;
1083 static int snd_opti9xx_isa_suspend(struct device *dev, unsigned int n,
1084 pm_message_t state)
1086 return snd_opti9xx_suspend(dev_get_drvdata(dev));
1089 static int snd_opti9xx_isa_resume(struct device *dev, unsigned int n)
1091 return snd_opti9xx_resume(dev_get_drvdata(dev));
1093 #endif
1095 static struct isa_driver snd_opti9xx_driver = {
1096 .match = snd_opti9xx_isa_match,
1097 .probe = snd_opti9xx_isa_probe,
1098 .remove = __devexit_p(snd_opti9xx_isa_remove),
1099 #ifdef CONFIG_PM
1100 .suspend = snd_opti9xx_isa_suspend,
1101 .resume = snd_opti9xx_isa_resume,
1102 #endif
1103 .driver = {
1104 .name = DEV_NAME
1108 #ifdef CONFIG_PNP
1109 static int __devinit snd_opti9xx_pnp_probe(struct pnp_card_link *pcard,
1110 const struct pnp_card_device_id *pid)
1112 struct snd_card *card;
1113 int error, hw;
1114 struct snd_opti9xx *chip;
1116 if (snd_opti9xx_pnp_is_probed)
1117 return -EBUSY;
1118 if (! isapnp)
1119 return -ENODEV;
1120 error = snd_opti9xx_card_new(&card);
1121 if (error < 0)
1122 return error;
1123 chip = card->private_data;
1125 hw = snd_card_opti9xx_pnp(chip, pcard, pid);
1126 switch (hw) {
1127 case 0x0924:
1128 hw = OPTi9XX_HW_82C924;
1129 break;
1130 case 0x0925:
1131 hw = OPTi9XX_HW_82C925;
1132 break;
1133 case 0x0931:
1134 hw = OPTi9XX_HW_82C931;
1135 break;
1136 default:
1137 snd_card_free(card);
1138 return -ENODEV;
1141 if ((error = snd_opti9xx_init(chip, hw))) {
1142 snd_card_free(card);
1143 return error;
1145 error = snd_opti9xx_read_check(chip);
1146 if (error) {
1147 snd_printk(KERN_ERR "OPTI chip not found\n");
1148 snd_card_free(card);
1149 return error;
1151 snd_card_set_dev(card, &pcard->card->dev);
1152 if ((error = snd_opti9xx_probe(card)) < 0) {
1153 snd_card_free(card);
1154 return error;
1156 pnp_set_card_drvdata(pcard, card);
1157 snd_opti9xx_pnp_is_probed = 1;
1158 return 0;
1161 static void __devexit snd_opti9xx_pnp_remove(struct pnp_card_link * pcard)
1163 snd_card_free(pnp_get_card_drvdata(pcard));
1164 pnp_set_card_drvdata(pcard, NULL);
1165 snd_opti9xx_pnp_is_probed = 0;
1168 #ifdef CONFIG_PM
1169 static int snd_opti9xx_pnp_suspend(struct pnp_card_link *pcard,
1170 pm_message_t state)
1172 return snd_opti9xx_suspend(pnp_get_card_drvdata(pcard));
1175 static int snd_opti9xx_pnp_resume(struct pnp_card_link *pcard)
1177 return snd_opti9xx_resume(pnp_get_card_drvdata(pcard));
1179 #endif
1181 static struct pnp_card_driver opti9xx_pnpc_driver = {
1182 .flags = PNP_DRIVER_RES_DISABLE,
1183 .name = "opti9xx",
1184 .id_table = snd_opti9xx_pnpids,
1185 .probe = snd_opti9xx_pnp_probe,
1186 .remove = __devexit_p(snd_opti9xx_pnp_remove),
1187 #ifdef CONFIG_PM
1188 .suspend = snd_opti9xx_pnp_suspend,
1189 .resume = snd_opti9xx_pnp_resume,
1190 #endif
1192 #endif
1194 #ifdef OPTi93X
1195 #define CHIP_NAME "82C93x"
1196 #else
1197 #define CHIP_NAME "82C92x"
1198 #endif
1200 static int __init alsa_card_opti9xx_init(void)
1202 #ifdef CONFIG_PNP
1203 pnp_register_card_driver(&opti9xx_pnpc_driver);
1204 if (snd_opti9xx_pnp_is_probed)
1205 return 0;
1206 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
1207 #endif
1208 return isa_register_driver(&snd_opti9xx_driver, 1);
1211 static void __exit alsa_card_opti9xx_exit(void)
1213 if (!snd_opti9xx_pnp_is_probed) {
1214 isa_unregister_driver(&snd_opti9xx_driver);
1215 return;
1217 #ifdef CONFIG_PNP
1218 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
1219 #endif
1222 module_init(alsa_card_opti9xx_init)
1223 module_exit(alsa_card_opti9xx_exit)