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