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