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