Merge current mainline tree into linux-omap tree
[pandora-kernel.git] / sound / oss / omap-audio-dma-intfc.c
1 /*
2  * linux/sound/oss/omap-audio-dma-intfc.c
3  *
4  * Common audio DMA handling for the OMAP processors
5  *
6  * Copyright (C) 2004 Texas Instruments, Inc.
7  *
8  * Copyright (C) 2000, 2001 Nicolas Pitre <nico@cam.org>
9  *
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.
13  *
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.
17  *
18  * History:
19  *
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
22  *                                audio driver.
23  *
24  * 2004-06-22   Sriram Kannan   - removed legacy code (auto-init). Self-linking of DMA logical channel.
25  *
26  * 2004-08-12   Nishanth Menon  - Modified to integrate Audio requirements on 1610,1710 platforms
27  *
28  * 2004-11-01   Nishanth Menon  - 16xx platform code base modified to support multi channel chaining.
29  *
30  * 2004-12-15   Nishanth Menon  - Improved 16xx platform channel logic introduced - tasklets, queue handling updated
31  *
32  * 2005-12-10   Dirk Behme      - Added L/R Channel Interchange fix as proposed by Ajaya Babu
33  */
34
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/types.h>
38 #include <linux/fs.h>
39 #include <linux/mm.h>
40 #include <linux/slab.h>
41 #include <linux/sched.h>
42 #include <linux/poll.h>
43 #include <linux/pm.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>
51
52 #include <asm/uaccess.h>
53 #include <asm/io.h>
54 #include <mach/hardware.h>
55 #include <asm/semaphore.h>
56
57 #include <mach/dma.h>
58 #include "omap-audio-dma-intfc.h"
59
60 #include <mach/mcbsp.h>
61
62 #include "omap-audio.h"
63
64 #undef DEBUG
65 //#define DEBUG
66 #ifdef DEBUG
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)
70 #else
71
72 #define DPRINTK( x... )
73 #define FN_IN
74 #define FN_OUT(x)
75 #endif
76
77 #define ERR(ARGS...) printk(KERN_ERR "{%s}-ERROR: ", __FUNCTION__);printk(ARGS);
78
79 #define AUDIO_NAME              "omap-audio"
80 #define AUDIO_NBFRAGS_DEFAULT   8
81 #define AUDIO_FRAGSIZE_DEFAULT  8192
82
83 #define AUDIO_ACTIVE(state)     ((state)->rd_ref || (state)->wr_ref)
84
85 #define SPIN_ADDR               (dma_addr_t)0
86 #define SPIN_SIZE               2048
87
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
92  */
93
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++;
101
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)
108
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
124
125 /**************************** DATA STRUCTURES *****************************************/
126
127 static spinlock_t dma_list_lock = SPIN_LOCK_UNLOCKED;
128
129 struct audio_isr_work_item {
130         int current_lch;
131         u16 ch_status;
132         audio_stream_t *s;
133 };
134
135 static char work_item_running = 0;
136 static char nr_linked_channels = 1;
137 static struct audio_isr_work_item work1, work2;
138
139
140 /*********************************** MODULE SPECIFIC FUNCTIONS PROTOTYPES *************/
141
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);
147
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,
151                                 u_int size);
152 static int audio_set_dma_params_play(int channel, dma_addr_t dma_ptr,
153                                      u_int dma_size);
154 static int audio_set_dma_params_capture(int channel, dma_addr_t dma_ptr,
155                                         u_int dma_size);
156 static int audio_start_dma_chain(audio_stream_t * s);
157
158 /*********************************** GLOBAL FUNCTIONS DEFINTIONS ***********************/
159
160 /***************************************************************************************
161  *
162  * Buffer creation/destruction
163  *
164  **************************************************************************************/
165 int audio_setup_buf(audio_stream_t * s)
166 {
167         int frag;
168         int dmasize = 0;
169         char *dmabuf = NULL;
170         dma_addr_t dmaphys = 0;
171         FN_IN;
172         if (s->buffers) {
173                 FN_OUT(1);
174                 return -EBUSY;
175         }
176         s->buffers = kmalloc(sizeof(audio_buf_t) * s->nbfrags, GFP_KERNEL);
177         if (!s->buffers)
178                 goto err;
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];
182                 /*
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.
187                  */
188                 if (!dmasize) {
189                         dmasize = (s->nbfrags - frag) * s->fragsize;
190                         do {
191                                 dmabuf =
192                                     dma_alloc_coherent(NULL, dmasize, &dmaphys,
193                                                        0);
194                                 if (!dmabuf)
195                                         dmasize -= s->fragsize;
196                         }
197                         while (!dmabuf && dmasize);
198                         if (!dmabuf)
199                                 goto err;
200                         b->master = dmasize;
201                         memzero(dmabuf, dmasize);
202                 }
203                 b->data = dmabuf;
204                 b->dma_addr = dmaphys;
205                 dmabuf += s->fragsize;
206                 dmaphys += s->fragsize;
207                 dmasize -= s->fragsize;
208         }
209         s->usr_head = s->dma_head = s->dma_tail = 0;
210         AUDIO_QUEUE_INIT(s);
211         s->started = 0;
212         s->bytecount = 0;
213         s->fragcount = 0;
214         init_completion(&s->wfc);
215         s->wfc.done = s->nbfrags;
216         FN_OUT(0);
217         return 0;
218       err:
219         audio_discard_buf(s);
220         FN_OUT(1);
221         return -ENOMEM;
222 }
223
224 void audio_discard_buf(audio_stream_t * s)
225 {
226         FN_IN;
227         /* ensure DMA isn't using those buffers */
228         audio_reset(s);
229         if (s->buffers) {
230                 int frag;
231                 for (frag = 0; frag < s->nbfrags; frag++) {
232                         if (!s->buffers[frag].master)
233                                 continue;
234                         dma_free_coherent(NULL,
235                                           s->buffers[frag].master,
236                                           s->buffers[frag].data,
237                                           s->buffers[frag].dma_addr);
238                 }
239                 kfree(s->buffers);
240                 s->buffers = NULL;
241         }
242         FN_OUT(0);
243 }
244
245 /***************************************************************************************
246  *
247  * DMA channel requests
248  *
249  **************************************************************************************/
250 static void omap_sound_dma_link_lch(void *data)
251 {
252         audio_stream_t *s = (audio_stream_t *) data;
253         int *chan = s->lch;
254         int i;
255
256         FN_IN;
257         if (s->linked) {
258                 FN_OUT(1);
259                 return;
260         }
261         for (i = 0; i < nr_linked_channels; i++) {
262                 int cur_chan = chan[i];
263                 int nex_chan =
264                     ((nr_linked_channels - 1 ==
265                       i) ? chan[0] : chan[i + 1]);
266                 omap_dma_link_lch(cur_chan, nex_chan);
267         }
268         s->linked = 1;
269         FN_OUT(0);
270 }
271
272 int
273 omap_request_sound_dma(int device_id, const char *device_name, void *data,
274                        int **channels)
275 {
276         int i, err = 0;
277         int *chan = NULL;
278         FN_IN;
279         if (unlikely((NULL == channels) || (NULL == device_name))) {
280                 BUG();
281                 return -EPERM;
282         }
283         /* Try allocate memory for the num channels */
284         *channels =
285             (int *)kmalloc(sizeof(int) * nr_linked_channels,
286                            GFP_KERNEL);
287         chan = *channels;
288         if (NULL == chan) {
289                 ERR("No Memory for channel allocs!\n");
290                 FN_OUT(-ENOMEM);
291                 return -ENOMEM;
292         }
293         spin_lock(&dma_list_lock);
294         for (i = 0; i < nr_linked_channels; i++) {
295                 err =
296                     omap_request_dma(device_id, device_name,
297                                      sound_dma_irq_handler, data, &chan[i]);
298                 /* Handle Failure condition here */
299                 if (err < 0) {
300                         int j;
301                         for (j = 0; j < i; j++) {
302                                 omap_free_dma(chan[j]);
303                         }
304                         spin_unlock(&dma_list_lock);
305                         kfree(chan);
306                         *channels = NULL;
307                         ERR("Error in requesting channel %d=0x%x\n", i, err);
308                         FN_OUT(err);
309                         return err;
310                 }
311         }
312
313         /* Chain the channels together */
314         if (!cpu_is_omap15xx())
315                 omap_sound_dma_link_lch(data);
316
317         spin_unlock(&dma_list_lock);
318         FN_OUT(0);
319         return 0;
320 }
321
322 /***************************************************************************************
323  *
324  * DMA channel requests Freeing
325  *
326  **************************************************************************************/
327 static void omap_sound_dma_unlink_lch(void *data)
328 {
329         audio_stream_t *s = (audio_stream_t *) data;
330         int *chan = s->lch;
331         int i;
332
333         FN_IN;
334         if (!s->linked) {
335                 FN_OUT(1);
336                 return;
337         }
338         for (i = 0; i < nr_linked_channels; i++) {
339                 int cur_chan = chan[i];
340                 int nex_chan =
341                     ((nr_linked_channels - 1 ==
342                       i) ? chan[0] : chan[i + 1]);
343                 omap_dma_unlink_lch(cur_chan, nex_chan);
344         }
345         s->linked = 0;
346         FN_OUT(0);
347 }
348
349 int omap_free_sound_dma(void *data, int **channels)
350 {
351         int i;
352         int *chan = NULL;
353         FN_IN;
354         if (unlikely(NULL == channels)) {
355                 BUG();
356                 return -EPERM;
357         }
358         if (unlikely(NULL == *channels)) {
359                 BUG();
360                 return -EPERM;
361         }
362         chan = (*channels);
363
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);
370         }
371         kfree(*channels);
372         *channels = NULL;
373         FN_OUT(0);
374         return 0;
375 }
376
377 /***************************************************************************************
378  *
379  * Process DMA requests - This will end up starting the transfer. Proper fragments of
380  * Transfers will be initiated.
381  *
382  **************************************************************************************/
383 int audio_process_dma(audio_stream_t * s)
384 {
385         int ret = 0;
386         unsigned long flags;
387         FN_IN;
388
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");
394                 return 0;
395         }
396         s->in_use = 1;
397         local_irq_restore(flags);
398
399         if (s->stopped)
400                 goto spin;
401
402         if (s->dma_spinref > 0 && s->pending_frags) {
403                 s->dma_spinref = 0;
404                 DMA_CLEAR(s);
405         }
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;
411                 ret =
412                     omap_start_sound_dma(s, b->dma_addr + b->offset, dma_size);
413                 if (ret) {
414                         goto process_out;
415                 }
416                 b->dma_ref++;
417                 b->offset += dma_size;
418                 if (b->offset >= s->fragsize) {
419                         s->pending_frags--;
420                         if (++s->dma_head >= s->nbfrags)
421                                 s->dma_head = 0;
422                 }
423         }
424       spin:
425         if (s->spin_idle) {
426                 int spincnt = 0;
427                 ERR("we are spinning\n");
428                 while (omap_start_sound_dma(s, SPIN_ADDR, SPIN_SIZE) == 0)
429                         spincnt++;
430                 /*
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.
437                  */
438                 if (s->buffers && s->buffers[s->dma_tail].dma_ref)
439                         spincnt = -spincnt;
440                 s->dma_spinref += spincnt;
441         }
442
443       process_out:
444         s->in_use = 0;
445
446         FN_OUT(ret);
447         return ret;
448 }
449
450 /***************************************************************************************
451  *
452  * Prime Rx - Since the recieve buffer has no time limit as to when it would arrive,
453  *            we need to prime it
454  *            
455  **************************************************************************************/
456 void audio_prime_rx(audio_state_t * state)
457 {
458         audio_stream_t *is = state->input_stream;
459
460         FN_IN;
461         if (state->need_tx_for_rx) {
462                 /*
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
468                  * area for that.
469                  */
470                 state->output_stream->spin_idle = 1;
471                 audio_process_dma(state->output_stream);
472         }
473         is->pending_frags = is->nbfrags;
474         init_completion(&is->wfc);
475         is->wfc.done = 0;
476
477         is->active = 1;
478         audio_process_dma(is);
479
480         FN_OUT(0);
481         return;
482 }
483
484 /***************************************************************************************
485  *
486  * set the fragment size
487  *
488  **************************************************************************************/
489 int audio_set_fragments(audio_stream_t * s, int val)
490 {
491         FN_IN;
492         if (s->active)
493                 return -EBUSY;
494         if (s->buffers)
495                 audio_discard_buf(s);
496         s->nbfrags = (val >> 16) & 0x7FFF;
497         val &= 0xFFFF;
498         if (val < 4)
499                 val = 4;
500         if (val > 15)
501                 val = 15;
502         s->fragsize = 1 << val;
503         if (s->nbfrags < 2)
504                 s->nbfrags = 2;
505         if (s->nbfrags * s->fragsize > 128 * 1024)
506                 s->nbfrags = 128 * 1024 / s->fragsize;
507         FN_OUT(0);
508         if (audio_setup_buf(s))
509                 return -ENOMEM;
510         return val | (s->nbfrags << 16);
511
512 }
513
514 /***************************************************************************************
515  *
516  * Sync up the buffers before we shutdown, else under-run errors will happen
517  *
518  **************************************************************************************/
519 int audio_sync(struct file *file)
520 {
521         audio_state_t *state = file->private_data;
522         audio_stream_t *s = state->output_stream;
523         audio_buf_t *b;
524         u_int shiftval = 0;
525         unsigned long flags;
526
527         DECLARE_WAITQUEUE(wait, current);
528
529         FN_IN;
530
531         if (!(file->f_mode & FMODE_WRITE) || !s->buffers || s->mapped) {
532                 FN_OUT(1);
533                 return 0;
534         }
535
536         /*
537          * Send current buffer if it contains data.  Be sure to send
538          * a full sample count.
539          */
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))
544                         return 0;
545                 /*
546                  * HACK ALERT !
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
550                  * it later.
551                  */
552                 
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.
556                  */
557                 if (!cpu_is_omap15xx() && AUDIO_QUEUE_EMPTY(s))
558                         omap_sound_dma_unlink_lch(s);
559
560                 shiftval = s->fragsize - b->offset;
561                 b->offset = shiftval;
562                 b->dma_addr -= shiftval;
563                 b->data -= shiftval;
564                 local_irq_save(flags);
565                 s->bytecount -= shiftval;
566                 if (++s->usr_head >= s->nbfrags)
567                         s->usr_head = 0;
568
569                 s->pending_frags++;
570                 audio_process_dma(s);
571                 local_irq_restore(flags);
572         }
573
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)) {
579                 schedule();
580                 set_current_state(TASK_INTERRUPTIBLE);
581         }
582         set_current_state(TASK_RUNNING);
583         remove_wait_queue(&s->wq, &wait);
584
585         /* undo the pointer hack above */
586         if (shiftval) {
587                 local_irq_save(flags);
588                 b->dma_addr += shiftval;
589                 b->data += shiftval;
590                 /* ensure sane DMA code behavior if not yet processed */
591                 if (b->offset != 0)
592                         b->offset = s->fragsize;
593                 local_irq_restore(flags);
594         }
595
596         FN_OUT(0);
597         return 0;
598 }
599
600 /***************************************************************************************
601  *
602  * Stop all the DMA channels of the stream
603  *
604  **************************************************************************************/
605 void audio_stop_dma(audio_stream_t * s)
606 {
607         int *chan = s->lch;
608         int i;
609         FN_IN;
610         if (unlikely(NULL == chan)) {
611                 BUG();
612                 return;
613         }
614         for (i = 0; i < nr_linked_channels; i++) {
615                 int cur_chan = chan[i];
616                 omap_stop_dma(cur_chan);
617         }
618         s->started = 0;
619         FN_OUT(0);
620         return;
621 }
622
623 /***************************************************************************************
624  *
625  * Get the dma posn
626  *
627  **************************************************************************************/
628 u_int audio_get_dma_pos(audio_stream_t * s)
629 {
630         audio_buf_t *b = &s->buffers[s->dma_tail];
631         u_int offset;
632
633         FN_IN;
634         if (b->dma_ref) {
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) {
639                 offset = b->offset;
640         } else {
641                 offset = 0;
642         }
643         FN_OUT(offset);
644         return offset;
645 }
646
647 /***************************************************************************************
648  *
649  * Reset the audio buffers
650  *
651  **************************************************************************************/
652 void audio_reset(audio_stream_t * s)
653 {
654         FN_IN;
655         if (s->buffers) {
656                 audio_stop_dma(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;
663         }
664         s->active = 0;
665         s->stopped = 0;
666         s->started = 0;
667         FN_OUT(0);
668         return;
669 }
670
671 /***************************************************************************************
672  *
673  * Clear any pending transfers
674  *
675  **************************************************************************************/
676 void omap_clear_sound_dma(audio_stream_t * s)
677 {
678         FN_IN;
679         omap_clear_dma(s->lch[s->dma_q_head]);
680         FN_OUT(0);
681         return;
682 }
683
684 /***************************************************************************************
685  *
686  * DMA related functions
687  *
688  **************************************************************************************/
689 static int audio_set_dma_params_play(int channel, dma_addr_t dma_ptr,
690                                      u_int dma_size)
691 {
692         int dt = 0x1;           /* data type 16 */
693         int cen = 32;           /* Stereo */
694         int cfn = dma_size / (2 * cen);
695         unsigned long dest_start;
696         int dest_port = 0;
697         int sync_dev = 0;
698
699         FN_IN;
700
701         if (cpu_is_omap15xx() || cpu_is_omap16xx()) {
702                 dest_start = AUDIO_MCBSP_DATAWRITE;
703                 dest_port = OMAP_DMA_PORT_MPUI;
704         }
705         if (cpu_is_omap24xx()) {
706                 dest_start = AUDIO_MCBSP_DATAWRITE;
707                 sync_dev = AUDIO_DMA_TX;
708         }
709
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);
713
714         FN_OUT(0);
715         return 0;
716 }
717
718 static int audio_set_dma_params_capture(int channel, dma_addr_t dma_ptr,
719                                         u_int dma_size)
720 {
721         int dt = 0x1;           /* data type 16 */
722         int cen = 16;           /* mono */
723         int cfn = dma_size / (2 * cen);
724         unsigned long src_start;
725         int src_port = 0;
726         int sync_dev = 0;
727         int src_sync = 0;
728
729         FN_IN;
730
731         if (cpu_is_omap15xx() || cpu_is_omap16xx()) {
732                 src_start = AUDIO_MCBSP_DATAREAD;
733                 src_port = OMAP_DMA_PORT_MPUI;
734         }
735         if (cpu_is_omap24xx()) {
736                 src_start = AUDIO_MCBSP_DATAREAD;
737                 sync_dev = AUDIO_DMA_RX;
738                 src_sync = 1;
739         }
740
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);
744
745         FN_OUT(0);
746         return 0;
747 }
748
749 static int audio_start_dma_chain(audio_stream_t * s)
750 {
751         int channel = s->lch[s->dma_q_head];
752         FN_IN;
753         if (!s->started) {
754                 s->hw_stop();           /* stops McBSP Interface */
755                 omap_start_dma(channel);
756                 s->started = 1;
757                 s->hw_start();          /* start McBSP interface */
758         }
759         /* else the dma itself will progress forward with out our help */
760         FN_OUT(0);
761         return 0;
762 }
763
764 /* Start DMA -
765  * Do the initial set of work to initialize all the channels as required.
766  * We shall then initate a transfer
767  */
768 static int omap_start_sound_dma(audio_stream_t * s, dma_addr_t dma_ptr,
769                                 u_int dma_size)
770 {
771         int ret = -EPERM;
772
773         FN_IN;
774         if (unlikely(dma_size > MAX_DMA_SIZE)) {
775                 ERR("DmaSoundDma: Start: overflowed %d-%d\n", dma_size,
776                     MAX_DMA_SIZE);
777                 return -EOVERFLOW;
778         }
779
780         if (AUDIO_QUEUE_FULL(s)) {
781                 ret = -2;
782                 goto sound_out;
783         }
784         
785         if (s->input_or_output == FMODE_WRITE)
786                 /*playback */
787         {
788                 ret =
789                     audio_set_dma_params_play(s->lch[s->dma_q_tail], dma_ptr,
790                                               dma_size);
791         } else {
792                 ret =
793                     audio_set_dma_params_capture(s->lch[s->dma_q_tail], dma_ptr,
794                                                  dma_size);
795         }
796         if (ret != 0) {
797                 ret = -2;       /* indicate queue full */
798                 goto sound_out;
799         }
800         AUDIO_INCREMENT_TAIL(s);
801         ret = audio_start_dma_chain(s);
802         if (ret) {
803                 ERR("dma start failed");
804         }
805       sound_out:
806         FN_OUT(ret);
807         return ret;
808
809 }
810
811 /***************************************************************************************
812  *
813  * ISR related functions
814  *
815  **************************************************************************************/
816 /* The work item handler */
817 static void audio_dsr_handler(unsigned long inData)
818 {
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;
824
825         FN_IN;
826         DPRINTK("lch=%d,status=0x%x, data=%p as=%p\n", sound_curr_lch,
827                 ch_status, data, s);
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,
833                     s->dma_tail);
834                 FN_OUT(-1);
835                 return;
836         }
837
838         AUDIO_INCREMENT_HEAD(s);        /* Empty the queue */
839
840         /* Try to fill again */
841         audio_dma_callback(sound_curr_lch, ch_status, s);
842         FN_OUT(0);
843
844 }
845
846 /* Macro to trace the IRQ calls - checks for multi-channel irqs */
847 //#define IRQ_TRACE
848 #ifdef IRQ_TRACE
849 #define MAX_UP 10
850 static char xyz[MAX_UP] = { 0 };
851 static int h = 0;
852 #endif
853
854 /* ISRs have to be short and smart.. So we transfer every heavy duty stuff to the 
855  * work item
856  */
857 static void sound_dma_irq_handler(int sound_curr_lch, u16 ch_status, void *data)
858 {
859         int dma_status = ch_status;
860         audio_stream_t *s = (audio_stream_t *) data;
861         FN_IN;
862 #ifdef IRQ_TRACE
863         xyz[h++] = '0' + sound_curr_lch;
864         if (h == MAX_UP - 1) {
865                 printk("%s-", xyz);
866                 h = 0;
867         }
868 #endif
869         DPRINTK("lch=%d,status=0x%x, dma_status=%d, data=%p\n", sound_curr_lch,
870                 ch_status, dma_status, data);
871
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");
876                 FN_OUT(-1);
877                 return;
878         }
879
880         if (AUDIO_QUEUE_LAST(s))
881                 audio_stop_dma(s);
882
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;
887                 work1.s = s;
888                 /* schedule tasklet 1 */
889                 tasklet_schedule(&audio_isr_work1);
890                 work_item_running = 1;
891         } else {
892                 work2.current_lch = sound_curr_lch;
893                 work2.ch_status = ch_status;
894                 work2.s = s;
895                 /* schedule tasklet 2 */
896                 tasklet_schedule(&audio_isr_work2);
897                 work_item_running = 0;
898         }
899         FN_OUT(0);
900         return;
901 }
902
903 /* The call back that handles buffer stuff */
904 static void audio_dma_callback(int lch, u16 ch_status, void *data)
905 {
906         audio_stream_t *s = data;
907         audio_buf_t *b = &s->buffers[s->dma_tail];
908         FN_IN;
909
910         if (s->dma_spinref > 0) {
911                 s->dma_spinref--;
912         } else if (!s->buffers) {
913                 printk(KERN_CRIT
914                        "omap_audio: received DMA IRQ for non existent buffers!\n");
915                 return;
916         } else if (b->dma_ref && --b->dma_ref == 0 && b->offset >= s->fragsize) {
917                 /* This fragment is done */
918                 b->offset = 0;
919                 s->bytecount += s->fragsize;
920                 s->fragcount++;
921                 s->dma_spinref = -s->dma_spinref;
922
923                 if (++s->dma_tail >= s->nbfrags)
924                         s->dma_tail = 0;
925
926                 if (!s->mapped)
927                         complete(&s->wfc);
928                 else
929                         s->pending_frags++;
930
931                 wake_up(&s->wq);
932         }
933
934         audio_process_dma(s);
935         
936         FN_OUT(0);
937         return;
938 }
939
940 /*********************************************************************************
941  *
942  * audio_get_dma_callback(): return the dma interface call back function
943  *
944  *********************************************************************************/
945 dma_callback_t audio_get_dma_callback(void)
946 {
947         FN_IN;
948         FN_OUT(0);
949         return audio_dma_callback;
950 }
951
952 static int __init audio_dma_init(void)
953 {
954         if (!cpu_is_omap15xx())
955                 nr_linked_channels = 2;
956
957         return 0;
958 }
959
960 static void __exit audio_dma_exit(void)
961 {
962         /* Nothing */
963 }
964
965 module_init(audio_dma_init);
966 module_exit(audio_dma_exit);
967
968 MODULE_AUTHOR("Texas Instruments");
969 MODULE_DESCRIPTION("Common DMA handling for Audio driver on OMAP processors");
970 MODULE_LICENSE("GPL");
971
972 EXPORT_SYMBOL(omap_clear_sound_dma);
973 EXPORT_SYMBOL(omap_request_sound_dma);
974 EXPORT_SYMBOL(omap_free_sound_dma);
975
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);