2 * ALSA PCM interface for the TI DAVINCI processor
4 * Author: Vladimir Barinov, <vbarinov@embeddedalley.com>
5 * Copyright: (C) 2007 MontaVista Software, Inc., <source@mvista.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/kernel.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
25 #include <mach/edma.h>
27 #include "davinci-pcm.h"
29 static struct snd_pcm_hardware davinci_pcm_hardware = {
30 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
31 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
32 SNDRV_PCM_INFO_PAUSE),
33 .formats = (SNDRV_PCM_FMTBIT_S16_LE),
34 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
35 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
36 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
37 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
43 .buffer_bytes_max = 128 * 1024,
44 .period_bytes_min = 32,
45 .period_bytes_max = 8 * 1024,
51 struct davinci_runtime_data {
53 int period; /* current DMA period */
54 int master_lch; /* Master DMA channel */
55 int slave_lch; /* linked parameter RAM reload slot */
56 struct davinci_pcm_dma_params *params; /* DMA params */
59 static void davinci_pcm_enqueue_dma(struct snd_pcm_substream *substream)
61 struct davinci_runtime_data *prtd = substream->runtime->private_data;
62 struct snd_pcm_runtime *runtime = substream->runtime;
63 int lch = prtd->slave_lch;
64 unsigned int period_size;
65 unsigned int dma_offset;
68 unsigned short src_bidx, dst_bidx;
69 unsigned int data_type;
72 period_size = snd_pcm_lib_period_bytes(substream);
73 dma_offset = prtd->period * period_size;
74 dma_pos = runtime->dma_addr + dma_offset;
76 pr_debug("davinci_pcm: audio_set_dma_params_play channel = %d "
77 "dma_ptr = %x period_size=%x\n", lch, dma_pos, period_size);
79 data_type = prtd->params->data_type;
80 count = period_size / data_type;
82 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
84 dst = prtd->params->dma_addr;
88 src = prtd->params->dma_addr;
94 edma_set_src(lch, src, INCR, W8BIT);
95 edma_set_dest(lch, dst, INCR, W8BIT);
96 edma_set_src_index(lch, src_bidx, 0);
97 edma_set_dest_index(lch, dst_bidx, 0);
98 edma_set_transfer_params(lch, data_type, count, 1, 0, ASYNC);
101 if (unlikely(prtd->period >= runtime->periods))
105 static void davinci_pcm_dma_irq(unsigned lch, u16 ch_status, void *data)
107 struct snd_pcm_substream *substream = data;
108 struct davinci_runtime_data *prtd = substream->runtime->private_data;
110 pr_debug("davinci_pcm: lch=%d, status=0x%x\n", lch, ch_status);
112 if (unlikely(ch_status != DMA_COMPLETE))
115 if (snd_pcm_running(substream)) {
116 snd_pcm_period_elapsed(substream);
118 spin_lock(&prtd->lock);
119 davinci_pcm_enqueue_dma(substream);
120 spin_unlock(&prtd->lock);
124 static int davinci_pcm_dma_request(struct snd_pcm_substream *substream)
126 struct davinci_runtime_data *prtd = substream->runtime->private_data;
127 struct snd_soc_pcm_runtime *rtd = substream->private_data;
128 struct davinci_pcm_dma_params *dma_data = rtd->dai->cpu_dai->dma_data;
129 struct edmacc_param p_ram;
135 prtd->params = dma_data;
137 /* Request master DMA channel */
138 ret = edma_alloc_channel(prtd->params->channel,
139 davinci_pcm_dma_irq, substream,
143 prtd->master_lch = ret;
145 /* Request parameter RAM reload slot */
146 ret = edma_alloc_slot(EDMA_SLOT_ANY);
148 edma_free_channel(prtd->master_lch);
151 prtd->slave_lch = ret;
153 /* Issue transfer completion IRQ when the channel completes a
154 * transfer, then always reload from the same slot (by a kind
155 * of loopback link). The completion IRQ handler will update
156 * the reload slot with a new buffer.
158 * REVISIT save p_ram here after setting up everything except
159 * the buffer and its length (ccnt) ... use it as a template
160 * so davinci_pcm_enqueue_dma() takes less time in IRQ.
162 edma_read_slot(prtd->slave_lch, &p_ram);
163 p_ram.opt |= TCINTEN | EDMA_TCC(prtd->master_lch);
164 p_ram.link_bcntrld = prtd->slave_lch << 5;
165 edma_write_slot(prtd->slave_lch, &p_ram);
170 static int davinci_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
172 struct davinci_runtime_data *prtd = substream->runtime->private_data;
175 spin_lock(&prtd->lock);
178 case SNDRV_PCM_TRIGGER_START:
179 case SNDRV_PCM_TRIGGER_RESUME:
180 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
181 edma_start(prtd->master_lch);
183 case SNDRV_PCM_TRIGGER_STOP:
184 case SNDRV_PCM_TRIGGER_SUSPEND:
185 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
186 edma_stop(prtd->master_lch);
193 spin_unlock(&prtd->lock);
198 static int davinci_pcm_prepare(struct snd_pcm_substream *substream)
200 struct davinci_runtime_data *prtd = substream->runtime->private_data;
201 struct edmacc_param temp;
204 davinci_pcm_enqueue_dma(substream);
206 /* Copy self-linked parameter RAM entry into master channel */
207 edma_read_slot(prtd->slave_lch, &temp);
208 edma_write_slot(prtd->master_lch, &temp);
213 static snd_pcm_uframes_t
214 davinci_pcm_pointer(struct snd_pcm_substream *substream)
216 struct snd_pcm_runtime *runtime = substream->runtime;
217 struct davinci_runtime_data *prtd = runtime->private_data;
222 spin_lock(&prtd->lock);
224 edma_get_position(prtd->master_lch, &src, &dst);
225 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
226 count = src - runtime->dma_addr;
228 count = dst - runtime->dma_addr;
230 spin_unlock(&prtd->lock);
232 offset = bytes_to_frames(runtime, count);
233 if (offset >= runtime->buffer_size)
239 static int davinci_pcm_open(struct snd_pcm_substream *substream)
241 struct snd_pcm_runtime *runtime = substream->runtime;
242 struct davinci_runtime_data *prtd;
245 snd_soc_set_runtime_hwparams(substream, &davinci_pcm_hardware);
247 prtd = kzalloc(sizeof(struct davinci_runtime_data), GFP_KERNEL);
251 spin_lock_init(&prtd->lock);
253 runtime->private_data = prtd;
255 ret = davinci_pcm_dma_request(substream);
257 printk(KERN_ERR "davinci_pcm: Failed to get dma channels\n");
264 static int davinci_pcm_close(struct snd_pcm_substream *substream)
266 struct snd_pcm_runtime *runtime = substream->runtime;
267 struct davinci_runtime_data *prtd = runtime->private_data;
269 edma_unlink(prtd->slave_lch);
271 edma_free_slot(prtd->slave_lch);
272 edma_free_channel(prtd->master_lch);
279 static int davinci_pcm_hw_params(struct snd_pcm_substream *substream,
280 struct snd_pcm_hw_params *hw_params)
282 return snd_pcm_lib_malloc_pages(substream,
283 params_buffer_bytes(hw_params));
286 static int davinci_pcm_hw_free(struct snd_pcm_substream *substream)
288 return snd_pcm_lib_free_pages(substream);
291 static int davinci_pcm_mmap(struct snd_pcm_substream *substream,
292 struct vm_area_struct *vma)
294 struct snd_pcm_runtime *runtime = substream->runtime;
296 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
302 static struct snd_pcm_ops davinci_pcm_ops = {
303 .open = davinci_pcm_open,
304 .close = davinci_pcm_close,
305 .ioctl = snd_pcm_lib_ioctl,
306 .hw_params = davinci_pcm_hw_params,
307 .hw_free = davinci_pcm_hw_free,
308 .prepare = davinci_pcm_prepare,
309 .trigger = davinci_pcm_trigger,
310 .pointer = davinci_pcm_pointer,
311 .mmap = davinci_pcm_mmap,
314 static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
316 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
317 struct snd_dma_buffer *buf = &substream->dma_buffer;
318 size_t size = davinci_pcm_hardware.buffer_bytes_max;
320 buf->dev.type = SNDRV_DMA_TYPE_DEV;
321 buf->dev.dev = pcm->card->dev;
322 buf->private_data = NULL;
323 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
324 &buf->addr, GFP_KERNEL);
326 pr_debug("davinci_pcm: preallocate_dma_buffer: area=%p, addr=%p, "
327 "size=%d\n", (void *) buf->area, (void *) buf->addr, size);
336 static void davinci_pcm_free(struct snd_pcm *pcm)
338 struct snd_pcm_substream *substream;
339 struct snd_dma_buffer *buf;
342 for (stream = 0; stream < 2; stream++) {
343 substream = pcm->streams[stream].substream;
347 buf = &substream->dma_buffer;
351 dma_free_writecombine(pcm->card->dev, buf->bytes,
352 buf->area, buf->addr);
357 static u64 davinci_pcm_dmamask = 0xffffffff;
359 static int davinci_pcm_new(struct snd_card *card,
360 struct snd_soc_dai *dai, struct snd_pcm *pcm)
364 if (!card->dev->dma_mask)
365 card->dev->dma_mask = &davinci_pcm_dmamask;
366 if (!card->dev->coherent_dma_mask)
367 card->dev->coherent_dma_mask = 0xffffffff;
369 if (dai->playback.channels_min) {
370 ret = davinci_pcm_preallocate_dma_buffer(pcm,
371 SNDRV_PCM_STREAM_PLAYBACK);
376 if (dai->capture.channels_min) {
377 ret = davinci_pcm_preallocate_dma_buffer(pcm,
378 SNDRV_PCM_STREAM_CAPTURE);
386 struct snd_soc_platform davinci_soc_platform = {
387 .name = "davinci-audio",
388 .pcm_ops = &davinci_pcm_ops,
389 .pcm_new = davinci_pcm_new,
390 .pcm_free = davinci_pcm_free,
392 EXPORT_SYMBOL_GPL(davinci_soc_platform);
394 static int __init davinci_soc_platform_init(void)
396 return snd_soc_register_platform(&davinci_soc_platform);
398 module_init(davinci_soc_platform_init);
400 static void __exit davinci_soc_platform_exit(void)
402 snd_soc_unregister_platform(&davinci_soc_platform);
404 module_exit(davinci_soc_platform_exit);
406 MODULE_AUTHOR("Vladimir Barinov");
407 MODULE_DESCRIPTION("TI DAVINCI PCM DMA module");
408 MODULE_LICENSE("GPL");