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