2 * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
4 * Based on sound/soc/imx/imx-pcm-dma-mx2.c
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <linux/clk.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/module.h>
28 #include <linux/platform_device.h>
29 #include <linux/slab.h>
30 #include <linux/dmaengine.h>
32 #include <sound/core.h>
33 #include <sound/initval.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/soc.h>
41 static struct snd_pcm_hardware snd_mxs_hardware = {
42 .info = SNDRV_PCM_INFO_MMAP |
43 SNDRV_PCM_INFO_MMAP_VALID |
44 SNDRV_PCM_INFO_PAUSE |
45 SNDRV_PCM_INFO_RESUME |
46 SNDRV_PCM_INFO_INTERLEAVED,
47 .formats = SNDRV_PCM_FMTBIT_S16_LE |
48 SNDRV_PCM_FMTBIT_S20_3LE |
49 SNDRV_PCM_FMTBIT_S24_LE,
52 .period_bytes_min = 32,
53 .period_bytes_max = 8192,
56 .buffer_bytes_max = 64 * 1024,
61 static void audio_dma_irq(void *data)
63 struct snd_pcm_substream *substream = (struct snd_pcm_substream *)data;
64 struct snd_pcm_runtime *runtime = substream->runtime;
65 struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
67 iprtd->offset += iprtd->period_bytes;
68 iprtd->offset %= iprtd->period_bytes * iprtd->periods;
69 snd_pcm_period_elapsed(substream);
72 static bool filter(struct dma_chan *chan, void *param)
74 struct mxs_pcm_runtime_data *iprtd = param;
75 struct mxs_pcm_dma_params *dma_params = iprtd->dma_params;
77 if (!mxs_dma_is_apbx(chan))
80 if (chan->chan_id != dma_params->chan_num)
83 chan->private = &iprtd->dma_data;
88 static int mxs_dma_alloc(struct snd_pcm_substream *substream,
89 struct snd_pcm_hw_params *params)
91 struct snd_soc_pcm_runtime *rtd = substream->private_data;
92 struct snd_pcm_runtime *runtime = substream->runtime;
93 struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
96 iprtd->dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
99 dma_cap_set(DMA_SLAVE, mask);
100 iprtd->dma_data.chan_irq = iprtd->dma_params->chan_irq;
101 iprtd->dma_chan = dma_request_channel(mask, filter, iprtd);
102 if (!iprtd->dma_chan)
108 static int snd_mxs_pcm_hw_params(struct snd_pcm_substream *substream,
109 struct snd_pcm_hw_params *params)
111 struct snd_pcm_runtime *runtime = substream->runtime;
112 struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
113 unsigned long dma_addr;
114 struct dma_chan *chan;
117 ret = mxs_dma_alloc(substream, params);
120 chan = iprtd->dma_chan;
122 iprtd->periods = params_periods(params);
123 iprtd->period_bytes = params_period_bytes(params);
126 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
128 dma_addr = runtime->dma_addr;
130 iprtd->desc = chan->device->device_prep_dma_cyclic(chan, dma_addr,
131 iprtd->period_bytes * iprtd->periods,
133 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
134 DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
136 dev_err(&chan->dev->device, "cannot prepare slave dma\n");
140 iprtd->desc->callback = audio_dma_irq;
141 iprtd->desc->callback_param = substream;
146 static int snd_mxs_pcm_hw_free(struct snd_pcm_substream *substream)
148 struct snd_pcm_runtime *runtime = substream->runtime;
149 struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
151 if (iprtd->dma_chan) {
152 dma_release_channel(iprtd->dma_chan);
153 iprtd->dma_chan = NULL;
159 static int snd_mxs_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
161 struct snd_pcm_runtime *runtime = substream->runtime;
162 struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
165 case SNDRV_PCM_TRIGGER_START:
166 case SNDRV_PCM_TRIGGER_RESUME:
167 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
168 dmaengine_submit(iprtd->desc);
171 case SNDRV_PCM_TRIGGER_STOP:
172 case SNDRV_PCM_TRIGGER_SUSPEND:
173 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
174 dmaengine_terminate_all(iprtd->dma_chan);
184 static snd_pcm_uframes_t snd_mxs_pcm_pointer(
185 struct snd_pcm_substream *substream)
187 struct snd_pcm_runtime *runtime = substream->runtime;
188 struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
190 return bytes_to_frames(substream->runtime, iprtd->offset);
193 static int snd_mxs_open(struct snd_pcm_substream *substream)
195 struct snd_pcm_runtime *runtime = substream->runtime;
196 struct mxs_pcm_runtime_data *iprtd;
199 iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
202 runtime->private_data = iprtd;
204 ret = snd_pcm_hw_constraint_integer(substream->runtime,
205 SNDRV_PCM_HW_PARAM_PERIODS);
211 snd_soc_set_runtime_hwparams(substream, &snd_mxs_hardware);
216 static int snd_mxs_close(struct snd_pcm_substream *substream)
218 struct snd_pcm_runtime *runtime = substream->runtime;
219 struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
226 static int snd_mxs_pcm_mmap(struct snd_pcm_substream *substream,
227 struct vm_area_struct *vma)
229 struct snd_pcm_runtime *runtime = substream->runtime;
231 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
237 static struct snd_pcm_ops mxs_pcm_ops = {
238 .open = snd_mxs_open,
239 .close = snd_mxs_close,
240 .ioctl = snd_pcm_lib_ioctl,
241 .hw_params = snd_mxs_pcm_hw_params,
242 .hw_free = snd_mxs_pcm_hw_free,
243 .trigger = snd_mxs_pcm_trigger,
244 .pointer = snd_mxs_pcm_pointer,
245 .mmap = snd_mxs_pcm_mmap,
248 static int mxs_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
250 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
251 struct snd_dma_buffer *buf = &substream->dma_buffer;
252 size_t size = snd_mxs_hardware.buffer_bytes_max;
254 buf->dev.type = SNDRV_DMA_TYPE_DEV;
255 buf->dev.dev = pcm->card->dev;
256 buf->private_data = NULL;
257 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
258 &buf->addr, GFP_KERNEL);
266 static u64 mxs_pcm_dmamask = DMA_BIT_MASK(32);
267 static int mxs_pcm_new(struct snd_soc_pcm_runtime *rtd)
269 struct snd_card *card = rtd->card->snd_card;
270 struct snd_pcm *pcm = rtd->pcm;
273 if (!card->dev->dma_mask)
274 card->dev->dma_mask = &mxs_pcm_dmamask;
275 if (!card->dev->coherent_dma_mask)
276 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
278 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
279 ret = mxs_pcm_preallocate_dma_buffer(pcm,
280 SNDRV_PCM_STREAM_PLAYBACK);
285 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
286 ret = mxs_pcm_preallocate_dma_buffer(pcm,
287 SNDRV_PCM_STREAM_CAPTURE);
296 static void mxs_pcm_free(struct snd_pcm *pcm)
298 struct snd_pcm_substream *substream;
299 struct snd_dma_buffer *buf;
302 for (stream = 0; stream < 2; stream++) {
303 substream = pcm->streams[stream].substream;
307 buf = &substream->dma_buffer;
311 dma_free_writecombine(pcm->card->dev, buf->bytes,
312 buf->area, buf->addr);
317 static struct snd_soc_platform_driver mxs_soc_platform = {
319 .pcm_new = mxs_pcm_new,
320 .pcm_free = mxs_pcm_free,
323 static int __devinit mxs_soc_platform_probe(struct platform_device *pdev)
325 return snd_soc_register_platform(&pdev->dev, &mxs_soc_platform);
328 static int __devexit mxs_soc_platform_remove(struct platform_device *pdev)
330 snd_soc_unregister_platform(&pdev->dev);
335 static struct platform_driver mxs_pcm_driver = {
337 .name = "mxs-pcm-audio",
338 .owner = THIS_MODULE,
340 .probe = mxs_soc_platform_probe,
341 .remove = __devexit_p(mxs_soc_platform_remove),
344 module_platform_driver(mxs_pcm_driver);
346 MODULE_LICENSE("GPL");
347 MODULE_ALIAS("platform:mxs-pcm-audio");