blob: fa8fc617c0db209f09c946b0bb98cc0f7dcdb232 [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>
Troy Kisky1e224f32009-11-18 17:49:53 -07006 * added SRAM ping/pong (C) 2008 Troy Kisky <troy.kisky@boundarydevices.com>
Vladimir Barinov310355c2008-02-18 11:40:22 +01007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/platform_device.h>
16#include <linux/slab.h>
17#include <linux/dma-mapping.h>
Alexander Beregalov9cd28ab2008-12-13 16:25:27 +030018#include <linux/kernel.h>
Vladimir Barinov310355c2008-02-18 11:40:22 +010019
20#include <sound/core.h>
21#include <sound/pcm.h>
22#include <sound/pcm_params.h>
23#include <sound/soc.h>
24
25#include <asm/dma.h>
David Brownell82075af2009-05-14 12:41:22 -070026#include <mach/edma.h>
Troy Kisky1e224f32009-11-18 17:49:53 -070027#include <mach/sram.h>
Vladimir Barinov310355c2008-02-18 11:40:22 +010028
29#include "davinci-pcm.h"
30
Troy Kisky1e224f32009-11-18 17:49:53 -070031#ifdef DEBUG
32static void print_buf_info(int slot, char *name)
33{
34 struct edmacc_param p;
35 if (slot < 0)
36 return;
37 edma_read_slot(slot, &p);
38 printk(KERN_DEBUG "%s: 0x%x, opt=%x, src=%x, a_b_cnt=%x dst=%x\n",
39 name, slot, p.opt, p.src, p.a_b_cnt, p.dst);
40 printk(KERN_DEBUG " src_dst_bidx=%x link_bcntrld=%x src_dst_cidx=%x ccnt=%x\n",
41 p.src_dst_bidx, p.link_bcntrld, p.src_dst_cidx, p.ccnt);
42}
43#else
44static void print_buf_info(int slot, char *name)
45{
46}
47#endif
48
Ben Gardiner8e56d5b2011-05-24 14:50:16 -040049#define DAVINCI_PCM_FMTBITS (\
50 SNDRV_PCM_FMTBIT_S8 |\
51 SNDRV_PCM_FMTBIT_U8 |\
52 SNDRV_PCM_FMTBIT_S16_LE |\
53 SNDRV_PCM_FMTBIT_S16_BE |\
54 SNDRV_PCM_FMTBIT_U16_LE |\
55 SNDRV_PCM_FMTBIT_U16_BE |\
56 SNDRV_PCM_FMTBIT_S24_LE |\
57 SNDRV_PCM_FMTBIT_S24_BE |\
58 SNDRV_PCM_FMTBIT_U24_LE |\
59 SNDRV_PCM_FMTBIT_U24_BE |\
60 SNDRV_PCM_FMTBIT_S32_LE |\
61 SNDRV_PCM_FMTBIT_S32_BE |\
62 SNDRV_PCM_FMTBIT_U32_LE |\
63 SNDRV_PCM_FMTBIT_U32_BE)
64
Troy Kisky1e224f32009-11-18 17:49:53 -070065static struct snd_pcm_hardware pcm_hardware_playback = {
Vladimir Barinov310355c2008-02-18 11:40:22 +010066 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
67 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
Ben Gardiner52e2c5d2011-05-24 14:50:20 -040068 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME|
69 SNDRV_PCM_INFO_BATCH),
Ben Gardiner8e56d5b2011-05-24 14:50:16 -040070 .formats = DAVINCI_PCM_FMTBITS,
Vladimir Barinov310355c2008-02-18 11:40:22 +010071 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
72 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
73 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
74 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
75 SNDRV_PCM_RATE_KNOT),
76 .rate_min = 8000,
77 .rate_max = 96000,
78 .channels_min = 2,
Ben Gardineracb8e262011-05-24 14:50:17 -040079 .channels_max = 384,
Vladimir Barinov310355c2008-02-18 11:40:22 +010080 .buffer_bytes_max = 128 * 1024,
81 .period_bytes_min = 32,
82 .period_bytes_max = 8 * 1024,
83 .periods_min = 16,
84 .periods_max = 255,
85 .fifo_size = 0,
86};
87
Troy Kisky1e224f32009-11-18 17:49:53 -070088static struct snd_pcm_hardware pcm_hardware_capture = {
89 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
90 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
Ben Gardiner52e2c5d2011-05-24 14:50:20 -040091 SNDRV_PCM_INFO_PAUSE |
92 SNDRV_PCM_INFO_BATCH),
Ben Gardiner8e56d5b2011-05-24 14:50:16 -040093 .formats = DAVINCI_PCM_FMTBITS,
Troy Kisky1e224f32009-11-18 17:49:53 -070094 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
95 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
96 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
97 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
98 SNDRV_PCM_RATE_KNOT),
99 .rate_min = 8000,
100 .rate_max = 96000,
101 .channels_min = 2,
Ben Gardineracb8e262011-05-24 14:50:17 -0400102 .channels_max = 384,
Troy Kisky1e224f32009-11-18 17:49:53 -0700103 .buffer_bytes_max = 128 * 1024,
104 .period_bytes_min = 32,
105 .period_bytes_max = 8 * 1024,
106 .periods_min = 16,
107 .periods_max = 255,
108 .fifo_size = 0,
109};
110
111/*
112 * How ping/pong works....
113 *
114 * Playback:
115 * ram_params - copys 2*ping_size from start of SDRAM to iram,
116 * links to ram_link2
117 * ram_link2 - copys rest of SDRAM to iram in ping_size units,
118 * links to ram_link
119 * ram_link - copys entire SDRAM to iram in ping_size uints,
120 * links to self
121 *
122 * asp_params - same as asp_link[0]
123 * asp_link[0] - copys from lower half of iram to asp port
124 * links to asp_link[1], triggers iram copy event on completion
125 * asp_link[1] - copys from upper half of iram to asp port
126 * links to asp_link[0], triggers iram copy event on completion
127 * triggers interrupt only needed to let upper SOC levels update position
128 * in stream on completion
129 *
130 * When playback is started:
131 * ram_params started
132 * asp_params started
133 *
134 * Capture:
135 * ram_params - same as ram_link,
136 * links to ram_link
137 * ram_link - same as playback
138 * links to self
139 *
140 * asp_params - same as playback
141 * asp_link[0] - same as playback
142 * asp_link[1] - same as playback
143 *
144 * When capture is started:
145 * asp_params started
146 */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100147struct davinci_runtime_data {
148 spinlock_t lock;
149 int period; /* current DMA period */
Troy Kisky1587ea312009-11-18 17:49:52 -0700150 int asp_channel; /* Master DMA channel */
151 int asp_link[2]; /* asp parameter link channel, ping/pong */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100152 struct davinci_pcm_dma_params *params; /* DMA params */
Troy Kisky1e224f32009-11-18 17:49:53 -0700153 int ram_channel;
154 int ram_link;
155 int ram_link2;
156 struct edmacc_param asp_params;
157 struct edmacc_param ram_params;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100158};
159
Ben Gardiner10ab3bf2011-05-24 14:50:19 -0400160static void davinci_pcm_period_elapsed(struct snd_pcm_substream *substream)
161{
162 struct davinci_runtime_data *prtd = substream->runtime->private_data;
163 struct snd_pcm_runtime *runtime = substream->runtime;
164
165 prtd->period++;
166 if (unlikely(prtd->period >= runtime->periods))
167 prtd->period = 0;
168}
169
170static void davinci_pcm_period_reset(struct snd_pcm_substream *substream)
171{
172 struct davinci_runtime_data *prtd = substream->runtime->private_data;
173
174 prtd->period = 0;
175}
Troy Kisky1e224f32009-11-18 17:49:53 -0700176/*
177 * Not used with ping/pong
178 */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100179static void davinci_pcm_enqueue_dma(struct snd_pcm_substream *substream)
180{
181 struct davinci_runtime_data *prtd = substream->runtime->private_data;
182 struct snd_pcm_runtime *runtime = substream->runtime;
Troy Kisky1587ea312009-11-18 17:49:52 -0700183 int link = prtd->asp_link[0];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100184 unsigned int period_size;
185 unsigned int dma_offset;
186 dma_addr_t dma_pos;
187 dma_addr_t src, dst;
188 unsigned short src_bidx, dst_bidx;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400189 unsigned short src_cidx, dst_cidx;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100190 unsigned int data_type;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400191 unsigned short acnt;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100192 unsigned int count;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400193 unsigned int fifo_level;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100194
195 period_size = snd_pcm_lib_period_bytes(substream);
196 dma_offset = prtd->period * period_size;
197 dma_pos = runtime->dma_addr + dma_offset;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400198 fifo_level = prtd->params->fifo_level;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100199
Alexander Beregalov9cd28ab2008-12-13 16:25:27 +0300200 pr_debug("davinci_pcm: audio_set_dma_params_play channel = %d "
Troy Kisky1587ea312009-11-18 17:49:52 -0700201 "dma_ptr = %x period_size=%x\n", link, dma_pos, period_size);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100202
203 data_type = prtd->params->data_type;
204 count = period_size / data_type;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400205 if (fifo_level)
206 count /= fifo_level;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100207
208 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
209 src = dma_pos;
210 dst = prtd->params->dma_addr;
211 src_bidx = data_type;
212 dst_bidx = 0;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400213 src_cidx = data_type * fifo_level;
214 dst_cidx = 0;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100215 } else {
216 src = prtd->params->dma_addr;
217 dst = dma_pos;
218 src_bidx = 0;
219 dst_bidx = data_type;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400220 src_cidx = 0;
221 dst_cidx = data_type * fifo_level;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100222 }
223
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400224 acnt = prtd->params->acnt;
Troy Kisky1587ea312009-11-18 17:49:52 -0700225 edma_set_src(link, src, INCR, W8BIT);
226 edma_set_dest(link, dst, INCR, W8BIT);
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400227
Troy Kisky1587ea312009-11-18 17:49:52 -0700228 edma_set_src_index(link, src_bidx, src_cidx);
229 edma_set_dest_index(link, dst_bidx, dst_cidx);
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400230
231 if (!fifo_level)
Troy Kisky1587ea312009-11-18 17:49:52 -0700232 edma_set_transfer_params(link, acnt, count, 1, 0, ASYNC);
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400233 else
Troy Kisky1587ea312009-11-18 17:49:52 -0700234 edma_set_transfer_params(link, acnt, fifo_level, count,
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400235 fifo_level, ABSYNC);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100236}
237
Troy Kisky1587ea312009-11-18 17:49:52 -0700238static void davinci_pcm_dma_irq(unsigned link, u16 ch_status, void *data)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100239{
240 struct snd_pcm_substream *substream = data;
241 struct davinci_runtime_data *prtd = substream->runtime->private_data;
242
Troy Kisky1e224f32009-11-18 17:49:53 -0700243 print_buf_info(prtd->ram_channel, "i ram_channel");
Troy Kisky1587ea312009-11-18 17:49:52 -0700244 pr_debug("davinci_pcm: link=%d, status=0x%x\n", link, ch_status);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100245
246 if (unlikely(ch_status != DMA_COMPLETE))
247 return;
248
249 if (snd_pcm_running(substream)) {
Ben Gardiner52e2c5d2011-05-24 14:50:20 -0400250 spin_lock(&prtd->lock);
Troy Kisky1e224f32009-11-18 17:49:53 -0700251 if (prtd->ram_channel < 0) {
252 /* No ping/pong must fix up link dma data*/
Troy Kisky1e224f32009-11-18 17:49:53 -0700253 davinci_pcm_enqueue_dma(substream);
Troy Kisky1e224f32009-11-18 17:49:53 -0700254 }
Ben Gardiner52e2c5d2011-05-24 14:50:20 -0400255 davinci_pcm_period_elapsed(substream);
256 spin_unlock(&prtd->lock);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100257 snd_pcm_period_elapsed(substream);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100258 }
259}
260
Troy Kisky1e224f32009-11-18 17:49:53 -0700261static int allocate_sram(struct snd_pcm_substream *substream, unsigned size,
262 struct snd_pcm_hardware *ppcm)
263{
264 struct snd_dma_buffer *buf = &substream->dma_buffer;
265 struct snd_dma_buffer *iram_dma = NULL;
266 dma_addr_t iram_phys = 0;
267 void *iram_virt = NULL;
268
269 if (buf->private_data || !size)
270 return 0;
271
272 ppcm->period_bytes_max = size;
273 iram_virt = sram_alloc(size, &iram_phys);
274 if (!iram_virt)
275 goto exit1;
276 iram_dma = kzalloc(sizeof(*iram_dma), GFP_KERNEL);
277 if (!iram_dma)
278 goto exit2;
279 iram_dma->area = iram_virt;
280 iram_dma->addr = iram_phys;
281 memset(iram_dma->area, 0, size);
282 iram_dma->bytes = size;
283 buf->private_data = iram_dma;
284 return 0;
285exit2:
286 if (iram_virt)
287 sram_free(iram_virt, size);
288exit1:
289 return -ENOMEM;
290}
291
292/*
293 * Only used with ping/pong.
294 * This is called after runtime->dma_addr, period_bytes and data_type are valid
295 */
296static int ping_pong_dma_setup(struct snd_pcm_substream *substream)
297{
298 unsigned short ram_src_cidx, ram_dst_cidx;
299 struct snd_pcm_runtime *runtime = substream->runtime;
300 struct davinci_runtime_data *prtd = runtime->private_data;
301 struct snd_dma_buffer *iram_dma =
302 (struct snd_dma_buffer *)substream->dma_buffer.private_data;
303 struct davinci_pcm_dma_params *params = prtd->params;
304 unsigned int data_type = params->data_type;
305 unsigned int acnt = params->acnt;
306 /* divide by 2 for ping/pong */
307 unsigned int ping_size = snd_pcm_lib_period_bytes(substream) >> 1;
308 int link = prtd->asp_link[1];
309 unsigned int fifo_level = prtd->params->fifo_level;
310 unsigned int count;
311 if ((data_type == 0) || (data_type > 4)) {
312 printk(KERN_ERR "%s: data_type=%i\n", __func__, data_type);
313 return -EINVAL;
314 }
315 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
316 dma_addr_t asp_src_pong = iram_dma->addr + ping_size;
317 ram_src_cidx = ping_size;
318 ram_dst_cidx = -ping_size;
319 edma_set_src(link, asp_src_pong, INCR, W8BIT);
320
321 link = prtd->asp_link[0];
322 edma_set_src_index(link, data_type, data_type * fifo_level);
323 link = prtd->asp_link[1];
324 edma_set_src_index(link, data_type, data_type * fifo_level);
325
326 link = prtd->ram_link;
327 edma_set_src(link, runtime->dma_addr, INCR, W32BIT);
328 } else {
329 dma_addr_t asp_dst_pong = iram_dma->addr + ping_size;
330 ram_src_cidx = -ping_size;
331 ram_dst_cidx = ping_size;
332 edma_set_dest(link, asp_dst_pong, INCR, W8BIT);
333
334 link = prtd->asp_link[0];
335 edma_set_dest_index(link, data_type, data_type * fifo_level);
336 link = prtd->asp_link[1];
337 edma_set_dest_index(link, data_type, data_type * fifo_level);
338
339 link = prtd->ram_link;
340 edma_set_dest(link, runtime->dma_addr, INCR, W32BIT);
341 }
342
343 if (!fifo_level) {
344 count = ping_size / data_type;
345 edma_set_transfer_params(prtd->asp_link[0], acnt, count,
346 1, 0, ASYNC);
347 edma_set_transfer_params(prtd->asp_link[1], acnt, count,
348 1, 0, ASYNC);
349 } else {
350 count = ping_size / (data_type * fifo_level);
351 edma_set_transfer_params(prtd->asp_link[0], acnt, fifo_level,
352 count, fifo_level, ABSYNC);
353 edma_set_transfer_params(prtd->asp_link[1], acnt, fifo_level,
354 count, fifo_level, ABSYNC);
355 }
356
357 link = prtd->ram_link;
358 edma_set_src_index(link, ping_size, ram_src_cidx);
359 edma_set_dest_index(link, ping_size, ram_dst_cidx);
360 edma_set_transfer_params(link, ping_size, 2,
361 runtime->periods, 2, ASYNC);
362
363 /* init master params */
364 edma_read_slot(prtd->asp_link[0], &prtd->asp_params);
365 edma_read_slot(prtd->ram_link, &prtd->ram_params);
366 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
367 struct edmacc_param p_ram;
368 /* Copy entire iram buffer before playback started */
369 prtd->ram_params.a_b_cnt = (1 << 16) | (ping_size << 1);
370 /* 0 dst_bidx */
371 prtd->ram_params.src_dst_bidx = (ping_size << 1);
372 /* 0 dst_cidx */
373 prtd->ram_params.src_dst_cidx = (ping_size << 1);
374 prtd->ram_params.ccnt = 1;
375
376 /* Skip 1st period */
377 edma_read_slot(prtd->ram_link, &p_ram);
378 p_ram.src += (ping_size << 1);
379 p_ram.ccnt -= 1;
380 edma_write_slot(prtd->ram_link2, &p_ram);
381 /*
382 * When 1st started, ram -> iram dma channel will fill the
383 * entire iram. Then, whenever a ping/pong asp buffer finishes,
384 * 1/2 iram will be filled.
385 */
386 prtd->ram_params.link_bcntrld =
387 EDMA_CHAN_SLOT(prtd->ram_link2) << 5;
388 }
389 return 0;
390}
391
392/* 1 asp tx or rx channel using 2 parameter channels
393 * 1 ram to/from iram channel using 1 parameter channel
394 *
395 * Playback
396 * ram copy channel kicks off first,
397 * 1st ram copy of entire iram buffer completion kicks off asp channel
398 * asp tcc always kicks off ram copy of 1/2 iram buffer
399 *
400 * Record
401 * asp channel starts, tcc kicks off ram copy
402 */
403static int request_ping_pong(struct snd_pcm_substream *substream,
404 struct davinci_runtime_data *prtd,
405 struct snd_dma_buffer *iram_dma)
406{
407 dma_addr_t asp_src_ping;
408 dma_addr_t asp_dst_ping;
409 int link;
410 struct davinci_pcm_dma_params *params = prtd->params;
411
412 /* Request ram master channel */
413 link = prtd->ram_channel = edma_alloc_channel(EDMA_CHANNEL_ANY,
414 davinci_pcm_dma_irq, substream,
Sekhar Nori48519f02010-07-19 12:31:16 +0530415 prtd->params->ram_chan_q);
Troy Kisky1e224f32009-11-18 17:49:53 -0700416 if (link < 0)
417 goto exit1;
418
419 /* Request ram link channel */
420 link = prtd->ram_link = edma_alloc_slot(
421 EDMA_CTLR(prtd->ram_channel), EDMA_SLOT_ANY);
422 if (link < 0)
423 goto exit2;
424
425 link = prtd->asp_link[1] = edma_alloc_slot(
426 EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY);
427 if (link < 0)
428 goto exit3;
429
430 prtd->ram_link2 = -1;
431 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
432 link = prtd->ram_link2 = edma_alloc_slot(
433 EDMA_CTLR(prtd->ram_channel), EDMA_SLOT_ANY);
434 if (link < 0)
435 goto exit4;
436 }
437 /* circle ping-pong buffers */
438 edma_link(prtd->asp_link[0], prtd->asp_link[1]);
439 edma_link(prtd->asp_link[1], prtd->asp_link[0]);
440 /* circle ram buffers */
441 edma_link(prtd->ram_link, prtd->ram_link);
442
443 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
444 asp_src_ping = iram_dma->addr;
445 asp_dst_ping = params->dma_addr; /* fifo */
446 } else {
447 asp_src_ping = params->dma_addr; /* fifo */
448 asp_dst_ping = iram_dma->addr;
449 }
450 /* ping */
451 link = prtd->asp_link[0];
452 edma_set_src(link, asp_src_ping, INCR, W16BIT);
453 edma_set_dest(link, asp_dst_ping, INCR, W16BIT);
454 edma_set_src_index(link, 0, 0);
455 edma_set_dest_index(link, 0, 0);
456
457 edma_read_slot(link, &prtd->asp_params);
458 prtd->asp_params.opt &= ~(TCCMODE | EDMA_TCC(0x3f) | TCINTEN);
Ben Gardinerfb1e97032011-05-24 14:50:15 -0400459 prtd->asp_params.opt |= TCCHEN |
460 EDMA_TCC(prtd->ram_channel & 0x3f);
Troy Kisky1e224f32009-11-18 17:49:53 -0700461 edma_write_slot(link, &prtd->asp_params);
462
463 /* pong */
464 link = prtd->asp_link[1];
465 edma_set_src(link, asp_src_ping, INCR, W16BIT);
466 edma_set_dest(link, asp_dst_ping, INCR, W16BIT);
467 edma_set_src_index(link, 0, 0);
468 edma_set_dest_index(link, 0, 0);
469
470 edma_read_slot(link, &prtd->asp_params);
471 prtd->asp_params.opt &= ~(TCCMODE | EDMA_TCC(0x3f));
472 /* interrupt after every pong completion */
473 prtd->asp_params.opt |= TCINTEN | TCCHEN |
Ben Gardinerfb1e97032011-05-24 14:50:15 -0400474 EDMA_TCC(prtd->ram_channel & 0x3f);
Troy Kisky1e224f32009-11-18 17:49:53 -0700475 edma_write_slot(link, &prtd->asp_params);
476
477 /* ram */
478 link = prtd->ram_link;
479 edma_set_src(link, iram_dma->addr, INCR, W32BIT);
480 edma_set_dest(link, iram_dma->addr, INCR, W32BIT);
481 pr_debug("%s: audio dma channels/slots in use for ram:%u %u %u,"
482 "for asp:%u %u %u\n", __func__,
483 prtd->ram_channel, prtd->ram_link, prtd->ram_link2,
484 prtd->asp_channel, prtd->asp_link[0],
485 prtd->asp_link[1]);
486 return 0;
487exit4:
488 edma_free_channel(prtd->asp_link[1]);
489 prtd->asp_link[1] = -1;
490exit3:
491 edma_free_channel(prtd->ram_link);
492 prtd->ram_link = -1;
493exit2:
494 edma_free_channel(prtd->ram_channel);
495 prtd->ram_channel = -1;
496exit1:
497 return link;
498}
499
Vladimir Barinov310355c2008-02-18 11:40:22 +0100500static int davinci_pcm_dma_request(struct snd_pcm_substream *substream)
501{
Troy Kisky1e224f32009-11-18 17:49:53 -0700502 struct snd_dma_buffer *iram_dma;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100503 struct davinci_runtime_data *prtd = substream->runtime->private_data;
Troy Kisky1e224f32009-11-18 17:49:53 -0700504 struct davinci_pcm_dma_params *params = prtd->params;
505 int link;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100506
Troy Kisky1e224f32009-11-18 17:49:53 -0700507 if (!params)
508 return -ENODEV;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100509
Troy Kisky1e224f32009-11-18 17:49:53 -0700510 /* Request asp master DMA channel */
511 link = prtd->asp_channel = edma_alloc_channel(params->channel,
Sekhar Nori48519f02010-07-19 12:31:16 +0530512 davinci_pcm_dma_irq, substream,
513 prtd->params->asp_chan_q);
Troy Kisky1e224f32009-11-18 17:49:53 -0700514 if (link < 0)
515 goto exit1;
516
517 /* Request asp link channels */
518 link = prtd->asp_link[0] = edma_alloc_slot(
519 EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY);
520 if (link < 0)
521 goto exit2;
522
523 iram_dma = (struct snd_dma_buffer *)substream->dma_buffer.private_data;
524 if (iram_dma) {
525 if (request_ping_pong(substream, prtd, iram_dma) == 0)
526 return 0;
527 printk(KERN_WARNING "%s: dma channel allocation failed,"
528 "not using sram\n", __func__);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100529 }
530
David Brownell82075af2009-05-14 12:41:22 -0700531 /* Issue transfer completion IRQ when the channel completes a
532 * transfer, then always reload from the same slot (by a kind
533 * of loopback link). The completion IRQ handler will update
534 * the reload slot with a new buffer.
535 *
536 * REVISIT save p_ram here after setting up everything except
537 * the buffer and its length (ccnt) ... use it as a template
538 * so davinci_pcm_enqueue_dma() takes less time in IRQ.
539 */
Troy Kisky1e224f32009-11-18 17:49:53 -0700540 edma_read_slot(link, &prtd->asp_params);
541 prtd->asp_params.opt |= TCINTEN |
542 EDMA_TCC(EDMA_CHAN_SLOT(prtd->asp_channel));
543 prtd->asp_params.link_bcntrld = EDMA_CHAN_SLOT(link) << 5;
544 edma_write_slot(link, &prtd->asp_params);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100545 return 0;
Troy Kisky1e224f32009-11-18 17:49:53 -0700546exit2:
547 edma_free_channel(prtd->asp_channel);
548 prtd->asp_channel = -1;
549exit1:
550 return link;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100551}
552
553static int davinci_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
554{
555 struct davinci_runtime_data *prtd = substream->runtime->private_data;
556 int ret = 0;
557
558 spin_lock(&prtd->lock);
559
560 switch (cmd) {
561 case SNDRV_PCM_TRIGGER_START:
Ben Gardineref39eb62011-05-24 14:50:18 -0400562 edma_start(prtd->asp_channel);
563 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
564 prtd->ram_channel >= 0) {
565 /* copy 1st iram buffer */
566 edma_start(prtd->ram_channel);
567 }
568 break;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100569 case SNDRV_PCM_TRIGGER_RESUME:
570 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Troy Kisky2b7b2502009-11-18 17:49:54 -0700571 edma_resume(prtd->asp_channel);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100572 break;
573 case SNDRV_PCM_TRIGGER_STOP:
574 case SNDRV_PCM_TRIGGER_SUSPEND:
575 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Troy Kisky2b7b2502009-11-18 17:49:54 -0700576 edma_pause(prtd->asp_channel);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100577 break;
578 default:
579 ret = -EINVAL;
580 break;
581 }
582
583 spin_unlock(&prtd->lock);
584
585 return ret;
586}
587
588static int davinci_pcm_prepare(struct snd_pcm_substream *substream)
589{
590 struct davinci_runtime_data *prtd = substream->runtime->private_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100591
Ben Gardiner52e2c5d2011-05-24 14:50:20 -0400592 davinci_pcm_period_reset(substream);
Troy Kisky1e224f32009-11-18 17:49:53 -0700593 if (prtd->ram_channel >= 0) {
594 int ret = ping_pong_dma_setup(substream);
595 if (ret < 0)
596 return ret;
597
598 edma_write_slot(prtd->ram_channel, &prtd->ram_params);
599 edma_write_slot(prtd->asp_channel, &prtd->asp_params);
600
601 print_buf_info(prtd->ram_channel, "ram_channel");
602 print_buf_info(prtd->ram_link, "ram_link");
603 print_buf_info(prtd->ram_link2, "ram_link2");
604 print_buf_info(prtd->asp_channel, "asp_channel");
605 print_buf_info(prtd->asp_link[0], "asp_link[0]");
606 print_buf_info(prtd->asp_link[1], "asp_link[1]");
607
Ben Gardiner52e2c5d2011-05-24 14:50:20 -0400608 davinci_pcm_period_elapsed(substream);
609 davinci_pcm_period_elapsed(substream);
610
Troy Kisky1e224f32009-11-18 17:49:53 -0700611 return 0;
612 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100613 davinci_pcm_enqueue_dma(substream);
Ben Gardiner52e2c5d2011-05-24 14:50:20 -0400614 davinci_pcm_period_elapsed(substream);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100615
David Brownell82075af2009-05-14 12:41:22 -0700616 /* Copy self-linked parameter RAM entry into master channel */
Troy Kisky1e224f32009-11-18 17:49:53 -0700617 edma_read_slot(prtd->asp_link[0], &prtd->asp_params);
618 edma_write_slot(prtd->asp_channel, &prtd->asp_params);
Troy Kisky6e541472009-07-07 17:36:06 -0700619 davinci_pcm_enqueue_dma(substream);
Ben Gardiner52e2c5d2011-05-24 14:50:20 -0400620 davinci_pcm_period_elapsed(substream);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100621
622 return 0;
623}
624
625static snd_pcm_uframes_t
626davinci_pcm_pointer(struct snd_pcm_substream *substream)
627{
628 struct snd_pcm_runtime *runtime = substream->runtime;
629 struct davinci_runtime_data *prtd = runtime->private_data;
630 unsigned int offset;
Troy Kisky1587ea312009-11-18 17:49:52 -0700631 int asp_count;
Ben Gardiner52e2c5d2011-05-24 14:50:20 -0400632 unsigned int period_size = snd_pcm_lib_period_bytes(substream);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100633
634 spin_lock(&prtd->lock);
Ben Gardiner52e2c5d2011-05-24 14:50:20 -0400635 asp_count = prtd->period - 2;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100636 spin_unlock(&prtd->lock);
637
Ben Gardiner52e2c5d2011-05-24 14:50:20 -0400638 if (asp_count < 0)
639 asp_count += runtime->periods;
640 asp_count *= period_size;
641
Troy Kisky1587ea312009-11-18 17:49:52 -0700642 offset = bytes_to_frames(runtime, asp_count);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100643 if (offset >= runtime->buffer_size)
644 offset = 0;
645
646 return offset;
647}
648
649static int davinci_pcm_open(struct snd_pcm_substream *substream)
650{
651 struct snd_pcm_runtime *runtime = substream->runtime;
652 struct davinci_runtime_data *prtd;
Troy Kisky1e224f32009-11-18 17:49:53 -0700653 struct snd_pcm_hardware *ppcm;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100654 int ret = 0;
Troy Kisky81ac55a2009-09-11 14:29:02 -0700655 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Daniel Mack5f712b22010-03-22 10:11:15 +0100656 struct davinci_pcm_dma_params *pa;
Troy Kisky57512c62009-11-16 16:52:31 -0700657 struct davinci_pcm_dma_params *params;
Daniel Mack5f712b22010-03-22 10:11:15 +0100658
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000659 pa = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Troy Kisky57512c62009-11-16 16:52:31 -0700660 if (!pa)
Troy Kisky81ac55a2009-09-11 14:29:02 -0700661 return -ENODEV;
Troy Kisky57512c62009-11-16 16:52:31 -0700662 params = &pa[substream->stream];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100663
Troy Kisky1e224f32009-11-18 17:49:53 -0700664 ppcm = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
665 &pcm_hardware_playback : &pcm_hardware_capture;
666 allocate_sram(substream, params->sram_size, ppcm);
667 snd_soc_set_runtime_hwparams(substream, ppcm);
Troy Kisky6a90d532009-08-06 16:55:32 -0700668 /* ensure that buffer size is a multiple of period size */
669 ret = snd_pcm_hw_constraint_integer(runtime,
670 SNDRV_PCM_HW_PARAM_PERIODS);
671 if (ret < 0)
672 return ret;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100673
674 prtd = kzalloc(sizeof(struct davinci_runtime_data), GFP_KERNEL);
675 if (prtd == NULL)
676 return -ENOMEM;
677
678 spin_lock_init(&prtd->lock);
Troy Kisky81ac55a2009-09-11 14:29:02 -0700679 prtd->params = params;
Troy Kisky1e224f32009-11-18 17:49:53 -0700680 prtd->asp_channel = -1;
681 prtd->asp_link[0] = prtd->asp_link[1] = -1;
682 prtd->ram_channel = -1;
683 prtd->ram_link = -1;
684 prtd->ram_link2 = -1;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100685
686 runtime->private_data = prtd;
687
688 ret = davinci_pcm_dma_request(substream);
689 if (ret) {
690 printk(KERN_ERR "davinci_pcm: Failed to get dma channels\n");
691 kfree(prtd);
692 }
693
694 return ret;
695}
696
697static int davinci_pcm_close(struct snd_pcm_substream *substream)
698{
699 struct snd_pcm_runtime *runtime = substream->runtime;
700 struct davinci_runtime_data *prtd = runtime->private_data;
701
Troy Kisky1e224f32009-11-18 17:49:53 -0700702 if (prtd->ram_channel >= 0)
703 edma_stop(prtd->ram_channel);
704 if (prtd->asp_channel >= 0)
705 edma_stop(prtd->asp_channel);
706 if (prtd->asp_link[0] >= 0)
707 edma_unlink(prtd->asp_link[0]);
708 if (prtd->asp_link[1] >= 0)
709 edma_unlink(prtd->asp_link[1]);
710 if (prtd->ram_link >= 0)
711 edma_unlink(prtd->ram_link);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100712
Troy Kisky1e224f32009-11-18 17:49:53 -0700713 if (prtd->asp_link[0] >= 0)
714 edma_free_slot(prtd->asp_link[0]);
715 if (prtd->asp_link[1] >= 0)
716 edma_free_slot(prtd->asp_link[1]);
717 if (prtd->asp_channel >= 0)
718 edma_free_channel(prtd->asp_channel);
719 if (prtd->ram_link >= 0)
720 edma_free_slot(prtd->ram_link);
721 if (prtd->ram_link2 >= 0)
722 edma_free_slot(prtd->ram_link2);
723 if (prtd->ram_channel >= 0)
724 edma_free_channel(prtd->ram_channel);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100725
726 kfree(prtd);
727
728 return 0;
729}
730
731static int davinci_pcm_hw_params(struct snd_pcm_substream *substream,
732 struct snd_pcm_hw_params *hw_params)
733{
734 return snd_pcm_lib_malloc_pages(substream,
735 params_buffer_bytes(hw_params));
736}
737
738static int davinci_pcm_hw_free(struct snd_pcm_substream *substream)
739{
740 return snd_pcm_lib_free_pages(substream);
741}
742
743static int davinci_pcm_mmap(struct snd_pcm_substream *substream,
744 struct vm_area_struct *vma)
745{
746 struct snd_pcm_runtime *runtime = substream->runtime;
747
748 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
749 runtime->dma_area,
750 runtime->dma_addr,
751 runtime->dma_bytes);
752}
753
Mark Brownb2a19d02009-01-17 19:14:26 +0000754static struct snd_pcm_ops davinci_pcm_ops = {
Vladimir Barinov310355c2008-02-18 11:40:22 +0100755 .open = davinci_pcm_open,
756 .close = davinci_pcm_close,
757 .ioctl = snd_pcm_lib_ioctl,
758 .hw_params = davinci_pcm_hw_params,
759 .hw_free = davinci_pcm_hw_free,
760 .prepare = davinci_pcm_prepare,
761 .trigger = davinci_pcm_trigger,
762 .pointer = davinci_pcm_pointer,
763 .mmap = davinci_pcm_mmap,
764};
765
Troy Kisky1e224f32009-11-18 17:49:53 -0700766static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream,
767 size_t size)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100768{
769 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
770 struct snd_dma_buffer *buf = &substream->dma_buffer;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100771
772 buf->dev.type = SNDRV_DMA_TYPE_DEV;
773 buf->dev.dev = pcm->card->dev;
774 buf->private_data = NULL;
775 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
776 &buf->addr, GFP_KERNEL);
777
Alexander Beregalov9cd28ab2008-12-13 16:25:27 +0300778 pr_debug("davinci_pcm: preallocate_dma_buffer: area=%p, addr=%p, "
779 "size=%d\n", (void *) buf->area, (void *) buf->addr, size);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100780
781 if (!buf->area)
782 return -ENOMEM;
783
784 buf->bytes = size;
785 return 0;
786}
787
788static void davinci_pcm_free(struct snd_pcm *pcm)
789{
790 struct snd_pcm_substream *substream;
791 struct snd_dma_buffer *buf;
792 int stream;
793
794 for (stream = 0; stream < 2; stream++) {
Troy Kisky1e224f32009-11-18 17:49:53 -0700795 struct snd_dma_buffer *iram_dma;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100796 substream = pcm->streams[stream].substream;
797 if (!substream)
798 continue;
799
800 buf = &substream->dma_buffer;
801 if (!buf->area)
802 continue;
803
804 dma_free_writecombine(pcm->card->dev, buf->bytes,
805 buf->area, buf->addr);
806 buf->area = NULL;
Joe Perches4726a572010-07-12 13:50:27 -0700807 iram_dma = buf->private_data;
Troy Kisky1e224f32009-11-18 17:49:53 -0700808 if (iram_dma) {
809 sram_free(iram_dma->area, iram_dma->bytes);
810 kfree(iram_dma);
811 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100812 }
813}
814
815static u64 davinci_pcm_dmamask = 0xffffffff;
816
817static int davinci_pcm_new(struct snd_card *card,
Liam Girdwood9cb132d2008-07-07 16:07:42 +0100818 struct snd_soc_dai *dai, struct snd_pcm *pcm)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100819{
820 int ret;
821
822 if (!card->dev->dma_mask)
823 card->dev->dma_mask = &davinci_pcm_dmamask;
824 if (!card->dev->coherent_dma_mask)
825 card->dev->coherent_dma_mask = 0xffffffff;
826
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000827 if (dai->driver->playback.channels_min) {
Vladimir Barinov310355c2008-02-18 11:40:22 +0100828 ret = davinci_pcm_preallocate_dma_buffer(pcm,
Troy Kisky1e224f32009-11-18 17:49:53 -0700829 SNDRV_PCM_STREAM_PLAYBACK,
830 pcm_hardware_playback.buffer_bytes_max);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100831 if (ret)
832 return ret;
833 }
834
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000835 if (dai->driver->capture.channels_min) {
Vladimir Barinov310355c2008-02-18 11:40:22 +0100836 ret = davinci_pcm_preallocate_dma_buffer(pcm,
Troy Kisky1e224f32009-11-18 17:49:53 -0700837 SNDRV_PCM_STREAM_CAPTURE,
838 pcm_hardware_capture.buffer_bytes_max);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100839 if (ret)
840 return ret;
841 }
842
843 return 0;
844}
845
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000846static struct snd_soc_platform_driver davinci_soc_platform = {
847 .ops = &davinci_pcm_ops,
Vladimir Barinov310355c2008-02-18 11:40:22 +0100848 .pcm_new = davinci_pcm_new,
849 .pcm_free = davinci_pcm_free,
850};
Vladimir Barinov310355c2008-02-18 11:40:22 +0100851
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000852static int __devinit davinci_soc_platform_probe(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000853{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000854 return snd_soc_register_platform(&pdev->dev, &davinci_soc_platform);
Mark Brown958e7922008-12-03 19:58:17 +0000855}
Mark Brown958e7922008-12-03 19:58:17 +0000856
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000857static int __devexit davinci_soc_platform_remove(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000858{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000859 snd_soc_unregister_platform(&pdev->dev);
860 return 0;
Mark Brown958e7922008-12-03 19:58:17 +0000861}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000862
863static struct platform_driver davinci_pcm_driver = {
864 .driver = {
865 .name = "davinci-pcm-audio",
866 .owner = THIS_MODULE,
867 },
868
869 .probe = davinci_soc_platform_probe,
870 .remove = __devexit_p(davinci_soc_platform_remove),
871};
872
873static int __init snd_davinci_pcm_init(void)
874{
875 return platform_driver_register(&davinci_pcm_driver);
876}
877module_init(snd_davinci_pcm_init);
878
879static void __exit snd_davinci_pcm_exit(void)
880{
881 platform_driver_unregister(&davinci_pcm_driver);
882}
883module_exit(snd_davinci_pcm_exit);
Mark Brown958e7922008-12-03 19:58:17 +0000884
Vladimir Barinov310355c2008-02-18 11:40:22 +0100885MODULE_AUTHOR("Vladimir Barinov");
886MODULE_DESCRIPTION("TI DAVINCI PCM DMA module");
887MODULE_LICENSE("GPL");