media: dt-bindings: media: renesas,drif: Convert to json-schema
[platform/kernel/linux-starfive.git] / sound / isa / opl3sa2.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Driver for Yamaha OPL3-SA[2,3] soundcards
4  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5  */
6
7 #include <linux/init.h>
8 #include <linux/err.h>
9 #include <linux/isa.h>
10 #include <linux/interrupt.h>
11 #include <linux/pm.h>
12 #include <linux/pnp.h>
13 #include <linux/module.h>
14 #include <linux/io.h>
15 #include <sound/core.h>
16 #include <sound/wss.h>
17 #include <sound/mpu401.h>
18 #include <sound/opl3.h>
19 #include <sound/initval.h>
20 #include <sound/tlv.h>
21
22 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
23 MODULE_DESCRIPTION("Yamaha OPL3SA2+");
24 MODULE_LICENSE("GPL");
25 MODULE_SUPPORTED_DEVICE("{{Yamaha,YMF719E-S},"
26                 "{Genius,Sound Maker 3DX},"
27                 "{Yamaha,OPL3SA3},"
28                 "{Intel,AL440LX sound},"
29                 "{NeoMagic,MagicWave 3DX}}");
30
31 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
32 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
33 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
34 #ifdef CONFIG_PNP
35 static bool isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
36 #endif
37 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;     /* 0xf86,0x370,0x100 */
38 static long sb_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;  /* 0x220,0x240,0x260 */
39 static long wss_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;/* 0x530,0xe80,0xf40,0x604 */
40 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;  /* 0x388 */
41 static long midi_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;/* 0x330,0x300 */
42 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;        /* 0,1,3,5,9,11,12,15 */
43 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 1,3,5,6,7 */
44 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 1,3,5,6,7 */
45 static int opl3sa3_ymode[SNDRV_CARDS];   /* 0,1,2,3 */ /*SL Added*/
46
47 module_param_array(index, int, NULL, 0444);
48 MODULE_PARM_DESC(index, "Index value for OPL3-SA soundcard.");
49 module_param_array(id, charp, NULL, 0444);
50 MODULE_PARM_DESC(id, "ID string for OPL3-SA soundcard.");
51 module_param_array(enable, bool, NULL, 0444);
52 MODULE_PARM_DESC(enable, "Enable OPL3-SA soundcard.");
53 #ifdef CONFIG_PNP
54 module_param_array(isapnp, bool, NULL, 0444);
55 MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
56 #endif
57 module_param_hw_array(port, long, ioport, NULL, 0444);
58 MODULE_PARM_DESC(port, "Port # for OPL3-SA driver.");
59 module_param_hw_array(sb_port, long, ioport, NULL, 0444);
60 MODULE_PARM_DESC(sb_port, "SB port # for OPL3-SA driver.");
61 module_param_hw_array(wss_port, long, ioport, NULL, 0444);
62 MODULE_PARM_DESC(wss_port, "WSS port # for OPL3-SA driver.");
63 module_param_hw_array(fm_port, long, ioport, NULL, 0444);
64 MODULE_PARM_DESC(fm_port, "FM port # for OPL3-SA driver.");
65 module_param_hw_array(midi_port, long, ioport, NULL, 0444);
66 MODULE_PARM_DESC(midi_port, "MIDI port # for OPL3-SA driver.");
67 module_param_hw_array(irq, int, irq, NULL, 0444);
68 MODULE_PARM_DESC(irq, "IRQ # for OPL3-SA driver.");
69 module_param_hw_array(dma1, int, dma, NULL, 0444);
70 MODULE_PARM_DESC(dma1, "DMA1 # for OPL3-SA driver.");
71 module_param_hw_array(dma2, int, dma, NULL, 0444);
72 MODULE_PARM_DESC(dma2, "DMA2 # for OPL3-SA driver.");
73 module_param_array(opl3sa3_ymode, int, NULL, 0444);
74 MODULE_PARM_DESC(opl3sa3_ymode, "Speaker size selection for 3D Enhancement mode: Desktop/Large Notebook/Small Notebook/HiFi.");
75
76 #ifdef CONFIG_PNP
77 static int isa_registered;
78 static int pnp_registered;
79 static int pnpc_registered;
80 #endif
81
82 /* control ports */
83 #define OPL3SA2_PM_CTRL         0x01
84 #define OPL3SA2_SYS_CTRL                0x02
85 #define OPL3SA2_IRQ_CONFIG      0x03
86 #define OPL3SA2_IRQ_STATUS      0x04
87 #define OPL3SA2_DMA_CONFIG      0x06
88 #define OPL3SA2_MASTER_LEFT     0x07
89 #define OPL3SA2_MASTER_RIGHT    0x08
90 #define OPL3SA2_MIC             0x09
91 #define OPL3SA2_MISC            0x0A
92
93 /* opl3sa3 only */
94 #define OPL3SA3_DGTL_DOWN       0x12
95 #define OPL3SA3_ANLG_DOWN       0x13
96 #define OPL3SA3_WIDE            0x14
97 #define OPL3SA3_BASS            0x15
98 #define OPL3SA3_TREBLE          0x16
99
100 /* power management bits */
101 #define OPL3SA2_PM_ADOWN                0x20
102 #define OPL3SA2_PM_PSV          0x04            
103 #define OPL3SA2_PM_PDN          0x02
104 #define OPL3SA2_PM_PDX          0x01
105
106 #define OPL3SA2_PM_D0   0x00
107 #define OPL3SA2_PM_D3   (OPL3SA2_PM_ADOWN|OPL3SA2_PM_PSV|OPL3SA2_PM_PDN|OPL3SA2_PM_PDX)
108
109 struct snd_opl3sa2 {
110         int version;            /* 2 or 3 */
111         unsigned long port;     /* control port */
112         struct resource *res_port; /* control port resource */
113         int irq;
114         int single_dma;
115         spinlock_t reg_lock;
116         struct snd_hwdep *synth;
117         struct snd_rawmidi *rmidi;
118         struct snd_wss *wss;
119         unsigned char ctlregs[0x20];
120         int ymode;              /* SL added */
121         struct snd_kcontrol *master_switch;
122         struct snd_kcontrol *master_volume;
123 };
124
125 #define PFX     "opl3sa2: "
126
127 #ifdef CONFIG_PNP
128
129 static const struct pnp_device_id snd_opl3sa2_pnpbiosids[] = {
130         { .id = "YMH0021" },
131         { .id = "NMX2210" },    /* Gateway Solo 2500 */
132         { .id = "" }            /* end */
133 };
134
135 MODULE_DEVICE_TABLE(pnp, snd_opl3sa2_pnpbiosids);
136
137 static const struct pnp_card_device_id snd_opl3sa2_pnpids[] = {
138         /* Yamaha YMF719E-S (Genius Sound Maker 3DX) */
139         { .id = "YMH0020", .devs = { { "YMH0021" } } },
140         /* Yamaha OPL3-SA3 (integrated on Intel's Pentium II AL440LX motherboard) */
141         { .id = "YMH0030", .devs = { { "YMH0021" } } },
142         /* Yamaha OPL3-SA2 */
143         { .id = "YMH0800", .devs = { { "YMH0021" } } },
144         /* Yamaha OPL3-SA2 */
145         { .id = "YMH0801", .devs = { { "YMH0021" } } },
146         /* NeoMagic MagicWave 3DX */
147         { .id = "NMX2200", .devs = { { "YMH2210" } } },
148         /* NeoMagic MagicWave 3D */
149         { .id = "NMX2200", .devs = { { "NMX2210" } } },
150         /* --- */
151         { .id = "" }    /* end */
152 };
153
154 MODULE_DEVICE_TABLE(pnp_card, snd_opl3sa2_pnpids);
155
156 #endif /* CONFIG_PNP */
157
158
159 /* read control port (w/o spinlock) */
160 static unsigned char __snd_opl3sa2_read(struct snd_opl3sa2 *chip, unsigned char reg)
161 {
162         unsigned char result;
163 #if 0
164         outb(0x1d, port);       /* password */
165         printk(KERN_DEBUG "read [0x%lx] = 0x%x\n", port, inb(port));
166 #endif
167         outb(reg, chip->port);  /* register */
168         result = inb(chip->port + 1);
169 #if 0
170         printk(KERN_DEBUG "read [0x%lx] = 0x%x [0x%x]\n",
171                port, result, inb(port));
172 #endif
173         return result;
174 }
175
176 /* read control port (with spinlock) */
177 static unsigned char snd_opl3sa2_read(struct snd_opl3sa2 *chip, unsigned char reg)
178 {
179         unsigned long flags;
180         unsigned char result;
181
182         spin_lock_irqsave(&chip->reg_lock, flags);
183         result = __snd_opl3sa2_read(chip, reg);
184         spin_unlock_irqrestore(&chip->reg_lock, flags);
185         return result;
186 }
187
188 /* write control port (w/o spinlock) */
189 static void __snd_opl3sa2_write(struct snd_opl3sa2 *chip, unsigned char reg, unsigned char value)
190 {
191 #if 0
192         outb(0x1d, port);       /* password */
193 #endif
194         outb(reg, chip->port);  /* register */
195         outb(value, chip->port + 1);
196         chip->ctlregs[reg] = value;
197 }
198
199 /* write control port (with spinlock) */
200 static void snd_opl3sa2_write(struct snd_opl3sa2 *chip, unsigned char reg, unsigned char value)
201 {
202         unsigned long flags;
203         spin_lock_irqsave(&chip->reg_lock, flags);
204         __snd_opl3sa2_write(chip, reg, value);
205         spin_unlock_irqrestore(&chip->reg_lock, flags);
206 }
207
208 static int snd_opl3sa2_detect(struct snd_card *card)
209 {
210         struct snd_opl3sa2 *chip = card->private_data;
211         unsigned long port;
212         unsigned char tmp, tmp1;
213         char str[2];
214
215         port = chip->port;
216         if ((chip->res_port = request_region(port, 2, "OPL3-SA control")) == NULL) {
217                 snd_printk(KERN_ERR PFX "can't grab port 0x%lx\n", port);
218                 return -EBUSY;
219         }
220         /*
221         snd_printk(KERN_DEBUG "REG 0A = 0x%x\n",
222                    snd_opl3sa2_read(chip, 0x0a));
223         */
224         chip->version = 0;
225         tmp = snd_opl3sa2_read(chip, OPL3SA2_MISC);
226         if (tmp == 0xff) {
227                 snd_printd("OPL3-SA [0x%lx] detect = 0x%x\n", port, tmp);
228                 return -ENODEV;
229         }
230         switch (tmp & 0x07) {
231         case 0x01:
232                 chip->version = 2; /* YMF711 */
233                 break;
234         default:
235                 chip->version = 3;
236                 /* 0x02 - standard */
237                 /* 0x03 - YM715B */
238                 /* 0x04 - YM719 - OPL-SA4? */
239                 /* 0x05 - OPL3-SA3 - Libretto 100 */
240                 /* 0x07 - unknown - Neomagic MagicWave 3D */
241                 break;
242         }
243         str[0] = chip->version + '0';
244         str[1] = 0;
245         strcat(card->shortname, str);
246         snd_opl3sa2_write(chip, OPL3SA2_MISC, tmp ^ 7);
247         if ((tmp1 = snd_opl3sa2_read(chip, OPL3SA2_MISC)) != tmp) {
248                 snd_printd("OPL3-SA [0x%lx] detect (1) = 0x%x (0x%x)\n", port, tmp, tmp1);
249                 return -ENODEV;
250         }
251         /* try if the MIC register is accessible */
252         tmp = snd_opl3sa2_read(chip, OPL3SA2_MIC);
253         snd_opl3sa2_write(chip, OPL3SA2_MIC, 0x8a);
254         if (((tmp1 = snd_opl3sa2_read(chip, OPL3SA2_MIC)) & 0x9f) != 0x8a) {
255                 snd_printd("OPL3-SA [0x%lx] detect (2) = 0x%x (0x%x)\n", port, tmp, tmp1);
256                 return -ENODEV;
257         }
258         snd_opl3sa2_write(chip, OPL3SA2_MIC, 0x9f);
259         /* initialization */
260         /* Power Management - full on */
261         snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D0);
262         if (chip->version > 2) {
263                 /* ymode is bits 4&5 (of 0 to 7) on all but opl3sa2 versions */
264                 snd_opl3sa2_write(chip, OPL3SA2_SYS_CTRL, (chip->ymode << 4));
265         } else {
266                 /* default for opl3sa2 versions */
267                 snd_opl3sa2_write(chip, OPL3SA2_SYS_CTRL, 0x00);
268         }
269         snd_opl3sa2_write(chip, OPL3SA2_IRQ_CONFIG, 0x0d);      /* Interrupt Channel Configuration - IRQ A = OPL3 + MPU + WSS */
270         if (chip->single_dma) {
271                 snd_opl3sa2_write(chip, OPL3SA2_DMA_CONFIG, 0x03);      /* DMA Configuration - DMA A = WSS-R + WSS-P */
272         } else {
273                 snd_opl3sa2_write(chip, OPL3SA2_DMA_CONFIG, 0x21);      /* DMA Configuration - DMA B = WSS-R, DMA A = WSS-P */
274         }
275         snd_opl3sa2_write(chip, OPL3SA2_MISC, 0x80 | (tmp & 7));        /* Miscellaneous - default */
276         if (chip->version > 2) {
277                 snd_opl3sa2_write(chip, OPL3SA3_DGTL_DOWN, 0x00);       /* Digital Block Partial Power Down - default */
278                 snd_opl3sa2_write(chip, OPL3SA3_ANLG_DOWN, 0x00);       /* Analog Block Partial Power Down - default */
279         }
280         return 0;
281 }
282
283 static irqreturn_t snd_opl3sa2_interrupt(int irq, void *dev_id)
284 {
285         unsigned short status;
286         struct snd_card *card = dev_id;
287         struct snd_opl3sa2 *chip;
288         int handled = 0;
289
290         if (card == NULL)
291                 return IRQ_NONE;
292
293         chip = card->private_data;
294         status = snd_opl3sa2_read(chip, OPL3SA2_IRQ_STATUS);
295
296         if (status & 0x20) {
297                 handled = 1;
298                 snd_opl3_interrupt(chip->synth);
299         }
300
301         if ((status & 0x10) && chip->rmidi != NULL) {
302                 handled = 1;
303                 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
304         }
305
306         if (status & 0x07) {    /* TI,CI,PI */
307                 handled = 1;
308                 snd_wss_interrupt(irq, chip->wss);
309         }
310
311         if (status & 0x40) { /* hardware volume change */
312                 handled = 1;
313                 /* reading from Master Lch register at 0x07 clears this bit */
314                 snd_opl3sa2_read(chip, OPL3SA2_MASTER_RIGHT);
315                 snd_opl3sa2_read(chip, OPL3SA2_MASTER_LEFT);
316                 if (chip->master_switch && chip->master_volume) {
317                         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
318                                         &chip->master_switch->id);
319                         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
320                                         &chip->master_volume->id);
321                 }
322         }
323         return IRQ_RETVAL(handled);
324 }
325
326 #define OPL3SA2_SINGLE(xname, xindex, reg, shift, mask, invert) \
327 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
328   .info = snd_wss_info_single, \
329   .get = snd_opl3sa2_get_single, .put = snd_opl3sa2_put_single, \
330   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
331 #define OPL3SA2_SINGLE_TLV(xname, xindex, reg, shift, mask, invert, xtlv) \
332 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
333   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
334   .name = xname, .index = xindex, \
335   .info = snd_wss_info_single, \
336   .get = snd_opl3sa2_get_single, .put = snd_opl3sa2_put_single, \
337   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24), \
338   .tlv = { .p = (xtlv) } }
339
340 static int snd_opl3sa2_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
341 {
342         struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
343         unsigned long flags;
344         int reg = kcontrol->private_value & 0xff;
345         int shift = (kcontrol->private_value >> 8) & 0xff;
346         int mask = (kcontrol->private_value >> 16) & 0xff;
347         int invert = (kcontrol->private_value >> 24) & 0xff;
348
349         spin_lock_irqsave(&chip->reg_lock, flags);
350         ucontrol->value.integer.value[0] = (chip->ctlregs[reg] >> shift) & mask;
351         spin_unlock_irqrestore(&chip->reg_lock, flags);
352         if (invert)
353                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
354         return 0;
355 }
356
357 static int snd_opl3sa2_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
358 {
359         struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
360         unsigned long flags;
361         int reg = kcontrol->private_value & 0xff;
362         int shift = (kcontrol->private_value >> 8) & 0xff;
363         int mask = (kcontrol->private_value >> 16) & 0xff;
364         int invert = (kcontrol->private_value >> 24) & 0xff;
365         int change;
366         unsigned short val, oval;
367         
368         val = (ucontrol->value.integer.value[0] & mask);
369         if (invert)
370                 val = mask - val;
371         val <<= shift;
372         spin_lock_irqsave(&chip->reg_lock, flags);
373         oval = chip->ctlregs[reg];
374         val = (oval & ~(mask << shift)) | val;
375         change = val != oval;
376         __snd_opl3sa2_write(chip, reg, val);
377         spin_unlock_irqrestore(&chip->reg_lock, flags);
378         return change;
379 }
380
381 #define OPL3SA2_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
382 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
383   .info = snd_wss_info_double, \
384   .get = snd_opl3sa2_get_double, .put = snd_opl3sa2_put_double, \
385   .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
386 #define OPL3SA2_DOUBLE_TLV(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert, xtlv) \
387 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
388   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
389   .name = xname, .index = xindex, \
390   .info = snd_wss_info_double, \
391   .get = snd_opl3sa2_get_double, .put = snd_opl3sa2_put_double, \
392   .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22), \
393   .tlv = { .p = (xtlv) } }
394
395 static int snd_opl3sa2_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
396 {
397         struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
398         unsigned long flags;
399         int left_reg = kcontrol->private_value & 0xff;
400         int right_reg = (kcontrol->private_value >> 8) & 0xff;
401         int shift_left = (kcontrol->private_value >> 16) & 0x07;
402         int shift_right = (kcontrol->private_value >> 19) & 0x07;
403         int mask = (kcontrol->private_value >> 24) & 0xff;
404         int invert = (kcontrol->private_value >> 22) & 1;
405         
406         spin_lock_irqsave(&chip->reg_lock, flags);
407         ucontrol->value.integer.value[0] = (chip->ctlregs[left_reg] >> shift_left) & mask;
408         ucontrol->value.integer.value[1] = (chip->ctlregs[right_reg] >> shift_right) & mask;
409         spin_unlock_irqrestore(&chip->reg_lock, flags);
410         if (invert) {
411                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
412                 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
413         }
414         return 0;
415 }
416
417 static int snd_opl3sa2_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
418 {
419         struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
420         unsigned long flags;
421         int left_reg = kcontrol->private_value & 0xff;
422         int right_reg = (kcontrol->private_value >> 8) & 0xff;
423         int shift_left = (kcontrol->private_value >> 16) & 0x07;
424         int shift_right = (kcontrol->private_value >> 19) & 0x07;
425         int mask = (kcontrol->private_value >> 24) & 0xff;
426         int invert = (kcontrol->private_value >> 22) & 1;
427         int change;
428         unsigned short val1, val2, oval1, oval2;
429         
430         val1 = ucontrol->value.integer.value[0] & mask;
431         val2 = ucontrol->value.integer.value[1] & mask;
432         if (invert) {
433                 val1 = mask - val1;
434                 val2 = mask - val2;
435         }
436         val1 <<= shift_left;
437         val2 <<= shift_right;
438         spin_lock_irqsave(&chip->reg_lock, flags);
439         if (left_reg != right_reg) {
440                 oval1 = chip->ctlregs[left_reg];
441                 oval2 = chip->ctlregs[right_reg];
442                 val1 = (oval1 & ~(mask << shift_left)) | val1;
443                 val2 = (oval2 & ~(mask << shift_right)) | val2;
444                 change = val1 != oval1 || val2 != oval2;
445                 __snd_opl3sa2_write(chip, left_reg, val1);
446                 __snd_opl3sa2_write(chip, right_reg, val2);
447         } else {
448                 oval1 = chip->ctlregs[left_reg];
449                 val1 = (oval1 & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
450                 change = val1 != oval1;
451                 __snd_opl3sa2_write(chip, left_reg, val1);
452         }
453         spin_unlock_irqrestore(&chip->reg_lock, flags);
454         return change;
455 }
456
457 static const DECLARE_TLV_DB_SCALE(db_scale_master, -3000, 200, 0);
458 static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0);
459
460 static const struct snd_kcontrol_new snd_opl3sa2_controls[] = {
461 OPL3SA2_DOUBLE("Master Playback Switch", 0, 0x07, 0x08, 7, 7, 1, 1),
462 OPL3SA2_DOUBLE_TLV("Master Playback Volume", 0, 0x07, 0x08, 0, 0, 15, 1,
463                    db_scale_master),
464 OPL3SA2_SINGLE("Mic Playback Switch", 0, 0x09, 7, 1, 1),
465 OPL3SA2_SINGLE_TLV("Mic Playback Volume", 0, 0x09, 0, 31, 1,
466                    db_scale_5bit_12db_max),
467 OPL3SA2_SINGLE("ZV Port Switch", 0, 0x02, 0, 1, 0),
468 };
469
470 static const struct snd_kcontrol_new snd_opl3sa2_tone_controls[] = {
471 OPL3SA2_DOUBLE("3D Control - Wide", 0, 0x14, 0x14, 4, 0, 7, 0),
472 OPL3SA2_DOUBLE("Tone Control - Bass", 0, 0x15, 0x15, 4, 0, 7, 0),
473 OPL3SA2_DOUBLE("Tone Control - Treble", 0, 0x16, 0x16, 4, 0, 7, 0)
474 };
475
476 static void snd_opl3sa2_master_free(struct snd_kcontrol *kcontrol)
477 {
478         struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
479         chip->master_switch = NULL;
480         chip->master_volume = NULL;
481 }
482
483 static int snd_opl3sa2_mixer(struct snd_card *card)
484 {
485         struct snd_opl3sa2 *chip = card->private_data;
486         struct snd_ctl_elem_id id1, id2;
487         struct snd_kcontrol *kctl;
488         unsigned int idx;
489         int err;
490
491         memset(&id1, 0, sizeof(id1));
492         memset(&id2, 0, sizeof(id2));
493         id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
494         /* reassign AUX0 to CD */
495         strcpy(id1.name, "Aux Playback Switch");
496         strcpy(id2.name, "CD Playback Switch");
497         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
498                 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
499                 return err;
500         }
501         strcpy(id1.name, "Aux Playback Volume");
502         strcpy(id2.name, "CD Playback Volume");
503         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
504                 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
505                 return err;
506         }
507         /* reassign AUX1 to FM */
508         strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
509         strcpy(id2.name, "FM Playback Switch");
510         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
511                 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
512                 return err;
513         }
514         strcpy(id1.name, "Aux Playback Volume");
515         strcpy(id2.name, "FM Playback Volume");
516         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
517                 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
518                 return err;
519         }
520         /* add OPL3SA2 controls */
521         for (idx = 0; idx < ARRAY_SIZE(snd_opl3sa2_controls); idx++) {
522                 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_opl3sa2_controls[idx], chip))) < 0)
523                         return err;
524                 switch (idx) {
525                 case 0: chip->master_switch = kctl; kctl->private_free = snd_opl3sa2_master_free; break;
526                 case 1: chip->master_volume = kctl; kctl->private_free = snd_opl3sa2_master_free; break;
527                 }
528         }
529         if (chip->version > 2) {
530                 for (idx = 0; idx < ARRAY_SIZE(snd_opl3sa2_tone_controls); idx++)
531                         if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_opl3sa2_tone_controls[idx], chip))) < 0)
532                                 return err;
533         }
534         return 0;
535 }
536
537 /* Power Management support functions */
538 #ifdef CONFIG_PM
539 static int snd_opl3sa2_suspend(struct snd_card *card, pm_message_t state)
540 {
541         if (card) {
542                 struct snd_opl3sa2 *chip = card->private_data;
543
544                 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
545                 chip->wss->suspend(chip->wss);
546                 /* power down */
547                 snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D3);
548         }
549
550         return 0;
551 }
552
553 static int snd_opl3sa2_resume(struct snd_card *card)
554 {
555         struct snd_opl3sa2 *chip;
556         int i;
557
558         if (!card)
559                 return 0;
560
561         chip = card->private_data;
562         /* power up */
563         snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D0);
564
565         /* restore registers */
566         for (i = 2; i <= 0x0a; i++) {
567                 if (i != OPL3SA2_IRQ_STATUS)
568                         snd_opl3sa2_write(chip, i, chip->ctlregs[i]);
569         }
570         if (chip->version > 2) {
571                 for (i = 0x12; i <= 0x16; i++)
572                         snd_opl3sa2_write(chip, i, chip->ctlregs[i]);
573         }
574         /* restore wss */
575         chip->wss->resume(chip->wss);
576
577         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
578         return 0;
579 }
580 #endif /* CONFIG_PM */
581
582 #ifdef CONFIG_PNP
583 static int snd_opl3sa2_pnp(int dev, struct snd_opl3sa2 *chip,
584                            struct pnp_dev *pdev)
585 {
586         if (pnp_activate_dev(pdev) < 0) {
587                 snd_printk(KERN_ERR "PnP configure failure (out of resources?)\n");
588                 return -EBUSY;
589         }
590         sb_port[dev] = pnp_port_start(pdev, 0);
591         wss_port[dev] = pnp_port_start(pdev, 1);
592         fm_port[dev] = pnp_port_start(pdev, 2);
593         midi_port[dev] = pnp_port_start(pdev, 3);
594         port[dev] = pnp_port_start(pdev, 4);
595         dma1[dev] = pnp_dma(pdev, 0);
596         dma2[dev] = pnp_dma(pdev, 1);
597         irq[dev] = pnp_irq(pdev, 0);
598         snd_printdd("%sPnP OPL3-SA: sb port=0x%lx, wss port=0x%lx, fm port=0x%lx, midi port=0x%lx\n",
599                 pnp_device_is_pnpbios(pdev) ? "BIOS" : "ISA", sb_port[dev], wss_port[dev], fm_port[dev], midi_port[dev]);
600         snd_printdd("%sPnP OPL3-SA: control port=0x%lx, dma1=%i, dma2=%i, irq=%i\n",
601                 pnp_device_is_pnpbios(pdev) ? "BIOS" : "ISA", port[dev], dma1[dev], dma2[dev], irq[dev]);
602         return 0;
603 }
604 #endif /* CONFIG_PNP */
605
606 static void snd_opl3sa2_free(struct snd_card *card)
607 {
608         struct snd_opl3sa2 *chip = card->private_data;
609         if (chip->irq >= 0)
610                 free_irq(chip->irq, card);
611         release_and_free_resource(chip->res_port);
612 }
613
614 static int snd_opl3sa2_card_new(struct device *pdev, int dev,
615                                 struct snd_card **cardp)
616 {
617         struct snd_card *card;
618         struct snd_opl3sa2 *chip;
619         int err;
620
621         err = snd_card_new(pdev, index[dev], id[dev], THIS_MODULE,
622                            sizeof(struct snd_opl3sa2), &card);
623         if (err < 0)
624                 return err;
625         strcpy(card->driver, "OPL3SA2");
626         strcpy(card->shortname, "Yamaha OPL3-SA");
627         chip = card->private_data;
628         spin_lock_init(&chip->reg_lock);
629         chip->irq = -1;
630         card->private_free = snd_opl3sa2_free;
631         *cardp = card;
632         return 0;
633 }
634
635 static int snd_opl3sa2_probe(struct snd_card *card, int dev)
636 {
637         int xirq, xdma1, xdma2;
638         struct snd_opl3sa2 *chip;
639         struct snd_wss *wss;
640         struct snd_opl3 *opl3;
641         int err;
642
643         /* initialise this card from supplied (or default) parameter*/ 
644         chip = card->private_data;
645         chip->ymode = opl3sa3_ymode[dev] & 0x03 ;
646         chip->port = port[dev];
647         xirq = irq[dev];
648         xdma1 = dma1[dev];
649         xdma2 = dma2[dev];
650         if (xdma2 < 0)
651                 chip->single_dma = 1;
652         err = snd_opl3sa2_detect(card);
653         if (err < 0)
654                 return err;
655         err = request_irq(xirq, snd_opl3sa2_interrupt, 0,
656                           "OPL3-SA2", card);
657         if (err) {
658                 snd_printk(KERN_ERR PFX "can't grab IRQ %d\n", xirq);
659                 return -ENODEV;
660         }
661         chip->irq = xirq;
662         card->sync_irq = chip->irq;
663         err = snd_wss_create(card,
664                              wss_port[dev] + 4, -1,
665                              xirq, xdma1, xdma2,
666                              WSS_HW_OPL3SA2, WSS_HWSHARE_IRQ, &wss);
667         if (err < 0) {
668                 snd_printd("Oops, WSS not detected at 0x%lx\n", wss_port[dev] + 4);
669                 return err;
670         }
671         chip->wss = wss;
672         err = snd_wss_pcm(wss, 0);
673         if (err < 0)
674                 return err;
675         err = snd_wss_mixer(wss);
676         if (err < 0)
677                 return err;
678         err = snd_opl3sa2_mixer(card);
679         if (err < 0)
680                 return err;
681         err = snd_wss_timer(wss, 0);
682         if (err < 0)
683                 return err;
684         if (fm_port[dev] >= 0x340 && fm_port[dev] < 0x400) {
685                 if ((err = snd_opl3_create(card, fm_port[dev],
686                                            fm_port[dev] + 2,
687                                            OPL3_HW_OPL3, 0, &opl3)) < 0)
688                         return err;
689                 if ((err = snd_opl3_timer_new(opl3, 1, 2)) < 0)
690                         return err;
691                 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, &chip->synth)) < 0)
692                         return err;
693         }
694         if (midi_port[dev] >= 0x300 && midi_port[dev] < 0x340) {
695                 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_OPL3SA2,
696                                                midi_port[dev],
697                                                MPU401_INFO_IRQ_HOOK, -1,
698                                                &chip->rmidi)) < 0)
699                         return err;
700         }
701         sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
702                 card->shortname, chip->port, xirq, xdma1);
703         if (xdma2 >= 0)
704                 sprintf(card->longname + strlen(card->longname), "&%d", xdma2);
705
706         return snd_card_register(card);
707 }
708
709 #ifdef CONFIG_PNP
710 static int snd_opl3sa2_pnp_detect(struct pnp_dev *pdev,
711                                   const struct pnp_device_id *id)
712 {
713         static int dev;
714         int err;
715         struct snd_card *card;
716
717         if (pnp_device_is_isapnp(pdev))
718                 return -ENOENT; /* we have another procedure - card */
719         for (; dev < SNDRV_CARDS; dev++) {
720                 if (enable[dev] && isapnp[dev])
721                         break;
722         }
723         if (dev >= SNDRV_CARDS)
724                 return -ENODEV;
725
726         err = snd_opl3sa2_card_new(&pdev->dev, dev, &card);
727         if (err < 0)
728                 return err;
729         if ((err = snd_opl3sa2_pnp(dev, card->private_data, pdev)) < 0) {
730                 snd_card_free(card);
731                 return err;
732         }
733         if ((err = snd_opl3sa2_probe(card, dev)) < 0) {
734                 snd_card_free(card);
735                 return err;
736         }
737         pnp_set_drvdata(pdev, card);
738         dev++;
739         return 0;
740 }
741
742 static void snd_opl3sa2_pnp_remove(struct pnp_dev *pdev)
743 {
744         snd_card_free(pnp_get_drvdata(pdev));
745 }
746
747 #ifdef CONFIG_PM
748 static int snd_opl3sa2_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
749 {
750         return snd_opl3sa2_suspend(pnp_get_drvdata(pdev), state);
751 }
752 static int snd_opl3sa2_pnp_resume(struct pnp_dev *pdev)
753 {
754         return snd_opl3sa2_resume(pnp_get_drvdata(pdev));
755 }
756 #endif
757
758 static struct pnp_driver opl3sa2_pnp_driver = {
759         .name = "snd-opl3sa2-pnpbios",
760         .id_table = snd_opl3sa2_pnpbiosids,
761         .probe = snd_opl3sa2_pnp_detect,
762         .remove = snd_opl3sa2_pnp_remove,
763 #ifdef CONFIG_PM
764         .suspend = snd_opl3sa2_pnp_suspend,
765         .resume = snd_opl3sa2_pnp_resume,
766 #endif
767 };
768
769 static int snd_opl3sa2_pnp_cdetect(struct pnp_card_link *pcard,
770                                    const struct pnp_card_device_id *id)
771 {
772         static int dev;
773         struct pnp_dev *pdev;
774         int err;
775         struct snd_card *card;
776
777         pdev = pnp_request_card_device(pcard, id->devs[0].id, NULL);
778         if (pdev == NULL) {
779                 snd_printk(KERN_ERR PFX "can't get pnp device from id '%s'\n",
780                            id->devs[0].id);
781                 return -EBUSY;
782         }
783         for (; dev < SNDRV_CARDS; dev++) {
784                 if (enable[dev] && isapnp[dev])
785                         break;
786         }
787         if (dev >= SNDRV_CARDS)
788                 return -ENODEV;
789
790         err = snd_opl3sa2_card_new(&pdev->dev, dev, &card);
791         if (err < 0)
792                 return err;
793         if ((err = snd_opl3sa2_pnp(dev, card->private_data, pdev)) < 0) {
794                 snd_card_free(card);
795                 return err;
796         }
797         if ((err = snd_opl3sa2_probe(card, dev)) < 0) {
798                 snd_card_free(card);
799                 return err;
800         }
801         pnp_set_card_drvdata(pcard, card);
802         dev++;
803         return 0;
804 }
805
806 static void snd_opl3sa2_pnp_cremove(struct pnp_card_link *pcard)
807 {
808         snd_card_free(pnp_get_card_drvdata(pcard));
809         pnp_set_card_drvdata(pcard, NULL);
810 }
811
812 #ifdef CONFIG_PM
813 static int snd_opl3sa2_pnp_csuspend(struct pnp_card_link *pcard, pm_message_t state)
814 {
815         return snd_opl3sa2_suspend(pnp_get_card_drvdata(pcard), state);
816 }
817 static int snd_opl3sa2_pnp_cresume(struct pnp_card_link *pcard)
818 {
819         return snd_opl3sa2_resume(pnp_get_card_drvdata(pcard));
820 }
821 #endif
822
823 static struct pnp_card_driver opl3sa2_pnpc_driver = {
824         .flags = PNP_DRIVER_RES_DISABLE,
825         .name = "snd-opl3sa2-cpnp",
826         .id_table = snd_opl3sa2_pnpids,
827         .probe = snd_opl3sa2_pnp_cdetect,
828         .remove = snd_opl3sa2_pnp_cremove,
829 #ifdef CONFIG_PM
830         .suspend = snd_opl3sa2_pnp_csuspend,
831         .resume = snd_opl3sa2_pnp_cresume,
832 #endif
833 };
834 #endif /* CONFIG_PNP */
835
836 static int snd_opl3sa2_isa_match(struct device *pdev,
837                                  unsigned int dev)
838 {
839         if (!enable[dev])
840                 return 0;
841 #ifdef CONFIG_PNP
842         if (isapnp[dev])
843                 return 0;
844 #endif
845         if (port[dev] == SNDRV_AUTO_PORT) {
846                 snd_printk(KERN_ERR PFX "specify port\n");
847                 return 0;
848         }
849         if (wss_port[dev] == SNDRV_AUTO_PORT) {
850                 snd_printk(KERN_ERR PFX "specify wss_port\n");
851                 return 0;
852         }
853         if (fm_port[dev] == SNDRV_AUTO_PORT) {
854                 snd_printk(KERN_ERR PFX "specify fm_port\n");
855                 return 0;
856         }
857         if (midi_port[dev] == SNDRV_AUTO_PORT) {
858                 snd_printk(KERN_ERR PFX "specify midi_port\n");
859                 return 0;
860         }
861         return 1;
862 }
863
864 static int snd_opl3sa2_isa_probe(struct device *pdev,
865                                  unsigned int dev)
866 {
867         struct snd_card *card;
868         int err;
869
870         err = snd_opl3sa2_card_new(pdev, dev, &card);
871         if (err < 0)
872                 return err;
873         if ((err = snd_opl3sa2_probe(card, dev)) < 0) {
874                 snd_card_free(card);
875                 return err;
876         }
877         dev_set_drvdata(pdev, card);
878         return 0;
879 }
880
881 static void snd_opl3sa2_isa_remove(struct device *devptr,
882                                   unsigned int dev)
883 {
884         snd_card_free(dev_get_drvdata(devptr));
885 }
886
887 #ifdef CONFIG_PM
888 static int snd_opl3sa2_isa_suspend(struct device *dev, unsigned int n,
889                                    pm_message_t state)
890 {
891         return snd_opl3sa2_suspend(dev_get_drvdata(dev), state);
892 }
893
894 static int snd_opl3sa2_isa_resume(struct device *dev, unsigned int n)
895 {
896         return snd_opl3sa2_resume(dev_get_drvdata(dev));
897 }
898 #endif
899
900 #define DEV_NAME "opl3sa2"
901
902 static struct isa_driver snd_opl3sa2_isa_driver = {
903         .match          = snd_opl3sa2_isa_match,
904         .probe          = snd_opl3sa2_isa_probe,
905         .remove         = snd_opl3sa2_isa_remove,
906 #ifdef CONFIG_PM
907         .suspend        = snd_opl3sa2_isa_suspend,
908         .resume         = snd_opl3sa2_isa_resume,
909 #endif
910         .driver         = {
911                 .name   = DEV_NAME
912         },
913 };
914
915 static int __init alsa_card_opl3sa2_init(void)
916 {
917         int err;
918
919         err = isa_register_driver(&snd_opl3sa2_isa_driver, SNDRV_CARDS);
920 #ifdef CONFIG_PNP
921         if (!err)
922                 isa_registered = 1;
923
924         err = pnp_register_driver(&opl3sa2_pnp_driver);
925         if (!err)
926                 pnp_registered = 1;
927
928         err = pnp_register_card_driver(&opl3sa2_pnpc_driver);
929         if (!err)
930                 pnpc_registered = 1;
931
932         if (isa_registered || pnp_registered)
933                 err = 0;
934 #endif
935         return err;
936 }
937
938 static void __exit alsa_card_opl3sa2_exit(void)
939 {
940 #ifdef CONFIG_PNP
941         if (pnpc_registered)
942                 pnp_unregister_card_driver(&opl3sa2_pnpc_driver);
943         if (pnp_registered)
944                 pnp_unregister_driver(&opl3sa2_pnp_driver);
945         if (isa_registered)
946 #endif
947                 isa_unregister_driver(&snd_opl3sa2_isa_driver);
948 }
949
950 module_init(alsa_card_opl3sa2_init)
951 module_exit(alsa_card_opl3sa2_exit)