2 * Driver for audio on multifunction CS5535 companion device
3 * Copyright (C) Jaya Kumar
5 * Based on Jaroslav Kysela and Takashi Iwai's examples.
6 * This work was sponsored by CIS(M) Sdn Bhd.
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/delay.h>
26 #include <linux/interrupt.h>
27 #include <linux/init.h>
28 #include <linux/pci.h>
29 #include <linux/slab.h>
30 #include <linux/moduleparam.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"
41 #define DRIVER_NAME "cs5535audio"
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;
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, },
54 MODULE_DEVICE_TABLE(pci, snd_cs5535audio_ids);
56 static void wait_till_cmd_acked(struct cs5535audio *cs5535au, unsigned long timeout)
60 tmp = cs_readl(cs5535au, ACC_CODEC_CNTL);
66 snd_printk(KERN_ERR "Failure writing to cs5535 codec\n");
69 static unsigned short snd_cs5535audio_codec_read(struct cs5535audio *cs5535au,
76 regdata = ((unsigned int) reg) << 24;
77 regdata |= ACC_CODEC_CNTL_RD_CMD;
80 cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
81 wait_till_cmd_acked(cs5535au, 500);
85 val = cs_readl(cs5535au, ACC_CODEC_STATUS);
86 if ((val & STS_NEW) && reg == (val >> 24))
91 snd_printk(KERN_ERR "Failure reading cs5535 codec\n");
93 return (unsigned short) val;
96 static void snd_cs5535audio_codec_write(struct cs5535audio *cs5535au,
97 unsigned short reg, unsigned short val)
101 regdata = ((unsigned int) reg) << 24;
105 regdata &= ACC_CODEC_CNTL_WR_CMD;
107 cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
108 wait_till_cmd_acked(cs5535au, 50);
111 static void snd_cs5535audio_ac97_codec_write(struct snd_ac97 *ac97,
112 unsigned short reg, unsigned short val)
114 struct cs5535audio *cs5535au = ac97->private_data;
115 snd_cs5535audio_codec_write(cs5535au, reg, val);
118 static unsigned short snd_cs5535audio_ac97_codec_read(struct snd_ac97 *ac97,
121 struct cs5535audio *cs5535au = ac97->private_data;
122 return snd_cs5535audio_codec_read(cs5535au, reg);
125 static int snd_cs5535audio_mixer(struct cs5535audio *cs5535au)
127 struct snd_card *card = cs5535au->card;
128 struct snd_ac97_bus *pbus;
129 struct snd_ac97_template ac97;
131 static struct snd_ac97_bus_ops ops = {
132 .write = snd_cs5535audio_ac97_codec_write,
133 .read = snd_cs5535audio_ac97_codec_read,
136 if ((err = snd_ac97_bus(card, 0, &ops, NULL, &pbus)) < 0)
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;
144 if ((err = snd_ac97_mixer(pbus, &ac97, &cs5535au->ac97)) < 0) {
145 snd_printk(KERN_ERR "mixer failed\n");
152 static void process_bm0_irq(struct cs5535audio *cs5535au)
155 spin_lock(&cs5535au->reg_lock);
156 bm_stat = cs_readb(cs5535au, ACC_BM0_STATUS);
157 spin_unlock(&cs5535au->reg_lock);
159 struct cs5535audio_dma *dma;
160 dma = cs5535au->playback_substream->runtime->private_data;
161 snd_pcm_period_elapsed(cs5535au->playback_substream);
163 snd_printk(KERN_ERR "unexpected bm0 irq src, bm_stat=%x\n",
168 static void process_bm1_irq(struct cs5535audio *cs5535au)
171 spin_lock(&cs5535au->reg_lock);
172 bm_stat = cs_readb(cs5535au, ACC_BM1_STATUS);
173 spin_unlock(&cs5535au->reg_lock);
175 struct cs5535audio_dma *dma;
176 dma = cs5535au->capture_substream->runtime->private_data;
177 snd_pcm_period_elapsed(cs5535au->capture_substream);
181 static irqreturn_t snd_cs5535audio_interrupt(int irq, void *dev_id,
182 struct pt_regs *regs)
187 struct cs5535audio *cs5535au = dev_id;
189 if (cs5535au == NULL)
192 acc_irq_stat = cs_readw(cs5535au, ACC_IRQ_STATUS);
196 for (count = 0; count < 10; count++) {
197 if (acc_irq_stat & (1 << count)) {
200 cs_readl(cs5535au, ACC_GPIO_STATUS);
203 cs_readl(cs5535au, ACC_GPIO_STATUS);
206 process_bm0_irq(cs5535au);
209 process_bm1_irq(cs5535au);
212 bm_stat = cs_readb(cs5535au, ACC_BM2_STATUS);
215 bm_stat = cs_readb(cs5535au, ACC_BM3_STATUS);
218 bm_stat = cs_readb(cs5535au, ACC_BM4_STATUS);
221 bm_stat = cs_readb(cs5535au, ACC_BM5_STATUS);
224 bm_stat = cs_readb(cs5535au, ACC_BM6_STATUS);
227 bm_stat = cs_readb(cs5535au, ACC_BM7_STATUS);
230 snd_printk(KERN_ERR "Unexpected irq src\n");
238 static int snd_cs5535audio_free(struct cs5535audio *cs5535au)
240 synchronize_irq(cs5535au->irq);
241 pci_set_power_state(cs5535au->pci, 3);
243 if (cs5535au->irq >= 0)
244 free_irq(cs5535au->irq, cs5535au);
246 pci_release_regions(cs5535au->pci);
247 pci_disable_device(cs5535au->pci);
252 static int snd_cs5535audio_dev_free(struct snd_device *device)
254 struct cs5535audio *cs5535au = device->device_data;
255 return snd_cs5535audio_free(cs5535au);
258 static int __devinit snd_cs5535audio_create(struct snd_card *card,
260 struct cs5535audio **rcs5535au)
262 struct cs5535audio *cs5535au;
265 static struct snd_device_ops ops = {
266 .dev_free = snd_cs5535audio_dev_free,
270 if ((err = pci_enable_device(pci)) < 0)
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");
280 cs5535au = kzalloc(sizeof(*cs5535au), GFP_KERNEL);
281 if (cs5535au == NULL) {
286 spin_lock_init(&cs5535au->reg_lock);
287 cs5535au->card = card;
291 if ((err = pci_request_regions(pci, "CS5535 Audio")) < 0) {
296 cs5535au->port = pci_resource_start(pci, 0);
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);
305 cs5535au->irq = pci->irq;
308 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
309 cs5535au, &ops)) < 0)
312 snd_card_set_dev(card, &pci->dev);
314 *rcs5535au = cs5535au;
317 sndfail: /* leave the device alive, just kill the snd */
318 snd_cs5535audio_free(cs5535au);
322 pci_disable_device(pci);
326 static int __devinit snd_cs5535audio_probe(struct pci_dev *pci,
327 const struct pci_device_id *pci_id)
330 struct snd_card *card;
331 struct cs5535audio *cs5535au;
334 if (dev >= SNDRV_CARDS)
341 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
345 if ((err = snd_cs5535audio_create(card, pci, &cs5535au)) < 0)
348 if ((err = snd_cs5535audio_mixer(cs5535au)) < 0)
351 if ((err = snd_cs5535audio_pcm(cs5535au)) < 0)
354 strcpy(card->driver, DRIVER_NAME);
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);
361 if ((err = snd_card_register(card)) < 0)
364 pci_set_drvdata(pci, card);
373 static void __devexit snd_cs5535audio_remove(struct pci_dev *pci)
375 snd_card_free(pci_get_drvdata(pci));
376 pci_set_drvdata(pci, NULL);
379 static struct pci_driver driver = {
381 .id_table = snd_cs5535audio_ids,
382 .probe = snd_cs5535audio_probe,
383 .remove = __devexit_p(snd_cs5535audio_remove),
386 static int __init alsa_card_cs5535audio_init(void)
388 return pci_module_init(&driver);
391 static void __exit alsa_card_cs5535audio_exit(void)
393 pci_unregister_driver(&driver);
396 module_init(alsa_card_cs5535audio_init)
397 module_exit(alsa_card_cs5535audio_exit)
399 MODULE_AUTHOR("Jaya Kumar");
400 MODULE_LICENSE("GPL");
401 MODULE_DESCRIPTION("CS5535 Audio");
402 MODULE_SUPPORTED_DEVICE("CS5535 Audio");