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