[ALSA] Minor clean up and fixes for CS5535 audio driver
[safe/jmp/linux-2.6] / sound / pci / cs5535audio / cs5535audio.c
1 /*
2  * Driver for audio on multifunction CS5535 companion device
3  * Copyright (C) Jaya Kumar
4  *
5  * Based on Jaroslav Kysela and Takashi Iwai's examples.
6  * This work was sponsored by CIS(M) Sdn Bhd.
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 <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/init.h>
27 #include <linux/pci.h>
28 #include <linux/slab.h>
29 #include <linux/moduleparam.h>
30 #include <asm/io.h>
31 #include <sound/driver.h>
32 #include <sound/core.h>
33 #include <sound/control.h>
34 #include <sound/pcm.h>
35 #include <sound/rawmidi.h>
36 #include <sound/ac97_codec.h>
37 #include <sound/initval.h>
38 #include <sound/asoundef.h>
39 #include "cs5535audio.h"
40
41 #define DRIVER_NAME "cs5535audio"
42
43
44 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
45 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
46 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
47
48 static struct pci_device_id snd_cs5535audio_ids[] = {
49         { PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_AUDIO, PCI_ANY_ID,
50                 PCI_ANY_ID, 0, 0, 0, },
51         {}
52 };
53
54 MODULE_DEVICE_TABLE(pci, snd_cs5535audio_ids);
55
56 static void wait_till_cmd_acked(cs5535audio_t *cs5535au, unsigned long timeout)
57 {
58         unsigned int tmp;
59         do {
60                 tmp = cs_readl(cs5535au, ACC_CODEC_CNTL);
61                 if (!(tmp & CMD_NEW))
62                         break;
63                 msleep(10);
64         } while (--timeout);
65         if (!timeout)
66                 snd_printk(KERN_ERR "Failure writing to cs5535 codec\n");
67 }
68
69 static unsigned short snd_cs5535audio_codec_read(cs5535audio_t *cs5535au,
70                                                         unsigned short reg)
71 {
72         unsigned int regdata;
73         int timeout;
74         unsigned int val;
75
76         regdata = ((unsigned int) reg) << 24;
77         regdata |= ACC_CODEC_CNTL_RD_CMD;
78         regdata |= CMD_NEW;
79
80         cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
81         wait_till_cmd_acked(cs5535au, 500);
82
83         timeout = 50;
84         do {
85                 val = cs_readl(cs5535au, ACC_CODEC_STATUS);
86                 if ((val & STS_NEW) && reg == (val >> 24))
87                         break;
88                 msleep(10);
89         } while (--timeout);
90         if (!timeout)
91                 snd_printk(KERN_ERR "Failure reading cs5535 codec\n");
92
93         return (unsigned short) val;
94 }
95
96 static void snd_cs5535audio_codec_write(cs5535audio_t *cs5535au,
97                                    unsigned short reg, unsigned short val)
98 {
99         unsigned int regdata;
100
101         regdata = ((unsigned int) reg) << 24;
102         regdata |= val;
103         regdata &= CMD_MASK;
104         regdata |= CMD_NEW;
105         regdata &= ACC_CODEC_CNTL_WR_CMD;
106
107         cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
108         wait_till_cmd_acked(cs5535au, 50);
109 }
110
111 static void snd_cs5535audio_ac97_codec_write(ac97_t *ac97,
112                                    unsigned short reg, unsigned short val)
113 {
114         cs5535audio_t *cs5535au = ac97->private_data;
115         snd_cs5535audio_codec_write(cs5535au, reg, val);
116 }
117
118 static unsigned short snd_cs5535audio_ac97_codec_read(ac97_t *ac97,
119                                             unsigned short reg)
120 {
121         cs5535audio_t *cs5535au = ac97->private_data;
122         return snd_cs5535audio_codec_read(cs5535au, reg);
123 }
124
125 static int snd_cs5535audio_mixer(cs5535audio_t *cs5535au)
126 {
127         snd_card_t *card = cs5535au->card;
128         ac97_bus_t *pbus;
129         ac97_template_t ac97;
130         int err;
131         static ac97_bus_ops_t ops = {
132                 .write = snd_cs5535audio_ac97_codec_write,
133                 .read = snd_cs5535audio_ac97_codec_read,
134         };
135
136         if ((err = snd_ac97_bus(card, 0, &ops, NULL, &pbus)) < 0)
137                 return err;
138
139         memset(&ac97, 0, sizeof(ac97));
140         ac97.scaps = AC97_SCAP_AUDIO|AC97_SCAP_SKIP_MODEM;
141         ac97.private_data = cs5535au;
142         ac97.pci = cs5535au->pci;
143
144         if ((err = snd_ac97_mixer(pbus, &ac97, &cs5535au->ac97)) < 0) {
145                 snd_printk(KERN_ERR "mixer failed\n");
146                 return err;
147         }
148
149         return 0;
150 }
151
152 static void process_bm0_irq(cs5535audio_t *cs5535au)
153 {
154         u8 bm_stat;
155         spin_lock(&cs5535au->reg_lock);
156         bm_stat = cs_readb(cs5535au, ACC_BM0_STATUS);
157         spin_unlock(&cs5535au->reg_lock);
158         if (bm_stat & EOP) {
159                 cs5535audio_dma_t *dma;
160                 dma = cs5535au->playback_substream->runtime->private_data;
161                 snd_pcm_period_elapsed(cs5535au->playback_substream);
162         } else {
163                 snd_printk(KERN_ERR "unexpected bm0 irq src, bm_stat=%x\n",
164                                         bm_stat);
165         }
166 }
167
168 static void process_bm1_irq(cs5535audio_t *cs5535au)
169 {
170         u8 bm_stat;
171         spin_lock(&cs5535au->reg_lock);
172         bm_stat = cs_readb(cs5535au, ACC_BM1_STATUS);
173         spin_unlock(&cs5535au->reg_lock);
174         if (bm_stat & EOP) {
175                 cs5535audio_dma_t *dma;
176                 dma = cs5535au->capture_substream->runtime->private_data;
177                 snd_pcm_period_elapsed(cs5535au->capture_substream);
178         }
179 }
180
181 static irqreturn_t snd_cs5535audio_interrupt(int irq, void *dev_id,
182                                                 struct pt_regs *regs)
183 {
184         u16 acc_irq_stat;
185         u8 bm_stat;
186         unsigned char count;
187         cs5535audio_t *cs5535au = dev_id;
188
189         if (cs5535au == NULL)
190                 return IRQ_NONE;
191
192         acc_irq_stat = cs_readw(cs5535au, ACC_IRQ_STATUS);
193
194         if (!acc_irq_stat)
195                 return IRQ_NONE;
196         for (count = 0; count < 10; count++) {
197                 if (acc_irq_stat & (1 << count)) {
198                         switch (count) {
199                         case IRQ_STS:
200                                 cs_readl(cs5535au, ACC_GPIO_STATUS);
201                                 break;
202                         case WU_IRQ_STS:
203                                 cs_readl(cs5535au, ACC_GPIO_STATUS);
204                                 break;
205                         case BM0_IRQ_STS:
206                                 process_bm0_irq(cs5535au);
207                                 break;
208                         case BM1_IRQ_STS:
209                                 process_bm1_irq(cs5535au);
210                                 break;
211                         case BM2_IRQ_STS:
212                                 bm_stat = cs_readb(cs5535au, ACC_BM2_STATUS);
213                                 break;
214                         case BM3_IRQ_STS:
215                                 bm_stat = cs_readb(cs5535au, ACC_BM3_STATUS);
216                                 break;
217                         case BM4_IRQ_STS:
218                                 bm_stat = cs_readb(cs5535au, ACC_BM4_STATUS);
219                                 break;
220                         case BM5_IRQ_STS:
221                                 bm_stat = cs_readb(cs5535au, ACC_BM5_STATUS);
222                                 break;
223                         case BM6_IRQ_STS:
224                                 bm_stat = cs_readb(cs5535au, ACC_BM6_STATUS);
225                                 break;
226                         case BM7_IRQ_STS:
227                                 bm_stat = cs_readb(cs5535au, ACC_BM7_STATUS);
228                                 break;
229                         default:
230                                 snd_printk(KERN_ERR "Unexpected irq src\n");
231                                 break;
232                         }
233                 }
234         }
235         return IRQ_HANDLED;
236 }
237
238 static int snd_cs5535audio_free(cs5535audio_t *cs5535au)
239 {
240         synchronize_irq(cs5535au->irq);
241         pci_set_power_state(cs5535au->pci, 3);
242
243         if (cs5535au->irq >= 0)
244                 free_irq(cs5535au->irq, cs5535au);
245
246         pci_release_regions(cs5535au->pci);
247         pci_disable_device(cs5535au->pci);
248         kfree(cs5535au);
249         return 0;
250 }
251
252 static int snd_cs5535audio_dev_free(snd_device_t *device)
253 {
254         cs5535audio_t *cs5535au = device->device_data;
255         return snd_cs5535audio_free(cs5535au);
256 }
257
258 static int __devinit snd_cs5535audio_create(snd_card_t *card,
259                                      struct pci_dev *pci,
260                                      cs5535audio_t **rcs5535au)
261 {
262         cs5535audio_t *cs5535au;
263
264         int err;
265         static snd_device_ops_t ops = {
266                 .dev_free =     snd_cs5535audio_dev_free,
267         };
268
269         *rcs5535au = NULL;
270         if ((err = pci_enable_device(pci)) < 0)
271                 return err;
272
273         if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0 ||
274                 pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0) {
275                 printk(KERN_WARNING "unable to get 32bit dma\n");
276                 err = -ENXIO;
277                 goto pcifail;
278         }
279
280         cs5535au = kzalloc(sizeof(*cs5535au), GFP_KERNEL);
281         if (cs5535au == NULL) {
282                 err = -ENOMEM;
283                 goto pcifail;
284         }
285
286         spin_lock_init(&cs5535au->reg_lock);
287         cs5535au->card = card;
288         cs5535au->pci = pci;
289         cs5535au->irq = -1;
290
291         if ((err = pci_request_regions(pci, "CS5535 Audio")) < 0) {
292                 kfree(cs5535au);
293                 goto pcifail;
294         }
295
296         cs5535au->port = pci_resource_start(pci, 0);
297
298         if (request_irq(pci->irq, snd_cs5535audio_interrupt,
299                 SA_INTERRUPT|SA_SHIRQ, "CS5535 Audio", cs5535au)) {
300                 snd_printk("unable to grab IRQ %d\n", pci->irq);
301                 err = -EBUSY;
302                 goto sndfail;
303         }
304
305         cs5535au->irq = pci->irq;
306         pci_set_master(pci);
307
308         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
309                                         cs5535au, &ops)) < 0)
310                 goto sndfail;
311
312         snd_card_set_dev(card, &pci->dev);
313
314         *rcs5535au = cs5535au;
315         return 0;
316
317 sndfail: /* leave the device alive, just kill the snd */
318         snd_cs5535audio_free(cs5535au);
319         return err;
320
321 pcifail:
322         pci_disable_device(pci);
323         return err;
324 }
325
326 static int __devinit snd_cs5535audio_probe(struct pci_dev *pci,
327                                         const struct pci_device_id *pci_id)
328 {
329         static int dev;
330         snd_card_t *card;
331         cs5535audio_t *cs5535au;
332         int err;
333
334         if (dev >= SNDRV_CARDS)
335                 return -ENODEV;
336         if (!enable[dev]) {
337                 dev++;
338                 return -ENOENT;
339         }
340
341         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
342         if (card == NULL)
343                 return -ENOMEM;
344
345         if ((err = snd_cs5535audio_create(card, pci, &cs5535au)) < 0)
346                 goto probefail_out;
347
348         if ((err = snd_cs5535audio_mixer(cs5535au)) < 0)
349                 goto probefail_out;
350
351         if ((err = snd_cs5535audio_pcm(cs5535au)) < 0)
352                 goto probefail_out;
353
354         strcpy(card->driver, DRIVER_NAME);
355
356         strcpy(card->shortname, "CS5535 Audio");
357         sprintf(card->longname, "%s %s at 0x%lx, irq %i",
358                 card->shortname, card->driver,
359                 cs5535au->port, cs5535au->irq);
360
361         if ((err = snd_card_register(card)) < 0)
362                 goto probefail_out;
363
364         pci_set_drvdata(pci, card);
365         dev++;
366         return 0;
367
368 probefail_out:
369         snd_card_free(card);
370         return err;
371 }
372
373 static void __devexit snd_cs5535audio_remove(struct pci_dev *pci)
374 {
375         snd_card_free(pci_get_drvdata(pci));
376         pci_set_drvdata(pci, NULL);
377 }
378
379 static struct pci_driver driver = {
380         .name = DRIVER_NAME,
381         .id_table = snd_cs5535audio_ids,
382         .probe = snd_cs5535audio_probe,
383         .remove = __devexit_p(snd_cs5535audio_remove),
384 };
385
386 static int __init alsa_card_cs5535audio_init(void)
387 {
388         return pci_module_init(&driver);
389 }
390
391 static void __exit alsa_card_cs5535audio_exit(void)
392 {
393         pci_unregister_driver(&driver);
394 }
395
396 module_init(alsa_card_cs5535audio_init)
397 module_exit(alsa_card_cs5535audio_exit)
398
399 MODULE_AUTHOR("Jaya Kumar");
400 MODULE_LICENSE("GPL");
401 MODULE_DESCRIPTION("CS5535 Audio");
402 MODULE_SUPPORTED_DEVICE("CS5535 Audio");