dmaengine/dma_slave: introduce inline wrappers
[linux-2.6-block.git] / sound / soc / imx / imx-pcm-dma-mx2.c
CommitLineData
8380222e
SH
1/*
2 * imx-pcm-dma-mx2.c -- ALSA Soc Audio Layer
3 *
4 * Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
5 *
6 * This code is based on code copyrighted by Freescale,
7 * Liam Girdwood, Javier Martin and probably others.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 */
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/device.h>
17#include <linux/dma-mapping.h>
18#include <linux/init.h>
19#include <linux/interrupt.h>
20#include <linux/module.h>
21#include <linux/platform_device.h>
5a0e3ad6 22#include <linux/slab.h>
bf974a0d 23#include <linux/dmaengine.h>
258aea76 24#include <linux/types.h>
8380222e
SH
25
26#include <sound/core.h>
27#include <sound/initval.h>
28#include <sound/pcm.h>
29#include <sound/pcm_params.h>
30#include <sound/soc.h>
31
bf974a0d 32#include <mach/dma.h>
8380222e
SH
33
34#include "imx-ssi.h"
35
36struct imx_pcm_runtime_data {
bf974a0d 37 int period_bytes;
8380222e 38 int periods;
8380222e 39 int dma;
8380222e
SH
40 unsigned long offset;
41 unsigned long size;
8380222e
SH
42 void *buf;
43 int period_time;
bf974a0d
SH
44 struct dma_async_tx_descriptor *desc;
45 struct dma_chan *dma_chan;
46 struct imx_dma_data dma_data;
8380222e
SH
47};
48
bf974a0d 49static void audio_dma_irq(void *data)
8380222e 50{
bf974a0d 51 struct snd_pcm_substream *substream = (struct snd_pcm_substream *)data;
8380222e
SH
52 struct snd_pcm_runtime *runtime = substream->runtime;
53 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
54
bf974a0d
SH
55 iprtd->offset += iprtd->period_bytes;
56 iprtd->offset %= iprtd->period_bytes * iprtd->periods;
8380222e 57
bf974a0d 58 snd_pcm_period_elapsed(substream);
8380222e
SH
59}
60
bf974a0d 61static bool filter(struct dma_chan *chan, void *param)
8380222e 62{
bf974a0d 63 struct imx_pcm_runtime_data *iprtd = param;
8380222e 64
bf974a0d
SH
65 if (!imx_dma_is_general_purpose(chan))
66 return false;
8380222e 67
bf974a0d 68 chan->private = &iprtd->dma_data;
671999cb 69
bf974a0d 70 return true;
8380222e
SH
71}
72
bf974a0d
SH
73static int imx_ssi_dma_alloc(struct snd_pcm_substream *substream,
74 struct snd_pcm_hw_params *params)
8380222e
SH
75{
76 struct snd_soc_pcm_runtime *rtd = substream->private_data;
5f712b2b 77 struct imx_pcm_dma_params *dma_params;
8380222e
SH
78 struct snd_pcm_runtime *runtime = substream->runtime;
79 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
bf974a0d
SH
80 struct dma_slave_config slave_config;
81 dma_cap_mask_t mask;
82 enum dma_slave_buswidth buswidth;
8380222e
SH
83 int ret;
84
f0fba2ad 85 dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
5f712b2b 86
bf974a0d
SH
87 iprtd->dma_data.peripheral_type = IMX_DMATYPE_SSI;
88 iprtd->dma_data.priority = DMA_PRIO_HIGH;
89 iprtd->dma_data.dma_request = dma_params->dma;
8380222e 90
bf974a0d 91 /* Try to grab a DMA channel */
220d9f25
JM
92 if (!iprtd->dma_chan) {
93 dma_cap_zero(mask);
94 dma_cap_set(DMA_SLAVE, mask);
95 iprtd->dma_chan = dma_request_channel(mask, filter, iprtd);
96 if (!iprtd->dma_chan)
97 return -EINVAL;
98 }
8380222e 99
bf974a0d
SH
100 switch (params_format(params)) {
101 case SNDRV_PCM_FORMAT_S16_LE:
102 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
103 break;
104 case SNDRV_PCM_FORMAT_S20_3LE:
105 case SNDRV_PCM_FORMAT_S24_LE:
106 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
107 break;
108 default:
109 return 0;
8380222e
SH
110 }
111
258aea76
VK
112 slave_config.device_fc = false;
113
bf974a0d 114 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
35e16581 115 slave_config.direction = DMA_MEM_TO_DEV;
bf974a0d
SH
116 slave_config.dst_addr = dma_params->dma_addr;
117 slave_config.dst_addr_width = buswidth;
6584cb88 118 slave_config.dst_maxburst = dma_params->burstsize;
bf974a0d 119 } else {
35e16581 120 slave_config.direction = DMA_DEV_TO_MEM;
bf974a0d
SH
121 slave_config.src_addr = dma_params->dma_addr;
122 slave_config.src_addr_width = buswidth;
6584cb88 123 slave_config.src_maxburst = dma_params->burstsize;
8380222e
SH
124 }
125
bf974a0d
SH
126 ret = dmaengine_slave_config(iprtd->dma_chan, &slave_config);
127 if (ret)
128 return ret;
8380222e
SH
129
130 return 0;
8380222e
SH
131}
132
133static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
134 struct snd_pcm_hw_params *params)
135{
bf974a0d 136 struct snd_soc_pcm_runtime *rtd = substream->private_data;
8380222e
SH
137 struct snd_pcm_runtime *runtime = substream->runtime;
138 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
8380222e 139 unsigned long dma_addr;
bf974a0d
SH
140 struct dma_chan *chan;
141 struct imx_pcm_dma_params *dma_params;
142 int ret;
8380222e 143
bf974a0d
SH
144 dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
145 ret = imx_ssi_dma_alloc(substream, params);
146 if (ret)
147 return ret;
148 chan = iprtd->dma_chan;
8380222e
SH
149
150 iprtd->size = params_buffer_bytes(params);
151 iprtd->periods = params_periods(params);
bf974a0d 152 iprtd->period_bytes = params_period_bytes(params);
8380222e
SH
153 iprtd->offset = 0;
154 iprtd->period_time = HZ / (params_rate(params) /
155 params_period_size(params));
156
157 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
158
8380222e
SH
159 dma_addr = runtime->dma_addr;
160
bf974a0d
SH
161 iprtd->buf = (unsigned int *)substream->dma_buffer.area;
162
16052827 163 iprtd->desc = dmaengine_prep_dma_cyclic(chan, dma_addr,
bf974a0d
SH
164 iprtd->period_bytes * iprtd->periods,
165 iprtd->period_bytes,
166 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
35e16581 167 DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
bf974a0d
SH
168 if (!iprtd->desc) {
169 dev_err(&chan->dev->device, "cannot prepare slave dma\n");
170 return -EINVAL;
8380222e
SH
171 }
172
bf974a0d
SH
173 iprtd->desc->callback = audio_dma_irq;
174 iprtd->desc->callback_param = substream;
175
8380222e
SH
176 return 0;
177}
178
179static int snd_imx_pcm_hw_free(struct snd_pcm_substream *substream)
180{
181 struct snd_pcm_runtime *runtime = substream->runtime;
182 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
183
bf974a0d
SH
184 if (iprtd->dma_chan) {
185 dma_release_channel(iprtd->dma_chan);
186 iprtd->dma_chan = NULL;
8380222e
SH
187 }
188
8380222e
SH
189 return 0;
190}
191
192static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
193{
8380222e 194 struct snd_soc_pcm_runtime *rtd = substream->private_data;
5f712b2b 195 struct imx_pcm_dma_params *dma_params;
8380222e 196
f0fba2ad 197 dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
5f712b2b 198
8380222e
SH
199 return 0;
200}
201
202static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
203{
204 struct snd_pcm_runtime *runtime = substream->runtime;
205 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
206
207 switch (cmd) {
208 case SNDRV_PCM_TRIGGER_START:
209 case SNDRV_PCM_TRIGGER_RESUME:
210 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
bf974a0d 211 dmaengine_submit(iprtd->desc);
c0fa6c8a 212 dma_async_issue_pending(iprtd->dma_chan);
8380222e
SH
213
214 break;
215
216 case SNDRV_PCM_TRIGGER_STOP:
217 case SNDRV_PCM_TRIGGER_SUSPEND:
218 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
bf974a0d 219 dmaengine_terminate_all(iprtd->dma_chan);
8380222e
SH
220
221 break;
222 default:
223 return -EINVAL;
224 }
225
226 return 0;
227}
228
229static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream)
230{
231 struct snd_pcm_runtime *runtime = substream->runtime;
232 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
233
bf974a0d
SH
234 pr_debug("%s: %ld %ld\n", __func__, iprtd->offset,
235 bytes_to_frames(substream->runtime, iprtd->offset));
236
8380222e
SH
237 return bytes_to_frames(substream->runtime, iprtd->offset);
238}
239
240static struct snd_pcm_hardware snd_imx_hardware = {
241 .info = SNDRV_PCM_INFO_INTERLEAVED |
242 SNDRV_PCM_INFO_BLOCK_TRANSFER |
243 SNDRV_PCM_INFO_MMAP |
244 SNDRV_PCM_INFO_MMAP_VALID |
245 SNDRV_PCM_INFO_PAUSE |
246 SNDRV_PCM_INFO_RESUME,
247 .formats = SNDRV_PCM_FMTBIT_S16_LE,
248 .rate_min = 8000,
249 .channels_min = 2,
250 .channels_max = 2,
251 .buffer_bytes_max = IMX_SSI_DMABUF_SIZE,
252 .period_bytes_min = 128,
bf974a0d 253 .period_bytes_max = 65535, /* Limited by SDMA engine */
8380222e
SH
254 .periods_min = 2,
255 .periods_max = 255,
256 .fifo_size = 0,
257};
258
259static int snd_imx_open(struct snd_pcm_substream *substream)
260{
261 struct snd_pcm_runtime *runtime = substream->runtime;
262 struct imx_pcm_runtime_data *iprtd;
263 int ret;
264
265 iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
51b6dfb6
KV
266 if (iprtd == NULL)
267 return -ENOMEM;
8380222e
SH
268 runtime->private_data = iprtd;
269
270 ret = snd_pcm_hw_constraint_integer(substream->runtime,
271 SNDRV_PCM_HW_PARAM_PERIODS);
51b6dfb6
KV
272 if (ret < 0) {
273 kfree(iprtd);
8380222e 274 return ret;
51b6dfb6 275 }
8380222e
SH
276
277 snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
bf974a0d
SH
278
279 return 0;
280}
281
282static int snd_imx_close(struct snd_pcm_substream *substream)
283{
284 struct snd_pcm_runtime *runtime = substream->runtime;
285 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
286
287 kfree(iprtd);
288
8380222e
SH
289 return 0;
290}
291
292static struct snd_pcm_ops imx_pcm_ops = {
293 .open = snd_imx_open,
bf974a0d 294 .close = snd_imx_close,
8380222e
SH
295 .ioctl = snd_pcm_lib_ioctl,
296 .hw_params = snd_imx_pcm_hw_params,
297 .hw_free = snd_imx_pcm_hw_free,
298 .prepare = snd_imx_pcm_prepare,
299 .trigger = snd_imx_pcm_trigger,
300 .pointer = snd_imx_pcm_pointer,
301 .mmap = snd_imx_pcm_mmap,
302};
303
f0fba2ad
LG
304static struct snd_soc_platform_driver imx_soc_platform_mx2 = {
305 .ops = &imx_pcm_ops,
8380222e
SH
306 .pcm_new = imx_pcm_new,
307 .pcm_free = imx_pcm_free,
308};
309
f0fba2ad 310static int __devinit imx_soc_platform_probe(struct platform_device *pdev)
8380222e 311{
2c4cf17a
WS
312 struct imx_ssi *ssi = platform_get_drvdata(pdev);
313
314 ssi->dma_params_tx.burstsize = 6;
315 ssi->dma_params_rx.burstsize = 4;
316
f0fba2ad
LG
317 return snd_soc_register_platform(&pdev->dev, &imx_soc_platform_mx2);
318}
319
320static int __devexit imx_soc_platform_remove(struct platform_device *pdev)
321{
322 snd_soc_unregister_platform(&pdev->dev);
323 return 0;
324}
325
326static struct platform_driver imx_pcm_driver = {
327 .driver = {
328 .name = "imx-pcm-audio",
329 .owner = THIS_MODULE,
330 },
f0fba2ad
LG
331 .probe = imx_soc_platform_probe,
332 .remove = __devexit_p(imx_soc_platform_remove),
333};
334
7a24b2ba 335module_platform_driver(imx_pcm_driver);
96dcabb9
APR
336MODULE_LICENSE("GPL");
337MODULE_ALIAS("platform:imx-pcm-audio");