xref: /openbmc/linux/sound/soc/fsl/fsl_asrc_dma.c (revision c8ed9fc9)
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/platform_data/dma-imx.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_all(pair->dma_chan[OUT]);
118 		dmaengine_terminate_all(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 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
133 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
134 	struct snd_dmaengine_dai_dma_data *dma_params_fe = NULL;
135 	struct snd_dmaengine_dai_dma_data *dma_params_be = NULL;
136 	struct snd_pcm_runtime *runtime = substream->runtime;
137 	struct fsl_asrc_pair *pair = runtime->private_data;
138 	struct fsl_asrc *asrc = pair->asrc;
139 	struct dma_slave_config config_fe, config_be;
140 	enum asrc_pair_index index = pair->index;
141 	struct device *dev = component->dev;
142 	int stream = substream->stream;
143 	struct imx_dma_data *tmp_data;
144 	struct snd_soc_dpcm *dpcm;
145 	struct dma_chan *tmp_chan;
146 	struct device *dev_be;
147 	u8 dir = tx ? OUT : IN;
148 	dma_cap_mask_t mask;
149 	int ret, width;
150 
151 	/* Fetch the Back-End dma_data from DPCM */
152 	for_each_dpcm_be(rtd, stream, dpcm) {
153 		struct snd_soc_pcm_runtime *be = dpcm->be;
154 		struct snd_pcm_substream *substream_be;
155 		struct snd_soc_dai *dai = asoc_rtd_to_cpu(be, 0);
156 
157 		if (dpcm->fe != rtd)
158 			continue;
159 
160 		substream_be = snd_soc_dpcm_get_substream(be, stream);
161 		dma_params_be = snd_soc_dai_get_dma_data(dai, substream_be);
162 		dev_be = dai->dev;
163 		break;
164 	}
165 
166 	if (!dma_params_be) {
167 		dev_err(dev, "failed to get the substream of Back-End\n");
168 		return -EINVAL;
169 	}
170 
171 	/* Override dma_data of the Front-End and config its dmaengine */
172 	dma_params_fe = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
173 	dma_params_fe->addr = asrc->paddr + asrc->get_fifo_addr(!dir, index);
174 	dma_params_fe->maxburst = dma_params_be->maxburst;
175 
176 	pair->dma_chan[!dir] = asrc->get_dma_channel(pair, !dir);
177 	if (!pair->dma_chan[!dir]) {
178 		dev_err(dev, "failed to request DMA channel\n");
179 		return -EINVAL;
180 	}
181 
182 	memset(&config_fe, 0, sizeof(config_fe));
183 	ret = snd_dmaengine_pcm_prepare_slave_config(substream, params, &config_fe);
184 	if (ret) {
185 		dev_err(dev, "failed to prepare DMA config for Front-End\n");
186 		return ret;
187 	}
188 
189 	ret = dmaengine_slave_config(pair->dma_chan[!dir], &config_fe);
190 	if (ret) {
191 		dev_err(dev, "failed to config DMA channel for Front-End\n");
192 		return ret;
193 	}
194 
195 	/* Request and config DMA channel for Back-End */
196 	dma_cap_zero(mask);
197 	dma_cap_set(DMA_SLAVE, mask);
198 	dma_cap_set(DMA_CYCLIC, mask);
199 
200 	/*
201 	 * An EDMA DEV_TO_DEV channel is fixed and bound with DMA event of each
202 	 * peripheral, unlike SDMA channel that is allocated dynamically. So no
203 	 * need to configure dma_request and dma_request2, but get dma_chan via
204 	 * dma_request_slave_channel directly with dma name of Front-End device
205 	 */
206 	if (!asrc->use_edma) {
207 		/* Get DMA request of Back-End */
208 		tmp_chan = dma_request_slave_channel(dev_be, tx ? "tx" : "rx");
209 		tmp_data = tmp_chan->private;
210 		pair->dma_data.dma_request = tmp_data->dma_request;
211 		dma_release_channel(tmp_chan);
212 
213 		/* Get DMA request of Front-End */
214 		tmp_chan = asrc->get_dma_channel(pair, dir);
215 		tmp_data = tmp_chan->private;
216 		pair->dma_data.dma_request2 = tmp_data->dma_request;
217 		pair->dma_data.peripheral_type = tmp_data->peripheral_type;
218 		pair->dma_data.priority = tmp_data->priority;
219 		dma_release_channel(tmp_chan);
220 
221 		pair->dma_chan[dir] =
222 			dma_request_channel(mask, filter, &pair->dma_data);
223 	} else {
224 		pair->dma_chan[dir] =
225 			asrc->get_dma_channel(pair, dir);
226 	}
227 
228 	if (!pair->dma_chan[dir]) {
229 		dev_err(dev, "failed to request DMA channel for Back-End\n");
230 		return -EINVAL;
231 	}
232 
233 	width = snd_pcm_format_physical_width(asrc->asrc_format);
234 	if (width < 8 || width > 64)
235 		return -EINVAL;
236 	else if (width == 8)
237 		buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
238 	else if (width == 16)
239 		buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
240 	else if (width == 24)
241 		buswidth = DMA_SLAVE_BUSWIDTH_3_BYTES;
242 	else if (width <= 32)
243 		buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
244 	else
245 		buswidth = DMA_SLAVE_BUSWIDTH_8_BYTES;
246 
247 	config_be.direction = DMA_DEV_TO_DEV;
248 	config_be.src_addr_width = buswidth;
249 	config_be.src_maxburst = dma_params_be->maxburst;
250 	config_be.dst_addr_width = buswidth;
251 	config_be.dst_maxburst = dma_params_be->maxburst;
252 
253 	if (tx) {
254 		config_be.src_addr = asrc->paddr + asrc->get_fifo_addr(OUT, index);
255 		config_be.dst_addr = dma_params_be->addr;
256 	} else {
257 		config_be.dst_addr = asrc->paddr + asrc->get_fifo_addr(IN, index);
258 		config_be.src_addr = dma_params_be->addr;
259 	}
260 
261 	ret = dmaengine_slave_config(pair->dma_chan[dir], &config_be);
262 	if (ret) {
263 		dev_err(dev, "failed to config DMA channel for Back-End\n");
264 		dma_release_channel(pair->dma_chan[dir]);
265 		return ret;
266 	}
267 
268 	snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
269 
270 	return 0;
271 }
272 
273 static int fsl_asrc_dma_hw_free(struct snd_soc_component *component,
274 				struct snd_pcm_substream *substream)
275 {
276 	struct snd_pcm_runtime *runtime = substream->runtime;
277 	struct fsl_asrc_pair *pair = runtime->private_data;
278 
279 	snd_pcm_set_runtime_buffer(substream, NULL);
280 
281 	if (pair->dma_chan[IN])
282 		dma_release_channel(pair->dma_chan[IN]);
283 
284 	if (pair->dma_chan[OUT])
285 		dma_release_channel(pair->dma_chan[OUT]);
286 
287 	pair->dma_chan[IN] = NULL;
288 	pair->dma_chan[OUT] = NULL;
289 
290 	return 0;
291 }
292 
293 static int fsl_asrc_dma_startup(struct snd_soc_component *component,
294 				struct snd_pcm_substream *substream)
295 {
296 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
297 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
298 	struct snd_pcm_runtime *runtime = substream->runtime;
299 	struct snd_dmaengine_dai_dma_data *dma_data;
300 	struct device *dev = component->dev;
301 	struct fsl_asrc *asrc = dev_get_drvdata(dev);
302 	struct fsl_asrc_pair *pair;
303 	struct dma_chan *tmp_chan = NULL;
304 	u8 dir = tx ? OUT : IN;
305 	bool release_pair = true;
306 	int ret = 0;
307 
308 	ret = snd_pcm_hw_constraint_integer(substream->runtime,
309 					    SNDRV_PCM_HW_PARAM_PERIODS);
310 	if (ret < 0) {
311 		dev_err(dev, "failed to set pcm hw params periods\n");
312 		return ret;
313 	}
314 
315 	pair = kzalloc(sizeof(*pair) + asrc->pair_priv_size, GFP_KERNEL);
316 	if (!pair)
317 		return -ENOMEM;
318 
319 	pair->asrc = asrc;
320 	pair->private = (void *)pair + sizeof(struct fsl_asrc_pair);
321 
322 	runtime->private_data = pair;
323 
324 	/* Request a dummy pair, which will be released later.
325 	 * Request pair function needs channel num as input, for this
326 	 * dummy pair, we just request "1" channel temporarily.
327 	 */
328 	ret = asrc->request_pair(1, pair);
329 	if (ret < 0) {
330 		dev_err(dev, "failed to request asrc pair\n");
331 		goto req_pair_err;
332 	}
333 
334 	/* Request a dummy dma channel, which will be released later. */
335 	tmp_chan = asrc->get_dma_channel(pair, dir);
336 	if (!tmp_chan) {
337 		dev_err(dev, "failed to get dma channel\n");
338 		ret = -EINVAL;
339 		goto dma_chan_err;
340 	}
341 
342 	dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
343 
344 	/* Refine the snd_imx_hardware according to caps of DMA. */
345 	ret = snd_dmaengine_pcm_refine_runtime_hwparams(substream,
346 							dma_data,
347 							&snd_imx_hardware,
348 							tmp_chan);
349 	if (ret < 0) {
350 		dev_err(dev, "failed to refine runtime hwparams\n");
351 		goto out;
352 	}
353 
354 	release_pair = false;
355 	snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
356 
357 out:
358 	dma_release_channel(tmp_chan);
359 
360 dma_chan_err:
361 	asrc->release_pair(pair);
362 
363 req_pair_err:
364 	if (release_pair)
365 		kfree(pair);
366 
367 	return ret;
368 }
369 
370 static int fsl_asrc_dma_shutdown(struct snd_soc_component *component,
371 				 struct snd_pcm_substream *substream)
372 {
373 	struct snd_pcm_runtime *runtime = substream->runtime;
374 	struct fsl_asrc_pair *pair = runtime->private_data;
375 	struct fsl_asrc *asrc;
376 
377 	if (!pair)
378 		return 0;
379 
380 	asrc = pair->asrc;
381 
382 	if (asrc->pair[pair->index] == pair)
383 		asrc->pair[pair->index] = NULL;
384 
385 	kfree(pair);
386 
387 	return 0;
388 }
389 
390 static snd_pcm_uframes_t
391 fsl_asrc_dma_pcm_pointer(struct snd_soc_component *component,
392 			 struct snd_pcm_substream *substream)
393 {
394 	struct snd_pcm_runtime *runtime = substream->runtime;
395 	struct fsl_asrc_pair *pair = runtime->private_data;
396 
397 	return bytes_to_frames(substream->runtime, pair->pos);
398 }
399 
400 static int fsl_asrc_dma_pcm_new(struct snd_soc_component *component,
401 				struct snd_soc_pcm_runtime *rtd)
402 {
403 	struct snd_card *card = rtd->card->snd_card;
404 	struct snd_pcm_substream *substream;
405 	struct snd_pcm *pcm = rtd->pcm;
406 	int ret, i;
407 
408 	ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
409 	if (ret) {
410 		dev_err(card->dev, "failed to set DMA mask\n");
411 		return ret;
412 	}
413 
414 	for_each_pcm_streams(i) {
415 		substream = pcm->streams[i].substream;
416 		if (!substream)
417 			continue;
418 
419 		ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, pcm->card->dev,
420 				FSL_ASRC_DMABUF_SIZE, &substream->dma_buffer);
421 		if (ret) {
422 			dev_err(card->dev, "failed to allocate DMA buffer\n");
423 			goto err;
424 		}
425 	}
426 
427 	return 0;
428 
429 err:
430 	if (--i == 0 && pcm->streams[i].substream)
431 		snd_dma_free_pages(&pcm->streams[i].substream->dma_buffer);
432 
433 	return ret;
434 }
435 
436 static void fsl_asrc_dma_pcm_free(struct snd_soc_component *component,
437 				  struct snd_pcm *pcm)
438 {
439 	struct snd_pcm_substream *substream;
440 	int i;
441 
442 	for_each_pcm_streams(i) {
443 		substream = pcm->streams[i].substream;
444 		if (!substream)
445 			continue;
446 
447 		snd_dma_free_pages(&substream->dma_buffer);
448 		substream->dma_buffer.area = NULL;
449 		substream->dma_buffer.addr = 0;
450 	}
451 }
452 
453 struct snd_soc_component_driver fsl_asrc_component = {
454 	.name		= DRV_NAME,
455 	.hw_params	= fsl_asrc_dma_hw_params,
456 	.hw_free	= fsl_asrc_dma_hw_free,
457 	.trigger	= fsl_asrc_dma_trigger,
458 	.open		= fsl_asrc_dma_startup,
459 	.close		= fsl_asrc_dma_shutdown,
460 	.pointer	= fsl_asrc_dma_pcm_pointer,
461 	.pcm_construct	= fsl_asrc_dma_pcm_new,
462 	.pcm_destruct	= fsl_asrc_dma_pcm_free,
463 };
464 EXPORT_SYMBOL_GPL(fsl_asrc_component);
465