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