staging: android: logger: Change logger_offset() from macro to function
[linux-2.6.git] / sound / soc / imx / imx-pcm-dma-mx2.c
blob5780c9b9d569cf78f82edb63104322c896b7c234
1 /*
2 * imx-pcm-dma-mx2.c -- ALSA Soc Audio Layer
4 * Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
6 * This code is based on code copyrighted by Freescale,
7 * Liam Girdwood, Javier Martin and probably others.
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.
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>
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>
31 #include <mach/dma.h>
33 #include "imx-ssi.h"
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;
48 static void audio_dma_irq(void *data)
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;
54 iprtd->offset += iprtd->period_bytes;
55 iprtd->offset %= iprtd->period_bytes * iprtd->periods;
57 snd_pcm_period_elapsed(substream);
60 static bool filter(struct dma_chan *chan, void *param)
62 struct imx_pcm_runtime_data *iprtd = param;
64 if (!imx_dma_is_general_purpose(chan))
65 return false;
67 chan->private = &iprtd->dma_data;
69 return true;
72 static int imx_ssi_dma_alloc(struct snd_pcm_substream *substream,
73 struct snd_pcm_hw_params *params)
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;
84 dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
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;
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;
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;
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;
123 ret = dmaengine_slave_config(iprtd->dma_chan, &slave_config);
124 if (ret)
125 return ret;
127 return 0;
130 static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
131 struct snd_pcm_hw_params *params)
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;
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;
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));
154 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
156 dma_addr = runtime->dma_addr;
158 iprtd->buf = (unsigned int *)substream->dma_buffer.area;
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;
170 iprtd->desc->callback = audio_dma_irq;
171 iprtd->desc->callback_param = substream;
173 return 0;
176 static int snd_imx_pcm_hw_free(struct snd_pcm_substream *substream)
178 struct snd_pcm_runtime *runtime = substream->runtime;
179 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
181 if (iprtd->dma_chan) {
182 dma_release_channel(iprtd->dma_chan);
183 iprtd->dma_chan = NULL;
186 return 0;
189 static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
191 struct snd_soc_pcm_runtime *rtd = substream->private_data;
192 struct imx_pcm_dma_params *dma_params;
194 dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
196 return 0;
199 static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
201 struct snd_pcm_runtime *runtime = substream->runtime;
202 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
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);
210 break;
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);
217 break;
218 default:
219 return -EINVAL;
222 return 0;
225 static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream)
227 struct snd_pcm_runtime *runtime = substream->runtime;
228 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
230 pr_debug("%s: %ld %ld\n", __func__, iprtd->offset,
231 bytes_to_frames(substream->runtime, iprtd->offset));
233 return bytes_to_frames(substream->runtime, iprtd->offset);
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,
255 static int snd_imx_open(struct snd_pcm_substream *substream)
257 struct snd_pcm_runtime *runtime = substream->runtime;
258 struct imx_pcm_runtime_data *iprtd;
259 int ret;
261 iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
262 if (iprtd == NULL)
263 return -ENOMEM;
264 runtime->private_data = iprtd;
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;
273 snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
275 return 0;
278 static int snd_imx_close(struct snd_pcm_substream *substream)
280 struct snd_pcm_runtime *runtime = substream->runtime;
281 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
283 kfree(iprtd);
285 return 0;
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,
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,
306 static int __devinit imx_soc_platform_probe(struct platform_device *pdev)
308 struct imx_ssi *ssi = platform_get_drvdata(pdev);
310 ssi->dma_params_tx.burstsize = 6;
311 ssi->dma_params_rx.burstsize = 4;
313 return snd_soc_register_platform(&pdev->dev, &imx_soc_platform_mx2);
316 static int __devexit imx_soc_platform_remove(struct platform_device *pdev)
318 snd_soc_unregister_platform(&pdev->dev);
319 return 0;
322 static struct platform_driver imx_pcm_driver = {
323 .driver = {
324 .name = "imx-pcm-audio",
325 .owner = THIS_MODULE,
327 .probe = imx_soc_platform_probe,
328 .remove = __devexit_p(imx_soc_platform_remove),
331 module_platform_driver(imx_pcm_driver);
332 MODULE_LICENSE("GPL");
333 MODULE_ALIAS("platform:imx-pcm-audio");