Linux 2.6.25.1
[linux/fpc-iii.git] / sound / soc / s3c24xx / s3c24xx-pcm.c
blob29a6c82f873ac7c5f50def0470ef4d44e6fb5222
1 /*
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.
16 * Revision history
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>
32 #include <asm/dma.h>
33 #include <asm/io.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
41 #if S3C24XX_PCM_DEBUG
42 #define DBG(x...) printk(KERN_DEBUG x)
43 #else
44 #define DBG(x...)
45 #endif
47 static const struct snd_pcm_hardware s3c24xx_pcm_hardware = {
48 .info = SNDRV_PCM_INFO_INTERLEAVED |
49 SNDRV_PCM_INFO_BLOCK_TRANSFER |
50 SNDRV_PCM_INFO_MMAP |
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 |
56 SNDRV_PCM_FMTBIT_U8 |
57 SNDRV_PCM_FMTBIT_S8,
58 .channels_min = 2,
59 .channels_max = 2,
60 .buffer_bytes_max = 128*1024,
61 .period_bytes_min = PAGE_SIZE,
62 .period_bytes_max = PAGE_SIZE*2,
63 .periods_min = 2,
64 .periods_max = 128,
65 .fifo_size = 32,
68 struct s3c24xx_runtime_data {
69 spinlock_t lock;
70 int state;
71 unsigned int dma_loaded;
72 unsigned int dma_limit;
73 unsigned int dma_period;
74 dma_addr_t dma_start;
75 dma_addr_t dma_pos;
76 dma_addr_t dma_end;
77 struct s3c24xx_pcm_dma_params *params;
80 /* s3c24xx_pcm_enqueue
82 * place a dma buffer onto the queue for the dma system
83 * to handle.
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;
89 int ret;
91 DBG("Entered %s\n", __FUNCTION__);
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",
101 __FUNCTION__, len);
104 ret = s3c2410_dma_enqueue(prtd->params->channel,
105 substream, pos, len);
107 if (ret == 0) {
108 prtd->dma_loaded++;
109 pos += prtd->dma_period;
110 if (pos >= prtd->dma_end)
111 pos = prtd->dma_start;
112 } else
113 break;
116 prtd->dma_pos = pos;
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", __FUNCTION__);
128 if (result == S3C2410_RES_ABORT || result == S3C2410_RES_ERR)
129 return;
131 prtd = substream->runtime->private_data;
133 if (substream)
134 snd_pcm_period_elapsed(substream);
136 spin_lock(&prtd->lock);
137 if (prtd->state & ST_RUNNING) {
138 prtd->dma_loaded--;
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);
153 int ret=0;
155 DBG("Entered %s\n", __FUNCTION__);
157 /* return if this is a bufferless transfer e.g.
158 * codec <--> BT codec or GSM modem -- lg FIXME */
159 if (!dma)
160 return 0;
162 /* this may get called several times by oss emulation
163 * with different params -HW */
164 if (prtd->params == NULL) {
165 /* prepare DMA */
166 prtd->params = dma;
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);
174 if (ret) {
175 DBG(KERN_ERR "failed to get dma channel\n");
176 return ret;
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);
196 return 0;
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", __FUNCTION__);
205 /* TODO - do we need to ensure DMA flushed */
206 snd_pcm_set_runtime_buffer(substream, NULL);
208 if (prtd->params) {
209 s3c2410_dma_free(prtd->params->channel, prtd->params->client);
210 prtd->params = NULL;
213 return 0;
216 static int s3c24xx_pcm_prepare(struct snd_pcm_substream *substream)
218 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
219 int ret = 0;
221 DBG("Entered %s\n", __FUNCTION__);
223 /* return if this is a bufferless transfer e.g.
224 * codec <--> BT codec or GSM modem -- lg FIXME */
225 if (!prtd->params)
226 return 0;
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);
239 } else {
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);
258 return ret;
261 static int s3c24xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
263 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
264 int ret = 0;
266 DBG("Entered %s\n", __FUNCTION__);
268 spin_lock(&prtd->lock);
270 switch (cmd) {
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);
277 break;
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);
284 break;
286 default:
287 ret = -EINVAL;
288 break;
291 spin_unlock(&prtd->lock);
293 return ret;
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;
301 unsigned long res;
302 dma_addr_t src, dst;
304 DBG("Entered %s\n", __FUNCTION__);
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;
311 else
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))
326 res = 0;
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", __FUNCTION__);
339 snd_soc_set_runtime_hwparams(substream, &s3c24xx_pcm_hardware);
341 prtd = kzalloc(sizeof(struct s3c24xx_runtime_data), GFP_KERNEL);
342 if (prtd == NULL)
343 return -ENOMEM;
345 spin_lock_init(&prtd->lock);
347 runtime->private_data = prtd;
348 return 0;
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", __FUNCTION__);
358 if (prtd)
359 kfree(prtd);
360 else
361 DBG("s3c24xx_pcm_close called with prtd == NULL\n");
363 return 0;
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", __FUNCTION__);
373 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
374 runtime->dma_area,
375 runtime->dma_addr,
376 runtime->dma_bytes);
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", __FUNCTION__);
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);
404 if (!buf->area)
405 return -ENOMEM;
406 buf->bytes = size;
407 return 0;
410 static void s3c24xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
412 struct snd_pcm_substream *substream;
413 struct snd_dma_buffer *buf;
414 int stream;
416 DBG("Entered %s\n", __FUNCTION__);
418 for (stream = 0; stream < 2; stream++) {
419 substream = pcm->streams[stream].substream;
420 if (!substream)
421 continue;
423 buf = &substream->dma_buffer;
424 if (!buf->area)
425 continue;
427 dma_free_writecombine(pcm->card->dev, buf->bytes,
428 buf->area, buf->addr);
429 buf->area = NULL;
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)
438 int ret = 0;
440 DBG("Entered %s\n", __FUNCTION__);
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);
450 if (ret)
451 goto out;
454 if (dai->capture.channels_min) {
455 ret = s3c24xx_pcm_preallocate_dma_buffer(pcm,
456 SNDRV_PCM_STREAM_CAPTURE);
457 if (ret)
458 goto out;
460 out:
461 return ret;
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,
471 EXPORT_SYMBOL_GPL(s3c24xx_soc_platform);
473 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
474 MODULE_DESCRIPTION("Samsung S3C24XX PCM DMA module");
475 MODULE_LICENSE("GPL");