[NETFILTER]: nf_conntrack: export hash allocation/destruction functions
[linux-2.6/verdex.git] / sound / soc / s3c24xx / s3c24xx-pcm.c
blobbfbdc3cbd43b5e936c0614c9429d1414e4a4bb56
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 /* prepare DMA */
162 prtd->params = dma;
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);
170 if (ret) {
171 DBG(KERN_ERR "failed to get dma channel\n");
172 return ret;
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);
186 } else {
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);
213 return 0;
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);
225 if (prtd->params) {
226 s3c2410_dma_free(prtd->params->channel, prtd->params->client);
227 prtd->params = NULL;
230 return 0;
233 static int s3c24xx_pcm_prepare(struct snd_pcm_substream *substream)
235 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
236 int ret = 0;
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 */
242 if (!prtd->params)
243 return 0;
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);
253 return ret;
256 static int s3c24xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
258 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
259 int ret = 0;
261 DBG("Entered %s\n", __FUNCTION__);
263 spin_lock(&prtd->lock);
265 switch (cmd) {
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);
272 break;
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);
279 break;
281 default:
282 ret = -EINVAL;
283 break;
286 spin_unlock(&prtd->lock);
288 return ret;
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;
296 unsigned long res;
297 dma_addr_t src, dst;
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;
306 else
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))
321 res = 0;
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);
337 if (prtd == NULL)
338 return -ENOMEM;
340 spin_lock_init(&prtd->lock);
342 runtime->private_data = prtd;
343 return 0;
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__);
353 if (prtd)
354 kfree(prtd);
355 else
356 DBG("s3c24xx_pcm_close called with prtd == NULL\n");
358 return 0;
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,
369 runtime->dma_area,
370 runtime->dma_addr,
371 runtime->dma_bytes);
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);
399 if (!buf->area)
400 return -ENOMEM;
401 buf->bytes = size;
402 return 0;
405 static void s3c24xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
407 struct snd_pcm_substream *substream;
408 struct snd_dma_buffer *buf;
409 int stream;
411 DBG("Entered %s\n", __FUNCTION__);
413 for (stream = 0; stream < 2; stream++) {
414 substream = pcm->streams[stream].substream;
415 if (!substream)
416 continue;
418 buf = &substream->dma_buffer;
419 if (!buf->area)
420 continue;
422 dma_free_writecombine(pcm->card->dev, buf->bytes,
423 buf->area, buf->addr);
424 buf->area = NULL;
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)
433 int ret = 0;
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);
445 if (ret)
446 goto out;
449 if (dai->capture.channels_min) {
450 ret = s3c24xx_pcm_preallocate_dma_buffer(pcm,
451 SNDRV_PCM_STREAM_CAPTURE);
452 if (ret)
453 goto out;
455 out:
456 return ret;
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");