blob: 52b0dedbf99641b7d99d05047dba5b66b31c5dd3 [file] [log] [blame]
Sascha Hauer83802222009-11-25 16:41:04 +01001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Sascha Hauerbf974a02010-11-04 17:05:43 +010023#include <linux/dmaengine.h>
Viresh Kumar258aea72012-02-01 16:12:19 +053024#include <linux/types.h>
Sascha Hauer83802222009-11-25 16:41:04 +010025
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
Sascha Hauerbf974a02010-11-04 17:05:43 +010032#include <mach/dma.h>
Sascha Hauer83802222009-11-25 16:41:04 +010033
34#include "imx-ssi.h"
35
36struct imx_pcm_runtime_data {
Sascha Hauerbf974a02010-11-04 17:05:43 +010037 int period_bytes;
Sascha Hauer83802222009-11-25 16:41:04 +010038 int periods;
Sascha Hauer83802222009-11-25 16:41:04 +010039 int dma;
Sascha Hauer83802222009-11-25 16:41:04 +010040 unsigned long offset;
41 unsigned long size;
Sascha Hauer83802222009-11-25 16:41:04 +010042 void *buf;
43 int period_time;
Sascha Hauerbf974a02010-11-04 17:05:43 +010044 struct dma_async_tx_descriptor *desc;
45 struct dma_chan *dma_chan;
46 struct imx_dma_data dma_data;
Sascha Hauer83802222009-11-25 16:41:04 +010047};
48
Sascha Hauerbf974a02010-11-04 17:05:43 +010049static void audio_dma_irq(void *data)
Sascha Hauer83802222009-11-25 16:41:04 +010050{
Sascha Hauerbf974a02010-11-04 17:05:43 +010051 struct snd_pcm_substream *substream = (struct snd_pcm_substream *)data;
Sascha Hauer83802222009-11-25 16:41:04 +010052 struct snd_pcm_runtime *runtime = substream->runtime;
53 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
54
Sascha Hauerbf974a02010-11-04 17:05:43 +010055 iprtd->offset += iprtd->period_bytes;
56 iprtd->offset %= iprtd->period_bytes * iprtd->periods;
Sascha Hauer83802222009-11-25 16:41:04 +010057
Sascha Hauerbf974a02010-11-04 17:05:43 +010058 snd_pcm_period_elapsed(substream);
Sascha Hauer83802222009-11-25 16:41:04 +010059}
60
Sascha Hauerbf974a02010-11-04 17:05:43 +010061static bool filter(struct dma_chan *chan, void *param)
Sascha Hauer83802222009-11-25 16:41:04 +010062{
Sascha Hauerbf974a02010-11-04 17:05:43 +010063 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;
Sascha Hauer83802222009-11-25 16:41:04 +010071}
72
Sascha Hauerbf974a02010-11-04 17:05:43 +010073static int imx_ssi_dma_alloc(struct snd_pcm_substream *substream,
74 struct snd_pcm_hw_params *params)
Sascha Hauer83802222009-11-25 16:41:04 +010075{
76 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Daniel Mack5f712b22010-03-22 10:11:15 +010077 struct imx_pcm_dma_params *dma_params;
Sascha Hauer83802222009-11-25 16:41:04 +010078 struct snd_pcm_runtime *runtime = substream->runtime;
79 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
Sascha Hauerbf974a02010-11-04 17:05:43 +010080 struct dma_slave_config slave_config;
81 dma_cap_mask_t mask;
82 enum dma_slave_buswidth buswidth;
Sascha Hauer83802222009-11-25 16:41:04 +010083 int ret;
84
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000085 dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Daniel Mack5f712b22010-03-22 10:11:15 +010086
Sascha Hauerbf974a02010-11-04 17:05:43 +010087 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 */
Javier Martin220d9f22012-01-12 12:30:38 +010092 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 }
Sascha Hauerbf974a02010-11-04 17:05:43 +010099
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;
Sascha Hauer83802222009-11-25 16:41:04 +0100110 }
111
Viresh Kumar258aea72012-02-01 16:12:19 +0530112 slave_config.device_fc = false;
113
Sascha Hauerbf974a02010-11-04 17:05:43 +0100114 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Vinod Koul35e16582011-10-14 10:49:30 +0530115 slave_config.direction = DMA_MEM_TO_DEV;
Sascha Hauerbf974a02010-11-04 17:05:43 +0100116 slave_config.dst_addr = dma_params->dma_addr;
117 slave_config.dst_addr_width = buswidth;
Sascha Hauer6584cb82011-07-06 11:18:33 +0200118 slave_config.dst_maxburst = dma_params->burstsize;
Sascha Hauerbf974a02010-11-04 17:05:43 +0100119 } else {
Vinod Koul35e16582011-10-14 10:49:30 +0530120 slave_config.direction = DMA_DEV_TO_MEM;
Sascha Hauerbf974a02010-11-04 17:05:43 +0100121 slave_config.src_addr = dma_params->dma_addr;
122 slave_config.src_addr_width = buswidth;
Sascha Hauer6584cb82011-07-06 11:18:33 +0200123 slave_config.src_maxburst = dma_params->burstsize;
Sascha Hauerbf974a02010-11-04 17:05:43 +0100124 }
125
126 ret = dmaengine_slave_config(iprtd->dma_chan, &slave_config);
Sascha Hauer83802222009-11-25 16:41:04 +0100127 if (ret)
Sascha Hauerbf974a02010-11-04 17:05:43 +0100128 return ret;
Sascha Hauer83802222009-11-25 16:41:04 +0100129
130 return 0;
Sascha Hauer83802222009-11-25 16:41:04 +0100131}
132
133static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
134 struct snd_pcm_hw_params *params)
135{
Sascha Hauerbf974a02010-11-04 17:05:43 +0100136 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Sascha Hauer83802222009-11-25 16:41:04 +0100137 struct snd_pcm_runtime *runtime = substream->runtime;
138 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
Sascha Hauer83802222009-11-25 16:41:04 +0100139 unsigned long dma_addr;
Sascha Hauerbf974a02010-11-04 17:05:43 +0100140 struct dma_chan *chan;
141 struct imx_pcm_dma_params *dma_params;
142 int ret;
Sascha Hauer83802222009-11-25 16:41:04 +0100143
Sascha Hauerbf974a02010-11-04 17:05:43 +0100144 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;
Sascha Hauer83802222009-11-25 16:41:04 +0100149
150 iprtd->size = params_buffer_bytes(params);
151 iprtd->periods = params_periods(params);
Sascha Hauerbf974a02010-11-04 17:05:43 +0100152 iprtd->period_bytes = params_period_bytes(params);
Sascha Hauer83802222009-11-25 16:41:04 +0100153 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
Sascha Hauer83802222009-11-25 16:41:04 +0100159 dma_addr = runtime->dma_addr;
160
Sascha Hauerbf974a02010-11-04 17:05:43 +0100161 iprtd->buf = (unsigned int *)substream->dma_buffer.area;
162
163 iprtd->desc = chan->device->device_prep_dma_cyclic(chan, dma_addr,
164 iprtd->period_bytes * iprtd->periods,
165 iprtd->period_bytes,
166 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
Vinod Koul35e16582011-10-14 10:49:30 +0530167 DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
Sascha Hauerbf974a02010-11-04 17:05:43 +0100168 if (!iprtd->desc) {
169 dev_err(&chan->dev->device, "cannot prepare slave dma\n");
170 return -EINVAL;
Sascha Hauer83802222009-11-25 16:41:04 +0100171 }
172
Sascha Hauerbf974a02010-11-04 17:05:43 +0100173 iprtd->desc->callback = audio_dma_irq;
174 iprtd->desc->callback_param = substream;
175
Sascha Hauer83802222009-11-25 16:41:04 +0100176 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
Sascha Hauerbf974a02010-11-04 17:05:43 +0100184 if (iprtd->dma_chan) {
185 dma_release_channel(iprtd->dma_chan);
186 iprtd->dma_chan = NULL;
Sascha Hauer83802222009-11-25 16:41:04 +0100187 }
188
Sascha Hauer83802222009-11-25 16:41:04 +0100189 return 0;
190}
191
192static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
193{
Sascha Hauer83802222009-11-25 16:41:04 +0100194 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Daniel Mack5f712b22010-03-22 10:11:15 +0100195 struct imx_pcm_dma_params *dma_params;
Sascha Hauer83802222009-11-25 16:41:04 +0100196
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000197 dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Daniel Mack5f712b22010-03-22 10:11:15 +0100198
Sascha Hauer83802222009-11-25 16:41:04 +0100199 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:
Sascha Hauerbf974a02010-11-04 17:05:43 +0100211 dmaengine_submit(iprtd->desc);
Sascha Hauerc0fa6c82012-01-09 10:32:48 +0100212 dma_async_issue_pending(iprtd->dma_chan);
Sascha Hauer83802222009-11-25 16:41:04 +0100213
214 break;
215
216 case SNDRV_PCM_TRIGGER_STOP:
217 case SNDRV_PCM_TRIGGER_SUSPEND:
218 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Sascha Hauerbf974a02010-11-04 17:05:43 +0100219 dmaengine_terminate_all(iprtd->dma_chan);
Sascha Hauer83802222009-11-25 16:41:04 +0100220
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
Sascha Hauerbf974a02010-11-04 17:05:43 +0100234 pr_debug("%s: %ld %ld\n", __func__, iprtd->offset,
235 bytes_to_frames(substream->runtime, iprtd->offset));
236
Sascha Hauer83802222009-11-25 16:41:04 +0100237 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,
Sascha Hauerbf974a02010-11-04 17:05:43 +0100253 .period_bytes_max = 65535, /* Limited by SDMA engine */
Sascha Hauer83802222009-11-25 16:41:04 +0100254 .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);
Kulikov Vasiliy51b6dfb2010-07-16 20:16:34 +0400266 if (iprtd == NULL)
267 return -ENOMEM;
Sascha Hauer83802222009-11-25 16:41:04 +0100268 runtime->private_data = iprtd;
269
270 ret = snd_pcm_hw_constraint_integer(substream->runtime,
271 SNDRV_PCM_HW_PARAM_PERIODS);
Kulikov Vasiliy51b6dfb2010-07-16 20:16:34 +0400272 if (ret < 0) {
273 kfree(iprtd);
Sascha Hauer83802222009-11-25 16:41:04 +0100274 return ret;
Kulikov Vasiliy51b6dfb2010-07-16 20:16:34 +0400275 }
Sascha Hauer83802222009-11-25 16:41:04 +0100276
277 snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
Sascha Hauerbf974a02010-11-04 17:05:43 +0100278
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
Sascha Hauer83802222009-11-25 16:41:04 +0100289 return 0;
290}
291
292static struct snd_pcm_ops imx_pcm_ops = {
293 .open = snd_imx_open,
Sascha Hauerbf974a02010-11-04 17:05:43 +0100294 .close = snd_imx_close,
Sascha Hauer83802222009-11-25 16:41:04 +0100295 .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
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000304static struct snd_soc_platform_driver imx_soc_platform_mx2 = {
305 .ops = &imx_pcm_ops,
Sascha Hauer83802222009-11-25 16:41:04 +0100306 .pcm_new = imx_pcm_new,
307 .pcm_free = imx_pcm_free,
308};
309
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000310static int __devinit imx_soc_platform_probe(struct platform_device *pdev)
Sascha Hauer83802222009-11-25 16:41:04 +0100311{
Wolfram Sang2c4cf172011-03-25 16:51:44 +0100312 struct imx_ssi *ssi = platform_get_drvdata(pdev);
313
314 ssi->dma_params_tx.burstsize = 6;
315 ssi->dma_params_rx.burstsize = 4;
316
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000317 return snd_soc_register_platform(&pdev->dev, &imx_soc_platform_mx2);
Sascha Hauer83802222009-11-25 16:41:04 +0100318}
319
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000320static 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 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000331 .probe = imx_soc_platform_probe,
332 .remove = __devexit_p(imx_soc_platform_remove),
333};
334
Axel Lin7a24b2b2011-11-24 15:03:50 +0800335module_platform_driver(imx_pcm_driver);
Arnaud Patard (Rtp)96dcabb2011-06-22 22:21:49 +0200336MODULE_LICENSE("GPL");
337MODULE_ALIAS("platform:imx-pcm-audio");