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>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/dma-mapping.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #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 "s3c24xx-pcm: " 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 SNDRV_PCM_INFO_PAUSE |
53 SNDRV_PCM_INFO_RESUME,
54 .formats = SNDRV_PCM_FMTBIT_S16_LE |
55 SNDRV_PCM_FMTBIT_U16_LE |
60 .buffer_bytes_max = 128*1024,
61 .period_bytes_min = PAGE_SIZE,
62 .period_bytes_max = PAGE_SIZE*2,
68 struct s3c24xx_runtime_data {
71 unsigned int dma_loaded;
72 unsigned int dma_limit;
73 unsigned int dma_period;
77 struct s3c24xx_pcm_dma_params *params;
80 /* s3c24xx_pcm_enqueue
82 * place a dma buffer onto the queue for the dma system
85 static void s3c24xx_pcm_enqueue(struct snd_pcm_substream *substream)
87 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
88 dma_addr_t pos = prtd->dma_pos;
91 DBG("Entered %s\n", __func__);
93 while (prtd->dma_loaded < prtd->dma_limit) {
94 unsigned long len = prtd->dma_period;
96 DBG("dma_loaded: %d\n", prtd->dma_loaded);
98 if ((pos + len) > prtd->dma_end) {
99 len = prtd->dma_end - pos;
100 DBG(KERN_DEBUG "%s: corrected dma len %ld\n",
104 ret = s3c2410_dma_enqueue(prtd->params->channel,
105 substream, pos, len);
109 pos += prtd->dma_period;
110 if (pos >= prtd->dma_end)
111 pos = prtd->dma_start;
119 static void s3c24xx_audio_buffdone(struct s3c2410_dma_chan *channel,
120 void *dev_id, int size,
121 enum s3c2410_dma_buffresult result)
123 struct snd_pcm_substream *substream = dev_id;
124 struct s3c24xx_runtime_data *prtd;
126 DBG("Entered %s\n", __func__);
128 if (result == S3C2410_RES_ABORT || result == S3C2410_RES_ERR)
131 prtd = substream->runtime->private_data;
134 snd_pcm_period_elapsed(substream);
136 spin_lock(&prtd->lock);
137 if (prtd->state & ST_RUNNING) {
139 s3c24xx_pcm_enqueue(substream);
142 spin_unlock(&prtd->lock);
145 static int s3c24xx_pcm_hw_params(struct snd_pcm_substream *substream,
146 struct snd_pcm_hw_params *params)
148 struct snd_pcm_runtime *runtime = substream->runtime;
149 struct s3c24xx_runtime_data *prtd = runtime->private_data;
150 struct snd_soc_pcm_runtime *rtd = substream->private_data;
151 struct s3c24xx_pcm_dma_params *dma = rtd->dai->cpu_dai->dma_data;
152 unsigned long totbytes = params_buffer_bytes(params);
155 DBG("Entered %s\n", __func__);
157 /* return if this is a bufferless transfer e.g.
158 * codec <--> BT codec or GSM modem -- lg FIXME */
162 /* this may get called several times by oss emulation
163 * with different params -HW */
164 if (prtd->params == NULL) {
168 DBG("params %p, client %p, channel %d\n", prtd->params,
169 prtd->params->client, prtd->params->channel);
171 ret = s3c2410_dma_request(prtd->params->channel,
172 prtd->params->client, NULL);
175 DBG(KERN_ERR "failed to get dma channel\n");
180 s3c2410_dma_set_buffdone_fn(prtd->params->channel,
181 s3c24xx_audio_buffdone);
183 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
185 runtime->dma_bytes = totbytes;
187 spin_lock_irq(&prtd->lock);
188 prtd->dma_loaded = 0;
189 prtd->dma_limit = runtime->hw.periods_min;
190 prtd->dma_period = params_period_bytes(params);
191 prtd->dma_start = runtime->dma_addr;
192 prtd->dma_pos = prtd->dma_start;
193 prtd->dma_end = prtd->dma_start + totbytes;
194 spin_unlock_irq(&prtd->lock);
199 static int s3c24xx_pcm_hw_free(struct snd_pcm_substream *substream)
201 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
203 DBG("Entered %s\n", __func__);
205 /* TODO - do we need to ensure DMA flushed */
206 snd_pcm_set_runtime_buffer(substream, NULL);
209 s3c2410_dma_free(prtd->params->channel, prtd->params->client);
216 static int s3c24xx_pcm_prepare(struct snd_pcm_substream *substream)
218 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
221 DBG("Entered %s\n", __func__);
223 /* return if this is a bufferless transfer e.g.
224 * codec <--> BT codec or GSM modem -- lg FIXME */
228 /* channel needs configuring for mem=>device, increment memory addr,
229 * sync to pclk, half-word transfers to the IIS-FIFO. */
230 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
231 s3c2410_dma_devconfig(prtd->params->channel,
232 S3C2410_DMASRC_MEM, S3C2410_DISRCC_INC |
233 S3C2410_DISRCC_APB, prtd->params->dma_addr);
235 s3c2410_dma_config(prtd->params->channel,
236 prtd->params->dma_size,
237 S3C2410_DCON_SYNC_PCLK |
238 S3C2410_DCON_HANDSHAKE);
240 s3c2410_dma_config(prtd->params->channel,
241 prtd->params->dma_size,
242 S3C2410_DCON_HANDSHAKE |
243 S3C2410_DCON_SYNC_PCLK);
245 s3c2410_dma_devconfig(prtd->params->channel,
246 S3C2410_DMASRC_HW, 0x3,
247 prtd->params->dma_addr);
250 /* flush the DMA channel */
251 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_FLUSH);
252 prtd->dma_loaded = 0;
253 prtd->dma_pos = prtd->dma_start;
255 /* enqueue dma buffers */
256 s3c24xx_pcm_enqueue(substream);
261 static int s3c24xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
263 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
266 DBG("Entered %s\n", __func__);
268 spin_lock(&prtd->lock);
271 case SNDRV_PCM_TRIGGER_START:
272 case SNDRV_PCM_TRIGGER_RESUME:
273 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
274 prtd->state |= ST_RUNNING;
275 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_START);
276 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STARTED);
279 case SNDRV_PCM_TRIGGER_STOP:
280 case SNDRV_PCM_TRIGGER_SUSPEND:
281 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
282 prtd->state &= ~ST_RUNNING;
283 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STOP);
291 spin_unlock(&prtd->lock);
296 static snd_pcm_uframes_t
297 s3c24xx_pcm_pointer(struct snd_pcm_substream *substream)
299 struct snd_pcm_runtime *runtime = substream->runtime;
300 struct s3c24xx_runtime_data *prtd = runtime->private_data;
304 DBG("Entered %s\n", __func__);
306 spin_lock(&prtd->lock);
307 s3c2410_dma_getposition(prtd->params->channel, &src, &dst);
309 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
310 res = dst - prtd->dma_start;
312 res = src - prtd->dma_start;
314 spin_unlock(&prtd->lock);
316 DBG("Pointer %x %x\n", src, dst);
318 /* we seem to be getting the odd error from the pcm library due
319 * to out-of-bounds pointers. this is maybe due to the dma engine
320 * not having loaded the new values for the channel before being
321 * callled... (todo - fix )
324 if (res >= snd_pcm_lib_buffer_bytes(substream)) {
325 if (res == snd_pcm_lib_buffer_bytes(substream))
329 return bytes_to_frames(substream->runtime, res);
332 static int s3c24xx_pcm_open(struct snd_pcm_substream *substream)
334 struct snd_pcm_runtime *runtime = substream->runtime;
335 struct s3c24xx_runtime_data *prtd;
337 DBG("Entered %s\n", __func__);
339 snd_soc_set_runtime_hwparams(substream, &s3c24xx_pcm_hardware);
341 prtd = kzalloc(sizeof(struct s3c24xx_runtime_data), GFP_KERNEL);
345 spin_lock_init(&prtd->lock);
347 runtime->private_data = prtd;
351 static int s3c24xx_pcm_close(struct snd_pcm_substream *substream)
353 struct snd_pcm_runtime *runtime = substream->runtime;
354 struct s3c24xx_runtime_data *prtd = runtime->private_data;
356 DBG("Entered %s\n", __func__);
359 DBG("s3c24xx_pcm_close called with prtd == NULL\n");
366 static int s3c24xx_pcm_mmap(struct snd_pcm_substream *substream,
367 struct vm_area_struct *vma)
369 struct snd_pcm_runtime *runtime = substream->runtime;
371 DBG("Entered %s\n", __func__);
373 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
379 static struct snd_pcm_ops s3c24xx_pcm_ops = {
380 .open = s3c24xx_pcm_open,
381 .close = s3c24xx_pcm_close,
382 .ioctl = snd_pcm_lib_ioctl,
383 .hw_params = s3c24xx_pcm_hw_params,
384 .hw_free = s3c24xx_pcm_hw_free,
385 .prepare = s3c24xx_pcm_prepare,
386 .trigger = s3c24xx_pcm_trigger,
387 .pointer = s3c24xx_pcm_pointer,
388 .mmap = s3c24xx_pcm_mmap,
391 static int s3c24xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
393 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
394 struct snd_dma_buffer *buf = &substream->dma_buffer;
395 size_t size = s3c24xx_pcm_hardware.buffer_bytes_max;
397 DBG("Entered %s\n", __func__);
399 buf->dev.type = SNDRV_DMA_TYPE_DEV;
400 buf->dev.dev = pcm->card->dev;
401 buf->private_data = NULL;
402 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
403 &buf->addr, GFP_KERNEL);
410 static void s3c24xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
412 struct snd_pcm_substream *substream;
413 struct snd_dma_buffer *buf;
416 DBG("Entered %s\n", __func__);
418 for (stream = 0; stream < 2; stream++) {
419 substream = pcm->streams[stream].substream;
423 buf = &substream->dma_buffer;
427 dma_free_writecombine(pcm->card->dev, buf->bytes,
428 buf->area, buf->addr);
433 static u64 s3c24xx_pcm_dmamask = DMA_32BIT_MASK;
435 static int s3c24xx_pcm_new(struct snd_card *card,
436 struct snd_soc_codec_dai *dai, struct snd_pcm *pcm)
440 DBG("Entered %s\n", __func__);
442 if (!card->dev->dma_mask)
443 card->dev->dma_mask = &s3c24xx_pcm_dmamask;
444 if (!card->dev->coherent_dma_mask)
445 card->dev->coherent_dma_mask = 0xffffffff;
447 if (dai->playback.channels_min) {
448 ret = s3c24xx_pcm_preallocate_dma_buffer(pcm,
449 SNDRV_PCM_STREAM_PLAYBACK);
454 if (dai->capture.channels_min) {
455 ret = s3c24xx_pcm_preallocate_dma_buffer(pcm,
456 SNDRV_PCM_STREAM_CAPTURE);
464 struct snd_soc_platform s3c24xx_soc_platform = {
465 .name = "s3c24xx-audio",
466 .pcm_ops = &s3c24xx_pcm_ops,
467 .pcm_new = s3c24xx_pcm_new,
468 .pcm_free = s3c24xx_pcm_free_dma_buffers,
470 EXPORT_SYMBOL_GPL(s3c24xx_soc_platform);
472 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
473 MODULE_DESCRIPTION("Samsung S3C24XX PCM DMA module");
474 MODULE_LICENSE("GPL");