Merge branch 'irq-threaded-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / sound / soc / omap / omap-pcm.c
1 /*
2  * omap-pcm.c  --  ALSA PCM interface for the OMAP SoC
3  *
4  * Copyright (C) 2008 Nokia Corporation
5  *
6  * Contact: Jarkko Nikula <jhnikula@gmail.com>
7  *          Peter Ujfalusi <peter.ujfalusi@nokia.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 #include <linux/dma-mapping.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30
31 #include <mach/dma.h>
32 #include "omap-pcm.h"
33
34 static const struct snd_pcm_hardware omap_pcm_hardware = {
35         .info                   = SNDRV_PCM_INFO_MMAP |
36                                   SNDRV_PCM_INFO_MMAP_VALID |
37                                   SNDRV_PCM_INFO_INTERLEAVED |
38                                   SNDRV_PCM_INFO_PAUSE |
39                                   SNDRV_PCM_INFO_RESUME,
40         .formats                = SNDRV_PCM_FMTBIT_S16_LE,
41         .period_bytes_min       = 32,
42         .period_bytes_max       = 64 * 1024,
43         .periods_min            = 2,
44         .periods_max            = 255,
45         .buffer_bytes_max       = 128 * 1024,
46 };
47
48 struct omap_runtime_data {
49         spinlock_t                      lock;
50         struct omap_pcm_dma_data        *dma_data;
51         int                             dma_ch;
52         int                             period_index;
53 };
54
55 static void omap_pcm_dma_irq(int ch, u16 stat, void *data)
56 {
57         struct snd_pcm_substream *substream = data;
58         struct snd_pcm_runtime *runtime = substream->runtime;
59         struct omap_runtime_data *prtd = runtime->private_data;
60         unsigned long flags;
61
62         if ((cpu_is_omap1510()) &&
63                         (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)) {
64                 /*
65                  * OMAP1510 doesn't fully support DMA progress counter
66                  * and there is no software emulation implemented yet,
67                  * so have to maintain our own playback progress counter
68                  * that can be used by omap_pcm_pointer() instead.
69                  */
70                 spin_lock_irqsave(&prtd->lock, flags);
71                 if ((stat == OMAP_DMA_LAST_IRQ) &&
72                                 (prtd->period_index == runtime->periods - 1)) {
73                         /* we are in sync, do nothing */
74                         spin_unlock_irqrestore(&prtd->lock, flags);
75                         return;
76                 }
77                 if (prtd->period_index >= 0) {
78                         if (stat & OMAP_DMA_BLOCK_IRQ) {
79                                 /* end of buffer reached, loop back */
80                                 prtd->period_index = 0;
81                         } else if (stat & OMAP_DMA_LAST_IRQ) {
82                                 /* update the counter for the last period */
83                                 prtd->period_index = runtime->periods - 1;
84                         } else if (++prtd->period_index >= runtime->periods) {
85                                 /* end of buffer missed? loop back */
86                                 prtd->period_index = 0;
87                         }
88                 }
89                 spin_unlock_irqrestore(&prtd->lock, flags);
90         }
91
92         snd_pcm_period_elapsed(substream);
93 }
94
95 /* this may get called several times by oss emulation */
96 static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
97                               struct snd_pcm_hw_params *params)
98 {
99         struct snd_pcm_runtime *runtime = substream->runtime;
100         struct snd_soc_pcm_runtime *rtd = substream->private_data;
101         struct omap_runtime_data *prtd = runtime->private_data;
102         struct omap_pcm_dma_data *dma_data = rtd->dai->cpu_dai->dma_data;
103         int err = 0;
104
105         /* return if this is a bufferless transfer e.g.
106          * codec <--> BT codec or GSM modem -- lg FIXME */
107         if (!dma_data)
108                 return 0;
109
110         snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
111         runtime->dma_bytes = params_buffer_bytes(params);
112
113         if (prtd->dma_data)
114                 return 0;
115         prtd->dma_data = dma_data;
116         err = omap_request_dma(dma_data->dma_req, dma_data->name,
117                                omap_pcm_dma_irq, substream, &prtd->dma_ch);
118         if (!err) {
119                 /*
120                  * Link channel with itself so DMA doesn't need any
121                  * reprogramming while looping the buffer
122                  */
123                 omap_dma_link_lch(prtd->dma_ch, prtd->dma_ch);
124         }
125
126         return err;
127 }
128
129 static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
130 {
131         struct snd_pcm_runtime *runtime = substream->runtime;
132         struct omap_runtime_data *prtd = runtime->private_data;
133
134         if (prtd->dma_data == NULL)
135                 return 0;
136
137         omap_dma_unlink_lch(prtd->dma_ch, prtd->dma_ch);
138         omap_free_dma(prtd->dma_ch);
139         prtd->dma_data = NULL;
140
141         snd_pcm_set_runtime_buffer(substream, NULL);
142
143         return 0;
144 }
145
146 static int omap_pcm_prepare(struct snd_pcm_substream *substream)
147 {
148         struct snd_pcm_runtime *runtime = substream->runtime;
149         struct omap_runtime_data *prtd = runtime->private_data;
150         struct omap_pcm_dma_data *dma_data = prtd->dma_data;
151         struct omap_dma_channel_params dma_params;
152
153         /* return if this is a bufferless transfer e.g.
154          * codec <--> BT codec or GSM modem -- lg FIXME */
155         if (!prtd->dma_data)
156                 return 0;
157
158         memset(&dma_params, 0, sizeof(dma_params));
159         /*
160          * Note: Regardless of interface data formats supported by OMAP McBSP
161          * or EAC blocks, internal representation is always fixed 16-bit/sample
162          */
163         dma_params.data_type                    = OMAP_DMA_DATA_TYPE_S16;
164         dma_params.trigger                      = dma_data->dma_req;
165         dma_params.sync_mode                    = dma_data->sync_mode;
166         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
167                 dma_params.src_amode            = OMAP_DMA_AMODE_POST_INC;
168                 dma_params.dst_amode            = OMAP_DMA_AMODE_CONSTANT;
169                 dma_params.src_or_dst_synch     = OMAP_DMA_DST_SYNC;
170                 dma_params.src_start            = runtime->dma_addr;
171                 dma_params.dst_start            = dma_data->port_addr;
172                 dma_params.dst_port             = OMAP_DMA_PORT_MPUI;
173         } else {
174                 dma_params.src_amode            = OMAP_DMA_AMODE_CONSTANT;
175                 dma_params.dst_amode            = OMAP_DMA_AMODE_POST_INC;
176                 dma_params.src_or_dst_synch     = OMAP_DMA_SRC_SYNC;
177                 dma_params.src_start            = dma_data->port_addr;
178                 dma_params.dst_start            = runtime->dma_addr;
179                 dma_params.src_port             = OMAP_DMA_PORT_MPUI;
180         }
181         /*
182          * Set DMA transfer frame size equal to ALSA period size and frame
183          * count as no. of ALSA periods. Then with DMA frame interrupt enabled,
184          * we can transfer the whole ALSA buffer with single DMA transfer but
185          * still can get an interrupt at each period bounary
186          */
187         dma_params.elem_count   = snd_pcm_lib_period_bytes(substream) / 2;
188         dma_params.frame_count  = runtime->periods;
189         omap_set_dma_params(prtd->dma_ch, &dma_params);
190
191         if ((cpu_is_omap1510()) &&
192                         (substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
193                 omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ |
194                               OMAP_DMA_LAST_IRQ | OMAP_DMA_BLOCK_IRQ);
195         else
196                 omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ);
197
198         omap_set_dma_src_burst_mode(prtd->dma_ch, OMAP_DMA_DATA_BURST_16);
199         omap_set_dma_dest_burst_mode(prtd->dma_ch, OMAP_DMA_DATA_BURST_16);
200
201         return 0;
202 }
203
204 static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
205 {
206         struct snd_pcm_runtime *runtime = substream->runtime;
207         struct omap_runtime_data *prtd = runtime->private_data;
208         struct omap_pcm_dma_data *dma_data = prtd->dma_data;
209         unsigned long flags;
210         int ret = 0;
211
212         spin_lock_irqsave(&prtd->lock, flags);
213         switch (cmd) {
214         case SNDRV_PCM_TRIGGER_START:
215         case SNDRV_PCM_TRIGGER_RESUME:
216         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
217                 prtd->period_index = 0;
218                 /* Configure McBSP internal buffer usage */
219                 if (dma_data->set_threshold)
220                         dma_data->set_threshold(substream);
221
222                 omap_start_dma(prtd->dma_ch);
223                 break;
224
225         case SNDRV_PCM_TRIGGER_STOP:
226         case SNDRV_PCM_TRIGGER_SUSPEND:
227         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
228                 prtd->period_index = -1;
229                 omap_stop_dma(prtd->dma_ch);
230                 break;
231         default:
232                 ret = -EINVAL;
233         }
234         spin_unlock_irqrestore(&prtd->lock, flags);
235
236         return ret;
237 }
238
239 static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
240 {
241         struct snd_pcm_runtime *runtime = substream->runtime;
242         struct omap_runtime_data *prtd = runtime->private_data;
243         dma_addr_t ptr;
244         snd_pcm_uframes_t offset;
245
246         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
247                 ptr = omap_get_dma_dst_pos(prtd->dma_ch);
248                 offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
249         } else if (!(cpu_is_omap1510())) {
250                 ptr = omap_get_dma_src_pos(prtd->dma_ch);
251                 offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
252         } else
253                 offset = prtd->period_index * runtime->period_size;
254
255         if (offset >= runtime->buffer_size)
256                 offset = 0;
257
258         return offset;
259 }
260
261 static int omap_pcm_open(struct snd_pcm_substream *substream)
262 {
263         struct snd_pcm_runtime *runtime = substream->runtime;
264         struct omap_runtime_data *prtd;
265         int ret;
266
267         snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
268
269         /* Ensure that buffer size is a multiple of period size */
270         ret = snd_pcm_hw_constraint_integer(runtime,
271                                             SNDRV_PCM_HW_PARAM_PERIODS);
272         if (ret < 0)
273                 goto out;
274
275         prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
276         if (prtd == NULL) {
277                 ret = -ENOMEM;
278                 goto out;
279         }
280         spin_lock_init(&prtd->lock);
281         runtime->private_data = prtd;
282
283 out:
284         return ret;
285 }
286
287 static int omap_pcm_close(struct snd_pcm_substream *substream)
288 {
289         struct snd_pcm_runtime *runtime = substream->runtime;
290
291         kfree(runtime->private_data);
292         return 0;
293 }
294
295 static int omap_pcm_mmap(struct snd_pcm_substream *substream,
296         struct vm_area_struct *vma)
297 {
298         struct snd_pcm_runtime *runtime = substream->runtime;
299
300         return dma_mmap_writecombine(substream->pcm->card->dev, vma,
301                                      runtime->dma_area,
302                                      runtime->dma_addr,
303                                      runtime->dma_bytes);
304 }
305
306 static struct snd_pcm_ops omap_pcm_ops = {
307         .open           = omap_pcm_open,
308         .close          = omap_pcm_close,
309         .ioctl          = snd_pcm_lib_ioctl,
310         .hw_params      = omap_pcm_hw_params,
311         .hw_free        = omap_pcm_hw_free,
312         .prepare        = omap_pcm_prepare,
313         .trigger        = omap_pcm_trigger,
314         .pointer        = omap_pcm_pointer,
315         .mmap           = omap_pcm_mmap,
316 };
317
318 static u64 omap_pcm_dmamask = DMA_BIT_MASK(64);
319
320 static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
321         int stream)
322 {
323         struct snd_pcm_substream *substream = pcm->streams[stream].substream;
324         struct snd_dma_buffer *buf = &substream->dma_buffer;
325         size_t size = omap_pcm_hardware.buffer_bytes_max;
326
327         buf->dev.type = SNDRV_DMA_TYPE_DEV;
328         buf->dev.dev = pcm->card->dev;
329         buf->private_data = NULL;
330         buf->area = dma_alloc_writecombine(pcm->card->dev, size,
331                                            &buf->addr, GFP_KERNEL);
332         if (!buf->area)
333                 return -ENOMEM;
334
335         buf->bytes = size;
336         return 0;
337 }
338
339 static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
340 {
341         struct snd_pcm_substream *substream;
342         struct snd_dma_buffer *buf;
343         int stream;
344
345         for (stream = 0; stream < 2; stream++) {
346                 substream = pcm->streams[stream].substream;
347                 if (!substream)
348                         continue;
349
350                 buf = &substream->dma_buffer;
351                 if (!buf->area)
352                         continue;
353
354                 dma_free_writecombine(pcm->card->dev, buf->bytes,
355                                       buf->area, buf->addr);
356                 buf->area = NULL;
357         }
358 }
359
360 static int omap_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
361                  struct snd_pcm *pcm)
362 {
363         int ret = 0;
364
365         if (!card->dev->dma_mask)
366                 card->dev->dma_mask = &omap_pcm_dmamask;
367         if (!card->dev->coherent_dma_mask)
368                 card->dev->coherent_dma_mask = DMA_BIT_MASK(64);
369
370         if (dai->playback.channels_min) {
371                 ret = omap_pcm_preallocate_dma_buffer(pcm,
372                         SNDRV_PCM_STREAM_PLAYBACK);
373                 if (ret)
374                         goto out;
375         }
376
377         if (dai->capture.channels_min) {
378                 ret = omap_pcm_preallocate_dma_buffer(pcm,
379                         SNDRV_PCM_STREAM_CAPTURE);
380                 if (ret)
381                         goto out;
382         }
383
384 out:
385         return ret;
386 }
387
388 struct snd_soc_platform omap_soc_platform = {
389         .name           = "omap-pcm-audio",
390         .pcm_ops        = &omap_pcm_ops,
391         .pcm_new        = omap_pcm_new,
392         .pcm_free       = omap_pcm_free_dma_buffers,
393 };
394 EXPORT_SYMBOL_GPL(omap_soc_platform);
395
396 static int __init omap_soc_platform_init(void)
397 {
398         return snd_soc_register_platform(&omap_soc_platform);
399 }
400 module_init(omap_soc_platform_init);
401
402 static void __exit omap_soc_platform_exit(void)
403 {
404         snd_soc_unregister_platform(&omap_soc_platform);
405 }
406 module_exit(omap_soc_platform_exit);
407
408 MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
409 MODULE_DESCRIPTION("OMAP PCM DMA module");
410 MODULE_LICENSE("GPL");