Merge refs/heads/upstream from master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[pandora-kernel.git] / sound / isa / sgalaxy.c
1 /*
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>
25 #include <asm/dma.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/time.h>
29 #include <linux/interrupt.h>
30 #include <linux/moduleparam.h>
31 #include <sound/core.h>
32 #include <sound/sb.h>
33 #include <sound/ad1848.h>
34 #include <sound/control.h>
35 #define SNDRV_LEGACY_FIND_FREE_IRQ
36 #define SNDRV_LEGACY_FIND_FREE_DMA
37 #include <sound/initval.h>
38
39 MODULE_AUTHOR("Christopher Butler <chrisb@sandy.force9.co.uk>");
40 MODULE_DESCRIPTION("Aztech Sound Galaxy");
41 MODULE_LICENSE("GPL");
42 MODULE_SUPPORTED_DEVICE("{{Aztech Systems,Sound Galaxy}}");
43
44 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
45 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
46 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;  /* Enable this card */
47 static long sbport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;   /* 0x220,0x240 */
48 static long wssport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;  /* 0x530,0xe80,0xf40,0x604 */
49 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;        /* 7,9,10,11 */
50 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 0,1,3 */
51
52 module_param_array(index, int, NULL, 0444);
53 MODULE_PARM_DESC(index, "Index value for Sound Galaxy soundcard.");
54 module_param_array(id, charp, NULL, 0444);
55 MODULE_PARM_DESC(id, "ID string for Sound Galaxy soundcard.");
56 module_param_array(sbport, long, NULL, 0444);
57 MODULE_PARM_DESC(sbport, "Port # for Sound Galaxy SB driver.");
58 module_param_array(wssport, long, NULL, 0444);
59 MODULE_PARM_DESC(wssport, "Port # for Sound Galaxy WSS driver.");
60 module_param_array(irq, int, NULL, 0444);
61 MODULE_PARM_DESC(irq, "IRQ # for Sound Galaxy driver.");
62 module_param_array(dma1, int, NULL, 0444);
63 MODULE_PARM_DESC(dma1, "DMA1 # for Sound Galaxy driver.");
64
65 #define SGALAXY_AUXC_LEFT 18
66 #define SGALAXY_AUXC_RIGHT 19
67
68 static snd_card_t *snd_sgalaxy_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
69
70 /*
71
72  */
73
74 #define AD1848P1( port, x ) ( port + c_d_c_AD1848##x )
75
76 /* from lowlevel/sb/sb.c - to avoid having to allocate a sb_t for the */
77 /* short time we actually need it.. */
78
79 static int snd_sgalaxy_sbdsp_reset(unsigned long port)
80 {
81         int i;
82
83         outb(1, SBP1(port, RESET));
84         udelay(10);
85         outb(0, SBP1(port, RESET));
86         udelay(30);
87         for (i = 0; i < 1000 && !(inb(SBP1(port, DATA_AVAIL)) & 0x80); i++);
88         if (inb(SBP1(port, READ)) != 0xaa) {
89                 snd_printd("sb_reset: failed at 0x%lx!!!\n", port);
90                 return -ENODEV;
91         }
92         return 0;
93 }
94
95 static int __init snd_sgalaxy_sbdsp_command(unsigned long port, unsigned char val)
96 {
97         int i;
98         
99         for (i = 10000; i; i--)
100                 if ((inb(SBP1(port, STATUS)) & 0x80) == 0) {
101                         outb(val, SBP1(port, COMMAND));
102                         return 1;
103                 }
104
105         return 0;
106 }
107
108 static irqreturn_t snd_sgalaxy_dummy_interrupt(int irq, void *dev_id, struct pt_regs *regs)
109 {
110         return IRQ_NONE;
111 }
112
113 static int __init snd_sgalaxy_setup_wss(unsigned long port, int irq, int dma)
114 {
115         static int interrupt_bits[] = {-1, -1, -1, -1, -1, -1, -1, 0x08, -1, 
116                                        0x10, 0x18, 0x20, -1, -1, -1, -1};
117         static int dma_bits[] = {1, 2, 0, 3};
118         int tmp, tmp1;
119
120         if ((tmp = inb(port + 3)) == 0xff)
121         {
122                 snd_printdd("I/O address dead (0x%lx)\n", port);
123                 return 0;
124         }
125 #if 0
126         snd_printdd("WSS signature = 0x%x\n", tmp);
127 #endif
128
129         if ((tmp & 0x3f) != 0x04 &&
130             (tmp & 0x3f) != 0x0f &&
131             (tmp & 0x3f) != 0x00) {
132                 snd_printdd("No WSS signature detected on port 0x%lx\n",
133                             port + 3);
134                 return 0;
135         }
136
137 #if 0
138         snd_printdd("sgalaxy - setting up IRQ/DMA for WSS\n");
139 #endif
140
141         /* initialize IRQ for WSS codec */
142         tmp = interrupt_bits[irq % 16];
143         if (tmp < 0)
144                 return -EINVAL;
145
146         if (request_irq(irq, snd_sgalaxy_dummy_interrupt, SA_INTERRUPT, "sgalaxy", NULL)) {
147                 snd_printk(KERN_ERR "sgalaxy: can't grab irq %d\n", irq);
148                 return -EIO;
149         }
150
151         outb(tmp | 0x40, port);
152         tmp1 = dma_bits[dma % 4];
153         outb(tmp | tmp1, port);
154
155         free_irq(irq, NULL);
156
157         return 0;
158 }
159
160 static int __init snd_sgalaxy_detect(int dev, int irq, int dma)
161 {
162 #if 0
163         snd_printdd("sgalaxy - switching to WSS mode\n");
164 #endif
165
166         /* switch to WSS mode */
167         snd_sgalaxy_sbdsp_reset(sbport[dev]);
168
169         snd_sgalaxy_sbdsp_command(sbport[dev], 9);
170         snd_sgalaxy_sbdsp_command(sbport[dev], 0);
171
172         udelay(400);
173         return snd_sgalaxy_setup_wss(wssport[dev], irq, dma);
174 }
175
176 static struct ad1848_mix_elem snd_sgalaxy_controls[] = {
177 AD1848_DOUBLE("Aux Playback Switch", 0, SGALAXY_AUXC_LEFT, SGALAXY_AUXC_RIGHT, 7, 7, 1, 1),
178 AD1848_DOUBLE("Aux Playback Volume", 0, SGALAXY_AUXC_LEFT, SGALAXY_AUXC_RIGHT, 0, 0, 31, 0)
179 };
180
181 static int __init snd_sgalaxy_mixer(ad1848_t *chip)
182 {
183         snd_card_t *card = chip->card;
184         snd_ctl_elem_id_t id1, id2;
185         unsigned int idx;
186         int err;
187
188         memset(&id1, 0, sizeof(id1));
189         memset(&id2, 0, sizeof(id2));
190         id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
191         /* reassign AUX0 to LINE */
192         strcpy(id1.name, "Aux Playback Switch");
193         strcpy(id2.name, "Line Playback Switch");
194         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
195                 return err;
196         strcpy(id1.name, "Aux Playback Volume");
197         strcpy(id2.name, "Line Playback Volume");
198         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
199                 return err;
200         /* reassign AUX1 to FM */
201         strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
202         strcpy(id2.name, "FM Playback Switch");
203         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
204                 return err;
205         strcpy(id1.name, "Aux Playback Volume");
206         strcpy(id2.name, "FM Playback Volume");
207         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
208                 return err;
209         /* build AUX2 input */
210         for (idx = 0; idx < ARRAY_SIZE(snd_sgalaxy_controls); idx++) {
211                 if ((err = snd_ad1848_add_ctl_elem(chip, &snd_sgalaxy_controls[idx])) < 0)
212                         return err;
213         }
214         return 0;
215 }
216
217 static int __init snd_sgalaxy_probe(int dev)
218 {
219         static int possible_irqs[] = {7, 9, 10, 11, -1};
220         static int possible_dmas[] = {1, 3, 0, -1};
221         int err, xirq, xdma1;
222         snd_card_t *card;
223         ad1848_t *chip;
224
225         if (sbport[dev] == SNDRV_AUTO_PORT) {
226                 snd_printk("specify SB port\n");
227                 return -EINVAL;
228         }
229         if (wssport[dev] == SNDRV_AUTO_PORT) {
230                 snd_printk("specify WSS port\n");
231                 return -EINVAL;
232         }
233         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
234         if (card == NULL)
235                 return -ENOMEM;
236
237         xirq = irq[dev];
238         if (xirq == SNDRV_AUTO_IRQ) {
239                 if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
240                         snd_card_free(card);
241                         snd_printk("unable to find a free IRQ\n");
242                         return -EBUSY;
243                 }
244         }
245         xdma1 = dma1[dev];
246         if (xdma1 == SNDRV_AUTO_DMA) {
247                 if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
248                         snd_card_free(card);
249                         snd_printk("unable to find a free DMA\n");
250                         return -EBUSY;
251                 }
252         }
253
254         if ((err = snd_sgalaxy_detect(dev, xirq, xdma1)) < 0) {
255                 snd_card_free(card);
256                 return err;
257         }
258
259         if ((err = snd_ad1848_create(card, wssport[dev] + 4,
260                                      xirq, xdma1,
261                                      AD1848_HW_DETECT, &chip)) < 0) {
262                 snd_card_free(card);
263                 return err;
264         }
265
266         if ((err = snd_ad1848_pcm(chip, 0, NULL)) < 0) {
267                 snd_printdd("sgalaxy - error creating new ad1848 PCM device\n");
268                 snd_card_free(card);
269                 return err;
270         }
271         if ((err = snd_ad1848_mixer(chip)) < 0) {
272                 snd_printdd("sgalaxy - error creating new ad1848 mixer\n");
273                 snd_card_free(card);
274                 return err;
275         }
276         if (snd_sgalaxy_mixer(chip) < 0) {
277                 snd_printdd("sgalaxy - the mixer rewrite failed\n");
278                 snd_card_free(card);
279                 return err;
280         }
281
282         strcpy(card->driver, "Sound Galaxy");
283         strcpy(card->shortname, "Sound Galaxy");
284         sprintf(card->longname, "Sound Galaxy at 0x%lx, irq %d, dma %d",
285                 wssport[dev], xirq, xdma1);
286
287         if ((err = snd_card_register(card)) < 0) {
288                 snd_card_free(card);
289                 return err;
290         }
291         snd_sgalaxy_cards[dev] = card;
292         return 0;
293 }
294
295 static int __init alsa_card_sgalaxy_init(void)
296 {
297         int dev, cards;
298
299         for (dev = cards = 0; dev < SNDRV_CARDS && enable[dev]; dev++) {
300                 if (snd_sgalaxy_probe(dev) >= 0)
301                         cards++;
302         }
303         if (!cards) {
304 #ifdef MODULE
305                 snd_printk(KERN_ERR "Sound Galaxy soundcard not found or device busy\n");
306 #endif
307                 return -ENODEV;
308         }
309
310         return 0;
311 }
312
313 static void __exit alsa_card_sgalaxy_exit(void)
314 {
315         int idx;
316
317         for (idx = 0; idx < SNDRV_CARDS; idx++)
318                 snd_card_free(snd_sgalaxy_cards[idx]);
319 }
320
321 module_init(alsa_card_sgalaxy_init)
322 module_exit(alsa_card_sgalaxy_exit)