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