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