blob: ab43a539c11de04b458e4baf837a66439e3a87bc [file] [log] [blame]
Vladimir Barinov310355c2008-02-18 11:40:22 +01001/*
2 * ALSA PCM interface for the TI DAVINCI processor
3 *
Vladimir Barinovd6b52032008-09-29 23:14:11 +04004 * Author: Vladimir Barinov, <vbarinov@embeddedalley.com>
Vladimir Barinov310355c2008-02-18 11:40:22 +01005 * Copyright: (C) 2007 MontaVista Software, Inc., <source@mvista.com>
6 *
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.
10 */
11
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>
Alexander Beregalov9cd28ab2008-12-13 16:25:27 +030017#include <linux/kernel.h>
Vladimir Barinov310355c2008-02-18 11:40:22 +010018
19#include <sound/core.h>
20#include <sound/pcm.h>
21#include <sound/pcm_params.h>
22#include <sound/soc.h>
23
24#include <asm/dma.h>
David Brownell82075af2009-05-14 12:41:22 -070025#include <mach/edma.h>
Vladimir Barinov310355c2008-02-18 11:40:22 +010026
27#include "davinci-pcm.h"
28
Vladimir Barinov310355c2008-02-18 11:40:22 +010029static 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 |
38 SNDRV_PCM_RATE_KNOT),
39 .rate_min = 8000,
40 .rate_max = 96000,
41 .channels_min = 2,
42 .channels_max = 2,
43 .buffer_bytes_max = 128 * 1024,
44 .period_bytes_min = 32,
45 .period_bytes_max = 8 * 1024,
46 .periods_min = 16,
47 .periods_max = 255,
48 .fifo_size = 0,
49};
50
51struct davinci_runtime_data {
52 spinlock_t lock;
53 int period; /* current DMA period */
54 int master_lch; /* Master DMA channel */
David Brownell82075af2009-05-14 12:41:22 -070055 int slave_lch; /* linked parameter RAM reload slot */
Vladimir Barinov310355c2008-02-18 11:40:22 +010056 struct davinci_pcm_dma_params *params; /* DMA params */
57};
58
59static void davinci_pcm_enqueue_dma(struct snd_pcm_substream *substream)
60{
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;
66 dma_addr_t dma_pos;
67 dma_addr_t src, dst;
68 unsigned short src_bidx, dst_bidx;
69 unsigned int data_type;
70 unsigned int count;
71
72 period_size = snd_pcm_lib_period_bytes(substream);
73 dma_offset = prtd->period * period_size;
74 dma_pos = runtime->dma_addr + dma_offset;
75
Alexander Beregalov9cd28ab2008-12-13 16:25:27 +030076 pr_debug("davinci_pcm: audio_set_dma_params_play channel = %d "
77 "dma_ptr = %x period_size=%x\n", lch, dma_pos, period_size);
Vladimir Barinov310355c2008-02-18 11:40:22 +010078
79 data_type = prtd->params->data_type;
80 count = period_size / data_type;
81
82 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
83 src = dma_pos;
84 dst = prtd->params->dma_addr;
85 src_bidx = data_type;
86 dst_bidx = 0;
87 } else {
88 src = prtd->params->dma_addr;
89 dst = dma_pos;
90 src_bidx = 0;
91 dst_bidx = data_type;
92 }
93
David Brownell82075af2009-05-14 12:41:22 -070094 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);
Vladimir Barinov310355c2008-02-18 11:40:22 +010099
100 prtd->period++;
101 if (unlikely(prtd->period >= runtime->periods))
102 prtd->period = 0;
103}
104
David Brownell82075af2009-05-14 12:41:22 -0700105static void davinci_pcm_dma_irq(unsigned lch, u16 ch_status, void *data)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100106{
107 struct snd_pcm_substream *substream = data;
108 struct davinci_runtime_data *prtd = substream->runtime->private_data;
109
Alexander Beregalov9cd28ab2008-12-13 16:25:27 +0300110 pr_debug("davinci_pcm: lch=%d, status=0x%x\n", lch, ch_status);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100111
112 if (unlikely(ch_status != DMA_COMPLETE))
113 return;
114
115 if (snd_pcm_running(substream)) {
116 snd_pcm_period_elapsed(substream);
117
118 spin_lock(&prtd->lock);
119 davinci_pcm_enqueue_dma(substream);
120 spin_unlock(&prtd->lock);
121 }
122}
123
124static int davinci_pcm_dma_request(struct snd_pcm_substream *substream)
125{
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;
David Brownell82075af2009-05-14 12:41:22 -0700129 struct edmacc_param p_ram;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100130 int ret;
131
132 if (!dma_data)
133 return -ENODEV;
134
135 prtd->params = dma_data;
136
137 /* Request master DMA channel */
David Brownell82075af2009-05-14 12:41:22 -0700138 ret = edma_alloc_channel(prtd->params->channel,
Vladimir Barinov310355c2008-02-18 11:40:22 +0100139 davinci_pcm_dma_irq, substream,
David Brownell82075af2009-05-14 12:41:22 -0700140 EVENTQ_0);
141 if (ret < 0)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100142 return ret;
David Brownell82075af2009-05-14 12:41:22 -0700143 prtd->master_lch = ret;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100144
David Brownell82075af2009-05-14 12:41:22 -0700145 /* Request parameter RAM reload slot */
146 ret = edma_alloc_slot(EDMA_SLOT_ANY);
147 if (ret < 0) {
148 edma_free_channel(prtd->master_lch);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100149 return ret;
150 }
David Brownell82075af2009-05-14 12:41:22 -0700151 prtd->slave_lch = ret;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100152
David Brownell82075af2009-05-14 12:41:22 -0700153 /* 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.
157 *
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.
161 */
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);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100166
167 return 0;
168}
169
170static int davinci_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
171{
172 struct davinci_runtime_data *prtd = substream->runtime->private_data;
173 int ret = 0;
174
175 spin_lock(&prtd->lock);
176
177 switch (cmd) {
178 case SNDRV_PCM_TRIGGER_START:
179 case SNDRV_PCM_TRIGGER_RESUME:
180 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
David Brownell82075af2009-05-14 12:41:22 -0700181 edma_start(prtd->master_lch);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100182 break;
183 case SNDRV_PCM_TRIGGER_STOP:
184 case SNDRV_PCM_TRIGGER_SUSPEND:
185 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
David Brownell82075af2009-05-14 12:41:22 -0700186 edma_stop(prtd->master_lch);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100187 break;
188 default:
189 ret = -EINVAL;
190 break;
191 }
192
193 spin_unlock(&prtd->lock);
194
195 return ret;
196}
197
198static int davinci_pcm_prepare(struct snd_pcm_substream *substream)
199{
200 struct davinci_runtime_data *prtd = substream->runtime->private_data;
David Brownell82075af2009-05-14 12:41:22 -0700201 struct edmacc_param temp;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100202
203 prtd->period = 0;
204 davinci_pcm_enqueue_dma(substream);
205
David Brownell82075af2009-05-14 12:41:22 -0700206 /* 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);
Troy Kisky6e541472009-07-07 17:36:06 -0700209 davinci_pcm_enqueue_dma(substream);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100210
211 return 0;
212}
213
214static snd_pcm_uframes_t
215davinci_pcm_pointer(struct snd_pcm_substream *substream)
216{
217 struct snd_pcm_runtime *runtime = substream->runtime;
218 struct davinci_runtime_data *prtd = runtime->private_data;
219 unsigned int offset;
220 dma_addr_t count;
221 dma_addr_t src, dst;
222
223 spin_lock(&prtd->lock);
224
David Brownell82075af2009-05-14 12:41:22 -0700225 edma_get_position(prtd->master_lch, &src, &dst);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100226 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
227 count = src - runtime->dma_addr;
228 else
Fernando Carrijoc19a28e2009-01-07 18:09:08 -0800229 count = dst - runtime->dma_addr;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100230
231 spin_unlock(&prtd->lock);
232
233 offset = bytes_to_frames(runtime, count);
234 if (offset >= runtime->buffer_size)
235 offset = 0;
236
237 return offset;
238}
239
240static int davinci_pcm_open(struct snd_pcm_substream *substream)
241{
242 struct snd_pcm_runtime *runtime = substream->runtime;
243 struct davinci_runtime_data *prtd;
244 int ret = 0;
245
246 snd_soc_set_runtime_hwparams(substream, &davinci_pcm_hardware);
Troy Kisky6a90d532009-08-06 16:55:32 -0700247 /* ensure that buffer size is a multiple of period size */
248 ret = snd_pcm_hw_constraint_integer(runtime,
249 SNDRV_PCM_HW_PARAM_PERIODS);
250 if (ret < 0)
251 return ret;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100252
253 prtd = kzalloc(sizeof(struct davinci_runtime_data), GFP_KERNEL);
254 if (prtd == NULL)
255 return -ENOMEM;
256
257 spin_lock_init(&prtd->lock);
258
259 runtime->private_data = prtd;
260
261 ret = davinci_pcm_dma_request(substream);
262 if (ret) {
263 printk(KERN_ERR "davinci_pcm: Failed to get dma channels\n");
264 kfree(prtd);
265 }
266
267 return ret;
268}
269
270static int davinci_pcm_close(struct snd_pcm_substream *substream)
271{
272 struct snd_pcm_runtime *runtime = substream->runtime;
273 struct davinci_runtime_data *prtd = runtime->private_data;
274
David Brownell82075af2009-05-14 12:41:22 -0700275 edma_unlink(prtd->slave_lch);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100276
David Brownell82075af2009-05-14 12:41:22 -0700277 edma_free_slot(prtd->slave_lch);
278 edma_free_channel(prtd->master_lch);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100279
280 kfree(prtd);
281
282 return 0;
283}
284
285static int davinci_pcm_hw_params(struct snd_pcm_substream *substream,
286 struct snd_pcm_hw_params *hw_params)
287{
288 return snd_pcm_lib_malloc_pages(substream,
289 params_buffer_bytes(hw_params));
290}
291
292static int davinci_pcm_hw_free(struct snd_pcm_substream *substream)
293{
294 return snd_pcm_lib_free_pages(substream);
295}
296
297static int davinci_pcm_mmap(struct snd_pcm_substream *substream,
298 struct vm_area_struct *vma)
299{
300 struct snd_pcm_runtime *runtime = substream->runtime;
301
302 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
303 runtime->dma_area,
304 runtime->dma_addr,
305 runtime->dma_bytes);
306}
307
Mark Brownb2a19d02009-01-17 19:14:26 +0000308static struct snd_pcm_ops davinci_pcm_ops = {
Vladimir Barinov310355c2008-02-18 11:40:22 +0100309 .open = davinci_pcm_open,
310 .close = davinci_pcm_close,
311 .ioctl = snd_pcm_lib_ioctl,
312 .hw_params = davinci_pcm_hw_params,
313 .hw_free = davinci_pcm_hw_free,
314 .prepare = davinci_pcm_prepare,
315 .trigger = davinci_pcm_trigger,
316 .pointer = davinci_pcm_pointer,
317 .mmap = davinci_pcm_mmap,
318};
319
320static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
321{
322 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
323 struct snd_dma_buffer *buf = &substream->dma_buffer;
324 size_t size = davinci_pcm_hardware.buffer_bytes_max;
325
326 buf->dev.type = SNDRV_DMA_TYPE_DEV;
327 buf->dev.dev = pcm->card->dev;
328 buf->private_data = NULL;
329 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
330 &buf->addr, GFP_KERNEL);
331
Alexander Beregalov9cd28ab2008-12-13 16:25:27 +0300332 pr_debug("davinci_pcm: preallocate_dma_buffer: area=%p, addr=%p, "
333 "size=%d\n", (void *) buf->area, (void *) buf->addr, size);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100334
335 if (!buf->area)
336 return -ENOMEM;
337
338 buf->bytes = size;
339 return 0;
340}
341
342static void davinci_pcm_free(struct snd_pcm *pcm)
343{
344 struct snd_pcm_substream *substream;
345 struct snd_dma_buffer *buf;
346 int stream;
347
348 for (stream = 0; stream < 2; stream++) {
349 substream = pcm->streams[stream].substream;
350 if (!substream)
351 continue;
352
353 buf = &substream->dma_buffer;
354 if (!buf->area)
355 continue;
356
357 dma_free_writecombine(pcm->card->dev, buf->bytes,
358 buf->area, buf->addr);
359 buf->area = NULL;
360 }
361}
362
363static u64 davinci_pcm_dmamask = 0xffffffff;
364
365static int davinci_pcm_new(struct snd_card *card,
Liam Girdwood9cb132d2008-07-07 16:07:42 +0100366 struct snd_soc_dai *dai, struct snd_pcm *pcm)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100367{
368 int ret;
369
370 if (!card->dev->dma_mask)
371 card->dev->dma_mask = &davinci_pcm_dmamask;
372 if (!card->dev->coherent_dma_mask)
373 card->dev->coherent_dma_mask = 0xffffffff;
374
375 if (dai->playback.channels_min) {
376 ret = davinci_pcm_preallocate_dma_buffer(pcm,
377 SNDRV_PCM_STREAM_PLAYBACK);
378 if (ret)
379 return ret;
380 }
381
382 if (dai->capture.channels_min) {
383 ret = davinci_pcm_preallocate_dma_buffer(pcm,
384 SNDRV_PCM_STREAM_CAPTURE);
385 if (ret)
386 return ret;
387 }
388
389 return 0;
390}
391
392struct snd_soc_platform davinci_soc_platform = {
393 .name = "davinci-audio",
394 .pcm_ops = &davinci_pcm_ops,
395 .pcm_new = davinci_pcm_new,
396 .pcm_free = davinci_pcm_free,
397};
398EXPORT_SYMBOL_GPL(davinci_soc_platform);
399
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100400static int __init davinci_soc_platform_init(void)
Mark Brown958e7922008-12-03 19:58:17 +0000401{
402 return snd_soc_register_platform(&davinci_soc_platform);
403}
404module_init(davinci_soc_platform_init);
405
406static void __exit davinci_soc_platform_exit(void)
407{
408 snd_soc_unregister_platform(&davinci_soc_platform);
409}
410module_exit(davinci_soc_platform_exit);
411
Vladimir Barinov310355c2008-02-18 11:40:22 +0100412MODULE_AUTHOR("Vladimir Barinov");
413MODULE_DESCRIPTION("TI DAVINCI PCM DMA module");
414MODULE_LICENSE("GPL");