blob: 5d9269a5176184ef79144ce6f9cef1ebfac3f1e1 [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 |
Chaithrika U Sa47979b2009-12-03 18:56:56 +053068 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
Ben Gardiner8e56d5b2011-05-24 14:50:16 -040069 .formats = DAVINCI_PCM_FMTBITS,
Vladimir Barinov310355c2008-02-18 11:40:22 +010070 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
71 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
72 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
73 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
74 SNDRV_PCM_RATE_KNOT),
75 .rate_min = 8000,
76 .rate_max = 96000,
77 .channels_min = 2,
Ben Gardineracb8e262011-05-24 14:50:17 -040078 .channels_max = 384,
Vladimir Barinov310355c2008-02-18 11:40:22 +010079 .buffer_bytes_max = 128 * 1024,
80 .period_bytes_min = 32,
81 .period_bytes_max = 8 * 1024,
82 .periods_min = 16,
83 .periods_max = 255,
84 .fifo_size = 0,
85};
86
Troy Kisky1e224f32009-11-18 17:49:53 -070087static struct snd_pcm_hardware pcm_hardware_capture = {
88 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
89 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
90 SNDRV_PCM_INFO_PAUSE),
Ben Gardiner8e56d5b2011-05-24 14:50:16 -040091 .formats = DAVINCI_PCM_FMTBITS,
Troy Kisky1e224f32009-11-18 17:49:53 -070092 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
93 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
94 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
95 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
96 SNDRV_PCM_RATE_KNOT),
97 .rate_min = 8000,
98 .rate_max = 96000,
99 .channels_min = 2,
Ben Gardineracb8e262011-05-24 14:50:17 -0400100 .channels_max = 384,
Troy Kisky1e224f32009-11-18 17:49:53 -0700101 .buffer_bytes_max = 128 * 1024,
102 .period_bytes_min = 32,
103 .period_bytes_max = 8 * 1024,
104 .periods_min = 16,
105 .periods_max = 255,
106 .fifo_size = 0,
107};
108
109/*
110 * How ping/pong works....
111 *
112 * Playback:
113 * ram_params - copys 2*ping_size from start of SDRAM to iram,
114 * links to ram_link2
115 * ram_link2 - copys rest of SDRAM to iram in ping_size units,
116 * links to ram_link
117 * ram_link - copys entire SDRAM to iram in ping_size uints,
118 * links to self
119 *
120 * asp_params - same as asp_link[0]
121 * asp_link[0] - copys from lower half of iram to asp port
122 * links to asp_link[1], triggers iram copy event on completion
123 * asp_link[1] - copys from upper half of iram to asp port
124 * links to asp_link[0], triggers iram copy event on completion
125 * triggers interrupt only needed to let upper SOC levels update position
126 * in stream on completion
127 *
128 * When playback is started:
129 * ram_params started
130 * asp_params started
131 *
132 * Capture:
133 * ram_params - same as ram_link,
134 * links to ram_link
135 * ram_link - same as playback
136 * links to self
137 *
138 * asp_params - same as playback
139 * asp_link[0] - same as playback
140 * asp_link[1] - same as playback
141 *
142 * When capture is started:
143 * asp_params started
144 */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100145struct davinci_runtime_data {
146 spinlock_t lock;
147 int period; /* current DMA period */
Troy Kisky1587ea312009-11-18 17:49:52 -0700148 int asp_channel; /* Master DMA channel */
149 int asp_link[2]; /* asp parameter link channel, ping/pong */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100150 struct davinci_pcm_dma_params *params; /* DMA params */
Troy Kisky1e224f32009-11-18 17:49:53 -0700151 int ram_channel;
152 int ram_link;
153 int ram_link2;
154 struct edmacc_param asp_params;
155 struct edmacc_param ram_params;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100156};
157
Troy Kisky1e224f32009-11-18 17:49:53 -0700158/*
159 * Not used with ping/pong
160 */
Vladimir Barinov310355c2008-02-18 11:40:22 +0100161static void davinci_pcm_enqueue_dma(struct snd_pcm_substream *substream)
162{
163 struct davinci_runtime_data *prtd = substream->runtime->private_data;
164 struct snd_pcm_runtime *runtime = substream->runtime;
Troy Kisky1587ea312009-11-18 17:49:52 -0700165 int link = prtd->asp_link[0];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100166 unsigned int period_size;
167 unsigned int dma_offset;
168 dma_addr_t dma_pos;
169 dma_addr_t src, dst;
170 unsigned short src_bidx, dst_bidx;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400171 unsigned short src_cidx, dst_cidx;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100172 unsigned int data_type;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400173 unsigned short acnt;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100174 unsigned int count;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400175 unsigned int fifo_level;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100176
177 period_size = snd_pcm_lib_period_bytes(substream);
178 dma_offset = prtd->period * period_size;
179 dma_pos = runtime->dma_addr + dma_offset;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400180 fifo_level = prtd->params->fifo_level;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100181
Alexander Beregalov9cd28ab2008-12-13 16:25:27 +0300182 pr_debug("davinci_pcm: audio_set_dma_params_play channel = %d "
Troy Kisky1587ea312009-11-18 17:49:52 -0700183 "dma_ptr = %x period_size=%x\n", link, dma_pos, period_size);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100184
185 data_type = prtd->params->data_type;
186 count = period_size / data_type;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400187 if (fifo_level)
188 count /= fifo_level;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100189
190 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
191 src = dma_pos;
192 dst = prtd->params->dma_addr;
193 src_bidx = data_type;
194 dst_bidx = 0;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400195 src_cidx = data_type * fifo_level;
196 dst_cidx = 0;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100197 } else {
198 src = prtd->params->dma_addr;
199 dst = dma_pos;
200 src_bidx = 0;
201 dst_bidx = data_type;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400202 src_cidx = 0;
203 dst_cidx = data_type * fifo_level;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100204 }
205
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400206 acnt = prtd->params->acnt;
Troy Kisky1587ea312009-11-18 17:49:52 -0700207 edma_set_src(link, src, INCR, W8BIT);
208 edma_set_dest(link, dst, INCR, W8BIT);
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400209
Troy Kisky1587ea312009-11-18 17:49:52 -0700210 edma_set_src_index(link, src_bidx, src_cidx);
211 edma_set_dest_index(link, dst_bidx, dst_cidx);
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400212
213 if (!fifo_level)
Troy Kisky1587ea312009-11-18 17:49:52 -0700214 edma_set_transfer_params(link, acnt, count, 1, 0, ASYNC);
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400215 else
Troy Kisky1587ea312009-11-18 17:49:52 -0700216 edma_set_transfer_params(link, acnt, fifo_level, count,
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400217 fifo_level, ABSYNC);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100218
219 prtd->period++;
220 if (unlikely(prtd->period >= runtime->periods))
221 prtd->period = 0;
222}
223
Troy Kisky1587ea312009-11-18 17:49:52 -0700224static void davinci_pcm_dma_irq(unsigned link, u16 ch_status, void *data)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100225{
226 struct snd_pcm_substream *substream = data;
227 struct davinci_runtime_data *prtd = substream->runtime->private_data;
228
Troy Kisky1e224f32009-11-18 17:49:53 -0700229 print_buf_info(prtd->ram_channel, "i ram_channel");
Troy Kisky1587ea312009-11-18 17:49:52 -0700230 pr_debug("davinci_pcm: link=%d, status=0x%x\n", link, ch_status);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100231
232 if (unlikely(ch_status != DMA_COMPLETE))
233 return;
234
235 if (snd_pcm_running(substream)) {
Troy Kisky1e224f32009-11-18 17:49:53 -0700236 if (prtd->ram_channel < 0) {
237 /* No ping/pong must fix up link dma data*/
238 spin_lock(&prtd->lock);
239 davinci_pcm_enqueue_dma(substream);
240 spin_unlock(&prtd->lock);
241 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100242 snd_pcm_period_elapsed(substream);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100243 }
244}
245
Troy Kisky1e224f32009-11-18 17:49:53 -0700246static int allocate_sram(struct snd_pcm_substream *substream, unsigned size,
247 struct snd_pcm_hardware *ppcm)
248{
249 struct snd_dma_buffer *buf = &substream->dma_buffer;
250 struct snd_dma_buffer *iram_dma = NULL;
251 dma_addr_t iram_phys = 0;
252 void *iram_virt = NULL;
253
254 if (buf->private_data || !size)
255 return 0;
256
257 ppcm->period_bytes_max = size;
258 iram_virt = sram_alloc(size, &iram_phys);
259 if (!iram_virt)
260 goto exit1;
261 iram_dma = kzalloc(sizeof(*iram_dma), GFP_KERNEL);
262 if (!iram_dma)
263 goto exit2;
264 iram_dma->area = iram_virt;
265 iram_dma->addr = iram_phys;
266 memset(iram_dma->area, 0, size);
267 iram_dma->bytes = size;
268 buf->private_data = iram_dma;
269 return 0;
270exit2:
271 if (iram_virt)
272 sram_free(iram_virt, size);
273exit1:
274 return -ENOMEM;
275}
276
277/*
278 * Only used with ping/pong.
279 * This is called after runtime->dma_addr, period_bytes and data_type are valid
280 */
281static int ping_pong_dma_setup(struct snd_pcm_substream *substream)
282{
283 unsigned short ram_src_cidx, ram_dst_cidx;
284 struct snd_pcm_runtime *runtime = substream->runtime;
285 struct davinci_runtime_data *prtd = runtime->private_data;
286 struct snd_dma_buffer *iram_dma =
287 (struct snd_dma_buffer *)substream->dma_buffer.private_data;
288 struct davinci_pcm_dma_params *params = prtd->params;
289 unsigned int data_type = params->data_type;
290 unsigned int acnt = params->acnt;
291 /* divide by 2 for ping/pong */
292 unsigned int ping_size = snd_pcm_lib_period_bytes(substream) >> 1;
293 int link = prtd->asp_link[1];
294 unsigned int fifo_level = prtd->params->fifo_level;
295 unsigned int count;
296 if ((data_type == 0) || (data_type > 4)) {
297 printk(KERN_ERR "%s: data_type=%i\n", __func__, data_type);
298 return -EINVAL;
299 }
300 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
301 dma_addr_t asp_src_pong = iram_dma->addr + ping_size;
302 ram_src_cidx = ping_size;
303 ram_dst_cidx = -ping_size;
304 edma_set_src(link, asp_src_pong, INCR, W8BIT);
305
306 link = prtd->asp_link[0];
307 edma_set_src_index(link, data_type, data_type * fifo_level);
308 link = prtd->asp_link[1];
309 edma_set_src_index(link, data_type, data_type * fifo_level);
310
311 link = prtd->ram_link;
312 edma_set_src(link, runtime->dma_addr, INCR, W32BIT);
313 } else {
314 dma_addr_t asp_dst_pong = iram_dma->addr + ping_size;
315 ram_src_cidx = -ping_size;
316 ram_dst_cidx = ping_size;
317 edma_set_dest(link, asp_dst_pong, INCR, W8BIT);
318
319 link = prtd->asp_link[0];
320 edma_set_dest_index(link, data_type, data_type * fifo_level);
321 link = prtd->asp_link[1];
322 edma_set_dest_index(link, data_type, data_type * fifo_level);
323
324 link = prtd->ram_link;
325 edma_set_dest(link, runtime->dma_addr, INCR, W32BIT);
326 }
327
328 if (!fifo_level) {
329 count = ping_size / data_type;
330 edma_set_transfer_params(prtd->asp_link[0], acnt, count,
331 1, 0, ASYNC);
332 edma_set_transfer_params(prtd->asp_link[1], acnt, count,
333 1, 0, ASYNC);
334 } else {
335 count = ping_size / (data_type * fifo_level);
336 edma_set_transfer_params(prtd->asp_link[0], acnt, fifo_level,
337 count, fifo_level, ABSYNC);
338 edma_set_transfer_params(prtd->asp_link[1], acnt, fifo_level,
339 count, fifo_level, ABSYNC);
340 }
341
342 link = prtd->ram_link;
343 edma_set_src_index(link, ping_size, ram_src_cidx);
344 edma_set_dest_index(link, ping_size, ram_dst_cidx);
345 edma_set_transfer_params(link, ping_size, 2,
346 runtime->periods, 2, ASYNC);
347
348 /* init master params */
349 edma_read_slot(prtd->asp_link[0], &prtd->asp_params);
350 edma_read_slot(prtd->ram_link, &prtd->ram_params);
351 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
352 struct edmacc_param p_ram;
353 /* Copy entire iram buffer before playback started */
354 prtd->ram_params.a_b_cnt = (1 << 16) | (ping_size << 1);
355 /* 0 dst_bidx */
356 prtd->ram_params.src_dst_bidx = (ping_size << 1);
357 /* 0 dst_cidx */
358 prtd->ram_params.src_dst_cidx = (ping_size << 1);
359 prtd->ram_params.ccnt = 1;
360
361 /* Skip 1st period */
362 edma_read_slot(prtd->ram_link, &p_ram);
363 p_ram.src += (ping_size << 1);
364 p_ram.ccnt -= 1;
365 edma_write_slot(prtd->ram_link2, &p_ram);
366 /*
367 * When 1st started, ram -> iram dma channel will fill the
368 * entire iram. Then, whenever a ping/pong asp buffer finishes,
369 * 1/2 iram will be filled.
370 */
371 prtd->ram_params.link_bcntrld =
372 EDMA_CHAN_SLOT(prtd->ram_link2) << 5;
373 }
374 return 0;
375}
376
377/* 1 asp tx or rx channel using 2 parameter channels
378 * 1 ram to/from iram channel using 1 parameter channel
379 *
380 * Playback
381 * ram copy channel kicks off first,
382 * 1st ram copy of entire iram buffer completion kicks off asp channel
383 * asp tcc always kicks off ram copy of 1/2 iram buffer
384 *
385 * Record
386 * asp channel starts, tcc kicks off ram copy
387 */
388static int request_ping_pong(struct snd_pcm_substream *substream,
389 struct davinci_runtime_data *prtd,
390 struct snd_dma_buffer *iram_dma)
391{
392 dma_addr_t asp_src_ping;
393 dma_addr_t asp_dst_ping;
394 int link;
395 struct davinci_pcm_dma_params *params = prtd->params;
396
397 /* Request ram master channel */
398 link = prtd->ram_channel = edma_alloc_channel(EDMA_CHANNEL_ANY,
399 davinci_pcm_dma_irq, substream,
Sekhar Nori48519f02010-07-19 12:31:16 +0530400 prtd->params->ram_chan_q);
Troy Kisky1e224f32009-11-18 17:49:53 -0700401 if (link < 0)
402 goto exit1;
403
404 /* Request ram link channel */
405 link = prtd->ram_link = edma_alloc_slot(
406 EDMA_CTLR(prtd->ram_channel), EDMA_SLOT_ANY);
407 if (link < 0)
408 goto exit2;
409
410 link = prtd->asp_link[1] = edma_alloc_slot(
411 EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY);
412 if (link < 0)
413 goto exit3;
414
415 prtd->ram_link2 = -1;
416 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
417 link = prtd->ram_link2 = edma_alloc_slot(
418 EDMA_CTLR(prtd->ram_channel), EDMA_SLOT_ANY);
419 if (link < 0)
420 goto exit4;
421 }
422 /* circle ping-pong buffers */
423 edma_link(prtd->asp_link[0], prtd->asp_link[1]);
424 edma_link(prtd->asp_link[1], prtd->asp_link[0]);
425 /* circle ram buffers */
426 edma_link(prtd->ram_link, prtd->ram_link);
427
428 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
429 asp_src_ping = iram_dma->addr;
430 asp_dst_ping = params->dma_addr; /* fifo */
431 } else {
432 asp_src_ping = params->dma_addr; /* fifo */
433 asp_dst_ping = iram_dma->addr;
434 }
435 /* ping */
436 link = prtd->asp_link[0];
437 edma_set_src(link, asp_src_ping, INCR, W16BIT);
438 edma_set_dest(link, asp_dst_ping, INCR, W16BIT);
439 edma_set_src_index(link, 0, 0);
440 edma_set_dest_index(link, 0, 0);
441
442 edma_read_slot(link, &prtd->asp_params);
443 prtd->asp_params.opt &= ~(TCCMODE | EDMA_TCC(0x3f) | TCINTEN);
Ben Gardinerfb1e97032011-05-24 14:50:15 -0400444 prtd->asp_params.opt |= TCCHEN |
445 EDMA_TCC(prtd->ram_channel & 0x3f);
Troy Kisky1e224f32009-11-18 17:49:53 -0700446 edma_write_slot(link, &prtd->asp_params);
447
448 /* pong */
449 link = prtd->asp_link[1];
450 edma_set_src(link, asp_src_ping, INCR, W16BIT);
451 edma_set_dest(link, asp_dst_ping, INCR, W16BIT);
452 edma_set_src_index(link, 0, 0);
453 edma_set_dest_index(link, 0, 0);
454
455 edma_read_slot(link, &prtd->asp_params);
456 prtd->asp_params.opt &= ~(TCCMODE | EDMA_TCC(0x3f));
457 /* interrupt after every pong completion */
458 prtd->asp_params.opt |= TCINTEN | TCCHEN |
Ben Gardinerfb1e97032011-05-24 14:50:15 -0400459 EDMA_TCC(prtd->ram_channel & 0x3f);
Troy Kisky1e224f32009-11-18 17:49:53 -0700460 edma_write_slot(link, &prtd->asp_params);
461
462 /* ram */
463 link = prtd->ram_link;
464 edma_set_src(link, iram_dma->addr, INCR, W32BIT);
465 edma_set_dest(link, iram_dma->addr, INCR, W32BIT);
466 pr_debug("%s: audio dma channels/slots in use for ram:%u %u %u,"
467 "for asp:%u %u %u\n", __func__,
468 prtd->ram_channel, prtd->ram_link, prtd->ram_link2,
469 prtd->asp_channel, prtd->asp_link[0],
470 prtd->asp_link[1]);
471 return 0;
472exit4:
473 edma_free_channel(prtd->asp_link[1]);
474 prtd->asp_link[1] = -1;
475exit3:
476 edma_free_channel(prtd->ram_link);
477 prtd->ram_link = -1;
478exit2:
479 edma_free_channel(prtd->ram_channel);
480 prtd->ram_channel = -1;
481exit1:
482 return link;
483}
484
Vladimir Barinov310355c2008-02-18 11:40:22 +0100485static int davinci_pcm_dma_request(struct snd_pcm_substream *substream)
486{
Troy Kisky1e224f32009-11-18 17:49:53 -0700487 struct snd_dma_buffer *iram_dma;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100488 struct davinci_runtime_data *prtd = substream->runtime->private_data;
Troy Kisky1e224f32009-11-18 17:49:53 -0700489 struct davinci_pcm_dma_params *params = prtd->params;
490 int link;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100491
Troy Kisky1e224f32009-11-18 17:49:53 -0700492 if (!params)
493 return -ENODEV;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100494
Troy Kisky1e224f32009-11-18 17:49:53 -0700495 /* Request asp master DMA channel */
496 link = prtd->asp_channel = edma_alloc_channel(params->channel,
Sekhar Nori48519f02010-07-19 12:31:16 +0530497 davinci_pcm_dma_irq, substream,
498 prtd->params->asp_chan_q);
Troy Kisky1e224f32009-11-18 17:49:53 -0700499 if (link < 0)
500 goto exit1;
501
502 /* Request asp link channels */
503 link = prtd->asp_link[0] = edma_alloc_slot(
504 EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY);
505 if (link < 0)
506 goto exit2;
507
508 iram_dma = (struct snd_dma_buffer *)substream->dma_buffer.private_data;
509 if (iram_dma) {
510 if (request_ping_pong(substream, prtd, iram_dma) == 0)
511 return 0;
512 printk(KERN_WARNING "%s: dma channel allocation failed,"
513 "not using sram\n", __func__);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100514 }
515
David Brownell82075af2009-05-14 12:41:22 -0700516 /* Issue transfer completion IRQ when the channel completes a
517 * transfer, then always reload from the same slot (by a kind
518 * of loopback link). The completion IRQ handler will update
519 * the reload slot with a new buffer.
520 *
521 * REVISIT save p_ram here after setting up everything except
522 * the buffer and its length (ccnt) ... use it as a template
523 * so davinci_pcm_enqueue_dma() takes less time in IRQ.
524 */
Troy Kisky1e224f32009-11-18 17:49:53 -0700525 edma_read_slot(link, &prtd->asp_params);
526 prtd->asp_params.opt |= TCINTEN |
527 EDMA_TCC(EDMA_CHAN_SLOT(prtd->asp_channel));
528 prtd->asp_params.link_bcntrld = EDMA_CHAN_SLOT(link) << 5;
529 edma_write_slot(link, &prtd->asp_params);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100530 return 0;
Troy Kisky1e224f32009-11-18 17:49:53 -0700531exit2:
532 edma_free_channel(prtd->asp_channel);
533 prtd->asp_channel = -1;
534exit1:
535 return link;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100536}
537
538static int davinci_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
539{
540 struct davinci_runtime_data *prtd = substream->runtime->private_data;
541 int ret = 0;
542
543 spin_lock(&prtd->lock);
544
545 switch (cmd) {
546 case SNDRV_PCM_TRIGGER_START:
Ben Gardineref39eb62011-05-24 14:50:18 -0400547 edma_start(prtd->asp_channel);
548 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
549 prtd->ram_channel >= 0) {
550 /* copy 1st iram buffer */
551 edma_start(prtd->ram_channel);
552 }
553 break;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100554 case SNDRV_PCM_TRIGGER_RESUME:
555 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Troy Kisky2b7b2502009-11-18 17:49:54 -0700556 edma_resume(prtd->asp_channel);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100557 break;
558 case SNDRV_PCM_TRIGGER_STOP:
559 case SNDRV_PCM_TRIGGER_SUSPEND:
560 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Troy Kisky2b7b2502009-11-18 17:49:54 -0700561 edma_pause(prtd->asp_channel);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100562 break;
563 default:
564 ret = -EINVAL;
565 break;
566 }
567
568 spin_unlock(&prtd->lock);
569
570 return ret;
571}
572
573static int davinci_pcm_prepare(struct snd_pcm_substream *substream)
574{
575 struct davinci_runtime_data *prtd = substream->runtime->private_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100576
Troy Kisky1e224f32009-11-18 17:49:53 -0700577 if (prtd->ram_channel >= 0) {
578 int ret = ping_pong_dma_setup(substream);
579 if (ret < 0)
580 return ret;
581
582 edma_write_slot(prtd->ram_channel, &prtd->ram_params);
583 edma_write_slot(prtd->asp_channel, &prtd->asp_params);
584
585 print_buf_info(prtd->ram_channel, "ram_channel");
586 print_buf_info(prtd->ram_link, "ram_link");
587 print_buf_info(prtd->ram_link2, "ram_link2");
588 print_buf_info(prtd->asp_channel, "asp_channel");
589 print_buf_info(prtd->asp_link[0], "asp_link[0]");
590 print_buf_info(prtd->asp_link[1], "asp_link[1]");
591
Troy Kisky1e224f32009-11-18 17:49:53 -0700592 return 0;
593 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100594 prtd->period = 0;
595 davinci_pcm_enqueue_dma(substream);
596
David Brownell82075af2009-05-14 12:41:22 -0700597 /* Copy self-linked parameter RAM entry into master channel */
Troy Kisky1e224f32009-11-18 17:49:53 -0700598 edma_read_slot(prtd->asp_link[0], &prtd->asp_params);
599 edma_write_slot(prtd->asp_channel, &prtd->asp_params);
Troy Kisky6e541472009-07-07 17:36:06 -0700600 davinci_pcm_enqueue_dma(substream);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100601
602 return 0;
603}
604
605static snd_pcm_uframes_t
606davinci_pcm_pointer(struct snd_pcm_substream *substream)
607{
608 struct snd_pcm_runtime *runtime = substream->runtime;
609 struct davinci_runtime_data *prtd = runtime->private_data;
610 unsigned int offset;
Troy Kisky1587ea312009-11-18 17:49:52 -0700611 int asp_count;
612 dma_addr_t asp_src, asp_dst;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100613
614 spin_lock(&prtd->lock);
Troy Kisky1e224f32009-11-18 17:49:53 -0700615 if (prtd->ram_channel >= 0) {
616 int ram_count;
617 int mod_ram;
618 dma_addr_t ram_src, ram_dst;
619 unsigned int period_size = snd_pcm_lib_period_bytes(substream);
620 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
621 /* reading ram before asp should be safe
622 * as long as the asp transfers less than a ping size
623 * of bytes between the 2 reads
624 */
625 edma_get_position(prtd->ram_channel,
626 &ram_src, &ram_dst);
627 edma_get_position(prtd->asp_channel,
628 &asp_src, &asp_dst);
629 asp_count = asp_src - prtd->asp_params.src;
630 ram_count = ram_src - prtd->ram_params.src;
631 mod_ram = ram_count % period_size;
632 mod_ram -= asp_count;
633 if (mod_ram < 0)
634 mod_ram += period_size;
635 else if (mod_ram == 0) {
636 if (snd_pcm_running(substream))
637 mod_ram += period_size;
638 }
639 ram_count -= mod_ram;
640 if (ram_count < 0)
641 ram_count += period_size * runtime->periods;
642 } else {
643 edma_get_position(prtd->ram_channel,
644 &ram_src, &ram_dst);
645 ram_count = ram_dst - prtd->ram_params.dst;
646 }
647 asp_count = ram_count;
648 } else {
649 edma_get_position(prtd->asp_channel, &asp_src, &asp_dst);
650 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
651 asp_count = asp_src - runtime->dma_addr;
652 else
653 asp_count = asp_dst - runtime->dma_addr;
654 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100655 spin_unlock(&prtd->lock);
656
Troy Kisky1587ea312009-11-18 17:49:52 -0700657 offset = bytes_to_frames(runtime, asp_count);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100658 if (offset >= runtime->buffer_size)
659 offset = 0;
660
661 return offset;
662}
663
664static int davinci_pcm_open(struct snd_pcm_substream *substream)
665{
666 struct snd_pcm_runtime *runtime = substream->runtime;
667 struct davinci_runtime_data *prtd;
Troy Kisky1e224f32009-11-18 17:49:53 -0700668 struct snd_pcm_hardware *ppcm;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100669 int ret = 0;
Troy Kisky81ac55a2009-09-11 14:29:02 -0700670 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Daniel Mack5f712b22010-03-22 10:11:15 +0100671 struct davinci_pcm_dma_params *pa;
Troy Kisky57512c62009-11-16 16:52:31 -0700672 struct davinci_pcm_dma_params *params;
Daniel Mack5f712b22010-03-22 10:11:15 +0100673
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000674 pa = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Troy Kisky57512c62009-11-16 16:52:31 -0700675 if (!pa)
Troy Kisky81ac55a2009-09-11 14:29:02 -0700676 return -ENODEV;
Troy Kisky57512c62009-11-16 16:52:31 -0700677 params = &pa[substream->stream];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100678
Troy Kisky1e224f32009-11-18 17:49:53 -0700679 ppcm = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
680 &pcm_hardware_playback : &pcm_hardware_capture;
681 allocate_sram(substream, params->sram_size, ppcm);
682 snd_soc_set_runtime_hwparams(substream, ppcm);
Troy Kisky6a90d532009-08-06 16:55:32 -0700683 /* ensure that buffer size is a multiple of period size */
684 ret = snd_pcm_hw_constraint_integer(runtime,
685 SNDRV_PCM_HW_PARAM_PERIODS);
686 if (ret < 0)
687 return ret;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100688
689 prtd = kzalloc(sizeof(struct davinci_runtime_data), GFP_KERNEL);
690 if (prtd == NULL)
691 return -ENOMEM;
692
693 spin_lock_init(&prtd->lock);
Troy Kisky81ac55a2009-09-11 14:29:02 -0700694 prtd->params = params;
Troy Kisky1e224f32009-11-18 17:49:53 -0700695 prtd->asp_channel = -1;
696 prtd->asp_link[0] = prtd->asp_link[1] = -1;
697 prtd->ram_channel = -1;
698 prtd->ram_link = -1;
699 prtd->ram_link2 = -1;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100700
701 runtime->private_data = prtd;
702
703 ret = davinci_pcm_dma_request(substream);
704 if (ret) {
705 printk(KERN_ERR "davinci_pcm: Failed to get dma channels\n");
706 kfree(prtd);
707 }
708
709 return ret;
710}
711
712static int davinci_pcm_close(struct snd_pcm_substream *substream)
713{
714 struct snd_pcm_runtime *runtime = substream->runtime;
715 struct davinci_runtime_data *prtd = runtime->private_data;
716
Troy Kisky1e224f32009-11-18 17:49:53 -0700717 if (prtd->ram_channel >= 0)
718 edma_stop(prtd->ram_channel);
719 if (prtd->asp_channel >= 0)
720 edma_stop(prtd->asp_channel);
721 if (prtd->asp_link[0] >= 0)
722 edma_unlink(prtd->asp_link[0]);
723 if (prtd->asp_link[1] >= 0)
724 edma_unlink(prtd->asp_link[1]);
725 if (prtd->ram_link >= 0)
726 edma_unlink(prtd->ram_link);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100727
Troy Kisky1e224f32009-11-18 17:49:53 -0700728 if (prtd->asp_link[0] >= 0)
729 edma_free_slot(prtd->asp_link[0]);
730 if (prtd->asp_link[1] >= 0)
731 edma_free_slot(prtd->asp_link[1]);
732 if (prtd->asp_channel >= 0)
733 edma_free_channel(prtd->asp_channel);
734 if (prtd->ram_link >= 0)
735 edma_free_slot(prtd->ram_link);
736 if (prtd->ram_link2 >= 0)
737 edma_free_slot(prtd->ram_link2);
738 if (prtd->ram_channel >= 0)
739 edma_free_channel(prtd->ram_channel);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100740
741 kfree(prtd);
742
743 return 0;
744}
745
746static int davinci_pcm_hw_params(struct snd_pcm_substream *substream,
747 struct snd_pcm_hw_params *hw_params)
748{
749 return snd_pcm_lib_malloc_pages(substream,
750 params_buffer_bytes(hw_params));
751}
752
753static int davinci_pcm_hw_free(struct snd_pcm_substream *substream)
754{
755 return snd_pcm_lib_free_pages(substream);
756}
757
758static int davinci_pcm_mmap(struct snd_pcm_substream *substream,
759 struct vm_area_struct *vma)
760{
761 struct snd_pcm_runtime *runtime = substream->runtime;
762
763 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
764 runtime->dma_area,
765 runtime->dma_addr,
766 runtime->dma_bytes);
767}
768
Mark Brownb2a19d02009-01-17 19:14:26 +0000769static struct snd_pcm_ops davinci_pcm_ops = {
Vladimir Barinov310355c2008-02-18 11:40:22 +0100770 .open = davinci_pcm_open,
771 .close = davinci_pcm_close,
772 .ioctl = snd_pcm_lib_ioctl,
773 .hw_params = davinci_pcm_hw_params,
774 .hw_free = davinci_pcm_hw_free,
775 .prepare = davinci_pcm_prepare,
776 .trigger = davinci_pcm_trigger,
777 .pointer = davinci_pcm_pointer,
778 .mmap = davinci_pcm_mmap,
779};
780
Troy Kisky1e224f32009-11-18 17:49:53 -0700781static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream,
782 size_t size)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100783{
784 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
785 struct snd_dma_buffer *buf = &substream->dma_buffer;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100786
787 buf->dev.type = SNDRV_DMA_TYPE_DEV;
788 buf->dev.dev = pcm->card->dev;
789 buf->private_data = NULL;
790 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
791 &buf->addr, GFP_KERNEL);
792
Alexander Beregalov9cd28ab2008-12-13 16:25:27 +0300793 pr_debug("davinci_pcm: preallocate_dma_buffer: area=%p, addr=%p, "
794 "size=%d\n", (void *) buf->area, (void *) buf->addr, size);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100795
796 if (!buf->area)
797 return -ENOMEM;
798
799 buf->bytes = size;
800 return 0;
801}
802
803static void davinci_pcm_free(struct snd_pcm *pcm)
804{
805 struct snd_pcm_substream *substream;
806 struct snd_dma_buffer *buf;
807 int stream;
808
809 for (stream = 0; stream < 2; stream++) {
Troy Kisky1e224f32009-11-18 17:49:53 -0700810 struct snd_dma_buffer *iram_dma;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100811 substream = pcm->streams[stream].substream;
812 if (!substream)
813 continue;
814
815 buf = &substream->dma_buffer;
816 if (!buf->area)
817 continue;
818
819 dma_free_writecombine(pcm->card->dev, buf->bytes,
820 buf->area, buf->addr);
821 buf->area = NULL;
Joe Perches4726a572010-07-12 13:50:27 -0700822 iram_dma = buf->private_data;
Troy Kisky1e224f32009-11-18 17:49:53 -0700823 if (iram_dma) {
824 sram_free(iram_dma->area, iram_dma->bytes);
825 kfree(iram_dma);
826 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100827 }
828}
829
830static u64 davinci_pcm_dmamask = 0xffffffff;
831
832static int davinci_pcm_new(struct snd_card *card,
Liam Girdwood9cb132d2008-07-07 16:07:42 +0100833 struct snd_soc_dai *dai, struct snd_pcm *pcm)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100834{
835 int ret;
836
837 if (!card->dev->dma_mask)
838 card->dev->dma_mask = &davinci_pcm_dmamask;
839 if (!card->dev->coherent_dma_mask)
840 card->dev->coherent_dma_mask = 0xffffffff;
841
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000842 if (dai->driver->playback.channels_min) {
Vladimir Barinov310355c2008-02-18 11:40:22 +0100843 ret = davinci_pcm_preallocate_dma_buffer(pcm,
Troy Kisky1e224f32009-11-18 17:49:53 -0700844 SNDRV_PCM_STREAM_PLAYBACK,
845 pcm_hardware_playback.buffer_bytes_max);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100846 if (ret)
847 return ret;
848 }
849
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000850 if (dai->driver->capture.channels_min) {
Vladimir Barinov310355c2008-02-18 11:40:22 +0100851 ret = davinci_pcm_preallocate_dma_buffer(pcm,
Troy Kisky1e224f32009-11-18 17:49:53 -0700852 SNDRV_PCM_STREAM_CAPTURE,
853 pcm_hardware_capture.buffer_bytes_max);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100854 if (ret)
855 return ret;
856 }
857
858 return 0;
859}
860
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000861static struct snd_soc_platform_driver davinci_soc_platform = {
862 .ops = &davinci_pcm_ops,
Vladimir Barinov310355c2008-02-18 11:40:22 +0100863 .pcm_new = davinci_pcm_new,
864 .pcm_free = davinci_pcm_free,
865};
Vladimir Barinov310355c2008-02-18 11:40:22 +0100866
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000867static int __devinit davinci_soc_platform_probe(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000868{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000869 return snd_soc_register_platform(&pdev->dev, &davinci_soc_platform);
Mark Brown958e7922008-12-03 19:58:17 +0000870}
Mark Brown958e7922008-12-03 19:58:17 +0000871
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000872static int __devexit davinci_soc_platform_remove(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000873{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000874 snd_soc_unregister_platform(&pdev->dev);
875 return 0;
Mark Brown958e7922008-12-03 19:58:17 +0000876}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000877
878static struct platform_driver davinci_pcm_driver = {
879 .driver = {
880 .name = "davinci-pcm-audio",
881 .owner = THIS_MODULE,
882 },
883
884 .probe = davinci_soc_platform_probe,
885 .remove = __devexit_p(davinci_soc_platform_remove),
886};
887
888static int __init snd_davinci_pcm_init(void)
889{
890 return platform_driver_register(&davinci_pcm_driver);
891}
892module_init(snd_davinci_pcm_init);
893
894static void __exit snd_davinci_pcm_exit(void)
895{
896 platform_driver_unregister(&davinci_pcm_driver);
897}
898module_exit(snd_davinci_pcm_exit);
Mark Brown958e7922008-12-03 19:58:17 +0000899
Vladimir Barinov310355c2008-02-18 11:40:22 +0100900MODULE_AUTHOR("Vladimir Barinov");
901MODULE_DESCRIPTION("TI DAVINCI PCM DMA module");
902MODULE_LICENSE("GPL");