ASoC: Flush Samsung DMA on free
[linux-2.6/libata-dev.git] / sound / soc / samsung / dma.c
blob2d622b635e68babe502615286aaf7e475741918e
1 /*
2 * dma.c -- ALSA Soc Audio Layer
4 * (c) 2006 Wolfson Microelectronics PLC.
5 * Graeme Gregory graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
7 * Copyright 2004-2005 Simtec Electronics
8 * http://armlinux.simtec.co.uk/
9 * Ben Dooks <ben@simtec.co.uk>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
17 #include <linux/slab.h>
18 #include <linux/dma-mapping.h>
20 #include <sound/soc.h>
21 #include <sound/pcm_params.h>
23 #include <asm/dma.h>
24 #include <mach/hardware.h>
25 #include <mach/dma.h>
27 #include "dma.h"
29 #define ST_RUNNING (1<<0)
30 #define ST_OPENED (1<<1)
32 static const struct snd_pcm_hardware dma_hardware = {
33 .info = SNDRV_PCM_INFO_INTERLEAVED |
34 SNDRV_PCM_INFO_BLOCK_TRANSFER |
35 SNDRV_PCM_INFO_MMAP |
36 SNDRV_PCM_INFO_MMAP_VALID |
37 SNDRV_PCM_INFO_PAUSE |
38 SNDRV_PCM_INFO_RESUME,
39 .formats = SNDRV_PCM_FMTBIT_S16_LE |
40 SNDRV_PCM_FMTBIT_U16_LE |
41 SNDRV_PCM_FMTBIT_U8 |
42 SNDRV_PCM_FMTBIT_S8,
43 .channels_min = 2,
44 .channels_max = 2,
45 .buffer_bytes_max = 128*1024,
46 .period_bytes_min = PAGE_SIZE,
47 .period_bytes_max = PAGE_SIZE*2,
48 .periods_min = 2,
49 .periods_max = 128,
50 .fifo_size = 32,
53 struct runtime_data {
54 spinlock_t lock;
55 int state;
56 unsigned int dma_loaded;
57 unsigned int dma_period;
58 dma_addr_t dma_start;
59 dma_addr_t dma_pos;
60 dma_addr_t dma_end;
61 struct s3c_dma_params *params;
64 static void audio_buffdone(void *data);
66 /* dma_enqueue
68 * place a dma buffer onto the queue for the dma system
69 * to handle.
71 static void dma_enqueue(struct snd_pcm_substream *substream)
73 struct runtime_data *prtd = substream->runtime->private_data;
74 dma_addr_t pos = prtd->dma_pos;
75 unsigned int limit;
76 struct samsung_dma_prep_info dma_info;
78 pr_debug("Entered %s\n", __func__);
80 limit = (prtd->dma_end - prtd->dma_start) / prtd->dma_period;
82 pr_debug("%s: loaded %d, limit %d\n",
83 __func__, prtd->dma_loaded, limit);
85 dma_info.cap = (samsung_dma_has_circular() ? DMA_CYCLIC : DMA_SLAVE);
86 dma_info.direction =
87 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
88 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
89 dma_info.fp = audio_buffdone;
90 dma_info.fp_param = substream;
91 dma_info.period = prtd->dma_period;
92 dma_info.len = prtd->dma_period*limit;
94 while (prtd->dma_loaded < limit) {
95 pr_debug("dma_loaded: %d\n", prtd->dma_loaded);
97 if ((pos + dma_info.period) > prtd->dma_end) {
98 dma_info.period = prtd->dma_end - pos;
99 pr_debug("%s: corrected dma len %ld\n",
100 __func__, dma_info.period);
103 dma_info.buf = pos;
104 prtd->params->ops->prepare(prtd->params->ch, &dma_info);
106 prtd->dma_loaded++;
107 pos += prtd->dma_period;
108 if (pos >= prtd->dma_end)
109 pos = prtd->dma_start;
112 prtd->dma_pos = pos;
115 static void audio_buffdone(void *data)
117 struct snd_pcm_substream *substream = data;
118 struct runtime_data *prtd = substream->runtime->private_data;
120 pr_debug("Entered %s\n", __func__);
122 if (prtd->state & ST_RUNNING) {
123 prtd->dma_pos += prtd->dma_period;
124 if (prtd->dma_pos >= prtd->dma_end)
125 prtd->dma_pos = prtd->dma_start;
127 if (substream)
128 snd_pcm_period_elapsed(substream);
130 spin_lock(&prtd->lock);
131 if (!samsung_dma_has_circular()) {
132 prtd->dma_loaded--;
133 dma_enqueue(substream);
135 spin_unlock(&prtd->lock);
139 static int dma_hw_params(struct snd_pcm_substream *substream,
140 struct snd_pcm_hw_params *params)
142 struct snd_pcm_runtime *runtime = substream->runtime;
143 struct runtime_data *prtd = runtime->private_data;
144 struct snd_soc_pcm_runtime *rtd = substream->private_data;
145 unsigned long totbytes = params_buffer_bytes(params);
146 struct s3c_dma_params *dma =
147 snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
148 struct samsung_dma_info dma_info;
150 pr_debug("Entered %s\n", __func__);
152 /* return if this is a bufferless transfer e.g.
153 * codec <--> BT codec or GSM modem -- lg FIXME */
154 if (!dma)
155 return 0;
157 /* this may get called several times by oss emulation
158 * with different params -HW */
159 if (prtd->params == NULL) {
160 /* prepare DMA */
161 prtd->params = dma;
163 pr_debug("params %p, client %p, channel %d\n", prtd->params,
164 prtd->params->client, prtd->params->channel);
166 prtd->params->ops = samsung_dma_get_ops();
168 dma_info.cap = (samsung_dma_has_circular() ?
169 DMA_CYCLIC : DMA_SLAVE);
170 dma_info.client = prtd->params->client;
171 dma_info.direction =
172 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
173 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
174 dma_info.width = prtd->params->dma_size;
175 dma_info.fifo = prtd->params->dma_addr;
176 prtd->params->ch = prtd->params->ops->request(
177 prtd->params->channel, &dma_info);
180 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
182 runtime->dma_bytes = totbytes;
184 spin_lock_irq(&prtd->lock);
185 prtd->dma_loaded = 0;
186 prtd->dma_period = params_period_bytes(params);
187 prtd->dma_start = runtime->dma_addr;
188 prtd->dma_pos = prtd->dma_start;
189 prtd->dma_end = prtd->dma_start + totbytes;
190 spin_unlock_irq(&prtd->lock);
192 return 0;
195 static int dma_hw_free(struct snd_pcm_substream *substream)
197 struct runtime_data *prtd = substream->runtime->private_data;
199 pr_debug("Entered %s\n", __func__);
201 snd_pcm_set_runtime_buffer(substream, NULL);
203 if (prtd->params) {
204 prtd->params->ops->flush(prtd->params->ch);
205 prtd->params->ops->release(prtd->params->ch,
206 prtd->params->client);
207 prtd->params = NULL;
210 return 0;
213 static int dma_prepare(struct snd_pcm_substream *substream)
215 struct runtime_data *prtd = substream->runtime->private_data;
216 int ret = 0;
218 pr_debug("Entered %s\n", __func__);
220 /* return if this is a bufferless transfer e.g.
221 * codec <--> BT codec or GSM modem -- lg FIXME */
222 if (!prtd->params)
223 return 0;
225 /* flush the DMA channel */
226 prtd->params->ops->flush(prtd->params->ch);
228 prtd->dma_loaded = 0;
229 prtd->dma_pos = prtd->dma_start;
231 /* enqueue dma buffers */
232 dma_enqueue(substream);
234 return ret;
237 static int dma_trigger(struct snd_pcm_substream *substream, int cmd)
239 struct runtime_data *prtd = substream->runtime->private_data;
240 int ret = 0;
242 pr_debug("Entered %s\n", __func__);
244 spin_lock(&prtd->lock);
246 switch (cmd) {
247 case SNDRV_PCM_TRIGGER_START:
248 case SNDRV_PCM_TRIGGER_RESUME:
249 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
250 prtd->state |= ST_RUNNING;
251 prtd->params->ops->trigger(prtd->params->ch);
252 break;
254 case SNDRV_PCM_TRIGGER_STOP:
255 case SNDRV_PCM_TRIGGER_SUSPEND:
256 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
257 prtd->state &= ~ST_RUNNING;
258 prtd->params->ops->stop(prtd->params->ch);
259 break;
261 default:
262 ret = -EINVAL;
263 break;
266 spin_unlock(&prtd->lock);
268 return ret;
271 static snd_pcm_uframes_t
272 dma_pointer(struct snd_pcm_substream *substream)
274 struct snd_pcm_runtime *runtime = substream->runtime;
275 struct runtime_data *prtd = runtime->private_data;
276 unsigned long res;
278 pr_debug("Entered %s\n", __func__);
280 res = prtd->dma_pos - prtd->dma_start;
282 pr_debug("Pointer offset: %lu\n", res);
284 /* we seem to be getting the odd error from the pcm library due
285 * to out-of-bounds pointers. this is maybe due to the dma engine
286 * not having loaded the new values for the channel before being
287 * called... (todo - fix )
290 if (res >= snd_pcm_lib_buffer_bytes(substream)) {
291 if (res == snd_pcm_lib_buffer_bytes(substream))
292 res = 0;
295 return bytes_to_frames(substream->runtime, res);
298 static int dma_open(struct snd_pcm_substream *substream)
300 struct snd_pcm_runtime *runtime = substream->runtime;
301 struct runtime_data *prtd;
303 pr_debug("Entered %s\n", __func__);
305 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
306 snd_soc_set_runtime_hwparams(substream, &dma_hardware);
308 prtd = kzalloc(sizeof(struct runtime_data), GFP_KERNEL);
309 if (prtd == NULL)
310 return -ENOMEM;
312 spin_lock_init(&prtd->lock);
314 runtime->private_data = prtd;
315 return 0;
318 static int dma_close(struct snd_pcm_substream *substream)
320 struct snd_pcm_runtime *runtime = substream->runtime;
321 struct runtime_data *prtd = runtime->private_data;
323 pr_debug("Entered %s\n", __func__);
325 if (!prtd)
326 pr_debug("dma_close called with prtd == NULL\n");
328 kfree(prtd);
330 return 0;
333 static int dma_mmap(struct snd_pcm_substream *substream,
334 struct vm_area_struct *vma)
336 struct snd_pcm_runtime *runtime = substream->runtime;
338 pr_debug("Entered %s\n", __func__);
340 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
341 runtime->dma_area,
342 runtime->dma_addr,
343 runtime->dma_bytes);
346 static struct snd_pcm_ops dma_ops = {
347 .open = dma_open,
348 .close = dma_close,
349 .ioctl = snd_pcm_lib_ioctl,
350 .hw_params = dma_hw_params,
351 .hw_free = dma_hw_free,
352 .prepare = dma_prepare,
353 .trigger = dma_trigger,
354 .pointer = dma_pointer,
355 .mmap = dma_mmap,
358 static int preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
360 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
361 struct snd_dma_buffer *buf = &substream->dma_buffer;
362 size_t size = dma_hardware.buffer_bytes_max;
364 pr_debug("Entered %s\n", __func__);
366 buf->dev.type = SNDRV_DMA_TYPE_DEV;
367 buf->dev.dev = pcm->card->dev;
368 buf->private_data = NULL;
369 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
370 &buf->addr, GFP_KERNEL);
371 if (!buf->area)
372 return -ENOMEM;
373 buf->bytes = size;
374 return 0;
377 static void dma_free_dma_buffers(struct snd_pcm *pcm)
379 struct snd_pcm_substream *substream;
380 struct snd_dma_buffer *buf;
381 int stream;
383 pr_debug("Entered %s\n", __func__);
385 for (stream = 0; stream < 2; stream++) {
386 substream = pcm->streams[stream].substream;
387 if (!substream)
388 continue;
390 buf = &substream->dma_buffer;
391 if (!buf->area)
392 continue;
394 dma_free_writecombine(pcm->card->dev, buf->bytes,
395 buf->area, buf->addr);
396 buf->area = NULL;
400 static u64 dma_mask = DMA_BIT_MASK(32);
402 static int dma_new(struct snd_soc_pcm_runtime *rtd)
404 struct snd_card *card = rtd->card->snd_card;
405 struct snd_soc_dai *dai = rtd->cpu_dai;
406 struct snd_pcm *pcm = rtd->pcm;
407 int ret = 0;
409 pr_debug("Entered %s\n", __func__);
411 if (!card->dev->dma_mask)
412 card->dev->dma_mask = &dma_mask;
413 if (!card->dev->coherent_dma_mask)
414 card->dev->coherent_dma_mask = 0xffffffff;
416 if (dai->driver->playback.channels_min) {
417 ret = preallocate_dma_buffer(pcm,
418 SNDRV_PCM_STREAM_PLAYBACK);
419 if (ret)
420 goto out;
423 if (dai->driver->capture.channels_min) {
424 ret = preallocate_dma_buffer(pcm,
425 SNDRV_PCM_STREAM_CAPTURE);
426 if (ret)
427 goto out;
429 out:
430 return ret;
433 static struct snd_soc_platform_driver samsung_asoc_platform = {
434 .ops = &dma_ops,
435 .pcm_new = dma_new,
436 .pcm_free = dma_free_dma_buffers,
439 static int __devinit samsung_asoc_platform_probe(struct platform_device *pdev)
441 return snd_soc_register_platform(&pdev->dev, &samsung_asoc_platform);
444 static int __devexit samsung_asoc_platform_remove(struct platform_device *pdev)
446 snd_soc_unregister_platform(&pdev->dev);
447 return 0;
450 static struct platform_driver asoc_dma_driver = {
451 .driver = {
452 .name = "samsung-audio",
453 .owner = THIS_MODULE,
456 .probe = samsung_asoc_platform_probe,
457 .remove = __devexit_p(samsung_asoc_platform_remove),
460 static int __init samsung_asoc_init(void)
462 return platform_driver_register(&asoc_dma_driver);
464 module_init(samsung_asoc_init);
466 static void __exit samsung_asoc_exit(void)
468 platform_driver_unregister(&asoc_dma_driver);
470 module_exit(samsung_asoc_exit);
472 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
473 MODULE_DESCRIPTION("Samsung ASoC DMA Driver");
474 MODULE_LICENSE("GPL");
475 MODULE_ALIAS("platform:samsung-audio");