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