ASoC: OMAP: Make use of DMA channel self linking on OMAP1510
[linux-2.6.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 (prtd->period_index >= 0) {
72                         if (++prtd->period_index == runtime->periods) {
73                                 prtd->period_index = 0;
74                         }
75                 }
76                 spin_unlock_irqrestore(&prtd->lock, flags);
77         }
78
79         snd_pcm_period_elapsed(substream);
80 }
81
82 /* this may get called several times by oss emulation */
83 static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
84                               struct snd_pcm_hw_params *params)
85 {
86         struct snd_pcm_runtime *runtime = substream->runtime;
87         struct snd_soc_pcm_runtime *rtd = substream->private_data;
88         struct omap_runtime_data *prtd = runtime->private_data;
89         struct omap_pcm_dma_data *dma_data = rtd->dai->cpu_dai->dma_data;
90         int err = 0;
91
92         /* return if this is a bufferless transfer e.g.
93          * codec <--> BT codec or GSM modem -- lg FIXME */
94         if (!dma_data)
95                 return 0;
96
97         snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
98         runtime->dma_bytes = params_buffer_bytes(params);
99
100         if (prtd->dma_data)
101                 return 0;
102         prtd->dma_data = dma_data;
103         err = omap_request_dma(dma_data->dma_req, dma_data->name,
104                                omap_pcm_dma_irq, substream, &prtd->dma_ch);
105         if (!err) {
106                 /*
107                  * Link channel with itself so DMA doesn't need any
108                  * reprogramming while looping the buffer
109                  */
110                 omap_dma_link_lch(prtd->dma_ch, prtd->dma_ch);
111         }
112
113         return err;
114 }
115
116 static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
117 {
118         struct snd_pcm_runtime *runtime = substream->runtime;
119         struct omap_runtime_data *prtd = runtime->private_data;
120
121         if (prtd->dma_data == NULL)
122                 return 0;
123
124         omap_dma_unlink_lch(prtd->dma_ch, prtd->dma_ch);
125         omap_free_dma(prtd->dma_ch);
126         prtd->dma_data = NULL;
127
128         snd_pcm_set_runtime_buffer(substream, NULL);
129
130         return 0;
131 }
132
133 static int omap_pcm_prepare(struct snd_pcm_substream *substream)
134 {
135         struct snd_pcm_runtime *runtime = substream->runtime;
136         struct omap_runtime_data *prtd = runtime->private_data;
137         struct omap_pcm_dma_data *dma_data = prtd->dma_data;
138         struct omap_dma_channel_params dma_params;
139
140         /* return if this is a bufferless transfer e.g.
141          * codec <--> BT codec or GSM modem -- lg FIXME */
142         if (!prtd->dma_data)
143                 return 0;
144
145         memset(&dma_params, 0, sizeof(dma_params));
146         /*
147          * Note: Regardless of interface data formats supported by OMAP McBSP
148          * or EAC blocks, internal representation is always fixed 16-bit/sample
149          */
150         dma_params.data_type                    = OMAP_DMA_DATA_TYPE_S16;
151         dma_params.trigger                      = dma_data->dma_req;
152         dma_params.sync_mode                    = OMAP_DMA_SYNC_ELEMENT;
153         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
154                 dma_params.src_amode            = OMAP_DMA_AMODE_POST_INC;
155                 dma_params.dst_amode            = OMAP_DMA_AMODE_CONSTANT;
156                 dma_params.src_or_dst_synch     = OMAP_DMA_DST_SYNC;
157                 dma_params.src_start            = runtime->dma_addr;
158                 dma_params.dst_start            = dma_data->port_addr;
159                 dma_params.dst_port             = OMAP_DMA_PORT_MPUI;
160         } else {
161                 dma_params.src_amode            = OMAP_DMA_AMODE_CONSTANT;
162                 dma_params.dst_amode            = OMAP_DMA_AMODE_POST_INC;
163                 dma_params.src_or_dst_synch     = OMAP_DMA_SRC_SYNC;
164                 dma_params.src_start            = dma_data->port_addr;
165                 dma_params.dst_start            = runtime->dma_addr;
166                 dma_params.src_port             = OMAP_DMA_PORT_MPUI;
167         }
168         /*
169          * Set DMA transfer frame size equal to ALSA period size and frame
170          * count as no. of ALSA periods. Then with DMA frame interrupt enabled,
171          * we can transfer the whole ALSA buffer with single DMA transfer but
172          * still can get an interrupt at each period bounary
173          */
174         dma_params.elem_count   = snd_pcm_lib_period_bytes(substream) / 2;
175         dma_params.frame_count  = runtime->periods;
176         omap_set_dma_params(prtd->dma_ch, &dma_params);
177
178         omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ);
179
180         return 0;
181 }
182
183 static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
184 {
185         struct snd_pcm_runtime *runtime = substream->runtime;
186         struct omap_runtime_data *prtd = runtime->private_data;
187         unsigned long flags;
188         int ret = 0;
189
190         spin_lock_irqsave(&prtd->lock, flags);
191         switch (cmd) {
192         case SNDRV_PCM_TRIGGER_START:
193         case SNDRV_PCM_TRIGGER_RESUME:
194         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
195                 prtd->period_index = 0;
196                 omap_start_dma(prtd->dma_ch);
197                 break;
198
199         case SNDRV_PCM_TRIGGER_STOP:
200         case SNDRV_PCM_TRIGGER_SUSPEND:
201         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
202                 prtd->period_index = -1;
203                 omap_stop_dma(prtd->dma_ch);
204                 break;
205         default:
206                 ret = -EINVAL;
207         }
208         spin_unlock_irqrestore(&prtd->lock, flags);
209
210         return ret;
211 }
212
213 static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
214 {
215         struct snd_pcm_runtime *runtime = substream->runtime;
216         struct omap_runtime_data *prtd = runtime->private_data;
217         dma_addr_t ptr;
218         snd_pcm_uframes_t offset;
219
220         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
221                 ptr = omap_get_dma_dst_pos(prtd->dma_ch);
222                 offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
223         } else if (!(cpu_is_omap1510())) {
224                 ptr = omap_get_dma_src_pos(prtd->dma_ch);
225                 offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
226         } else
227                 offset = prtd->period_index * runtime->period_size;
228
229         if (offset >= runtime->buffer_size)
230                 offset = 0;
231
232         return offset;
233 }
234
235 static int omap_pcm_open(struct snd_pcm_substream *substream)
236 {
237         struct snd_pcm_runtime *runtime = substream->runtime;
238         struct omap_runtime_data *prtd;
239         int ret;
240
241         snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
242
243         /* Ensure that buffer size is a multiple of period size */
244         ret = snd_pcm_hw_constraint_integer(runtime,
245                                             SNDRV_PCM_HW_PARAM_PERIODS);
246         if (ret < 0)
247                 goto out;
248
249         prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
250         if (prtd == NULL) {
251                 ret = -ENOMEM;
252                 goto out;
253         }
254         spin_lock_init(&prtd->lock);
255         runtime->private_data = prtd;
256
257 out:
258         return ret;
259 }
260
261 static int omap_pcm_close(struct snd_pcm_substream *substream)
262 {
263         struct snd_pcm_runtime *runtime = substream->runtime;
264
265         kfree(runtime->private_data);
266         return 0;
267 }
268
269 static int omap_pcm_mmap(struct snd_pcm_substream *substream,
270         struct vm_area_struct *vma)
271 {
272         struct snd_pcm_runtime *runtime = substream->runtime;
273
274         return dma_mmap_writecombine(substream->pcm->card->dev, vma,
275                                      runtime->dma_area,
276                                      runtime->dma_addr,
277                                      runtime->dma_bytes);
278 }
279
280 static struct snd_pcm_ops omap_pcm_ops = {
281         .open           = omap_pcm_open,
282         .close          = omap_pcm_close,
283         .ioctl          = snd_pcm_lib_ioctl,
284         .hw_params      = omap_pcm_hw_params,
285         .hw_free        = omap_pcm_hw_free,
286         .prepare        = omap_pcm_prepare,
287         .trigger        = omap_pcm_trigger,
288         .pointer        = omap_pcm_pointer,
289         .mmap           = omap_pcm_mmap,
290 };
291
292 static u64 omap_pcm_dmamask = DMA_BIT_MASK(32);
293
294 static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
295         int stream)
296 {
297         struct snd_pcm_substream *substream = pcm->streams[stream].substream;
298         struct snd_dma_buffer *buf = &substream->dma_buffer;
299         size_t size = omap_pcm_hardware.buffer_bytes_max;
300
301         buf->dev.type = SNDRV_DMA_TYPE_DEV;
302         buf->dev.dev = pcm->card->dev;
303         buf->private_data = NULL;
304         buf->area = dma_alloc_writecombine(pcm->card->dev, size,
305                                            &buf->addr, GFP_KERNEL);
306         if (!buf->area)
307                 return -ENOMEM;
308
309         buf->bytes = size;
310         return 0;
311 }
312
313 static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
314 {
315         struct snd_pcm_substream *substream;
316         struct snd_dma_buffer *buf;
317         int stream;
318
319         for (stream = 0; stream < 2; stream++) {
320                 substream = pcm->streams[stream].substream;
321                 if (!substream)
322                         continue;
323
324                 buf = &substream->dma_buffer;
325                 if (!buf->area)
326                         continue;
327
328                 dma_free_writecombine(pcm->card->dev, buf->bytes,
329                                       buf->area, buf->addr);
330                 buf->area = NULL;
331         }
332 }
333
334 static int omap_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
335                  struct snd_pcm *pcm)
336 {
337         int ret = 0;
338
339         if (!card->dev->dma_mask)
340                 card->dev->dma_mask = &omap_pcm_dmamask;
341         if (!card->dev->coherent_dma_mask)
342                 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
343
344         if (dai->playback.channels_min) {
345                 ret = omap_pcm_preallocate_dma_buffer(pcm,
346                         SNDRV_PCM_STREAM_PLAYBACK);
347                 if (ret)
348                         goto out;
349         }
350
351         if (dai->capture.channels_min) {
352                 ret = omap_pcm_preallocate_dma_buffer(pcm,
353                         SNDRV_PCM_STREAM_CAPTURE);
354                 if (ret)
355                         goto out;
356         }
357
358 out:
359         return ret;
360 }
361
362 struct snd_soc_platform omap_soc_platform = {
363         .name           = "omap-pcm-audio",
364         .pcm_ops        = &omap_pcm_ops,
365         .pcm_new        = omap_pcm_new,
366         .pcm_free       = omap_pcm_free_dma_buffers,
367 };
368 EXPORT_SYMBOL_GPL(omap_soc_platform);
369
370 static int __init omap_soc_platform_init(void)
371 {
372         return snd_soc_register_platform(&omap_soc_platform);
373 }
374 module_init(omap_soc_platform_init);
375
376 static void __exit omap_soc_platform_exit(void)
377 {
378         snd_soc_unregister_platform(&omap_soc_platform);
379 }
380 module_exit(omap_soc_platform_exit);
381
382 MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
383 MODULE_DESCRIPTION("OMAP PCM DMA module");
384 MODULE_LICENSE("GPL");