1 /*
2  *  Copyright (C) 2013, Analog Devices Inc.
3  *	Author: Lars-Peter Clausen <lars@metafoo.de>
4  *
5  *  This program is free software; you can redistribute it and/or modify it
6  *  under  the terms of the GNU General  Public License as published by the
7  *  Free Software Foundation;  either version 2 of the License, or (at your
8  *  option) any later version.
9  *
10  *  You should have received a copy of the GNU General Public License along
11  *  with this program; if not, write to the Free Software Foundation, Inc.,
12  *  675 Mass Ave, Cambridge, MA 02139, USA.
13  *
14  */
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/dmaengine.h>
18 #include <linux/slab.h>
19 #include <sound/pcm.h>
20 #include <sound/pcm_params.h>
21 #include <sound/soc.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/of.h>
24 
25 #include <sound/dmaengine_pcm.h>
26 
27 /*
28  * The platforms dmaengine driver does not support reporting the amount of
29  * bytes that are still left to transfer.
30  */
31 #define SND_DMAENGINE_PCM_FLAG_NO_RESIDUE BIT(31)
32 
33 struct dmaengine_pcm {
34 	struct dma_chan *chan[SNDRV_PCM_STREAM_LAST + 1];
35 	const struct snd_dmaengine_pcm_config *config;
36 	struct snd_soc_component component;
37 	unsigned int flags;
38 };
39 
40 static struct dmaengine_pcm *soc_component_to_pcm(struct snd_soc_component *p)
41 {
42 	return container_of(p, struct dmaengine_pcm, component);
43 }
44 
45 static struct device *dmaengine_dma_dev(struct dmaengine_pcm *pcm,
46 	struct snd_pcm_substream *substream)
47 {
48 	if (!pcm->chan[substream->stream])
49 		return NULL;
50 
51 	return pcm->chan[substream->stream]->device->dev;
52 }
53 
54 /**
55  * snd_dmaengine_pcm_prepare_slave_config() - Generic prepare_slave_config callback
56  * @substream: PCM substream
57  * @params: hw_params
58  * @slave_config: DMA slave config to prepare
59  *
60  * This function can be used as a generic prepare_slave_config callback for
61  * platforms which make use of the snd_dmaengine_dai_dma_data struct for their
62  * DAI DMA data. Internally the function will first call
63  * snd_hwparams_to_dma_slave_config to fill in the slave config based on the
64  * hw_params, followed by snd_dmaengine_set_config_from_dai_data to fill in the
65  * remaining fields based on the DAI DMA data.
66  */
67 int snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream *substream,
68 	struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config)
69 {
70 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
71 	struct snd_dmaengine_dai_dma_data *dma_data;
72 	int ret;
73 
74 	dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
75 
76 	ret = snd_hwparams_to_dma_slave_config(substream, params, slave_config);
77 	if (ret)
78 		return ret;
79 
80 	snd_dmaengine_pcm_set_config_from_dai_data(substream, dma_data,
81 		slave_config);
82 
83 	return 0;
84 }
85 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_prepare_slave_config);
86 
87 static int dmaengine_pcm_hw_params(struct snd_pcm_substream *substream,
88 	struct snd_pcm_hw_params *params)
89 {
90 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
91 	struct snd_soc_component *component =
92 		snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
93 	struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
94 	struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
95 	int (*prepare_slave_config)(struct snd_pcm_substream *substream,
96 			struct snd_pcm_hw_params *params,
97 			struct dma_slave_config *slave_config);
98 	struct dma_slave_config slave_config;
99 	int ret;
100 
101 	memset(&slave_config, 0, sizeof(slave_config));
102 
103 	if (!pcm->config)
104 		prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config;
105 	else
106 		prepare_slave_config = pcm->config->prepare_slave_config;
107 
108 	if (prepare_slave_config) {
109 		ret = prepare_slave_config(substream, params, &slave_config);
110 		if (ret)
111 			return ret;
112 
113 		ret = dmaengine_slave_config(chan, &slave_config);
114 		if (ret)
115 			return ret;
116 	}
117 
118 	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
119 }
120 
121 static int dmaengine_pcm_set_runtime_hwparams(struct snd_pcm_substream *substream)
122 {
123 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
124 	struct snd_soc_component *component =
125 		snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
126 	struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
127 	struct device *dma_dev = dmaengine_dma_dev(pcm, substream);
128 	struct dma_chan *chan = pcm->chan[substream->stream];
129 	struct snd_dmaengine_dai_dma_data *dma_data;
130 	struct dma_slave_caps dma_caps;
131 	struct snd_pcm_hardware hw;
132 	u32 addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
133 			  BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
134 			  BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
135 	snd_pcm_format_t i;
136 	int ret;
137 
138 	if (pcm->config && pcm->config->pcm_hardware)
139 		return snd_soc_set_runtime_hwparams(substream,
140 				pcm->config->pcm_hardware);
141 
142 	dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
143 
144 	memset(&hw, 0, sizeof(hw));
145 	hw.info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
146 			SNDRV_PCM_INFO_INTERLEAVED;
147 	hw.periods_min = 2;
148 	hw.periods_max = UINT_MAX;
149 	hw.period_bytes_min = 256;
150 	hw.period_bytes_max = dma_get_max_seg_size(dma_dev);
151 	hw.buffer_bytes_max = SIZE_MAX;
152 	hw.fifo_size = dma_data->fifo_size;
153 
154 	if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
155 		hw.info |= SNDRV_PCM_INFO_BATCH;
156 
157 	ret = dma_get_slave_caps(chan, &dma_caps);
158 	if (ret == 0) {
159 		if (dma_caps.cmd_pause)
160 			hw.info |= SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME;
161 		if (dma_caps.residue_granularity <= DMA_RESIDUE_GRANULARITY_SEGMENT)
162 			hw.info |= SNDRV_PCM_INFO_BATCH;
163 
164 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
165 			addr_widths = dma_caps.dst_addr_widths;
166 		else
167 			addr_widths = dma_caps.src_addr_widths;
168 	}
169 
170 	/*
171 	 * If SND_DMAENGINE_PCM_DAI_FLAG_PACK is set keep
172 	 * hw.formats set to 0, meaning no restrictions are in place.
173 	 * In this case it's the responsibility of the DAI driver to
174 	 * provide the supported format information.
175 	 */
176 	if (!(dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK))
177 		/*
178 		 * Prepare formats mask for valid/allowed sample types. If the
179 		 * dma does not have support for the given physical word size,
180 		 * it needs to be masked out so user space can not use the
181 		 * format which produces corrupted audio.
182 		 * In case the dma driver does not implement the slave_caps the
183 		 * default assumption is that it supports 1, 2 and 4 bytes
184 		 * widths.
185 		 */
186 		for (i = SNDRV_PCM_FORMAT_FIRST; i <= SNDRV_PCM_FORMAT_LAST; i++) {
187 			int bits = snd_pcm_format_physical_width(i);
188 
189 			/*
190 			 * Enable only samples with DMA supported physical
191 			 * widths
192 			 */
193 			switch (bits) {
194 			case 8:
195 			case 16:
196 			case 24:
197 			case 32:
198 			case 64:
199 				if (addr_widths & (1 << (bits / 8)))
200 					hw.formats |= (1LL << i);
201 				break;
202 			default:
203 				/* Unsupported types */
204 				break;
205 			}
206 		}
207 
208 	return snd_soc_set_runtime_hwparams(substream, &hw);
209 }
210 
211 static int dmaengine_pcm_open(struct snd_pcm_substream *substream)
212 {
213 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
214 	struct snd_soc_component *component =
215 		snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
216 	struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
217 	struct dma_chan *chan = pcm->chan[substream->stream];
218 	int ret;
219 
220 	ret = dmaengine_pcm_set_runtime_hwparams(substream);
221 	if (ret)
222 		return ret;
223 
224 	return snd_dmaengine_pcm_open(substream, chan);
225 }
226 
227 static struct dma_chan *dmaengine_pcm_compat_request_channel(
228 	struct snd_soc_pcm_runtime *rtd,
229 	struct snd_pcm_substream *substream)
230 {
231 	struct snd_soc_component *component =
232 		snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
233 	struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
234 	struct snd_dmaengine_dai_dma_data *dma_data;
235 	dma_filter_fn fn = NULL;
236 
237 	dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
238 
239 	if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) && pcm->chan[0])
240 		return pcm->chan[0];
241 
242 	if (pcm->config && pcm->config->compat_request_channel)
243 		return pcm->config->compat_request_channel(rtd, substream);
244 
245 	if (pcm->config)
246 		fn = pcm->config->compat_filter_fn;
247 
248 	return snd_dmaengine_pcm_request_channel(fn, dma_data->filter_data);
249 }
250 
251 static bool dmaengine_pcm_can_report_residue(struct device *dev,
252 	struct dma_chan *chan)
253 {
254 	struct dma_slave_caps dma_caps;
255 	int ret;
256 
257 	ret = dma_get_slave_caps(chan, &dma_caps);
258 	if (ret != 0) {
259 		dev_warn(dev, "Failed to get DMA channel capabilities, falling back to period counting: %d\n",
260 			 ret);
261 		return false;
262 	}
263 
264 	if (dma_caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR)
265 		return false;
266 
267 	return true;
268 }
269 
270 static int dmaengine_pcm_new(struct snd_soc_pcm_runtime *rtd)
271 {
272 	struct snd_soc_component *component =
273 		snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
274 	struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
275 	const struct snd_dmaengine_pcm_config *config = pcm->config;
276 	struct device *dev = component->dev;
277 	struct snd_dmaengine_dai_dma_data *dma_data;
278 	struct snd_pcm_substream *substream;
279 	size_t prealloc_buffer_size;
280 	size_t max_buffer_size;
281 	unsigned int i;
282 	int ret;
283 
284 	if (config && config->prealloc_buffer_size) {
285 		prealloc_buffer_size = config->prealloc_buffer_size;
286 		max_buffer_size = config->pcm_hardware->buffer_bytes_max;
287 	} else {
288 		prealloc_buffer_size = 512 * 1024;
289 		max_buffer_size = SIZE_MAX;
290 	}
291 
292 	for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE; i++) {
293 		substream = rtd->pcm->streams[i].substream;
294 		if (!substream)
295 			continue;
296 
297 		dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
298 
299 		if (!pcm->chan[i] &&
300 		    (pcm->flags & SND_DMAENGINE_PCM_FLAG_CUSTOM_CHANNEL_NAME))
301 			pcm->chan[i] = dma_request_slave_channel(dev,
302 				dma_data->chan_name);
303 
304 		if (!pcm->chan[i] && (pcm->flags & SND_DMAENGINE_PCM_FLAG_COMPAT)) {
305 			pcm->chan[i] = dmaengine_pcm_compat_request_channel(rtd,
306 				substream);
307 		}
308 
309 		if (!pcm->chan[i]) {
310 			dev_err(component->dev,
311 				"Missing dma channel for stream: %d\n", i);
312 			return -EINVAL;
313 		}
314 
315 		ret = snd_pcm_lib_preallocate_pages(substream,
316 				SNDRV_DMA_TYPE_DEV_IRAM,
317 				dmaengine_dma_dev(pcm, substream),
318 				prealloc_buffer_size,
319 				max_buffer_size);
320 		if (ret)
321 			return ret;
322 
323 		if (!dmaengine_pcm_can_report_residue(dev, pcm->chan[i]))
324 			pcm->flags |= SND_DMAENGINE_PCM_FLAG_NO_RESIDUE;
325 	}
326 
327 	return 0;
328 }
329 
330 static snd_pcm_uframes_t dmaengine_pcm_pointer(
331 	struct snd_pcm_substream *substream)
332 {
333 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
334 	struct snd_soc_component *component =
335 		snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
336 	struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
337 
338 	if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
339 		return snd_dmaengine_pcm_pointer_no_residue(substream);
340 	else
341 		return snd_dmaengine_pcm_pointer(substream);
342 }
343 
344 static int dmaengine_copy_user(struct snd_pcm_substream *substream,
345 			       int channel, unsigned long hwoff,
346 			       void *buf, unsigned long bytes)
347 {
348 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
349 	struct snd_soc_component *component =
350 		snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
351 	struct snd_pcm_runtime *runtime = substream->runtime;
352 	struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
353 	int (*process)(struct snd_pcm_substream *substream,
354 		       int channel, unsigned long hwoff,
355 		       void *buf, unsigned long bytes) = pcm->config->process;
356 	bool is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
357 	void *dma_ptr = runtime->dma_area + hwoff +
358 			channel * (runtime->dma_bytes / runtime->channels);
359 	int ret;
360 
361 	if (is_playback)
362 		if (copy_from_user(dma_ptr, (void __user *)buf, bytes))
363 			return -EFAULT;
364 
365 	if (process) {
366 		ret = process(substream, channel, hwoff,
367 			      (void __user *)buf, bytes);
368 		if (ret < 0)
369 			return ret;
370 	}
371 
372 	if (!is_playback)
373 		if (copy_to_user((void __user *)buf, dma_ptr, bytes))
374 			return -EFAULT;
375 
376 	return 0;
377 }
378 
379 static const struct snd_pcm_ops dmaengine_pcm_ops = {
380 	.open		= dmaengine_pcm_open,
381 	.close		= snd_dmaengine_pcm_close,
382 	.ioctl		= snd_pcm_lib_ioctl,
383 	.hw_params	= dmaengine_pcm_hw_params,
384 	.hw_free	= snd_pcm_lib_free_pages,
385 	.trigger	= snd_dmaengine_pcm_trigger,
386 	.pointer	= dmaengine_pcm_pointer,
387 };
388 
389 static const struct snd_pcm_ops dmaengine_pcm_process_ops = {
390 	.open		= dmaengine_pcm_open,
391 	.close		= snd_dmaengine_pcm_close,
392 	.ioctl		= snd_pcm_lib_ioctl,
393 	.hw_params	= dmaengine_pcm_hw_params,
394 	.hw_free	= snd_pcm_lib_free_pages,
395 	.trigger	= snd_dmaengine_pcm_trigger,
396 	.pointer	= dmaengine_pcm_pointer,
397 	.copy_user	= dmaengine_copy_user,
398 };
399 
400 static const struct snd_soc_component_driver dmaengine_pcm_component = {
401 	.name		= SND_DMAENGINE_PCM_DRV_NAME,
402 	.probe_order	= SND_SOC_COMP_ORDER_LATE,
403 	.ops		= &dmaengine_pcm_ops,
404 	.pcm_new	= dmaengine_pcm_new,
405 };
406 
407 static const struct snd_soc_component_driver dmaengine_pcm_component_process = {
408 	.name		= SND_DMAENGINE_PCM_DRV_NAME,
409 	.probe_order	= SND_SOC_COMP_ORDER_LATE,
410 	.ops		= &dmaengine_pcm_process_ops,
411 	.pcm_new	= dmaengine_pcm_new,
412 };
413 
414 static const char * const dmaengine_pcm_dma_channel_names[] = {
415 	[SNDRV_PCM_STREAM_PLAYBACK] = "tx",
416 	[SNDRV_PCM_STREAM_CAPTURE] = "rx",
417 };
418 
419 static int dmaengine_pcm_request_chan_of(struct dmaengine_pcm *pcm,
420 	struct device *dev, const struct snd_dmaengine_pcm_config *config)
421 {
422 	unsigned int i;
423 	const char *name;
424 	struct dma_chan *chan;
425 
426 	if ((pcm->flags & (SND_DMAENGINE_PCM_FLAG_NO_DT |
427 			   SND_DMAENGINE_PCM_FLAG_CUSTOM_CHANNEL_NAME)) ||
428 	    !dev->of_node)
429 		return 0;
430 
431 	if (config && config->dma_dev) {
432 		/*
433 		 * If this warning is seen, it probably means that your Linux
434 		 * device structure does not match your HW device structure.
435 		 * It would be best to refactor the Linux device structure to
436 		 * correctly match the HW structure.
437 		 */
438 		dev_warn(dev, "DMA channels sourced from device %s",
439 			 dev_name(config->dma_dev));
440 		dev = config->dma_dev;
441 	}
442 
443 	for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE;
444 	     i++) {
445 		if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
446 			name = "rx-tx";
447 		else
448 			name = dmaengine_pcm_dma_channel_names[i];
449 		if (config && config->chan_names[i])
450 			name = config->chan_names[i];
451 		chan = dma_request_slave_channel_reason(dev, name);
452 		if (IS_ERR(chan)) {
453 			if (PTR_ERR(chan) == -EPROBE_DEFER)
454 				return -EPROBE_DEFER;
455 			pcm->chan[i] = NULL;
456 		} else {
457 			pcm->chan[i] = chan;
458 		}
459 		if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
460 			break;
461 	}
462 
463 	if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
464 		pcm->chan[1] = pcm->chan[0];
465 
466 	return 0;
467 }
468 
469 static void dmaengine_pcm_release_chan(struct dmaengine_pcm *pcm)
470 {
471 	unsigned int i;
472 
473 	for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE;
474 	     i++) {
475 		if (!pcm->chan[i])
476 			continue;
477 		dma_release_channel(pcm->chan[i]);
478 		if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
479 			break;
480 	}
481 }
482 
483 /**
484  * snd_dmaengine_pcm_register - Register a dmaengine based PCM device
485  * @dev: The parent device for the PCM device
486  * @config: Platform specific PCM configuration
487  * @flags: Platform specific quirks
488  */
489 int snd_dmaengine_pcm_register(struct device *dev,
490 	const struct snd_dmaengine_pcm_config *config, unsigned int flags)
491 {
492 	struct dmaengine_pcm *pcm;
493 	int ret;
494 
495 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
496 	if (!pcm)
497 		return -ENOMEM;
498 
499 #ifdef CONFIG_DEBUG_FS
500 	pcm->component.debugfs_prefix = "dma";
501 #endif
502 	pcm->config = config;
503 	pcm->flags = flags;
504 
505 	ret = dmaengine_pcm_request_chan_of(pcm, dev, config);
506 	if (ret)
507 		goto err_free_dma;
508 
509 	if (config && config->process)
510 		ret = snd_soc_add_component(dev, &pcm->component,
511 					    &dmaengine_pcm_component_process,
512 					    NULL, 0);
513 	else
514 		ret = snd_soc_add_component(dev, &pcm->component,
515 					    &dmaengine_pcm_component, NULL, 0);
516 	if (ret)
517 		goto err_free_dma;
518 
519 	return 0;
520 
521 err_free_dma:
522 	dmaengine_pcm_release_chan(pcm);
523 	kfree(pcm);
524 	return ret;
525 }
526 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_register);
527 
528 /**
529  * snd_dmaengine_pcm_unregister - Removes a dmaengine based PCM device
530  * @dev: Parent device the PCM was register with
531  *
532  * Removes a dmaengine based PCM device previously registered with
533  * snd_dmaengine_pcm_register.
534  */
535 void snd_dmaengine_pcm_unregister(struct device *dev)
536 {
537 	struct snd_soc_component *component;
538 	struct dmaengine_pcm *pcm;
539 
540 	component = snd_soc_lookup_component(dev, SND_DMAENGINE_PCM_DRV_NAME);
541 	if (!component)
542 		return;
543 
544 	pcm = soc_component_to_pcm(component);
545 
546 	snd_soc_unregister_component(dev);
547 	dmaengine_pcm_release_chan(pcm);
548 	kfree(pcm);
549 }
550 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_unregister);
551 
552 MODULE_LICENSE("GPL");
553