ASoC: imx-ssi: honor IMX_SSI_DMA flag
[deliverable/linux.git] / sound / soc / imx / imx-pcm-dma-mx2.c
CommitLineData
8380222e
SH
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
23#include <sound/core.h>
24#include <sound/initval.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27#include <sound/soc.h>
28
29#include <mach/dma-mx1-mx2.h>
30
31#include "imx-ssi.h"
32
33struct imx_pcm_runtime_data {
34 int sg_count;
35 struct scatterlist *sg_list;
36 int period;
37 int periods;
38 unsigned long dma_addr;
39 int dma;
40 struct snd_pcm_substream *substream;
41 unsigned long offset;
42 unsigned long size;
43 unsigned long period_cnt;
44 void *buf;
45 int period_time;
46};
47
48/* Called by the DMA framework when a period has elapsed */
49static void imx_ssi_dma_progression(int channel, void *data,
50 struct scatterlist *sg)
51{
52 struct snd_pcm_substream *substream = data;
53 struct snd_pcm_runtime *runtime = substream->runtime;
54 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
55
56 if (!sg)
57 return;
58
59 runtime = iprtd->substream->runtime;
60
61 iprtd->offset = sg->dma_address - runtime->dma_addr;
62
63 snd_pcm_period_elapsed(iprtd->substream);
64}
65
66static void imx_ssi_dma_callback(int channel, void *data)
67{
68 pr_err("%s shouldn't be called\n", __func__);
69}
70
71static void snd_imx_dma_err_callback(int channel, void *data, int err)
72{
73 pr_err("DMA error callback called\n");
74
75 pr_err("DMA timeout on channel %d -%s%s%s%s\n",
76 channel,
77 err & IMX_DMA_ERR_BURST ? " burst" : "",
78 err & IMX_DMA_ERR_REQUEST ? " request" : "",
79 err & IMX_DMA_ERR_TRANSFER ? " transfer" : "",
80 err & IMX_DMA_ERR_BUFFER ? " buffer" : "");
81}
82
83static int imx_ssi_dma_alloc(struct snd_pcm_substream *substream)
84{
85 struct snd_soc_pcm_runtime *rtd = substream->private_data;
5f712b2b 86 struct imx_pcm_dma_params *dma_params;
8380222e
SH
87 struct snd_pcm_runtime *runtime = substream->runtime;
88 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
89 int ret;
90
5f712b2b
DM
91 dma_params = snd_soc_get_dma_data(rtd->dai->cpu_dai, substream);
92
8380222e
SH
93 iprtd->dma = imx_dma_request_by_prio(DRV_NAME, DMA_PRIO_HIGH);
94 if (iprtd->dma < 0) {
95 pr_err("Failed to claim the audio DMA\n");
96 return -ENODEV;
97 }
98
99 ret = imx_dma_setup_handlers(iprtd->dma,
100 imx_ssi_dma_callback,
101 snd_imx_dma_err_callback, substream);
102 if (ret)
103 goto out;
104
105 ret = imx_dma_setup_progression_handler(iprtd->dma,
106 imx_ssi_dma_progression);
107 if (ret) {
108 pr_err("Failed to setup the DMA handler\n");
109 goto out;
110 }
111
112 ret = imx_dma_config_channel(iprtd->dma,
113 IMX_DMA_MEMSIZE_16 | IMX_DMA_TYPE_FIFO,
114 IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR,
115 dma_params->dma, 1);
116 if (ret < 0) {
117 pr_err("Cannot configure DMA channel: %d\n", ret);
118 goto out;
119 }
120
121 imx_dma_config_burstlen(iprtd->dma, dma_params->burstsize * 2);
122
123 return 0;
124out:
125 imx_dma_free(iprtd->dma);
126 return ret;
127}
128
129static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
130 struct snd_pcm_hw_params *params)
131{
132 struct snd_pcm_runtime *runtime = substream->runtime;
133 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
134 int i;
135 unsigned long dma_addr;
136
137 imx_ssi_dma_alloc(substream);
138
139 iprtd->size = params_buffer_bytes(params);
140 iprtd->periods = params_periods(params);
141 iprtd->period = params_period_bytes(params);
142 iprtd->offset = 0;
143 iprtd->period_time = HZ / (params_rate(params) /
144 params_period_size(params));
145
146 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
147
148 if (iprtd->sg_count != iprtd->periods) {
149 kfree(iprtd->sg_list);
150
151 iprtd->sg_list = kcalloc(iprtd->periods + 1,
152 sizeof(struct scatterlist), GFP_KERNEL);
153 if (!iprtd->sg_list)
154 return -ENOMEM;
155 iprtd->sg_count = iprtd->periods + 1;
156 }
157
158 sg_init_table(iprtd->sg_list, iprtd->sg_count);
159 dma_addr = runtime->dma_addr;
160
161 for (i = 0; i < iprtd->periods; i++) {
162 iprtd->sg_list[i].page_link = 0;
163 iprtd->sg_list[i].offset = 0;
164 iprtd->sg_list[i].dma_address = dma_addr;
165 iprtd->sg_list[i].length = iprtd->period;
166 dma_addr += iprtd->period;
167 }
168
169 /* close the loop */
170 iprtd->sg_list[iprtd->sg_count - 1].offset = 0;
171 iprtd->sg_list[iprtd->sg_count - 1].length = 0;
172 iprtd->sg_list[iprtd->sg_count - 1].page_link =
173 ((unsigned long) iprtd->sg_list | 0x01) & ~0x02;
174 return 0;
175}
176
177static int snd_imx_pcm_hw_free(struct snd_pcm_substream *substream)
178{
179 struct snd_pcm_runtime *runtime = substream->runtime;
180 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
181
182 if (iprtd->dma >= 0) {
183 imx_dma_free(iprtd->dma);
184 iprtd->dma = -EINVAL;
185 }
186
187 kfree(iprtd->sg_list);
188 iprtd->sg_list = NULL;
189
190 return 0;
191}
192
193static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
194{
195 struct snd_pcm_runtime *runtime = substream->runtime;
196 struct snd_soc_pcm_runtime *rtd = substream->private_data;
5f712b2b 197 struct imx_pcm_dma_params *dma_params;
8380222e
SH
198 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
199 int err;
200
5f712b2b
DM
201 dma_params = snd_soc_get_dma_data(rtd->dai->cpu_dai, substream);
202
8380222e
SH
203 iprtd->substream = substream;
204 iprtd->buf = (unsigned int *)substream->dma_buffer.area;
205 iprtd->period_cnt = 0;
206
207 pr_debug("%s: buf: %p period: %d periods: %d\n",
208 __func__, iprtd->buf, iprtd->period, iprtd->periods);
209
210 err = imx_dma_setup_sg(iprtd->dma, iprtd->sg_list, iprtd->sg_count,
211 IMX_DMA_LENGTH_LOOP, dma_params->dma_addr,
212 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
213 DMA_MODE_WRITE : DMA_MODE_READ);
214 if (err)
215 return err;
216
217 return 0;
218}
219
220static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
221{
222 struct snd_pcm_runtime *runtime = substream->runtime;
223 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
224
225 switch (cmd) {
226 case SNDRV_PCM_TRIGGER_START:
227 case SNDRV_PCM_TRIGGER_RESUME:
228 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
229 imx_dma_enable(iprtd->dma);
230
231 break;
232
233 case SNDRV_PCM_TRIGGER_STOP:
234 case SNDRV_PCM_TRIGGER_SUSPEND:
235 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
236 imx_dma_disable(iprtd->dma);
237
238 break;
239 default:
240 return -EINVAL;
241 }
242
243 return 0;
244}
245
246static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream)
247{
248 struct snd_pcm_runtime *runtime = substream->runtime;
249 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
250
251 return bytes_to_frames(substream->runtime, iprtd->offset);
252}
253
254static struct snd_pcm_hardware snd_imx_hardware = {
255 .info = SNDRV_PCM_INFO_INTERLEAVED |
256 SNDRV_PCM_INFO_BLOCK_TRANSFER |
257 SNDRV_PCM_INFO_MMAP |
258 SNDRV_PCM_INFO_MMAP_VALID |
259 SNDRV_PCM_INFO_PAUSE |
260 SNDRV_PCM_INFO_RESUME,
261 .formats = SNDRV_PCM_FMTBIT_S16_LE,
262 .rate_min = 8000,
263 .channels_min = 2,
264 .channels_max = 2,
265 .buffer_bytes_max = IMX_SSI_DMABUF_SIZE,
266 .period_bytes_min = 128,
267 .period_bytes_max = 16 * 1024,
268 .periods_min = 2,
269 .periods_max = 255,
270 .fifo_size = 0,
271};
272
273static int snd_imx_open(struct snd_pcm_substream *substream)
274{
275 struct snd_pcm_runtime *runtime = substream->runtime;
276 struct imx_pcm_runtime_data *iprtd;
277 int ret;
278
279 iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
280 runtime->private_data = iprtd;
281
282 ret = snd_pcm_hw_constraint_integer(substream->runtime,
283 SNDRV_PCM_HW_PARAM_PERIODS);
284 if (ret < 0)
285 return ret;
286
287 snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
288 return 0;
289}
290
291static struct snd_pcm_ops imx_pcm_ops = {
292 .open = snd_imx_open,
293 .ioctl = snd_pcm_lib_ioctl,
294 .hw_params = snd_imx_pcm_hw_params,
295 .hw_free = snd_imx_pcm_hw_free,
296 .prepare = snd_imx_pcm_prepare,
297 .trigger = snd_imx_pcm_trigger,
298 .pointer = snd_imx_pcm_pointer,
299 .mmap = snd_imx_pcm_mmap,
300};
301
302static struct snd_soc_platform imx_soc_platform_dma = {
303 .name = "imx-audio",
304 .pcm_ops = &imx_pcm_ops,
305 .pcm_new = imx_pcm_new,
306 .pcm_free = imx_pcm_free,
307};
308
309struct snd_soc_platform *imx_ssi_dma_mx2_init(struct platform_device *pdev,
310 struct imx_ssi *ssi)
311{
312 ssi->dma_params_tx.burstsize = DMA_TXFIFO_BURST;
313 ssi->dma_params_rx.burstsize = DMA_RXFIFO_BURST;
314
315 return &imx_soc_platform_dma;
316}
317
This page took 0.054255 seconds and 5 git commands to generate.