blob: 8f46190f24addb37424ed5b95102bc1b95959586 [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
Jaya Kumar77389b42006-05-19 12:04:22 +020059static int index = SNDRV_DEFAULT_IDX1;
60static char *id = SNDRV_DEFAULT_STR1;
61/* for backward compatibility */
62static int enable;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010063
Jaya Kumar77389b42006-05-19 12:04:22 +020064module_param(index, int, 0444);
Takashi Iwai1dbfd8c2006-05-02 18:31:31 +020065MODULE_PARM_DESC(index, "Index value for " DRIVER_NAME);
Jaya Kumar77389b42006-05-19 12:04:22 +020066module_param(id, charp, 0444);
Takashi Iwai1dbfd8c2006-05-02 18:31:31 +020067MODULE_PARM_DESC(id, "ID string for " DRIVER_NAME);
Jaya Kumar77389b42006-05-19 12:04:22 +020068module_param(enable, bool, 0444);
69MODULE_PARM_DESC(enable, "Enable for " DRIVER_NAME);
Takashi Iwai1dbfd8c2006-05-02 18:31:31 +020070
Henrik Kretzschmar396c9b92006-04-24 15:59:04 +020071static struct pci_device_id snd_cs5535audio_ids[] __devinitdata = {
Jaya Kumar9ac25592006-04-28 14:34:49 +020072 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_AUDIO) },
73 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_AUDIO) },
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010074 {}
75};
76
77MODULE_DEVICE_TABLE(pci, snd_cs5535audio_ids);
78
Takashi Iwai66f8df62005-11-17 14:56:21 +010079static void wait_till_cmd_acked(struct cs5535audio *cs5535au, unsigned long timeout)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010080{
Takashi Iwai3e873172005-11-17 10:15:37 +010081 unsigned int tmp;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010082 do {
83 tmp = cs_readl(cs5535au, ACC_CODEC_CNTL);
84 if (!(tmp & CMD_NEW))
85 break;
David Vrabel10375932006-03-03 18:01:57 +010086 udelay(1);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010087 } while (--timeout);
88 if (!timeout)
89 snd_printk(KERN_ERR "Failure writing to cs5535 codec\n");
90}
91
Takashi Iwai66f8df62005-11-17 14:56:21 +010092static unsigned short snd_cs5535audio_codec_read(struct cs5535audio *cs5535au,
93 unsigned short reg)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010094{
Takashi Iwai3e873172005-11-17 10:15:37 +010095 unsigned int regdata;
Takashi Iwai66f8df62005-11-17 14:56:21 +010096 unsigned int timeout;
Takashi Iwai3e873172005-11-17 10:15:37 +010097 unsigned int val;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010098
Takashi Iwai3e873172005-11-17 10:15:37 +010099 regdata = ((unsigned int) reg) << 24;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100100 regdata |= ACC_CODEC_CNTL_RD_CMD;
101 regdata |= CMD_NEW;
102
103 cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
David Vrabel10375932006-03-03 18:01:57 +0100104 wait_till_cmd_acked(cs5535au, 50);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100105
106 timeout = 50;
107 do {
108 val = cs_readl(cs5535au, ACC_CODEC_STATUS);
Takashi Iwai3e873172005-11-17 10:15:37 +0100109 if ((val & STS_NEW) && reg == (val >> 24))
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100110 break;
David Vrabel10375932006-03-03 18:01:57 +0100111 udelay(1);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100112 } while (--timeout);
113 if (!timeout)
114 snd_printk(KERN_ERR "Failure reading cs5535 codec\n");
115
Takashi Iwai3e873172005-11-17 10:15:37 +0100116 return (unsigned short) val;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100117}
118
Takashi Iwai66f8df62005-11-17 14:56:21 +0100119static void snd_cs5535audio_codec_write(struct cs5535audio *cs5535au,
120 unsigned short reg, unsigned short val)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100121{
Takashi Iwai3e873172005-11-17 10:15:37 +0100122 unsigned int regdata;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100123
Takashi Iwai3e873172005-11-17 10:15:37 +0100124 regdata = ((unsigned int) reg) << 24;
125 regdata |= val;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100126 regdata &= CMD_MASK;
127 regdata |= CMD_NEW;
128 regdata &= ACC_CODEC_CNTL_WR_CMD;
129
130 cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
131 wait_till_cmd_acked(cs5535au, 50);
132}
133
Takashi Iwai66f8df62005-11-17 14:56:21 +0100134static void snd_cs5535audio_ac97_codec_write(struct snd_ac97 *ac97,
135 unsigned short reg, unsigned short val)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100136{
Takashi Iwai66f8df62005-11-17 14:56:21 +0100137 struct cs5535audio *cs5535au = ac97->private_data;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100138 snd_cs5535audio_codec_write(cs5535au, reg, val);
139}
140
Takashi Iwai66f8df62005-11-17 14:56:21 +0100141static unsigned short snd_cs5535audio_ac97_codec_read(struct snd_ac97 *ac97,
142 unsigned short reg)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100143{
Takashi Iwai66f8df62005-11-17 14:56:21 +0100144 struct cs5535audio *cs5535au = ac97->private_data;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100145 return snd_cs5535audio_codec_read(cs5535au, reg);
146}
147
Takashi Iwai66f8df62005-11-17 14:56:21 +0100148static int snd_cs5535audio_mixer(struct cs5535audio *cs5535au)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100149{
Takashi Iwai66f8df62005-11-17 14:56:21 +0100150 struct snd_card *card = cs5535au->card;
151 struct snd_ac97_bus *pbus;
152 struct snd_ac97_template ac97;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100153 int err;
Takashi Iwai66f8df62005-11-17 14:56:21 +0100154 static struct snd_ac97_bus_ops ops = {
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100155 .write = snd_cs5535audio_ac97_codec_write,
156 .read = snd_cs5535audio_ac97_codec_read,
157 };
158
159 if ((err = snd_ac97_bus(card, 0, &ops, NULL, &pbus)) < 0)
160 return err;
161
162 memset(&ac97, 0, sizeof(ac97));
163 ac97.scaps = AC97_SCAP_AUDIO|AC97_SCAP_SKIP_MODEM;
164 ac97.private_data = cs5535au;
165 ac97.pci = cs5535au->pci;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100166
167 if ((err = snd_ac97_mixer(pbus, &ac97, &cs5535au->ac97)) < 0) {
Takashi Iwai3e873172005-11-17 10:15:37 +0100168 snd_printk(KERN_ERR "mixer failed\n");
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100169 return err;
170 }
171
Jaya Kumar9ac25592006-04-28 14:34:49 +0200172 snd_ac97_tune_hardware(cs5535au->ac97, ac97_quirks, ac97_quirk);
173
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100174 return 0;
175}
176
Takashi Iwai66f8df62005-11-17 14:56:21 +0100177static void process_bm0_irq(struct cs5535audio *cs5535au)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100178{
179 u8 bm_stat;
180 spin_lock(&cs5535au->reg_lock);
181 bm_stat = cs_readb(cs5535au, ACC_BM0_STATUS);
182 spin_unlock(&cs5535au->reg_lock);
183 if (bm_stat & EOP) {
Takashi Iwai66f8df62005-11-17 14:56:21 +0100184 struct cs5535audio_dma *dma;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100185 dma = cs5535au->playback_substream->runtime->private_data;
186 snd_pcm_period_elapsed(cs5535au->playback_substream);
187 } else {
188 snd_printk(KERN_ERR "unexpected bm0 irq src, bm_stat=%x\n",
189 bm_stat);
190 }
191}
192
Takashi Iwai66f8df62005-11-17 14:56:21 +0100193static void process_bm1_irq(struct cs5535audio *cs5535au)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100194{
195 u8 bm_stat;
196 spin_lock(&cs5535au->reg_lock);
197 bm_stat = cs_readb(cs5535au, ACC_BM1_STATUS);
198 spin_unlock(&cs5535au->reg_lock);
199 if (bm_stat & EOP) {
Takashi Iwai66f8df62005-11-17 14:56:21 +0100200 struct cs5535audio_dma *dma;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100201 dma = cs5535au->capture_substream->runtime->private_data;
202 snd_pcm_period_elapsed(cs5535au->capture_substream);
203 }
204}
205
206static irqreturn_t snd_cs5535audio_interrupt(int irq, void *dev_id,
Takashi Iwai66f8df62005-11-17 14:56:21 +0100207 struct pt_regs *regs)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100208{
209 u16 acc_irq_stat;
210 u8 bm_stat;
211 unsigned char count;
Takashi Iwai66f8df62005-11-17 14:56:21 +0100212 struct cs5535audio *cs5535au = dev_id;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100213
214 if (cs5535au == NULL)
215 return IRQ_NONE;
216
217 acc_irq_stat = cs_readw(cs5535au, ACC_IRQ_STATUS);
218
219 if (!acc_irq_stat)
220 return IRQ_NONE;
Takashi Iwai3e873172005-11-17 10:15:37 +0100221 for (count = 0; count < 10; count++) {
222 if (acc_irq_stat & (1 << count)) {
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100223 switch (count) {
224 case IRQ_STS:
225 cs_readl(cs5535au, ACC_GPIO_STATUS);
226 break;
227 case WU_IRQ_STS:
228 cs_readl(cs5535au, ACC_GPIO_STATUS);
229 break;
230 case BM0_IRQ_STS:
231 process_bm0_irq(cs5535au);
232 break;
233 case BM1_IRQ_STS:
234 process_bm1_irq(cs5535au);
235 break;
236 case BM2_IRQ_STS:
237 bm_stat = cs_readb(cs5535au, ACC_BM2_STATUS);
238 break;
239 case BM3_IRQ_STS:
240 bm_stat = cs_readb(cs5535au, ACC_BM3_STATUS);
241 break;
242 case BM4_IRQ_STS:
243 bm_stat = cs_readb(cs5535au, ACC_BM4_STATUS);
244 break;
245 case BM5_IRQ_STS:
246 bm_stat = cs_readb(cs5535au, ACC_BM5_STATUS);
247 break;
248 case BM6_IRQ_STS:
249 bm_stat = cs_readb(cs5535au, ACC_BM6_STATUS);
250 break;
251 case BM7_IRQ_STS:
252 bm_stat = cs_readb(cs5535au, ACC_BM7_STATUS);
253 break;
254 default:
255 snd_printk(KERN_ERR "Unexpected irq src\n");
256 break;
257 }
258 }
259 }
260 return IRQ_HANDLED;
261}
262
Takashi Iwai66f8df62005-11-17 14:56:21 +0100263static int snd_cs5535audio_free(struct cs5535audio *cs5535au)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100264{
265 synchronize_irq(cs5535au->irq);
266 pci_set_power_state(cs5535au->pci, 3);
267
268 if (cs5535au->irq >= 0)
269 free_irq(cs5535au->irq, cs5535au);
270
271 pci_release_regions(cs5535au->pci);
272 pci_disable_device(cs5535au->pci);
273 kfree(cs5535au);
274 return 0;
275}
276
Takashi Iwai66f8df62005-11-17 14:56:21 +0100277static int snd_cs5535audio_dev_free(struct snd_device *device)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100278{
Takashi Iwai66f8df62005-11-17 14:56:21 +0100279 struct cs5535audio *cs5535au = device->device_data;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100280 return snd_cs5535audio_free(cs5535au);
281}
282
Takashi Iwai66f8df62005-11-17 14:56:21 +0100283static int __devinit snd_cs5535audio_create(struct snd_card *card,
284 struct pci_dev *pci,
285 struct cs5535audio **rcs5535au)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100286{
Takashi Iwai66f8df62005-11-17 14:56:21 +0100287 struct cs5535audio *cs5535au;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100288
289 int err;
Takashi Iwai66f8df62005-11-17 14:56:21 +0100290 static struct snd_device_ops ops = {
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100291 .dev_free = snd_cs5535audio_dev_free,
292 };
293
294 *rcs5535au = NULL;
295 if ((err = pci_enable_device(pci)) < 0)
296 return err;
297
298 if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0 ||
Takashi Iwai66f8df62005-11-17 14:56:21 +0100299 pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0) {
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100300 printk(KERN_WARNING "unable to get 32bit dma\n");
301 err = -ENXIO;
302 goto pcifail;
303 }
304
305 cs5535au = kzalloc(sizeof(*cs5535au), GFP_KERNEL);
306 if (cs5535au == NULL) {
307 err = -ENOMEM;
308 goto pcifail;
309 }
310
311 spin_lock_init(&cs5535au->reg_lock);
312 cs5535au->card = card;
313 cs5535au->pci = pci;
314 cs5535au->irq = -1;
315
316 if ((err = pci_request_regions(pci, "CS5535 Audio")) < 0) {
317 kfree(cs5535au);
318 goto pcifail;
319 }
320
321 cs5535au->port = pci_resource_start(pci, 0);
322
323 if (request_irq(pci->irq, snd_cs5535audio_interrupt,
Takashi Iwai66f8df62005-11-17 14:56:21 +0100324 SA_INTERRUPT|SA_SHIRQ, "CS5535 Audio", cs5535au)) {
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100325 snd_printk("unable to grab IRQ %d\n", pci->irq);
326 err = -EBUSY;
327 goto sndfail;
328 }
329
330 cs5535au->irq = pci->irq;
331 pci_set_master(pci);
332
333 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
Takashi Iwai66f8df62005-11-17 14:56:21 +0100334 cs5535au, &ops)) < 0)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100335 goto sndfail;
336
337 snd_card_set_dev(card, &pci->dev);
338
339 *rcs5535au = cs5535au;
340 return 0;
341
342sndfail: /* leave the device alive, just kill the snd */
343 snd_cs5535audio_free(cs5535au);
344 return err;
345
346pcifail:
347 pci_disable_device(pci);
348 return err;
349}
350
351static int __devinit snd_cs5535audio_probe(struct pci_dev *pci,
Takashi Iwai66f8df62005-11-17 14:56:21 +0100352 const struct pci_device_id *pci_id)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100353{
354 static int dev;
Takashi Iwai66f8df62005-11-17 14:56:21 +0100355 struct snd_card *card;
356 struct cs5535audio *cs5535au;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100357 int err;
358
359 if (dev >= SNDRV_CARDS)
360 return -ENODEV;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100361
Jaya Kumar77389b42006-05-19 12:04:22 +0200362 card = snd_card_new(index, id, THIS_MODULE, 0);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100363 if (card == NULL)
364 return -ENOMEM;
365
366 if ((err = snd_cs5535audio_create(card, pci, &cs5535au)) < 0)
367 goto probefail_out;
368
Jaya Kumar9ac25592006-04-28 14:34:49 +0200369 card->private_data = cs5535au;
370
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100371 if ((err = snd_cs5535audio_mixer(cs5535au)) < 0)
372 goto probefail_out;
373
374 if ((err = snd_cs5535audio_pcm(cs5535au)) < 0)
375 goto probefail_out;
376
377 strcpy(card->driver, DRIVER_NAME);
378
379 strcpy(card->shortname, "CS5535 Audio");
380 sprintf(card->longname, "%s %s at 0x%lx, irq %i",
381 card->shortname, card->driver,
382 cs5535au->port, cs5535au->irq);
383
384 if ((err = snd_card_register(card)) < 0)
385 goto probefail_out;
386
387 pci_set_drvdata(pci, card);
388 dev++;
389 return 0;
390
391probefail_out:
392 snd_card_free(card);
393 return err;
394}
395
396static void __devexit snd_cs5535audio_remove(struct pci_dev *pci)
397{
398 snd_card_free(pci_get_drvdata(pci));
399 pci_set_drvdata(pci, NULL);
400}
401
402static struct pci_driver driver = {
403 .name = DRIVER_NAME,
404 .id_table = snd_cs5535audio_ids,
405 .probe = snd_cs5535audio_probe,
406 .remove = __devexit_p(snd_cs5535audio_remove),
Jaya Kumar9ac25592006-04-28 14:34:49 +0200407#ifdef CONFIG_PM
408 .suspend = snd_cs5535audio_suspend,
409 .resume = snd_cs5535audio_resume,
410#endif
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100411};
412
413static int __init alsa_card_cs5535audio_init(void)
414{
Ben Gardnere3291132006-01-09 20:51:29 -0800415 return pci_register_driver(&driver);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100416}
417
418static void __exit alsa_card_cs5535audio_exit(void)
419{
420 pci_unregister_driver(&driver);
421}
422
423module_init(alsa_card_cs5535audio_init)
424module_exit(alsa_card_cs5535audio_exit)
425
426MODULE_AUTHOR("Jaya Kumar");
427MODULE_LICENSE("GPL");
428MODULE_DESCRIPTION("CS5535 Audio");
429MODULE_SUPPORTED_DEVICE("CS5535 Audio");