blob: 21b79262010ecb3774d8a5ad5ad7f8f87707549c [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 |
Dylan Reid57b2d6882012-09-01 01:38:19 -070037 SNDRV_PCM_INFO_MMAP_VALID,
Ben Dooksc0f41bb2007-02-14 13:20:03 +010038 .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
Jassi Brarc3f20282010-11-22 15:36:50 +090052struct runtime_data {
Ben Dooksc0f41bb2007-02-14 13:20:03 +010053 spinlock_t lock;
54 int state;
55 unsigned int dma_loaded;
Ben Dooksc0f41bb2007-02-14 13:20:03 +010056 unsigned int dma_period;
57 dma_addr_t dma_start;
58 dma_addr_t dma_pos;
59 dma_addr_t dma_end;
Jassi Brarfaa31772009-11-17 16:53:23 +090060 struct s3c_dma_params *params;
Ben Dooksc0f41bb2007-02-14 13:20:03 +010061};
62
Boojin Kim344b4c42011-09-02 09:44:43 +090063static void audio_buffdone(void *data);
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.
Boojin Kim344b4c42011-09-02 09:44:43 +090069 */
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;
Boojin Kim21c4afe2012-06-19 13:27:07 +090075 struct samsung_dma_prep dma_info;
Ben Dooksc0f41bb2007-02-14 13:20:03 +010076
Mark Brownee7d4762009-03-06 18:04:34 +000077 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +010078
Boojin Kim344b4c42011-09-02 09:44:43 +090079 limit = (prtd->dma_end - prtd->dma_start) / prtd->dma_period;
Ben Dookse3d80242009-09-22 16:48:56 +010080
Jassi Brard3ff5a32009-11-17 16:53:31 +090081 pr_debug("%s: loaded %d, limit %d\n",
82 __func__, prtd->dma_loaded, limit);
Ben Dookse3d80242009-09-22 16:48:56 +010083
Boojin Kim344b4c42011-09-02 09:44:43 +090084 dma_info.cap = (samsung_dma_has_circular() ? DMA_CYCLIC : DMA_SLAVE);
85 dma_info.direction =
86 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
Vinod Koul35e16582011-10-14 10:49:30 +053087 ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
Boojin Kim344b4c42011-09-02 09:44:43 +090088 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;
Ben Dooksc0f41bb2007-02-14 13:20:03 +010092
Boojin Kim344b4c42011-09-02 09:44:43 +090093 while (prtd->dma_loaded < limit) {
Mark Brownee7d4762009-03-06 18:04:34 +000094 pr_debug("dma_loaded: %d\n", prtd->dma_loaded);
Ben Dooksc0f41bb2007-02-14 13:20:03 +010095
Boojin Kim344b4c42011-09-02 09:44:43 +090096 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);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100100 }
101
Boojin Kim344b4c42011-09-02 09:44:43 +0900102 dma_info.buf = pos;
103 prtd->params->ops->prepare(prtd->params->ch, &dma_info);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100104
Boojin Kim344b4c42011-09-02 09:44:43 +0900105 prtd->dma_loaded++;
106 pos += prtd->dma_period;
107 if (pos >= prtd->dma_end)
108 pos = prtd->dma_start;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100109 }
110
111 prtd->dma_pos = pos;
112}
113
Boojin Kim344b4c42011-09-02 09:44:43 +0900114static void audio_buffdone(void *data)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100115{
Boojin Kim344b4c42011-09-02 09:44:43 +0900116 struct snd_pcm_substream *substream = data;
117 struct runtime_data *prtd = substream->runtime->private_data;
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
Boojin Kim344b4c42011-09-02 09:44:43 +0900121 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;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100125
Boojin Kim344b4c42011-09-02 09:44:43 +0900126 if (substream)
127 snd_pcm_period_elapsed(substream);
Mark Brown5111c072008-04-30 17:19:32 +0200128
Boojin Kim344b4c42011-09-02 09:44:43 +0900129 spin_lock(&prtd->lock);
130 if (!samsung_dma_has_circular()) {
131 prtd->dma_loaded--;
132 dma_enqueue(substream);
133 }
134 spin_unlock(&prtd->lock);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100135 }
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100136}
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);
Boojin Kim21c4afe2012-06-19 13:27:07 +0900147 struct samsung_dma_req req;
148 struct samsung_dma_config config;
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
Boojin Kim344b4c42011-09-02 09:44:43 +0900166 prtd->params->ops = samsung_dma_get_ops();
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100167
Boojin Kim21c4afe2012-06-19 13:27:07 +0900168 req.cap = (samsung_dma_has_circular() ?
Boojin Kim344b4c42011-09-02 09:44:43 +0900169 DMA_CYCLIC : DMA_SLAVE);
Boojin Kim21c4afe2012-06-19 13:27:07 +0900170 req.client = prtd->params->client;
171 config.direction =
Boojin Kim344b4c42011-09-02 09:44:43 +0900172 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
Vinod Koul35e16582011-10-14 10:49:30 +0530173 ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
Boojin Kim21c4afe2012-06-19 13:27:07 +0900174 config.width = prtd->params->dma_size;
175 config.fifo = prtd->params->dma_addr;
Boojin Kim344b4c42011-09-02 09:44:43 +0900176 prtd->params->ch = prtd->params->ops->request(
Padmavathi Venna40476f62013-01-18 17:17:01 +0530177 prtd->params->channel, &req, rtd->cpu_dai->dev,
178 prtd->params->ch_name);
Boojin Kim21c4afe2012-06-19 13:27:07 +0900179 prtd->params->ops->config(prtd->params->ch, &config);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100180 }
181
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100182 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
183
184 runtime->dma_bytes = totbytes;
185
186 spin_lock_irq(&prtd->lock);
187 prtd->dma_loaded = 0;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100188 prtd->dma_period = params_period_bytes(params);
189 prtd->dma_start = runtime->dma_addr;
190 prtd->dma_pos = prtd->dma_start;
191 prtd->dma_end = prtd->dma_start + totbytes;
192 spin_unlock_irq(&prtd->lock);
193
194 return 0;
195}
196
Jassi Brarc3f20282010-11-22 15:36:50 +0900197static int dma_hw_free(struct snd_pcm_substream *substream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100198{
Jassi Brarc3f20282010-11-22 15:36:50 +0900199 struct runtime_data *prtd = substream->runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100200
Mark Brownee7d4762009-03-06 18:04:34 +0000201 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100202
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100203 snd_pcm_set_runtime_buffer(substream, NULL);
204
Graeme Gregory7f1bc262007-04-17 12:35:18 +0200205 if (prtd->params) {
Mark Brown2ca95762011-09-27 07:29:11 +0900206 prtd->params->ops->flush(prtd->params->ch);
Boojin Kim344b4c42011-09-02 09:44:43 +0900207 prtd->params->ops->release(prtd->params->ch,
208 prtd->params->client);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100209 prtd->params = NULL;
210 }
211
212 return 0;
213}
214
Jassi Brarc3f20282010-11-22 15:36:50 +0900215static int dma_prepare(struct snd_pcm_substream *substream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100216{
Jassi Brarc3f20282010-11-22 15:36:50 +0900217 struct runtime_data *prtd = substream->runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100218 int ret = 0;
219
Mark Brownee7d4762009-03-06 18:04:34 +0000220 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100221
222 /* return if this is a bufferless transfer e.g.
223 * codec <--> BT codec or GSM modem -- lg FIXME */
224 if (!prtd->params)
Mark Brown5111c072008-04-30 17:19:32 +0200225 return 0;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100226
227 /* flush the DMA channel */
Boojin Kim344b4c42011-09-02 09:44:43 +0900228 prtd->params->ops->flush(prtd->params->ch);
229
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100230 prtd->dma_loaded = 0;
231 prtd->dma_pos = prtd->dma_start;
232
233 /* enqueue dma buffers */
Jassi Brarc3f20282010-11-22 15:36:50 +0900234 dma_enqueue(substream);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100235
236 return ret;
237}
238
Jassi Brarc3f20282010-11-22 15:36:50 +0900239static int dma_trigger(struct snd_pcm_substream *substream, int cmd)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100240{
Jassi Brarc3f20282010-11-22 15:36:50 +0900241 struct runtime_data *prtd = substream->runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100242 int ret = 0;
243
Mark Brownee7d4762009-03-06 18:04:34 +0000244 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100245
246 spin_lock(&prtd->lock);
247
248 switch (cmd) {
249 case SNDRV_PCM_TRIGGER_START:
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100250 prtd->state |= ST_RUNNING;
Boojin Kim344b4c42011-09-02 09:44:43 +0900251 prtd->params->ops->trigger(prtd->params->ch);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100252 break;
253
254 case SNDRV_PCM_TRIGGER_STOP:
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100255 prtd->state &= ~ST_RUNNING;
Boojin Kim344b4c42011-09-02 09:44:43 +0900256 prtd->params->ops->stop(prtd->params->ch);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100257 break;
258
259 default:
260 ret = -EINVAL;
261 break;
262 }
263
264 spin_unlock(&prtd->lock);
265
266 return ret;
267}
268
Mark Brown5111c072008-04-30 17:19:32 +0200269static snd_pcm_uframes_t
Jassi Brarc3f20282010-11-22 15:36:50 +0900270dma_pointer(struct snd_pcm_substream *substream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100271{
272 struct snd_pcm_runtime *runtime = substream->runtime;
Jassi Brarc3f20282010-11-22 15:36:50 +0900273 struct runtime_data *prtd = runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100274 unsigned long res;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100275
Mark Brownee7d4762009-03-06 18:04:34 +0000276 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100277
Boojin Kim344b4c42011-09-02 09:44:43 +0900278 res = prtd->dma_pos - prtd->dma_start;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100279
Boojin Kim344b4c42011-09-02 09:44:43 +0900280 pr_debug("Pointer offset: %lu\n", res);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100281
282 /* we seem to be getting the odd error from the pcm library due
283 * to out-of-bounds pointers. this is maybe due to the dma engine
284 * not having loaded the new values for the channel before being
Justin P. Mattockceade6c2011-02-24 22:16:37 -0800285 * called... (todo - fix )
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100286 */
287
288 if (res >= snd_pcm_lib_buffer_bytes(substream)) {
289 if (res == snd_pcm_lib_buffer_bytes(substream))
290 res = 0;
291 }
292
293 return bytes_to_frames(substream->runtime, res);
294}
295
Jassi Brarc3f20282010-11-22 15:36:50 +0900296static int dma_open(struct snd_pcm_substream *substream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100297{
298 struct snd_pcm_runtime *runtime = substream->runtime;
Jassi Brarc3f20282010-11-22 15:36:50 +0900299 struct runtime_data *prtd;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100300
Mark Brownee7d4762009-03-06 18:04:34 +0000301 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100302
Shine Liuf61c8902009-08-20 23:02:23 +0800303 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
Jassi Brarc3f20282010-11-22 15:36:50 +0900304 snd_soc_set_runtime_hwparams(substream, &dma_hardware);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100305
Jassi Brarc3f20282010-11-22 15:36:50 +0900306 prtd = kzalloc(sizeof(struct runtime_data), GFP_KERNEL);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100307 if (prtd == NULL)
308 return -ENOMEM;
309
Zoltan Devaic72816b2007-05-22 16:17:05 +0200310 spin_lock_init(&prtd->lock);
311
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100312 runtime->private_data = prtd;
313 return 0;
314}
315
Jassi Brarc3f20282010-11-22 15:36:50 +0900316static int dma_close(struct snd_pcm_substream *substream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100317{
318 struct snd_pcm_runtime *runtime = substream->runtime;
Jassi Brarc3f20282010-11-22 15:36:50 +0900319 struct runtime_data *prtd = runtime->private_data;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100320
Mark Brownee7d4762009-03-06 18:04:34 +0000321 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100322
Mark Brown5111c072008-04-30 17:19:32 +0200323 if (!prtd)
Jassi Brarc3f20282010-11-22 15:36:50 +0900324 pr_debug("dma_close called with prtd == NULL\n");
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100325
Mark Brown5111c072008-04-30 17:19:32 +0200326 kfree(prtd);
327
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100328 return 0;
329}
330
Jassi Brarc3f20282010-11-22 15:36:50 +0900331static int dma_mmap(struct snd_pcm_substream *substream,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100332 struct vm_area_struct *vma)
333{
334 struct snd_pcm_runtime *runtime = substream->runtime;
335
Mark Brownee7d4762009-03-06 18:04:34 +0000336 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100337
338 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
Mark Brown5111c072008-04-30 17:19:32 +0200339 runtime->dma_area,
340 runtime->dma_addr,
341 runtime->dma_bytes);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100342}
343
Jassi Brarc3f20282010-11-22 15:36:50 +0900344static struct snd_pcm_ops dma_ops = {
345 .open = dma_open,
346 .close = dma_close,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100347 .ioctl = snd_pcm_lib_ioctl,
Jassi Brarc3f20282010-11-22 15:36:50 +0900348 .hw_params = dma_hw_params,
349 .hw_free = dma_hw_free,
350 .prepare = dma_prepare,
351 .trigger = dma_trigger,
352 .pointer = dma_pointer,
353 .mmap = dma_mmap,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100354};
355
Jassi Brarc3f20282010-11-22 15:36:50 +0900356static int preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100357{
358 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
359 struct snd_dma_buffer *buf = &substream->dma_buffer;
Jassi Brarc3f20282010-11-22 15:36:50 +0900360 size_t size = dma_hardware.buffer_bytes_max;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100361
Mark Brownee7d4762009-03-06 18:04:34 +0000362 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100363
364 buf->dev.type = SNDRV_DMA_TYPE_DEV;
365 buf->dev.dev = pcm->card->dev;
366 buf->private_data = NULL;
367 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
368 &buf->addr, GFP_KERNEL);
369 if (!buf->area)
370 return -ENOMEM;
371 buf->bytes = size;
372 return 0;
373}
374
Jassi Brarc3f20282010-11-22 15:36:50 +0900375static void dma_free_dma_buffers(struct snd_pcm *pcm)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100376{
377 struct snd_pcm_substream *substream;
378 struct snd_dma_buffer *buf;
379 int stream;
380
Mark Brownee7d4762009-03-06 18:04:34 +0000381 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100382
383 for (stream = 0; stream < 2; stream++) {
384 substream = pcm->streams[stream].substream;
385 if (!substream)
386 continue;
387
388 buf = &substream->dma_buffer;
389 if (!buf->area)
390 continue;
391
392 dma_free_writecombine(pcm->card->dev, buf->bytes,
393 buf->area, buf->addr);
394 buf->area = NULL;
395 }
396}
397
Jassi Brarc3f20282010-11-22 15:36:50 +0900398static u64 dma_mask = DMA_BIT_MASK(32);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100399
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100400static int dma_new(struct snd_soc_pcm_runtime *rtd)
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100401{
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100402 struct snd_card *card = rtd->card->snd_card;
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100403 struct snd_pcm *pcm = rtd->pcm;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100404 int ret = 0;
405
Mark Brownee7d4762009-03-06 18:04:34 +0000406 pr_debug("Entered %s\n", __func__);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100407
408 if (!card->dev->dma_mask)
Jassi Brarc3f20282010-11-22 15:36:50 +0900409 card->dev->dma_mask = &dma_mask;
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100410 if (!card->dev->coherent_dma_mask)
Joachim Eastwood350e16d2012-01-01 02:43:03 +0100411 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100412
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100413 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
Jassi Brarc3f20282010-11-22 15:36:50 +0900414 ret = preallocate_dma_buffer(pcm,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100415 SNDRV_PCM_STREAM_PLAYBACK);
416 if (ret)
417 goto out;
418 }
419
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100420 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
Jassi Brarc3f20282010-11-22 15:36:50 +0900421 ret = preallocate_dma_buffer(pcm,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100422 SNDRV_PCM_STREAM_CAPTURE);
423 if (ret)
424 goto out;
425 }
Jassi Brar4b640cf2010-11-22 15:35:57 +0900426out:
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100427 return ret;
428}
429
Jassi Brarc3f20282010-11-22 15:36:50 +0900430static struct snd_soc_platform_driver samsung_asoc_platform = {
431 .ops = &dma_ops,
432 .pcm_new = dma_new,
433 .pcm_free = dma_free_dma_buffers,
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100434};
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100435
Bill Pembertonfdca21a2012-12-07 09:26:15 -0500436int asoc_dma_platform_register(struct device *dev)
Mark Brown958e7922008-12-03 19:58:17 +0000437{
Padmavathi Vennaa08485d2012-12-07 13:59:21 +0530438 return snd_soc_register_platform(dev, &samsung_asoc_platform);
Mark Brown958e7922008-12-03 19:58:17 +0000439}
Padmavathi Vennaa08485d2012-12-07 13:59:21 +0530440EXPORT_SYMBOL_GPL(asoc_dma_platform_register);
Mark Brown958e7922008-12-03 19:58:17 +0000441
Bill Pembertonfdca21a2012-12-07 09:26:15 -0500442void asoc_dma_platform_unregister(struct device *dev)
Mark Brown958e7922008-12-03 19:58:17 +0000443{
Padmavathi Vennaa08485d2012-12-07 13:59:21 +0530444 snd_soc_unregister_platform(dev);
Mark Brown958e7922008-12-03 19:58:17 +0000445}
Padmavathi Vennaa08485d2012-12-07 13:59:21 +0530446EXPORT_SYMBOL_GPL(asoc_dma_platform_unregister);
Mark Brown958e7922008-12-03 19:58:17 +0000447
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100448MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
Jassi Brarc3f20282010-11-22 15:36:50 +0900449MODULE_DESCRIPTION("Samsung ASoC DMA Driver");
Ben Dooksc0f41bb2007-02-14 13:20:03 +0100450MODULE_LICENSE("GPL");