[ALSA] soc - ln2440sbc ac97 support
[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/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
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,
65 };
66
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;
77 };
78
79 /* s3c24xx_pcm_enqueue
80  *
81  * place a dma buffer onto the queue for the dma system
82  * to handle.
83 */
84 static 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
92         while (prtd->dma_loaded < prtd->dma_limit) {
93                 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
103                 ret = s3c2410_dma_enqueue(prtd->params->channel, 
104                         substream, pos, len);
105
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
118 static void s3c24xx_audio_buffdone(struct s3c2410_dma_chan *channel,
119                                 void *dev_id, int size,
120                                 enum s3c2410_dma_buffresult result)
121 {
122         struct snd_pcm_substream *substream = dev_id;
123         struct s3c24xx_runtime_data *prtd;
124
125         DBG("Entered %s\n", __FUNCTION__);
126
127         if (result == S3C2410_RES_ABORT || result == S3C2410_RES_ERR)
128                 return;
129
130         prtd = substream->runtime->private_data;
131         
132         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
144 static 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         /* 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;
166
167                 DBG("params %p, client %p, channel %d\n", prtd->params,
168                         prtd->params->client, prtd->params->channel);
169
170                 ret = s3c2410_dma_request(prtd->params->channel,
171                                           prtd->params->client, NULL);
172
173                 if (ret) {
174                         DBG(KERN_ERR "failed to get dma channel\n");
175                         return ret;
176                 }
177         }
178
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);
185
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);
195
196                 s3c2410_dma_devconfig(prtd->params->channel,
197                                         S3C2410_DMASRC_HW, 0x3,
198                                         prtd->params->dma_addr);
199         }
200
201         s3c2410_dma_set_buffdone_fn(prtd->params->channel,
202                                     s3c24xx_audio_buffdone);
203
204         snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
205
206         runtime->dma_bytes = totbytes;
207
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);
216
217         return 0;
218 }
219
220 static int s3c24xx_pcm_hw_free(struct snd_pcm_substream *substream)
221 {
222         struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
223
224         DBG("Entered %s\n", __FUNCTION__);
225
226         /* TODO - do we need to ensure DMA flushed */
227         snd_pcm_set_runtime_buffer(substream, NULL);
228
229         if (prtd->params) {
230                 s3c2410_dma_free(prtd->params->channel, prtd->params->client);
231                 prtd->params = NULL;
232         }
233
234         return 0;
235 }
236
237 static int s3c24xx_pcm_prepare(struct snd_pcm_substream *substream)
238 {
239         struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
240         int ret = 0;
241
242         DBG("Entered %s\n", __FUNCTION__);
243
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;
248
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;
253
254         /* enqueue dma buffers */
255         s3c24xx_pcm_enqueue(substream);
256
257         return ret;
258 }
259
260 static int s3c24xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
261 {
262         struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
263         int ret = 0;
264
265         DBG("Entered %s\n", __FUNCTION__);
266
267         spin_lock(&prtd->lock);
268
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;
277
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;
284
285         default:
286                 ret = -EINVAL;
287                 break;
288         }
289
290         spin_unlock(&prtd->lock);
291
292         return ret;
293 }
294
295 static snd_pcm_uframes_t 
296         s3c24xx_pcm_pointer(struct snd_pcm_substream *substream)
297 {
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;
302
303         DBG("Entered %s\n", __FUNCTION__);
304
305         spin_lock(&prtd->lock);
306         s3c2410_dma_getposition(prtd->params->channel, &src, &dst);
307
308         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
309                 res = dst - prtd->dma_start;
310         else
311                 res = src - prtd->dma_start;
312
313         spin_unlock(&prtd->lock);
314
315         DBG("Pointer %x %x\n",src,dst);
316
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 )
321          */
322
323         if (res >= snd_pcm_lib_buffer_bytes(substream)) {
324                 if (res == snd_pcm_lib_buffer_bytes(substream))
325                         res = 0;
326         }
327
328         return bytes_to_frames(substream->runtime, res);
329 }
330
331 static int s3c24xx_pcm_open(struct snd_pcm_substream *substream)
332 {
333         struct snd_pcm_runtime *runtime = substream->runtime;
334         struct s3c24xx_runtime_data *prtd;
335
336         DBG("Entered %s\n", __FUNCTION__);
337
338         snd_soc_set_runtime_hwparams(substream, &s3c24xx_pcm_hardware);
339
340         prtd = kzalloc(sizeof(struct s3c24xx_runtime_data), GFP_KERNEL);
341         if (prtd == NULL)
342                 return -ENOMEM;
343
344         spin_lock_init(&prtd->lock);
345
346         runtime->private_data = prtd;
347         return 0;
348 }
349
350 static int s3c24xx_pcm_close(struct snd_pcm_substream *substream)
351 {
352         struct snd_pcm_runtime *runtime = substream->runtime;
353         struct s3c24xx_runtime_data *prtd = runtime->private_data;
354
355         DBG("Entered %s\n", __FUNCTION__);
356
357         if (prtd)
358                 kfree(prtd);
359         else
360                 DBG("s3c24xx_pcm_close called with prtd == NULL\n");
361
362         return 0;
363 }
364
365 static int s3c24xx_pcm_mmap(struct snd_pcm_substream *substream,
366         struct vm_area_struct *vma)
367 {
368         struct snd_pcm_runtime *runtime = substream->runtime;
369
370         DBG("Entered %s\n", __FUNCTION__);
371
372         return dma_mmap_writecombine(substream->pcm->card->dev, vma,
373                                      runtime->dma_area,
374                                      runtime->dma_addr,
375                                      runtime->dma_bytes);
376 }
377
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,
388 };
389
390 static int s3c24xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
391 {
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;
395
396         DBG("Entered %s\n", __FUNCTION__);
397
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;
407 }
408
409 static void s3c24xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
410 {
411         struct snd_pcm_substream *substream;
412         struct snd_dma_buffer *buf;
413         int stream;
414
415         DBG("Entered %s\n", __FUNCTION__);
416
417         for (stream = 0; stream < 2; stream++) {
418                 substream = pcm->streams[stream].substream;
419                 if (!substream)
420                         continue;
421
422                 buf = &substream->dma_buffer;
423                 if (!buf->area)
424                         continue;
425
426                 dma_free_writecombine(pcm->card->dev, buf->bytes,
427                                       buf->area, buf->addr);
428                 buf->area = NULL;
429         }
430 }
431
432 static u64 s3c24xx_pcm_dmamask = DMA_32BIT_MASK;
433
434 static int s3c24xx_pcm_new(struct snd_card *card, 
435         struct snd_soc_codec_dai *dai, struct snd_pcm *pcm)
436 {
437         int ret = 0;
438
439         DBG("Entered %s\n", __FUNCTION__);
440
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;
445
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;
451         }
452
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;
458         }
459  out:
460         return ret;
461 }
462
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,
468 };
469
470 EXPORT_SYMBOL_GPL(s3c24xx_soc_platform);
471
472 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
473 MODULE_DESCRIPTION("Samsung S3C24XX PCM DMA module");
474 MODULE_LICENSE("GPL");