Merge branch 'next-samsung-devel' into next-samsung-devel-2
[deliverable/linux.git] / sound / soc / samsung / dma.c
1 /*
2 * dma.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/slab.h>
18 #include <linux/dma-mapping.h>
19
20 #include <sound/soc.h>
21 #include <sound/pcm_params.h>
22
23 #include <asm/dma.h>
24 #include <mach/hardware.h>
25 #include <mach/dma.h>
26
27 #include "dma.h"
28
29 #define ST_RUNNING (1<<0)
30 #define ST_OPENED (1<<1)
31
32 static const struct snd_pcm_hardware dma_hardware = {
33 .info = SNDRV_PCM_INFO_INTERLEAVED |
34 SNDRV_PCM_INFO_BLOCK_TRANSFER |
35 SNDRV_PCM_INFO_MMAP |
36 SNDRV_PCM_INFO_MMAP_VALID |
37 SNDRV_PCM_INFO_PAUSE |
38 SNDRV_PCM_INFO_RESUME,
39 .formats = SNDRV_PCM_FMTBIT_S16_LE |
40 SNDRV_PCM_FMTBIT_U16_LE |
41 SNDRV_PCM_FMTBIT_U8 |
42 SNDRV_PCM_FMTBIT_S8,
43 .channels_min = 2,
44 .channels_max = 2,
45 .buffer_bytes_max = 128*1024,
46 .period_bytes_min = PAGE_SIZE,
47 .period_bytes_max = PAGE_SIZE*2,
48 .periods_min = 2,
49 .periods_max = 128,
50 .fifo_size = 32,
51 };
52
53 struct runtime_data {
54 spinlock_t lock;
55 int state;
56 unsigned int dma_loaded;
57 unsigned int dma_period;
58 dma_addr_t dma_start;
59 dma_addr_t dma_pos;
60 dma_addr_t dma_end;
61 struct s3c_dma_params *params;
62 };
63
64 static void audio_buffdone(void *data);
65
66 /* dma_enqueue
67 *
68 * place a dma buffer onto the queue for the dma system
69 * to handle.
70 */
71 static void dma_enqueue(struct snd_pcm_substream *substream)
72 {
73 struct runtime_data *prtd = substream->runtime->private_data;
74 dma_addr_t pos = prtd->dma_pos;
75 unsigned int limit;
76 struct samsung_dma_prep_info dma_info;
77
78 pr_debug("Entered %s\n", __func__);
79
80 limit = (prtd->dma_end - prtd->dma_start) / prtd->dma_period;
81
82 pr_debug("%s: loaded %d, limit %d\n",
83 __func__, prtd->dma_loaded, limit);
84
85 dma_info.cap = (samsung_dma_has_circular() ? DMA_CYCLIC : DMA_SLAVE);
86 dma_info.direction =
87 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
88 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
89 dma_info.fp = audio_buffdone;
90 dma_info.fp_param = substream;
91 dma_info.period = prtd->dma_period;
92 dma_info.len = prtd->dma_period*limit;
93
94 while (prtd->dma_loaded < limit) {
95 pr_debug("dma_loaded: %d\n", prtd->dma_loaded);
96
97 if ((pos + dma_info.period) > prtd->dma_end) {
98 dma_info.period = prtd->dma_end - pos;
99 pr_debug("%s: corrected dma len %ld\n",
100 __func__, dma_info.period);
101 }
102
103 dma_info.buf = pos;
104 prtd->params->ops->prepare(prtd->params->ch, &dma_info);
105
106 prtd->dma_loaded++;
107 pos += prtd->dma_period;
108 if (pos >= prtd->dma_end)
109 pos = prtd->dma_start;
110 }
111
112 prtd->dma_pos = pos;
113 }
114
115 static void audio_buffdone(void *data)
116 {
117 struct snd_pcm_substream *substream = data;
118 struct runtime_data *prtd = substream->runtime->private_data;
119
120 pr_debug("Entered %s\n", __func__);
121
122 if (prtd->state & ST_RUNNING) {
123 prtd->dma_pos += prtd->dma_period;
124 if (prtd->dma_pos >= prtd->dma_end)
125 prtd->dma_pos = prtd->dma_start;
126
127 if (substream)
128 snd_pcm_period_elapsed(substream);
129
130 spin_lock(&prtd->lock);
131 if (!samsung_dma_has_circular()) {
132 prtd->dma_loaded--;
133 dma_enqueue(substream);
134 }
135 spin_unlock(&prtd->lock);
136 }
137 }
138
139 static int dma_hw_params(struct snd_pcm_substream *substream,
140 struct snd_pcm_hw_params *params)
141 {
142 struct snd_pcm_runtime *runtime = substream->runtime;
143 struct runtime_data *prtd = runtime->private_data;
144 struct snd_soc_pcm_runtime *rtd = substream->private_data;
145 unsigned long totbytes = params_buffer_bytes(params);
146 struct s3c_dma_params *dma =
147 snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
148 struct samsung_dma_info dma_info;
149
150 pr_debug("Entered %s\n", __func__);
151
152 /* return if this is a bufferless transfer e.g.
153 * codec <--> BT codec or GSM modem -- lg FIXME */
154 if (!dma)
155 return 0;
156
157 /* this may get called several times by oss emulation
158 * with different params -HW */
159 if (prtd->params == NULL) {
160 /* prepare DMA */
161 prtd->params = dma;
162
163 pr_debug("params %p, client %p, channel %d\n", prtd->params,
164 prtd->params->client, prtd->params->channel);
165
166 prtd->params->ops = samsung_dma_get_ops();
167
168 dma_info.cap = (samsung_dma_has_circular() ?
169 DMA_CYCLIC : DMA_SLAVE);
170 dma_info.client = prtd->params->client;
171 dma_info.direction =
172 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
173 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
174 dma_info.width = prtd->params->dma_size;
175 dma_info.fifo = prtd->params->dma_addr;
176 prtd->params->ch = prtd->params->ops->request(
177 prtd->params->channel, &dma_info);
178 }
179
180 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
181
182 runtime->dma_bytes = totbytes;
183
184 spin_lock_irq(&prtd->lock);
185 prtd->dma_loaded = 0;
186 prtd->dma_period = params_period_bytes(params);
187 prtd->dma_start = runtime->dma_addr;
188 prtd->dma_pos = prtd->dma_start;
189 prtd->dma_end = prtd->dma_start + totbytes;
190 spin_unlock_irq(&prtd->lock);
191
192 return 0;
193 }
194
195 static int dma_hw_free(struct snd_pcm_substream *substream)
196 {
197 struct runtime_data *prtd = substream->runtime->private_data;
198
199 pr_debug("Entered %s\n", __func__);
200
201 snd_pcm_set_runtime_buffer(substream, NULL);
202
203 if (prtd->params) {
204 prtd->params->ops->flush(prtd->params->ch);
205 prtd->params->ops->release(prtd->params->ch,
206 prtd->params->client);
207 prtd->params = NULL;
208 }
209
210 return 0;
211 }
212
213 static int dma_prepare(struct snd_pcm_substream *substream)
214 {
215 struct runtime_data *prtd = substream->runtime->private_data;
216 int ret = 0;
217
218 pr_debug("Entered %s\n", __func__);
219
220 /* return if this is a bufferless transfer e.g.
221 * codec <--> BT codec or GSM modem -- lg FIXME */
222 if (!prtd->params)
223 return 0;
224
225 /* flush the DMA channel */
226 prtd->params->ops->flush(prtd->params->ch);
227
228 prtd->dma_loaded = 0;
229 prtd->dma_pos = prtd->dma_start;
230
231 /* enqueue dma buffers */
232 dma_enqueue(substream);
233
234 return ret;
235 }
236
237 static int dma_trigger(struct snd_pcm_substream *substream, int cmd)
238 {
239 struct runtime_data *prtd = substream->runtime->private_data;
240 int ret = 0;
241
242 pr_debug("Entered %s\n", __func__);
243
244 spin_lock(&prtd->lock);
245
246 switch (cmd) {
247 case SNDRV_PCM_TRIGGER_START:
248 case SNDRV_PCM_TRIGGER_RESUME:
249 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
250 prtd->state |= ST_RUNNING;
251 prtd->params->ops->trigger(prtd->params->ch);
252 break;
253
254 case SNDRV_PCM_TRIGGER_STOP:
255 case SNDRV_PCM_TRIGGER_SUSPEND:
256 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
257 prtd->state &= ~ST_RUNNING;
258 prtd->params->ops->stop(prtd->params->ch);
259 break;
260
261 default:
262 ret = -EINVAL;
263 break;
264 }
265
266 spin_unlock(&prtd->lock);
267
268 return ret;
269 }
270
271 static snd_pcm_uframes_t
272 dma_pointer(struct snd_pcm_substream *substream)
273 {
274 struct snd_pcm_runtime *runtime = substream->runtime;
275 struct runtime_data *prtd = runtime->private_data;
276 unsigned long res;
277
278 pr_debug("Entered %s\n", __func__);
279
280 res = prtd->dma_pos - prtd->dma_start;
281
282 pr_debug("Pointer offset: %lu\n", res);
283
284 /* we seem to be getting the odd error from the pcm library due
285 * to out-of-bounds pointers. this is maybe due to the dma engine
286 * not having loaded the new values for the channel before being
287 * called... (todo - fix )
288 */
289
290 if (res >= snd_pcm_lib_buffer_bytes(substream)) {
291 if (res == snd_pcm_lib_buffer_bytes(substream))
292 res = 0;
293 }
294
295 return bytes_to_frames(substream->runtime, res);
296 }
297
298 static int dma_open(struct snd_pcm_substream *substream)
299 {
300 struct snd_pcm_runtime *runtime = substream->runtime;
301 struct runtime_data *prtd;
302
303 pr_debug("Entered %s\n", __func__);
304
305 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
306 snd_soc_set_runtime_hwparams(substream, &dma_hardware);
307
308 prtd = kzalloc(sizeof(struct runtime_data), GFP_KERNEL);
309 if (prtd == NULL)
310 return -ENOMEM;
311
312 spin_lock_init(&prtd->lock);
313
314 runtime->private_data = prtd;
315 return 0;
316 }
317
318 static int dma_close(struct snd_pcm_substream *substream)
319 {
320 struct snd_pcm_runtime *runtime = substream->runtime;
321 struct runtime_data *prtd = runtime->private_data;
322
323 pr_debug("Entered %s\n", __func__);
324
325 if (!prtd)
326 pr_debug("dma_close called with prtd == NULL\n");
327
328 kfree(prtd);
329
330 return 0;
331 }
332
333 static int dma_mmap(struct snd_pcm_substream *substream,
334 struct vm_area_struct *vma)
335 {
336 struct snd_pcm_runtime *runtime = substream->runtime;
337
338 pr_debug("Entered %s\n", __func__);
339
340 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
341 runtime->dma_area,
342 runtime->dma_addr,
343 runtime->dma_bytes);
344 }
345
346 static struct snd_pcm_ops dma_ops = {
347 .open = dma_open,
348 .close = dma_close,
349 .ioctl = snd_pcm_lib_ioctl,
350 .hw_params = dma_hw_params,
351 .hw_free = dma_hw_free,
352 .prepare = dma_prepare,
353 .trigger = dma_trigger,
354 .pointer = dma_pointer,
355 .mmap = dma_mmap,
356 };
357
358 static int preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
359 {
360 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
361 struct snd_dma_buffer *buf = &substream->dma_buffer;
362 size_t size = dma_hardware.buffer_bytes_max;
363
364 pr_debug("Entered %s\n", __func__);
365
366 buf->dev.type = SNDRV_DMA_TYPE_DEV;
367 buf->dev.dev = pcm->card->dev;
368 buf->private_data = NULL;
369 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
370 &buf->addr, GFP_KERNEL);
371 if (!buf->area)
372 return -ENOMEM;
373 buf->bytes = size;
374 return 0;
375 }
376
377 static void dma_free_dma_buffers(struct snd_pcm *pcm)
378 {
379 struct snd_pcm_substream *substream;
380 struct snd_dma_buffer *buf;
381 int stream;
382
383 pr_debug("Entered %s\n", __func__);
384
385 for (stream = 0; stream < 2; stream++) {
386 substream = pcm->streams[stream].substream;
387 if (!substream)
388 continue;
389
390 buf = &substream->dma_buffer;
391 if (!buf->area)
392 continue;
393
394 dma_free_writecombine(pcm->card->dev, buf->bytes,
395 buf->area, buf->addr);
396 buf->area = NULL;
397 }
398 }
399
400 static u64 dma_mask = DMA_BIT_MASK(32);
401
402 static int dma_new(struct snd_soc_pcm_runtime *rtd)
403 {
404 struct snd_card *card = rtd->card->snd_card;
405 struct snd_soc_dai *dai = rtd->cpu_dai;
406 struct snd_pcm *pcm = rtd->pcm;
407 int ret = 0;
408
409 pr_debug("Entered %s\n", __func__);
410
411 if (!card->dev->dma_mask)
412 card->dev->dma_mask = &dma_mask;
413 if (!card->dev->coherent_dma_mask)
414 card->dev->coherent_dma_mask = 0xffffffff;
415
416 if (dai->driver->playback.channels_min) {
417 ret = preallocate_dma_buffer(pcm,
418 SNDRV_PCM_STREAM_PLAYBACK);
419 if (ret)
420 goto out;
421 }
422
423 if (dai->driver->capture.channels_min) {
424 ret = preallocate_dma_buffer(pcm,
425 SNDRV_PCM_STREAM_CAPTURE);
426 if (ret)
427 goto out;
428 }
429 out:
430 return ret;
431 }
432
433 static struct snd_soc_platform_driver samsung_asoc_platform = {
434 .ops = &dma_ops,
435 .pcm_new = dma_new,
436 .pcm_free = dma_free_dma_buffers,
437 };
438
439 static int __devinit samsung_asoc_platform_probe(struct platform_device *pdev)
440 {
441 return snd_soc_register_platform(&pdev->dev, &samsung_asoc_platform);
442 }
443
444 static int __devexit samsung_asoc_platform_remove(struct platform_device *pdev)
445 {
446 snd_soc_unregister_platform(&pdev->dev);
447 return 0;
448 }
449
450 static struct platform_driver asoc_dma_driver = {
451 .driver = {
452 .name = "samsung-audio",
453 .owner = THIS_MODULE,
454 },
455
456 .probe = samsung_asoc_platform_probe,
457 .remove = __devexit_p(samsung_asoc_platform_remove),
458 };
459
460 static int __init samsung_asoc_init(void)
461 {
462 return platform_driver_register(&asoc_dma_driver);
463 }
464 module_init(samsung_asoc_init);
465
466 static void __exit samsung_asoc_exit(void)
467 {
468 platform_driver_unregister(&asoc_dma_driver);
469 }
470 module_exit(samsung_asoc_exit);
471
472 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
473 MODULE_DESCRIPTION("Samsung ASoC DMA Driver");
474 MODULE_LICENSE("GPL");
475 MODULE_ALIAS("platform:samsung-audio");
This page took 0.038907 seconds and 5 git commands to generate.