blob: 41f02f05dfdc8dd8d4b1f3bececd006011ee5bb5 [file] [log] [blame]
Jaya Kumar9b4ffa42005-11-17 10:12:23 +01001/*
Jaya Kumar9ac25592006-04-28 14:34:49 +02002 * Driver for audio on multifunction CS5535/6 companion device
Jaya Kumar9b4ffa42005-11-17 10:12:23 +01003 * 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
Clemens Ladischac09a922005-11-20 13:58:28 +010024#include <sound/driver.h>
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010025#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>
31#include <asm/io.h>
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010032#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
Jaya Kumar9ac25592006-04-28 14:34:49 +020043static char *ac97_quirk;
44module_param(ac97_quirk, charp, 0444);
45MODULE_PARM_DESC(ac97_quirk, "AC'97 board specific workarounds.");
46
47static struct ac97_quirk ac97_quirks[] __devinitdata = {
48#if 0 /* Not yet confirmed if all 5536 boards are HP only */
49 {
50 .subvendor = PCI_VENDOR_ID_AMD,
51 .subdevice = PCI_DEVICE_ID_AMD_CS5536_AUDIO,
52 .name = "AMD RDK",
53 .type = AC97_TUNE_HP_ONLY
54 },
55#endif
56 {}
57};
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010058
59static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
60static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
61static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
62
Henrik Kretzschmar396c9b92006-04-24 15:59:04 +020063static struct pci_device_id snd_cs5535audio_ids[] __devinitdata = {
Jaya Kumar9ac25592006-04-28 14:34:49 +020064 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_AUDIO) },
65 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_AUDIO) },
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010066 {}
67};
68
69MODULE_DEVICE_TABLE(pci, snd_cs5535audio_ids);
70
Takashi Iwai66f8df62005-11-17 14:56:21 +010071static void wait_till_cmd_acked(struct cs5535audio *cs5535au, unsigned long timeout)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010072{
Takashi Iwai3e873172005-11-17 10:15:37 +010073 unsigned int tmp;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010074 do {
75 tmp = cs_readl(cs5535au, ACC_CODEC_CNTL);
76 if (!(tmp & CMD_NEW))
77 break;
David Vrabel10375932006-03-03 18:01:57 +010078 udelay(1);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010079 } while (--timeout);
80 if (!timeout)
81 snd_printk(KERN_ERR "Failure writing to cs5535 codec\n");
82}
83
Takashi Iwai66f8df62005-11-17 14:56:21 +010084static unsigned short snd_cs5535audio_codec_read(struct cs5535audio *cs5535au,
85 unsigned short reg)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010086{
Takashi Iwai3e873172005-11-17 10:15:37 +010087 unsigned int regdata;
Takashi Iwai66f8df62005-11-17 14:56:21 +010088 unsigned int timeout;
Takashi Iwai3e873172005-11-17 10:15:37 +010089 unsigned int val;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010090
Takashi Iwai3e873172005-11-17 10:15:37 +010091 regdata = ((unsigned int) reg) << 24;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010092 regdata |= ACC_CODEC_CNTL_RD_CMD;
93 regdata |= CMD_NEW;
94
95 cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
David Vrabel10375932006-03-03 18:01:57 +010096 wait_till_cmd_acked(cs5535au, 50);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010097
98 timeout = 50;
99 do {
100 val = cs_readl(cs5535au, ACC_CODEC_STATUS);
Takashi Iwai3e873172005-11-17 10:15:37 +0100101 if ((val & STS_NEW) && reg == (val >> 24))
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100102 break;
David Vrabel10375932006-03-03 18:01:57 +0100103 udelay(1);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100104 } while (--timeout);
105 if (!timeout)
106 snd_printk(KERN_ERR "Failure reading cs5535 codec\n");
107
Takashi Iwai3e873172005-11-17 10:15:37 +0100108 return (unsigned short) val;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100109}
110
Takashi Iwai66f8df62005-11-17 14:56:21 +0100111static void snd_cs5535audio_codec_write(struct cs5535audio *cs5535au,
112 unsigned short reg, unsigned short val)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100113{
Takashi Iwai3e873172005-11-17 10:15:37 +0100114 unsigned int regdata;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100115
Takashi Iwai3e873172005-11-17 10:15:37 +0100116 regdata = ((unsigned int) reg) << 24;
117 regdata |= val;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100118 regdata &= CMD_MASK;
119 regdata |= CMD_NEW;
120 regdata &= ACC_CODEC_CNTL_WR_CMD;
121
122 cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
123 wait_till_cmd_acked(cs5535au, 50);
124}
125
Takashi Iwai66f8df62005-11-17 14:56:21 +0100126static void snd_cs5535audio_ac97_codec_write(struct snd_ac97 *ac97,
127 unsigned short reg, unsigned short val)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100128{
Takashi Iwai66f8df62005-11-17 14:56:21 +0100129 struct cs5535audio *cs5535au = ac97->private_data;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100130 snd_cs5535audio_codec_write(cs5535au, reg, val);
131}
132
Takashi Iwai66f8df62005-11-17 14:56:21 +0100133static unsigned short snd_cs5535audio_ac97_codec_read(struct snd_ac97 *ac97,
134 unsigned short reg)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100135{
Takashi Iwai66f8df62005-11-17 14:56:21 +0100136 struct cs5535audio *cs5535au = ac97->private_data;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100137 return snd_cs5535audio_codec_read(cs5535au, reg);
138}
139
Takashi Iwai66f8df62005-11-17 14:56:21 +0100140static int snd_cs5535audio_mixer(struct cs5535audio *cs5535au)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100141{
Takashi Iwai66f8df62005-11-17 14:56:21 +0100142 struct snd_card *card = cs5535au->card;
143 struct snd_ac97_bus *pbus;
144 struct snd_ac97_template ac97;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100145 int err;
Takashi Iwai66f8df62005-11-17 14:56:21 +0100146 static struct snd_ac97_bus_ops ops = {
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100147 .write = snd_cs5535audio_ac97_codec_write,
148 .read = snd_cs5535audio_ac97_codec_read,
149 };
150
151 if ((err = snd_ac97_bus(card, 0, &ops, NULL, &pbus)) < 0)
152 return err;
153
154 memset(&ac97, 0, sizeof(ac97));
155 ac97.scaps = AC97_SCAP_AUDIO|AC97_SCAP_SKIP_MODEM;
156 ac97.private_data = cs5535au;
157 ac97.pci = cs5535au->pci;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100158
159 if ((err = snd_ac97_mixer(pbus, &ac97, &cs5535au->ac97)) < 0) {
Takashi Iwai3e873172005-11-17 10:15:37 +0100160 snd_printk(KERN_ERR "mixer failed\n");
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100161 return err;
162 }
163
Jaya Kumar9ac25592006-04-28 14:34:49 +0200164 snd_ac97_tune_hardware(cs5535au->ac97, ac97_quirks, ac97_quirk);
165
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100166 return 0;
167}
168
Takashi Iwai66f8df62005-11-17 14:56:21 +0100169static void process_bm0_irq(struct cs5535audio *cs5535au)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100170{
171 u8 bm_stat;
172 spin_lock(&cs5535au->reg_lock);
173 bm_stat = cs_readb(cs5535au, ACC_BM0_STATUS);
174 spin_unlock(&cs5535au->reg_lock);
175 if (bm_stat & EOP) {
Takashi Iwai66f8df62005-11-17 14:56:21 +0100176 struct cs5535audio_dma *dma;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100177 dma = cs5535au->playback_substream->runtime->private_data;
178 snd_pcm_period_elapsed(cs5535au->playback_substream);
179 } else {
180 snd_printk(KERN_ERR "unexpected bm0 irq src, bm_stat=%x\n",
181 bm_stat);
182 }
183}
184
Takashi Iwai66f8df62005-11-17 14:56:21 +0100185static void process_bm1_irq(struct cs5535audio *cs5535au)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100186{
187 u8 bm_stat;
188 spin_lock(&cs5535au->reg_lock);
189 bm_stat = cs_readb(cs5535au, ACC_BM1_STATUS);
190 spin_unlock(&cs5535au->reg_lock);
191 if (bm_stat & EOP) {
Takashi Iwai66f8df62005-11-17 14:56:21 +0100192 struct cs5535audio_dma *dma;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100193 dma = cs5535au->capture_substream->runtime->private_data;
194 snd_pcm_period_elapsed(cs5535au->capture_substream);
195 }
196}
197
198static irqreturn_t snd_cs5535audio_interrupt(int irq, void *dev_id,
Takashi Iwai66f8df62005-11-17 14:56:21 +0100199 struct pt_regs *regs)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100200{
201 u16 acc_irq_stat;
202 u8 bm_stat;
203 unsigned char count;
Takashi Iwai66f8df62005-11-17 14:56:21 +0100204 struct cs5535audio *cs5535au = dev_id;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100205
206 if (cs5535au == NULL)
207 return IRQ_NONE;
208
209 acc_irq_stat = cs_readw(cs5535au, ACC_IRQ_STATUS);
210
211 if (!acc_irq_stat)
212 return IRQ_NONE;
Takashi Iwai3e873172005-11-17 10:15:37 +0100213 for (count = 0; count < 10; count++) {
214 if (acc_irq_stat & (1 << count)) {
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100215 switch (count) {
216 case IRQ_STS:
217 cs_readl(cs5535au, ACC_GPIO_STATUS);
218 break;
219 case WU_IRQ_STS:
220 cs_readl(cs5535au, ACC_GPIO_STATUS);
221 break;
222 case BM0_IRQ_STS:
223 process_bm0_irq(cs5535au);
224 break;
225 case BM1_IRQ_STS:
226 process_bm1_irq(cs5535au);
227 break;
228 case BM2_IRQ_STS:
229 bm_stat = cs_readb(cs5535au, ACC_BM2_STATUS);
230 break;
231 case BM3_IRQ_STS:
232 bm_stat = cs_readb(cs5535au, ACC_BM3_STATUS);
233 break;
234 case BM4_IRQ_STS:
235 bm_stat = cs_readb(cs5535au, ACC_BM4_STATUS);
236 break;
237 case BM5_IRQ_STS:
238 bm_stat = cs_readb(cs5535au, ACC_BM5_STATUS);
239 break;
240 case BM6_IRQ_STS:
241 bm_stat = cs_readb(cs5535au, ACC_BM6_STATUS);
242 break;
243 case BM7_IRQ_STS:
244 bm_stat = cs_readb(cs5535au, ACC_BM7_STATUS);
245 break;
246 default:
247 snd_printk(KERN_ERR "Unexpected irq src\n");
248 break;
249 }
250 }
251 }
252 return IRQ_HANDLED;
253}
254
Takashi Iwai66f8df62005-11-17 14:56:21 +0100255static int snd_cs5535audio_free(struct cs5535audio *cs5535au)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100256{
257 synchronize_irq(cs5535au->irq);
258 pci_set_power_state(cs5535au->pci, 3);
259
260 if (cs5535au->irq >= 0)
261 free_irq(cs5535au->irq, cs5535au);
262
263 pci_release_regions(cs5535au->pci);
264 pci_disable_device(cs5535au->pci);
265 kfree(cs5535au);
266 return 0;
267}
268
Takashi Iwai66f8df62005-11-17 14:56:21 +0100269static int snd_cs5535audio_dev_free(struct snd_device *device)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100270{
Takashi Iwai66f8df62005-11-17 14:56:21 +0100271 struct cs5535audio *cs5535au = device->device_data;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100272 return snd_cs5535audio_free(cs5535au);
273}
274
Takashi Iwai66f8df62005-11-17 14:56:21 +0100275static int __devinit snd_cs5535audio_create(struct snd_card *card,
276 struct pci_dev *pci,
277 struct cs5535audio **rcs5535au)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100278{
Takashi Iwai66f8df62005-11-17 14:56:21 +0100279 struct cs5535audio *cs5535au;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100280
281 int err;
Takashi Iwai66f8df62005-11-17 14:56:21 +0100282 static struct snd_device_ops ops = {
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100283 .dev_free = snd_cs5535audio_dev_free,
284 };
285
286 *rcs5535au = NULL;
287 if ((err = pci_enable_device(pci)) < 0)
288 return err;
289
290 if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0 ||
Takashi Iwai66f8df62005-11-17 14:56:21 +0100291 pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0) {
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100292 printk(KERN_WARNING "unable to get 32bit dma\n");
293 err = -ENXIO;
294 goto pcifail;
295 }
296
297 cs5535au = kzalloc(sizeof(*cs5535au), GFP_KERNEL);
298 if (cs5535au == NULL) {
299 err = -ENOMEM;
300 goto pcifail;
301 }
302
303 spin_lock_init(&cs5535au->reg_lock);
304 cs5535au->card = card;
305 cs5535au->pci = pci;
306 cs5535au->irq = -1;
307
308 if ((err = pci_request_regions(pci, "CS5535 Audio")) < 0) {
309 kfree(cs5535au);
310 goto pcifail;
311 }
312
313 cs5535au->port = pci_resource_start(pci, 0);
314
315 if (request_irq(pci->irq, snd_cs5535audio_interrupt,
Takashi Iwai66f8df62005-11-17 14:56:21 +0100316 SA_INTERRUPT|SA_SHIRQ, "CS5535 Audio", cs5535au)) {
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100317 snd_printk("unable to grab IRQ %d\n", pci->irq);
318 err = -EBUSY;
319 goto sndfail;
320 }
321
322 cs5535au->irq = pci->irq;
323 pci_set_master(pci);
324
325 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
Takashi Iwai66f8df62005-11-17 14:56:21 +0100326 cs5535au, &ops)) < 0)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100327 goto sndfail;
328
329 snd_card_set_dev(card, &pci->dev);
330
331 *rcs5535au = cs5535au;
332 return 0;
333
334sndfail: /* leave the device alive, just kill the snd */
335 snd_cs5535audio_free(cs5535au);
336 return err;
337
338pcifail:
339 pci_disable_device(pci);
340 return err;
341}
342
343static int __devinit snd_cs5535audio_probe(struct pci_dev *pci,
Takashi Iwai66f8df62005-11-17 14:56:21 +0100344 const struct pci_device_id *pci_id)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100345{
346 static int dev;
Takashi Iwai66f8df62005-11-17 14:56:21 +0100347 struct snd_card *card;
348 struct cs5535audio *cs5535au;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100349 int err;
350
351 if (dev >= SNDRV_CARDS)
352 return -ENODEV;
353 if (!enable[dev]) {
354 dev++;
355 return -ENOENT;
356 }
357
358 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
359 if (card == NULL)
360 return -ENOMEM;
361
362 if ((err = snd_cs5535audio_create(card, pci, &cs5535au)) < 0)
363 goto probefail_out;
364
Jaya Kumar9ac25592006-04-28 14:34:49 +0200365 card->private_data = cs5535au;
366
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100367 if ((err = snd_cs5535audio_mixer(cs5535au)) < 0)
368 goto probefail_out;
369
370 if ((err = snd_cs5535audio_pcm(cs5535au)) < 0)
371 goto probefail_out;
372
373 strcpy(card->driver, DRIVER_NAME);
374
375 strcpy(card->shortname, "CS5535 Audio");
376 sprintf(card->longname, "%s %s at 0x%lx, irq %i",
377 card->shortname, card->driver,
378 cs5535au->port, cs5535au->irq);
379
380 if ((err = snd_card_register(card)) < 0)
381 goto probefail_out;
382
383 pci_set_drvdata(pci, card);
384 dev++;
385 return 0;
386
387probefail_out:
388 snd_card_free(card);
389 return err;
390}
391
392static void __devexit snd_cs5535audio_remove(struct pci_dev *pci)
393{
394 snd_card_free(pci_get_drvdata(pci));
395 pci_set_drvdata(pci, NULL);
396}
397
398static struct pci_driver driver = {
399 .name = DRIVER_NAME,
400 .id_table = snd_cs5535audio_ids,
401 .probe = snd_cs5535audio_probe,
402 .remove = __devexit_p(snd_cs5535audio_remove),
Jaya Kumar9ac25592006-04-28 14:34:49 +0200403#ifdef CONFIG_PM
404 .suspend = snd_cs5535audio_suspend,
405 .resume = snd_cs5535audio_resume,
406#endif
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100407};
408
409static int __init alsa_card_cs5535audio_init(void)
410{
Ben Gardnere3291132006-01-09 20:51:29 -0800411 return pci_register_driver(&driver);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100412}
413
414static void __exit alsa_card_cs5535audio_exit(void)
415{
416 pci_unregister_driver(&driver);
417}
418
419module_init(alsa_card_cs5535audio_init)
420module_exit(alsa_card_cs5535audio_exit)
421
422MODULE_AUTHOR("Jaya Kumar");
423MODULE_LICENSE("GPL");
424MODULE_DESCRIPTION("CS5535 Audio");
425MODULE_SUPPORTED_DEVICE("CS5535 Audio");