aboutsummaryrefslogtreecommitdiffstats
path: root/sound/atmel
diff options
context:
space:
mode:
Diffstat (limited to 'sound/atmel')
-rw-r--r--sound/atmel/Kconfig19
-rw-r--r--sound/atmel/Makefile5
-rw-r--r--sound/atmel/abdac.c602
-rw-r--r--sound/atmel/ac97c.c1022
-rw-r--r--sound/atmel/ac97c.h73
5 files changed, 1721 insertions, 0 deletions
diff --git a/sound/atmel/Kconfig b/sound/atmel/Kconfig
new file mode 100644
index 000000000000..6c228a91940d
--- /dev/null
+++ b/sound/atmel/Kconfig
@@ -0,0 +1,19 @@
1menu "Atmel devices (AVR32 and AT91)"
2 depends on AVR32 || ARCH_AT91
3
4config SND_ATMEL_ABDAC
5 tristate "Atmel Audio Bitstream DAC (ABDAC) driver"
6 select SND_PCM
7 depends on DW_DMAC && AVR32
8 help
9 ALSA sound driver for the Atmel Audio Bitstream DAC (ABDAC).
10
11config SND_ATMEL_AC97C
12 tristate "Atmel AC97 Controller (AC97C) driver"
13 select SND_PCM
14 select SND_AC97_CODEC
15 depends on DW_DMAC && AVR32
16 help
17 ALSA sound driver for the Atmel AC97 controller.
18
19endmenu
diff --git a/sound/atmel/Makefile b/sound/atmel/Makefile
new file mode 100644
index 000000000000..219dcfac6086
--- /dev/null
+++ b/sound/atmel/Makefile
@@ -0,0 +1,5 @@
1snd-atmel-abdac-objs := abdac.o
2snd-atmel-ac97c-objs := ac97c.o
3
4obj-$(CONFIG_SND_ATMEL_ABDAC) += snd-atmel-abdac.o
5obj-$(CONFIG_SND_ATMEL_AC97C) += snd-atmel-ac97c.o
diff --git a/sound/atmel/abdac.c b/sound/atmel/abdac.c
new file mode 100644
index 000000000000..f2f41c854221
--- /dev/null
+++ b/sound/atmel/abdac.c
@@ -0,0 +1,602 @@
1/*
2 * Driver for the Atmel on-chip Audio Bitstream DAC (ABDAC)
3 *
4 * Copyright (C) 2006-2009 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 */
10#include <linux/clk.h>
11#include <linux/bitmap.h>
12#include <linux/dw_dmac.h>
13#include <linux/dmaengine.h>
14#include <linux/dma-mapping.h>
15#include <linux/init.h>
16#include <linux/interrupt.h>
17#include <linux/module.h>
18#include <linux/platform_device.h>
19#include <linux/io.h>
20
21#include <sound/core.h>
22#include <sound/initval.h>
23#include <sound/pcm.h>
24#include <sound/pcm_params.h>
25#include <sound/atmel-abdac.h>
26
27/* DAC register offsets */
28#define DAC_DATA 0x0000
29#define DAC_CTRL 0x0008
30#define DAC_INT_MASK 0x000c
31#define DAC_INT_EN 0x0010
32#define DAC_INT_DIS 0x0014
33#define DAC_INT_CLR 0x0018
34#define DAC_INT_STATUS 0x001c
35
36/* Bitfields in CTRL */
37#define DAC_SWAP_OFFSET 30
38#define DAC_SWAP_SIZE 1
39#define DAC_EN_OFFSET 31
40#define DAC_EN_SIZE 1
41
42/* Bitfields in INT_MASK/INT_EN/INT_DIS/INT_STATUS/INT_CLR */
43#define DAC_UNDERRUN_OFFSET 28
44#define DAC_UNDERRUN_SIZE 1
45#define DAC_TX_READY_OFFSET 29
46#define DAC_TX_READY_SIZE 1
47
48/* Bit manipulation macros */
49#define DAC_BIT(name) \
50 (1 << DAC_##name##_OFFSET)
51#define DAC_BF(name, value) \
52 (((value) & ((1 << DAC_##name##_SIZE) - 1)) \
53 << DAC_##name##_OFFSET)
54#define DAC_BFEXT(name, value) \
55 (((value) >> DAC_##name##_OFFSET) \
56 & ((1 << DAC_##name##_SIZE) - 1))
57#define DAC_BFINS(name, value, old) \
58 (((old) & ~(((1 << DAC_##name##_SIZE) - 1) \
59 << DAC_##name##_OFFSET)) \
60 | DAC_BF(name, value))
61
62/* Register access macros */
63#define dac_readl(port, reg) \
64 __raw_readl((port)->regs + DAC_##reg)
65#define dac_writel(port, reg, value) \
66 __raw_writel((value), (port)->regs + DAC_##reg)
67
68/*
69 * ABDAC supports a maximum of 6 different rates from a generic clock. The
70 * generic clock has a power of two divider, which gives 6 steps from 192 kHz
71 * to 5112 Hz.
72 */
73#define MAX_NUM_RATES 6
74/* ALSA seems to use rates between 192000 Hz and 5112 Hz. */
75#define RATE_MAX 192000
76#define RATE_MIN 5112
77
78enum {
79 DMA_READY = 0,
80};
81
82struct atmel_abdac_dma {
83 struct dma_chan *chan;
84 struct dw_cyclic_desc *cdesc;
85};
86
87struct atmel_abdac {
88 struct clk *pclk;
89 struct clk *sample_clk;
90 struct platform_device *pdev;
91 struct atmel_abdac_dma dma;
92
93 struct snd_pcm_hw_constraint_list constraints_rates;
94 struct snd_pcm_substream *substream;
95 struct snd_card *card;
96 struct snd_pcm *pcm;
97
98 void __iomem *regs;
99 unsigned long flags;
100 unsigned int rates[MAX_NUM_RATES];
101 unsigned int rates_num;
102 int irq;
103};
104
105#define get_dac(card) ((struct atmel_abdac *)(card)->private_data)
106
107/* This function is called by the DMA driver. */
108static void atmel_abdac_dma_period_done(void *arg)
109{
110 struct atmel_abdac *dac = arg;
111 snd_pcm_period_elapsed(dac->substream);
112}
113
114static int atmel_abdac_prepare_dma(struct atmel_abdac *dac,
115 struct snd_pcm_substream *substream,
116 enum dma_data_direction direction)
117{
118 struct dma_chan *chan = dac->dma.chan;
119 struct dw_cyclic_desc *cdesc;
120 struct snd_pcm_runtime *runtime = substream->runtime;
121 unsigned long buffer_len, period_len;
122
123 /*
124 * We don't do DMA on "complex" transfers, i.e. with
125 * non-halfword-aligned buffers or lengths.
126 */
127 if (runtime->dma_addr & 1 || runtime->buffer_size & 1) {
128 dev_dbg(&dac->pdev->dev, "too complex transfer\n");
129 return -EINVAL;
130 }
131
132 buffer_len = frames_to_bytes(runtime, runtime->buffer_size);
133 period_len = frames_to_bytes(runtime, runtime->period_size);
134
135 cdesc = dw_dma_cyclic_prep(chan, runtime->dma_addr, buffer_len,
136 period_len, DMA_TO_DEVICE);
137 if (IS_ERR(cdesc)) {
138 dev_dbg(&dac->pdev->dev, "could not prepare cyclic DMA\n");
139 return PTR_ERR(cdesc);
140 }
141
142 cdesc->period_callback = atmel_abdac_dma_period_done;
143 cdesc->period_callback_param = dac;
144
145 dac->dma.cdesc = cdesc;
146
147 set_bit(DMA_READY, &dac->flags);
148
149 return 0;
150}
151
152static struct snd_pcm_hardware atmel_abdac_hw = {
153 .info = (SNDRV_PCM_INFO_MMAP
154 | SNDRV_PCM_INFO_MMAP_VALID
155 | SNDRV_PCM_INFO_INTERLEAVED
156 | SNDRV_PCM_INFO_BLOCK_TRANSFER
157 | SNDRV_PCM_INFO_RESUME
158 | SNDRV_PCM_INFO_PAUSE),
159 .formats = (SNDRV_PCM_FMTBIT_S16_BE),
160 .rates = (SNDRV_PCM_RATE_KNOT),
161 .rate_min = RATE_MIN,
162 .rate_max = RATE_MAX,
163 .channels_min = 2,
164 .channels_max = 2,
165 .buffer_bytes_max = 64 * 4096,
166 .period_bytes_min = 4096,
167 .period_bytes_max = 4096,
168 .periods_min = 6,
169 .periods_max = 64,
170};
171
172static int atmel_abdac_open(struct snd_pcm_substream *substream)
173{
174 struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
175
176 dac->substream = substream;
177 atmel_abdac_hw.rate_max = dac->rates[dac->rates_num - 1];
178 atmel_abdac_hw.rate_min = dac->rates[0];
179 substream->runtime->hw = atmel_abdac_hw;
180
181 return snd_pcm_hw_constraint_list(substream->runtime, 0,
182 SNDRV_PCM_HW_PARAM_RATE, &dac->constraints_rates);
183}
184
185static int atmel_abdac_close(struct snd_pcm_substream *substream)
186{
187 struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
188 dac->substream = NULL;
189 return 0;
190}
191
192static int atmel_abdac_hw_params(struct snd_pcm_substream *substream,
193 struct snd_pcm_hw_params *hw_params)
194{
195 struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
196 int retval;
197
198 retval = snd_pcm_lib_malloc_pages(substream,
199 params_buffer_bytes(hw_params));
200 if (retval < 0)
201 return retval;
202 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
203 if (retval == 1)
204 if (test_and_clear_bit(DMA_READY, &dac->flags))
205 dw_dma_cyclic_free(dac->dma.chan);
206
207 return retval;
208}
209
210static int atmel_abdac_hw_free(struct snd_pcm_substream *substream)
211{
212 struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
213 if (test_and_clear_bit(DMA_READY, &dac->flags))
214 dw_dma_cyclic_free(dac->dma.chan);
215 return snd_pcm_lib_free_pages(substream);
216}
217
218static int atmel_abdac_prepare(struct snd_pcm_substream *substream)
219{
220 struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
221 int retval;
222
223 retval = clk_set_rate(dac->sample_clk, 256 * substream->runtime->rate);
224 if (retval)
225 return retval;
226
227 if (!test_bit(DMA_READY, &dac->flags))
228 retval = atmel_abdac_prepare_dma(dac, substream, DMA_TO_DEVICE);
229
230 return retval;
231}
232
233static int atmel_abdac_trigger(struct snd_pcm_substream *substream, int cmd)
234{
235 struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
236 int retval = 0;
237
238 switch (cmd) {
239 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
240 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
241 case SNDRV_PCM_TRIGGER_START:
242 clk_enable(dac->sample_clk);
243 retval = dw_dma_cyclic_start(dac->dma.chan);
244 if (retval)
245 goto out;
246 dac_writel(dac, CTRL, DAC_BIT(EN));
247 break;
248 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
249 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
250 case SNDRV_PCM_TRIGGER_STOP:
251 dw_dma_cyclic_stop(dac->dma.chan);
252 dac_writel(dac, DATA, 0);
253 dac_writel(dac, CTRL, 0);
254 clk_disable(dac->sample_clk);
255 break;
256 default:
257 retval = -EINVAL;
258 break;
259 }
260out:
261 return retval;
262}
263
264static snd_pcm_uframes_t
265atmel_abdac_pointer(struct snd_pcm_substream *substream)
266{
267 struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
268 struct snd_pcm_runtime *runtime = substream->runtime;
269 snd_pcm_uframes_t frames;
270 unsigned long bytes;
271
272 bytes = dw_dma_get_src_addr(dac->dma.chan);
273 bytes -= runtime->dma_addr;
274
275 frames = bytes_to_frames(runtime, bytes);
276 if (frames >= runtime->buffer_size)
277 frames -= runtime->buffer_size;
278
279 return frames;
280}
281
282static irqreturn_t abdac_interrupt(int irq, void *dev_id)
283{
284 struct atmel_abdac *dac = dev_id;
285 u32 status;
286
287 status = dac_readl(dac, INT_STATUS);
288 if (status & DAC_BIT(UNDERRUN)) {
289 dev_err(&dac->pdev->dev, "underrun detected\n");
290 dac_writel(dac, INT_CLR, DAC_BIT(UNDERRUN));
291 } else {
292 dev_err(&dac->pdev->dev, "spurious interrupt (status=0x%x)\n",
293 status);
294 dac_writel(dac, INT_CLR, status);
295 }
296
297 return IRQ_HANDLED;
298}
299
300static struct snd_pcm_ops atmel_abdac_ops = {
301 .open = atmel_abdac_open,
302 .close = atmel_abdac_close,
303 .ioctl = snd_pcm_lib_ioctl,
304 .hw_params = atmel_abdac_hw_params,
305 .hw_free = atmel_abdac_hw_free,
306 .prepare = atmel_abdac_prepare,
307 .trigger = atmel_abdac_trigger,
308 .pointer = atmel_abdac_pointer,
309};
310
311static int __devinit atmel_abdac_pcm_new(struct atmel_abdac *dac)
312{
313 struct snd_pcm_hardware hw = atmel_abdac_hw;
314 struct snd_pcm *pcm;
315 int retval;
316
317 retval = snd_pcm_new(dac->card, dac->card->shortname,
318 dac->pdev->id, 1, 0, &pcm);
319 if (retval)
320 return retval;
321
322 strcpy(pcm->name, dac->card->shortname);
323 pcm->private_data = dac;
324 pcm->info_flags = 0;
325 dac->pcm = pcm;
326
327 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &atmel_abdac_ops);
328
329 retval = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
330 &dac->pdev->dev, hw.periods_min * hw.period_bytes_min,
331 hw.buffer_bytes_max);
332
333 return retval;
334}
335
336static bool filter(struct dma_chan *chan, void *slave)
337{
338 struct dw_dma_slave *dws = slave;
339
340 if (dws->dma_dev == chan->device->dev) {
341 chan->private = dws;
342 return true;
343 } else
344 return false;
345}
346
347static int set_sample_rates(struct atmel_abdac *dac)
348{
349 long new_rate = RATE_MAX;
350 int retval = -EINVAL;
351 int index = 0;
352
353 /* we start at 192 kHz and work our way down to 5112 Hz */
354 while (new_rate >= RATE_MIN && index < (MAX_NUM_RATES + 1)) {
355 new_rate = clk_round_rate(dac->sample_clk, 256 * new_rate);
356 if (new_rate < 0)
357 break;
358 /* make sure we are below the ABDAC clock */
359 if (new_rate <= clk_get_rate(dac->pclk)) {
360 dac->rates[index] = new_rate / 256;
361 index++;
362 }
363 /* divide by 256 and then by two to get next rate */
364 new_rate /= 256 * 2;
365 }
366
367 if (index) {
368 int i;
369
370 /* reverse array, smallest go first */
371 for (i = 0; i < (index / 2); i++) {
372 unsigned int tmp = dac->rates[index - 1 - i];
373 dac->rates[index - 1 - i] = dac->rates[i];
374 dac->rates[i] = tmp;
375 }
376
377 dac->constraints_rates.count = index;
378 dac->constraints_rates.list = dac->rates;
379 dac->constraints_rates.mask = 0;
380 dac->rates_num = index;
381
382 retval = 0;
383 }
384
385 return retval;
386}
387
388static int __devinit atmel_abdac_probe(struct platform_device *pdev)
389{
390 struct snd_card *card;
391 struct atmel_abdac *dac;
392 struct resource *regs;
393 struct atmel_abdac_pdata *pdata;
394 struct clk *pclk;
395 struct clk *sample_clk;
396 int retval;
397 int irq;
398
399 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
400 if (!regs) {
401 dev_dbg(&pdev->dev, "no memory resource\n");
402 return -ENXIO;
403 }
404
405 irq = platform_get_irq(pdev, 0);
406 if (irq < 0) {
407 dev_dbg(&pdev->dev, "could not get IRQ number\n");
408 return irq;
409 }
410
411 pdata = pdev->dev.platform_data;
412 if (!pdata) {
413 dev_dbg(&pdev->dev, "no platform data\n");
414 return -ENXIO;
415 }
416
417 pclk = clk_get(&pdev->dev, "pclk");
418 if (IS_ERR(pclk)) {
419 dev_dbg(&pdev->dev, "no peripheral clock\n");
420 return PTR_ERR(pclk);
421 }
422 sample_clk = clk_get(&pdev->dev, "sample_clk");
423 if (IS_ERR(pclk)) {
424 dev_dbg(&pdev->dev, "no sample clock\n");
425 retval = PTR_ERR(pclk);
426 goto out_put_pclk;
427 }
428 clk_enable(pclk);
429
430 retval = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
431 THIS_MODULE, sizeof(struct atmel_abdac), &card);
432 if (retval) {
433 dev_dbg(&pdev->dev, "could not create sound card device\n");
434 goto out_put_sample_clk;
435 }
436
437 dac = get_dac(card);
438
439 dac->irq = irq;
440 dac->card = card;
441 dac->pclk = pclk;
442 dac->sample_clk = sample_clk;
443 dac->pdev = pdev;
444
445 retval = set_sample_rates(dac);
446 if (retval < 0) {
447 dev_dbg(&pdev->dev, "could not set supported rates\n");
448 goto out_free_card;
449 }
450
451 dac->regs = ioremap(regs->start, regs->end - regs->start + 1);
452 if (!dac->regs) {
453 dev_dbg(&pdev->dev, "could not remap register memory\n");
454 goto out_free_card;
455 }
456
457 /* make sure the DAC is silent and disabled */
458 dac_writel(dac, DATA, 0);
459 dac_writel(dac, CTRL, 0);
460
461 retval = request_irq(irq, abdac_interrupt, 0, "abdac", dac);
462 if (retval) {
463 dev_dbg(&pdev->dev, "could not request irq\n");
464 goto out_unmap_regs;
465 }
466
467 snd_card_set_dev(card, &pdev->dev);
468
469 if (pdata->dws.dma_dev) {
470 struct dw_dma_slave *dws = &pdata->dws;
471 dma_cap_mask_t mask;
472
473 dws->tx_reg = regs->start + DAC_DATA;
474
475 dma_cap_zero(mask);
476 dma_cap_set(DMA_SLAVE, mask);
477
478 dac->dma.chan = dma_request_channel(mask, filter, dws);
479 }
480 if (!pdata->dws.dma_dev || !dac->dma.chan) {
481 dev_dbg(&pdev->dev, "DMA not available\n");
482 retval = -ENODEV;
483 goto out_unset_card_dev;
484 }
485
486 strcpy(card->driver, "Atmel ABDAC");
487 strcpy(card->shortname, "Atmel ABDAC");
488 sprintf(card->longname, "Atmel Audio Bitstream DAC");
489
490 retval = atmel_abdac_pcm_new(dac);
491 if (retval) {
492 dev_dbg(&pdev->dev, "could not register ABDAC pcm device\n");
493 goto out_release_dma;
494 }
495
496 retval = snd_card_register(card);
497 if (retval) {
498 dev_dbg(&pdev->dev, "could not register sound card\n");
499 goto out_release_dma;
500 }
501
502 platform_set_drvdata(pdev, card);
503
504 dev_info(&pdev->dev, "Atmel ABDAC at 0x%p using %s\n",
505 dac->regs, dev_name(&dac->dma.chan->dev->device));
506
507 return retval;
508
509out_release_dma:
510 dma_release_channel(dac->dma.chan);
511 dac->dma.chan = NULL;
512out_unset_card_dev:
513 snd_card_set_dev(card, NULL);
514 free_irq(irq, dac);
515out_unmap_regs:
516 iounmap(dac->regs);
517out_free_card:
518 snd_card_free(card);
519out_put_sample_clk:
520 clk_put(sample_clk);
521 clk_disable(pclk);
522out_put_pclk:
523 clk_put(pclk);
524 return retval;
525}
526
527#ifdef CONFIG_PM
528static int atmel_abdac_suspend(struct platform_device *pdev, pm_message_t msg)
529{
530 struct snd_card *card = platform_get_drvdata(pdev);
531 struct atmel_abdac *dac = card->private_data;
532
533 dw_dma_cyclic_stop(dac->dma.chan);
534 clk_disable(dac->sample_clk);
535 clk_disable(dac->pclk);
536
537 return 0;
538}
539
540static int atmel_abdac_resume(struct platform_device *pdev)
541{
542 struct snd_card *card = platform_get_drvdata(pdev);
543 struct atmel_abdac *dac = card->private_data;
544
545 clk_enable(dac->pclk);
546 clk_enable(dac->sample_clk);
547 if (test_bit(DMA_READY, &dac->flags))
548 dw_dma_cyclic_start(dac->dma.chan);
549
550 return 0;
551}
552#else
553#define atmel_abdac_suspend NULL
554#define atmel_abdac_resume NULL
555#endif
556
557static int __devexit atmel_abdac_remove(struct platform_device *pdev)
558{
559 struct snd_card *card = platform_get_drvdata(pdev);
560 struct atmel_abdac *dac = get_dac(card);
561
562 clk_put(dac->sample_clk);
563 clk_disable(dac->pclk);
564 clk_put(dac->pclk);
565
566 dma_release_channel(dac->dma.chan);
567 dac->dma.chan = NULL;
568 snd_card_set_dev(card, NULL);
569 iounmap(dac->regs);
570 free_irq(dac->irq, dac);
571 snd_card_free(card);
572
573 platform_set_drvdata(pdev, NULL);
574
575 return 0;
576}
577
578static struct platform_driver atmel_abdac_driver = {
579 .remove = __devexit_p(atmel_abdac_remove),
580 .driver = {
581 .name = "atmel_abdac",
582 },
583 .suspend = atmel_abdac_suspend,
584 .resume = atmel_abdac_resume,
585};
586
587static int __init atmel_abdac_init(void)
588{
589 return platform_driver_probe(&atmel_abdac_driver,
590 atmel_abdac_probe);
591}
592module_init(atmel_abdac_init);
593
594static void __exit atmel_abdac_exit(void)
595{
596 platform_driver_unregister(&atmel_abdac_driver);
597}
598module_exit(atmel_abdac_exit);
599
600MODULE_LICENSE("GPL");
601MODULE_DESCRIPTION("Driver for Atmel Audio Bitstream DAC (ABDAC)");
602MODULE_AUTHOR("Hans-Christian Egtvedt <hans-christian.egtvedt@atmel.com>");
diff --git a/sound/atmel/ac97c.c b/sound/atmel/ac97c.c
new file mode 100644
index 000000000000..0c0f8771656a
--- /dev/null
+++ b/sound/atmel/ac97c.c
@@ -0,0 +1,1022 @@
1/*
2 * Driver for Atmel AC97C
3 *
4 * Copyright (C) 2005-2009 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 */
10#include <linux/clk.h>
11#include <linux/delay.h>
12#include <linux/bitmap.h>
13#include <linux/device.h>
14#include <linux/dmaengine.h>
15#include <linux/dma-mapping.h>
16#include <linux/init.h>
17#include <linux/interrupt.h>
18#include <linux/module.h>
19#include <linux/platform_device.h>
20#include <linux/mutex.h>
21#include <linux/gpio.h>
22#include <linux/io.h>
23
24#include <sound/core.h>
25#include <sound/initval.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include <sound/ac97_codec.h>
29#include <sound/atmel-ac97c.h>
30#include <sound/memalloc.h>
31
32#include <linux/dw_dmac.h>
33
34#include "ac97c.h"
35
36enum {
37 DMA_TX_READY = 0,
38 DMA_RX_READY,
39 DMA_TX_CHAN_PRESENT,
40 DMA_RX_CHAN_PRESENT,
41};
42
43/* Serialize access to opened variable */
44static DEFINE_MUTEX(opened_mutex);
45
46struct atmel_ac97c_dma {
47 struct dma_chan *rx_chan;
48 struct dma_chan *tx_chan;
49};
50
51struct atmel_ac97c {
52 struct clk *pclk;
53 struct platform_device *pdev;
54 struct atmel_ac97c_dma dma;
55
56 struct snd_pcm_substream *playback_substream;
57 struct snd_pcm_substream *capture_substream;
58 struct snd_card *card;
59 struct snd_pcm *pcm;
60 struct snd_ac97 *ac97;
61 struct snd_ac97_bus *ac97_bus;
62
63 u64 cur_format;
64 unsigned int cur_rate;
65 unsigned long flags;
66 /* Serialize access to opened variable */
67 spinlock_t lock;
68 void __iomem *regs;
69 int irq;
70 int opened;
71 int reset_pin;
72};
73
74#define get_chip(card) ((struct atmel_ac97c *)(card)->private_data)
75
76#define ac97c_writel(chip, reg, val) \
77 __raw_writel((val), (chip)->regs + AC97C_##reg)
78#define ac97c_readl(chip, reg) \
79 __raw_readl((chip)->regs + AC97C_##reg)
80
81/* This function is called by the DMA driver. */
82static void atmel_ac97c_dma_playback_period_done(void *arg)
83{
84 struct atmel_ac97c *chip = arg;
85 snd_pcm_period_elapsed(chip->playback_substream);
86}
87
88static void atmel_ac97c_dma_capture_period_done(void *arg)
89{
90 struct atmel_ac97c *chip = arg;
91 snd_pcm_period_elapsed(chip->capture_substream);
92}
93
94static int atmel_ac97c_prepare_dma(struct atmel_ac97c *chip,
95 struct snd_pcm_substream *substream,
96 enum dma_data_direction direction)
97{
98 struct dma_chan *chan;
99 struct dw_cyclic_desc *cdesc;
100 struct snd_pcm_runtime *runtime = substream->runtime;
101 unsigned long buffer_len, period_len;
102
103 /*
104 * We don't do DMA on "complex" transfers, i.e. with
105 * non-halfword-aligned buffers or lengths.
106 */
107 if (runtime->dma_addr & 1 || runtime->buffer_size & 1) {
108 dev_dbg(&chip->pdev->dev, "too complex transfer\n");
109 return -EINVAL;
110 }
111
112 if (direction == DMA_TO_DEVICE)
113 chan = chip->dma.tx_chan;
114 else
115 chan = chip->dma.rx_chan;
116
117 buffer_len = frames_to_bytes(runtime, runtime->buffer_size);
118 period_len = frames_to_bytes(runtime, runtime->period_size);
119
120 cdesc = dw_dma_cyclic_prep(chan, runtime->dma_addr, buffer_len,
121 period_len, direction);
122 if (IS_ERR(cdesc)) {
123 dev_dbg(&chip->pdev->dev, "could not prepare cyclic DMA\n");
124 return PTR_ERR(cdesc);
125 }
126
127 if (direction == DMA_TO_DEVICE) {
128 cdesc->period_callback = atmel_ac97c_dma_playback_period_done;
129 set_bit(DMA_TX_READY, &chip->flags);
130 } else {
131 cdesc->period_callback = atmel_ac97c_dma_capture_period_done;
132 set_bit(DMA_RX_READY, &chip->flags);
133 }
134
135 cdesc->period_callback_param = chip;
136
137 return 0;
138}
139
140static struct snd_pcm_hardware atmel_ac97c_hw = {
141 .info = (SNDRV_PCM_INFO_MMAP
142 | SNDRV_PCM_INFO_MMAP_VALID
143 | SNDRV_PCM_INFO_INTERLEAVED
144 | SNDRV_PCM_INFO_BLOCK_TRANSFER
145 | SNDRV_PCM_INFO_JOINT_DUPLEX
146 | SNDRV_PCM_INFO_RESUME
147 | SNDRV_PCM_INFO_PAUSE),
148 .formats = (SNDRV_PCM_FMTBIT_S16_BE
149 | SNDRV_PCM_FMTBIT_S16_LE),
150 .rates = (SNDRV_PCM_RATE_CONTINUOUS),
151 .rate_min = 4000,
152 .rate_max = 48000,
153 .channels_min = 1,
154 .channels_max = 2,
155 .buffer_bytes_max = 2 * 2 * 64 * 2048,
156 .period_bytes_min = 4096,
157 .period_bytes_max = 4096,
158 .periods_min = 6,
159 .periods_max = 64,
160};
161
162static int atmel_ac97c_playback_open(struct snd_pcm_substream *substream)
163{
164 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
165 struct snd_pcm_runtime *runtime = substream->runtime;
166
167 mutex_lock(&opened_mutex);
168 chip->opened++;
169 runtime->hw = atmel_ac97c_hw;
170 if (chip->cur_rate) {
171 runtime->hw.rate_min = chip->cur_rate;
172 runtime->hw.rate_max = chip->cur_rate;
173 }
174 if (chip->cur_format)
175 runtime->hw.formats = (1ULL << chip->cur_format);
176 mutex_unlock(&opened_mutex);
177 chip->playback_substream = substream;
178 return 0;
179}
180
181static int atmel_ac97c_capture_open(struct snd_pcm_substream *substream)
182{
183 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
184 struct snd_pcm_runtime *runtime = substream->runtime;
185
186 mutex_lock(&opened_mutex);
187 chip->opened++;
188 runtime->hw = atmel_ac97c_hw;
189 if (chip->cur_rate) {
190 runtime->hw.rate_min = chip->cur_rate;
191 runtime->hw.rate_max = chip->cur_rate;
192 }
193 if (chip->cur_format)
194 runtime->hw.formats = (1ULL << chip->cur_format);
195 mutex_unlock(&opened_mutex);
196 chip->capture_substream = substream;
197 return 0;
198}
199
200static int atmel_ac97c_playback_close(struct snd_pcm_substream *substream)
201{
202 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
203
204 mutex_lock(&opened_mutex);
205 chip->opened--;
206 if (!chip->opened) {
207 chip->cur_rate = 0;
208 chip->cur_format = 0;
209 }
210 mutex_unlock(&opened_mutex);
211
212 chip->playback_substream = NULL;
213
214 return 0;
215}
216
217static int atmel_ac97c_capture_close(struct snd_pcm_substream *substream)
218{
219 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
220
221 mutex_lock(&opened_mutex);
222 chip->opened--;
223 if (!chip->opened) {
224 chip->cur_rate = 0;
225 chip->cur_format = 0;
226 }
227 mutex_unlock(&opened_mutex);
228
229 chip->capture_substream = NULL;
230
231 return 0;
232}
233
234static int atmel_ac97c_playback_hw_params(struct snd_pcm_substream *substream,
235 struct snd_pcm_hw_params *hw_params)
236{
237 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
238 int retval;
239
240 retval = snd_pcm_lib_malloc_pages(substream,
241 params_buffer_bytes(hw_params));
242 if (retval < 0)
243 return retval;
244 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
245 if (retval == 1)
246 if (test_and_clear_bit(DMA_TX_READY, &chip->flags))
247 dw_dma_cyclic_free(chip->dma.tx_chan);
248
249 /* Set restrictions to params. */
250 mutex_lock(&opened_mutex);
251 chip->cur_rate = params_rate(hw_params);
252 chip->cur_format = params_format(hw_params);
253 mutex_unlock(&opened_mutex);
254
255 return retval;
256}
257
258static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream *substream,
259 struct snd_pcm_hw_params *hw_params)
260{
261 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
262 int retval;
263
264 retval = snd_pcm_lib_malloc_pages(substream,
265 params_buffer_bytes(hw_params));
266 if (retval < 0)
267 return retval;
268 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
269 if (retval == 1)
270 if (test_and_clear_bit(DMA_RX_READY, &chip->flags))
271 dw_dma_cyclic_free(chip->dma.rx_chan);
272
273 /* Set restrictions to params. */
274 mutex_lock(&opened_mutex);
275 chip->cur_rate = params_rate(hw_params);
276 chip->cur_format = params_format(hw_params);
277 mutex_unlock(&opened_mutex);
278
279 return retval;
280}
281
282static int atmel_ac97c_playback_hw_free(struct snd_pcm_substream *substream)
283{
284 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
285 if (test_and_clear_bit(DMA_TX_READY, &chip->flags))
286 dw_dma_cyclic_free(chip->dma.tx_chan);
287 return snd_pcm_lib_free_pages(substream);
288}
289
290static int atmel_ac97c_capture_hw_free(struct snd_pcm_substream *substream)
291{
292 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
293 if (test_and_clear_bit(DMA_RX_READY, &chip->flags))
294 dw_dma_cyclic_free(chip->dma.rx_chan);
295 return snd_pcm_lib_free_pages(substream);
296}
297
298static int atmel_ac97c_playback_prepare(struct snd_pcm_substream *substream)
299{
300 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
301 struct snd_pcm_runtime *runtime = substream->runtime;
302 unsigned long word = ac97c_readl(chip, OCA);
303 int retval;
304
305 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
306
307 /* assign channels to AC97C channel A */
308 switch (runtime->channels) {
309 case 1:
310 word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
311 break;
312 case 2:
313 word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
314 | AC97C_CH_ASSIGN(PCM_RIGHT, A);
315 break;
316 default:
317 /* TODO: support more than two channels */
318 return -EINVAL;
319 }
320 ac97c_writel(chip, OCA, word);
321
322 /* configure sample format and size */
323 word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
324
325 switch (runtime->format) {
326 case SNDRV_PCM_FORMAT_S16_LE:
327 word |= AC97C_CMR_CEM_LITTLE;
328 break;
329 case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
330 word &= ~(AC97C_CMR_CEM_LITTLE);
331 break;
332 default:
333 word = ac97c_readl(chip, OCA);
334 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
335 ac97c_writel(chip, OCA, word);
336 return -EINVAL;
337 }
338
339 /* Enable underrun interrupt on channel A */
340 word |= AC97C_CSR_UNRUN;
341
342 ac97c_writel(chip, CAMR, word);
343
344 /* Enable channel A event interrupt */
345 word = ac97c_readl(chip, IMR);
346 word |= AC97C_SR_CAEVT;
347 ac97c_writel(chip, IER, word);
348
349 /* set variable rate if needed */
350 if (runtime->rate != 48000) {
351 word = ac97c_readl(chip, MR);
352 word |= AC97C_MR_VRA;
353 ac97c_writel(chip, MR, word);
354 } else {
355 word = ac97c_readl(chip, MR);
356 word &= ~(AC97C_MR_VRA);
357 ac97c_writel(chip, MR, word);
358 }
359
360 retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
361 runtime->rate);
362 if (retval)
363 dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
364 runtime->rate);
365
366 if (!test_bit(DMA_TX_READY, &chip->flags))
367 retval = atmel_ac97c_prepare_dma(chip, substream,
368 DMA_TO_DEVICE);
369
370 return retval;
371}
372
373static int atmel_ac97c_capture_prepare(struct snd_pcm_substream *substream)
374{
375 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
376 struct snd_pcm_runtime *runtime = substream->runtime;
377 unsigned long word = ac97c_readl(chip, ICA);
378 int retval;
379
380 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
381
382 /* assign channels to AC97C channel A */
383 switch (runtime->channels) {
384 case 1:
385 word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
386 break;
387 case 2:
388 word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
389 | AC97C_CH_ASSIGN(PCM_RIGHT, A);
390 break;
391 default:
392 /* TODO: support more than two channels */
393 return -EINVAL;
394 }
395 ac97c_writel(chip, ICA, word);
396
397 /* configure sample format and size */
398 word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
399
400 switch (runtime->format) {
401 case SNDRV_PCM_FORMAT_S16_LE:
402 word |= AC97C_CMR_CEM_LITTLE;
403 break;
404 case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
405 word &= ~(AC97C_CMR_CEM_LITTLE);
406 break;
407 default:
408 word = ac97c_readl(chip, ICA);
409 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
410 ac97c_writel(chip, ICA, word);
411 return -EINVAL;
412 }
413
414 /* Enable overrun interrupt on channel A */
415 word |= AC97C_CSR_OVRUN;
416
417 ac97c_writel(chip, CAMR, word);
418
419 /* Enable channel A event interrupt */
420 word = ac97c_readl(chip, IMR);
421 word |= AC97C_SR_CAEVT;
422 ac97c_writel(chip, IER, word);
423
424 /* set variable rate if needed */
425 if (runtime->rate != 48000) {
426 word = ac97c_readl(chip, MR);
427 word |= AC97C_MR_VRA;
428 ac97c_writel(chip, MR, word);
429 } else {
430 word = ac97c_readl(chip, MR);
431 word &= ~(AC97C_MR_VRA);
432 ac97c_writel(chip, MR, word);
433 }
434
435 retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE,
436 runtime->rate);
437 if (retval)
438 dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
439 runtime->rate);
440
441 if (!test_bit(DMA_RX_READY, &chip->flags))
442 retval = atmel_ac97c_prepare_dma(chip, substream,
443 DMA_FROM_DEVICE);
444
445 return retval;
446}
447
448static int
449atmel_ac97c_playback_trigger(struct snd_pcm_substream *substream, int cmd)
450{
451 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
452 unsigned long camr;
453 int retval = 0;
454
455 camr = ac97c_readl(chip, CAMR);
456
457 switch (cmd) {
458 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
459 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
460 case SNDRV_PCM_TRIGGER_START:
461 retval = dw_dma_cyclic_start(chip->dma.tx_chan);
462 if (retval)
463 goto out;
464 camr |= AC97C_CMR_CENA;
465 break;
466 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
467 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
468 case SNDRV_PCM_TRIGGER_STOP:
469 dw_dma_cyclic_stop(chip->dma.tx_chan);
470 if (chip->opened <= 1)
471 camr &= ~AC97C_CMR_CENA;
472 break;
473 default:
474 retval = -EINVAL;
475 goto out;
476 }
477
478 ac97c_writel(chip, CAMR, camr);
479out:
480 return retval;
481}
482
483static int
484atmel_ac97c_capture_trigger(struct snd_pcm_substream *substream, int cmd)
485{
486 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
487 unsigned long camr;
488 int retval = 0;
489
490 camr = ac97c_readl(chip, CAMR);
491
492 switch (cmd) {
493 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
494 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
495 case SNDRV_PCM_TRIGGER_START:
496 retval = dw_dma_cyclic_start(chip->dma.rx_chan);
497 if (retval)
498 goto out;
499 camr |= AC97C_CMR_CENA;
500 break;
501 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
502 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
503 case SNDRV_PCM_TRIGGER_STOP:
504 dw_dma_cyclic_stop(chip->dma.rx_chan);
505 if (chip->opened <= 1)
506 camr &= ~AC97C_CMR_CENA;
507 break;
508 default:
509 retval = -EINVAL;
510 break;
511 }
512
513 ac97c_writel(chip, CAMR, camr);
514out:
515 return retval;
516}
517
518static snd_pcm_uframes_t
519atmel_ac97c_playback_pointer(struct snd_pcm_substream *substream)
520{
521 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
522 struct snd_pcm_runtime *runtime = substream->runtime;
523 snd_pcm_uframes_t frames;
524 unsigned long bytes;
525
526 bytes = dw_dma_get_src_addr(chip->dma.tx_chan);
527 bytes -= runtime->dma_addr;
528
529 frames = bytes_to_frames(runtime, bytes);
530 if (frames >= runtime->buffer_size)
531 frames -= runtime->buffer_size;
532 return frames;
533}
534
535static snd_pcm_uframes_t
536atmel_ac97c_capture_pointer(struct snd_pcm_substream *substream)
537{
538 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
539 struct snd_pcm_runtime *runtime = substream->runtime;
540 snd_pcm_uframes_t frames;
541 unsigned long bytes;
542
543 bytes = dw_dma_get_dst_addr(chip->dma.rx_chan);
544 bytes -= runtime->dma_addr;
545
546 frames = bytes_to_frames(runtime, bytes);
547 if (frames >= runtime->buffer_size)
548 frames -= runtime->buffer_size;
549 return frames;
550}
551
552static struct snd_pcm_ops atmel_ac97_playback_ops = {
553 .open = atmel_ac97c_playback_open,
554 .close = atmel_ac97c_playback_close,
555 .ioctl = snd_pcm_lib_ioctl,
556 .hw_params = atmel_ac97c_playback_hw_params,
557 .hw_free = atmel_ac97c_playback_hw_free,
558 .prepare = atmel_ac97c_playback_prepare,
559 .trigger = atmel_ac97c_playback_trigger,
560 .pointer = atmel_ac97c_playback_pointer,
561};
562
563static struct snd_pcm_ops atmel_ac97_capture_ops = {
564 .open = atmel_ac97c_capture_open,
565 .close = atmel_ac97c_capture_close,
566 .ioctl = snd_pcm_lib_ioctl,
567 .hw_params = atmel_ac97c_capture_hw_params,
568 .hw_free = atmel_ac97c_capture_hw_free,
569 .prepare = atmel_ac97c_capture_prepare,
570 .trigger = atmel_ac97c_capture_trigger,
571 .pointer = atmel_ac97c_capture_pointer,
572};
573
574static irqreturn_t atmel_ac97c_interrupt(int irq, void *dev)
575{
576 struct atmel_ac97c *chip = (struct atmel_ac97c *)dev;
577 irqreturn_t retval = IRQ_NONE;
578 u32 sr = ac97c_readl(chip, SR);
579 u32 casr = ac97c_readl(chip, CASR);
580 u32 cosr = ac97c_readl(chip, COSR);
581
582 if (sr & AC97C_SR_CAEVT) {
583 dev_info(&chip->pdev->dev, "channel A event%s%s%s%s%s%s\n",
584 casr & AC97C_CSR_OVRUN ? " OVRUN" : "",
585 casr & AC97C_CSR_RXRDY ? " RXRDY" : "",
586 casr & AC97C_CSR_UNRUN ? " UNRUN" : "",
587 casr & AC97C_CSR_TXEMPTY ? " TXEMPTY" : "",
588 casr & AC97C_CSR_TXRDY ? " TXRDY" : "",
589 !casr ? " NONE" : "");
590 retval = IRQ_HANDLED;
591 }
592
593 if (sr & AC97C_SR_COEVT) {
594 dev_info(&chip->pdev->dev, "codec channel event%s%s%s%s%s\n",
595 cosr & AC97C_CSR_OVRUN ? " OVRUN" : "",
596 cosr & AC97C_CSR_RXRDY ? " RXRDY" : "",
597 cosr & AC97C_CSR_TXEMPTY ? " TXEMPTY" : "",
598 cosr & AC97C_CSR_TXRDY ? " TXRDY" : "",
599 !cosr ? " NONE" : "");
600 retval = IRQ_HANDLED;
601 }
602
603 if (retval == IRQ_NONE) {
604 dev_err(&chip->pdev->dev, "spurious interrupt sr 0x%08x "
605 "casr 0x%08x cosr 0x%08x\n", sr, casr, cosr);
606 }
607
608 return retval;
609}
610
611static int __devinit atmel_ac97c_pcm_new(struct atmel_ac97c *chip)
612{
613 struct snd_pcm *pcm;
614 struct snd_pcm_hardware hw = atmel_ac97c_hw;
615 int capture, playback, retval;
616
617 capture = test_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
618 playback = test_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
619
620 retval = snd_pcm_new(chip->card, chip->card->shortname,
621 chip->pdev->id, playback, capture, &pcm);
622 if (retval)
623 return retval;
624
625 if (capture)
626 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
627 &atmel_ac97_capture_ops);
628 if (playback)
629 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
630 &atmel_ac97_playback_ops);
631
632 retval = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
633 &chip->pdev->dev, hw.periods_min * hw.period_bytes_min,
634 hw.buffer_bytes_max);
635 if (retval)
636 return retval;
637
638 pcm->private_data = chip;
639 pcm->info_flags = 0;
640 strcpy(pcm->name, chip->card->shortname);
641 chip->pcm = pcm;
642
643 return 0;
644}
645
646static int atmel_ac97c_mixer_new(struct atmel_ac97c *chip)
647{
648 struct snd_ac97_template template;
649 memset(&template, 0, sizeof(template));
650 template.private_data = chip;
651 return snd_ac97_mixer(chip->ac97_bus, &template, &chip->ac97);
652}
653
654static void atmel_ac97c_write(struct snd_ac97 *ac97, unsigned short reg,
655 unsigned short val)
656{
657 struct atmel_ac97c *chip = get_chip(ac97);
658 unsigned long word;
659 int timeout = 40;
660
661 word = (reg & 0x7f) << 16 | val;
662
663 do {
664 if (ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) {
665 ac97c_writel(chip, COTHR, word);
666 return;
667 }
668 udelay(1);
669 } while (--timeout);
670
671 dev_dbg(&chip->pdev->dev, "codec write timeout\n");
672}
673
674static unsigned short atmel_ac97c_read(struct snd_ac97 *ac97,
675 unsigned short reg)
676{
677 struct atmel_ac97c *chip = get_chip(ac97);
678 unsigned long word;
679 int timeout = 40;
680 int write = 10;
681
682 word = (0x80 | (reg & 0x7f)) << 16;
683
684 if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0)
685 ac97c_readl(chip, CORHR);
686
687retry_write:
688 timeout = 40;
689
690 do {
691 if ((ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) != 0) {
692 ac97c_writel(chip, COTHR, word);
693 goto read_reg;
694 }
695 udelay(10);
696 } while (--timeout);
697
698 if (!--write)
699 goto timed_out;
700 goto retry_write;
701
702read_reg:
703 do {
704 if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0) {
705 unsigned short val = ac97c_readl(chip, CORHR);
706 return val;
707 }
708 udelay(10);
709 } while (--timeout);
710
711 if (!--write)
712 goto timed_out;
713 goto retry_write;
714
715timed_out:
716 dev_dbg(&chip->pdev->dev, "codec read timeout\n");
717 return 0xffff;
718}
719
720static bool filter(struct dma_chan *chan, void *slave)
721{
722 struct dw_dma_slave *dws = slave;
723
724 if (dws->dma_dev == chan->device->dev) {
725 chan->private = dws;
726 return true;
727 } else
728 return false;
729}
730
731static void atmel_ac97c_reset(struct atmel_ac97c *chip)
732{
733 ac97c_writel(chip, MR, 0);
734 ac97c_writel(chip, MR, AC97C_MR_ENA);
735 ac97c_writel(chip, CAMR, 0);
736 ac97c_writel(chip, COMR, 0);
737
738 if (gpio_is_valid(chip->reset_pin)) {
739 gpio_set_value(chip->reset_pin, 0);
740 /* AC97 v2.2 specifications says minimum 1 us. */
741 udelay(2);
742 gpio_set_value(chip->reset_pin, 1);
743 }
744}
745
746static int __devinit atmel_ac97c_probe(struct platform_device *pdev)
747{
748 struct snd_card *card;
749 struct atmel_ac97c *chip;
750 struct resource *regs;
751 struct ac97c_platform_data *pdata;
752 struct clk *pclk;
753 static struct snd_ac97_bus_ops ops = {
754 .write = atmel_ac97c_write,
755 .read = atmel_ac97c_read,
756 };
757 int retval;
758 int irq;
759
760 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
761 if (!regs) {
762 dev_dbg(&pdev->dev, "no memory resource\n");
763 return -ENXIO;
764 }
765
766 pdata = pdev->dev.platform_data;
767 if (!pdata) {
768 dev_dbg(&pdev->dev, "no platform data\n");
769 return -ENXIO;
770 }
771
772 irq = platform_get_irq(pdev, 0);
773 if (irq < 0) {
774 dev_dbg(&pdev->dev, "could not get irq\n");
775 return -ENXIO;
776 }
777
778 pclk = clk_get(&pdev->dev, "pclk");
779 if (IS_ERR(pclk)) {
780 dev_dbg(&pdev->dev, "no peripheral clock\n");
781 return PTR_ERR(pclk);
782 }
783 clk_enable(pclk);
784
785 retval = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
786 THIS_MODULE, sizeof(struct atmel_ac97c), &card);
787 if (retval) {
788 dev_dbg(&pdev->dev, "could not create sound card device\n");
789 goto err_snd_card_new;
790 }
791
792 chip = get_chip(card);
793
794 retval = request_irq(irq, atmel_ac97c_interrupt, 0, "AC97C", chip);
795 if (retval) {
796 dev_dbg(&pdev->dev, "unable to request irq %d\n", irq);
797 goto err_request_irq;
798 }
799 chip->irq = irq;
800
801 spin_lock_init(&chip->lock);
802
803 strcpy(card->driver, "Atmel AC97C");
804 strcpy(card->shortname, "Atmel AC97C");
805 sprintf(card->longname, "Atmel AC97 controller");
806
807 chip->card = card;
808 chip->pclk = pclk;
809 chip->pdev = pdev;
810 chip->regs = ioremap(regs->start, regs->end - regs->start + 1);
811
812 if (!chip->regs) {
813 dev_dbg(&pdev->dev, "could not remap register memory\n");
814 goto err_ioremap;
815 }
816
817 if (gpio_is_valid(pdata->reset_pin)) {
818 if (gpio_request(pdata->reset_pin, "reset_pin")) {
819 dev_dbg(&pdev->dev, "reset pin not available\n");
820 chip->reset_pin = -ENODEV;
821 } else {
822 gpio_direction_output(pdata->reset_pin, 1);
823 chip->reset_pin = pdata->reset_pin;
824 }
825 }
826
827 snd_card_set_dev(card, &pdev->dev);
828
829 atmel_ac97c_reset(chip);
830
831 /* Enable overrun interrupt from codec channel */
832 ac97c_writel(chip, COMR, AC97C_CSR_OVRUN);
833 ac97c_writel(chip, IER, ac97c_readl(chip, IMR) | AC97C_SR_COEVT);
834
835 retval = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus);
836 if (retval) {
837 dev_dbg(&pdev->dev, "could not register on ac97 bus\n");
838 goto err_ac97_bus;
839 }
840
841 retval = atmel_ac97c_mixer_new(chip);
842 if (retval) {
843 dev_dbg(&pdev->dev, "could not register ac97 mixer\n");
844 goto err_ac97_bus;
845 }
846
847 if (pdata->rx_dws.dma_dev) {
848 struct dw_dma_slave *dws = &pdata->rx_dws;
849 dma_cap_mask_t mask;
850
851 dws->rx_reg = regs->start + AC97C_CARHR + 2;
852
853 dma_cap_zero(mask);
854 dma_cap_set(DMA_SLAVE, mask);
855
856 chip->dma.rx_chan = dma_request_channel(mask, filter, dws);
857
858 dev_info(&chip->pdev->dev, "using %s for DMA RX\n",
859 dev_name(&chip->dma.rx_chan->dev->device));
860 set_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
861 }
862
863 if (pdata->tx_dws.dma_dev) {
864 struct dw_dma_slave *dws = &pdata->tx_dws;
865 dma_cap_mask_t mask;
866
867 dws->tx_reg = regs->start + AC97C_CATHR + 2;
868
869 dma_cap_zero(mask);
870 dma_cap_set(DMA_SLAVE, mask);
871
872 chip->dma.tx_chan = dma_request_channel(mask, filter, dws);
873
874 dev_info(&chip->pdev->dev, "using %s for DMA TX\n",
875 dev_name(&chip->dma.tx_chan->dev->device));
876 set_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
877 }
878
879 if (!test_bit(DMA_RX_CHAN_PRESENT, &chip->flags) &&
880 !test_bit(DMA_TX_CHAN_PRESENT, &chip->flags)) {
881 dev_dbg(&pdev->dev, "DMA not available\n");
882 retval = -ENODEV;
883 goto err_dma;
884 }
885
886 retval = atmel_ac97c_pcm_new(chip);
887 if (retval) {
888 dev_dbg(&pdev->dev, "could not register ac97 pcm device\n");
889 goto err_dma;
890 }
891
892 retval = snd_card_register(card);
893 if (retval) {
894 dev_dbg(&pdev->dev, "could not register sound card\n");
895 goto err_dma;
896 }
897
898 platform_set_drvdata(pdev, card);
899
900 dev_info(&pdev->dev, "Atmel AC97 controller at 0x%p\n",
901 chip->regs);
902
903 return 0;
904
905err_dma:
906 if (test_bit(DMA_RX_CHAN_PRESENT, &chip->flags))
907 dma_release_channel(chip->dma.rx_chan);
908 if (test_bit(DMA_TX_CHAN_PRESENT, &chip->flags))
909 dma_release_channel(chip->dma.tx_chan);
910 clear_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
911 clear_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
912 chip->dma.rx_chan = NULL;
913 chip->dma.tx_chan = NULL;
914err_ac97_bus:
915 snd_card_set_dev(card, NULL);
916
917 if (gpio_is_valid(chip->reset_pin))
918 gpio_free(chip->reset_pin);
919
920 iounmap(chip->regs);
921err_ioremap:
922 free_irq(irq, chip);
923err_request_irq:
924 snd_card_free(card);
925err_snd_card_new:
926 clk_disable(pclk);
927 clk_put(pclk);
928 return retval;
929}
930
931#ifdef CONFIG_PM
932static int atmel_ac97c_suspend(struct platform_device *pdev, pm_message_t msg)
933{
934 struct snd_card *card = platform_get_drvdata(pdev);
935 struct atmel_ac97c *chip = card->private_data;
936
937 if (test_bit(DMA_RX_READY, &chip->flags))
938 dw_dma_cyclic_stop(chip->dma.rx_chan);
939 if (test_bit(DMA_TX_READY, &chip->flags))
940 dw_dma_cyclic_stop(chip->dma.tx_chan);
941 clk_disable(chip->pclk);
942
943 return 0;
944}
945
946static int atmel_ac97c_resume(struct platform_device *pdev)
947{
948 struct snd_card *card = platform_get_drvdata(pdev);
949 struct atmel_ac97c *chip = card->private_data;
950
951 clk_enable(chip->pclk);
952 if (test_bit(DMA_RX_READY, &chip->flags))
953 dw_dma_cyclic_start(chip->dma.rx_chan);
954 if (test_bit(DMA_TX_READY, &chip->flags))
955 dw_dma_cyclic_start(chip->dma.tx_chan);
956
957 return 0;
958}
959#else
960#define atmel_ac97c_suspend NULL
961#define atmel_ac97c_resume NULL
962#endif
963
964static int __devexit atmel_ac97c_remove(struct platform_device *pdev)
965{
966 struct snd_card *card = platform_get_drvdata(pdev);
967 struct atmel_ac97c *chip = get_chip(card);
968
969 if (gpio_is_valid(chip->reset_pin))
970 gpio_free(chip->reset_pin);
971
972 ac97c_writel(chip, CAMR, 0);
973 ac97c_writel(chip, COMR, 0);
974 ac97c_writel(chip, MR, 0);
975
976 clk_disable(chip->pclk);
977 clk_put(chip->pclk);
978 iounmap(chip->regs);
979 free_irq(chip->irq, chip);
980
981 if (test_bit(DMA_RX_CHAN_PRESENT, &chip->flags))
982 dma_release_channel(chip->dma.rx_chan);
983 if (test_bit(DMA_TX_CHAN_PRESENT, &chip->flags))
984 dma_release_channel(chip->dma.tx_chan);
985 clear_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
986 clear_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
987 chip->dma.rx_chan = NULL;
988 chip->dma.tx_chan = NULL;
989
990 snd_card_set_dev(card, NULL);
991 snd_card_free(card);
992
993 platform_set_drvdata(pdev, NULL);
994
995 return 0;
996}
997
998static struct platform_driver atmel_ac97c_driver = {
999 .remove = __devexit_p(atmel_ac97c_remove),
1000 .driver = {
1001 .name = "atmel_ac97c",
1002 },
1003 .suspend = atmel_ac97c_suspend,
1004 .resume = atmel_ac97c_resume,
1005};
1006
1007static int __init atmel_ac97c_init(void)
1008{
1009 return platform_driver_probe(&atmel_ac97c_driver,
1010 atmel_ac97c_probe);
1011}
1012module_init(atmel_ac97c_init);
1013
1014static void __exit atmel_ac97c_exit(void)
1015{
1016 platform_driver_unregister(&atmel_ac97c_driver);
1017}
1018module_exit(atmel_ac97c_exit);
1019
1020MODULE_LICENSE("GPL");
1021MODULE_DESCRIPTION("Driver for Atmel AC97 controller");
1022MODULE_AUTHOR("Hans-Christian Egtvedt <hans-christian.egtvedt@atmel.com>");
diff --git a/sound/atmel/ac97c.h b/sound/atmel/ac97c.h
new file mode 100644
index 000000000000..ecbba5021c80
--- /dev/null
+++ b/sound/atmel/ac97c.h
@@ -0,0 +1,73 @@
1/*
2 * Register definitions for Atmel AC97C
3 *
4 * Copyright (C) 2005-2009 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 */
10#ifndef __SOUND_ATMEL_AC97C_H
11#define __SOUND_ATMEL_AC97C_H
12
13#define AC97C_MR 0x08
14#define AC97C_ICA 0x10
15#define AC97C_OCA 0x14
16#define AC97C_CARHR 0x20
17#define AC97C_CATHR 0x24
18#define AC97C_CASR 0x28
19#define AC97C_CAMR 0x2c
20#define AC97C_CORHR 0x40
21#define AC97C_COTHR 0x44
22#define AC97C_COSR 0x48
23#define AC97C_COMR 0x4c
24#define AC97C_SR 0x50
25#define AC97C_IER 0x54
26#define AC97C_IDR 0x58
27#define AC97C_IMR 0x5c
28#define AC97C_VERSION 0xfc
29
30#define AC97C_CATPR PDC_TPR
31#define AC97C_CATCR PDC_TCR
32#define AC97C_CATNPR PDC_TNPR
33#define AC97C_CATNCR PDC_TNCR
34#define AC97C_CARPR PDC_RPR
35#define AC97C_CARCR PDC_RCR
36#define AC97C_CARNPR PDC_RNPR
37#define AC97C_CARNCR PDC_RNCR
38#define AC97C_PTCR PDC_PTCR
39
40#define AC97C_MR_ENA (1 << 0)
41#define AC97C_MR_WRST (1 << 1)
42#define AC97C_MR_VRA (1 << 2)
43
44#define AC97C_CSR_TXRDY (1 << 0)
45#define AC97C_CSR_TXEMPTY (1 << 1)
46#define AC97C_CSR_UNRUN (1 << 2)
47#define AC97C_CSR_RXRDY (1 << 4)
48#define AC97C_CSR_OVRUN (1 << 5)
49#define AC97C_CSR_ENDTX (1 << 10)
50#define AC97C_CSR_ENDRX (1 << 14)
51
52#define AC97C_CMR_SIZE_20 (0 << 16)
53#define AC97C_CMR_SIZE_18 (1 << 16)
54#define AC97C_CMR_SIZE_16 (2 << 16)
55#define AC97C_CMR_SIZE_10 (3 << 16)
56#define AC97C_CMR_CEM_LITTLE (1 << 18)
57#define AC97C_CMR_CEM_BIG (0 << 18)
58#define AC97C_CMR_CENA (1 << 21)
59#define AC97C_CMR_DMAEN (1 << 22)
60
61#define AC97C_SR_CAEVT (1 << 3)
62#define AC97C_SR_COEVT (1 << 2)
63#define AC97C_SR_WKUP (1 << 1)
64#define AC97C_SR_SOF (1 << 0)
65
66#define AC97C_CH_MASK(slot) \
67 (0x7 << (3 * (AC97_SLOT_##slot - 3)))
68#define AC97C_CH_ASSIGN(slot, channel) \
69 (AC97C_CHANNEL_##channel << (3 * (AC97_SLOT_##slot - 3)))
70#define AC97C_CHANNEL_NONE 0x0
71#define AC97C_CHANNEL_A 0x1
72
73#endif /* __SOUND_ATMEL_AC97C_H */