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