7458f00dfab2292a754ea7b71137be87be8fc101
[kernel.git] / sound / soc / s3c24xx / s3c24xx-pcm.c
1 /*
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  * Copyright 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
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/io.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <linux/dma-mapping.h>
23
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
28
29 #include <asm/dma.h>
30 #include <mach/hardware.h>
31 #include <mach/dma.h>
32 #include <plat/audio.h>
33
34 #include "s3c24xx-pcm.h"
35
36 #define S3C24XX_PCM_DEBUG 0
37 #if S3C24XX_PCM_DEBUG
38 #define DBG(x...) printk(KERN_DEBUG "s3c24xx-pcm: " x)
39 #else
40 #define DBG(x...)
41 #endif
42
43 static const struct snd_pcm_hardware s3c24xx_pcm_hardware = {
44         .info                   = SNDRV_PCM_INFO_INTERLEAVED |
45                                     SNDRV_PCM_INFO_BLOCK_TRANSFER |
46                                     SNDRV_PCM_INFO_MMAP |
47                                     SNDRV_PCM_INFO_MMAP_VALID |
48                                     SNDRV_PCM_INFO_PAUSE |
49                                     SNDRV_PCM_INFO_RESUME,
50         .formats                = SNDRV_PCM_FMTBIT_S16_LE |
51                                     SNDRV_PCM_FMTBIT_U16_LE |
52                                     SNDRV_PCM_FMTBIT_U8 |
53                                     SNDRV_PCM_FMTBIT_S8,
54         .channels_min           = 2,
55         .channels_max           = 2,
56         .buffer_bytes_max       = 128*1024,
57         .period_bytes_min       = PAGE_SIZE,
58         .period_bytes_max       = PAGE_SIZE*2,
59         .periods_min            = 2,
60         .periods_max            = 128,
61         .fifo_size              = 32,
62 };
63
64 struct s3c24xx_runtime_data {
65         spinlock_t lock;
66         int state;
67         unsigned int dma_loaded;
68         unsigned int dma_limit;
69         unsigned int dma_period;
70         dma_addr_t dma_start;
71         dma_addr_t dma_pos;
72         dma_addr_t dma_end;
73         struct s3c24xx_pcm_dma_params *params;
74 };
75
76 /* s3c24xx_pcm_enqueue
77  *
78  * place a dma buffer onto the queue for the dma system
79  * to handle.
80 */
81 static void s3c24xx_pcm_enqueue(struct snd_pcm_substream *substream)
82 {
83         struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
84         dma_addr_t pos = prtd->dma_pos;
85         unsigned int limit;
86         int ret;
87
88         DBG("Entered %s\n", __func__);
89
90         if (s3c_dma_has_circular()) {
91                 limit = (prtd->dma_end - prtd->dma_start) / prtd->dma_period;
92         } else
93                 limit = prtd->dma_limit;
94
95         printk(KERN_INFO "%s: loaded %d, limit %d\n", __func__, prtd->dma_loaded, limit);
96
97         while (prtd->dma_loaded < limit) {
98                 unsigned long len = prtd->dma_period;
99
100                 DBG("dma_loaded: %d\n", prtd->dma_loaded);
101
102                 if ((pos + len) > prtd->dma_end) {
103                         len  = prtd->dma_end - pos;
104                         DBG(KERN_DEBUG "%s: corrected dma len %ld\n",
105                                __func__, len);
106                 }
107
108                 ret = s3c2410_dma_enqueue(prtd->params->channel,
109                         substream, pos, len);
110
111                 if (ret == 0) {
112                         prtd->dma_loaded++;
113                         pos += prtd->dma_period;
114                         if (pos >= prtd->dma_end)
115                                 pos = prtd->dma_start;
116                 } else
117                         break;
118         }
119
120         prtd->dma_pos = pos;
121 }
122
123 static void s3c24xx_audio_buffdone(struct s3c2410_dma_chan *channel,
124                                 void *dev_id, int size,
125                                 enum s3c2410_dma_buffresult result)
126 {
127         struct snd_pcm_substream *substream = dev_id;
128         struct s3c24xx_runtime_data *prtd;
129
130         DBG("Entered %s\n", __func__);
131
132         if (result == S3C2410_RES_ABORT || result == S3C2410_RES_ERR)
133                 return;
134
135         prtd = substream->runtime->private_data;
136
137         if (substream)
138                 snd_pcm_period_elapsed(substream);
139
140         spin_lock(&prtd->lock);
141         if (prtd->state & ST_RUNNING && !s3c_dma_has_circular()) {
142                 prtd->dma_loaded--;
143                 s3c24xx_pcm_enqueue(substream);
144         }
145
146         spin_unlock(&prtd->lock);
147 }
148
149 static int s3c24xx_pcm_hw_params(struct snd_pcm_substream *substream,
150         struct snd_pcm_hw_params *params)
151 {
152         struct snd_pcm_runtime *runtime = substream->runtime;
153         struct s3c24xx_runtime_data *prtd = runtime->private_data;
154         struct snd_soc_pcm_runtime *rtd = substream->private_data;
155         struct s3c24xx_pcm_dma_params *dma = rtd->dai->cpu_dai->dma_data;
156         unsigned long totbytes = params_buffer_bytes(params);
157         int ret = 0;
158
159         DBG("Entered %s\n", __func__);
160
161         /* return if this is a bufferless transfer e.g.
162          * codec <--> BT codec or GSM modem -- lg FIXME */
163         if (!dma)
164                 return 0;
165
166         /* this may get called several times by oss emulation
167          * with different params -HW */
168         if (prtd->params == NULL) {
169                 /* prepare DMA */
170                 prtd->params = dma;
171
172                 DBG("params %p, client %p, channel %d\n", prtd->params,
173                         prtd->params->client, prtd->params->channel);
174
175                 ret = s3c2410_dma_request(prtd->params->channel,
176                                           prtd->params->client, NULL);
177
178                 if (ret < 0) {
179                         DBG(KERN_ERR "failed to get dma channel: %d\n", ret);
180                         return ret;
181                 }
182
183                 /* use the circular buffering if we have it available. */
184                 if (s3c_dma_has_circular())
185                         s3c2410_dma_setflags(prtd->params->channel,
186                                              S3C2410_DMAF_CIRCULAR);
187         }
188
189         s3c2410_dma_set_buffdone_fn(prtd->params->channel,
190                                     s3c24xx_audio_buffdone);
191
192         snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
193
194         runtime->dma_bytes = totbytes;
195
196         spin_lock_irq(&prtd->lock);
197         prtd->dma_loaded = 0;
198         prtd->dma_limit = runtime->hw.periods_min;
199         prtd->dma_period = params_period_bytes(params);
200         prtd->dma_start = runtime->dma_addr;
201         prtd->dma_pos = prtd->dma_start;
202         prtd->dma_end = prtd->dma_start + totbytes;
203         spin_unlock_irq(&prtd->lock);
204
205         return 0;
206 }
207
208 static int s3c24xx_pcm_hw_free(struct snd_pcm_substream *substream)
209 {
210         struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
211
212         DBG("Entered %s\n", __func__);
213
214         /* TODO - do we need to ensure DMA flushed */
215         snd_pcm_set_runtime_buffer(substream, NULL);
216
217         if (prtd->params) {
218                 s3c2410_dma_free(prtd->params->channel, prtd->params->client);
219                 prtd->params = NULL;
220         }
221
222         return 0;
223 }
224
225 static int s3c24xx_pcm_prepare(struct snd_pcm_substream *substream)
226 {
227         struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
228         int ret = 0;
229
230         DBG("Entered %s\n", __func__);
231
232         /* return if this is a bufferless transfer e.g.
233          * codec <--> BT codec or GSM modem -- lg FIXME */
234         if (!prtd->params)
235                 return 0;
236
237         /* channel needs configuring for mem=>device, increment memory addr,
238          * sync to pclk, half-word transfers to the IIS-FIFO. */
239         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
240                 s3c2410_dma_devconfig(prtd->params->channel,
241                                       S3C2410_DMASRC_MEM,
242                                       prtd->params->dma_addr);
243         } else {
244                 s3c2410_dma_devconfig(prtd->params->channel,
245                                       S3C2410_DMASRC_HW, 
246                                       prtd->params->dma_addr);
247         }
248         
249         s3c2410_dma_config(prtd->params->channel,
250                            prtd->params->dma_size);
251
252         /* flush the DMA channel */
253         s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_FLUSH);
254         prtd->dma_loaded = 0;
255         prtd->dma_pos = prtd->dma_start;
256
257         /* enqueue dma buffers */
258         s3c24xx_pcm_enqueue(substream);
259
260         return ret;
261 }
262
263 static int s3c24xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
264 {
265         struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
266         int ret = 0;
267
268         DBG("Entered %s\n", __func__);
269
270         spin_lock(&prtd->lock);
271
272         switch (cmd) {
273         case SNDRV_PCM_TRIGGER_START:
274         case SNDRV_PCM_TRIGGER_RESUME:
275         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
276                 prtd->state |= ST_RUNNING;
277                 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_START);
278                 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STARTED);
279                 break;
280
281         case SNDRV_PCM_TRIGGER_STOP:
282         case SNDRV_PCM_TRIGGER_SUSPEND:
283         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
284                 prtd->state &= ~ST_RUNNING;
285                 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STOP);
286                 break;
287
288         default:
289                 ret = -EINVAL;
290                 break;
291         }
292
293         spin_unlock(&prtd->lock);
294
295         return ret;
296 }
297
298 static snd_pcm_uframes_t
299 s3c24xx_pcm_pointer(struct snd_pcm_substream *substream)
300 {
301         struct snd_pcm_runtime *runtime = substream->runtime;
302         struct s3c24xx_runtime_data *prtd = runtime->private_data;
303         unsigned long res;
304         dma_addr_t src, dst;
305
306         DBG("Entered %s\n", __func__);
307
308         spin_lock(&prtd->lock);
309         s3c2410_dma_getposition(prtd->params->channel, &src, &dst);
310
311         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
312                 res = dst - prtd->dma_start;
313         else
314                 res = src - prtd->dma_start;
315
316         spin_unlock(&prtd->lock);
317
318         DBG("Pointer %x %x\n", src, dst);
319
320         /* we seem to be getting the odd error from the pcm library due
321          * to out-of-bounds pointers. this is maybe due to the dma engine
322          * not having loaded the new values for the channel before being
323          * callled... (todo - fix )
324          */
325
326         if (res >= snd_pcm_lib_buffer_bytes(substream)) {
327                 if (res == snd_pcm_lib_buffer_bytes(substream))
328                         res = 0;
329         }
330
331         return bytes_to_frames(substream->runtime, res);
332 }
333
334 static int s3c24xx_pcm_open(struct snd_pcm_substream *substream)
335 {
336         struct snd_pcm_runtime *runtime = substream->runtime;
337         struct s3c24xx_runtime_data *prtd;
338
339         DBG("Entered %s\n", __func__);
340
341         snd_soc_set_runtime_hwparams(substream, &s3c24xx_pcm_hardware);
342
343         prtd = kzalloc(sizeof(struct s3c24xx_runtime_data), GFP_KERNEL);
344         if (prtd == NULL)
345                 return -ENOMEM;
346
347         spin_lock_init(&prtd->lock);
348
349         runtime->private_data = prtd;
350         return 0;
351 }
352
353 static int s3c24xx_pcm_close(struct snd_pcm_substream *substream)
354 {
355         struct snd_pcm_runtime *runtime = substream->runtime;
356         struct s3c24xx_runtime_data *prtd = runtime->private_data;
357
358         DBG("Entered %s\n", __func__);
359
360         if (!prtd)
361                 DBG("s3c24xx_pcm_close called with prtd == NULL\n");
362
363         kfree(prtd);
364
365         return 0;
366 }
367
368 static int s3c24xx_pcm_mmap(struct snd_pcm_substream *substream,
369         struct vm_area_struct *vma)
370 {
371         struct snd_pcm_runtime *runtime = substream->runtime;
372
373         DBG("Entered %s\n", __func__);
374
375         return dma_mmap_writecombine(substream->pcm->card->dev, vma,
376                                      runtime->dma_area,
377                                      runtime->dma_addr,
378                                      runtime->dma_bytes);
379 }
380
381 static struct snd_pcm_ops s3c24xx_pcm_ops = {
382         .open           = s3c24xx_pcm_open,
383         .close          = s3c24xx_pcm_close,
384         .ioctl          = snd_pcm_lib_ioctl,
385         .hw_params      = s3c24xx_pcm_hw_params,
386         .hw_free        = s3c24xx_pcm_hw_free,
387         .prepare        = s3c24xx_pcm_prepare,
388         .trigger        = s3c24xx_pcm_trigger,
389         .pointer        = s3c24xx_pcm_pointer,
390         .mmap           = s3c24xx_pcm_mmap,
391 };
392
393 static int s3c24xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
394 {
395         struct snd_pcm_substream *substream = pcm->streams[stream].substream;
396         struct snd_dma_buffer *buf = &substream->dma_buffer;
397         size_t size = s3c24xx_pcm_hardware.buffer_bytes_max;
398
399         DBG("Entered %s\n", __func__);
400
401         buf->dev.type = SNDRV_DMA_TYPE_DEV;
402         buf->dev.dev = pcm->card->dev;
403         buf->private_data = NULL;
404         buf->area = dma_alloc_writecombine(pcm->card->dev, size,
405                                            &buf->addr, GFP_KERNEL);
406         if (!buf->area)
407                 return -ENOMEM;
408         buf->bytes = size;
409         return 0;
410 }
411
412 static void s3c24xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
413 {
414         struct snd_pcm_substream *substream;
415         struct snd_dma_buffer *buf;
416         int stream;
417
418         DBG("Entered %s\n", __func__);
419
420         for (stream = 0; stream < 2; stream++) {
421                 substream = pcm->streams[stream].substream;
422                 if (!substream)
423                         continue;
424
425                 buf = &substream->dma_buffer;
426                 if (!buf->area)
427                         continue;
428
429                 dma_free_writecombine(pcm->card->dev, buf->bytes,
430                                       buf->area, buf->addr);
431                 buf->area = NULL;
432         }
433 }
434
435 static u64 s3c24xx_pcm_dmamask = DMA_32BIT_MASK;
436
437 static int s3c24xx_pcm_new(struct snd_card *card,
438         struct snd_soc_dai *dai, struct snd_pcm *pcm)
439 {
440         int ret = 0;
441
442         DBG("Entered %s\n", __func__);
443
444         if (!card->dev->dma_mask)
445                 card->dev->dma_mask = &s3c24xx_pcm_dmamask;
446         if (!card->dev->coherent_dma_mask)
447                 card->dev->coherent_dma_mask = 0xffffffff;
448
449         if (dai->playback.channels_min) {
450                 ret = s3c24xx_pcm_preallocate_dma_buffer(pcm,
451                         SNDRV_PCM_STREAM_PLAYBACK);
452                 if (ret)
453                         goto out;
454         }
455
456         if (dai->capture.channels_min) {
457                 ret = s3c24xx_pcm_preallocate_dma_buffer(pcm,
458                         SNDRV_PCM_STREAM_CAPTURE);
459                 if (ret)
460                         goto out;
461         }
462  out:
463         return ret;
464 }
465
466 struct snd_soc_platform s3c24xx_soc_platform = {
467         .name           = "s3c24xx-audio",
468         .pcm_ops        = &s3c24xx_pcm_ops,
469         .pcm_new        = s3c24xx_pcm_new,
470         .pcm_free       = s3c24xx_pcm_free_dma_buffers,
471 };
472 EXPORT_SYMBOL_GPL(s3c24xx_soc_platform);
473
474 static int __init s3c24xx_soc_platform_init(void)
475 {
476         return snd_soc_register_platform(&s3c24xx_soc_platform);
477 }
478 module_init(s3c24xx_soc_platform_init);
479
480 static void __exit s3c24xx_soc_platform_exit(void)
481 {
482         snd_soc_unregister_platform(&s3c24xx_soc_platform);
483 }
484 module_exit(s3c24xx_soc_platform_exit);
485
486 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
487 MODULE_DESCRIPTION("Samsung S3C24XX PCM DMA module");
488 MODULE_LICENSE("GPL");