blob: ddc6cde14e2a588763531d94f22b0dab2b1d9a49 [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>
Paul Gortmakerda155d52011-07-15 12:38:28 -040019#include <linux/module.h>
Ben Dooksc0f41bb2007-02-14 13:20:03 +010020
Ben Dooksc0f41bb2007-02-14 13:20:03 +010021#include <sound/soc.h>
Seungwhan Youn0378b6a2011-01-11 07:26:06 +090022#include <sound/pcm_params.h>
Ben Dooksc0f41bb2007-02-14 13:20:03 +010023
24#include <asm/dma.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/hardware.h>
26#include <mach/dma.h>
Ben Dooksc0f41bb2007-02-14 13:20:03 +010027
Jassi Brar4b640cf2010-11-22 15:35:57 +090028#include "dma.h"
Ben Dooksc0f41bb2007-02-14 13:20:03 +010029
Seungwhan Younf7f74182011-01-07 13:51:26 +090030#define ST_RUNNING (1<<0)
31#define ST_OPENED (1<<1)
32
Jassi Brarc3f20282010-11-22 15:36:50 +090033static const struct snd_pcm_hardware dma_hardware = {
Ben Dooksc0f41bb2007-02-14 13:20:03 +010034 .info = SNDRV_PCM_INFO_INTERLEAVED |
35 SNDRV_PCM_INFO_BLOCK_TRANSFER |
36 SNDRV_PCM_INFO_MMAP |
Graeme Gregory96d90e12008-01-10 14:44:24 +010037 SNDRV_PCM_INFO_MMAP_VALID |
38 SNDRV_PCM_INFO_PAUSE |
39 SNDRV_PCM_INFO_RESUME,
Ben Dooksc0f41bb2007-02-14 13:20:03 +010040 .formats = SNDRV_PCM_FMTBIT_S16_LE |
41 SNDRV_PCM_FMTBIT_U16_LE |
42 SNDRV_PCM_FMTBIT_U8 |
43 SNDRV_PCM_FMTBIT_S8,
44 .channels_min = 2,
45 .channels_max = 2,
46 .buffer_bytes_max = 128*1024,
47 .period_bytes_min = PAGE_SIZE,
48 .period_bytes_max = PAGE_SIZE*2,
49 .periods_min = 2,
50 .periods_max = 128,
51 .fifo_size = 32,
52};
53
Jassi Brarc3f20282010-11-22 15:36:50 +090054struct runtime_data {
Ben Dooksc0f41bb2007-02-14 13:20:03 +010055 spinlock_t lock;
56 int state;
57 unsigned int dma_loaded;
Ben Dooksc0f41bb2007-02-14 13:20:03 +010058 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
Boojin Kim344b4c42011-09-02 09:44:43 +090065static void audio_buffdone(void *data);
66
Jassi Brarc3f20282010-11-22 15:36:50 +090067/* dma_enqueue
Ben Dooksc0f41bb2007-02-14 13:20:03 +010068 *
69 * place a dma buffer onto the queue for the dma system
70 * to handle.
Boojin Kim344b4c42011-09-02 09:44:43 +090071 */
Jassi Brarc3f20282010-11-22 15:36:50 +090072static void dma_enqueue(struct snd_pcm_substream *substream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +010073{
Jassi Brarc3f20282010-11-22 15:36:50 +090074 struct runtime_data *prtd = substream->runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +010075 dma_addr_t pos = prtd->dma_pos;
Ben Dookse3d80242009-09-22 16:48:56 +010076 unsigned int limit;
Boojin Kim344b4c42011-09-02 09:44:43 +090077 struct samsung_dma_prep_info dma_info;
Ben Dooksc0f41bb2007-02-14 13:20:03 +010078
Mark Brownee7d4762009-03-06 18:04:34 +000079 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +010080
Boojin Kim344b4c42011-09-02 09:44:43 +090081 limit = (prtd->dma_end - prtd->dma_start) / prtd->dma_period;
Ben Dookse3d80242009-09-22 16:48:56 +010082
Jassi Brard3ff5a32009-11-17 16:53:31 +090083 pr_debug("%s: loaded %d, limit %d\n",
84 __func__, prtd->dma_loaded, limit);
Ben Dookse3d80242009-09-22 16:48:56 +010085
Boojin Kim344b4c42011-09-02 09:44:43 +090086 dma_info.cap = (samsung_dma_has_circular() ? DMA_CYCLIC : DMA_SLAVE);
87 dma_info.direction =
88 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
Vinod Koul35e16582011-10-14 10:49:30 +053089 ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
Boojin Kim344b4c42011-09-02 09:44:43 +090090 dma_info.fp = audio_buffdone;
91 dma_info.fp_param = substream;
92 dma_info.period = prtd->dma_period;
93 dma_info.len = prtd->dma_period*limit;
Ben Dooksc0f41bb2007-02-14 13:20:03 +010094
Boojin Kim344b4c42011-09-02 09:44:43 +090095 while (prtd->dma_loaded < limit) {
Mark Brownee7d4762009-03-06 18:04:34 +000096 pr_debug("dma_loaded: %d\n", prtd->dma_loaded);
Ben Dooksc0f41bb2007-02-14 13:20:03 +010097
Boojin Kim344b4c42011-09-02 09:44:43 +090098 if ((pos + dma_info.period) > prtd->dma_end) {
99 dma_info.period = prtd->dma_end - pos;
100 pr_debug("%s: corrected dma len %ld\n",
101 __func__, dma_info.period);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100102 }
103
Boojin Kim344b4c42011-09-02 09:44:43 +0900104 dma_info.buf = pos;
105 prtd->params->ops->prepare(prtd->params->ch, &dma_info);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100106
Boojin Kim344b4c42011-09-02 09:44:43 +0900107 prtd->dma_loaded++;
108 pos += prtd->dma_period;
109 if (pos >= prtd->dma_end)
110 pos = prtd->dma_start;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100111 }
112
113 prtd->dma_pos = pos;
114}
115
Boojin Kim344b4c42011-09-02 09:44:43 +0900116static void audio_buffdone(void *data)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100117{
Boojin Kim344b4c42011-09-02 09:44:43 +0900118 struct snd_pcm_substream *substream = data;
119 struct runtime_data *prtd = substream->runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100120
Mark Brownee7d4762009-03-06 18:04:34 +0000121 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100122
Boojin Kim344b4c42011-09-02 09:44:43 +0900123 if (prtd->state & ST_RUNNING) {
124 prtd->dma_pos += prtd->dma_period;
125 if (prtd->dma_pos >= prtd->dma_end)
126 prtd->dma_pos = prtd->dma_start;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100127
Boojin Kim344b4c42011-09-02 09:44:43 +0900128 if (substream)
129 snd_pcm_period_elapsed(substream);
Mark Brown5111c072008-04-30 17:19:32 +0200130
Boojin Kim344b4c42011-09-02 09:44:43 +0900131 spin_lock(&prtd->lock);
132 if (!samsung_dma_has_circular()) {
133 prtd->dma_loaded--;
134 dma_enqueue(substream);
135 }
136 spin_unlock(&prtd->lock);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100137 }
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100138}
139
Jassi Brarc3f20282010-11-22 15:36:50 +0900140static int dma_hw_params(struct snd_pcm_substream *substream,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100141 struct snd_pcm_hw_params *params)
142{
143 struct snd_pcm_runtime *runtime = substream->runtime;
Jassi Brarc3f20282010-11-22 15:36:50 +0900144 struct runtime_data *prtd = runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100145 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100146 unsigned long totbytes = params_buffer_bytes(params);
Daniel Mack5f712b22010-03-22 10:11:15 +0100147 struct s3c_dma_params *dma =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000148 snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Boojin Kim344b4c42011-09-02 09:44:43 +0900149 struct samsung_dma_info dma_info;
Daniel Mack5f712b22010-03-22 10:11:15 +0100150
Mark Brownee7d4762009-03-06 18:04:34 +0000151 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100152
153 /* return if this is a bufferless transfer e.g.
154 * codec <--> BT codec or GSM modem -- lg FIXME */
155 if (!dma)
156 return 0;
157
Harald Welte646ab162007-07-24 12:49:39 +0200158 /* this may get called several times by oss emulation
159 * with different params -HW */
160 if (prtd->params == NULL) {
161 /* prepare DMA */
162 prtd->params = dma;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100163
Mark Brownee7d4762009-03-06 18:04:34 +0000164 pr_debug("params %p, client %p, channel %d\n", prtd->params,
Harald Welte646ab162007-07-24 12:49:39 +0200165 prtd->params->client, prtd->params->channel);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100166
Boojin Kim344b4c42011-09-02 09:44:43 +0900167 prtd->params->ops = samsung_dma_get_ops();
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100168
Boojin Kim344b4c42011-09-02 09:44:43 +0900169 dma_info.cap = (samsung_dma_has_circular() ?
170 DMA_CYCLIC : DMA_SLAVE);
171 dma_info.client = prtd->params->client;
172 dma_info.direction =
173 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
Vinod Koul35e16582011-10-14 10:49:30 +0530174 ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
Boojin Kim344b4c42011-09-02 09:44:43 +0900175 dma_info.width = prtd->params->dma_size;
176 dma_info.fifo = prtd->params->dma_addr;
177 prtd->params->ch = prtd->params->ops->request(
178 prtd->params->channel, &dma_info);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100179 }
180
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100181 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
182
183 runtime->dma_bytes = totbytes;
184
185 spin_lock_irq(&prtd->lock);
186 prtd->dma_loaded = 0;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100187 prtd->dma_period = params_period_bytes(params);
188 prtd->dma_start = runtime->dma_addr;
189 prtd->dma_pos = prtd->dma_start;
190 prtd->dma_end = prtd->dma_start + totbytes;
191 spin_unlock_irq(&prtd->lock);
192
193 return 0;
194}
195
Jassi Brarc3f20282010-11-22 15:36:50 +0900196static int dma_hw_free(struct snd_pcm_substream *substream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100197{
Jassi Brarc3f20282010-11-22 15:36:50 +0900198 struct runtime_data *prtd = substream->runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100199
Mark Brownee7d4762009-03-06 18:04:34 +0000200 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100201
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100202 snd_pcm_set_runtime_buffer(substream, NULL);
203
Graeme Gregory7f1bc262007-04-17 12:35:18 +0200204 if (prtd->params) {
Mark Brown2ca95762011-09-27 07:29:11 +0900205 prtd->params->ops->flush(prtd->params->ch);
Boojin Kim344b4c42011-09-02 09:44:43 +0900206 prtd->params->ops->release(prtd->params->ch,
207 prtd->params->client);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100208 prtd->params = NULL;
209 }
210
211 return 0;
212}
213
Jassi Brarc3f20282010-11-22 15:36:50 +0900214static int dma_prepare(struct snd_pcm_substream *substream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100215{
Jassi Brarc3f20282010-11-22 15:36:50 +0900216 struct runtime_data *prtd = substream->runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100217 int ret = 0;
218
Mark Brownee7d4762009-03-06 18:04:34 +0000219 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100220
221 /* return if this is a bufferless transfer e.g.
222 * codec <--> BT codec or GSM modem -- lg FIXME */
223 if (!prtd->params)
Mark Brown5111c072008-04-30 17:19:32 +0200224 return 0;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100225
226 /* flush the DMA channel */
Boojin Kim344b4c42011-09-02 09:44:43 +0900227 prtd->params->ops->flush(prtd->params->ch);
228
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100229 prtd->dma_loaded = 0;
230 prtd->dma_pos = prtd->dma_start;
231
232 /* enqueue dma buffers */
Jassi Brarc3f20282010-11-22 15:36:50 +0900233 dma_enqueue(substream);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100234
235 return ret;
236}
237
Jassi Brarc3f20282010-11-22 15:36:50 +0900238static int dma_trigger(struct snd_pcm_substream *substream, int cmd)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100239{
Jassi Brarc3f20282010-11-22 15:36:50 +0900240 struct runtime_data *prtd = substream->runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100241 int ret = 0;
242
Mark Brownee7d4762009-03-06 18:04:34 +0000243 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100244
245 spin_lock(&prtd->lock);
246
247 switch (cmd) {
248 case SNDRV_PCM_TRIGGER_START:
249 case SNDRV_PCM_TRIGGER_RESUME:
250 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
251 prtd->state |= ST_RUNNING;
Boojin Kim344b4c42011-09-02 09:44:43 +0900252 prtd->params->ops->trigger(prtd->params->ch);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100253 break;
254
255 case SNDRV_PCM_TRIGGER_STOP:
256 case SNDRV_PCM_TRIGGER_SUSPEND:
257 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
258 prtd->state &= ~ST_RUNNING;
Boojin Kim344b4c42011-09-02 09:44:43 +0900259 prtd->params->ops->stop(prtd->params->ch);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100260 break;
261
262 default:
263 ret = -EINVAL;
264 break;
265 }
266
267 spin_unlock(&prtd->lock);
268
269 return ret;
270}
271
Mark Brown5111c072008-04-30 17:19:32 +0200272static snd_pcm_uframes_t
Jassi Brarc3f20282010-11-22 15:36:50 +0900273dma_pointer(struct snd_pcm_substream *substream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100274{
275 struct snd_pcm_runtime *runtime = substream->runtime;
Jassi Brarc3f20282010-11-22 15:36:50 +0900276 struct runtime_data *prtd = runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100277 unsigned long res;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100278
Mark Brownee7d4762009-03-06 18:04:34 +0000279 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100280
Boojin Kim344b4c42011-09-02 09:44:43 +0900281 res = prtd->dma_pos - prtd->dma_start;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100282
Boojin Kim344b4c42011-09-02 09:44:43 +0900283 pr_debug("Pointer offset: %lu\n", res);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100284
285 /* we seem to be getting the odd error from the pcm library due
286 * to out-of-bounds pointers. this is maybe due to the dma engine
287 * not having loaded the new values for the channel before being
Justin P. Mattockceade6c2011-02-24 22:16:37 -0800288 * called... (todo - fix )
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100289 */
290
291 if (res >= snd_pcm_lib_buffer_bytes(substream)) {
292 if (res == snd_pcm_lib_buffer_bytes(substream))
293 res = 0;
294 }
295
296 return bytes_to_frames(substream->runtime, res);
297}
298
Jassi Brarc3f20282010-11-22 15:36:50 +0900299static int dma_open(struct snd_pcm_substream *substream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100300{
301 struct snd_pcm_runtime *runtime = substream->runtime;
Jassi Brarc3f20282010-11-22 15:36:50 +0900302 struct runtime_data *prtd;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100303
Mark Brownee7d4762009-03-06 18:04:34 +0000304 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100305
Shine Liuf61c8902009-08-20 23:02:23 +0800306 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
Jassi Brarc3f20282010-11-22 15:36:50 +0900307 snd_soc_set_runtime_hwparams(substream, &dma_hardware);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100308
Jassi Brarc3f20282010-11-22 15:36:50 +0900309 prtd = kzalloc(sizeof(struct runtime_data), GFP_KERNEL);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100310 if (prtd == NULL)
311 return -ENOMEM;
312
Zoltan Devaic72816b2007-05-22 16:17:05 +0200313 spin_lock_init(&prtd->lock);
314
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100315 runtime->private_data = prtd;
316 return 0;
317}
318
Jassi Brarc3f20282010-11-22 15:36:50 +0900319static int dma_close(struct snd_pcm_substream *substream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100320{
321 struct snd_pcm_runtime *runtime = substream->runtime;
Jassi Brarc3f20282010-11-22 15:36:50 +0900322 struct runtime_data *prtd = runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100323
Mark Brownee7d4762009-03-06 18:04:34 +0000324 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100325
Mark Brown5111c072008-04-30 17:19:32 +0200326 if (!prtd)
Jassi Brarc3f20282010-11-22 15:36:50 +0900327 pr_debug("dma_close called with prtd == NULL\n");
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100328
Mark Brown5111c072008-04-30 17:19:32 +0200329 kfree(prtd);
330
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100331 return 0;
332}
333
Jassi Brarc3f20282010-11-22 15:36:50 +0900334static int dma_mmap(struct snd_pcm_substream *substream,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100335 struct vm_area_struct *vma)
336{
337 struct snd_pcm_runtime *runtime = substream->runtime;
338
Mark Brownee7d4762009-03-06 18:04:34 +0000339 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100340
341 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
Mark Brown5111c072008-04-30 17:19:32 +0200342 runtime->dma_area,
343 runtime->dma_addr,
344 runtime->dma_bytes);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100345}
346
Jassi Brarc3f20282010-11-22 15:36:50 +0900347static struct snd_pcm_ops dma_ops = {
348 .open = dma_open,
349 .close = dma_close,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100350 .ioctl = snd_pcm_lib_ioctl,
Jassi Brarc3f20282010-11-22 15:36:50 +0900351 .hw_params = dma_hw_params,
352 .hw_free = dma_hw_free,
353 .prepare = dma_prepare,
354 .trigger = dma_trigger,
355 .pointer = dma_pointer,
356 .mmap = dma_mmap,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100357};
358
Jassi Brarc3f20282010-11-22 15:36:50 +0900359static int preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100360{
361 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
362 struct snd_dma_buffer *buf = &substream->dma_buffer;
Jassi Brarc3f20282010-11-22 15:36:50 +0900363 size_t size = dma_hardware.buffer_bytes_max;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100364
Mark Brownee7d4762009-03-06 18:04:34 +0000365 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100366
367 buf->dev.type = SNDRV_DMA_TYPE_DEV;
368 buf->dev.dev = pcm->card->dev;
369 buf->private_data = NULL;
370 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
371 &buf->addr, GFP_KERNEL);
372 if (!buf->area)
373 return -ENOMEM;
374 buf->bytes = size;
375 return 0;
376}
377
Jassi Brarc3f20282010-11-22 15:36:50 +0900378static void dma_free_dma_buffers(struct snd_pcm *pcm)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100379{
380 struct snd_pcm_substream *substream;
381 struct snd_dma_buffer *buf;
382 int stream;
383
Mark Brownee7d4762009-03-06 18:04:34 +0000384 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100385
386 for (stream = 0; stream < 2; stream++) {
387 substream = pcm->streams[stream].substream;
388 if (!substream)
389 continue;
390
391 buf = &substream->dma_buffer;
392 if (!buf->area)
393 continue;
394
395 dma_free_writecombine(pcm->card->dev, buf->bytes,
396 buf->area, buf->addr);
397 buf->area = NULL;
398 }
399}
400
Jassi Brarc3f20282010-11-22 15:36:50 +0900401static u64 dma_mask = DMA_BIT_MASK(32);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100402
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100403static int dma_new(struct snd_soc_pcm_runtime *rtd)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100404{
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100405 struct snd_card *card = rtd->card->snd_card;
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100406 struct snd_pcm *pcm = rtd->pcm;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100407 int ret = 0;
408
Mark Brownee7d4762009-03-06 18:04:34 +0000409 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100410
411 if (!card->dev->dma_mask)
Jassi Brarc3f20282010-11-22 15:36:50 +0900412 card->dev->dma_mask = &dma_mask;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100413 if (!card->dev->coherent_dma_mask)
Joachim Eastwood350e16d2012-01-01 02:43:03 +0100414 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100415
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100416 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
Jassi Brarc3f20282010-11-22 15:36:50 +0900417 ret = preallocate_dma_buffer(pcm,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100418 SNDRV_PCM_STREAM_PLAYBACK);
419 if (ret)
420 goto out;
421 }
422
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100423 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
Jassi Brarc3f20282010-11-22 15:36:50 +0900424 ret = preallocate_dma_buffer(pcm,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100425 SNDRV_PCM_STREAM_CAPTURE);
426 if (ret)
427 goto out;
428 }
Jassi Brar4b640cf2010-11-22 15:35:57 +0900429out:
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100430 return ret;
431}
432
Jassi Brarc3f20282010-11-22 15:36:50 +0900433static struct snd_soc_platform_driver samsung_asoc_platform = {
434 .ops = &dma_ops,
435 .pcm_new = dma_new,
436 .pcm_free = dma_free_dma_buffers,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100437};
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100438
Jassi Brarc3f20282010-11-22 15:36:50 +0900439static int __devinit samsung_asoc_platform_probe(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000440{
Jassi Brarc3f20282010-11-22 15:36:50 +0900441 return snd_soc_register_platform(&pdev->dev, &samsung_asoc_platform);
Mark Brown958e7922008-12-03 19:58:17 +0000442}
Mark Brown958e7922008-12-03 19:58:17 +0000443
Jassi Brarc3f20282010-11-22 15:36:50 +0900444static int __devexit samsung_asoc_platform_remove(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000445{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000446 snd_soc_unregister_platform(&pdev->dev);
447 return 0;
Mark Brown958e7922008-12-03 19:58:17 +0000448}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000449
Jassi Brarc3f20282010-11-22 15:36:50 +0900450static struct platform_driver asoc_dma_driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000451 .driver = {
Jassi Brar58bb4072010-11-22 15:35:50 +0900452 .name = "samsung-audio",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000453 .owner = THIS_MODULE,
454 },
455
Jassi Brarc3f20282010-11-22 15:36:50 +0900456 .probe = samsung_asoc_platform_probe,
457 .remove = __devexit_p(samsung_asoc_platform_remove),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000458};
459
Mark Browne00c3f52011-11-23 15:20:13 +0000460module_platform_driver(asoc_dma_driver);
Mark Brown958e7922008-12-03 19:58:17 +0000461
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100462MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
Jassi Brarc3f20282010-11-22 15:36:50 +0900463MODULE_DESCRIPTION("Samsung ASoC DMA Driver");
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100464MODULE_LICENSE("GPL");
Jassi Brar58bb4072010-11-22 15:35:50 +0900465MODULE_ALIAS("platform:samsung-audio");