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/edma.h>
27 #include <mach/sram.h>
29 #include "davinci-pcm.h"
32 static void print_buf_info(int slot
, char *name
)
34 struct edmacc_param p
;
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
);
44 static void print_buf_info(int slot
, char *name
)
49 static struct snd_pcm_hardware pcm_hardware_playback
= {
50 .info
= (SNDRV_PCM_INFO_INTERLEAVED
| SNDRV_PCM_INFO_BLOCK_TRANSFER
|
51 SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_MMAP_VALID
|
52 SNDRV_PCM_INFO_PAUSE
| SNDRV_PCM_INFO_RESUME
),
53 .formats
= (SNDRV_PCM_FMTBIT_S16_LE
),
54 .rates
= (SNDRV_PCM_RATE_8000
| SNDRV_PCM_RATE_16000
|
55 SNDRV_PCM_RATE_22050
| SNDRV_PCM_RATE_32000
|
56 SNDRV_PCM_RATE_44100
| SNDRV_PCM_RATE_48000
|
57 SNDRV_PCM_RATE_88200
| SNDRV_PCM_RATE_96000
|
63 .buffer_bytes_max
= 128 * 1024,
64 .period_bytes_min
= 32,
65 .period_bytes_max
= 8 * 1024,
71 static struct snd_pcm_hardware pcm_hardware_capture
= {
72 .info
= (SNDRV_PCM_INFO_INTERLEAVED
| SNDRV_PCM_INFO_BLOCK_TRANSFER
|
73 SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_MMAP_VALID
|
74 SNDRV_PCM_INFO_PAUSE
),
75 .formats
= (SNDRV_PCM_FMTBIT_S16_LE
),
76 .rates
= (SNDRV_PCM_RATE_8000
| SNDRV_PCM_RATE_16000
|
77 SNDRV_PCM_RATE_22050
| SNDRV_PCM_RATE_32000
|
78 SNDRV_PCM_RATE_44100
| SNDRV_PCM_RATE_48000
|
79 SNDRV_PCM_RATE_88200
| SNDRV_PCM_RATE_96000
|
85 .buffer_bytes_max
= 128 * 1024,
86 .period_bytes_min
= 32,
87 .period_bytes_max
= 8 * 1024,
94 * How ping/pong works....
97 * ram_params - copys 2*ping_size from start of SDRAM to iram,
99 * ram_link2 - copys rest of SDRAM to iram in ping_size units,
101 * ram_link - copys entire SDRAM to iram in ping_size uints,
104 * asp_params - same as asp_link[0]
105 * asp_link[0] - copys from lower half of iram to asp port
106 * links to asp_link[1], triggers iram copy event on completion
107 * asp_link[1] - copys from upper half of iram to asp port
108 * links to asp_link[0], triggers iram copy event on completion
109 * triggers interrupt only needed to let upper SOC levels update position
110 * in stream on completion
112 * When playback is started:
117 * ram_params - same as ram_link,
119 * ram_link - same as playback
122 * asp_params - same as playback
123 * asp_link[0] - same as playback
124 * asp_link[1] - same as playback
126 * When capture is started:
129 struct davinci_runtime_data
{
131 int period
; /* current DMA period */
132 int asp_channel
; /* Master DMA channel */
133 int asp_link
[2]; /* asp parameter link channel, ping/pong */
134 struct davinci_pcm_dma_params
*params
; /* DMA params */
138 struct edmacc_param asp_params
;
139 struct edmacc_param ram_params
;
143 * Not used with ping/pong
145 static void davinci_pcm_enqueue_dma(struct snd_pcm_substream
*substream
)
147 struct davinci_runtime_data
*prtd
= substream
->runtime
->private_data
;
148 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
149 int link
= prtd
->asp_link
[0];
150 unsigned int period_size
;
151 unsigned int dma_offset
;
154 unsigned short src_bidx
, dst_bidx
;
155 unsigned short src_cidx
, dst_cidx
;
156 unsigned int data_type
;
159 unsigned int fifo_level
;
161 period_size
= snd_pcm_lib_period_bytes(substream
);
162 dma_offset
= prtd
->period
* period_size
;
163 dma_pos
= runtime
->dma_addr
+ dma_offset
;
164 fifo_level
= prtd
->params
->fifo_level
;
166 pr_debug("davinci_pcm: audio_set_dma_params_play channel = %d "
167 "dma_ptr = %x period_size=%x\n", link
, dma_pos
, period_size
);
169 data_type
= prtd
->params
->data_type
;
170 count
= period_size
/ data_type
;
174 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
176 dst
= prtd
->params
->dma_addr
;
177 src_bidx
= data_type
;
179 src_cidx
= data_type
* fifo_level
;
182 src
= prtd
->params
->dma_addr
;
185 dst_bidx
= data_type
;
187 dst_cidx
= data_type
* fifo_level
;
190 acnt
= prtd
->params
->acnt
;
191 edma_set_src(link
, src
, INCR
, W8BIT
);
192 edma_set_dest(link
, dst
, INCR
, W8BIT
);
194 edma_set_src_index(link
, src_bidx
, src_cidx
);
195 edma_set_dest_index(link
, dst_bidx
, dst_cidx
);
198 edma_set_transfer_params(link
, acnt
, count
, 1, 0, ASYNC
);
200 edma_set_transfer_params(link
, acnt
, fifo_level
, count
,
204 if (unlikely(prtd
->period
>= runtime
->periods
))
208 static void davinci_pcm_dma_irq(unsigned link
, u16 ch_status
, void *data
)
210 struct snd_pcm_substream
*substream
= data
;
211 struct davinci_runtime_data
*prtd
= substream
->runtime
->private_data
;
213 print_buf_info(prtd
->ram_channel
, "i ram_channel");
214 pr_debug("davinci_pcm: link=%d, status=0x%x\n", link
, ch_status
);
216 if (unlikely(ch_status
!= DMA_COMPLETE
))
219 if (snd_pcm_running(substream
)) {
220 if (prtd
->ram_channel
< 0) {
221 /* No ping/pong must fix up link dma data*/
222 spin_lock(&prtd
->lock
);
223 davinci_pcm_enqueue_dma(substream
);
224 spin_unlock(&prtd
->lock
);
226 snd_pcm_period_elapsed(substream
);
230 static int allocate_sram(struct snd_pcm_substream
*substream
, unsigned size
,
231 struct snd_pcm_hardware
*ppcm
)
233 struct snd_dma_buffer
*buf
= &substream
->dma_buffer
;
234 struct snd_dma_buffer
*iram_dma
= NULL
;
235 dma_addr_t iram_phys
= 0;
236 void *iram_virt
= NULL
;
238 if (buf
->private_data
|| !size
)
241 ppcm
->period_bytes_max
= size
;
242 iram_virt
= sram_alloc(size
, &iram_phys
);
245 iram_dma
= kzalloc(sizeof(*iram_dma
), GFP_KERNEL
);
248 iram_dma
->area
= iram_virt
;
249 iram_dma
->addr
= iram_phys
;
250 memset(iram_dma
->area
, 0, size
);
251 iram_dma
->bytes
= size
;
252 buf
->private_data
= iram_dma
;
256 sram_free(iram_virt
, size
);
262 * Only used with ping/pong.
263 * This is called after runtime->dma_addr, period_bytes and data_type are valid
265 static int ping_pong_dma_setup(struct snd_pcm_substream
*substream
)
267 unsigned short ram_src_cidx
, ram_dst_cidx
;
268 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
269 struct davinci_runtime_data
*prtd
= runtime
->private_data
;
270 struct snd_dma_buffer
*iram_dma
=
271 (struct snd_dma_buffer
*)substream
->dma_buffer
.private_data
;
272 struct davinci_pcm_dma_params
*params
= prtd
->params
;
273 unsigned int data_type
= params
->data_type
;
274 unsigned int acnt
= params
->acnt
;
275 /* divide by 2 for ping/pong */
276 unsigned int ping_size
= snd_pcm_lib_period_bytes(substream
) >> 1;
277 int link
= prtd
->asp_link
[1];
278 unsigned int fifo_level
= prtd
->params
->fifo_level
;
280 if ((data_type
== 0) || (data_type
> 4)) {
281 printk(KERN_ERR
"%s: data_type=%i\n", __func__
, data_type
);
284 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
285 dma_addr_t asp_src_pong
= iram_dma
->addr
+ ping_size
;
286 ram_src_cidx
= ping_size
;
287 ram_dst_cidx
= -ping_size
;
288 edma_set_src(link
, asp_src_pong
, INCR
, W8BIT
);
290 link
= prtd
->asp_link
[0];
291 edma_set_src_index(link
, data_type
, data_type
* fifo_level
);
292 link
= prtd
->asp_link
[1];
293 edma_set_src_index(link
, data_type
, data_type
* fifo_level
);
295 link
= prtd
->ram_link
;
296 edma_set_src(link
, runtime
->dma_addr
, INCR
, W32BIT
);
298 dma_addr_t asp_dst_pong
= iram_dma
->addr
+ ping_size
;
299 ram_src_cidx
= -ping_size
;
300 ram_dst_cidx
= ping_size
;
301 edma_set_dest(link
, asp_dst_pong
, INCR
, W8BIT
);
303 link
= prtd
->asp_link
[0];
304 edma_set_dest_index(link
, data_type
, data_type
* fifo_level
);
305 link
= prtd
->asp_link
[1];
306 edma_set_dest_index(link
, data_type
, data_type
* fifo_level
);
308 link
= prtd
->ram_link
;
309 edma_set_dest(link
, runtime
->dma_addr
, INCR
, W32BIT
);
313 count
= ping_size
/ data_type
;
314 edma_set_transfer_params(prtd
->asp_link
[0], acnt
, count
,
316 edma_set_transfer_params(prtd
->asp_link
[1], acnt
, count
,
319 count
= ping_size
/ (data_type
* fifo_level
);
320 edma_set_transfer_params(prtd
->asp_link
[0], acnt
, fifo_level
,
321 count
, fifo_level
, ABSYNC
);
322 edma_set_transfer_params(prtd
->asp_link
[1], acnt
, fifo_level
,
323 count
, fifo_level
, ABSYNC
);
326 link
= prtd
->ram_link
;
327 edma_set_src_index(link
, ping_size
, ram_src_cidx
);
328 edma_set_dest_index(link
, ping_size
, ram_dst_cidx
);
329 edma_set_transfer_params(link
, ping_size
, 2,
330 runtime
->periods
, 2, ASYNC
);
332 /* init master params */
333 edma_read_slot(prtd
->asp_link
[0], &prtd
->asp_params
);
334 edma_read_slot(prtd
->ram_link
, &prtd
->ram_params
);
335 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
336 struct edmacc_param p_ram
;
337 /* Copy entire iram buffer before playback started */
338 prtd
->ram_params
.a_b_cnt
= (1 << 16) | (ping_size
<< 1);
340 prtd
->ram_params
.src_dst_bidx
= (ping_size
<< 1);
342 prtd
->ram_params
.src_dst_cidx
= (ping_size
<< 1);
343 prtd
->ram_params
.ccnt
= 1;
345 /* Skip 1st period */
346 edma_read_slot(prtd
->ram_link
, &p_ram
);
347 p_ram
.src
+= (ping_size
<< 1);
349 edma_write_slot(prtd
->ram_link2
, &p_ram
);
351 * When 1st started, ram -> iram dma channel will fill the
352 * entire iram. Then, whenever a ping/pong asp buffer finishes,
353 * 1/2 iram will be filled.
355 prtd
->ram_params
.link_bcntrld
=
356 EDMA_CHAN_SLOT(prtd
->ram_link2
) << 5;
361 /* 1 asp tx or rx channel using 2 parameter channels
362 * 1 ram to/from iram channel using 1 parameter channel
365 * ram copy channel kicks off first,
366 * 1st ram copy of entire iram buffer completion kicks off asp channel
367 * asp tcc always kicks off ram copy of 1/2 iram buffer
370 * asp channel starts, tcc kicks off ram copy
372 static int request_ping_pong(struct snd_pcm_substream
*substream
,
373 struct davinci_runtime_data
*prtd
,
374 struct snd_dma_buffer
*iram_dma
)
376 dma_addr_t asp_src_ping
;
377 dma_addr_t asp_dst_ping
;
379 struct davinci_pcm_dma_params
*params
= prtd
->params
;
381 /* Request ram master channel */
382 link
= prtd
->ram_channel
= edma_alloc_channel(EDMA_CHANNEL_ANY
,
383 davinci_pcm_dma_irq
, substream
,
388 /* Request ram link channel */
389 link
= prtd
->ram_link
= edma_alloc_slot(
390 EDMA_CTLR(prtd
->ram_channel
), EDMA_SLOT_ANY
);
394 link
= prtd
->asp_link
[1] = edma_alloc_slot(
395 EDMA_CTLR(prtd
->asp_channel
), EDMA_SLOT_ANY
);
399 prtd
->ram_link2
= -1;
400 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
401 link
= prtd
->ram_link2
= edma_alloc_slot(
402 EDMA_CTLR(prtd
->ram_channel
), EDMA_SLOT_ANY
);
406 /* circle ping-pong buffers */
407 edma_link(prtd
->asp_link
[0], prtd
->asp_link
[1]);
408 edma_link(prtd
->asp_link
[1], prtd
->asp_link
[0]);
409 /* circle ram buffers */
410 edma_link(prtd
->ram_link
, prtd
->ram_link
);
412 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
413 asp_src_ping
= iram_dma
->addr
;
414 asp_dst_ping
= params
->dma_addr
; /* fifo */
416 asp_src_ping
= params
->dma_addr
; /* fifo */
417 asp_dst_ping
= iram_dma
->addr
;
420 link
= prtd
->asp_link
[0];
421 edma_set_src(link
, asp_src_ping
, INCR
, W16BIT
);
422 edma_set_dest(link
, asp_dst_ping
, INCR
, W16BIT
);
423 edma_set_src_index(link
, 0, 0);
424 edma_set_dest_index(link
, 0, 0);
426 edma_read_slot(link
, &prtd
->asp_params
);
427 prtd
->asp_params
.opt
&= ~(TCCMODE
| EDMA_TCC(0x3f) | TCINTEN
);
428 prtd
->asp_params
.opt
|= TCCHEN
| EDMA_TCC(prtd
->ram_channel
& 0x3f);
429 edma_write_slot(link
, &prtd
->asp_params
);
432 link
= prtd
->asp_link
[1];
433 edma_set_src(link
, asp_src_ping
, INCR
, W16BIT
);
434 edma_set_dest(link
, asp_dst_ping
, INCR
, W16BIT
);
435 edma_set_src_index(link
, 0, 0);
436 edma_set_dest_index(link
, 0, 0);
438 edma_read_slot(link
, &prtd
->asp_params
);
439 prtd
->asp_params
.opt
&= ~(TCCMODE
| EDMA_TCC(0x3f));
440 /* interrupt after every pong completion */
441 prtd
->asp_params
.opt
|= TCINTEN
| TCCHEN
|
442 EDMA_TCC(EDMA_CHAN_SLOT(prtd
->ram_channel
));
443 edma_write_slot(link
, &prtd
->asp_params
);
446 link
= prtd
->ram_link
;
447 edma_set_src(link
, iram_dma
->addr
, INCR
, W32BIT
);
448 edma_set_dest(link
, iram_dma
->addr
, INCR
, W32BIT
);
449 pr_debug("%s: audio dma channels/slots in use for ram:%u %u %u,"
450 "for asp:%u %u %u\n", __func__
,
451 prtd
->ram_channel
, prtd
->ram_link
, prtd
->ram_link2
,
452 prtd
->asp_channel
, prtd
->asp_link
[0],
456 edma_free_channel(prtd
->asp_link
[1]);
457 prtd
->asp_link
[1] = -1;
459 edma_free_channel(prtd
->ram_link
);
462 edma_free_channel(prtd
->ram_channel
);
463 prtd
->ram_channel
= -1;
468 static int davinci_pcm_dma_request(struct snd_pcm_substream
*substream
)
470 struct snd_dma_buffer
*iram_dma
;
471 struct davinci_runtime_data
*prtd
= substream
->runtime
->private_data
;
472 struct davinci_pcm_dma_params
*params
= prtd
->params
;
478 /* Request asp master DMA channel */
479 link
= prtd
->asp_channel
= edma_alloc_channel(params
->channel
,
480 davinci_pcm_dma_irq
, substream
, EVENTQ_0
);
484 /* Request asp link channels */
485 link
= prtd
->asp_link
[0] = edma_alloc_slot(
486 EDMA_CTLR(prtd
->asp_channel
), EDMA_SLOT_ANY
);
490 iram_dma
= (struct snd_dma_buffer
*)substream
->dma_buffer
.private_data
;
492 if (request_ping_pong(substream
, prtd
, iram_dma
) == 0)
494 printk(KERN_WARNING
"%s: dma channel allocation failed,"
495 "not using sram\n", __func__
);
498 /* Issue transfer completion IRQ when the channel completes a
499 * transfer, then always reload from the same slot (by a kind
500 * of loopback link). The completion IRQ handler will update
501 * the reload slot with a new buffer.
503 * REVISIT save p_ram here after setting up everything except
504 * the buffer and its length (ccnt) ... use it as a template
505 * so davinci_pcm_enqueue_dma() takes less time in IRQ.
507 edma_read_slot(link
, &prtd
->asp_params
);
508 prtd
->asp_params
.opt
|= TCINTEN
|
509 EDMA_TCC(EDMA_CHAN_SLOT(prtd
->asp_channel
));
510 prtd
->asp_params
.link_bcntrld
= EDMA_CHAN_SLOT(link
) << 5;
511 edma_write_slot(link
, &prtd
->asp_params
);
514 edma_free_channel(prtd
->asp_channel
);
515 prtd
->asp_channel
= -1;
520 static int davinci_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
522 struct davinci_runtime_data
*prtd
= substream
->runtime
->private_data
;
525 spin_lock(&prtd
->lock
);
528 case SNDRV_PCM_TRIGGER_START
:
529 case SNDRV_PCM_TRIGGER_RESUME
:
530 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
531 edma_resume(prtd
->asp_channel
);
533 case SNDRV_PCM_TRIGGER_STOP
:
534 case SNDRV_PCM_TRIGGER_SUSPEND
:
535 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
536 edma_pause(prtd
->asp_channel
);
543 spin_unlock(&prtd
->lock
);
548 static int davinci_pcm_prepare(struct snd_pcm_substream
*substream
)
550 struct davinci_runtime_data
*prtd
= substream
->runtime
->private_data
;
552 if (prtd
->ram_channel
>= 0) {
553 int ret
= ping_pong_dma_setup(substream
);
557 edma_write_slot(prtd
->ram_channel
, &prtd
->ram_params
);
558 edma_write_slot(prtd
->asp_channel
, &prtd
->asp_params
);
560 print_buf_info(prtd
->ram_channel
, "ram_channel");
561 print_buf_info(prtd
->ram_link
, "ram_link");
562 print_buf_info(prtd
->ram_link2
, "ram_link2");
563 print_buf_info(prtd
->asp_channel
, "asp_channel");
564 print_buf_info(prtd
->asp_link
[0], "asp_link[0]");
565 print_buf_info(prtd
->asp_link
[1], "asp_link[1]");
567 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
568 /* copy 1st iram buffer */
569 edma_start(prtd
->ram_channel
);
571 edma_start(prtd
->asp_channel
);
575 davinci_pcm_enqueue_dma(substream
);
577 /* Copy self-linked parameter RAM entry into master channel */
578 edma_read_slot(prtd
->asp_link
[0], &prtd
->asp_params
);
579 edma_write_slot(prtd
->asp_channel
, &prtd
->asp_params
);
580 davinci_pcm_enqueue_dma(substream
);
581 edma_start(prtd
->asp_channel
);
586 static snd_pcm_uframes_t
587 davinci_pcm_pointer(struct snd_pcm_substream
*substream
)
589 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
590 struct davinci_runtime_data
*prtd
= runtime
->private_data
;
593 dma_addr_t asp_src
, asp_dst
;
595 spin_lock(&prtd
->lock
);
596 if (prtd
->ram_channel
>= 0) {
599 dma_addr_t ram_src
, ram_dst
;
600 unsigned int period_size
= snd_pcm_lib_period_bytes(substream
);
601 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
602 /* reading ram before asp should be safe
603 * as long as the asp transfers less than a ping size
604 * of bytes between the 2 reads
606 edma_get_position(prtd
->ram_channel
,
608 edma_get_position(prtd
->asp_channel
,
610 asp_count
= asp_src
- prtd
->asp_params
.src
;
611 ram_count
= ram_src
- prtd
->ram_params
.src
;
612 mod_ram
= ram_count
% period_size
;
613 mod_ram
-= asp_count
;
615 mod_ram
+= period_size
;
616 else if (mod_ram
== 0) {
617 if (snd_pcm_running(substream
))
618 mod_ram
+= period_size
;
620 ram_count
-= mod_ram
;
622 ram_count
+= period_size
* runtime
->periods
;
624 edma_get_position(prtd
->ram_channel
,
626 ram_count
= ram_dst
- prtd
->ram_params
.dst
;
628 asp_count
= ram_count
;
630 edma_get_position(prtd
->asp_channel
, &asp_src
, &asp_dst
);
631 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
632 asp_count
= asp_src
- runtime
->dma_addr
;
634 asp_count
= asp_dst
- runtime
->dma_addr
;
636 spin_unlock(&prtd
->lock
);
638 offset
= bytes_to_frames(runtime
, asp_count
);
639 if (offset
>= runtime
->buffer_size
)
645 static int davinci_pcm_open(struct snd_pcm_substream
*substream
)
647 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
648 struct davinci_runtime_data
*prtd
;
649 struct snd_pcm_hardware
*ppcm
;
651 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
652 struct davinci_pcm_dma_params
*pa
;
653 struct davinci_pcm_dma_params
*params
;
655 pa
= snd_soc_dai_get_dma_data(rtd
->dai
->cpu_dai
, substream
);
658 params
= &pa
[substream
->stream
];
660 ppcm
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) ?
661 &pcm_hardware_playback
: &pcm_hardware_capture
;
662 allocate_sram(substream
, params
->sram_size
, ppcm
);
663 snd_soc_set_runtime_hwparams(substream
, ppcm
);
664 /* ensure that buffer size is a multiple of period size */
665 ret
= snd_pcm_hw_constraint_integer(runtime
,
666 SNDRV_PCM_HW_PARAM_PERIODS
);
670 prtd
= kzalloc(sizeof(struct davinci_runtime_data
), GFP_KERNEL
);
674 spin_lock_init(&prtd
->lock
);
675 prtd
->params
= params
;
676 prtd
->asp_channel
= -1;
677 prtd
->asp_link
[0] = prtd
->asp_link
[1] = -1;
678 prtd
->ram_channel
= -1;
680 prtd
->ram_link2
= -1;
682 runtime
->private_data
= prtd
;
684 ret
= davinci_pcm_dma_request(substream
);
686 printk(KERN_ERR
"davinci_pcm: Failed to get dma channels\n");
693 static int davinci_pcm_close(struct snd_pcm_substream
*substream
)
695 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
696 struct davinci_runtime_data
*prtd
= runtime
->private_data
;
698 if (prtd
->ram_channel
>= 0)
699 edma_stop(prtd
->ram_channel
);
700 if (prtd
->asp_channel
>= 0)
701 edma_stop(prtd
->asp_channel
);
702 if (prtd
->asp_link
[0] >= 0)
703 edma_unlink(prtd
->asp_link
[0]);
704 if (prtd
->asp_link
[1] >= 0)
705 edma_unlink(prtd
->asp_link
[1]);
706 if (prtd
->ram_link
>= 0)
707 edma_unlink(prtd
->ram_link
);
709 if (prtd
->asp_link
[0] >= 0)
710 edma_free_slot(prtd
->asp_link
[0]);
711 if (prtd
->asp_link
[1] >= 0)
712 edma_free_slot(prtd
->asp_link
[1]);
713 if (prtd
->asp_channel
>= 0)
714 edma_free_channel(prtd
->asp_channel
);
715 if (prtd
->ram_link
>= 0)
716 edma_free_slot(prtd
->ram_link
);
717 if (prtd
->ram_link2
>= 0)
718 edma_free_slot(prtd
->ram_link2
);
719 if (prtd
->ram_channel
>= 0)
720 edma_free_channel(prtd
->ram_channel
);
727 static int davinci_pcm_hw_params(struct snd_pcm_substream
*substream
,
728 struct snd_pcm_hw_params
*hw_params
)
730 return snd_pcm_lib_malloc_pages(substream
,
731 params_buffer_bytes(hw_params
));
734 static int davinci_pcm_hw_free(struct snd_pcm_substream
*substream
)
736 return snd_pcm_lib_free_pages(substream
);
739 static int davinci_pcm_mmap(struct snd_pcm_substream
*substream
,
740 struct vm_area_struct
*vma
)
742 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
744 return dma_mmap_writecombine(substream
->pcm
->card
->dev
, vma
,
750 static struct snd_pcm_ops davinci_pcm_ops
= {
751 .open
= davinci_pcm_open
,
752 .close
= davinci_pcm_close
,
753 .ioctl
= snd_pcm_lib_ioctl
,
754 .hw_params
= davinci_pcm_hw_params
,
755 .hw_free
= davinci_pcm_hw_free
,
756 .prepare
= davinci_pcm_prepare
,
757 .trigger
= davinci_pcm_trigger
,
758 .pointer
= davinci_pcm_pointer
,
759 .mmap
= davinci_pcm_mmap
,
762 static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm
*pcm
, int stream
,
765 struct snd_pcm_substream
*substream
= pcm
->streams
[stream
].substream
;
766 struct snd_dma_buffer
*buf
= &substream
->dma_buffer
;
768 buf
->dev
.type
= SNDRV_DMA_TYPE_DEV
;
769 buf
->dev
.dev
= pcm
->card
->dev
;
770 buf
->private_data
= NULL
;
771 buf
->area
= dma_alloc_writecombine(pcm
->card
->dev
, size
,
772 &buf
->addr
, GFP_KERNEL
);
774 pr_debug("davinci_pcm: preallocate_dma_buffer: area=%p, addr=%p, "
775 "size=%d\n", (void *) buf
->area
, (void *) buf
->addr
, size
);
784 static void davinci_pcm_free(struct snd_pcm
*pcm
)
786 struct snd_pcm_substream
*substream
;
787 struct snd_dma_buffer
*buf
;
790 for (stream
= 0; stream
< 2; stream
++) {
791 struct snd_dma_buffer
*iram_dma
;
792 substream
= pcm
->streams
[stream
].substream
;
796 buf
= &substream
->dma_buffer
;
800 dma_free_writecombine(pcm
->card
->dev
, buf
->bytes
,
801 buf
->area
, buf
->addr
);
803 iram_dma
= (struct snd_dma_buffer
*)buf
->private_data
;
805 sram_free(iram_dma
->area
, iram_dma
->bytes
);
811 static u64 davinci_pcm_dmamask
= 0xffffffff;
813 static int davinci_pcm_new(struct snd_card
*card
,
814 struct snd_soc_dai
*dai
, struct snd_pcm
*pcm
)
818 if (!card
->dev
->dma_mask
)
819 card
->dev
->dma_mask
= &davinci_pcm_dmamask
;
820 if (!card
->dev
->coherent_dma_mask
)
821 card
->dev
->coherent_dma_mask
= 0xffffffff;
823 if (dai
->playback
.channels_min
) {
824 ret
= davinci_pcm_preallocate_dma_buffer(pcm
,
825 SNDRV_PCM_STREAM_PLAYBACK
,
826 pcm_hardware_playback
.buffer_bytes_max
);
831 if (dai
->capture
.channels_min
) {
832 ret
= davinci_pcm_preallocate_dma_buffer(pcm
,
833 SNDRV_PCM_STREAM_CAPTURE
,
834 pcm_hardware_capture
.buffer_bytes_max
);
842 struct snd_soc_platform davinci_soc_platform
= {
843 .name
= "davinci-audio",
844 .pcm_ops
= &davinci_pcm_ops
,
845 .pcm_new
= davinci_pcm_new
,
846 .pcm_free
= davinci_pcm_free
,
848 EXPORT_SYMBOL_GPL(davinci_soc_platform
);
850 static int __init
davinci_soc_platform_init(void)
852 return snd_soc_register_platform(&davinci_soc_platform
);
854 module_init(davinci_soc_platform_init
);
856 static void __exit
davinci_soc_platform_exit(void)
858 snd_soc_unregister_platform(&davinci_soc_platform
);
860 module_exit(davinci_soc_platform_exit
);
862 MODULE_AUTHOR("Vladimir Barinov");
863 MODULE_DESCRIPTION("TI DAVINCI PCM DMA module");
864 MODULE_LICENSE("GPL");