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
25 #include <linux/dma-mapping.h>
26 #include <linux/slab.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
35 static const struct snd_pcm_hardware omap_pcm_hardware = {
36 .info = SNDRV_PCM_INFO_MMAP |
37 SNDRV_PCM_INFO_MMAP_VALID |
38 SNDRV_PCM_INFO_INTERLEAVED |
39 SNDRV_PCM_INFO_PAUSE |
40 SNDRV_PCM_INFO_RESUME,
41 .formats = SNDRV_PCM_FMTBIT_S16_LE |
42 SNDRV_PCM_FMTBIT_S32_LE,
43 .period_bytes_min = 32,
44 .period_bytes_max = 64 * 1024,
47 .buffer_bytes_max = 128 * 1024,
50 struct omap_runtime_data {
52 struct omap_pcm_dma_data *dma_data;
57 static void omap_pcm_dma_irq(int ch, u16 stat, void *data)
59 struct snd_pcm_substream *substream = data;
60 struct snd_pcm_runtime *runtime = substream->runtime;
61 struct omap_runtime_data *prtd = runtime->private_data;
64 if ((cpu_is_omap1510()) &&
65 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)) {
67 * OMAP1510 doesn't fully support DMA progress counter
68 * and there is no software emulation implemented yet,
69 * so have to maintain our own playback progress counter
70 * that can be used by omap_pcm_pointer() instead.
72 spin_lock_irqsave(&prtd->lock, flags);
73 if ((stat == OMAP_DMA_LAST_IRQ) &&
74 (prtd->period_index == runtime->periods - 1)) {
75 /* we are in sync, do nothing */
76 spin_unlock_irqrestore(&prtd->lock, flags);
79 if (prtd->period_index >= 0) {
80 if (stat & OMAP_DMA_BLOCK_IRQ) {
81 /* end of buffer reached, loop back */
82 prtd->period_index = 0;
83 } else if (stat & OMAP_DMA_LAST_IRQ) {
84 /* update the counter for the last period */
85 prtd->period_index = runtime->periods - 1;
86 } else if (++prtd->period_index >= runtime->periods) {
87 /* end of buffer missed? loop back */
88 prtd->period_index = 0;
91 spin_unlock_irqrestore(&prtd->lock, flags);
94 snd_pcm_period_elapsed(substream);
97 /* this may get called several times by oss emulation */
98 static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
99 struct snd_pcm_hw_params *params)
101 struct snd_pcm_runtime *runtime = substream->runtime;
102 struct snd_soc_pcm_runtime *rtd = substream->private_data;
103 struct omap_runtime_data *prtd = runtime->private_data;
104 struct omap_pcm_dma_data *dma_data = rtd->dai->cpu_dai->dma_data;
107 /* return if this is a bufferless transfer e.g.
108 * codec <--> BT codec or GSM modem -- lg FIXME */
112 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
113 runtime->dma_bytes = params_buffer_bytes(params);
117 prtd->dma_data = dma_data;
118 err = omap_request_dma(dma_data->dma_req, dma_data->name,
119 omap_pcm_dma_irq, substream, &prtd->dma_ch);
122 * Link channel with itself so DMA doesn't need any
123 * reprogramming while looping the buffer
125 omap_dma_link_lch(prtd->dma_ch, prtd->dma_ch);
131 static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
133 struct snd_pcm_runtime *runtime = substream->runtime;
134 struct omap_runtime_data *prtd = runtime->private_data;
136 if (prtd->dma_data == NULL)
139 omap_dma_unlink_lch(prtd->dma_ch, prtd->dma_ch);
140 omap_free_dma(prtd->dma_ch);
141 prtd->dma_data = NULL;
143 snd_pcm_set_runtime_buffer(substream, NULL);
148 static int omap_pcm_prepare(struct snd_pcm_substream *substream)
150 struct snd_pcm_runtime *runtime = substream->runtime;
151 struct omap_runtime_data *prtd = runtime->private_data;
152 struct omap_pcm_dma_data *dma_data = prtd->dma_data;
153 struct omap_dma_channel_params dma_params;
156 /* return if this is a bufferless transfer e.g.
157 * codec <--> BT codec or GSM modem -- lg FIXME */
161 memset(&dma_params, 0, sizeof(dma_params));
162 dma_params.data_type = dma_data->data_type;
163 dma_params.trigger = dma_data->dma_req;
164 dma_params.sync_mode = dma_data->sync_mode;
165 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
166 dma_params.src_amode = OMAP_DMA_AMODE_POST_INC;
167 dma_params.dst_amode = OMAP_DMA_AMODE_CONSTANT;
168 dma_params.src_or_dst_synch = OMAP_DMA_DST_SYNC;
169 dma_params.src_start = runtime->dma_addr;
170 dma_params.dst_start = dma_data->port_addr;
171 dma_params.dst_port = OMAP_DMA_PORT_MPUI;
172 dma_params.dst_fi = dma_data->packet_size;
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 dma_params.src_fi = dma_data->packet_size;
183 * Set DMA transfer frame size equal to ALSA period size and frame
184 * count as no. of ALSA periods. Then with DMA frame interrupt enabled,
185 * we can transfer the whole ALSA buffer with single DMA transfer but
186 * still can get an interrupt at each period bounary
188 bytes = snd_pcm_lib_period_bytes(substream);
189 dma_params.elem_count = bytes >> dma_data->data_type;
190 dma_params.frame_count = runtime->periods;
191 omap_set_dma_params(prtd->dma_ch, &dma_params);
193 if ((cpu_is_omap1510()) &&
194 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
195 omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ |
196 OMAP_DMA_LAST_IRQ | OMAP_DMA_BLOCK_IRQ);
198 omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ);
200 if (!(cpu_class_is_omap1())) {
201 omap_set_dma_src_burst_mode(prtd->dma_ch,
202 OMAP_DMA_DATA_BURST_16);
203 omap_set_dma_dest_burst_mode(prtd->dma_ch,
204 OMAP_DMA_DATA_BURST_16);
210 static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
212 struct snd_pcm_runtime *runtime = substream->runtime;
213 struct omap_runtime_data *prtd = runtime->private_data;
214 struct omap_pcm_dma_data *dma_data = prtd->dma_data;
218 spin_lock_irqsave(&prtd->lock, flags);
220 case SNDRV_PCM_TRIGGER_START:
221 case SNDRV_PCM_TRIGGER_RESUME:
222 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
223 prtd->period_index = 0;
224 /* Configure McBSP internal buffer usage */
225 if (dma_data->set_threshold)
226 dma_data->set_threshold(substream);
228 omap_start_dma(prtd->dma_ch);
231 case SNDRV_PCM_TRIGGER_STOP:
232 case SNDRV_PCM_TRIGGER_SUSPEND:
233 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
234 prtd->period_index = -1;
235 omap_stop_dma(prtd->dma_ch);
240 spin_unlock_irqrestore(&prtd->lock, flags);
245 static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
247 struct snd_pcm_runtime *runtime = substream->runtime;
248 struct omap_runtime_data *prtd = runtime->private_data;
250 snd_pcm_uframes_t offset;
252 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
253 ptr = omap_get_dma_dst_pos(prtd->dma_ch);
254 offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
255 } else if (!(cpu_is_omap1510())) {
256 ptr = omap_get_dma_src_pos(prtd->dma_ch);
257 offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
259 offset = prtd->period_index * runtime->period_size;
261 if (offset >= runtime->buffer_size)
267 static int omap_pcm_open(struct snd_pcm_substream *substream)
269 struct snd_pcm_runtime *runtime = substream->runtime;
270 struct omap_runtime_data *prtd;
273 snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
275 /* Ensure that buffer size is a multiple of period size */
276 ret = snd_pcm_hw_constraint_integer(runtime,
277 SNDRV_PCM_HW_PARAM_PERIODS);
281 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
286 spin_lock_init(&prtd->lock);
287 runtime->private_data = prtd;
293 static int omap_pcm_close(struct snd_pcm_substream *substream)
295 struct snd_pcm_runtime *runtime = substream->runtime;
297 kfree(runtime->private_data);
301 static int omap_pcm_mmap(struct snd_pcm_substream *substream,
302 struct vm_area_struct *vma)
304 struct snd_pcm_runtime *runtime = substream->runtime;
306 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
312 static struct snd_pcm_ops omap_pcm_ops = {
313 .open = omap_pcm_open,
314 .close = omap_pcm_close,
315 .ioctl = snd_pcm_lib_ioctl,
316 .hw_params = omap_pcm_hw_params,
317 .hw_free = omap_pcm_hw_free,
318 .prepare = omap_pcm_prepare,
319 .trigger = omap_pcm_trigger,
320 .pointer = omap_pcm_pointer,
321 .mmap = omap_pcm_mmap,
324 static u64 omap_pcm_dmamask = DMA_BIT_MASK(64);
326 static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
329 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
330 struct snd_dma_buffer *buf = &substream->dma_buffer;
331 size_t size = omap_pcm_hardware.buffer_bytes_max;
333 buf->dev.type = SNDRV_DMA_TYPE_DEV;
334 buf->dev.dev = pcm->card->dev;
335 buf->private_data = NULL;
336 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
337 &buf->addr, GFP_KERNEL);
345 static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
347 struct snd_pcm_substream *substream;
348 struct snd_dma_buffer *buf;
351 for (stream = 0; stream < 2; stream++) {
352 substream = pcm->streams[stream].substream;
356 buf = &substream->dma_buffer;
360 dma_free_writecombine(pcm->card->dev, buf->bytes,
361 buf->area, buf->addr);
366 static int omap_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
371 if (!card->dev->dma_mask)
372 card->dev->dma_mask = &omap_pcm_dmamask;
373 if (!card->dev->coherent_dma_mask)
374 card->dev->coherent_dma_mask = DMA_BIT_MASK(64);
376 if (dai->playback.channels_min) {
377 ret = omap_pcm_preallocate_dma_buffer(pcm,
378 SNDRV_PCM_STREAM_PLAYBACK);
383 if (dai->capture.channels_min) {
384 ret = omap_pcm_preallocate_dma_buffer(pcm,
385 SNDRV_PCM_STREAM_CAPTURE);
394 struct snd_soc_platform omap_soc_platform = {
395 .name = "omap-pcm-audio",
396 .pcm_ops = &omap_pcm_ops,
397 .pcm_new = omap_pcm_new,
398 .pcm_free = omap_pcm_free_dma_buffers,
400 EXPORT_SYMBOL_GPL(omap_soc_platform);
402 static int __init omap_soc_platform_init(void)
404 return snd_soc_register_platform(&omap_soc_platform);
406 module_init(omap_soc_platform_init);
408 static void __exit omap_soc_platform_exit(void)
410 snd_soc_unregister_platform(&omap_soc_platform);
412 module_exit(omap_soc_platform_exit);
414 MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
415 MODULE_DESCRIPTION("OMAP PCM DMA module");
416 MODULE_LICENSE("GPL");