[ALSA] sgalaxy - Use platform_device, add PM support
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / isa / sgalaxy.c
blob2e1d4677fe12feaebe71c0d5d02eeca5c6b10800
1 /*
2 * Driver for Aztech Sound Galaxy cards
3 * Copyright (c) by Christopher Butler <chrisb@sandy.force9.co.uk.
5 * I don't have documentation for this card, I based this driver on the
6 * driver for OSS/Free included in the kernel source (drivers/sound/sgalaxy.c)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sound/driver.h>
25 #include <linux/init.h>
26 #include <linux/err.h>
27 #include <linux/platform_device.h>
28 #include <linux/delay.h>
29 #include <linux/time.h>
30 #include <linux/interrupt.h>
31 #include <linux/moduleparam.h>
32 #include <asm/dma.h>
33 #include <sound/core.h>
34 #include <sound/sb.h>
35 #include <sound/ad1848.h>
36 #include <sound/control.h>
37 #define SNDRV_LEGACY_FIND_FREE_IRQ
38 #define SNDRV_LEGACY_FIND_FREE_DMA
39 #include <sound/initval.h>
41 MODULE_AUTHOR("Christopher Butler <chrisb@sandy.force9.co.uk>");
42 MODULE_DESCRIPTION("Aztech Sound Galaxy");
43 MODULE_LICENSE("GPL");
44 MODULE_SUPPORTED_DEVICE("{{Aztech Systems,Sound Galaxy}}");
46 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
47 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
48 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
49 static long sbport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240 */
50 static long wssport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x530,0xe80,0xf40,0x604 */
51 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 7,9,10,11 */
52 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
54 module_param_array(index, int, NULL, 0444);
55 MODULE_PARM_DESC(index, "Index value for Sound Galaxy soundcard.");
56 module_param_array(id, charp, NULL, 0444);
57 MODULE_PARM_DESC(id, "ID string for Sound Galaxy soundcard.");
58 module_param_array(sbport, long, NULL, 0444);
59 MODULE_PARM_DESC(sbport, "Port # for Sound Galaxy SB driver.");
60 module_param_array(wssport, long, NULL, 0444);
61 MODULE_PARM_DESC(wssport, "Port # for Sound Galaxy WSS driver.");
62 module_param_array(irq, int, NULL, 0444);
63 MODULE_PARM_DESC(irq, "IRQ # for Sound Galaxy driver.");
64 module_param_array(dma1, int, NULL, 0444);
65 MODULE_PARM_DESC(dma1, "DMA1 # for Sound Galaxy driver.");
67 #define SGALAXY_AUXC_LEFT 18
68 #define SGALAXY_AUXC_RIGHT 19
70 #define PFX "sgalaxy: "
76 #define AD1848P1( port, x ) ( port + c_d_c_AD1848##x )
78 /* from lowlevel/sb/sb.c - to avoid having to allocate a struct snd_sb for the */
79 /* short time we actually need it.. */
81 static int snd_sgalaxy_sbdsp_reset(unsigned long port)
83 int i;
85 outb(1, SBP1(port, RESET));
86 udelay(10);
87 outb(0, SBP1(port, RESET));
88 udelay(30);
89 for (i = 0; i < 1000 && !(inb(SBP1(port, DATA_AVAIL)) & 0x80); i++);
90 if (inb(SBP1(port, READ)) != 0xaa) {
91 snd_printd("sb_reset: failed at 0x%lx!!!\n", port);
92 return -ENODEV;
94 return 0;
97 static int __init snd_sgalaxy_sbdsp_command(unsigned long port, unsigned char val)
99 int i;
101 for (i = 10000; i; i--)
102 if ((inb(SBP1(port, STATUS)) & 0x80) == 0) {
103 outb(val, SBP1(port, COMMAND));
104 return 1;
107 return 0;
110 static irqreturn_t snd_sgalaxy_dummy_interrupt(int irq, void *dev_id, struct pt_regs *regs)
112 return IRQ_NONE;
115 static int __init snd_sgalaxy_setup_wss(unsigned long port, int irq, int dma)
117 static int interrupt_bits[] = {-1, -1, -1, -1, -1, -1, -1, 0x08, -1,
118 0x10, 0x18, 0x20, -1, -1, -1, -1};
119 static int dma_bits[] = {1, 2, 0, 3};
120 int tmp, tmp1;
122 if ((tmp = inb(port + 3)) == 0xff)
124 snd_printdd("I/O address dead (0x%lx)\n", port);
125 return 0;
127 #if 0
128 snd_printdd("WSS signature = 0x%x\n", tmp);
129 #endif
131 if ((tmp & 0x3f) != 0x04 &&
132 (tmp & 0x3f) != 0x0f &&
133 (tmp & 0x3f) != 0x00) {
134 snd_printdd("No WSS signature detected on port 0x%lx\n",
135 port + 3);
136 return 0;
139 #if 0
140 snd_printdd(PFX "setting up IRQ/DMA for WSS\n");
141 #endif
143 /* initialize IRQ for WSS codec */
144 tmp = interrupt_bits[irq % 16];
145 if (tmp < 0)
146 return -EINVAL;
148 if (request_irq(irq, snd_sgalaxy_dummy_interrupt, SA_INTERRUPT, "sgalaxy", NULL)) {
149 snd_printk(KERN_ERR "sgalaxy: can't grab irq %d\n", irq);
150 return -EIO;
153 outb(tmp | 0x40, port);
154 tmp1 = dma_bits[dma % 4];
155 outb(tmp | tmp1, port);
157 free_irq(irq, NULL);
159 return 0;
162 static int __init snd_sgalaxy_detect(int dev, int irq, int dma)
164 #if 0
165 snd_printdd(PFX "switching to WSS mode\n");
166 #endif
168 /* switch to WSS mode */
169 snd_sgalaxy_sbdsp_reset(sbport[dev]);
171 snd_sgalaxy_sbdsp_command(sbport[dev], 9);
172 snd_sgalaxy_sbdsp_command(sbport[dev], 0);
174 udelay(400);
175 return snd_sgalaxy_setup_wss(wssport[dev], irq, dma);
178 static struct ad1848_mix_elem snd_sgalaxy_controls[] = {
179 AD1848_DOUBLE("Aux Playback Switch", 0, SGALAXY_AUXC_LEFT, SGALAXY_AUXC_RIGHT, 7, 7, 1, 1),
180 AD1848_DOUBLE("Aux Playback Volume", 0, SGALAXY_AUXC_LEFT, SGALAXY_AUXC_RIGHT, 0, 0, 31, 0)
183 static int __init snd_sgalaxy_mixer(struct snd_ad1848 *chip)
185 struct snd_card *card = chip->card;
186 struct snd_ctl_elem_id id1, id2;
187 unsigned int idx;
188 int err;
190 memset(&id1, 0, sizeof(id1));
191 memset(&id2, 0, sizeof(id2));
192 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
193 /* reassign AUX0 to LINE */
194 strcpy(id1.name, "Aux Playback Switch");
195 strcpy(id2.name, "Line Playback Switch");
196 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
197 return err;
198 strcpy(id1.name, "Aux Playback Volume");
199 strcpy(id2.name, "Line Playback Volume");
200 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
201 return err;
202 /* reassign AUX1 to FM */
203 strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
204 strcpy(id2.name, "FM Playback Switch");
205 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
206 return err;
207 strcpy(id1.name, "Aux Playback Volume");
208 strcpy(id2.name, "FM Playback Volume");
209 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
210 return err;
211 /* build AUX2 input */
212 for (idx = 0; idx < ARRAY_SIZE(snd_sgalaxy_controls); idx++) {
213 if ((err = snd_ad1848_add_ctl_elem(chip, &snd_sgalaxy_controls[idx])) < 0)
214 return err;
216 return 0;
219 static int __init snd_sgalaxy_probe(struct platform_device *devptr)
221 int dev = devptr->id;
222 static int possible_irqs[] = {7, 9, 10, 11, -1};
223 static int possible_dmas[] = {1, 3, 0, -1};
224 int err, xirq, xdma1;
225 struct snd_card *card;
226 struct snd_ad1848 *chip;
228 if (sbport[dev] == SNDRV_AUTO_PORT) {
229 snd_printk(KERN_ERR PFX "specify SB port\n");
230 return -EINVAL;
232 if (wssport[dev] == SNDRV_AUTO_PORT) {
233 snd_printk(KERN_ERR PFX "specify WSS port\n");
234 return -EINVAL;
236 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
237 if (card == NULL)
238 return -ENOMEM;
240 xirq = irq[dev];
241 if (xirq == SNDRV_AUTO_IRQ) {
242 if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
243 snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
244 err = -EBUSY;
245 goto _err;
248 xdma1 = dma1[dev];
249 if (xdma1 == SNDRV_AUTO_DMA) {
250 if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
251 snd_printk(KERN_ERR PFX "unable to find a free DMA\n");
252 err = -EBUSY;
253 goto _err;
257 if ((err = snd_sgalaxy_detect(dev, xirq, xdma1)) < 0)
258 goto _err;
260 if ((err = snd_ad1848_create(card, wssport[dev] + 4,
261 xirq, xdma1,
262 AD1848_HW_DETECT, &chip)) < 0)
263 goto _err;
264 card->private_data = chip;
266 if ((err = snd_ad1848_pcm(chip, 0, NULL)) < 0) {
267 snd_printdd(PFX "error creating new ad1848 PCM device\n");
268 goto _err;
270 if ((err = snd_ad1848_mixer(chip)) < 0) {
271 snd_printdd(PFX "error creating new ad1848 mixer\n");
272 goto _err;
274 if ((err = snd_sgalaxy_mixer(chip)) < 0) {
275 snd_printdd(PFX "the mixer rewrite failed\n");
276 goto _err;
279 strcpy(card->driver, "Sound Galaxy");
280 strcpy(card->shortname, "Sound Galaxy");
281 sprintf(card->longname, "Sound Galaxy at 0x%lx, irq %d, dma %d",
282 wssport[dev], xirq, xdma1);
284 snd_card_set_dev(card, &devptr->dev);
286 if ((err = snd_card_register(card)) < 0)
287 goto _err;
289 platform_set_drvdata(devptr, card);
290 return 0;
292 _err:
293 snd_card_free(card);
294 return err;
297 static int __devexit snd_sgalaxy_remove(struct platform_device *devptr)
299 snd_card_free(platform_get_drvdata(devptr));
300 platform_set_drvdata(devptr, NULL);
301 return 0;
304 #ifdef CONFIG_PM
305 static int snd_sgalaxy_suspend(struct platform_device *pdev, pm_message_t state)
307 struct snd_card *card = platform_get_drvdata(pdev);
308 struct snd_ad1848 *chip = card->private_data;
310 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
311 chip->suspend(chip);
312 return 0;
315 static int snd_sgalaxy_resume(struct platform_device *pdev)
317 struct snd_card *card = platform_get_drvdata(pdev);
318 struct snd_ad1848 *chip = card->private_data;
320 chip->resume(chip);
321 snd_ad1848_out(chip, SGALAXY_AUXC_LEFT, chip->image[SGALAXY_AUXC_LEFT]);
322 snd_ad1848_out(chip, SGALAXY_AUXC_RIGHT, chip->image[SGALAXY_AUXC_RIGHT]);
324 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
325 return 0;
327 #endif
329 #define SND_SGALAXY_DRIVER "snd_sgalaxy"
331 static struct platform_driver snd_sgalaxy_driver = {
332 .probe = snd_sgalaxy_probe,
333 .remove = __devexit_p(snd_sgalaxy_remove),
334 #ifdef CONFIG_PM
335 .suspend = snd_sgalaxy_suspend,
336 .resume = snd_sgalaxy_resume,
337 #endif
338 .driver = {
339 .name = SND_SGALAXY_DRIVER
343 static int __init alsa_card_sgalaxy_init(void)
345 int i, cards, err;
347 err = platform_driver_register(&snd_sgalaxy_driver);
348 if (err < 0)
349 return err;
351 cards = 0;
352 for (i = 0; i < SNDRV_CARDS && enable[i]; i++) {
353 struct platform_device *device;
354 device = platform_device_register_simple(SND_SGALAXY_DRIVER,
355 i, NULL, 0);
356 if (IS_ERR(device)) {
357 err = PTR_ERR(device);
358 goto errout;
360 cards++;
362 if (!cards) {
363 #ifdef MODULE
364 snd_printk(KERN_ERR "Sound Galaxy soundcard not found or device busy\n");
365 #endif
366 err = -ENODEV;
367 goto errout;
369 return 0;
371 errout:
372 platform_driver_unregister(&snd_sgalaxy_driver);
373 return err;
376 static void __exit alsa_card_sgalaxy_exit(void)
378 platform_driver_unregister(&snd_sgalaxy_driver);
381 module_init(alsa_card_sgalaxy_init)
382 module_exit(alsa_card_sgalaxy_exit)