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