xref: /linux/sound/soc/fsl/fsl_asrc_dma.c (revision 6c8c1406)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Freescale ASRC ALSA SoC Platform (DMA) driver
4 //
5 // Copyright (C) 2014 Freescale Semiconductor, Inc.
6 //
7 // Author: Nicolin Chen <nicoleotsuka@gmail.com>
8 
9 #include <linux/dma-mapping.h>
10 #include <linux/module.h>
11 #include <linux/dma/imx-dma.h>
12 #include <sound/dmaengine_pcm.h>
13 #include <sound/pcm_params.h>
14 
15 #include "fsl_asrc_common.h"
16 
17 #define FSL_ASRC_DMABUF_SIZE	(256 * 1024)
18 
19 static struct snd_pcm_hardware snd_imx_hardware = {
20 	.info = SNDRV_PCM_INFO_INTERLEAVED |
21 		SNDRV_PCM_INFO_BLOCK_TRANSFER |
22 		SNDRV_PCM_INFO_MMAP |
23 		SNDRV_PCM_INFO_MMAP_VALID,
24 	.buffer_bytes_max = FSL_ASRC_DMABUF_SIZE,
25 	.period_bytes_min = 128,
26 	.period_bytes_max = 65535, /* Limited by SDMA engine */
27 	.periods_min = 2,
28 	.periods_max = 255,
29 	.fifo_size = 0,
30 };
31 
32 static bool filter(struct dma_chan *chan, void *param)
33 {
34 	if (!imx_dma_is_general_purpose(chan))
35 		return false;
36 
37 	chan->private = param;
38 
39 	return true;
40 }
41 
42 static void fsl_asrc_dma_complete(void *arg)
43 {
44 	struct snd_pcm_substream *substream = arg;
45 	struct snd_pcm_runtime *runtime = substream->runtime;
46 	struct fsl_asrc_pair *pair = runtime->private_data;
47 
48 	pair->pos += snd_pcm_lib_period_bytes(substream);
49 	if (pair->pos >= snd_pcm_lib_buffer_bytes(substream))
50 		pair->pos = 0;
51 
52 	snd_pcm_period_elapsed(substream);
53 }
54 
55 static int fsl_asrc_dma_prepare_and_submit(struct snd_pcm_substream *substream,
56 					   struct snd_soc_component *component)
57 {
58 	u8 dir = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? OUT : IN;
59 	struct snd_pcm_runtime *runtime = substream->runtime;
60 	struct fsl_asrc_pair *pair = runtime->private_data;
61 	struct device *dev = component->dev;
62 	unsigned long flags = DMA_CTRL_ACK;
63 
64 	/* Prepare and submit Front-End DMA channel */
65 	if (!substream->runtime->no_period_wakeup)
66 		flags |= DMA_PREP_INTERRUPT;
67 
68 	pair->pos = 0;
69 	pair->desc[!dir] = dmaengine_prep_dma_cyclic(
70 			pair->dma_chan[!dir], runtime->dma_addr,
71 			snd_pcm_lib_buffer_bytes(substream),
72 			snd_pcm_lib_period_bytes(substream),
73 			dir == OUT ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM, flags);
74 	if (!pair->desc[!dir]) {
75 		dev_err(dev, "failed to prepare slave DMA for Front-End\n");
76 		return -ENOMEM;
77 	}
78 
79 	pair->desc[!dir]->callback = fsl_asrc_dma_complete;
80 	pair->desc[!dir]->callback_param = substream;
81 
82 	dmaengine_submit(pair->desc[!dir]);
83 
84 	/* Prepare and submit Back-End DMA channel */
85 	pair->desc[dir] = dmaengine_prep_dma_cyclic(
86 			pair->dma_chan[dir], 0xffff, 64, 64, DMA_DEV_TO_DEV, 0);
87 	if (!pair->desc[dir]) {
88 		dev_err(dev, "failed to prepare slave DMA for Back-End\n");
89 		return -ENOMEM;
90 	}
91 
92 	dmaengine_submit(pair->desc[dir]);
93 
94 	return 0;
95 }
96 
97 static int fsl_asrc_dma_trigger(struct snd_soc_component *component,
98 				struct snd_pcm_substream *substream, int cmd)
99 {
100 	struct snd_pcm_runtime *runtime = substream->runtime;
101 	struct fsl_asrc_pair *pair = runtime->private_data;
102 	int ret;
103 
104 	switch (cmd) {
105 	case SNDRV_PCM_TRIGGER_START:
106 	case SNDRV_PCM_TRIGGER_RESUME:
107 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
108 		ret = fsl_asrc_dma_prepare_and_submit(substream, component);
109 		if (ret)
110 			return ret;
111 		dma_async_issue_pending(pair->dma_chan[IN]);
112 		dma_async_issue_pending(pair->dma_chan[OUT]);
113 		break;
114 	case SNDRV_PCM_TRIGGER_STOP:
115 	case SNDRV_PCM_TRIGGER_SUSPEND:
116 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
117 		dmaengine_terminate_async(pair->dma_chan[OUT]);
118 		dmaengine_terminate_async(pair->dma_chan[IN]);
119 		break;
120 	default:
121 		return -EINVAL;
122 	}
123 
124 	return 0;
125 }
126 
127 static int fsl_asrc_dma_hw_params(struct snd_soc_component *component,
128 				  struct snd_pcm_substream *substream,
129 				  struct snd_pcm_hw_params *params)
130 {
131 	enum dma_slave_buswidth buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
132 	enum sdma_peripheral_type be_peripheral_type = IMX_DMATYPE_SSI;
133 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
134 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
135 	struct snd_dmaengine_dai_dma_data *dma_params_fe = NULL;
136 	struct snd_dmaengine_dai_dma_data *dma_params_be = NULL;
137 	struct snd_pcm_runtime *runtime = substream->runtime;
138 	struct fsl_asrc_pair *pair = runtime->private_data;
139 	struct dma_chan *tmp_chan = NULL, *be_chan = NULL;
140 	struct snd_soc_component *component_be = NULL;
141 	struct fsl_asrc *asrc = pair->asrc;
142 	struct dma_slave_config config_fe = {}, config_be = {};
143 	struct sdma_peripheral_config audio_config;
144 	enum asrc_pair_index index = pair->index;
145 	struct device *dev = component->dev;
146 	struct device_node *of_dma_node;
147 	int stream = substream->stream;
148 	struct imx_dma_data *tmp_data;
149 	struct snd_soc_dpcm *dpcm;
150 	struct device *dev_be;
151 	u8 dir = tx ? OUT : IN;
152 	dma_cap_mask_t mask;
153 	int ret, width;
154 
155 	/* Fetch the Back-End dma_data from DPCM */
156 	for_each_dpcm_be(rtd, stream, dpcm) {
157 		struct snd_soc_pcm_runtime *be = dpcm->be;
158 		struct snd_pcm_substream *substream_be;
159 		struct snd_soc_dai *dai = asoc_rtd_to_cpu(be, 0);
160 
161 		if (dpcm->fe != rtd)
162 			continue;
163 
164 		substream_be = snd_soc_dpcm_get_substream(be, stream);
165 		dma_params_be = snd_soc_dai_get_dma_data(dai, substream_be);
166 		dev_be = dai->dev;
167 		break;
168 	}
169 
170 	if (!dma_params_be) {
171 		dev_err(dev, "failed to get the substream of Back-End\n");
172 		return -EINVAL;
173 	}
174 
175 	/* Override dma_data of the Front-End and config its dmaengine */
176 	dma_params_fe = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
177 	dma_params_fe->addr = asrc->paddr + asrc->get_fifo_addr(!dir, index);
178 	dma_params_fe->maxburst = dma_params_be->maxburst;
179 
180 	pair->dma_chan[!dir] = asrc->get_dma_channel(pair, !dir);
181 	if (!pair->dma_chan[!dir]) {
182 		dev_err(dev, "failed to request DMA channel\n");
183 		return -EINVAL;
184 	}
185 
186 	ret = snd_dmaengine_pcm_prepare_slave_config(substream, params, &config_fe);
187 	if (ret) {
188 		dev_err(dev, "failed to prepare DMA config for Front-End\n");
189 		return ret;
190 	}
191 
192 	ret = dmaengine_slave_config(pair->dma_chan[!dir], &config_fe);
193 	if (ret) {
194 		dev_err(dev, "failed to config DMA channel for Front-End\n");
195 		return ret;
196 	}
197 
198 	/* Request and config DMA channel for Back-End */
199 	dma_cap_zero(mask);
200 	dma_cap_set(DMA_SLAVE, mask);
201 	dma_cap_set(DMA_CYCLIC, mask);
202 
203 	/*
204 	 * The Back-End device might have already requested a DMA channel,
205 	 * so try to reuse it first, and then request a new one upon NULL.
206 	 */
207 	component_be = snd_soc_lookup_component_nolocked(dev_be, SND_DMAENGINE_PCM_DRV_NAME);
208 	if (component_be) {
209 		be_chan = soc_component_to_pcm(component_be)->chan[substream->stream];
210 		tmp_chan = be_chan;
211 	}
212 	if (!tmp_chan)
213 		tmp_chan = dma_request_slave_channel(dev_be, tx ? "tx" : "rx");
214 
215 	/*
216 	 * An EDMA DEV_TO_DEV channel is fixed and bound with DMA event of each
217 	 * peripheral, unlike SDMA channel that is allocated dynamically. So no
218 	 * need to configure dma_request and dma_request2, but get dma_chan of
219 	 * Back-End device directly via dma_request_slave_channel.
220 	 */
221 	if (!asrc->use_edma) {
222 		/* Get DMA request of Back-End */
223 		tmp_data = tmp_chan->private;
224 		pair->dma_data.dma_request = tmp_data->dma_request;
225 		be_peripheral_type = tmp_data->peripheral_type;
226 		if (!be_chan)
227 			dma_release_channel(tmp_chan);
228 
229 		/* Get DMA request of Front-End */
230 		tmp_chan = asrc->get_dma_channel(pair, dir);
231 		tmp_data = tmp_chan->private;
232 		pair->dma_data.dma_request2 = tmp_data->dma_request;
233 		pair->dma_data.peripheral_type = tmp_data->peripheral_type;
234 		pair->dma_data.priority = tmp_data->priority;
235 		dma_release_channel(tmp_chan);
236 
237 		of_dma_node = pair->dma_chan[!dir]->device->dev->of_node;
238 		pair->dma_chan[dir] =
239 			__dma_request_channel(&mask, filter, &pair->dma_data,
240 					      of_dma_node);
241 		pair->req_dma_chan = true;
242 	} else {
243 		pair->dma_chan[dir] = tmp_chan;
244 		/* Do not flag to release if we are reusing the Back-End one */
245 		pair->req_dma_chan = !be_chan;
246 	}
247 
248 	if (!pair->dma_chan[dir]) {
249 		dev_err(dev, "failed to request DMA channel for Back-End\n");
250 		return -EINVAL;
251 	}
252 
253 	width = snd_pcm_format_physical_width(asrc->asrc_format);
254 	if (width < 8 || width > 64)
255 		return -EINVAL;
256 	else if (width == 8)
257 		buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
258 	else if (width == 16)
259 		buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
260 	else if (width == 24)
261 		buswidth = DMA_SLAVE_BUSWIDTH_3_BYTES;
262 	else if (width <= 32)
263 		buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
264 	else
265 		buswidth = DMA_SLAVE_BUSWIDTH_8_BYTES;
266 
267 	config_be.direction = DMA_DEV_TO_DEV;
268 	config_be.src_addr_width = buswidth;
269 	config_be.src_maxburst = dma_params_be->maxburst;
270 	config_be.dst_addr_width = buswidth;
271 	config_be.dst_maxburst = dma_params_be->maxburst;
272 
273 	memset(&audio_config, 0, sizeof(audio_config));
274 	config_be.peripheral_config = &audio_config;
275 	config_be.peripheral_size  = sizeof(audio_config);
276 
277 	if (tx && (be_peripheral_type == IMX_DMATYPE_SSI_DUAL ||
278 		   be_peripheral_type == IMX_DMATYPE_SPDIF))
279 		audio_config.n_fifos_dst = 2;
280 	if (!tx && (be_peripheral_type == IMX_DMATYPE_SSI_DUAL ||
281 		    be_peripheral_type == IMX_DMATYPE_SPDIF))
282 		audio_config.n_fifos_src = 2;
283 
284 	if (tx) {
285 		config_be.src_addr = asrc->paddr + asrc->get_fifo_addr(OUT, index);
286 		config_be.dst_addr = dma_params_be->addr;
287 	} else {
288 		config_be.dst_addr = asrc->paddr + asrc->get_fifo_addr(IN, index);
289 		config_be.src_addr = dma_params_be->addr;
290 	}
291 
292 	ret = dmaengine_slave_config(pair->dma_chan[dir], &config_be);
293 	if (ret) {
294 		dev_err(dev, "failed to config DMA channel for Back-End\n");
295 		if (pair->req_dma_chan)
296 			dma_release_channel(pair->dma_chan[dir]);
297 		return ret;
298 	}
299 
300 	return 0;
301 }
302 
303 static int fsl_asrc_dma_hw_free(struct snd_soc_component *component,
304 				struct snd_pcm_substream *substream)
305 {
306 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
307 	struct snd_pcm_runtime *runtime = substream->runtime;
308 	struct fsl_asrc_pair *pair = runtime->private_data;
309 	u8 dir = tx ? OUT : IN;
310 
311 	if (pair->dma_chan[!dir])
312 		dma_release_channel(pair->dma_chan[!dir]);
313 
314 	/* release dev_to_dev chan if we aren't reusing the Back-End one */
315 	if (pair->dma_chan[dir] && pair->req_dma_chan)
316 		dma_release_channel(pair->dma_chan[dir]);
317 
318 	pair->dma_chan[!dir] = NULL;
319 	pair->dma_chan[dir] = NULL;
320 
321 	return 0;
322 }
323 
324 static int fsl_asrc_dma_startup(struct snd_soc_component *component,
325 				struct snd_pcm_substream *substream)
326 {
327 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
328 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
329 	struct snd_pcm_runtime *runtime = substream->runtime;
330 	struct snd_dmaengine_dai_dma_data *dma_data;
331 	struct device *dev = component->dev;
332 	struct fsl_asrc *asrc = dev_get_drvdata(dev);
333 	struct fsl_asrc_pair *pair;
334 	struct dma_chan *tmp_chan = NULL;
335 	u8 dir = tx ? OUT : IN;
336 	bool release_pair = true;
337 	int ret = 0;
338 
339 	ret = snd_pcm_hw_constraint_integer(substream->runtime,
340 					    SNDRV_PCM_HW_PARAM_PERIODS);
341 	if (ret < 0) {
342 		dev_err(dev, "failed to set pcm hw params periods\n");
343 		return ret;
344 	}
345 
346 	pair = kzalloc(sizeof(*pair) + asrc->pair_priv_size, GFP_KERNEL);
347 	if (!pair)
348 		return -ENOMEM;
349 
350 	pair->asrc = asrc;
351 	pair->private = (void *)pair + sizeof(struct fsl_asrc_pair);
352 
353 	runtime->private_data = pair;
354 
355 	/* Request a dummy pair, which will be released later.
356 	 * Request pair function needs channel num as input, for this
357 	 * dummy pair, we just request "1" channel temporarily.
358 	 */
359 	ret = asrc->request_pair(1, pair);
360 	if (ret < 0) {
361 		dev_err(dev, "failed to request asrc pair\n");
362 		goto req_pair_err;
363 	}
364 
365 	/* Request a dummy dma channel, which will be released later. */
366 	tmp_chan = asrc->get_dma_channel(pair, dir);
367 	if (!tmp_chan) {
368 		dev_err(dev, "failed to get dma channel\n");
369 		ret = -EINVAL;
370 		goto dma_chan_err;
371 	}
372 
373 	dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
374 
375 	/* Refine the snd_imx_hardware according to caps of DMA. */
376 	ret = snd_dmaengine_pcm_refine_runtime_hwparams(substream,
377 							dma_data,
378 							&snd_imx_hardware,
379 							tmp_chan);
380 	if (ret < 0) {
381 		dev_err(dev, "failed to refine runtime hwparams\n");
382 		goto out;
383 	}
384 
385 	release_pair = false;
386 	snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
387 
388 out:
389 	dma_release_channel(tmp_chan);
390 
391 dma_chan_err:
392 	asrc->release_pair(pair);
393 
394 req_pair_err:
395 	if (release_pair)
396 		kfree(pair);
397 
398 	return ret;
399 }
400 
401 static int fsl_asrc_dma_shutdown(struct snd_soc_component *component,
402 				 struct snd_pcm_substream *substream)
403 {
404 	struct snd_pcm_runtime *runtime = substream->runtime;
405 	struct fsl_asrc_pair *pair = runtime->private_data;
406 	struct fsl_asrc *asrc;
407 
408 	if (!pair)
409 		return 0;
410 
411 	asrc = pair->asrc;
412 
413 	if (asrc->pair[pair->index] == pair)
414 		asrc->pair[pair->index] = NULL;
415 
416 	kfree(pair);
417 
418 	return 0;
419 }
420 
421 static snd_pcm_uframes_t
422 fsl_asrc_dma_pcm_pointer(struct snd_soc_component *component,
423 			 struct snd_pcm_substream *substream)
424 {
425 	struct snd_pcm_runtime *runtime = substream->runtime;
426 	struct fsl_asrc_pair *pair = runtime->private_data;
427 
428 	return bytes_to_frames(substream->runtime, pair->pos);
429 }
430 
431 static int fsl_asrc_dma_pcm_new(struct snd_soc_component *component,
432 				struct snd_soc_pcm_runtime *rtd)
433 {
434 	struct snd_card *card = rtd->card->snd_card;
435 	struct snd_pcm *pcm = rtd->pcm;
436 	int ret;
437 
438 	ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
439 	if (ret) {
440 		dev_err(card->dev, "failed to set DMA mask\n");
441 		return ret;
442 	}
443 
444 	return snd_pcm_set_fixed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
445 					    card->dev, FSL_ASRC_DMABUF_SIZE);
446 }
447 
448 struct snd_soc_component_driver fsl_asrc_component = {
449 	.name		= DRV_NAME,
450 	.hw_params	= fsl_asrc_dma_hw_params,
451 	.hw_free	= fsl_asrc_dma_hw_free,
452 	.trigger	= fsl_asrc_dma_trigger,
453 	.open		= fsl_asrc_dma_startup,
454 	.close		= fsl_asrc_dma_shutdown,
455 	.pointer	= fsl_asrc_dma_pcm_pointer,
456 	.pcm_construct	= fsl_asrc_dma_pcm_new,
457 	.legacy_dai_naming = 1,
458 };
459 EXPORT_SYMBOL_GPL(fsl_asrc_component);
460