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