blob: 56fcd8a946a485822539099505e9bc41043e66fe [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];
Takashi Iwai59b1b342006-01-04 15:06:44 +010094#ifdef CONFIG_PNP
Clemens Ladischf7a92752005-12-07 09:13:42 +010095static int pnp_registered;
96static int pnpc_registered;
Takashi Iwai59b1b342006-01-04 15:06:44 +010097#endif
Clemens Ladischf7a92752005-12-07 09:13:42 +010098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/* control ports */
100#define OPL3SA2_PM_CTRL 0x01
101#define OPL3SA2_SYS_CTRL 0x02
102#define OPL3SA2_IRQ_CONFIG 0x03
103#define OPL3SA2_IRQ_STATUS 0x04
104#define OPL3SA2_DMA_CONFIG 0x06
105#define OPL3SA2_MASTER_LEFT 0x07
106#define OPL3SA2_MASTER_RIGHT 0x08
107#define OPL3SA2_MIC 0x09
108#define OPL3SA2_MISC 0x0A
109
110/* opl3sa3 only */
111#define OPL3SA3_DGTL_DOWN 0x12
112#define OPL3SA3_ANLG_DOWN 0x13
113#define OPL3SA3_WIDE 0x14
114#define OPL3SA3_BASS 0x15
115#define OPL3SA3_TREBLE 0x16
116
117/* power management bits */
118#define OPL3SA2_PM_ADOWN 0x20
119#define OPL3SA2_PM_PSV 0x04
120#define OPL3SA2_PM_PDN 0x02
121#define OPL3SA2_PM_PDX 0x01
122
123#define OPL3SA2_PM_D0 0x00
124#define OPL3SA2_PM_D3 (OPL3SA2_PM_ADOWN|OPL3SA2_PM_PSV|OPL3SA2_PM_PDN|OPL3SA2_PM_PDX)
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126struct snd_opl3sa2 {
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100127 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int version; /* 2 or 3 */
129 unsigned long port; /* control port */
130 struct resource *res_port; /* control port resource */
131 int irq;
132 int single_dma;
133 spinlock_t reg_lock;
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100134 struct snd_hwdep *synth;
135 struct snd_rawmidi *rmidi;
136 struct snd_cs4231 *cs4231;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 unsigned char ctlregs[0x20];
138 int ymode; /* SL added */
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100139 struct snd_kcontrol *master_switch;
140 struct snd_kcontrol *master_volume;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
Takashi Iwai43bcd972005-09-05 17:19:20 +0200143#define PFX "opl3sa2: "
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#ifdef CONFIG_PNP
146
Jaroslav Kysela1265509c2005-08-12 17:24:24 +0200147static struct pnp_device_id snd_opl3sa2_pnpbiosids[] = {
Jaroslav Kysela53b26662005-08-12 17:35:09 +0200148 { .id = "YMH0021" },
Jaroslav Kysela1265509c2005-08-12 17:24:24 +0200149 { .id = "NMX2210" }, /* Gateway Solo 2500 */
150 { .id = "" } /* end */
151};
152
153MODULE_DEVICE_TABLE(pnp, snd_opl3sa2_pnpbiosids);
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static struct pnp_card_device_id snd_opl3sa2_pnpids[] = {
156 /* Yamaha YMF719E-S (Genius Sound Maker 3DX) */
157 { .id = "YMH0020", .devs = { { "YMH0021" } } },
158 /* Yamaha OPL3-SA3 (integrated on Intel's Pentium II AL440LX motherboard) */
159 { .id = "YMH0030", .devs = { { "YMH0021" } } },
160 /* Yamaha OPL3-SA2 */
161 { .id = "YMH0800", .devs = { { "YMH0021" } } },
162 /* Yamaha OPL3-SA2 */
163 { .id = "YMH0801", .devs = { { "YMH0021" } } },
164 /* NeoMagic MagicWave 3DX */
165 { .id = "NMX2200", .devs = { { "YMH2210" } } },
166 /* --- */
167 { .id = "" } /* end */
168};
169
170MODULE_DEVICE_TABLE(pnp_card, snd_opl3sa2_pnpids);
171
172#endif /* CONFIG_PNP */
173
174
175/* read control port (w/o spinlock) */
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100176static unsigned char __snd_opl3sa2_read(struct snd_opl3sa2 *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 unsigned char result;
179#if 0
180 outb(0x1d, port); /* password */
181 printk("read [0x%lx] = 0x%x\n", port, inb(port));
182#endif
183 outb(reg, chip->port); /* register */
184 result = inb(chip->port + 1);
185#if 0
186 printk("read [0x%lx] = 0x%x [0x%x]\n", port, result, inb(port));
187#endif
188 return result;
189}
190
191/* read control port (with spinlock) */
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100192static unsigned char snd_opl3sa2_read(struct snd_opl3sa2 *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 unsigned long flags;
195 unsigned char result;
196
197 spin_lock_irqsave(&chip->reg_lock, flags);
198 result = __snd_opl3sa2_read(chip, reg);
199 spin_unlock_irqrestore(&chip->reg_lock, flags);
200 return result;
201}
202
203/* write control port (w/o spinlock) */
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100204static void __snd_opl3sa2_write(struct snd_opl3sa2 *chip, unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206#if 0
207 outb(0x1d, port); /* password */
208#endif
209 outb(reg, chip->port); /* register */
210 outb(value, chip->port + 1);
211 chip->ctlregs[reg] = value;
212}
213
214/* write control port (with spinlock) */
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100215static void snd_opl3sa2_write(struct snd_opl3sa2 *chip, unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 unsigned long flags;
218 spin_lock_irqsave(&chip->reg_lock, flags);
219 __snd_opl3sa2_write(chip, reg, value);
220 spin_unlock_irqrestore(&chip->reg_lock, flags);
221}
222
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100223static int __init snd_opl3sa2_detect(struct snd_opl3sa2 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100225 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 unsigned long port;
227 unsigned char tmp, tmp1;
228 char str[2];
229
230 card = chip->card;
231 port = chip->port;
232 if ((chip->res_port = request_region(port, 2, "OPL3-SA control")) == NULL) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200233 snd_printk(KERN_ERR PFX "can't grab port 0x%lx\n", port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return -EBUSY;
235 }
236 // snd_printk("REG 0A = 0x%x\n", snd_opl3sa2_read(chip, 0x0a));
237 chip->version = 0;
238 tmp = snd_opl3sa2_read(chip, OPL3SA2_MISC);
239 if (tmp == 0xff) {
240 snd_printd("OPL3-SA [0x%lx] detect = 0x%x\n", port, tmp);
241 return -ENODEV;
242 }
243 switch (tmp & 0x07) {
244 case 0x01:
245 chip->version = 2; /* YMF711 */
246 break;
247 default:
248 chip->version = 3;
249 /* 0x02 - standard */
250 /* 0x03 - YM715B */
251 /* 0x04 - YM719 - OPL-SA4? */
252 /* 0x05 - OPL3-SA3 - Libretto 100 */
253 break;
254 }
255 str[0] = chip->version + '0';
256 str[1] = 0;
257 strcat(card->shortname, str);
258 snd_opl3sa2_write(chip, OPL3SA2_MISC, tmp ^ 7);
259 if ((tmp1 = snd_opl3sa2_read(chip, OPL3SA2_MISC)) != tmp) {
260 snd_printd("OPL3-SA [0x%lx] detect (1) = 0x%x (0x%x)\n", port, tmp, tmp1);
261 return -ENODEV;
262 }
263 /* try if the MIC register is accesible */
264 tmp = snd_opl3sa2_read(chip, OPL3SA2_MIC);
265 snd_opl3sa2_write(chip, OPL3SA2_MIC, 0x8a);
266 if (((tmp1 = snd_opl3sa2_read(chip, OPL3SA2_MIC)) & 0x9f) != 0x8a) {
267 snd_printd("OPL3-SA [0x%lx] detect (2) = 0x%x (0x%x)\n", port, tmp, tmp1);
268 return -ENODEV;
269 }
270 snd_opl3sa2_write(chip, OPL3SA2_MIC, 0x9f);
271 /* initialization */
272 /* Power Management - full on */
273 snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D0);
274 if (chip->version > 2) {
275 /* ymode is bits 4&5 (of 0 to 7) on all but opl3sa2 versions */
276 snd_opl3sa2_write(chip, OPL3SA2_SYS_CTRL, (chip->ymode << 4));
277 } else {
278 /* default for opl3sa2 versions */
279 snd_opl3sa2_write(chip, OPL3SA2_SYS_CTRL, 0x00);
280 }
281 snd_opl3sa2_write(chip, OPL3SA2_IRQ_CONFIG, 0x0d); /* Interrupt Channel Configuration - IRQ A = OPL3 + MPU + WSS */
282 if (chip->single_dma) {
283 snd_opl3sa2_write(chip, OPL3SA2_DMA_CONFIG, 0x03); /* DMA Configuration - DMA A = WSS-R + WSS-P */
284 } else {
285 snd_opl3sa2_write(chip, OPL3SA2_DMA_CONFIG, 0x21); /* DMA Configuration - DMA B = WSS-R, DMA A = WSS-P */
286 }
287 snd_opl3sa2_write(chip, OPL3SA2_MISC, 0x80 | (tmp & 7)); /* Miscellaneous - default */
288 if (chip->version > 2) {
289 snd_opl3sa2_write(chip, OPL3SA3_DGTL_DOWN, 0x00); /* Digital Block Partial Power Down - default */
290 snd_opl3sa2_write(chip, OPL3SA3_ANLG_DOWN, 0x00); /* Analog Block Partial Power Down - default */
291 }
292 return 0;
293}
294
295static irqreturn_t snd_opl3sa2_interrupt(int irq, void *dev_id, struct pt_regs *regs)
296{
297 unsigned short status;
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100298 struct snd_opl3sa2 *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 int handled = 0;
300
301 if (chip == NULL || chip->card == NULL)
302 return IRQ_NONE;
303
304 status = snd_opl3sa2_read(chip, OPL3SA2_IRQ_STATUS);
305
306 if (status & 0x20) {
307 handled = 1;
308 snd_opl3_interrupt(chip->synth);
309 }
310
311 if ((status & 0x10) && chip->rmidi != NULL) {
312 handled = 1;
313 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
314 }
315
316 if (status & 0x07) { /* TI,CI,PI */
317 handled = 1;
318 snd_cs4231_interrupt(irq, chip->cs4231, regs);
319 }
320
321 if (status & 0x40) { /* hardware volume change */
322 handled = 1;
323 /* reading from Master Lch register at 0x07 clears this bit */
324 snd_opl3sa2_read(chip, OPL3SA2_MASTER_RIGHT);
325 snd_opl3sa2_read(chip, OPL3SA2_MASTER_LEFT);
326 if (chip->master_switch && chip->master_volume) {
327 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_switch->id);
328 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_volume->id);
329 }
330 }
331 return IRQ_RETVAL(handled);
332}
333
334#define OPL3SA2_SINGLE(xname, xindex, reg, shift, mask, invert) \
335{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
336 .info = snd_opl3sa2_info_single, \
337 .get = snd_opl3sa2_get_single, .put = snd_opl3sa2_put_single, \
338 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
339
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100340static int snd_opl3sa2_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 int mask = (kcontrol->private_value >> 16) & 0xff;
343
344 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
345 uinfo->count = 1;
346 uinfo->value.integer.min = 0;
347 uinfo->value.integer.max = mask;
348 return 0;
349}
350
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100351static int snd_opl3sa2_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100353 struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 unsigned long flags;
355 int reg = kcontrol->private_value & 0xff;
356 int shift = (kcontrol->private_value >> 8) & 0xff;
357 int mask = (kcontrol->private_value >> 16) & 0xff;
358 int invert = (kcontrol->private_value >> 24) & 0xff;
359
360 spin_lock_irqsave(&chip->reg_lock, flags);
361 ucontrol->value.integer.value[0] = (chip->ctlregs[reg] >> shift) & mask;
362 spin_unlock_irqrestore(&chip->reg_lock, flags);
363 if (invert)
364 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
365 return 0;
366}
367
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100368static int snd_opl3sa2_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100370 struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 unsigned long flags;
372 int reg = kcontrol->private_value & 0xff;
373 int shift = (kcontrol->private_value >> 8) & 0xff;
374 int mask = (kcontrol->private_value >> 16) & 0xff;
375 int invert = (kcontrol->private_value >> 24) & 0xff;
376 int change;
377 unsigned short val, oval;
378
379 val = (ucontrol->value.integer.value[0] & mask);
380 if (invert)
381 val = mask - val;
382 val <<= shift;
383 spin_lock_irqsave(&chip->reg_lock, flags);
384 oval = chip->ctlregs[reg];
385 val = (oval & ~(mask << shift)) | val;
386 change = val != oval;
387 __snd_opl3sa2_write(chip, reg, val);
388 spin_unlock_irqrestore(&chip->reg_lock, flags);
389 return change;
390}
391
392#define OPL3SA2_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
393{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
394 .info = snd_opl3sa2_info_double, \
395 .get = snd_opl3sa2_get_double, .put = snd_opl3sa2_put_double, \
396 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
397
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100398static int snd_opl3sa2_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 int mask = (kcontrol->private_value >> 24) & 0xff;
401
402 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
403 uinfo->count = 2;
404 uinfo->value.integer.min = 0;
405 uinfo->value.integer.max = mask;
406 return 0;
407}
408
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100409static int snd_opl3sa2_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100411 struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 unsigned long flags;
413 int left_reg = kcontrol->private_value & 0xff;
414 int right_reg = (kcontrol->private_value >> 8) & 0xff;
415 int shift_left = (kcontrol->private_value >> 16) & 0x07;
416 int shift_right = (kcontrol->private_value >> 19) & 0x07;
417 int mask = (kcontrol->private_value >> 24) & 0xff;
418 int invert = (kcontrol->private_value >> 22) & 1;
419
420 spin_lock_irqsave(&chip->reg_lock, flags);
421 ucontrol->value.integer.value[0] = (chip->ctlregs[left_reg] >> shift_left) & mask;
422 ucontrol->value.integer.value[1] = (chip->ctlregs[right_reg] >> shift_right) & mask;
423 spin_unlock_irqrestore(&chip->reg_lock, flags);
424 if (invert) {
425 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
426 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
427 }
428 return 0;
429}
430
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100431static int snd_opl3sa2_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100433 struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 unsigned long flags;
435 int left_reg = kcontrol->private_value & 0xff;
436 int right_reg = (kcontrol->private_value >> 8) & 0xff;
437 int shift_left = (kcontrol->private_value >> 16) & 0x07;
438 int shift_right = (kcontrol->private_value >> 19) & 0x07;
439 int mask = (kcontrol->private_value >> 24) & 0xff;
440 int invert = (kcontrol->private_value >> 22) & 1;
441 int change;
442 unsigned short val1, val2, oval1, oval2;
443
444 val1 = ucontrol->value.integer.value[0] & mask;
445 val2 = ucontrol->value.integer.value[1] & mask;
446 if (invert) {
447 val1 = mask - val1;
448 val2 = mask - val2;
449 }
450 val1 <<= shift_left;
451 val2 <<= shift_right;
452 spin_lock_irqsave(&chip->reg_lock, flags);
453 if (left_reg != right_reg) {
454 oval1 = chip->ctlregs[left_reg];
455 oval2 = chip->ctlregs[right_reg];
456 val1 = (oval1 & ~(mask << shift_left)) | val1;
457 val2 = (oval2 & ~(mask << shift_right)) | val2;
458 change = val1 != oval1 || val2 != oval2;
459 __snd_opl3sa2_write(chip, left_reg, val1);
460 __snd_opl3sa2_write(chip, right_reg, val2);
461 } else {
462 oval1 = chip->ctlregs[left_reg];
463 val1 = (oval1 & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
464 change = val1 != oval1;
465 __snd_opl3sa2_write(chip, left_reg, val1);
466 }
467 spin_unlock_irqrestore(&chip->reg_lock, flags);
468 return change;
469}
470
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100471static struct snd_kcontrol_new snd_opl3sa2_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472OPL3SA2_DOUBLE("Master Playback Switch", 0, 0x07, 0x08, 7, 7, 1, 1),
473OPL3SA2_DOUBLE("Master Playback Volume", 0, 0x07, 0x08, 0, 0, 15, 1),
474OPL3SA2_SINGLE("Mic Playback Switch", 0, 0x09, 7, 1, 1),
475OPL3SA2_SINGLE("Mic Playback Volume", 0, 0x09, 0, 31, 1)
476};
477
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100478static struct snd_kcontrol_new snd_opl3sa2_tone_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479OPL3SA2_DOUBLE("3D Control - Wide", 0, 0x14, 0x14, 4, 0, 7, 0),
480OPL3SA2_DOUBLE("Tone Control - Bass", 0, 0x15, 0x15, 4, 0, 7, 0),
481OPL3SA2_DOUBLE("Tone Control - Treble", 0, 0x16, 0x16, 4, 0, 7, 0)
482};
483
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100484static void snd_opl3sa2_master_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100486 struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 chip->master_switch = NULL;
488 chip->master_volume = NULL;
489}
490
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100491static int __init snd_opl3sa2_mixer(struct snd_opl3sa2 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100493 struct snd_card *card = chip->card;
494 struct snd_ctl_elem_id id1, id2;
495 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 unsigned int idx;
497 int err;
498
499 memset(&id1, 0, sizeof(id1));
500 memset(&id2, 0, sizeof(id2));
501 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
502 /* reassign AUX0 to CD */
503 strcpy(id1.name, "Aux Playback Switch");
504 strcpy(id2.name, "CD Playback Switch");
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100505 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
506 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return err;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 strcpy(id1.name, "Aux Playback Volume");
510 strcpy(id2.name, "CD Playback Volume");
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100511 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
512 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return err;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* reassign AUX1 to FM */
516 strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
517 strcpy(id2.name, "FM Playback Switch");
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100518 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
519 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return err;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 strcpy(id1.name, "Aux Playback Volume");
523 strcpy(id2.name, "FM Playback Volume");
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100524 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
525 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return err;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /* add OPL3SA2 controls */
529 for (idx = 0; idx < ARRAY_SIZE(snd_opl3sa2_controls); idx++) {
530 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_opl3sa2_controls[idx], chip))) < 0)
531 return err;
532 switch (idx) {
533 case 0: chip->master_switch = kctl; kctl->private_free = snd_opl3sa2_master_free; break;
534 case 1: chip->master_volume = kctl; kctl->private_free = snd_opl3sa2_master_free; break;
535 }
536 }
537 if (chip->version > 2) {
538 for (idx = 0; idx < ARRAY_SIZE(snd_opl3sa2_tone_controls); idx++)
539 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_opl3sa2_tone_controls[idx], chip))) < 0)
540 return err;
541 }
542 return 0;
543}
544
545/* Power Management support functions */
546#ifdef CONFIG_PM
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100547static int snd_opl3sa2_suspend(struct snd_card *card, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Takashi Iwai704e0522005-11-17 17:11:53 +0100549 struct snd_opl3sa2 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Takashi Iwai704e0522005-11-17 17:11:53 +0100551 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
552 chip->cs4231->suspend(chip->cs4231);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* power down */
554 snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D3);
555
556 return 0;
557}
558
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100559static int snd_opl3sa2_resume(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Takashi Iwai704e0522005-11-17 17:11:53 +0100561 struct snd_opl3sa2 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 int i;
563
564 /* power up */
565 snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D0);
566
567 /* restore registers */
568 for (i = 2; i <= 0x0a; i++) {
569 if (i != OPL3SA2_IRQ_STATUS)
570 snd_opl3sa2_write(chip, i, chip->ctlregs[i]);
571 }
572 if (chip->version > 2) {
573 for (i = 0x12; i <= 0x16; i++)
574 snd_opl3sa2_write(chip, i, chip->ctlregs[i]);
575 }
576 /* restore cs4231 */
Takashi Iwai704e0522005-11-17 17:11:53 +0100577 chip->cs4231->resume(chip->cs4231);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Takashi Iwai704e0522005-11-17 17:11:53 +0100579 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return 0;
581}
582#endif /* CONFIG_PM */
583
584#ifdef CONFIG_PNP
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100585static int __init snd_opl3sa2_pnp(int dev, struct snd_opl3sa2 *chip,
Takashi Iwai704e0522005-11-17 17:11:53 +0100586 struct pnp_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Jaroslav Kysela1265509c2005-08-12 17:24:24 +0200588 struct pnp_resource_table * cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 int err;
590
Jaroslav Kysela1265509c2005-08-12 17:24:24 +0200591 cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100592 if (!cfg) {
593 snd_printk(KERN_ERR PFX "cannot allocate pnp cfg\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /* PnP initialization */
597 pnp_init_resource_table(cfg);
598 if (sb_port[dev] != SNDRV_AUTO_PORT)
599 pnp_resource_change(&cfg->port_resource[0], sb_port[dev], 16);
600 if (wss_port[dev] != SNDRV_AUTO_PORT)
601 pnp_resource_change(&cfg->port_resource[1], wss_port[dev], 8);
602 if (fm_port[dev] != SNDRV_AUTO_PORT)
603 pnp_resource_change(&cfg->port_resource[2], fm_port[dev], 4);
604 if (midi_port[dev] != SNDRV_AUTO_PORT)
605 pnp_resource_change(&cfg->port_resource[3], midi_port[dev], 2);
606 if (port[dev] != SNDRV_AUTO_PORT)
607 pnp_resource_change(&cfg->port_resource[4], port[dev], 2);
608 if (dma1[dev] != SNDRV_AUTO_DMA)
609 pnp_resource_change(&cfg->dma_resource[0], dma1[dev], 1);
610 if (dma2[dev] != SNDRV_AUTO_DMA)
611 pnp_resource_change(&cfg->dma_resource[1], dma2[dev], 1);
612 if (irq[dev] != SNDRV_AUTO_IRQ)
613 pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
614 err = pnp_manual_config_dev(pdev, cfg, 0);
Takashi Iwai704e0522005-11-17 17:11:53 +0100615 if (err < 0)
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100616 snd_printk(KERN_WARNING "PnP manual resources are invalid, using auto config\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 err = pnp_activate_dev(pdev);
618 if (err < 0) {
619 kfree(cfg);
620 snd_printk(KERN_ERR "PnP configure failure (out of resources?) err = %d\n", err);
621 return -EBUSY;
622 }
623 sb_port[dev] = pnp_port_start(pdev, 0);
624 wss_port[dev] = pnp_port_start(pdev, 1);
625 fm_port[dev] = pnp_port_start(pdev, 2);
626 midi_port[dev] = pnp_port_start(pdev, 3);
627 port[dev] = pnp_port_start(pdev, 4);
628 dma1[dev] = pnp_dma(pdev, 0);
629 dma2[dev] = pnp_dma(pdev, 1);
630 irq[dev] = pnp_irq(pdev, 0);
Jaroslav Kysela1265509c2005-08-12 17:24:24 +0200631 snd_printdd("%sPnP OPL3-SA: sb port=0x%lx, wss port=0x%lx, fm port=0x%lx, midi port=0x%lx\n",
632 pnp_device_is_pnpbios(pdev) ? "BIOS" : "ISA", sb_port[dev], wss_port[dev], fm_port[dev], midi_port[dev]);
633 snd_printdd("%sPnP OPL3-SA: control port=0x%lx, dma1=%i, dma2=%i, irq=%i\n",
634 pnp_device_is_pnpbios(pdev) ? "BIOS" : "ISA", port[dev], dma1[dev], dma2[dev], irq[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 kfree(cfg);
636 return 0;
637}
638#endif /* CONFIG_PNP */
639
Takashi Iwai704e0522005-11-17 17:11:53 +0100640static void snd_opl3sa2_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Takashi Iwai704e0522005-11-17 17:11:53 +0100642 struct snd_opl3sa2 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (chip->irq >= 0)
644 free_irq(chip->irq, (void *)chip);
Takashi Iwaib1d57762005-10-10 11:56:31 +0200645 release_and_free_resource(chip->res_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Takashi Iwai704e0522005-11-17 17:11:53 +0100648static struct snd_card *snd_opl3sa2_card_new(int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Takashi Iwai704e0522005-11-17 17:11:53 +0100650 struct snd_card *card;
651 struct snd_opl3sa2 *chip;
652
653 card = snd_card_new(index[dev], id[dev], THIS_MODULE, sizeof(struct snd_opl3sa2));
654 if (card == NULL)
655 return NULL;
656 strcpy(card->driver, "OPL3SA2");
657 strcpy(card->shortname, "Yamaha OPL3-SA2");
658 chip = card->private_data;
659 spin_lock_init(&chip->reg_lock);
660 chip->irq = -1;
661 chip->card = card;
662 card->private_free = snd_opl3sa2_free;
663 return card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Takashi Iwai704e0522005-11-17 17:11:53 +0100666static int __devinit snd_opl3sa2_probe(struct snd_card *card, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 int xirq, xdma1, xdma2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 struct snd_opl3sa2 *chip;
Takashi Iwaiec6c5ae2005-11-17 14:41:45 +0100670 struct snd_cs4231 *cs4231;
671 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 int err;
673
Takashi Iwai704e0522005-11-17 17:11:53 +0100674 /* initialise this card from supplied (or default) parameter*/
675 chip = card->private_data;
676 chip->ymode = opl3sa3_ymode[dev] & 0x03 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 chip->port = port[dev];
678 xirq = irq[dev];
679 xdma1 = dma1[dev];
680 xdma2 = dma2[dev];
681 if (xdma2 < 0)
682 chip->single_dma = 1;
683 if ((err = snd_opl3sa2_detect(chip)) < 0)
Takashi Iwai704e0522005-11-17 17:11:53 +0100684 return err;
685 if (request_irq(xirq, snd_opl3sa2_interrupt, SA_INTERRUPT, "OPL3-SA2", chip)) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200686 snd_printk(KERN_ERR PFX "can't grab IRQ %d\n", xirq);
Takashi Iwai704e0522005-11-17 17:11:53 +0100687 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689 chip->irq = xirq;
690 if ((err = snd_cs4231_create(card,
691 wss_port[dev] + 4, -1,
692 xirq, xdma1, xdma2,
693 CS4231_HW_OPL3SA2,
694 CS4231_HWSHARE_IRQ,
695 &cs4231)) < 0) {
696 snd_printd("Oops, WSS not detected at 0x%lx\n", wss_port[dev] + 4);
Takashi Iwai704e0522005-11-17 17:11:53 +0100697 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699 chip->cs4231 = cs4231;
700 if ((err = snd_cs4231_pcm(cs4231, 0, NULL)) < 0)
Takashi Iwai704e0522005-11-17 17:11:53 +0100701 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if ((err = snd_cs4231_mixer(cs4231)) < 0)
Takashi Iwai704e0522005-11-17 17:11:53 +0100703 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if ((err = snd_opl3sa2_mixer(chip)) < 0)
Takashi Iwai704e0522005-11-17 17:11:53 +0100705 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if ((err = snd_cs4231_timer(cs4231, 0, NULL)) < 0)
Takashi Iwai704e0522005-11-17 17:11:53 +0100707 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (fm_port[dev] >= 0x340 && fm_port[dev] < 0x400) {
709 if ((err = snd_opl3_create(card, fm_port[dev],
710 fm_port[dev] + 2,
711 OPL3_HW_OPL3, 0, &opl3)) < 0)
Takashi Iwai704e0522005-11-17 17:11:53 +0100712 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if ((err = snd_opl3_timer_new(opl3, 1, 2)) < 0)
Takashi Iwai704e0522005-11-17 17:11:53 +0100714 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, &chip->synth)) < 0)
Takashi Iwai704e0522005-11-17 17:11:53 +0100716 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718 if (midi_port[dev] >= 0x300 && midi_port[dev] < 0x340) {
719 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_OPL3SA2,
720 midi_port[dev], 0,
721 xirq, 0, &chip->rmidi)) < 0)
Takashi Iwai704e0522005-11-17 17:11:53 +0100722 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
725 card->shortname, chip->port, xirq, xdma1);
Alexey Dobriyan1d797162006-01-25 14:30:44 +0100726 if (xdma2 >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 sprintf(card->longname + strlen(card->longname), "&%d", xdma2);
728
Takashi Iwai704e0522005-11-17 17:11:53 +0100729 return snd_card_register(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
732#ifdef CONFIG_PNP
Jaroslav Kysela1265509c2005-08-12 17:24:24 +0200733static int __devinit snd_opl3sa2_pnp_detect(struct pnp_dev *pdev,
734 const struct pnp_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Takashi Iwai704e0522005-11-17 17:11:53 +0100736 static int dev;
737 int err;
738 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Takashi Iwai704e0522005-11-17 17:11:53 +0100740 if (pnp_device_is_isapnp(pdev))
741 return -ENOENT; /* we have another procedure - card */
742 for (; dev < SNDRV_CARDS; dev++) {
743 if (enable[dev] && isapnp[dev])
744 break;
745 }
746 if (dev >= SNDRV_CARDS)
747 return -ENODEV;
748
749 card = snd_opl3sa2_card_new(dev);
750 if (! card)
751 return -ENOMEM;
752 if ((err = snd_opl3sa2_pnp(dev, card->private_data, pdev)) < 0) {
753 snd_card_free(card);
754 return err;
755 }
756 snd_card_set_dev(card, &pdev->dev);
757 if ((err = snd_opl3sa2_probe(card, dev)) < 0) {
758 snd_card_free(card);
759 return err;
760 }
761 pnp_set_drvdata(pdev, card);
762 dev++;
763 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Jaroslav Kysela1265509c2005-08-12 17:24:24 +0200766static void __devexit snd_opl3sa2_pnp_remove(struct pnp_dev * pdev)
767{
Takashi Iwai704e0522005-11-17 17:11:53 +0100768 snd_card_free(pnp_get_drvdata(pdev));
769 pnp_set_drvdata(pdev, NULL);
Jaroslav Kysela1265509c2005-08-12 17:24:24 +0200770}
771
Takashi Iwai704e0522005-11-17 17:11:53 +0100772#ifdef CONFIG_PM
773static int snd_opl3sa2_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
774{
775 return snd_opl3sa2_suspend(pnp_get_drvdata(pdev), state);
776}
777static int snd_opl3sa2_pnp_resume(struct pnp_dev *pdev)
778{
779 return snd_opl3sa2_resume(pnp_get_drvdata(pdev));
780}
781#endif
782
Jaroslav Kysela1265509c2005-08-12 17:24:24 +0200783static struct pnp_driver opl3sa2_pnp_driver = {
Takashi Iwai62af9902006-01-20 14:03:06 +0100784 .name = "snd-opl3sa2-pnpbios",
Jaroslav Kysela1265509c2005-08-12 17:24:24 +0200785 .id_table = snd_opl3sa2_pnpbiosids,
786 .probe = snd_opl3sa2_pnp_detect,
787 .remove = __devexit_p(snd_opl3sa2_pnp_remove),
Takashi Iwai704e0522005-11-17 17:11:53 +0100788#ifdef CONFIG_PM
789 .suspend = snd_opl3sa2_pnp_suspend,
790 .resume = snd_opl3sa2_pnp_resume,
791#endif
Jaroslav Kysela1265509c2005-08-12 17:24:24 +0200792};
793
Takashi Iwai704e0522005-11-17 17:11:53 +0100794static int __devinit snd_opl3sa2_pnp_cdetect(struct pnp_card_link *pcard,
Jaroslav Kysela1265509c2005-08-12 17:24:24 +0200795 const struct pnp_card_device_id *id)
796{
Takashi Iwai704e0522005-11-17 17:11:53 +0100797 static int dev;
798 struct pnp_dev *pdev;
799 int err;
800 struct snd_card *card;
Jaroslav Kysela1265509c2005-08-12 17:24:24 +0200801
Takashi Iwai704e0522005-11-17 17:11:53 +0100802 pdev = pnp_request_card_device(pcard, id->devs[0].id, NULL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100803 if (pdev == NULL) {
804 snd_printk(KERN_ERR PFX "can't get pnp device from id '%s'\n",
805 id->devs[0].id);
Takashi Iwai704e0522005-11-17 17:11:53 +0100806 return -EBUSY;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100807 }
Takashi Iwai704e0522005-11-17 17:11:53 +0100808 for (; dev < SNDRV_CARDS; dev++) {
809 if (enable[dev] && isapnp[dev])
810 break;
811 }
812 if (dev >= SNDRV_CARDS)
813 return -ENODEV;
814
815 card = snd_opl3sa2_card_new(dev);
816 if (! card)
817 return -ENOMEM;
818 if ((err = snd_opl3sa2_pnp(dev, card->private_data, pdev)) < 0) {
819 snd_card_free(card);
820 return err;
821 }
822 snd_card_set_dev(card, &pdev->dev);
823 if ((err = snd_opl3sa2_probe(card, dev)) < 0) {
824 snd_card_free(card);
825 return err;
826 }
827 pnp_set_card_drvdata(pcard, card);
828 dev++;
829 return 0;
Jaroslav Kysela1265509c2005-08-12 17:24:24 +0200830}
831
832static void __devexit snd_opl3sa2_pnp_cremove(struct pnp_card_link * pcard)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Takashi Iwai704e0522005-11-17 17:11:53 +0100834 snd_card_free(pnp_get_card_drvdata(pcard));
835 pnp_set_card_drvdata(pcard, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Takashi Iwai704e0522005-11-17 17:11:53 +0100838#ifdef CONFIG_PM
839static int snd_opl3sa2_pnp_csuspend(struct pnp_card_link *pcard, pm_message_t state)
840{
841 return snd_opl3sa2_suspend(pnp_get_card_drvdata(pcard), state);
842}
843static int snd_opl3sa2_pnp_cresume(struct pnp_card_link *pcard)
844{
845 return snd_opl3sa2_resume(pnp_get_card_drvdata(pcard));
846}
847#endif
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849static struct pnp_card_driver opl3sa2_pnpc_driver = {
850 .flags = PNP_DRIVER_RES_DISABLE,
Takashi Iwai62af9902006-01-20 14:03:06 +0100851 .name = "snd-opl3sa2-cpnp",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 .id_table = snd_opl3sa2_pnpids,
Jaroslav Kysela1265509c2005-08-12 17:24:24 +0200853 .probe = snd_opl3sa2_pnp_cdetect,
854 .remove = __devexit_p(snd_opl3sa2_pnp_cremove),
Takashi Iwai704e0522005-11-17 17:11:53 +0100855#ifdef CONFIG_PM
856 .suspend = snd_opl3sa2_pnp_csuspend,
857 .resume = snd_opl3sa2_pnp_cresume,
858#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859};
860#endif /* CONFIG_PNP */
861
Takashi Iwai704e0522005-11-17 17:11:53 +0100862static int __init snd_opl3sa2_nonpnp_probe(struct platform_device *pdev)
863{
864 struct snd_card *card;
865 int err;
866 int dev = pdev->id;
867
868 if (port[dev] == SNDRV_AUTO_PORT) {
869 snd_printk(KERN_ERR PFX "specify port\n");
870 return -EINVAL;
871 }
872 if (wss_port[dev] == SNDRV_AUTO_PORT) {
873 snd_printk(KERN_ERR PFX "specify wss_port\n");
874 return -EINVAL;
875 }
876 if (fm_port[dev] == SNDRV_AUTO_PORT) {
877 snd_printk(KERN_ERR PFX "specify fm_port\n");
878 return -EINVAL;
879 }
880 if (midi_port[dev] == SNDRV_AUTO_PORT) {
881 snd_printk(KERN_ERR PFX "specify midi_port\n");
882 return -EINVAL;
883 }
884
885 card = snd_opl3sa2_card_new(dev);
886 if (! card)
887 return -ENOMEM;
888 snd_card_set_dev(card, &pdev->dev);
889 if ((err = snd_opl3sa2_probe(card, dev)) < 0) {
890 snd_card_free(card);
891 return err;
892 }
893 platform_set_drvdata(pdev, card);
894 return 0;
895}
896
897static int snd_opl3sa2_nonpnp_remove(struct platform_device *devptr)
898{
899 snd_card_free(platform_get_drvdata(devptr));
900 platform_set_drvdata(devptr, NULL);
901 return 0;
902}
903
904#ifdef CONFIG_PM
905static int snd_opl3sa2_nonpnp_suspend(struct platform_device *dev, pm_message_t state)
906{
907 return snd_opl3sa2_suspend(platform_get_drvdata(dev), state);
908}
909
910static int snd_opl3sa2_nonpnp_resume(struct platform_device *dev)
911{
912 return snd_opl3sa2_resume(platform_get_drvdata(dev));
913}
914#endif
915
916#define OPL3SA2_DRIVER "snd_opl3sa2"
917
918static struct platform_driver snd_opl3sa2_nonpnp_driver = {
919 .probe = snd_opl3sa2_nonpnp_probe,
920 .remove = snd_opl3sa2_nonpnp_remove,
921#ifdef CONFIG_PM
922 .suspend = snd_opl3sa2_nonpnp_suspend,
923 .resume = snd_opl3sa2_nonpnp_resume,
924#endif
925 .driver = {
926 .name = OPL3SA2_DRIVER
927 },
928};
929
Clemens Ladischf7a92752005-12-07 09:13:42 +0100930static void __init_or_module snd_opl3sa2_unregister_all(void)
931{
932 int i;
933
Takashi Iwai59b1b342006-01-04 15:06:44 +0100934#ifdef CONFIG_PNP
Clemens Ladischf7a92752005-12-07 09:13:42 +0100935 if (pnpc_registered)
936 pnp_unregister_card_driver(&opl3sa2_pnpc_driver);
937 if (pnp_registered)
938 pnp_unregister_driver(&opl3sa2_pnp_driver);
Takashi Iwai59b1b342006-01-04 15:06:44 +0100939#endif
Clemens Ladischf7a92752005-12-07 09:13:42 +0100940 for (i = 0; i < ARRAY_SIZE(platform_devices); ++i)
941 platform_device_unregister(platform_devices[i]);
942 platform_driver_unregister(&snd_opl3sa2_nonpnp_driver);
943}
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945static int __init alsa_card_opl3sa2_init(void)
946{
Takashi Iwai704e0522005-11-17 17:11:53 +0100947 int i, err, cards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Takashi Iwai704e0522005-11-17 17:11:53 +0100949 if ((err = platform_driver_register(&snd_opl3sa2_nonpnp_driver)) < 0)
950 return err;
951
Takashi Iwai8278ca82006-02-20 11:57:34 +0100952 for (i = 0; i < SNDRV_CARDS; i++) {
Takashi Iwai704e0522005-11-17 17:11:53 +0100953 struct platform_device *device;
Takashi Iwai8278ca82006-02-20 11:57:34 +0100954 if (! enable[i])
955 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956#ifdef CONFIG_PNP
Takashi Iwai704e0522005-11-17 17:11:53 +0100957 if (isapnp[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 continue;
959#endif
Takashi Iwai704e0522005-11-17 17:11:53 +0100960 device = platform_device_register_simple(OPL3SA2_DRIVER,
961 i, NULL, 0);
962 if (IS_ERR(device)) {
963 err = PTR_ERR(device);
Clemens Ladischf7a92752005-12-07 09:13:42 +0100964 goto errout;
Takashi Iwai704e0522005-11-17 17:11:53 +0100965 }
Clemens Ladischf7a92752005-12-07 09:13:42 +0100966 platform_devices[i] = device;
Takashi Iwai704e0522005-11-17 17:11:53 +0100967 cards++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
Takashi Iwai704e0522005-11-17 17:11:53 +0100969
Takashi Iwai59b1b342006-01-04 15:06:44 +0100970#ifdef CONFIG_PNP
Takashi Iwai704e0522005-11-17 17:11:53 +0100971 err = pnp_register_driver(&opl3sa2_pnp_driver);
Clemens Ladischf7a92752005-12-07 09:13:42 +0100972 if (err >= 0) {
973 pnp_registered = 1;
Takashi Iwai704e0522005-11-17 17:11:53 +0100974 cards += err;
Clemens Ladischf7a92752005-12-07 09:13:42 +0100975 }
Takashi Iwai704e0522005-11-17 17:11:53 +0100976 err = pnp_register_card_driver(&opl3sa2_pnpc_driver);
Clemens Ladischf7a92752005-12-07 09:13:42 +0100977 if (err >= 0) {
978 pnpc_registered = 1;
Takashi Iwai704e0522005-11-17 17:11:53 +0100979 cards += err;
Clemens Ladischf7a92752005-12-07 09:13:42 +0100980 }
Takashi Iwai59b1b342006-01-04 15:06:44 +0100981#endif
Takashi Iwai704e0522005-11-17 17:11:53 +0100982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (!cards) {
984#ifdef MODULE
985 snd_printk(KERN_ERR "Yamaha OPL3-SA soundcard not found or device busy\n");
986#endif
Clemens Ladischf7a92752005-12-07 09:13:42 +0100987 err = -ENODEV;
988 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990 return 0;
Clemens Ladischf7a92752005-12-07 09:13:42 +0100991
992 errout:
993 snd_opl3sa2_unregister_all();
994 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
997static void __exit alsa_card_opl3sa2_exit(void)
998{
Clemens Ladischf7a92752005-12-07 09:13:42 +0100999 snd_opl3sa2_unregister_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
1002module_init(alsa_card_opl3sa2_init)
1003module_exit(alsa_card_opl3sa2_exit)