aboutsummaryrefslogtreecommitdiffstats
path: root/sound
diff options
context:
space:
mode:
authorTroy Kisky <troy.kisky@boundarydevices.com>2009-11-18 19:49:53 -0500
committerMark Brown <broonie@opensource.wolfsonmicro.com>2009-11-19 05:48:08 -0500
commit1e224f322bf22280957a5f76164d848526ed9b08 (patch)
treebdf7af09be46172aa6048bb25fda35980e8b2159 /sound
parent1587ea31572e25a0a2c9c491b7f8c937b6c0454e (diff)
ASoC: DaVinci: pcm, fix underrun by using sram
Fix underruns by using dma to copy 1st to sram in a ping/pong buffer style and then copying from the sram to the ASP. This also has the advantage of tolerating very long interrupt latency on dma completion. Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com> Acked-by: Liam Girdwood <lrg@slimlogic.co.uk> Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
Diffstat (limited to 'sound')
-rw-r--r--sound/soc/davinci/davinci-i2s.c7
-rw-r--r--sound/soc/davinci/davinci-pcm.c515
-rw-r--r--sound/soc/davinci/davinci-pcm.h1
3 files changed, 479 insertions, 44 deletions
diff --git a/sound/soc/davinci/davinci-i2s.c b/sound/soc/davinci/davinci-i2s.c
index b2a5372ef72c..6362ca05506e 100644
--- a/sound/soc/davinci/davinci-i2s.c
+++ b/sound/soc/davinci/davinci-i2s.c
@@ -545,8 +545,13 @@ static int davinci_i2s_probe(struct platform_device *pdev)
545 ret = -ENOMEM; 545 ret = -ENOMEM;
546 goto err_release_region; 546 goto err_release_region;
547 } 547 }
548 if (pdata) 548 if (pdata) {
549 dev->enable_channel_combine = pdata->enable_channel_combine; 549 dev->enable_channel_combine = pdata->enable_channel_combine;
550 dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].sram_size =
551 pdata->sram_size_playback;
552 dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].sram_size =
553 pdata->sram_size_capture;
554 }
550 dev->clk = clk_get(&pdev->dev, NULL); 555 dev->clk = clk_get(&pdev->dev, NULL);
551 if (IS_ERR(dev->clk)) { 556 if (IS_ERR(dev->clk)) {
552 ret = -ENODEV; 557 ret = -ENODEV;
diff --git a/sound/soc/davinci/davinci-pcm.c b/sound/soc/davinci/davinci-pcm.c
index 42a657ea49cb..664d49336508 100644
--- a/sound/soc/davinci/davinci-pcm.c
+++ b/sound/soc/davinci/davinci-pcm.c
@@ -3,6 +3,7 @@
3 * 3 *
4 * Author: Vladimir Barinov, <vbarinov@embeddedalley.com> 4 * Author: Vladimir Barinov, <vbarinov@embeddedalley.com>
5 * Copyright: (C) 2007 MontaVista Software, Inc., <source@mvista.com> 5 * Copyright: (C) 2007 MontaVista Software, Inc., <source@mvista.com>
6 * added SRAM ping/pong (C) 2008 Troy Kisky <troy.kisky@boundarydevices.com>
6 * 7 *
7 * This program is free software; you can redistribute it and/or modify 8 * 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 * it under the terms of the GNU General Public License version 2 as
@@ -23,10 +24,29 @@
23 24
24#include <asm/dma.h> 25#include <asm/dma.h>
25#include <mach/edma.h> 26#include <mach/edma.h>
27#include <mach/sram.h>
26 28
27#include "davinci-pcm.h" 29#include "davinci-pcm.h"
28 30
29static struct snd_pcm_hardware davinci_pcm_hardware = { 31#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
49static struct snd_pcm_hardware pcm_hardware_playback = {
30 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER | 50 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
31 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID | 51 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
32 SNDRV_PCM_INFO_PAUSE), 52 SNDRV_PCM_INFO_PAUSE),
@@ -48,14 +68,80 @@ static struct snd_pcm_hardware davinci_pcm_hardware = {
48 .fifo_size = 0, 68 .fifo_size = 0,
49}; 69};
50 70
71static struct snd_pcm_hardware pcm_hardware_capture = {
72 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
73 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
74 SNDRV_PCM_INFO_PAUSE),
75 .formats = (SNDRV_PCM_FMTBIT_S16_LE),
76 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
77 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
78 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
79 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
80 SNDRV_PCM_RATE_KNOT),
81 .rate_min = 8000,
82 .rate_max = 96000,
83 .channels_min = 2,
84 .channels_max = 2,
85 .buffer_bytes_max = 128 * 1024,
86 .period_bytes_min = 32,
87 .period_bytes_max = 8 * 1024,
88 .periods_min = 16,
89 .periods_max = 255,
90 .fifo_size = 0,
91};
92
93/*
94 * How ping/pong works....
95 *
96 * Playback:
97 * ram_params - copys 2*ping_size from start of SDRAM to iram,
98 * links to ram_link2
99 * ram_link2 - copys rest of SDRAM to iram in ping_size units,
100 * links to ram_link
101 * ram_link - copys entire SDRAM to iram in ping_size uints,
102 * links to self
103 *
104 * asp_params - same as asp_link[0]
105 * asp_link[0] - copys from lower half of iram to asp port
106 * links to asp_link[1], triggers iram copy event on completion
107 * asp_link[1] - copys from upper half of iram to asp port
108 * links to asp_link[0], triggers iram copy event on completion
109 * triggers interrupt only needed to let upper SOC levels update position
110 * in stream on completion
111 *
112 * When playback is started:
113 * ram_params started
114 * asp_params started
115 *
116 * Capture:
117 * ram_params - same as ram_link,
118 * links to ram_link
119 * ram_link - same as playback
120 * links to self
121 *
122 * asp_params - same as playback
123 * asp_link[0] - same as playback
124 * asp_link[1] - same as playback
125 *
126 * When capture is started:
127 * asp_params started
128 */
51struct davinci_runtime_data { 129struct davinci_runtime_data {
52 spinlock_t lock; 130 spinlock_t lock;
53 int period; /* current DMA period */ 131 int period; /* current DMA period */
54 int asp_channel; /* Master DMA channel */ 132 int asp_channel; /* Master DMA channel */
55 int asp_link[2]; /* asp parameter link channel, ping/pong */ 133 int asp_link[2]; /* asp parameter link channel, ping/pong */
56 struct davinci_pcm_dma_params *params; /* DMA params */ 134 struct davinci_pcm_dma_params *params; /* DMA params */
135 int ram_channel;
136 int ram_link;
137 int ram_link2;
138 struct edmacc_param asp_params;
139 struct edmacc_param ram_params;
57}; 140};
58 141
142/*
143 * Not used with ping/pong
144 */
59static void davinci_pcm_enqueue_dma(struct snd_pcm_substream *substream) 145static void davinci_pcm_enqueue_dma(struct snd_pcm_substream *substream)
60{ 146{
61 struct davinci_runtime_data *prtd = substream->runtime->private_data; 147 struct davinci_runtime_data *prtd = substream->runtime->private_data;
@@ -124,41 +210,290 @@ static void davinci_pcm_dma_irq(unsigned link, u16 ch_status, void *data)
124 struct snd_pcm_substream *substream = data; 210 struct snd_pcm_substream *substream = data;
125 struct davinci_runtime_data *prtd = substream->runtime->private_data; 211 struct davinci_runtime_data *prtd = substream->runtime->private_data;
126 212
213 print_buf_info(prtd->ram_channel, "i ram_channel");
127 pr_debug("davinci_pcm: link=%d, status=0x%x\n", link, ch_status); 214 pr_debug("davinci_pcm: link=%d, status=0x%x\n", link, ch_status);
128 215
129 if (unlikely(ch_status != DMA_COMPLETE)) 216 if (unlikely(ch_status != DMA_COMPLETE))
130 return; 217 return;
131 218
132 if (snd_pcm_running(substream)) { 219 if (snd_pcm_running(substream)) {
220 if (prtd->ram_channel < 0) {
221 /* No ping/pong must fix up link dma data*/
222 spin_lock(&prtd->lock);
223 davinci_pcm_enqueue_dma(substream);
224 spin_unlock(&prtd->lock);
225 }
133 snd_pcm_period_elapsed(substream); 226 snd_pcm_period_elapsed(substream);
227 }
228}
229
230static int allocate_sram(struct snd_pcm_substream *substream, unsigned size,
231 struct snd_pcm_hardware *ppcm)
232{
233 struct snd_dma_buffer *buf = &substream->dma_buffer;
234 struct snd_dma_buffer *iram_dma = NULL;
235 dma_addr_t iram_phys = 0;
236 void *iram_virt = NULL;
237
238 if (buf->private_data || !size)
239 return 0;
240
241 ppcm->period_bytes_max = size;
242 iram_virt = sram_alloc(size, &iram_phys);
243 if (!iram_virt)
244 goto exit1;
245 iram_dma = kzalloc(sizeof(*iram_dma), GFP_KERNEL);
246 if (!iram_dma)
247 goto exit2;
248 iram_dma->area = iram_virt;
249 iram_dma->addr = iram_phys;
250 memset(iram_dma->area, 0, size);
251 iram_dma->bytes = size;
252 buf->private_data = iram_dma;
253 return 0;
254exit2:
255 if (iram_virt)
256 sram_free(iram_virt, size);
257exit1:
258 return -ENOMEM;
259}
134 260
135 spin_lock(&prtd->lock); 261/*
136 davinci_pcm_enqueue_dma(substream); 262 * Only used with ping/pong.
137 spin_unlock(&prtd->lock); 263 * This is called after runtime->dma_addr, period_bytes and data_type are valid
264 */
265static int ping_pong_dma_setup(struct snd_pcm_substream *substream)
266{
267 unsigned short ram_src_cidx, ram_dst_cidx;
268 struct snd_pcm_runtime *runtime = substream->runtime;
269 struct davinci_runtime_data *prtd = runtime->private_data;
270 struct snd_dma_buffer *iram_dma =
271 (struct snd_dma_buffer *)substream->dma_buffer.private_data;
272 struct davinci_pcm_dma_params *params = prtd->params;
273 unsigned int data_type = params->data_type;
274 unsigned int acnt = params->acnt;
275 /* divide by 2 for ping/pong */
276 unsigned int ping_size = snd_pcm_lib_period_bytes(substream) >> 1;
277 int link = prtd->asp_link[1];
278 unsigned int fifo_level = prtd->params->fifo_level;
279 unsigned int count;
280 if ((data_type == 0) || (data_type > 4)) {
281 printk(KERN_ERR "%s: data_type=%i\n", __func__, data_type);
282 return -EINVAL;
283 }
284 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
285 dma_addr_t asp_src_pong = iram_dma->addr + ping_size;
286 ram_src_cidx = ping_size;
287 ram_dst_cidx = -ping_size;
288 edma_set_src(link, asp_src_pong, INCR, W8BIT);
289
290 link = prtd->asp_link[0];
291 edma_set_src_index(link, data_type, data_type * fifo_level);
292 link = prtd->asp_link[1];
293 edma_set_src_index(link, data_type, data_type * fifo_level);
294
295 link = prtd->ram_link;
296 edma_set_src(link, runtime->dma_addr, INCR, W32BIT);
297 } else {
298 dma_addr_t asp_dst_pong = iram_dma->addr + ping_size;
299 ram_src_cidx = -ping_size;
300 ram_dst_cidx = ping_size;
301 edma_set_dest(link, asp_dst_pong, INCR, W8BIT);
302
303 link = prtd->asp_link[0];
304 edma_set_dest_index(link, data_type, data_type * fifo_level);
305 link = prtd->asp_link[1];
306 edma_set_dest_index(link, data_type, data_type * fifo_level);
307
308 link = prtd->ram_link;
309 edma_set_dest(link, runtime->dma_addr, INCR, W32BIT);
310 }
311
312 if (!fifo_level) {
313 count = ping_size / data_type;
314 edma_set_transfer_params(prtd->asp_link[0], acnt, count,
315 1, 0, ASYNC);
316 edma_set_transfer_params(prtd->asp_link[1], acnt, count,
317 1, 0, ASYNC);
318 } else {
319 count = ping_size / (data_type * fifo_level);
320 edma_set_transfer_params(prtd->asp_link[0], acnt, fifo_level,
321 count, fifo_level, ABSYNC);
322 edma_set_transfer_params(prtd->asp_link[1], acnt, fifo_level,
323 count, fifo_level, ABSYNC);
324 }
325
326 link = prtd->ram_link;
327 edma_set_src_index(link, ping_size, ram_src_cidx);
328 edma_set_dest_index(link, ping_size, ram_dst_cidx);
329 edma_set_transfer_params(link, ping_size, 2,
330 runtime->periods, 2, ASYNC);
331
332 /* init master params */
333 edma_read_slot(prtd->asp_link[0], &prtd->asp_params);
334 edma_read_slot(prtd->ram_link, &prtd->ram_params);
335 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
336 struct edmacc_param p_ram;
337 /* Copy entire iram buffer before playback started */
338 prtd->ram_params.a_b_cnt = (1 << 16) | (ping_size << 1);
339 /* 0 dst_bidx */
340 prtd->ram_params.src_dst_bidx = (ping_size << 1);
341 /* 0 dst_cidx */
342 prtd->ram_params.src_dst_cidx = (ping_size << 1);
343 prtd->ram_params.ccnt = 1;
344
345 /* Skip 1st period */
346 edma_read_slot(prtd->ram_link, &p_ram);
347 p_ram.src += (ping_size << 1);
348 p_ram.ccnt -= 1;
349 edma_write_slot(prtd->ram_link2, &p_ram);
350 /*
351 * When 1st started, ram -> iram dma channel will fill the
352 * entire iram. Then, whenever a ping/pong asp buffer finishes,
353 * 1/2 iram will be filled.
354 */
355 prtd->ram_params.link_bcntrld =
356 EDMA_CHAN_SLOT(prtd->ram_link2) << 5;
357 }
358 return 0;
359}
360
361/* 1 asp tx or rx channel using 2 parameter channels
362 * 1 ram to/from iram channel using 1 parameter channel
363 *
364 * Playback
365 * ram copy channel kicks off first,
366 * 1st ram copy of entire iram buffer completion kicks off asp channel
367 * asp tcc always kicks off ram copy of 1/2 iram buffer
368 *
369 * Record
370 * asp channel starts, tcc kicks off ram copy
371 */
372static int request_ping_pong(struct snd_pcm_substream *substream,
373 struct davinci_runtime_data *prtd,
374 struct snd_dma_buffer *iram_dma)
375{
376 dma_addr_t asp_src_ping;
377 dma_addr_t asp_dst_ping;
378 int link;
379 struct davinci_pcm_dma_params *params = prtd->params;
380
381 /* Request ram master channel */
382 link = prtd->ram_channel = edma_alloc_channel(EDMA_CHANNEL_ANY,
383 davinci_pcm_dma_irq, substream,
384 EVENTQ_1);
385 if (link < 0)
386 goto exit1;
387
388 /* Request ram link channel */
389 link = prtd->ram_link = edma_alloc_slot(
390 EDMA_CTLR(prtd->ram_channel), EDMA_SLOT_ANY);
391 if (link < 0)
392 goto exit2;
393
394 link = prtd->asp_link[1] = edma_alloc_slot(
395 EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY);
396 if (link < 0)
397 goto exit3;
398
399 prtd->ram_link2 = -1;
400 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
401 link = prtd->ram_link2 = edma_alloc_slot(
402 EDMA_CTLR(prtd->ram_channel), EDMA_SLOT_ANY);
403 if (link < 0)
404 goto exit4;
405 }
406 /* circle ping-pong buffers */
407 edma_link(prtd->asp_link[0], prtd->asp_link[1]);
408 edma_link(prtd->asp_link[1], prtd->asp_link[0]);
409 /* circle ram buffers */
410 edma_link(prtd->ram_link, prtd->ram_link);
411
412 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
413 asp_src_ping = iram_dma->addr;
414 asp_dst_ping = params->dma_addr; /* fifo */
415 } else {
416 asp_src_ping = params->dma_addr; /* fifo */
417 asp_dst_ping = iram_dma->addr;
138 } 418 }
419 /* ping */
420 link = prtd->asp_link[0];
421 edma_set_src(link, asp_src_ping, INCR, W16BIT);
422 edma_set_dest(link, asp_dst_ping, INCR, W16BIT);
423 edma_set_src_index(link, 0, 0);
424 edma_set_dest_index(link, 0, 0);
425
426 edma_read_slot(link, &prtd->asp_params);
427 prtd->asp_params.opt &= ~(TCCMODE | EDMA_TCC(0x3f) | TCINTEN);
428 prtd->asp_params.opt |= TCCHEN | EDMA_TCC(prtd->ram_channel & 0x3f);
429 edma_write_slot(link, &prtd->asp_params);
430
431 /* pong */
432 link = prtd->asp_link[1];
433 edma_set_src(link, asp_src_ping, INCR, W16BIT);
434 edma_set_dest(link, asp_dst_ping, INCR, W16BIT);
435 edma_set_src_index(link, 0, 0);
436 edma_set_dest_index(link, 0, 0);
437
438 edma_read_slot(link, &prtd->asp_params);
439 prtd->asp_params.opt &= ~(TCCMODE | EDMA_TCC(0x3f));
440 /* interrupt after every pong completion */
441 prtd->asp_params.opt |= TCINTEN | TCCHEN |
442 EDMA_TCC(EDMA_CHAN_SLOT(prtd->ram_channel));
443 edma_write_slot(link, &prtd->asp_params);
444
445 /* ram */
446 link = prtd->ram_link;
447 edma_set_src(link, iram_dma->addr, INCR, W32BIT);
448 edma_set_dest(link, iram_dma->addr, INCR, W32BIT);
449 pr_debug("%s: audio dma channels/slots in use for ram:%u %u %u,"
450 "for asp:%u %u %u\n", __func__,
451 prtd->ram_channel, prtd->ram_link, prtd->ram_link2,
452 prtd->asp_channel, prtd->asp_link[0],
453 prtd->asp_link[1]);
454 return 0;
455exit4:
456 edma_free_channel(prtd->asp_link[1]);
457 prtd->asp_link[1] = -1;
458exit3:
459 edma_free_channel(prtd->ram_link);
460 prtd->ram_link = -1;
461exit2:
462 edma_free_channel(prtd->ram_channel);
463 prtd->ram_channel = -1;
464exit1:
465 return link;
139} 466}
140 467
141static int davinci_pcm_dma_request(struct snd_pcm_substream *substream) 468static int davinci_pcm_dma_request(struct snd_pcm_substream *substream)
142{ 469{
470 struct snd_dma_buffer *iram_dma;
143 struct davinci_runtime_data *prtd = substream->runtime->private_data; 471 struct davinci_runtime_data *prtd = substream->runtime->private_data;
144 struct edmacc_param p_ram; 472 struct davinci_pcm_dma_params *params = prtd->params;
145 int ret; 473 int link;
146 474
147 /* Request master DMA channel */ 475 if (!params)
148 ret = edma_alloc_channel(prtd->params->channel, 476 return -ENODEV;
149 davinci_pcm_dma_irq, substream,
150 EVENTQ_0);
151 if (ret < 0)
152 return ret;
153 prtd->asp_channel = ret;
154 477
155 /* Request parameter RAM reload slot */ 478 /* Request asp master DMA channel */
156 ret = edma_alloc_slot(EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY); 479 link = prtd->asp_channel = edma_alloc_channel(params->channel,
157 if (ret < 0) { 480 davinci_pcm_dma_irq, substream, EVENTQ_0);
158 edma_free_channel(prtd->asp_channel); 481 if (link < 0)
159 return ret; 482 goto exit1;
483
484 /* Request asp link channels */
485 link = prtd->asp_link[0] = edma_alloc_slot(
486 EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY);
487 if (link < 0)
488 goto exit2;
489
490 iram_dma = (struct snd_dma_buffer *)substream->dma_buffer.private_data;
491 if (iram_dma) {
492 if (request_ping_pong(substream, prtd, iram_dma) == 0)
493 return 0;
494 printk(KERN_WARNING "%s: dma channel allocation failed,"
495 "not using sram\n", __func__);
160 } 496 }
161 prtd->asp_link[0] = ret;
162 497
163 /* Issue transfer completion IRQ when the channel completes a 498 /* Issue transfer completion IRQ when the channel completes a
164 * transfer, then always reload from the same slot (by a kind 499 * transfer, then always reload from the same slot (by a kind
@@ -169,12 +504,17 @@ static int davinci_pcm_dma_request(struct snd_pcm_substream *substream)
169 * the buffer and its length (ccnt) ... use it as a template 504 * the buffer and its length (ccnt) ... use it as a template
170 * so davinci_pcm_enqueue_dma() takes less time in IRQ. 505 * so davinci_pcm_enqueue_dma() takes less time in IRQ.
171 */ 506 */
172 edma_read_slot(prtd->asp_link[0], &p_ram); 507 edma_read_slot(link, &prtd->asp_params);
173 p_ram.opt |= TCINTEN | EDMA_TCC(EDMA_CHAN_SLOT(prtd->asp_channel)); 508 prtd->asp_params.opt |= TCINTEN |
174 p_ram.link_bcntrld = EDMA_CHAN_SLOT(prtd->asp_link[0]) << 5; 509 EDMA_TCC(EDMA_CHAN_SLOT(prtd->asp_channel));
175 edma_write_slot(prtd->asp_link[0], &p_ram); 510 prtd->asp_params.link_bcntrld = EDMA_CHAN_SLOT(link) << 5;
176 511 edma_write_slot(link, &prtd->asp_params);
177 return 0; 512 return 0;
513exit2:
514 edma_free_channel(prtd->asp_channel);
515 prtd->asp_channel = -1;
516exit1:
517 return link;
178} 518}
179 519
180static int davinci_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 520static int davinci_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
@@ -208,14 +548,34 @@ static int davinci_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
208static int davinci_pcm_prepare(struct snd_pcm_substream *substream) 548static int davinci_pcm_prepare(struct snd_pcm_substream *substream)
209{ 549{
210 struct davinci_runtime_data *prtd = substream->runtime->private_data; 550 struct davinci_runtime_data *prtd = substream->runtime->private_data;
211 struct edmacc_param temp;
212 551
552 if (prtd->ram_channel >= 0) {
553 int ret = ping_pong_dma_setup(substream);
554 if (ret < 0)
555 return ret;
556
557 edma_write_slot(prtd->ram_channel, &prtd->ram_params);
558 edma_write_slot(prtd->asp_channel, &prtd->asp_params);
559
560 print_buf_info(prtd->ram_channel, "ram_channel");
561 print_buf_info(prtd->ram_link, "ram_link");
562 print_buf_info(prtd->ram_link2, "ram_link2");
563 print_buf_info(prtd->asp_channel, "asp_channel");
564 print_buf_info(prtd->asp_link[0], "asp_link[0]");
565 print_buf_info(prtd->asp_link[1], "asp_link[1]");
566
567 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
568 /* copy 1st iram buffer */
569 edma_start(prtd->ram_channel);
570 }
571 return 0;
572 }
213 prtd->period = 0; 573 prtd->period = 0;
214 davinci_pcm_enqueue_dma(substream); 574 davinci_pcm_enqueue_dma(substream);
215 575
216 /* Copy self-linked parameter RAM entry into master channel */ 576 /* Copy self-linked parameter RAM entry into master channel */
217 edma_read_slot(prtd->asp_link[0], &temp); 577 edma_read_slot(prtd->asp_link[0], &prtd->asp_params);
218 edma_write_slot(prtd->asp_channel, &temp); 578 edma_write_slot(prtd->asp_channel, &prtd->asp_params);
219 davinci_pcm_enqueue_dma(substream); 579 davinci_pcm_enqueue_dma(substream);
220 580
221 return 0; 581 return 0;
@@ -231,13 +591,46 @@ davinci_pcm_pointer(struct snd_pcm_substream *substream)
231 dma_addr_t asp_src, asp_dst; 591 dma_addr_t asp_src, asp_dst;
232 592
233 spin_lock(&prtd->lock); 593 spin_lock(&prtd->lock);
234 594 if (prtd->ram_channel >= 0) {
235 edma_get_position(prtd->asp_channel, &asp_src, &asp_dst); 595 int ram_count;
236 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 596 int mod_ram;
237 asp_count = asp_src - runtime->dma_addr; 597 dma_addr_t ram_src, ram_dst;
238 else 598 unsigned int period_size = snd_pcm_lib_period_bytes(substream);
239 asp_count = asp_dst - runtime->dma_addr; 599 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
240 600 /* reading ram before asp should be safe
601 * as long as the asp transfers less than a ping size
602 * of bytes between the 2 reads
603 */
604 edma_get_position(prtd->ram_channel,
605 &ram_src, &ram_dst);
606 edma_get_position(prtd->asp_channel,
607 &asp_src, &asp_dst);
608 asp_count = asp_src - prtd->asp_params.src;
609 ram_count = ram_src - prtd->ram_params.src;
610 mod_ram = ram_count % period_size;
611 mod_ram -= asp_count;
612 if (mod_ram < 0)
613 mod_ram += period_size;
614 else if (mod_ram == 0) {
615 if (snd_pcm_running(substream))
616 mod_ram += period_size;
617 }
618 ram_count -= mod_ram;
619 if (ram_count < 0)
620 ram_count += period_size * runtime->periods;
621 } else {
622 edma_get_position(prtd->ram_channel,
623 &ram_src, &ram_dst);
624 ram_count = ram_dst - prtd->ram_params.dst;
625 }
626 asp_count = ram_count;
627 } else {
628 edma_get_position(prtd->asp_channel, &asp_src, &asp_dst);
629 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
630 asp_count = asp_src - runtime->dma_addr;
631 else
632 asp_count = asp_dst - runtime->dma_addr;
633 }
241 spin_unlock(&prtd->lock); 634 spin_unlock(&prtd->lock);
242 635
243 offset = bytes_to_frames(runtime, asp_count); 636 offset = bytes_to_frames(runtime, asp_count);
@@ -251,6 +644,7 @@ static int davinci_pcm_open(struct snd_pcm_substream *substream)
251{ 644{
252 struct snd_pcm_runtime *runtime = substream->runtime; 645 struct snd_pcm_runtime *runtime = substream->runtime;
253 struct davinci_runtime_data *prtd; 646 struct davinci_runtime_data *prtd;
647 struct snd_pcm_hardware *ppcm;
254 int ret = 0; 648 int ret = 0;
255 struct snd_soc_pcm_runtime *rtd = substream->private_data; 649 struct snd_soc_pcm_runtime *rtd = substream->private_data;
256 struct davinci_pcm_dma_params *pa = rtd->dai->cpu_dai->dma_data; 650 struct davinci_pcm_dma_params *pa = rtd->dai->cpu_dai->dma_data;
@@ -259,7 +653,10 @@ static int davinci_pcm_open(struct snd_pcm_substream *substream)
259 return -ENODEV; 653 return -ENODEV;
260 params = &pa[substream->stream]; 654 params = &pa[substream->stream];
261 655
262 snd_soc_set_runtime_hwparams(substream, &davinci_pcm_hardware); 656 ppcm = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
657 &pcm_hardware_playback : &pcm_hardware_capture;
658 allocate_sram(substream, params->sram_size, ppcm);
659 snd_soc_set_runtime_hwparams(substream, ppcm);
263 /* ensure that buffer size is a multiple of period size */ 660 /* ensure that buffer size is a multiple of period size */
264 ret = snd_pcm_hw_constraint_integer(runtime, 661 ret = snd_pcm_hw_constraint_integer(runtime,
265 SNDRV_PCM_HW_PARAM_PERIODS); 662 SNDRV_PCM_HW_PARAM_PERIODS);
@@ -272,6 +669,11 @@ static int davinci_pcm_open(struct snd_pcm_substream *substream)
272 669
273 spin_lock_init(&prtd->lock); 670 spin_lock_init(&prtd->lock);
274 prtd->params = params; 671 prtd->params = params;
672 prtd->asp_channel = -1;
673 prtd->asp_link[0] = prtd->asp_link[1] = -1;
674 prtd->ram_channel = -1;
675 prtd->ram_link = -1;
676 prtd->ram_link2 = -1;
275 677
276 runtime->private_data = prtd; 678 runtime->private_data = prtd;
277 679
@@ -289,10 +691,29 @@ static int davinci_pcm_close(struct snd_pcm_substream *substream)
289 struct snd_pcm_runtime *runtime = substream->runtime; 691 struct snd_pcm_runtime *runtime = substream->runtime;
290 struct davinci_runtime_data *prtd = runtime->private_data; 692 struct davinci_runtime_data *prtd = runtime->private_data;
291 693
292 edma_unlink(prtd->asp_link[0]); 694 if (prtd->ram_channel >= 0)
293 695 edma_stop(prtd->ram_channel);
294 edma_free_slot(prtd->asp_link[0]); 696 if (prtd->asp_channel >= 0)
295 edma_free_channel(prtd->asp_channel); 697 edma_stop(prtd->asp_channel);
698 if (prtd->asp_link[0] >= 0)
699 edma_unlink(prtd->asp_link[0]);
700 if (prtd->asp_link[1] >= 0)
701 edma_unlink(prtd->asp_link[1]);
702 if (prtd->ram_link >= 0)
703 edma_unlink(prtd->ram_link);
704
705 if (prtd->asp_link[0] >= 0)
706 edma_free_slot(prtd->asp_link[0]);
707 if (prtd->asp_link[1] >= 0)
708 edma_free_slot(prtd->asp_link[1]);
709 if (prtd->asp_channel >= 0)
710 edma_free_channel(prtd->asp_channel);
711 if (prtd->ram_link >= 0)
712 edma_free_slot(prtd->ram_link);
713 if (prtd->ram_link2 >= 0)
714 edma_free_slot(prtd->ram_link2);
715 if (prtd->ram_channel >= 0)
716 edma_free_channel(prtd->ram_channel);
296 717
297 kfree(prtd); 718 kfree(prtd);
298 719
@@ -334,11 +755,11 @@ static struct snd_pcm_ops davinci_pcm_ops = {
334 .mmap = davinci_pcm_mmap, 755 .mmap = davinci_pcm_mmap,
335}; 756};
336 757
337static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream) 758static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream,
759 size_t size)
338{ 760{
339 struct snd_pcm_substream *substream = pcm->streams[stream].substream; 761 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
340 struct snd_dma_buffer *buf = &substream->dma_buffer; 762 struct snd_dma_buffer *buf = &substream->dma_buffer;
341 size_t size = davinci_pcm_hardware.buffer_bytes_max;
342 763
343 buf->dev.type = SNDRV_DMA_TYPE_DEV; 764 buf->dev.type = SNDRV_DMA_TYPE_DEV;
344 buf->dev.dev = pcm->card->dev; 765 buf->dev.dev = pcm->card->dev;
@@ -363,6 +784,7 @@ static void davinci_pcm_free(struct snd_pcm *pcm)
363 int stream; 784 int stream;
364 785
365 for (stream = 0; stream < 2; stream++) { 786 for (stream = 0; stream < 2; stream++) {
787 struct snd_dma_buffer *iram_dma;
366 substream = pcm->streams[stream].substream; 788 substream = pcm->streams[stream].substream;
367 if (!substream) 789 if (!substream)
368 continue; 790 continue;
@@ -374,6 +796,11 @@ static void davinci_pcm_free(struct snd_pcm *pcm)
374 dma_free_writecombine(pcm->card->dev, buf->bytes, 796 dma_free_writecombine(pcm->card->dev, buf->bytes,
375 buf->area, buf->addr); 797 buf->area, buf->addr);
376 buf->area = NULL; 798 buf->area = NULL;
799 iram_dma = (struct snd_dma_buffer *)buf->private_data;
800 if (iram_dma) {
801 sram_free(iram_dma->area, iram_dma->bytes);
802 kfree(iram_dma);
803 }
377 } 804 }
378} 805}
379 806
@@ -391,14 +818,16 @@ static int davinci_pcm_new(struct snd_card *card,
391 818
392 if (dai->playback.channels_min) { 819 if (dai->playback.channels_min) {
393 ret = davinci_pcm_preallocate_dma_buffer(pcm, 820 ret = davinci_pcm_preallocate_dma_buffer(pcm,
394 SNDRV_PCM_STREAM_PLAYBACK); 821 SNDRV_PCM_STREAM_PLAYBACK,
822 pcm_hardware_playback.buffer_bytes_max);
395 if (ret) 823 if (ret)
396 return ret; 824 return ret;
397 } 825 }
398 826
399 if (dai->capture.channels_min) { 827 if (dai->capture.channels_min) {
400 ret = davinci_pcm_preallocate_dma_buffer(pcm, 828 ret = davinci_pcm_preallocate_dma_buffer(pcm,
401 SNDRV_PCM_STREAM_CAPTURE); 829 SNDRV_PCM_STREAM_CAPTURE,
830 pcm_hardware_capture.buffer_bytes_max);
402 if (ret) 831 if (ret)
403 return ret; 832 return ret;
404 } 833 }
diff --git a/sound/soc/davinci/davinci-pcm.h b/sound/soc/davinci/davinci-pcm.h
index c8b0d2baf05a..0764944cf10f 100644
--- a/sound/soc/davinci/davinci-pcm.h
+++ b/sound/soc/davinci/davinci-pcm.h
@@ -20,6 +20,7 @@ struct davinci_pcm_dma_params {
20 int channel; /* sync dma channel ID */ 20 int channel; /* sync dma channel ID */
21 unsigned short acnt; 21 unsigned short acnt;
22 dma_addr_t dma_addr; /* device physical address for DMA */ 22 dma_addr_t dma_addr; /* device physical address for DMA */
23 unsigned sram_size;
23 enum dma_event_q eventq_no; /* event queue number */ 24 enum dma_event_q eventq_no; /* event queue number */
24 unsigned char data_type; /* xfer data type */ 25 unsigned char data_type; /* xfer data type */
25 unsigned char convert_mono_stereo; 26 unsigned char convert_mono_stereo;