2 * Driver for audio on multifunction CS5535/6 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 <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>
31 #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"
40 #define DRIVER_NAME "cs5535audio"
42 static char *ac97_quirk
;
43 module_param(ac97_quirk
, charp
, 0444);
44 MODULE_PARM_DESC(ac97_quirk
, "AC'97 board specific workarounds.");
46 static struct ac97_quirk ac97_quirks
[] __devinitdata
= {
47 #if 0 /* Not yet confirmed if all 5536 boards are HP only */
49 .subvendor
= PCI_VENDOR_ID_AMD
,
50 .subdevice
= PCI_DEVICE_ID_AMD_CS5536_AUDIO
,
52 .type
= AC97_TUNE_HP_ONLY
58 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
;
59 static char *id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
;
60 static int enable
[SNDRV_CARDS
] = SNDRV_DEFAULT_ENABLE_PNP
;
62 module_param_array(index
, int, NULL
, 0444);
63 MODULE_PARM_DESC(index
, "Index value for " DRIVER_NAME
);
64 module_param_array(id
, charp
, NULL
, 0444);
65 MODULE_PARM_DESC(id
, "ID string for " DRIVER_NAME
);
66 module_param_array(enable
, bool, NULL
, 0444);
67 MODULE_PARM_DESC(enable
, "Enable " DRIVER_NAME
);
69 static struct pci_device_id snd_cs5535audio_ids
[] = {
70 { 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
) },
75 MODULE_DEVICE_TABLE(pci
, snd_cs5535audio_ids
);
77 static void wait_till_cmd_acked(struct cs5535audio
*cs5535au
, unsigned long timeout
)
81 tmp
= cs_readl(cs5535au
, ACC_CODEC_CNTL
);
87 snd_printk(KERN_ERR
"Failure writing to cs5535 codec\n");
90 static unsigned short snd_cs5535audio_codec_read(struct cs5535audio
*cs5535au
,
97 regdata
= ((unsigned int) reg
) << 24;
98 regdata
|= ACC_CODEC_CNTL_RD_CMD
;
101 cs_writel(cs5535au
, ACC_CODEC_CNTL
, regdata
);
102 wait_till_cmd_acked(cs5535au
, 50);
106 val
= cs_readl(cs5535au
, ACC_CODEC_STATUS
);
107 if ((val
& STS_NEW
) && reg
== (val
>> 24))
112 snd_printk(KERN_ERR
"Failure reading codec reg 0x%x,"
113 "Last value=0x%x\n", reg
, val
);
115 return (unsigned short) val
;
118 static void snd_cs5535audio_codec_write(struct cs5535audio
*cs5535au
,
119 unsigned short reg
, unsigned short val
)
121 unsigned int regdata
;
123 regdata
= ((unsigned int) reg
) << 24;
127 regdata
&= ACC_CODEC_CNTL_WR_CMD
;
129 cs_writel(cs5535au
, ACC_CODEC_CNTL
, regdata
);
130 wait_till_cmd_acked(cs5535au
, 50);
133 static void snd_cs5535audio_ac97_codec_write(struct snd_ac97
*ac97
,
134 unsigned short reg
, unsigned short val
)
136 struct cs5535audio
*cs5535au
= ac97
->private_data
;
137 snd_cs5535audio_codec_write(cs5535au
, reg
, val
);
140 static unsigned short snd_cs5535audio_ac97_codec_read(struct snd_ac97
*ac97
,
143 struct cs5535audio
*cs5535au
= ac97
->private_data
;
144 return snd_cs5535audio_codec_read(cs5535au
, reg
);
147 static int __devinit
snd_cs5535audio_mixer(struct cs5535audio
*cs5535au
)
149 struct snd_card
*card
= cs5535au
->card
;
150 struct snd_ac97_bus
*pbus
;
151 struct snd_ac97_template ac97
;
153 static struct snd_ac97_bus_ops ops
= {
154 .write
= snd_cs5535audio_ac97_codec_write
,
155 .read
= snd_cs5535audio_ac97_codec_read
,
158 if ((err
= snd_ac97_bus(card
, 0, &ops
, NULL
, &pbus
)) < 0)
161 memset(&ac97
, 0, sizeof(ac97
));
162 ac97
.scaps
= AC97_SCAP_AUDIO
|AC97_SCAP_SKIP_MODEM
;
163 ac97
.private_data
= cs5535au
;
164 ac97
.pci
= cs5535au
->pci
;
166 if ((err
= snd_ac97_mixer(pbus
, &ac97
, &cs5535au
->ac97
)) < 0) {
167 snd_printk(KERN_ERR
"mixer failed\n");
171 snd_ac97_tune_hardware(cs5535au
->ac97
, ac97_quirks
, ac97_quirk
);
176 static void process_bm0_irq(struct cs5535audio
*cs5535au
)
179 spin_lock(&cs5535au
->reg_lock
);
180 bm_stat
= cs_readb(cs5535au
, ACC_BM0_STATUS
);
181 spin_unlock(&cs5535au
->reg_lock
);
183 struct cs5535audio_dma
*dma
;
184 dma
= cs5535au
->playback_substream
->runtime
->private_data
;
185 snd_pcm_period_elapsed(cs5535au
->playback_substream
);
187 snd_printk(KERN_ERR
"unexpected bm0 irq src, bm_stat=%x\n",
192 static void process_bm1_irq(struct cs5535audio
*cs5535au
)
195 spin_lock(&cs5535au
->reg_lock
);
196 bm_stat
= cs_readb(cs5535au
, ACC_BM1_STATUS
);
197 spin_unlock(&cs5535au
->reg_lock
);
199 struct cs5535audio_dma
*dma
;
200 dma
= cs5535au
->capture_substream
->runtime
->private_data
;
201 snd_pcm_period_elapsed(cs5535au
->capture_substream
);
205 static irqreturn_t
snd_cs5535audio_interrupt(int irq
, void *dev_id
)
209 struct cs5535audio
*cs5535au
= dev_id
;
211 if (cs5535au
== NULL
)
214 acc_irq_stat
= cs_readw(cs5535au
, ACC_IRQ_STATUS
);
218 for (count
= 0; count
< 4; count
++) {
219 if (acc_irq_stat
& (1 << count
)) {
222 cs_readl(cs5535au
, ACC_GPIO_STATUS
);
225 cs_readl(cs5535au
, ACC_GPIO_STATUS
);
228 process_bm0_irq(cs5535au
);
231 process_bm1_irq(cs5535au
);
234 snd_printk(KERN_ERR
"Unexpected irq src: "
235 "0x%x\n", acc_irq_stat
);
243 static int snd_cs5535audio_free(struct cs5535audio
*cs5535au
)
245 synchronize_irq(cs5535au
->irq
);
246 pci_set_power_state(cs5535au
->pci
, 3);
248 if (cs5535au
->irq
>= 0)
249 free_irq(cs5535au
->irq
, cs5535au
);
251 pci_release_regions(cs5535au
->pci
);
252 pci_disable_device(cs5535au
->pci
);
257 static int snd_cs5535audio_dev_free(struct snd_device
*device
)
259 struct cs5535audio
*cs5535au
= device
->device_data
;
260 return snd_cs5535audio_free(cs5535au
);
263 static int __devinit
snd_cs5535audio_create(struct snd_card
*card
,
265 struct cs5535audio
**rcs5535au
)
267 struct cs5535audio
*cs5535au
;
270 static struct snd_device_ops ops
= {
271 .dev_free
= snd_cs5535audio_dev_free
,
275 if ((err
= pci_enable_device(pci
)) < 0)
278 if (pci_set_dma_mask(pci
, DMA_32BIT_MASK
) < 0 ||
279 pci_set_consistent_dma_mask(pci
, DMA_32BIT_MASK
) < 0) {
280 printk(KERN_WARNING
"unable to get 32bit dma\n");
285 cs5535au
= kzalloc(sizeof(*cs5535au
), GFP_KERNEL
);
286 if (cs5535au
== NULL
) {
291 spin_lock_init(&cs5535au
->reg_lock
);
292 cs5535au
->card
= card
;
296 if ((err
= pci_request_regions(pci
, "CS5535 Audio")) < 0) {
301 cs5535au
->port
= pci_resource_start(pci
, 0);
303 if (request_irq(pci
->irq
, snd_cs5535audio_interrupt
,
304 IRQF_SHARED
, "CS5535 Audio", cs5535au
)) {
305 snd_printk("unable to grab IRQ %d\n", pci
->irq
);
310 cs5535au
->irq
= pci
->irq
;
313 if ((err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
,
314 cs5535au
, &ops
)) < 0)
317 snd_card_set_dev(card
, &pci
->dev
);
319 *rcs5535au
= cs5535au
;
322 sndfail
: /* leave the device alive, just kill the snd */
323 snd_cs5535audio_free(cs5535au
);
327 pci_disable_device(pci
);
331 static int __devinit
snd_cs5535audio_probe(struct pci_dev
*pci
,
332 const struct pci_device_id
*pci_id
)
335 struct snd_card
*card
;
336 struct cs5535audio
*cs5535au
;
339 if (dev
>= SNDRV_CARDS
)
346 card
= snd_card_new(index
[dev
], id
[dev
], THIS_MODULE
, 0);
350 if ((err
= snd_cs5535audio_create(card
, pci
, &cs5535au
)) < 0)
353 card
->private_data
= cs5535au
;
355 if ((err
= snd_cs5535audio_mixer(cs5535au
)) < 0)
358 if ((err
= snd_cs5535audio_pcm(cs5535au
)) < 0)
361 strcpy(card
->driver
, DRIVER_NAME
);
363 strcpy(card
->shortname
, "CS5535 Audio");
364 sprintf(card
->longname
, "%s %s at 0x%lx, irq %i",
365 card
->shortname
, card
->driver
,
366 cs5535au
->port
, cs5535au
->irq
);
368 if ((err
= snd_card_register(card
)) < 0)
371 pci_set_drvdata(pci
, card
);
380 static void __devexit
snd_cs5535audio_remove(struct pci_dev
*pci
)
382 snd_card_free(pci_get_drvdata(pci
));
383 pci_set_drvdata(pci
, NULL
);
386 static struct pci_driver driver
= {
388 .id_table
= snd_cs5535audio_ids
,
389 .probe
= snd_cs5535audio_probe
,
390 .remove
= __devexit_p(snd_cs5535audio_remove
),
392 .suspend
= snd_cs5535audio_suspend
,
393 .resume
= snd_cs5535audio_resume
,
397 static int __init
alsa_card_cs5535audio_init(void)
399 return pci_register_driver(&driver
);
402 static void __exit
alsa_card_cs5535audio_exit(void)
404 pci_unregister_driver(&driver
);
407 module_init(alsa_card_cs5535audio_init
)
408 module_exit(alsa_card_cs5535audio_exit
)
410 MODULE_AUTHOR("Jaya Kumar");
411 MODULE_LICENSE("GPL");
412 MODULE_DESCRIPTION("CS5535 Audio");
413 MODULE_SUPPORTED_DEVICE("CS5535 Audio");