Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / sound / soc / davinci / davinci-pcm.c
bloba49e667373bcf7542854696e4c2ee63fc38249ab
1 /*
2 * ALSA PCM interface for the TI DAVINCI processor
4 * Author: Vladimir Barinov, <vbarinov@embeddedalley.com>
5 * Copyright: (C) 2007 MontaVista Software, Inc., <source@mvista.com>
6 * added SRAM ping/pong (C) 2008 Troy Kisky <troy.kisky@boundarydevices.com>
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.
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>
18 #include <linux/kernel.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/soc.h>
25 #include <asm/dma.h>
26 #include <mach/edma.h>
27 #include <mach/sram.h>
29 #include "davinci-pcm.h"
31 #ifdef DEBUG
32 static void print_buf_info(int slot, char *name)
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);
43 #else
44 static void print_buf_info(int slot, char *name)
47 #endif
49 #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)
65 static struct snd_pcm_hardware pcm_hardware_playback = {
66 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
67 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
68 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME|
69 SNDRV_PCM_INFO_BATCH),
70 .formats = DAVINCI_PCM_FMTBITS,
71 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
72 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
73 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
74 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
75 SNDRV_PCM_RATE_KNOT),
76 .rate_min = 8000,
77 .rate_max = 96000,
78 .channels_min = 2,
79 .channels_max = 384,
80 .buffer_bytes_max = 128 * 1024,
81 .period_bytes_min = 32,
82 .period_bytes_max = 8 * 1024,
83 .periods_min = 16,
84 .periods_max = 255,
85 .fifo_size = 0,
88 static struct snd_pcm_hardware pcm_hardware_capture = {
89 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
90 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
91 SNDRV_PCM_INFO_PAUSE |
92 SNDRV_PCM_INFO_BATCH),
93 .formats = DAVINCI_PCM_FMTBITS,
94 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
95 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
96 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
97 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
98 SNDRV_PCM_RATE_KNOT),
99 .rate_min = 8000,
100 .rate_max = 96000,
101 .channels_min = 2,
102 .channels_max = 384,
103 .buffer_bytes_max = 128 * 1024,
104 .period_bytes_min = 32,
105 .period_bytes_max = 8 * 1024,
106 .periods_min = 16,
107 .periods_max = 255,
108 .fifo_size = 0,
112 * How ping/pong works....
114 * Playback:
115 * ram_params - copys 2*ping_size from start of SDRAM to iram,
116 * links to ram_link2
117 * ram_link2 - copys rest of SDRAM to iram in ping_size units,
118 * links to ram_link
119 * ram_link - copys entire SDRAM to iram in ping_size uints,
120 * links to self
122 * asp_params - same as asp_link[0]
123 * asp_link[0] - copys from lower half of iram to asp port
124 * links to asp_link[1], triggers iram copy event on completion
125 * asp_link[1] - copys from upper half of iram to asp port
126 * links to asp_link[0], triggers iram copy event on completion
127 * triggers interrupt only needed to let upper SOC levels update position
128 * in stream on completion
130 * When playback is started:
131 * ram_params started
132 * asp_params started
134 * Capture:
135 * ram_params - same as ram_link,
136 * links to ram_link
137 * ram_link - same as playback
138 * links to self
140 * asp_params - same as playback
141 * asp_link[0] - same as playback
142 * asp_link[1] - same as playback
144 * When capture is started:
145 * asp_params started
147 struct davinci_runtime_data {
148 spinlock_t lock;
149 int period; /* current DMA period */
150 int asp_channel; /* Master DMA channel */
151 int asp_link[2]; /* asp parameter link channel, ping/pong */
152 struct davinci_pcm_dma_params *params; /* DMA params */
153 int ram_channel;
154 int ram_link;
155 int ram_link2;
156 struct edmacc_param asp_params;
157 struct edmacc_param ram_params;
160 static void davinci_pcm_period_elapsed(struct snd_pcm_substream *substream)
162 struct davinci_runtime_data *prtd = substream->runtime->private_data;
163 struct snd_pcm_runtime *runtime = substream->runtime;
165 prtd->period++;
166 if (unlikely(prtd->period >= runtime->periods))
167 prtd->period = 0;
170 static void davinci_pcm_period_reset(struct snd_pcm_substream *substream)
172 struct davinci_runtime_data *prtd = substream->runtime->private_data;
174 prtd->period = 0;
177 * Not used with ping/pong
179 static void davinci_pcm_enqueue_dma(struct snd_pcm_substream *substream)
181 struct davinci_runtime_data *prtd = substream->runtime->private_data;
182 struct snd_pcm_runtime *runtime = substream->runtime;
183 int link = prtd->asp_link[0];
184 unsigned int period_size;
185 unsigned int dma_offset;
186 dma_addr_t dma_pos;
187 dma_addr_t src, dst;
188 unsigned short src_bidx, dst_bidx;
189 unsigned short src_cidx, dst_cidx;
190 unsigned int data_type;
191 unsigned short acnt;
192 unsigned int count;
193 unsigned int fifo_level;
195 period_size = snd_pcm_lib_period_bytes(substream);
196 dma_offset = prtd->period * period_size;
197 dma_pos = runtime->dma_addr + dma_offset;
198 fifo_level = prtd->params->fifo_level;
200 pr_debug("davinci_pcm: audio_set_dma_params_play channel = %d "
201 "dma_ptr = %x period_size=%x\n", link, dma_pos, period_size);
203 data_type = prtd->params->data_type;
204 count = period_size / data_type;
205 if (fifo_level)
206 count /= fifo_level;
208 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
209 src = dma_pos;
210 dst = prtd->params->dma_addr;
211 src_bidx = data_type;
212 dst_bidx = 0;
213 src_cidx = data_type * fifo_level;
214 dst_cidx = 0;
215 } else {
216 src = prtd->params->dma_addr;
217 dst = dma_pos;
218 src_bidx = 0;
219 dst_bidx = data_type;
220 src_cidx = 0;
221 dst_cidx = data_type * fifo_level;
224 acnt = prtd->params->acnt;
225 edma_set_src(link, src, INCR, W8BIT);
226 edma_set_dest(link, dst, INCR, W8BIT);
228 edma_set_src_index(link, src_bidx, src_cidx);
229 edma_set_dest_index(link, dst_bidx, dst_cidx);
231 if (!fifo_level)
232 edma_set_transfer_params(link, acnt, count, 1, 0, ASYNC);
233 else
234 edma_set_transfer_params(link, acnt, fifo_level, count,
235 fifo_level, ABSYNC);
238 static void davinci_pcm_dma_irq(unsigned link, u16 ch_status, void *data)
240 struct snd_pcm_substream *substream = data;
241 struct davinci_runtime_data *prtd = substream->runtime->private_data;
243 print_buf_info(prtd->ram_channel, "i ram_channel");
244 pr_debug("davinci_pcm: link=%d, status=0x%x\n", link, ch_status);
246 if (unlikely(ch_status != DMA_COMPLETE))
247 return;
249 if (snd_pcm_running(substream)) {
250 spin_lock(&prtd->lock);
251 if (prtd->ram_channel < 0) {
252 /* No ping/pong must fix up link dma data*/
253 davinci_pcm_enqueue_dma(substream);
255 davinci_pcm_period_elapsed(substream);
256 spin_unlock(&prtd->lock);
257 snd_pcm_period_elapsed(substream);
261 static int allocate_sram(struct snd_pcm_substream *substream, unsigned size,
262 struct snd_pcm_hardware *ppcm)
264 struct snd_dma_buffer *buf = &substream->dma_buffer;
265 struct snd_dma_buffer *iram_dma = NULL;
266 dma_addr_t iram_phys = 0;
267 void *iram_virt = NULL;
269 if (buf->private_data || !size)
270 return 0;
272 ppcm->period_bytes_max = size;
273 iram_virt = sram_alloc(size, &iram_phys);
274 if (!iram_virt)
275 goto exit1;
276 iram_dma = kzalloc(sizeof(*iram_dma), GFP_KERNEL);
277 if (!iram_dma)
278 goto exit2;
279 iram_dma->area = iram_virt;
280 iram_dma->addr = iram_phys;
281 memset(iram_dma->area, 0, size);
282 iram_dma->bytes = size;
283 buf->private_data = iram_dma;
284 return 0;
285 exit2:
286 if (iram_virt)
287 sram_free(iram_virt, size);
288 exit1:
289 return -ENOMEM;
293 * Only used with ping/pong.
294 * This is called after runtime->dma_addr, period_bytes and data_type are valid
296 static int ping_pong_dma_setup(struct snd_pcm_substream *substream)
298 unsigned short ram_src_cidx, ram_dst_cidx;
299 struct snd_pcm_runtime *runtime = substream->runtime;
300 struct davinci_runtime_data *prtd = runtime->private_data;
301 struct snd_dma_buffer *iram_dma =
302 (struct snd_dma_buffer *)substream->dma_buffer.private_data;
303 struct davinci_pcm_dma_params *params = prtd->params;
304 unsigned int data_type = params->data_type;
305 unsigned int acnt = params->acnt;
306 /* divide by 2 for ping/pong */
307 unsigned int ping_size = snd_pcm_lib_period_bytes(substream) >> 1;
308 int link = prtd->asp_link[1];
309 unsigned int fifo_level = prtd->params->fifo_level;
310 unsigned int count;
311 if ((data_type == 0) || (data_type > 4)) {
312 printk(KERN_ERR "%s: data_type=%i\n", __func__, data_type);
313 return -EINVAL;
315 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
316 dma_addr_t asp_src_pong = iram_dma->addr + ping_size;
317 ram_src_cidx = ping_size;
318 ram_dst_cidx = -ping_size;
319 edma_set_src(link, asp_src_pong, INCR, W8BIT);
321 link = prtd->asp_link[0];
322 edma_set_src_index(link, data_type, data_type * fifo_level);
323 link = prtd->asp_link[1];
324 edma_set_src_index(link, data_type, data_type * fifo_level);
326 link = prtd->ram_link;
327 edma_set_src(link, runtime->dma_addr, INCR, W32BIT);
328 } else {
329 dma_addr_t asp_dst_pong = iram_dma->addr + ping_size;
330 ram_src_cidx = -ping_size;
331 ram_dst_cidx = ping_size;
332 edma_set_dest(link, asp_dst_pong, INCR, W8BIT);
334 link = prtd->asp_link[0];
335 edma_set_dest_index(link, data_type, data_type * fifo_level);
336 link = prtd->asp_link[1];
337 edma_set_dest_index(link, data_type, data_type * fifo_level);
339 link = prtd->ram_link;
340 edma_set_dest(link, runtime->dma_addr, INCR, W32BIT);
343 if (!fifo_level) {
344 count = ping_size / data_type;
345 edma_set_transfer_params(prtd->asp_link[0], acnt, count,
346 1, 0, ASYNC);
347 edma_set_transfer_params(prtd->asp_link[1], acnt, count,
348 1, 0, ASYNC);
349 } else {
350 count = ping_size / (data_type * fifo_level);
351 edma_set_transfer_params(prtd->asp_link[0], acnt, fifo_level,
352 count, fifo_level, ABSYNC);
353 edma_set_transfer_params(prtd->asp_link[1], acnt, fifo_level,
354 count, fifo_level, ABSYNC);
357 link = prtd->ram_link;
358 edma_set_src_index(link, ping_size, ram_src_cidx);
359 edma_set_dest_index(link, ping_size, ram_dst_cidx);
360 edma_set_transfer_params(link, ping_size, 2,
361 runtime->periods, 2, ASYNC);
363 /* init master params */
364 edma_read_slot(prtd->asp_link[0], &prtd->asp_params);
365 edma_read_slot(prtd->ram_link, &prtd->ram_params);
366 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
367 struct edmacc_param p_ram;
368 /* Copy entire iram buffer before playback started */
369 prtd->ram_params.a_b_cnt = (1 << 16) | (ping_size << 1);
370 /* 0 dst_bidx */
371 prtd->ram_params.src_dst_bidx = (ping_size << 1);
372 /* 0 dst_cidx */
373 prtd->ram_params.src_dst_cidx = (ping_size << 1);
374 prtd->ram_params.ccnt = 1;
376 /* Skip 1st period */
377 edma_read_slot(prtd->ram_link, &p_ram);
378 p_ram.src += (ping_size << 1);
379 p_ram.ccnt -= 1;
380 edma_write_slot(prtd->ram_link2, &p_ram);
382 * When 1st started, ram -> iram dma channel will fill the
383 * entire iram. Then, whenever a ping/pong asp buffer finishes,
384 * 1/2 iram will be filled.
386 prtd->ram_params.link_bcntrld =
387 EDMA_CHAN_SLOT(prtd->ram_link2) << 5;
389 return 0;
392 /* 1 asp tx or rx channel using 2 parameter channels
393 * 1 ram to/from iram channel using 1 parameter channel
395 * Playback
396 * ram copy channel kicks off first,
397 * 1st ram copy of entire iram buffer completion kicks off asp channel
398 * asp tcc always kicks off ram copy of 1/2 iram buffer
400 * Record
401 * asp channel starts, tcc kicks off ram copy
403 static int request_ping_pong(struct snd_pcm_substream *substream,
404 struct davinci_runtime_data *prtd,
405 struct snd_dma_buffer *iram_dma)
407 dma_addr_t asp_src_ping;
408 dma_addr_t asp_dst_ping;
409 int link;
410 struct davinci_pcm_dma_params *params = prtd->params;
412 /* Request ram master channel */
413 link = prtd->ram_channel = edma_alloc_channel(EDMA_CHANNEL_ANY,
414 davinci_pcm_dma_irq, substream,
415 prtd->params->ram_chan_q);
416 if (link < 0)
417 goto exit1;
419 /* Request ram link channel */
420 link = prtd->ram_link = edma_alloc_slot(
421 EDMA_CTLR(prtd->ram_channel), EDMA_SLOT_ANY);
422 if (link < 0)
423 goto exit2;
425 link = prtd->asp_link[1] = edma_alloc_slot(
426 EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY);
427 if (link < 0)
428 goto exit3;
430 prtd->ram_link2 = -1;
431 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
432 link = prtd->ram_link2 = edma_alloc_slot(
433 EDMA_CTLR(prtd->ram_channel), EDMA_SLOT_ANY);
434 if (link < 0)
435 goto exit4;
437 /* circle ping-pong buffers */
438 edma_link(prtd->asp_link[0], prtd->asp_link[1]);
439 edma_link(prtd->asp_link[1], prtd->asp_link[0]);
440 /* circle ram buffers */
441 edma_link(prtd->ram_link, prtd->ram_link);
443 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
444 asp_src_ping = iram_dma->addr;
445 asp_dst_ping = params->dma_addr; /* fifo */
446 } else {
447 asp_src_ping = params->dma_addr; /* fifo */
448 asp_dst_ping = iram_dma->addr;
450 /* ping */
451 link = prtd->asp_link[0];
452 edma_set_src(link, asp_src_ping, INCR, W16BIT);
453 edma_set_dest(link, asp_dst_ping, INCR, W16BIT);
454 edma_set_src_index(link, 0, 0);
455 edma_set_dest_index(link, 0, 0);
457 edma_read_slot(link, &prtd->asp_params);
458 prtd->asp_params.opt &= ~(TCCMODE | EDMA_TCC(0x3f) | TCINTEN);
459 prtd->asp_params.opt |= TCCHEN |
460 EDMA_TCC(prtd->ram_channel & 0x3f);
461 edma_write_slot(link, &prtd->asp_params);
463 /* pong */
464 link = prtd->asp_link[1];
465 edma_set_src(link, asp_src_ping, INCR, W16BIT);
466 edma_set_dest(link, asp_dst_ping, INCR, W16BIT);
467 edma_set_src_index(link, 0, 0);
468 edma_set_dest_index(link, 0, 0);
470 edma_read_slot(link, &prtd->asp_params);
471 prtd->asp_params.opt &= ~(TCCMODE | EDMA_TCC(0x3f));
472 /* interrupt after every pong completion */
473 prtd->asp_params.opt |= TCINTEN | TCCHEN |
474 EDMA_TCC(prtd->ram_channel & 0x3f);
475 edma_write_slot(link, &prtd->asp_params);
477 /* ram */
478 link = prtd->ram_link;
479 edma_set_src(link, iram_dma->addr, INCR, W32BIT);
480 edma_set_dest(link, iram_dma->addr, INCR, W32BIT);
481 pr_debug("%s: audio dma channels/slots in use for ram:%u %u %u,"
482 "for asp:%u %u %u\n", __func__,
483 prtd->ram_channel, prtd->ram_link, prtd->ram_link2,
484 prtd->asp_channel, prtd->asp_link[0],
485 prtd->asp_link[1]);
486 return 0;
487 exit4:
488 edma_free_channel(prtd->asp_link[1]);
489 prtd->asp_link[1] = -1;
490 exit3:
491 edma_free_channel(prtd->ram_link);
492 prtd->ram_link = -1;
493 exit2:
494 edma_free_channel(prtd->ram_channel);
495 prtd->ram_channel = -1;
496 exit1:
497 return link;
500 static int davinci_pcm_dma_request(struct snd_pcm_substream *substream)
502 struct snd_dma_buffer *iram_dma;
503 struct davinci_runtime_data *prtd = substream->runtime->private_data;
504 struct davinci_pcm_dma_params *params = prtd->params;
505 int link;
507 if (!params)
508 return -ENODEV;
510 /* Request asp master DMA channel */
511 link = prtd->asp_channel = edma_alloc_channel(params->channel,
512 davinci_pcm_dma_irq, substream,
513 prtd->params->asp_chan_q);
514 if (link < 0)
515 goto exit1;
517 /* Request asp link channels */
518 link = prtd->asp_link[0] = edma_alloc_slot(
519 EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY);
520 if (link < 0)
521 goto exit2;
523 iram_dma = (struct snd_dma_buffer *)substream->dma_buffer.private_data;
524 if (iram_dma) {
525 if (request_ping_pong(substream, prtd, iram_dma) == 0)
526 return 0;
527 printk(KERN_WARNING "%s: dma channel allocation failed,"
528 "not using sram\n", __func__);
531 /* Issue transfer completion IRQ when the channel completes a
532 * transfer, then always reload from the same slot (by a kind
533 * of loopback link). The completion IRQ handler will update
534 * the reload slot with a new buffer.
536 * REVISIT save p_ram here after setting up everything except
537 * the buffer and its length (ccnt) ... use it as a template
538 * so davinci_pcm_enqueue_dma() takes less time in IRQ.
540 edma_read_slot(link, &prtd->asp_params);
541 prtd->asp_params.opt |= TCINTEN |
542 EDMA_TCC(EDMA_CHAN_SLOT(prtd->asp_channel));
543 prtd->asp_params.link_bcntrld = EDMA_CHAN_SLOT(link) << 5;
544 edma_write_slot(link, &prtd->asp_params);
545 return 0;
546 exit2:
547 edma_free_channel(prtd->asp_channel);
548 prtd->asp_channel = -1;
549 exit1:
550 return link;
553 static int davinci_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
555 struct davinci_runtime_data *prtd = substream->runtime->private_data;
556 int ret = 0;
558 spin_lock(&prtd->lock);
560 switch (cmd) {
561 case SNDRV_PCM_TRIGGER_START:
562 edma_start(prtd->asp_channel);
563 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
564 prtd->ram_channel >= 0) {
565 /* copy 1st iram buffer */
566 edma_start(prtd->ram_channel);
568 break;
569 case SNDRV_PCM_TRIGGER_RESUME:
570 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
571 edma_resume(prtd->asp_channel);
572 break;
573 case SNDRV_PCM_TRIGGER_STOP:
574 case SNDRV_PCM_TRIGGER_SUSPEND:
575 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
576 edma_pause(prtd->asp_channel);
577 break;
578 default:
579 ret = -EINVAL;
580 break;
583 spin_unlock(&prtd->lock);
585 return ret;
588 static int davinci_pcm_prepare(struct snd_pcm_substream *substream)
590 struct davinci_runtime_data *prtd = substream->runtime->private_data;
592 davinci_pcm_period_reset(substream);
593 if (prtd->ram_channel >= 0) {
594 int ret = ping_pong_dma_setup(substream);
595 if (ret < 0)
596 return ret;
598 edma_write_slot(prtd->ram_channel, &prtd->ram_params);
599 edma_write_slot(prtd->asp_channel, &prtd->asp_params);
601 print_buf_info(prtd->ram_channel, "ram_channel");
602 print_buf_info(prtd->ram_link, "ram_link");
603 print_buf_info(prtd->ram_link2, "ram_link2");
604 print_buf_info(prtd->asp_channel, "asp_channel");
605 print_buf_info(prtd->asp_link[0], "asp_link[0]");
606 print_buf_info(prtd->asp_link[1], "asp_link[1]");
609 * There is a phase offset of 2 periods between the position
610 * used by dma setup and the position reported in the pointer
611 * function.
613 * The phase offset, when not using ping-pong buffers, is due to
614 * the two consecutive calls to davinci_pcm_enqueue_dma() below.
616 * Whereas here, with ping-pong buffers, the phase is due to
617 * there being an entire buffer transfer complete before the
618 * first dma completion event triggers davinci_pcm_dma_irq().
620 davinci_pcm_period_elapsed(substream);
621 davinci_pcm_period_elapsed(substream);
623 return 0;
625 davinci_pcm_enqueue_dma(substream);
626 davinci_pcm_period_elapsed(substream);
628 /* Copy self-linked parameter RAM entry into master channel */
629 edma_read_slot(prtd->asp_link[0], &prtd->asp_params);
630 edma_write_slot(prtd->asp_channel, &prtd->asp_params);
631 davinci_pcm_enqueue_dma(substream);
632 davinci_pcm_period_elapsed(substream);
634 return 0;
637 static snd_pcm_uframes_t
638 davinci_pcm_pointer(struct snd_pcm_substream *substream)
640 struct snd_pcm_runtime *runtime = substream->runtime;
641 struct davinci_runtime_data *prtd = runtime->private_data;
642 unsigned int offset;
643 int asp_count;
644 unsigned int period_size = snd_pcm_lib_period_bytes(substream);
647 * There is a phase offset of 2 periods between the position used by dma
648 * setup and the position reported in the pointer function. Either +2 in
649 * the dma setup or -2 here in the pointer function (with wrapping,
650 * both) accounts for this offset -- choose the latter since it makes
651 * the first-time setup clearer.
653 spin_lock(&prtd->lock);
654 asp_count = prtd->period - 2;
655 spin_unlock(&prtd->lock);
657 if (asp_count < 0)
658 asp_count += runtime->periods;
659 asp_count *= period_size;
661 offset = bytes_to_frames(runtime, asp_count);
662 if (offset >= runtime->buffer_size)
663 offset = 0;
665 return offset;
668 static int davinci_pcm_open(struct snd_pcm_substream *substream)
670 struct snd_pcm_runtime *runtime = substream->runtime;
671 struct davinci_runtime_data *prtd;
672 struct snd_pcm_hardware *ppcm;
673 int ret = 0;
674 struct snd_soc_pcm_runtime *rtd = substream->private_data;
675 struct davinci_pcm_dma_params *pa;
676 struct davinci_pcm_dma_params *params;
678 pa = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
679 if (!pa)
680 return -ENODEV;
681 params = &pa[substream->stream];
683 ppcm = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
684 &pcm_hardware_playback : &pcm_hardware_capture;
685 allocate_sram(substream, params->sram_size, ppcm);
686 snd_soc_set_runtime_hwparams(substream, ppcm);
687 /* ensure that buffer size is a multiple of period size */
688 ret = snd_pcm_hw_constraint_integer(runtime,
689 SNDRV_PCM_HW_PARAM_PERIODS);
690 if (ret < 0)
691 return ret;
693 prtd = kzalloc(sizeof(struct davinci_runtime_data), GFP_KERNEL);
694 if (prtd == NULL)
695 return -ENOMEM;
697 spin_lock_init(&prtd->lock);
698 prtd->params = params;
699 prtd->asp_channel = -1;
700 prtd->asp_link[0] = prtd->asp_link[1] = -1;
701 prtd->ram_channel = -1;
702 prtd->ram_link = -1;
703 prtd->ram_link2 = -1;
705 runtime->private_data = prtd;
707 ret = davinci_pcm_dma_request(substream);
708 if (ret) {
709 printk(KERN_ERR "davinci_pcm: Failed to get dma channels\n");
710 kfree(prtd);
713 return ret;
716 static int davinci_pcm_close(struct snd_pcm_substream *substream)
718 struct snd_pcm_runtime *runtime = substream->runtime;
719 struct davinci_runtime_data *prtd = runtime->private_data;
721 if (prtd->ram_channel >= 0)
722 edma_stop(prtd->ram_channel);
723 if (prtd->asp_channel >= 0)
724 edma_stop(prtd->asp_channel);
725 if (prtd->asp_link[0] >= 0)
726 edma_unlink(prtd->asp_link[0]);
727 if (prtd->asp_link[1] >= 0)
728 edma_unlink(prtd->asp_link[1]);
729 if (prtd->ram_link >= 0)
730 edma_unlink(prtd->ram_link);
732 if (prtd->asp_link[0] >= 0)
733 edma_free_slot(prtd->asp_link[0]);
734 if (prtd->asp_link[1] >= 0)
735 edma_free_slot(prtd->asp_link[1]);
736 if (prtd->asp_channel >= 0)
737 edma_free_channel(prtd->asp_channel);
738 if (prtd->ram_link >= 0)
739 edma_free_slot(prtd->ram_link);
740 if (prtd->ram_link2 >= 0)
741 edma_free_slot(prtd->ram_link2);
742 if (prtd->ram_channel >= 0)
743 edma_free_channel(prtd->ram_channel);
745 kfree(prtd);
747 return 0;
750 static int davinci_pcm_hw_params(struct snd_pcm_substream *substream,
751 struct snd_pcm_hw_params *hw_params)
753 return snd_pcm_lib_malloc_pages(substream,
754 params_buffer_bytes(hw_params));
757 static int davinci_pcm_hw_free(struct snd_pcm_substream *substream)
759 return snd_pcm_lib_free_pages(substream);
762 static int davinci_pcm_mmap(struct snd_pcm_substream *substream,
763 struct vm_area_struct *vma)
765 struct snd_pcm_runtime *runtime = substream->runtime;
767 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
768 runtime->dma_area,
769 runtime->dma_addr,
770 runtime->dma_bytes);
773 static struct snd_pcm_ops davinci_pcm_ops = {
774 .open = davinci_pcm_open,
775 .close = davinci_pcm_close,
776 .ioctl = snd_pcm_lib_ioctl,
777 .hw_params = davinci_pcm_hw_params,
778 .hw_free = davinci_pcm_hw_free,
779 .prepare = davinci_pcm_prepare,
780 .trigger = davinci_pcm_trigger,
781 .pointer = davinci_pcm_pointer,
782 .mmap = davinci_pcm_mmap,
785 static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream,
786 size_t size)
788 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
789 struct snd_dma_buffer *buf = &substream->dma_buffer;
791 buf->dev.type = SNDRV_DMA_TYPE_DEV;
792 buf->dev.dev = pcm->card->dev;
793 buf->private_data = NULL;
794 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
795 &buf->addr, GFP_KERNEL);
797 pr_debug("davinci_pcm: preallocate_dma_buffer: area=%p, addr=%p, "
798 "size=%d\n", (void *) buf->area, (void *) buf->addr, size);
800 if (!buf->area)
801 return -ENOMEM;
803 buf->bytes = size;
804 return 0;
807 static void davinci_pcm_free(struct snd_pcm *pcm)
809 struct snd_pcm_substream *substream;
810 struct snd_dma_buffer *buf;
811 int stream;
813 for (stream = 0; stream < 2; stream++) {
814 struct snd_dma_buffer *iram_dma;
815 substream = pcm->streams[stream].substream;
816 if (!substream)
817 continue;
819 buf = &substream->dma_buffer;
820 if (!buf->area)
821 continue;
823 dma_free_writecombine(pcm->card->dev, buf->bytes,
824 buf->area, buf->addr);
825 buf->area = NULL;
826 iram_dma = buf->private_data;
827 if (iram_dma) {
828 sram_free(iram_dma->area, iram_dma->bytes);
829 kfree(iram_dma);
834 static u64 davinci_pcm_dmamask = 0xffffffff;
836 static int davinci_pcm_new(struct snd_soc_pcm_runtime *rtd)
838 struct snd_card *card = rtd->card->snd_card;
839 struct snd_soc_dai *dai = rtd->cpu_dai;
840 struct snd_pcm *pcm = rtd->pcm;
841 int ret;
843 if (!card->dev->dma_mask)
844 card->dev->dma_mask = &davinci_pcm_dmamask;
845 if (!card->dev->coherent_dma_mask)
846 card->dev->coherent_dma_mask = 0xffffffff;
848 if (dai->driver->playback.channels_min) {
849 ret = davinci_pcm_preallocate_dma_buffer(pcm,
850 SNDRV_PCM_STREAM_PLAYBACK,
851 pcm_hardware_playback.buffer_bytes_max);
852 if (ret)
853 return ret;
856 if (dai->driver->capture.channels_min) {
857 ret = davinci_pcm_preallocate_dma_buffer(pcm,
858 SNDRV_PCM_STREAM_CAPTURE,
859 pcm_hardware_capture.buffer_bytes_max);
860 if (ret)
861 return ret;
864 return 0;
867 static struct snd_soc_platform_driver davinci_soc_platform = {
868 .ops = &davinci_pcm_ops,
869 .pcm_new = davinci_pcm_new,
870 .pcm_free = davinci_pcm_free,
873 static int __devinit davinci_soc_platform_probe(struct platform_device *pdev)
875 return snd_soc_register_platform(&pdev->dev, &davinci_soc_platform);
878 static int __devexit davinci_soc_platform_remove(struct platform_device *pdev)
880 snd_soc_unregister_platform(&pdev->dev);
881 return 0;
884 static struct platform_driver davinci_pcm_driver = {
885 .driver = {
886 .name = "davinci-pcm-audio",
887 .owner = THIS_MODULE,
890 .probe = davinci_soc_platform_probe,
891 .remove = __devexit_p(davinci_soc_platform_remove),
894 static int __init snd_davinci_pcm_init(void)
896 return platform_driver_register(&davinci_pcm_driver);
898 module_init(snd_davinci_pcm_init);
900 static void __exit snd_davinci_pcm_exit(void)
902 platform_driver_unregister(&davinci_pcm_driver);
904 module_exit(snd_davinci_pcm_exit);
906 MODULE_AUTHOR("Vladimir Barinov");
907 MODULE_DESCRIPTION("TI DAVINCI PCM DMA module");
908 MODULE_LICENSE("GPL");