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/config.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/types.h>
41 #include <linux/slab.h>
42 #include <linux/sched.h>
43 #include <linux/poll.h>
45 #include <linux/errno.h>
46 #include <linux/sound.h>
47 #include <linux/soundcard.h>
48 #include <linux/sysrq.h>
49 #include <linux/interrupt.h>
50 #include <linux/dma-mapping.h>
51 #include <linux/completion.h>
53 #include <asm/uaccess.h>
55 #include <asm/hardware.h>
56 #include <asm/semaphore.h>
58 #include <asm/arch/dma.h>
59 #include "omap-audio-dma-intfc.h"
61 #include <asm/arch/mcbsp.h>
63 #include "omap-audio.h"
68 #define DPRINTK(ARGS...) printk(KERN_INFO "<%s>: ",__FUNCTION__);printk(ARGS)
69 #define FN_IN printk(KERN_INFO "[%s]: start\n", __FUNCTION__)
70 #define FN_OUT(n) printk(KERN_INFO "[%s]: end(%u)\n",__FUNCTION__, n)
73 #define DPRINTK( x... )
78 #define ERR(ARGS...) printk(KERN_ERR "{%s}-ERROR: ", __FUNCTION__);printk(ARGS);
80 #define AUDIO_NAME "omap-audio"
81 #define AUDIO_NBFRAGS_DEFAULT 8
82 #define AUDIO_FRAGSIZE_DEFAULT 8192
84 #define AUDIO_ACTIVE(state) ((state)->rd_ref || (state)->wr_ref)
86 #define SPIN_ADDR (dma_addr_t)0
87 #define SPIN_SIZE 2048
89 /* Channel Queue Handling macros
90 * tail always points to the current free entry
91 * Head always points to the current entry being used
92 * end is either head or tail
95 #define AUDIO_QUEUE_INIT(s) s->dma_q_head = s->dma_q_tail = s->dma_q_count = 0;
96 #define AUDIO_QUEUE_FULL(s) (nr_linked_channels == s->dma_q_count)
97 #define AUDIO_QUEUE_LAST(s) (1 == s->dma_q_count)
98 #define AUDIO_QUEUE_EMPTY(s) (0 == s->dma_q_count)
99 #define __AUDIO_INCREMENT_QUEUE(end) ((end)=((end)+1) % nr_linked_channels)
100 #define AUDIO_INCREMENT_HEAD(s) __AUDIO_INCREMENT_QUEUE(s->dma_q_head); s->dma_q_count--;
101 #define AUDIO_INCREMENT_TAIL(s) __AUDIO_INCREMENT_QUEUE(s->dma_q_tail); s->dma_q_count++;
103 /* DMA buffer fragmentation sizes */
104 #define MAX_DMA_SIZE 0x1000000
105 #define CUT_DMA_SIZE 0x1000
106 /* TODO: To be moved to more appropriate location */
107 #define DCSR_ERROR 0x3
108 #define DCSR_SYNC_SET (1 << 6)
110 #define DCCR_FS (1 << 5)
111 #define DCCR_PRIO (1 << 6)
112 #define DCCR_EN (1 << 7)
113 #define DCCR_AI (1 << 8)
114 #define DCCR_REPEAT (1 << 9)
115 /* if 0 the channel works in 3.1 compatible mode*/
116 #define DCCR_N31COMP (1 << 10)
117 #define DCCR_EP (1 << 11)
118 #define DCCR_SRC_AMODE_BIT 12
119 #define DCCR_SRC_AMODE_MASK (0x3<<12)
120 #define DCCR_DST_AMODE_BIT 14
121 #define DCCR_DST_AMODE_MASK (0x3<<14)
122 #define AMODE_CONST 0x0
123 #define AMODE_POST_INC 0x1
124 #define AMODE_SINGLE_INDEX 0x2
125 #define AMODE_DOUBLE_INDEX 0x3
127 /**************************** DATA STRUCTURES *****************************************/
129 static spinlock_t dma_list_lock = SPIN_LOCK_UNLOCKED;
131 struct audio_isr_work_item {
137 static char work_item_running = 0;
138 static char nr_linked_channels = 1;
139 static struct audio_isr_work_item work1, work2;
142 /*********************************** MODULE SPECIFIC FUNCTIONS PROTOTYPES *************/
144 static void audio_dsr_handler(unsigned long);
145 static DECLARE_TASKLET(audio_isr_work1, audio_dsr_handler,
146 (unsigned long)&work1);
147 static DECLARE_TASKLET(audio_isr_work2, audio_dsr_handler,
148 (unsigned long)&work2);
150 static void sound_dma_irq_handler(int lch, u16 ch_status, void *data);
151 static void audio_dma_callback(int lch, u16 ch_status, void *data);
152 static int omap_start_sound_dma(audio_stream_t * s, dma_addr_t dma_ptr,
154 static int audio_set_dma_params_play(int channel, dma_addr_t dma_ptr,
156 static int audio_set_dma_params_capture(int channel, dma_addr_t dma_ptr,
158 static int audio_start_dma_chain(audio_stream_t * s);
160 /*********************************** GLOBAL FUNCTIONS DEFINTIONS ***********************/
162 /***************************************************************************************
164 * Buffer creation/destruction
166 **************************************************************************************/
167 int audio_setup_buf(audio_stream_t * s)
172 dma_addr_t dmaphys = 0;
178 s->buffers = kmalloc(sizeof(audio_buf_t) * s->nbfrags, GFP_KERNEL);
181 memset(s->buffers, 0, sizeof(audio_buf_t) * s->nbfrags);
182 for (frag = 0; frag < s->nbfrags; frag++) {
183 audio_buf_t *b = &s->buffers[frag];
185 * Let's allocate non-cached memory for DMA buffers.
186 * We try to allocate all memory at once.
187 * If this fails (a common reason is memory fragmentation),
188 * then we allocate more smaller buffers.
191 dmasize = (s->nbfrags - frag) * s->fragsize;
194 dma_alloc_coherent(NULL, dmasize, &dmaphys,
197 dmasize -= s->fragsize;
199 while (!dmabuf && dmasize);
203 memzero(dmabuf, dmasize);
206 b->dma_addr = dmaphys;
207 dmabuf += s->fragsize;
208 dmaphys += s->fragsize;
209 dmasize -= s->fragsize;
211 s->usr_head = s->dma_head = s->dma_tail = 0;
216 init_completion(&s->wfc);
217 s->wfc.done = s->nbfrags;
221 audio_discard_buf(s);
226 void audio_discard_buf(audio_stream_t * s)
229 /* ensure DMA isn't using those buffers */
233 for (frag = 0; frag < s->nbfrags; frag++) {
234 if (!s->buffers[frag].master)
236 dma_free_coherent(NULL,
237 s->buffers[frag].master,
238 s->buffers[frag].data,
239 s->buffers[frag].dma_addr);
247 /***************************************************************************************
249 * DMA channel requests
251 **************************************************************************************/
252 static void omap_sound_dma_link_lch(void *data)
254 audio_stream_t *s = (audio_stream_t *) data;
263 for (i = 0; i < nr_linked_channels; i++) {
264 int cur_chan = chan[i];
266 ((nr_linked_channels - 1 ==
267 i) ? chan[0] : chan[i + 1]);
268 omap_dma_link_lch(cur_chan, nex_chan);
275 omap_request_sound_dma(int device_id, const char *device_name, void *data,
281 if (unlikely((NULL == channels) || (NULL == device_name))) {
285 /* Try allocate memory for the num channels */
287 (int *)kmalloc(sizeof(int) * nr_linked_channels,
291 ERR("No Memory for channel allocs!\n");
295 spin_lock(&dma_list_lock);
296 for (i = 0; i < nr_linked_channels; i++) {
298 omap_request_dma(device_id, device_name,
299 sound_dma_irq_handler, data, &chan[i]);
300 /* Handle Failure condition here */
303 for (j = 0; j < i; j++) {
304 omap_free_dma(chan[j]);
306 spin_unlock(&dma_list_lock);
309 ERR("Error in requesting channel %d=0x%x\n", i, err);
315 /* Chain the channels together */
316 if (!cpu_is_omap1510())
317 omap_sound_dma_link_lch(data);
319 spin_unlock(&dma_list_lock);
324 /***************************************************************************************
326 * DMA channel requests Freeing
328 **************************************************************************************/
329 static void omap_sound_dma_unlink_lch(void *data)
331 audio_stream_t *s = (audio_stream_t *) data;
340 for (i = 0; i < nr_linked_channels; i++) {
341 int cur_chan = chan[i];
343 ((nr_linked_channels - 1 ==
344 i) ? chan[0] : chan[i + 1]);
345 omap_dma_unlink_lch(cur_chan, nex_chan);
351 int omap_free_sound_dma(void *data, int **channels)
356 if (unlikely(NULL == channels)) {
360 if (unlikely(NULL == *channels)) {
366 if (!cpu_is_omap1510())
367 omap_sound_dma_unlink_lch(data);
368 for (i = 0; i < nr_linked_channels; i++) {
369 int cur_chan = chan[i];
370 omap_stop_dma(cur_chan);
371 omap_free_dma(cur_chan);
379 /***************************************************************************************
381 * Process DMA requests - This will end up starting the transfer. Proper fragments of
382 * Transfers will be initiated.
384 **************************************************************************************/
385 int audio_process_dma(audio_stream_t * s)
391 /* Dont let the ISR over ride touching the in_use flag */
392 local_irq_save(flags);
393 if (1 == s->in_use) {
394 local_irq_restore(flags);
395 ERR("Called again while In Use\n");
399 local_irq_restore(flags);
404 if (s->dma_spinref > 0 && s->pending_frags) {
408 while (s->pending_frags) {
409 audio_buf_t *b = &s->buffers[s->dma_head];
410 u_int dma_size = s->fragsize - b->offset;
411 if (dma_size > MAX_DMA_SIZE)
412 dma_size = CUT_DMA_SIZE;
414 omap_start_sound_dma(s, b->dma_addr + b->offset, dma_size);
419 b->offset += dma_size;
420 if (b->offset >= s->fragsize) {
422 if (++s->dma_head >= s->nbfrags)
429 ERR("we are spinning\n");
430 while (omap_start_sound_dma(s, SPIN_ADDR, SPIN_SIZE) == 0)
433 * Note: if there is still a data buffer being
434 * processed then the ref count is negative. This
435 * allows for the DMA termination to be accounted in
436 * the proper order. Of course dma_spinref can't be
437 * greater than 0 if dma_ref is not 0 since we kill
438 * the spinning above as soon as there is real data to process.
440 if (s->buffers && s->buffers[s->dma_tail].dma_ref)
442 s->dma_spinref += spincnt;
452 /***************************************************************************************
454 * Prime Rx - Since the recieve buffer has no time limit as to when it would arrive,
455 * we need to prime it
457 **************************************************************************************/
458 void audio_prime_rx(audio_state_t * state)
460 audio_stream_t *is = state->input_stream;
463 if (state->need_tx_for_rx) {
465 * With some codecs like the Philips UDA1341 we must ensure
466 * there is an output stream at any time while recording since
467 * this is how the UDA1341 gets its clock from the SA1100.
468 * So while there is no playback data to send, the output DMA
469 * will spin with all zeroes. We use the cache flush special
472 state->output_stream->spin_idle = 1;
473 audio_process_dma(state->output_stream);
475 is->pending_frags = is->nbfrags;
476 init_completion(&is->wfc);
480 audio_process_dma(is);
486 /***************************************************************************************
488 * set the fragment size
490 **************************************************************************************/
491 int audio_set_fragments(audio_stream_t * s, int val)
497 audio_discard_buf(s);
498 s->nbfrags = (val >> 16) & 0x7FFF;
504 s->fragsize = 1 << val;
507 if (s->nbfrags * s->fragsize > 128 * 1024)
508 s->nbfrags = 128 * 1024 / s->fragsize;
510 if (audio_setup_buf(s))
512 return val | (s->nbfrags << 16);
516 /***************************************************************************************
518 * Sync up the buffers before we shutdown, else under-run errors will happen
520 **************************************************************************************/
521 int audio_sync(struct file *file)
523 audio_state_t *state = file->private_data;
524 audio_stream_t *s = state->output_stream;
529 DECLARE_WAITQUEUE(wait, current);
533 if (!(file->f_mode & FMODE_WRITE) || !s->buffers || s->mapped) {
539 * Send current buffer if it contains data. Be sure to send
540 * a full sample count.
542 b = &s->buffers[s->usr_head];
543 if (b->offset &= ~3) {
544 /* Wait for a buffer to become free */
545 if (wait_for_completion_interruptible(&s->wfc))
549 * To avoid increased complexity in the rest of the code
550 * where full fragment sizes are assumed, we cheat a little
551 * with the start pointer here and don't forget to restore
555 /* As this is a last frag we need only one dma channel
556 * to complete. So it's need to unlink dma channels
557 * to avoid empty dma work.
559 if (!cpu_is_omap1510() && AUDIO_QUEUE_EMPTY(s))
560 omap_sound_dma_unlink_lch(s);
562 shiftval = s->fragsize - b->offset;
563 b->offset = shiftval;
564 b->dma_addr -= shiftval;
566 local_irq_save(flags);
567 s->bytecount -= shiftval;
568 if (++s->usr_head >= s->nbfrags)
572 audio_process_dma(s);
573 local_irq_restore(flags);
576 /* Let's wait for all buffers to complete */
577 set_current_state(TASK_INTERRUPTIBLE);
578 add_wait_queue(&s->wq, &wait);
579 while ((s->pending_frags || (s->wfc.done < s->nbfrags))
580 && !signal_pending(current)) {
582 set_current_state(TASK_INTERRUPTIBLE);
584 set_current_state(TASK_RUNNING);
585 remove_wait_queue(&s->wq, &wait);
587 /* undo the pointer hack above */
589 local_irq_save(flags);
590 b->dma_addr += shiftval;
592 /* ensure sane DMA code behavior if not yet processed */
594 b->offset = s->fragsize;
595 local_irq_restore(flags);
602 /***************************************************************************************
604 * Stop all the DMA channels of the stream
606 **************************************************************************************/
607 void audio_stop_dma(audio_stream_t * s)
612 if (unlikely(NULL == chan)) {
616 for (i = 0; i < nr_linked_channels; i++) {
617 int cur_chan = chan[i];
618 omap_stop_dma(cur_chan);
625 /***************************************************************************************
629 **************************************************************************************/
630 u_int audio_get_dma_pos(audio_stream_t * s)
632 audio_buf_t *b = &s->buffers[s->dma_tail];
637 offset = omap_get_dma_src_pos(s->lch[s->dma_q_head]) - b->dma_addr;
638 if (offset >= s->fragsize)
639 offset = s->fragsize - 4;
640 } else if (s->pending_frags) {
649 /***************************************************************************************
651 * Reset the audio buffers
653 **************************************************************************************/
654 void audio_reset(audio_stream_t * s)
659 s->buffers[s->dma_head].offset = 0;
660 s->buffers[s->usr_head].offset = 0;
661 s->usr_head = s->dma_head;
662 s->pending_frags = 0;
663 init_completion(&s->wfc);
664 s->wfc.done = s->nbfrags;
673 /***************************************************************************************
675 * Clear any pending transfers
677 **************************************************************************************/
678 void omap_clear_sound_dma(audio_stream_t * s)
681 omap_clear_dma(s->lch[s->dma_q_head]);
686 /***************************************************************************************
688 * DMA related functions
690 **************************************************************************************/
691 static int audio_set_dma_params_play(int channel, dma_addr_t dma_ptr,
694 int dt = 0x1; /* data type 16 */
695 int cen = 32; /* Stereo */
696 int cfn = dma_size / (2 * cen);
697 unsigned long dest_start;
703 if (cpu_is_omap1510() || cpu_is_omap16xx()) {
704 dest_start = AUDIO_MCBSP_DATAWRITE;
705 dest_port = OMAP_DMA_PORT_MPUI;
707 if (cpu_is_omap24xx()) {
708 dest_start = AUDIO_MCBSP_DATAWRITE;
709 sync_dev = AUDIO_DMA_TX;
712 omap_set_dma_dest_params(channel, dest_port, OMAP_DMA_AMODE_CONSTANT, dest_start, 0, 0);
713 omap_set_dma_src_params(channel, 0, OMAP_DMA_AMODE_POST_INC, dma_ptr, 0, 0);
714 omap_set_dma_transfer_params(channel, dt, cen, cfn, OMAP_DMA_SYNC_ELEMENT, sync_dev, 0);
720 static int audio_set_dma_params_capture(int channel, dma_addr_t dma_ptr,
723 int dt = 0x1; /* data type 16 */
724 int cen = 16; /* mono */
725 int cfn = dma_size / (2 * cen);
726 unsigned long src_start;
733 if (cpu_is_omap1510() || cpu_is_omap16xx()) {
734 src_start = AUDIO_MCBSP_DATAREAD;
735 src_port = OMAP_DMA_PORT_MPUI;
737 if (cpu_is_omap24xx()) {
738 src_start = AUDIO_MCBSP_DATAREAD;
739 sync_dev = AUDIO_DMA_RX;
743 omap_set_dma_src_params(channel, src_port, OMAP_DMA_AMODE_CONSTANT, src_start, 0, 0);
744 omap_set_dma_dest_params(channel, 0, OMAP_DMA_AMODE_POST_INC, dma_ptr, 0, 0);
745 omap_set_dma_transfer_params(channel, dt, cen, cfn, OMAP_DMA_SYNC_ELEMENT, sync_dev, src_sync);
751 static int audio_start_dma_chain(audio_stream_t * s)
753 int channel = s->lch[s->dma_q_head];
756 s->hw_stop(); /* stops McBSP Interface */
757 omap_start_dma(channel);
759 s->hw_start(); /* start McBSP interface */
761 /* else the dma itself will progress forward with out our help */
767 * Do the initial set of work to initialize all the channels as required.
768 * We shall then initate a transfer
770 static int omap_start_sound_dma(audio_stream_t * s, dma_addr_t dma_ptr,
776 if (unlikely(dma_size > MAX_DMA_SIZE)) {
777 ERR("DmaSoundDma: Start: overflowed %d-%d\n", dma_size,
782 if (AUDIO_QUEUE_FULL(s)) {
787 if (s->input_or_output == FMODE_WRITE)
791 audio_set_dma_params_play(s->lch[s->dma_q_tail], dma_ptr,
795 audio_set_dma_params_capture(s->lch[s->dma_q_tail], dma_ptr,
799 ret = -2; /* indicate queue full */
802 AUDIO_INCREMENT_TAIL(s);
803 ret = audio_start_dma_chain(s);
805 ERR("dma start failed");
813 /***************************************************************************************
815 * ISR related functions
817 **************************************************************************************/
818 /* The work item handler */
819 static void audio_dsr_handler(unsigned long inData)
821 void *data = (void *)inData;
822 struct audio_isr_work_item *work = data;
823 audio_stream_t *s = (work->s);
824 int sound_curr_lch = work->current_lch;
825 u16 ch_status = work->ch_status;
828 DPRINTK("lch=%d,status=0x%x, data=%p as=%p\n", sound_curr_lch,
830 if (AUDIO_QUEUE_EMPTY(s)) {
831 ERR("Interrupt(%d) for empty queue(h=%d, T=%d)???\n",
832 sound_curr_lch, s->dma_q_head, s->dma_q_tail);
833 ERR("nbfrag=%d,pendfrags=%d,USR-H=%d, QH-%d QT-%d\n",
834 s->nbfrags, s->pending_frags, s->usr_head, s->dma_head,
840 AUDIO_INCREMENT_HEAD(s); /* Empty the queue */
842 /* Try to fill again */
843 audio_dma_callback(sound_curr_lch, ch_status, s);
848 /* Macro to trace the IRQ calls - checks for multi-channel irqs */
852 static char xyz[MAX_UP] = { 0 };
856 /* ISRs have to be short and smart.. So we transfer every heavy duty stuff to the
859 static void sound_dma_irq_handler(int sound_curr_lch, u16 ch_status, void *data)
861 int dma_status = ch_status;
862 audio_stream_t *s = (audio_stream_t *) data;
865 xyz[h++] = '0' + sound_curr_lch;
866 if (h == MAX_UP - 1) {
871 DPRINTK("lch=%d,status=0x%x, dma_status=%d, data=%p\n", sound_curr_lch,
872 ch_status, dma_status, data);
874 if (dma_status & (DCSR_ERROR)) {
875 if (cpu_is_omap1510() || cpu_is_omap16xx())
876 OMAP_DMA_CCR_REG(sound_curr_lch) &= ~DCCR_EN;
877 ERR("DCSR_ERROR!\n");
882 if (AUDIO_QUEUE_LAST(s))
885 /* Start the work item - we ping pong the work items */
886 if (!work_item_running) {
887 work1.current_lch = sound_curr_lch;
888 work1.ch_status = ch_status;
890 /* schedule tasklet 1 */
891 tasklet_schedule(&audio_isr_work1);
892 work_item_running = 1;
894 work2.current_lch = sound_curr_lch;
895 work2.ch_status = ch_status;
897 /* schedule tasklet 2 */
898 tasklet_schedule(&audio_isr_work2);
899 work_item_running = 0;
905 /* The call back that handles buffer stuff */
906 static void audio_dma_callback(int lch, u16 ch_status, void *data)
908 audio_stream_t *s = data;
909 audio_buf_t *b = &s->buffers[s->dma_tail];
912 if (s->dma_spinref > 0) {
914 } else if (!s->buffers) {
916 "omap_audio: received DMA IRQ for non existent buffers!\n");
918 } else if (b->dma_ref && --b->dma_ref == 0 && b->offset >= s->fragsize) {
919 /* This fragment is done */
921 s->bytecount += s->fragsize;
923 s->dma_spinref = -s->dma_spinref;
925 if (++s->dma_tail >= s->nbfrags)
936 audio_process_dma(s);
942 /*********************************************************************************
944 * audio_get_dma_callback(): return the dma interface call back function
946 *********************************************************************************/
947 dma_callback_t audio_get_dma_callback(void)
951 return audio_dma_callback;
954 static int __init audio_dma_init(void)
956 if (!cpu_is_omap1510())
957 nr_linked_channels = 2;
962 static void __exit audio_dma_exit(void)
967 module_init(audio_dma_init);
968 module_exit(audio_dma_exit);
970 MODULE_AUTHOR("Texas Instruments");
971 MODULE_DESCRIPTION("Common DMA handling for Audio driver on OMAP processors");
972 MODULE_LICENSE("GPL");
974 EXPORT_SYMBOL(omap_clear_sound_dma);
975 EXPORT_SYMBOL(omap_request_sound_dma);
976 EXPORT_SYMBOL(omap_free_sound_dma);
978 EXPORT_SYMBOL(audio_get_dma_callback);
979 EXPORT_SYMBOL(audio_setup_buf);
980 EXPORT_SYMBOL(audio_process_dma);
981 EXPORT_SYMBOL(audio_prime_rx);
982 EXPORT_SYMBOL(audio_set_fragments);
983 EXPORT_SYMBOL(audio_sync);
984 EXPORT_SYMBOL(audio_stop_dma);
985 EXPORT_SYMBOL(audio_get_dma_pos);
986 EXPORT_SYMBOL(audio_reset);
987 EXPORT_SYMBOL(audio_discard_buf);