]>
Commit | Line | Data |
---|---|---|
9b4ffa48 | 1 | /* |
9ac25594 | 2 | * Driver for audio on multifunction CS5535/6 companion device |
9b4ffa48 JK |
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> | |
9b4ffa48 JK |
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" | |
39 | ||
40 | #define DRIVER_NAME "cs5535audio" | |
41 | ||
9ac25594 JK |
42 | static char *ac97_quirk; |
43 | module_param(ac97_quirk, charp, 0444); | |
44 | MODULE_PARM_DESC(ac97_quirk, "AC'97 board specific workarounds."); | |
45 | ||
46 | static 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 | }; | |
9b4ffa48 | 57 | |
302e4c2f TI |
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; | |
9b4ffa48 | 61 | |
302e4c2f | 62 | module_param_array(index, int, NULL, 0444); |
1dbfd8c5 | 63 | MODULE_PARM_DESC(index, "Index value for " DRIVER_NAME); |
302e4c2f | 64 | module_param_array(id, charp, NULL, 0444); |
1dbfd8c5 | 65 | MODULE_PARM_DESC(id, "ID string for " DRIVER_NAME); |
302e4c2f TI |
66 | module_param_array(enable, bool, NULL, 0444); |
67 | MODULE_PARM_DESC(enable, "Enable " DRIVER_NAME); | |
1dbfd8c5 | 68 | |
f40b6890 | 69 | static struct pci_device_id snd_cs5535audio_ids[] = { |
9ac25594 JK |
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) }, | |
9b4ffa48 JK |
72 | {} |
73 | }; | |
74 | ||
75 | MODULE_DEVICE_TABLE(pci, snd_cs5535audio_ids); | |
76 | ||
66f8df6b | 77 | static void wait_till_cmd_acked(struct cs5535audio *cs5535au, unsigned long timeout) |
9b4ffa48 | 78 | { |
3e873174 | 79 | unsigned int tmp; |
9b4ffa48 JK |
80 | do { |
81 | tmp = cs_readl(cs5535au, ACC_CODEC_CNTL); | |
82 | if (!(tmp & CMD_NEW)) | |
83 | break; | |
1037593c | 84 | udelay(1); |
9b4ffa48 JK |
85 | } while (--timeout); |
86 | if (!timeout) | |
87 | snd_printk(KERN_ERR "Failure writing to cs5535 codec\n"); | |
88 | } | |
89 | ||
66f8df6b TI |
90 | static unsigned short snd_cs5535audio_codec_read(struct cs5535audio *cs5535au, |
91 | unsigned short reg) | |
9b4ffa48 | 92 | { |
3e873174 | 93 | unsigned int regdata; |
66f8df6b | 94 | unsigned int timeout; |
3e873174 | 95 | unsigned int val; |
9b4ffa48 | 96 | |
3e873174 | 97 | regdata = ((unsigned int) reg) << 24; |
9b4ffa48 JK |
98 | regdata |= ACC_CODEC_CNTL_RD_CMD; |
99 | regdata |= CMD_NEW; | |
100 | ||
101 | cs_writel(cs5535au, ACC_CODEC_CNTL, regdata); | |
1037593c | 102 | wait_till_cmd_acked(cs5535au, 50); |
9b4ffa48 JK |
103 | |
104 | timeout = 50; | |
105 | do { | |
106 | val = cs_readl(cs5535au, ACC_CODEC_STATUS); | |
3e873174 | 107 | if ((val & STS_NEW) && reg == (val >> 24)) |
9b4ffa48 | 108 | break; |
1037593c | 109 | udelay(1); |
9b4ffa48 JK |
110 | } while (--timeout); |
111 | if (!timeout) | |
40a4f7a0 JK |
112 | snd_printk(KERN_ERR "Failure reading codec reg 0x%x," |
113 | "Last value=0x%x\n", reg, val); | |
9b4ffa48 | 114 | |
3e873174 | 115 | return (unsigned short) val; |
9b4ffa48 JK |
116 | } |
117 | ||
66f8df6b TI |
118 | static void snd_cs5535audio_codec_write(struct cs5535audio *cs5535au, |
119 | unsigned short reg, unsigned short val) | |
9b4ffa48 | 120 | { |
3e873174 | 121 | unsigned int regdata; |
9b4ffa48 | 122 | |
3e873174 TI |
123 | regdata = ((unsigned int) reg) << 24; |
124 | regdata |= val; | |
9b4ffa48 JK |
125 | 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 | ||
66f8df6b TI |
133 | static void snd_cs5535audio_ac97_codec_write(struct snd_ac97 *ac97, |
134 | unsigned short reg, unsigned short val) | |
9b4ffa48 | 135 | { |
66f8df6b | 136 | struct cs5535audio *cs5535au = ac97->private_data; |
9b4ffa48 JK |
137 | snd_cs5535audio_codec_write(cs5535au, reg, val); |
138 | } | |
139 | ||
66f8df6b TI |
140 | static unsigned short snd_cs5535audio_ac97_codec_read(struct snd_ac97 *ac97, |
141 | unsigned short reg) | |
9b4ffa48 | 142 | { |
66f8df6b | 143 | struct cs5535audio *cs5535au = ac97->private_data; |
9b4ffa48 JK |
144 | return snd_cs5535audio_codec_read(cs5535au, reg); |
145 | } | |
146 | ||
5be55be5 | 147 | static int __devinit snd_cs5535audio_mixer(struct cs5535audio *cs5535au) |
9b4ffa48 | 148 | { |
66f8df6b TI |
149 | struct snd_card *card = cs5535au->card; |
150 | struct snd_ac97_bus *pbus; | |
151 | struct snd_ac97_template ac97; | |
9b4ffa48 | 152 | int err; |
66f8df6b | 153 | static struct snd_ac97_bus_ops ops = { |
9b4ffa48 JK |
154 | .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)); | |
b035ce0f JK |
162 | ac97.scaps = AC97_SCAP_AUDIO | AC97_SCAP_SKIP_MODEM |
163 | | AC97_SCAP_POWER_SAVE; | |
9b4ffa48 JK |
164 | ac97.private_data = cs5535au; |
165 | ac97.pci = cs5535au->pci; | |
9b4ffa48 JK |
166 | |
167 | if ((err = snd_ac97_mixer(pbus, &ac97, &cs5535au->ac97)) < 0) { | |
3e873174 | 168 | snd_printk(KERN_ERR "mixer failed\n"); |
9b4ffa48 JK |
169 | return err; |
170 | } | |
171 | ||
9ac25594 JK |
172 | snd_ac97_tune_hardware(cs5535au->ac97, ac97_quirks, ac97_quirk); |
173 | ||
57d4bf6d JK |
174 | /* olpc_quirks is dummied out if not olpc */ |
175 | err = olpc_quirks(card, cs5535au->ac97); | |
176 | if (err < 0) { | |
177 | snd_printk(KERN_ERR "olpc quirks failed\n"); | |
178 | return err; | |
179 | } | |
180 | ||
9b4ffa48 JK |
181 | return 0; |
182 | } | |
183 | ||
66f8df6b | 184 | static void process_bm0_irq(struct cs5535audio *cs5535au) |
9b4ffa48 JK |
185 | { |
186 | u8 bm_stat; | |
187 | spin_lock(&cs5535au->reg_lock); | |
188 | bm_stat = cs_readb(cs5535au, ACC_BM0_STATUS); | |
189 | spin_unlock(&cs5535au->reg_lock); | |
190 | if (bm_stat & EOP) { | |
66f8df6b | 191 | struct cs5535audio_dma *dma; |
9b4ffa48 JK |
192 | dma = cs5535au->playback_substream->runtime->private_data; |
193 | snd_pcm_period_elapsed(cs5535au->playback_substream); | |
194 | } else { | |
195 | snd_printk(KERN_ERR "unexpected bm0 irq src, bm_stat=%x\n", | |
196 | bm_stat); | |
197 | } | |
198 | } | |
199 | ||
66f8df6b | 200 | static void process_bm1_irq(struct cs5535audio *cs5535au) |
9b4ffa48 JK |
201 | { |
202 | u8 bm_stat; | |
203 | spin_lock(&cs5535au->reg_lock); | |
204 | bm_stat = cs_readb(cs5535au, ACC_BM1_STATUS); | |
205 | spin_unlock(&cs5535au->reg_lock); | |
206 | if (bm_stat & EOP) { | |
66f8df6b | 207 | struct cs5535audio_dma *dma; |
9b4ffa48 JK |
208 | dma = cs5535au->capture_substream->runtime->private_data; |
209 | snd_pcm_period_elapsed(cs5535au->capture_substream); | |
210 | } | |
211 | } | |
212 | ||
7d12e780 | 213 | static irqreturn_t snd_cs5535audio_interrupt(int irq, void *dev_id) |
9b4ffa48 JK |
214 | { |
215 | u16 acc_irq_stat; | |
9b4ffa48 | 216 | unsigned char count; |
66f8df6b | 217 | struct cs5535audio *cs5535au = dev_id; |
9b4ffa48 JK |
218 | |
219 | if (cs5535au == NULL) | |
220 | return IRQ_NONE; | |
221 | ||
222 | acc_irq_stat = cs_readw(cs5535au, ACC_IRQ_STATUS); | |
223 | ||
224 | if (!acc_irq_stat) | |
225 | return IRQ_NONE; | |
4ea24163 | 226 | for (count = 0; count < 4; count++) { |
3e873174 | 227 | if (acc_irq_stat & (1 << count)) { |
9b4ffa48 JK |
228 | switch (count) { |
229 | case IRQ_STS: | |
230 | cs_readl(cs5535au, ACC_GPIO_STATUS); | |
231 | break; | |
232 | case WU_IRQ_STS: | |
233 | cs_readl(cs5535au, ACC_GPIO_STATUS); | |
234 | break; | |
235 | case BM0_IRQ_STS: | |
236 | process_bm0_irq(cs5535au); | |
237 | break; | |
238 | case BM1_IRQ_STS: | |
239 | process_bm1_irq(cs5535au); | |
240 | break; | |
9b4ffa48 | 241 | default: |
4ea24163 AS |
242 | snd_printk(KERN_ERR "Unexpected irq src: " |
243 | "0x%x\n", acc_irq_stat); | |
9b4ffa48 JK |
244 | break; |
245 | } | |
246 | } | |
247 | } | |
248 | return IRQ_HANDLED; | |
249 | } | |
250 | ||
66f8df6b | 251 | static int snd_cs5535audio_free(struct cs5535audio *cs5535au) |
9b4ffa48 JK |
252 | { |
253 | synchronize_irq(cs5535au->irq); | |
254 | pci_set_power_state(cs5535au->pci, 3); | |
255 | ||
256 | if (cs5535au->irq >= 0) | |
257 | free_irq(cs5535au->irq, cs5535au); | |
258 | ||
259 | pci_release_regions(cs5535au->pci); | |
260 | pci_disable_device(cs5535au->pci); | |
261 | kfree(cs5535au); | |
262 | return 0; | |
263 | } | |
264 | ||
66f8df6b | 265 | static int snd_cs5535audio_dev_free(struct snd_device *device) |
9b4ffa48 | 266 | { |
66f8df6b | 267 | struct cs5535audio *cs5535au = device->device_data; |
9b4ffa48 JK |
268 | return snd_cs5535audio_free(cs5535au); |
269 | } | |
270 | ||
66f8df6b TI |
271 | static int __devinit snd_cs5535audio_create(struct snd_card *card, |
272 | struct pci_dev *pci, | |
273 | struct cs5535audio **rcs5535au) | |
9b4ffa48 | 274 | { |
66f8df6b | 275 | struct cs5535audio *cs5535au; |
9b4ffa48 JK |
276 | |
277 | int err; | |
66f8df6b | 278 | static struct snd_device_ops ops = { |
9b4ffa48 JK |
279 | .dev_free = snd_cs5535audio_dev_free, |
280 | }; | |
281 | ||
282 | *rcs5535au = NULL; | |
283 | if ((err = pci_enable_device(pci)) < 0) | |
284 | return err; | |
285 | ||
286 | if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0 || | |
66f8df6b | 287 | pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0) { |
9b4ffa48 JK |
288 | printk(KERN_WARNING "unable to get 32bit dma\n"); |
289 | err = -ENXIO; | |
290 | goto pcifail; | |
291 | } | |
292 | ||
293 | cs5535au = kzalloc(sizeof(*cs5535au), GFP_KERNEL); | |
294 | if (cs5535au == NULL) { | |
295 | err = -ENOMEM; | |
296 | goto pcifail; | |
297 | } | |
298 | ||
299 | spin_lock_init(&cs5535au->reg_lock); | |
300 | cs5535au->card = card; | |
301 | cs5535au->pci = pci; | |
302 | cs5535au->irq = -1; | |
303 | ||
304 | if ((err = pci_request_regions(pci, "CS5535 Audio")) < 0) { | |
305 | kfree(cs5535au); | |
306 | goto pcifail; | |
307 | } | |
308 | ||
309 | cs5535au->port = pci_resource_start(pci, 0); | |
310 | ||
311 | if (request_irq(pci->irq, snd_cs5535audio_interrupt, | |
437a5a46 | 312 | IRQF_SHARED, "CS5535 Audio", cs5535au)) { |
9b4ffa48 JK |
313 | snd_printk("unable to grab IRQ %d\n", pci->irq); |
314 | err = -EBUSY; | |
315 | goto sndfail; | |
316 | } | |
317 | ||
318 | cs5535au->irq = pci->irq; | |
319 | pci_set_master(pci); | |
320 | ||
321 | if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, | |
66f8df6b | 322 | cs5535au, &ops)) < 0) |
9b4ffa48 JK |
323 | goto sndfail; |
324 | ||
325 | snd_card_set_dev(card, &pci->dev); | |
326 | ||
327 | *rcs5535au = cs5535au; | |
328 | return 0; | |
329 | ||
330 | sndfail: /* leave the device alive, just kill the snd */ | |
331 | snd_cs5535audio_free(cs5535au); | |
332 | return err; | |
333 | ||
334 | pcifail: | |
335 | pci_disable_device(pci); | |
336 | return err; | |
337 | } | |
338 | ||
339 | static int __devinit snd_cs5535audio_probe(struct pci_dev *pci, | |
66f8df6b | 340 | const struct pci_device_id *pci_id) |
9b4ffa48 JK |
341 | { |
342 | static int dev; | |
66f8df6b TI |
343 | struct snd_card *card; |
344 | struct cs5535audio *cs5535au; | |
9b4ffa48 JK |
345 | int err; |
346 | ||
347 | if (dev >= SNDRV_CARDS) | |
348 | return -ENODEV; | |
302e4c2f TI |
349 | if (!enable[dev]) { |
350 | dev++; | |
351 | return -ENOENT; | |
352 | } | |
9b4ffa48 | 353 | |
302e4c2f | 354 | card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0); |
9b4ffa48 JK |
355 | if (card == NULL) |
356 | return -ENOMEM; | |
357 | ||
358 | if ((err = snd_cs5535audio_create(card, pci, &cs5535au)) < 0) | |
359 | goto probefail_out; | |
360 | ||
9ac25594 JK |
361 | card->private_data = cs5535au; |
362 | ||
9b4ffa48 JK |
363 | if ((err = snd_cs5535audio_mixer(cs5535au)) < 0) |
364 | goto probefail_out; | |
365 | ||
366 | if ((err = snd_cs5535audio_pcm(cs5535au)) < 0) | |
367 | goto probefail_out; | |
368 | ||
369 | strcpy(card->driver, DRIVER_NAME); | |
370 | ||
371 | strcpy(card->shortname, "CS5535 Audio"); | |
372 | sprintf(card->longname, "%s %s at 0x%lx, irq %i", | |
373 | card->shortname, card->driver, | |
374 | cs5535au->port, cs5535au->irq); | |
375 | ||
376 | if ((err = snd_card_register(card)) < 0) | |
377 | goto probefail_out; | |
378 | ||
379 | pci_set_drvdata(pci, card); | |
380 | dev++; | |
381 | return 0; | |
382 | ||
383 | probefail_out: | |
384 | snd_card_free(card); | |
385 | return err; | |
386 | } | |
387 | ||
388 | static void __devexit snd_cs5535audio_remove(struct pci_dev *pci) | |
389 | { | |
390 | snd_card_free(pci_get_drvdata(pci)); | |
391 | pci_set_drvdata(pci, NULL); | |
392 | } | |
393 | ||
394 | static struct pci_driver driver = { | |
395 | .name = DRIVER_NAME, | |
396 | .id_table = snd_cs5535audio_ids, | |
397 | .probe = snd_cs5535audio_probe, | |
398 | .remove = __devexit_p(snd_cs5535audio_remove), | |
9ac25594 JK |
399 | #ifdef CONFIG_PM |
400 | .suspend = snd_cs5535audio_suspend, | |
401 | .resume = snd_cs5535audio_resume, | |
402 | #endif | |
9b4ffa48 JK |
403 | }; |
404 | ||
405 | static int __init alsa_card_cs5535audio_init(void) | |
406 | { | |
e329113c | 407 | return pci_register_driver(&driver); |
9b4ffa48 JK |
408 | } |
409 | ||
410 | static void __exit alsa_card_cs5535audio_exit(void) | |
411 | { | |
412 | pci_unregister_driver(&driver); | |
413 | } | |
414 | ||
415 | module_init(alsa_card_cs5535audio_init) | |
416 | module_exit(alsa_card_cs5535audio_exit) | |
417 | ||
418 | MODULE_AUTHOR("Jaya Kumar"); | |
419 | MODULE_LICENSE("GPL"); | |
420 | MODULE_DESCRIPTION("CS5535 Audio"); | |
421 | MODULE_SUPPORTED_DEVICE("CS5535 Audio"); |