]> err.no Git - linux-2.6/blob - sound/soc/s3c24xx/s3c24xx-pcm.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6] / 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  * (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/core.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
31
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>
37
38 #include "s3c24xx-pcm.h"
39
40 #define S3C24XX_PCM_DEBUG 0
41 #if S3C24XX_PCM_DEBUG
42 #define DBG(x...) printk(KERN_DEBUG "s3c24xx-pcm: " x)
43 #else
44 #define DBG(x...)
45 #endif
46
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,
66 };
67
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;
78 };
79
80 /* s3c24xx_pcm_enqueue
81  *
82  * place a dma buffer onto the queue for the dma system
83  * to handle.
84 */
85 static void s3c24xx_pcm_enqueue(struct snd_pcm_substream *substream)
86 {
87         struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
88         dma_addr_t pos = prtd->dma_pos;
89         int ret;
90
91         DBG("Entered %s\n", __func__);
92
93         while (prtd->dma_loaded < prtd->dma_limit) {
94                 unsigned long len = prtd->dma_period;
95
96                 DBG("dma_loaded: %d\n",prtd->dma_loaded);
97
98                 if ((pos + len) > prtd->dma_end) {
99                         len  = prtd->dma_end - pos;
100                         DBG(KERN_DEBUG "%s: corrected dma len %ld\n",
101                                __func__, len);
102                 }
103
104                 ret = s3c2410_dma_enqueue(prtd->params->channel, 
105                         substream, pos, len);
106
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;
114         }
115
116         prtd->dma_pos = pos;
117 }
118
119 static void s3c24xx_audio_buffdone(struct s3c2410_dma_chan *channel,
120                                 void *dev_id, int size,
121                                 enum s3c2410_dma_buffresult result)
122 {
123         struct snd_pcm_substream *substream = dev_id;
124         struct s3c24xx_runtime_data *prtd;
125
126         DBG("Entered %s\n", __func__);
127
128         if (result == S3C2410_RES_ABORT || result == S3C2410_RES_ERR)
129                 return;
130
131         prtd = substream->runtime->private_data;
132         
133         if (substream)
134                 snd_pcm_period_elapsed(substream);
135
136         spin_lock(&prtd->lock);
137         if (prtd->state & ST_RUNNING) {
138                 prtd->dma_loaded--;
139                 s3c24xx_pcm_enqueue(substream);
140         }
141
142         spin_unlock(&prtd->lock);
143 }
144
145 static int s3c24xx_pcm_hw_params(struct snd_pcm_substream *substream,
146         struct snd_pcm_hw_params *params)
147 {
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;
154
155         DBG("Entered %s\n", __func__);
156
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;
161
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;
167
168                 DBG("params %p, client %p, channel %d\n", prtd->params,
169                         prtd->params->client, prtd->params->channel);
170
171                 ret = s3c2410_dma_request(prtd->params->channel,
172                                           prtd->params->client, NULL);
173
174                 if (ret) {
175                         DBG(KERN_ERR "failed to get dma channel\n");
176                         return ret;
177                 }
178         }
179
180         s3c2410_dma_set_buffdone_fn(prtd->params->channel,
181                                     s3c24xx_audio_buffdone);
182
183         snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
184
185         runtime->dma_bytes = totbytes;
186
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);
195
196         return 0;
197 }
198
199 static int s3c24xx_pcm_hw_free(struct snd_pcm_substream *substream)
200 {
201         struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
202
203         DBG("Entered %s\n", __func__);
204
205         /* TODO - do we need to ensure DMA flushed */
206         snd_pcm_set_runtime_buffer(substream, NULL);
207
208         if (prtd->params) {
209                 s3c2410_dma_free(prtd->params->channel, prtd->params->client);
210                 prtd->params = NULL;
211         }
212
213         return 0;
214 }
215
216 static int s3c24xx_pcm_prepare(struct snd_pcm_substream *substream)
217 {
218         struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
219         int ret = 0;
220
221         DBG("Entered %s\n", __func__);
222
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;
227
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);
234
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);
244
245                 s3c2410_dma_devconfig(prtd->params->channel,
246                                         S3C2410_DMASRC_HW, 0x3,
247                                         prtd->params->dma_addr);
248         }
249
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;
254
255         /* enqueue dma buffers */
256         s3c24xx_pcm_enqueue(substream);
257
258         return ret;
259 }
260
261 static int s3c24xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
262 {
263         struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
264         int ret = 0;
265
266         DBG("Entered %s\n", __func__);
267
268         spin_lock(&prtd->lock);
269
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;
278
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;
285
286         default:
287                 ret = -EINVAL;
288                 break;
289         }
290
291         spin_unlock(&prtd->lock);
292
293         return ret;
294 }
295
296 static snd_pcm_uframes_t 
297         s3c24xx_pcm_pointer(struct snd_pcm_substream *substream)
298 {
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;
303
304         DBG("Entered %s\n", __func__);
305
306         spin_lock(&prtd->lock);
307         s3c2410_dma_getposition(prtd->params->channel, &src, &dst);
308
309         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
310                 res = dst - prtd->dma_start;
311         else
312                 res = src - prtd->dma_start;
313
314         spin_unlock(&prtd->lock);
315
316         DBG("Pointer %x %x\n",src,dst);
317
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 )
322          */
323
324         if (res >= snd_pcm_lib_buffer_bytes(substream)) {
325                 if (res == snd_pcm_lib_buffer_bytes(substream))
326                         res = 0;
327         }
328
329         return bytes_to_frames(substream->runtime, res);
330 }
331
332 static int s3c24xx_pcm_open(struct snd_pcm_substream *substream)
333 {
334         struct snd_pcm_runtime *runtime = substream->runtime;
335         struct s3c24xx_runtime_data *prtd;
336
337         DBG("Entered %s\n", __func__);
338
339         snd_soc_set_runtime_hwparams(substream, &s3c24xx_pcm_hardware);
340
341         prtd = kzalloc(sizeof(struct s3c24xx_runtime_data), GFP_KERNEL);
342         if (prtd == NULL)
343                 return -ENOMEM;
344
345         spin_lock_init(&prtd->lock);
346
347         runtime->private_data = prtd;
348         return 0;
349 }
350
351 static int s3c24xx_pcm_close(struct snd_pcm_substream *substream)
352 {
353         struct snd_pcm_runtime *runtime = substream->runtime;
354         struct s3c24xx_runtime_data *prtd = runtime->private_data;
355
356         DBG("Entered %s\n", __func__);
357
358         if (prtd)
359                 kfree(prtd);
360         else
361                 DBG("s3c24xx_pcm_close called with prtd == NULL\n");
362
363         return 0;
364 }
365
366 static int s3c24xx_pcm_mmap(struct snd_pcm_substream *substream,
367         struct vm_area_struct *vma)
368 {
369         struct snd_pcm_runtime *runtime = substream->runtime;
370
371         DBG("Entered %s\n", __func__);
372
373         return dma_mmap_writecombine(substream->pcm->card->dev, vma,
374                                      runtime->dma_area,
375                                      runtime->dma_addr,
376                                      runtime->dma_bytes);
377 }
378
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,
389 };
390
391 static int s3c24xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
392 {
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;
396
397         DBG("Entered %s\n", __func__);
398
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;
408 }
409
410 static void s3c24xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
411 {
412         struct snd_pcm_substream *substream;
413         struct snd_dma_buffer *buf;
414         int stream;
415
416         DBG("Entered %s\n", __func__);
417
418         for (stream = 0; stream < 2; stream++) {
419                 substream = pcm->streams[stream].substream;
420                 if (!substream)
421                         continue;
422
423                 buf = &substream->dma_buffer;
424                 if (!buf->area)
425                         continue;
426
427                 dma_free_writecombine(pcm->card->dev, buf->bytes,
428                                       buf->area, buf->addr);
429                 buf->area = NULL;
430         }
431 }
432
433 static u64 s3c24xx_pcm_dmamask = DMA_32BIT_MASK;
434
435 static int s3c24xx_pcm_new(struct snd_card *card, 
436         struct snd_soc_codec_dai *dai, struct snd_pcm *pcm)
437 {
438         int ret = 0;
439
440         DBG("Entered %s\n", __func__);
441
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;
446
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;
452         }
453
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;
459         }
460  out:
461         return ret;
462 }
463
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,
469 };
470
471 EXPORT_SYMBOL_GPL(s3c24xx_soc_platform);
472
473 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
474 MODULE_DESCRIPTION("Samsung S3C24XX PCM DMA module");
475 MODULE_LICENSE("GPL");