xref: /linux/sound/core/pcm_dmaengine.c (revision 73fe01cfb3babff01748a9fbc95cc3ea2079cc7f)
1b7ae6f31SDaniel Mack /*
2b7ae6f31SDaniel Mack  *  Copyright (C) 2012, Analog Devices Inc.
3b7ae6f31SDaniel Mack  *	Author: Lars-Peter Clausen <lars@metafoo.de>
4b7ae6f31SDaniel Mack  *
5b7ae6f31SDaniel Mack  *  Based on:
6b7ae6f31SDaniel Mack  *	imx-pcm-dma-mx2.c, Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
7b7ae6f31SDaniel Mack  *	mxs-pcm.c, Copyright (C) 2011 Freescale Semiconductor, Inc.
8b7ae6f31SDaniel Mack  *	ep93xx-pcm.c, Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
9b7ae6f31SDaniel Mack  *		      Copyright (C) 2006 Applied Data Systems
10b7ae6f31SDaniel Mack  *
11b7ae6f31SDaniel Mack  *  This program is free software; you can redistribute it and/or modify it
12b7ae6f31SDaniel Mack  *  under  the terms of the GNU General  Public License as published by the
13b7ae6f31SDaniel Mack  *  Free Software Foundation;  either version 2 of the License, or (at your
14b7ae6f31SDaniel Mack  *  option) any later version.
15b7ae6f31SDaniel Mack  *
16b7ae6f31SDaniel Mack  *  You should have received a copy of the GNU General Public License along
17b7ae6f31SDaniel Mack  *  with this program; if not, write to the Free Software Foundation, Inc.,
18b7ae6f31SDaniel Mack  *  675 Mass Ave, Cambridge, MA 02139, USA.
19b7ae6f31SDaniel Mack  *
20b7ae6f31SDaniel Mack  */
21b7ae6f31SDaniel Mack #include <linux/module.h>
22b7ae6f31SDaniel Mack #include <linux/init.h>
23b7ae6f31SDaniel Mack #include <linux/dmaengine.h>
24b7ae6f31SDaniel Mack #include <linux/slab.h>
25b7ae6f31SDaniel Mack #include <sound/pcm.h>
26b7ae6f31SDaniel Mack #include <sound/pcm_params.h>
27b7ae6f31SDaniel Mack #include <sound/soc.h>
28b7ae6f31SDaniel Mack 
29b7ae6f31SDaniel Mack #include <sound/dmaengine_pcm.h>
30b7ae6f31SDaniel Mack 
31b7ae6f31SDaniel Mack struct dmaengine_pcm_runtime_data {
32b7ae6f31SDaniel Mack 	struct dma_chan *dma_chan;
33b7ae6f31SDaniel Mack 	dma_cookie_t cookie;
34b7ae6f31SDaniel Mack 
35b7ae6f31SDaniel Mack 	unsigned int pos;
36b7ae6f31SDaniel Mack };
37b7ae6f31SDaniel Mack 
38b7ae6f31SDaniel Mack static inline struct dmaengine_pcm_runtime_data *substream_to_prtd(
39b7ae6f31SDaniel Mack 	const struct snd_pcm_substream *substream)
40b7ae6f31SDaniel Mack {
41b7ae6f31SDaniel Mack 	return substream->runtime->private_data;
42b7ae6f31SDaniel Mack }
43b7ae6f31SDaniel Mack 
44b7ae6f31SDaniel Mack struct dma_chan *snd_dmaengine_pcm_get_chan(struct snd_pcm_substream *substream)
45b7ae6f31SDaniel Mack {
46b7ae6f31SDaniel Mack 	struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
47b7ae6f31SDaniel Mack 
48b7ae6f31SDaniel Mack 	return prtd->dma_chan;
49b7ae6f31SDaniel Mack }
50b7ae6f31SDaniel Mack EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_chan);
51b7ae6f31SDaniel Mack 
52b7ae6f31SDaniel Mack /**
53b7ae6f31SDaniel Mack  * snd_hwparams_to_dma_slave_config - Convert hw_params to dma_slave_config
54b7ae6f31SDaniel Mack  * @substream: PCM substream
55b7ae6f31SDaniel Mack  * @params: hw_params
56b7ae6f31SDaniel Mack  * @slave_config: DMA slave config
57b7ae6f31SDaniel Mack  *
58b7ae6f31SDaniel Mack  * This function can be used to initialize a dma_slave_config from a substream
59b7ae6f31SDaniel Mack  * and hw_params in a dmaengine based PCM driver implementation.
60b7ae6f31SDaniel Mack  */
61b7ae6f31SDaniel Mack int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream *substream,
62b7ae6f31SDaniel Mack 	const struct snd_pcm_hw_params *params,
63b7ae6f31SDaniel Mack 	struct dma_slave_config *slave_config)
64b7ae6f31SDaniel Mack {
65b7ae6f31SDaniel Mack 	enum dma_slave_buswidth buswidth;
66a655f75cSTakashi Iwai 	int bits;
67b7ae6f31SDaniel Mack 
68732814c8SPeter Ujfalusi 	bits = params_physical_width(params);
69a655f75cSTakashi Iwai 	if (bits < 8 || bits > 64)
70b7ae6f31SDaniel Mack 		return -EINVAL;
71a655f75cSTakashi Iwai 	else if (bits == 8)
72a655f75cSTakashi Iwai 		buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
73a655f75cSTakashi Iwai 	else if (bits == 16)
74a655f75cSTakashi Iwai 		buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
7575f850fcSPeter Ujfalusi 	else if (bits == 24)
7675f850fcSPeter Ujfalusi 		buswidth = DMA_SLAVE_BUSWIDTH_3_BYTES;
77a655f75cSTakashi Iwai 	else if (bits <= 32)
78a655f75cSTakashi Iwai 		buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
79a655f75cSTakashi Iwai 	else
80a655f75cSTakashi Iwai 		buswidth = DMA_SLAVE_BUSWIDTH_8_BYTES;
81b7ae6f31SDaniel Mack 
82b7ae6f31SDaniel Mack 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
83b7ae6f31SDaniel Mack 		slave_config->direction = DMA_MEM_TO_DEV;
84b7ae6f31SDaniel Mack 		slave_config->dst_addr_width = buswidth;
85b7ae6f31SDaniel Mack 	} else {
86b7ae6f31SDaniel Mack 		slave_config->direction = DMA_DEV_TO_MEM;
87b7ae6f31SDaniel Mack 		slave_config->src_addr_width = buswidth;
88b7ae6f31SDaniel Mack 	}
89b7ae6f31SDaniel Mack 
90b7ae6f31SDaniel Mack 	slave_config->device_fc = false;
91b7ae6f31SDaniel Mack 
92b7ae6f31SDaniel Mack 	return 0;
93b7ae6f31SDaniel Mack }
94b7ae6f31SDaniel Mack EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
95b7ae6f31SDaniel Mack 
96b7ae6f31SDaniel Mack /**
97b7ae6f31SDaniel Mack  * snd_dmaengine_pcm_set_config_from_dai_data() - Initializes a dma slave config
98b7ae6f31SDaniel Mack  *  using DAI DMA data.
99b7ae6f31SDaniel Mack  * @substream: PCM substream
100b7ae6f31SDaniel Mack  * @dma_data: DAI DMA data
101b7ae6f31SDaniel Mack  * @slave_config: DMA slave configuration
102b7ae6f31SDaniel Mack  *
103b7ae6f31SDaniel Mack  * Initializes the {dst,src}_addr, {dst,src}_maxburst, {dst,src}_addr_width and
104b7ae6f31SDaniel Mack  * slave_id fields of the DMA slave config from the same fields of the DAI DMA
105b7ae6f31SDaniel Mack  * data struct. The src and dst fields will be initialized depending on the
106b7ae6f31SDaniel Mack  * direction of the substream. If the substream is a playback stream the dst
107b7ae6f31SDaniel Mack  * fields will be initialized, if it is a capture stream the src fields will be
108b7ae6f31SDaniel Mack  * initialized. The {dst,src}_addr_width field will only be initialized if the
109*73fe01cfSMatthias Reichl  * SND_DMAENGINE_PCM_DAI_FLAG_PACK flag is set or if the addr_width field of
110*73fe01cfSMatthias Reichl  * the DAI DMA data struct is not equal to DMA_SLAVE_BUSWIDTH_UNDEFINED. If
111*73fe01cfSMatthias Reichl  * both conditions are met the latter takes priority.
112b7ae6f31SDaniel Mack  */
113b7ae6f31SDaniel Mack void snd_dmaengine_pcm_set_config_from_dai_data(
114b7ae6f31SDaniel Mack 	const struct snd_pcm_substream *substream,
115b7ae6f31SDaniel Mack 	const struct snd_dmaengine_dai_dma_data *dma_data,
116b7ae6f31SDaniel Mack 	struct dma_slave_config *slave_config)
117b7ae6f31SDaniel Mack {
118b7ae6f31SDaniel Mack 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
119b7ae6f31SDaniel Mack 		slave_config->dst_addr = dma_data->addr;
120b7ae6f31SDaniel Mack 		slave_config->dst_maxburst = dma_data->maxburst;
121*73fe01cfSMatthias Reichl 		if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK)
122*73fe01cfSMatthias Reichl 			slave_config->dst_addr_width =
123*73fe01cfSMatthias Reichl 				DMA_SLAVE_BUSWIDTH_UNDEFINED;
124b7ae6f31SDaniel Mack 		if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
125b7ae6f31SDaniel Mack 			slave_config->dst_addr_width = dma_data->addr_width;
126b7ae6f31SDaniel Mack 	} else {
127b7ae6f31SDaniel Mack 		slave_config->src_addr = dma_data->addr;
128b7ae6f31SDaniel Mack 		slave_config->src_maxburst = dma_data->maxburst;
129*73fe01cfSMatthias Reichl 		if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK)
130*73fe01cfSMatthias Reichl 			slave_config->src_addr_width =
131*73fe01cfSMatthias Reichl 				DMA_SLAVE_BUSWIDTH_UNDEFINED;
132b7ae6f31SDaniel Mack 		if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
133b7ae6f31SDaniel Mack 			slave_config->src_addr_width = dma_data->addr_width;
134b7ae6f31SDaniel Mack 	}
135b7ae6f31SDaniel Mack 
136b7ae6f31SDaniel Mack 	slave_config->slave_id = dma_data->slave_id;
137b7ae6f31SDaniel Mack }
138b7ae6f31SDaniel Mack EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data);
139b7ae6f31SDaniel Mack 
140b7ae6f31SDaniel Mack static void dmaengine_pcm_dma_complete(void *arg)
141b7ae6f31SDaniel Mack {
142b7ae6f31SDaniel Mack 	struct snd_pcm_substream *substream = arg;
143b7ae6f31SDaniel Mack 	struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
144b7ae6f31SDaniel Mack 
145b7ae6f31SDaniel Mack 	prtd->pos += snd_pcm_lib_period_bytes(substream);
146b7ae6f31SDaniel Mack 	if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
147b7ae6f31SDaniel Mack 		prtd->pos = 0;
148b7ae6f31SDaniel Mack 
149b7ae6f31SDaniel Mack 	snd_pcm_period_elapsed(substream);
150b7ae6f31SDaniel Mack }
151b7ae6f31SDaniel Mack 
152b7ae6f31SDaniel Mack static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
153b7ae6f31SDaniel Mack {
154b7ae6f31SDaniel Mack 	struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
155b7ae6f31SDaniel Mack 	struct dma_chan *chan = prtd->dma_chan;
156b7ae6f31SDaniel Mack 	struct dma_async_tx_descriptor *desc;
157b7ae6f31SDaniel Mack 	enum dma_transfer_direction direction;
158b7ae6f31SDaniel Mack 	unsigned long flags = DMA_CTRL_ACK;
159b7ae6f31SDaniel Mack 
160b7ae6f31SDaniel Mack 	direction = snd_pcm_substream_to_dma_direction(substream);
161b7ae6f31SDaniel Mack 
162b7ae6f31SDaniel Mack 	if (!substream->runtime->no_period_wakeup)
163b7ae6f31SDaniel Mack 		flags |= DMA_PREP_INTERRUPT;
164b7ae6f31SDaniel Mack 
165b7ae6f31SDaniel Mack 	prtd->pos = 0;
166b7ae6f31SDaniel Mack 	desc = dmaengine_prep_dma_cyclic(chan,
167b7ae6f31SDaniel Mack 		substream->runtime->dma_addr,
168b7ae6f31SDaniel Mack 		snd_pcm_lib_buffer_bytes(substream),
169b7ae6f31SDaniel Mack 		snd_pcm_lib_period_bytes(substream), direction, flags);
170b7ae6f31SDaniel Mack 
171b7ae6f31SDaniel Mack 	if (!desc)
172b7ae6f31SDaniel Mack 		return -ENOMEM;
173b7ae6f31SDaniel Mack 
174b7ae6f31SDaniel Mack 	desc->callback = dmaengine_pcm_dma_complete;
175b7ae6f31SDaniel Mack 	desc->callback_param = substream;
176b7ae6f31SDaniel Mack 	prtd->cookie = dmaengine_submit(desc);
177b7ae6f31SDaniel Mack 
178b7ae6f31SDaniel Mack 	return 0;
179b7ae6f31SDaniel Mack }
180b7ae6f31SDaniel Mack 
181b7ae6f31SDaniel Mack /**
182b7ae6f31SDaniel Mack  * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
183b7ae6f31SDaniel Mack  * @substream: PCM substream
184b7ae6f31SDaniel Mack  * @cmd: Trigger command
185b7ae6f31SDaniel Mack  *
186b7ae6f31SDaniel Mack  * Returns 0 on success, a negative error code otherwise.
187b7ae6f31SDaniel Mack  *
188b7ae6f31SDaniel Mack  * This function can be used as the PCM trigger callback for dmaengine based PCM
189b7ae6f31SDaniel Mack  * driver implementations.
190b7ae6f31SDaniel Mack  */
191b7ae6f31SDaniel Mack int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
192b7ae6f31SDaniel Mack {
193b7ae6f31SDaniel Mack 	struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
19402fb05a5STushar Behera 	struct snd_pcm_runtime *runtime = substream->runtime;
195b7ae6f31SDaniel Mack 	int ret;
196b7ae6f31SDaniel Mack 
197b7ae6f31SDaniel Mack 	switch (cmd) {
198b7ae6f31SDaniel Mack 	case SNDRV_PCM_TRIGGER_START:
199b7ae6f31SDaniel Mack 		ret = dmaengine_pcm_prepare_and_submit(substream);
200b7ae6f31SDaniel Mack 		if (ret)
201b7ae6f31SDaniel Mack 			return ret;
202b7ae6f31SDaniel Mack 		dma_async_issue_pending(prtd->dma_chan);
203b7ae6f31SDaniel Mack 		break;
204b7ae6f31SDaniel Mack 	case SNDRV_PCM_TRIGGER_RESUME:
205b7ae6f31SDaniel Mack 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
206b7ae6f31SDaniel Mack 		dmaengine_resume(prtd->dma_chan);
207b7ae6f31SDaniel Mack 		break;
208b7ae6f31SDaniel Mack 	case SNDRV_PCM_TRIGGER_SUSPEND:
20902fb05a5STushar Behera 		if (runtime->info & SNDRV_PCM_INFO_PAUSE)
21002fb05a5STushar Behera 			dmaengine_pause(prtd->dma_chan);
21102fb05a5STushar Behera 		else
212bc0e7345SLars-Peter Clausen 			dmaengine_terminate_async(prtd->dma_chan);
21302fb05a5STushar Behera 		break;
214b7ae6f31SDaniel Mack 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
215b7ae6f31SDaniel Mack 		dmaengine_pause(prtd->dma_chan);
216b7ae6f31SDaniel Mack 		break;
217b7ae6f31SDaniel Mack 	case SNDRV_PCM_TRIGGER_STOP:
218bc0e7345SLars-Peter Clausen 		dmaengine_terminate_async(prtd->dma_chan);
219b7ae6f31SDaniel Mack 		break;
220b7ae6f31SDaniel Mack 	default:
221b7ae6f31SDaniel Mack 		return -EINVAL;
222b7ae6f31SDaniel Mack 	}
223b7ae6f31SDaniel Mack 
224b7ae6f31SDaniel Mack 	return 0;
225b7ae6f31SDaniel Mack }
226b7ae6f31SDaniel Mack EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
227b7ae6f31SDaniel Mack 
228b7ae6f31SDaniel Mack /**
229b7ae6f31SDaniel Mack  * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
230b7ae6f31SDaniel Mack  * @substream: PCM substream
231b7ae6f31SDaniel Mack  *
232b7ae6f31SDaniel Mack  * This function is deprecated and should not be used by new drivers, as its
233b7ae6f31SDaniel Mack  * results may be unreliable.
234b7ae6f31SDaniel Mack  */
235b7ae6f31SDaniel Mack snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
236b7ae6f31SDaniel Mack {
237b7ae6f31SDaniel Mack 	struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
238b7ae6f31SDaniel Mack 	return bytes_to_frames(substream->runtime, prtd->pos);
239b7ae6f31SDaniel Mack }
240b7ae6f31SDaniel Mack EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
241b7ae6f31SDaniel Mack 
242b7ae6f31SDaniel Mack /**
243b7ae6f31SDaniel Mack  * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
244b7ae6f31SDaniel Mack  * @substream: PCM substream
245b7ae6f31SDaniel Mack  *
246b7ae6f31SDaniel Mack  * This function can be used as the PCM pointer callback for dmaengine based PCM
247b7ae6f31SDaniel Mack  * driver implementations.
248b7ae6f31SDaniel Mack  */
249b7ae6f31SDaniel Mack snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
250b7ae6f31SDaniel Mack {
251b7ae6f31SDaniel Mack 	struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
252b7ae6f31SDaniel Mack 	struct dma_tx_state state;
253b7ae6f31SDaniel Mack 	enum dma_status status;
254b7ae6f31SDaniel Mack 	unsigned int buf_size;
255b7ae6f31SDaniel Mack 	unsigned int pos = 0;
256b7ae6f31SDaniel Mack 
257b7ae6f31SDaniel Mack 	status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
258b7ae6f31SDaniel Mack 	if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
259b7ae6f31SDaniel Mack 		buf_size = snd_pcm_lib_buffer_bytes(substream);
260b7ae6f31SDaniel Mack 		if (state.residue > 0 && state.residue <= buf_size)
261b7ae6f31SDaniel Mack 			pos = buf_size - state.residue;
262b7ae6f31SDaniel Mack 	}
263b7ae6f31SDaniel Mack 
264b7ae6f31SDaniel Mack 	return bytes_to_frames(substream->runtime, pos);
265b7ae6f31SDaniel Mack }
266b7ae6f31SDaniel Mack EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
267b7ae6f31SDaniel Mack 
268b7ae6f31SDaniel Mack /**
269b7ae6f31SDaniel Mack  * snd_dmaengine_pcm_request_channel - Request channel for the dmaengine PCM
270b7ae6f31SDaniel Mack  * @filter_fn: Filter function used to request the DMA channel
271b7ae6f31SDaniel Mack  * @filter_data: Data passed to the DMA filter function
272b7ae6f31SDaniel Mack  *
273b7ae6f31SDaniel Mack  * Returns NULL or the requested DMA channel.
274b7ae6f31SDaniel Mack  *
275b7ae6f31SDaniel Mack  * This function request a DMA channel for usage with dmaengine PCM.
276b7ae6f31SDaniel Mack  */
277b7ae6f31SDaniel Mack struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn,
278b7ae6f31SDaniel Mack 	void *filter_data)
279b7ae6f31SDaniel Mack {
280b7ae6f31SDaniel Mack 	dma_cap_mask_t mask;
281b7ae6f31SDaniel Mack 
282b7ae6f31SDaniel Mack 	dma_cap_zero(mask);
283b7ae6f31SDaniel Mack 	dma_cap_set(DMA_SLAVE, mask);
284b7ae6f31SDaniel Mack 	dma_cap_set(DMA_CYCLIC, mask);
285b7ae6f31SDaniel Mack 
286b7ae6f31SDaniel Mack 	return dma_request_channel(mask, filter_fn, filter_data);
287b7ae6f31SDaniel Mack }
288b7ae6f31SDaniel Mack EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_request_channel);
289b7ae6f31SDaniel Mack 
290b7ae6f31SDaniel Mack /**
291b7ae6f31SDaniel Mack  * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
292b7ae6f31SDaniel Mack  * @substream: PCM substream
293b7ae6f31SDaniel Mack  * @chan: DMA channel to use for data transfers
294b7ae6f31SDaniel Mack  *
295b7ae6f31SDaniel Mack  * Returns 0 on success, a negative error code otherwise.
296b7ae6f31SDaniel Mack  *
297b7ae6f31SDaniel Mack  * The function should usually be called from the pcm open callback. Note that
298b7ae6f31SDaniel Mack  * this function will use private_data field of the substream's runtime. So it
2991a6ab46fSMasanari Iida  * is not available to your pcm driver implementation.
300b7ae6f31SDaniel Mack  */
301b7ae6f31SDaniel Mack int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
302b7ae6f31SDaniel Mack 	struct dma_chan *chan)
303b7ae6f31SDaniel Mack {
304b7ae6f31SDaniel Mack 	struct dmaengine_pcm_runtime_data *prtd;
305b7ae6f31SDaniel Mack 	int ret;
306b7ae6f31SDaniel Mack 
307b7ae6f31SDaniel Mack 	if (!chan)
308b7ae6f31SDaniel Mack 		return -ENXIO;
309b7ae6f31SDaniel Mack 
310b7ae6f31SDaniel Mack 	ret = snd_pcm_hw_constraint_integer(substream->runtime,
311b7ae6f31SDaniel Mack 					    SNDRV_PCM_HW_PARAM_PERIODS);
312b7ae6f31SDaniel Mack 	if (ret < 0)
313b7ae6f31SDaniel Mack 		return ret;
314b7ae6f31SDaniel Mack 
315b7ae6f31SDaniel Mack 	prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
316b7ae6f31SDaniel Mack 	if (!prtd)
317b7ae6f31SDaniel Mack 		return -ENOMEM;
318b7ae6f31SDaniel Mack 
319b7ae6f31SDaniel Mack 	prtd->dma_chan = chan;
320b7ae6f31SDaniel Mack 
321b7ae6f31SDaniel Mack 	substream->runtime->private_data = prtd;
322b7ae6f31SDaniel Mack 
323b7ae6f31SDaniel Mack 	return 0;
324b7ae6f31SDaniel Mack }
325b7ae6f31SDaniel Mack EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
326b7ae6f31SDaniel Mack 
327b7ae6f31SDaniel Mack /**
328b7ae6f31SDaniel Mack  * snd_dmaengine_pcm_open_request_chan - Open a dmaengine based PCM substream and request channel
329b7ae6f31SDaniel Mack  * @substream: PCM substream
330b7ae6f31SDaniel Mack  * @filter_fn: Filter function used to request the DMA channel
331b7ae6f31SDaniel Mack  * @filter_data: Data passed to the DMA filter function
332b7ae6f31SDaniel Mack  *
333b7ae6f31SDaniel Mack  * Returns 0 on success, a negative error code otherwise.
334b7ae6f31SDaniel Mack  *
335b7ae6f31SDaniel Mack  * This function will request a DMA channel using the passed filter function and
336b7ae6f31SDaniel Mack  * data. The function should usually be called from the pcm open callback. Note
337b7ae6f31SDaniel Mack  * that this function will use private_data field of the substream's runtime. So
3381a6ab46fSMasanari Iida  * it is not available to your pcm driver implementation.
339b7ae6f31SDaniel Mack  */
340b7ae6f31SDaniel Mack int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream *substream,
341b7ae6f31SDaniel Mack 	dma_filter_fn filter_fn, void *filter_data)
342b7ae6f31SDaniel Mack {
343b7ae6f31SDaniel Mack 	return snd_dmaengine_pcm_open(substream,
344b7ae6f31SDaniel Mack 		    snd_dmaengine_pcm_request_channel(filter_fn, filter_data));
345b7ae6f31SDaniel Mack }
346b7ae6f31SDaniel Mack EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open_request_chan);
347b7ae6f31SDaniel Mack 
348b7ae6f31SDaniel Mack /**
349b7ae6f31SDaniel Mack  * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
350b7ae6f31SDaniel Mack  * @substream: PCM substream
351b7ae6f31SDaniel Mack  */
352b7ae6f31SDaniel Mack int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
353b7ae6f31SDaniel Mack {
354b7ae6f31SDaniel Mack 	struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
355b7ae6f31SDaniel Mack 
356bc0e7345SLars-Peter Clausen 	dmaengine_synchronize(prtd->dma_chan);
357b7ae6f31SDaniel Mack 	kfree(prtd);
358b7ae6f31SDaniel Mack 
359b7ae6f31SDaniel Mack 	return 0;
360b7ae6f31SDaniel Mack }
361b7ae6f31SDaniel Mack EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
362b7ae6f31SDaniel Mack 
363b7ae6f31SDaniel Mack /**
364b7ae6f31SDaniel Mack  * snd_dmaengine_pcm_release_chan_close - Close a dmaengine based PCM substream and release channel
365b7ae6f31SDaniel Mack  * @substream: PCM substream
366b7ae6f31SDaniel Mack  *
367b7ae6f31SDaniel Mack  * Releases the DMA channel associated with the PCM substream.
368b7ae6f31SDaniel Mack  */
369b7ae6f31SDaniel Mack int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream)
370b7ae6f31SDaniel Mack {
371b7ae6f31SDaniel Mack 	struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
372b7ae6f31SDaniel Mack 
373bc0e7345SLars-Peter Clausen 	dmaengine_synchronize(prtd->dma_chan);
374b7ae6f31SDaniel Mack 	dma_release_channel(prtd->dma_chan);
375bc0e7345SLars-Peter Clausen 	kfree(prtd);
376b7ae6f31SDaniel Mack 
377bc0e7345SLars-Peter Clausen 	return 0;
378b7ae6f31SDaniel Mack }
379b7ae6f31SDaniel Mack EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close_release_chan);
380b7ae6f31SDaniel Mack 
381b7ae6f31SDaniel Mack MODULE_LICENSE("GPL");
382