blob: 922519def0997f2ff21ee74fc84ecb073012411c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for Aztech Sound Galaxy cards
3 * Copyright (c) by Christopher Butler <chrisb@sandy.force9.co.uk.
4 *
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)
7 *
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.
12 *
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.
17 *
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
21 *
22 */
23
24#include <sound/driver.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/init.h>
Takashi Iwaifeb158e6a2005-11-17 17:12:43 +010026#include <linux/err.h>
Takashi Iwai5e24c1c2007-02-22 12:50:54 +010027#include <linux/isa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/delay.h>
29#include <linux/time.h>
30#include <linux/interrupt.h>
31#include <linux/moduleparam.h>
Takashi Iwaifeb158e6a2005-11-17 17:12:43 +010032#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#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>
40
41MODULE_AUTHOR("Christopher Butler <chrisb@sandy.force9.co.uk>");
42MODULE_DESCRIPTION("Aztech Sound Galaxy");
43MODULE_LICENSE("GPL");
44MODULE_SUPPORTED_DEVICE("{{Aztech Systems,Sound Galaxy}}");
45
46static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
47static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
48static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
49static long sbport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240 */
50static long wssport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x530,0xe80,0xf40,0x604 */
51static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 7,9,10,11 */
52static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
53
54module_param_array(index, int, NULL, 0444);
55MODULE_PARM_DESC(index, "Index value for Sound Galaxy soundcard.");
56module_param_array(id, charp, NULL, 0444);
57MODULE_PARM_DESC(id, "ID string for Sound Galaxy soundcard.");
58module_param_array(sbport, long, NULL, 0444);
59MODULE_PARM_DESC(sbport, "Port # for Sound Galaxy SB driver.");
60module_param_array(wssport, long, NULL, 0444);
61MODULE_PARM_DESC(wssport, "Port # for Sound Galaxy WSS driver.");
62module_param_array(irq, int, NULL, 0444);
63MODULE_PARM_DESC(irq, "IRQ # for Sound Galaxy driver.");
64module_param_array(dma1, int, NULL, 0444);
65MODULE_PARM_DESC(dma1, "DMA1 # for Sound Galaxy driver.");
66
67#define SGALAXY_AUXC_LEFT 18
68#define SGALAXY_AUXC_RIGHT 19
69
Takashi Iwai43bcd972005-09-05 17:19:20 +020070#define PFX "sgalaxy: "
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
73
74 */
75
76#define AD1848P1( port, x ) ( port + c_d_c_AD1848##x )
77
Takashi Iwai11ff5c62005-11-17 14:42:36 +010078/* from lowlevel/sb/sb.c - to avoid having to allocate a struct snd_sb for the */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* short time we actually need it.. */
80
81static int snd_sgalaxy_sbdsp_reset(unsigned long port)
82{
83 int i;
84
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;
93 }
94 return 0;
95}
96
Takashi Iwai5e24c1c2007-02-22 12:50:54 +010097static int __devinit snd_sgalaxy_sbdsp_command(unsigned long port,
98 unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 int i;
101
102 for (i = 10000; i; i--)
103 if ((inb(SBP1(port, STATUS)) & 0x80) == 0) {
104 outb(val, SBP1(port, COMMAND));
105 return 1;
106 }
107
108 return 0;
109}
110
David Howells7d12e782006-10-05 14:55:46 +0100111static irqreturn_t snd_sgalaxy_dummy_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 return IRQ_NONE;
114}
115
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100116static int __devinit snd_sgalaxy_setup_wss(unsigned long port, int irq, int dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 static int interrupt_bits[] = {-1, -1, -1, -1, -1, -1, -1, 0x08, -1,
119 0x10, 0x18, 0x20, -1, -1, -1, -1};
120 static int dma_bits[] = {1, 2, 0, 3};
121 int tmp, tmp1;
122
123 if ((tmp = inb(port + 3)) == 0xff)
124 {
125 snd_printdd("I/O address dead (0x%lx)\n", port);
126 return 0;
127 }
128#if 0
129 snd_printdd("WSS signature = 0x%x\n", tmp);
130#endif
131
132 if ((tmp & 0x3f) != 0x04 &&
133 (tmp & 0x3f) != 0x0f &&
134 (tmp & 0x3f) != 0x00) {
135 snd_printdd("No WSS signature detected on port 0x%lx\n",
136 port + 3);
137 return 0;
138 }
139
140#if 0
Takashi Iwai43bcd972005-09-05 17:19:20 +0200141 snd_printdd(PFX "setting up IRQ/DMA for WSS\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#endif
143
144 /* initialize IRQ for WSS codec */
145 tmp = interrupt_bits[irq % 16];
146 if (tmp < 0)
147 return -EINVAL;
148
Thomas Gleixner65ca68b2006-07-01 19:29:46 -0700149 if (request_irq(irq, snd_sgalaxy_dummy_interrupt, IRQF_DISABLED, "sgalaxy", NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 snd_printk(KERN_ERR "sgalaxy: can't grab irq %d\n", irq);
151 return -EIO;
152 }
153
154 outb(tmp | 0x40, port);
155 tmp1 = dma_bits[dma % 4];
156 outb(tmp | tmp1, port);
157
158 free_irq(irq, NULL);
159
160 return 0;
161}
162
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100163static int __devinit snd_sgalaxy_detect(int dev, int irq, int dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165#if 0
Takashi Iwai43bcd972005-09-05 17:19:20 +0200166 snd_printdd(PFX "switching to WSS mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#endif
168
169 /* switch to WSS mode */
170 snd_sgalaxy_sbdsp_reset(sbport[dev]);
171
172 snd_sgalaxy_sbdsp_command(sbport[dev], 9);
173 snd_sgalaxy_sbdsp_command(sbport[dev], 0);
174
175 udelay(400);
176 return snd_sgalaxy_setup_wss(wssport[dev], irq, dma);
177}
178
179static struct ad1848_mix_elem snd_sgalaxy_controls[] = {
180AD1848_DOUBLE("Aux Playback Switch", 0, SGALAXY_AUXC_LEFT, SGALAXY_AUXC_RIGHT, 7, 7, 1, 1),
181AD1848_DOUBLE("Aux Playback Volume", 0, SGALAXY_AUXC_LEFT, SGALAXY_AUXC_RIGHT, 0, 0, 31, 0)
182};
183
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100184static int __devinit snd_sgalaxy_mixer(struct snd_ad1848 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Takashi Iwai11ff5c62005-11-17 14:42:36 +0100186 struct snd_card *card = chip->card;
187 struct snd_ctl_elem_id id1, id2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 unsigned int idx;
189 int err;
190
191 memset(&id1, 0, sizeof(id1));
192 memset(&id2, 0, sizeof(id2));
193 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
194 /* reassign AUX0 to LINE */
195 strcpy(id1.name, "Aux Playback Switch");
196 strcpy(id2.name, "Line Playback Switch");
197 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
198 return err;
199 strcpy(id1.name, "Aux Playback Volume");
200 strcpy(id2.name, "Line Playback Volume");
201 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
202 return err;
203 /* reassign AUX1 to FM */
204 strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
205 strcpy(id2.name, "FM Playback Switch");
206 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
207 return err;
208 strcpy(id1.name, "Aux Playback Volume");
209 strcpy(id2.name, "FM Playback Volume");
210 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
211 return err;
212 /* build AUX2 input */
213 for (idx = 0; idx < ARRAY_SIZE(snd_sgalaxy_controls); idx++) {
214 if ((err = snd_ad1848_add_ctl_elem(chip, &snd_sgalaxy_controls[idx])) < 0)
215 return err;
216 }
217 return 0;
218}
219
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100220static int __devinit snd_sgalaxy_match(struct device *devptr, unsigned int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100222 if (!enable[dev])
223 return 0;
224 if (sbport[dev] == SNDRV_AUTO_PORT) {
225 snd_printk(KERN_ERR PFX "specify SB port\n");
226 return 0;
227 }
228 if (wssport[dev] == SNDRV_AUTO_PORT) {
229 snd_printk(KERN_ERR PFX "specify WSS port\n");
230 return 0;
231 }
232 return 1;
233}
234
235static int __devinit snd_sgalaxy_probe(struct device *devptr, unsigned int dev)
236{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 static int possible_irqs[] = {7, 9, 10, 11, -1};
238 static int possible_dmas[] = {1, 3, 0, -1};
239 int err, xirq, xdma1;
Takashi Iwai11ff5c62005-11-17 14:42:36 +0100240 struct snd_card *card;
241 struct snd_ad1848 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
244 if (card == NULL)
245 return -ENOMEM;
246
247 xirq = irq[dev];
248 if (xirq == SNDRV_AUTO_IRQ) {
249 if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200250 snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
251 err = -EBUSY;
252 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254 }
255 xdma1 = dma1[dev];
256 if (xdma1 == SNDRV_AUTO_DMA) {
257 if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200258 snd_printk(KERN_ERR PFX "unable to find a free DMA\n");
259 err = -EBUSY;
260 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262 }
263
Takashi Iwai43bcd972005-09-05 17:19:20 +0200264 if ((err = snd_sgalaxy_detect(dev, xirq, xdma1)) < 0)
265 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 if ((err = snd_ad1848_create(card, wssport[dev] + 4,
268 xirq, xdma1,
Takashi Iwai43bcd972005-09-05 17:19:20 +0200269 AD1848_HW_DETECT, &chip)) < 0)
270 goto _err;
Takashi Iwaifeb158e6a2005-11-17 17:12:43 +0100271 card->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 if ((err = snd_ad1848_pcm(chip, 0, NULL)) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200274 snd_printdd(PFX "error creating new ad1848 PCM device\n");
275 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277 if ((err = snd_ad1848_mixer(chip)) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200278 snd_printdd(PFX "error creating new ad1848 mixer\n");
279 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
Takashi Iwai43bcd972005-09-05 17:19:20 +0200281 if ((err = snd_sgalaxy_mixer(chip)) < 0) {
282 snd_printdd(PFX "the mixer rewrite failed\n");
283 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285
286 strcpy(card->driver, "Sound Galaxy");
287 strcpy(card->shortname, "Sound Galaxy");
288 sprintf(card->longname, "Sound Galaxy at 0x%lx, irq %d, dma %d",
289 wssport[dev], xirq, xdma1);
290
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100291 snd_card_set_dev(card, devptr);
Takashi Iwai43bcd972005-09-05 17:19:20 +0200292
293 if ((err = snd_card_register(card)) < 0)
294 goto _err;
295
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100296 dev_set_drvdata(devptr, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return 0;
Takashi Iwai43bcd972005-09-05 17:19:20 +0200298
299 _err:
300 snd_card_free(card);
301 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100304static int __devexit snd_sgalaxy_remove(struct device *devptr, unsigned int dev)
Takashi Iwaifeb158e6a2005-11-17 17:12:43 +0100305{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100306 snd_card_free(dev_get_drvdata(devptr));
307 dev_set_drvdata(devptr, NULL);
Takashi Iwaifeb158e6a2005-11-17 17:12:43 +0100308 return 0;
309}
310
311#ifdef CONFIG_PM
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100312static int snd_sgalaxy_suspend(struct device *pdev, unsigned int n,
313 pm_message_t state)
Takashi Iwaifeb158e6a2005-11-17 17:12:43 +0100314{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100315 struct snd_card *card = dev_get_drvdata(pdev);
Takashi Iwaifeb158e6a2005-11-17 17:12:43 +0100316 struct snd_ad1848 *chip = card->private_data;
317
318 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
319 chip->suspend(chip);
320 return 0;
321}
322
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100323static int snd_sgalaxy_resume(struct device *pdev, unsigned int n)
Takashi Iwaifeb158e6a2005-11-17 17:12:43 +0100324{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100325 struct snd_card *card = dev_get_drvdata(pdev);
Takashi Iwaifeb158e6a2005-11-17 17:12:43 +0100326 struct snd_ad1848 *chip = card->private_data;
327
328 chip->resume(chip);
329 snd_ad1848_out(chip, SGALAXY_AUXC_LEFT, chip->image[SGALAXY_AUXC_LEFT]);
330 snd_ad1848_out(chip, SGALAXY_AUXC_RIGHT, chip->image[SGALAXY_AUXC_RIGHT]);
331
332 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
333 return 0;
334}
335#endif
336
Rene Herman83c51c02007-03-20 11:33:46 +0100337#define DEV_NAME "sgalaxy"
Takashi Iwaifeb158e6a2005-11-17 17:12:43 +0100338
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100339static struct isa_driver snd_sgalaxy_driver = {
340 .match = snd_sgalaxy_match,
Takashi Iwaifeb158e6a2005-11-17 17:12:43 +0100341 .probe = snd_sgalaxy_probe,
342 .remove = __devexit_p(snd_sgalaxy_remove),
343#ifdef CONFIG_PM
344 .suspend = snd_sgalaxy_suspend,
345 .resume = snd_sgalaxy_resume,
346#endif
347 .driver = {
Rene Herman83c51c02007-03-20 11:33:46 +0100348 .name = DEV_NAME
Takashi Iwaifeb158e6a2005-11-17 17:12:43 +0100349 },
350};
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352static int __init alsa_card_sgalaxy_init(void)
353{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100354 return isa_register_driver(&snd_sgalaxy_driver, SNDRV_CARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357static void __exit alsa_card_sgalaxy_exit(void)
358{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100359 isa_unregister_driver(&snd_sgalaxy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
362module_init(alsa_card_sgalaxy_init)
363module_exit(alsa_card_sgalaxy_exit)