blob: 9b5a9bf9393afd2d4f5d33e6014d3e30b7dfb377 [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:
547 case SNDRV_PCM_TRIGGER_RESUME:
548 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Troy Kisky2b7b2502009-11-18 17:49:54 -0700549 edma_resume(prtd->asp_channel);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100550 break;
551 case SNDRV_PCM_TRIGGER_STOP:
552 case SNDRV_PCM_TRIGGER_SUSPEND:
553 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Troy Kisky2b7b2502009-11-18 17:49:54 -0700554 edma_pause(prtd->asp_channel);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100555 break;
556 default:
557 ret = -EINVAL;
558 break;
559 }
560
561 spin_unlock(&prtd->lock);
562
563 return ret;
564}
565
566static int davinci_pcm_prepare(struct snd_pcm_substream *substream)
567{
568 struct davinci_runtime_data *prtd = substream->runtime->private_data;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100569
Troy Kisky1e224f32009-11-18 17:49:53 -0700570 if (prtd->ram_channel >= 0) {
571 int ret = ping_pong_dma_setup(substream);
572 if (ret < 0)
573 return ret;
574
575 edma_write_slot(prtd->ram_channel, &prtd->ram_params);
576 edma_write_slot(prtd->asp_channel, &prtd->asp_params);
577
578 print_buf_info(prtd->ram_channel, "ram_channel");
579 print_buf_info(prtd->ram_link, "ram_link");
580 print_buf_info(prtd->ram_link2, "ram_link2");
581 print_buf_info(prtd->asp_channel, "asp_channel");
582 print_buf_info(prtd->asp_link[0], "asp_link[0]");
583 print_buf_info(prtd->asp_link[1], "asp_link[1]");
584
585 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
586 /* copy 1st iram buffer */
587 edma_start(prtd->ram_channel);
588 }
Troy Kisky2b7b2502009-11-18 17:49:54 -0700589 edma_start(prtd->asp_channel);
Troy Kisky1e224f32009-11-18 17:49:53 -0700590 return 0;
591 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100592 prtd->period = 0;
593 davinci_pcm_enqueue_dma(substream);
594
David Brownell82075af2009-05-14 12:41:22 -0700595 /* Copy self-linked parameter RAM entry into master channel */
Troy Kisky1e224f32009-11-18 17:49:53 -0700596 edma_read_slot(prtd->asp_link[0], &prtd->asp_params);
597 edma_write_slot(prtd->asp_channel, &prtd->asp_params);
Troy Kisky6e541472009-07-07 17:36:06 -0700598 davinci_pcm_enqueue_dma(substream);
Troy Kisky2b7b2502009-11-18 17:49:54 -0700599 edma_start(prtd->asp_channel);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100600
601 return 0;
602}
603
604static snd_pcm_uframes_t
605davinci_pcm_pointer(struct snd_pcm_substream *substream)
606{
607 struct snd_pcm_runtime *runtime = substream->runtime;
608 struct davinci_runtime_data *prtd = runtime->private_data;
609 unsigned int offset;
Troy Kisky1587ea312009-11-18 17:49:52 -0700610 int asp_count;
611 dma_addr_t asp_src, asp_dst;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100612
613 spin_lock(&prtd->lock);
Troy Kisky1e224f32009-11-18 17:49:53 -0700614 if (prtd->ram_channel >= 0) {
615 int ram_count;
616 int mod_ram;
617 dma_addr_t ram_src, ram_dst;
618 unsigned int period_size = snd_pcm_lib_period_bytes(substream);
619 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
620 /* reading ram before asp should be safe
621 * as long as the asp transfers less than a ping size
622 * of bytes between the 2 reads
623 */
624 edma_get_position(prtd->ram_channel,
625 &ram_src, &ram_dst);
626 edma_get_position(prtd->asp_channel,
627 &asp_src, &asp_dst);
628 asp_count = asp_src - prtd->asp_params.src;
629 ram_count = ram_src - prtd->ram_params.src;
630 mod_ram = ram_count % period_size;
631 mod_ram -= asp_count;
632 if (mod_ram < 0)
633 mod_ram += period_size;
634 else if (mod_ram == 0) {
635 if (snd_pcm_running(substream))
636 mod_ram += period_size;
637 }
638 ram_count -= mod_ram;
639 if (ram_count < 0)
640 ram_count += period_size * runtime->periods;
641 } else {
642 edma_get_position(prtd->ram_channel,
643 &ram_src, &ram_dst);
644 ram_count = ram_dst - prtd->ram_params.dst;
645 }
646 asp_count = ram_count;
647 } else {
648 edma_get_position(prtd->asp_channel, &asp_src, &asp_dst);
649 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
650 asp_count = asp_src - runtime->dma_addr;
651 else
652 asp_count = asp_dst - runtime->dma_addr;
653 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100654 spin_unlock(&prtd->lock);
655
Troy Kisky1587ea312009-11-18 17:49:52 -0700656 offset = bytes_to_frames(runtime, asp_count);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100657 if (offset >= runtime->buffer_size)
658 offset = 0;
659
660 return offset;
661}
662
663static int davinci_pcm_open(struct snd_pcm_substream *substream)
664{
665 struct snd_pcm_runtime *runtime = substream->runtime;
666 struct davinci_runtime_data *prtd;
Troy Kisky1e224f32009-11-18 17:49:53 -0700667 struct snd_pcm_hardware *ppcm;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100668 int ret = 0;
Troy Kisky81ac55a2009-09-11 14:29:02 -0700669 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Daniel Mack5f712b22010-03-22 10:11:15 +0100670 struct davinci_pcm_dma_params *pa;
Troy Kisky57512c62009-11-16 16:52:31 -0700671 struct davinci_pcm_dma_params *params;
Daniel Mack5f712b22010-03-22 10:11:15 +0100672
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000673 pa = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Troy Kisky57512c62009-11-16 16:52:31 -0700674 if (!pa)
Troy Kisky81ac55a2009-09-11 14:29:02 -0700675 return -ENODEV;
Troy Kisky57512c62009-11-16 16:52:31 -0700676 params = &pa[substream->stream];
Vladimir Barinov310355c2008-02-18 11:40:22 +0100677
Troy Kisky1e224f32009-11-18 17:49:53 -0700678 ppcm = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
679 &pcm_hardware_playback : &pcm_hardware_capture;
680 allocate_sram(substream, params->sram_size, ppcm);
681 snd_soc_set_runtime_hwparams(substream, ppcm);
Troy Kisky6a90d532009-08-06 16:55:32 -0700682 /* ensure that buffer size is a multiple of period size */
683 ret = snd_pcm_hw_constraint_integer(runtime,
684 SNDRV_PCM_HW_PARAM_PERIODS);
685 if (ret < 0)
686 return ret;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100687
688 prtd = kzalloc(sizeof(struct davinci_runtime_data), GFP_KERNEL);
689 if (prtd == NULL)
690 return -ENOMEM;
691
692 spin_lock_init(&prtd->lock);
Troy Kisky81ac55a2009-09-11 14:29:02 -0700693 prtd->params = params;
Troy Kisky1e224f32009-11-18 17:49:53 -0700694 prtd->asp_channel = -1;
695 prtd->asp_link[0] = prtd->asp_link[1] = -1;
696 prtd->ram_channel = -1;
697 prtd->ram_link = -1;
698 prtd->ram_link2 = -1;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100699
700 runtime->private_data = prtd;
701
702 ret = davinci_pcm_dma_request(substream);
703 if (ret) {
704 printk(KERN_ERR "davinci_pcm: Failed to get dma channels\n");
705 kfree(prtd);
706 }
707
708 return ret;
709}
710
711static int davinci_pcm_close(struct snd_pcm_substream *substream)
712{
713 struct snd_pcm_runtime *runtime = substream->runtime;
714 struct davinci_runtime_data *prtd = runtime->private_data;
715
Troy Kisky1e224f32009-11-18 17:49:53 -0700716 if (prtd->ram_channel >= 0)
717 edma_stop(prtd->ram_channel);
718 if (prtd->asp_channel >= 0)
719 edma_stop(prtd->asp_channel);
720 if (prtd->asp_link[0] >= 0)
721 edma_unlink(prtd->asp_link[0]);
722 if (prtd->asp_link[1] >= 0)
723 edma_unlink(prtd->asp_link[1]);
724 if (prtd->ram_link >= 0)
725 edma_unlink(prtd->ram_link);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100726
Troy Kisky1e224f32009-11-18 17:49:53 -0700727 if (prtd->asp_link[0] >= 0)
728 edma_free_slot(prtd->asp_link[0]);
729 if (prtd->asp_link[1] >= 0)
730 edma_free_slot(prtd->asp_link[1]);
731 if (prtd->asp_channel >= 0)
732 edma_free_channel(prtd->asp_channel);
733 if (prtd->ram_link >= 0)
734 edma_free_slot(prtd->ram_link);
735 if (prtd->ram_link2 >= 0)
736 edma_free_slot(prtd->ram_link2);
737 if (prtd->ram_channel >= 0)
738 edma_free_channel(prtd->ram_channel);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100739
740 kfree(prtd);
741
742 return 0;
743}
744
745static int davinci_pcm_hw_params(struct snd_pcm_substream *substream,
746 struct snd_pcm_hw_params *hw_params)
747{
748 return snd_pcm_lib_malloc_pages(substream,
749 params_buffer_bytes(hw_params));
750}
751
752static int davinci_pcm_hw_free(struct snd_pcm_substream *substream)
753{
754 return snd_pcm_lib_free_pages(substream);
755}
756
757static int davinci_pcm_mmap(struct snd_pcm_substream *substream,
758 struct vm_area_struct *vma)
759{
760 struct snd_pcm_runtime *runtime = substream->runtime;
761
762 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
763 runtime->dma_area,
764 runtime->dma_addr,
765 runtime->dma_bytes);
766}
767
Mark Brownb2a19d02009-01-17 19:14:26 +0000768static struct snd_pcm_ops davinci_pcm_ops = {
Vladimir Barinov310355c2008-02-18 11:40:22 +0100769 .open = davinci_pcm_open,
770 .close = davinci_pcm_close,
771 .ioctl = snd_pcm_lib_ioctl,
772 .hw_params = davinci_pcm_hw_params,
773 .hw_free = davinci_pcm_hw_free,
774 .prepare = davinci_pcm_prepare,
775 .trigger = davinci_pcm_trigger,
776 .pointer = davinci_pcm_pointer,
777 .mmap = davinci_pcm_mmap,
778};
779
Troy Kisky1e224f32009-11-18 17:49:53 -0700780static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream,
781 size_t size)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100782{
783 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
784 struct snd_dma_buffer *buf = &substream->dma_buffer;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100785
786 buf->dev.type = SNDRV_DMA_TYPE_DEV;
787 buf->dev.dev = pcm->card->dev;
788 buf->private_data = NULL;
789 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
790 &buf->addr, GFP_KERNEL);
791
Alexander Beregalov9cd28ab2008-12-13 16:25:27 +0300792 pr_debug("davinci_pcm: preallocate_dma_buffer: area=%p, addr=%p, "
793 "size=%d\n", (void *) buf->area, (void *) buf->addr, size);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100794
795 if (!buf->area)
796 return -ENOMEM;
797
798 buf->bytes = size;
799 return 0;
800}
801
802static void davinci_pcm_free(struct snd_pcm *pcm)
803{
804 struct snd_pcm_substream *substream;
805 struct snd_dma_buffer *buf;
806 int stream;
807
808 for (stream = 0; stream < 2; stream++) {
Troy Kisky1e224f32009-11-18 17:49:53 -0700809 struct snd_dma_buffer *iram_dma;
Vladimir Barinov310355c2008-02-18 11:40:22 +0100810 substream = pcm->streams[stream].substream;
811 if (!substream)
812 continue;
813
814 buf = &substream->dma_buffer;
815 if (!buf->area)
816 continue;
817
818 dma_free_writecombine(pcm->card->dev, buf->bytes,
819 buf->area, buf->addr);
820 buf->area = NULL;
Joe Perches4726a572010-07-12 13:50:27 -0700821 iram_dma = buf->private_data;
Troy Kisky1e224f32009-11-18 17:49:53 -0700822 if (iram_dma) {
823 sram_free(iram_dma->area, iram_dma->bytes);
824 kfree(iram_dma);
825 }
Vladimir Barinov310355c2008-02-18 11:40:22 +0100826 }
827}
828
829static u64 davinci_pcm_dmamask = 0xffffffff;
830
831static int davinci_pcm_new(struct snd_card *card,
Liam Girdwood9cb132d2008-07-07 16:07:42 +0100832 struct snd_soc_dai *dai, struct snd_pcm *pcm)
Vladimir Barinov310355c2008-02-18 11:40:22 +0100833{
834 int ret;
835
836 if (!card->dev->dma_mask)
837 card->dev->dma_mask = &davinci_pcm_dmamask;
838 if (!card->dev->coherent_dma_mask)
839 card->dev->coherent_dma_mask = 0xffffffff;
840
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000841 if (dai->driver->playback.channels_min) {
Vladimir Barinov310355c2008-02-18 11:40:22 +0100842 ret = davinci_pcm_preallocate_dma_buffer(pcm,
Troy Kisky1e224f32009-11-18 17:49:53 -0700843 SNDRV_PCM_STREAM_PLAYBACK,
844 pcm_hardware_playback.buffer_bytes_max);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100845 if (ret)
846 return ret;
847 }
848
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000849 if (dai->driver->capture.channels_min) {
Vladimir Barinov310355c2008-02-18 11:40:22 +0100850 ret = davinci_pcm_preallocate_dma_buffer(pcm,
Troy Kisky1e224f32009-11-18 17:49:53 -0700851 SNDRV_PCM_STREAM_CAPTURE,
852 pcm_hardware_capture.buffer_bytes_max);
Vladimir Barinov310355c2008-02-18 11:40:22 +0100853 if (ret)
854 return ret;
855 }
856
857 return 0;
858}
859
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000860static struct snd_soc_platform_driver davinci_soc_platform = {
861 .ops = &davinci_pcm_ops,
Vladimir Barinov310355c2008-02-18 11:40:22 +0100862 .pcm_new = davinci_pcm_new,
863 .pcm_free = davinci_pcm_free,
864};
Vladimir Barinov310355c2008-02-18 11:40:22 +0100865
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000866static int __devinit davinci_soc_platform_probe(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000867{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000868 return snd_soc_register_platform(&pdev->dev, &davinci_soc_platform);
Mark Brown958e7922008-12-03 19:58:17 +0000869}
Mark Brown958e7922008-12-03 19:58:17 +0000870
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000871static int __devexit davinci_soc_platform_remove(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000872{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000873 snd_soc_unregister_platform(&pdev->dev);
874 return 0;
Mark Brown958e7922008-12-03 19:58:17 +0000875}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000876
877static struct platform_driver davinci_pcm_driver = {
878 .driver = {
879 .name = "davinci-pcm-audio",
880 .owner = THIS_MODULE,
881 },
882
883 .probe = davinci_soc_platform_probe,
884 .remove = __devexit_p(davinci_soc_platform_remove),
885};
886
887static int __init snd_davinci_pcm_init(void)
888{
889 return platform_driver_register(&davinci_pcm_driver);
890}
891module_init(snd_davinci_pcm_init);
892
893static void __exit snd_davinci_pcm_exit(void)
894{
895 platform_driver_unregister(&davinci_pcm_driver);
896}
897module_exit(snd_davinci_pcm_exit);
Mark Brown958e7922008-12-03 19:58:17 +0000898
Vladimir Barinov310355c2008-02-18 11:40:22 +0100899MODULE_AUTHOR("Vladimir Barinov");
900MODULE_DESCRIPTION("TI DAVINCI PCM DMA module");
901MODULE_LICENSE("GPL");