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 <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.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,
45 .buffer_bytes_max = 128 * 1024,
48 struct omap_runtime_data {
50 struct omap_pcm_dma_data *dma_data;
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;
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;
90 /* return if this is a bufferless transfer e.g.
91 * codec <--> BT codec or GSM modem -- lg FIXME */
95 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
96 runtime->dma_bytes = params_buffer_bytes(params);
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);
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)
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);
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 */
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;
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);
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;
189 spin_lock_irqsave(&prtd->lock, flags);
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);
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);
207 spin_unlock_irqrestore(&prtd->lock, flags);
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;
217 snd_pcm_uframes_t offset;
219 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
220 ptr = omap_get_dma_src_pos(prtd->dma_ch);
222 ptr = omap_get_dma_dst_pos(prtd->dma_ch);
224 offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
225 if (offset >= runtime->buffer_size)
231 static int omap_pcm_open(struct snd_pcm_substream *substream)
233 struct snd_pcm_runtime *runtime = substream->runtime;
234 struct omap_runtime_data *prtd;
237 snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
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);
245 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
250 spin_lock_init(&prtd->lock);
251 runtime->private_data = prtd;
257 static int omap_pcm_close(struct snd_pcm_substream *substream)
259 struct snd_pcm_runtime *runtime = substream->runtime;
261 kfree(runtime->private_data);
265 static int omap_pcm_mmap(struct snd_pcm_substream *substream,
266 struct vm_area_struct *vma)
268 struct snd_pcm_runtime *runtime = substream->runtime;
270 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
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,
288 static u64 omap_pcm_dmamask = DMA_BIT_MASK(32);
290 static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
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;
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);
309 static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
311 struct snd_pcm_substream *substream;
312 struct snd_dma_buffer *buf;
315 for (stream = 0; stream < 2; stream++) {
316 substream = pcm->streams[stream].substream;
320 buf = &substream->dma_buffer;
324 dma_free_writecombine(pcm->card->dev, buf->bytes,
325 buf->area, buf->addr);
330 int omap_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
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);
340 if (dai->playback.channels_min) {
341 ret = omap_pcm_preallocate_dma_buffer(pcm,
342 SNDRV_PCM_STREAM_PLAYBACK);
347 if (dai->capture.channels_min) {
348 ret = omap_pcm_preallocate_dma_buffer(pcm,
349 SNDRV_PCM_STREAM_CAPTURE);
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,
364 EXPORT_SYMBOL_GPL(omap_soc_platform);
366 static int __init omap_soc_platform_init(void)
368 return snd_soc_register_platform(&omap_soc_platform);
370 module_init(omap_soc_platform_init);
372 static void __exit omap_soc_platform_exit(void)
374 snd_soc_unregister_platform(&omap_soc_platform);
376 module_exit(omap_soc_platform_exit);
378 MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
379 MODULE_DESCRIPTION("OMAP PCM DMA module");
380 MODULE_LICENSE("GPL");