ASoC: dmaengine-pcm: Remove snd_dmaengine_pcm_{set,get}_data
[deliverable/linux.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;
e7f73a16
LPC
36};
37
38static inline struct dmaengine_pcm_runtime_data *substream_to_prtd(
39 const struct snd_pcm_substream *substream)
40{
41 return substream->runtime->private_data;
42}
43
e7f73a16
LPC
44struct dma_chan *snd_dmaengine_pcm_get_chan(struct snd_pcm_substream *substream)
45{
46 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
47
48 return prtd->dma_chan;
49}
50EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_chan);
51
52/**
53 * snd_hwparams_to_dma_slave_config - Convert hw_params to dma_slave_config
54 * @substream: PCM substream
55 * @params: hw_params
56 * @slave_config: DMA slave config
57 *
58 * This function can be used to initialize a dma_slave_config from a substream
59 * and hw_params in a dmaengine based PCM driver implementation.
60 */
61int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream *substream,
62 const struct snd_pcm_hw_params *params,
63 struct dma_slave_config *slave_config)
64{
65 enum dma_slave_buswidth buswidth;
66
67 switch (params_format(params)) {
68 case SNDRV_PCM_FORMAT_S8:
69 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
70 break;
71 case SNDRV_PCM_FORMAT_S16_LE:
72 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
73 break;
74 case SNDRV_PCM_FORMAT_S18_3LE:
75 case SNDRV_PCM_FORMAT_S20_3LE:
76 case SNDRV_PCM_FORMAT_S24_LE:
77 case SNDRV_PCM_FORMAT_S32_LE:
78 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
79 break;
80 default:
81 return -EINVAL;
82 }
83
84 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
85 slave_config->direction = DMA_MEM_TO_DEV;
86 slave_config->dst_addr_width = buswidth;
87 } else {
88 slave_config->direction = DMA_DEV_TO_MEM;
89 slave_config->src_addr_width = buswidth;
90 }
91
92 return 0;
93}
94EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
95
96static void dmaengine_pcm_dma_complete(void *arg)
97{
98 struct snd_pcm_substream *substream = arg;
99 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
100
101 prtd->pos += snd_pcm_lib_period_bytes(substream);
102 if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
103 prtd->pos = 0;
104
105 snd_pcm_period_elapsed(substream);
106}
107
108static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
109{
110 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
111 struct dma_chan *chan = prtd->dma_chan;
112 struct dma_async_tx_descriptor *desc;
113 enum dma_transfer_direction direction;
e7736cde 114 unsigned long flags = DMA_CTRL_ACK;
e7f73a16
LPC
115
116 direction = snd_pcm_substream_to_dma_direction(substream);
117
e7736cde
PU
118 if (!substream->runtime->no_period_wakeup)
119 flags |= DMA_PREP_INTERRUPT;
120
7a08cf70 121 prtd->pos = 0;
41ba6b71 122 desc = dmaengine_prep_dma_cyclic(chan,
e7f73a16
LPC
123 substream->runtime->dma_addr,
124 snd_pcm_lib_buffer_bytes(substream),
e7736cde 125 snd_pcm_lib_period_bytes(substream), direction, flags);
e7f73a16
LPC
126
127 if (!desc)
128 return -ENOMEM;
129
130 desc->callback = dmaengine_pcm_dma_complete;
131 desc->callback_param = substream;
3528f27a 132 prtd->cookie = dmaengine_submit(desc);
e7f73a16
LPC
133
134 return 0;
135}
136
137/**
138 * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
139 * @substream: PCM substream
140 * @cmd: Trigger command
141 *
142 * Returns 0 on success, a negative error code otherwise.
143 *
144 * This function can be used as the PCM trigger callback for dmaengine based PCM
145 * driver implementations.
146 */
147int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
148{
149 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
150 int ret;
151
152 switch (cmd) {
153 case SNDRV_PCM_TRIGGER_START:
154 ret = dmaengine_pcm_prepare_and_submit(substream);
155 if (ret)
156 return ret;
157 dma_async_issue_pending(prtd->dma_chan);
158 break;
159 case SNDRV_PCM_TRIGGER_RESUME:
160 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
161 dmaengine_resume(prtd->dma_chan);
162 break;
163 case SNDRV_PCM_TRIGGER_SUSPEND:
164 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
165 dmaengine_pause(prtd->dma_chan);
166 break;
167 case SNDRV_PCM_TRIGGER_STOP:
168 dmaengine_terminate_all(prtd->dma_chan);
169 break;
170 default:
171 return -EINVAL;
172 }
173
174 return 0;
175}
176EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
177
178/**
9883ab22 179 * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
e7f73a16
LPC
180 * @substream: PCM substream
181 *
9883ab22
LPC
182 * This function is deprecated and should not be used by new drivers, as its
183 * results may be unreliable.
e7f73a16 184 */
9883ab22 185snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
e7f73a16
LPC
186{
187 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
188 return bytes_to_frames(substream->runtime, prtd->pos);
189}
9883ab22 190EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
e7f73a16 191
3528f27a
LPC
192/**
193 * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
194 * @substream: PCM substream
195 *
196 * This function can be used as the PCM pointer callback for dmaengine based PCM
197 * driver implementations.
198 */
199snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
200{
201 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
202 struct dma_tx_state state;
203 enum dma_status status;
204 unsigned int buf_size;
205 unsigned int pos = 0;
206
207 status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
208 if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
209 buf_size = snd_pcm_lib_buffer_bytes(substream);
210 if (state.residue > 0 && state.residue <= buf_size)
211 pos = buf_size - state.residue;
212 }
213
214 return bytes_to_frames(substream->runtime, pos);
215}
216EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
217
e7f73a16
LPC
218static int dmaengine_pcm_request_channel(struct dmaengine_pcm_runtime_data *prtd,
219 dma_filter_fn filter_fn, void *filter_data)
220{
221 dma_cap_mask_t mask;
222
223 dma_cap_zero(mask);
224 dma_cap_set(DMA_SLAVE, mask);
225 dma_cap_set(DMA_CYCLIC, mask);
226 prtd->dma_chan = dma_request_channel(mask, filter_fn, filter_data);
227
228 if (!prtd->dma_chan)
229 return -ENXIO;
230
231 return 0;
232}
233
234/**
235 * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
236 * @substream: PCM substream
237 * @filter_fn: Filter function used to request the DMA channel
238 * @filter_data: Data passed to the DMA filter function
239 *
240 * Returns 0 on success, a negative error code otherwise.
241 *
242 * This function will request a DMA channel using the passed filter function and
243 * data. The function should usually be called from the pcm open callback.
244 *
245 * Note that this function will use private_data field of the substream's
246 * runtime. So it is not availabe to your pcm driver implementation. If you need
247 * to keep additional data attached to a substream use
1573ee81 248 * snd_dmaengine_pcm_{set,get}_data.
e7f73a16
LPC
249 */
250int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
251 dma_filter_fn filter_fn, void *filter_data)
252{
253 struct dmaengine_pcm_runtime_data *prtd;
254 int ret;
255
256 ret = snd_pcm_hw_constraint_integer(substream->runtime,
257 SNDRV_PCM_HW_PARAM_PERIODS);
258 if (ret < 0)
259 return ret;
260
261 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
262 if (!prtd)
263 return -ENOMEM;
264
265 ret = dmaengine_pcm_request_channel(prtd, filter_fn, filter_data);
266 if (ret < 0) {
267 kfree(prtd);
268 return ret;
269 }
270
271 substream->runtime->private_data = prtd;
272
273 return 0;
274}
275EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
276
277/**
278 * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
279 * @substream: PCM substream
280 */
281int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
282{
283 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
284
285 dma_release_channel(prtd->dma_chan);
286 kfree(prtd);
287
288 return 0;
289}
290EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
cba69b4d
LW
291
292MODULE_LICENSE("GPL");
This page took 0.087312 seconds and 5 git commands to generate.