blob: 93272966b84892ce113f676ce0034d2856d4afbc [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>
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{
Sascha Hauer671999c2010-04-08 11:31:25 +020073 struct snd_pcm_substream *substream = data;
74 struct snd_soc_pcm_runtime *rtd = substream->private_data;
75 struct imx_pcm_dma_params *dma_params = rtd->dai->cpu_dai->dma_data;
76 struct snd_pcm_runtime *runtime = substream->runtime;
77 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
78 int ret;
Sascha Hauer83802222009-11-25 16:41:04 +010079
80 pr_err("DMA timeout on channel %d -%s%s%s%s\n",
81 channel,
82 err & IMX_DMA_ERR_BURST ? " burst" : "",
83 err & IMX_DMA_ERR_REQUEST ? " request" : "",
84 err & IMX_DMA_ERR_TRANSFER ? " transfer" : "",
85 err & IMX_DMA_ERR_BUFFER ? " buffer" : "");
Sascha Hauer671999c2010-04-08 11:31:25 +020086
87 imx_dma_disable(iprtd->dma);
88 ret = imx_dma_setup_sg(iprtd->dma, iprtd->sg_list, iprtd->sg_count,
89 IMX_DMA_LENGTH_LOOP, dma_params->dma_addr,
90 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
91 DMA_MODE_WRITE : DMA_MODE_READ);
92 if (!ret)
93 imx_dma_enable(iprtd->dma);
Sascha Hauer83802222009-11-25 16:41:04 +010094}
95
96static int imx_ssi_dma_alloc(struct snd_pcm_substream *substream)
97{
98 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Daniel Mack5f712b22010-03-22 10:11:15 +010099 struct imx_pcm_dma_params *dma_params;
Sascha Hauer83802222009-11-25 16:41:04 +0100100 struct snd_pcm_runtime *runtime = substream->runtime;
101 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
102 int ret;
103
Daniel Mack5f712b22010-03-22 10:11:15 +0100104 dma_params = snd_soc_get_dma_data(rtd->dai->cpu_dai, substream);
105
Sascha Hauer83802222009-11-25 16:41:04 +0100106 iprtd->dma = imx_dma_request_by_prio(DRV_NAME, DMA_PRIO_HIGH);
107 if (iprtd->dma < 0) {
108 pr_err("Failed to claim the audio DMA\n");
109 return -ENODEV;
110 }
111
112 ret = imx_dma_setup_handlers(iprtd->dma,
113 imx_ssi_dma_callback,
114 snd_imx_dma_err_callback, substream);
115 if (ret)
116 goto out;
117
118 ret = imx_dma_setup_progression_handler(iprtd->dma,
119 imx_ssi_dma_progression);
120 if (ret) {
121 pr_err("Failed to setup the DMA handler\n");
122 goto out;
123 }
124
125 ret = imx_dma_config_channel(iprtd->dma,
126 IMX_DMA_MEMSIZE_16 | IMX_DMA_TYPE_FIFO,
127 IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR,
128 dma_params->dma, 1);
129 if (ret < 0) {
130 pr_err("Cannot configure DMA channel: %d\n", ret);
131 goto out;
132 }
133
134 imx_dma_config_burstlen(iprtd->dma, dma_params->burstsize * 2);
135
136 return 0;
137out:
138 imx_dma_free(iprtd->dma);
139 return ret;
140}
141
142static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
143 struct snd_pcm_hw_params *params)
144{
145 struct snd_pcm_runtime *runtime = substream->runtime;
146 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
147 int i;
148 unsigned long dma_addr;
149
150 imx_ssi_dma_alloc(substream);
151
152 iprtd->size = params_buffer_bytes(params);
153 iprtd->periods = params_periods(params);
154 iprtd->period = params_period_bytes(params);
155 iprtd->offset = 0;
156 iprtd->period_time = HZ / (params_rate(params) /
157 params_period_size(params));
158
159 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
160
161 if (iprtd->sg_count != iprtd->periods) {
162 kfree(iprtd->sg_list);
163
164 iprtd->sg_list = kcalloc(iprtd->periods + 1,
165 sizeof(struct scatterlist), GFP_KERNEL);
166 if (!iprtd->sg_list)
167 return -ENOMEM;
168 iprtd->sg_count = iprtd->periods + 1;
169 }
170
171 sg_init_table(iprtd->sg_list, iprtd->sg_count);
172 dma_addr = runtime->dma_addr;
173
174 for (i = 0; i < iprtd->periods; i++) {
175 iprtd->sg_list[i].page_link = 0;
176 iprtd->sg_list[i].offset = 0;
177 iprtd->sg_list[i].dma_address = dma_addr;
178 iprtd->sg_list[i].length = iprtd->period;
179 dma_addr += iprtd->period;
180 }
181
182 /* close the loop */
183 iprtd->sg_list[iprtd->sg_count - 1].offset = 0;
184 iprtd->sg_list[iprtd->sg_count - 1].length = 0;
185 iprtd->sg_list[iprtd->sg_count - 1].page_link =
186 ((unsigned long) iprtd->sg_list | 0x01) & ~0x02;
187 return 0;
188}
189
190static int snd_imx_pcm_hw_free(struct snd_pcm_substream *substream)
191{
192 struct snd_pcm_runtime *runtime = substream->runtime;
193 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
194
195 if (iprtd->dma >= 0) {
196 imx_dma_free(iprtd->dma);
197 iprtd->dma = -EINVAL;
198 }
199
200 kfree(iprtd->sg_list);
201 iprtd->sg_list = NULL;
202
203 return 0;
204}
205
206static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
207{
208 struct snd_pcm_runtime *runtime = substream->runtime;
209 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Daniel Mack5f712b22010-03-22 10:11:15 +0100210 struct imx_pcm_dma_params *dma_params;
Sascha Hauer83802222009-11-25 16:41:04 +0100211 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
212 int err;
213
Daniel Mack5f712b22010-03-22 10:11:15 +0100214 dma_params = snd_soc_get_dma_data(rtd->dai->cpu_dai, substream);
215
Sascha Hauer83802222009-11-25 16:41:04 +0100216 iprtd->substream = substream;
217 iprtd->buf = (unsigned int *)substream->dma_buffer.area;
218 iprtd->period_cnt = 0;
219
220 pr_debug("%s: buf: %p period: %d periods: %d\n",
221 __func__, iprtd->buf, iprtd->period, iprtd->periods);
222
223 err = imx_dma_setup_sg(iprtd->dma, iprtd->sg_list, iprtd->sg_count,
224 IMX_DMA_LENGTH_LOOP, dma_params->dma_addr,
225 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
226 DMA_MODE_WRITE : DMA_MODE_READ);
227 if (err)
228 return err;
229
230 return 0;
231}
232
233static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
234{
235 struct snd_pcm_runtime *runtime = substream->runtime;
236 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
237
238 switch (cmd) {
239 case SNDRV_PCM_TRIGGER_START:
240 case SNDRV_PCM_TRIGGER_RESUME:
241 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
242 imx_dma_enable(iprtd->dma);
243
244 break;
245
246 case SNDRV_PCM_TRIGGER_STOP:
247 case SNDRV_PCM_TRIGGER_SUSPEND:
248 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
249 imx_dma_disable(iprtd->dma);
250
251 break;
252 default:
253 return -EINVAL;
254 }
255
256 return 0;
257}
258
259static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream)
260{
261 struct snd_pcm_runtime *runtime = substream->runtime;
262 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
263
264 return bytes_to_frames(substream->runtime, iprtd->offset);
265}
266
267static struct snd_pcm_hardware snd_imx_hardware = {
268 .info = SNDRV_PCM_INFO_INTERLEAVED |
269 SNDRV_PCM_INFO_BLOCK_TRANSFER |
270 SNDRV_PCM_INFO_MMAP |
271 SNDRV_PCM_INFO_MMAP_VALID |
272 SNDRV_PCM_INFO_PAUSE |
273 SNDRV_PCM_INFO_RESUME,
274 .formats = SNDRV_PCM_FMTBIT_S16_LE,
275 .rate_min = 8000,
276 .channels_min = 2,
277 .channels_max = 2,
278 .buffer_bytes_max = IMX_SSI_DMABUF_SIZE,
279 .period_bytes_min = 128,
280 .period_bytes_max = 16 * 1024,
281 .periods_min = 2,
282 .periods_max = 255,
283 .fifo_size = 0,
284};
285
286static int snd_imx_open(struct snd_pcm_substream *substream)
287{
288 struct snd_pcm_runtime *runtime = substream->runtime;
289 struct imx_pcm_runtime_data *iprtd;
290 int ret;
291
292 iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
293 runtime->private_data = iprtd;
294
295 ret = snd_pcm_hw_constraint_integer(substream->runtime,
296 SNDRV_PCM_HW_PARAM_PERIODS);
297 if (ret < 0)
298 return ret;
299
300 snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
301 return 0;
302}
303
304static struct snd_pcm_ops imx_pcm_ops = {
305 .open = snd_imx_open,
306 .ioctl = snd_pcm_lib_ioctl,
307 .hw_params = snd_imx_pcm_hw_params,
308 .hw_free = snd_imx_pcm_hw_free,
309 .prepare = snd_imx_pcm_prepare,
310 .trigger = snd_imx_pcm_trigger,
311 .pointer = snd_imx_pcm_pointer,
312 .mmap = snd_imx_pcm_mmap,
313};
314
315static struct snd_soc_platform imx_soc_platform_dma = {
316 .name = "imx-audio",
317 .pcm_ops = &imx_pcm_ops,
318 .pcm_new = imx_pcm_new,
319 .pcm_free = imx_pcm_free,
320};
321
322struct snd_soc_platform *imx_ssi_dma_mx2_init(struct platform_device *pdev,
323 struct imx_ssi *ssi)
324{
325 ssi->dma_params_tx.burstsize = DMA_TXFIFO_BURST;
326 ssi->dma_params_rx.burstsize = DMA_RXFIFO_BURST;
327
328 return &imx_soc_platform_dma;
329}
330