2 * s3c24xx-pcm.c -- ALSA Soc Audio Layer
4 * (c) 2006 Wolfson Microelectronics PLC.
5 * Graeme Gregory graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
7 * (c) 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 * 11th Dec 2006 Merged with Simtec driver
18 * 10th Nov 2006 Initial version.
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <linux/dma-mapping.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
34 #include <asm/hardware.h>
35 #include <asm/arch/dma.h>
36 #include <asm/arch/audio.h>
38 #include "s3c24xx-pcm.h"
40 #define S3C24XX_PCM_DEBUG 0
42 #define DBG(x...) printk(KERN_DEBUG x)
47 static const struct snd_pcm_hardware s3c24xx_pcm_hardware = {
48 .info = SNDRV_PCM_INFO_INTERLEAVED |
49 SNDRV_PCM_INFO_BLOCK_TRANSFER |
51 SNDRV_PCM_INFO_MMAP_VALID,
52 .formats = SNDRV_PCM_FMTBIT_S16_LE |
53 SNDRV_PCM_FMTBIT_U16_LE |
58 .buffer_bytes_max = 128*1024,
59 .period_bytes_min = PAGE_SIZE,
60 .period_bytes_max = PAGE_SIZE*2,
66 struct s3c24xx_runtime_data {
69 unsigned int dma_loaded;
70 unsigned int dma_limit;
71 unsigned int dma_period;
75 struct s3c24xx_pcm_dma_params *params;
78 /* s3c24xx_pcm_enqueue
80 * place a dma buffer onto the queue for the dma system
83 static void s3c24xx_pcm_enqueue(struct snd_pcm_substream *substream)
85 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
86 dma_addr_t pos = prtd->dma_pos;
89 DBG("Entered %s\n", __FUNCTION__);
91 while (prtd->dma_loaded < prtd->dma_limit) {
92 unsigned long len = prtd->dma_period;
94 DBG("dma_loaded: %d\n",prtd->dma_loaded);
96 if ((pos + len) > prtd->dma_end) {
97 len = prtd->dma_end - pos;
98 DBG(KERN_DEBUG "%s: corrected dma len %ld\n",
102 ret = s3c2410_dma_enqueue(prtd->params->channel,
103 substream, pos, len);
107 pos += prtd->dma_period;
108 if (pos >= prtd->dma_end)
109 pos = prtd->dma_start;
117 static void s3c24xx_audio_buffdone(struct s3c2410_dma_chan *channel,
118 void *dev_id, int size,
119 enum s3c2410_dma_buffresult result)
121 struct snd_pcm_substream *substream = dev_id;
122 struct s3c24xx_runtime_data *prtd;
124 DBG("Entered %s\n", __FUNCTION__);
126 if (result == S3C2410_RES_ABORT || result == S3C2410_RES_ERR)
129 prtd = substream->runtime->private_data;
132 snd_pcm_period_elapsed(substream);
134 spin_lock(&prtd->lock);
135 if (prtd->state & ST_RUNNING) {
137 s3c24xx_pcm_enqueue(substream);
140 spin_unlock(&prtd->lock);
143 static int s3c24xx_pcm_hw_params(struct snd_pcm_substream *substream,
144 struct snd_pcm_hw_params *params)
146 struct snd_pcm_runtime *runtime = substream->runtime;
147 struct s3c24xx_runtime_data *prtd = runtime->private_data;
148 struct snd_soc_pcm_runtime *rtd = substream->private_data;
149 struct s3c24xx_pcm_dma_params *dma = rtd->dai->cpu_dai->dma_data;
150 unsigned long totbytes = params_buffer_bytes(params);
153 DBG("Entered %s\n", __FUNCTION__);
155 /* return if this is a bufferless transfer e.g.
156 * codec <--> BT codec or GSM modem -- lg FIXME */
160 /* this may get called several times by oss emulation
161 * with different params -HW */
162 if (prtd->params == NULL) {
166 DBG("params %p, client %p, channel %d\n", prtd->params,
167 prtd->params->client, prtd->params->channel);
169 ret = s3c2410_dma_request(prtd->params->channel,
170 prtd->params->client, NULL);
173 DBG(KERN_ERR "failed to get dma channel\n");
178 /* channel needs configuring for mem=>device, increment memory addr,
179 * sync to pclk, half-word transfers to the IIS-FIFO. */
180 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
181 s3c2410_dma_devconfig(prtd->params->channel,
182 S3C2410_DMASRC_MEM, S3C2410_DISRCC_INC |
183 S3C2410_DISRCC_APB, prtd->params->dma_addr);
185 s3c2410_dma_config(prtd->params->channel,
186 prtd->params->dma_size,
187 S3C2410_DCON_SYNC_PCLK |
188 S3C2410_DCON_HANDSHAKE);
190 s3c2410_dma_config(prtd->params->channel,
191 prtd->params->dma_size,
192 S3C2410_DCON_HANDSHAKE |
193 S3C2410_DCON_SYNC_PCLK);
195 s3c2410_dma_devconfig(prtd->params->channel,
196 S3C2410_DMASRC_HW, 0x3,
197 prtd->params->dma_addr);
200 s3c2410_dma_set_buffdone_fn(prtd->params->channel,
201 s3c24xx_audio_buffdone);
203 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
205 runtime->dma_bytes = totbytes;
207 spin_lock_irq(&prtd->lock);
208 prtd->dma_loaded = 0;
209 prtd->dma_limit = runtime->hw.periods_min;
210 prtd->dma_period = params_period_bytes(params);
211 prtd->dma_start = runtime->dma_addr;
212 prtd->dma_pos = prtd->dma_start;
213 prtd->dma_end = prtd->dma_start + totbytes;
214 spin_unlock_irq(&prtd->lock);
219 static int s3c24xx_pcm_hw_free(struct snd_pcm_substream *substream)
221 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
223 DBG("Entered %s\n", __FUNCTION__);
225 /* TODO - do we need to ensure DMA flushed */
226 snd_pcm_set_runtime_buffer(substream, NULL);
229 s3c2410_dma_free(prtd->params->channel, prtd->params->client);
236 static int s3c24xx_pcm_prepare(struct snd_pcm_substream *substream)
238 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
241 DBG("Entered %s\n", __FUNCTION__);
243 /* return if this is a bufferless transfer e.g.
244 * codec <--> BT codec or GSM modem -- lg FIXME */
248 /* flush the DMA channel */
249 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_FLUSH);
250 prtd->dma_loaded = 0;
251 prtd->dma_pos = prtd->dma_start;
253 /* enqueue dma buffers */
254 s3c24xx_pcm_enqueue(substream);
259 static int s3c24xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
261 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
264 DBG("Entered %s\n", __FUNCTION__);
266 spin_lock(&prtd->lock);
269 case SNDRV_PCM_TRIGGER_START:
270 case SNDRV_PCM_TRIGGER_RESUME:
271 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
272 prtd->state |= ST_RUNNING;
273 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_START);
274 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STARTED);
277 case SNDRV_PCM_TRIGGER_STOP:
278 case SNDRV_PCM_TRIGGER_SUSPEND:
279 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
280 prtd->state &= ~ST_RUNNING;
281 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STOP);
289 spin_unlock(&prtd->lock);
294 static snd_pcm_uframes_t
295 s3c24xx_pcm_pointer(struct snd_pcm_substream *substream)
297 struct snd_pcm_runtime *runtime = substream->runtime;
298 struct s3c24xx_runtime_data *prtd = runtime->private_data;
302 DBG("Entered %s\n", __FUNCTION__);
304 spin_lock(&prtd->lock);
305 s3c2410_dma_getposition(prtd->params->channel, &src, &dst);
307 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
308 res = dst - prtd->dma_start;
310 res = src - prtd->dma_start;
312 spin_unlock(&prtd->lock);
314 DBG("Pointer %x %x\n",src,dst);
316 /* we seem to be getting the odd error from the pcm library due
317 * to out-of-bounds pointers. this is maybe due to the dma engine
318 * not having loaded the new values for the channel before being
319 * callled... (todo - fix )
322 if (res >= snd_pcm_lib_buffer_bytes(substream)) {
323 if (res == snd_pcm_lib_buffer_bytes(substream))
327 return bytes_to_frames(substream->runtime, res);
330 static int s3c24xx_pcm_open(struct snd_pcm_substream *substream)
332 struct snd_pcm_runtime *runtime = substream->runtime;
333 struct s3c24xx_runtime_data *prtd;
335 DBG("Entered %s\n", __FUNCTION__);
337 snd_soc_set_runtime_hwparams(substream, &s3c24xx_pcm_hardware);
339 prtd = kzalloc(sizeof(struct s3c24xx_runtime_data), GFP_KERNEL);
343 spin_lock_init(&prtd->lock);
345 runtime->private_data = prtd;
349 static int s3c24xx_pcm_close(struct snd_pcm_substream *substream)
351 struct snd_pcm_runtime *runtime = substream->runtime;
352 struct s3c24xx_runtime_data *prtd = runtime->private_data;
354 DBG("Entered %s\n", __FUNCTION__);
359 DBG("s3c24xx_pcm_close called with prtd == NULL\n");
364 static int s3c24xx_pcm_mmap(struct snd_pcm_substream *substream,
365 struct vm_area_struct *vma)
367 struct snd_pcm_runtime *runtime = substream->runtime;
369 DBG("Entered %s\n", __FUNCTION__);
371 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
377 static struct snd_pcm_ops s3c24xx_pcm_ops = {
378 .open = s3c24xx_pcm_open,
379 .close = s3c24xx_pcm_close,
380 .ioctl = snd_pcm_lib_ioctl,
381 .hw_params = s3c24xx_pcm_hw_params,
382 .hw_free = s3c24xx_pcm_hw_free,
383 .prepare = s3c24xx_pcm_prepare,
384 .trigger = s3c24xx_pcm_trigger,
385 .pointer = s3c24xx_pcm_pointer,
386 .mmap = s3c24xx_pcm_mmap,
389 static int s3c24xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
391 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
392 struct snd_dma_buffer *buf = &substream->dma_buffer;
393 size_t size = s3c24xx_pcm_hardware.buffer_bytes_max;
395 DBG("Entered %s\n", __FUNCTION__);
397 buf->dev.type = SNDRV_DMA_TYPE_DEV;
398 buf->dev.dev = pcm->card->dev;
399 buf->private_data = NULL;
400 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
401 &buf->addr, GFP_KERNEL);
408 static void s3c24xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
410 struct snd_pcm_substream *substream;
411 struct snd_dma_buffer *buf;
414 DBG("Entered %s\n", __FUNCTION__);
416 for (stream = 0; stream < 2; stream++) {
417 substream = pcm->streams[stream].substream;
421 buf = &substream->dma_buffer;
425 dma_free_writecombine(pcm->card->dev, buf->bytes,
426 buf->area, buf->addr);
431 static u64 s3c24xx_pcm_dmamask = DMA_32BIT_MASK;
433 static int s3c24xx_pcm_new(struct snd_card *card,
434 struct snd_soc_codec_dai *dai, struct snd_pcm *pcm)
438 DBG("Entered %s\n", __FUNCTION__);
440 if (!card->dev->dma_mask)
441 card->dev->dma_mask = &s3c24xx_pcm_dmamask;
442 if (!card->dev->coherent_dma_mask)
443 card->dev->coherent_dma_mask = 0xffffffff;
445 if (dai->playback.channels_min) {
446 ret = s3c24xx_pcm_preallocate_dma_buffer(pcm,
447 SNDRV_PCM_STREAM_PLAYBACK);
452 if (dai->capture.channels_min) {
453 ret = s3c24xx_pcm_preallocate_dma_buffer(pcm,
454 SNDRV_PCM_STREAM_CAPTURE);
462 struct snd_soc_platform s3c24xx_soc_platform = {
463 .name = "s3c24xx-audio",
464 .pcm_ops = &s3c24xx_pcm_ops,
465 .pcm_new = s3c24xx_pcm_new,
466 .pcm_free = s3c24xx_pcm_free_dma_buffers,
469 EXPORT_SYMBOL_GPL(s3c24xx_soc_platform);
471 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
472 MODULE_DESCRIPTION("Samsung S3C24XX PCM DMA module");
473 MODULE_LICENSE("GPL");