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