Merge branch 'fix/asoc' into for-linus
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / soc / imx / imx-pcm-dma-mx2.c
blob2b31ac673ea4d95a05b986d7f6b5059d4e5cd615
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>
24 #include <sound/core.h>
25 #include <sound/initval.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
30 #include <mach/dma-mx1-mx2.h>
32 #include "imx-ssi.h"
34 struct imx_pcm_runtime_data {
35 int sg_count;
36 struct scatterlist *sg_list;
37 int period;
38 int periods;
39 unsigned long dma_addr;
40 int dma;
41 struct snd_pcm_substream *substream;
42 unsigned long offset;
43 unsigned long size;
44 unsigned long period_cnt;
45 void *buf;
46 int period_time;
49 /* Called by the DMA framework when a period has elapsed */
50 static void imx_ssi_dma_progression(int channel, void *data,
51 struct scatterlist *sg)
53 struct snd_pcm_substream *substream = data;
54 struct snd_pcm_runtime *runtime = substream->runtime;
55 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
57 if (!sg)
58 return;
60 runtime = iprtd->substream->runtime;
62 iprtd->offset = sg->dma_address - runtime->dma_addr;
64 snd_pcm_period_elapsed(iprtd->substream);
67 static void imx_ssi_dma_callback(int channel, void *data)
69 pr_err("%s shouldn't be called\n", __func__);
72 static void snd_imx_dma_err_callback(int channel, void *data, int err)
74 struct snd_pcm_substream *substream = data;
75 struct snd_soc_pcm_runtime *rtd = substream->private_data;
76 struct imx_pcm_dma_params *dma_params = rtd->dai->cpu_dai->dma_data;
77 struct snd_pcm_runtime *runtime = substream->runtime;
78 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
79 int ret;
81 pr_err("DMA timeout on channel %d -%s%s%s%s\n",
82 channel,
83 err & IMX_DMA_ERR_BURST ? " burst" : "",
84 err & IMX_DMA_ERR_REQUEST ? " request" : "",
85 err & IMX_DMA_ERR_TRANSFER ? " transfer" : "",
86 err & IMX_DMA_ERR_BUFFER ? " buffer" : "");
88 imx_dma_disable(iprtd->dma);
89 ret = imx_dma_setup_sg(iprtd->dma, iprtd->sg_list, iprtd->sg_count,
90 IMX_DMA_LENGTH_LOOP, dma_params->dma_addr,
91 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
92 DMA_MODE_WRITE : DMA_MODE_READ);
93 if (!ret)
94 imx_dma_enable(iprtd->dma);
97 static int imx_ssi_dma_alloc(struct snd_pcm_substream *substream)
99 struct snd_soc_pcm_runtime *rtd = substream->private_data;
100 struct imx_pcm_dma_params *dma_params;
101 struct snd_pcm_runtime *runtime = substream->runtime;
102 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
103 int ret;
105 dma_params = snd_soc_get_dma_data(rtd->dai->cpu_dai, substream);
107 iprtd->dma = imx_dma_request_by_prio(DRV_NAME, DMA_PRIO_HIGH);
108 if (iprtd->dma < 0) {
109 pr_err("Failed to claim the audio DMA\n");
110 return -ENODEV;
113 ret = imx_dma_setup_handlers(iprtd->dma,
114 imx_ssi_dma_callback,
115 snd_imx_dma_err_callback, substream);
116 if (ret)
117 goto out;
119 ret = imx_dma_setup_progression_handler(iprtd->dma,
120 imx_ssi_dma_progression);
121 if (ret) {
122 pr_err("Failed to setup the DMA handler\n");
123 goto out;
126 ret = imx_dma_config_channel(iprtd->dma,
127 IMX_DMA_MEMSIZE_16 | IMX_DMA_TYPE_FIFO,
128 IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR,
129 dma_params->dma, 1);
130 if (ret < 0) {
131 pr_err("Cannot configure DMA channel: %d\n", ret);
132 goto out;
135 imx_dma_config_burstlen(iprtd->dma, dma_params->burstsize * 2);
137 return 0;
138 out:
139 imx_dma_free(iprtd->dma);
140 return ret;
143 static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
144 struct snd_pcm_hw_params *params)
146 struct snd_pcm_runtime *runtime = substream->runtime;
147 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
148 int i;
149 unsigned long dma_addr;
151 imx_ssi_dma_alloc(substream);
153 iprtd->size = params_buffer_bytes(params);
154 iprtd->periods = params_periods(params);
155 iprtd->period = params_period_bytes(params);
156 iprtd->offset = 0;
157 iprtd->period_time = HZ / (params_rate(params) /
158 params_period_size(params));
160 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
162 if (iprtd->sg_count != iprtd->periods) {
163 kfree(iprtd->sg_list);
165 iprtd->sg_list = kcalloc(iprtd->periods + 1,
166 sizeof(struct scatterlist), GFP_KERNEL);
167 if (!iprtd->sg_list)
168 return -ENOMEM;
169 iprtd->sg_count = iprtd->periods + 1;
172 sg_init_table(iprtd->sg_list, iprtd->sg_count);
173 dma_addr = runtime->dma_addr;
175 for (i = 0; i < iprtd->periods; i++) {
176 iprtd->sg_list[i].page_link = 0;
177 iprtd->sg_list[i].offset = 0;
178 iprtd->sg_list[i].dma_address = dma_addr;
179 iprtd->sg_list[i].length = iprtd->period;
180 dma_addr += iprtd->period;
183 /* close the loop */
184 iprtd->sg_list[iprtd->sg_count - 1].offset = 0;
185 iprtd->sg_list[iprtd->sg_count - 1].length = 0;
186 iprtd->sg_list[iprtd->sg_count - 1].page_link =
187 ((unsigned long) iprtd->sg_list | 0x01) & ~0x02;
188 return 0;
191 static int snd_imx_pcm_hw_free(struct snd_pcm_substream *substream)
193 struct snd_pcm_runtime *runtime = substream->runtime;
194 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
196 if (iprtd->dma >= 0) {
197 imx_dma_free(iprtd->dma);
198 iprtd->dma = -EINVAL;
201 kfree(iprtd->sg_list);
202 iprtd->sg_list = NULL;
204 return 0;
207 static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
209 struct snd_pcm_runtime *runtime = substream->runtime;
210 struct snd_soc_pcm_runtime *rtd = substream->private_data;
211 struct imx_pcm_dma_params *dma_params;
212 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
213 int err;
215 dma_params = snd_soc_get_dma_data(rtd->dai->cpu_dai, substream);
217 iprtd->substream = substream;
218 iprtd->buf = (unsigned int *)substream->dma_buffer.area;
219 iprtd->period_cnt = 0;
221 pr_debug("%s: buf: %p period: %d periods: %d\n",
222 __func__, iprtd->buf, iprtd->period, iprtd->periods);
224 err = imx_dma_setup_sg(iprtd->dma, iprtd->sg_list, iprtd->sg_count,
225 IMX_DMA_LENGTH_LOOP, dma_params->dma_addr,
226 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
227 DMA_MODE_WRITE : DMA_MODE_READ);
228 if (err)
229 return err;
231 return 0;
234 static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
236 struct snd_pcm_runtime *runtime = substream->runtime;
237 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
239 switch (cmd) {
240 case SNDRV_PCM_TRIGGER_START:
241 case SNDRV_PCM_TRIGGER_RESUME:
242 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
243 imx_dma_enable(iprtd->dma);
245 break;
247 case SNDRV_PCM_TRIGGER_STOP:
248 case SNDRV_PCM_TRIGGER_SUSPEND:
249 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
250 imx_dma_disable(iprtd->dma);
252 break;
253 default:
254 return -EINVAL;
257 return 0;
260 static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream)
262 struct snd_pcm_runtime *runtime = substream->runtime;
263 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
265 return bytes_to_frames(substream->runtime, iprtd->offset);
268 static struct snd_pcm_hardware snd_imx_hardware = {
269 .info = SNDRV_PCM_INFO_INTERLEAVED |
270 SNDRV_PCM_INFO_BLOCK_TRANSFER |
271 SNDRV_PCM_INFO_MMAP |
272 SNDRV_PCM_INFO_MMAP_VALID |
273 SNDRV_PCM_INFO_PAUSE |
274 SNDRV_PCM_INFO_RESUME,
275 .formats = SNDRV_PCM_FMTBIT_S16_LE,
276 .rate_min = 8000,
277 .channels_min = 2,
278 .channels_max = 2,
279 .buffer_bytes_max = IMX_SSI_DMABUF_SIZE,
280 .period_bytes_min = 128,
281 .period_bytes_max = 16 * 1024,
282 .periods_min = 2,
283 .periods_max = 255,
284 .fifo_size = 0,
287 static int snd_imx_open(struct snd_pcm_substream *substream)
289 struct snd_pcm_runtime *runtime = substream->runtime;
290 struct imx_pcm_runtime_data *iprtd;
291 int ret;
293 iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
294 runtime->private_data = iprtd;
296 ret = snd_pcm_hw_constraint_integer(substream->runtime,
297 SNDRV_PCM_HW_PARAM_PERIODS);
298 if (ret < 0)
299 return ret;
301 snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
302 return 0;
305 static struct snd_pcm_ops imx_pcm_ops = {
306 .open = snd_imx_open,
307 .ioctl = snd_pcm_lib_ioctl,
308 .hw_params = snd_imx_pcm_hw_params,
309 .hw_free = snd_imx_pcm_hw_free,
310 .prepare = snd_imx_pcm_prepare,
311 .trigger = snd_imx_pcm_trigger,
312 .pointer = snd_imx_pcm_pointer,
313 .mmap = snd_imx_pcm_mmap,
316 static struct snd_soc_platform imx_soc_platform_dma = {
317 .name = "imx-audio",
318 .pcm_ops = &imx_pcm_ops,
319 .pcm_new = imx_pcm_new,
320 .pcm_free = imx_pcm_free,
323 struct snd_soc_platform *imx_ssi_dma_mx2_init(struct platform_device *pdev,
324 struct imx_ssi *ssi)
326 ssi->dma_params_tx.burstsize = DMA_TXFIFO_BURST;
327 ssi->dma_params_rx.burstsize = DMA_RXFIFO_BURST;
329 return &imx_soc_platform_dma;