]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - sound/arm/omap-alsa-dma.c
8530acf7694d9f87c0b8917626f3e825289d8ae8
[linux-2.6-omap-h63xx.git] / sound / arm / omap-alsa-dma.c
1 /*
2  * sound/arm/omap-alsa-dma.c
3  *
4  * Common audio DMA handling for the OMAP processors
5  *
6  * Copyright (C) 2005 Instituto Nokia de Tecnologia - INdT - Manaus Brazil
7  * 
8  * Copyright (C) 2004 Texas Instruments, Inc.
9  *
10  * Copyright (C) 2000, 2001 Nicolas Pitre <nico@cam.org>
11  *
12  * This package is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  *
16  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * History:
21  *
22  * 2004-06-07   Sriram Kannan   - Created new file from omap_audio_dma_intfc.c. This file
23  *                                will contain only the DMA interface and buffer handling of OMAP
24  *                                audio driver.
25  *
26  * 2004-06-22   Sriram Kannan   - removed legacy code (auto-init). Self-linking of DMA logical channel.
27  *
28  * 2004-08-12   Nishanth Menon  - Modified to integrate Audio requirements on 1610,1710 platforms
29  *
30  * 2004-11-01   Nishanth Menon  - 16xx platform code base modified to support multi channel chaining.
31  *
32  * 2004-12-15   Nishanth Menon  - Improved 16xx platform channel logic introduced - tasklets, queue handling updated
33  * 
34  * 2005-07-19   INdT Kernel Team - Alsa port. Creation of new file omap-alsa-dma.c based in
35  *                                 omap-audio-dma-intfc.c oss file. Support for aic23 codec.
36  *                                 Removal of buffer handling (Alsa does that), modifications
37  *      in dma handling and port to alsa structures.
38  *
39  * 2005-12-18   Dirk Behme      - Added L/R Channel Interchange fix as proposed by Ajaya Babu
40  */
41
42 #include <linux/config.h>
43 #include <linux/module.h>
44 #include <linux/init.h>
45 #include <linux/types.h>
46 #include <linux/fs.h>
47 #include <linux/mm.h>
48 #include <linux/slab.h>
49 #include <linux/sched.h>
50 #include <linux/poll.h>
51 #include <linux/pm.h>
52 #include <linux/errno.h>
53 #include <linux/sound.h>
54 #include <linux/soundcard.h>
55 #include <linux/sysrq.h>
56 #include <linux/interrupt.h>
57 #include <linux/dma-mapping.h>
58
59 #include <asm/uaccess.h>
60 #include <asm/io.h>
61 #include <asm/hardware.h>
62 #include <asm/semaphore.h>
63
64 #include <asm/arch/dma.h>
65 #include "omap-alsa-dma.h"
66
67 #include <asm/arch/mcbsp.h>
68
69 #include "omap-aic23.h"
70
71 #undef DEBUG
72 //#define DEBUG
73 #ifdef DEBUG
74 #define DPRINTK(ARGS...)  printk(KERN_INFO "<%s>: ",__FUNCTION__);printk(ARGS)
75 #define FN_IN printk(KERN_INFO "[%s]: start\n", __FUNCTION__)
76 #define FN_OUT(n) printk(KERN_INFO "[%s]: end(%u)\n",__FUNCTION__, n)
77 #else
78
79 #define DPRINTK( x... )
80 #define FN_IN
81 #define FN_OUT(x)
82 #endif
83
84 #define ERR(ARGS...) printk(KERN_ERR "{%s}-ERROR: ", __FUNCTION__);printk(ARGS);
85
86 /* Channel Queue Handling macros
87  * tail always points to the current free entry
88  * Head always points to the current entry being used
89  * end is either head or tail
90  */
91
92 #define AUDIO_QUEUE_INIT(s) s->dma_q_head = s->dma_q_tail = s->dma_q_count = 0;
93 #define AUDIO_QUEUE_FULL(s) (nr_linked_channels == s->dma_q_count)
94 #define AUDIO_QUEUE_LAST(s) (1 == s->dma_q_count)
95 #define AUDIO_QUEUE_EMPTY(s) (0 == s->dma_q_count)
96 #define __AUDIO_INCREMENT_QUEUE(end) ((end)=((end)+1) % nr_linked_channels)
97 #define AUDIO_INCREMENT_HEAD(s) __AUDIO_INCREMENT_QUEUE(s->dma_q_head); s->dma_q_count--;
98 #define AUDIO_INCREMENT_TAIL(s) __AUDIO_INCREMENT_QUEUE(s->dma_q_tail); s->dma_q_count++;
99
100 /* DMA buffer fragmentation sizes */
101 #define MAX_DMA_SIZE             0x1000000 /* todo: sync with alsa */
102 //#define CUT_DMA_SIZE           0x1000
103 /* TODO: To be moved to more appropriate location */
104 #define DCSR_ERROR           0x3
105 #define DCSR_END_BLOCK       (1 << 5)
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 static char nr_linked_channels = 1;
130
131 /*********************************** MODULE SPECIFIC FUNCTIONS ***********************/
132
133 static void sound_dma_irq_handler(int lch, u16 ch_status, void *data);
134 static int audio_set_dma_params_play(int channel, dma_addr_t dma_ptr,
135                                      u_int dma_size);
136 static int audio_set_dma_params_capture(int channel, dma_addr_t dma_ptr,
137                                         u_int dma_size);
138 static int audio_start_dma_chain(struct audio_stream * s);
139
140 /***************************************************************************************
141  *
142  * DMA channel requests
143  *
144  **************************************************************************************/
145 static void omap_sound_dma_link_lch(void *data)
146 {
147
148         struct audio_stream *s = (struct audio_stream *) data;
149         int *chan = s->lch;
150         int i;
151
152         FN_IN;
153         if (s->linked) {
154                 FN_OUT(1);
155                 return;
156         }
157         for (i = 0; i < nr_linked_channels; i++) {
158                 int cur_chan = chan[i];
159                 int nex_chan =
160                     ((nr_linked_channels - 1 ==
161                       i) ? chan[0] : chan[i + 1]);
162                 omap_dma_link_lch(cur_chan, nex_chan);
163         }
164         s->linked = 1;
165         FN_OUT(0);
166 }
167
168 int omap_request_sound_dma(int device_id, const char *device_name,
169                            void *data, int **channels)
170 {
171         int i, err = 0;
172         int *chan = NULL;
173         FN_IN;
174         if (unlikely((NULL == channels) || (NULL == device_name))) {
175                 BUG();
176                 return -EPERM;
177         }
178         /* Try allocate memory for the num channels */
179         *channels =
180             (int *) kmalloc(sizeof(int) * nr_linked_channels, GFP_KERNEL);
181         chan = *channels;
182         if (NULL == chan) {
183                 ERR("No Memory for channel allocs!\n");
184                 FN_OUT(-ENOMEM);
185                 return -ENOMEM;
186         }
187         spin_lock(&dma_list_lock);
188         for (i = 0; i < nr_linked_channels; i++) {
189                 err =
190                     omap_request_dma(device_id, device_name,
191                                      sound_dma_irq_handler, data,
192                                      &chan[i]);
193
194                 /* Handle Failure condition here */
195                 if (err < 0) {
196                         int j;
197                         for (j = 0; j < i; j++) {
198                                 omap_free_dma(chan[j]);
199                         }
200                         spin_unlock(&dma_list_lock);
201                         kfree(chan);
202                         *channels = NULL;
203                         ERR("Error in requesting channel %d=0x%x\n", i,
204                             err);
205                         FN_OUT(err);
206                         return err;
207                 }
208         }
209
210         /* Chain the channels together */
211         if (!cpu_is_omap1510())
212                 omap_sound_dma_link_lch(data);
213
214         spin_unlock(&dma_list_lock);
215         FN_OUT(0);
216         return 0;
217 }
218
219 /***************************************************************************************
220  *
221  * DMA channel requests Freeing
222  *
223  **************************************************************************************/
224 static void omap_sound_dma_unlink_lch(void *data)
225 {
226         struct audio_stream *s = (struct audio_stream *) data;
227         int *chan = s->lch;
228         int i;
229
230         FN_IN;
231         if (!s->linked) {
232                 FN_OUT(1);
233                 return;
234         }
235         for (i = 0; i < nr_linked_channels; i++) {
236                 int cur_chan = chan[i];
237                 int nex_chan =
238                     ((nr_linked_channels - 1 ==
239                       i) ? chan[0] : chan[i + 1]);
240                 omap_dma_unlink_lch(cur_chan, nex_chan);
241         }
242         s->linked = 0;
243         FN_OUT(0);
244 }
245
246 int omap_free_sound_dma(void *data, int **channels)
247 {
248
249         int i;
250         int *chan = NULL;
251         FN_IN;
252         if (unlikely(NULL == channels)) {
253                 BUG();
254                 return -EPERM;
255         }
256         if (unlikely(NULL == *channels)) {
257                 BUG();
258                 return -EPERM;
259         }
260         chan = (*channels);
261
262         if (!cpu_is_omap1510())
263                 omap_sound_dma_unlink_lch(data);
264         for (i = 0; i < nr_linked_channels; i++) {
265                 int cur_chan = chan[i];
266                 omap_stop_dma(cur_chan);
267                 omap_free_dma(cur_chan);
268         }
269         kfree(*channels);
270         *channels = NULL;
271         FN_OUT(0);
272         return 0;
273 }
274
275 /***************************************************************************************
276  *
277  * Stop all the DMA channels of the stream
278  *
279  **************************************************************************************/
280 void omap_audio_stop_dma(struct audio_stream *s)
281 {
282         int *chan = s->lch;
283         int i;
284         FN_IN;
285         if (unlikely(NULL == chan)) {
286                 BUG();
287                 return;
288         }
289         for (i = 0; i < nr_linked_channels; i++) {
290                 int cur_chan = chan[i];
291                 omap_stop_dma(cur_chan);
292         }
293         s->started = 0;
294         FN_OUT(0);
295         return;
296 }
297 /***************************************************************************************
298  *
299  * Clear any pending transfers
300  *
301  **************************************************************************************/
302 void omap_clear_sound_dma(struct audio_stream * s)
303 {
304         FN_IN;
305         omap_clear_dma(s->lch[s->dma_q_head]);
306         FN_OUT(0);
307         return;
308 }
309
310 /*********************************** MODULE FUNCTIONS DEFINTIONS ***********************/
311
312 #ifdef OMAP1610_MCBSP1_BASE
313 #undef OMAP1610_MCBSP1_BASE
314 #endif
315 #define OMAP1610_MCBSP1_BASE    0xE1011000
316
317 /***************************************************************************************
318  *
319  * DMA related functions
320  *
321  **************************************************************************************/
322 static int audio_set_dma_params_play(int channel, dma_addr_t dma_ptr,
323                                      u_int dma_size)
324 {
325         int dt = 0x1;           /* data type 16 */
326         int cen = 32;           /* Stereo */
327         int cfn = dma_size / (2 * cen);
328         FN_IN;
329         omap_set_dma_dest_params(channel, 0x05, 0x00,
330                                  (OMAP1610_MCBSP1_BASE + 0x806),
331                                  0, 0);
332         omap_set_dma_src_params(channel, 0x00, 0x01, dma_ptr,
333                                 0, 0);
334         omap_set_dma_transfer_params(channel, dt, cen, cfn, 0x00, 0, 0);
335         FN_OUT(0);
336         return 0;
337 }
338
339 static int audio_set_dma_params_capture(int channel, dma_addr_t dma_ptr,
340                                         u_int dma_size)
341 {
342         int dt = 0x1;           /* data type 16 */
343         int cen = 32;           /* stereo */
344         
345         int cfn = dma_size / (2 * cen);
346         FN_IN;
347         omap_set_dma_src_params(channel, 0x05, 0x00,
348                                 (OMAP1610_MCBSP1_BASE + 0x802),
349                                 0, 0);
350         omap_set_dma_dest_params(channel, 0x00, 0x01, dma_ptr, 0, 0);
351         omap_set_dma_transfer_params(channel, dt, cen, cfn, 0x00, 0, 0);
352         FN_OUT(0);
353         return 0;
354 }
355
356 static int audio_start_dma_chain(struct audio_stream *s)
357 {
358         int channel = s->lch[s->dma_q_head];
359         FN_IN;
360         if (!s->started) {
361          s->hw_stop();      /* stops McBSP Interface */
362                 omap_start_dma(channel);
363                 s->started = 1;
364                 s->hw_start();     /* start McBSP interface */
365         }
366         /* else the dma itself will progress forward with out our help */
367         FN_OUT(0);
368         return 0;
369 }
370
371 /* Start DMA -
372  * Do the initial set of work to initialize all the channels as required.
373  * We shall then initate a transfer
374  */
375 int omap_start_sound_dma(struct audio_stream *s, dma_addr_t dma_ptr,
376                          u_int dma_size)
377 {
378         int ret = -EPERM;
379
380         FN_IN;
381
382         if (unlikely(dma_size > MAX_DMA_SIZE)) {
383                 ERR("DmaSoundDma: Start: overflowed %d-%d\n", dma_size,
384                     MAX_DMA_SIZE);
385                 return -EOVERFLOW;
386         }
387         //if (AUDIO_QUEUE_FULL(s)) {
388         //      ret = -2;
389         //      goto sound_out;
390         //}
391
392         if (s->stream_id == SNDRV_PCM_STREAM_PLAYBACK) {
393                 /*playback */
394                 ret =
395                     audio_set_dma_params_play(s->lch[s->dma_q_tail],
396                                               dma_ptr, dma_size);
397         } else {
398                 ret =
399                     audio_set_dma_params_capture(s->lch[s->dma_q_tail],
400                                                  dma_ptr, dma_size);
401         }
402         if (ret != 0) {
403                 ret = -3;       /* indicate queue full */
404                 goto sound_out;
405         }
406         AUDIO_INCREMENT_TAIL(s);
407         ret = audio_start_dma_chain(s);
408         if (ret) {
409                 ERR("dma start failed");
410         }
411       sound_out:
412         FN_OUT(ret);
413         return ret;
414
415 }
416
417 /* 
418  * ISRs have to be short and smart.. 
419  * Here we call alsa handling, after some error checking
420  */
421 static void sound_dma_irq_handler(int sound_curr_lch, u16 ch_status,
422                                   void *data)
423 {
424         int dma_status = ch_status;
425         struct audio_stream *s = (struct audio_stream *) data;
426         FN_IN;
427
428         /*
429          * some register checkings
430          */ 
431         DPRINTK("lch=%d,status=0x%x, dma_status=%d, data=%p\n",
432                 sound_curr_lch, ch_status, dma_status, data);
433
434         if (dma_status & (DCSR_ERROR)) {
435                 OMAP_DMA_CCR_REG(sound_curr_lch) &= ~DCCR_EN;
436                 ERR("DCSR_ERROR!\n");
437                 FN_OUT(-1);
438                 return;
439         }
440
441         if (ch_status & DCSR_END_BLOCK) 
442                 audio_dma_callback(s);
443         FN_OUT(0);
444         return;
445 }
446
447 MODULE_AUTHOR("Texas Instruments");
448 MODULE_DESCRIPTION
449     ("Common DMA handling for Audio driver on OMAP processors");
450 MODULE_LICENSE("GPL");
451
452 EXPORT_SYMBOL(omap_start_sound_dma);
453 EXPORT_SYMBOL(omap_clear_sound_dma);
454 EXPORT_SYMBOL(omap_request_sound_dma);
455 EXPORT_SYMBOL(omap_free_sound_dma);
456 EXPORT_SYMBOL(omap_audio_stop_dma);