blob: 42a657ea49cb94c22a9aa4f3f718da92f5211c0f [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 */
Troy Kisky1587ea312009-11-18 17:49:52 -070054 int asp_channel; /* Master DMA channel */
55 int asp_link[2]; /* asp parameter link channel, ping/pong */
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;
Troy Kisky1587ea312009-11-18 17:49:52 -070063 int link = prtd->asp_link[0];
Vladimir Barinov310355c2008-02-18 11:40:22 +010064 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;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -040069 unsigned short src_cidx, dst_cidx;
Vladimir Barinov310355c2008-02-18 11:40:22 +010070 unsigned int data_type;
Chaithrika U S6a99fb52009-08-11 16:58:52 -040071 unsigned short acnt;
Vladimir Barinov310355c2008-02-18 11:40:22 +010072 unsigned int count;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -040073 unsigned int fifo_level;
Vladimir Barinov310355c2008-02-18 11:40:22 +010074
75 period_size = snd_pcm_lib_period_bytes(substream);
76 dma_offset = prtd->period * period_size;
77 dma_pos = runtime->dma_addr + dma_offset;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -040078 fifo_level = prtd->params->fifo_level;
Vladimir Barinov310355c2008-02-18 11:40:22 +010079
Alexander Beregalov9cd28ab2008-12-13 16:25:27 +030080 pr_debug("davinci_pcm: audio_set_dma_params_play channel = %d "
Troy Kisky1587ea312009-11-18 17:49:52 -070081 "dma_ptr = %x period_size=%x\n", link, dma_pos, period_size);
Vladimir Barinov310355c2008-02-18 11:40:22 +010082
83 data_type = prtd->params->data_type;
84 count = period_size / data_type;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -040085 if (fifo_level)
86 count /= fifo_level;
Vladimir Barinov310355c2008-02-18 11:40:22 +010087
88 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
89 src = dma_pos;
90 dst = prtd->params->dma_addr;
91 src_bidx = data_type;
92 dst_bidx = 0;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -040093 src_cidx = data_type * fifo_level;
94 dst_cidx = 0;
Vladimir Barinov310355c2008-02-18 11:40:22 +010095 } else {
96 src = prtd->params->dma_addr;
97 dst = dma_pos;
98 src_bidx = 0;
99 dst_bidx = data_type;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400100 src_cidx = 0;
101 dst_cidx = data_type * fifo_level;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100102 }
103
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400104 acnt = prtd->params->acnt;
Troy Kisky1587ea312009-11-18 17:49:52 -0700105 edma_set_src(link, src, INCR, W8BIT);
106 edma_set_dest(link, dst, INCR, W8BIT);
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400107
Troy Kisky1587ea312009-11-18 17:49:52 -0700108 edma_set_src_index(link, src_bidx, src_cidx);
109 edma_set_dest_index(link, dst_bidx, dst_cidx);
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400110
111 if (!fifo_level)
Troy Kisky1587ea312009-11-18 17:49:52 -0700112 edma_set_transfer_params(link, acnt, count, 1, 0, ASYNC);
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400113 else
Troy Kisky1587ea312009-11-18 17:49:52 -0700114 edma_set_transfer_params(link, acnt, fifo_level, count,
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400115 fifo_level, ABSYNC);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100116
117 prtd->period++;
118 if (unlikely(prtd->period >= runtime->periods))
119 prtd->period = 0;
120}
121
Troy Kisky1587ea312009-11-18 17:49:52 -0700122static void davinci_pcm_dma_irq(unsigned link, u16 ch_status, void *data)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100123{
124 struct snd_pcm_substream *substream = data;
125 struct davinci_runtime_data *prtd = substream->runtime->private_data;
126
Troy Kisky1587ea312009-11-18 17:49:52 -0700127 pr_debug("davinci_pcm: link=%d, status=0x%x\n", link, ch_status);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100128
129 if (unlikely(ch_status != DMA_COMPLETE))
130 return;
131
132 if (snd_pcm_running(substream)) {
133 snd_pcm_period_elapsed(substream);
134
135 spin_lock(&prtd->lock);
136 davinci_pcm_enqueue_dma(substream);
137 spin_unlock(&prtd->lock);
138 }
139}
140
141static int davinci_pcm_dma_request(struct snd_pcm_substream *substream)
142{
143 struct davinci_runtime_data *prtd = substream->runtime->private_data;
David Brownell82075af2009-05-14 12:41:22 -0700144 struct edmacc_param p_ram;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100145 int ret;
146
Vladimir Barinov310355c2008-02-18 11:40:22 +0100147 /* Request master DMA channel */
David Brownell82075af2009-05-14 12:41:22 -0700148 ret = edma_alloc_channel(prtd->params->channel,
Vladimir Barinov310355c2008-02-18 11:40:22 +0100149 davinci_pcm_dma_irq, substream,
David Brownell82075af2009-05-14 12:41:22 -0700150 EVENTQ_0);
151 if (ret < 0)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100152 return ret;
Troy Kisky1587ea312009-11-18 17:49:52 -0700153 prtd->asp_channel = ret;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100154
David Brownell82075af2009-05-14 12:41:22 -0700155 /* Request parameter RAM reload slot */
Troy Kisky1587ea312009-11-18 17:49:52 -0700156 ret = edma_alloc_slot(EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY);
David Brownell82075af2009-05-14 12:41:22 -0700157 if (ret < 0) {
Troy Kisky1587ea312009-11-18 17:49:52 -0700158 edma_free_channel(prtd->asp_channel);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100159 return ret;
160 }
Troy Kisky1587ea312009-11-18 17:49:52 -0700161 prtd->asp_link[0] = ret;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100162
David Brownell82075af2009-05-14 12:41:22 -0700163 /* Issue transfer completion IRQ when the channel completes a
164 * transfer, then always reload from the same slot (by a kind
165 * of loopback link). The completion IRQ handler will update
166 * the reload slot with a new buffer.
167 *
168 * REVISIT save p_ram here after setting up everything except
169 * the buffer and its length (ccnt) ... use it as a template
170 * so davinci_pcm_enqueue_dma() takes less time in IRQ.
171 */
Troy Kisky1587ea312009-11-18 17:49:52 -0700172 edma_read_slot(prtd->asp_link[0], &p_ram);
173 p_ram.opt |= TCINTEN | EDMA_TCC(EDMA_CHAN_SLOT(prtd->asp_channel));
174 p_ram.link_bcntrld = EDMA_CHAN_SLOT(prtd->asp_link[0]) << 5;
175 edma_write_slot(prtd->asp_link[0], &p_ram);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100176
177 return 0;
178}
179
180static int davinci_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
181{
182 struct davinci_runtime_data *prtd = substream->runtime->private_data;
183 int ret = 0;
184
185 spin_lock(&prtd->lock);
186
187 switch (cmd) {
188 case SNDRV_PCM_TRIGGER_START:
189 case SNDRV_PCM_TRIGGER_RESUME:
190 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Troy Kisky1587ea312009-11-18 17:49:52 -0700191 edma_start(prtd->asp_channel);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100192 break;
193 case SNDRV_PCM_TRIGGER_STOP:
194 case SNDRV_PCM_TRIGGER_SUSPEND:
195 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Troy Kisky1587ea312009-11-18 17:49:52 -0700196 edma_stop(prtd->asp_channel);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100197 break;
198 default:
199 ret = -EINVAL;
200 break;
201 }
202
203 spin_unlock(&prtd->lock);
204
205 return ret;
206}
207
208static int davinci_pcm_prepare(struct snd_pcm_substream *substream)
209{
210 struct davinci_runtime_data *prtd = substream->runtime->private_data;
David Brownell82075af2009-05-14 12:41:22 -0700211 struct edmacc_param temp;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100212
213 prtd->period = 0;
214 davinci_pcm_enqueue_dma(substream);
215
David Brownell82075af2009-05-14 12:41:22 -0700216 /* Copy self-linked parameter RAM entry into master channel */
Troy Kisky1587ea312009-11-18 17:49:52 -0700217 edma_read_slot(prtd->asp_link[0], &temp);
218 edma_write_slot(prtd->asp_channel, &temp);
Troy Kisky6e541472009-07-07 17:36:06 -0700219 davinci_pcm_enqueue_dma(substream);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100220
221 return 0;
222}
223
224static snd_pcm_uframes_t
225davinci_pcm_pointer(struct snd_pcm_substream *substream)
226{
227 struct snd_pcm_runtime *runtime = substream->runtime;
228 struct davinci_runtime_data *prtd = runtime->private_data;
229 unsigned int offset;
Troy Kisky1587ea312009-11-18 17:49:52 -0700230 int asp_count;
231 dma_addr_t asp_src, asp_dst;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100232
233 spin_lock(&prtd->lock);
234
Troy Kisky1587ea312009-11-18 17:49:52 -0700235 edma_get_position(prtd->asp_channel, &asp_src, &asp_dst);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100236 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Troy Kisky1587ea312009-11-18 17:49:52 -0700237 asp_count = asp_src - runtime->dma_addr;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100238 else
Troy Kisky1587ea312009-11-18 17:49:52 -0700239 asp_count = asp_dst - runtime->dma_addr;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100240
241 spin_unlock(&prtd->lock);
242
Troy Kisky1587ea312009-11-18 17:49:52 -0700243 offset = bytes_to_frames(runtime, asp_count);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100244 if (offset >= runtime->buffer_size)
245 offset = 0;
246
247 return offset;
248}
249
250static int davinci_pcm_open(struct snd_pcm_substream *substream)
251{
252 struct snd_pcm_runtime *runtime = substream->runtime;
253 struct davinci_runtime_data *prtd;
254 int ret = 0;
Troy Kisky81ac55a2009-09-11 14:29:02 -0700255 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Troy Kisky57512c62009-11-16 16:52:31 -0700256 struct davinci_pcm_dma_params *pa = rtd->dai->cpu_dai->dma_data;
257 struct davinci_pcm_dma_params *params;
258 if (!pa)
Troy Kisky81ac55a2009-09-11 14:29:02 -0700259 return -ENODEV;
Troy Kisky57512c62009-11-16 16:52:31 -0700260 params = &pa[substream->stream];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100261
262 snd_soc_set_runtime_hwparams(substream, &davinci_pcm_hardware);
Troy Kisky6a90d532009-08-06 16:55:32 -0700263 /* ensure that buffer size is a multiple of period size */
264 ret = snd_pcm_hw_constraint_integer(runtime,
265 SNDRV_PCM_HW_PARAM_PERIODS);
266 if (ret < 0)
267 return ret;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100268
269 prtd = kzalloc(sizeof(struct davinci_runtime_data), GFP_KERNEL);
270 if (prtd == NULL)
271 return -ENOMEM;
272
273 spin_lock_init(&prtd->lock);
Troy Kisky81ac55a2009-09-11 14:29:02 -0700274 prtd->params = params;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100275
276 runtime->private_data = prtd;
277
278 ret = davinci_pcm_dma_request(substream);
279 if (ret) {
280 printk(KERN_ERR "davinci_pcm: Failed to get dma channels\n");
281 kfree(prtd);
282 }
283
284 return ret;
285}
286
287static int davinci_pcm_close(struct snd_pcm_substream *substream)
288{
289 struct snd_pcm_runtime *runtime = substream->runtime;
290 struct davinci_runtime_data *prtd = runtime->private_data;
291
Troy Kisky1587ea312009-11-18 17:49:52 -0700292 edma_unlink(prtd->asp_link[0]);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100293
Troy Kisky1587ea312009-11-18 17:49:52 -0700294 edma_free_slot(prtd->asp_link[0]);
295 edma_free_channel(prtd->asp_channel);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100296
297 kfree(prtd);
298
299 return 0;
300}
301
302static int davinci_pcm_hw_params(struct snd_pcm_substream *substream,
303 struct snd_pcm_hw_params *hw_params)
304{
305 return snd_pcm_lib_malloc_pages(substream,
306 params_buffer_bytes(hw_params));
307}
308
309static int davinci_pcm_hw_free(struct snd_pcm_substream *substream)
310{
311 return snd_pcm_lib_free_pages(substream);
312}
313
314static int davinci_pcm_mmap(struct snd_pcm_substream *substream,
315 struct vm_area_struct *vma)
316{
317 struct snd_pcm_runtime *runtime = substream->runtime;
318
319 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
320 runtime->dma_area,
321 runtime->dma_addr,
322 runtime->dma_bytes);
323}
324
Mark Brownb2a19d02009-01-17 19:14:26 +0000325static struct snd_pcm_ops davinci_pcm_ops = {
Vladimir Barinov310355c2008-02-18 11:40:22 +0100326 .open = davinci_pcm_open,
327 .close = davinci_pcm_close,
328 .ioctl = snd_pcm_lib_ioctl,
329 .hw_params = davinci_pcm_hw_params,
330 .hw_free = davinci_pcm_hw_free,
331 .prepare = davinci_pcm_prepare,
332 .trigger = davinci_pcm_trigger,
333 .pointer = davinci_pcm_pointer,
334 .mmap = davinci_pcm_mmap,
335};
336
337static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
338{
339 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
340 struct snd_dma_buffer *buf = &substream->dma_buffer;
341 size_t size = davinci_pcm_hardware.buffer_bytes_max;
342
343 buf->dev.type = SNDRV_DMA_TYPE_DEV;
344 buf->dev.dev = pcm->card->dev;
345 buf->private_data = NULL;
346 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
347 &buf->addr, GFP_KERNEL);
348
Alexander Beregalov9cd28ab2008-12-13 16:25:27 +0300349 pr_debug("davinci_pcm: preallocate_dma_buffer: area=%p, addr=%p, "
350 "size=%d\n", (void *) buf->area, (void *) buf->addr, size);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100351
352 if (!buf->area)
353 return -ENOMEM;
354
355 buf->bytes = size;
356 return 0;
357}
358
359static void davinci_pcm_free(struct snd_pcm *pcm)
360{
361 struct snd_pcm_substream *substream;
362 struct snd_dma_buffer *buf;
363 int stream;
364
365 for (stream = 0; stream < 2; stream++) {
366 substream = pcm->streams[stream].substream;
367 if (!substream)
368 continue;
369
370 buf = &substream->dma_buffer;
371 if (!buf->area)
372 continue;
373
374 dma_free_writecombine(pcm->card->dev, buf->bytes,
375 buf->area, buf->addr);
376 buf->area = NULL;
377 }
378}
379
380static u64 davinci_pcm_dmamask = 0xffffffff;
381
382static int davinci_pcm_new(struct snd_card *card,
Liam Girdwood9cb132d2008-07-07 16:07:42 +0100383 struct snd_soc_dai *dai, struct snd_pcm *pcm)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100384{
385 int ret;
386
387 if (!card->dev->dma_mask)
388 card->dev->dma_mask = &davinci_pcm_dmamask;
389 if (!card->dev->coherent_dma_mask)
390 card->dev->coherent_dma_mask = 0xffffffff;
391
392 if (dai->playback.channels_min) {
393 ret = davinci_pcm_preallocate_dma_buffer(pcm,
394 SNDRV_PCM_STREAM_PLAYBACK);
395 if (ret)
396 return ret;
397 }
398
399 if (dai->capture.channels_min) {
400 ret = davinci_pcm_preallocate_dma_buffer(pcm,
401 SNDRV_PCM_STREAM_CAPTURE);
402 if (ret)
403 return ret;
404 }
405
406 return 0;
407}
408
409struct snd_soc_platform davinci_soc_platform = {
410 .name = "davinci-audio",
411 .pcm_ops = &davinci_pcm_ops,
412 .pcm_new = davinci_pcm_new,
413 .pcm_free = davinci_pcm_free,
414};
415EXPORT_SYMBOL_GPL(davinci_soc_platform);
416
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100417static int __init davinci_soc_platform_init(void)
Mark Brown958e7922008-12-03 19:58:17 +0000418{
419 return snd_soc_register_platform(&davinci_soc_platform);
420}
421module_init(davinci_soc_platform_init);
422
423static void __exit davinci_soc_platform_exit(void)
424{
425 snd_soc_unregister_platform(&davinci_soc_platform);
426}
427module_exit(davinci_soc_platform_exit);
428
Vladimir Barinov310355c2008-02-18 11:40:22 +0100429MODULE_AUTHOR("Vladimir Barinov");
430MODULE_DESCRIPTION("TI DAVINCI PCM DMA module");
431MODULE_LICENSE("GPL");