]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - sound/oss/omap-audio-dma-intfc.c
9f3451731c7106e1e9c9a8966025f0d50baa46c4
[linux-2.6-omap-h63xx.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
33 #include <linux/config.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/types.h>
37 #include <linux/fs.h>
38 #include <linux/mm.h>
39 #include <linux/slab.h>
40 #include <linux/sched.h>
41 #include <linux/poll.h>
42 #include <linux/pm.h>
43 #include <linux/errno.h>
44 #include <linux/sound.h>
45 #include <linux/soundcard.h>
46 #include <linux/sysrq.h>
47 #include <linux/interrupt.h>
48 #include <linux/dma-mapping.h>
49 #include <linux/completion.h>
50
51 #include <asm/uaccess.h>
52 #include <asm/io.h>
53 #include <asm/hardware.h>
54 #include <asm/semaphore.h>
55
56 #include <asm/arch/dma.h>
57 #include "omap-audio-dma-intfc.h"
58
59 #include <asm/arch/mcbsp.h>
60
61 #include "omap-audio.h"
62
63 #undef DEBUG
64 //#define DEBUG
65 #ifdef DEBUG
66 #define DPRINTK(ARGS...)  printk(KERN_INFO "<%s>: ",__FUNCTION__);printk(ARGS)
67 #define FN_IN printk(KERN_INFO "[%s]: start\n", __FUNCTION__)
68 #define FN_OUT(n) printk(KERN_INFO "[%s]: end(%u)\n",__FUNCTION__, n)
69 #else
70
71 #define DPRINTK( x... )
72 #define FN_IN
73 #define FN_OUT(x)
74 #endif
75
76 #define ERR(ARGS...) printk(KERN_ERR "{%s}-ERROR: ", __FUNCTION__);printk(ARGS);
77
78 #define AUDIO_NAME              "omap-audio"
79 #define AUDIO_NBFRAGS_DEFAULT   8
80 #define AUDIO_FRAGSIZE_DEFAULT  8192
81
82 #define AUDIO_ACTIVE(state)     ((state)->rd_ref || (state)->wr_ref)
83
84 #define SPIN_ADDR               (dma_addr_t)0
85 #define SPIN_SIZE               2048
86
87 /* Channel Queue Handling macros
88  * tail always points to the current free entry
89  * Head always points to the current entry being used
90  * end is either head or tail
91  */
92
93 #define AUDIO_QUEUE_INIT(s) s->dma_q_head = s->dma_q_tail = s->dma_q_count = 0;
94 #define AUDIO_QUEUE_FULL(s) (nr_linked_channels == s->dma_q_count)
95 #define AUDIO_QUEUE_LAST(s) (1 == s->dma_q_count)
96 #define AUDIO_QUEUE_EMPTY(s) (0 == s->dma_q_count)
97 #define __AUDIO_INCREMENT_QUEUE(end) ((end)=((end)+1) % nr_linked_channels)
98 #define AUDIO_INCREMENT_HEAD(s) __AUDIO_INCREMENT_QUEUE(s->dma_q_head); s->dma_q_count--;
99 #define AUDIO_INCREMENT_TAIL(s) __AUDIO_INCREMENT_QUEUE(s->dma_q_tail); s->dma_q_count++;
100
101 /* DMA buffer fragmentation sizes */
102 #define MAX_DMA_SIZE             0x1000000
103 #define CUT_DMA_SIZE             0x1000
104 /* TODO: To be moved to more appropriate location */
105 #define DCSR_ERROR           0x3
106 #define DCSR_SYNC_SET        (1 << 6)
107
108 #define DCCR_FS              (1 << 5)
109 #define DCCR_PRIO            (1 << 6)
110 #define DCCR_EN              (1 << 7)
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_omap1510())
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_omap1510())
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_omap1510() && 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 /*********************************** MODULE FUNCTIONS DEFINTIONS ***********************/
685
686 #ifdef OMAP1610_MCBSP1_BASE
687 #undef OMAP1610_MCBSP1_BASE
688 #endif
689 #define OMAP1610_MCBSP1_BASE    0xE1011000
690
691 /***************************************************************************************
692  *
693  * DMA related functions
694  *
695  **************************************************************************************/
696 static int audio_set_dma_params_play(int channel, dma_addr_t dma_ptr,
697                                      u_int dma_size)
698 {
699         int dt = 0x1;           /* data type 16 */
700         int cen = 32;           /* Stereo */
701         int cfn = dma_size / (2 * cen);
702         FN_IN;
703         omap_set_dma_dest_params(channel, 0x05, 0x00,
704                                  (OMAP1610_MCBSP1_BASE + 0x806),
705                                  0, 0);
706         omap_set_dma_src_params(channel, 0x00, 0x01, dma_ptr, 0, 0);
707         omap_set_dma_transfer_params(channel, dt, cen, cfn, 0x00, 0, 0);
708         FN_OUT(0);
709         return 0;
710 }
711
712 static int audio_set_dma_params_capture(int channel, dma_addr_t dma_ptr,
713                                         u_int dma_size)
714 {
715         int dt = 0x1;           /* data type 16 */
716         int cen = 16;           /* mono */
717         int cfn = dma_size / (2 * cen);
718         FN_IN;
719         omap_set_dma_src_params(channel, 0x05, 0x00,
720                                 (OMAP1610_MCBSP1_BASE + 0x802),
721                                 0, 0);
722         omap_set_dma_dest_params(channel, 0x00, 0x01, dma_ptr, 0, 0);
723         omap_set_dma_transfer_params(channel, dt, cen, cfn, 0x00, 0, 0);
724         FN_OUT(0);
725         return 0;
726 }
727
728 static int audio_start_dma_chain(audio_stream_t * s)
729 {
730         int channel = s->lch[s->dma_q_head];
731         FN_IN;
732         if (!s->started) {
733                 omap_start_dma(channel);
734                 s->started = 1;
735         }
736         /* else the dma itself will progress forward with out our help */
737         FN_OUT(0);
738         return 0;
739 }
740
741 /* Start DMA -
742  * Do the initial set of work to initialize all the channels as required.
743  * We shall then initate a transfer
744  */
745 static int omap_start_sound_dma(audio_stream_t * s, dma_addr_t dma_ptr,
746                                 u_int dma_size)
747 {
748         int ret = -EPERM;
749
750         FN_IN;
751         if (unlikely(dma_size > MAX_DMA_SIZE)) {
752                 ERR("DmaSoundDma: Start: overflowed %d-%d\n", dma_size,
753                     MAX_DMA_SIZE);
754                 return -EOVERFLOW;
755         }
756
757         if (AUDIO_QUEUE_FULL(s)) {
758                 ret = -2;
759                 goto sound_out;
760         }
761         
762         if (s->input_or_output == FMODE_WRITE)
763                 /*playback */
764         {
765                 ret =
766                     audio_set_dma_params_play(s->lch[s->dma_q_tail], dma_ptr,
767                                               dma_size);
768         } else {
769                 ret =
770                     audio_set_dma_params_capture(s->lch[s->dma_q_tail], dma_ptr,
771                                                  dma_size);
772         }
773         if (ret != 0) {
774                 ret = -2;       /* indicate queue full */
775                 goto sound_out;
776         }
777         AUDIO_INCREMENT_TAIL(s);
778         ret = audio_start_dma_chain(s);
779         if (ret) {
780                 ERR("dma start failed");
781         }
782       sound_out:
783         FN_OUT(ret);
784         return ret;
785
786 }
787
788 /***************************************************************************************
789  *
790  * ISR related functions
791  *
792  **************************************************************************************/
793 /* The work item handler */
794 static void audio_dsr_handler(unsigned long inData)
795 {
796         void *data = (void *)inData;
797         struct audio_isr_work_item *work = data;
798         audio_stream_t *s = (work->s);
799         int sound_curr_lch = work->current_lch;
800         u16 ch_status = work->ch_status;
801
802         FN_IN;
803         DPRINTK("lch=%d,status=0x%x, data=%p as=%p\n", sound_curr_lch,
804                 ch_status, data, s);
805         if (AUDIO_QUEUE_EMPTY(s)) {
806                 ERR("Interrupt(%d)  for empty queue(h=%d, T=%d)???\n",
807                     sound_curr_lch, s->dma_q_head, s->dma_q_tail);
808                 ERR("nbfrag=%d,pendfrags=%d,USR-H=%d, QH-%d QT-%d\n",
809                     s->nbfrags, s->pending_frags, s->usr_head, s->dma_head,
810                     s->dma_tail);
811                 FN_OUT(-1);
812                 return;
813         }
814
815         AUDIO_INCREMENT_HEAD(s);        /* Empty the queue */
816
817         /* Try to fill again */
818         audio_dma_callback(sound_curr_lch, ch_status, s);
819         FN_OUT(0);
820
821 }
822
823 /* Macro to trace the IRQ calls - checks for multi-channel irqs */
824 //#define IRQ_TRACE
825 #ifdef IRQ_TRACE
826 #define MAX_UP 10
827 static char xyz[MAX_UP] = { 0 };
828 static int h = 0;
829 #endif
830
831 /* ISRs have to be short and smart.. So we transfer every heavy duty stuff to the 
832  * work item
833  */
834 static void sound_dma_irq_handler(int sound_curr_lch, u16 ch_status, void *data)
835 {
836         int dma_status = ch_status;
837         audio_stream_t *s = (audio_stream_t *) data;
838         FN_IN;
839 #ifdef IRQ_TRACE
840         xyz[h++] = '0' + sound_curr_lch;
841         if (h == MAX_UP - 1) {
842                 printk("%s-", xyz);
843                 h = 0;
844         }
845 #endif
846         DPRINTK("lch=%d,status=0x%x, dma_status=%d, data=%p\n", sound_curr_lch,
847                 ch_status, dma_status, data);
848
849         if (dma_status & (DCSR_ERROR)) {
850                 OMAP_DMA_CCR_REG(sound_curr_lch) &= ~DCCR_EN;
851                 ERR("DCSR_ERROR!\n");
852                 FN_OUT(-1);
853                 return;
854         }
855
856         if (AUDIO_QUEUE_LAST(s))
857                 audio_stop_dma(s);
858
859         /* Start the work item  - we ping pong the work items */
860         if (!work_item_running) {
861                 work1.current_lch = sound_curr_lch;
862                 work1.ch_status = ch_status;
863                 work1.s = s;
864                 /* schedule tasklet 1 */
865                 tasklet_schedule(&audio_isr_work1);
866                 work_item_running = 1;
867         } else {
868                 work2.current_lch = sound_curr_lch;
869                 work2.ch_status = ch_status;
870                 work2.s = s;
871                 /* schedule tasklet 2 */
872                 tasklet_schedule(&audio_isr_work2);
873                 work_item_running = 0;
874         }
875         FN_OUT(0);
876         return;
877 }
878
879 /* The call back that handles buffer stuff */
880 static void audio_dma_callback(int lch, u16 ch_status, void *data)
881 {
882         audio_stream_t *s = data;
883         audio_buf_t *b = &s->buffers[s->dma_tail];
884         FN_IN;
885
886         if (s->dma_spinref > 0) {
887                 s->dma_spinref--;
888         } else if (!s->buffers) {
889                 printk(KERN_CRIT
890                        "omap_audio: received DMA IRQ for non existent buffers!\n");
891                 return;
892         } else if (b->dma_ref && --b->dma_ref == 0 && b->offset >= s->fragsize) {
893                 /* This fragment is done */
894                 b->offset = 0;
895                 s->bytecount += s->fragsize;
896                 s->fragcount++;
897                 s->dma_spinref = -s->dma_spinref;
898
899                 if (++s->dma_tail >= s->nbfrags)
900                         s->dma_tail = 0;
901
902                 if (!s->mapped)
903                         complete(&s->wfc);
904                 else
905                         s->pending_frags++;
906
907                 wake_up(&s->wq);
908         }
909
910         audio_process_dma(s);
911         
912         FN_OUT(0);
913         return;
914 }
915
916 /*********************************************************************************
917  *
918  * audio_get_dma_callback(): return the dma interface call back function
919  *
920  *********************************************************************************/
921 dma_callback_t audio_get_dma_callback(void)
922 {
923         FN_IN;
924         FN_OUT(0);
925         return audio_dma_callback;
926 }
927
928 static int __init audio_dma_init(void)
929 {
930         if (!cpu_is_omap1510())
931                 nr_linked_channels = 2;
932
933         return 0;
934 }
935
936 static void __exit audio_dma_exit(void)
937 {
938         /* Nothing */
939 }
940
941 module_init(audio_dma_init);
942 module_exit(audio_dma_exit);
943
944 MODULE_AUTHOR("Texas Instruments");
945 MODULE_DESCRIPTION("Common DMA handling for Audio driver on OMAP processors");
946 MODULE_LICENSE("GPL");
947
948 EXPORT_SYMBOL(omap_clear_sound_dma);
949 EXPORT_SYMBOL(omap_request_sound_dma);
950 EXPORT_SYMBOL(omap_free_sound_dma);
951
952 EXPORT_SYMBOL(audio_get_dma_callback);
953 EXPORT_SYMBOL(audio_setup_buf);
954 EXPORT_SYMBOL(audio_process_dma);
955 EXPORT_SYMBOL(audio_prime_rx);
956 EXPORT_SYMBOL(audio_set_fragments);
957 EXPORT_SYMBOL(audio_sync);
958 EXPORT_SYMBOL(audio_stop_dma);
959 EXPORT_SYMBOL(audio_get_dma_pos);
960 EXPORT_SYMBOL(audio_reset);
961 EXPORT_SYMBOL(audio_discard_buf);