2 * omap-pcm.c -- ALSA PCM interface for the OMAP SoC
4 * Copyright (C) 2008 Nokia Corporation
6 * Contact: Jarkko Nikula <jarkko.nikula@nokia.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 #include <linux/dma-mapping.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
33 static const struct snd_pcm_hardware omap_pcm_hardware = {
34 .info = SNDRV_PCM_INFO_MMAP |
35 SNDRV_PCM_INFO_MMAP_VALID |
36 SNDRV_PCM_INFO_INTERLEAVED |
37 SNDRV_PCM_INFO_PAUSE |
38 SNDRV_PCM_INFO_RESUME,
39 .formats = SNDRV_PCM_FMTBIT_S16_LE,
40 .period_bytes_min = 32,
41 .period_bytes_max = 64 * 1024,
44 .buffer_bytes_max = 128 * 1024,
47 struct omap_runtime_data {
49 struct omap_pcm_dma_data *dma_data;
54 static void omap_pcm_dma_irq(int ch, u16 stat, void *data)
56 struct snd_pcm_substream *substream = data;
57 struct snd_pcm_runtime *runtime = substream->runtime;
58 struct omap_runtime_data *prtd = runtime->private_data;
61 if (cpu_is_omap1510()) {
63 * OMAP1510 doesn't support DMA chaining so have to restart
64 * the transfer after all periods are transferred
66 spin_lock_irqsave(&prtd->lock, flags);
67 if (prtd->period_index >= 0) {
68 if (++prtd->period_index == runtime->periods) {
69 prtd->period_index = 0;
70 omap_start_dma(prtd->dma_ch);
73 spin_unlock_irqrestore(&prtd->lock, flags);
76 snd_pcm_period_elapsed(substream);
79 /* this may get called several times by oss emulation */
80 static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
81 struct snd_pcm_hw_params *params)
83 struct snd_pcm_runtime *runtime = substream->runtime;
84 struct snd_soc_pcm_runtime *rtd = substream->private_data;
85 struct omap_runtime_data *prtd = runtime->private_data;
86 struct omap_pcm_dma_data *dma_data = rtd->dai->cpu_dai->dma_data;
92 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
93 runtime->dma_bytes = params_buffer_bytes(params);
97 prtd->dma_data = dma_data;
98 err = omap_request_dma(dma_data->dma_req, dma_data->name,
99 omap_pcm_dma_irq, substream, &prtd->dma_ch);
100 if (!err & !cpu_is_omap1510()) {
102 * Link channel with itself so DMA doesn't need any
103 * reprogramming while looping the buffer
105 omap_dma_link_lch(prtd->dma_ch, prtd->dma_ch);
111 static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
113 struct snd_pcm_runtime *runtime = substream->runtime;
114 struct omap_runtime_data *prtd = runtime->private_data;
116 if (prtd->dma_data == NULL)
119 if (!cpu_is_omap1510())
120 omap_dma_unlink_lch(prtd->dma_ch, prtd->dma_ch);
121 omap_free_dma(prtd->dma_ch);
122 prtd->dma_data = NULL;
124 snd_pcm_set_runtime_buffer(substream, NULL);
129 static int omap_pcm_prepare(struct snd_pcm_substream *substream)
131 struct snd_pcm_runtime *runtime = substream->runtime;
132 struct omap_runtime_data *prtd = runtime->private_data;
133 struct omap_pcm_dma_data *dma_data = prtd->dma_data;
134 struct omap_dma_channel_params dma_params;
136 memset(&dma_params, 0, sizeof(dma_params));
138 * Note: Regardless of interface data formats supported by OMAP McBSP
139 * or EAC blocks, internal representation is always fixed 16-bit/sample
141 dma_params.data_type = OMAP_DMA_DATA_TYPE_S16;
142 dma_params.trigger = dma_data->dma_req;
143 dma_params.sync_mode = OMAP_DMA_SYNC_ELEMENT;
144 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
145 dma_params.src_amode = OMAP_DMA_AMODE_POST_INC;
146 dma_params.dst_amode = OMAP_DMA_AMODE_CONSTANT;
147 dma_params.src_or_dst_synch = OMAP_DMA_DST_SYNC;
148 dma_params.src_start = runtime->dma_addr;
149 dma_params.dst_start = dma_data->port_addr;
150 dma_params.dst_port = OMAP_DMA_PORT_MPUI;
152 dma_params.src_amode = OMAP_DMA_AMODE_CONSTANT;
153 dma_params.dst_amode = OMAP_DMA_AMODE_POST_INC;
154 dma_params.src_or_dst_synch = OMAP_DMA_SRC_SYNC;
155 dma_params.src_start = dma_data->port_addr;
156 dma_params.dst_start = runtime->dma_addr;
157 dma_params.src_port = OMAP_DMA_PORT_MPUI;
160 * Set DMA transfer frame size equal to ALSA period size and frame
161 * count as no. of ALSA periods. Then with DMA frame interrupt enabled,
162 * we can transfer the whole ALSA buffer with single DMA transfer but
163 * still can get an interrupt at each period bounary
165 dma_params.elem_count = snd_pcm_lib_period_bytes(substream) / 2;
166 dma_params.frame_count = runtime->periods;
167 omap_set_dma_params(prtd->dma_ch, &dma_params);
169 omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ);
172 * To handle realtime streaming sources properly, we need to be sure
173 * we have at least 2 periods of size larger than FIFO in the buffer
174 * to start playing without underflowing. This is because DMA is always
175 * ahead of playback by amount close to FIFO size and needs to have
176 * next period ready when previous one finishes transfering to FIFO.
178 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
179 runtime->periods > 1) {
180 struct snd_soc_pcm_runtime *rtd = substream->private_data;
181 int *mcbsp_bus_id = rtd->dai->cpu_dai->private_data;
182 int fifo_samples = (*mcbsp_bus_id == 1) ? 1024 : 256;
183 int period_samples, period_frames;
185 period_samples = bytes_to_samples(runtime,
186 snd_pcm_lib_period_bytes(substream));
187 if (period_samples < fifo_samples + runtime->channels)
188 period_samples = fifo_samples + runtime->channels;
189 period_frames = bytes_to_frames(runtime,
190 samples_to_bytes(runtime, period_samples)) * 2;
192 if (runtime->start_threshold < period_frames)
193 runtime->start_threshold = period_frames;
199 static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
201 struct snd_pcm_runtime *runtime = substream->runtime;
202 struct omap_runtime_data *prtd = runtime->private_data;
205 spin_lock_irq(&prtd->lock);
207 case SNDRV_PCM_TRIGGER_START:
208 case SNDRV_PCM_TRIGGER_RESUME:
209 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
210 prtd->period_index = 0;
211 omap_start_dma(prtd->dma_ch);
214 case SNDRV_PCM_TRIGGER_STOP:
215 case SNDRV_PCM_TRIGGER_SUSPEND:
216 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
217 prtd->period_index = -1;
218 omap_stop_dma(prtd->dma_ch);
223 spin_unlock_irq(&prtd->lock);
228 static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
230 struct snd_pcm_runtime *runtime = substream->runtime;
231 struct omap_runtime_data *prtd = runtime->private_data;
233 snd_pcm_uframes_t offset;
235 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
236 ptr = omap_get_dma_src_pos(prtd->dma_ch);
238 ptr = omap_get_dma_dst_pos(prtd->dma_ch);
240 offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
241 if (offset >= runtime->buffer_size)
247 static int omap_pcm_open(struct snd_pcm_substream *substream)
249 struct snd_pcm_runtime *runtime = substream->runtime;
250 struct omap_runtime_data *prtd;
253 snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
255 /* Ensure that buffer size is a multiple of period size */
256 ret = snd_pcm_hw_constraint_integer(runtime,
257 SNDRV_PCM_HW_PARAM_PERIODS);
261 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
266 spin_lock_init(&prtd->lock);
267 runtime->private_data = prtd;
273 static int omap_pcm_close(struct snd_pcm_substream *substream)
275 struct snd_pcm_runtime *runtime = substream->runtime;
277 kfree(runtime->private_data);
281 static int omap_pcm_mmap(struct snd_pcm_substream *substream,
282 struct vm_area_struct *vma)
284 struct snd_pcm_runtime *runtime = substream->runtime;
286 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
292 struct snd_pcm_ops omap_pcm_ops = {
293 .open = omap_pcm_open,
294 .close = omap_pcm_close,
295 .ioctl = snd_pcm_lib_ioctl,
296 .hw_params = omap_pcm_hw_params,
297 .hw_free = omap_pcm_hw_free,
298 .prepare = omap_pcm_prepare,
299 .trigger = omap_pcm_trigger,
300 .pointer = omap_pcm_pointer,
301 .mmap = omap_pcm_mmap,
304 static u64 omap_pcm_dmamask = DMA_BIT_MASK(32);
306 static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
309 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
310 struct snd_dma_buffer *buf = &substream->dma_buffer;
311 size_t size = omap_pcm_hardware.buffer_bytes_max;
313 buf->dev.type = SNDRV_DMA_TYPE_DEV;
314 buf->dev.dev = pcm->card->dev;
315 buf->private_data = NULL;
316 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
317 &buf->addr, GFP_KERNEL);
325 static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
327 struct snd_pcm_substream *substream;
328 struct snd_dma_buffer *buf;
331 for (stream = 0; stream < 2; stream++) {
332 substream = pcm->streams[stream].substream;
336 buf = &substream->dma_buffer;
340 dma_free_writecombine(pcm->card->dev, buf->bytes,
341 buf->area, buf->addr);
346 int omap_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
351 if (!card->dev->dma_mask)
352 card->dev->dma_mask = &omap_pcm_dmamask;
353 if (!card->dev->coherent_dma_mask)
354 card->dev->coherent_dma_mask = DMA_32BIT_MASK;
356 if (dai->playback.channels_min) {
357 ret = omap_pcm_preallocate_dma_buffer(pcm,
358 SNDRV_PCM_STREAM_PLAYBACK);
363 if (dai->capture.channels_min) {
364 ret = omap_pcm_preallocate_dma_buffer(pcm,
365 SNDRV_PCM_STREAM_CAPTURE);
374 struct snd_soc_platform omap_soc_platform = {
375 .name = "omap-pcm-audio",
376 .pcm_ops = &omap_pcm_ops,
377 .pcm_new = omap_pcm_new,
378 .pcm_free = omap_pcm_free_dma_buffers,
380 EXPORT_SYMBOL_GPL(omap_soc_platform);
382 MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@nokia.com>");
383 MODULE_DESCRIPTION("OMAP PCM DMA module");
384 MODULE_LICENSE("GPL");