2 * Driver for Yamaha OPL3-SA[2,3] soundcards
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
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.
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.
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
22 #include <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.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>
33 #include <sound/initval.h>
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},"
44 "{Intel,AL440LX sound},"
45 "{NeoMagic,MagicWave 3DX}}");
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 */
51 static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
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*/
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);
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);
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 */
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
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
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
129 #define OPL3SA2_PM_D0 0x00
130 #define OPL3SA2_PM_D3 (OPL3SA2_PM_ADOWN|OPL3SA2_PM_PSV|OPL3SA2_PM_PDN|OPL3SA2_PM_PDX)
132 typedef struct snd_opl3sa2 opl3sa2_t;
133 #define chip_t opl3sa2_t
137 int version; /* 2 or 3 */
138 unsigned long port; /* control port */
139 struct resource *res_port; /* control port resource */
144 snd_rawmidi_t *rmidi;
149 unsigned char ctlregs[0x20];
150 int ymode; /* SL added */
151 snd_kcontrol_t *master_switch;
152 snd_kcontrol_t *master_volume;
154 struct pm_dev *pm_dev;
155 void (*cs4231_suspend)(cs4231_t *);
156 void (*cs4231_resume)(cs4231_t *);
160 static snd_card_t *snd_opl3sa2_legacy[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
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" } } },
174 { .id = "" } /* end */
177 MODULE_DEVICE_TABLE(pnp_card, snd_opl3sa2_pnpids);
179 #endif /* CONFIG_PNP */
182 /* read control port (w/o spinlock) */
183 static unsigned char __snd_opl3sa2_read(opl3sa2_t *chip, unsigned char reg)
185 unsigned char result;
187 outb(0x1d, port); /* password */
188 printk("read [0x%lx] = 0x%x\n", port, inb(port));
190 outb(reg, chip->port); /* register */
191 result = inb(chip->port + 1);
193 printk("read [0x%lx] = 0x%x [0x%x]\n", port, result, inb(port));
198 /* read control port (with spinlock) */
199 static unsigned char snd_opl3sa2_read(opl3sa2_t *chip, unsigned char reg)
202 unsigned char result;
204 spin_lock_irqsave(&chip->reg_lock, flags);
205 result = __snd_opl3sa2_read(chip, reg);
206 spin_unlock_irqrestore(&chip->reg_lock, flags);
210 /* write control port (w/o spinlock) */
211 static void __snd_opl3sa2_write(opl3sa2_t *chip, unsigned char reg, unsigned char value)
214 outb(0x1d, port); /* password */
216 outb(reg, chip->port); /* register */
217 outb(value, chip->port + 1);
218 chip->ctlregs[reg] = value;
221 /* write control port (with spinlock) */
222 static void snd_opl3sa2_write(opl3sa2_t *chip, unsigned char reg, unsigned char value)
225 spin_lock_irqsave(&chip->reg_lock, flags);
226 __snd_opl3sa2_write(chip, reg, value);
227 spin_unlock_irqrestore(&chip->reg_lock, flags);
230 static int __init snd_opl3sa2_detect(opl3sa2_t *chip)
234 unsigned char tmp, tmp1;
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);
243 // snd_printk("REG 0A = 0x%x\n", snd_opl3sa2_read(chip, 0x0a));
245 tmp = snd_opl3sa2_read(chip, OPL3SA2_MISC);
247 snd_printd("OPL3-SA [0x%lx] detect = 0x%x\n", port, tmp);
250 switch (tmp & 0x07) {
252 chip->version = 2; /* YMF711 */
256 /* 0x02 - standard */
258 /* 0x04 - YM719 - OPL-SA4? */
259 /* 0x05 - OPL3-SA3 - Libretto 100 */
262 str[0] = chip->version + '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);
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);
277 snd_opl3sa2_write(chip, OPL3SA2_MIC, 0x9f);
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));
285 /* default for opl3sa2 versions */
286 snd_opl3sa2_write(chip, OPL3SA2_SYS_CTRL, 0x00);
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 */
292 snd_opl3sa2_write(chip, OPL3SA2_DMA_CONFIG, 0x21); /* DMA Configuration - DMA B = WSS-R, DMA A = WSS-P */
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 */
302 static irqreturn_t snd_opl3sa2_interrupt(int irq, void *dev_id, struct pt_regs *regs)
304 unsigned short status;
305 opl3sa2_t *chip = snd_magic_cast(opl3sa2_t, dev_id, return IRQ_NONE);
308 if (chip == NULL || chip->card == NULL)
311 status = snd_opl3sa2_read(chip, OPL3SA2_IRQ_STATUS);
315 snd_opl3_interrupt(chip->synth);
318 if ((status & 0x10) && chip->rmidi != NULL) {
320 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
323 if (status & 0x07) { /* TI,CI,PI */
325 snd_cs4231_interrupt(irq, chip->cs4231, regs);
328 if (status & 0x40) { /* hardware volume change */
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);
338 return IRQ_RETVAL(handled);
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) }
347 static int snd_opl3sa2_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
349 int mask = (kcontrol->private_value >> 16) & 0xff;
351 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
353 uinfo->value.integer.min = 0;
354 uinfo->value.integer.max = mask;
358 int snd_opl3sa2_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
360 opl3sa2_t *chip = snd_kcontrol_chip(kcontrol);
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;
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);
371 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
375 int snd_opl3sa2_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
377 opl3sa2_t *chip = snd_kcontrol_chip(kcontrol);
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;
384 unsigned short val, oval;
386 val = (ucontrol->value.integer.value[0] & mask);
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);
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) }
405 int snd_opl3sa2_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
407 int mask = (kcontrol->private_value >> 24) & 0xff;
409 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
411 uinfo->value.integer.min = 0;
412 uinfo->value.integer.max = mask;
416 int snd_opl3sa2_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
418 opl3sa2_t *chip = snd_kcontrol_chip(kcontrol);
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;
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);
432 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
433 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
438 int snd_opl3sa2_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
440 opl3sa2_t *chip = snd_kcontrol_chip(kcontrol);
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;
449 unsigned short val1, val2, oval1, oval2;
451 val1 = ucontrol->value.integer.value[0] & mask;
452 val2 = ucontrol->value.integer.value[1] & mask;
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);
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);
474 spin_unlock_irqrestore(&chip->reg_lock, flags);
478 #define OPL3SA2_CONTROLS (sizeof(snd_opl3sa2_controls)/sizeof(snd_kcontrol_new_t))
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)
487 #define OPL3SA2_TONE_CONTROLS (sizeof(snd_opl3sa2_tone_controls)/sizeof(snd_kcontrol_new_t))
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)
495 static void snd_opl3sa2_master_free(snd_kcontrol_t *kcontrol)
497 opl3sa2_t *chip = snd_magic_cast(opl3sa2_t, _snd_kcontrol_chip(kcontrol), return);
498 chip->master_switch = NULL;
499 chip->master_volume = NULL;
502 static int __init snd_opl3sa2_mixer(opl3sa2_t *chip)
504 snd_card_t *card = chip->card;
505 snd_ctl_elem_id_t id1, id2;
506 snd_kcontrol_t *kctl;
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)
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)
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)
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)
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)
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;
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)
548 /* Power Management support functions */
550 static void snd_opl3sa2_suspend(opl3sa2_t *chip)
552 snd_card_t *card = chip->card;
554 if (card->power_state == SNDRV_CTL_POWER_D3hot)
557 snd_pcm_suspend_all(chip->cs4231->pcm); /* stop before saving regs */
558 chip->cs4231_suspend(chip->cs4231);
561 snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D3);
563 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
566 static void snd_opl3sa2_resume(opl3sa2_t *chip)
568 snd_card_t *card = chip->card;
571 if (card->power_state == SNDRV_CTL_POWER_D0)
575 snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D0);
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]);
582 if (chip->version > 2) {
583 for (i = 0x12; i <= 0x16; i++)
584 snd_opl3sa2_write(chip, i, chip->ctlregs[i]);
587 chip->cs4231_resume(chip->cs4231);
589 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
592 /* callback for control API */
593 static int snd_opl3sa2_set_power_state(snd_card_t *card, unsigned int power_state)
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);
602 case SNDRV_CTL_POWER_D3hot:
603 case SNDRV_CTL_POWER_D3cold:
604 snd_opl3sa2_suspend(chip);
612 static int snd_opl3sa2_pm_callback(struct pm_dev *dev, pm_request_t rqst, void *data)
614 opl3sa2_t *chip = snd_magic_cast(opl3sa2_t, dev->data, return 0);
618 snd_opl3sa2_suspend(chip);
621 snd_opl3sa2_resume(chip);
627 #endif /* CONFIG_PM */
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)
634 struct pnp_dev *pdev;
635 struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
640 pdev = chip->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
641 if (chip->dev == NULL) {
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);
665 snd_printk(KERN_ERR "PnP manual resources are invalid, using auto config\n");
666 err = pnp_activate_dev(pdev);
669 snd_printk(KERN_ERR "PnP configure failure (out of resources?) err = %d\n", err);
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]);
687 #endif /* CONFIG_PNP */
689 static int snd_opl3sa2_free(opl3sa2_t *chip)
693 pm_unregister(chip->pm_dev);
696 free_irq(chip->irq, (void *)chip);
697 if (chip->res_port) {
698 release_resource(chip->res_port);
699 kfree_nocheck(chip->res_port);
701 snd_magic_kfree(chip);
705 static int snd_opl3sa2_dev_free(snd_device_t *device)
707 opl3sa2_t *chip = snd_magic_cast(opl3sa2_t, device->device_data, return -ENXIO);
708 return snd_opl3sa2_free(chip);
711 static int __devinit snd_opl3sa2_probe(int dev,
712 struct pnp_card_link *pcard,
713 const struct pnp_card_device_id *pid)
715 int xirq, xdma1, xdma2;
717 struct snd_opl3sa2 *chip;
720 static snd_device_ops_t ops = {
721 .dev_free = snd_opl3sa2_dev_free,
728 if (port[dev] == SNDRV_AUTO_PORT) {
729 snd_printk("specify port\n");
732 if (wss_port[dev] == SNDRV_AUTO_PORT) {
733 snd_printk("specify wss_port\n");
736 if (fm_port[dev] == SNDRV_AUTO_PORT) {
737 snd_printk("specify fm_port\n");
740 if (midi_port[dev] == SNDRV_AUTO_PORT) {
741 snd_printk("specify midi_port\n");
747 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
750 strcpy(card->driver, "OPL3SA2");
751 strcpy(card->shortname, "Yamaha OPL3-SA2");
752 chip = snd_magic_kcalloc(opl3sa2_t, 0, GFP_KERNEL);
757 spin_lock_init(&chip->reg_lock);
759 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
763 if ((err = snd_opl3sa2_pnp(dev, chip, pcard, pid)) < 0)
765 snd_card_set_dev(card, &pcard->card->dev);
768 chip->ymode = opl3sa3_ymode[dev] & 0x03 ; /* initialise this card from supplied (or default) parameter*/
770 chip->port = port[dev];
775 chip->single_dma = 1;
776 if ((err = snd_opl3sa2_detect(chip)) < 0)
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);
784 if ((err = snd_cs4231_create(card,
785 wss_port[dev] + 4, -1,
790 snd_printd("Oops, WSS not detected at 0x%lx\n", wss_port[dev] + 4);
793 chip->cs4231 = cs4231;
794 if ((err = snd_cs4231_pcm(cs4231, 0, NULL)) < 0)
796 if ((err = snd_cs4231_mixer(cs4231)) < 0)
798 if ((err = snd_opl3sa2_mixer(chip)) < 0)
800 if ((err = snd_cs4231_timer(cs4231, 0, NULL)) < 0)
802 if (fm_port[dev] >= 0x340 && fm_port[dev] < 0x400) {
803 if ((err = snd_opl3_create(card, fm_port[dev],
805 OPL3_HW_OPL3, 0, &opl3)) < 0)
807 if ((err = snd_opl3_timer_new(opl3, 1, 2)) < 0)
809 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, &chip->synth)) < 0)
812 if (midi_port[dev] >= 0x300 && midi_port[dev] < 0x340) {
813 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_OPL3SA2,
815 xirq, 0, &chip->rmidi)) < 0)
819 /* Power Management */
820 chip->pm_dev = pm_register(PM_ISA_DEV, 0, snd_opl3sa2_pm_callback);
822 chip->pm_dev->data = chip;
823 /* remember callbacks for cs4231 - they are called inside
824 * opl3sa2 pm callback
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;
837 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
838 card->shortname, chip->port, xirq, xdma1);
840 sprintf(card->longname + strlen(card->longname), "&%d", xdma2);
842 if ((err = snd_card_register(card)) < 0)
846 pnp_set_card_drvdata(pcard, card);
848 snd_opl3sa2_legacy[dev] = card;
857 static int __devinit snd_opl3sa2_pnp_detect(struct pnp_card_link *card,
858 const struct pnp_card_device_id *id)
863 for ( ; dev < SNDRV_CARDS; dev++) {
864 if (!enable[dev] && !isapnp[dev])
866 res = snd_opl3sa2_probe(dev, card, id);
875 static void __devexit snd_opl3sa2_pnp_remove(struct pnp_card_link * pcard)
877 snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
879 snd_card_disconnect(card);
880 snd_card_free_in_thread(card);
883 static struct pnp_card_driver opl3sa2_pnpc_driver = {
884 .flags = PNP_DRIVER_RES_DISABLE,
886 .id_table = snd_opl3sa2_pnpids,
887 .probe = snd_opl3sa2_pnp_detect,
888 .remove = __devexit_p(snd_opl3sa2_pnp_remove),
890 #endif /* CONFIG_PNP */
892 static int __init alsa_card_opl3sa2_init(void)
896 for (dev = 0; dev < SNDRV_CARDS; dev++) {
903 if (snd_opl3sa2_probe(dev, NULL, NULL) >= 0)
907 cards += pnp_register_card_driver(&opl3sa2_pnpc_driver);
911 snd_printk(KERN_ERR "Yamaha OPL3-SA soundcard not found or device busy\n");
914 pnp_unregister_card_driver(&opl3sa2_pnpc_driver);
921 static void __exit alsa_card_opl3sa2_exit(void)
926 /* PnP cards first */
927 pnp_unregister_card_driver(&opl3sa2_pnpc_driver);
929 for (idx = 0; idx < SNDRV_CARDS; idx++)
930 snd_card_free(snd_opl3sa2_legacy[idx]);
933 module_init(alsa_card_opl3sa2_init)
934 module_exit(alsa_card_opl3sa2_exit)
938 /* format is: snd-opl3sa2=enable,index,id,isapnp,
939 port,sb_port,wss_port,fm_port,
940 midi_port,irq,dma1,dma2,
943 static int __init alsa_card_opl3sa2_setup(char *str)
945 static unsigned __initdata nr_dev = 0;
946 int __attribute__ ((__unused__)) pnp = INT_MAX;
948 if (nr_dev >= SNDRV_CARDS)
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);
965 isapnp[nr_dev] = pnp;
971 __setup("snd-opl3sa2=", alsa_card_opl3sa2_setup);
973 #endif /* ifndef MODULE */