xref: /openbmc/linux/sound/soc/soc-generic-dmaengine-pcm.c (revision d708c2b36b5d23a7266ca9bfe648533a0f61bdfd)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 //  Copyright (C) 2013, Analog Devices Inc.
4 //	Author: Lars-Peter Clausen <lars@metafoo.de>
5 
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/dmaengine.h>
9 #include <linux/slab.h>
10 #include <sound/pcm.h>
11 #include <sound/pcm_params.h>
12 #include <sound/soc.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/of.h>
15 
16 #include <sound/dmaengine_pcm.h>
17 
18 /*
19  * The platforms dmaengine driver does not support reporting the amount of
20  * bytes that are still left to transfer.
21  */
22 #define SND_DMAENGINE_PCM_FLAG_NO_RESIDUE BIT(31)
23 
24 struct dmaengine_pcm {
25 	struct dma_chan *chan[SNDRV_PCM_STREAM_LAST + 1];
26 	const struct snd_dmaengine_pcm_config *config;
27 	struct snd_soc_component component;
28 	unsigned int flags;
29 };
30 
31 static struct dmaengine_pcm *soc_component_to_pcm(struct snd_soc_component *p)
32 {
33 	return container_of(p, struct dmaengine_pcm, component);
34 }
35 
36 static struct device *dmaengine_dma_dev(struct dmaengine_pcm *pcm,
37 	struct snd_pcm_substream *substream)
38 {
39 	if (!pcm->chan[substream->stream])
40 		return NULL;
41 
42 	return pcm->chan[substream->stream]->device->dev;
43 }
44 
45 /**
46  * snd_dmaengine_pcm_prepare_slave_config() - Generic prepare_slave_config callback
47  * @substream: PCM substream
48  * @params: hw_params
49  * @slave_config: DMA slave config to prepare
50  *
51  * This function can be used as a generic prepare_slave_config callback for
52  * platforms which make use of the snd_dmaengine_dai_dma_data struct for their
53  * DAI DMA data. Internally the function will first call
54  * snd_hwparams_to_dma_slave_config to fill in the slave config based on the
55  * hw_params, followed by snd_dmaengine_set_config_from_dai_data to fill in the
56  * remaining fields based on the DAI DMA data.
57  */
58 int snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream *substream,
59 	struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config)
60 {
61 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
62 	struct snd_dmaengine_dai_dma_data *dma_data;
63 	int ret;
64 
65 	dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
66 
67 	ret = snd_hwparams_to_dma_slave_config(substream, params, slave_config);
68 	if (ret)
69 		return ret;
70 
71 	snd_dmaengine_pcm_set_config_from_dai_data(substream, dma_data,
72 		slave_config);
73 
74 	return 0;
75 }
76 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_prepare_slave_config);
77 
78 static int dmaengine_pcm_hw_params(struct snd_soc_component *component,
79 				   struct snd_pcm_substream *substream,
80 				   struct snd_pcm_hw_params *params)
81 {
82 	struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
83 	struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
84 	int (*prepare_slave_config)(struct snd_pcm_substream *substream,
85 			struct snd_pcm_hw_params *params,
86 			struct dma_slave_config *slave_config);
87 	struct dma_slave_config slave_config;
88 	int ret;
89 
90 	memset(&slave_config, 0, sizeof(slave_config));
91 
92 	if (!pcm->config)
93 		prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config;
94 	else
95 		prepare_slave_config = pcm->config->prepare_slave_config;
96 
97 	if (prepare_slave_config) {
98 		ret = prepare_slave_config(substream, params, &slave_config);
99 		if (ret)
100 			return ret;
101 
102 		ret = dmaengine_slave_config(chan, &slave_config);
103 		if (ret)
104 			return ret;
105 	}
106 
107 	return 0;
108 }
109 
110 static int
111 dmaengine_pcm_set_runtime_hwparams(struct snd_soc_component *component,
112 				   struct snd_pcm_substream *substream)
113 {
114 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
115 	struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
116 	struct device *dma_dev = dmaengine_dma_dev(pcm, substream);
117 	struct dma_chan *chan = pcm->chan[substream->stream];
118 	struct snd_dmaengine_dai_dma_data *dma_data;
119 	struct snd_pcm_hardware hw;
120 	int ret;
121 
122 	if (pcm->config && pcm->config->pcm_hardware)
123 		return snd_soc_set_runtime_hwparams(substream,
124 				pcm->config->pcm_hardware);
125 
126 	dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
127 
128 	memset(&hw, 0, sizeof(hw));
129 	hw.info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
130 			SNDRV_PCM_INFO_INTERLEAVED;
131 	hw.periods_min = 2;
132 	hw.periods_max = UINT_MAX;
133 	hw.period_bytes_min = 256;
134 	hw.period_bytes_max = dma_get_max_seg_size(dma_dev);
135 	hw.buffer_bytes_max = SIZE_MAX;
136 	hw.fifo_size = dma_data->fifo_size;
137 
138 	if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
139 		hw.info |= SNDRV_PCM_INFO_BATCH;
140 
141 	ret = snd_dmaengine_pcm_refine_runtime_hwparams(substream,
142 							dma_data,
143 							&hw,
144 							chan);
145 	if (ret)
146 		return ret;
147 
148 	return snd_soc_set_runtime_hwparams(substream, &hw);
149 }
150 
151 static int dmaengine_pcm_open(struct snd_soc_component *component,
152 			      struct snd_pcm_substream *substream)
153 {
154 	struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
155 	struct dma_chan *chan = pcm->chan[substream->stream];
156 	int ret;
157 
158 	ret = dmaengine_pcm_set_runtime_hwparams(component, substream);
159 	if (ret)
160 		return ret;
161 
162 	return snd_dmaengine_pcm_open(substream, chan);
163 }
164 
165 static int dmaengine_pcm_close(struct snd_soc_component *component,
166 			       struct snd_pcm_substream *substream)
167 {
168 	return snd_dmaengine_pcm_close(substream);
169 }
170 
171 static int dmaengine_pcm_trigger(struct snd_soc_component *component,
172 				 struct snd_pcm_substream *substream, int cmd)
173 {
174 	return snd_dmaengine_pcm_trigger(substream, cmd);
175 }
176 
177 static struct dma_chan *dmaengine_pcm_compat_request_channel(
178 	struct snd_soc_component *component,
179 	struct snd_soc_pcm_runtime *rtd,
180 	struct snd_pcm_substream *substream)
181 {
182 	struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
183 	struct snd_dmaengine_dai_dma_data *dma_data;
184 	dma_filter_fn fn = NULL;
185 
186 	dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
187 
188 	if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) && pcm->chan[0])
189 		return pcm->chan[0];
190 
191 	if (pcm->config && pcm->config->compat_request_channel)
192 		return pcm->config->compat_request_channel(rtd, substream);
193 
194 	if (pcm->config)
195 		fn = pcm->config->compat_filter_fn;
196 
197 	return snd_dmaengine_pcm_request_channel(fn, dma_data->filter_data);
198 }
199 
200 static bool dmaengine_pcm_can_report_residue(struct device *dev,
201 	struct dma_chan *chan)
202 {
203 	struct dma_slave_caps dma_caps;
204 	int ret;
205 
206 	ret = dma_get_slave_caps(chan, &dma_caps);
207 	if (ret != 0) {
208 		dev_warn(dev, "Failed to get DMA channel capabilities, falling back to period counting: %d\n",
209 			 ret);
210 		return false;
211 	}
212 
213 	if (dma_caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR)
214 		return false;
215 
216 	return true;
217 }
218 
219 static int dmaengine_pcm_new(struct snd_soc_component *component,
220 			     struct snd_soc_pcm_runtime *rtd)
221 {
222 	struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
223 	const struct snd_dmaengine_pcm_config *config = pcm->config;
224 	struct device *dev = component->dev;
225 	struct snd_pcm_substream *substream;
226 	size_t prealloc_buffer_size;
227 	size_t max_buffer_size;
228 	unsigned int i;
229 
230 	if (config && config->prealloc_buffer_size) {
231 		prealloc_buffer_size = config->prealloc_buffer_size;
232 		max_buffer_size = config->pcm_hardware->buffer_bytes_max;
233 	} else {
234 		prealloc_buffer_size = 512 * 1024;
235 		max_buffer_size = SIZE_MAX;
236 	}
237 
238 	for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE; i++) {
239 		substream = rtd->pcm->streams[i].substream;
240 		if (!substream)
241 			continue;
242 
243 		if (!pcm->chan[i] && config && config->chan_names[i])
244 			pcm->chan[i] = dma_request_slave_channel(dev,
245 				config->chan_names[i]);
246 
247 		if (!pcm->chan[i] && (pcm->flags & SND_DMAENGINE_PCM_FLAG_COMPAT)) {
248 			pcm->chan[i] = dmaengine_pcm_compat_request_channel(
249 				component, rtd, substream);
250 		}
251 
252 		if (!pcm->chan[i]) {
253 			dev_err(component->dev,
254 				"Missing dma channel for stream: %d\n", i);
255 			return -EINVAL;
256 		}
257 
258 		snd_pcm_set_managed_buffer(substream,
259 				SNDRV_DMA_TYPE_DEV_IRAM,
260 				dmaengine_dma_dev(pcm, substream),
261 				prealloc_buffer_size,
262 				max_buffer_size);
263 
264 		if (!dmaengine_pcm_can_report_residue(dev, pcm->chan[i]))
265 			pcm->flags |= SND_DMAENGINE_PCM_FLAG_NO_RESIDUE;
266 
267 		if (rtd->pcm->streams[i].pcm->name[0] == '\0') {
268 			strscpy_pad(rtd->pcm->streams[i].pcm->name,
269 				    rtd->pcm->streams[i].pcm->id,
270 				    sizeof(rtd->pcm->streams[i].pcm->name));
271 		}
272 	}
273 
274 	return 0;
275 }
276 
277 static snd_pcm_uframes_t dmaengine_pcm_pointer(
278 	struct snd_soc_component *component,
279 	struct snd_pcm_substream *substream)
280 {
281 	struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
282 
283 	if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
284 		return snd_dmaengine_pcm_pointer_no_residue(substream);
285 	else
286 		return snd_dmaengine_pcm_pointer(substream);
287 }
288 
289 static int dmaengine_copy_user(struct snd_soc_component *component,
290 			       struct snd_pcm_substream *substream,
291 			       int channel, unsigned long hwoff,
292 			       void __user *buf, unsigned long bytes)
293 {
294 	struct snd_pcm_runtime *runtime = substream->runtime;
295 	struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
296 	int (*process)(struct snd_pcm_substream *substream,
297 		       int channel, unsigned long hwoff,
298 		       void *buf, unsigned long bytes) = pcm->config->process;
299 	bool is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
300 	void *dma_ptr = runtime->dma_area + hwoff +
301 			channel * (runtime->dma_bytes / runtime->channels);
302 	int ret;
303 
304 	if (is_playback)
305 		if (copy_from_user(dma_ptr, buf, bytes))
306 			return -EFAULT;
307 
308 	if (process) {
309 		ret = process(substream, channel, hwoff, (__force void *)buf, bytes);
310 		if (ret < 0)
311 			return ret;
312 	}
313 
314 	if (!is_playback)
315 		if (copy_to_user(buf, dma_ptr, bytes))
316 			return -EFAULT;
317 
318 	return 0;
319 }
320 
321 static const struct snd_soc_component_driver dmaengine_pcm_component = {
322 	.name		= SND_DMAENGINE_PCM_DRV_NAME,
323 	.probe_order	= SND_SOC_COMP_ORDER_LATE,
324 	.open		= dmaengine_pcm_open,
325 	.close		= dmaengine_pcm_close,
326 	.hw_params	= dmaengine_pcm_hw_params,
327 	.trigger	= dmaengine_pcm_trigger,
328 	.pointer	= dmaengine_pcm_pointer,
329 	.pcm_construct	= dmaengine_pcm_new,
330 };
331 
332 static const struct snd_soc_component_driver dmaengine_pcm_component_process = {
333 	.name		= SND_DMAENGINE_PCM_DRV_NAME,
334 	.probe_order	= SND_SOC_COMP_ORDER_LATE,
335 	.open		= dmaengine_pcm_open,
336 	.close		= dmaengine_pcm_close,
337 	.hw_params	= dmaengine_pcm_hw_params,
338 	.trigger	= dmaengine_pcm_trigger,
339 	.pointer	= dmaengine_pcm_pointer,
340 	.copy_user	= dmaengine_copy_user,
341 	.pcm_construct	= dmaengine_pcm_new,
342 };
343 
344 static const char * const dmaengine_pcm_dma_channel_names[] = {
345 	[SNDRV_PCM_STREAM_PLAYBACK] = "tx",
346 	[SNDRV_PCM_STREAM_CAPTURE] = "rx",
347 };
348 
349 static int dmaengine_pcm_request_chan_of(struct dmaengine_pcm *pcm,
350 	struct device *dev, const struct snd_dmaengine_pcm_config *config)
351 {
352 	unsigned int i;
353 	const char *name;
354 	struct dma_chan *chan;
355 
356 	if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_DT) || (!dev->of_node &&
357 	    !(config && config->dma_dev && config->dma_dev->of_node)))
358 		return 0;
359 
360 	if (config && config->dma_dev) {
361 		/*
362 		 * If this warning is seen, it probably means that your Linux
363 		 * device structure does not match your HW device structure.
364 		 * It would be best to refactor the Linux device structure to
365 		 * correctly match the HW structure.
366 		 */
367 		dev_warn(dev, "DMA channels sourced from device %s",
368 			 dev_name(config->dma_dev));
369 		dev = config->dma_dev;
370 	}
371 
372 	for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE;
373 	     i++) {
374 		if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
375 			name = "rx-tx";
376 		else
377 			name = dmaengine_pcm_dma_channel_names[i];
378 		if (config && config->chan_names[i])
379 			name = config->chan_names[i];
380 		chan = dma_request_chan(dev, name);
381 		if (IS_ERR(chan)) {
382 			if (PTR_ERR(chan) == -EPROBE_DEFER)
383 				return -EPROBE_DEFER;
384 			pcm->chan[i] = NULL;
385 		} else {
386 			pcm->chan[i] = chan;
387 		}
388 		if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
389 			break;
390 	}
391 
392 	if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
393 		pcm->chan[1] = pcm->chan[0];
394 
395 	return 0;
396 }
397 
398 static void dmaengine_pcm_release_chan(struct dmaengine_pcm *pcm)
399 {
400 	unsigned int i;
401 
402 	for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE;
403 	     i++) {
404 		if (!pcm->chan[i])
405 			continue;
406 		dma_release_channel(pcm->chan[i]);
407 		if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
408 			break;
409 	}
410 }
411 
412 /**
413  * snd_dmaengine_pcm_register - Register a dmaengine based PCM device
414  * @dev: The parent device for the PCM device
415  * @config: Platform specific PCM configuration
416  * @flags: Platform specific quirks
417  */
418 int snd_dmaengine_pcm_register(struct device *dev,
419 	const struct snd_dmaengine_pcm_config *config, unsigned int flags)
420 {
421 	struct dmaengine_pcm *pcm;
422 	int ret;
423 
424 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
425 	if (!pcm)
426 		return -ENOMEM;
427 
428 #ifdef CONFIG_DEBUG_FS
429 	pcm->component.debugfs_prefix = "dma";
430 #endif
431 	pcm->config = config;
432 	pcm->flags = flags;
433 
434 	ret = dmaengine_pcm_request_chan_of(pcm, dev, config);
435 	if (ret)
436 		goto err_free_dma;
437 
438 	if (config && config->process)
439 		ret = snd_soc_add_component(dev, &pcm->component,
440 					    &dmaengine_pcm_component_process,
441 					    NULL, 0);
442 	else
443 		ret = snd_soc_add_component(dev, &pcm->component,
444 					    &dmaengine_pcm_component, NULL, 0);
445 	if (ret)
446 		goto err_free_dma;
447 
448 	return 0;
449 
450 err_free_dma:
451 	dmaengine_pcm_release_chan(pcm);
452 	kfree(pcm);
453 	return ret;
454 }
455 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_register);
456 
457 /**
458  * snd_dmaengine_pcm_unregister - Removes a dmaengine based PCM device
459  * @dev: Parent device the PCM was register with
460  *
461  * Removes a dmaengine based PCM device previously registered with
462  * snd_dmaengine_pcm_register.
463  */
464 void snd_dmaengine_pcm_unregister(struct device *dev)
465 {
466 	struct snd_soc_component *component;
467 	struct dmaengine_pcm *pcm;
468 
469 	component = snd_soc_lookup_component(dev, SND_DMAENGINE_PCM_DRV_NAME);
470 	if (!component)
471 		return;
472 
473 	pcm = soc_component_to_pcm(component);
474 
475 	snd_soc_unregister_component(dev);
476 	dmaengine_pcm_release_chan(pcm);
477 	kfree(pcm);
478 }
479 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_unregister);
480 
481 MODULE_LICENSE("GPL");
482