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/driver.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
35 #include <asm/hardware.h>
36 #include <asm/arch/dma.h>
37 #include <asm/arch/audio.h>
39 #include "s3c24xx-pcm.h"
41 #define S3C24XX_PCM_DEBUG 0
43 #define DBG(x...) printk(KERN_DEBUG x)
48 static const struct snd_pcm_hardware s3c24xx_pcm_hardware = {
49 .info = SNDRV_PCM_INFO_INTERLEAVED |
50 SNDRV_PCM_INFO_BLOCK_TRANSFER |
52 SNDRV_PCM_INFO_MMAP_VALID,
53 .formats = SNDRV_PCM_FMTBIT_S16_LE |
54 SNDRV_PCM_FMTBIT_U16_LE |
59 .buffer_bytes_max = 128*1024,
60 .period_bytes_min = PAGE_SIZE,
61 .period_bytes_max = PAGE_SIZE*2,
67 struct s3c24xx_runtime_data {
70 unsigned int dma_loaded;
71 unsigned int dma_limit;
72 unsigned int dma_period;
76 struct s3c24xx_pcm_dma_params *params;
79 /* s3c24xx_pcm_enqueue
81 * place a dma buffer onto the queue for the dma system
84 static void s3c24xx_pcm_enqueue(struct snd_pcm_substream *substream)
86 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
87 dma_addr_t pos = prtd->dma_pos;
90 DBG("Entered %s\n", __FUNCTION__);
92 while (prtd->dma_loaded < prtd->dma_limit) {
93 unsigned long len = prtd->dma_period;
95 DBG("dma_loaded: %d\n",prtd->dma_loaded);
97 if ((pos + len) > prtd->dma_end) {
98 len = prtd->dma_end - pos;
99 DBG(KERN_DEBUG "%s: corrected dma len %ld\n",
103 ret = s3c2410_dma_enqueue(prtd->params->channel,
104 substream, pos, len);
108 pos += prtd->dma_period;
109 if (pos >= prtd->dma_end)
110 pos = prtd->dma_start;
118 static void s3c24xx_audio_buffdone(struct s3c2410_dma_chan *channel,
119 void *dev_id, int size,
120 enum s3c2410_dma_buffresult result)
122 struct snd_pcm_substream *substream = dev_id;
123 struct s3c24xx_runtime_data *prtd;
125 DBG("Entered %s\n", __FUNCTION__);
127 if (result == S3C2410_RES_ABORT || result == S3C2410_RES_ERR)
130 prtd = substream->runtime->private_data;
133 snd_pcm_period_elapsed(substream);
135 spin_lock(&prtd->lock);
136 if (prtd->state & ST_RUNNING) {
138 s3c24xx_pcm_enqueue(substream);
141 spin_unlock(&prtd->lock);
144 static int s3c24xx_pcm_hw_params(struct snd_pcm_substream *substream,
145 struct snd_pcm_hw_params *params)
147 struct snd_pcm_runtime *runtime = substream->runtime;
148 struct s3c24xx_runtime_data *prtd = runtime->private_data;
149 struct snd_soc_pcm_runtime *rtd = substream->private_data;
150 struct s3c24xx_pcm_dma_params *dma = rtd->dai->cpu_dai->dma_data;
151 unsigned long totbytes = params_buffer_bytes(params);
154 DBG("Entered %s\n", __FUNCTION__);
156 /* return if this is a bufferless transfer e.g.
157 * codec <--> BT codec or GSM modem -- lg FIXME */
164 DBG("params %p, client %p, channel %d\n", prtd->params,
165 prtd->params->client, prtd->params->channel);
167 ret = s3c2410_dma_request(prtd->params->channel,
168 prtd->params->client, NULL);
171 DBG(KERN_ERR "failed to get dma channel\n");
175 /* channel needs configuring for mem=>device, increment memory addr,
176 * sync to pclk, half-word transfers to the IIS-FIFO. */
177 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
178 s3c2410_dma_devconfig(prtd->params->channel,
179 S3C2410_DMASRC_MEM, S3C2410_DISRCC_INC |
180 S3C2410_DISRCC_APB, prtd->params->dma_addr);
182 s3c2410_dma_config(prtd->params->channel,
183 prtd->params->dma_size,
184 S3C2410_DCON_SYNC_PCLK |
185 S3C2410_DCON_HANDSHAKE);
187 s3c2410_dma_config(prtd->params->channel,
188 prtd->params->dma_size,
189 S3C2410_DCON_HANDSHAKE |
190 S3C2410_DCON_SYNC_PCLK);
192 s3c2410_dma_devconfig(prtd->params->channel,
193 S3C2410_DMASRC_HW, 0x3,
194 prtd->params->dma_addr);
197 s3c2410_dma_set_buffdone_fn(prtd->params->channel,
198 s3c24xx_audio_buffdone);
200 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
202 runtime->dma_bytes = totbytes;
204 spin_lock_irq(&prtd->lock);
205 prtd->dma_loaded = 0;
206 prtd->dma_limit = runtime->hw.periods_min;
207 prtd->dma_period = params_period_bytes(params);
208 prtd->dma_start = runtime->dma_addr;
209 prtd->dma_pos = prtd->dma_start;
210 prtd->dma_end = prtd->dma_start + totbytes;
211 spin_unlock_irq(&prtd->lock);
216 static int s3c24xx_pcm_hw_free(struct snd_pcm_substream *substream)
218 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
220 DBG("Entered %s\n", __FUNCTION__);
222 /* TODO - do we need to ensure DMA flushed */
223 snd_pcm_set_runtime_buffer(substream, NULL);
226 s3c2410_dma_free(prtd->params->channel, prtd->params->client);
233 static int s3c24xx_pcm_prepare(struct snd_pcm_substream *substream)
235 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
238 DBG("Entered %s\n", __FUNCTION__);
240 /* return if this is a bufferless transfer e.g.
241 * codec <--> BT codec or GSM modem -- lg FIXME */
245 /* flush the DMA channel */
246 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_FLUSH);
247 prtd->dma_loaded = 0;
248 prtd->dma_pos = prtd->dma_start;
250 /* enqueue dma buffers */
251 s3c24xx_pcm_enqueue(substream);
256 static int s3c24xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
258 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
261 DBG("Entered %s\n", __FUNCTION__);
263 spin_lock(&prtd->lock);
266 case SNDRV_PCM_TRIGGER_START:
267 case SNDRV_PCM_TRIGGER_RESUME:
268 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
269 prtd->state |= ST_RUNNING;
270 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_START);
271 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STARTED);
274 case SNDRV_PCM_TRIGGER_STOP:
275 case SNDRV_PCM_TRIGGER_SUSPEND:
276 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
277 prtd->state &= ~ST_RUNNING;
278 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STOP);
286 spin_unlock(&prtd->lock);
291 static snd_pcm_uframes_t
292 s3c24xx_pcm_pointer(struct snd_pcm_substream *substream)
294 struct snd_pcm_runtime *runtime = substream->runtime;
295 struct s3c24xx_runtime_data *prtd = runtime->private_data;
299 DBG("Entered %s\n", __FUNCTION__);
301 spin_lock(&prtd->lock);
302 s3c2410_dma_getposition(prtd->params->channel, &src, &dst);
304 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
305 res = dst - prtd->dma_start;
307 res = src - prtd->dma_start;
309 spin_unlock(&prtd->lock);
311 DBG("Pointer %x %x\n",src,dst);
313 /* we seem to be getting the odd error from the pcm library due
314 * to out-of-bounds pointers. this is maybe due to the dma engine
315 * not having loaded the new values for the channel before being
316 * callled... (todo - fix )
319 if (res >= snd_pcm_lib_buffer_bytes(substream)) {
320 if (res == snd_pcm_lib_buffer_bytes(substream))
324 return bytes_to_frames(substream->runtime, res);
327 static int s3c24xx_pcm_open(struct snd_pcm_substream *substream)
329 struct snd_pcm_runtime *runtime = substream->runtime;
330 struct s3c24xx_runtime_data *prtd;
332 DBG("Entered %s\n", __FUNCTION__);
334 snd_soc_set_runtime_hwparams(substream, &s3c24xx_pcm_hardware);
336 prtd = kzalloc(sizeof(struct s3c24xx_runtime_data), GFP_KERNEL);
340 spin_lock_init(&prtd->lock);
342 runtime->private_data = prtd;
346 static int s3c24xx_pcm_close(struct snd_pcm_substream *substream)
348 struct snd_pcm_runtime *runtime = substream->runtime;
349 struct s3c24xx_runtime_data *prtd = runtime->private_data;
351 DBG("Entered %s\n", __FUNCTION__);
356 DBG("s3c24xx_pcm_close called with prtd == NULL\n");
361 static int s3c24xx_pcm_mmap(struct snd_pcm_substream *substream,
362 struct vm_area_struct *vma)
364 struct snd_pcm_runtime *runtime = substream->runtime;
366 DBG("Entered %s\n", __FUNCTION__);
368 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
374 static struct snd_pcm_ops s3c24xx_pcm_ops = {
375 .open = s3c24xx_pcm_open,
376 .close = s3c24xx_pcm_close,
377 .ioctl = snd_pcm_lib_ioctl,
378 .hw_params = s3c24xx_pcm_hw_params,
379 .hw_free = s3c24xx_pcm_hw_free,
380 .prepare = s3c24xx_pcm_prepare,
381 .trigger = s3c24xx_pcm_trigger,
382 .pointer = s3c24xx_pcm_pointer,
383 .mmap = s3c24xx_pcm_mmap,
386 static int s3c24xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
388 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
389 struct snd_dma_buffer *buf = &substream->dma_buffer;
390 size_t size = s3c24xx_pcm_hardware.buffer_bytes_max;
392 DBG("Entered %s\n", __FUNCTION__);
394 buf->dev.type = SNDRV_DMA_TYPE_DEV;
395 buf->dev.dev = pcm->card->dev;
396 buf->private_data = NULL;
397 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
398 &buf->addr, GFP_KERNEL);
405 static void s3c24xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
407 struct snd_pcm_substream *substream;
408 struct snd_dma_buffer *buf;
411 DBG("Entered %s\n", __FUNCTION__);
413 for (stream = 0; stream < 2; stream++) {
414 substream = pcm->streams[stream].substream;
418 buf = &substream->dma_buffer;
422 dma_free_writecombine(pcm->card->dev, buf->bytes,
423 buf->area, buf->addr);
428 static u64 s3c24xx_pcm_dmamask = DMA_32BIT_MASK;
430 static int s3c24xx_pcm_new(struct snd_card *card,
431 struct snd_soc_codec_dai *dai, struct snd_pcm *pcm)
435 DBG("Entered %s\n", __FUNCTION__);
437 if (!card->dev->dma_mask)
438 card->dev->dma_mask = &s3c24xx_pcm_dmamask;
439 if (!card->dev->coherent_dma_mask)
440 card->dev->coherent_dma_mask = 0xffffffff;
442 if (dai->playback.channels_min) {
443 ret = s3c24xx_pcm_preallocate_dma_buffer(pcm,
444 SNDRV_PCM_STREAM_PLAYBACK);
449 if (dai->capture.channels_min) {
450 ret = s3c24xx_pcm_preallocate_dma_buffer(pcm,
451 SNDRV_PCM_STREAM_CAPTURE);
459 struct snd_soc_platform s3c24xx_soc_platform = {
460 .name = "s3c24xx-audio",
461 .pcm_ops = &s3c24xx_pcm_ops,
462 .pcm_new = s3c24xx_pcm_new,
463 .pcm_free = s3c24xx_pcm_free_dma_buffers,
466 EXPORT_SYMBOL_GPL(s3c24xx_soc_platform);
468 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
469 MODULE_DESCRIPTION("Samsung S3C24XX PCM DMA module");
470 MODULE_LICENSE("GPL");