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