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