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