1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Yamaha OPL3-SA[2,3] soundcards
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
7 #include <linux/init.h>
10 #include <linux/interrupt.h>
12 #include <linux/pnp.h>
13 #include <linux/module.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},"
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 */
35 static bool isapnp
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
- 1)] = 1};
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.");
54 module_param_array(isapnp
, bool, NULL
, 0444);
55 MODULE_PARM_DESC(isapnp
, "PnP detection for specified soundcard.");
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.");
77 static int isa_registered
;
78 static int pnp_registered
;
79 static int pnpc_registered
;
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
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)
110 int version
; /* 2 or 3 */
111 unsigned long port
; /* control port */
112 struct resource
*res_port
; /* control port resource */
116 struct snd_hwdep
*synth
;
117 struct snd_rawmidi
*rmidi
;
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: "
129 static const struct pnp_device_id snd_opl3sa2_pnpbiosids
[] = {
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" } } },
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
;
164 outb(0x1d, port
); /* password */
165 printk(KERN_DEBUG
"read [0x%lx] = 0x%x\n", port
, inb(port
));
167 outb(reg
, chip
->port
); /* register */
168 result
= inb(chip
->port
+ 1);
170 printk(KERN_DEBUG
"read [0x%lx] = 0x%x [0x%x]\n",
171 port
, result
, inb(port
));
176 /* read control port (with spinlock) */
177 static unsigned char snd_opl3sa2_read(struct snd_opl3sa2
*chip
, unsigned char reg
)
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
);
188 /* write control port (w/o spinlock) */
189 static void __snd_opl3sa2_write(struct snd_opl3sa2
*chip
, unsigned char reg
, unsigned char value
)
192 outb(0x1d, port
); /* password */
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
)
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
;
212 unsigned char tmp
, tmp1
;
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
);
221 snd_printk(KERN_DEBUG "REG 0A = 0x%x\n",
222 snd_opl3sa2_read(chip, 0x0a));
225 tmp
= snd_opl3sa2_read(chip
, OPL3SA2_MISC
);
227 snd_printd("OPL3-SA [0x%lx] detect = 0x%x\n", port
, tmp
);
230 switch (tmp
& 0x07) {
232 chip
->version
= 2; /* YMF711 */
236 /* 0x02 - standard */
238 /* 0x04 - YM719 - OPL-SA4? */
239 /* 0x05 - OPL3-SA3 - Libretto 100 */
240 /* 0x07 - unknown - Neomagic MagicWave 3D */
243 str
[0] = chip
->version
+ '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
);
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
);
258 snd_opl3sa2_write(chip
, OPL3SA2_MIC
, 0x9f);
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));
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 */
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 */
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
;
293 chip
= card
->private_data
;
294 status
= snd_opl3sa2_read(chip
, OPL3SA2_IRQ_STATUS
);
298 snd_opl3_interrupt(chip
->synth
);
301 if ((status
& 0x10) && chip
->rmidi
!= NULL
) {
303 snd_mpu401_uart_interrupt(irq
, chip
->rmidi
->private_data
);
306 if (status
& 0x07) { /* TI,CI,PI */
308 snd_wss_interrupt(irq
, chip
->wss
);
311 if (status
& 0x40) { /* hardware volume change */
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
);
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
);
353 ucontrol
->value
.integer
.value
[0] = mask
- ucontrol
->value
.integer
.value
[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
);
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;
366 unsigned short val
, oval
;
368 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
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
);
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
);
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
);
411 ucontrol
->value
.integer
.value
[0] = mask
- ucontrol
->value
.integer
.value
[0];
412 ucontrol
->value
.integer
.value
[1] = mask
- ucontrol
->value
.integer
.value
[1];
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
);
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;
428 unsigned short val1
, val2
, oval1
, oval2
;
430 val1
= ucontrol
->value
.integer
.value
[0] & mask
;
431 val2
= ucontrol
->value
.integer
.value
[1] & mask
;
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
);
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
);
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,
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
;
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");
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");
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");
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");
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)
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)
537 /* Power Management support functions */
539 static int snd_opl3sa2_suspend(struct snd_card
*card
, pm_message_t state
)
542 struct snd_opl3sa2
*chip
= card
->private_data
;
544 snd_power_change_state(card
, SNDRV_CTL_POWER_D3hot
);
545 chip
->wss
->suspend(chip
->wss
);
547 snd_opl3sa2_write(chip
, OPL3SA2_PM_CTRL
, OPL3SA2_PM_D3
);
553 static int snd_opl3sa2_resume(struct snd_card
*card
)
555 struct snd_opl3sa2
*chip
;
561 chip
= card
->private_data
;
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
]);
575 chip
->wss
->resume(chip
->wss
);
577 snd_power_change_state(card
, SNDRV_CTL_POWER_D0
);
580 #endif /* CONFIG_PM */
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");
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
]);
604 #endif /* CONFIG_PNP */
606 static void snd_opl3sa2_free(struct snd_card
*card
)
608 struct snd_opl3sa2
*chip
= card
->private_data
;
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
;
621 err
= snd_card_new(pdev
, index
[dev
], id
[dev
], THIS_MODULE
,
622 sizeof(struct snd_opl3sa2
), &card
);
625 strcpy(card
->driver
, "OPL3SA2");
626 strcpy(card
->shortname
, "Yamaha OPL3-SA");
627 chip
= card
->private_data
;
628 spin_lock_init(&chip
->reg_lock
);
630 card
->private_free
= snd_opl3sa2_free
;
635 static int snd_opl3sa2_probe(struct snd_card
*card
, int dev
)
637 int xirq
, xdma1
, xdma2
;
638 struct snd_opl3sa2
*chip
;
640 struct snd_opl3
*opl3
;
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
];
651 chip
->single_dma
= 1;
652 err
= snd_opl3sa2_detect(card
);
655 err
= request_irq(xirq
, snd_opl3sa2_interrupt
, 0,
658 snd_printk(KERN_ERR PFX
"can't grab IRQ %d\n", xirq
);
662 card
->sync_irq
= chip
->irq
;
663 err
= snd_wss_create(card
,
664 wss_port
[dev
] + 4, -1,
666 WSS_HW_OPL3SA2
, WSS_HWSHARE_IRQ
, &wss
);
668 snd_printd("Oops, WSS not detected at 0x%lx\n", wss_port
[dev
] + 4);
672 err
= snd_wss_pcm(wss
, 0);
675 err
= snd_wss_mixer(wss
);
678 err
= snd_opl3sa2_mixer(card
);
681 err
= snd_wss_timer(wss
, 0);
684 if (fm_port
[dev
] >= 0x340 && fm_port
[dev
] < 0x400) {
685 if ((err
= snd_opl3_create(card
, fm_port
[dev
],
687 OPL3_HW_OPL3
, 0, &opl3
)) < 0)
689 if ((err
= snd_opl3_timer_new(opl3
, 1, 2)) < 0)
691 if ((err
= snd_opl3_hwdep_new(opl3
, 0, 1, &chip
->synth
)) < 0)
694 if (midi_port
[dev
] >= 0x300 && midi_port
[dev
] < 0x340) {
695 if ((err
= snd_mpu401_uart_new(card
, 0, MPU401_HW_OPL3SA2
,
697 MPU401_INFO_IRQ_HOOK
, -1,
701 sprintf(card
->longname
, "%s at 0x%lx, irq %d, dma %d",
702 card
->shortname
, chip
->port
, xirq
, xdma1
);
704 sprintf(card
->longname
+ strlen(card
->longname
), "&%d", xdma2
);
706 return snd_card_register(card
);
710 static int snd_opl3sa2_pnp_detect(struct pnp_dev
*pdev
,
711 const struct pnp_device_id
*id
)
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
])
723 if (dev
>= SNDRV_CARDS
)
726 err
= snd_opl3sa2_card_new(&pdev
->dev
, dev
, &card
);
729 if ((err
= snd_opl3sa2_pnp(dev
, card
->private_data
, pdev
)) < 0) {
733 if ((err
= snd_opl3sa2_probe(card
, dev
)) < 0) {
737 pnp_set_drvdata(pdev
, card
);
742 static void snd_opl3sa2_pnp_remove(struct pnp_dev
*pdev
)
744 snd_card_free(pnp_get_drvdata(pdev
));
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
));
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
,
764 .suspend
= snd_opl3sa2_pnp_suspend
,
765 .resume
= snd_opl3sa2_pnp_resume
,
769 static int snd_opl3sa2_pnp_cdetect(struct pnp_card_link
*pcard
,
770 const struct pnp_card_device_id
*id
)
773 struct pnp_dev
*pdev
;
775 struct snd_card
*card
;
777 pdev
= pnp_request_card_device(pcard
, id
->devs
[0].id
, NULL
);
779 snd_printk(KERN_ERR PFX
"can't get pnp device from id '%s'\n",
783 for (; dev
< SNDRV_CARDS
; dev
++) {
784 if (enable
[dev
] && isapnp
[dev
])
787 if (dev
>= SNDRV_CARDS
)
790 err
= snd_opl3sa2_card_new(&pdev
->dev
, dev
, &card
);
793 if ((err
= snd_opl3sa2_pnp(dev
, card
->private_data
, pdev
)) < 0) {
797 if ((err
= snd_opl3sa2_probe(card
, dev
)) < 0) {
801 pnp_set_card_drvdata(pcard
, card
);
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
);
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
));
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
,
830 .suspend
= snd_opl3sa2_pnp_csuspend
,
831 .resume
= snd_opl3sa2_pnp_cresume
,
834 #endif /* CONFIG_PNP */
836 static int snd_opl3sa2_isa_match(struct device
*pdev
,
845 if (port
[dev
] == SNDRV_AUTO_PORT
) {
846 snd_printk(KERN_ERR PFX
"specify port\n");
849 if (wss_port
[dev
] == SNDRV_AUTO_PORT
) {
850 snd_printk(KERN_ERR PFX
"specify wss_port\n");
853 if (fm_port
[dev
] == SNDRV_AUTO_PORT
) {
854 snd_printk(KERN_ERR PFX
"specify fm_port\n");
857 if (midi_port
[dev
] == SNDRV_AUTO_PORT
) {
858 snd_printk(KERN_ERR PFX
"specify midi_port\n");
864 static int snd_opl3sa2_isa_probe(struct device
*pdev
,
867 struct snd_card
*card
;
870 err
= snd_opl3sa2_card_new(pdev
, dev
, &card
);
873 if ((err
= snd_opl3sa2_probe(card
, dev
)) < 0) {
877 dev_set_drvdata(pdev
, card
);
881 static int snd_opl3sa2_isa_remove(struct device
*devptr
,
884 snd_card_free(dev_get_drvdata(devptr
));
889 static int snd_opl3sa2_isa_suspend(struct device
*dev
, unsigned int n
,
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
));
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
,
908 .suspend
= snd_opl3sa2_isa_suspend
,
909 .resume
= snd_opl3sa2_isa_resume
,
916 static int __init
alsa_card_opl3sa2_init(void)
920 err
= isa_register_driver(&snd_opl3sa2_isa_driver
, SNDRV_CARDS
);
925 err
= pnp_register_driver(&opl3sa2_pnp_driver
);
929 err
= pnp_register_card_driver(&opl3sa2_pnpc_driver
);
933 if (isa_registered
|| pnp_registered
)
939 static void __exit
alsa_card_opl3sa2_exit(void)
943 pnp_unregister_card_driver(&opl3sa2_pnpc_driver
);
945 pnp_unregister_driver(&opl3sa2_pnp_driver
);
948 isa_unregister_driver(&snd_opl3sa2_isa_driver
);
951 module_init(alsa_card_opl3sa2_init
)
952 module_exit(alsa_card_opl3sa2_exit
)