[ALSA] gus - Use platform_device
[linux-2.6.git] / sound / isa / gus / gusmax.c
1 /*
2  *  Driver for Gravis UltraSound MAX soundcard
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>
24 #include <linux/err.h>
25 #include <linux/platform_device.h>
26 #include <linux/delay.h>
27 #include <linux/time.h>
28 #include <linux/moduleparam.h>
29 #include <asm/dma.h>
30 #include <sound/core.h>
31 #include <sound/gus.h>
32 #include <sound/cs4231.h>
33 #define SNDRV_LEGACY_FIND_FREE_IRQ
34 #define SNDRV_LEGACY_FIND_FREE_DMA
35 #include <sound/initval.h>
36
37 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
38 MODULE_DESCRIPTION("Gravis UltraSound MAX");
39 MODULE_LICENSE("GPL");
40 MODULE_SUPPORTED_DEVICE("{{Gravis,UltraSound MAX}}");
41
42 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
43 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
44 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;  /* Enable this card */
45 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;     /* 0x220,0x230,0x240,0x250,0x260 */
46 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;        /* 2,3,5,9,11,12,15 */
47 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 1,3,5,6,7 */
48 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 1,3,5,6,7 */
49 static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29};
50                                 /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */
51 static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24};
52 static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
53
54 module_param_array(index, int, NULL, 0444);
55 MODULE_PARM_DESC(index, "Index value for GUS MAX soundcard.");
56 module_param_array(id, charp, NULL, 0444);
57 MODULE_PARM_DESC(id, "ID string for GUS MAX soundcard.");
58 module_param_array(enable, bool, NULL, 0444);
59 MODULE_PARM_DESC(enable, "Enable GUS MAX soundcard.");
60 module_param_array(port, long, NULL, 0444);
61 MODULE_PARM_DESC(port, "Port # for GUS MAX driver.");
62 module_param_array(irq, int, NULL, 0444);
63 MODULE_PARM_DESC(irq, "IRQ # for GUS MAX driver.");
64 module_param_array(dma1, int, NULL, 0444);
65 MODULE_PARM_DESC(dma1, "DMA1 # for GUS MAX driver.");
66 module_param_array(dma2, int, NULL, 0444);
67 MODULE_PARM_DESC(dma2, "DMA2 # for GUS MAX driver.");
68 module_param_array(joystick_dac, int, NULL, 0444);
69 MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for GUS MAX driver.");
70 module_param_array(channels, int, NULL, 0444);
71 MODULE_PARM_DESC(channels, "Used GF1 channels for GUS MAX driver.");
72 module_param_array(pcm_channels, int, NULL, 0444);
73 MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for GUS MAX driver.");
74
75 struct snd_gusmax {
76         int irq;
77         struct snd_card *card;
78         struct snd_gus_card *gus;
79         struct snd_cs4231 *cs4231;
80         unsigned short gus_status_reg;
81         unsigned short pcm_status_reg;
82 };
83
84 #define PFX     "gusmax: "
85
86 static int __init snd_gusmax_detect(struct snd_gus_card * gus)
87 {
88         unsigned char d;
89
90         snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0);   /* reset GF1 */
91         if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 0) {
92                 snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d);
93                 return -ENODEV;
94         }
95         udelay(160);
96         snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1);   /* release reset */
97         udelay(160);
98         if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 1) {
99                 snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d);
100                 return -ENODEV;
101         }
102
103         return 0;
104 }
105
106 static irqreturn_t snd_gusmax_interrupt(int irq, void *dev_id, struct pt_regs *regs)
107 {
108         struct snd_gusmax *maxcard = (struct snd_gusmax *) dev_id;
109         int loop, max = 5;
110         int handled = 0;
111
112         do {
113                 loop = 0;
114                 if (inb(maxcard->gus_status_reg)) {
115                         handled = 1;
116                         snd_gus_interrupt(irq, maxcard->gus, regs);
117                         loop++;
118                 }
119                 if (inb(maxcard->pcm_status_reg) & 0x01) { /* IRQ bit is set? */
120                         handled = 1;
121                         snd_cs4231_interrupt(irq, maxcard->cs4231, regs);
122                         loop++;
123                 }
124         } while (loop && --max > 0);
125         return IRQ_RETVAL(handled);
126 }
127
128 static void __init snd_gusmax_init(int dev, struct snd_card *card, struct snd_gus_card * gus)
129 {
130         gus->equal_irq = 1;
131         gus->codec_flag = 1;
132         gus->joystick_dac = joystick_dac[dev];
133         /* init control register */
134         gus->max_cntrl_val = (gus->gf1.port >> 4) & 0x0f;
135         if (gus->gf1.dma1 > 3)
136                 gus->max_cntrl_val |= 0x10;
137         if (gus->gf1.dma2 > 3)
138                 gus->max_cntrl_val |= 0x20;
139         gus->max_cntrl_val |= 0x40;
140         outb(gus->max_cntrl_val, GUSP(gus, MAXCNTRLPORT));
141 }
142
143 #define CS4231_PRIVATE( left, right, shift, mute ) \
144                         ((left << 24)|(right << 16)|(shift<<8)|mute)
145
146 static int __init snd_gusmax_mixer(struct snd_cs4231 *chip)
147 {
148         struct snd_card *card = chip->card;
149         struct snd_ctl_elem_id id1, id2;
150         int err;
151         
152         memset(&id1, 0, sizeof(id1));
153         memset(&id2, 0, sizeof(id2));
154         id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
155         /* reassign AUXA to SYNTHESIZER */
156         strcpy(id1.name, "Aux Playback Switch");
157         strcpy(id2.name, "Synth Playback Switch");
158         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
159                 return err;
160         strcpy(id1.name, "Aux Playback Volume");
161         strcpy(id2.name, "Synth Playback Volume");
162         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
163                 return err;
164         /* reassign AUXB to CD */
165         strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
166         strcpy(id2.name, "CD Playback Switch");
167         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
168                 return err;
169         strcpy(id1.name, "Aux Playback Volume");
170         strcpy(id2.name, "CD Playback Volume");
171         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
172                 return err;
173 #if 0
174         /* reassign Mono Input to MIC */
175         if (snd_mixer_group_rename(mixer,
176                                 SNDRV_MIXER_IN_MONO, 0,
177                                 SNDRV_MIXER_IN_MIC, 0) < 0)
178                 goto __error;
179         if (snd_mixer_elem_rename(mixer,
180                                 SNDRV_MIXER_IN_MONO, 0, SNDRV_MIXER_ETYPE_INPUT,
181                                 SNDRV_MIXER_IN_MIC, 0) < 0)
182                 goto __error;
183         if (snd_mixer_elem_rename(mixer,
184                                 "Mono Capture Volume", 0, SNDRV_MIXER_ETYPE_VOLUME1,
185                                 "Mic Capture Volume", 0) < 0)
186                 goto __error;
187         if (snd_mixer_elem_rename(mixer,
188                                 "Mono Capture Switch", 0, SNDRV_MIXER_ETYPE_SWITCH1,
189                                 "Mic Capture Switch", 0) < 0)
190                 goto __error;
191 #endif
192         return 0;
193 }
194
195 static void snd_gusmax_free(struct snd_card *card)
196 {
197         struct snd_gusmax *maxcard = (struct snd_gusmax *)card->private_data;
198         
199         if (maxcard == NULL)
200                 return;
201         if (maxcard->irq >= 0)
202                 free_irq(maxcard->irq, (void *)maxcard);
203 }
204
205 static int __init snd_gusmax_probe(struct platform_device *pdev)
206 {
207         int dev = pdev->id;
208         static int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, -1};
209         static int possible_dmas[] = {5, 6, 7, 1, 3, -1};
210         int xirq, xdma1, xdma2, err;
211         struct snd_card *card;
212         struct snd_gus_card *gus = NULL;
213         struct snd_cs4231 *cs4231;
214         struct snd_gusmax *maxcard;
215
216         card = snd_card_new(index[dev], id[dev], THIS_MODULE,
217                             sizeof(struct snd_gusmax));
218         if (card == NULL)
219                 return -ENOMEM;
220         card->private_free = snd_gusmax_free;
221         maxcard = (struct snd_gusmax *)card->private_data;
222         maxcard->card = card;
223         maxcard->irq = -1;
224         
225         xirq = irq[dev];
226         if (xirq == SNDRV_AUTO_IRQ) {
227                 if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
228                         snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
229                         err = -EBUSY;
230                         goto _err;
231                 }
232         }
233         xdma1 = dma1[dev];
234         if (xdma1 == SNDRV_AUTO_DMA) {
235                 if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
236                         snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
237                         err = -EBUSY;
238                         goto _err;
239                 }
240         }
241         xdma2 = dma2[dev];
242         if (xdma2 == SNDRV_AUTO_DMA) {
243                 if ((xdma2 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
244                         snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
245                         err = -EBUSY;
246                         goto _err;
247                 }
248         }
249
250         if (port[dev] != SNDRV_AUTO_PORT) {
251                 err = snd_gus_create(card,
252                                      port[dev],
253                                      -xirq, xdma1, xdma2,
254                                      0, channels[dev],
255                                      pcm_channels[dev],
256                                      0, &gus);
257         } else {
258                 static unsigned long possible_ports[] = {
259                         0x220, 0x230, 0x240, 0x250, 0x260
260                 };
261                 int i;
262                 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
263                         err = snd_gus_create(card,
264                                              possible_ports[i],
265                                              -xirq, xdma1, xdma2,
266                                              0, channels[dev],
267                                              pcm_channels[dev],
268                                              0, &gus);
269                         if (err >= 0) {
270                                 port[dev] = possible_ports[i];
271                                 break;
272                         }
273                 }
274         }
275         if (err < 0)
276                 goto _err;
277
278         if ((err = snd_gusmax_detect(gus)) < 0)
279                 goto _err;
280
281         maxcard->gus_status_reg = gus->gf1.reg_irqstat;
282         maxcard->pcm_status_reg = gus->gf1.port + 0x10c + 2;
283         snd_gusmax_init(dev, card, gus);
284         if ((err = snd_gus_initialize(gus)) < 0)
285                 goto _err;
286
287         if (!gus->max_flag) {
288                 snd_printk(KERN_ERR PFX "GUS MAX soundcard was not detected at 0x%lx\n", gus->gf1.port);
289                 err = -ENODEV;
290                 goto _err;
291         }
292
293         if (request_irq(xirq, snd_gusmax_interrupt, SA_INTERRUPT, "GUS MAX", (void *)maxcard)) {
294                 snd_printk(KERN_ERR PFX "unable to grab IRQ %d\n", xirq);
295                 err = -EBUSY;
296                 goto _err;
297         }
298         maxcard->irq = xirq;
299         
300         if ((err = snd_cs4231_create(card,
301                                      gus->gf1.port + 0x10c, -1, xirq,
302                                      xdma2 < 0 ? xdma1 : xdma2, xdma1,
303                                      CS4231_HW_DETECT,
304                                      CS4231_HWSHARE_IRQ |
305                                      CS4231_HWSHARE_DMA1 |
306                                      CS4231_HWSHARE_DMA2,
307                                      &cs4231)) < 0)
308                 goto _err;
309
310         if ((err = snd_cs4231_pcm(cs4231, 0, NULL)) < 0)
311                 goto _err;
312
313         if ((err = snd_cs4231_mixer(cs4231)) < 0)
314                 goto _err;
315
316         if ((err = snd_cs4231_timer(cs4231, 2, NULL)) < 0)
317                 goto _err;
318
319         if (pcm_channels[dev] > 0) {
320                 if ((err = snd_gf1_pcm_new(gus, 1, 1, NULL)) < 0)
321                         goto _err;
322         }
323         if ((err = snd_gusmax_mixer(cs4231)) < 0)
324                 goto _err;
325
326         if ((err = snd_gf1_rawmidi_new(gus, 0, NULL)) < 0)
327                 goto _err;
328
329         sprintf(card->longname + strlen(card->longname), " at 0x%lx, irq %i, dma %i", gus->gf1.port, xirq, xdma1);
330         if (xdma2 >= 0)
331                 sprintf(card->longname + strlen(card->longname), "&%i", xdma2);
332
333         snd_card_set_dev(card, &pdev->dev);
334
335         if ((err = snd_card_register(card)) < 0)
336                 goto _err;
337                 
338         maxcard->gus = gus;
339         maxcard->cs4231 = cs4231;
340
341         platform_set_drvdata(pdev, card);
342         return 0;
343
344  _err:
345         snd_card_free(card);
346         return err;
347 }
348
349 static int snd_gusmax_remove(struct platform_device *devptr)
350 {
351         snd_card_free(platform_get_drvdata(devptr));
352         platform_set_drvdata(devptr, NULL);
353         return 0;
354 }
355
356 #define GUSMAX_DRIVER   "snd_gusmax"
357
358 static struct platform_driver snd_gusmax_driver = {
359         .probe          = snd_gusmax_probe,
360         .remove         = snd_gusmax_remove,
361         /* FIXME: suspend/resume */
362         .driver         = {
363                 .name   = GUSMAX_DRIVER
364         },
365 };
366
367 static int __init alsa_card_gusmax_init(void)
368 {
369         int i, cards, err;
370
371         err = platform_driver_register(&snd_gusmax_driver);
372         if (err < 0)
373                 return err;
374
375         cards = 0;
376         for (i = 0; i < SNDRV_CARDS && enable[i]; i++) {
377                 struct platform_device *device;
378                 device = platform_device_register_simple(GUSMAX_DRIVER,
379                                                          i, NULL, 0);
380                 if (IS_ERR(device)) {
381                         err = PTR_ERR(device);
382                         goto errout;
383                 }
384                 cards++;
385         }
386         if (!cards) {
387 #ifdef MODULE
388                 printk(KERN_ERR "GUS MAX soundcard not found or device busy\n");
389 #endif
390                 err = -ENODEV;
391                 goto errout;
392         }
393         return 0;
394
395  errout:
396         platform_driver_unregister(&snd_gusmax_driver);
397         return err;
398 }
399
400 static void __exit alsa_card_gusmax_exit(void)
401 {
402         platform_driver_unregister(&snd_gusmax_driver);
403 }
404
405 module_init(alsa_card_gusmax_init)
406 module_exit(alsa_card_gusmax_exit)