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