Loading...
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;
33 dma_cookie_t cookie;
34
35 unsigned int pos;
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
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 int bits;
67
68 bits = params_physical_width(params);
69 if (bits < 8 || bits > 64)
70 return -EINVAL;
71 else if (bits == 8)
72 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
73 else if (bits == 16)
74 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
75 else if (bits == 24)
76 buswidth = DMA_SLAVE_BUSWIDTH_3_BYTES;
77 else if (bits <= 32)
78 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
79 else
80 buswidth = DMA_SLAVE_BUSWIDTH_8_BYTES;
81
82 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
83 slave_config->direction = DMA_MEM_TO_DEV;
84 slave_config->dst_addr_width = buswidth;
85 } else {
86 slave_config->direction = DMA_DEV_TO_MEM;
87 slave_config->src_addr_width = buswidth;
88 }
89
90 slave_config->device_fc = false;
91
92 return 0;
93}
94EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
95
96/**
97 * snd_dmaengine_pcm_set_config_from_dai_data() - Initializes a dma slave config
98 * using DAI DMA data.
99 * @substream: PCM substream
100 * @dma_data: DAI DMA data
101 * @slave_config: DMA slave configuration
102 *
103 * Initializes the {dst,src}_addr, {dst,src}_maxburst, {dst,src}_addr_width and
104 * slave_id fields of the DMA slave config from the same fields of the DAI DMA
105 * data struct. The src and dst fields will be initialized depending on the
106 * direction of the substream. If the substream is a playback stream the dst
107 * fields will be initialized, if it is a capture stream the src fields will be
108 * initialized. The {dst,src}_addr_width field will only be initialized if the
109 * SND_DMAENGINE_PCM_DAI_FLAG_PACK flag is set or if the addr_width field of
110 * the DAI DMA data struct is not equal to DMA_SLAVE_BUSWIDTH_UNDEFINED. If
111 * both conditions are met the latter takes priority.
112 */
113void snd_dmaengine_pcm_set_config_from_dai_data(
114 const struct snd_pcm_substream *substream,
115 const struct snd_dmaengine_dai_dma_data *dma_data,
116 struct dma_slave_config *slave_config)
117{
118 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
119 slave_config->dst_addr = dma_data->addr;
120 slave_config->dst_maxburst = dma_data->maxburst;
121 if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK)
122 slave_config->dst_addr_width =
123 DMA_SLAVE_BUSWIDTH_UNDEFINED;
124 if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
125 slave_config->dst_addr_width = dma_data->addr_width;
126 } else {
127 slave_config->src_addr = dma_data->addr;
128 slave_config->src_maxburst = dma_data->maxburst;
129 if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK)
130 slave_config->src_addr_width =
131 DMA_SLAVE_BUSWIDTH_UNDEFINED;
132 if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
133 slave_config->src_addr_width = dma_data->addr_width;
134 }
135
136 slave_config->slave_id = dma_data->slave_id;
137}
138EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data);
139
140static void dmaengine_pcm_dma_complete(void *arg)
141{
142 struct snd_pcm_substream *substream = arg;
143 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
144
145 prtd->pos += snd_pcm_lib_period_bytes(substream);
146 if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
147 prtd->pos = 0;
148
149 snd_pcm_period_elapsed(substream);
150}
151
152static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
153{
154 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
155 struct dma_chan *chan = prtd->dma_chan;
156 struct dma_async_tx_descriptor *desc;
157 enum dma_transfer_direction direction;
158 unsigned long flags = DMA_CTRL_ACK;
159
160 direction = snd_pcm_substream_to_dma_direction(substream);
161
162 if (!substream->runtime->no_period_wakeup)
163 flags |= DMA_PREP_INTERRUPT;
164
165 prtd->pos = 0;
166 desc = dmaengine_prep_dma_cyclic(chan,
167 substream->runtime->dma_addr,
168 snd_pcm_lib_buffer_bytes(substream),
169 snd_pcm_lib_period_bytes(substream), direction, flags);
170
171 if (!desc)
172 return -ENOMEM;
173
174 desc->callback = dmaengine_pcm_dma_complete;
175 desc->callback_param = substream;
176 prtd->cookie = dmaengine_submit(desc);
177
178 return 0;
179}
180
181/**
182 * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
183 * @substream: PCM substream
184 * @cmd: Trigger command
185 *
186 * Returns 0 on success, a negative error code otherwise.
187 *
188 * This function can be used as the PCM trigger callback for dmaengine based PCM
189 * driver implementations.
190 */
191int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
192{
193 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
194 struct snd_pcm_runtime *runtime = substream->runtime;
195 int ret;
196
197 switch (cmd) {
198 case SNDRV_PCM_TRIGGER_START:
199 ret = dmaengine_pcm_prepare_and_submit(substream);
200 if (ret)
201 return ret;
202 dma_async_issue_pending(prtd->dma_chan);
203 break;
204 case SNDRV_PCM_TRIGGER_RESUME:
205 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
206 dmaengine_resume(prtd->dma_chan);
207 break;
208 case SNDRV_PCM_TRIGGER_SUSPEND:
209 if (runtime->info & SNDRV_PCM_INFO_PAUSE)
210 dmaengine_pause(prtd->dma_chan);
211 else
212 dmaengine_terminate_async(prtd->dma_chan);
213 break;
214 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
215 dmaengine_pause(prtd->dma_chan);
216 break;
217 case SNDRV_PCM_TRIGGER_STOP:
218 dmaengine_terminate_async(prtd->dma_chan);
219 break;
220 default:
221 return -EINVAL;
222 }
223
224 return 0;
225}
226EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
227
228/**
229 * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
230 * @substream: PCM substream
231 *
232 * This function is deprecated and should not be used by new drivers, as its
233 * results may be unreliable.
234 */
235snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
236{
237 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
238 return bytes_to_frames(substream->runtime, prtd->pos);
239}
240EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
241
242/**
243 * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
244 * @substream: PCM substream
245 *
246 * This function can be used as the PCM pointer callback for dmaengine based PCM
247 * driver implementations.
248 */
249snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
250{
251 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
252 struct dma_tx_state state;
253 enum dma_status status;
254 unsigned int buf_size;
255 unsigned int pos = 0;
256
257 status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
258 if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
259 buf_size = snd_pcm_lib_buffer_bytes(substream);
260 if (state.residue > 0 && state.residue <= buf_size)
261 pos = buf_size - state.residue;
262 }
263
264 return bytes_to_frames(substream->runtime, pos);
265}
266EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
267
268/**
269 * snd_dmaengine_pcm_request_channel - Request channel for the dmaengine PCM
270 * @filter_fn: Filter function used to request the DMA channel
271 * @filter_data: Data passed to the DMA filter function
272 *
273 * Returns NULL or the requested DMA channel.
274 *
275 * This function request a DMA channel for usage with dmaengine PCM.
276 */
277struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn,
278 void *filter_data)
279{
280 dma_cap_mask_t mask;
281
282 dma_cap_zero(mask);
283 dma_cap_set(DMA_SLAVE, mask);
284 dma_cap_set(DMA_CYCLIC, mask);
285
286 return dma_request_channel(mask, filter_fn, filter_data);
287}
288EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_request_channel);
289
290/**
291 * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
292 * @substream: PCM substream
293 * @chan: DMA channel to use for data transfers
294 *
295 * Returns 0 on success, a negative error code otherwise.
296 *
297 * The function should usually be called from the pcm open callback. Note that
298 * this function will use private_data field of the substream's runtime. So it
299 * is not available to your pcm driver implementation.
300 */
301int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
302 struct dma_chan *chan)
303{
304 struct dmaengine_pcm_runtime_data *prtd;
305 int ret;
306
307 if (!chan)
308 return -ENXIO;
309
310 ret = snd_pcm_hw_constraint_integer(substream->runtime,
311 SNDRV_PCM_HW_PARAM_PERIODS);
312 if (ret < 0)
313 return ret;
314
315 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
316 if (!prtd)
317 return -ENOMEM;
318
319 prtd->dma_chan = chan;
320
321 substream->runtime->private_data = prtd;
322
323 return 0;
324}
325EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
326
327/**
328 * snd_dmaengine_pcm_open_request_chan - Open a dmaengine based PCM substream and request channel
329 * @substream: PCM substream
330 * @filter_fn: Filter function used to request the DMA channel
331 * @filter_data: Data passed to the DMA filter function
332 *
333 * Returns 0 on success, a negative error code otherwise.
334 *
335 * This function will request a DMA channel using the passed filter function and
336 * data. The function should usually be called from the pcm open callback. Note
337 * that this function will use private_data field of the substream's runtime. So
338 * it is not available to your pcm driver implementation.
339 */
340int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream *substream,
341 dma_filter_fn filter_fn, void *filter_data)
342{
343 return snd_dmaengine_pcm_open(substream,
344 snd_dmaengine_pcm_request_channel(filter_fn, filter_data));
345}
346EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open_request_chan);
347
348/**
349 * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
350 * @substream: PCM substream
351 */
352int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
353{
354 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
355
356 dmaengine_synchronize(prtd->dma_chan);
357 kfree(prtd);
358
359 return 0;
360}
361EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
362
363/**
364 * snd_dmaengine_pcm_release_chan_close - Close a dmaengine based PCM substream and release channel
365 * @substream: PCM substream
366 *
367 * Releases the DMA channel associated with the PCM substream.
368 */
369int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream)
370{
371 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
372
373 dmaengine_synchronize(prtd->dma_chan);
374 dma_release_channel(prtd->dma_chan);
375 kfree(prtd);
376
377 return 0;
378}
379EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close_release_chan);
380
381MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2012, Analog Devices Inc.
4 * Author: Lars-Peter Clausen <lars@metafoo.de>
5 *
6 * Based on:
7 * imx-pcm-dma-mx2.c, Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
8 * mxs-pcm.c, Copyright (C) 2011 Freescale Semiconductor, Inc.
9 * ep93xx-pcm.c, Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
10 * Copyright (C) 2006 Applied Data Systems
11 */
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/dmaengine.h>
15#include <linux/slab.h>
16#include <sound/pcm.h>
17#include <sound/pcm_params.h>
18#include <sound/soc.h>
19
20#include <sound/dmaengine_pcm.h>
21
22struct dmaengine_pcm_runtime_data {
23 struct dma_chan *dma_chan;
24 dma_cookie_t cookie;
25
26 unsigned int pos;
27};
28
29static inline struct dmaengine_pcm_runtime_data *substream_to_prtd(
30 const struct snd_pcm_substream *substream)
31{
32 return substream->runtime->private_data;
33}
34
35struct dma_chan *snd_dmaengine_pcm_get_chan(struct snd_pcm_substream *substream)
36{
37 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
38
39 return prtd->dma_chan;
40}
41EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_chan);
42
43/**
44 * snd_hwparams_to_dma_slave_config - Convert hw_params to dma_slave_config
45 * @substream: PCM substream
46 * @params: hw_params
47 * @slave_config: DMA slave config
48 *
49 * This function can be used to initialize a dma_slave_config from a substream
50 * and hw_params in a dmaengine based PCM driver implementation.
51 */
52int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream *substream,
53 const struct snd_pcm_hw_params *params,
54 struct dma_slave_config *slave_config)
55{
56 enum dma_slave_buswidth buswidth;
57 int bits;
58
59 bits = params_physical_width(params);
60 if (bits < 8 || bits > 64)
61 return -EINVAL;
62 else if (bits == 8)
63 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
64 else if (bits == 16)
65 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
66 else if (bits == 24)
67 buswidth = DMA_SLAVE_BUSWIDTH_3_BYTES;
68 else if (bits <= 32)
69 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
70 else
71 buswidth = DMA_SLAVE_BUSWIDTH_8_BYTES;
72
73 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
74 slave_config->direction = DMA_MEM_TO_DEV;
75 slave_config->dst_addr_width = buswidth;
76 } else {
77 slave_config->direction = DMA_DEV_TO_MEM;
78 slave_config->src_addr_width = buswidth;
79 }
80
81 slave_config->device_fc = false;
82
83 return 0;
84}
85EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
86
87/**
88 * snd_dmaengine_pcm_set_config_from_dai_data() - Initializes a dma slave config
89 * using DAI DMA data.
90 * @substream: PCM substream
91 * @dma_data: DAI DMA data
92 * @slave_config: DMA slave configuration
93 *
94 * Initializes the {dst,src}_addr, {dst,src}_maxburst, {dst,src}_addr_width and
95 * slave_id fields of the DMA slave config from the same fields of the DAI DMA
96 * data struct. The src and dst fields will be initialized depending on the
97 * direction of the substream. If the substream is a playback stream the dst
98 * fields will be initialized, if it is a capture stream the src fields will be
99 * initialized. The {dst,src}_addr_width field will only be initialized if the
100 * SND_DMAENGINE_PCM_DAI_FLAG_PACK flag is set or if the addr_width field of
101 * the DAI DMA data struct is not equal to DMA_SLAVE_BUSWIDTH_UNDEFINED. If
102 * both conditions are met the latter takes priority.
103 */
104void snd_dmaengine_pcm_set_config_from_dai_data(
105 const struct snd_pcm_substream *substream,
106 const struct snd_dmaengine_dai_dma_data *dma_data,
107 struct dma_slave_config *slave_config)
108{
109 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
110 slave_config->dst_addr = dma_data->addr;
111 slave_config->dst_maxburst = dma_data->maxburst;
112 if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK)
113 slave_config->dst_addr_width =
114 DMA_SLAVE_BUSWIDTH_UNDEFINED;
115 if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
116 slave_config->dst_addr_width = dma_data->addr_width;
117 } else {
118 slave_config->src_addr = dma_data->addr;
119 slave_config->src_maxburst = dma_data->maxburst;
120 if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK)
121 slave_config->src_addr_width =
122 DMA_SLAVE_BUSWIDTH_UNDEFINED;
123 if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
124 slave_config->src_addr_width = dma_data->addr_width;
125 }
126
127 slave_config->slave_id = dma_data->slave_id;
128 slave_config->peripheral_config = dma_data->peripheral_config;
129 slave_config->peripheral_size = dma_data->peripheral_size;
130}
131EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data);
132
133static void dmaengine_pcm_dma_complete(void *arg)
134{
135 struct snd_pcm_substream *substream = arg;
136 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
137
138 prtd->pos += snd_pcm_lib_period_bytes(substream);
139 if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
140 prtd->pos = 0;
141
142 snd_pcm_period_elapsed(substream);
143}
144
145static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
146{
147 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
148 struct dma_chan *chan = prtd->dma_chan;
149 struct dma_async_tx_descriptor *desc;
150 enum dma_transfer_direction direction;
151 unsigned long flags = DMA_CTRL_ACK;
152
153 direction = snd_pcm_substream_to_dma_direction(substream);
154
155 if (!substream->runtime->no_period_wakeup)
156 flags |= DMA_PREP_INTERRUPT;
157
158 prtd->pos = 0;
159 desc = dmaengine_prep_dma_cyclic(chan,
160 substream->runtime->dma_addr,
161 snd_pcm_lib_buffer_bytes(substream),
162 snd_pcm_lib_period_bytes(substream), direction, flags);
163
164 if (!desc)
165 return -ENOMEM;
166
167 desc->callback = dmaengine_pcm_dma_complete;
168 desc->callback_param = substream;
169 prtd->cookie = dmaengine_submit(desc);
170
171 return 0;
172}
173
174/**
175 * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
176 * @substream: PCM substream
177 * @cmd: Trigger command
178 *
179 * Returns 0 on success, a negative error code otherwise.
180 *
181 * This function can be used as the PCM trigger callback for dmaengine based PCM
182 * driver implementations.
183 */
184int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
185{
186 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
187 struct snd_pcm_runtime *runtime = substream->runtime;
188 int ret;
189
190 switch (cmd) {
191 case SNDRV_PCM_TRIGGER_START:
192 ret = dmaengine_pcm_prepare_and_submit(substream);
193 if (ret)
194 return ret;
195 dma_async_issue_pending(prtd->dma_chan);
196 break;
197 case SNDRV_PCM_TRIGGER_RESUME:
198 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
199 dmaengine_resume(prtd->dma_chan);
200 break;
201 case SNDRV_PCM_TRIGGER_SUSPEND:
202 if (runtime->info & SNDRV_PCM_INFO_PAUSE)
203 dmaengine_pause(prtd->dma_chan);
204 else
205 dmaengine_terminate_async(prtd->dma_chan);
206 break;
207 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
208 dmaengine_pause(prtd->dma_chan);
209 break;
210 case SNDRV_PCM_TRIGGER_STOP:
211 dmaengine_terminate_async(prtd->dma_chan);
212 break;
213 default:
214 return -EINVAL;
215 }
216
217 return 0;
218}
219EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
220
221/**
222 * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
223 * @substream: PCM substream
224 *
225 * This function is deprecated and should not be used by new drivers, as its
226 * results may be unreliable.
227 */
228snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
229{
230 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
231 return bytes_to_frames(substream->runtime, prtd->pos);
232}
233EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
234
235/**
236 * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
237 * @substream: PCM substream
238 *
239 * This function can be used as the PCM pointer callback for dmaengine based PCM
240 * driver implementations.
241 */
242snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
243{
244 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
245 struct snd_pcm_runtime *runtime = substream->runtime;
246 struct dma_tx_state state;
247 enum dma_status status;
248 unsigned int buf_size;
249 unsigned int pos = 0;
250
251 status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
252 if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
253 buf_size = snd_pcm_lib_buffer_bytes(substream);
254 if (state.residue > 0 && state.residue <= buf_size)
255 pos = buf_size - state.residue;
256
257 runtime->delay = bytes_to_frames(runtime,
258 state.in_flight_bytes);
259 }
260
261 return bytes_to_frames(runtime, pos);
262}
263EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
264
265/**
266 * snd_dmaengine_pcm_request_channel - Request channel for the dmaengine PCM
267 * @filter_fn: Filter function used to request the DMA channel
268 * @filter_data: Data passed to the DMA filter function
269 *
270 * Returns NULL or the requested DMA channel.
271 *
272 * This function request a DMA channel for usage with dmaengine PCM.
273 */
274struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn,
275 void *filter_data)
276{
277 dma_cap_mask_t mask;
278
279 dma_cap_zero(mask);
280 dma_cap_set(DMA_SLAVE, mask);
281 dma_cap_set(DMA_CYCLIC, mask);
282
283 return dma_request_channel(mask, filter_fn, filter_data);
284}
285EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_request_channel);
286
287/**
288 * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
289 * @substream: PCM substream
290 * @chan: DMA channel to use for data transfers
291 *
292 * Returns 0 on success, a negative error code otherwise.
293 *
294 * The function should usually be called from the pcm open callback. Note that
295 * this function will use private_data field of the substream's runtime. So it
296 * is not available to your pcm driver implementation.
297 */
298int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
299 struct dma_chan *chan)
300{
301 struct dmaengine_pcm_runtime_data *prtd;
302 int ret;
303
304 if (!chan)
305 return -ENXIO;
306
307 ret = snd_pcm_hw_constraint_integer(substream->runtime,
308 SNDRV_PCM_HW_PARAM_PERIODS);
309 if (ret < 0)
310 return ret;
311
312 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
313 if (!prtd)
314 return -ENOMEM;
315
316 prtd->dma_chan = chan;
317
318 substream->runtime->private_data = prtd;
319
320 return 0;
321}
322EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
323
324/**
325 * snd_dmaengine_pcm_open_request_chan - Open a dmaengine based PCM substream and request channel
326 * @substream: PCM substream
327 * @filter_fn: Filter function used to request the DMA channel
328 * @filter_data: Data passed to the DMA filter function
329 *
330 * Returns 0 on success, a negative error code otherwise.
331 *
332 * This function will request a DMA channel using the passed filter function and
333 * data. The function should usually be called from the pcm open callback. Note
334 * that this function will use private_data field of the substream's runtime. So
335 * it is not available to your pcm driver implementation.
336 */
337int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream *substream,
338 dma_filter_fn filter_fn, void *filter_data)
339{
340 return snd_dmaengine_pcm_open(substream,
341 snd_dmaengine_pcm_request_channel(filter_fn, filter_data));
342}
343EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open_request_chan);
344
345/**
346 * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
347 * @substream: PCM substream
348 */
349int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
350{
351 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
352
353 dmaengine_synchronize(prtd->dma_chan);
354 kfree(prtd);
355
356 return 0;
357}
358EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
359
360/**
361 * snd_dmaengine_pcm_close_release_chan - Close a dmaengine based PCM
362 * substream and release channel
363 * @substream: PCM substream
364 *
365 * Releases the DMA channel associated with the PCM substream.
366 */
367int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream)
368{
369 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
370
371 dmaengine_synchronize(prtd->dma_chan);
372 dma_release_channel(prtd->dma_chan);
373 kfree(prtd);
374
375 return 0;
376}
377EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close_release_chan);
378
379/**
380 * snd_dmaengine_pcm_refine_runtime_hwparams - Refine runtime hw params
381 * @substream: PCM substream
382 * @dma_data: DAI DMA data
383 * @hw: PCM hw params
384 * @chan: DMA channel to use for data transfers
385 *
386 * Returns 0 on success, a negative error code otherwise.
387 *
388 * This function will query DMA capability, then refine the pcm hardware
389 * parameters.
390 */
391int snd_dmaengine_pcm_refine_runtime_hwparams(
392 struct snd_pcm_substream *substream,
393 struct snd_dmaengine_dai_dma_data *dma_data,
394 struct snd_pcm_hardware *hw,
395 struct dma_chan *chan)
396{
397 struct dma_slave_caps dma_caps;
398 u32 addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
399 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
400 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
401 snd_pcm_format_t i;
402 int ret = 0;
403
404 if (!hw || !chan || !dma_data)
405 return -EINVAL;
406
407 ret = dma_get_slave_caps(chan, &dma_caps);
408 if (ret == 0) {
409 if (dma_caps.cmd_pause && dma_caps.cmd_resume)
410 hw->info |= SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME;
411 if (dma_caps.residue_granularity <= DMA_RESIDUE_GRANULARITY_SEGMENT)
412 hw->info |= SNDRV_PCM_INFO_BATCH;
413
414 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
415 addr_widths = dma_caps.dst_addr_widths;
416 else
417 addr_widths = dma_caps.src_addr_widths;
418 }
419
420 /*
421 * If SND_DMAENGINE_PCM_DAI_FLAG_PACK is set keep
422 * hw.formats set to 0, meaning no restrictions are in place.
423 * In this case it's the responsibility of the DAI driver to
424 * provide the supported format information.
425 */
426 if (!(dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK))
427 /*
428 * Prepare formats mask for valid/allowed sample types. If the
429 * dma does not have support for the given physical word size,
430 * it needs to be masked out so user space can not use the
431 * format which produces corrupted audio.
432 * In case the dma driver does not implement the slave_caps the
433 * default assumption is that it supports 1, 2 and 4 bytes
434 * widths.
435 */
436 pcm_for_each_format(i) {
437 int bits = snd_pcm_format_physical_width(i);
438
439 /*
440 * Enable only samples with DMA supported physical
441 * widths
442 */
443 switch (bits) {
444 case 8:
445 case 16:
446 case 24:
447 case 32:
448 case 64:
449 if (addr_widths & (1 << (bits / 8)))
450 hw->formats |= pcm_format_to_bits(i);
451 break;
452 default:
453 /* Unsupported types */
454 break;
455 }
456 }
457
458 return ret;
459}
460EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_refine_runtime_hwparams);
461
462MODULE_LICENSE("GPL");