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