Merge tag 'firewire-updates' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee139...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / soc / soc-dmaengine-pcm.c
CommitLineData
e7f73a16
LPC
1/*
2 * Copyright (C) 2012, Analog Devices Inc.
3 * Author: Lars-Peter Clausen <lars@metafoo.de>
4 *
5 * Based on:
6 * imx-pcm-dma-mx2.c, Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
7 * mxs-pcm.c, Copyright (C) 2011 Freescale Semiconductor, Inc.
8 * ep93xx-pcm.c, Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
9 * Copyright (C) 2006 Applied Data Systems
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 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 */
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/dmaengine.h>
24#include <linux/slab.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27#include <sound/soc.h>
28
29#include <sound/dmaengine_pcm.h>
30
31struct dmaengine_pcm_runtime_data {
32 struct dma_chan *dma_chan;
3528f27a 33 dma_cookie_t cookie;
e7f73a16
LPC
34
35 unsigned int pos;
36
37 void *data;
38};
39
40static inline struct dmaengine_pcm_runtime_data *substream_to_prtd(
41 const struct snd_pcm_substream *substream)
42{
43 return substream->runtime->private_data;
44}
45
46/**
47 * snd_dmaengine_pcm_set_data - Set dmaengine substream private data
48 * @substream: PCM substream
49 * @data: Data to set
50 */
51void snd_dmaengine_pcm_set_data(struct snd_pcm_substream *substream, void *data)
52{
53 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
54
55 prtd->data = data;
56}
57EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_data);
58
59/**
60 * snd_dmaengine_pcm_get_data - Get dmaeinge substream private data
61 * @substream: PCM substream
62 *
63 * Returns the data previously set with snd_dmaengine_pcm_set_data
64 */
65void *snd_dmaengine_pcm_get_data(struct snd_pcm_substream *substream)
66{
67 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
68
69 return prtd->data;
70}
71EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_data);
72
73struct dma_chan *snd_dmaengine_pcm_get_chan(struct snd_pcm_substream *substream)
74{
75 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
76
77 return prtd->dma_chan;
78}
79EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_chan);
80
81/**
82 * snd_hwparams_to_dma_slave_config - Convert hw_params to dma_slave_config
83 * @substream: PCM substream
84 * @params: hw_params
85 * @slave_config: DMA slave config
86 *
87 * This function can be used to initialize a dma_slave_config from a substream
88 * and hw_params in a dmaengine based PCM driver implementation.
89 */
90int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream *substream,
91 const struct snd_pcm_hw_params *params,
92 struct dma_slave_config *slave_config)
93{
94 enum dma_slave_buswidth buswidth;
95
96 switch (params_format(params)) {
97 case SNDRV_PCM_FORMAT_S8:
98 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
99 break;
100 case SNDRV_PCM_FORMAT_S16_LE:
101 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
102 break;
103 case SNDRV_PCM_FORMAT_S18_3LE:
104 case SNDRV_PCM_FORMAT_S20_3LE:
105 case SNDRV_PCM_FORMAT_S24_LE:
106 case SNDRV_PCM_FORMAT_S32_LE:
107 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
108 break;
109 default:
110 return -EINVAL;
111 }
112
113 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
114 slave_config->direction = DMA_MEM_TO_DEV;
115 slave_config->dst_addr_width = buswidth;
116 } else {
117 slave_config->direction = DMA_DEV_TO_MEM;
118 slave_config->src_addr_width = buswidth;
119 }
120
121 return 0;
122}
123EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
124
125static void dmaengine_pcm_dma_complete(void *arg)
126{
127 struct snd_pcm_substream *substream = arg;
128 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
129
130 prtd->pos += snd_pcm_lib_period_bytes(substream);
131 if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
132 prtd->pos = 0;
133
134 snd_pcm_period_elapsed(substream);
135}
136
137static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
138{
139 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
140 struct dma_chan *chan = prtd->dma_chan;
141 struct dma_async_tx_descriptor *desc;
142 enum dma_transfer_direction direction;
143
144 direction = snd_pcm_substream_to_dma_direction(substream);
145
7a08cf70 146 prtd->pos = 0;
41ba6b71 147 desc = dmaengine_prep_dma_cyclic(chan,
e7f73a16
LPC
148 substream->runtime->dma_addr,
149 snd_pcm_lib_buffer_bytes(substream),
41ba6b71 150 snd_pcm_lib_period_bytes(substream), direction);
e7f73a16
LPC
151
152 if (!desc)
153 return -ENOMEM;
154
155 desc->callback = dmaengine_pcm_dma_complete;
156 desc->callback_param = substream;
3528f27a 157 prtd->cookie = dmaengine_submit(desc);
e7f73a16
LPC
158
159 return 0;
160}
161
162/**
163 * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
164 * @substream: PCM substream
165 * @cmd: Trigger command
166 *
167 * Returns 0 on success, a negative error code otherwise.
168 *
169 * This function can be used as the PCM trigger callback for dmaengine based PCM
170 * driver implementations.
171 */
172int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
173{
174 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
175 int ret;
176
177 switch (cmd) {
178 case SNDRV_PCM_TRIGGER_START:
179 ret = dmaengine_pcm_prepare_and_submit(substream);
180 if (ret)
181 return ret;
182 dma_async_issue_pending(prtd->dma_chan);
183 break;
184 case SNDRV_PCM_TRIGGER_RESUME:
185 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
186 dmaengine_resume(prtd->dma_chan);
187 break;
188 case SNDRV_PCM_TRIGGER_SUSPEND:
189 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
190 dmaengine_pause(prtd->dma_chan);
191 break;
192 case SNDRV_PCM_TRIGGER_STOP:
193 dmaengine_terminate_all(prtd->dma_chan);
194 break;
195 default:
196 return -EINVAL;
197 }
198
199 return 0;
200}
201EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
202
203/**
9883ab22 204 * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
e7f73a16
LPC
205 * @substream: PCM substream
206 *
9883ab22
LPC
207 * This function is deprecated and should not be used by new drivers, as its
208 * results may be unreliable.
e7f73a16 209 */
9883ab22 210snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
e7f73a16
LPC
211{
212 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
213 return bytes_to_frames(substream->runtime, prtd->pos);
214}
9883ab22 215EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
e7f73a16 216
3528f27a
LPC
217/**
218 * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
219 * @substream: PCM substream
220 *
221 * This function can be used as the PCM pointer callback for dmaengine based PCM
222 * driver implementations.
223 */
224snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
225{
226 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
227 struct dma_tx_state state;
228 enum dma_status status;
229 unsigned int buf_size;
230 unsigned int pos = 0;
231
232 status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
233 if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
234 buf_size = snd_pcm_lib_buffer_bytes(substream);
235 if (state.residue > 0 && state.residue <= buf_size)
236 pos = buf_size - state.residue;
237 }
238
239 return bytes_to_frames(substream->runtime, pos);
240}
241EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
242
e7f73a16
LPC
243static int dmaengine_pcm_request_channel(struct dmaengine_pcm_runtime_data *prtd,
244 dma_filter_fn filter_fn, void *filter_data)
245{
246 dma_cap_mask_t mask;
247
248 dma_cap_zero(mask);
249 dma_cap_set(DMA_SLAVE, mask);
250 dma_cap_set(DMA_CYCLIC, mask);
251 prtd->dma_chan = dma_request_channel(mask, filter_fn, filter_data);
252
253 if (!prtd->dma_chan)
254 return -ENXIO;
255
256 return 0;
257}
258
259/**
260 * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
261 * @substream: PCM substream
262 * @filter_fn: Filter function used to request the DMA channel
263 * @filter_data: Data passed to the DMA filter function
264 *
265 * Returns 0 on success, a negative error code otherwise.
266 *
267 * This function will request a DMA channel using the passed filter function and
268 * data. The function should usually be called from the pcm open callback.
269 *
270 * Note that this function will use private_data field of the substream's
271 * runtime. So it is not availabe to your pcm driver implementation. If you need
272 * to keep additional data attached to a substream use
1573ee81 273 * snd_dmaengine_pcm_{set,get}_data.
e7f73a16
LPC
274 */
275int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
276 dma_filter_fn filter_fn, void *filter_data)
277{
278 struct dmaengine_pcm_runtime_data *prtd;
279 int ret;
280
281 ret = snd_pcm_hw_constraint_integer(substream->runtime,
282 SNDRV_PCM_HW_PARAM_PERIODS);
283 if (ret < 0)
284 return ret;
285
286 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
287 if (!prtd)
288 return -ENOMEM;
289
290 ret = dmaengine_pcm_request_channel(prtd, filter_fn, filter_data);
291 if (ret < 0) {
292 kfree(prtd);
293 return ret;
294 }
295
296 substream->runtime->private_data = prtd;
297
298 return 0;
299}
300EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
301
302/**
303 * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
304 * @substream: PCM substream
305 */
306int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
307{
308 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
309
310 dma_release_channel(prtd->dma_chan);
311 kfree(prtd);
312
313 return 0;
314}
315EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);