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 SNDRV_PCM_INFO_PAUSE
|
39 SNDRV_PCM_INFO_RESUME
,
40 .formats
= SNDRV_PCM_FMTBIT_S16_LE
|
41 SNDRV_PCM_FMTBIT_U16_LE
|
46 .buffer_bytes_max
= 128*1024,
47 .period_bytes_min
= PAGE_SIZE
,
48 .period_bytes_max
= PAGE_SIZE
*2,
57 unsigned int dma_loaded
;
58 unsigned int dma_period
;
62 struct s3c_dma_params
*params
;
65 static void audio_buffdone(void *data
);
69 * place a dma buffer onto the queue for the dma system
72 static void dma_enqueue(struct snd_pcm_substream
*substream
)
74 struct runtime_data
*prtd
= substream
->runtime
->private_data
;
75 dma_addr_t pos
= prtd
->dma_pos
;
77 struct samsung_dma_prep_info dma_info
;
79 pr_debug("Entered %s\n", __func__
);
81 limit
= (prtd
->dma_end
- prtd
->dma_start
) / prtd
->dma_period
;
83 pr_debug("%s: loaded %d, limit %d\n",
84 __func__
, prtd
->dma_loaded
, limit
);
86 dma_info
.cap
= (samsung_dma_has_circular() ? DMA_CYCLIC
: DMA_SLAVE
);
88 (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
89 ? DMA_MEM_TO_DEV
: DMA_DEV_TO_MEM
);
90 dma_info
.fp
= audio_buffdone
;
91 dma_info
.fp_param
= substream
;
92 dma_info
.period
= prtd
->dma_period
;
93 dma_info
.len
= prtd
->dma_period
*limit
;
95 while (prtd
->dma_loaded
< limit
) {
96 pr_debug("dma_loaded: %d\n", prtd
->dma_loaded
);
98 if ((pos
+ dma_info
.period
) > prtd
->dma_end
) {
99 dma_info
.period
= prtd
->dma_end
- pos
;
100 pr_debug("%s: corrected dma len %ld\n",
101 __func__
, dma_info
.period
);
105 prtd
->params
->ops
->prepare(prtd
->params
->ch
, &dma_info
);
108 pos
+= prtd
->dma_period
;
109 if (pos
>= prtd
->dma_end
)
110 pos
= prtd
->dma_start
;
116 static void audio_buffdone(void *data
)
118 struct snd_pcm_substream
*substream
= data
;
119 struct runtime_data
*prtd
= substream
->runtime
->private_data
;
121 pr_debug("Entered %s\n", __func__
);
123 if (prtd
->state
& ST_RUNNING
) {
124 prtd
->dma_pos
+= prtd
->dma_period
;
125 if (prtd
->dma_pos
>= prtd
->dma_end
)
126 prtd
->dma_pos
= prtd
->dma_start
;
129 snd_pcm_period_elapsed(substream
);
131 spin_lock(&prtd
->lock
);
132 if (!samsung_dma_has_circular()) {
134 dma_enqueue(substream
);
136 spin_unlock(&prtd
->lock
);
140 static int dma_hw_params(struct snd_pcm_substream
*substream
,
141 struct snd_pcm_hw_params
*params
)
143 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
144 struct runtime_data
*prtd
= runtime
->private_data
;
145 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
146 unsigned long totbytes
= params_buffer_bytes(params
);
147 struct s3c_dma_params
*dma
=
148 snd_soc_dai_get_dma_data(rtd
->cpu_dai
, substream
);
149 struct samsung_dma_info dma_info
;
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 dma_info
.cap
= (samsung_dma_has_circular() ?
170 DMA_CYCLIC
: DMA_SLAVE
);
171 dma_info
.client
= prtd
->params
->client
;
173 (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
174 ? DMA_MEM_TO_DEV
: DMA_DEV_TO_MEM
);
175 dma_info
.width
= prtd
->params
->dma_size
;
176 dma_info
.fifo
= prtd
->params
->dma_addr
;
177 prtd
->params
->ch
= prtd
->params
->ops
->request(
178 prtd
->params
->channel
, &dma_info
);
181 snd_pcm_set_runtime_buffer(substream
, &substream
->dma_buffer
);
183 runtime
->dma_bytes
= totbytes
;
185 spin_lock_irq(&prtd
->lock
);
186 prtd
->dma_loaded
= 0;
187 prtd
->dma_period
= params_period_bytes(params
);
188 prtd
->dma_start
= runtime
->dma_addr
;
189 prtd
->dma_pos
= prtd
->dma_start
;
190 prtd
->dma_end
= prtd
->dma_start
+ totbytes
;
191 spin_unlock_irq(&prtd
->lock
);
196 static int dma_hw_free(struct snd_pcm_substream
*substream
)
198 struct runtime_data
*prtd
= substream
->runtime
->private_data
;
200 pr_debug("Entered %s\n", __func__
);
202 snd_pcm_set_runtime_buffer(substream
, NULL
);
205 prtd
->params
->ops
->flush(prtd
->params
->ch
);
206 prtd
->params
->ops
->release(prtd
->params
->ch
,
207 prtd
->params
->client
);
214 static int dma_prepare(struct snd_pcm_substream
*substream
)
216 struct runtime_data
*prtd
= substream
->runtime
->private_data
;
219 pr_debug("Entered %s\n", __func__
);
221 /* return if this is a bufferless transfer e.g.
222 * codec <--> BT codec or GSM modem -- lg FIXME */
226 /* flush the DMA channel */
227 prtd
->params
->ops
->flush(prtd
->params
->ch
);
229 prtd
->dma_loaded
= 0;
230 prtd
->dma_pos
= prtd
->dma_start
;
232 /* enqueue dma buffers */
233 dma_enqueue(substream
);
238 static int dma_trigger(struct snd_pcm_substream
*substream
, int cmd
)
240 struct runtime_data
*prtd
= substream
->runtime
->private_data
;
243 pr_debug("Entered %s\n", __func__
);
245 spin_lock(&prtd
->lock
);
248 case SNDRV_PCM_TRIGGER_START
:
249 case SNDRV_PCM_TRIGGER_RESUME
:
250 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
251 prtd
->state
|= ST_RUNNING
;
252 prtd
->params
->ops
->trigger(prtd
->params
->ch
);
255 case SNDRV_PCM_TRIGGER_STOP
:
256 case SNDRV_PCM_TRIGGER_SUSPEND
:
257 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
258 prtd
->state
&= ~ST_RUNNING
;
259 prtd
->params
->ops
->stop(prtd
->params
->ch
);
267 spin_unlock(&prtd
->lock
);
272 static snd_pcm_uframes_t
273 dma_pointer(struct snd_pcm_substream
*substream
)
275 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
276 struct runtime_data
*prtd
= runtime
->private_data
;
279 pr_debug("Entered %s\n", __func__
);
281 res
= prtd
->dma_pos
- prtd
->dma_start
;
283 pr_debug("Pointer offset: %lu\n", res
);
285 /* we seem to be getting the odd error from the pcm library due
286 * to out-of-bounds pointers. this is maybe due to the dma engine
287 * not having loaded the new values for the channel before being
288 * called... (todo - fix )
291 if (res
>= snd_pcm_lib_buffer_bytes(substream
)) {
292 if (res
== snd_pcm_lib_buffer_bytes(substream
))
296 return bytes_to_frames(substream
->runtime
, res
);
299 static int dma_open(struct snd_pcm_substream
*substream
)
301 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
302 struct runtime_data
*prtd
;
304 pr_debug("Entered %s\n", __func__
);
306 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
307 snd_soc_set_runtime_hwparams(substream
, &dma_hardware
);
309 prtd
= kzalloc(sizeof(struct runtime_data
), GFP_KERNEL
);
313 spin_lock_init(&prtd
->lock
);
315 runtime
->private_data
= prtd
;
319 static int dma_close(struct snd_pcm_substream
*substream
)
321 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
322 struct runtime_data
*prtd
= runtime
->private_data
;
324 pr_debug("Entered %s\n", __func__
);
327 pr_debug("dma_close called with prtd == NULL\n");
334 static int dma_mmap(struct snd_pcm_substream
*substream
,
335 struct vm_area_struct
*vma
)
337 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
339 pr_debug("Entered %s\n", __func__
);
341 return dma_mmap_writecombine(substream
->pcm
->card
->dev
, vma
,
347 static struct snd_pcm_ops dma_ops
= {
350 .ioctl
= snd_pcm_lib_ioctl
,
351 .hw_params
= dma_hw_params
,
352 .hw_free
= dma_hw_free
,
353 .prepare
= dma_prepare
,
354 .trigger
= dma_trigger
,
355 .pointer
= dma_pointer
,
359 static int preallocate_dma_buffer(struct snd_pcm
*pcm
, int stream
)
361 struct snd_pcm_substream
*substream
= pcm
->streams
[stream
].substream
;
362 struct snd_dma_buffer
*buf
= &substream
->dma_buffer
;
363 size_t size
= dma_hardware
.buffer_bytes_max
;
365 pr_debug("Entered %s\n", __func__
);
367 buf
->dev
.type
= SNDRV_DMA_TYPE_DEV
;
368 buf
->dev
.dev
= pcm
->card
->dev
;
369 buf
->private_data
= NULL
;
370 buf
->area
= dma_alloc_writecombine(pcm
->card
->dev
, size
,
371 &buf
->addr
, GFP_KERNEL
);
378 static void dma_free_dma_buffers(struct snd_pcm
*pcm
)
380 struct snd_pcm_substream
*substream
;
381 struct snd_dma_buffer
*buf
;
384 pr_debug("Entered %s\n", __func__
);
386 for (stream
= 0; stream
< 2; stream
++) {
387 substream
= pcm
->streams
[stream
].substream
;
391 buf
= &substream
->dma_buffer
;
395 dma_free_writecombine(pcm
->card
->dev
, buf
->bytes
,
396 buf
->area
, buf
->addr
);
401 static u64 dma_mask
= DMA_BIT_MASK(32);
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 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 (pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
) {
417 ret
= preallocate_dma_buffer(pcm
,
418 SNDRV_PCM_STREAM_PLAYBACK
);
423 if (pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
) {
424 ret
= preallocate_dma_buffer(pcm
,
425 SNDRV_PCM_STREAM_CAPTURE
);
433 static struct snd_soc_platform_driver samsung_asoc_platform
= {
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
);
450 static struct platform_driver asoc_dma_driver
= {
452 .name
= "samsung-audio",
453 .owner
= THIS_MODULE
,
456 .probe
= samsung_asoc_platform_probe
,
457 .remove
= __devexit_p(samsung_asoc_platform_remove
),
460 module_platform_driver(asoc_dma_driver
);
462 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
463 MODULE_DESCRIPTION("Samsung ASoC DMA Driver");
464 MODULE_LICENSE("GPL");
465 MODULE_ALIAS("platform:samsung-audio");