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>
26 #include <mach/sram.h>
28 #include "davinci-pcm.h"
31 static void print_buf_info(int slot, char *name)
33 struct edmacc_param p;
36 edma_read_slot(slot, &p);
37 printk(KERN_DEBUG "%s: 0x%x, opt=%x, src=%x, a_b_cnt=%x dst=%x\n",
38 name, slot, p.opt, p.src, p.a_b_cnt, p.dst);
39 printk(KERN_DEBUG " src_dst_bidx=%x link_bcntrld=%x src_dst_cidx=%x ccnt=%x\n",
40 p.src_dst_bidx, p.link_bcntrld, p.src_dst_cidx, p.ccnt);
43 static void print_buf_info(int slot, char *name)
48 #define DAVINCI_PCM_FMTBITS (\
49 SNDRV_PCM_FMTBIT_S8 |\
50 SNDRV_PCM_FMTBIT_U8 |\
51 SNDRV_PCM_FMTBIT_S16_LE |\
52 SNDRV_PCM_FMTBIT_S16_BE |\
53 SNDRV_PCM_FMTBIT_U16_LE |\
54 SNDRV_PCM_FMTBIT_U16_BE |\
55 SNDRV_PCM_FMTBIT_S24_LE |\
56 SNDRV_PCM_FMTBIT_S24_BE |\
57 SNDRV_PCM_FMTBIT_U24_LE |\
58 SNDRV_PCM_FMTBIT_U24_BE |\
59 SNDRV_PCM_FMTBIT_S32_LE |\
60 SNDRV_PCM_FMTBIT_S32_BE |\
61 SNDRV_PCM_FMTBIT_U32_LE |\
62 SNDRV_PCM_FMTBIT_U32_BE)
64 static struct snd_pcm_hardware pcm_hardware_playback = {
65 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
66 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
67 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME|
68 SNDRV_PCM_INFO_BATCH),
69 .formats = DAVINCI_PCM_FMTBITS,
70 .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 |
79 .buffer_bytes_max = 128 * 1024,
80 .period_bytes_min = 32,
81 .period_bytes_max = 8 * 1024,
87 static 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 |
91 SNDRV_PCM_INFO_BATCH),
92 .formats = DAVINCI_PCM_FMTBITS,
93 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
94 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
95 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
96 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
102 .buffer_bytes_max = 128 * 1024,
103 .period_bytes_min = 32,
104 .period_bytes_max = 8 * 1024,
111 * How ping/pong works....
114 * ram_params - copys 2*ping_size from start of SDRAM to iram,
116 * ram_link2 - copys rest of SDRAM to iram in ping_size units,
118 * ram_link - copys entire SDRAM to iram in ping_size uints,
121 * asp_params - same as asp_link[0]
122 * asp_link[0] - copys from lower half of iram to asp port
123 * links to asp_link[1], triggers iram copy event on completion
124 * asp_link[1] - copys from upper half of iram to asp port
125 * links to asp_link[0], triggers iram copy event on completion
126 * triggers interrupt only needed to let upper SOC levels update position
127 * in stream on completion
129 * When playback is started:
134 * ram_params - same as ram_link,
136 * ram_link - same as playback
139 * asp_params - same as playback
140 * asp_link[0] - same as playback
141 * asp_link[1] - same as playback
143 * When capture is started:
146 struct davinci_runtime_data {
148 int period; /* current DMA period */
149 int asp_channel; /* Master DMA channel */
150 int asp_link[2]; /* asp parameter link channel, ping/pong */
151 struct davinci_pcm_dma_params *params; /* DMA params */
155 struct edmacc_param asp_params;
156 struct edmacc_param ram_params;
159 static void davinci_pcm_period_elapsed(struct snd_pcm_substream *substream)
161 struct davinci_runtime_data *prtd = substream->runtime->private_data;
162 struct snd_pcm_runtime *runtime = substream->runtime;
165 if (unlikely(prtd->period >= runtime->periods))
169 static void davinci_pcm_period_reset(struct snd_pcm_substream *substream)
171 struct davinci_runtime_data *prtd = substream->runtime->private_data;
176 * Not used with ping/pong
178 static void davinci_pcm_enqueue_dma(struct snd_pcm_substream *substream)
180 struct davinci_runtime_data *prtd = substream->runtime->private_data;
181 struct snd_pcm_runtime *runtime = substream->runtime;
182 unsigned int period_size;
183 unsigned int dma_offset;
186 unsigned short src_bidx, dst_bidx;
187 unsigned short src_cidx, dst_cidx;
188 unsigned int data_type;
191 unsigned int fifo_level;
193 period_size = snd_pcm_lib_period_bytes(substream);
194 dma_offset = prtd->period * period_size;
195 dma_pos = runtime->dma_addr + dma_offset;
196 fifo_level = prtd->params->fifo_level;
198 pr_debug("davinci_pcm: audio_set_dma_params_play channel = %d "
199 "dma_ptr = %x period_size=%x\n", prtd->asp_link[0], dma_pos,
202 data_type = prtd->params->data_type;
203 count = period_size / data_type;
207 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
209 dst = prtd->params->dma_addr;
210 src_bidx = data_type;
212 src_cidx = data_type * fifo_level;
215 src = prtd->params->dma_addr;
218 dst_bidx = data_type;
220 dst_cidx = data_type * fifo_level;
223 acnt = prtd->params->acnt;
224 edma_set_src(prtd->asp_link[0], src, INCR, W8BIT);
225 edma_set_dest(prtd->asp_link[0], dst, INCR, W8BIT);
227 edma_set_src_index(prtd->asp_link[0], src_bidx, src_cidx);
228 edma_set_dest_index(prtd->asp_link[0], dst_bidx, dst_cidx);
231 edma_set_transfer_params(prtd->asp_link[0], acnt, count, 1, 0,
234 edma_set_transfer_params(prtd->asp_link[0], acnt, fifo_level,
239 static void davinci_pcm_dma_irq(unsigned link, u16 ch_status, void *data)
241 struct snd_pcm_substream *substream = data;
242 struct davinci_runtime_data *prtd = substream->runtime->private_data;
244 print_buf_info(prtd->ram_channel, "i ram_channel");
245 pr_debug("davinci_pcm: link=%d, status=0x%x\n", link, ch_status);
247 if (unlikely(ch_status != DMA_COMPLETE))
250 if (snd_pcm_running(substream)) {
251 spin_lock(&prtd->lock);
252 if (prtd->ram_channel < 0) {
253 /* No ping/pong must fix up link dma data*/
254 davinci_pcm_enqueue_dma(substream);
256 davinci_pcm_period_elapsed(substream);
257 spin_unlock(&prtd->lock);
258 snd_pcm_period_elapsed(substream);
262 static int allocate_sram(struct snd_pcm_substream *substream, unsigned size,
263 struct snd_pcm_hardware *ppcm)
265 struct snd_dma_buffer *buf = &substream->dma_buffer;
266 struct snd_dma_buffer *iram_dma = NULL;
267 dma_addr_t iram_phys = 0;
268 void *iram_virt = NULL;
270 if (buf->private_data || !size)
273 ppcm->period_bytes_max = size;
274 iram_virt = sram_alloc(size, &iram_phys);
277 iram_dma = kzalloc(sizeof(*iram_dma), GFP_KERNEL);
280 iram_dma->area = iram_virt;
281 iram_dma->addr = iram_phys;
282 memset(iram_dma->area, 0, size);
283 iram_dma->bytes = size;
284 buf->private_data = iram_dma;
288 sram_free(iram_virt, size);
294 * Only used with ping/pong.
295 * This is called after runtime->dma_addr, period_bytes and data_type are valid
297 static int ping_pong_dma_setup(struct snd_pcm_substream *substream)
299 unsigned short ram_src_cidx, ram_dst_cidx;
300 struct snd_pcm_runtime *runtime = substream->runtime;
301 struct davinci_runtime_data *prtd = runtime->private_data;
302 struct snd_dma_buffer *iram_dma =
303 (struct snd_dma_buffer *)substream->dma_buffer.private_data;
304 struct davinci_pcm_dma_params *params = prtd->params;
305 unsigned int data_type = params->data_type;
306 unsigned int acnt = params->acnt;
307 /* divide by 2 for ping/pong */
308 unsigned int ping_size = snd_pcm_lib_period_bytes(substream) >> 1;
309 unsigned int fifo_level = prtd->params->fifo_level;
311 if ((data_type == 0) || (data_type > 4)) {
312 printk(KERN_ERR "%s: data_type=%i\n", __func__, data_type);
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(prtd->asp_link[1], asp_src_pong, INCR, W8BIT);
321 edma_set_src_index(prtd->asp_link[0], data_type,
322 data_type * fifo_level);
323 edma_set_src_index(prtd->asp_link[1], data_type,
324 data_type * fifo_level);
326 edma_set_src(prtd->ram_link, runtime->dma_addr, INCR, W32BIT);
328 dma_addr_t asp_dst_pong = iram_dma->addr + ping_size;
329 ram_src_cidx = -ping_size;
330 ram_dst_cidx = ping_size;
331 edma_set_dest(prtd->asp_link[1], asp_dst_pong, INCR, W8BIT);
333 edma_set_dest_index(prtd->asp_link[0], data_type,
334 data_type * fifo_level);
335 edma_set_dest_index(prtd->asp_link[1], data_type,
336 data_type * fifo_level);
338 edma_set_dest(prtd->ram_link, runtime->dma_addr, INCR, W32BIT);
342 count = ping_size / data_type;
343 edma_set_transfer_params(prtd->asp_link[0], acnt, count,
345 edma_set_transfer_params(prtd->asp_link[1], acnt, count,
348 count = ping_size / (data_type * fifo_level);
349 edma_set_transfer_params(prtd->asp_link[0], acnt, fifo_level,
350 count, fifo_level, ABSYNC);
351 edma_set_transfer_params(prtd->asp_link[1], acnt, fifo_level,
352 count, fifo_level, ABSYNC);
355 edma_set_src_index(prtd->ram_link, ping_size, ram_src_cidx);
356 edma_set_dest_index(prtd->ram_link, ping_size, ram_dst_cidx);
357 edma_set_transfer_params(prtd->ram_link, ping_size, 2,
358 runtime->periods, 2, ASYNC);
360 /* init master params */
361 edma_read_slot(prtd->asp_link[0], &prtd->asp_params);
362 edma_read_slot(prtd->ram_link, &prtd->ram_params);
363 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
364 struct edmacc_param p_ram;
365 /* Copy entire iram buffer before playback started */
366 prtd->ram_params.a_b_cnt = (1 << 16) | (ping_size << 1);
368 prtd->ram_params.src_dst_bidx = (ping_size << 1);
370 prtd->ram_params.src_dst_cidx = (ping_size << 1);
371 prtd->ram_params.ccnt = 1;
373 /* Skip 1st period */
374 edma_read_slot(prtd->ram_link, &p_ram);
375 p_ram.src += (ping_size << 1);
377 edma_write_slot(prtd->ram_link2, &p_ram);
379 * When 1st started, ram -> iram dma channel will fill the
380 * entire iram. Then, whenever a ping/pong asp buffer finishes,
381 * 1/2 iram will be filled.
383 prtd->ram_params.link_bcntrld =
384 EDMA_CHAN_SLOT(prtd->ram_link2) << 5;
389 /* 1 asp tx or rx channel using 2 parameter channels
390 * 1 ram to/from iram channel using 1 parameter channel
393 * ram copy channel kicks off first,
394 * 1st ram copy of entire iram buffer completion kicks off asp channel
395 * asp tcc always kicks off ram copy of 1/2 iram buffer
398 * asp channel starts, tcc kicks off ram copy
400 static int request_ping_pong(struct snd_pcm_substream *substream,
401 struct davinci_runtime_data *prtd,
402 struct snd_dma_buffer *iram_dma)
404 dma_addr_t asp_src_ping;
405 dma_addr_t asp_dst_ping;
407 struct davinci_pcm_dma_params *params = prtd->params;
409 /* Request ram master channel */
410 ret = prtd->ram_channel = edma_alloc_channel(EDMA_CHANNEL_ANY,
411 davinci_pcm_dma_irq, substream,
412 prtd->params->ram_chan_q);
416 /* Request ram link channel */
417 ret = prtd->ram_link = edma_alloc_slot(
418 EDMA_CTLR(prtd->ram_channel), EDMA_SLOT_ANY);
422 ret = prtd->asp_link[1] = edma_alloc_slot(
423 EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY);
427 prtd->ram_link2 = -1;
428 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
429 ret = prtd->ram_link2 = edma_alloc_slot(
430 EDMA_CTLR(prtd->ram_channel), EDMA_SLOT_ANY);
434 /* circle ping-pong buffers */
435 edma_link(prtd->asp_link[0], prtd->asp_link[1]);
436 edma_link(prtd->asp_link[1], prtd->asp_link[0]);
437 /* circle ram buffers */
438 edma_link(prtd->ram_link, prtd->ram_link);
440 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
441 asp_src_ping = iram_dma->addr;
442 asp_dst_ping = params->dma_addr; /* fifo */
444 asp_src_ping = params->dma_addr; /* fifo */
445 asp_dst_ping = iram_dma->addr;
448 edma_set_src(prtd->asp_link[0], asp_src_ping, INCR, W16BIT);
449 edma_set_dest(prtd->asp_link[0], asp_dst_ping, INCR, W16BIT);
450 edma_set_src_index(prtd->asp_link[0], 0, 0);
451 edma_set_dest_index(prtd->asp_link[0], 0, 0);
453 edma_read_slot(prtd->asp_link[0], &prtd->asp_params);
454 prtd->asp_params.opt &= ~(TCCMODE | EDMA_TCC(0x3f) | TCINTEN);
455 prtd->asp_params.opt |= TCCHEN |
456 EDMA_TCC(prtd->ram_channel & 0x3f);
457 edma_write_slot(prtd->asp_link[0], &prtd->asp_params);
460 edma_set_src(prtd->asp_link[1], asp_src_ping, INCR, W16BIT);
461 edma_set_dest(prtd->asp_link[1], asp_dst_ping, INCR, W16BIT);
462 edma_set_src_index(prtd->asp_link[1], 0, 0);
463 edma_set_dest_index(prtd->asp_link[1], 0, 0);
465 edma_read_slot(prtd->asp_link[1], &prtd->asp_params);
466 prtd->asp_params.opt &= ~(TCCMODE | EDMA_TCC(0x3f));
467 /* interrupt after every pong completion */
468 prtd->asp_params.opt |= TCINTEN | TCCHEN |
469 EDMA_TCC(prtd->ram_channel & 0x3f);
470 edma_write_slot(prtd->asp_link[1], &prtd->asp_params);
473 edma_set_src(prtd->ram_link, iram_dma->addr, INCR, W32BIT);
474 edma_set_dest(prtd->ram_link, iram_dma->addr, INCR, W32BIT);
475 pr_debug("%s: audio dma channels/slots in use for ram:%u %u %u,"
476 "for asp:%u %u %u\n", __func__,
477 prtd->ram_channel, prtd->ram_link, prtd->ram_link2,
478 prtd->asp_channel, prtd->asp_link[0],
482 edma_free_channel(prtd->asp_link[1]);
483 prtd->asp_link[1] = -1;
485 edma_free_channel(prtd->ram_link);
488 edma_free_channel(prtd->ram_channel);
489 prtd->ram_channel = -1;
494 static int davinci_pcm_dma_request(struct snd_pcm_substream *substream)
496 struct snd_dma_buffer *iram_dma;
497 struct davinci_runtime_data *prtd = substream->runtime->private_data;
498 struct davinci_pcm_dma_params *params = prtd->params;
504 /* Request asp master DMA channel */
505 ret = prtd->asp_channel = edma_alloc_channel(params->channel,
506 davinci_pcm_dma_irq, substream,
507 prtd->params->asp_chan_q);
511 /* Request asp link channels */
512 ret = prtd->asp_link[0] = edma_alloc_slot(
513 EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY);
517 iram_dma = (struct snd_dma_buffer *)substream->dma_buffer.private_data;
519 if (request_ping_pong(substream, prtd, iram_dma) == 0)
521 printk(KERN_WARNING "%s: dma channel allocation failed,"
522 "not using sram\n", __func__);
525 /* Issue transfer completion IRQ when the channel completes a
526 * transfer, then always reload from the same slot (by a kind
527 * of loopback link). The completion IRQ handler will update
528 * the reload slot with a new buffer.
530 * REVISIT save p_ram here after setting up everything except
531 * the buffer and its length (ccnt) ... use it as a template
532 * so davinci_pcm_enqueue_dma() takes less time in IRQ.
534 edma_read_slot(prtd->asp_link[0], &prtd->asp_params);
535 prtd->asp_params.opt |= TCINTEN |
536 EDMA_TCC(EDMA_CHAN_SLOT(prtd->asp_channel));
537 prtd->asp_params.link_bcntrld = EDMA_CHAN_SLOT(prtd->asp_link[0]) << 5;
538 edma_write_slot(prtd->asp_link[0], &prtd->asp_params);
541 edma_free_channel(prtd->asp_channel);
542 prtd->asp_channel = -1;
547 static int davinci_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
549 struct davinci_runtime_data *prtd = substream->runtime->private_data;
552 spin_lock(&prtd->lock);
555 case SNDRV_PCM_TRIGGER_START:
556 edma_start(prtd->asp_channel);
557 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
558 prtd->ram_channel >= 0) {
559 /* copy 1st iram buffer */
560 edma_start(prtd->ram_channel);
563 case SNDRV_PCM_TRIGGER_RESUME:
564 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
565 edma_resume(prtd->asp_channel);
567 case SNDRV_PCM_TRIGGER_STOP:
568 case SNDRV_PCM_TRIGGER_SUSPEND:
569 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
570 edma_pause(prtd->asp_channel);
577 spin_unlock(&prtd->lock);
582 static int davinci_pcm_prepare(struct snd_pcm_substream *substream)
584 struct davinci_runtime_data *prtd = substream->runtime->private_data;
586 davinci_pcm_period_reset(substream);
587 if (prtd->ram_channel >= 0) {
588 int ret = ping_pong_dma_setup(substream);
592 edma_write_slot(prtd->ram_channel, &prtd->ram_params);
593 edma_write_slot(prtd->asp_channel, &prtd->asp_params);
595 print_buf_info(prtd->ram_channel, "ram_channel");
596 print_buf_info(prtd->ram_link, "ram_link");
597 print_buf_info(prtd->ram_link2, "ram_link2");
598 print_buf_info(prtd->asp_channel, "asp_channel");
599 print_buf_info(prtd->asp_link[0], "asp_link[0]");
600 print_buf_info(prtd->asp_link[1], "asp_link[1]");
603 * There is a phase offset of 2 periods between the position
604 * used by dma setup and the position reported in the pointer
607 * The phase offset, when not using ping-pong buffers, is due to
608 * the two consecutive calls to davinci_pcm_enqueue_dma() below.
610 * Whereas here, with ping-pong buffers, the phase is due to
611 * there being an entire buffer transfer complete before the
612 * first dma completion event triggers davinci_pcm_dma_irq().
614 davinci_pcm_period_elapsed(substream);
615 davinci_pcm_period_elapsed(substream);
619 davinci_pcm_enqueue_dma(substream);
620 davinci_pcm_period_elapsed(substream);
622 /* Copy self-linked parameter RAM entry into master channel */
623 edma_read_slot(prtd->asp_link[0], &prtd->asp_params);
624 edma_write_slot(prtd->asp_channel, &prtd->asp_params);
625 davinci_pcm_enqueue_dma(substream);
626 davinci_pcm_period_elapsed(substream);
631 static snd_pcm_uframes_t
632 davinci_pcm_pointer(struct snd_pcm_substream *substream)
634 struct snd_pcm_runtime *runtime = substream->runtime;
635 struct davinci_runtime_data *prtd = runtime->private_data;
638 unsigned int period_size = snd_pcm_lib_period_bytes(substream);
641 * There is a phase offset of 2 periods between the position used by dma
642 * setup and the position reported in the pointer function. Either +2 in
643 * the dma setup or -2 here in the pointer function (with wrapping,
644 * both) accounts for this offset -- choose the latter since it makes
645 * the first-time setup clearer.
647 spin_lock(&prtd->lock);
648 asp_count = prtd->period - 2;
649 spin_unlock(&prtd->lock);
652 asp_count += runtime->periods;
653 asp_count *= period_size;
655 offset = bytes_to_frames(runtime, asp_count);
656 if (offset >= runtime->buffer_size)
662 static int davinci_pcm_open(struct snd_pcm_substream *substream)
664 struct snd_pcm_runtime *runtime = substream->runtime;
665 struct davinci_runtime_data *prtd;
666 struct snd_pcm_hardware *ppcm;
668 struct snd_soc_pcm_runtime *rtd = substream->private_data;
669 struct davinci_pcm_dma_params *pa;
670 struct davinci_pcm_dma_params *params;
672 pa = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
675 params = &pa[substream->stream];
677 ppcm = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
678 &pcm_hardware_playback : &pcm_hardware_capture;
679 allocate_sram(substream, params->sram_size, ppcm);
680 snd_soc_set_runtime_hwparams(substream, ppcm);
681 /* ensure that buffer size is a multiple of period size */
682 ret = snd_pcm_hw_constraint_integer(runtime,
683 SNDRV_PCM_HW_PARAM_PERIODS);
687 prtd = kzalloc(sizeof(struct davinci_runtime_data), GFP_KERNEL);
691 spin_lock_init(&prtd->lock);
692 prtd->params = params;
693 prtd->asp_channel = -1;
694 prtd->asp_link[0] = prtd->asp_link[1] = -1;
695 prtd->ram_channel = -1;
697 prtd->ram_link2 = -1;
699 runtime->private_data = prtd;
701 ret = davinci_pcm_dma_request(substream);
703 printk(KERN_ERR "davinci_pcm: Failed to get dma channels\n");
710 static int davinci_pcm_close(struct snd_pcm_substream *substream)
712 struct snd_pcm_runtime *runtime = substream->runtime;
713 struct davinci_runtime_data *prtd = runtime->private_data;
715 if (prtd->ram_channel >= 0)
716 edma_stop(prtd->ram_channel);
717 if (prtd->asp_channel >= 0)
718 edma_stop(prtd->asp_channel);
719 if (prtd->asp_link[0] >= 0)
720 edma_unlink(prtd->asp_link[0]);
721 if (prtd->asp_link[1] >= 0)
722 edma_unlink(prtd->asp_link[1]);
723 if (prtd->ram_link >= 0)
724 edma_unlink(prtd->ram_link);
726 if (prtd->asp_link[0] >= 0)
727 edma_free_slot(prtd->asp_link[0]);
728 if (prtd->asp_link[1] >= 0)
729 edma_free_slot(prtd->asp_link[1]);
730 if (prtd->asp_channel >= 0)
731 edma_free_channel(prtd->asp_channel);
732 if (prtd->ram_link >= 0)
733 edma_free_slot(prtd->ram_link);
734 if (prtd->ram_link2 >= 0)
735 edma_free_slot(prtd->ram_link2);
736 if (prtd->ram_channel >= 0)
737 edma_free_channel(prtd->ram_channel);
744 static int davinci_pcm_hw_params(struct snd_pcm_substream *substream,
745 struct snd_pcm_hw_params *hw_params)
747 return snd_pcm_lib_malloc_pages(substream,
748 params_buffer_bytes(hw_params));
751 static int davinci_pcm_hw_free(struct snd_pcm_substream *substream)
753 return snd_pcm_lib_free_pages(substream);
756 static int davinci_pcm_mmap(struct snd_pcm_substream *substream,
757 struct vm_area_struct *vma)
759 struct snd_pcm_runtime *runtime = substream->runtime;
761 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
767 static struct snd_pcm_ops davinci_pcm_ops = {
768 .open = davinci_pcm_open,
769 .close = davinci_pcm_close,
770 .ioctl = snd_pcm_lib_ioctl,
771 .hw_params = davinci_pcm_hw_params,
772 .hw_free = davinci_pcm_hw_free,
773 .prepare = davinci_pcm_prepare,
774 .trigger = davinci_pcm_trigger,
775 .pointer = davinci_pcm_pointer,
776 .mmap = davinci_pcm_mmap,
779 static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream,
782 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
783 struct snd_dma_buffer *buf = &substream->dma_buffer;
785 buf->dev.type = SNDRV_DMA_TYPE_DEV;
786 buf->dev.dev = pcm->card->dev;
787 buf->private_data = NULL;
788 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
789 &buf->addr, GFP_KERNEL);
791 pr_debug("davinci_pcm: preallocate_dma_buffer: area=%p, addr=%p, "
792 "size=%d\n", (void *) buf->area, (void *) buf->addr, size);
801 static void davinci_pcm_free(struct snd_pcm *pcm)
803 struct snd_pcm_substream *substream;
804 struct snd_dma_buffer *buf;
807 for (stream = 0; stream < 2; stream++) {
808 struct snd_dma_buffer *iram_dma;
809 substream = pcm->streams[stream].substream;
813 buf = &substream->dma_buffer;
817 dma_free_writecombine(pcm->card->dev, buf->bytes,
818 buf->area, buf->addr);
820 iram_dma = buf->private_data;
822 sram_free(iram_dma->area, iram_dma->bytes);
828 static u64 davinci_pcm_dmamask = DMA_BIT_MASK(32);
830 static int davinci_pcm_new(struct snd_soc_pcm_runtime *rtd)
832 struct snd_card *card = rtd->card->snd_card;
833 struct snd_pcm *pcm = rtd->pcm;
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 = DMA_BIT_MASK(32);
841 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
842 ret = davinci_pcm_preallocate_dma_buffer(pcm,
843 SNDRV_PCM_STREAM_PLAYBACK,
844 pcm_hardware_playback.buffer_bytes_max);
849 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
850 ret = davinci_pcm_preallocate_dma_buffer(pcm,
851 SNDRV_PCM_STREAM_CAPTURE,
852 pcm_hardware_capture.buffer_bytes_max);
860 static struct snd_soc_platform_driver davinci_soc_platform = {
861 .ops = &davinci_pcm_ops,
862 .pcm_new = davinci_pcm_new,
863 .pcm_free = davinci_pcm_free,
866 int davinci_soc_platform_register(struct device *dev)
868 return snd_soc_register_platform(dev, &davinci_soc_platform);
870 EXPORT_SYMBOL_GPL(davinci_soc_platform_register);
872 void davinci_soc_platform_unregister(struct device *dev)
874 snd_soc_unregister_platform(dev);
876 EXPORT_SYMBOL_GPL(davinci_soc_platform_unregister);
878 MODULE_AUTHOR("Vladimir Barinov");
879 MODULE_DESCRIPTION("TI DAVINCI PCM DMA module");
880 MODULE_LICENSE("GPL");