blob: 0d04e0c4a148474a403ea5d94fbdd1033422e883 [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
49static struct snd_pcm_hardware pcm_hardware_playback = {
Vladimir Barinov310355c2008-02-18 11:40:22 +010050 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
51 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
Chaithrika U Sa47979b2009-12-03 18:56:56 +053052 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
Vladimir Barinov310355c2008-02-18 11:40:22 +010053 .formats = (SNDRV_PCM_FMTBIT_S16_LE),
54 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
55 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
56 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
57 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
58 SNDRV_PCM_RATE_KNOT),
59 .rate_min = 8000,
60 .rate_max = 96000,
61 .channels_min = 2,
62 .channels_max = 2,
63 .buffer_bytes_max = 128 * 1024,
64 .period_bytes_min = 32,
65 .period_bytes_max = 8 * 1024,
66 .periods_min = 16,
67 .periods_max = 255,
68 .fifo_size = 0,
69};
70
Troy Kisky1e224f32009-11-18 17:49:53 -070071static 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 */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100129struct davinci_runtime_data {
130 spinlock_t lock;
131 int period; /* current DMA period */
Troy Kisky1587ea312009-11-18 17:49:52 -0700132 int asp_channel; /* Master DMA channel */
133 int asp_link[2]; /* asp parameter link channel, ping/pong */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100134 struct davinci_pcm_dma_params *params; /* DMA params */
Troy Kisky1e224f32009-11-18 17:49:53 -0700135 int ram_channel;
136 int ram_link;
137 int ram_link2;
138 struct edmacc_param asp_params;
139 struct edmacc_param ram_params;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100140};
141
Troy Kisky1e224f32009-11-18 17:49:53 -0700142/*
143 * Not used with ping/pong
144 */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100145static void davinci_pcm_enqueue_dma(struct snd_pcm_substream *substream)
146{
147 struct davinci_runtime_data *prtd = substream->runtime->private_data;
148 struct snd_pcm_runtime *runtime = substream->runtime;
Troy Kisky1587ea312009-11-18 17:49:52 -0700149 int link = prtd->asp_link[0];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100150 unsigned int period_size;
151 unsigned int dma_offset;
152 dma_addr_t dma_pos;
153 dma_addr_t src, dst;
154 unsigned short src_bidx, dst_bidx;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400155 unsigned short src_cidx, dst_cidx;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100156 unsigned int data_type;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400157 unsigned short acnt;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100158 unsigned int count;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400159 unsigned int fifo_level;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100160
161 period_size = snd_pcm_lib_period_bytes(substream);
162 dma_offset = prtd->period * period_size;
163 dma_pos = runtime->dma_addr + dma_offset;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400164 fifo_level = prtd->params->fifo_level;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100165
Alexander Beregalov9cd28ab2008-12-13 16:25:27 +0300166 pr_debug("davinci_pcm: audio_set_dma_params_play channel = %d "
Troy Kisky1587ea312009-11-18 17:49:52 -0700167 "dma_ptr = %x period_size=%x\n", link, dma_pos, period_size);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100168
169 data_type = prtd->params->data_type;
170 count = period_size / data_type;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400171 if (fifo_level)
172 count /= fifo_level;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100173
174 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
175 src = dma_pos;
176 dst = prtd->params->dma_addr;
177 src_bidx = data_type;
178 dst_bidx = 0;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400179 src_cidx = data_type * fifo_level;
180 dst_cidx = 0;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100181 } else {
182 src = prtd->params->dma_addr;
183 dst = dma_pos;
184 src_bidx = 0;
185 dst_bidx = data_type;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400186 src_cidx = 0;
187 dst_cidx = data_type * fifo_level;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100188 }
189
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400190 acnt = prtd->params->acnt;
Troy Kisky1587ea312009-11-18 17:49:52 -0700191 edma_set_src(link, src, INCR, W8BIT);
192 edma_set_dest(link, dst, INCR, W8BIT);
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400193
Troy Kisky1587ea312009-11-18 17:49:52 -0700194 edma_set_src_index(link, src_bidx, src_cidx);
195 edma_set_dest_index(link, dst_bidx, dst_cidx);
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400196
197 if (!fifo_level)
Troy Kisky1587ea312009-11-18 17:49:52 -0700198 edma_set_transfer_params(link, acnt, count, 1, 0, ASYNC);
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400199 else
Troy Kisky1587ea312009-11-18 17:49:52 -0700200 edma_set_transfer_params(link, acnt, fifo_level, count,
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400201 fifo_level, ABSYNC);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100202
203 prtd->period++;
204 if (unlikely(prtd->period >= runtime->periods))
205 prtd->period = 0;
206}
207
Troy Kisky1587ea312009-11-18 17:49:52 -0700208static void davinci_pcm_dma_irq(unsigned link, u16 ch_status, void *data)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100209{
210 struct snd_pcm_substream *substream = data;
211 struct davinci_runtime_data *prtd = substream->runtime->private_data;
212
Troy Kisky1e224f32009-11-18 17:49:53 -0700213 print_buf_info(prtd->ram_channel, "i ram_channel");
Troy Kisky1587ea312009-11-18 17:49:52 -0700214 pr_debug("davinci_pcm: link=%d, status=0x%x\n", link, ch_status);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100215
216 if (unlikely(ch_status != DMA_COMPLETE))
217 return;
218
219 if (snd_pcm_running(substream)) {
Troy Kisky1e224f32009-11-18 17:49:53 -0700220 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 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100226 snd_pcm_period_elapsed(substream);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100227 }
228}
229
Troy Kisky1e224f32009-11-18 17:49:53 -0700230static 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}
260
261/*
262 * Only used with ping/pong.
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,
Sekhar Nori48519f02010-07-19 12:31:16 +0530384 prtd->params->ram_chan_q);
Troy Kisky1e224f32009-11-18 17:49:53 -0700385 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;
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);
Ben Gardinerfb1e97032011-05-24 14:50:15 -0400428 prtd->asp_params.opt |= TCCHEN |
429 EDMA_TCC(prtd->ram_channel & 0x3f);
Troy Kisky1e224f32009-11-18 17:49:53 -0700430 edma_write_slot(link, &prtd->asp_params);
431
432 /* pong */
433 link = prtd->asp_link[1];
434 edma_set_src(link, asp_src_ping, INCR, W16BIT);
435 edma_set_dest(link, asp_dst_ping, INCR, W16BIT);
436 edma_set_src_index(link, 0, 0);
437 edma_set_dest_index(link, 0, 0);
438
439 edma_read_slot(link, &prtd->asp_params);
440 prtd->asp_params.opt &= ~(TCCMODE | EDMA_TCC(0x3f));
441 /* interrupt after every pong completion */
442 prtd->asp_params.opt |= TCINTEN | TCCHEN |
Ben Gardinerfb1e97032011-05-24 14:50:15 -0400443 EDMA_TCC(prtd->ram_channel & 0x3f);
Troy Kisky1e224f32009-11-18 17:49:53 -0700444 edma_write_slot(link, &prtd->asp_params);
445
446 /* ram */
447 link = prtd->ram_link;
448 edma_set_src(link, iram_dma->addr, INCR, W32BIT);
449 edma_set_dest(link, iram_dma->addr, INCR, W32BIT);
450 pr_debug("%s: audio dma channels/slots in use for ram:%u %u %u,"
451 "for asp:%u %u %u\n", __func__,
452 prtd->ram_channel, prtd->ram_link, prtd->ram_link2,
453 prtd->asp_channel, prtd->asp_link[0],
454 prtd->asp_link[1]);
455 return 0;
456exit4:
457 edma_free_channel(prtd->asp_link[1]);
458 prtd->asp_link[1] = -1;
459exit3:
460 edma_free_channel(prtd->ram_link);
461 prtd->ram_link = -1;
462exit2:
463 edma_free_channel(prtd->ram_channel);
464 prtd->ram_channel = -1;
465exit1:
466 return link;
467}
468
Vladimir Barinov310355c2008-02-18 11:40:22 +0100469static int davinci_pcm_dma_request(struct snd_pcm_substream *substream)
470{
Troy Kisky1e224f32009-11-18 17:49:53 -0700471 struct snd_dma_buffer *iram_dma;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100472 struct davinci_runtime_data *prtd = substream->runtime->private_data;
Troy Kisky1e224f32009-11-18 17:49:53 -0700473 struct davinci_pcm_dma_params *params = prtd->params;
474 int link;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100475
Troy Kisky1e224f32009-11-18 17:49:53 -0700476 if (!params)
477 return -ENODEV;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100478
Troy Kisky1e224f32009-11-18 17:49:53 -0700479 /* Request asp master DMA channel */
480 link = prtd->asp_channel = edma_alloc_channel(params->channel,
Sekhar Nori48519f02010-07-19 12:31:16 +0530481 davinci_pcm_dma_irq, substream,
482 prtd->params->asp_chan_q);
Troy Kisky1e224f32009-11-18 17:49:53 -0700483 if (link < 0)
484 goto exit1;
485
486 /* Request asp link channels */
487 link = prtd->asp_link[0] = edma_alloc_slot(
488 EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY);
489 if (link < 0)
490 goto exit2;
491
492 iram_dma = (struct snd_dma_buffer *)substream->dma_buffer.private_data;
493 if (iram_dma) {
494 if (request_ping_pong(substream, prtd, iram_dma) == 0)
495 return 0;
496 printk(KERN_WARNING "%s: dma channel allocation failed,"
497 "not using sram\n", __func__);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100498 }
499
David Brownell82075af2009-05-14 12:41:22 -0700500 /* Issue transfer completion IRQ when the channel completes a
501 * transfer, then always reload from the same slot (by a kind
502 * of loopback link). The completion IRQ handler will update
503 * the reload slot with a new buffer.
504 *
505 * REVISIT save p_ram here after setting up everything except
506 * the buffer and its length (ccnt) ... use it as a template
507 * so davinci_pcm_enqueue_dma() takes less time in IRQ.
508 */
Troy Kisky1e224f32009-11-18 17:49:53 -0700509 edma_read_slot(link, &prtd->asp_params);
510 prtd->asp_params.opt |= TCINTEN |
511 EDMA_TCC(EDMA_CHAN_SLOT(prtd->asp_channel));
512 prtd->asp_params.link_bcntrld = EDMA_CHAN_SLOT(link) << 5;
513 edma_write_slot(link, &prtd->asp_params);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100514 return 0;
Troy Kisky1e224f32009-11-18 17:49:53 -0700515exit2:
516 edma_free_channel(prtd->asp_channel);
517 prtd->asp_channel = -1;
518exit1:
519 return link;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100520}
521
522static int davinci_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
523{
524 struct davinci_runtime_data *prtd = substream->runtime->private_data;
525 int ret = 0;
526
527 spin_lock(&prtd->lock);
528
529 switch (cmd) {
530 case SNDRV_PCM_TRIGGER_START:
531 case SNDRV_PCM_TRIGGER_RESUME:
532 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Troy Kisky2b7b2502009-11-18 17:49:54 -0700533 edma_resume(prtd->asp_channel);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100534 break;
535 case SNDRV_PCM_TRIGGER_STOP:
536 case SNDRV_PCM_TRIGGER_SUSPEND:
537 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Troy Kisky2b7b2502009-11-18 17:49:54 -0700538 edma_pause(prtd->asp_channel);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100539 break;
540 default:
541 ret = -EINVAL;
542 break;
543 }
544
545 spin_unlock(&prtd->lock);
546
547 return ret;
548}
549
550static int davinci_pcm_prepare(struct snd_pcm_substream *substream)
551{
552 struct davinci_runtime_data *prtd = substream->runtime->private_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100553
Troy Kisky1e224f32009-11-18 17:49:53 -0700554 if (prtd->ram_channel >= 0) {
555 int ret = ping_pong_dma_setup(substream);
556 if (ret < 0)
557 return ret;
558
559 edma_write_slot(prtd->ram_channel, &prtd->ram_params);
560 edma_write_slot(prtd->asp_channel, &prtd->asp_params);
561
562 print_buf_info(prtd->ram_channel, "ram_channel");
563 print_buf_info(prtd->ram_link, "ram_link");
564 print_buf_info(prtd->ram_link2, "ram_link2");
565 print_buf_info(prtd->asp_channel, "asp_channel");
566 print_buf_info(prtd->asp_link[0], "asp_link[0]");
567 print_buf_info(prtd->asp_link[1], "asp_link[1]");
568
569 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
570 /* copy 1st iram buffer */
571 edma_start(prtd->ram_channel);
572 }
Troy Kisky2b7b2502009-11-18 17:49:54 -0700573 edma_start(prtd->asp_channel);
Troy Kisky1e224f32009-11-18 17:49:53 -0700574 return 0;
575 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100576 prtd->period = 0;
577 davinci_pcm_enqueue_dma(substream);
578
David Brownell82075af2009-05-14 12:41:22 -0700579 /* Copy self-linked parameter RAM entry into master channel */
Troy Kisky1e224f32009-11-18 17:49:53 -0700580 edma_read_slot(prtd->asp_link[0], &prtd->asp_params);
581 edma_write_slot(prtd->asp_channel, &prtd->asp_params);
Troy Kisky6e541472009-07-07 17:36:06 -0700582 davinci_pcm_enqueue_dma(substream);
Troy Kisky2b7b2502009-11-18 17:49:54 -0700583 edma_start(prtd->asp_channel);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100584
585 return 0;
586}
587
588static snd_pcm_uframes_t
589davinci_pcm_pointer(struct snd_pcm_substream *substream)
590{
591 struct snd_pcm_runtime *runtime = substream->runtime;
592 struct davinci_runtime_data *prtd = runtime->private_data;
593 unsigned int offset;
Troy Kisky1587ea312009-11-18 17:49:52 -0700594 int asp_count;
595 dma_addr_t asp_src, asp_dst;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100596
597 spin_lock(&prtd->lock);
Troy Kisky1e224f32009-11-18 17:49:53 -0700598 if (prtd->ram_channel >= 0) {
599 int ram_count;
600 int mod_ram;
601 dma_addr_t ram_src, ram_dst;
602 unsigned int period_size = snd_pcm_lib_period_bytes(substream);
603 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
604 /* reading ram before asp should be safe
605 * as long as the asp transfers less than a ping size
606 * of bytes between the 2 reads
607 */
608 edma_get_position(prtd->ram_channel,
609 &ram_src, &ram_dst);
610 edma_get_position(prtd->asp_channel,
611 &asp_src, &asp_dst);
612 asp_count = asp_src - prtd->asp_params.src;
613 ram_count = ram_src - prtd->ram_params.src;
614 mod_ram = ram_count % period_size;
615 mod_ram -= asp_count;
616 if (mod_ram < 0)
617 mod_ram += period_size;
618 else if (mod_ram == 0) {
619 if (snd_pcm_running(substream))
620 mod_ram += period_size;
621 }
622 ram_count -= mod_ram;
623 if (ram_count < 0)
624 ram_count += period_size * runtime->periods;
625 } else {
626 edma_get_position(prtd->ram_channel,
627 &ram_src, &ram_dst);
628 ram_count = ram_dst - prtd->ram_params.dst;
629 }
630 asp_count = ram_count;
631 } else {
632 edma_get_position(prtd->asp_channel, &asp_src, &asp_dst);
633 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
634 asp_count = asp_src - runtime->dma_addr;
635 else
636 asp_count = asp_dst - runtime->dma_addr;
637 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100638 spin_unlock(&prtd->lock);
639
Troy Kisky1587ea312009-11-18 17:49:52 -0700640 offset = bytes_to_frames(runtime, asp_count);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100641 if (offset >= runtime->buffer_size)
642 offset = 0;
643
644 return offset;
645}
646
647static int davinci_pcm_open(struct snd_pcm_substream *substream)
648{
649 struct snd_pcm_runtime *runtime = substream->runtime;
650 struct davinci_runtime_data *prtd;
Troy Kisky1e224f32009-11-18 17:49:53 -0700651 struct snd_pcm_hardware *ppcm;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100652 int ret = 0;
Troy Kisky81ac55a2009-09-11 14:29:02 -0700653 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Daniel Mack5f712b22010-03-22 10:11:15 +0100654 struct davinci_pcm_dma_params *pa;
Troy Kisky57512c62009-11-16 16:52:31 -0700655 struct davinci_pcm_dma_params *params;
Daniel Mack5f712b22010-03-22 10:11:15 +0100656
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000657 pa = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Troy Kisky57512c62009-11-16 16:52:31 -0700658 if (!pa)
Troy Kisky81ac55a2009-09-11 14:29:02 -0700659 return -ENODEV;
Troy Kisky57512c62009-11-16 16:52:31 -0700660 params = &pa[substream->stream];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100661
Troy Kisky1e224f32009-11-18 17:49:53 -0700662 ppcm = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
663 &pcm_hardware_playback : &pcm_hardware_capture;
664 allocate_sram(substream, params->sram_size, ppcm);
665 snd_soc_set_runtime_hwparams(substream, ppcm);
Troy Kisky6a90d532009-08-06 16:55:32 -0700666 /* ensure that buffer size is a multiple of period size */
667 ret = snd_pcm_hw_constraint_integer(runtime,
668 SNDRV_PCM_HW_PARAM_PERIODS);
669 if (ret < 0)
670 return ret;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100671
672 prtd = kzalloc(sizeof(struct davinci_runtime_data), GFP_KERNEL);
673 if (prtd == NULL)
674 return -ENOMEM;
675
676 spin_lock_init(&prtd->lock);
Troy Kisky81ac55a2009-09-11 14:29:02 -0700677 prtd->params = params;
Troy Kisky1e224f32009-11-18 17:49:53 -0700678 prtd->asp_channel = -1;
679 prtd->asp_link[0] = prtd->asp_link[1] = -1;
680 prtd->ram_channel = -1;
681 prtd->ram_link = -1;
682 prtd->ram_link2 = -1;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100683
684 runtime->private_data = prtd;
685
686 ret = davinci_pcm_dma_request(substream);
687 if (ret) {
688 printk(KERN_ERR "davinci_pcm: Failed to get dma channels\n");
689 kfree(prtd);
690 }
691
692 return ret;
693}
694
695static int davinci_pcm_close(struct snd_pcm_substream *substream)
696{
697 struct snd_pcm_runtime *runtime = substream->runtime;
698 struct davinci_runtime_data *prtd = runtime->private_data;
699
Troy Kisky1e224f32009-11-18 17:49:53 -0700700 if (prtd->ram_channel >= 0)
701 edma_stop(prtd->ram_channel);
702 if (prtd->asp_channel >= 0)
703 edma_stop(prtd->asp_channel);
704 if (prtd->asp_link[0] >= 0)
705 edma_unlink(prtd->asp_link[0]);
706 if (prtd->asp_link[1] >= 0)
707 edma_unlink(prtd->asp_link[1]);
708 if (prtd->ram_link >= 0)
709 edma_unlink(prtd->ram_link);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100710
Troy Kisky1e224f32009-11-18 17:49:53 -0700711 if (prtd->asp_link[0] >= 0)
712 edma_free_slot(prtd->asp_link[0]);
713 if (prtd->asp_link[1] >= 0)
714 edma_free_slot(prtd->asp_link[1]);
715 if (prtd->asp_channel >= 0)
716 edma_free_channel(prtd->asp_channel);
717 if (prtd->ram_link >= 0)
718 edma_free_slot(prtd->ram_link);
719 if (prtd->ram_link2 >= 0)
720 edma_free_slot(prtd->ram_link2);
721 if (prtd->ram_channel >= 0)
722 edma_free_channel(prtd->ram_channel);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100723
724 kfree(prtd);
725
726 return 0;
727}
728
729static int davinci_pcm_hw_params(struct snd_pcm_substream *substream,
730 struct snd_pcm_hw_params *hw_params)
731{
732 return snd_pcm_lib_malloc_pages(substream,
733 params_buffer_bytes(hw_params));
734}
735
736static int davinci_pcm_hw_free(struct snd_pcm_substream *substream)
737{
738 return snd_pcm_lib_free_pages(substream);
739}
740
741static int davinci_pcm_mmap(struct snd_pcm_substream *substream,
742 struct vm_area_struct *vma)
743{
744 struct snd_pcm_runtime *runtime = substream->runtime;
745
746 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
747 runtime->dma_area,
748 runtime->dma_addr,
749 runtime->dma_bytes);
750}
751
Mark Brownb2a19d02009-01-17 19:14:26 +0000752static struct snd_pcm_ops davinci_pcm_ops = {
Vladimir Barinov310355c2008-02-18 11:40:22 +0100753 .open = davinci_pcm_open,
754 .close = davinci_pcm_close,
755 .ioctl = snd_pcm_lib_ioctl,
756 .hw_params = davinci_pcm_hw_params,
757 .hw_free = davinci_pcm_hw_free,
758 .prepare = davinci_pcm_prepare,
759 .trigger = davinci_pcm_trigger,
760 .pointer = davinci_pcm_pointer,
761 .mmap = davinci_pcm_mmap,
762};
763
Troy Kisky1e224f32009-11-18 17:49:53 -0700764static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream,
765 size_t size)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100766{
767 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
768 struct snd_dma_buffer *buf = &substream->dma_buffer;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100769
770 buf->dev.type = SNDRV_DMA_TYPE_DEV;
771 buf->dev.dev = pcm->card->dev;
772 buf->private_data = NULL;
773 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
774 &buf->addr, GFP_KERNEL);
775
Alexander Beregalov9cd28ab2008-12-13 16:25:27 +0300776 pr_debug("davinci_pcm: preallocate_dma_buffer: area=%p, addr=%p, "
777 "size=%d\n", (void *) buf->area, (void *) buf->addr, size);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100778
779 if (!buf->area)
780 return -ENOMEM;
781
782 buf->bytes = size;
783 return 0;
784}
785
786static void davinci_pcm_free(struct snd_pcm *pcm)
787{
788 struct snd_pcm_substream *substream;
789 struct snd_dma_buffer *buf;
790 int stream;
791
792 for (stream = 0; stream < 2; stream++) {
Troy Kisky1e224f32009-11-18 17:49:53 -0700793 struct snd_dma_buffer *iram_dma;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100794 substream = pcm->streams[stream].substream;
795 if (!substream)
796 continue;
797
798 buf = &substream->dma_buffer;
799 if (!buf->area)
800 continue;
801
802 dma_free_writecombine(pcm->card->dev, buf->bytes,
803 buf->area, buf->addr);
804 buf->area = NULL;
Joe Perches4726a572010-07-12 13:50:27 -0700805 iram_dma = buf->private_data;
Troy Kisky1e224f32009-11-18 17:49:53 -0700806 if (iram_dma) {
807 sram_free(iram_dma->area, iram_dma->bytes);
808 kfree(iram_dma);
809 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100810 }
811}
812
813static u64 davinci_pcm_dmamask = 0xffffffff;
814
815static int davinci_pcm_new(struct snd_card *card,
Liam Girdwood9cb132d2008-07-07 16:07:42 +0100816 struct snd_soc_dai *dai, struct snd_pcm *pcm)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100817{
818 int ret;
819
820 if (!card->dev->dma_mask)
821 card->dev->dma_mask = &davinci_pcm_dmamask;
822 if (!card->dev->coherent_dma_mask)
823 card->dev->coherent_dma_mask = 0xffffffff;
824
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000825 if (dai->driver->playback.channels_min) {
Vladimir Barinov310355c2008-02-18 11:40:22 +0100826 ret = davinci_pcm_preallocate_dma_buffer(pcm,
Troy Kisky1e224f32009-11-18 17:49:53 -0700827 SNDRV_PCM_STREAM_PLAYBACK,
828 pcm_hardware_playback.buffer_bytes_max);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100829 if (ret)
830 return ret;
831 }
832
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000833 if (dai->driver->capture.channels_min) {
Vladimir Barinov310355c2008-02-18 11:40:22 +0100834 ret = davinci_pcm_preallocate_dma_buffer(pcm,
Troy Kisky1e224f32009-11-18 17:49:53 -0700835 SNDRV_PCM_STREAM_CAPTURE,
836 pcm_hardware_capture.buffer_bytes_max);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100837 if (ret)
838 return ret;
839 }
840
841 return 0;
842}
843
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000844static struct snd_soc_platform_driver davinci_soc_platform = {
845 .ops = &davinci_pcm_ops,
Vladimir Barinov310355c2008-02-18 11:40:22 +0100846 .pcm_new = davinci_pcm_new,
847 .pcm_free = davinci_pcm_free,
848};
Vladimir Barinov310355c2008-02-18 11:40:22 +0100849
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000850static int __devinit davinci_soc_platform_probe(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000851{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000852 return snd_soc_register_platform(&pdev->dev, &davinci_soc_platform);
Mark Brown958e7922008-12-03 19:58:17 +0000853}
Mark Brown958e7922008-12-03 19:58:17 +0000854
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000855static int __devexit davinci_soc_platform_remove(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000856{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000857 snd_soc_unregister_platform(&pdev->dev);
858 return 0;
Mark Brown958e7922008-12-03 19:58:17 +0000859}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000860
861static struct platform_driver davinci_pcm_driver = {
862 .driver = {
863 .name = "davinci-pcm-audio",
864 .owner = THIS_MODULE,
865 },
866
867 .probe = davinci_soc_platform_probe,
868 .remove = __devexit_p(davinci_soc_platform_remove),
869};
870
871static int __init snd_davinci_pcm_init(void)
872{
873 return platform_driver_register(&davinci_pcm_driver);
874}
875module_init(snd_davinci_pcm_init);
876
877static void __exit snd_davinci_pcm_exit(void)
878{
879 platform_driver_unregister(&davinci_pcm_driver);
880}
881module_exit(snd_davinci_pcm_exit);
Mark Brown958e7922008-12-03 19:58:17 +0000882
Vladimir Barinov310355c2008-02-18 11:40:22 +0100883MODULE_AUTHOR("Vladimir Barinov");
884MODULE_DESCRIPTION("TI DAVINCI PCM DMA module");
885MODULE_LICENSE("GPL");