Linux Audio

Check our new training course

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