2 * linux/sound/oss/omap-audio-dma-intfc.c
4 * Common audio DMA handling for the OMAP processors
6 * Copyright (C) 2004 Texas Instruments, Inc.
8 * Copyright (C) 2000, 2001 Nicolas Pitre <nico@cam.org>
10 * This package is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 * 2004-06-07 Sriram Kannan - Created new file from omap_audio_dma_intfc.c. This file
21 * will contain only the DMA interface and buffer handling of OMAP
24 * 2004-06-22 Sriram Kannan - removed legacy code (auto-init). Self-linking of DMA logical channel.
26 * 2004-08-12 Nishanth Menon - Modified to integrate Audio requirements on 1610,1710 platforms
28 * 2004-11-01 Nishanth Menon - 16xx platform code base modified to support multi channel chaining.
30 * 2004-12-15 Nishanth Menon - Improved 16xx platform channel logic introduced - tasklets, queue handling updated
32 * 2005-12-10 Dirk Behme - Added L/R Channel Interchange fix as proposed by Ajaya Babu
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/types.h>
40 #include <linux/slab.h>
41 #include <linux/sched.h>
42 #include <linux/poll.h>
44 #include <linux/errno.h>
45 #include <linux/sound.h>
46 #include <linux/soundcard.h>
47 #include <linux/sysrq.h>
48 #include <linux/interrupt.h>
49 #include <linux/dma-mapping.h>
50 #include <linux/completion.h>
52 #include <asm/uaccess.h>
54 #include <asm/hardware.h>
55 #include <asm/semaphore.h>
57 #include <asm/arch/dma.h>
58 #include "omap-audio-dma-intfc.h"
60 #include <asm/arch/mcbsp.h>
62 #include "omap-audio.h"
67 #define DPRINTK(ARGS...) printk(KERN_INFO "<%s>: ",__FUNCTION__);printk(ARGS)
68 #define FN_IN printk(KERN_INFO "[%s]: start\n", __FUNCTION__)
69 #define FN_OUT(n) printk(KERN_INFO "[%s]: end(%u)\n",__FUNCTION__, n)
72 #define DPRINTK( x... )
77 #define ERR(ARGS...) printk(KERN_ERR "{%s}-ERROR: ", __FUNCTION__);printk(ARGS);
79 #define AUDIO_NAME "omap-audio"
80 #define AUDIO_NBFRAGS_DEFAULT 8
81 #define AUDIO_FRAGSIZE_DEFAULT 8192
83 #define AUDIO_ACTIVE(state) ((state)->rd_ref || (state)->wr_ref)
85 #define SPIN_ADDR (dma_addr_t)0
86 #define SPIN_SIZE 2048
88 /* Channel Queue Handling macros
89 * tail always points to the current free entry
90 * Head always points to the current entry being used
91 * end is either head or tail
94 #define AUDIO_QUEUE_INIT(s) s->dma_q_head = s->dma_q_tail = s->dma_q_count = 0;
95 #define AUDIO_QUEUE_FULL(s) (nr_linked_channels == s->dma_q_count)
96 #define AUDIO_QUEUE_LAST(s) (1 == s->dma_q_count)
97 #define AUDIO_QUEUE_EMPTY(s) (0 == s->dma_q_count)
98 #define __AUDIO_INCREMENT_QUEUE(end) ((end)=((end)+1) % nr_linked_channels)
99 #define AUDIO_INCREMENT_HEAD(s) __AUDIO_INCREMENT_QUEUE(s->dma_q_head); s->dma_q_count--;
100 #define AUDIO_INCREMENT_TAIL(s) __AUDIO_INCREMENT_QUEUE(s->dma_q_tail); s->dma_q_count++;
102 /* DMA buffer fragmentation sizes */
103 #define MAX_DMA_SIZE 0x1000000
104 #define CUT_DMA_SIZE 0x1000
105 /* TODO: To be moved to more appropriate location */
106 #define DCSR_ERROR 0x3
107 #define DCSR_SYNC_SET (1 << 6)
109 #define DCCR_FS (1 << 5)
110 #define DCCR_PRIO (1 << 6)
111 #define DCCR_EN (1 << 7)
112 #define DCCR_AI (1 << 8)
113 #define DCCR_REPEAT (1 << 9)
114 /* if 0 the channel works in 3.1 compatible mode*/
115 #define DCCR_N31COMP (1 << 10)
116 #define DCCR_EP (1 << 11)
117 #define DCCR_SRC_AMODE_BIT 12
118 #define DCCR_SRC_AMODE_MASK (0x3<<12)
119 #define DCCR_DST_AMODE_BIT 14
120 #define DCCR_DST_AMODE_MASK (0x3<<14)
121 #define AMODE_CONST 0x0
122 #define AMODE_POST_INC 0x1
123 #define AMODE_SINGLE_INDEX 0x2
124 #define AMODE_DOUBLE_INDEX 0x3
126 /**************************** DATA STRUCTURES *****************************************/
128 static spinlock_t dma_list_lock = SPIN_LOCK_UNLOCKED;
130 struct audio_isr_work_item {
136 static char work_item_running = 0;
137 static char nr_linked_channels = 1;
138 static struct audio_isr_work_item work1, work2;
141 /*********************************** MODULE SPECIFIC FUNCTIONS PROTOTYPES *************/
143 static void audio_dsr_handler(unsigned long);
144 static DECLARE_TASKLET(audio_isr_work1, audio_dsr_handler,
145 (unsigned long)&work1);
146 static DECLARE_TASKLET(audio_isr_work2, audio_dsr_handler,
147 (unsigned long)&work2);
149 static void sound_dma_irq_handler(int lch, u16 ch_status, void *data);
150 static void audio_dma_callback(int lch, u16 ch_status, void *data);
151 static int omap_start_sound_dma(audio_stream_t * s, dma_addr_t dma_ptr,
153 static int audio_set_dma_params_play(int channel, dma_addr_t dma_ptr,
155 static int audio_set_dma_params_capture(int channel, dma_addr_t dma_ptr,
157 static int audio_start_dma_chain(audio_stream_t * s);
159 /*********************************** GLOBAL FUNCTIONS DEFINTIONS ***********************/
161 /***************************************************************************************
163 * Buffer creation/destruction
165 **************************************************************************************/
166 int audio_setup_buf(audio_stream_t * s)
171 dma_addr_t dmaphys = 0;
177 s->buffers = kmalloc(sizeof(audio_buf_t) * s->nbfrags, GFP_KERNEL);
180 memset(s->buffers, 0, sizeof(audio_buf_t) * s->nbfrags);
181 for (frag = 0; frag < s->nbfrags; frag++) {
182 audio_buf_t *b = &s->buffers[frag];
184 * Let's allocate non-cached memory for DMA buffers.
185 * We try to allocate all memory at once.
186 * If this fails (a common reason is memory fragmentation),
187 * then we allocate more smaller buffers.
190 dmasize = (s->nbfrags - frag) * s->fragsize;
193 dma_alloc_coherent(NULL, dmasize, &dmaphys,
196 dmasize -= s->fragsize;
198 while (!dmabuf && dmasize);
202 memzero(dmabuf, dmasize);
205 b->dma_addr = dmaphys;
206 dmabuf += s->fragsize;
207 dmaphys += s->fragsize;
208 dmasize -= s->fragsize;
210 s->usr_head = s->dma_head = s->dma_tail = 0;
215 init_completion(&s->wfc);
216 s->wfc.done = s->nbfrags;
220 audio_discard_buf(s);
225 void audio_discard_buf(audio_stream_t * s)
228 /* ensure DMA isn't using those buffers */
232 for (frag = 0; frag < s->nbfrags; frag++) {
233 if (!s->buffers[frag].master)
235 dma_free_coherent(NULL,
236 s->buffers[frag].master,
237 s->buffers[frag].data,
238 s->buffers[frag].dma_addr);
246 /***************************************************************************************
248 * DMA channel requests
250 **************************************************************************************/
251 static void omap_sound_dma_link_lch(void *data)
253 audio_stream_t *s = (audio_stream_t *) data;
262 for (i = 0; i < nr_linked_channels; i++) {
263 int cur_chan = chan[i];
265 ((nr_linked_channels - 1 ==
266 i) ? chan[0] : chan[i + 1]);
267 omap_dma_link_lch(cur_chan, nex_chan);
274 omap_request_sound_dma(int device_id, const char *device_name, void *data,
280 if (unlikely((NULL == channels) || (NULL == device_name))) {
284 /* Try allocate memory for the num channels */
286 (int *)kmalloc(sizeof(int) * nr_linked_channels,
290 ERR("No Memory for channel allocs!\n");
294 spin_lock(&dma_list_lock);
295 for (i = 0; i < nr_linked_channels; i++) {
297 omap_request_dma(device_id, device_name,
298 sound_dma_irq_handler, data, &chan[i]);
299 /* Handle Failure condition here */
302 for (j = 0; j < i; j++) {
303 omap_free_dma(chan[j]);
305 spin_unlock(&dma_list_lock);
308 ERR("Error in requesting channel %d=0x%x\n", i, err);
314 /* Chain the channels together */
315 if (!cpu_is_omap15xx())
316 omap_sound_dma_link_lch(data);
318 spin_unlock(&dma_list_lock);
323 /***************************************************************************************
325 * DMA channel requests Freeing
327 **************************************************************************************/
328 static void omap_sound_dma_unlink_lch(void *data)
330 audio_stream_t *s = (audio_stream_t *) data;
339 for (i = 0; i < nr_linked_channels; i++) {
340 int cur_chan = chan[i];
342 ((nr_linked_channels - 1 ==
343 i) ? chan[0] : chan[i + 1]);
344 omap_dma_unlink_lch(cur_chan, nex_chan);
350 int omap_free_sound_dma(void *data, int **channels)
355 if (unlikely(NULL == channels)) {
359 if (unlikely(NULL == *channels)) {
365 if (!cpu_is_omap15xx())
366 omap_sound_dma_unlink_lch(data);
367 for (i = 0; i < nr_linked_channels; i++) {
368 int cur_chan = chan[i];
369 omap_stop_dma(cur_chan);
370 omap_free_dma(cur_chan);
378 /***************************************************************************************
380 * Process DMA requests - This will end up starting the transfer. Proper fragments of
381 * Transfers will be initiated.
383 **************************************************************************************/
384 int audio_process_dma(audio_stream_t * s)
390 /* Dont let the ISR over ride touching the in_use flag */
391 local_irq_save(flags);
392 if (1 == s->in_use) {
393 local_irq_restore(flags);
394 ERR("Called again while In Use\n");
398 local_irq_restore(flags);
403 if (s->dma_spinref > 0 && s->pending_frags) {
407 while (s->pending_frags) {
408 audio_buf_t *b = &s->buffers[s->dma_head];
409 u_int dma_size = s->fragsize - b->offset;
410 if (dma_size > MAX_DMA_SIZE)
411 dma_size = CUT_DMA_SIZE;
413 omap_start_sound_dma(s, b->dma_addr + b->offset, dma_size);
418 b->offset += dma_size;
419 if (b->offset >= s->fragsize) {
421 if (++s->dma_head >= s->nbfrags)
428 ERR("we are spinning\n");
429 while (omap_start_sound_dma(s, SPIN_ADDR, SPIN_SIZE) == 0)
432 * Note: if there is still a data buffer being
433 * processed then the ref count is negative. This
434 * allows for the DMA termination to be accounted in
435 * the proper order. Of course dma_spinref can't be
436 * greater than 0 if dma_ref is not 0 since we kill
437 * the spinning above as soon as there is real data to process.
439 if (s->buffers && s->buffers[s->dma_tail].dma_ref)
441 s->dma_spinref += spincnt;
451 /***************************************************************************************
453 * Prime Rx - Since the recieve buffer has no time limit as to when it would arrive,
454 * we need to prime it
456 **************************************************************************************/
457 void audio_prime_rx(audio_state_t * state)
459 audio_stream_t *is = state->input_stream;
462 if (state->need_tx_for_rx) {
464 * With some codecs like the Philips UDA1341 we must ensure
465 * there is an output stream at any time while recording since
466 * this is how the UDA1341 gets its clock from the SA1100.
467 * So while there is no playback data to send, the output DMA
468 * will spin with all zeroes. We use the cache flush special
471 state->output_stream->spin_idle = 1;
472 audio_process_dma(state->output_stream);
474 is->pending_frags = is->nbfrags;
475 init_completion(&is->wfc);
479 audio_process_dma(is);
485 /***************************************************************************************
487 * set the fragment size
489 **************************************************************************************/
490 int audio_set_fragments(audio_stream_t * s, int val)
496 audio_discard_buf(s);
497 s->nbfrags = (val >> 16) & 0x7FFF;
503 s->fragsize = 1 << val;
506 if (s->nbfrags * s->fragsize > 128 * 1024)
507 s->nbfrags = 128 * 1024 / s->fragsize;
509 if (audio_setup_buf(s))
511 return val | (s->nbfrags << 16);
515 /***************************************************************************************
517 * Sync up the buffers before we shutdown, else under-run errors will happen
519 **************************************************************************************/
520 int audio_sync(struct file *file)
522 audio_state_t *state = file->private_data;
523 audio_stream_t *s = state->output_stream;
528 DECLARE_WAITQUEUE(wait, current);
532 if (!(file->f_mode & FMODE_WRITE) || !s->buffers || s->mapped) {
538 * Send current buffer if it contains data. Be sure to send
539 * a full sample count.
541 b = &s->buffers[s->usr_head];
542 if (b->offset &= ~3) {
543 /* Wait for a buffer to become free */
544 if (wait_for_completion_interruptible(&s->wfc))
548 * To avoid increased complexity in the rest of the code
549 * where full fragment sizes are assumed, we cheat a little
550 * with the start pointer here and don't forget to restore
554 /* As this is a last frag we need only one dma channel
555 * to complete. So it's need to unlink dma channels
556 * to avoid empty dma work.
558 if (!cpu_is_omap15xx() && AUDIO_QUEUE_EMPTY(s))
559 omap_sound_dma_unlink_lch(s);
561 shiftval = s->fragsize - b->offset;
562 b->offset = shiftval;
563 b->dma_addr -= shiftval;
565 local_irq_save(flags);
566 s->bytecount -= shiftval;
567 if (++s->usr_head >= s->nbfrags)
571 audio_process_dma(s);
572 local_irq_restore(flags);
575 /* Let's wait for all buffers to complete */
576 set_current_state(TASK_INTERRUPTIBLE);
577 add_wait_queue(&s->wq, &wait);
578 while ((s->pending_frags || (s->wfc.done < s->nbfrags))
579 && !signal_pending(current)) {
581 set_current_state(TASK_INTERRUPTIBLE);
583 set_current_state(TASK_RUNNING);
584 remove_wait_queue(&s->wq, &wait);
586 /* undo the pointer hack above */
588 local_irq_save(flags);
589 b->dma_addr += shiftval;
591 /* ensure sane DMA code behavior if not yet processed */
593 b->offset = s->fragsize;
594 local_irq_restore(flags);
601 /***************************************************************************************
603 * Stop all the DMA channels of the stream
605 **************************************************************************************/
606 void audio_stop_dma(audio_stream_t * s)
611 if (unlikely(NULL == chan)) {
615 for (i = 0; i < nr_linked_channels; i++) {
616 int cur_chan = chan[i];
617 omap_stop_dma(cur_chan);
624 /***************************************************************************************
628 **************************************************************************************/
629 u_int audio_get_dma_pos(audio_stream_t * s)
631 audio_buf_t *b = &s->buffers[s->dma_tail];
636 offset = omap_get_dma_src_pos(s->lch[s->dma_q_head]) - b->dma_addr;
637 if (offset >= s->fragsize)
638 offset = s->fragsize - 4;
639 } else if (s->pending_frags) {
648 /***************************************************************************************
650 * Reset the audio buffers
652 **************************************************************************************/
653 void audio_reset(audio_stream_t * s)
658 s->buffers[s->dma_head].offset = 0;
659 s->buffers[s->usr_head].offset = 0;
660 s->usr_head = s->dma_head;
661 s->pending_frags = 0;
662 init_completion(&s->wfc);
663 s->wfc.done = s->nbfrags;
672 /***************************************************************************************
674 * Clear any pending transfers
676 **************************************************************************************/
677 void omap_clear_sound_dma(audio_stream_t * s)
680 omap_clear_dma(s->lch[s->dma_q_head]);
685 /***************************************************************************************
687 * DMA related functions
689 **************************************************************************************/
690 static int audio_set_dma_params_play(int channel, dma_addr_t dma_ptr,
693 int dt = 0x1; /* data type 16 */
694 int cen = 32; /* Stereo */
695 int cfn = dma_size / (2 * cen);
696 unsigned long dest_start;
702 if (cpu_is_omap15xx() || cpu_is_omap16xx()) {
703 dest_start = AUDIO_MCBSP_DATAWRITE;
704 dest_port = OMAP_DMA_PORT_MPUI;
706 if (cpu_is_omap24xx()) {
707 dest_start = AUDIO_MCBSP_DATAWRITE;
708 sync_dev = AUDIO_DMA_TX;
711 omap_set_dma_dest_params(channel, dest_port, OMAP_DMA_AMODE_CONSTANT, dest_start, 0, 0);
712 omap_set_dma_src_params(channel, 0, OMAP_DMA_AMODE_POST_INC, dma_ptr, 0, 0);
713 omap_set_dma_transfer_params(channel, dt, cen, cfn, OMAP_DMA_SYNC_ELEMENT, sync_dev, 0);
719 static int audio_set_dma_params_capture(int channel, dma_addr_t dma_ptr,
722 int dt = 0x1; /* data type 16 */
723 int cen = 16; /* mono */
724 int cfn = dma_size / (2 * cen);
725 unsigned long src_start;
732 if (cpu_is_omap15xx() || cpu_is_omap16xx()) {
733 src_start = AUDIO_MCBSP_DATAREAD;
734 src_port = OMAP_DMA_PORT_MPUI;
736 if (cpu_is_omap24xx()) {
737 src_start = AUDIO_MCBSP_DATAREAD;
738 sync_dev = AUDIO_DMA_RX;
742 omap_set_dma_src_params(channel, src_port, OMAP_DMA_AMODE_CONSTANT, src_start, 0, 0);
743 omap_set_dma_dest_params(channel, 0, OMAP_DMA_AMODE_POST_INC, dma_ptr, 0, 0);
744 omap_set_dma_transfer_params(channel, dt, cen, cfn, OMAP_DMA_SYNC_ELEMENT, sync_dev, src_sync);
750 static int audio_start_dma_chain(audio_stream_t * s)
752 int channel = s->lch[s->dma_q_head];
755 s->hw_stop(); /* stops McBSP Interface */
756 omap_start_dma(channel);
758 s->hw_start(); /* start McBSP interface */
760 /* else the dma itself will progress forward with out our help */
766 * Do the initial set of work to initialize all the channels as required.
767 * We shall then initate a transfer
769 static int omap_start_sound_dma(audio_stream_t * s, dma_addr_t dma_ptr,
775 if (unlikely(dma_size > MAX_DMA_SIZE)) {
776 ERR("DmaSoundDma: Start: overflowed %d-%d\n", dma_size,
781 if (AUDIO_QUEUE_FULL(s)) {
786 if (s->input_or_output == FMODE_WRITE)
790 audio_set_dma_params_play(s->lch[s->dma_q_tail], dma_ptr,
794 audio_set_dma_params_capture(s->lch[s->dma_q_tail], dma_ptr,
798 ret = -2; /* indicate queue full */
801 AUDIO_INCREMENT_TAIL(s);
802 ret = audio_start_dma_chain(s);
804 ERR("dma start failed");
812 /***************************************************************************************
814 * ISR related functions
816 **************************************************************************************/
817 /* The work item handler */
818 static void audio_dsr_handler(unsigned long inData)
820 void *data = (void *)inData;
821 struct audio_isr_work_item *work = data;
822 audio_stream_t *s = (work->s);
823 int sound_curr_lch = work->current_lch;
824 u16 ch_status = work->ch_status;
827 DPRINTK("lch=%d,status=0x%x, data=%p as=%p\n", sound_curr_lch,
829 if (AUDIO_QUEUE_EMPTY(s)) {
830 ERR("Interrupt(%d) for empty queue(h=%d, T=%d)???\n",
831 sound_curr_lch, s->dma_q_head, s->dma_q_tail);
832 ERR("nbfrag=%d,pendfrags=%d,USR-H=%d, QH-%d QT-%d\n",
833 s->nbfrags, s->pending_frags, s->usr_head, s->dma_head,
839 AUDIO_INCREMENT_HEAD(s); /* Empty the queue */
841 /* Try to fill again */
842 audio_dma_callback(sound_curr_lch, ch_status, s);
847 /* Macro to trace the IRQ calls - checks for multi-channel irqs */
851 static char xyz[MAX_UP] = { 0 };
855 /* ISRs have to be short and smart.. So we transfer every heavy duty stuff to the
858 static void sound_dma_irq_handler(int sound_curr_lch, u16 ch_status, void *data)
860 int dma_status = ch_status;
861 audio_stream_t *s = (audio_stream_t *) data;
864 xyz[h++] = '0' + sound_curr_lch;
865 if (h == MAX_UP - 1) {
870 DPRINTK("lch=%d,status=0x%x, dma_status=%d, data=%p\n", sound_curr_lch,
871 ch_status, dma_status, data);
873 if (dma_status & (DCSR_ERROR)) {
874 if (cpu_is_omap15xx() || cpu_is_omap16xx())
875 OMAP_DMA_CCR_REG(sound_curr_lch) &= ~DCCR_EN;
876 ERR("DCSR_ERROR!\n");
881 if (AUDIO_QUEUE_LAST(s))
884 /* Start the work item - we ping pong the work items */
885 if (!work_item_running) {
886 work1.current_lch = sound_curr_lch;
887 work1.ch_status = ch_status;
889 /* schedule tasklet 1 */
890 tasklet_schedule(&audio_isr_work1);
891 work_item_running = 1;
893 work2.current_lch = sound_curr_lch;
894 work2.ch_status = ch_status;
896 /* schedule tasklet 2 */
897 tasklet_schedule(&audio_isr_work2);
898 work_item_running = 0;
904 /* The call back that handles buffer stuff */
905 static void audio_dma_callback(int lch, u16 ch_status, void *data)
907 audio_stream_t *s = data;
908 audio_buf_t *b = &s->buffers[s->dma_tail];
911 if (s->dma_spinref > 0) {
913 } else if (!s->buffers) {
915 "omap_audio: received DMA IRQ for non existent buffers!\n");
917 } else if (b->dma_ref && --b->dma_ref == 0 && b->offset >= s->fragsize) {
918 /* This fragment is done */
920 s->bytecount += s->fragsize;
922 s->dma_spinref = -s->dma_spinref;
924 if (++s->dma_tail >= s->nbfrags)
935 audio_process_dma(s);
941 /*********************************************************************************
943 * audio_get_dma_callback(): return the dma interface call back function
945 *********************************************************************************/
946 dma_callback_t audio_get_dma_callback(void)
950 return audio_dma_callback;
953 static int __init audio_dma_init(void)
955 if (!cpu_is_omap15xx())
956 nr_linked_channels = 2;
961 static void __exit audio_dma_exit(void)
966 module_init(audio_dma_init);
967 module_exit(audio_dma_exit);
969 MODULE_AUTHOR("Texas Instruments");
970 MODULE_DESCRIPTION("Common DMA handling for Audio driver on OMAP processors");
971 MODULE_LICENSE("GPL");
973 EXPORT_SYMBOL(omap_clear_sound_dma);
974 EXPORT_SYMBOL(omap_request_sound_dma);
975 EXPORT_SYMBOL(omap_free_sound_dma);
977 EXPORT_SYMBOL(audio_get_dma_callback);
978 EXPORT_SYMBOL(audio_setup_buf);
979 EXPORT_SYMBOL(audio_process_dma);
980 EXPORT_SYMBOL(audio_prime_rx);
981 EXPORT_SYMBOL(audio_set_fragments);
982 EXPORT_SYMBOL(audio_sync);
983 EXPORT_SYMBOL(audio_stop_dma);
984 EXPORT_SYMBOL(audio_get_dma_pos);
985 EXPORT_SYMBOL(audio_reset);
986 EXPORT_SYMBOL(audio_discard_buf);