blob: 5cb3b880f0d59e84f039dce9c0e519577b451c11 [file] [log] [blame]
Ben Dooksc0f41bb2007-02-14 13:20:03 +01001/*
Jassi Brar4b640cf2010-11-22 15:35:57 +09002 * dma.c -- ALSA Soc Audio Layer
Ben Dooksc0f41bb2007-02-14 13:20:03 +01003 *
4 * (c) 2006 Wolfson Microelectronics PLC.
5 * Graeme Gregory graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
6 *
Ben Dooksc8efef12009-02-28 17:09:57 +00007 * Copyright 2004-2005 Simtec Electronics
Ben Dooksc0f41bb2007-02-14 13:20:03 +01008 * 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.
Ben Dooksc0f41bb2007-02-14 13:20:03 +010015 */
16
Ben Dooksc0f41bb2007-02-14 13:20:03 +010017#include <linux/slab.h>
18#include <linux/dma-mapping.h>
19
Ben Dooksc0f41bb2007-02-14 13:20:03 +010020#include <sound/soc.h>
Seungwhan Youn0378b6a2011-01-11 07:26:06 +090021#include <sound/pcm_params.h>
Ben Dooksc0f41bb2007-02-14 13:20:03 +010022
23#include <asm/dma.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010024#include <mach/hardware.h>
25#include <mach/dma.h>
Ben Dooksc0f41bb2007-02-14 13:20:03 +010026
Jassi Brar4b640cf2010-11-22 15:35:57 +090027#include "dma.h"
Ben Dooksc0f41bb2007-02-14 13:20:03 +010028
Seungwhan Younf7f74182011-01-07 13:51:26 +090029#define ST_RUNNING (1<<0)
30#define ST_OPENED (1<<1)
31
Jassi Brarc3f20282010-11-22 15:36:50 +090032static const struct snd_pcm_hardware dma_hardware = {
Ben Dooksc0f41bb2007-02-14 13:20:03 +010033 .info = SNDRV_PCM_INFO_INTERLEAVED |
34 SNDRV_PCM_INFO_BLOCK_TRANSFER |
35 SNDRV_PCM_INFO_MMAP |
Graeme Gregory96d90e12008-01-10 14:44:24 +010036 SNDRV_PCM_INFO_MMAP_VALID |
37 SNDRV_PCM_INFO_PAUSE |
38 SNDRV_PCM_INFO_RESUME,
Ben Dooksc0f41bb2007-02-14 13:20:03 +010039 .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
Jassi Brarc3f20282010-11-22 15:36:50 +090053struct runtime_data {
Ben Dooksc0f41bb2007-02-14 13:20:03 +010054 spinlock_t lock;
55 int state;
56 unsigned int dma_loaded;
57 unsigned int dma_limit;
58 unsigned int dma_period;
59 dma_addr_t dma_start;
60 dma_addr_t dma_pos;
61 dma_addr_t dma_end;
Jassi Brarfaa31772009-11-17 16:53:23 +090062 struct s3c_dma_params *params;
Ben Dooksc0f41bb2007-02-14 13:20:03 +010063};
64
Jassi Brarc3f20282010-11-22 15:36:50 +090065/* dma_enqueue
Ben Dooksc0f41bb2007-02-14 13:20:03 +010066 *
67 * place a dma buffer onto the queue for the dma system
68 * to handle.
69*/
Jassi Brarc3f20282010-11-22 15:36:50 +090070static void dma_enqueue(struct snd_pcm_substream *substream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +010071{
Jassi Brarc3f20282010-11-22 15:36:50 +090072 struct runtime_data *prtd = substream->runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +010073 dma_addr_t pos = prtd->dma_pos;
Ben Dookse3d80242009-09-22 16:48:56 +010074 unsigned int limit;
Ben Dooksc0f41bb2007-02-14 13:20:03 +010075 int ret;
76
Mark Brownee7d4762009-03-06 18:04:34 +000077 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +010078
Jassi Brard3ff5a32009-11-17 16:53:31 +090079 if (s3c_dma_has_circular())
Ben Dookse3d80242009-09-22 16:48:56 +010080 limit = (prtd->dma_end - prtd->dma_start) / prtd->dma_period;
Jassi Brard3ff5a32009-11-17 16:53:31 +090081 else
Ben Dookse3d80242009-09-22 16:48:56 +010082 limit = prtd->dma_limit;
83
Jassi Brard3ff5a32009-11-17 16:53:31 +090084 pr_debug("%s: loaded %d, limit %d\n",
85 __func__, prtd->dma_loaded, limit);
Ben Dookse3d80242009-09-22 16:48:56 +010086
87 while (prtd->dma_loaded < limit) {
Ben Dooksc0f41bb2007-02-14 13:20:03 +010088 unsigned long len = prtd->dma_period;
89
Mark Brownee7d4762009-03-06 18:04:34 +000090 pr_debug("dma_loaded: %d\n", prtd->dma_loaded);
Ben Dooksc0f41bb2007-02-14 13:20:03 +010091
92 if ((pos + len) > prtd->dma_end) {
93 len = prtd->dma_end - pos;
Joe Perches147fcf12010-09-11 22:10:59 -070094 pr_debug("%s: corrected dma len %ld\n", __func__, len);
Ben Dooksc0f41bb2007-02-14 13:20:03 +010095 }
96
Mark Brown5111c072008-04-30 17:19:32 +020097 ret = s3c2410_dma_enqueue(prtd->params->channel,
Graeme Gregory7f1bc262007-04-17 12:35:18 +020098 substream, pos, len);
Ben Dooksc0f41bb2007-02-14 13:20:03 +010099
100 if (ret == 0) {
101 prtd->dma_loaded++;
102 pos += prtd->dma_period;
103 if (pos >= prtd->dma_end)
104 pos = prtd->dma_start;
105 } else
106 break;
107 }
108
109 prtd->dma_pos = pos;
110}
111
Jassi Brarc3f20282010-11-22 15:36:50 +0900112static void audio_buffdone(struct s3c2410_dma_chan *channel,
Graeme Gregory7f1bc262007-04-17 12:35:18 +0200113 void *dev_id, int size,
114 enum s3c2410_dma_buffresult result)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100115{
116 struct snd_pcm_substream *substream = dev_id;
Jassi Brarc3f20282010-11-22 15:36:50 +0900117 struct runtime_data *prtd;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100118
Mark Brownee7d4762009-03-06 18:04:34 +0000119 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100120
121 if (result == S3C2410_RES_ABORT || result == S3C2410_RES_ERR)
122 return;
123
Graeme Gregory7f1bc262007-04-17 12:35:18 +0200124 prtd = substream->runtime->private_data;
Mark Brown5111c072008-04-30 17:19:32 +0200125
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100126 if (substream)
127 snd_pcm_period_elapsed(substream);
128
129 spin_lock(&prtd->lock);
Ben Dookse3d80242009-09-22 16:48:56 +0100130 if (prtd->state & ST_RUNNING && !s3c_dma_has_circular()) {
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100131 prtd->dma_loaded--;
Jassi Brarc3f20282010-11-22 15:36:50 +0900132 dma_enqueue(substream);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100133 }
134
135 spin_unlock(&prtd->lock);
136}
137
Jassi Brarc3f20282010-11-22 15:36:50 +0900138static int dma_hw_params(struct snd_pcm_substream *substream,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100139 struct snd_pcm_hw_params *params)
140{
141 struct snd_pcm_runtime *runtime = substream->runtime;
Jassi Brarc3f20282010-11-22 15:36:50 +0900142 struct runtime_data *prtd = runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100143 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100144 unsigned long totbytes = params_buffer_bytes(params);
Daniel Mack5f712b22010-03-22 10:11:15 +0100145 struct s3c_dma_params *dma =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000146 snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Mark Brown5111c072008-04-30 17:19:32 +0200147 int ret = 0;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100148
Daniel Mack5f712b22010-03-22 10:11:15 +0100149
Mark Brownee7d4762009-03-06 18:04:34 +0000150 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100151
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
Harald Welte646ab162007-07-24 12:49:39 +0200157 /* 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;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100162
Mark Brownee7d4762009-03-06 18:04:34 +0000163 pr_debug("params %p, client %p, channel %d\n", prtd->params,
Harald Welte646ab162007-07-24 12:49:39 +0200164 prtd->params->client, prtd->params->channel);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100165
Harald Welte646ab162007-07-24 12:49:39 +0200166 ret = s3c2410_dma_request(prtd->params->channel,
167 prtd->params->client, NULL);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100168
Davide Rizzod6426172008-05-05 14:56:07 +0200169 if (ret < 0) {
Mark Brownb52a5192009-03-06 18:13:43 +0000170 printk(KERN_ERR "failed to get dma channel\n");
Harald Welte646ab162007-07-24 12:49:39 +0200171 return ret;
172 }
Ben Dookse3d80242009-09-22 16:48:56 +0100173
174 /* use the circular buffering if we have it available. */
175 if (s3c_dma_has_circular())
176 s3c2410_dma_setflags(prtd->params->channel,
177 S3C2410_DMAF_CIRCULAR);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100178 }
179
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100180 s3c2410_dma_set_buffdone_fn(prtd->params->channel,
Jassi Brarc3f20282010-11-22 15:36:50 +0900181 audio_buffdone);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100182
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
Jassi Brarc3f20282010-11-22 15:36:50 +0900199static int dma_hw_free(struct snd_pcm_substream *substream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100200{
Jassi Brarc3f20282010-11-22 15:36:50 +0900201 struct runtime_data *prtd = substream->runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100202
Mark Brownee7d4762009-03-06 18:04:34 +0000203 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100204
205 /* TODO - do we need to ensure DMA flushed */
206 snd_pcm_set_runtime_buffer(substream, NULL);
207
Graeme Gregory7f1bc262007-04-17 12:35:18 +0200208 if (prtd->params) {
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100209 s3c2410_dma_free(prtd->params->channel, prtd->params->client);
210 prtd->params = NULL;
211 }
212
213 return 0;
214}
215
Jassi Brarc3f20282010-11-22 15:36:50 +0900216static int dma_prepare(struct snd_pcm_substream *substream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100217{
Jassi Brarc3f20282010-11-22 15:36:50 +0900218 struct runtime_data *prtd = substream->runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100219 int ret = 0;
220
Mark Brownee7d4762009-03-06 18:04:34 +0000221 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100222
223 /* return if this is a bufferless transfer e.g.
224 * codec <--> BT codec or GSM modem -- lg FIXME */
225 if (!prtd->params)
Mark Brown5111c072008-04-30 17:19:32 +0200226 return 0;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100227
Graeme Gregory96d90e12008-01-10 14:44:24 +0100228 /* 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,
Ben Dooks8970ef42009-03-19 15:02:34 +0000232 S3C2410_DMASRC_MEM,
233 prtd->params->dma_addr);
Graeme Gregory96d90e12008-01-10 14:44:24 +0100234 } else {
Graeme Gregory96d90e12008-01-10 14:44:24 +0100235 s3c2410_dma_devconfig(prtd->params->channel,
Ben Dooks8970ef42009-03-19 15:02:34 +0000236 S3C2410_DMASRC_HW,
237 prtd->params->dma_addr);
Graeme Gregory96d90e12008-01-10 14:44:24 +0100238 }
239
Ben Dooks8970ef42009-03-19 15:02:34 +0000240 s3c2410_dma_config(prtd->params->channel,
241 prtd->params->dma_size);
242
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100243 /* flush the DMA channel */
244 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_FLUSH);
245 prtd->dma_loaded = 0;
246 prtd->dma_pos = prtd->dma_start;
247
248 /* enqueue dma buffers */
Jassi Brarc3f20282010-11-22 15:36:50 +0900249 dma_enqueue(substream);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100250
251 return ret;
252}
253
Jassi Brarc3f20282010-11-22 15:36:50 +0900254static int dma_trigger(struct snd_pcm_substream *substream, int cmd)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100255{
Jassi Brarc3f20282010-11-22 15:36:50 +0900256 struct runtime_data *prtd = substream->runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100257 int ret = 0;
258
Mark Brownee7d4762009-03-06 18:04:34 +0000259 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100260
261 spin_lock(&prtd->lock);
262
263 switch (cmd) {
264 case SNDRV_PCM_TRIGGER_START:
265 case SNDRV_PCM_TRIGGER_RESUME:
266 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
267 prtd->state |= ST_RUNNING;
268 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_START);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100269 break;
270
271 case SNDRV_PCM_TRIGGER_STOP:
272 case SNDRV_PCM_TRIGGER_SUSPEND:
273 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
274 prtd->state &= ~ST_RUNNING;
275 s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STOP);
276 break;
277
278 default:
279 ret = -EINVAL;
280 break;
281 }
282
283 spin_unlock(&prtd->lock);
284
285 return ret;
286}
287
Mark Brown5111c072008-04-30 17:19:32 +0200288static snd_pcm_uframes_t
Jassi Brarc3f20282010-11-22 15:36:50 +0900289dma_pointer(struct snd_pcm_substream *substream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100290{
291 struct snd_pcm_runtime *runtime = substream->runtime;
Jassi Brarc3f20282010-11-22 15:36:50 +0900292 struct runtime_data *prtd = runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100293 unsigned long res;
294 dma_addr_t src, dst;
295
Mark Brownee7d4762009-03-06 18:04:34 +0000296 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100297
298 spin_lock(&prtd->lock);
299 s3c2410_dma_getposition(prtd->params->channel, &src, &dst);
300
301 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
302 res = dst - prtd->dma_start;
303 else
304 res = src - prtd->dma_start;
305
306 spin_unlock(&prtd->lock);
307
Mark Brownee7d4762009-03-06 18:04:34 +0000308 pr_debug("Pointer %x %x\n", src, dst);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100309
310 /* we seem to be getting the odd error from the pcm library due
311 * to out-of-bounds pointers. this is maybe due to the dma engine
312 * not having loaded the new values for the channel before being
Justin P. Mattockceade6c2011-02-24 22:16:37 -0800313 * called... (todo - fix )
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100314 */
315
316 if (res >= snd_pcm_lib_buffer_bytes(substream)) {
317 if (res == snd_pcm_lib_buffer_bytes(substream))
318 res = 0;
319 }
320
321 return bytes_to_frames(substream->runtime, res);
322}
323
Jassi Brarc3f20282010-11-22 15:36:50 +0900324static int dma_open(struct snd_pcm_substream *substream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100325{
326 struct snd_pcm_runtime *runtime = substream->runtime;
Jassi Brarc3f20282010-11-22 15:36:50 +0900327 struct runtime_data *prtd;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100328
Mark Brownee7d4762009-03-06 18:04:34 +0000329 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100330
Shine Liuf61c8902009-08-20 23:02:23 +0800331 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
Jassi Brarc3f20282010-11-22 15:36:50 +0900332 snd_soc_set_runtime_hwparams(substream, &dma_hardware);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100333
Jassi Brarc3f20282010-11-22 15:36:50 +0900334 prtd = kzalloc(sizeof(struct runtime_data), GFP_KERNEL);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100335 if (prtd == NULL)
336 return -ENOMEM;
337
Zoltan Devaic72816b2007-05-22 16:17:05 +0200338 spin_lock_init(&prtd->lock);
339
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100340 runtime->private_data = prtd;
341 return 0;
342}
343
Jassi Brarc3f20282010-11-22 15:36:50 +0900344static int dma_close(struct snd_pcm_substream *substream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100345{
346 struct snd_pcm_runtime *runtime = substream->runtime;
Jassi Brarc3f20282010-11-22 15:36:50 +0900347 struct runtime_data *prtd = runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100348
Mark Brownee7d4762009-03-06 18:04:34 +0000349 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100350
Mark Brown5111c072008-04-30 17:19:32 +0200351 if (!prtd)
Jassi Brarc3f20282010-11-22 15:36:50 +0900352 pr_debug("dma_close called with prtd == NULL\n");
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100353
Mark Brown5111c072008-04-30 17:19:32 +0200354 kfree(prtd);
355
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100356 return 0;
357}
358
Jassi Brarc3f20282010-11-22 15:36:50 +0900359static int dma_mmap(struct snd_pcm_substream *substream,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100360 struct vm_area_struct *vma)
361{
362 struct snd_pcm_runtime *runtime = substream->runtime;
363
Mark Brownee7d4762009-03-06 18:04:34 +0000364 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100365
366 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
Mark Brown5111c072008-04-30 17:19:32 +0200367 runtime->dma_area,
368 runtime->dma_addr,
369 runtime->dma_bytes);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100370}
371
Jassi Brarc3f20282010-11-22 15:36:50 +0900372static struct snd_pcm_ops dma_ops = {
373 .open = dma_open,
374 .close = dma_close,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100375 .ioctl = snd_pcm_lib_ioctl,
Jassi Brarc3f20282010-11-22 15:36:50 +0900376 .hw_params = dma_hw_params,
377 .hw_free = dma_hw_free,
378 .prepare = dma_prepare,
379 .trigger = dma_trigger,
380 .pointer = dma_pointer,
381 .mmap = dma_mmap,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100382};
383
Jassi Brarc3f20282010-11-22 15:36:50 +0900384static int preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100385{
386 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
387 struct snd_dma_buffer *buf = &substream->dma_buffer;
Jassi Brarc3f20282010-11-22 15:36:50 +0900388 size_t size = dma_hardware.buffer_bytes_max;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100389
Mark Brownee7d4762009-03-06 18:04:34 +0000390 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100391
392 buf->dev.type = SNDRV_DMA_TYPE_DEV;
393 buf->dev.dev = pcm->card->dev;
394 buf->private_data = NULL;
395 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
396 &buf->addr, GFP_KERNEL);
397 if (!buf->area)
398 return -ENOMEM;
399 buf->bytes = size;
400 return 0;
401}
402
Jassi Brarc3f20282010-11-22 15:36:50 +0900403static void dma_free_dma_buffers(struct snd_pcm *pcm)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100404{
405 struct snd_pcm_substream *substream;
406 struct snd_dma_buffer *buf;
407 int stream;
408
Mark Brownee7d4762009-03-06 18:04:34 +0000409 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100410
411 for (stream = 0; stream < 2; stream++) {
412 substream = pcm->streams[stream].substream;
413 if (!substream)
414 continue;
415
416 buf = &substream->dma_buffer;
417 if (!buf->area)
418 continue;
419
420 dma_free_writecombine(pcm->card->dev, buf->bytes,
421 buf->area, buf->addr);
422 buf->area = NULL;
423 }
424}
425
Jassi Brarc3f20282010-11-22 15:36:50 +0900426static u64 dma_mask = DMA_BIT_MASK(32);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100427
Jassi Brarc3f20282010-11-22 15:36:50 +0900428static int dma_new(struct snd_card *card,
Liam Girdwood1992a6f2008-07-07 16:08:24 +0100429 struct snd_soc_dai *dai, struct snd_pcm *pcm)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100430{
431 int ret = 0;
432
Mark Brownee7d4762009-03-06 18:04:34 +0000433 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100434
435 if (!card->dev->dma_mask)
Jassi Brarc3f20282010-11-22 15:36:50 +0900436 card->dev->dma_mask = &dma_mask;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100437 if (!card->dev->coherent_dma_mask)
438 card->dev->coherent_dma_mask = 0xffffffff;
439
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000440 if (dai->driver->playback.channels_min) {
Jassi Brarc3f20282010-11-22 15:36:50 +0900441 ret = preallocate_dma_buffer(pcm,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100442 SNDRV_PCM_STREAM_PLAYBACK);
443 if (ret)
444 goto out;
445 }
446
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000447 if (dai->driver->capture.channels_min) {
Jassi Brarc3f20282010-11-22 15:36:50 +0900448 ret = preallocate_dma_buffer(pcm,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100449 SNDRV_PCM_STREAM_CAPTURE);
450 if (ret)
451 goto out;
452 }
Jassi Brar4b640cf2010-11-22 15:35:57 +0900453out:
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100454 return ret;
455}
456
Jassi Brarc3f20282010-11-22 15:36:50 +0900457static struct snd_soc_platform_driver samsung_asoc_platform = {
458 .ops = &dma_ops,
459 .pcm_new = dma_new,
460 .pcm_free = dma_free_dma_buffers,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100461};
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100462
Jassi Brarc3f20282010-11-22 15:36:50 +0900463static int __devinit samsung_asoc_platform_probe(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000464{
Jassi Brarc3f20282010-11-22 15:36:50 +0900465 return snd_soc_register_platform(&pdev->dev, &samsung_asoc_platform);
Mark Brown958e7922008-12-03 19:58:17 +0000466}
Mark Brown958e7922008-12-03 19:58:17 +0000467
Jassi Brarc3f20282010-11-22 15:36:50 +0900468static int __devexit samsung_asoc_platform_remove(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000469{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000470 snd_soc_unregister_platform(&pdev->dev);
471 return 0;
Mark Brown958e7922008-12-03 19:58:17 +0000472}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000473
Jassi Brarc3f20282010-11-22 15:36:50 +0900474static struct platform_driver asoc_dma_driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000475 .driver = {
Jassi Brar58bb4072010-11-22 15:35:50 +0900476 .name = "samsung-audio",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000477 .owner = THIS_MODULE,
478 },
479
Jassi Brarc3f20282010-11-22 15:36:50 +0900480 .probe = samsung_asoc_platform_probe,
481 .remove = __devexit_p(samsung_asoc_platform_remove),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000482};
483
Jassi Brarc3f20282010-11-22 15:36:50 +0900484static int __init samsung_asoc_init(void)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000485{
Jassi Brarc3f20282010-11-22 15:36:50 +0900486 return platform_driver_register(&asoc_dma_driver);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000487}
Jassi Brarc3f20282010-11-22 15:36:50 +0900488module_init(samsung_asoc_init);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000489
Jassi Brarc3f20282010-11-22 15:36:50 +0900490static void __exit samsung_asoc_exit(void)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000491{
Jassi Brarc3f20282010-11-22 15:36:50 +0900492 platform_driver_unregister(&asoc_dma_driver);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000493}
Jassi Brarc3f20282010-11-22 15:36:50 +0900494module_exit(samsung_asoc_exit);
Mark Brown958e7922008-12-03 19:58:17 +0000495
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100496MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
Jassi Brarc3f20282010-11-22 15:36:50 +0900497MODULE_DESCRIPTION("Samsung ASoC DMA Driver");
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100498MODULE_LICENSE("GPL");
Jassi Brar58bb4072010-11-22 15:35:50 +0900499MODULE_ALIAS("platform:samsung-audio");