Revert "microblaze_mmu_v2: Update signal returning address"
[linux/fpc-iii.git] / sound / atmel / abdac.c
blob277ebce23a452be6cb027590eb997c278971eba8
1 /*
2 * Driver for the Atmel on-chip Audio Bitstream DAC (ABDAC)
4 * Copyright (C) 2006-2009 Atmel Corporation
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/types.h>
20 #include <linux/io.h>
22 #include <sound/core.h>
23 #include <sound/initval.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/atmel-abdac.h>
28 /* DAC register offsets */
29 #define DAC_DATA 0x0000
30 #define DAC_CTRL 0x0008
31 #define DAC_INT_MASK 0x000c
32 #define DAC_INT_EN 0x0010
33 #define DAC_INT_DIS 0x0014
34 #define DAC_INT_CLR 0x0018
35 #define DAC_INT_STATUS 0x001c
37 /* Bitfields in CTRL */
38 #define DAC_SWAP_OFFSET 30
39 #define DAC_SWAP_SIZE 1
40 #define DAC_EN_OFFSET 31
41 #define DAC_EN_SIZE 1
43 /* Bitfields in INT_MASK/INT_EN/INT_DIS/INT_STATUS/INT_CLR */
44 #define DAC_UNDERRUN_OFFSET 28
45 #define DAC_UNDERRUN_SIZE 1
46 #define DAC_TX_READY_OFFSET 29
47 #define DAC_TX_READY_SIZE 1
49 /* Bit manipulation macros */
50 #define DAC_BIT(name) \
51 (1 << DAC_##name##_OFFSET)
52 #define DAC_BF(name, value) \
53 (((value) & ((1 << DAC_##name##_SIZE) - 1)) \
54 << DAC_##name##_OFFSET)
55 #define DAC_BFEXT(name, value) \
56 (((value) >> DAC_##name##_OFFSET) \
57 & ((1 << DAC_##name##_SIZE) - 1))
58 #define DAC_BFINS(name, value, old) \
59 (((old) & ~(((1 << DAC_##name##_SIZE) - 1) \
60 << DAC_##name##_OFFSET)) \
61 | DAC_BF(name, value))
63 /* Register access macros */
64 #define dac_readl(port, reg) \
65 __raw_readl((port)->regs + DAC_##reg)
66 #define dac_writel(port, reg, value) \
67 __raw_writel((value), (port)->regs + DAC_##reg)
70 * ABDAC supports a maximum of 6 different rates from a generic clock. The
71 * generic clock has a power of two divider, which gives 6 steps from 192 kHz
72 * to 5112 Hz.
74 #define MAX_NUM_RATES 6
75 /* ALSA seems to use rates between 192000 Hz and 5112 Hz. */
76 #define RATE_MAX 192000
77 #define RATE_MIN 5112
79 enum {
80 DMA_READY = 0,
83 struct atmel_abdac_dma {
84 struct dma_chan *chan;
85 struct dw_cyclic_desc *cdesc;
88 struct atmel_abdac {
89 struct clk *pclk;
90 struct clk *sample_clk;
91 struct platform_device *pdev;
92 struct atmel_abdac_dma dma;
94 struct snd_pcm_hw_constraint_list constraints_rates;
95 struct snd_pcm_substream *substream;
96 struct snd_card *card;
97 struct snd_pcm *pcm;
99 void __iomem *regs;
100 unsigned long flags;
101 unsigned int rates[MAX_NUM_RATES];
102 unsigned int rates_num;
103 int irq;
106 #define get_dac(card) ((struct atmel_abdac *)(card)->private_data)
108 /* This function is called by the DMA driver. */
109 static void atmel_abdac_dma_period_done(void *arg)
111 struct atmel_abdac *dac = arg;
112 snd_pcm_period_elapsed(dac->substream);
115 static int atmel_abdac_prepare_dma(struct atmel_abdac *dac,
116 struct snd_pcm_substream *substream,
117 enum dma_data_direction direction)
119 struct dma_chan *chan = dac->dma.chan;
120 struct dw_cyclic_desc *cdesc;
121 struct snd_pcm_runtime *runtime = substream->runtime;
122 unsigned long buffer_len, period_len;
125 * We don't do DMA on "complex" transfers, i.e. with
126 * non-halfword-aligned buffers or lengths.
128 if (runtime->dma_addr & 1 || runtime->buffer_size & 1) {
129 dev_dbg(&dac->pdev->dev, "too complex transfer\n");
130 return -EINVAL;
133 buffer_len = frames_to_bytes(runtime, runtime->buffer_size);
134 period_len = frames_to_bytes(runtime, runtime->period_size);
136 cdesc = dw_dma_cyclic_prep(chan, runtime->dma_addr, buffer_len,
137 period_len, DMA_MEM_TO_DEV);
138 if (IS_ERR(cdesc)) {
139 dev_dbg(&dac->pdev->dev, "could not prepare cyclic DMA\n");
140 return PTR_ERR(cdesc);
143 cdesc->period_callback = atmel_abdac_dma_period_done;
144 cdesc->period_callback_param = dac;
146 dac->dma.cdesc = cdesc;
148 set_bit(DMA_READY, &dac->flags);
150 return 0;
153 static struct snd_pcm_hardware atmel_abdac_hw = {
154 .info = (SNDRV_PCM_INFO_MMAP
155 | SNDRV_PCM_INFO_MMAP_VALID
156 | SNDRV_PCM_INFO_INTERLEAVED
157 | SNDRV_PCM_INFO_BLOCK_TRANSFER
158 | SNDRV_PCM_INFO_RESUME
159 | SNDRV_PCM_INFO_PAUSE),
160 .formats = (SNDRV_PCM_FMTBIT_S16_BE),
161 .rates = (SNDRV_PCM_RATE_KNOT),
162 .rate_min = RATE_MIN,
163 .rate_max = RATE_MAX,
164 .channels_min = 2,
165 .channels_max = 2,
166 .buffer_bytes_max = 64 * 4096,
167 .period_bytes_min = 4096,
168 .period_bytes_max = 4096,
169 .periods_min = 6,
170 .periods_max = 64,
173 static int atmel_abdac_open(struct snd_pcm_substream *substream)
175 struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
177 dac->substream = substream;
178 atmel_abdac_hw.rate_max = dac->rates[dac->rates_num - 1];
179 atmel_abdac_hw.rate_min = dac->rates[0];
180 substream->runtime->hw = atmel_abdac_hw;
182 return snd_pcm_hw_constraint_list(substream->runtime, 0,
183 SNDRV_PCM_HW_PARAM_RATE, &dac->constraints_rates);
186 static int atmel_abdac_close(struct snd_pcm_substream *substream)
188 struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
189 dac->substream = NULL;
190 return 0;
193 static int atmel_abdac_hw_params(struct snd_pcm_substream *substream,
194 struct snd_pcm_hw_params *hw_params)
196 struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
197 int retval;
199 retval = snd_pcm_lib_malloc_pages(substream,
200 params_buffer_bytes(hw_params));
201 if (retval < 0)
202 return retval;
203 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
204 if (retval == 1)
205 if (test_and_clear_bit(DMA_READY, &dac->flags))
206 dw_dma_cyclic_free(dac->dma.chan);
208 return retval;
211 static int atmel_abdac_hw_free(struct snd_pcm_substream *substream)
213 struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
214 if (test_and_clear_bit(DMA_READY, &dac->flags))
215 dw_dma_cyclic_free(dac->dma.chan);
216 return snd_pcm_lib_free_pages(substream);
219 static int atmel_abdac_prepare(struct snd_pcm_substream *substream)
221 struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
222 int retval;
224 retval = clk_set_rate(dac->sample_clk, 256 * substream->runtime->rate);
225 if (retval)
226 return retval;
228 if (!test_bit(DMA_READY, &dac->flags))
229 retval = atmel_abdac_prepare_dma(dac, substream, DMA_TO_DEVICE);
231 return retval;
234 static int atmel_abdac_trigger(struct snd_pcm_substream *substream, int cmd)
236 struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
237 int retval = 0;
239 switch (cmd) {
240 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
241 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
242 case SNDRV_PCM_TRIGGER_START:
243 clk_enable(dac->sample_clk);
244 retval = dw_dma_cyclic_start(dac->dma.chan);
245 if (retval)
246 goto out;
247 dac_writel(dac, CTRL, DAC_BIT(EN));
248 break;
249 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
250 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
251 case SNDRV_PCM_TRIGGER_STOP:
252 dw_dma_cyclic_stop(dac->dma.chan);
253 dac_writel(dac, DATA, 0);
254 dac_writel(dac, CTRL, 0);
255 clk_disable(dac->sample_clk);
256 break;
257 default:
258 retval = -EINVAL;
259 break;
261 out:
262 return retval;
265 static snd_pcm_uframes_t
266 atmel_abdac_pointer(struct snd_pcm_substream *substream)
268 struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
269 struct snd_pcm_runtime *runtime = substream->runtime;
270 snd_pcm_uframes_t frames;
271 unsigned long bytes;
273 bytes = dw_dma_get_src_addr(dac->dma.chan);
274 bytes -= runtime->dma_addr;
276 frames = bytes_to_frames(runtime, bytes);
277 if (frames >= runtime->buffer_size)
278 frames -= runtime->buffer_size;
280 return frames;
283 static irqreturn_t abdac_interrupt(int irq, void *dev_id)
285 struct atmel_abdac *dac = dev_id;
286 u32 status;
288 status = dac_readl(dac, INT_STATUS);
289 if (status & DAC_BIT(UNDERRUN)) {
290 dev_err(&dac->pdev->dev, "underrun detected\n");
291 dac_writel(dac, INT_CLR, DAC_BIT(UNDERRUN));
292 } else {
293 dev_err(&dac->pdev->dev, "spurious interrupt (status=0x%x)\n",
294 status);
295 dac_writel(dac, INT_CLR, status);
298 return IRQ_HANDLED;
301 static struct snd_pcm_ops atmel_abdac_ops = {
302 .open = atmel_abdac_open,
303 .close = atmel_abdac_close,
304 .ioctl = snd_pcm_lib_ioctl,
305 .hw_params = atmel_abdac_hw_params,
306 .hw_free = atmel_abdac_hw_free,
307 .prepare = atmel_abdac_prepare,
308 .trigger = atmel_abdac_trigger,
309 .pointer = atmel_abdac_pointer,
312 static int __devinit atmel_abdac_pcm_new(struct atmel_abdac *dac)
314 struct snd_pcm_hardware hw = atmel_abdac_hw;
315 struct snd_pcm *pcm;
316 int retval;
318 retval = snd_pcm_new(dac->card, dac->card->shortname,
319 dac->pdev->id, 1, 0, &pcm);
320 if (retval)
321 return retval;
323 strcpy(pcm->name, dac->card->shortname);
324 pcm->private_data = dac;
325 pcm->info_flags = 0;
326 dac->pcm = pcm;
328 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &atmel_abdac_ops);
330 retval = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
331 &dac->pdev->dev, hw.periods_min * hw.period_bytes_min,
332 hw.buffer_bytes_max);
334 return retval;
337 static bool filter(struct dma_chan *chan, void *slave)
339 struct dw_dma_slave *dws = slave;
341 if (dws->dma_dev == chan->device->dev) {
342 chan->private = dws;
343 return true;
344 } else
345 return false;
348 static int set_sample_rates(struct atmel_abdac *dac)
350 long new_rate = RATE_MAX;
351 int retval = -EINVAL;
352 int index = 0;
354 /* we start at 192 kHz and work our way down to 5112 Hz */
355 while (new_rate >= RATE_MIN && index < (MAX_NUM_RATES + 1)) {
356 new_rate = clk_round_rate(dac->sample_clk, 256 * new_rate);
357 if (new_rate < 0)
358 break;
359 /* make sure we are below the ABDAC clock */
360 if (new_rate <= clk_get_rate(dac->pclk)) {
361 dac->rates[index] = new_rate / 256;
362 index++;
364 /* divide by 256 and then by two to get next rate */
365 new_rate /= 256 * 2;
368 if (index) {
369 int i;
371 /* reverse array, smallest go first */
372 for (i = 0; i < (index / 2); i++) {
373 unsigned int tmp = dac->rates[index - 1 - i];
374 dac->rates[index - 1 - i] = dac->rates[i];
375 dac->rates[i] = tmp;
378 dac->constraints_rates.count = index;
379 dac->constraints_rates.list = dac->rates;
380 dac->constraints_rates.mask = 0;
381 dac->rates_num = index;
383 retval = 0;
386 return retval;
389 static int __devinit atmel_abdac_probe(struct platform_device *pdev)
391 struct snd_card *card;
392 struct atmel_abdac *dac;
393 struct resource *regs;
394 struct atmel_abdac_pdata *pdata;
395 struct clk *pclk;
396 struct clk *sample_clk;
397 int retval;
398 int irq;
400 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
401 if (!regs) {
402 dev_dbg(&pdev->dev, "no memory resource\n");
403 return -ENXIO;
406 irq = platform_get_irq(pdev, 0);
407 if (irq < 0) {
408 dev_dbg(&pdev->dev, "could not get IRQ number\n");
409 return irq;
412 pdata = pdev->dev.platform_data;
413 if (!pdata) {
414 dev_dbg(&pdev->dev, "no platform data\n");
415 return -ENXIO;
418 pclk = clk_get(&pdev->dev, "pclk");
419 if (IS_ERR(pclk)) {
420 dev_dbg(&pdev->dev, "no peripheral clock\n");
421 return PTR_ERR(pclk);
423 sample_clk = clk_get(&pdev->dev, "sample_clk");
424 if (IS_ERR(sample_clk)) {
425 dev_dbg(&pdev->dev, "no sample clock\n");
426 retval = PTR_ERR(sample_clk);
427 goto out_put_pclk;
429 clk_enable(pclk);
431 retval = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
432 THIS_MODULE, sizeof(struct atmel_abdac), &card);
433 if (retval) {
434 dev_dbg(&pdev->dev, "could not create sound card device\n");
435 goto out_put_sample_clk;
438 dac = get_dac(card);
440 dac->irq = irq;
441 dac->card = card;
442 dac->pclk = pclk;
443 dac->sample_clk = sample_clk;
444 dac->pdev = pdev;
446 retval = set_sample_rates(dac);
447 if (retval < 0) {
448 dev_dbg(&pdev->dev, "could not set supported rates\n");
449 goto out_free_card;
452 dac->regs = ioremap(regs->start, resource_size(regs));
453 if (!dac->regs) {
454 dev_dbg(&pdev->dev, "could not remap register memory\n");
455 retval = -ENOMEM;
456 goto out_free_card;
459 /* make sure the DAC is silent and disabled */
460 dac_writel(dac, DATA, 0);
461 dac_writel(dac, CTRL, 0);
463 retval = request_irq(irq, abdac_interrupt, 0, "abdac", dac);
464 if (retval) {
465 dev_dbg(&pdev->dev, "could not request irq\n");
466 goto out_unmap_regs;
469 snd_card_set_dev(card, &pdev->dev);
471 if (pdata->dws.dma_dev) {
472 dma_cap_mask_t mask;
474 dma_cap_zero(mask);
475 dma_cap_set(DMA_SLAVE, mask);
477 dac->dma.chan = dma_request_channel(mask, filter, &pdata->dws);
478 if (dac->dma.chan) {
479 struct dma_slave_config dma_conf = {
480 .dst_addr = regs->start + DAC_DATA,
481 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
482 .src_maxburst = 1,
483 .dst_maxburst = 1,
484 .direction = DMA_MEM_TO_DEV,
485 .device_fc = false,
488 dmaengine_slave_config(dac->dma.chan, &dma_conf);
491 if (!pdata->dws.dma_dev || !dac->dma.chan) {
492 dev_dbg(&pdev->dev, "DMA not available\n");
493 retval = -ENODEV;
494 goto out_unset_card_dev;
497 strcpy(card->driver, "Atmel ABDAC");
498 strcpy(card->shortname, "Atmel ABDAC");
499 sprintf(card->longname, "Atmel Audio Bitstream DAC");
501 retval = atmel_abdac_pcm_new(dac);
502 if (retval) {
503 dev_dbg(&pdev->dev, "could not register ABDAC pcm device\n");
504 goto out_release_dma;
507 retval = snd_card_register(card);
508 if (retval) {
509 dev_dbg(&pdev->dev, "could not register sound card\n");
510 goto out_release_dma;
513 platform_set_drvdata(pdev, card);
515 dev_info(&pdev->dev, "Atmel ABDAC at 0x%p using %s\n",
516 dac->regs, dev_name(&dac->dma.chan->dev->device));
518 return retval;
520 out_release_dma:
521 dma_release_channel(dac->dma.chan);
522 dac->dma.chan = NULL;
523 out_unset_card_dev:
524 snd_card_set_dev(card, NULL);
525 free_irq(irq, dac);
526 out_unmap_regs:
527 iounmap(dac->regs);
528 out_free_card:
529 snd_card_free(card);
530 out_put_sample_clk:
531 clk_put(sample_clk);
532 clk_disable(pclk);
533 out_put_pclk:
534 clk_put(pclk);
535 return retval;
538 #ifdef CONFIG_PM_SLEEP
539 static int atmel_abdac_suspend(struct device *pdev)
541 struct snd_card *card = dev_get_drvdata(pdev);
542 struct atmel_abdac *dac = card->private_data;
544 dw_dma_cyclic_stop(dac->dma.chan);
545 clk_disable(dac->sample_clk);
546 clk_disable(dac->pclk);
548 return 0;
551 static int atmel_abdac_resume(struct device *pdev)
553 struct snd_card *card = dev_get_drvdata(pdev);
554 struct atmel_abdac *dac = card->private_data;
556 clk_enable(dac->pclk);
557 clk_enable(dac->sample_clk);
558 if (test_bit(DMA_READY, &dac->flags))
559 dw_dma_cyclic_start(dac->dma.chan);
561 return 0;
564 static SIMPLE_DEV_PM_OPS(atmel_abdac_pm, atmel_abdac_suspend, atmel_abdac_resume);
565 #define ATMEL_ABDAC_PM_OPS &atmel_abdac_pm
566 #else
567 #define ATMEL_ABDAC_PM_OPS NULL
568 #endif
570 static int __devexit atmel_abdac_remove(struct platform_device *pdev)
572 struct snd_card *card = platform_get_drvdata(pdev);
573 struct atmel_abdac *dac = get_dac(card);
575 clk_put(dac->sample_clk);
576 clk_disable(dac->pclk);
577 clk_put(dac->pclk);
579 dma_release_channel(dac->dma.chan);
580 dac->dma.chan = NULL;
581 snd_card_set_dev(card, NULL);
582 iounmap(dac->regs);
583 free_irq(dac->irq, dac);
584 snd_card_free(card);
586 platform_set_drvdata(pdev, NULL);
588 return 0;
591 static struct platform_driver atmel_abdac_driver = {
592 .remove = __devexit_p(atmel_abdac_remove),
593 .driver = {
594 .name = "atmel_abdac",
595 .owner = THIS_MODULE,
596 .pm = ATMEL_ABDAC_PM_OPS,
600 static int __init atmel_abdac_init(void)
602 return platform_driver_probe(&atmel_abdac_driver,
603 atmel_abdac_probe);
605 module_init(atmel_abdac_init);
607 static void __exit atmel_abdac_exit(void)
609 platform_driver_unregister(&atmel_abdac_driver);
611 module_exit(atmel_abdac_exit);
613 MODULE_LICENSE("GPL");
614 MODULE_DESCRIPTION("Driver for Atmel Audio Bitstream DAC (ABDAC)");
615 MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");