[POWERPC] 4xx: Fix timebase clock selection on Walnut
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / soc / s3c24xx / s3c24xx-pcm.c
blob4107a87d4de3cb39fdcf7f8d51432e5a37361c93
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/driver.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
33 #include <asm/dma.h>
34 #include <asm/io.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
42 #if S3C24XX_PCM_DEBUG
43 #define DBG(x...) printk(KERN_DEBUG x)
44 #else
45 #define DBG(x...)
46 #endif
48 static const struct snd_pcm_hardware s3c24xx_pcm_hardware = {
49 .info = SNDRV_PCM_INFO_INTERLEAVED |
50 SNDRV_PCM_INFO_BLOCK_TRANSFER |
51 SNDRV_PCM_INFO_MMAP |
52 SNDRV_PCM_INFO_MMAP_VALID,
53 .formats = SNDRV_PCM_FMTBIT_S16_LE |
54 SNDRV_PCM_FMTBIT_U16_LE |
55 SNDRV_PCM_FMTBIT_U8 |
56 SNDRV_PCM_FMTBIT_S8,
57 .channels_min = 2,
58 .channels_max = 2,
59 .buffer_bytes_max = 128*1024,
60 .period_bytes_min = PAGE_SIZE,
61 .period_bytes_max = PAGE_SIZE*2,
62 .periods_min = 2,
63 .periods_max = 128,
64 .fifo_size = 32,
67 struct s3c24xx_runtime_data {
68 spinlock_t lock;
69 int state;
70 unsigned int dma_loaded;
71 unsigned int dma_limit;
72 unsigned int dma_period;
73 dma_addr_t dma_start;
74 dma_addr_t dma_pos;
75 dma_addr_t dma_end;
76 struct s3c24xx_pcm_dma_params *params;
79 /* s3c24xx_pcm_enqueue
81 * place a dma buffer onto the queue for the dma system
82 * to handle.
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;
88 int ret;
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",
100 __FUNCTION__, len);
103 ret = s3c2410_dma_enqueue(prtd->params->channel,
104 substream, pos, len);
106 if (ret == 0) {
107 prtd->dma_loaded++;
108 pos += prtd->dma_period;
109 if (pos >= prtd->dma_end)
110 pos = prtd->dma_start;
111 } else
112 break;
115 prtd->dma_pos = pos;
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)
128 return;
130 prtd = substream->runtime->private_data;
132 if (substream)
133 snd_pcm_period_elapsed(substream);
135 spin_lock(&prtd->lock);
136 if (prtd->state & ST_RUNNING) {
137 prtd->dma_loaded--;
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);
152 int ret=0;
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 */
158 if (!dma)
159 return 0;
161 /* this may get called several times by oss emulation
162 * with different params -HW */
163 if (prtd->params == NULL) {
164 /* prepare DMA */
165 prtd->params = dma;
167 DBG("params %p, client %p, channel %d\n", prtd->params,
168 prtd->params->client, prtd->params->channel);
170 ret = s3c2410_dma_request(prtd->params->channel,
171 prtd->params->client, NULL);
173 if (ret) {
174 DBG(KERN_ERR "failed to get dma channel\n");
175 return ret;
179 /* channel needs configuring for mem=>device, increment memory addr,
180 * sync to pclk, half-word transfers to the IIS-FIFO. */
181 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
182 s3c2410_dma_devconfig(prtd->params->channel,
183 S3C2410_DMASRC_MEM, S3C2410_DISRCC_INC |
184 S3C2410_DISRCC_APB, prtd->params->dma_addr);
186 s3c2410_dma_config(prtd->params->channel,
187 prtd->params->dma_size,
188 S3C2410_DCON_SYNC_PCLK |
189 S3C2410_DCON_HANDSHAKE);
190 } else {
191 s3c2410_dma_config(prtd->params->channel,
192 prtd->params->dma_size,
193 S3C2410_DCON_HANDSHAKE |
194 S3C2410_DCON_SYNC_PCLK);
196 s3c2410_dma_devconfig(prtd->params->channel,
197 S3C2410_DMASRC_HW, 0x3,
198 prtd->params->dma_addr);
201 s3c2410_dma_set_buffdone_fn(prtd->params->channel,
202 s3c24xx_audio_buffdone);
204 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
206 runtime->dma_bytes = totbytes;
208 spin_lock_irq(&prtd->lock);
209 prtd->dma_loaded = 0;
210 prtd->dma_limit = runtime->hw.periods_min;
211 prtd->dma_period = params_period_bytes(params);
212 prtd->dma_start = runtime->dma_addr;
213 prtd->dma_pos = prtd->dma_start;
214 prtd->dma_end = prtd->dma_start + totbytes;
215 spin_unlock_irq(&prtd->lock);
217 return 0;
220 static int s3c24xx_pcm_hw_free(struct snd_pcm_substream *substream)
222 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
224 DBG("Entered %s\n", __FUNCTION__);
226 /* TODO - do we need to ensure DMA flushed */
227 snd_pcm_set_runtime_buffer(substream, NULL);
229 if (prtd->params) {
230 s3c2410_dma_free(prtd->params->channel, prtd->params->client);
231 prtd->params = NULL;
234 return 0;
237 static int s3c24xx_pcm_prepare(struct snd_pcm_substream *substream)
239 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
240 int ret = 0;
242 DBG("Entered %s\n", __FUNCTION__);
244 /* return if this is a bufferless transfer e.g.
245 * codec <--> BT codec or GSM modem -- lg FIXME */
246 if (!prtd->params)
247 return 0;
249 /* flush the DMA channel */
250 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_FLUSH);
251 prtd->dma_loaded = 0;
252 prtd->dma_pos = prtd->dma_start;
254 /* enqueue dma buffers */
255 s3c24xx_pcm_enqueue(substream);
257 return ret;
260 static int s3c24xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
262 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
263 int ret = 0;
265 DBG("Entered %s\n", __FUNCTION__);
267 spin_lock(&prtd->lock);
269 switch (cmd) {
270 case SNDRV_PCM_TRIGGER_START:
271 case SNDRV_PCM_TRIGGER_RESUME:
272 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
273 prtd->state |= ST_RUNNING;
274 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_START);
275 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STARTED);
276 break;
278 case SNDRV_PCM_TRIGGER_STOP:
279 case SNDRV_PCM_TRIGGER_SUSPEND:
280 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
281 prtd->state &= ~ST_RUNNING;
282 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STOP);
283 break;
285 default:
286 ret = -EINVAL;
287 break;
290 spin_unlock(&prtd->lock);
292 return ret;
295 static snd_pcm_uframes_t
296 s3c24xx_pcm_pointer(struct snd_pcm_substream *substream)
298 struct snd_pcm_runtime *runtime = substream->runtime;
299 struct s3c24xx_runtime_data *prtd = runtime->private_data;
300 unsigned long res;
301 dma_addr_t src, dst;
303 DBG("Entered %s\n", __FUNCTION__);
305 spin_lock(&prtd->lock);
306 s3c2410_dma_getposition(prtd->params->channel, &src, &dst);
308 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
309 res = dst - prtd->dma_start;
310 else
311 res = src - prtd->dma_start;
313 spin_unlock(&prtd->lock);
315 DBG("Pointer %x %x\n",src,dst);
317 /* we seem to be getting the odd error from the pcm library due
318 * to out-of-bounds pointers. this is maybe due to the dma engine
319 * not having loaded the new values for the channel before being
320 * callled... (todo - fix )
323 if (res >= snd_pcm_lib_buffer_bytes(substream)) {
324 if (res == snd_pcm_lib_buffer_bytes(substream))
325 res = 0;
328 return bytes_to_frames(substream->runtime, res);
331 static int s3c24xx_pcm_open(struct snd_pcm_substream *substream)
333 struct snd_pcm_runtime *runtime = substream->runtime;
334 struct s3c24xx_runtime_data *prtd;
336 DBG("Entered %s\n", __FUNCTION__);
338 snd_soc_set_runtime_hwparams(substream, &s3c24xx_pcm_hardware);
340 prtd = kzalloc(sizeof(struct s3c24xx_runtime_data), GFP_KERNEL);
341 if (prtd == NULL)
342 return -ENOMEM;
344 spin_lock_init(&prtd->lock);
346 runtime->private_data = prtd;
347 return 0;
350 static int s3c24xx_pcm_close(struct snd_pcm_substream *substream)
352 struct snd_pcm_runtime *runtime = substream->runtime;
353 struct s3c24xx_runtime_data *prtd = runtime->private_data;
355 DBG("Entered %s\n", __FUNCTION__);
357 if (prtd)
358 kfree(prtd);
359 else
360 DBG("s3c24xx_pcm_close called with prtd == NULL\n");
362 return 0;
365 static int s3c24xx_pcm_mmap(struct snd_pcm_substream *substream,
366 struct vm_area_struct *vma)
368 struct snd_pcm_runtime *runtime = substream->runtime;
370 DBG("Entered %s\n", __FUNCTION__);
372 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
373 runtime->dma_area,
374 runtime->dma_addr,
375 runtime->dma_bytes);
378 static struct snd_pcm_ops s3c24xx_pcm_ops = {
379 .open = s3c24xx_pcm_open,
380 .close = s3c24xx_pcm_close,
381 .ioctl = snd_pcm_lib_ioctl,
382 .hw_params = s3c24xx_pcm_hw_params,
383 .hw_free = s3c24xx_pcm_hw_free,
384 .prepare = s3c24xx_pcm_prepare,
385 .trigger = s3c24xx_pcm_trigger,
386 .pointer = s3c24xx_pcm_pointer,
387 .mmap = s3c24xx_pcm_mmap,
390 static int s3c24xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
392 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
393 struct snd_dma_buffer *buf = &substream->dma_buffer;
394 size_t size = s3c24xx_pcm_hardware.buffer_bytes_max;
396 DBG("Entered %s\n", __FUNCTION__);
398 buf->dev.type = SNDRV_DMA_TYPE_DEV;
399 buf->dev.dev = pcm->card->dev;
400 buf->private_data = NULL;
401 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
402 &buf->addr, GFP_KERNEL);
403 if (!buf->area)
404 return -ENOMEM;
405 buf->bytes = size;
406 return 0;
409 static void s3c24xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
411 struct snd_pcm_substream *substream;
412 struct snd_dma_buffer *buf;
413 int stream;
415 DBG("Entered %s\n", __FUNCTION__);
417 for (stream = 0; stream < 2; stream++) {
418 substream = pcm->streams[stream].substream;
419 if (!substream)
420 continue;
422 buf = &substream->dma_buffer;
423 if (!buf->area)
424 continue;
426 dma_free_writecombine(pcm->card->dev, buf->bytes,
427 buf->area, buf->addr);
428 buf->area = NULL;
432 static u64 s3c24xx_pcm_dmamask = DMA_32BIT_MASK;
434 static int s3c24xx_pcm_new(struct snd_card *card,
435 struct snd_soc_codec_dai *dai, struct snd_pcm *pcm)
437 int ret = 0;
439 DBG("Entered %s\n", __FUNCTION__);
441 if (!card->dev->dma_mask)
442 card->dev->dma_mask = &s3c24xx_pcm_dmamask;
443 if (!card->dev->coherent_dma_mask)
444 card->dev->coherent_dma_mask = 0xffffffff;
446 if (dai->playback.channels_min) {
447 ret = s3c24xx_pcm_preallocate_dma_buffer(pcm,
448 SNDRV_PCM_STREAM_PLAYBACK);
449 if (ret)
450 goto out;
453 if (dai->capture.channels_min) {
454 ret = s3c24xx_pcm_preallocate_dma_buffer(pcm,
455 SNDRV_PCM_STREAM_CAPTURE);
456 if (ret)
457 goto out;
459 out:
460 return ret;
463 struct snd_soc_platform s3c24xx_soc_platform = {
464 .name = "s3c24xx-audio",
465 .pcm_ops = &s3c24xx_pcm_ops,
466 .pcm_new = s3c24xx_pcm_new,
467 .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");