blob: ca359e0c674b9bad8d4f677ff544a52a07991d8a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for Yamaha OPL3-SA[2,3] soundcards
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <sound/driver.h>
23#include <linux/init.h>
Takashi Iwai704e0522005-11-17 17:11:53 +010024#include <linux/err.h>
25#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/interrupt.h>
27#include <linux/pm.h>
28#include <linux/slab.h>
29#include <linux/pnp.h>
30#include <linux/moduleparam.h>
31#include <sound/core.h>
32#include <sound/cs4231.h>
33#include <sound/mpu401.h>
34#include <sound/opl3.h>
35#include <sound/initval.h>
36
37#include <asm/io.h>
38
39MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
40MODULE_DESCRIPTION("Yamaha OPL3SA2+");
41MODULE_LICENSE("GPL");
42MODULE_SUPPORTED_DEVICE("{{Yamaha,YMF719E-S},"
43 "{Genius,Sound Maker 3DX},"
44 "{Yamaha,OPL3SA3},"
45 "{Intel,AL440LX sound},"
46 "{NeoMagic,MagicWave 3DX}}");
47
48static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
49static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
50static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
51#ifdef CONFIG_PNP
52static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
53#endif
54static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0xf86,0x370,0x100 */
55static long sb_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260 */
56static long wss_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;/* 0x530,0xe80,0xf40,0x604 */
57static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x388 */
58static long midi_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;/* 0x330,0x300 */
59static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 0,1,3,5,9,11,12,15 */
60static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
61static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
62static int opl3sa3_ymode[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = 0 }; /* 0,1,2,3 */ /*SL Added*/
63
64module_param_array(index, int, NULL, 0444);
65MODULE_PARM_DESC(index, "Index value for OPL3-SA soundcard.");
66module_param_array(id, charp, NULL, 0444);
67MODULE_PARM_DESC(id, "ID string for OPL3-SA soundcard.");
68module_param_array(enable, bool, NULL, 0444);
69MODULE_PARM_DESC(enable, "Enable OPL3-SA soundcard.");
70#ifdef CONFIG_PNP
71module_param_array(isapnp, bool, NULL, 0444);
72MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
73#endif
74module_param_array(port, long, NULL, 0444);
75MODULE_PARM_DESC(port, "Port # for OPL3-SA driver.");
76module_param_array(sb_port, long, NULL, 0444);
77MODULE_PARM_DESC(sb_port, "SB port # for OPL3-SA driver.");
78module_param_array(wss_port, long, NULL, 0444);
79MODULE_PARM_DESC(wss_port, "WSS port # for OPL3-SA driver.");
80module_param_array(fm_port, long, NULL, 0444);
81MODULE_PARM_DESC(fm_port, "FM port # for OPL3-SA driver.");
82module_param_array(midi_port, long, NULL, 0444);
83MODULE_PARM_DESC(midi_port, "MIDI port # for OPL3-SA driver.");
84module_param_array(irq, int, NULL, 0444);
85MODULE_PARM_DESC(irq, "IRQ # for OPL3-SA driver.");
86module_param_array(dma1, int, NULL, 0444);
87MODULE_PARM_DESC(dma1, "DMA1 # for OPL3-SA driver.");
88module_param_array(dma2, int, NULL, 0444);
89MODULE_PARM_DESC(dma2, "DMA2 # for OPL3-SA driver.");
90module_param_array(opl3sa3_ymode, int, NULL, 0444);
91MODULE_PARM_DESC(opl3sa3_ymode, "Speaker size selection for 3D Enhancement mode: Desktop/Large Notebook/Small Notebook/HiFi.");
92
Clemens Ladischf7a92752005-12-07 09:13:42 +010093static struct platform_device *platform_devices[SNDRV_CARDS];
94static int pnp_registered;
95static int pnpc_registered;
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/* control ports */
98#define OPL3SA2_PM_CTRL 0x01
99#define OPL3SA2_SYS_CTRL 0x02
100#define OPL3SA2_IRQ_CONFIG 0x03
101#define OPL3SA2_IRQ_STATUS 0x04
102#define OPL3SA2_DMA_CONFIG 0x06
103#define OPL3SA2_MASTER_LEFT 0x07
104#define OPL3SA2_MASTER_RIGHT 0x08
105#define OPL3SA2_MIC 0x09
106#define OPL3SA2_MISC 0x0A
107
108/* opl3sa3 only */
109#define OPL3SA3_DGTL_DOWN 0x12
110#define OPL3SA3_ANLG_DOWN 0x13
111#define OPL3SA3_WIDE 0x14
112#define OPL3SA3_BASS 0x15
113#define OPL3SA3_TREBLE 0x16
114
115/* power management bits */
116#define OPL3SA2_PM_ADOWN 0x20
117#define OPL3SA2_PM_PSV 0x04
118#define OPL3SA2_PM_PDN 0x02
119#define OPL3SA2_PM_PDX 0x01
120
121#define OPL3SA2_PM_D0 0x00
122#define OPL3SA2_PM_D3 (OPL3SA2_PM_ADOWN|OPL3SA2_PM_PSV|OPL3SA2_PM_PDN|OPL3SA2_PM_PDX)
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124struct snd_opl3sa2 {
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100125 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int version; /* 2 or 3 */
127 unsigned long port; /* control port */
128 struct resource *res_port; /* control port resource */
129 int irq;
130 int single_dma;
131 spinlock_t reg_lock;
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100132 struct snd_hwdep *synth;
133 struct snd_rawmidi *rmidi;
134 struct snd_cs4231 *cs4231;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 unsigned char ctlregs[0x20];
136 int ymode; /* SL added */
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100137 struct snd_kcontrol *master_switch;
138 struct snd_kcontrol *master_volume;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
Takashi Iwai43bcd972005-09-05 17:19:20 +0200141#define PFX "opl3sa2: "
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#ifdef CONFIG_PNP
144
Jaroslav Kysela12655092005-08-12 17:24:24 +0200145static struct pnp_device_id snd_opl3sa2_pnpbiosids[] = {
Jaroslav Kysela53b26662005-08-12 17:35:09 +0200146 { .id = "YMH0021" },
Jaroslav Kysela12655092005-08-12 17:24:24 +0200147 { .id = "NMX2210" }, /* Gateway Solo 2500 */
148 { .id = "" } /* end */
149};
150
151MODULE_DEVICE_TABLE(pnp, snd_opl3sa2_pnpbiosids);
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static struct pnp_card_device_id snd_opl3sa2_pnpids[] = {
154 /* Yamaha YMF719E-S (Genius Sound Maker 3DX) */
155 { .id = "YMH0020", .devs = { { "YMH0021" } } },
156 /* Yamaha OPL3-SA3 (integrated on Intel's Pentium II AL440LX motherboard) */
157 { .id = "YMH0030", .devs = { { "YMH0021" } } },
158 /* Yamaha OPL3-SA2 */
159 { .id = "YMH0800", .devs = { { "YMH0021" } } },
160 /* Yamaha OPL3-SA2 */
161 { .id = "YMH0801", .devs = { { "YMH0021" } } },
162 /* NeoMagic MagicWave 3DX */
163 { .id = "NMX2200", .devs = { { "YMH2210" } } },
164 /* --- */
165 { .id = "" } /* end */
166};
167
168MODULE_DEVICE_TABLE(pnp_card, snd_opl3sa2_pnpids);
169
170#endif /* CONFIG_PNP */
171
172
173/* read control port (w/o spinlock) */
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100174static unsigned char __snd_opl3sa2_read(struct snd_opl3sa2 *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 unsigned char result;
177#if 0
178 outb(0x1d, port); /* password */
179 printk("read [0x%lx] = 0x%x\n", port, inb(port));
180#endif
181 outb(reg, chip->port); /* register */
182 result = inb(chip->port + 1);
183#if 0
184 printk("read [0x%lx] = 0x%x [0x%x]\n", port, result, inb(port));
185#endif
186 return result;
187}
188
189/* read control port (with spinlock) */
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100190static unsigned char snd_opl3sa2_read(struct snd_opl3sa2 *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 unsigned long flags;
193 unsigned char result;
194
195 spin_lock_irqsave(&chip->reg_lock, flags);
196 result = __snd_opl3sa2_read(chip, reg);
197 spin_unlock_irqrestore(&chip->reg_lock, flags);
198 return result;
199}
200
201/* write control port (w/o spinlock) */
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100202static void __snd_opl3sa2_write(struct snd_opl3sa2 *chip, unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204#if 0
205 outb(0x1d, port); /* password */
206#endif
207 outb(reg, chip->port); /* register */
208 outb(value, chip->port + 1);
209 chip->ctlregs[reg] = value;
210}
211
212/* write control port (with spinlock) */
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100213static void snd_opl3sa2_write(struct snd_opl3sa2 *chip, unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 unsigned long flags;
216 spin_lock_irqsave(&chip->reg_lock, flags);
217 __snd_opl3sa2_write(chip, reg, value);
218 spin_unlock_irqrestore(&chip->reg_lock, flags);
219}
220
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100221static int __init snd_opl3sa2_detect(struct snd_opl3sa2 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100223 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 unsigned long port;
225 unsigned char tmp, tmp1;
226 char str[2];
227
228 card = chip->card;
229 port = chip->port;
230 if ((chip->res_port = request_region(port, 2, "OPL3-SA control")) == NULL) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200231 snd_printk(KERN_ERR PFX "can't grab port 0x%lx\n", port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return -EBUSY;
233 }
234 // snd_printk("REG 0A = 0x%x\n", snd_opl3sa2_read(chip, 0x0a));
235 chip->version = 0;
236 tmp = snd_opl3sa2_read(chip, OPL3SA2_MISC);
237 if (tmp == 0xff) {
238 snd_printd("OPL3-SA [0x%lx] detect = 0x%x\n", port, tmp);
239 return -ENODEV;
240 }
241 switch (tmp & 0x07) {
242 case 0x01:
243 chip->version = 2; /* YMF711 */
244 break;
245 default:
246 chip->version = 3;
247 /* 0x02 - standard */
248 /* 0x03 - YM715B */
249 /* 0x04 - YM719 - OPL-SA4? */
250 /* 0x05 - OPL3-SA3 - Libretto 100 */
251 break;
252 }
253 str[0] = chip->version + '0';
254 str[1] = 0;
255 strcat(card->shortname, str);
256 snd_opl3sa2_write(chip, OPL3SA2_MISC, tmp ^ 7);
257 if ((tmp1 = snd_opl3sa2_read(chip, OPL3SA2_MISC)) != tmp) {
258 snd_printd("OPL3-SA [0x%lx] detect (1) = 0x%x (0x%x)\n", port, tmp, tmp1);
259 return -ENODEV;
260 }
261 /* try if the MIC register is accesible */
262 tmp = snd_opl3sa2_read(chip, OPL3SA2_MIC);
263 snd_opl3sa2_write(chip, OPL3SA2_MIC, 0x8a);
264 if (((tmp1 = snd_opl3sa2_read(chip, OPL3SA2_MIC)) & 0x9f) != 0x8a) {
265 snd_printd("OPL3-SA [0x%lx] detect (2) = 0x%x (0x%x)\n", port, tmp, tmp1);
266 return -ENODEV;
267 }
268 snd_opl3sa2_write(chip, OPL3SA2_MIC, 0x9f);
269 /* initialization */
270 /* Power Management - full on */
271 snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D0);
272 if (chip->version > 2) {
273 /* ymode is bits 4&5 (of 0 to 7) on all but opl3sa2 versions */
274 snd_opl3sa2_write(chip, OPL3SA2_SYS_CTRL, (chip->ymode << 4));
275 } else {
276 /* default for opl3sa2 versions */
277 snd_opl3sa2_write(chip, OPL3SA2_SYS_CTRL, 0x00);
278 }
279 snd_opl3sa2_write(chip, OPL3SA2_IRQ_CONFIG, 0x0d); /* Interrupt Channel Configuration - IRQ A = OPL3 + MPU + WSS */
280 if (chip->single_dma) {
281 snd_opl3sa2_write(chip, OPL3SA2_DMA_CONFIG, 0x03); /* DMA Configuration - DMA A = WSS-R + WSS-P */
282 } else {
283 snd_opl3sa2_write(chip, OPL3SA2_DMA_CONFIG, 0x21); /* DMA Configuration - DMA B = WSS-R, DMA A = WSS-P */
284 }
285 snd_opl3sa2_write(chip, OPL3SA2_MISC, 0x80 | (tmp & 7)); /* Miscellaneous - default */
286 if (chip->version > 2) {
287 snd_opl3sa2_write(chip, OPL3SA3_DGTL_DOWN, 0x00); /* Digital Block Partial Power Down - default */
288 snd_opl3sa2_write(chip, OPL3SA3_ANLG_DOWN, 0x00); /* Analog Block Partial Power Down - default */
289 }
290 return 0;
291}
292
293static irqreturn_t snd_opl3sa2_interrupt(int irq, void *dev_id, struct pt_regs *regs)
294{
295 unsigned short status;
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100296 struct snd_opl3sa2 *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 int handled = 0;
298
299 if (chip == NULL || chip->card == NULL)
300 return IRQ_NONE;
301
302 status = snd_opl3sa2_read(chip, OPL3SA2_IRQ_STATUS);
303
304 if (status & 0x20) {
305 handled = 1;
306 snd_opl3_interrupt(chip->synth);
307 }
308
309 if ((status & 0x10) && chip->rmidi != NULL) {
310 handled = 1;
311 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
312 }
313
314 if (status & 0x07) { /* TI,CI,PI */
315 handled = 1;
316 snd_cs4231_interrupt(irq, chip->cs4231, regs);
317 }
318
319 if (status & 0x40) { /* hardware volume change */
320 handled = 1;
321 /* reading from Master Lch register at 0x07 clears this bit */
322 snd_opl3sa2_read(chip, OPL3SA2_MASTER_RIGHT);
323 snd_opl3sa2_read(chip, OPL3SA2_MASTER_LEFT);
324 if (chip->master_switch && chip->master_volume) {
325 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_switch->id);
326 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_volume->id);
327 }
328 }
329 return IRQ_RETVAL(handled);
330}
331
332#define OPL3SA2_SINGLE(xname, xindex, reg, shift, mask, invert) \
333{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
334 .info = snd_opl3sa2_info_single, \
335 .get = snd_opl3sa2_get_single, .put = snd_opl3sa2_put_single, \
336 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
337
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100338static int snd_opl3sa2_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 int mask = (kcontrol->private_value >> 16) & 0xff;
341
342 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
343 uinfo->count = 1;
344 uinfo->value.integer.min = 0;
345 uinfo->value.integer.max = mask;
346 return 0;
347}
348
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100349static int snd_opl3sa2_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100351 struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 unsigned long flags;
353 int reg = kcontrol->private_value & 0xff;
354 int shift = (kcontrol->private_value >> 8) & 0xff;
355 int mask = (kcontrol->private_value >> 16) & 0xff;
356 int invert = (kcontrol->private_value >> 24) & 0xff;
357
358 spin_lock_irqsave(&chip->reg_lock, flags);
359 ucontrol->value.integer.value[0] = (chip->ctlregs[reg] >> shift) & mask;
360 spin_unlock_irqrestore(&chip->reg_lock, flags);
361 if (invert)
362 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
363 return 0;
364}
365
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100366static int snd_opl3sa2_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100368 struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 unsigned long flags;
370 int reg = kcontrol->private_value & 0xff;
371 int shift = (kcontrol->private_value >> 8) & 0xff;
372 int mask = (kcontrol->private_value >> 16) & 0xff;
373 int invert = (kcontrol->private_value >> 24) & 0xff;
374 int change;
375 unsigned short val, oval;
376
377 val = (ucontrol->value.integer.value[0] & mask);
378 if (invert)
379 val = mask - val;
380 val <<= shift;
381 spin_lock_irqsave(&chip->reg_lock, flags);
382 oval = chip->ctlregs[reg];
383 val = (oval & ~(mask << shift)) | val;
384 change = val != oval;
385 __snd_opl3sa2_write(chip, reg, val);
386 spin_unlock_irqrestore(&chip->reg_lock, flags);
387 return change;
388}
389
390#define OPL3SA2_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
391{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
392 .info = snd_opl3sa2_info_double, \
393 .get = snd_opl3sa2_get_double, .put = snd_opl3sa2_put_double, \
394 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
395
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100396static int snd_opl3sa2_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 int mask = (kcontrol->private_value >> 24) & 0xff;
399
400 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
401 uinfo->count = 2;
402 uinfo->value.integer.min = 0;
403 uinfo->value.integer.max = mask;
404 return 0;
405}
406
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100407static int snd_opl3sa2_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100409 struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 unsigned long flags;
411 int left_reg = kcontrol->private_value & 0xff;
412 int right_reg = (kcontrol->private_value >> 8) & 0xff;
413 int shift_left = (kcontrol->private_value >> 16) & 0x07;
414 int shift_right = (kcontrol->private_value >> 19) & 0x07;
415 int mask = (kcontrol->private_value >> 24) & 0xff;
416 int invert = (kcontrol->private_value >> 22) & 1;
417
418 spin_lock_irqsave(&chip->reg_lock, flags);
419 ucontrol->value.integer.value[0] = (chip->ctlregs[left_reg] >> shift_left) & mask;
420 ucontrol->value.integer.value[1] = (chip->ctlregs[right_reg] >> shift_right) & mask;
421 spin_unlock_irqrestore(&chip->reg_lock, flags);
422 if (invert) {
423 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
424 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
425 }
426 return 0;
427}
428
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100429static int snd_opl3sa2_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100431 struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 unsigned long flags;
433 int left_reg = kcontrol->private_value & 0xff;
434 int right_reg = (kcontrol->private_value >> 8) & 0xff;
435 int shift_left = (kcontrol->private_value >> 16) & 0x07;
436 int shift_right = (kcontrol->private_value >> 19) & 0x07;
437 int mask = (kcontrol->private_value >> 24) & 0xff;
438 int invert = (kcontrol->private_value >> 22) & 1;
439 int change;
440 unsigned short val1, val2, oval1, oval2;
441
442 val1 = ucontrol->value.integer.value[0] & mask;
443 val2 = ucontrol->value.integer.value[1] & mask;
444 if (invert) {
445 val1 = mask - val1;
446 val2 = mask - val2;
447 }
448 val1 <<= shift_left;
449 val2 <<= shift_right;
450 spin_lock_irqsave(&chip->reg_lock, flags);
451 if (left_reg != right_reg) {
452 oval1 = chip->ctlregs[left_reg];
453 oval2 = chip->ctlregs[right_reg];
454 val1 = (oval1 & ~(mask << shift_left)) | val1;
455 val2 = (oval2 & ~(mask << shift_right)) | val2;
456 change = val1 != oval1 || val2 != oval2;
457 __snd_opl3sa2_write(chip, left_reg, val1);
458 __snd_opl3sa2_write(chip, right_reg, val2);
459 } else {
460 oval1 = chip->ctlregs[left_reg];
461 val1 = (oval1 & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
462 change = val1 != oval1;
463 __snd_opl3sa2_write(chip, left_reg, val1);
464 }
465 spin_unlock_irqrestore(&chip->reg_lock, flags);
466 return change;
467}
468
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100469static struct snd_kcontrol_new snd_opl3sa2_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470OPL3SA2_DOUBLE("Master Playback Switch", 0, 0x07, 0x08, 7, 7, 1, 1),
471OPL3SA2_DOUBLE("Master Playback Volume", 0, 0x07, 0x08, 0, 0, 15, 1),
472OPL3SA2_SINGLE("Mic Playback Switch", 0, 0x09, 7, 1, 1),
473OPL3SA2_SINGLE("Mic Playback Volume", 0, 0x09, 0, 31, 1)
474};
475
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100476static struct snd_kcontrol_new snd_opl3sa2_tone_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477OPL3SA2_DOUBLE("3D Control - Wide", 0, 0x14, 0x14, 4, 0, 7, 0),
478OPL3SA2_DOUBLE("Tone Control - Bass", 0, 0x15, 0x15, 4, 0, 7, 0),
479OPL3SA2_DOUBLE("Tone Control - Treble", 0, 0x16, 0x16, 4, 0, 7, 0)
480};
481
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100482static void snd_opl3sa2_master_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100484 struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 chip->master_switch = NULL;
486 chip->master_volume = NULL;
487}
488
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100489static int __init snd_opl3sa2_mixer(struct snd_opl3sa2 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100491 struct snd_card *card = chip->card;
492 struct snd_ctl_elem_id id1, id2;
493 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 unsigned int idx;
495 int err;
496
497 memset(&id1, 0, sizeof(id1));
498 memset(&id2, 0, sizeof(id2));
499 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
500 /* reassign AUX0 to CD */
501 strcpy(id1.name, "Aux Playback Switch");
502 strcpy(id2.name, "CD Playback Switch");
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100503 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
504 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return err;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 strcpy(id1.name, "Aux Playback Volume");
508 strcpy(id2.name, "CD Playback Volume");
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100509 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
510 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return err;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /* reassign AUX1 to FM */
514 strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
515 strcpy(id2.name, "FM Playback Switch");
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100516 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
517 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return err;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 strcpy(id1.name, "Aux Playback Volume");
521 strcpy(id2.name, "FM Playback Volume");
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100522 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
523 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return err;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* add OPL3SA2 controls */
527 for (idx = 0; idx < ARRAY_SIZE(snd_opl3sa2_controls); idx++) {
528 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_opl3sa2_controls[idx], chip))) < 0)
529 return err;
530 switch (idx) {
531 case 0: chip->master_switch = kctl; kctl->private_free = snd_opl3sa2_master_free; break;
532 case 1: chip->master_volume = kctl; kctl->private_free = snd_opl3sa2_master_free; break;
533 }
534 }
535 if (chip->version > 2) {
536 for (idx = 0; idx < ARRAY_SIZE(snd_opl3sa2_tone_controls); idx++)
537 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_opl3sa2_tone_controls[idx], chip))) < 0)
538 return err;
539 }
540 return 0;
541}
542
543/* Power Management support functions */
544#ifdef CONFIG_PM
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100545static int snd_opl3sa2_suspend(struct snd_card *card, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Takashi Iwai704e0522005-11-17 17:11:53 +0100547 struct snd_opl3sa2 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Takashi Iwai704e0522005-11-17 17:11:53 +0100549 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
550 chip->cs4231->suspend(chip->cs4231);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* power down */
552 snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D3);
553
554 return 0;
555}
556
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100557static int snd_opl3sa2_resume(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Takashi Iwai704e0522005-11-17 17:11:53 +0100559 struct snd_opl3sa2 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 int i;
561
562 /* power up */
563 snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D0);
564
565 /* restore registers */
566 for (i = 2; i <= 0x0a; i++) {
567 if (i != OPL3SA2_IRQ_STATUS)
568 snd_opl3sa2_write(chip, i, chip->ctlregs[i]);
569 }
570 if (chip->version > 2) {
571 for (i = 0x12; i <= 0x16; i++)
572 snd_opl3sa2_write(chip, i, chip->ctlregs[i]);
573 }
574 /* restore cs4231 */
Takashi Iwai704e0522005-11-17 17:11:53 +0100575 chip->cs4231->resume(chip->cs4231);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Takashi Iwai704e0522005-11-17 17:11:53 +0100577 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return 0;
579}
580#endif /* CONFIG_PM */
581
582#ifdef CONFIG_PNP
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100583static int __init snd_opl3sa2_pnp(int dev, struct snd_opl3sa2 *chip,
Takashi Iwai704e0522005-11-17 17:11:53 +0100584 struct pnp_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Jaroslav Kysela12655092005-08-12 17:24:24 +0200586 struct pnp_resource_table * cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 int err;
588
Jaroslav Kysela12655092005-08-12 17:24:24 +0200589 cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100590 if (!cfg) {
591 snd_printk(KERN_ERR PFX "cannot allocate pnp cfg\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /* PnP initialization */
595 pnp_init_resource_table(cfg);
596 if (sb_port[dev] != SNDRV_AUTO_PORT)
597 pnp_resource_change(&cfg->port_resource[0], sb_port[dev], 16);
598 if (wss_port[dev] != SNDRV_AUTO_PORT)
599 pnp_resource_change(&cfg->port_resource[1], wss_port[dev], 8);
600 if (fm_port[dev] != SNDRV_AUTO_PORT)
601 pnp_resource_change(&cfg->port_resource[2], fm_port[dev], 4);
602 if (midi_port[dev] != SNDRV_AUTO_PORT)
603 pnp_resource_change(&cfg->port_resource[3], midi_port[dev], 2);
604 if (port[dev] != SNDRV_AUTO_PORT)
605 pnp_resource_change(&cfg->port_resource[4], port[dev], 2);
606 if (dma1[dev] != SNDRV_AUTO_DMA)
607 pnp_resource_change(&cfg->dma_resource[0], dma1[dev], 1);
608 if (dma2[dev] != SNDRV_AUTO_DMA)
609 pnp_resource_change(&cfg->dma_resource[1], dma2[dev], 1);
610 if (irq[dev] != SNDRV_AUTO_IRQ)
611 pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
612 err = pnp_manual_config_dev(pdev, cfg, 0);
Takashi Iwai704e0522005-11-17 17:11:53 +0100613 if (err < 0)
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100614 snd_printk(KERN_WARNING "PnP manual resources are invalid, using auto config\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 err = pnp_activate_dev(pdev);
616 if (err < 0) {
617 kfree(cfg);
618 snd_printk(KERN_ERR "PnP configure failure (out of resources?) err = %d\n", err);
619 return -EBUSY;
620 }
621 sb_port[dev] = pnp_port_start(pdev, 0);
622 wss_port[dev] = pnp_port_start(pdev, 1);
623 fm_port[dev] = pnp_port_start(pdev, 2);
624 midi_port[dev] = pnp_port_start(pdev, 3);
625 port[dev] = pnp_port_start(pdev, 4);
626 dma1[dev] = pnp_dma(pdev, 0);
627 dma2[dev] = pnp_dma(pdev, 1);
628 irq[dev] = pnp_irq(pdev, 0);
Jaroslav Kysela12655092005-08-12 17:24:24 +0200629 snd_printdd("%sPnP OPL3-SA: sb port=0x%lx, wss port=0x%lx, fm port=0x%lx, midi port=0x%lx\n",
630 pnp_device_is_pnpbios(pdev) ? "BIOS" : "ISA", sb_port[dev], wss_port[dev], fm_port[dev], midi_port[dev]);
631 snd_printdd("%sPnP OPL3-SA: control port=0x%lx, dma1=%i, dma2=%i, irq=%i\n",
632 pnp_device_is_pnpbios(pdev) ? "BIOS" : "ISA", port[dev], dma1[dev], dma2[dev], irq[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 kfree(cfg);
634 return 0;
635}
636#endif /* CONFIG_PNP */
637
Takashi Iwai704e0522005-11-17 17:11:53 +0100638static void snd_opl3sa2_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Takashi Iwai704e0522005-11-17 17:11:53 +0100640 struct snd_opl3sa2 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (chip->irq >= 0)
642 free_irq(chip->irq, (void *)chip);
Takashi Iwaib1d57762005-10-10 11:56:31 +0200643 release_and_free_resource(chip->res_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Takashi Iwai704e0522005-11-17 17:11:53 +0100646static struct snd_card *snd_opl3sa2_card_new(int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Takashi Iwai704e0522005-11-17 17:11:53 +0100648 struct snd_card *card;
649 struct snd_opl3sa2 *chip;
650
651 card = snd_card_new(index[dev], id[dev], THIS_MODULE, sizeof(struct snd_opl3sa2));
652 if (card == NULL)
653 return NULL;
654 strcpy(card->driver, "OPL3SA2");
655 strcpy(card->shortname, "Yamaha OPL3-SA2");
656 chip = card->private_data;
657 spin_lock_init(&chip->reg_lock);
658 chip->irq = -1;
659 chip->card = card;
660 card->private_free = snd_opl3sa2_free;
661 return card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Takashi Iwai704e0522005-11-17 17:11:53 +0100664static int __devinit snd_opl3sa2_probe(struct snd_card *card, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 int xirq, xdma1, xdma2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 struct snd_opl3sa2 *chip;
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100668 struct snd_cs4231 *cs4231;
669 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 int err;
671
Takashi Iwai704e0522005-11-17 17:11:53 +0100672 /* initialise this card from supplied (or default) parameter*/
673 chip = card->private_data;
674 chip->ymode = opl3sa3_ymode[dev] & 0x03 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 chip->port = port[dev];
676 xirq = irq[dev];
677 xdma1 = dma1[dev];
678 xdma2 = dma2[dev];
679 if (xdma2 < 0)
680 chip->single_dma = 1;
681 if ((err = snd_opl3sa2_detect(chip)) < 0)
Takashi Iwai704e0522005-11-17 17:11:53 +0100682 return err;
683 if (request_irq(xirq, snd_opl3sa2_interrupt, SA_INTERRUPT, "OPL3-SA2", chip)) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200684 snd_printk(KERN_ERR PFX "can't grab IRQ %d\n", xirq);
Takashi Iwai704e0522005-11-17 17:11:53 +0100685 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687 chip->irq = xirq;
688 if ((err = snd_cs4231_create(card,
689 wss_port[dev] + 4, -1,
690 xirq, xdma1, xdma2,
691 CS4231_HW_OPL3SA2,
692 CS4231_HWSHARE_IRQ,
693 &cs4231)) < 0) {
694 snd_printd("Oops, WSS not detected at 0x%lx\n", wss_port[dev] + 4);
Takashi Iwai704e0522005-11-17 17:11:53 +0100695 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697 chip->cs4231 = cs4231;
698 if ((err = snd_cs4231_pcm(cs4231, 0, NULL)) < 0)
Takashi Iwai704e0522005-11-17 17:11:53 +0100699 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if ((err = snd_cs4231_mixer(cs4231)) < 0)
Takashi Iwai704e0522005-11-17 17:11:53 +0100701 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if ((err = snd_opl3sa2_mixer(chip)) < 0)
Takashi Iwai704e0522005-11-17 17:11:53 +0100703 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if ((err = snd_cs4231_timer(cs4231, 0, NULL)) < 0)
Takashi Iwai704e0522005-11-17 17:11:53 +0100705 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (fm_port[dev] >= 0x340 && fm_port[dev] < 0x400) {
707 if ((err = snd_opl3_create(card, fm_port[dev],
708 fm_port[dev] + 2,
709 OPL3_HW_OPL3, 0, &opl3)) < 0)
Takashi Iwai704e0522005-11-17 17:11:53 +0100710 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if ((err = snd_opl3_timer_new(opl3, 1, 2)) < 0)
Takashi Iwai704e0522005-11-17 17:11:53 +0100712 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, &chip->synth)) < 0)
Takashi Iwai704e0522005-11-17 17:11:53 +0100714 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716 if (midi_port[dev] >= 0x300 && midi_port[dev] < 0x340) {
717 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_OPL3SA2,
718 midi_port[dev], 0,
719 xirq, 0, &chip->rmidi)) < 0)
Takashi Iwai704e0522005-11-17 17:11:53 +0100720 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
723 card->shortname, chip->port, xirq, xdma1);
724 if (dma2 >= 0)
725 sprintf(card->longname + strlen(card->longname), "&%d", xdma2);
726
Takashi Iwai704e0522005-11-17 17:11:53 +0100727 return snd_card_register(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
730#ifdef CONFIG_PNP
Jaroslav Kysela12655092005-08-12 17:24:24 +0200731static int __devinit snd_opl3sa2_pnp_detect(struct pnp_dev *pdev,
732 const struct pnp_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Takashi Iwai704e0522005-11-17 17:11:53 +0100734 static int dev;
735 int err;
736 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Takashi Iwai704e0522005-11-17 17:11:53 +0100738 if (pnp_device_is_isapnp(pdev))
739 return -ENOENT; /* we have another procedure - card */
740 for (; dev < SNDRV_CARDS; dev++) {
741 if (enable[dev] && isapnp[dev])
742 break;
743 }
744 if (dev >= SNDRV_CARDS)
745 return -ENODEV;
746
747 card = snd_opl3sa2_card_new(dev);
748 if (! card)
749 return -ENOMEM;
750 if ((err = snd_opl3sa2_pnp(dev, card->private_data, pdev)) < 0) {
751 snd_card_free(card);
752 return err;
753 }
754 snd_card_set_dev(card, &pdev->dev);
755 if ((err = snd_opl3sa2_probe(card, dev)) < 0) {
756 snd_card_free(card);
757 return err;
758 }
759 pnp_set_drvdata(pdev, card);
760 dev++;
761 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Jaroslav Kysela12655092005-08-12 17:24:24 +0200764static void __devexit snd_opl3sa2_pnp_remove(struct pnp_dev * pdev)
765{
Takashi Iwai704e0522005-11-17 17:11:53 +0100766 snd_card_free(pnp_get_drvdata(pdev));
767 pnp_set_drvdata(pdev, NULL);
Jaroslav Kysela12655092005-08-12 17:24:24 +0200768}
769
Takashi Iwai704e0522005-11-17 17:11:53 +0100770#ifdef CONFIG_PM
771static int snd_opl3sa2_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
772{
773 return snd_opl3sa2_suspend(pnp_get_drvdata(pdev), state);
774}
775static int snd_opl3sa2_pnp_resume(struct pnp_dev *pdev)
776{
777 return snd_opl3sa2_resume(pnp_get_drvdata(pdev));
778}
779#endif
780
Jaroslav Kysela12655092005-08-12 17:24:24 +0200781static struct pnp_driver opl3sa2_pnp_driver = {
Jaroslav Kyselacff79742005-08-15 13:08:04 +0200782 .name = "opl3sa2-pnpbios",
Jaroslav Kysela12655092005-08-12 17:24:24 +0200783 .id_table = snd_opl3sa2_pnpbiosids,
784 .probe = snd_opl3sa2_pnp_detect,
785 .remove = __devexit_p(snd_opl3sa2_pnp_remove),
Takashi Iwai704e0522005-11-17 17:11:53 +0100786#ifdef CONFIG_PM
787 .suspend = snd_opl3sa2_pnp_suspend,
788 .resume = snd_opl3sa2_pnp_resume,
789#endif
Jaroslav Kysela12655092005-08-12 17:24:24 +0200790};
791
Takashi Iwai704e0522005-11-17 17:11:53 +0100792static int __devinit snd_opl3sa2_pnp_cdetect(struct pnp_card_link *pcard,
Jaroslav Kysela12655092005-08-12 17:24:24 +0200793 const struct pnp_card_device_id *id)
794{
Takashi Iwai704e0522005-11-17 17:11:53 +0100795 static int dev;
796 struct pnp_dev *pdev;
797 int err;
798 struct snd_card *card;
Jaroslav Kysela12655092005-08-12 17:24:24 +0200799
Takashi Iwai704e0522005-11-17 17:11:53 +0100800 pdev = pnp_request_card_device(pcard, id->devs[0].id, NULL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100801 if (pdev == NULL) {
802 snd_printk(KERN_ERR PFX "can't get pnp device from id '%s'\n",
803 id->devs[0].id);
Takashi Iwai704e0522005-11-17 17:11:53 +0100804 return -EBUSY;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100805 }
Takashi Iwai704e0522005-11-17 17:11:53 +0100806 for (; dev < SNDRV_CARDS; dev++) {
807 if (enable[dev] && isapnp[dev])
808 break;
809 }
810 if (dev >= SNDRV_CARDS)
811 return -ENODEV;
812
813 card = snd_opl3sa2_card_new(dev);
814 if (! card)
815 return -ENOMEM;
816 if ((err = snd_opl3sa2_pnp(dev, card->private_data, pdev)) < 0) {
817 snd_card_free(card);
818 return err;
819 }
820 snd_card_set_dev(card, &pdev->dev);
821 if ((err = snd_opl3sa2_probe(card, dev)) < 0) {
822 snd_card_free(card);
823 return err;
824 }
825 pnp_set_card_drvdata(pcard, card);
826 dev++;
827 return 0;
Jaroslav Kysela12655092005-08-12 17:24:24 +0200828}
829
830static void __devexit snd_opl3sa2_pnp_cremove(struct pnp_card_link * pcard)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Takashi Iwai704e0522005-11-17 17:11:53 +0100832 snd_card_free(pnp_get_card_drvdata(pcard));
833 pnp_set_card_drvdata(pcard, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Takashi Iwai704e0522005-11-17 17:11:53 +0100836#ifdef CONFIG_PM
837static int snd_opl3sa2_pnp_csuspend(struct pnp_card_link *pcard, pm_message_t state)
838{
839 return snd_opl3sa2_suspend(pnp_get_card_drvdata(pcard), state);
840}
841static int snd_opl3sa2_pnp_cresume(struct pnp_card_link *pcard)
842{
843 return snd_opl3sa2_resume(pnp_get_card_drvdata(pcard));
844}
845#endif
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847static struct pnp_card_driver opl3sa2_pnpc_driver = {
848 .flags = PNP_DRIVER_RES_DISABLE,
849 .name = "opl3sa2",
850 .id_table = snd_opl3sa2_pnpids,
Jaroslav Kysela12655092005-08-12 17:24:24 +0200851 .probe = snd_opl3sa2_pnp_cdetect,
852 .remove = __devexit_p(snd_opl3sa2_pnp_cremove),
Takashi Iwai704e0522005-11-17 17:11:53 +0100853#ifdef CONFIG_PM
854 .suspend = snd_opl3sa2_pnp_csuspend,
855 .resume = snd_opl3sa2_pnp_cresume,
856#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857};
858#endif /* CONFIG_PNP */
859
Takashi Iwai704e0522005-11-17 17:11:53 +0100860static int __init snd_opl3sa2_nonpnp_probe(struct platform_device *pdev)
861{
862 struct snd_card *card;
863 int err;
864 int dev = pdev->id;
865
866 if (port[dev] == SNDRV_AUTO_PORT) {
867 snd_printk(KERN_ERR PFX "specify port\n");
868 return -EINVAL;
869 }
870 if (wss_port[dev] == SNDRV_AUTO_PORT) {
871 snd_printk(KERN_ERR PFX "specify wss_port\n");
872 return -EINVAL;
873 }
874 if (fm_port[dev] == SNDRV_AUTO_PORT) {
875 snd_printk(KERN_ERR PFX "specify fm_port\n");
876 return -EINVAL;
877 }
878 if (midi_port[dev] == SNDRV_AUTO_PORT) {
879 snd_printk(KERN_ERR PFX "specify midi_port\n");
880 return -EINVAL;
881 }
882
883 card = snd_opl3sa2_card_new(dev);
884 if (! card)
885 return -ENOMEM;
886 snd_card_set_dev(card, &pdev->dev);
887 if ((err = snd_opl3sa2_probe(card, dev)) < 0) {
888 snd_card_free(card);
889 return err;
890 }
891 platform_set_drvdata(pdev, card);
892 return 0;
893}
894
895static int snd_opl3sa2_nonpnp_remove(struct platform_device *devptr)
896{
897 snd_card_free(platform_get_drvdata(devptr));
898 platform_set_drvdata(devptr, NULL);
899 return 0;
900}
901
902#ifdef CONFIG_PM
903static int snd_opl3sa2_nonpnp_suspend(struct platform_device *dev, pm_message_t state)
904{
905 return snd_opl3sa2_suspend(platform_get_drvdata(dev), state);
906}
907
908static int snd_opl3sa2_nonpnp_resume(struct platform_device *dev)
909{
910 return snd_opl3sa2_resume(platform_get_drvdata(dev));
911}
912#endif
913
914#define OPL3SA2_DRIVER "snd_opl3sa2"
915
916static struct platform_driver snd_opl3sa2_nonpnp_driver = {
917 .probe = snd_opl3sa2_nonpnp_probe,
918 .remove = snd_opl3sa2_nonpnp_remove,
919#ifdef CONFIG_PM
920 .suspend = snd_opl3sa2_nonpnp_suspend,
921 .resume = snd_opl3sa2_nonpnp_resume,
922#endif
923 .driver = {
924 .name = OPL3SA2_DRIVER
925 },
926};
927
Clemens Ladischf7a92752005-12-07 09:13:42 +0100928static void __init_or_module snd_opl3sa2_unregister_all(void)
929{
930 int i;
931
932 if (pnpc_registered)
933 pnp_unregister_card_driver(&opl3sa2_pnpc_driver);
934 if (pnp_registered)
935 pnp_unregister_driver(&opl3sa2_pnp_driver);
936 for (i = 0; i < ARRAY_SIZE(platform_devices); ++i)
937 platform_device_unregister(platform_devices[i]);
938 platform_driver_unregister(&snd_opl3sa2_nonpnp_driver);
939}
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941static int __init alsa_card_opl3sa2_init(void)
942{
Takashi Iwai704e0522005-11-17 17:11:53 +0100943 int i, err, cards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Takashi Iwai704e0522005-11-17 17:11:53 +0100945 if ((err = platform_driver_register(&snd_opl3sa2_nonpnp_driver)) < 0)
946 return err;
947
948 for (i = 0; i < SNDRV_CARDS && enable[i]; i++) {
949 struct platform_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950#ifdef CONFIG_PNP
Takashi Iwai704e0522005-11-17 17:11:53 +0100951 if (isapnp[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 continue;
953#endif
Takashi Iwai704e0522005-11-17 17:11:53 +0100954 device = platform_device_register_simple(OPL3SA2_DRIVER,
955 i, NULL, 0);
956 if (IS_ERR(device)) {
957 err = PTR_ERR(device);
Clemens Ladischf7a92752005-12-07 09:13:42 +0100958 goto errout;
Takashi Iwai704e0522005-11-17 17:11:53 +0100959 }
Clemens Ladischf7a92752005-12-07 09:13:42 +0100960 platform_devices[i] = device;
Takashi Iwai704e0522005-11-17 17:11:53 +0100961 cards++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
Takashi Iwai704e0522005-11-17 17:11:53 +0100963
964 err = pnp_register_driver(&opl3sa2_pnp_driver);
Clemens Ladischf7a92752005-12-07 09:13:42 +0100965 if (err >= 0) {
966 pnp_registered = 1;
Takashi Iwai704e0522005-11-17 17:11:53 +0100967 cards += err;
Clemens Ladischf7a92752005-12-07 09:13:42 +0100968 }
Takashi Iwai704e0522005-11-17 17:11:53 +0100969 err = pnp_register_card_driver(&opl3sa2_pnpc_driver);
Clemens Ladischf7a92752005-12-07 09:13:42 +0100970 if (err >= 0) {
971 pnpc_registered = 1;
Takashi Iwai704e0522005-11-17 17:11:53 +0100972 cards += err;
Clemens Ladischf7a92752005-12-07 09:13:42 +0100973 }
Takashi Iwai704e0522005-11-17 17:11:53 +0100974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (!cards) {
976#ifdef MODULE
977 snd_printk(KERN_ERR "Yamaha OPL3-SA soundcard not found or device busy\n");
978#endif
Clemens Ladischf7a92752005-12-07 09:13:42 +0100979 err = -ENODEV;
980 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982 return 0;
Clemens Ladischf7a92752005-12-07 09:13:42 +0100983
984 errout:
985 snd_opl3sa2_unregister_all();
986 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
989static void __exit alsa_card_opl3sa2_exit(void)
990{
Clemens Ladischf7a92752005-12-07 09:13:42 +0100991 snd_opl3sa2_unregister_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
994module_init(alsa_card_opl3sa2_init)
995module_exit(alsa_card_opl3sa2_exit)