Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / sound / soc / davinci / davinci-pcm.c
1 /*
2  * ALSA PCM interface for the TI DAVINCI processor
3  *
4  * Author:      Vladimir Barinov, <vbarinov@embeddedalley.com>
5  * Copyright:   (C) 2007 MontaVista Software, Inc., <source@mvista.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/kernel.h>
18
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
23
24 #include <asm/dma.h>
25
26 #include "davinci-pcm.h"
27
28 static struct snd_pcm_hardware davinci_pcm_hardware = {
29         .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
30                  SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
31                  SNDRV_PCM_INFO_PAUSE),
32         .formats = (SNDRV_PCM_FMTBIT_S16_LE),
33         .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
34                   SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
35                   SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
36                   SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
37                   SNDRV_PCM_RATE_KNOT),
38         .rate_min = 8000,
39         .rate_max = 96000,
40         .channels_min = 2,
41         .channels_max = 2,
42         .buffer_bytes_max = 128 * 1024,
43         .period_bytes_min = 32,
44         .period_bytes_max = 8 * 1024,
45         .periods_min = 16,
46         .periods_max = 255,
47         .fifo_size = 0,
48 };
49
50 struct davinci_runtime_data {
51         spinlock_t lock;
52         int period;             /* current DMA period */
53         int master_lch;         /* Master DMA channel */
54         int slave_lch;          /* Slave DMA channel */
55         struct davinci_pcm_dma_params *params;  /* DMA params */
56 };
57
58 static void davinci_pcm_enqueue_dma(struct snd_pcm_substream *substream)
59 {
60         struct davinci_runtime_data *prtd = substream->runtime->private_data;
61         struct snd_pcm_runtime *runtime = substream->runtime;
62         int lch = prtd->slave_lch;
63         unsigned int period_size;
64         unsigned int dma_offset;
65         dma_addr_t dma_pos;
66         dma_addr_t src, dst;
67         unsigned short src_bidx, dst_bidx;
68         unsigned int data_type;
69         unsigned int count;
70
71         period_size = snd_pcm_lib_period_bytes(substream);
72         dma_offset = prtd->period * period_size;
73         dma_pos = runtime->dma_addr + dma_offset;
74
75         pr_debug("davinci_pcm: audio_set_dma_params_play channel = %d "
76                 "dma_ptr = %x period_size=%x\n", lch, dma_pos, period_size);
77
78         data_type = prtd->params->data_type;
79         count = period_size / data_type;
80
81         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
82                 src = dma_pos;
83                 dst = prtd->params->dma_addr;
84                 src_bidx = data_type;
85                 dst_bidx = 0;
86         } else {
87                 src = prtd->params->dma_addr;
88                 dst = dma_pos;
89                 src_bidx = 0;
90                 dst_bidx = data_type;
91         }
92
93         davinci_set_dma_src_params(lch, src, INCR, W8BIT);
94         davinci_set_dma_dest_params(lch, dst, INCR, W8BIT);
95         davinci_set_dma_src_index(lch, src_bidx, 0);
96         davinci_set_dma_dest_index(lch, dst_bidx, 0);
97         davinci_set_dma_transfer_params(lch, data_type, count, 1, 0, ASYNC);
98
99         prtd->period++;
100         if (unlikely(prtd->period >= runtime->periods))
101                 prtd->period = 0;
102 }
103
104 static void davinci_pcm_dma_irq(int lch, u16 ch_status, void *data)
105 {
106         struct snd_pcm_substream *substream = data;
107         struct davinci_runtime_data *prtd = substream->runtime->private_data;
108
109         pr_debug("davinci_pcm: lch=%d, status=0x%x\n", lch, ch_status);
110
111         if (unlikely(ch_status != DMA_COMPLETE))
112                 return;
113
114         if (snd_pcm_running(substream)) {
115                 snd_pcm_period_elapsed(substream);
116
117                 spin_lock(&prtd->lock);
118                 davinci_pcm_enqueue_dma(substream);
119                 spin_unlock(&prtd->lock);
120         }
121 }
122
123 static int davinci_pcm_dma_request(struct snd_pcm_substream *substream)
124 {
125         struct davinci_runtime_data *prtd = substream->runtime->private_data;
126         struct snd_soc_pcm_runtime *rtd = substream->private_data;
127         struct davinci_pcm_dma_params *dma_data = rtd->dai->cpu_dai->dma_data;
128         int tcc = TCC_ANY;
129         int ret;
130
131         if (!dma_data)
132                 return -ENODEV;
133
134         prtd->params = dma_data;
135
136         /* Request master DMA channel */
137         ret = davinci_request_dma(prtd->params->channel, prtd->params->name,
138                                   davinci_pcm_dma_irq, substream,
139                                   &prtd->master_lch, &tcc, EVENTQ_0);
140         if (ret)
141                 return ret;
142
143         /* Request slave DMA channel */
144         ret = davinci_request_dma(PARAM_ANY, "Link",
145                                   NULL, NULL, &prtd->slave_lch, &tcc, EVENTQ_0);
146         if (ret) {
147                 davinci_free_dma(prtd->master_lch);
148                 return ret;
149         }
150
151         /* Link slave DMA channel in loopback */
152         davinci_dma_link_lch(prtd->slave_lch, prtd->slave_lch);
153
154         return 0;
155 }
156
157 static int davinci_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
158 {
159         struct davinci_runtime_data *prtd = substream->runtime->private_data;
160         int ret = 0;
161
162         spin_lock(&prtd->lock);
163
164         switch (cmd) {
165         case SNDRV_PCM_TRIGGER_START:
166         case SNDRV_PCM_TRIGGER_RESUME:
167         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
168                 davinci_start_dma(prtd->master_lch);
169                 break;
170         case SNDRV_PCM_TRIGGER_STOP:
171         case SNDRV_PCM_TRIGGER_SUSPEND:
172         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
173                 davinci_stop_dma(prtd->master_lch);
174                 break;
175         default:
176                 ret = -EINVAL;
177                 break;
178         }
179
180         spin_unlock(&prtd->lock);
181
182         return ret;
183 }
184
185 static int davinci_pcm_prepare(struct snd_pcm_substream *substream)
186 {
187         struct davinci_runtime_data *prtd = substream->runtime->private_data;
188         struct paramentry_descriptor temp;
189
190         prtd->period = 0;
191         davinci_pcm_enqueue_dma(substream);
192
193         /* Get slave channel dma params for master channel startup */
194         davinci_get_dma_params(prtd->slave_lch, &temp);
195         davinci_set_dma_params(prtd->master_lch, &temp);
196
197         return 0;
198 }
199
200 static snd_pcm_uframes_t
201 davinci_pcm_pointer(struct snd_pcm_substream *substream)
202 {
203         struct snd_pcm_runtime *runtime = substream->runtime;
204         struct davinci_runtime_data *prtd = runtime->private_data;
205         unsigned int offset;
206         dma_addr_t count;
207         dma_addr_t src, dst;
208
209         spin_lock(&prtd->lock);
210
211         davinci_dma_getposition(prtd->master_lch, &src, &dst);
212         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
213                 count = src - runtime->dma_addr;
214         else
215                 count = dst - runtime->dma_addr;;
216
217         spin_unlock(&prtd->lock);
218
219         offset = bytes_to_frames(runtime, count);
220         if (offset >= runtime->buffer_size)
221                 offset = 0;
222
223         return offset;
224 }
225
226 static int davinci_pcm_open(struct snd_pcm_substream *substream)
227 {
228         struct snd_pcm_runtime *runtime = substream->runtime;
229         struct davinci_runtime_data *prtd;
230         int ret = 0;
231
232         snd_soc_set_runtime_hwparams(substream, &davinci_pcm_hardware);
233
234         prtd = kzalloc(sizeof(struct davinci_runtime_data), GFP_KERNEL);
235         if (prtd == NULL)
236                 return -ENOMEM;
237
238         spin_lock_init(&prtd->lock);
239
240         runtime->private_data = prtd;
241
242         ret = davinci_pcm_dma_request(substream);
243         if (ret) {
244                 printk(KERN_ERR "davinci_pcm: Failed to get dma channels\n");
245                 kfree(prtd);
246         }
247
248         return ret;
249 }
250
251 static int davinci_pcm_close(struct snd_pcm_substream *substream)
252 {
253         struct snd_pcm_runtime *runtime = substream->runtime;
254         struct davinci_runtime_data *prtd = runtime->private_data;
255
256         davinci_dma_unlink_lch(prtd->slave_lch, prtd->slave_lch);
257
258         davinci_free_dma(prtd->slave_lch);
259         davinci_free_dma(prtd->master_lch);
260
261         kfree(prtd);
262
263         return 0;
264 }
265
266 static int davinci_pcm_hw_params(struct snd_pcm_substream *substream,
267                                  struct snd_pcm_hw_params *hw_params)
268 {
269         return snd_pcm_lib_malloc_pages(substream,
270                                         params_buffer_bytes(hw_params));
271 }
272
273 static int davinci_pcm_hw_free(struct snd_pcm_substream *substream)
274 {
275         return snd_pcm_lib_free_pages(substream);
276 }
277
278 static int davinci_pcm_mmap(struct snd_pcm_substream *substream,
279                             struct vm_area_struct *vma)
280 {
281         struct snd_pcm_runtime *runtime = substream->runtime;
282
283         return dma_mmap_writecombine(substream->pcm->card->dev, vma,
284                                      runtime->dma_area,
285                                      runtime->dma_addr,
286                                      runtime->dma_bytes);
287 }
288
289 struct snd_pcm_ops davinci_pcm_ops = {
290         .open =         davinci_pcm_open,
291         .close =        davinci_pcm_close,
292         .ioctl =        snd_pcm_lib_ioctl,
293         .hw_params =    davinci_pcm_hw_params,
294         .hw_free =      davinci_pcm_hw_free,
295         .prepare =      davinci_pcm_prepare,
296         .trigger =      davinci_pcm_trigger,
297         .pointer =      davinci_pcm_pointer,
298         .mmap =         davinci_pcm_mmap,
299 };
300
301 static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
302 {
303         struct snd_pcm_substream *substream = pcm->streams[stream].substream;
304         struct snd_dma_buffer *buf = &substream->dma_buffer;
305         size_t size = davinci_pcm_hardware.buffer_bytes_max;
306
307         buf->dev.type = SNDRV_DMA_TYPE_DEV;
308         buf->dev.dev = pcm->card->dev;
309         buf->private_data = NULL;
310         buf->area = dma_alloc_writecombine(pcm->card->dev, size,
311                                            &buf->addr, GFP_KERNEL);
312
313         pr_debug("davinci_pcm: preallocate_dma_buffer: area=%p, addr=%p, "
314                 "size=%d\n", (void *) buf->area, (void *) buf->addr, size);
315
316         if (!buf->area)
317                 return -ENOMEM;
318
319         buf->bytes = size;
320         return 0;
321 }
322
323 static void davinci_pcm_free(struct snd_pcm *pcm)
324 {
325         struct snd_pcm_substream *substream;
326         struct snd_dma_buffer *buf;
327         int stream;
328
329         for (stream = 0; stream < 2; stream++) {
330                 substream = pcm->streams[stream].substream;
331                 if (!substream)
332                         continue;
333
334                 buf = &substream->dma_buffer;
335                 if (!buf->area)
336                         continue;
337
338                 dma_free_writecombine(pcm->card->dev, buf->bytes,
339                                       buf->area, buf->addr);
340                 buf->area = NULL;
341         }
342 }
343
344 static u64 davinci_pcm_dmamask = 0xffffffff;
345
346 static int davinci_pcm_new(struct snd_card *card,
347                            struct snd_soc_dai *dai, struct snd_pcm *pcm)
348 {
349         int ret;
350
351         if (!card->dev->dma_mask)
352                 card->dev->dma_mask = &davinci_pcm_dmamask;
353         if (!card->dev->coherent_dma_mask)
354                 card->dev->coherent_dma_mask = 0xffffffff;
355
356         if (dai->playback.channels_min) {
357                 ret = davinci_pcm_preallocate_dma_buffer(pcm,
358                         SNDRV_PCM_STREAM_PLAYBACK);
359                 if (ret)
360                         return ret;
361         }
362
363         if (dai->capture.channels_min) {
364                 ret = davinci_pcm_preallocate_dma_buffer(pcm,
365                         SNDRV_PCM_STREAM_CAPTURE);
366                 if (ret)
367                         return ret;
368         }
369
370         return 0;
371 }
372
373 struct snd_soc_platform davinci_soc_platform = {
374         .name =         "davinci-audio",
375         .pcm_ops =      &davinci_pcm_ops,
376         .pcm_new =      davinci_pcm_new,
377         .pcm_free =     davinci_pcm_free,
378 };
379 EXPORT_SYMBOL_GPL(davinci_soc_platform);
380
381 static int __init davinci_soc_platform_init(void)
382 {
383         return snd_soc_register_platform(&davinci_soc_platform);
384 }
385 module_init(davinci_soc_platform_init);
386
387 static void __exit davinci_soc_platform_exit(void)
388 {
389         snd_soc_unregister_platform(&davinci_soc_platform);
390 }
391 module_exit(davinci_soc_platform_exit);
392
393 MODULE_AUTHOR("Vladimir Barinov");
394 MODULE_DESCRIPTION("TI DAVINCI PCM DMA module");
395 MODULE_LICENSE("GPL");