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 <mach/hardware.h>
55 #include <asm/semaphore.h>
58 #include "omap-audio-dma-intfc.h"
60 #include <mach/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_AI (1 << 8)
112 #define DCCR_REPEAT (1 << 9)
113 /* if 0 the channel works in 3.1 compatible mode*/
114 #define DCCR_N31COMP (1 << 10)
115 #define DCCR_EP (1 << 11)
116 #define DCCR_SRC_AMODE_BIT 12
117 #define DCCR_SRC_AMODE_MASK (0x3<<12)
118 #define DCCR_DST_AMODE_BIT 14
119 #define DCCR_DST_AMODE_MASK (0x3<<14)
120 #define AMODE_CONST 0x0
121 #define AMODE_POST_INC 0x1
122 #define AMODE_SINGLE_INDEX 0x2
123 #define AMODE_DOUBLE_INDEX 0x3
125 /**************************** DATA STRUCTURES *****************************************/
127 static spinlock_t dma_list_lock = SPIN_LOCK_UNLOCKED;
129 struct audio_isr_work_item {
135 static char work_item_running = 0;
136 static char nr_linked_channels = 1;
137 static struct audio_isr_work_item work1, work2;
140 /*********************************** MODULE SPECIFIC FUNCTIONS PROTOTYPES *************/
142 static void audio_dsr_handler(unsigned long);
143 static DECLARE_TASKLET(audio_isr_work1, audio_dsr_handler,
144 (unsigned long)&work1);
145 static DECLARE_TASKLET(audio_isr_work2, audio_dsr_handler,
146 (unsigned long)&work2);
148 static void sound_dma_irq_handler(int lch, u16 ch_status, void *data);
149 static void audio_dma_callback(int lch, u16 ch_status, void *data);
150 static int omap_start_sound_dma(audio_stream_t * s, dma_addr_t dma_ptr,
152 static int audio_set_dma_params_play(int channel, dma_addr_t dma_ptr,
154 static int audio_set_dma_params_capture(int channel, dma_addr_t dma_ptr,
156 static int audio_start_dma_chain(audio_stream_t * s);
158 /*********************************** GLOBAL FUNCTIONS DEFINTIONS ***********************/
160 /***************************************************************************************
162 * Buffer creation/destruction
164 **************************************************************************************/
165 int audio_setup_buf(audio_stream_t * s)
170 dma_addr_t dmaphys = 0;
176 s->buffers = kmalloc(sizeof(audio_buf_t) * s->nbfrags, GFP_KERNEL);
179 memset(s->buffers, 0, sizeof(audio_buf_t) * s->nbfrags);
180 for (frag = 0; frag < s->nbfrags; frag++) {
181 audio_buf_t *b = &s->buffers[frag];
183 * Let's allocate non-cached memory for DMA buffers.
184 * We try to allocate all memory at once.
185 * If this fails (a common reason is memory fragmentation),
186 * then we allocate more smaller buffers.
189 dmasize = (s->nbfrags - frag) * s->fragsize;
192 dma_alloc_coherent(NULL, dmasize, &dmaphys,
195 dmasize -= s->fragsize;
197 while (!dmabuf && dmasize);
201 memzero(dmabuf, dmasize);
204 b->dma_addr = dmaphys;
205 dmabuf += s->fragsize;
206 dmaphys += s->fragsize;
207 dmasize -= s->fragsize;
209 s->usr_head = s->dma_head = s->dma_tail = 0;
214 init_completion(&s->wfc);
215 s->wfc.done = s->nbfrags;
219 audio_discard_buf(s);
224 void audio_discard_buf(audio_stream_t * s)
227 /* ensure DMA isn't using those buffers */
231 for (frag = 0; frag < s->nbfrags; frag++) {
232 if (!s->buffers[frag].master)
234 dma_free_coherent(NULL,
235 s->buffers[frag].master,
236 s->buffers[frag].data,
237 s->buffers[frag].dma_addr);
245 /***************************************************************************************
247 * DMA channel requests
249 **************************************************************************************/
250 static void omap_sound_dma_link_lch(void *data)
252 audio_stream_t *s = (audio_stream_t *) data;
261 for (i = 0; i < nr_linked_channels; i++) {
262 int cur_chan = chan[i];
264 ((nr_linked_channels - 1 ==
265 i) ? chan[0] : chan[i + 1]);
266 omap_dma_link_lch(cur_chan, nex_chan);
273 omap_request_sound_dma(int device_id, const char *device_name, void *data,
279 if (unlikely((NULL == channels) || (NULL == device_name))) {
283 /* Try allocate memory for the num channels */
285 (int *)kmalloc(sizeof(int) * nr_linked_channels,
289 ERR("No Memory for channel allocs!\n");
293 spin_lock(&dma_list_lock);
294 for (i = 0; i < nr_linked_channels; i++) {
296 omap_request_dma(device_id, device_name,
297 sound_dma_irq_handler, data, &chan[i]);
298 /* Handle Failure condition here */
301 for (j = 0; j < i; j++) {
302 omap_free_dma(chan[j]);
304 spin_unlock(&dma_list_lock);
307 ERR("Error in requesting channel %d=0x%x\n", i, err);
313 /* Chain the channels together */
314 if (!cpu_is_omap15xx())
315 omap_sound_dma_link_lch(data);
317 spin_unlock(&dma_list_lock);
322 /***************************************************************************************
324 * DMA channel requests Freeing
326 **************************************************************************************/
327 static void omap_sound_dma_unlink_lch(void *data)
329 audio_stream_t *s = (audio_stream_t *) data;
338 for (i = 0; i < nr_linked_channels; i++) {
339 int cur_chan = chan[i];
341 ((nr_linked_channels - 1 ==
342 i) ? chan[0] : chan[i + 1]);
343 omap_dma_unlink_lch(cur_chan, nex_chan);
349 int omap_free_sound_dma(void *data, int **channels)
354 if (unlikely(NULL == channels)) {
358 if (unlikely(NULL == *channels)) {
364 if (!cpu_is_omap15xx())
365 omap_sound_dma_unlink_lch(data);
366 for (i = 0; i < nr_linked_channels; i++) {
367 int cur_chan = chan[i];
368 omap_stop_dma(cur_chan);
369 omap_free_dma(cur_chan);
377 /***************************************************************************************
379 * Process DMA requests - This will end up starting the transfer. Proper fragments of
380 * Transfers will be initiated.
382 **************************************************************************************/
383 int audio_process_dma(audio_stream_t * s)
389 /* Dont let the ISR over ride touching the in_use flag */
390 local_irq_save(flags);
391 if (1 == s->in_use) {
392 local_irq_restore(flags);
393 ERR("Called again while In Use\n");
397 local_irq_restore(flags);
402 if (s->dma_spinref > 0 && s->pending_frags) {
406 while (s->pending_frags) {
407 audio_buf_t *b = &s->buffers[s->dma_head];
408 u_int dma_size = s->fragsize - b->offset;
409 if (dma_size > MAX_DMA_SIZE)
410 dma_size = CUT_DMA_SIZE;
412 omap_start_sound_dma(s, b->dma_addr + b->offset, dma_size);
417 b->offset += dma_size;
418 if (b->offset >= s->fragsize) {
420 if (++s->dma_head >= s->nbfrags)
427 ERR("we are spinning\n");
428 while (omap_start_sound_dma(s, SPIN_ADDR, SPIN_SIZE) == 0)
431 * Note: if there is still a data buffer being
432 * processed then the ref count is negative. This
433 * allows for the DMA termination to be accounted in
434 * the proper order. Of course dma_spinref can't be
435 * greater than 0 if dma_ref is not 0 since we kill
436 * the spinning above as soon as there is real data to process.
438 if (s->buffers && s->buffers[s->dma_tail].dma_ref)
440 s->dma_spinref += spincnt;
450 /***************************************************************************************
452 * Prime Rx - Since the recieve buffer has no time limit as to when it would arrive,
453 * we need to prime it
455 **************************************************************************************/
456 void audio_prime_rx(audio_state_t * state)
458 audio_stream_t *is = state->input_stream;
461 if (state->need_tx_for_rx) {
463 * With some codecs like the Philips UDA1341 we must ensure
464 * there is an output stream at any time while recording since
465 * this is how the UDA1341 gets its clock from the SA1100.
466 * So while there is no playback data to send, the output DMA
467 * will spin with all zeroes. We use the cache flush special
470 state->output_stream->spin_idle = 1;
471 audio_process_dma(state->output_stream);
473 is->pending_frags = is->nbfrags;
474 init_completion(&is->wfc);
478 audio_process_dma(is);
484 /***************************************************************************************
486 * set the fragment size
488 **************************************************************************************/
489 int audio_set_fragments(audio_stream_t * s, int val)
495 audio_discard_buf(s);
496 s->nbfrags = (val >> 16) & 0x7FFF;
502 s->fragsize = 1 << val;
505 if (s->nbfrags * s->fragsize > 128 * 1024)
506 s->nbfrags = 128 * 1024 / s->fragsize;
508 if (audio_setup_buf(s))
510 return val | (s->nbfrags << 16);
514 /***************************************************************************************
516 * Sync up the buffers before we shutdown, else under-run errors will happen
518 **************************************************************************************/
519 int audio_sync(struct file *file)
521 audio_state_t *state = file->private_data;
522 audio_stream_t *s = state->output_stream;
527 DECLARE_WAITQUEUE(wait, current);
531 if (!(file->f_mode & FMODE_WRITE) || !s->buffers || s->mapped) {
537 * Send current buffer if it contains data. Be sure to send
538 * a full sample count.
540 b = &s->buffers[s->usr_head];
541 if (b->offset &= ~3) {
542 /* Wait for a buffer to become free */
543 if (wait_for_completion_interruptible(&s->wfc))
547 * To avoid increased complexity in the rest of the code
548 * where full fragment sizes are assumed, we cheat a little
549 * with the start pointer here and don't forget to restore
553 /* As this is a last frag we need only one dma channel
554 * to complete. So it's need to unlink dma channels
555 * to avoid empty dma work.
557 if (!cpu_is_omap15xx() && AUDIO_QUEUE_EMPTY(s))
558 omap_sound_dma_unlink_lch(s);
560 shiftval = s->fragsize - b->offset;
561 b->offset = shiftval;
562 b->dma_addr -= shiftval;
564 local_irq_save(flags);
565 s->bytecount -= shiftval;
566 if (++s->usr_head >= s->nbfrags)
570 audio_process_dma(s);
571 local_irq_restore(flags);
574 /* Let's wait for all buffers to complete */
575 set_current_state(TASK_INTERRUPTIBLE);
576 add_wait_queue(&s->wq, &wait);
577 while ((s->pending_frags || (s->wfc.done < s->nbfrags))
578 && !signal_pending(current)) {
580 set_current_state(TASK_INTERRUPTIBLE);
582 set_current_state(TASK_RUNNING);
583 remove_wait_queue(&s->wq, &wait);
585 /* undo the pointer hack above */
587 local_irq_save(flags);
588 b->dma_addr += shiftval;
590 /* ensure sane DMA code behavior if not yet processed */
592 b->offset = s->fragsize;
593 local_irq_restore(flags);
600 /***************************************************************************************
602 * Stop all the DMA channels of the stream
604 **************************************************************************************/
605 void audio_stop_dma(audio_stream_t * s)
610 if (unlikely(NULL == chan)) {
614 for (i = 0; i < nr_linked_channels; i++) {
615 int cur_chan = chan[i];
616 omap_stop_dma(cur_chan);
623 /***************************************************************************************
627 **************************************************************************************/
628 u_int audio_get_dma_pos(audio_stream_t * s)
630 audio_buf_t *b = &s->buffers[s->dma_tail];
635 offset = omap_get_dma_src_pos(s->lch[s->dma_q_head]) - b->dma_addr;
636 if (offset >= s->fragsize)
637 offset = s->fragsize - 4;
638 } else if (s->pending_frags) {
647 /***************************************************************************************
649 * Reset the audio buffers
651 **************************************************************************************/
652 void audio_reset(audio_stream_t * s)
657 s->buffers[s->dma_head].offset = 0;
658 s->buffers[s->usr_head].offset = 0;
659 s->usr_head = s->dma_head;
660 s->pending_frags = 0;
661 init_completion(&s->wfc);
662 s->wfc.done = s->nbfrags;
671 /***************************************************************************************
673 * Clear any pending transfers
675 **************************************************************************************/
676 void omap_clear_sound_dma(audio_stream_t * s)
679 omap_clear_dma(s->lch[s->dma_q_head]);
684 /***************************************************************************************
686 * DMA related functions
688 **************************************************************************************/
689 static int audio_set_dma_params_play(int channel, dma_addr_t dma_ptr,
692 int dt = 0x1; /* data type 16 */
693 int cen = 32; /* Stereo */
694 int cfn = dma_size / (2 * cen);
695 unsigned long dest_start;
701 if (cpu_is_omap15xx() || cpu_is_omap16xx()) {
702 dest_start = AUDIO_MCBSP_DATAWRITE;
703 dest_port = OMAP_DMA_PORT_MPUI;
705 if (cpu_is_omap24xx()) {
706 dest_start = AUDIO_MCBSP_DATAWRITE;
707 sync_dev = AUDIO_DMA_TX;
710 omap_set_dma_dest_params(channel, dest_port, OMAP_DMA_AMODE_CONSTANT, dest_start, 0, 0);
711 omap_set_dma_src_params(channel, 0, OMAP_DMA_AMODE_POST_INC, dma_ptr, 0, 0);
712 omap_set_dma_transfer_params(channel, dt, cen, cfn, OMAP_DMA_SYNC_ELEMENT, sync_dev, 0);
718 static int audio_set_dma_params_capture(int channel, dma_addr_t dma_ptr,
721 int dt = 0x1; /* data type 16 */
722 int cen = 16; /* mono */
723 int cfn = dma_size / (2 * cen);
724 unsigned long src_start;
731 if (cpu_is_omap15xx() || cpu_is_omap16xx()) {
732 src_start = AUDIO_MCBSP_DATAREAD;
733 src_port = OMAP_DMA_PORT_MPUI;
735 if (cpu_is_omap24xx()) {
736 src_start = AUDIO_MCBSP_DATAREAD;
737 sync_dev = AUDIO_DMA_RX;
741 omap_set_dma_src_params(channel, src_port, OMAP_DMA_AMODE_CONSTANT, src_start, 0, 0);
742 omap_set_dma_dest_params(channel, 0, OMAP_DMA_AMODE_POST_INC, dma_ptr, 0, 0);
743 omap_set_dma_transfer_params(channel, dt, cen, cfn, OMAP_DMA_SYNC_ELEMENT, sync_dev, src_sync);
749 static int audio_start_dma_chain(audio_stream_t * s)
751 int channel = s->lch[s->dma_q_head];
754 s->hw_stop(); /* stops McBSP Interface */
755 omap_start_dma(channel);
757 s->hw_start(); /* start McBSP interface */
759 /* else the dma itself will progress forward with out our help */
765 * Do the initial set of work to initialize all the channels as required.
766 * We shall then initate a transfer
768 static int omap_start_sound_dma(audio_stream_t * s, dma_addr_t dma_ptr,
774 if (unlikely(dma_size > MAX_DMA_SIZE)) {
775 ERR("DmaSoundDma: Start: overflowed %d-%d\n", dma_size,
780 if (AUDIO_QUEUE_FULL(s)) {
785 if (s->input_or_output == FMODE_WRITE)
789 audio_set_dma_params_play(s->lch[s->dma_q_tail], dma_ptr,
793 audio_set_dma_params_capture(s->lch[s->dma_q_tail], dma_ptr,
797 ret = -2; /* indicate queue full */
800 AUDIO_INCREMENT_TAIL(s);
801 ret = audio_start_dma_chain(s);
803 ERR("dma start failed");
811 /***************************************************************************************
813 * ISR related functions
815 **************************************************************************************/
816 /* The work item handler */
817 static void audio_dsr_handler(unsigned long inData)
819 void *data = (void *)inData;
820 struct audio_isr_work_item *work = data;
821 audio_stream_t *s = (work->s);
822 int sound_curr_lch = work->current_lch;
823 u16 ch_status = work->ch_status;
826 DPRINTK("lch=%d,status=0x%x, data=%p as=%p\n", sound_curr_lch,
828 if (AUDIO_QUEUE_EMPTY(s)) {
829 ERR("Interrupt(%d) for empty queue(h=%d, T=%d)???\n",
830 sound_curr_lch, s->dma_q_head, s->dma_q_tail);
831 ERR("nbfrag=%d,pendfrags=%d,USR-H=%d, QH-%d QT-%d\n",
832 s->nbfrags, s->pending_frags, s->usr_head, s->dma_head,
838 AUDIO_INCREMENT_HEAD(s); /* Empty the queue */
840 /* Try to fill again */
841 audio_dma_callback(sound_curr_lch, ch_status, s);
846 /* Macro to trace the IRQ calls - checks for multi-channel irqs */
850 static char xyz[MAX_UP] = { 0 };
854 /* ISRs have to be short and smart.. So we transfer every heavy duty stuff to the
857 static void sound_dma_irq_handler(int sound_curr_lch, u16 ch_status, void *data)
859 int dma_status = ch_status;
860 audio_stream_t *s = (audio_stream_t *) data;
863 xyz[h++] = '0' + sound_curr_lch;
864 if (h == MAX_UP - 1) {
869 DPRINTK("lch=%d,status=0x%x, dma_status=%d, data=%p\n", sound_curr_lch,
870 ch_status, dma_status, data);
872 if (dma_status & (DCSR_ERROR)) {
873 if (cpu_is_omap15xx() || cpu_is_omap16xx())
874 omap_stop_dma(sound_curr_lch);
875 ERR("DCSR_ERROR!\n");
880 if (AUDIO_QUEUE_LAST(s))
883 /* Start the work item - we ping pong the work items */
884 if (!work_item_running) {
885 work1.current_lch = sound_curr_lch;
886 work1.ch_status = ch_status;
888 /* schedule tasklet 1 */
889 tasklet_schedule(&audio_isr_work1);
890 work_item_running = 1;
892 work2.current_lch = sound_curr_lch;
893 work2.ch_status = ch_status;
895 /* schedule tasklet 2 */
896 tasklet_schedule(&audio_isr_work2);
897 work_item_running = 0;
903 /* The call back that handles buffer stuff */
904 static void audio_dma_callback(int lch, u16 ch_status, void *data)
906 audio_stream_t *s = data;
907 audio_buf_t *b = &s->buffers[s->dma_tail];
910 if (s->dma_spinref > 0) {
912 } else if (!s->buffers) {
914 "omap_audio: received DMA IRQ for non existent buffers!\n");
916 } else if (b->dma_ref && --b->dma_ref == 0 && b->offset >= s->fragsize) {
917 /* This fragment is done */
919 s->bytecount += s->fragsize;
921 s->dma_spinref = -s->dma_spinref;
923 if (++s->dma_tail >= s->nbfrags)
934 audio_process_dma(s);
940 /*********************************************************************************
942 * audio_get_dma_callback(): return the dma interface call back function
944 *********************************************************************************/
945 dma_callback_t audio_get_dma_callback(void)
949 return audio_dma_callback;
952 static int __init audio_dma_init(void)
954 if (!cpu_is_omap15xx())
955 nr_linked_channels = 2;
960 static void __exit audio_dma_exit(void)
965 module_init(audio_dma_init);
966 module_exit(audio_dma_exit);
968 MODULE_AUTHOR("Texas Instruments");
969 MODULE_DESCRIPTION("Common DMA handling for Audio driver on OMAP processors");
970 MODULE_LICENSE("GPL");
972 EXPORT_SYMBOL(omap_clear_sound_dma);
973 EXPORT_SYMBOL(omap_request_sound_dma);
974 EXPORT_SYMBOL(omap_free_sound_dma);
976 EXPORT_SYMBOL(audio_get_dma_callback);
977 EXPORT_SYMBOL(audio_setup_buf);
978 EXPORT_SYMBOL(audio_process_dma);
979 EXPORT_SYMBOL(audio_prime_rx);
980 EXPORT_SYMBOL(audio_set_fragments);
981 EXPORT_SYMBOL(audio_sync);
982 EXPORT_SYMBOL(audio_stop_dma);
983 EXPORT_SYMBOL(audio_get_dma_pos);
984 EXPORT_SYMBOL(audio_reset);
985 EXPORT_SYMBOL(audio_discard_buf);