ALSA: snd-opti9xx: fixes for MED3931 card (opti931)
[linux-3.10.git] / sound / isa / opti9xx / opti92x-ad1848.c
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 <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>
45
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 */
63
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 */
79
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 */
106
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
115
116 #define OPTi9XX_MC_REG(n)       n
117
118 #ifdef OPTi93X
119
120 #define OPTi93X_STATUS                  0x02
121 #define OPTi93X_PORT(chip, r)           ((chip)->port + OPTi93X_##r)
122
123 #define OPTi93X_IRQ_PLAYBACK            0x04
124 #define OPTi93X_IRQ_CAPTURE             0x08
125
126 #endif /* OPTi93X */
127
128 struct snd_opti9xx {
129         unsigned short hardware;
130         unsigned char password;
131         char name[7];
132
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         struct snd_wss *codec;
140 #endif  /* OPTi93X */
141         unsigned long pwd_reg;
142
143         spinlock_t lock;
144
145         long wss_base;
146         int irq;
147 };
148
149 static int snd_opti9xx_pnp_is_probed;
150
151 #ifdef CONFIG_PNP
152
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 = "" }
169 };
170
171 MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids);
172
173 #endif  /* CONFIG_PNP */
174
175 #ifdef OPTi93X
176 #define DEV_NAME "opti93x"
177 #else
178 #define DEV_NAME "opti92x"
179 #endif
180
181 static char * snd_opti9xx_names[] = {
182         "unknown",
183         "82C928",       "82C929",
184         "82C924",       "82C925",
185         "82C930",       "82C931",       "82C933"
186 };
187
188
189 static long __devinit snd_legacy_find_free_ioport(long *port_table, long size)
190 {
191         while (*port_table != -1) {
192                 if (request_region(*port_table, size, "ALSA test")) {
193                         release_region(*port_table, size);
194                         return *port_table;
195                 }
196                 port_table++;
197         }
198         return -1;
199 }
200
201 static int __devinit snd_opti9xx_init(struct snd_opti9xx *chip,
202                                       unsigned short hardware)
203 {
204         static int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
205
206         chip->hardware = hardware;
207         strcpy(chip->name, snd_opti9xx_names[hardware]);
208
209         spin_lock_init(&chip->lock);
210
211         chip->irq = -1;
212
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 */
220         {
221                 chip->mc_base = 0xf8c;
222                 chip->mc_base_size = opti9xx_mc_size[hardware];
223         }
224 #else
225                 chip->mc_base_size = opti9xx_mc_size[hardware];
226 #endif
227
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;
235
236         case OPTi9XX_HW_82C924:
237         case OPTi9XX_HW_82C925:
238                 chip->password = 0xe5;
239                 chip->pwd_reg = 3;
240                 break;
241 #else   /* OPTi93X */
242
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 */
253
254         default:
255                 snd_printk(KERN_ERR "chip %d not supported\n", hardware);
256                 return -ENODEV;
257         }
258         return 0;
259 }
260
261 static unsigned char snd_opti9xx_read(struct snd_opti9xx *chip,
262                                       unsigned char reg)
263 {
264         unsigned long flags;
265         unsigned char retval = 0xff;
266
267         spin_lock_irqsave(&chip->lock, flags);
268         outb(chip->password, chip->mc_base + chip->pwd_reg);
269
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;
279                 }
280
281         case OPTi9XX_HW_82C928:
282         case OPTi9XX_HW_82C929:
283                 retval = inb(chip->mc_base + reg);
284                 break;
285 #else   /* OPTi93X */
286
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 */
295
296         default:
297                 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
298         }
299
300         spin_unlock_irqrestore(&chip->lock, flags);
301         return retval;
302 }
303
304 static void snd_opti9xx_write(struct snd_opti9xx *chip, unsigned char reg,
305                               unsigned char value)
306 {
307         unsigned long flags;
308
309         spin_lock_irqsave(&chip->lock, flags);
310         outb(chip->password, chip->mc_base + chip->pwd_reg);
311
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;
321                 }
322
323         case OPTi9XX_HW_82C928:
324         case OPTi9XX_HW_82C929:
325                 outb(value, chip->mc_base + reg);
326                 break;
327 #else   /* OPTi93X */
328
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 */
337
338         default:
339                 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
340         }
341
342         spin_unlock_irqrestore(&chip->lock, flags);
343 }
344
345
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)))
349
350
351 static int __devinit snd_opti9xx_configure(struct snd_opti9xx *chip,
352                                            long port,
353                                            int irq, int dma1, int dma2,
354                                            long mpu_port, int mpu_irq)
355 {
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;
361
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);
369
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;
385
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);
390                 /*
391                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae);
392                 */
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;
400
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 */
406                 /*
407                  * The BTC 1817DW has QS1000 wavetable which is connected
408                  * to the serial digital input of the OPTI931.
409                  */
410                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(21), 0x82, 0xff);
411                 /* 
412                  * This bit sets OPTI931 to automaticaly select FM
413                  * or digital input signal.
414                  */
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 */
425
426         default:
427                 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
428                 return -EINVAL;
429         }
430
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;
452         }
453         snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
454
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;
477         }
478
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;
492         }
493
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;
498         }
499
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;
507         }
508         dma_bits |= 0x04;
509 #endif  /* CS4231 || OPTi93X */
510
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 */
516
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;
539                 }
540
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;
558                 }
559
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);
564         }
565 __skip_mpu:
566
567         return 0;
568 }
569
570 #ifdef OPTi93X
571
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);
575
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),
606 };
607
608 static int __devinit snd_opti93x_mixer(struct snd_wss *chip)
609 {
610         struct snd_card *card;
611         unsigned int idx;
612         struct snd_ctl_elem_id id1, id2;
613         int err;
614
615         if (snd_BUG_ON(!chip || !chip->pcm))
616                 return -EINVAL;
617
618         card = chip->card;
619
620         strcpy(card->mixername, chip->pcm->name);
621
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;
632         }
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;
640         }
641         /* remove AUX1 volume */
642         strcpy(id1.name, "Aux Playback Volume"); id1.index = 1;
643         snd_ctl_remove_id(card, &id1);
644
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);
650
651                 err = snd_ctl_add(card,
652                                 snd_ctl_new1(&snd_opti93x_controls[idx], chip));
653                 if (err < 0)
654                         return err;
655         }
656         return 0;
657 }
658
659 static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id)
660 {
661         struct snd_opti9xx *chip = dev_id;
662         struct snd_wss *codec = chip->codec;
663         unsigned char status;
664
665         if (!codec)
666                 return IRQ_HANDLED;
667
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);
674         }
675         outb(0x00, OPTi93X_PORT(codec, STATUS));
676         return IRQ_HANDLED;
677 }
678
679 #endif /* OPTi93X */
680
681 static int __devinit snd_opti9xx_read_check(struct snd_opti9xx *chip)
682 {
683         unsigned char value;
684 #ifdef OPTi93X
685         unsigned long flags;
686 #endif
687
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;
702
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);
707
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;
712
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;
718
719         return -ENODEV;
720 }
721
722 static int __devinit snd_card_opti9xx_detect(struct snd_card *card,
723                                              struct snd_opti9xx *chip)
724 {
725         int i, err;
726
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;
735
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
742         }
743         return -ENODEV;
744 }
745
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)
750 {
751         struct pnp_dev *pdev;
752         int err;
753         struct pnp_dev *devmpu;
754 #ifndef OPTi93X
755         struct pnp_dev *devmc;
756 #endif
757
758         pdev = pnp_request_card_device(card, pid->devs[0].id, NULL);
759         if (pdev == NULL)
760                 return -EBUSY;
761
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;
766         }
767
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;
778
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;
783         }
784
785         port = pnp_port_start(pdev, 1);
786         fm_port = pnp_port_start(pdev, 2) + 8;
787         /*
788          * The MC(0) is never accessed and card does not
789          * include it in the PnP resource range. OPTI93x include it.
790          */
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 */
799
800         devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
801
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);
810                 }
811         }
812         return pid->driver_data;
813 }
814 #endif  /* CONFIG_PNP */
815
816 static void snd_card_opti9xx_free(struct snd_card *card)
817 {
818         struct snd_opti9xx *chip = card->private_data;
819
820         if (chip) {
821 #ifdef OPTi93X
822                 if (chip->irq > 0) {
823                         disable_irq(chip->irq);
824                         free_irq(chip->irq, chip);
825                 }
826                 release_and_free_resource(chip->res_mc_indir);
827 #endif
828                 release_and_free_resource(chip->res_mc_base);
829         }
830 }
831
832 static int __devinit snd_opti9xx_probe(struct snd_card *card)
833 {
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;
845
846 #if defined(CS4231) || defined(OPTi93X)
847         xdma2 = dma2;
848 #else
849         xdma2 = -1;
850 #endif
851
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;
857                 }
858         }
859         error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
860                                       mpu_port, mpu_irq);
861         if (error)
862                 return error;
863
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 #ifdef OPTi93X
874         chip->codec = codec;
875 #endif
876         error = snd_wss_pcm(codec, 0, &pcm);
877         if (error < 0)
878                 return error;
879         error = snd_wss_mixer(codec);
880         if (error < 0)
881                 return error;
882 #ifdef OPTi93X
883         error = snd_opti93x_mixer(codec);
884         if (error < 0)
885                 return error;
886 #endif
887 #ifdef CS4231
888         error = snd_wss_timer(codec, 0, &timer);
889         if (error < 0)
890                 return error;
891 #endif
892 #ifdef OPTi93X
893         error = request_irq(irq, snd_opti93x_interrupt,
894                             0, DEV_NAME" - WSS", chip);
895         if (error < 0) {
896                 snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", irq);
897                 return error;
898         }
899 #endif
900         chip->irq = irq;
901         strcpy(card->driver, chip->name);
902         sprintf(card->shortname, "OPTi %s", card->driver);
903 #if defined(CS4231) || defined(OPTi93X)
904         sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d&%d",
905                 card->shortname, pcm->name,
906                 chip->wss_base + 4, irq, dma1, xdma2);
907 #else
908         sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d",
909                 card->shortname, pcm->name, chip->wss_base + 4, irq, dma1);
910 #endif  /* CS4231 || OPTi93X */
911
912         if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
913                 rmidi = NULL;
914         else {
915                 error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
916                                 mpu_port, 0, mpu_irq, &rmidi);
917                 if (error)
918                         snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
919                                    mpu_port);
920         }
921
922         if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
923                 struct snd_opl3 *opl3 = NULL;
924 #ifndef OPTi93X
925                 if (chip->hardware == OPTi9XX_HW_82C928 ||
926                     chip->hardware == OPTi9XX_HW_82C929 ||
927                     chip->hardware == OPTi9XX_HW_82C924) {
928                         struct snd_opl4 *opl4;
929                         /* assume we have an OPL4 */
930                         snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
931                                                0x20, 0x20);
932                         if (snd_opl4_create(card, fm_port, fm_port - 8,
933                                             2, &opl3, &opl4) < 0) {
934                                 /* no luck, use OPL3 instead */
935                                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
936                                                        0x00, 0x20);
937                         }
938                 }
939 #endif  /* !OPTi93X */
940                 if (!opl3 && snd_opl3_create(card, fm_port, fm_port + 2,
941                                              OPL3_HW_AUTO, 0, &opl3) < 0) {
942                         snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n",
943                                    fm_port, fm_port + 4 - 1);
944                 }
945                 if (opl3) {
946                         error = snd_opl3_hwdep_new(opl3, 0, 1, &synth);
947                         if (error < 0)
948                                 return error;
949                 }
950         }
951
952         return snd_card_register(card);
953 }
954
955 static int snd_opti9xx_card_new(struct snd_card **cardp)
956 {
957         struct snd_card *card;
958         int err;
959
960         err = snd_card_create(index, id, THIS_MODULE,
961                               sizeof(struct snd_opti9xx), &card);
962         if (err < 0)
963                 return err;
964         card->private_free = snd_card_opti9xx_free;
965         *cardp = card;
966         return 0;
967 }
968
969 static int __devinit snd_opti9xx_isa_match(struct device *devptr,
970                                            unsigned int dev)
971 {
972 #ifdef CONFIG_PNP
973         if (snd_opti9xx_pnp_is_probed)
974                 return 0;
975         if (isapnp)
976                 return 0;
977 #endif
978         return 1;
979 }
980
981 static int __devinit snd_opti9xx_isa_probe(struct device *devptr,
982                                            unsigned int dev)
983 {
984         struct snd_card *card;
985         int error;
986         static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
987 #ifdef OPTi93X
988         static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
989 #else
990         static int possible_irqs[] = {9, 10, 11, 7, -1};
991 #endif  /* OPTi93X */
992         static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
993         static int possible_dma1s[] = {3, 1, 0, -1};
994 #if defined(CS4231) || defined(OPTi93X)
995         static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
996 #endif  /* CS4231 || OPTi93X */
997
998         if (mpu_port == SNDRV_AUTO_PORT) {
999                 if ((mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
1000                         snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
1001                         return -EBUSY;
1002                 }
1003         }
1004         if (irq == SNDRV_AUTO_IRQ) {
1005                 if ((irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
1006                         snd_printk(KERN_ERR "unable to find a free IRQ\n");
1007                         return -EBUSY;
1008                 }
1009         }
1010         if (mpu_irq == SNDRV_AUTO_IRQ) {
1011                 if ((mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
1012                         snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
1013                         return -EBUSY;
1014                 }
1015         }
1016         if (dma1 == SNDRV_AUTO_DMA) {
1017                 if ((dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
1018                         snd_printk(KERN_ERR "unable to find a free DMA1\n");
1019                         return -EBUSY;
1020                 }
1021         }
1022 #if defined(CS4231) || defined(OPTi93X)
1023         if (dma2 == SNDRV_AUTO_DMA) {
1024                 if ((dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4])) < 0) {
1025                         snd_printk(KERN_ERR "unable to find a free DMA2\n");
1026                         return -EBUSY;
1027                 }
1028         }
1029 #endif
1030
1031         error = snd_opti9xx_card_new(&card);
1032         if (error < 0)
1033                 return error;
1034
1035         if ((error = snd_card_opti9xx_detect(card, card->private_data)) < 0) {
1036                 snd_card_free(card);
1037                 return error;
1038         }
1039         snd_card_set_dev(card, devptr);
1040         if ((error = snd_opti9xx_probe(card)) < 0) {
1041                 snd_card_free(card);
1042                 return error;
1043         }
1044         dev_set_drvdata(devptr, card);
1045         return 0;
1046 }
1047
1048 static int __devexit snd_opti9xx_isa_remove(struct device *devptr,
1049                                             unsigned int dev)
1050 {
1051         snd_card_free(dev_get_drvdata(devptr));
1052         dev_set_drvdata(devptr, NULL);
1053         return 0;
1054 }
1055
1056 static struct isa_driver snd_opti9xx_driver = {
1057         .match          = snd_opti9xx_isa_match,
1058         .probe          = snd_opti9xx_isa_probe,
1059         .remove         = __devexit_p(snd_opti9xx_isa_remove),
1060         /* FIXME: suspend/resume */
1061         .driver         = {
1062                 .name   = DEV_NAME
1063         },
1064 };
1065
1066 #ifdef CONFIG_PNP
1067 static int __devinit snd_opti9xx_pnp_probe(struct pnp_card_link *pcard,
1068                                            const struct pnp_card_device_id *pid)
1069 {
1070         struct snd_card *card;
1071         int error, hw;
1072         struct snd_opti9xx *chip;
1073
1074         if (snd_opti9xx_pnp_is_probed)
1075                 return -EBUSY;
1076         if (! isapnp)
1077                 return -ENODEV;
1078         error = snd_opti9xx_card_new(&card);
1079         if (error < 0)
1080                 return error;
1081         chip = card->private_data;
1082
1083         hw = snd_card_opti9xx_pnp(chip, pcard, pid);
1084         switch (hw) {
1085         case 0x0924:
1086                 hw = OPTi9XX_HW_82C924;
1087                 break;
1088         case 0x0925:
1089                 hw = OPTi9XX_HW_82C925;
1090                 break;
1091         case 0x0931:
1092                 hw = OPTi9XX_HW_82C931;
1093                 break;
1094         default:
1095                 snd_card_free(card);
1096                 return -ENODEV;
1097         }
1098
1099         if ((error = snd_opti9xx_init(chip, hw))) {
1100                 snd_card_free(card);
1101                 return error;
1102         }
1103         error = snd_opti9xx_read_check(chip);
1104         if (error) {
1105                 snd_printk(KERN_ERR "OPTI chip not found\n");
1106                 snd_card_free(card);
1107                 return error;
1108         }
1109         snd_card_set_dev(card, &pcard->card->dev);
1110         if ((error = snd_opti9xx_probe(card)) < 0) {
1111                 snd_card_free(card);
1112                 return error;
1113         }
1114         pnp_set_card_drvdata(pcard, card);
1115         snd_opti9xx_pnp_is_probed = 1;
1116         return 0;
1117 }
1118
1119 static void __devexit snd_opti9xx_pnp_remove(struct pnp_card_link * pcard)
1120 {
1121         snd_card_free(pnp_get_card_drvdata(pcard));
1122         pnp_set_card_drvdata(pcard, NULL);
1123         snd_opti9xx_pnp_is_probed = 0;
1124 }
1125
1126 static struct pnp_card_driver opti9xx_pnpc_driver = {
1127         .flags          = PNP_DRIVER_RES_DISABLE,
1128         .name           = "opti9xx",
1129         .id_table       = snd_opti9xx_pnpids,
1130         .probe          = snd_opti9xx_pnp_probe,
1131         .remove         = __devexit_p(snd_opti9xx_pnp_remove),
1132 };
1133 #endif
1134
1135 #ifdef OPTi93X
1136 #define CHIP_NAME       "82C93x"
1137 #else
1138 #define CHIP_NAME       "82C92x"
1139 #endif
1140
1141 static int __init alsa_card_opti9xx_init(void)
1142 {
1143 #ifdef CONFIG_PNP
1144         pnp_register_card_driver(&opti9xx_pnpc_driver);
1145         if (snd_opti9xx_pnp_is_probed)
1146                 return 0;
1147         pnp_unregister_card_driver(&opti9xx_pnpc_driver);
1148 #endif
1149         return isa_register_driver(&snd_opti9xx_driver, 1);
1150 }
1151
1152 static void __exit alsa_card_opti9xx_exit(void)
1153 {
1154         if (!snd_opti9xx_pnp_is_probed) {
1155                 isa_unregister_driver(&snd_opti9xx_driver);
1156                 return;
1157         }
1158 #ifdef CONFIG_PNP
1159         pnp_unregister_card_driver(&opti9xx_pnpc_driver);
1160 #endif
1161 }
1162
1163 module_init(alsa_card_opti9xx_init)
1164 module_exit(alsa_card_opti9xx_exit)