Merge branch 'fix/asoc' into for-linus
[safe/jmp/linux-2.6] / sound / soc / imx / imx-pcm-dma-mx2.c
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 #include <linux/slab.h>
23
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>
29
30 #include <mach/dma-mx1-mx2.h>
31
32 #include "imx-ssi.h"
33
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;
47 };
48
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)
52 {
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;
56
57         if (!sg)
58                 return;
59
60         runtime = iprtd->substream->runtime;
61
62         iprtd->offset = sg->dma_address - runtime->dma_addr;
63
64         snd_pcm_period_elapsed(iprtd->substream);
65 }
66
67 static void imx_ssi_dma_callback(int channel, void *data)
68 {
69         pr_err("%s shouldn't be called\n", __func__);
70 }
71
72 static void snd_imx_dma_err_callback(int channel, void *data, int err)
73 {
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;
80
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" : "");
87
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);
95 }
96
97 static int imx_ssi_dma_alloc(struct snd_pcm_substream *substream)
98 {
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;
104
105         dma_params = snd_soc_get_dma_data(rtd->dai->cpu_dai, substream);
106
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;
111         }
112
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;
118
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;
124         }
125
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;
133         }
134
135         imx_dma_config_burstlen(iprtd->dma, dma_params->burstsize * 2);
136
137         return 0;
138 out:
139         imx_dma_free(iprtd->dma);
140         return ret;
141 }
142
143 static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
144                                 struct snd_pcm_hw_params *params)
145 {
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;
150
151         imx_ssi_dma_alloc(substream);
152
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));
159
160         snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
161
162         if (iprtd->sg_count != iprtd->periods) {
163                 kfree(iprtd->sg_list);
164
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;
170         }
171
172         sg_init_table(iprtd->sg_list, iprtd->sg_count);
173         dma_addr = runtime->dma_addr;
174
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;
181         }
182
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;
189 }
190
191 static int snd_imx_pcm_hw_free(struct snd_pcm_substream *substream)
192 {
193         struct snd_pcm_runtime *runtime = substream->runtime;
194         struct imx_pcm_runtime_data *iprtd = runtime->private_data;
195
196         if (iprtd->dma >= 0) {
197                 imx_dma_free(iprtd->dma);
198                 iprtd->dma = -EINVAL;
199         }
200
201         kfree(iprtd->sg_list);
202         iprtd->sg_list = NULL;
203
204         return 0;
205 }
206
207 static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
208 {
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;
214
215         dma_params = snd_soc_get_dma_data(rtd->dai->cpu_dai, substream);
216
217         iprtd->substream = substream;
218         iprtd->buf = (unsigned int *)substream->dma_buffer.area;
219         iprtd->period_cnt = 0;
220
221         pr_debug("%s: buf: %p period: %d periods: %d\n",
222                         __func__, iprtd->buf, iprtd->period, iprtd->periods);
223
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;
230
231         return 0;
232 }
233
234 static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
235 {
236         struct snd_pcm_runtime *runtime = substream->runtime;
237         struct imx_pcm_runtime_data *iprtd = runtime->private_data;
238
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);
244
245                 break;
246
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);
251
252                 break;
253         default:
254                 return -EINVAL;
255         }
256
257         return 0;
258 }
259
260 static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream)
261 {
262         struct snd_pcm_runtime *runtime = substream->runtime;
263         struct imx_pcm_runtime_data *iprtd = runtime->private_data;
264
265         return bytes_to_frames(substream->runtime, iprtd->offset);
266 }
267
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,
285 };
286
287 static int snd_imx_open(struct snd_pcm_substream *substream)
288 {
289         struct snd_pcm_runtime *runtime = substream->runtime;
290         struct imx_pcm_runtime_data *iprtd;
291         int ret;
292
293         iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
294         runtime->private_data = iprtd;
295
296         ret = snd_pcm_hw_constraint_integer(substream->runtime,
297                         SNDRV_PCM_HW_PARAM_PERIODS);
298         if (ret < 0)
299                 return ret;
300
301         snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
302         return 0;
303 }
304
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,
314 };
315
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,
321 };
322
323 struct snd_soc_platform *imx_ssi_dma_mx2_init(struct platform_device *pdev,
324                 struct imx_ssi *ssi)
325 {
326         ssi->dma_params_tx.burstsize = DMA_TXFIFO_BURST;
327         ssi->dma_params_rx.burstsize = DMA_RXFIFO_BURST;
328
329         return &imx_soc_platform_dma;
330 }
331