blob: bfbdc3cbd43b5e936c0614c9429d1414e4a4bb56 [file] [log] [blame]
Ben Dooksc0f41bb2007-02-14 13:20:03 +01001/*
2 * s3c24xx-pcm.c -- ALSA Soc Audio Layer
3 *
4 * (c) 2006 Wolfson Microelectronics PLC.
5 * Graeme Gregory graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
6 *
7 * (c) 2004-2005 Simtec Electronics
8 * http://armlinux.simtec.co.uk/
9 * Ben Dooks <ben@simtec.co.uk>
10 *
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.
15 *
16 * Revision history
17 * 11th Dec 2006 Merged with Simtec driver
18 * 10th Nov 2006 Initial version.
19 */
20
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>
26
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>
32
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>
38
39#include "s3c24xx-pcm.h"
40
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
47
48static 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,
65};
66
67struct 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;
77};
78
79/* s3c24xx_pcm_enqueue
80 *
81 * place a dma buffer onto the queue for the dma system
82 * to handle.
83*/
84static void s3c24xx_pcm_enqueue(struct snd_pcm_substream *substream)
85{
86 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
87 dma_addr_t pos = prtd->dma_pos;
88 int ret;
89
90 DBG("Entered %s\n", __FUNCTION__);
91
Graeme Gregory7f1bc262007-04-17 12:35:18 +020092 while (prtd->dma_loaded < prtd->dma_limit) {
Ben Dooksc0f41bb2007-02-14 13:20:03 +010093 unsigned long len = prtd->dma_period;
94
95 DBG("dma_loaded: %d\n",prtd->dma_loaded);
96
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);
101 }
102
Graeme Gregory7f1bc262007-04-17 12:35:18 +0200103 ret = s3c2410_dma_enqueue(prtd->params->channel,
104 substream, pos, len);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100105
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;
113 }
114
115 prtd->dma_pos = pos;
116}
117
118static void s3c24xx_audio_buffdone(struct s3c2410_dma_chan *channel,
Graeme Gregory7f1bc262007-04-17 12:35:18 +0200119 void *dev_id, int size,
120 enum s3c2410_dma_buffresult result)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100121{
122 struct snd_pcm_substream *substream = dev_id;
Graeme Gregory7f1bc262007-04-17 12:35:18 +0200123 struct s3c24xx_runtime_data *prtd;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100124
125 DBG("Entered %s\n", __FUNCTION__);
126
127 if (result == S3C2410_RES_ABORT || result == S3C2410_RES_ERR)
128 return;
129
Graeme Gregory7f1bc262007-04-17 12:35:18 +0200130 prtd = substream->runtime->private_data;
131
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100132 if (substream)
133 snd_pcm_period_elapsed(substream);
134
135 spin_lock(&prtd->lock);
136 if (prtd->state & ST_RUNNING) {
137 prtd->dma_loaded--;
138 s3c24xx_pcm_enqueue(substream);
139 }
140
141 spin_unlock(&prtd->lock);
142}
143
144static int s3c24xx_pcm_hw_params(struct snd_pcm_substream *substream,
145 struct snd_pcm_hw_params *params)
146{
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;
153
154 DBG("Entered %s\n", __FUNCTION__);
155
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;
160
161 /* prepare DMA */
162 prtd->params = dma;
163
164 DBG("params %p, client %p, channel %d\n", prtd->params,
165 prtd->params->client, prtd->params->channel);
166
167 ret = s3c2410_dma_request(prtd->params->channel,
168 prtd->params->client, NULL);
169
170 if (ret) {
171 DBG(KERN_ERR "failed to get dma channel\n");
172 return ret;
173 }
174
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,
Graeme Gregory7f1bc262007-04-17 12:35:18 +0200179 S3C2410_DMASRC_MEM, S3C2410_DISRCC_INC |
180 S3C2410_DISRCC_APB, prtd->params->dma_addr);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100181
182 s3c2410_dma_config(prtd->params->channel,
Graeme Gregory7f1bc262007-04-17 12:35:18 +0200183 prtd->params->dma_size,
184 S3C2410_DCON_SYNC_PCLK |
185 S3C2410_DCON_HANDSHAKE);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100186 } else {
187 s3c2410_dma_config(prtd->params->channel,
Graeme Gregory7f1bc262007-04-17 12:35:18 +0200188 prtd->params->dma_size,
189 S3C2410_DCON_HANDSHAKE |
190 S3C2410_DCON_SYNC_PCLK);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100191
192 s3c2410_dma_devconfig(prtd->params->channel,
Graeme Gregory7f1bc262007-04-17 12:35:18 +0200193 S3C2410_DMASRC_HW, 0x3,
194 prtd->params->dma_addr);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100195 }
196
197 s3c2410_dma_set_buffdone_fn(prtd->params->channel,
198 s3c24xx_audio_buffdone);
199
200 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
201
202 runtime->dma_bytes = totbytes;
203
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);
212
213 return 0;
214}
215
216static int s3c24xx_pcm_hw_free(struct snd_pcm_substream *substream)
217{
218 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
219
220 DBG("Entered %s\n", __FUNCTION__);
221
222 /* TODO - do we need to ensure DMA flushed */
223 snd_pcm_set_runtime_buffer(substream, NULL);
224
Graeme Gregory7f1bc262007-04-17 12:35:18 +0200225 if (prtd->params) {
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100226 s3c2410_dma_free(prtd->params->channel, prtd->params->client);
227 prtd->params = NULL;
228 }
229
230 return 0;
231}
232
233static int s3c24xx_pcm_prepare(struct snd_pcm_substream *substream)
234{
235 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
236 int ret = 0;
237
238 DBG("Entered %s\n", __FUNCTION__);
239
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;
244
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;
249
250 /* enqueue dma buffers */
251 s3c24xx_pcm_enqueue(substream);
252
253 return ret;
254}
255
256static int s3c24xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
257{
258 struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
259 int ret = 0;
260
261 DBG("Entered %s\n", __FUNCTION__);
262
263 spin_lock(&prtd->lock);
264
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;
273
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;
280
281 default:
282 ret = -EINVAL;
283 break;
284 }
285
286 spin_unlock(&prtd->lock);
287
288 return ret;
289}
290
Graeme Gregory7f1bc262007-04-17 12:35:18 +0200291static snd_pcm_uframes_t
292 s3c24xx_pcm_pointer(struct snd_pcm_substream *substream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100293{
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;
298
299 DBG("Entered %s\n", __FUNCTION__);
300
301 spin_lock(&prtd->lock);
302 s3c2410_dma_getposition(prtd->params->channel, &src, &dst);
303
304 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
305 res = dst - prtd->dma_start;
306 else
307 res = src - prtd->dma_start;
308
309 spin_unlock(&prtd->lock);
310
311 DBG("Pointer %x %x\n",src,dst);
312
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 )
317 */
318
319 if (res >= snd_pcm_lib_buffer_bytes(substream)) {
320 if (res == snd_pcm_lib_buffer_bytes(substream))
321 res = 0;
322 }
323
324 return bytes_to_frames(substream->runtime, res);
325}
326
327static int s3c24xx_pcm_open(struct snd_pcm_substream *substream)
328{
329 struct snd_pcm_runtime *runtime = substream->runtime;
330 struct s3c24xx_runtime_data *prtd;
331
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100332 DBG("Entered %s\n", __FUNCTION__);
333
334 snd_soc_set_runtime_hwparams(substream, &s3c24xx_pcm_hardware);
335
336 prtd = kzalloc(sizeof(struct s3c24xx_runtime_data), GFP_KERNEL);
337 if (prtd == NULL)
338 return -ENOMEM;
339
Zoltan Devaic72816b2007-05-22 16:17:05 +0200340 spin_lock_init(&prtd->lock);
341
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100342 runtime->private_data = prtd;
343 return 0;
344}
345
346static int s3c24xx_pcm_close(struct snd_pcm_substream *substream)
347{
348 struct snd_pcm_runtime *runtime = substream->runtime;
349 struct s3c24xx_runtime_data *prtd = runtime->private_data;
350
351 DBG("Entered %s\n", __FUNCTION__);
352
Graeme Gregory7f1bc262007-04-17 12:35:18 +0200353 if (prtd)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100354 kfree(prtd);
355 else
356 DBG("s3c24xx_pcm_close called with prtd == NULL\n");
357
358 return 0;
359}
360
361static int s3c24xx_pcm_mmap(struct snd_pcm_substream *substream,
362 struct vm_area_struct *vma)
363{
364 struct snd_pcm_runtime *runtime = substream->runtime;
365
366 DBG("Entered %s\n", __FUNCTION__);
367
368 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
369 runtime->dma_area,
370 runtime->dma_addr,
371 runtime->dma_bytes);
372}
373
374static 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,
384};
385
386static int s3c24xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
387{
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;
391
392 DBG("Entered %s\n", __FUNCTION__);
393
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;
403}
404
405static void s3c24xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
406{
407 struct snd_pcm_substream *substream;
408 struct snd_dma_buffer *buf;
409 int stream;
410
411 DBG("Entered %s\n", __FUNCTION__);
412
413 for (stream = 0; stream < 2; stream++) {
414 substream = pcm->streams[stream].substream;
415 if (!substream)
416 continue;
417
418 buf = &substream->dma_buffer;
419 if (!buf->area)
420 continue;
421
422 dma_free_writecombine(pcm->card->dev, buf->bytes,
423 buf->area, buf->addr);
424 buf->area = NULL;
425 }
426}
427
428static u64 s3c24xx_pcm_dmamask = DMA_32BIT_MASK;
429
Graeme Gregory7f1bc262007-04-17 12:35:18 +0200430static int s3c24xx_pcm_new(struct snd_card *card,
431 struct snd_soc_codec_dai *dai, struct snd_pcm *pcm)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100432{
433 int ret = 0;
434
435 DBG("Entered %s\n", __FUNCTION__);
436
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;
441
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;
447 }
448
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;
454 }
455 out:
456 return ret;
457}
458
459struct 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,
464};
465
466EXPORT_SYMBOL_GPL(s3c24xx_soc_platform);
467
468MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
469MODULE_DESCRIPTION("Samsung S3C24XX PCM DMA module");
470MODULE_LICENSE("GPL");