dmaengine: imx: fix the build failure on x86_64
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / soc / imx / imx-pcm-dma-mx2.c
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>
22 #include <linux/slab.h>
23 #include <linux/dmaengine.h>
24 #include <linux/types.h>
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
32 #include <mach/dma.h>
33
34 #include "imx-ssi.h"
35
36 struct imx_pcm_runtime_data {
37 int period_bytes;
38 int periods;
39 int dma;
40 unsigned long offset;
41 unsigned long size;
42 void *buf;
43 int period_time;
44 struct dma_async_tx_descriptor *desc;
45 struct dma_chan *dma_chan;
46 struct imx_dma_data dma_data;
47 };
48
49 static void audio_dma_irq(void *data)
50 {
51 struct snd_pcm_substream *substream = (struct snd_pcm_substream *)data;
52 struct snd_pcm_runtime *runtime = substream->runtime;
53 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
54
55 iprtd->offset += iprtd->period_bytes;
56 iprtd->offset %= iprtd->period_bytes * iprtd->periods;
57
58 snd_pcm_period_elapsed(substream);
59 }
60
61 static bool filter(struct dma_chan *chan, void *param)
62 {
63 struct imx_pcm_runtime_data *iprtd = param;
64
65 if (!imx_dma_is_general_purpose(chan))
66 return false;
67
68 chan->private = &iprtd->dma_data;
69
70 return true;
71 }
72
73 static int imx_ssi_dma_alloc(struct snd_pcm_substream *substream,
74 struct snd_pcm_hw_params *params)
75 {
76 struct snd_soc_pcm_runtime *rtd = substream->private_data;
77 struct imx_pcm_dma_params *dma_params;
78 struct snd_pcm_runtime *runtime = substream->runtime;
79 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
80 struct dma_slave_config slave_config;
81 dma_cap_mask_t mask;
82 enum dma_slave_buswidth buswidth;
83 int ret;
84
85 dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
86
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;
90
91 /* Try to grab a DMA channel */
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 }
99
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;
110 }
111
112 slave_config.device_fc = false;
113
114 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
115 slave_config.direction = DMA_MEM_TO_DEV;
116 slave_config.dst_addr = dma_params->dma_addr;
117 slave_config.dst_addr_width = buswidth;
118 slave_config.dst_maxburst = dma_params->burstsize;
119 } else {
120 slave_config.direction = DMA_DEV_TO_MEM;
121 slave_config.src_addr = dma_params->dma_addr;
122 slave_config.src_addr_width = buswidth;
123 slave_config.src_maxburst = dma_params->burstsize;
124 }
125
126 ret = dmaengine_slave_config(iprtd->dma_chan, &slave_config);
127 if (ret)
128 return ret;
129
130 return 0;
131 }
132
133 static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
134 struct snd_pcm_hw_params *params)
135 {
136 struct snd_soc_pcm_runtime *rtd = substream->private_data;
137 struct snd_pcm_runtime *runtime = substream->runtime;
138 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
139 unsigned long dma_addr;
140 struct dma_chan *chan;
141 struct imx_pcm_dma_params *dma_params;
142 int ret;
143
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;
149
150 iprtd->size = params_buffer_bytes(params);
151 iprtd->periods = params_periods(params);
152 iprtd->period_bytes = params_period_bytes(params);
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
159 dma_addr = runtime->dma_addr;
160
161 iprtd->buf = (unsigned int *)substream->dma_buffer.area;
162
163 iprtd->desc = dmaengine_prep_dma_cyclic(chan, dma_addr,
164 iprtd->period_bytes * iprtd->periods,
165 iprtd->period_bytes,
166 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
167 DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
168 if (!iprtd->desc) {
169 dev_err(&chan->dev->device, "cannot prepare slave dma\n");
170 return -EINVAL;
171 }
172
173 iprtd->desc->callback = audio_dma_irq;
174 iprtd->desc->callback_param = substream;
175
176 return 0;
177 }
178
179 static 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
184 if (iprtd->dma_chan) {
185 dma_release_channel(iprtd->dma_chan);
186 iprtd->dma_chan = NULL;
187 }
188
189 return 0;
190 }
191
192 static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
193 {
194 struct snd_soc_pcm_runtime *rtd = substream->private_data;
195 struct imx_pcm_dma_params *dma_params;
196
197 dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
198
199 return 0;
200 }
201
202 static 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:
211 dmaengine_submit(iprtd->desc);
212 dma_async_issue_pending(iprtd->dma_chan);
213
214 break;
215
216 case SNDRV_PCM_TRIGGER_STOP:
217 case SNDRV_PCM_TRIGGER_SUSPEND:
218 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
219 dmaengine_terminate_all(iprtd->dma_chan);
220
221 break;
222 default:
223 return -EINVAL;
224 }
225
226 return 0;
227 }
228
229 static 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
234 pr_debug("%s: %ld %ld\n", __func__, iprtd->offset,
235 bytes_to_frames(substream->runtime, iprtd->offset));
236
237 return bytes_to_frames(substream->runtime, iprtd->offset);
238 }
239
240 static 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,
253 .period_bytes_max = 65535, /* Limited by SDMA engine */
254 .periods_min = 2,
255 .periods_max = 255,
256 .fifo_size = 0,
257 };
258
259 static 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);
266 if (iprtd == NULL)
267 return -ENOMEM;
268 runtime->private_data = iprtd;
269
270 ret = snd_pcm_hw_constraint_integer(substream->runtime,
271 SNDRV_PCM_HW_PARAM_PERIODS);
272 if (ret < 0) {
273 kfree(iprtd);
274 return ret;
275 }
276
277 snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
278
279 return 0;
280 }
281
282 static 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
289 return 0;
290 }
291
292 static struct snd_pcm_ops imx_pcm_ops = {
293 .open = snd_imx_open,
294 .close = snd_imx_close,
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
304 static struct snd_soc_platform_driver imx_soc_platform_mx2 = {
305 .ops = &imx_pcm_ops,
306 .pcm_new = imx_pcm_new,
307 .pcm_free = imx_pcm_free,
308 };
309
310 static int __devinit imx_soc_platform_probe(struct platform_device *pdev)
311 {
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
317 return snd_soc_register_platform(&pdev->dev, &imx_soc_platform_mx2);
318 }
319
320 static int __devexit imx_soc_platform_remove(struct platform_device *pdev)
321 {
322 snd_soc_unregister_platform(&pdev->dev);
323 return 0;
324 }
325
326 static struct platform_driver imx_pcm_driver = {
327 .driver = {
328 .name = "imx-pcm-audio",
329 .owner = THIS_MODULE,
330 },
331 .probe = imx_soc_platform_probe,
332 .remove = __devexit_p(imx_soc_platform_remove),
333 };
334
335 module_platform_driver(imx_pcm_driver);
336 MODULE_LICENSE("GPL");
337 MODULE_ALIAS("platform:imx-pcm-audio");