blob: 6c886edd06db1fa5f223e6eea6b23239a3a64d95 [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
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>
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010031#include <sound/core.h>
32#include <sound/control.h>
33#include <sound/pcm.h>
34#include <sound/rawmidi.h>
35#include <sound/ac97_codec.h>
36#include <sound/initval.h>
37#include <sound/asoundef.h>
38#include "cs5535audio.h"
39
40#define DRIVER_NAME "cs5535audio"
41
Jaya Kumar9ac25592006-04-28 14:34:49 +020042static char *ac97_quirk;
43module_param(ac97_quirk, charp, 0444);
44MODULE_PARM_DESC(ac97_quirk, "AC'97 board specific workarounds.");
45
46static struct ac97_quirk ac97_quirks[] __devinitdata = {
47#if 0 /* Not yet confirmed if all 5536 boards are HP only */
48 {
49 .subvendor = PCI_VENDOR_ID_AMD,
50 .subdevice = PCI_DEVICE_ID_AMD_CS5536_AUDIO,
51 .name = "AMD RDK",
52 .type = AC97_TUNE_HP_ONLY
53 },
54#endif
55 {}
56};
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010057
Takashi Iwai302e4c22006-05-23 13:24:30 +020058static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
59static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
60static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010061
Takashi Iwai302e4c22006-05-23 13:24:30 +020062module_param_array(index, int, NULL, 0444);
Takashi Iwai1dbfd8c2006-05-02 18:31:31 +020063MODULE_PARM_DESC(index, "Index value for " DRIVER_NAME);
Takashi Iwai302e4c22006-05-23 13:24:30 +020064module_param_array(id, charp, NULL, 0444);
Takashi Iwai1dbfd8c2006-05-02 18:31:31 +020065MODULE_PARM_DESC(id, "ID string for " DRIVER_NAME);
Takashi Iwai302e4c22006-05-23 13:24:30 +020066module_param_array(enable, bool, NULL, 0444);
67MODULE_PARM_DESC(enable, "Enable " DRIVER_NAME);
Takashi Iwai1dbfd8c2006-05-02 18:31:31 +020068
Takashi Iwaif40b6892006-07-05 16:51:05 +020069static struct pci_device_id snd_cs5535audio_ids[] = {
Jaya Kumar9ac25592006-04-28 14:34:49 +020070 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_AUDIO) },
71 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_AUDIO) },
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010072 {}
73};
74
75MODULE_DEVICE_TABLE(pci, snd_cs5535audio_ids);
76
Takashi Iwai66f8df62005-11-17 14:56:21 +010077static void wait_till_cmd_acked(struct cs5535audio *cs5535au, unsigned long timeout)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010078{
Takashi Iwai3e873172005-11-17 10:15:37 +010079 unsigned int tmp;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010080 do {
81 tmp = cs_readl(cs5535au, ACC_CODEC_CNTL);
82 if (!(tmp & CMD_NEW))
83 break;
David Vrabel10375932006-03-03 18:01:57 +010084 udelay(1);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010085 } while (--timeout);
86 if (!timeout)
87 snd_printk(KERN_ERR "Failure writing to cs5535 codec\n");
88}
89
Takashi Iwai66f8df62005-11-17 14:56:21 +010090static unsigned short snd_cs5535audio_codec_read(struct cs5535audio *cs5535au,
91 unsigned short reg)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010092{
Takashi Iwai3e873172005-11-17 10:15:37 +010093 unsigned int regdata;
Takashi Iwai66f8df62005-11-17 14:56:21 +010094 unsigned int timeout;
Takashi Iwai3e873172005-11-17 10:15:37 +010095 unsigned int val;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010096
Takashi Iwai3e873172005-11-17 10:15:37 +010097 regdata = ((unsigned int) reg) << 24;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010098 regdata |= ACC_CODEC_CNTL_RD_CMD;
99 regdata |= CMD_NEW;
100
101 cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
David Vrabel10375932006-03-03 18:01:57 +0100102 wait_till_cmd_acked(cs5535au, 50);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100103
104 timeout = 50;
105 do {
106 val = cs_readl(cs5535au, ACC_CODEC_STATUS);
Takashi Iwai3e873172005-11-17 10:15:37 +0100107 if ((val & STS_NEW) && reg == (val >> 24))
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100108 break;
David Vrabel10375932006-03-03 18:01:57 +0100109 udelay(1);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100110 } while (--timeout);
111 if (!timeout)
Jaya Kumar40a4f7a2006-06-13 12:01:14 +0200112 snd_printk(KERN_ERR "Failure reading codec reg 0x%x,"
113 "Last value=0x%x\n", reg, val);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100114
Takashi Iwai3e873172005-11-17 10:15:37 +0100115 return (unsigned short) val;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100116}
117
Takashi Iwai66f8df62005-11-17 14:56:21 +0100118static void snd_cs5535audio_codec_write(struct cs5535audio *cs5535au,
119 unsigned short reg, unsigned short val)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100120{
Takashi Iwai3e873172005-11-17 10:15:37 +0100121 unsigned int regdata;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100122
Takashi Iwai3e873172005-11-17 10:15:37 +0100123 regdata = ((unsigned int) reg) << 24;
124 regdata |= val;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100125 regdata &= CMD_MASK;
126 regdata |= CMD_NEW;
127 regdata &= ACC_CODEC_CNTL_WR_CMD;
128
129 cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
130 wait_till_cmd_acked(cs5535au, 50);
131}
132
Takashi Iwai66f8df62005-11-17 14:56:21 +0100133static void snd_cs5535audio_ac97_codec_write(struct snd_ac97 *ac97,
134 unsigned short reg, unsigned short val)
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 snd_cs5535audio_codec_write(cs5535au, reg, val);
138}
139
Takashi Iwai66f8df62005-11-17 14:56:21 +0100140static unsigned short snd_cs5535audio_ac97_codec_read(struct snd_ac97 *ac97,
141 unsigned short reg)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100142{
Takashi Iwai66f8df62005-11-17 14:56:21 +0100143 struct cs5535audio *cs5535au = ac97->private_data;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100144 return snd_cs5535audio_codec_read(cs5535au, reg);
145}
146
Randy Dunlap5be55be2008-01-23 11:52:06 +0100147static int __devinit snd_cs5535audio_mixer(struct cs5535audio *cs5535au)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100148{
Takashi Iwai66f8df62005-11-17 14:56:21 +0100149 struct snd_card *card = cs5535au->card;
150 struct snd_ac97_bus *pbus;
151 struct snd_ac97_template ac97;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100152 int err;
Takashi Iwai66f8df62005-11-17 14:56:21 +0100153 static struct snd_ac97_bus_ops ops = {
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100154 .write = snd_cs5535audio_ac97_codec_write,
155 .read = snd_cs5535audio_ac97_codec_read,
156 };
157
158 if ((err = snd_ac97_bus(card, 0, &ops, NULL, &pbus)) < 0)
159 return err;
160
161 memset(&ac97, 0, sizeof(ac97));
Jaya Kumarb035ce02008-11-05 17:30:08 -0500162 ac97.scaps = AC97_SCAP_AUDIO | AC97_SCAP_SKIP_MODEM
163 | AC97_SCAP_POWER_SAVE;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100164 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
David Howells7d12e782006-10-05 14:55:46 +0100206static irqreturn_t snd_cs5535audio_interrupt(int irq, void *dev_id)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100207{
208 u16 acc_irq_stat;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100209 unsigned char count;
Takashi Iwai66f8df62005-11-17 14:56:21 +0100210 struct cs5535audio *cs5535au = dev_id;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100211
212 if (cs5535au == NULL)
213 return IRQ_NONE;
214
215 acc_irq_stat = cs_readw(cs5535au, ACC_IRQ_STATUS);
216
217 if (!acc_irq_stat)
218 return IRQ_NONE;
Andres Salomon4ea24162007-09-03 15:43:43 +0200219 for (count = 0; count < 4; count++) {
Takashi Iwai3e873172005-11-17 10:15:37 +0100220 if (acc_irq_stat & (1 << count)) {
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100221 switch (count) {
222 case IRQ_STS:
223 cs_readl(cs5535au, ACC_GPIO_STATUS);
224 break;
225 case WU_IRQ_STS:
226 cs_readl(cs5535au, ACC_GPIO_STATUS);
227 break;
228 case BM0_IRQ_STS:
229 process_bm0_irq(cs5535au);
230 break;
231 case BM1_IRQ_STS:
232 process_bm1_irq(cs5535au);
233 break;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100234 default:
Andres Salomon4ea24162007-09-03 15:43:43 +0200235 snd_printk(KERN_ERR "Unexpected irq src: "
236 "0x%x\n", acc_irq_stat);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100237 break;
238 }
239 }
240 }
241 return IRQ_HANDLED;
242}
243
Takashi Iwai66f8df62005-11-17 14:56:21 +0100244static int snd_cs5535audio_free(struct cs5535audio *cs5535au)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100245{
246 synchronize_irq(cs5535au->irq);
247 pci_set_power_state(cs5535au->pci, 3);
248
249 if (cs5535au->irq >= 0)
250 free_irq(cs5535au->irq, cs5535au);
251
252 pci_release_regions(cs5535au->pci);
253 pci_disable_device(cs5535au->pci);
254 kfree(cs5535au);
255 return 0;
256}
257
Takashi Iwai66f8df62005-11-17 14:56:21 +0100258static int snd_cs5535audio_dev_free(struct snd_device *device)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100259{
Takashi Iwai66f8df62005-11-17 14:56:21 +0100260 struct cs5535audio *cs5535au = device->device_data;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100261 return snd_cs5535audio_free(cs5535au);
262}
263
Takashi Iwai66f8df62005-11-17 14:56:21 +0100264static int __devinit snd_cs5535audio_create(struct snd_card *card,
265 struct pci_dev *pci,
266 struct cs5535audio **rcs5535au)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100267{
Takashi Iwai66f8df62005-11-17 14:56:21 +0100268 struct cs5535audio *cs5535au;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100269
270 int err;
Takashi Iwai66f8df62005-11-17 14:56:21 +0100271 static struct snd_device_ops ops = {
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100272 .dev_free = snd_cs5535audio_dev_free,
273 };
274
275 *rcs5535au = NULL;
276 if ((err = pci_enable_device(pci)) < 0)
277 return err;
278
279 if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0 ||
Takashi Iwai66f8df62005-11-17 14:56:21 +0100280 pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0) {
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100281 printk(KERN_WARNING "unable to get 32bit dma\n");
282 err = -ENXIO;
283 goto pcifail;
284 }
285
286 cs5535au = kzalloc(sizeof(*cs5535au), GFP_KERNEL);
287 if (cs5535au == NULL) {
288 err = -ENOMEM;
289 goto pcifail;
290 }
291
292 spin_lock_init(&cs5535au->reg_lock);
293 cs5535au->card = card;
294 cs5535au->pci = pci;
295 cs5535au->irq = -1;
296
297 if ((err = pci_request_regions(pci, "CS5535 Audio")) < 0) {
298 kfree(cs5535au);
299 goto pcifail;
300 }
301
302 cs5535au->port = pci_resource_start(pci, 0);
303
304 if (request_irq(pci->irq, snd_cs5535audio_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +0100305 IRQF_SHARED, "CS5535 Audio", cs5535au)) {
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100306 snd_printk("unable to grab IRQ %d\n", pci->irq);
307 err = -EBUSY;
308 goto sndfail;
309 }
310
311 cs5535au->irq = pci->irq;
312 pci_set_master(pci);
313
314 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
Takashi Iwai66f8df62005-11-17 14:56:21 +0100315 cs5535au, &ops)) < 0)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100316 goto sndfail;
317
318 snd_card_set_dev(card, &pci->dev);
319
320 *rcs5535au = cs5535au;
321 return 0;
322
323sndfail: /* leave the device alive, just kill the snd */
324 snd_cs5535audio_free(cs5535au);
325 return err;
326
327pcifail:
328 pci_disable_device(pci);
329 return err;
330}
331
332static int __devinit snd_cs5535audio_probe(struct pci_dev *pci,
Takashi Iwai66f8df62005-11-17 14:56:21 +0100333 const struct pci_device_id *pci_id)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100334{
335 static int dev;
Takashi Iwai66f8df62005-11-17 14:56:21 +0100336 struct snd_card *card;
337 struct cs5535audio *cs5535au;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100338 int err;
339
340 if (dev >= SNDRV_CARDS)
341 return -ENODEV;
Takashi Iwai302e4c22006-05-23 13:24:30 +0200342 if (!enable[dev]) {
343 dev++;
344 return -ENOENT;
345 }
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100346
Takashi Iwai302e4c22006-05-23 13:24:30 +0200347 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100348 if (card == NULL)
349 return -ENOMEM;
350
351 if ((err = snd_cs5535audio_create(card, pci, &cs5535au)) < 0)
352 goto probefail_out;
353
Jaya Kumar9ac25592006-04-28 14:34:49 +0200354 card->private_data = cs5535au;
355
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100356 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),
Jaya Kumar9ac25592006-04-28 14:34:49 +0200392#ifdef CONFIG_PM
393 .suspend = snd_cs5535audio_suspend,
394 .resume = snd_cs5535audio_resume,
395#endif
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100396};
397
398static int __init alsa_card_cs5535audio_init(void)
399{
Ben Gardnere3291132006-01-09 20:51:29 -0800400 return pci_register_driver(&driver);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100401}
402
403static void __exit alsa_card_cs5535audio_exit(void)
404{
405 pci_unregister_driver(&driver);
406}
407
408module_init(alsa_card_cs5535audio_init)
409module_exit(alsa_card_cs5535audio_exit)
410
411MODULE_AUTHOR("Jaya Kumar");
412MODULE_LICENSE("GPL");
413MODULE_DESCRIPTION("CS5535 Audio");
414MODULE_SUPPORTED_DEVICE("CS5535 Audio");