Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/nab/target...
[firefly-linux-kernel-4.4.55.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
25 #include <sound/core.h>
26 #include <sound/initval.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30
31 #include <mach/dma.h>
32
33 #include "imx-ssi.h"
34
35 struct imx_pcm_runtime_data {
36         int period_bytes;
37         int periods;
38         int dma;
39         unsigned long offset;
40         unsigned long size;
41         void *buf;
42         int period_time;
43         struct dma_async_tx_descriptor *desc;
44         struct dma_chan *dma_chan;
45         struct imx_dma_data dma_data;
46 };
47
48 static void audio_dma_irq(void *data)
49 {
50         struct snd_pcm_substream *substream = (struct snd_pcm_substream *)data;
51         struct snd_pcm_runtime *runtime = substream->runtime;
52         struct imx_pcm_runtime_data *iprtd = runtime->private_data;
53
54         iprtd->offset += iprtd->period_bytes;
55         iprtd->offset %= iprtd->period_bytes * iprtd->periods;
56
57         snd_pcm_period_elapsed(substream);
58 }
59
60 static bool filter(struct dma_chan *chan, void *param)
61 {
62         struct imx_pcm_runtime_data *iprtd = param;
63
64         if (!imx_dma_is_general_purpose(chan))
65                 return false;
66
67         chan->private = &iprtd->dma_data;
68
69         return true;
70 }
71
72 static int imx_ssi_dma_alloc(struct snd_pcm_substream *substream,
73                                 struct snd_pcm_hw_params *params)
74 {
75         struct snd_soc_pcm_runtime *rtd = substream->private_data;
76         struct imx_pcm_dma_params *dma_params;
77         struct snd_pcm_runtime *runtime = substream->runtime;
78         struct imx_pcm_runtime_data *iprtd = runtime->private_data;
79         struct dma_slave_config slave_config;
80         dma_cap_mask_t mask;
81         enum dma_slave_buswidth buswidth;
82         int ret;
83
84         dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
85
86         iprtd->dma_data.peripheral_type = IMX_DMATYPE_SSI;
87         iprtd->dma_data.priority = DMA_PRIO_HIGH;
88         iprtd->dma_data.dma_request = dma_params->dma;
89
90         /* Try to grab a DMA channel */
91         if (!iprtd->dma_chan) {
92                 dma_cap_zero(mask);
93                 dma_cap_set(DMA_SLAVE, mask);
94                 iprtd->dma_chan = dma_request_channel(mask, filter, iprtd);
95                 if (!iprtd->dma_chan)
96                         return -EINVAL;
97         }
98
99         switch (params_format(params)) {
100         case SNDRV_PCM_FORMAT_S16_LE:
101                 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
102                 break;
103         case SNDRV_PCM_FORMAT_S20_3LE:
104         case SNDRV_PCM_FORMAT_S24_LE:
105                 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
106                 break;
107         default:
108                 return 0;
109         }
110
111         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
112                 slave_config.direction = DMA_MEM_TO_DEV;
113                 slave_config.dst_addr = dma_params->dma_addr;
114                 slave_config.dst_addr_width = buswidth;
115                 slave_config.dst_maxburst = dma_params->burstsize;
116         } else {
117                 slave_config.direction = DMA_DEV_TO_MEM;
118                 slave_config.src_addr = dma_params->dma_addr;
119                 slave_config.src_addr_width = buswidth;
120                 slave_config.src_maxburst = dma_params->burstsize;
121         }
122
123         ret = dmaengine_slave_config(iprtd->dma_chan, &slave_config);
124         if (ret)
125                 return ret;
126
127         return 0;
128 }
129
130 static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
131                                 struct snd_pcm_hw_params *params)
132 {
133         struct snd_soc_pcm_runtime *rtd = substream->private_data;
134         struct snd_pcm_runtime *runtime = substream->runtime;
135         struct imx_pcm_runtime_data *iprtd = runtime->private_data;
136         unsigned long dma_addr;
137         struct dma_chan *chan;
138         struct imx_pcm_dma_params *dma_params;
139         int ret;
140
141         dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
142         ret = imx_ssi_dma_alloc(substream, params);
143         if (ret)
144                 return ret;
145         chan = iprtd->dma_chan;
146
147         iprtd->size = params_buffer_bytes(params);
148         iprtd->periods = params_periods(params);
149         iprtd->period_bytes = params_period_bytes(params);
150         iprtd->offset = 0;
151         iprtd->period_time = HZ / (params_rate(params) /
152                         params_period_size(params));
153
154         snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
155
156         dma_addr = runtime->dma_addr;
157
158         iprtd->buf = (unsigned int *)substream->dma_buffer.area;
159
160         iprtd->desc = chan->device->device_prep_dma_cyclic(chan, dma_addr,
161                         iprtd->period_bytes * iprtd->periods,
162                         iprtd->period_bytes,
163                         substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
164                         DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
165         if (!iprtd->desc) {
166                 dev_err(&chan->dev->device, "cannot prepare slave dma\n");
167                 return -EINVAL;
168         }
169
170         iprtd->desc->callback = audio_dma_irq;
171         iprtd->desc->callback_param = substream;
172
173         return 0;
174 }
175
176 static int snd_imx_pcm_hw_free(struct snd_pcm_substream *substream)
177 {
178         struct snd_pcm_runtime *runtime = substream->runtime;
179         struct imx_pcm_runtime_data *iprtd = runtime->private_data;
180
181         if (iprtd->dma_chan) {
182                 dma_release_channel(iprtd->dma_chan);
183                 iprtd->dma_chan = NULL;
184         }
185
186         return 0;
187 }
188
189 static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
190 {
191         struct snd_soc_pcm_runtime *rtd = substream->private_data;
192         struct imx_pcm_dma_params *dma_params;
193
194         dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
195
196         return 0;
197 }
198
199 static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
200 {
201         struct snd_pcm_runtime *runtime = substream->runtime;
202         struct imx_pcm_runtime_data *iprtd = runtime->private_data;
203
204         switch (cmd) {
205         case SNDRV_PCM_TRIGGER_START:
206         case SNDRV_PCM_TRIGGER_RESUME:
207         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
208                 dmaengine_submit(iprtd->desc);
209
210                 break;
211
212         case SNDRV_PCM_TRIGGER_STOP:
213         case SNDRV_PCM_TRIGGER_SUSPEND:
214         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
215                 dmaengine_terminate_all(iprtd->dma_chan);
216
217                 break;
218         default:
219                 return -EINVAL;
220         }
221
222         return 0;
223 }
224
225 static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream)
226 {
227         struct snd_pcm_runtime *runtime = substream->runtime;
228         struct imx_pcm_runtime_data *iprtd = runtime->private_data;
229
230         pr_debug("%s: %ld %ld\n", __func__, iprtd->offset,
231                         bytes_to_frames(substream->runtime, iprtd->offset));
232
233         return bytes_to_frames(substream->runtime, iprtd->offset);
234 }
235
236 static struct snd_pcm_hardware snd_imx_hardware = {
237         .info = SNDRV_PCM_INFO_INTERLEAVED |
238                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
239                 SNDRV_PCM_INFO_MMAP |
240                 SNDRV_PCM_INFO_MMAP_VALID |
241                 SNDRV_PCM_INFO_PAUSE |
242                 SNDRV_PCM_INFO_RESUME,
243         .formats = SNDRV_PCM_FMTBIT_S16_LE,
244         .rate_min = 8000,
245         .channels_min = 2,
246         .channels_max = 2,
247         .buffer_bytes_max = IMX_SSI_DMABUF_SIZE,
248         .period_bytes_min = 128,
249         .period_bytes_max = 65535, /* Limited by SDMA engine */
250         .periods_min = 2,
251         .periods_max = 255,
252         .fifo_size = 0,
253 };
254
255 static int snd_imx_open(struct snd_pcm_substream *substream)
256 {
257         struct snd_pcm_runtime *runtime = substream->runtime;
258         struct imx_pcm_runtime_data *iprtd;
259         int ret;
260
261         iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
262         if (iprtd == NULL)
263                 return -ENOMEM;
264         runtime->private_data = iprtd;
265
266         ret = snd_pcm_hw_constraint_integer(substream->runtime,
267                         SNDRV_PCM_HW_PARAM_PERIODS);
268         if (ret < 0) {
269                 kfree(iprtd);
270                 return ret;
271         }
272
273         snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
274
275         return 0;
276 }
277
278 static int snd_imx_close(struct snd_pcm_substream *substream)
279 {
280         struct snd_pcm_runtime *runtime = substream->runtime;
281         struct imx_pcm_runtime_data *iprtd = runtime->private_data;
282
283         kfree(iprtd);
284
285         return 0;
286 }
287
288 static struct snd_pcm_ops imx_pcm_ops = {
289         .open           = snd_imx_open,
290         .close          = snd_imx_close,
291         .ioctl          = snd_pcm_lib_ioctl,
292         .hw_params      = snd_imx_pcm_hw_params,
293         .hw_free        = snd_imx_pcm_hw_free,
294         .prepare        = snd_imx_pcm_prepare,
295         .trigger        = snd_imx_pcm_trigger,
296         .pointer        = snd_imx_pcm_pointer,
297         .mmap           = snd_imx_pcm_mmap,
298 };
299
300 static struct snd_soc_platform_driver imx_soc_platform_mx2 = {
301         .ops            = &imx_pcm_ops,
302         .pcm_new        = imx_pcm_new,
303         .pcm_free       = imx_pcm_free,
304 };
305
306 static int __devinit imx_soc_platform_probe(struct platform_device *pdev)
307 {
308         struct imx_ssi *ssi = platform_get_drvdata(pdev);
309
310         ssi->dma_params_tx.burstsize = 6;
311         ssi->dma_params_rx.burstsize = 4;
312
313         return snd_soc_register_platform(&pdev->dev, &imx_soc_platform_mx2);
314 }
315
316 static int __devexit imx_soc_platform_remove(struct platform_device *pdev)
317 {
318         snd_soc_unregister_platform(&pdev->dev);
319         return 0;
320 }
321
322 static struct platform_driver imx_pcm_driver = {
323         .driver = {
324                         .name = "imx-pcm-audio",
325                         .owner = THIS_MODULE,
326         },
327         .probe = imx_soc_platform_probe,
328         .remove = __devexit_p(imx_soc_platform_remove),
329 };
330
331 module_platform_driver(imx_pcm_driver);
332 MODULE_LICENSE("GPL");
333 MODULE_ALIAS("platform:imx-pcm-audio");