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