xref: /openbmc/linux/sound/core/pcm_dmaengine.c (revision 3b73c45e)
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 
22 struct dmaengine_pcm_runtime_data {
23 	struct dma_chan *dma_chan;
24 	dma_cookie_t cookie;
25 
26 	unsigned int pos;
27 };
28 
29 static 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 
35 struct 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 }
41 EXPORT_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  */
54 int 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 }
87 EXPORT_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  */
106 void 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 }
132 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data);
133 
134 static void dmaengine_pcm_dma_complete(void *arg)
135 {
136 	struct snd_pcm_substream *substream = arg;
137 	struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
138 
139 	prtd->pos += snd_pcm_lib_period_bytes(substream);
140 	if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
141 		prtd->pos = 0;
142 
143 	snd_pcm_period_elapsed(substream);
144 }
145 
146 static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
147 {
148 	struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
149 	struct dma_chan *chan = prtd->dma_chan;
150 	struct dma_async_tx_descriptor *desc;
151 	enum dma_transfer_direction direction;
152 	unsigned long flags = DMA_CTRL_ACK;
153 
154 	direction = snd_pcm_substream_to_dma_direction(substream);
155 
156 	if (!substream->runtime->no_period_wakeup)
157 		flags |= DMA_PREP_INTERRUPT;
158 
159 	prtd->pos = 0;
160 	desc = dmaengine_prep_dma_cyclic(chan,
161 		substream->runtime->dma_addr,
162 		snd_pcm_lib_buffer_bytes(substream),
163 		snd_pcm_lib_period_bytes(substream), direction, flags);
164 
165 	if (!desc)
166 		return -ENOMEM;
167 
168 	desc->callback = dmaengine_pcm_dma_complete;
169 	desc->callback_param = substream;
170 	prtd->cookie = dmaengine_submit(desc);
171 
172 	return 0;
173 }
174 
175 /**
176  * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
177  * @substream: PCM substream
178  * @cmd: Trigger command
179  *
180  * This function can be used as the PCM trigger callback for dmaengine based PCM
181  * driver implementations.
182  *
183  * Return: 0 on success, a negative error code otherwise
184  */
185 int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
186 {
187 	struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
188 	struct snd_pcm_runtime *runtime = substream->runtime;
189 	int ret;
190 
191 	switch (cmd) {
192 	case SNDRV_PCM_TRIGGER_START:
193 		ret = dmaengine_pcm_prepare_and_submit(substream);
194 		if (ret)
195 			return ret;
196 		dma_async_issue_pending(prtd->dma_chan);
197 		break;
198 	case SNDRV_PCM_TRIGGER_RESUME:
199 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
200 		dmaengine_resume(prtd->dma_chan);
201 		break;
202 	case SNDRV_PCM_TRIGGER_SUSPEND:
203 		if (runtime->info & SNDRV_PCM_INFO_PAUSE)
204 			dmaengine_pause(prtd->dma_chan);
205 		else
206 			dmaengine_terminate_async(prtd->dma_chan);
207 		break;
208 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
209 		dmaengine_pause(prtd->dma_chan);
210 		break;
211 	case SNDRV_PCM_TRIGGER_STOP:
212 		dmaengine_terminate_async(prtd->dma_chan);
213 		break;
214 	default:
215 		return -EINVAL;
216 	}
217 
218 	return 0;
219 }
220 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
221 
222 /**
223  * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
224  * @substream: PCM substream
225  *
226  * This function is deprecated and should not be used by new drivers, as its
227  * results may be unreliable.
228  *
229  * Return: PCM position in frames
230  */
231 snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
232 {
233 	struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
234 	return bytes_to_frames(substream->runtime, prtd->pos);
235 }
236 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
237 
238 /**
239  * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
240  * @substream: PCM substream
241  *
242  * This function can be used as the PCM pointer callback for dmaengine based PCM
243  * driver implementations.
244  *
245  * Return: PCM position in frames
246  */
247 snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
248 {
249 	struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
250 	struct snd_pcm_runtime *runtime = substream->runtime;
251 	struct dma_tx_state state;
252 	enum dma_status status;
253 	unsigned int buf_size;
254 	unsigned int pos = 0;
255 
256 	status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
257 	if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
258 		buf_size = snd_pcm_lib_buffer_bytes(substream);
259 		if (state.residue > 0 && state.residue <= buf_size)
260 			pos = buf_size - state.residue;
261 
262 		runtime->delay = bytes_to_frames(runtime,
263 						 state.in_flight_bytes);
264 	}
265 
266 	return bytes_to_frames(runtime, pos);
267 }
268 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
269 
270 /**
271  * snd_dmaengine_pcm_request_channel - Request channel for the dmaengine PCM
272  * @filter_fn: Filter function used to request the DMA channel
273  * @filter_data: Data passed to the DMA filter function
274  *
275  * This function request a DMA channel for usage with dmaengine PCM.
276  *
277  * Return: NULL or the requested DMA channel
278  */
279 struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn,
280 	void *filter_data)
281 {
282 	dma_cap_mask_t mask;
283 
284 	dma_cap_zero(mask);
285 	dma_cap_set(DMA_SLAVE, mask);
286 	dma_cap_set(DMA_CYCLIC, mask);
287 
288 	return dma_request_channel(mask, filter_fn, filter_data);
289 }
290 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_request_channel);
291 
292 /**
293  * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
294  * @substream: PCM substream
295  * @chan: DMA channel to use for data transfers
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  *
301  * Return: 0 on success, a negative error code otherwise
302  */
303 int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
304 	struct dma_chan *chan)
305 {
306 	struct dmaengine_pcm_runtime_data *prtd;
307 	int ret;
308 
309 	if (!chan)
310 		return -ENXIO;
311 
312 	ret = snd_pcm_hw_constraint_integer(substream->runtime,
313 					    SNDRV_PCM_HW_PARAM_PERIODS);
314 	if (ret < 0)
315 		return ret;
316 
317 	prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
318 	if (!prtd)
319 		return -ENOMEM;
320 
321 	prtd->dma_chan = chan;
322 
323 	substream->runtime->private_data = prtd;
324 
325 	return 0;
326 }
327 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
328 
329 /**
330  * snd_dmaengine_pcm_open_request_chan - Open a dmaengine based PCM substream and request channel
331  * @substream: PCM substream
332  * @filter_fn: Filter function used to request the DMA channel
333  * @filter_data: Data passed to the DMA filter function
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  *
340  * Return: 0 on success, a negative error code otherwise
341  */
342 int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream *substream,
343 	dma_filter_fn filter_fn, void *filter_data)
344 {
345 	return snd_dmaengine_pcm_open(substream,
346 		    snd_dmaengine_pcm_request_channel(filter_fn, filter_data));
347 }
348 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open_request_chan);
349 
350 /**
351  * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
352  * @substream: PCM substream
353  *
354  * Return: 0 on success, a negative error code otherwise
355  */
356 int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
357 {
358 	struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
359 
360 	dmaengine_synchronize(prtd->dma_chan);
361 	kfree(prtd);
362 
363 	return 0;
364 }
365 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
366 
367 /**
368  * snd_dmaengine_pcm_close_release_chan - Close a dmaengine based PCM
369  *					  substream and release channel
370  * @substream: PCM substream
371  *
372  * Releases the DMA channel associated with the PCM substream.
373  *
374  * Return: zero if successful, or a negative error code
375  */
376 int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream)
377 {
378 	struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
379 
380 	dmaengine_synchronize(prtd->dma_chan);
381 	dma_release_channel(prtd->dma_chan);
382 	kfree(prtd);
383 
384 	return 0;
385 }
386 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close_release_chan);
387 
388 /**
389  * snd_dmaengine_pcm_refine_runtime_hwparams - Refine runtime hw params
390  * @substream: PCM substream
391  * @dma_data: DAI DMA data
392  * @hw: PCM hw params
393  * @chan: DMA channel to use for data transfers
394  *
395  * This function will query DMA capability, then refine the pcm hardware
396  * parameters.
397  *
398  * Return: 0 on success, a negative error code otherwise
399  */
400 int snd_dmaengine_pcm_refine_runtime_hwparams(
401 	struct snd_pcm_substream *substream,
402 	struct snd_dmaengine_dai_dma_data *dma_data,
403 	struct snd_pcm_hardware *hw,
404 	struct dma_chan *chan)
405 {
406 	struct dma_slave_caps dma_caps;
407 	u32 addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
408 			  BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
409 			  BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
410 	snd_pcm_format_t i;
411 	int ret = 0;
412 
413 	if (!hw || !chan || !dma_data)
414 		return -EINVAL;
415 
416 	ret = dma_get_slave_caps(chan, &dma_caps);
417 	if (ret == 0) {
418 		if (dma_caps.cmd_pause && dma_caps.cmd_resume)
419 			hw->info |= SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME;
420 		if (dma_caps.residue_granularity <= DMA_RESIDUE_GRANULARITY_SEGMENT)
421 			hw->info |= SNDRV_PCM_INFO_BATCH;
422 
423 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
424 			addr_widths = dma_caps.dst_addr_widths;
425 		else
426 			addr_widths = dma_caps.src_addr_widths;
427 	}
428 
429 	/*
430 	 * If SND_DMAENGINE_PCM_DAI_FLAG_PACK is set keep
431 	 * hw.formats set to 0, meaning no restrictions are in place.
432 	 * In this case it's the responsibility of the DAI driver to
433 	 * provide the supported format information.
434 	 */
435 	if (!(dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK))
436 		/*
437 		 * Prepare formats mask for valid/allowed sample types. If the
438 		 * dma does not have support for the given physical word size,
439 		 * it needs to be masked out so user space can not use the
440 		 * format which produces corrupted audio.
441 		 * In case the dma driver does not implement the slave_caps the
442 		 * default assumption is that it supports 1, 2 and 4 bytes
443 		 * widths.
444 		 */
445 		pcm_for_each_format(i) {
446 			int bits = snd_pcm_format_physical_width(i);
447 
448 			/*
449 			 * Enable only samples with DMA supported physical
450 			 * widths
451 			 */
452 			switch (bits) {
453 			case 8:
454 			case 16:
455 			case 24:
456 			case 32:
457 			case 64:
458 				if (addr_widths & (1 << (bits / 8)))
459 					hw->formats |= pcm_format_to_bits(i);
460 				break;
461 			default:
462 				/* Unsupported types */
463 				break;
464 			}
465 		}
466 
467 	return ret;
468 }
469 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_refine_runtime_hwparams);
470 
471 MODULE_LICENSE("GPL");
472