blob: 5b8c8d3140605e8a53b109fc0219ed7755f2718b [file] [log] [blame]
Dong Aishenged6e1d02011-07-21 12:36:55 +08001/*
2 * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
3 *
4 * Based on sound/soc/imx/imx-pcm-dma-mx2.c
5 *
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.
10 *
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.
15 *
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.
19 */
20
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>
31
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>
37
38#include <mach/dma.h>
39#include "mxs-pcm.h"
40
41static 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,
50 .channels_min = 2,
51 .channels_max = 2,
52 .period_bytes_min = 32,
53 .period_bytes_max = 8192,
54 .periods_min = 1,
55 .periods_max = 52,
56 .buffer_bytes_max = 64 * 1024,
57 .fifo_size = 32,
58
59};
60
61static void audio_dma_irq(void *data)
62{
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;
66
67 iprtd->offset += iprtd->period_bytes;
68 iprtd->offset %= iprtd->period_bytes * iprtd->periods;
69 snd_pcm_period_elapsed(substream);
70}
71
72static bool filter(struct dma_chan *chan, void *param)
73{
74 struct mxs_pcm_runtime_data *iprtd = param;
75 struct mxs_pcm_dma_params *dma_params = iprtd->dma_params;
76
77 if (!mxs_dma_is_apbx(chan))
78 return false;
79
80 if (chan->chan_id != dma_params->chan_num)
81 return false;
82
83 chan->private = &iprtd->dma_data;
84
85 return true;
86}
87
Lars-Peter Clausen95a771c2012-02-22 10:49:07 +010088static int mxs_dma_alloc(struct snd_pcm_substream *substream)
Dong Aishenged6e1d02011-07-21 12:36:55 +080089{
90 struct snd_soc_pcm_runtime *rtd = substream->private_data;
91 struct snd_pcm_runtime *runtime = substream->runtime;
92 struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
93 dma_cap_mask_t mask;
94
95 iprtd->dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
96
97 dma_cap_zero(mask);
98 dma_cap_set(DMA_SLAVE, mask);
99 iprtd->dma_data.chan_irq = iprtd->dma_params->chan_irq;
100 iprtd->dma_chan = dma_request_channel(mask, filter, iprtd);
101 if (!iprtd->dma_chan)
102 return -EINVAL;
103
104 return 0;
105}
106
107static int snd_mxs_pcm_hw_params(struct snd_pcm_substream *substream,
108 struct snd_pcm_hw_params *params)
109{
110 struct snd_pcm_runtime *runtime = substream->runtime;
111 struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
112 unsigned long dma_addr;
113 struct dma_chan *chan;
Dong Aishenged6e1d02011-07-21 12:36:55 +0800114
Dong Aishenged6e1d02011-07-21 12:36:55 +0800115 chan = iprtd->dma_chan;
116
Dong Aishenged6e1d02011-07-21 12:36:55 +0800117 iprtd->periods = params_periods(params);
118 iprtd->period_bytes = params_period_bytes(params);
119 iprtd->offset = 0;
Dong Aishenged6e1d02011-07-21 12:36:55 +0800120
121 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
122
123 dma_addr = runtime->dma_addr;
124
Dong Aishenged6e1d02011-07-21 12:36:55 +0800125 iprtd->desc = chan->device->device_prep_dma_cyclic(chan, dma_addr,
126 iprtd->period_bytes * iprtd->periods,
127 iprtd->period_bytes,
128 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
Shawn Guod6077c92011-12-14 15:47:46 +0800129 DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
Dong Aishenged6e1d02011-07-21 12:36:55 +0800130 if (!iprtd->desc) {
131 dev_err(&chan->dev->device, "cannot prepare slave dma\n");
132 return -EINVAL;
133 }
134
135 iprtd->desc->callback = audio_dma_irq;
136 iprtd->desc->callback_param = substream;
137
138 return 0;
139}
140
Dong Aishenged6e1d02011-07-21 12:36:55 +0800141static int snd_mxs_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
142{
143 struct snd_pcm_runtime *runtime = substream->runtime;
144 struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
145
146 switch (cmd) {
147 case SNDRV_PCM_TRIGGER_START:
148 case SNDRV_PCM_TRIGGER_RESUME:
149 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
150 dmaengine_submit(iprtd->desc);
151
152 break;
153 case SNDRV_PCM_TRIGGER_STOP:
154 case SNDRV_PCM_TRIGGER_SUSPEND:
155 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
156 dmaengine_terminate_all(iprtd->dma_chan);
157
158 break;
159 default:
160 return -EINVAL;
161 }
162
163 return 0;
164}
165
166static snd_pcm_uframes_t snd_mxs_pcm_pointer(
167 struct snd_pcm_substream *substream)
168{
169 struct snd_pcm_runtime *runtime = substream->runtime;
170 struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
171
172 return bytes_to_frames(substream->runtime, iprtd->offset);
173}
174
175static int snd_mxs_open(struct snd_pcm_substream *substream)
176{
177 struct snd_pcm_runtime *runtime = substream->runtime;
178 struct mxs_pcm_runtime_data *iprtd;
179 int ret;
180
181 iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
182 if (iprtd == NULL)
183 return -ENOMEM;
184 runtime->private_data = iprtd;
185
186 ret = snd_pcm_hw_constraint_integer(substream->runtime,
187 SNDRV_PCM_HW_PARAM_PERIODS);
188 if (ret < 0) {
189 kfree(iprtd);
190 return ret;
191 }
192
Lars-Peter Clausen95a771c2012-02-22 10:49:07 +0100193 ret = mxs_dma_alloc(substream);
194 if (ret) {
195 kfree(iprtd);
196 return ret;
197 }
198
Dong Aishenged6e1d02011-07-21 12:36:55 +0800199 snd_soc_set_runtime_hwparams(substream, &snd_mxs_hardware);
200
201 return 0;
202}
203
204static int snd_mxs_close(struct snd_pcm_substream *substream)
205{
206 struct snd_pcm_runtime *runtime = substream->runtime;
207 struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
208
Lars-Peter Clausen95a771c2012-02-22 10:49:07 +0100209 dma_release_channel(iprtd->dma_chan);
Dong Aishenged6e1d02011-07-21 12:36:55 +0800210 kfree(iprtd);
211
212 return 0;
213}
214
215static int snd_mxs_pcm_mmap(struct snd_pcm_substream *substream,
216 struct vm_area_struct *vma)
217{
218 struct snd_pcm_runtime *runtime = substream->runtime;
219
220 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
221 runtime->dma_area,
222 runtime->dma_addr,
223 runtime->dma_bytes);
224}
225
226static struct snd_pcm_ops mxs_pcm_ops = {
227 .open = snd_mxs_open,
228 .close = snd_mxs_close,
229 .ioctl = snd_pcm_lib_ioctl,
230 .hw_params = snd_mxs_pcm_hw_params,
Dong Aishenged6e1d02011-07-21 12:36:55 +0800231 .trigger = snd_mxs_pcm_trigger,
232 .pointer = snd_mxs_pcm_pointer,
233 .mmap = snd_mxs_pcm_mmap,
234};
235
236static int mxs_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
237{
238 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
239 struct snd_dma_buffer *buf = &substream->dma_buffer;
240 size_t size = snd_mxs_hardware.buffer_bytes_max;
241
242 buf->dev.type = SNDRV_DMA_TYPE_DEV;
243 buf->dev.dev = pcm->card->dev;
244 buf->private_data = NULL;
245 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
246 &buf->addr, GFP_KERNEL);
247 if (!buf->area)
248 return -ENOMEM;
249 buf->bytes = size;
250
251 return 0;
252}
253
254static u64 mxs_pcm_dmamask = DMA_BIT_MASK(32);
255static int mxs_pcm_new(struct snd_soc_pcm_runtime *rtd)
256{
257 struct snd_card *card = rtd->card->snd_card;
258 struct snd_pcm *pcm = rtd->pcm;
259 int ret = 0;
260
261 if (!card->dev->dma_mask)
262 card->dev->dma_mask = &mxs_pcm_dmamask;
263 if (!card->dev->coherent_dma_mask)
264 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
265
266 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
267 ret = mxs_pcm_preallocate_dma_buffer(pcm,
268 SNDRV_PCM_STREAM_PLAYBACK);
269 if (ret)
270 goto out;
271 }
272
273 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
274 ret = mxs_pcm_preallocate_dma_buffer(pcm,
275 SNDRV_PCM_STREAM_CAPTURE);
276 if (ret)
277 goto out;
278 }
279
280out:
281 return ret;
282}
283
284static void mxs_pcm_free(struct snd_pcm *pcm)
285{
286 struct snd_pcm_substream *substream;
287 struct snd_dma_buffer *buf;
288 int stream;
289
290 for (stream = 0; stream < 2; stream++) {
291 substream = pcm->streams[stream].substream;
292 if (!substream)
293 continue;
294
295 buf = &substream->dma_buffer;
296 if (!buf->area)
297 continue;
298
299 dma_free_writecombine(pcm->card->dev, buf->bytes,
300 buf->area, buf->addr);
301 buf->area = NULL;
302 }
303}
304
305static struct snd_soc_platform_driver mxs_soc_platform = {
306 .ops = &mxs_pcm_ops,
307 .pcm_new = mxs_pcm_new,
308 .pcm_free = mxs_pcm_free,
309};
310
311static int __devinit mxs_soc_platform_probe(struct platform_device *pdev)
312{
313 return snd_soc_register_platform(&pdev->dev, &mxs_soc_platform);
314}
315
316static int __devexit mxs_soc_platform_remove(struct platform_device *pdev)
317{
318 snd_soc_unregister_platform(&pdev->dev);
319
320 return 0;
321}
322
323static struct platform_driver mxs_pcm_driver = {
324 .driver = {
325 .name = "mxs-pcm-audio",
326 .owner = THIS_MODULE,
327 },
328 .probe = mxs_soc_platform_probe,
329 .remove = __devexit_p(mxs_soc_platform_remove),
330};
331
Axel Lin85aa0962011-11-24 14:21:29 +0800332module_platform_driver(mxs_pcm_driver);
Lothar Waßmann06c8eb92011-12-09 14:38:11 +0100333
334MODULE_LICENSE("GPL");
Lothar Waßmann9fd369b2011-12-09 14:38:12 +0100335MODULE_ALIAS("platform:mxs-pcm-audio");