Merge branch 'for-2.6.32' into for-2.6.33
[cascardo/linux.git] / 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 #include <mach/edma.h>
26
27 #include "davinci-pcm.h"
28
29 static struct snd_pcm_hardware davinci_pcm_hardware = {
30         .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
31                  SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
32                  SNDRV_PCM_INFO_PAUSE),
33         .formats = (SNDRV_PCM_FMTBIT_S16_LE),
34         .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
35                   SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
36                   SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
37                   SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
38                   SNDRV_PCM_RATE_KNOT),
39         .rate_min = 8000,
40         .rate_max = 96000,
41         .channels_min = 2,
42         .channels_max = 2,
43         .buffer_bytes_max = 128 * 1024,
44         .period_bytes_min = 32,
45         .period_bytes_max = 8 * 1024,
46         .periods_min = 16,
47         .periods_max = 255,
48         .fifo_size = 0,
49 };
50
51 struct davinci_runtime_data {
52         spinlock_t lock;
53         int period;             /* current DMA period */
54         int master_lch;         /* Master DMA channel */
55         int slave_lch;          /* linked parameter RAM reload slot */
56         struct davinci_pcm_dma_params *params;  /* DMA params */
57 };
58
59 static void davinci_pcm_enqueue_dma(struct snd_pcm_substream *substream)
60 {
61         struct davinci_runtime_data *prtd = substream->runtime->private_data;
62         struct snd_pcm_runtime *runtime = substream->runtime;
63         int lch = prtd->slave_lch;
64         unsigned int period_size;
65         unsigned int dma_offset;
66         dma_addr_t dma_pos;
67         dma_addr_t src, dst;
68         unsigned short src_bidx, dst_bidx;
69         unsigned short src_cidx, dst_cidx;
70         unsigned int data_type;
71         unsigned short acnt;
72         unsigned int count;
73         unsigned int fifo_level;
74
75         period_size = snd_pcm_lib_period_bytes(substream);
76         dma_offset = prtd->period * period_size;
77         dma_pos = runtime->dma_addr + dma_offset;
78         fifo_level = prtd->params->fifo_level;
79
80         pr_debug("davinci_pcm: audio_set_dma_params_play channel = %d "
81                 "dma_ptr = %x period_size=%x\n", lch, dma_pos, period_size);
82
83         data_type = prtd->params->data_type;
84         count = period_size / data_type;
85         if (fifo_level)
86                 count /= fifo_level;
87
88         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
89                 src = dma_pos;
90                 dst = prtd->params->dma_addr;
91                 src_bidx = data_type;
92                 dst_bidx = 0;
93                 src_cidx = data_type * fifo_level;
94                 dst_cidx = 0;
95         } else {
96                 src = prtd->params->dma_addr;
97                 dst = dma_pos;
98                 src_bidx = 0;
99                 dst_bidx = data_type;
100                 src_cidx = 0;
101                 dst_cidx = data_type * fifo_level;
102         }
103
104         acnt = prtd->params->acnt;
105         edma_set_src(lch, src, INCR, W8BIT);
106         edma_set_dest(lch, dst, INCR, W8BIT);
107
108         edma_set_src_index(lch, src_bidx, src_cidx);
109         edma_set_dest_index(lch, dst_bidx, dst_cidx);
110
111         if (!fifo_level)
112                 edma_set_transfer_params(lch, acnt, count, 1, 0, ASYNC);
113         else
114                 edma_set_transfer_params(lch, acnt, fifo_level, count,
115                                                         fifo_level, ABSYNC);
116
117         prtd->period++;
118         if (unlikely(prtd->period >= runtime->periods))
119                 prtd->period = 0;
120 }
121
122 static void davinci_pcm_dma_irq(unsigned lch, u16 ch_status, void *data)
123 {
124         struct snd_pcm_substream *substream = data;
125         struct davinci_runtime_data *prtd = substream->runtime->private_data;
126
127         pr_debug("davinci_pcm: lch=%d, status=0x%x\n", lch, ch_status);
128
129         if (unlikely(ch_status != DMA_COMPLETE))
130                 return;
131
132         if (snd_pcm_running(substream)) {
133                 snd_pcm_period_elapsed(substream);
134
135                 spin_lock(&prtd->lock);
136                 davinci_pcm_enqueue_dma(substream);
137                 spin_unlock(&prtd->lock);
138         }
139 }
140
141 static int davinci_pcm_dma_request(struct snd_pcm_substream *substream)
142 {
143         struct davinci_runtime_data *prtd = substream->runtime->private_data;
144         struct edmacc_param p_ram;
145         int ret;
146
147         /* Request master DMA channel */
148         ret = edma_alloc_channel(prtd->params->channel,
149                                   davinci_pcm_dma_irq, substream,
150                                   EVENTQ_0);
151         if (ret < 0)
152                 return ret;
153         prtd->master_lch = ret;
154
155         /* Request parameter RAM reload slot */
156         ret = edma_alloc_slot(EDMA_CTLR(prtd->master_lch), EDMA_SLOT_ANY);
157         if (ret < 0) {
158                 edma_free_channel(prtd->master_lch);
159                 return ret;
160         }
161         prtd->slave_lch = ret;
162
163         /* Issue transfer completion IRQ when the channel completes a
164          * transfer, then always reload from the same slot (by a kind
165          * of loopback link).  The completion IRQ handler will update
166          * the reload slot with a new buffer.
167          *
168          * REVISIT save p_ram here after setting up everything except
169          * the buffer and its length (ccnt) ... use it as a template
170          * so davinci_pcm_enqueue_dma() takes less time in IRQ.
171          */
172         edma_read_slot(prtd->slave_lch, &p_ram);
173         p_ram.opt |= TCINTEN | EDMA_TCC(EDMA_CHAN_SLOT(prtd->master_lch));
174         p_ram.link_bcntrld = EDMA_CHAN_SLOT(prtd->slave_lch) << 5;
175         edma_write_slot(prtd->slave_lch, &p_ram);
176
177         return 0;
178 }
179
180 static int davinci_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
181 {
182         struct davinci_runtime_data *prtd = substream->runtime->private_data;
183         int ret = 0;
184
185         spin_lock(&prtd->lock);
186
187         switch (cmd) {
188         case SNDRV_PCM_TRIGGER_START:
189         case SNDRV_PCM_TRIGGER_RESUME:
190         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
191                 edma_start(prtd->master_lch);
192                 break;
193         case SNDRV_PCM_TRIGGER_STOP:
194         case SNDRV_PCM_TRIGGER_SUSPEND:
195         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
196                 edma_stop(prtd->master_lch);
197                 break;
198         default:
199                 ret = -EINVAL;
200                 break;
201         }
202
203         spin_unlock(&prtd->lock);
204
205         return ret;
206 }
207
208 static int davinci_pcm_prepare(struct snd_pcm_substream *substream)
209 {
210         struct davinci_runtime_data *prtd = substream->runtime->private_data;
211         struct edmacc_param temp;
212
213         prtd->period = 0;
214         davinci_pcm_enqueue_dma(substream);
215
216         /* Copy self-linked parameter RAM entry into master channel */
217         edma_read_slot(prtd->slave_lch, &temp);
218         edma_write_slot(prtd->master_lch, &temp);
219         davinci_pcm_enqueue_dma(substream);
220
221         return 0;
222 }
223
224 static snd_pcm_uframes_t
225 davinci_pcm_pointer(struct snd_pcm_substream *substream)
226 {
227         struct snd_pcm_runtime *runtime = substream->runtime;
228         struct davinci_runtime_data *prtd = runtime->private_data;
229         unsigned int offset;
230         dma_addr_t count;
231         dma_addr_t src, dst;
232
233         spin_lock(&prtd->lock);
234
235         edma_get_position(prtd->master_lch, &src, &dst);
236         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
237                 count = src - runtime->dma_addr;
238         else
239                 count = dst - runtime->dma_addr;
240
241         spin_unlock(&prtd->lock);
242
243         offset = bytes_to_frames(runtime, count);
244         if (offset >= runtime->buffer_size)
245                 offset = 0;
246
247         return offset;
248 }
249
250 static int davinci_pcm_open(struct snd_pcm_substream *substream)
251 {
252         struct snd_pcm_runtime *runtime = substream->runtime;
253         struct davinci_runtime_data *prtd;
254         int ret = 0;
255         struct snd_soc_pcm_runtime *rtd = substream->private_data;
256         struct davinci_pcm_dma_params *pa = rtd->dai->cpu_dai->private_data;
257         struct davinci_pcm_dma_params *params = &pa[substream->stream];
258         if (!params)
259                 return -ENODEV;
260
261         snd_soc_set_runtime_hwparams(substream, &davinci_pcm_hardware);
262         /* ensure that buffer size is a multiple of period size */
263         ret = snd_pcm_hw_constraint_integer(runtime,
264                                                 SNDRV_PCM_HW_PARAM_PERIODS);
265         if (ret < 0)
266                 return ret;
267
268         prtd = kzalloc(sizeof(struct davinci_runtime_data), GFP_KERNEL);
269         if (prtd == NULL)
270                 return -ENOMEM;
271
272         spin_lock_init(&prtd->lock);
273         prtd->params = params;
274
275         runtime->private_data = prtd;
276
277         ret = davinci_pcm_dma_request(substream);
278         if (ret) {
279                 printk(KERN_ERR "davinci_pcm: Failed to get dma channels\n");
280                 kfree(prtd);
281         }
282
283         return ret;
284 }
285
286 static int davinci_pcm_close(struct snd_pcm_substream *substream)
287 {
288         struct snd_pcm_runtime *runtime = substream->runtime;
289         struct davinci_runtime_data *prtd = runtime->private_data;
290
291         edma_unlink(prtd->slave_lch);
292
293         edma_free_slot(prtd->slave_lch);
294         edma_free_channel(prtd->master_lch);
295
296         kfree(prtd);
297
298         return 0;
299 }
300
301 static int davinci_pcm_hw_params(struct snd_pcm_substream *substream,
302                                  struct snd_pcm_hw_params *hw_params)
303 {
304         return snd_pcm_lib_malloc_pages(substream,
305                                         params_buffer_bytes(hw_params));
306 }
307
308 static int davinci_pcm_hw_free(struct snd_pcm_substream *substream)
309 {
310         return snd_pcm_lib_free_pages(substream);
311 }
312
313 static int davinci_pcm_mmap(struct snd_pcm_substream *substream,
314                             struct vm_area_struct *vma)
315 {
316         struct snd_pcm_runtime *runtime = substream->runtime;
317
318         return dma_mmap_writecombine(substream->pcm->card->dev, vma,
319                                      runtime->dma_area,
320                                      runtime->dma_addr,
321                                      runtime->dma_bytes);
322 }
323
324 static struct snd_pcm_ops davinci_pcm_ops = {
325         .open =         davinci_pcm_open,
326         .close =        davinci_pcm_close,
327         .ioctl =        snd_pcm_lib_ioctl,
328         .hw_params =    davinci_pcm_hw_params,
329         .hw_free =      davinci_pcm_hw_free,
330         .prepare =      davinci_pcm_prepare,
331         .trigger =      davinci_pcm_trigger,
332         .pointer =      davinci_pcm_pointer,
333         .mmap =         davinci_pcm_mmap,
334 };
335
336 static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
337 {
338         struct snd_pcm_substream *substream = pcm->streams[stream].substream;
339         struct snd_dma_buffer *buf = &substream->dma_buffer;
340         size_t size = davinci_pcm_hardware.buffer_bytes_max;
341
342         buf->dev.type = SNDRV_DMA_TYPE_DEV;
343         buf->dev.dev = pcm->card->dev;
344         buf->private_data = NULL;
345         buf->area = dma_alloc_writecombine(pcm->card->dev, size,
346                                            &buf->addr, GFP_KERNEL);
347
348         pr_debug("davinci_pcm: preallocate_dma_buffer: area=%p, addr=%p, "
349                 "size=%d\n", (void *) buf->area, (void *) buf->addr, size);
350
351         if (!buf->area)
352                 return -ENOMEM;
353
354         buf->bytes = size;
355         return 0;
356 }
357
358 static void davinci_pcm_free(struct snd_pcm *pcm)
359 {
360         struct snd_pcm_substream *substream;
361         struct snd_dma_buffer *buf;
362         int stream;
363
364         for (stream = 0; stream < 2; stream++) {
365                 substream = pcm->streams[stream].substream;
366                 if (!substream)
367                         continue;
368
369                 buf = &substream->dma_buffer;
370                 if (!buf->area)
371                         continue;
372
373                 dma_free_writecombine(pcm->card->dev, buf->bytes,
374                                       buf->area, buf->addr);
375                 buf->area = NULL;
376         }
377 }
378
379 static u64 davinci_pcm_dmamask = 0xffffffff;
380
381 static int davinci_pcm_new(struct snd_card *card,
382                            struct snd_soc_dai *dai, struct snd_pcm *pcm)
383 {
384         int ret;
385
386         if (!card->dev->dma_mask)
387                 card->dev->dma_mask = &davinci_pcm_dmamask;
388         if (!card->dev->coherent_dma_mask)
389                 card->dev->coherent_dma_mask = 0xffffffff;
390
391         if (dai->playback.channels_min) {
392                 ret = davinci_pcm_preallocate_dma_buffer(pcm,
393                         SNDRV_PCM_STREAM_PLAYBACK);
394                 if (ret)
395                         return ret;
396         }
397
398         if (dai->capture.channels_min) {
399                 ret = davinci_pcm_preallocate_dma_buffer(pcm,
400                         SNDRV_PCM_STREAM_CAPTURE);
401                 if (ret)
402                         return ret;
403         }
404
405         return 0;
406 }
407
408 struct snd_soc_platform davinci_soc_platform = {
409         .name =         "davinci-audio",
410         .pcm_ops =      &davinci_pcm_ops,
411         .pcm_new =      davinci_pcm_new,
412         .pcm_free =     davinci_pcm_free,
413 };
414 EXPORT_SYMBOL_GPL(davinci_soc_platform);
415
416 static int __init davinci_soc_platform_init(void)
417 {
418         return snd_soc_register_platform(&davinci_soc_platform);
419 }
420 module_init(davinci_soc_platform_init);
421
422 static void __exit davinci_soc_platform_exit(void)
423 {
424         snd_soc_unregister_platform(&davinci_soc_platform);
425 }
426 module_exit(davinci_soc_platform_exit);
427
428 MODULE_AUTHOR("Vladimir Barinov");
429 MODULE_DESCRIPTION("TI DAVINCI PCM DMA module");
430 MODULE_LICENSE("GPL");