blob: 5780c9b9d569cf78f82edb63104322c896b7c234 [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>
Sascha Hauer83802222009-11-25 16:41:04 +010024
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
Sascha Hauerbf974a02010-11-04 17:05:43 +010031#include <mach/dma.h>
Sascha Hauer83802222009-11-25 16:41:04 +010032
33#include "imx-ssi.h"
34
35struct imx_pcm_runtime_data {
Sascha Hauerbf974a02010-11-04 17:05:43 +010036 int period_bytes;
Sascha Hauer83802222009-11-25 16:41:04 +010037 int periods;
Sascha Hauer83802222009-11-25 16:41:04 +010038 int dma;
Sascha Hauer83802222009-11-25 16:41:04 +010039 unsigned long offset;
40 unsigned long size;
Sascha Hauer83802222009-11-25 16:41:04 +010041 void *buf;
42 int period_time;
Sascha Hauerbf974a02010-11-04 17:05:43 +010043 struct dma_async_tx_descriptor *desc;
44 struct dma_chan *dma_chan;
45 struct imx_dma_data dma_data;
Sascha Hauer83802222009-11-25 16:41:04 +010046};
47
Sascha Hauerbf974a02010-11-04 17:05:43 +010048static void audio_dma_irq(void *data)
Sascha Hauer83802222009-11-25 16:41:04 +010049{
Sascha Hauerbf974a02010-11-04 17:05:43 +010050 struct snd_pcm_substream *substream = (struct snd_pcm_substream *)data;
Sascha Hauer83802222009-11-25 16:41:04 +010051 struct snd_pcm_runtime *runtime = substream->runtime;
52 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
53
Sascha Hauerbf974a02010-11-04 17:05:43 +010054 iprtd->offset += iprtd->period_bytes;
55 iprtd->offset %= iprtd->period_bytes * iprtd->periods;
Sascha Hauer83802222009-11-25 16:41:04 +010056
Sascha Hauerbf974a02010-11-04 17:05:43 +010057 snd_pcm_period_elapsed(substream);
Sascha Hauer83802222009-11-25 16:41:04 +010058}
59
Sascha Hauerbf974a02010-11-04 17:05:43 +010060static bool filter(struct dma_chan *chan, void *param)
Sascha Hauer83802222009-11-25 16:41:04 +010061{
Sascha Hauerbf974a02010-11-04 17:05:43 +010062 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;
Sascha Hauer83802222009-11-25 16:41:04 +010070}
71
Sascha Hauerbf974a02010-11-04 17:05:43 +010072static int imx_ssi_dma_alloc(struct snd_pcm_substream *substream,
73 struct snd_pcm_hw_params *params)
Sascha Hauer83802222009-11-25 16:41:04 +010074{
75 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Daniel Mack5f712b22010-03-22 10:11:15 +010076 struct imx_pcm_dma_params *dma_params;
Sascha Hauer83802222009-11-25 16:41:04 +010077 struct snd_pcm_runtime *runtime = substream->runtime;
78 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
Sascha Hauerbf974a02010-11-04 17:05:43 +010079 struct dma_slave_config slave_config;
80 dma_cap_mask_t mask;
81 enum dma_slave_buswidth buswidth;
Sascha Hauer83802222009-11-25 16:41:04 +010082 int ret;
83
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000084 dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Daniel Mack5f712b22010-03-22 10:11:15 +010085
Sascha Hauerbf974a02010-11-04 17:05:43 +010086 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 */
Javier Martin220d9f22012-01-12 12:30:38 +010091 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 }
Sascha Hauerbf974a02010-11-04 17:05:43 +010098
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;
Sascha Hauer83802222009-11-25 16:41:04 +0100109 }
110
Sascha Hauerbf974a02010-11-04 17:05:43 +0100111 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Vinod Koul35e16582011-10-14 10:49:30 +0530112 slave_config.direction = DMA_MEM_TO_DEV;
Sascha Hauerbf974a02010-11-04 17:05:43 +0100113 slave_config.dst_addr = dma_params->dma_addr;
114 slave_config.dst_addr_width = buswidth;
Sascha Hauer6584cb82011-07-06 11:18:33 +0200115 slave_config.dst_maxburst = dma_params->burstsize;
Sascha Hauerbf974a02010-11-04 17:05:43 +0100116 } else {
Vinod Koul35e16582011-10-14 10:49:30 +0530117 slave_config.direction = DMA_DEV_TO_MEM;
Sascha Hauerbf974a02010-11-04 17:05:43 +0100118 slave_config.src_addr = dma_params->dma_addr;
119 slave_config.src_addr_width = buswidth;
Sascha Hauer6584cb82011-07-06 11:18:33 +0200120 slave_config.src_maxburst = dma_params->burstsize;
Sascha Hauerbf974a02010-11-04 17:05:43 +0100121 }
122
123 ret = dmaengine_slave_config(iprtd->dma_chan, &slave_config);
Sascha Hauer83802222009-11-25 16:41:04 +0100124 if (ret)
Sascha Hauerbf974a02010-11-04 17:05:43 +0100125 return ret;
Sascha Hauer83802222009-11-25 16:41:04 +0100126
127 return 0;
Sascha Hauer83802222009-11-25 16:41:04 +0100128}
129
130static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
131 struct snd_pcm_hw_params *params)
132{
Sascha Hauerbf974a02010-11-04 17:05:43 +0100133 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Sascha Hauer83802222009-11-25 16:41:04 +0100134 struct snd_pcm_runtime *runtime = substream->runtime;
135 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
Sascha Hauer83802222009-11-25 16:41:04 +0100136 unsigned long dma_addr;
Sascha Hauerbf974a02010-11-04 17:05:43 +0100137 struct dma_chan *chan;
138 struct imx_pcm_dma_params *dma_params;
139 int ret;
Sascha Hauer83802222009-11-25 16:41:04 +0100140
Sascha Hauerbf974a02010-11-04 17:05:43 +0100141 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;
Sascha Hauer83802222009-11-25 16:41:04 +0100146
147 iprtd->size = params_buffer_bytes(params);
148 iprtd->periods = params_periods(params);
Sascha Hauerbf974a02010-11-04 17:05:43 +0100149 iprtd->period_bytes = params_period_bytes(params);
Sascha Hauer83802222009-11-25 16:41:04 +0100150 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
Sascha Hauer83802222009-11-25 16:41:04 +0100156 dma_addr = runtime->dma_addr;
157
Sascha Hauerbf974a02010-11-04 17:05:43 +0100158 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 ?
Vinod Koul35e16582011-10-14 10:49:30 +0530164 DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
Sascha Hauerbf974a02010-11-04 17:05:43 +0100165 if (!iprtd->desc) {
166 dev_err(&chan->dev->device, "cannot prepare slave dma\n");
167 return -EINVAL;
Sascha Hauer83802222009-11-25 16:41:04 +0100168 }
169
Sascha Hauerbf974a02010-11-04 17:05:43 +0100170 iprtd->desc->callback = audio_dma_irq;
171 iprtd->desc->callback_param = substream;
172
Sascha Hauer83802222009-11-25 16:41:04 +0100173 return 0;
174}
175
176static 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
Sascha Hauerbf974a02010-11-04 17:05:43 +0100181 if (iprtd->dma_chan) {
182 dma_release_channel(iprtd->dma_chan);
183 iprtd->dma_chan = NULL;
Sascha Hauer83802222009-11-25 16:41:04 +0100184 }
185
Sascha Hauer83802222009-11-25 16:41:04 +0100186 return 0;
187}
188
189static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
190{
Sascha Hauer83802222009-11-25 16:41:04 +0100191 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Daniel Mack5f712b22010-03-22 10:11:15 +0100192 struct imx_pcm_dma_params *dma_params;
Sascha Hauer83802222009-11-25 16:41:04 +0100193
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000194 dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Daniel Mack5f712b22010-03-22 10:11:15 +0100195
Sascha Hauer83802222009-11-25 16:41:04 +0100196 return 0;
197}
198
199static 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:
Sascha Hauerbf974a02010-11-04 17:05:43 +0100208 dmaengine_submit(iprtd->desc);
Sascha Hauer83802222009-11-25 16:41:04 +0100209
210 break;
211
212 case SNDRV_PCM_TRIGGER_STOP:
213 case SNDRV_PCM_TRIGGER_SUSPEND:
214 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Sascha Hauerbf974a02010-11-04 17:05:43 +0100215 dmaengine_terminate_all(iprtd->dma_chan);
Sascha Hauer83802222009-11-25 16:41:04 +0100216
217 break;
218 default:
219 return -EINVAL;
220 }
221
222 return 0;
223}
224
225static 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
Sascha Hauerbf974a02010-11-04 17:05:43 +0100230 pr_debug("%s: %ld %ld\n", __func__, iprtd->offset,
231 bytes_to_frames(substream->runtime, iprtd->offset));
232
Sascha Hauer83802222009-11-25 16:41:04 +0100233 return bytes_to_frames(substream->runtime, iprtd->offset);
234}
235
236static 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,
Sascha Hauerbf974a02010-11-04 17:05:43 +0100249 .period_bytes_max = 65535, /* Limited by SDMA engine */
Sascha Hauer83802222009-11-25 16:41:04 +0100250 .periods_min = 2,
251 .periods_max = 255,
252 .fifo_size = 0,
253};
254
255static 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);
Kulikov Vasiliy51b6dfb2010-07-16 20:16:34 +0400262 if (iprtd == NULL)
263 return -ENOMEM;
Sascha Hauer83802222009-11-25 16:41:04 +0100264 runtime->private_data = iprtd;
265
266 ret = snd_pcm_hw_constraint_integer(substream->runtime,
267 SNDRV_PCM_HW_PARAM_PERIODS);
Kulikov Vasiliy51b6dfb2010-07-16 20:16:34 +0400268 if (ret < 0) {
269 kfree(iprtd);
Sascha Hauer83802222009-11-25 16:41:04 +0100270 return ret;
Kulikov Vasiliy51b6dfb2010-07-16 20:16:34 +0400271 }
Sascha Hauer83802222009-11-25 16:41:04 +0100272
273 snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
Sascha Hauerbf974a02010-11-04 17:05:43 +0100274
275 return 0;
276}
277
278static 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
Sascha Hauer83802222009-11-25 16:41:04 +0100285 return 0;
286}
287
288static struct snd_pcm_ops imx_pcm_ops = {
289 .open = snd_imx_open,
Sascha Hauerbf974a02010-11-04 17:05:43 +0100290 .close = snd_imx_close,
Sascha Hauer83802222009-11-25 16:41:04 +0100291 .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
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000300static struct snd_soc_platform_driver imx_soc_platform_mx2 = {
301 .ops = &imx_pcm_ops,
Sascha Hauer83802222009-11-25 16:41:04 +0100302 .pcm_new = imx_pcm_new,
303 .pcm_free = imx_pcm_free,
304};
305
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000306static int __devinit imx_soc_platform_probe(struct platform_device *pdev)
Sascha Hauer83802222009-11-25 16:41:04 +0100307{
Wolfram Sang2c4cf172011-03-25 16:51:44 +0100308 struct imx_ssi *ssi = platform_get_drvdata(pdev);
309
310 ssi->dma_params_tx.burstsize = 6;
311 ssi->dma_params_rx.burstsize = 4;
312
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000313 return snd_soc_register_platform(&pdev->dev, &imx_soc_platform_mx2);
Sascha Hauer83802222009-11-25 16:41:04 +0100314}
315
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000316static int __devexit imx_soc_platform_remove(struct platform_device *pdev)
317{
318 snd_soc_unregister_platform(&pdev->dev);
319 return 0;
320}
321
322static struct platform_driver imx_pcm_driver = {
323 .driver = {
324 .name = "imx-pcm-audio",
325 .owner = THIS_MODULE,
326 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000327 .probe = imx_soc_platform_probe,
328 .remove = __devexit_p(imx_soc_platform_remove),
329};
330
Axel Lin7a24b2b2011-11-24 15:03:50 +0800331module_platform_driver(imx_pcm_driver);
Arnaud Patard (Rtp)96dcabb2011-06-22 22:21:49 +0200332MODULE_LICENSE("GPL");
333MODULE_ALIAS("platform:imx-pcm-audio");