gpio: max77620: Fix missing release of interrupt
[linux/fpc-iii.git] / sound / isa / opl3sa2.c
blob85a181acd38841e4c11d70de5a2e99768ef05e30
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Driver for Yamaha OPL3-SA[2,3] soundcards
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5 */
7 #include <linux/init.h>
8 #include <linux/err.h>
9 #include <linux/isa.h>
10 #include <linux/interrupt.h>
11 #include <linux/pm.h>
12 #include <linux/pnp.h>
13 #include <linux/module.h>
14 #include <linux/io.h>
15 #include <sound/core.h>
16 #include <sound/wss.h>
17 #include <sound/mpu401.h>
18 #include <sound/opl3.h>
19 #include <sound/initval.h>
20 #include <sound/tlv.h>
22 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
23 MODULE_DESCRIPTION("Yamaha OPL3SA2+");
24 MODULE_LICENSE("GPL");
25 MODULE_SUPPORTED_DEVICE("{{Yamaha,YMF719E-S},"
26 "{Genius,Sound Maker 3DX},"
27 "{Yamaha,OPL3SA3},"
28 "{Intel,AL440LX sound},"
29 "{NeoMagic,MagicWave 3DX}}");
31 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
32 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
33 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
34 #ifdef CONFIG_PNP
35 static bool isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
36 #endif
37 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0xf86,0x370,0x100 */
38 static long sb_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260 */
39 static long wss_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;/* 0x530,0xe80,0xf40,0x604 */
40 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x388 */
41 static long midi_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;/* 0x330,0x300 */
42 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 0,1,3,5,9,11,12,15 */
43 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
44 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
45 static int opl3sa3_ymode[SNDRV_CARDS]; /* 0,1,2,3 */ /*SL Added*/
47 module_param_array(index, int, NULL, 0444);
48 MODULE_PARM_DESC(index, "Index value for OPL3-SA soundcard.");
49 module_param_array(id, charp, NULL, 0444);
50 MODULE_PARM_DESC(id, "ID string for OPL3-SA soundcard.");
51 module_param_array(enable, bool, NULL, 0444);
52 MODULE_PARM_DESC(enable, "Enable OPL3-SA soundcard.");
53 #ifdef CONFIG_PNP
54 module_param_array(isapnp, bool, NULL, 0444);
55 MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
56 #endif
57 module_param_hw_array(port, long, ioport, NULL, 0444);
58 MODULE_PARM_DESC(port, "Port # for OPL3-SA driver.");
59 module_param_hw_array(sb_port, long, ioport, NULL, 0444);
60 MODULE_PARM_DESC(sb_port, "SB port # for OPL3-SA driver.");
61 module_param_hw_array(wss_port, long, ioport, NULL, 0444);
62 MODULE_PARM_DESC(wss_port, "WSS port # for OPL3-SA driver.");
63 module_param_hw_array(fm_port, long, ioport, NULL, 0444);
64 MODULE_PARM_DESC(fm_port, "FM port # for OPL3-SA driver.");
65 module_param_hw_array(midi_port, long, ioport, NULL, 0444);
66 MODULE_PARM_DESC(midi_port, "MIDI port # for OPL3-SA driver.");
67 module_param_hw_array(irq, int, irq, NULL, 0444);
68 MODULE_PARM_DESC(irq, "IRQ # for OPL3-SA driver.");
69 module_param_hw_array(dma1, int, dma, NULL, 0444);
70 MODULE_PARM_DESC(dma1, "DMA1 # for OPL3-SA driver.");
71 module_param_hw_array(dma2, int, dma, NULL, 0444);
72 MODULE_PARM_DESC(dma2, "DMA2 # for OPL3-SA driver.");
73 module_param_array(opl3sa3_ymode, int, NULL, 0444);
74 MODULE_PARM_DESC(opl3sa3_ymode, "Speaker size selection for 3D Enhancement mode: Desktop/Large Notebook/Small Notebook/HiFi.");
76 #ifdef CONFIG_PNP
77 static int isa_registered;
78 static int pnp_registered;
79 static int pnpc_registered;
80 #endif
82 /* control ports */
83 #define OPL3SA2_PM_CTRL 0x01
84 #define OPL3SA2_SYS_CTRL 0x02
85 #define OPL3SA2_IRQ_CONFIG 0x03
86 #define OPL3SA2_IRQ_STATUS 0x04
87 #define OPL3SA2_DMA_CONFIG 0x06
88 #define OPL3SA2_MASTER_LEFT 0x07
89 #define OPL3SA2_MASTER_RIGHT 0x08
90 #define OPL3SA2_MIC 0x09
91 #define OPL3SA2_MISC 0x0A
93 /* opl3sa3 only */
94 #define OPL3SA3_DGTL_DOWN 0x12
95 #define OPL3SA3_ANLG_DOWN 0x13
96 #define OPL3SA3_WIDE 0x14
97 #define OPL3SA3_BASS 0x15
98 #define OPL3SA3_TREBLE 0x16
100 /* power management bits */
101 #define OPL3SA2_PM_ADOWN 0x20
102 #define OPL3SA2_PM_PSV 0x04
103 #define OPL3SA2_PM_PDN 0x02
104 #define OPL3SA2_PM_PDX 0x01
106 #define OPL3SA2_PM_D0 0x00
107 #define OPL3SA2_PM_D3 (OPL3SA2_PM_ADOWN|OPL3SA2_PM_PSV|OPL3SA2_PM_PDN|OPL3SA2_PM_PDX)
109 struct snd_opl3sa2 {
110 int version; /* 2 or 3 */
111 unsigned long port; /* control port */
112 struct resource *res_port; /* control port resource */
113 int irq;
114 int single_dma;
115 spinlock_t reg_lock;
116 struct snd_hwdep *synth;
117 struct snd_rawmidi *rmidi;
118 struct snd_wss *wss;
119 unsigned char ctlregs[0x20];
120 int ymode; /* SL added */
121 struct snd_kcontrol *master_switch;
122 struct snd_kcontrol *master_volume;
125 #define PFX "opl3sa2: "
127 #ifdef CONFIG_PNP
129 static const struct pnp_device_id snd_opl3sa2_pnpbiosids[] = {
130 { .id = "YMH0021" },
131 { .id = "NMX2210" }, /* Gateway Solo 2500 */
132 { .id = "" } /* end */
135 MODULE_DEVICE_TABLE(pnp, snd_opl3sa2_pnpbiosids);
137 static const struct pnp_card_device_id snd_opl3sa2_pnpids[] = {
138 /* Yamaha YMF719E-S (Genius Sound Maker 3DX) */
139 { .id = "YMH0020", .devs = { { "YMH0021" } } },
140 /* Yamaha OPL3-SA3 (integrated on Intel's Pentium II AL440LX motherboard) */
141 { .id = "YMH0030", .devs = { { "YMH0021" } } },
142 /* Yamaha OPL3-SA2 */
143 { .id = "YMH0800", .devs = { { "YMH0021" } } },
144 /* Yamaha OPL3-SA2 */
145 { .id = "YMH0801", .devs = { { "YMH0021" } } },
146 /* NeoMagic MagicWave 3DX */
147 { .id = "NMX2200", .devs = { { "YMH2210" } } },
148 /* NeoMagic MagicWave 3D */
149 { .id = "NMX2200", .devs = { { "NMX2210" } } },
150 /* --- */
151 { .id = "" } /* end */
154 MODULE_DEVICE_TABLE(pnp_card, snd_opl3sa2_pnpids);
156 #endif /* CONFIG_PNP */
159 /* read control port (w/o spinlock) */
160 static unsigned char __snd_opl3sa2_read(struct snd_opl3sa2 *chip, unsigned char reg)
162 unsigned char result;
163 #if 0
164 outb(0x1d, port); /* password */
165 printk(KERN_DEBUG "read [0x%lx] = 0x%x\n", port, inb(port));
166 #endif
167 outb(reg, chip->port); /* register */
168 result = inb(chip->port + 1);
169 #if 0
170 printk(KERN_DEBUG "read [0x%lx] = 0x%x [0x%x]\n",
171 port, result, inb(port));
172 #endif
173 return result;
176 /* read control port (with spinlock) */
177 static unsigned char snd_opl3sa2_read(struct snd_opl3sa2 *chip, unsigned char reg)
179 unsigned long flags;
180 unsigned char result;
182 spin_lock_irqsave(&chip->reg_lock, flags);
183 result = __snd_opl3sa2_read(chip, reg);
184 spin_unlock_irqrestore(&chip->reg_lock, flags);
185 return result;
188 /* write control port (w/o spinlock) */
189 static void __snd_opl3sa2_write(struct snd_opl3sa2 *chip, unsigned char reg, unsigned char value)
191 #if 0
192 outb(0x1d, port); /* password */
193 #endif
194 outb(reg, chip->port); /* register */
195 outb(value, chip->port + 1);
196 chip->ctlregs[reg] = value;
199 /* write control port (with spinlock) */
200 static void snd_opl3sa2_write(struct snd_opl3sa2 *chip, unsigned char reg, unsigned char value)
202 unsigned long flags;
203 spin_lock_irqsave(&chip->reg_lock, flags);
204 __snd_opl3sa2_write(chip, reg, value);
205 spin_unlock_irqrestore(&chip->reg_lock, flags);
208 static int snd_opl3sa2_detect(struct snd_card *card)
210 struct snd_opl3sa2 *chip = card->private_data;
211 unsigned long port;
212 unsigned char tmp, tmp1;
213 char str[2];
215 port = chip->port;
216 if ((chip->res_port = request_region(port, 2, "OPL3-SA control")) == NULL) {
217 snd_printk(KERN_ERR PFX "can't grab port 0x%lx\n", port);
218 return -EBUSY;
221 snd_printk(KERN_DEBUG "REG 0A = 0x%x\n",
222 snd_opl3sa2_read(chip, 0x0a));
224 chip->version = 0;
225 tmp = snd_opl3sa2_read(chip, OPL3SA2_MISC);
226 if (tmp == 0xff) {
227 snd_printd("OPL3-SA [0x%lx] detect = 0x%x\n", port, tmp);
228 return -ENODEV;
230 switch (tmp & 0x07) {
231 case 0x01:
232 chip->version = 2; /* YMF711 */
233 break;
234 default:
235 chip->version = 3;
236 /* 0x02 - standard */
237 /* 0x03 - YM715B */
238 /* 0x04 - YM719 - OPL-SA4? */
239 /* 0x05 - OPL3-SA3 - Libretto 100 */
240 /* 0x07 - unknown - Neomagic MagicWave 3D */
241 break;
243 str[0] = chip->version + '0';
244 str[1] = 0;
245 strcat(card->shortname, str);
246 snd_opl3sa2_write(chip, OPL3SA2_MISC, tmp ^ 7);
247 if ((tmp1 = snd_opl3sa2_read(chip, OPL3SA2_MISC)) != tmp) {
248 snd_printd("OPL3-SA [0x%lx] detect (1) = 0x%x (0x%x)\n", port, tmp, tmp1);
249 return -ENODEV;
251 /* try if the MIC register is accessible */
252 tmp = snd_opl3sa2_read(chip, OPL3SA2_MIC);
253 snd_opl3sa2_write(chip, OPL3SA2_MIC, 0x8a);
254 if (((tmp1 = snd_opl3sa2_read(chip, OPL3SA2_MIC)) & 0x9f) != 0x8a) {
255 snd_printd("OPL3-SA [0x%lx] detect (2) = 0x%x (0x%x)\n", port, tmp, tmp1);
256 return -ENODEV;
258 snd_opl3sa2_write(chip, OPL3SA2_MIC, 0x9f);
259 /* initialization */
260 /* Power Management - full on */
261 snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D0);
262 if (chip->version > 2) {
263 /* ymode is bits 4&5 (of 0 to 7) on all but opl3sa2 versions */
264 snd_opl3sa2_write(chip, OPL3SA2_SYS_CTRL, (chip->ymode << 4));
265 } else {
266 /* default for opl3sa2 versions */
267 snd_opl3sa2_write(chip, OPL3SA2_SYS_CTRL, 0x00);
269 snd_opl3sa2_write(chip, OPL3SA2_IRQ_CONFIG, 0x0d); /* Interrupt Channel Configuration - IRQ A = OPL3 + MPU + WSS */
270 if (chip->single_dma) {
271 snd_opl3sa2_write(chip, OPL3SA2_DMA_CONFIG, 0x03); /* DMA Configuration - DMA A = WSS-R + WSS-P */
272 } else {
273 snd_opl3sa2_write(chip, OPL3SA2_DMA_CONFIG, 0x21); /* DMA Configuration - DMA B = WSS-R, DMA A = WSS-P */
275 snd_opl3sa2_write(chip, OPL3SA2_MISC, 0x80 | (tmp & 7)); /* Miscellaneous - default */
276 if (chip->version > 2) {
277 snd_opl3sa2_write(chip, OPL3SA3_DGTL_DOWN, 0x00); /* Digital Block Partial Power Down - default */
278 snd_opl3sa2_write(chip, OPL3SA3_ANLG_DOWN, 0x00); /* Analog Block Partial Power Down - default */
280 return 0;
283 static irqreturn_t snd_opl3sa2_interrupt(int irq, void *dev_id)
285 unsigned short status;
286 struct snd_card *card = dev_id;
287 struct snd_opl3sa2 *chip;
288 int handled = 0;
290 if (card == NULL)
291 return IRQ_NONE;
293 chip = card->private_data;
294 status = snd_opl3sa2_read(chip, OPL3SA2_IRQ_STATUS);
296 if (status & 0x20) {
297 handled = 1;
298 snd_opl3_interrupt(chip->synth);
301 if ((status & 0x10) && chip->rmidi != NULL) {
302 handled = 1;
303 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
306 if (status & 0x07) { /* TI,CI,PI */
307 handled = 1;
308 snd_wss_interrupt(irq, chip->wss);
311 if (status & 0x40) { /* hardware volume change */
312 handled = 1;
313 /* reading from Master Lch register at 0x07 clears this bit */
314 snd_opl3sa2_read(chip, OPL3SA2_MASTER_RIGHT);
315 snd_opl3sa2_read(chip, OPL3SA2_MASTER_LEFT);
316 if (chip->master_switch && chip->master_volume) {
317 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
318 &chip->master_switch->id);
319 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
320 &chip->master_volume->id);
323 return IRQ_RETVAL(handled);
326 #define OPL3SA2_SINGLE(xname, xindex, reg, shift, mask, invert) \
327 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
328 .info = snd_wss_info_single, \
329 .get = snd_opl3sa2_get_single, .put = snd_opl3sa2_put_single, \
330 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
331 #define OPL3SA2_SINGLE_TLV(xname, xindex, reg, shift, mask, invert, xtlv) \
332 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
333 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
334 .name = xname, .index = xindex, \
335 .info = snd_wss_info_single, \
336 .get = snd_opl3sa2_get_single, .put = snd_opl3sa2_put_single, \
337 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24), \
338 .tlv = { .p = (xtlv) } }
340 static int snd_opl3sa2_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
342 struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
343 unsigned long flags;
344 int reg = kcontrol->private_value & 0xff;
345 int shift = (kcontrol->private_value >> 8) & 0xff;
346 int mask = (kcontrol->private_value >> 16) & 0xff;
347 int invert = (kcontrol->private_value >> 24) & 0xff;
349 spin_lock_irqsave(&chip->reg_lock, flags);
350 ucontrol->value.integer.value[0] = (chip->ctlregs[reg] >> shift) & mask;
351 spin_unlock_irqrestore(&chip->reg_lock, flags);
352 if (invert)
353 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
354 return 0;
357 static int snd_opl3sa2_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
359 struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
360 unsigned long flags;
361 int reg = kcontrol->private_value & 0xff;
362 int shift = (kcontrol->private_value >> 8) & 0xff;
363 int mask = (kcontrol->private_value >> 16) & 0xff;
364 int invert = (kcontrol->private_value >> 24) & 0xff;
365 int change;
366 unsigned short val, oval;
368 val = (ucontrol->value.integer.value[0] & mask);
369 if (invert)
370 val = mask - val;
371 val <<= shift;
372 spin_lock_irqsave(&chip->reg_lock, flags);
373 oval = chip->ctlregs[reg];
374 val = (oval & ~(mask << shift)) | val;
375 change = val != oval;
376 __snd_opl3sa2_write(chip, reg, val);
377 spin_unlock_irqrestore(&chip->reg_lock, flags);
378 return change;
381 #define OPL3SA2_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
382 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
383 .info = snd_wss_info_double, \
384 .get = snd_opl3sa2_get_double, .put = snd_opl3sa2_put_double, \
385 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
386 #define OPL3SA2_DOUBLE_TLV(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert, xtlv) \
387 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
388 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
389 .name = xname, .index = xindex, \
390 .info = snd_wss_info_double, \
391 .get = snd_opl3sa2_get_double, .put = snd_opl3sa2_put_double, \
392 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22), \
393 .tlv = { .p = (xtlv) } }
395 static int snd_opl3sa2_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
397 struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
398 unsigned long flags;
399 int left_reg = kcontrol->private_value & 0xff;
400 int right_reg = (kcontrol->private_value >> 8) & 0xff;
401 int shift_left = (kcontrol->private_value >> 16) & 0x07;
402 int shift_right = (kcontrol->private_value >> 19) & 0x07;
403 int mask = (kcontrol->private_value >> 24) & 0xff;
404 int invert = (kcontrol->private_value >> 22) & 1;
406 spin_lock_irqsave(&chip->reg_lock, flags);
407 ucontrol->value.integer.value[0] = (chip->ctlregs[left_reg] >> shift_left) & mask;
408 ucontrol->value.integer.value[1] = (chip->ctlregs[right_reg] >> shift_right) & mask;
409 spin_unlock_irqrestore(&chip->reg_lock, flags);
410 if (invert) {
411 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
412 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
414 return 0;
417 static int snd_opl3sa2_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
419 struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
420 unsigned long flags;
421 int left_reg = kcontrol->private_value & 0xff;
422 int right_reg = (kcontrol->private_value >> 8) & 0xff;
423 int shift_left = (kcontrol->private_value >> 16) & 0x07;
424 int shift_right = (kcontrol->private_value >> 19) & 0x07;
425 int mask = (kcontrol->private_value >> 24) & 0xff;
426 int invert = (kcontrol->private_value >> 22) & 1;
427 int change;
428 unsigned short val1, val2, oval1, oval2;
430 val1 = ucontrol->value.integer.value[0] & mask;
431 val2 = ucontrol->value.integer.value[1] & mask;
432 if (invert) {
433 val1 = mask - val1;
434 val2 = mask - val2;
436 val1 <<= shift_left;
437 val2 <<= shift_right;
438 spin_lock_irqsave(&chip->reg_lock, flags);
439 if (left_reg != right_reg) {
440 oval1 = chip->ctlregs[left_reg];
441 oval2 = chip->ctlregs[right_reg];
442 val1 = (oval1 & ~(mask << shift_left)) | val1;
443 val2 = (oval2 & ~(mask << shift_right)) | val2;
444 change = val1 != oval1 || val2 != oval2;
445 __snd_opl3sa2_write(chip, left_reg, val1);
446 __snd_opl3sa2_write(chip, right_reg, val2);
447 } else {
448 oval1 = chip->ctlregs[left_reg];
449 val1 = (oval1 & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
450 change = val1 != oval1;
451 __snd_opl3sa2_write(chip, left_reg, val1);
453 spin_unlock_irqrestore(&chip->reg_lock, flags);
454 return change;
457 static const DECLARE_TLV_DB_SCALE(db_scale_master, -3000, 200, 0);
458 static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0);
460 static const struct snd_kcontrol_new snd_opl3sa2_controls[] = {
461 OPL3SA2_DOUBLE("Master Playback Switch", 0, 0x07, 0x08, 7, 7, 1, 1),
462 OPL3SA2_DOUBLE_TLV("Master Playback Volume", 0, 0x07, 0x08, 0, 0, 15, 1,
463 db_scale_master),
464 OPL3SA2_SINGLE("Mic Playback Switch", 0, 0x09, 7, 1, 1),
465 OPL3SA2_SINGLE_TLV("Mic Playback Volume", 0, 0x09, 0, 31, 1,
466 db_scale_5bit_12db_max),
467 OPL3SA2_SINGLE("ZV Port Switch", 0, 0x02, 0, 1, 0),
470 static const struct snd_kcontrol_new snd_opl3sa2_tone_controls[] = {
471 OPL3SA2_DOUBLE("3D Control - Wide", 0, 0x14, 0x14, 4, 0, 7, 0),
472 OPL3SA2_DOUBLE("Tone Control - Bass", 0, 0x15, 0x15, 4, 0, 7, 0),
473 OPL3SA2_DOUBLE("Tone Control - Treble", 0, 0x16, 0x16, 4, 0, 7, 0)
476 static void snd_opl3sa2_master_free(struct snd_kcontrol *kcontrol)
478 struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
479 chip->master_switch = NULL;
480 chip->master_volume = NULL;
483 static int snd_opl3sa2_mixer(struct snd_card *card)
485 struct snd_opl3sa2 *chip = card->private_data;
486 struct snd_ctl_elem_id id1, id2;
487 struct snd_kcontrol *kctl;
488 unsigned int idx;
489 int err;
491 memset(&id1, 0, sizeof(id1));
492 memset(&id2, 0, sizeof(id2));
493 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
494 /* reassign AUX0 to CD */
495 strcpy(id1.name, "Aux Playback Switch");
496 strcpy(id2.name, "CD Playback Switch");
497 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
498 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
499 return err;
501 strcpy(id1.name, "Aux Playback Volume");
502 strcpy(id2.name, "CD Playback Volume");
503 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
504 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
505 return err;
507 /* reassign AUX1 to FM */
508 strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
509 strcpy(id2.name, "FM Playback Switch");
510 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
511 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
512 return err;
514 strcpy(id1.name, "Aux Playback Volume");
515 strcpy(id2.name, "FM Playback Volume");
516 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
517 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
518 return err;
520 /* add OPL3SA2 controls */
521 for (idx = 0; idx < ARRAY_SIZE(snd_opl3sa2_controls); idx++) {
522 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_opl3sa2_controls[idx], chip))) < 0)
523 return err;
524 switch (idx) {
525 case 0: chip->master_switch = kctl; kctl->private_free = snd_opl3sa2_master_free; break;
526 case 1: chip->master_volume = kctl; kctl->private_free = snd_opl3sa2_master_free; break;
529 if (chip->version > 2) {
530 for (idx = 0; idx < ARRAY_SIZE(snd_opl3sa2_tone_controls); idx++)
531 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_opl3sa2_tone_controls[idx], chip))) < 0)
532 return err;
534 return 0;
537 /* Power Management support functions */
538 #ifdef CONFIG_PM
539 static int snd_opl3sa2_suspend(struct snd_card *card, pm_message_t state)
541 if (card) {
542 struct snd_opl3sa2 *chip = card->private_data;
544 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
545 chip->wss->suspend(chip->wss);
546 /* power down */
547 snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D3);
550 return 0;
553 static int snd_opl3sa2_resume(struct snd_card *card)
555 struct snd_opl3sa2 *chip;
556 int i;
558 if (!card)
559 return 0;
561 chip = card->private_data;
562 /* power up */
563 snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D0);
565 /* restore registers */
566 for (i = 2; i <= 0x0a; i++) {
567 if (i != OPL3SA2_IRQ_STATUS)
568 snd_opl3sa2_write(chip, i, chip->ctlregs[i]);
570 if (chip->version > 2) {
571 for (i = 0x12; i <= 0x16; i++)
572 snd_opl3sa2_write(chip, i, chip->ctlregs[i]);
574 /* restore wss */
575 chip->wss->resume(chip->wss);
577 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
578 return 0;
580 #endif /* CONFIG_PM */
582 #ifdef CONFIG_PNP
583 static int snd_opl3sa2_pnp(int dev, struct snd_opl3sa2 *chip,
584 struct pnp_dev *pdev)
586 if (pnp_activate_dev(pdev) < 0) {
587 snd_printk(KERN_ERR "PnP configure failure (out of resources?)\n");
588 return -EBUSY;
590 sb_port[dev] = pnp_port_start(pdev, 0);
591 wss_port[dev] = pnp_port_start(pdev, 1);
592 fm_port[dev] = pnp_port_start(pdev, 2);
593 midi_port[dev] = pnp_port_start(pdev, 3);
594 port[dev] = pnp_port_start(pdev, 4);
595 dma1[dev] = pnp_dma(pdev, 0);
596 dma2[dev] = pnp_dma(pdev, 1);
597 irq[dev] = pnp_irq(pdev, 0);
598 snd_printdd("%sPnP OPL3-SA: sb port=0x%lx, wss port=0x%lx, fm port=0x%lx, midi port=0x%lx\n",
599 pnp_device_is_pnpbios(pdev) ? "BIOS" : "ISA", sb_port[dev], wss_port[dev], fm_port[dev], midi_port[dev]);
600 snd_printdd("%sPnP OPL3-SA: control port=0x%lx, dma1=%i, dma2=%i, irq=%i\n",
601 pnp_device_is_pnpbios(pdev) ? "BIOS" : "ISA", port[dev], dma1[dev], dma2[dev], irq[dev]);
602 return 0;
604 #endif /* CONFIG_PNP */
606 static void snd_opl3sa2_free(struct snd_card *card)
608 struct snd_opl3sa2 *chip = card->private_data;
609 if (chip->irq >= 0)
610 free_irq(chip->irq, card);
611 release_and_free_resource(chip->res_port);
614 static int snd_opl3sa2_card_new(struct device *pdev, int dev,
615 struct snd_card **cardp)
617 struct snd_card *card;
618 struct snd_opl3sa2 *chip;
619 int err;
621 err = snd_card_new(pdev, index[dev], id[dev], THIS_MODULE,
622 sizeof(struct snd_opl3sa2), &card);
623 if (err < 0)
624 return err;
625 strcpy(card->driver, "OPL3SA2");
626 strcpy(card->shortname, "Yamaha OPL3-SA");
627 chip = card->private_data;
628 spin_lock_init(&chip->reg_lock);
629 chip->irq = -1;
630 card->private_free = snd_opl3sa2_free;
631 *cardp = card;
632 return 0;
635 static int snd_opl3sa2_probe(struct snd_card *card, int dev)
637 int xirq, xdma1, xdma2;
638 struct snd_opl3sa2 *chip;
639 struct snd_wss *wss;
640 struct snd_opl3 *opl3;
641 int err;
643 /* initialise this card from supplied (or default) parameter*/
644 chip = card->private_data;
645 chip->ymode = opl3sa3_ymode[dev] & 0x03 ;
646 chip->port = port[dev];
647 xirq = irq[dev];
648 xdma1 = dma1[dev];
649 xdma2 = dma2[dev];
650 if (xdma2 < 0)
651 chip->single_dma = 1;
652 err = snd_opl3sa2_detect(card);
653 if (err < 0)
654 return err;
655 err = request_irq(xirq, snd_opl3sa2_interrupt, 0,
656 "OPL3-SA2", card);
657 if (err) {
658 snd_printk(KERN_ERR PFX "can't grab IRQ %d\n", xirq);
659 return -ENODEV;
661 chip->irq = xirq;
662 card->sync_irq = chip->irq;
663 err = snd_wss_create(card,
664 wss_port[dev] + 4, -1,
665 xirq, xdma1, xdma2,
666 WSS_HW_OPL3SA2, WSS_HWSHARE_IRQ, &wss);
667 if (err < 0) {
668 snd_printd("Oops, WSS not detected at 0x%lx\n", wss_port[dev] + 4);
669 return err;
671 chip->wss = wss;
672 err = snd_wss_pcm(wss, 0);
673 if (err < 0)
674 return err;
675 err = snd_wss_mixer(wss);
676 if (err < 0)
677 return err;
678 err = snd_opl3sa2_mixer(card);
679 if (err < 0)
680 return err;
681 err = snd_wss_timer(wss, 0);
682 if (err < 0)
683 return err;
684 if (fm_port[dev] >= 0x340 && fm_port[dev] < 0x400) {
685 if ((err = snd_opl3_create(card, fm_port[dev],
686 fm_port[dev] + 2,
687 OPL3_HW_OPL3, 0, &opl3)) < 0)
688 return err;
689 if ((err = snd_opl3_timer_new(opl3, 1, 2)) < 0)
690 return err;
691 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, &chip->synth)) < 0)
692 return err;
694 if (midi_port[dev] >= 0x300 && midi_port[dev] < 0x340) {
695 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_OPL3SA2,
696 midi_port[dev],
697 MPU401_INFO_IRQ_HOOK, -1,
698 &chip->rmidi)) < 0)
699 return err;
701 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
702 card->shortname, chip->port, xirq, xdma1);
703 if (xdma2 >= 0)
704 sprintf(card->longname + strlen(card->longname), "&%d", xdma2);
706 return snd_card_register(card);
709 #ifdef CONFIG_PNP
710 static int snd_opl3sa2_pnp_detect(struct pnp_dev *pdev,
711 const struct pnp_device_id *id)
713 static int dev;
714 int err;
715 struct snd_card *card;
717 if (pnp_device_is_isapnp(pdev))
718 return -ENOENT; /* we have another procedure - card */
719 for (; dev < SNDRV_CARDS; dev++) {
720 if (enable[dev] && isapnp[dev])
721 break;
723 if (dev >= SNDRV_CARDS)
724 return -ENODEV;
726 err = snd_opl3sa2_card_new(&pdev->dev, dev, &card);
727 if (err < 0)
728 return err;
729 if ((err = snd_opl3sa2_pnp(dev, card->private_data, pdev)) < 0) {
730 snd_card_free(card);
731 return err;
733 if ((err = snd_opl3sa2_probe(card, dev)) < 0) {
734 snd_card_free(card);
735 return err;
737 pnp_set_drvdata(pdev, card);
738 dev++;
739 return 0;
742 static void snd_opl3sa2_pnp_remove(struct pnp_dev *pdev)
744 snd_card_free(pnp_get_drvdata(pdev));
747 #ifdef CONFIG_PM
748 static int snd_opl3sa2_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
750 return snd_opl3sa2_suspend(pnp_get_drvdata(pdev), state);
752 static int snd_opl3sa2_pnp_resume(struct pnp_dev *pdev)
754 return snd_opl3sa2_resume(pnp_get_drvdata(pdev));
756 #endif
758 static struct pnp_driver opl3sa2_pnp_driver = {
759 .name = "snd-opl3sa2-pnpbios",
760 .id_table = snd_opl3sa2_pnpbiosids,
761 .probe = snd_opl3sa2_pnp_detect,
762 .remove = snd_opl3sa2_pnp_remove,
763 #ifdef CONFIG_PM
764 .suspend = snd_opl3sa2_pnp_suspend,
765 .resume = snd_opl3sa2_pnp_resume,
766 #endif
769 static int snd_opl3sa2_pnp_cdetect(struct pnp_card_link *pcard,
770 const struct pnp_card_device_id *id)
772 static int dev;
773 struct pnp_dev *pdev;
774 int err;
775 struct snd_card *card;
777 pdev = pnp_request_card_device(pcard, id->devs[0].id, NULL);
778 if (pdev == NULL) {
779 snd_printk(KERN_ERR PFX "can't get pnp device from id '%s'\n",
780 id->devs[0].id);
781 return -EBUSY;
783 for (; dev < SNDRV_CARDS; dev++) {
784 if (enable[dev] && isapnp[dev])
785 break;
787 if (dev >= SNDRV_CARDS)
788 return -ENODEV;
790 err = snd_opl3sa2_card_new(&pdev->dev, dev, &card);
791 if (err < 0)
792 return err;
793 if ((err = snd_opl3sa2_pnp(dev, card->private_data, pdev)) < 0) {
794 snd_card_free(card);
795 return err;
797 if ((err = snd_opl3sa2_probe(card, dev)) < 0) {
798 snd_card_free(card);
799 return err;
801 pnp_set_card_drvdata(pcard, card);
802 dev++;
803 return 0;
806 static void snd_opl3sa2_pnp_cremove(struct pnp_card_link *pcard)
808 snd_card_free(pnp_get_card_drvdata(pcard));
809 pnp_set_card_drvdata(pcard, NULL);
812 #ifdef CONFIG_PM
813 static int snd_opl3sa2_pnp_csuspend(struct pnp_card_link *pcard, pm_message_t state)
815 return snd_opl3sa2_suspend(pnp_get_card_drvdata(pcard), state);
817 static int snd_opl3sa2_pnp_cresume(struct pnp_card_link *pcard)
819 return snd_opl3sa2_resume(pnp_get_card_drvdata(pcard));
821 #endif
823 static struct pnp_card_driver opl3sa2_pnpc_driver = {
824 .flags = PNP_DRIVER_RES_DISABLE,
825 .name = "snd-opl3sa2-cpnp",
826 .id_table = snd_opl3sa2_pnpids,
827 .probe = snd_opl3sa2_pnp_cdetect,
828 .remove = snd_opl3sa2_pnp_cremove,
829 #ifdef CONFIG_PM
830 .suspend = snd_opl3sa2_pnp_csuspend,
831 .resume = snd_opl3sa2_pnp_cresume,
832 #endif
834 #endif /* CONFIG_PNP */
836 static int snd_opl3sa2_isa_match(struct device *pdev,
837 unsigned int dev)
839 if (!enable[dev])
840 return 0;
841 #ifdef CONFIG_PNP
842 if (isapnp[dev])
843 return 0;
844 #endif
845 if (port[dev] == SNDRV_AUTO_PORT) {
846 snd_printk(KERN_ERR PFX "specify port\n");
847 return 0;
849 if (wss_port[dev] == SNDRV_AUTO_PORT) {
850 snd_printk(KERN_ERR PFX "specify wss_port\n");
851 return 0;
853 if (fm_port[dev] == SNDRV_AUTO_PORT) {
854 snd_printk(KERN_ERR PFX "specify fm_port\n");
855 return 0;
857 if (midi_port[dev] == SNDRV_AUTO_PORT) {
858 snd_printk(KERN_ERR PFX "specify midi_port\n");
859 return 0;
861 return 1;
864 static int snd_opl3sa2_isa_probe(struct device *pdev,
865 unsigned int dev)
867 struct snd_card *card;
868 int err;
870 err = snd_opl3sa2_card_new(pdev, dev, &card);
871 if (err < 0)
872 return err;
873 if ((err = snd_opl3sa2_probe(card, dev)) < 0) {
874 snd_card_free(card);
875 return err;
877 dev_set_drvdata(pdev, card);
878 return 0;
881 static int snd_opl3sa2_isa_remove(struct device *devptr,
882 unsigned int dev)
884 snd_card_free(dev_get_drvdata(devptr));
885 return 0;
888 #ifdef CONFIG_PM
889 static int snd_opl3sa2_isa_suspend(struct device *dev, unsigned int n,
890 pm_message_t state)
892 return snd_opl3sa2_suspend(dev_get_drvdata(dev), state);
895 static int snd_opl3sa2_isa_resume(struct device *dev, unsigned int n)
897 return snd_opl3sa2_resume(dev_get_drvdata(dev));
899 #endif
901 #define DEV_NAME "opl3sa2"
903 static struct isa_driver snd_opl3sa2_isa_driver = {
904 .match = snd_opl3sa2_isa_match,
905 .probe = snd_opl3sa2_isa_probe,
906 .remove = snd_opl3sa2_isa_remove,
907 #ifdef CONFIG_PM
908 .suspend = snd_opl3sa2_isa_suspend,
909 .resume = snd_opl3sa2_isa_resume,
910 #endif
911 .driver = {
912 .name = DEV_NAME
916 static int __init alsa_card_opl3sa2_init(void)
918 int err;
920 err = isa_register_driver(&snd_opl3sa2_isa_driver, SNDRV_CARDS);
921 #ifdef CONFIG_PNP
922 if (!err)
923 isa_registered = 1;
925 err = pnp_register_driver(&opl3sa2_pnp_driver);
926 if (!err)
927 pnp_registered = 1;
929 err = pnp_register_card_driver(&opl3sa2_pnpc_driver);
930 if (!err)
931 pnpc_registered = 1;
933 if (isa_registered || pnp_registered)
934 err = 0;
935 #endif
936 return err;
939 static void __exit alsa_card_opl3sa2_exit(void)
941 #ifdef CONFIG_PNP
942 if (pnpc_registered)
943 pnp_unregister_card_driver(&opl3sa2_pnpc_driver);
944 if (pnp_registered)
945 pnp_unregister_driver(&opl3sa2_pnp_driver);
946 if (isa_registered)
947 #endif
948 isa_unregister_driver(&snd_opl3sa2_isa_driver);
951 module_init(alsa_card_opl3sa2_init)
952 module_exit(alsa_card_opl3sa2_exit)