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