2 * omap-pcm.c -- ALSA PCM interface for the OMAP SoC
4 * Copyright (C) 2008 Nokia Corporation
6 * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
7 * Peter Ujfalusi <peter.ujfalusi@ti.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 <linux/module.h>
28 #include <linux/omap-dma.h>
29 #include <sound/core.h>
30 #include <sound/pcm.h>
31 #include <sound/pcm_params.h>
32 #include <sound/dmaengine_pcm.h>
33 #include <sound/soc.h>
38 static const struct snd_pcm_hardware omap_pcm_hardware = {
39 .info = SNDRV_PCM_INFO_MMAP |
40 SNDRV_PCM_INFO_MMAP_VALID |
41 SNDRV_PCM_INFO_INTERLEAVED |
42 SNDRV_PCM_INFO_PAUSE |
43 SNDRV_PCM_INFO_RESUME |
44 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
45 .formats = SNDRV_PCM_FMTBIT_S16_LE |
46 SNDRV_PCM_FMTBIT_S32_LE,
47 .period_bytes_min = 32,
48 .period_bytes_max = 64 * 1024,
51 .buffer_bytes_max = 128 * 1024,
54 static int omap_pcm_get_dma_buswidth(int num_bits)
60 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
63 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
73 /* this may get called several times by oss emulation */
74 static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
75 struct snd_pcm_hw_params *params)
77 struct snd_pcm_runtime *runtime = substream->runtime;
78 struct snd_soc_pcm_runtime *rtd = substream->private_data;
79 struct omap_pcm_dma_data *dma_data;
80 struct dma_slave_config config;
81 struct dma_chan *chan;
84 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
86 /* return if this is a bufferless transfer e.g.
87 * codec <--> BT codec or GSM modem -- lg FIXME */
91 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
92 runtime->dma_bytes = params_buffer_bytes(params);
94 chan = snd_dmaengine_pcm_get_chan(substream);
98 /* fills in addr_width and direction */
99 err = snd_hwparams_to_dma_slave_config(substream, params, &config);
103 /* Override the *_dma addr_width if requested by the DAI driver */
104 if (dma_data->data_type) {
105 int buswidth = omap_pcm_get_dma_buswidth(dma_data->data_type);
107 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
108 config.dst_addr_width = buswidth;
110 config.src_addr_width = buswidth;
113 config.src_addr = dma_data->port_addr;
114 config.dst_addr = dma_data->port_addr;
115 config.src_maxburst = dma_data->packet_size;
116 config.dst_maxburst = dma_data->packet_size;
118 return dmaengine_slave_config(chan, &config);
121 static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
123 snd_pcm_set_runtime_buffer(substream, NULL);
127 static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
129 struct snd_soc_pcm_runtime *rtd = substream->private_data;
130 struct omap_pcm_dma_data *dma_data;
133 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
136 case SNDRV_PCM_TRIGGER_START:
137 case SNDRV_PCM_TRIGGER_RESUME:
138 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
139 /* Configure McBSP internal buffer usage */
140 if (dma_data->set_threshold)
141 dma_data->set_threshold(substream);
144 case SNDRV_PCM_TRIGGER_STOP:
145 case SNDRV_PCM_TRIGGER_SUSPEND:
146 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
153 ret = snd_dmaengine_pcm_trigger(substream, cmd);
158 static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
160 snd_pcm_uframes_t offset;
162 if (cpu_is_omap1510())
163 offset = snd_dmaengine_pcm_pointer_no_residue(substream);
165 offset = snd_dmaengine_pcm_pointer(substream);
170 static int omap_pcm_open(struct snd_pcm_substream *substream)
172 struct snd_pcm_runtime *runtime = substream->runtime;
173 struct snd_soc_pcm_runtime *rtd = substream->private_data;
174 struct omap_pcm_dma_data *dma_data;
177 snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
179 /* Ensure that buffer size is a multiple of period size */
180 ret = snd_pcm_hw_constraint_integer(runtime,
181 SNDRV_PCM_HW_PARAM_PERIODS);
185 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
186 ret = snd_dmaengine_pcm_open(substream, omap_dma_filter_fn,
191 static int omap_pcm_close(struct snd_pcm_substream *substream)
193 snd_dmaengine_pcm_close(substream);
197 static int omap_pcm_mmap(struct snd_pcm_substream *substream,
198 struct vm_area_struct *vma)
200 struct snd_pcm_runtime *runtime = substream->runtime;
202 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
208 static struct snd_pcm_ops omap_pcm_ops = {
209 .open = omap_pcm_open,
210 .close = omap_pcm_close,
211 .ioctl = snd_pcm_lib_ioctl,
212 .hw_params = omap_pcm_hw_params,
213 .hw_free = omap_pcm_hw_free,
214 .trigger = omap_pcm_trigger,
215 .pointer = omap_pcm_pointer,
216 .mmap = omap_pcm_mmap,
219 static u64 omap_pcm_dmamask = DMA_BIT_MASK(64);
221 static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
224 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
225 struct snd_dma_buffer *buf = &substream->dma_buffer;
226 size_t size = omap_pcm_hardware.buffer_bytes_max;
228 buf->dev.type = SNDRV_DMA_TYPE_DEV;
229 buf->dev.dev = pcm->card->dev;
230 buf->private_data = NULL;
231 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
232 &buf->addr, GFP_KERNEL);
240 static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
242 struct snd_pcm_substream *substream;
243 struct snd_dma_buffer *buf;
246 for (stream = 0; stream < 2; stream++) {
247 substream = pcm->streams[stream].substream;
251 buf = &substream->dma_buffer;
255 dma_free_writecombine(pcm->card->dev, buf->bytes,
256 buf->area, buf->addr);
261 static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd)
263 struct snd_card *card = rtd->card->snd_card;
264 struct snd_pcm *pcm = rtd->pcm;
267 if (!card->dev->dma_mask)
268 card->dev->dma_mask = &omap_pcm_dmamask;
269 if (!card->dev->coherent_dma_mask)
270 card->dev->coherent_dma_mask = DMA_BIT_MASK(64);
272 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
273 ret = omap_pcm_preallocate_dma_buffer(pcm,
274 SNDRV_PCM_STREAM_PLAYBACK);
279 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
280 ret = omap_pcm_preallocate_dma_buffer(pcm,
281 SNDRV_PCM_STREAM_CAPTURE);
287 /* free preallocated buffers in case of error */
289 omap_pcm_free_dma_buffers(pcm);
294 static struct snd_soc_platform_driver omap_soc_platform = {
295 .ops = &omap_pcm_ops,
296 .pcm_new = omap_pcm_new,
297 .pcm_free = omap_pcm_free_dma_buffers,
300 static __devinit int omap_pcm_probe(struct platform_device *pdev)
302 return snd_soc_register_platform(&pdev->dev,
306 static int __devexit omap_pcm_remove(struct platform_device *pdev)
308 snd_soc_unregister_platform(&pdev->dev);
312 static struct platform_driver omap_pcm_driver = {
314 .name = "omap-pcm-audio",
315 .owner = THIS_MODULE,
318 .probe = omap_pcm_probe,
319 .remove = __devexit_p(omap_pcm_remove),
322 module_platform_driver(omap_pcm_driver);
324 MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
325 MODULE_DESCRIPTION("OMAP PCM DMA module");
326 MODULE_LICENSE("GPL");
327 MODULE_ALIAS("platform:omap-pcm-audio");