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