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.
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>
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
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
74 #define MAX_NUM_RATES 6
75 /* ALSA seems to use rates between 192000 Hz and 5112 Hz. */
76 #define RATE_MAX 192000
83 struct atmel_abdac_dma {
84 struct dma_chan *chan;
85 struct dw_cyclic_desc *cdesc;
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;
101 unsigned int rates[MAX_NUM_RATES];
102 unsigned int rates_num;
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");
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);
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);
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,
166 .buffer_bytes_max = 64 * 4096,
167 .period_bytes_min = 4096,
168 .period_bytes_max = 4096,
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;
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);
199 retval = snd_pcm_lib_malloc_pages(substream,
200 params_buffer_bytes(hw_params));
203 /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
205 if (test_and_clear_bit(DMA_READY, &dac->flags))
206 dw_dma_cyclic_free(dac->dma.chan);
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);
224 retval = clk_set_rate(dac->sample_clk, 256 * substream->runtime->rate);
228 if (!test_bit(DMA_READY, &dac->flags))
229 retval = atmel_abdac_prepare_dma(dac, substream, DMA_TO_DEVICE);
234 static int atmel_abdac_trigger(struct snd_pcm_substream *substream, int cmd)
236 struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
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);
247 dac_writel(dac, CTRL, DAC_BIT(EN));
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);
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;
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;
283 static irqreturn_t abdac_interrupt(int irq, void *dev_id)
285 struct atmel_abdac *dac = dev_id;
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));
293 dev_err(&dac->pdev->dev, "spurious interrupt (status=0x%x)\n",
295 dac_writel(dac, INT_CLR, status);
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 atmel_abdac_pcm_new(struct atmel_abdac *dac)
314 struct snd_pcm_hardware hw = atmel_abdac_hw;
318 retval = snd_pcm_new(dac->card, dac->card->shortname,
319 dac->pdev->id, 1, 0, &pcm);
323 strcpy(pcm->name, dac->card->shortname);
324 pcm->private_data = dac;
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);
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) {
348 static int set_sample_rates(struct atmel_abdac *dac)
350 long new_rate = RATE_MAX;
351 int retval = -EINVAL;
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);
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;
364 /* divide by 256 and then by two to get next rate */
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];
378 dac->constraints_rates.count = index;
379 dac->constraints_rates.list = dac->rates;
380 dac->constraints_rates.mask = 0;
381 dac->rates_num = index;
389 static int 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;
396 struct clk *sample_clk;
400 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
402 dev_dbg(&pdev->dev, "no memory resource\n");
406 irq = platform_get_irq(pdev, 0);
408 dev_dbg(&pdev->dev, "could not get IRQ number\n");
412 pdata = pdev->dev.platform_data;
414 dev_dbg(&pdev->dev, "no platform data\n");
418 pclk = clk_get(&pdev->dev, "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);
431 retval = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
432 THIS_MODULE, sizeof(struct atmel_abdac), &card);
434 dev_dbg(&pdev->dev, "could not create sound card device\n");
435 goto out_put_sample_clk;
443 dac->sample_clk = sample_clk;
446 retval = set_sample_rates(dac);
448 dev_dbg(&pdev->dev, "could not set supported rates\n");
452 dac->regs = ioremap(regs->start, resource_size(regs));
454 dev_dbg(&pdev->dev, "could not remap register memory\n");
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);
465 dev_dbg(&pdev->dev, "could not request irq\n");
469 snd_card_set_dev(card, &pdev->dev);
471 if (pdata->dws.dma_dev) {
475 dma_cap_set(DMA_SLAVE, mask);
477 dac->dma.chan = dma_request_channel(mask, filter, &pdata->dws);
479 struct dma_slave_config dma_conf = {
480 .dst_addr = regs->start + DAC_DATA,
481 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
484 .direction = DMA_MEM_TO_DEV,
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");
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);
503 dev_dbg(&pdev->dev, "could not register ABDAC pcm device\n");
504 goto out_release_dma;
507 retval = snd_card_register(card);
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));
521 dma_release_channel(dac->dma.chan);
522 dac->dma.chan = NULL;
524 snd_card_set_dev(card, NULL);
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);
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);
564 static SIMPLE_DEV_PM_OPS(atmel_abdac_pm, atmel_abdac_suspend, atmel_abdac_resume);
565 #define ATMEL_ABDAC_PM_OPS &atmel_abdac_pm
567 #define ATMEL_ABDAC_PM_OPS NULL
570 static int 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);
579 dma_release_channel(dac->dma.chan);
580 dac->dma.chan = NULL;
581 snd_card_set_dev(card, NULL);
583 free_irq(dac->irq, dac);
586 platform_set_drvdata(pdev, NULL);
591 static struct platform_driver atmel_abdac_driver = {
592 .remove = atmel_abdac_remove,
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,
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>");