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>
19 #include <linux/module.h>
21 #include <sound/soc.h>
22 #include <sound/pcm_params.h>
25 #include <mach/hardware.h>
30 #define ST_RUNNING (1<<0)
31 #define ST_OPENED (1<<1)
33 static const struct snd_pcm_hardware dma_hardware = {
34 .info = SNDRV_PCM_INFO_INTERLEAVED |
35 SNDRV_PCM_INFO_BLOCK_TRANSFER |
37 SNDRV_PCM_INFO_MMAP_VALID,
38 .buffer_bytes_max = 128*1024,
39 .period_bytes_min = PAGE_SIZE,
40 .period_bytes_max = PAGE_SIZE*2,
49 unsigned int dma_loaded;
50 unsigned int dma_period;
54 struct s3c_dma_params *params;
57 static void audio_buffdone(void *data);
61 * place a dma buffer onto the queue for the dma system
64 static void dma_enqueue(struct snd_pcm_substream *substream)
66 struct runtime_data *prtd = substream->runtime->private_data;
67 dma_addr_t pos = prtd->dma_pos;
69 struct samsung_dma_prep dma_info;
71 pr_debug("Entered %s\n", __func__);
73 limit = (prtd->dma_end - prtd->dma_start) / prtd->dma_period;
75 pr_debug("%s: loaded %d, limit %d\n",
76 __func__, prtd->dma_loaded, limit);
78 dma_info.cap = (samsung_dma_has_circular() ? DMA_CYCLIC : DMA_SLAVE);
80 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
81 ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
82 dma_info.fp = audio_buffdone;
83 dma_info.fp_param = substream;
84 dma_info.period = prtd->dma_period;
85 dma_info.len = prtd->dma_period*limit;
87 if (dma_info.cap == DMA_CYCLIC) {
89 prtd->params->ops->prepare(prtd->params->ch, &dma_info);
90 prtd->dma_loaded += 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);
104 prtd->params->ops->prepare(prtd->params->ch, &dma_info);
107 pos += prtd->dma_period;
108 if (pos >= prtd->dma_end)
109 pos = prtd->dma_start;
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;
128 snd_pcm_period_elapsed(substream);
130 spin_lock(&prtd->lock);
131 if (!samsung_dma_has_circular()) {
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_req req;
149 struct samsung_dma_config config;
151 pr_debug("Entered %s\n", __func__);
153 /* return if this is a bufferless transfer e.g.
154 * codec <--> BT codec or GSM modem -- lg FIXME */
158 /* this may get called several times by oss emulation
159 * with different params -HW */
160 if (prtd->params == NULL) {
164 pr_debug("params %p, client %p, channel %d\n", prtd->params,
165 prtd->params->client, prtd->params->channel);
167 prtd->params->ops = samsung_dma_get_ops();
169 req.cap = (samsung_dma_has_circular() ?
170 DMA_CYCLIC : DMA_SLAVE);
171 req.client = prtd->params->client;
173 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
174 ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
175 config.width = prtd->params->dma_size;
176 config.fifo = prtd->params->dma_addr;
177 prtd->params->ch = prtd->params->ops->request(
178 prtd->params->channel, &req, rtd->cpu_dai->dev,
179 prtd->params->ch_name);
180 if (!prtd->params->ch) {
181 pr_err("Failed to allocate DMA channel\n");
184 prtd->params->ops->config(prtd->params->ch, &config);
187 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
189 runtime->dma_bytes = totbytes;
191 spin_lock_irq(&prtd->lock);
192 prtd->dma_loaded = 0;
193 prtd->dma_period = params_period_bytes(params);
194 prtd->dma_start = runtime->dma_addr;
195 prtd->dma_pos = prtd->dma_start;
196 prtd->dma_end = prtd->dma_start + totbytes;
197 spin_unlock_irq(&prtd->lock);
202 static int dma_hw_free(struct snd_pcm_substream *substream)
204 struct runtime_data *prtd = substream->runtime->private_data;
206 pr_debug("Entered %s\n", __func__);
208 snd_pcm_set_runtime_buffer(substream, NULL);
211 prtd->params->ops->flush(prtd->params->ch);
212 prtd->params->ops->release(prtd->params->ch,
213 prtd->params->client);
220 static int dma_prepare(struct snd_pcm_substream *substream)
222 struct runtime_data *prtd = substream->runtime->private_data;
225 pr_debug("Entered %s\n", __func__);
227 /* return if this is a bufferless transfer e.g.
228 * codec <--> BT codec or GSM modem -- lg FIXME */
232 /* flush the DMA channel */
233 prtd->params->ops->flush(prtd->params->ch);
235 prtd->dma_loaded = 0;
236 prtd->dma_pos = prtd->dma_start;
238 /* enqueue dma buffers */
239 dma_enqueue(substream);
244 static int dma_trigger(struct snd_pcm_substream *substream, int cmd)
246 struct runtime_data *prtd = substream->runtime->private_data;
249 pr_debug("Entered %s\n", __func__);
251 spin_lock(&prtd->lock);
254 case SNDRV_PCM_TRIGGER_START:
255 prtd->state |= ST_RUNNING;
256 prtd->params->ops->trigger(prtd->params->ch);
259 case SNDRV_PCM_TRIGGER_STOP:
260 prtd->state &= ~ST_RUNNING;
261 prtd->params->ops->stop(prtd->params->ch);
269 spin_unlock(&prtd->lock);
274 static snd_pcm_uframes_t
275 dma_pointer(struct snd_pcm_substream *substream)
277 struct snd_pcm_runtime *runtime = substream->runtime;
278 struct runtime_data *prtd = runtime->private_data;
281 pr_debug("Entered %s\n", __func__);
283 res = prtd->dma_pos - prtd->dma_start;
285 pr_debug("Pointer offset: %lu\n", res);
287 /* we seem to be getting the odd error from the pcm library due
288 * to out-of-bounds pointers. this is maybe due to the dma engine
289 * not having loaded the new values for the channel before being
290 * called... (todo - fix )
293 if (res >= snd_pcm_lib_buffer_bytes(substream)) {
294 if (res == snd_pcm_lib_buffer_bytes(substream))
298 return bytes_to_frames(substream->runtime, res);
301 static int dma_open(struct snd_pcm_substream *substream)
303 struct snd_pcm_runtime *runtime = substream->runtime;
304 struct runtime_data *prtd;
306 pr_debug("Entered %s\n", __func__);
308 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
309 snd_soc_set_runtime_hwparams(substream, &dma_hardware);
311 prtd = kzalloc(sizeof(struct runtime_data), GFP_KERNEL);
315 spin_lock_init(&prtd->lock);
317 runtime->private_data = prtd;
321 static int dma_close(struct snd_pcm_substream *substream)
323 struct snd_pcm_runtime *runtime = substream->runtime;
324 struct runtime_data *prtd = runtime->private_data;
326 pr_debug("Entered %s\n", __func__);
329 pr_debug("dma_close called with prtd == NULL\n");
336 static int dma_mmap(struct snd_pcm_substream *substream,
337 struct vm_area_struct *vma)
339 struct snd_pcm_runtime *runtime = substream->runtime;
341 pr_debug("Entered %s\n", __func__);
343 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
349 static struct snd_pcm_ops dma_ops = {
352 .ioctl = snd_pcm_lib_ioctl,
353 .hw_params = dma_hw_params,
354 .hw_free = dma_hw_free,
355 .prepare = dma_prepare,
356 .trigger = dma_trigger,
357 .pointer = dma_pointer,
361 static int preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
363 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
364 struct snd_dma_buffer *buf = &substream->dma_buffer;
365 size_t size = dma_hardware.buffer_bytes_max;
367 pr_debug("Entered %s\n", __func__);
369 buf->dev.type = SNDRV_DMA_TYPE_DEV;
370 buf->dev.dev = pcm->card->dev;
371 buf->private_data = NULL;
372 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
373 &buf->addr, GFP_KERNEL);
380 static void dma_free_dma_buffers(struct snd_pcm *pcm)
382 struct snd_pcm_substream *substream;
383 struct snd_dma_buffer *buf;
386 pr_debug("Entered %s\n", __func__);
388 for (stream = 0; stream < 2; stream++) {
389 substream = pcm->streams[stream].substream;
393 buf = &substream->dma_buffer;
397 dma_free_writecombine(pcm->card->dev, buf->bytes,
398 buf->area, buf->addr);
403 static int dma_new(struct snd_soc_pcm_runtime *rtd)
405 struct snd_card *card = rtd->card->snd_card;
406 struct snd_pcm *pcm = rtd->pcm;
409 pr_debug("Entered %s\n", __func__);
411 ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
415 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
416 ret = preallocate_dma_buffer(pcm,
417 SNDRV_PCM_STREAM_PLAYBACK);
422 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
423 ret = preallocate_dma_buffer(pcm,
424 SNDRV_PCM_STREAM_CAPTURE);
432 static struct snd_soc_platform_driver samsung_asoc_platform = {
435 .pcm_free = dma_free_dma_buffers,
438 void samsung_asoc_init_dma_data(struct snd_soc_dai *dai,
439 struct s3c_dma_params *playback,
440 struct s3c_dma_params *capture)
442 snd_soc_dai_init_dma_data(dai, playback, capture);
444 EXPORT_SYMBOL_GPL(samsung_asoc_init_dma_data);
446 int samsung_asoc_dma_platform_register(struct device *dev)
448 return snd_soc_register_platform(dev, &samsung_asoc_platform);
450 EXPORT_SYMBOL_GPL(samsung_asoc_dma_platform_register);
452 void samsung_asoc_dma_platform_unregister(struct device *dev)
454 snd_soc_unregister_platform(dev);
456 EXPORT_SYMBOL_GPL(samsung_asoc_dma_platform_unregister);
458 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
459 MODULE_DESCRIPTION("Samsung ASoC DMA Driver");
460 MODULE_LICENSE("GPL");