GNU Linux-libre 5.4.257-gnu1
[releases.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 static inline void snd_opti9xx_write_mask(struct snd_opti9xx *chip,
321                 unsigned char reg, unsigned char value, unsigned char mask)
322 {
323         unsigned char oldval = snd_opti9xx_read(chip, reg);
324
325         snd_opti9xx_write(chip, reg, (oldval & ~mask) | (value & mask));
326 }
327
328 static int snd_opti9xx_configure(struct snd_opti9xx *chip,
329                                            long port,
330                                            int irq, int dma1, int dma2,
331                                            long mpu_port, int mpu_irq)
332 {
333         unsigned char wss_base_bits;
334         unsigned char irq_bits;
335         unsigned char dma_bits;
336         unsigned char mpu_port_bits = 0;
337         unsigned char mpu_irq_bits;
338
339         switch (chip->hardware) {
340 #ifndef OPTi93X
341         case OPTi9XX_HW_82C924:
342                 /* opti 929 mode (?), OPL3 clock output, audio enable */
343                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc);
344                 /* enable wave audio */
345                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
346                 /* Fall through */
347
348         case OPTi9XX_HW_82C925:
349                 /* enable WSS mode */
350                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
351                 /* OPL3 FM synthesis */
352                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
353                 /* disable Sound Blaster IRQ and DMA */
354                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
355 #ifdef CS4231
356                 /* cs4231/4248 fix enabled */
357                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
358 #else
359                 /* cs4231/4248 fix disabled */
360                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
361 #endif  /* CS4231 */
362                 break;
363
364         case OPTi9XX_HW_82C928:
365         case OPTi9XX_HW_82C929:
366                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
367                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
368                 /*
369                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae);
370                 */
371                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
372 #ifdef CS4231
373                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
374 #else
375                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
376 #endif  /* CS4231 */
377                 break;
378
379 #else   /* OPTi93X */
380         case OPTi9XX_HW_82C931:
381                 /* disable 3D sound (set GPIO1 as output, low) */
382                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(20), 0x04, 0x0c);
383                 /* fall through */
384         case OPTi9XX_HW_82C933:
385                 /*
386                  * The BTC 1817DW has QS1000 wavetable which is connected
387                  * to the serial digital input of the OPTI931.
388                  */
389                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(21), 0x82, 0xff);
390                 /* 
391                  * This bit sets OPTI931 to automaticaly select FM
392                  * or digital input signal.
393                  */
394                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(26), 0x01, 0x01);
395                 /* fall through */
396         case OPTi9XX_HW_82C930:
397                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03);
398                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff);
399                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 |
400                         (chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04),
401                         0x34);
402                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf);
403                 break;
404 #endif  /* OPTi93X */
405
406         default:
407                 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
408                 return -EINVAL;
409         }
410
411         /* PnP resource says it decodes only 10 bits of address */
412         switch (port & 0x3ff) {
413         case 0x130:
414                 chip->wss_base = 0x530;
415                 wss_base_bits = 0x00;
416                 break;
417         case 0x204:
418                 chip->wss_base = 0x604;
419                 wss_base_bits = 0x03;
420                 break;
421         case 0x280:
422                 chip->wss_base = 0xe80;
423                 wss_base_bits = 0x01;
424                 break;
425         case 0x340:
426                 chip->wss_base = 0xf40;
427                 wss_base_bits = 0x02;
428                 break;
429         default:
430                 snd_printk(KERN_WARNING "WSS port 0x%lx not valid\n", port);
431                 goto __skip_base;
432         }
433         snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
434
435 __skip_base:
436         switch (irq) {
437 //#ifdef OPTi93X
438         case 5:
439                 irq_bits = 0x05;
440                 break;
441 //#endif        /* OPTi93X */
442         case 7:
443                 irq_bits = 0x01;
444                 break;
445         case 9:
446                 irq_bits = 0x02;
447                 break;
448         case 10:
449                 irq_bits = 0x03;
450                 break;
451         case 11:
452                 irq_bits = 0x04;
453                 break;
454         default:
455                 snd_printk(KERN_WARNING "WSS irq # %d not valid\n", irq);
456                 goto __skip_resources;
457         }
458
459         switch (dma1) {
460         case 0:
461                 dma_bits = 0x01;
462                 break;
463         case 1:
464                 dma_bits = 0x02;
465                 break;
466         case 3:
467                 dma_bits = 0x03;
468                 break;
469         default:
470                 snd_printk(KERN_WARNING "WSS dma1 # %d not valid\n", dma1);
471                 goto __skip_resources;
472         }
473
474 #if defined(CS4231) || defined(OPTi93X)
475         if (dma1 == dma2) {
476                 snd_printk(KERN_ERR "don't want to share dmas\n");
477                 return -EBUSY;
478         }
479
480         switch (dma2) {
481         case 0:
482         case 1:
483                 break;
484         default:
485                 snd_printk(KERN_WARNING "WSS dma2 # %d not valid\n", dma2);
486                 goto __skip_resources;
487         }
488         dma_bits |= 0x04;
489 #endif  /* CS4231 || OPTi93X */
490
491 #ifndef OPTi93X
492          outb(irq_bits << 3 | dma_bits, chip->wss_base);
493 #else /* OPTi93X */
494         snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits));
495 #endif /* OPTi93X */
496
497 __skip_resources:
498         if (chip->hardware > OPTi9XX_HW_82C928) {
499                 switch (mpu_port) {
500                 case 0:
501                 case -1:
502                         break;
503                 case 0x300:
504                         mpu_port_bits = 0x03;
505                         break;
506                 case 0x310:
507                         mpu_port_bits = 0x02;
508                         break;
509                 case 0x320:
510                         mpu_port_bits = 0x01;
511                         break;
512                 case 0x330:
513                         mpu_port_bits = 0x00;
514                         break;
515                 default:
516                         snd_printk(KERN_WARNING
517                                    "MPU-401 port 0x%lx not valid\n", mpu_port);
518                         goto __skip_mpu;
519                 }
520
521                 switch (mpu_irq) {
522                 case 5:
523                         mpu_irq_bits = 0x02;
524                         break;
525                 case 7:
526                         mpu_irq_bits = 0x03;
527                         break;
528                 case 9:
529                         mpu_irq_bits = 0x00;
530                         break;
531                 case 10:
532                         mpu_irq_bits = 0x01;
533                         break;
534                 default:
535                         snd_printk(KERN_WARNING "MPU-401 irq # %d not valid\n",
536                                 mpu_irq);
537                         goto __skip_mpu;
538                 }
539
540                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6),
541                         (mpu_port <= 0) ? 0x00 :
542                                 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
543                         0xf8);
544         }
545 __skip_mpu:
546
547         return 0;
548 }
549
550 #ifdef OPTi93X
551
552 static const DECLARE_TLV_DB_SCALE(db_scale_5bit_3db_step, -9300, 300, 0);
553 static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0);
554 static const DECLARE_TLV_DB_SCALE(db_scale_4bit_12db_max, -3300, 300, 0);
555
556 static struct snd_kcontrol_new snd_opti93x_controls[] = {
557 WSS_DOUBLE("Master Playback Switch", 0,
558                 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
559 WSS_DOUBLE_TLV("Master Playback Volume", 0,
560                 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1,
561                 db_scale_5bit_3db_step),
562 WSS_DOUBLE_TLV("PCM Playback Volume", 0,
563                 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1,
564                 db_scale_5bit),
565 WSS_DOUBLE_TLV("FM Playback Volume", 0,
566                 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1,
567                 db_scale_4bit_12db_max),
568 WSS_DOUBLE("Line Playback Switch", 0,
569                 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
570 WSS_DOUBLE_TLV("Line Playback Volume", 0,
571                 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1,
572                 db_scale_4bit_12db_max),
573 WSS_DOUBLE("Mic Playback Switch", 0,
574                 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
575 WSS_DOUBLE_TLV("Mic Playback Volume", 0,
576                 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1,
577                 db_scale_4bit_12db_max),
578 WSS_DOUBLE_TLV("CD Playback Volume", 0,
579                 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1,
580                 db_scale_4bit_12db_max),
581 WSS_DOUBLE("Aux Playback Switch", 0,
582                 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
583 WSS_DOUBLE_TLV("Aux Playback Volume", 0,
584                 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1,
585                 db_scale_4bit_12db_max),
586 };
587
588 static int snd_opti93x_mixer(struct snd_wss *chip)
589 {
590         struct snd_card *card;
591         unsigned int idx;
592         struct snd_ctl_elem_id id1, id2;
593         int err;
594
595         if (snd_BUG_ON(!chip || !chip->pcm))
596                 return -EINVAL;
597
598         card = chip->card;
599
600         strcpy(card->mixername, chip->pcm->name);
601
602         memset(&id1, 0, sizeof(id1));
603         memset(&id2, 0, sizeof(id2));
604         id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
605         /* reassign AUX0 switch to CD */
606         strcpy(id1.name, "Aux Playback Switch");
607         strcpy(id2.name, "CD Playback Switch");
608         err = snd_ctl_rename_id(card, &id1, &id2);
609         if (err < 0) {
610                 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
611                 return err;
612         }
613         /* reassign AUX1 switch to FM */
614         strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
615         strcpy(id2.name, "FM Playback Switch");
616         err = snd_ctl_rename_id(card, &id1, &id2);
617         if (err < 0) {
618                 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
619                 return err;
620         }
621         /* remove AUX1 volume */
622         strcpy(id1.name, "Aux Playback Volume"); id1.index = 1;
623         snd_ctl_remove_id(card, &id1);
624
625         /* Replace WSS volume controls with OPTi93x volume controls */
626         id1.index = 0;
627         for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
628                 strcpy(id1.name, snd_opti93x_controls[idx].name);
629                 snd_ctl_remove_id(card, &id1);
630
631                 err = snd_ctl_add(card,
632                                 snd_ctl_new1(&snd_opti93x_controls[idx], chip));
633                 if (err < 0)
634                         return err;
635         }
636         return 0;
637 }
638
639 static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id)
640 {
641         struct snd_opti9xx *chip = dev_id;
642         struct snd_wss *codec = chip->codec;
643         unsigned char status;
644
645         if (!codec)
646                 return IRQ_HANDLED;
647
648         status = snd_opti9xx_read(chip, OPTi9XX_MC_REG(11));
649         if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
650                 snd_pcm_period_elapsed(codec->playback_substream);
651         if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
652                 snd_wss_overrange(codec);
653                 snd_pcm_period_elapsed(codec->capture_substream);
654         }
655         outb(0x00, OPTi93X_PORT(codec, STATUS));
656         return IRQ_HANDLED;
657 }
658
659 #endif /* OPTi93X */
660
661 static int snd_opti9xx_read_check(struct snd_opti9xx *chip)
662 {
663         unsigned char value;
664 #ifdef OPTi93X
665         unsigned long flags;
666 #endif
667
668         chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
669                                            "OPTi9xx MC");
670         if (chip->res_mc_base == NULL)
671                 return -EBUSY;
672 #ifndef OPTi93X
673         value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
674         if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1)))
675                 if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
676                         return 0;
677 #else   /* OPTi93X */
678         chip->res_mc_indir = request_region(chip->mc_indir_index, 2,
679                                             "OPTi93x MC");
680         if (chip->res_mc_indir == NULL)
681                 return -EBUSY;
682
683         spin_lock_irqsave(&chip->lock, flags);
684         outb(chip->password, chip->mc_base + chip->pwd_reg);
685         outb(((chip->mc_indir_index & 0x1f0) >> 4), chip->mc_base);
686         spin_unlock_irqrestore(&chip->lock, flags);
687
688         value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
689         snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
690         if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
691                 return 0;
692
693         release_and_free_resource(chip->res_mc_indir);
694         chip->res_mc_indir = NULL;
695 #endif  /* OPTi93X */
696         release_and_free_resource(chip->res_mc_base);
697         chip->res_mc_base = NULL;
698
699         return -ENODEV;
700 }
701
702 static int snd_card_opti9xx_detect(struct snd_card *card,
703                                    struct snd_opti9xx *chip)
704 {
705         int i, err;
706
707 #ifndef OPTi93X
708         for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
709 #else
710         for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
711 #endif
712                 err = snd_opti9xx_init(chip, i);
713                 if (err < 0)
714                         return err;
715
716                 err = snd_opti9xx_read_check(chip);
717                 if (err == 0)
718                         return 1;
719 #ifdef OPTi93X
720                 chip->mc_indir_index = 0;
721 #endif
722         }
723         return -ENODEV;
724 }
725
726 #ifdef CONFIG_PNP
727 static int snd_card_opti9xx_pnp(struct snd_opti9xx *chip,
728                                 struct pnp_card_link *card,
729                                 const struct pnp_card_device_id *pid)
730 {
731         struct pnp_dev *pdev;
732         int err;
733         struct pnp_dev *devmpu;
734 #ifndef OPTi93X
735         struct pnp_dev *devmc;
736 #endif
737
738         pdev = pnp_request_card_device(card, pid->devs[0].id, NULL);
739         if (pdev == NULL)
740                 return -EBUSY;
741
742         err = pnp_activate_dev(pdev);
743         if (err < 0) {
744                 snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
745                 return err;
746         }
747
748 #ifdef OPTi93X
749         port = pnp_port_start(pdev, 0) - 4;
750         fm_port = pnp_port_start(pdev, 1) + 8;
751         /* adjust mc_indir_index - some cards report it at 0xe?d,
752            other at 0xe?c but it really is always at 0xe?e */
753         chip->mc_indir_index = (pnp_port_start(pdev, 3) & ~0xf) | 0xe;
754 #else
755         devmc = pnp_request_card_device(card, pid->devs[2].id, NULL);
756         if (devmc == NULL)
757                 return -EBUSY;
758
759         err = pnp_activate_dev(devmc);
760         if (err < 0) {
761                 snd_printk(KERN_ERR "MC pnp configure failure: %d\n", err);
762                 return err;
763         }
764
765         port = pnp_port_start(pdev, 1);
766         fm_port = pnp_port_start(pdev, 2) + 8;
767         /*
768          * The MC(0) is never accessed and card does not
769          * include it in the PnP resource range. OPTI93x include it.
770          */
771         chip->mc_base = pnp_port_start(devmc, 0) - 1;
772         chip->mc_base_size = pnp_port_len(devmc, 0) + 1;
773 #endif  /* OPTi93X */
774         irq = pnp_irq(pdev, 0);
775         dma1 = pnp_dma(pdev, 0);
776 #if defined(CS4231) || defined(OPTi93X)
777         dma2 = pnp_dma(pdev, 1);
778 #endif  /* CS4231 || OPTi93X */
779
780         devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
781
782         if (devmpu && mpu_port > 0) {
783                 err = pnp_activate_dev(devmpu);
784                 if (err < 0) {
785                         snd_printk(KERN_ERR "MPU401 pnp configure failure\n");
786                         mpu_port = -1;
787                 } else {
788                         mpu_port = pnp_port_start(devmpu, 0);
789                         mpu_irq = pnp_irq(devmpu, 0);
790                 }
791         }
792         return pid->driver_data;
793 }
794 #endif  /* CONFIG_PNP */
795
796 static void snd_card_opti9xx_free(struct snd_card *card)
797 {
798         struct snd_opti9xx *chip = card->private_data;
799
800         if (chip) {
801 #ifdef OPTi93X
802                 if (chip->irq > 0) {
803                         disable_irq(chip->irq);
804                         free_irq(chip->irq, chip);
805                 }
806                 release_and_free_resource(chip->res_mc_indir);
807 #endif
808                 release_and_free_resource(chip->res_mc_base);
809         }
810 }
811
812 static int snd_opti9xx_probe(struct snd_card *card)
813 {
814         static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
815         int error;
816         int xdma2;
817         struct snd_opti9xx *chip = card->private_data;
818         struct snd_wss *codec;
819         struct snd_rawmidi *rmidi;
820         struct snd_hwdep *synth;
821
822 #if defined(CS4231) || defined(OPTi93X)
823         xdma2 = dma2;
824 #else
825         xdma2 = -1;
826 #endif
827
828         if (port == SNDRV_AUTO_PORT) {
829                 port = snd_legacy_find_free_ioport(possible_ports, 4);
830                 if (port < 0) {
831                         snd_printk(KERN_ERR "unable to find a free WSS port\n");
832                         return -EBUSY;
833                 }
834         }
835         error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
836                                       mpu_port, mpu_irq);
837         if (error)
838                 return error;
839
840         error = snd_wss_create(card, chip->wss_base + 4, -1, irq, dma1, xdma2,
841 #ifdef OPTi93X
842                                WSS_HW_OPTI93X, WSS_HWSHARE_IRQ,
843 #else
844                                WSS_HW_DETECT, 0,
845 #endif
846                                &codec);
847         if (error < 0)
848                 return error;
849         chip->codec = codec;
850         error = snd_wss_pcm(codec, 0);
851         if (error < 0)
852                 return error;
853         error = snd_wss_mixer(codec);
854         if (error < 0)
855                 return error;
856 #ifdef OPTi93X
857         error = snd_opti93x_mixer(codec);
858         if (error < 0)
859                 return error;
860 #endif
861 #ifdef CS4231
862         error = snd_wss_timer(codec, 0);
863         if (error < 0)
864                 return error;
865 #endif
866 #ifdef OPTi93X
867         error = request_irq(irq, snd_opti93x_interrupt,
868                             0, DEV_NAME" - WSS", chip);
869         if (error < 0) {
870                 snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", irq);
871                 return error;
872         }
873 #endif
874         chip->irq = irq;
875         strcpy(card->driver, chip->name);
876         sprintf(card->shortname, "OPTi %s", card->driver);
877 #if defined(CS4231) || defined(OPTi93X)
878         snprintf(card->longname, sizeof(card->longname),
879                  "%s, %s at 0x%lx, irq %d, dma %d&%d",
880                  card->shortname, codec->pcm->name,
881                  chip->wss_base + 4, irq, dma1, xdma2);
882 #else
883         snprintf(card->longname, sizeof(card->longname),
884                  "%s, %s at 0x%lx, irq %d, dma %d",
885                  card->shortname, codec->pcm->name, chip->wss_base + 4, irq,
886                  dma1);
887 #endif  /* CS4231 || OPTi93X */
888
889         if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
890                 rmidi = NULL;
891         else {
892                 error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
893                                 mpu_port, 0, mpu_irq, &rmidi);
894                 if (error)
895                         snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
896                                    mpu_port);
897         }
898
899         if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
900                 struct snd_opl3 *opl3 = NULL;
901 #ifndef OPTi93X
902                 if (chip->hardware == OPTi9XX_HW_82C928 ||
903                     chip->hardware == OPTi9XX_HW_82C929 ||
904                     chip->hardware == OPTi9XX_HW_82C924) {
905                         struct snd_opl4 *opl4;
906                         /* assume we have an OPL4 */
907                         snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
908                                                0x20, 0x20);
909                         if (snd_opl4_create(card, fm_port, fm_port - 8,
910                                             2, &opl3, &opl4) < 0) {
911                                 /* no luck, use OPL3 instead */
912                                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
913                                                        0x00, 0x20);
914                         }
915                 }
916 #endif  /* !OPTi93X */
917                 if (!opl3 && snd_opl3_create(card, fm_port, fm_port + 2,
918                                              OPL3_HW_AUTO, 0, &opl3) < 0) {
919                         snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n",
920                                    fm_port, fm_port + 4 - 1);
921                 }
922                 if (opl3) {
923                         error = snd_opl3_hwdep_new(opl3, 0, 1, &synth);
924                         if (error < 0)
925                                 return error;
926                 }
927         }
928
929         return snd_card_register(card);
930 }
931
932 static int snd_opti9xx_card_new(struct device *pdev, struct snd_card **cardp)
933 {
934         struct snd_card *card;
935         int err;
936
937         err = snd_card_new(pdev, index, id, THIS_MODULE,
938                            sizeof(struct snd_opti9xx), &card);
939         if (err < 0)
940                 return err;
941         card->private_free = snd_card_opti9xx_free;
942         *cardp = card;
943         return 0;
944 }
945
946 static int snd_opti9xx_isa_match(struct device *devptr,
947                                  unsigned int dev)
948 {
949 #ifdef CONFIG_PNP
950         if (snd_opti9xx_pnp_is_probed)
951                 return 0;
952         if (isapnp)
953                 return 0;
954 #endif
955         return 1;
956 }
957
958 static int snd_opti9xx_isa_probe(struct device *devptr,
959                                  unsigned int dev)
960 {
961         struct snd_card *card;
962         int error;
963         static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
964 #ifdef OPTi93X
965         static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
966 #else
967         static int possible_irqs[] = {9, 10, 11, 7, -1};
968 #endif  /* OPTi93X */
969         static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
970         static int possible_dma1s[] = {3, 1, 0, -1};
971 #if defined(CS4231) || defined(OPTi93X)
972         static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
973 #endif  /* CS4231 || OPTi93X */
974
975         if (mpu_port == SNDRV_AUTO_PORT) {
976                 if ((mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
977                         snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
978                         return -EBUSY;
979                 }
980         }
981         if (irq == SNDRV_AUTO_IRQ) {
982                 if ((irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
983                         snd_printk(KERN_ERR "unable to find a free IRQ\n");
984                         return -EBUSY;
985                 }
986         }
987         if (mpu_irq == SNDRV_AUTO_IRQ) {
988                 if ((mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
989                         snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
990                         return -EBUSY;
991                 }
992         }
993         if (dma1 == SNDRV_AUTO_DMA) {
994                 if ((dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
995                         snd_printk(KERN_ERR "unable to find a free DMA1\n");
996                         return -EBUSY;
997                 }
998         }
999 #if defined(CS4231) || defined(OPTi93X)
1000         if (dma2 == SNDRV_AUTO_DMA) {
1001                 if ((dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4])) < 0) {
1002                         snd_printk(KERN_ERR "unable to find a free DMA2\n");
1003                         return -EBUSY;
1004                 }
1005         }
1006 #endif
1007
1008         error = snd_opti9xx_card_new(devptr, &card);
1009         if (error < 0)
1010                 return error;
1011
1012         if ((error = snd_card_opti9xx_detect(card, card->private_data)) < 0) {
1013                 snd_card_free(card);
1014                 return error;
1015         }
1016         if ((error = snd_opti9xx_probe(card)) < 0) {
1017                 snd_card_free(card);
1018                 return error;
1019         }
1020         dev_set_drvdata(devptr, card);
1021         return 0;
1022 }
1023
1024 static int snd_opti9xx_isa_remove(struct device *devptr,
1025                                   unsigned int dev)
1026 {
1027         snd_card_free(dev_get_drvdata(devptr));
1028         return 0;
1029 }
1030
1031 #ifdef CONFIG_PM
1032 static int snd_opti9xx_suspend(struct snd_card *card)
1033 {
1034         struct snd_opti9xx *chip = card->private_data;
1035
1036         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1037         chip->codec->suspend(chip->codec);
1038         return 0;
1039 }
1040
1041 static int snd_opti9xx_resume(struct snd_card *card)
1042 {
1043         struct snd_opti9xx *chip = card->private_data;
1044         int error, xdma2;
1045 #if defined(CS4231) || defined(OPTi93X)
1046         xdma2 = dma2;
1047 #else
1048         xdma2 = -1;
1049 #endif
1050
1051         error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
1052                                       mpu_port, mpu_irq);
1053         if (error)
1054                 return error;
1055         chip->codec->resume(chip->codec);
1056         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1057         return 0;
1058 }
1059
1060 static int snd_opti9xx_isa_suspend(struct device *dev, unsigned int n,
1061                                    pm_message_t state)
1062 {
1063         return snd_opti9xx_suspend(dev_get_drvdata(dev));
1064 }
1065
1066 static int snd_opti9xx_isa_resume(struct device *dev, unsigned int n)
1067 {
1068         return snd_opti9xx_resume(dev_get_drvdata(dev));
1069 }
1070 #endif
1071
1072 static struct isa_driver snd_opti9xx_driver = {
1073         .match          = snd_opti9xx_isa_match,
1074         .probe          = snd_opti9xx_isa_probe,
1075         .remove         = snd_opti9xx_isa_remove,
1076 #ifdef CONFIG_PM
1077         .suspend        = snd_opti9xx_isa_suspend,
1078         .resume         = snd_opti9xx_isa_resume,
1079 #endif
1080         .driver         = {
1081                 .name   = DEV_NAME
1082         },
1083 };
1084
1085 #ifdef CONFIG_PNP
1086 static int snd_opti9xx_pnp_probe(struct pnp_card_link *pcard,
1087                                  const struct pnp_card_device_id *pid)
1088 {
1089         struct snd_card *card;
1090         int error, hw;
1091         struct snd_opti9xx *chip;
1092
1093         if (snd_opti9xx_pnp_is_probed)
1094                 return -EBUSY;
1095         if (! isapnp)
1096                 return -ENODEV;
1097         error = snd_opti9xx_card_new(&pcard->card->dev, &card);
1098         if (error < 0)
1099                 return error;
1100         chip = card->private_data;
1101
1102         hw = snd_card_opti9xx_pnp(chip, pcard, pid);
1103         switch (hw) {
1104         case 0x0924:
1105                 hw = OPTi9XX_HW_82C924;
1106                 break;
1107         case 0x0925:
1108                 hw = OPTi9XX_HW_82C925;
1109                 break;
1110         case 0x0931:
1111                 hw = OPTi9XX_HW_82C931;
1112                 break;
1113         default:
1114                 snd_card_free(card);
1115                 return -ENODEV;
1116         }
1117
1118         if ((error = snd_opti9xx_init(chip, hw))) {
1119                 snd_card_free(card);
1120                 return error;
1121         }
1122         error = snd_opti9xx_read_check(chip);
1123         if (error) {
1124                 snd_printk(KERN_ERR "OPTI chip not found\n");
1125                 snd_card_free(card);
1126                 return error;
1127         }
1128         if ((error = snd_opti9xx_probe(card)) < 0) {
1129                 snd_card_free(card);
1130                 return error;
1131         }
1132         pnp_set_card_drvdata(pcard, card);
1133         snd_opti9xx_pnp_is_probed = 1;
1134         return 0;
1135 }
1136
1137 static void snd_opti9xx_pnp_remove(struct pnp_card_link *pcard)
1138 {
1139         snd_card_free(pnp_get_card_drvdata(pcard));
1140         pnp_set_card_drvdata(pcard, NULL);
1141         snd_opti9xx_pnp_is_probed = 0;
1142 }
1143
1144 #ifdef CONFIG_PM
1145 static int snd_opti9xx_pnp_suspend(struct pnp_card_link *pcard,
1146                                    pm_message_t state)
1147 {
1148         return snd_opti9xx_suspend(pnp_get_card_drvdata(pcard));
1149 }
1150
1151 static int snd_opti9xx_pnp_resume(struct pnp_card_link *pcard)
1152 {
1153         return snd_opti9xx_resume(pnp_get_card_drvdata(pcard));
1154 }
1155 #endif
1156
1157 static struct pnp_card_driver opti9xx_pnpc_driver = {
1158         .flags          = PNP_DRIVER_RES_DISABLE,
1159         .name           = DEV_NAME,
1160         .id_table       = snd_opti9xx_pnpids,
1161         .probe          = snd_opti9xx_pnp_probe,
1162         .remove         = snd_opti9xx_pnp_remove,
1163 #ifdef CONFIG_PM
1164         .suspend        = snd_opti9xx_pnp_suspend,
1165         .resume         = snd_opti9xx_pnp_resume,
1166 #endif
1167 };
1168 #endif
1169
1170 #ifdef OPTi93X
1171 #define CHIP_NAME       "82C93x"
1172 #else
1173 #define CHIP_NAME       "82C92x"
1174 #endif
1175
1176 static int __init alsa_card_opti9xx_init(void)
1177 {
1178 #ifdef CONFIG_PNP
1179         pnp_register_card_driver(&opti9xx_pnpc_driver);
1180         if (snd_opti9xx_pnp_is_probed)
1181                 return 0;
1182         pnp_unregister_card_driver(&opti9xx_pnpc_driver);
1183 #endif
1184         return isa_register_driver(&snd_opti9xx_driver, 1);
1185 }
1186
1187 static void __exit alsa_card_opti9xx_exit(void)
1188 {
1189         if (!snd_opti9xx_pnp_is_probed) {
1190                 isa_unregister_driver(&snd_opti9xx_driver);
1191                 return;
1192         }
1193 #ifdef CONFIG_PNP
1194         pnp_unregister_card_driver(&opti9xx_pnpc_driver);
1195 #endif
1196 }
1197
1198 module_init(alsa_card_opti9xx_init)
1199 module_exit(alsa_card_opti9xx_exit)