Linux 2.6.31.8
[linux/fpc-iii.git] / sound / soc / omap / omap-pcm.c
blob84a1950880ebc96143a749cd2c5c819b08575508
1 /*
2 * omap-pcm.c -- ALSA PCM interface for the OMAP SoC
4 * Copyright (C) 2008 Nokia Corporation
6 * Contact: Jarkko Nikula <jhnikula@gmail.com>
7 * Peter Ujfalusi <peter.ujfalusi@nokia.com>
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.
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.
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
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>
31 #include <mach/dma.h>
32 #include "omap-pcm.h"
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,
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;
55 static void omap_pcm_dma_irq(int ch, u16 stat, void *data)
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;
62 if (cpu_is_omap1510()) {
64 * OMAP1510 doesn't support DMA chaining so have to restart
65 * the transfer after all periods are transferred
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);
74 spin_unlock_irqrestore(&prtd->lock, flags);
77 snd_pcm_period_elapsed(substream);
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)
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;
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;
95 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
96 runtime->dma_bytes = params_buffer_bytes(params);
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()) {
105 * Link channel with itself so DMA doesn't need any
106 * reprogramming while looping the buffer
108 omap_dma_link_lch(prtd->dma_ch, prtd->dma_ch);
111 return err;
114 static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
116 struct snd_pcm_runtime *runtime = substream->runtime;
117 struct omap_runtime_data *prtd = runtime->private_data;
119 if (prtd->dma_data == NULL)
120 return 0;
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;
127 snd_pcm_set_runtime_buffer(substream, NULL);
129 return 0;
132 static int omap_pcm_prepare(struct snd_pcm_substream *substream)
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;
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;
144 memset(&dma_params, 0, sizeof(dma_params));
146 * Note: Regardless of interface data formats supported by OMAP McBSP
147 * or EAC blocks, internal representation is always fixed 16-bit/sample
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;
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
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);
177 omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ);
179 return 0;
182 static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
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;
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;
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;
207 spin_unlock_irqrestore(&prtd->lock, flags);
209 return ret;
212 static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
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;
219 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
220 ptr = omap_get_dma_dst_pos(prtd->dma_ch);
221 offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
222 } else if (!(cpu_is_omap1510())) {
223 ptr = omap_get_dma_src_pos(prtd->dma_ch);
224 offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
225 } else
226 offset = prtd->period_index * runtime->period_size;
228 if (offset >= runtime->buffer_size)
229 offset = 0;
231 return offset;
234 static int omap_pcm_open(struct snd_pcm_substream *substream)
236 struct snd_pcm_runtime *runtime = substream->runtime;
237 struct omap_runtime_data *prtd;
238 int ret;
240 snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
242 /* Ensure that buffer size is a multiple of period size */
243 ret = snd_pcm_hw_constraint_integer(runtime,
244 SNDRV_PCM_HW_PARAM_PERIODS);
245 if (ret < 0)
246 goto out;
248 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
249 if (prtd == NULL) {
250 ret = -ENOMEM;
251 goto out;
253 spin_lock_init(&prtd->lock);
254 runtime->private_data = prtd;
256 out:
257 return ret;
260 static int omap_pcm_close(struct snd_pcm_substream *substream)
262 struct snd_pcm_runtime *runtime = substream->runtime;
264 kfree(runtime->private_data);
265 return 0;
268 static int omap_pcm_mmap(struct snd_pcm_substream *substream,
269 struct vm_area_struct *vma)
271 struct snd_pcm_runtime *runtime = substream->runtime;
273 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
274 runtime->dma_area,
275 runtime->dma_addr,
276 runtime->dma_bytes);
279 static struct snd_pcm_ops omap_pcm_ops = {
280 .open = omap_pcm_open,
281 .close = omap_pcm_close,
282 .ioctl = snd_pcm_lib_ioctl,
283 .hw_params = omap_pcm_hw_params,
284 .hw_free = omap_pcm_hw_free,
285 .prepare = omap_pcm_prepare,
286 .trigger = omap_pcm_trigger,
287 .pointer = omap_pcm_pointer,
288 .mmap = omap_pcm_mmap,
291 static u64 omap_pcm_dmamask = DMA_BIT_MASK(32);
293 static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
294 int stream)
296 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
297 struct snd_dma_buffer *buf = &substream->dma_buffer;
298 size_t size = omap_pcm_hardware.buffer_bytes_max;
300 buf->dev.type = SNDRV_DMA_TYPE_DEV;
301 buf->dev.dev = pcm->card->dev;
302 buf->private_data = NULL;
303 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
304 &buf->addr, GFP_KERNEL);
305 if (!buf->area)
306 return -ENOMEM;
308 buf->bytes = size;
309 return 0;
312 static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
314 struct snd_pcm_substream *substream;
315 struct snd_dma_buffer *buf;
316 int stream;
318 for (stream = 0; stream < 2; stream++) {
319 substream = pcm->streams[stream].substream;
320 if (!substream)
321 continue;
323 buf = &substream->dma_buffer;
324 if (!buf->area)
325 continue;
327 dma_free_writecombine(pcm->card->dev, buf->bytes,
328 buf->area, buf->addr);
329 buf->area = NULL;
333 int omap_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
334 struct snd_pcm *pcm)
336 int ret = 0;
338 if (!card->dev->dma_mask)
339 card->dev->dma_mask = &omap_pcm_dmamask;
340 if (!card->dev->coherent_dma_mask)
341 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
343 if (dai->playback.channels_min) {
344 ret = omap_pcm_preallocate_dma_buffer(pcm,
345 SNDRV_PCM_STREAM_PLAYBACK);
346 if (ret)
347 goto out;
350 if (dai->capture.channels_min) {
351 ret = omap_pcm_preallocate_dma_buffer(pcm,
352 SNDRV_PCM_STREAM_CAPTURE);
353 if (ret)
354 goto out;
357 out:
358 return ret;
361 struct snd_soc_platform omap_soc_platform = {
362 .name = "omap-pcm-audio",
363 .pcm_ops = &omap_pcm_ops,
364 .pcm_new = omap_pcm_new,
365 .pcm_free = omap_pcm_free_dma_buffers,
367 EXPORT_SYMBOL_GPL(omap_soc_platform);
369 static int __init omap_soc_platform_init(void)
371 return snd_soc_register_platform(&omap_soc_platform);
373 module_init(omap_soc_platform_init);
375 static void __exit omap_soc_platform_exit(void)
377 snd_soc_unregister_platform(&omap_soc_platform);
379 module_exit(omap_soc_platform_exit);
381 MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
382 MODULE_DESCRIPTION("OMAP PCM DMA module");
383 MODULE_LICENSE("GPL");