xref: /openbmc/linux/sound/soc/fsl/fsl_asrc_dma.c (revision 4bb1e4e7)
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.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_priv = pair->asrc_priv;
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;
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 = be->cpu_dai;
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(rtd->cpu_dai, substream);
173 	dma_params_fe->addr = asrc_priv->paddr + REG_ASRDx(!dir, index);
174 	dma_params_fe->maxburst = dma_params_be->maxburst;
175 
176 	pair->dma_chan[!dir] = fsl_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 	/* Get DMA request of Back-End */
201 	tmp_chan = dma_request_slave_channel(dev_be, tx ? "tx" : "rx");
202 	tmp_data = tmp_chan->private;
203 	pair->dma_data.dma_request = tmp_data->dma_request;
204 	dma_release_channel(tmp_chan);
205 
206 	/* Get DMA request of Front-End */
207 	tmp_chan = fsl_asrc_get_dma_channel(pair, dir);
208 	tmp_data = tmp_chan->private;
209 	pair->dma_data.dma_request2 = tmp_data->dma_request;
210 	pair->dma_data.peripheral_type = tmp_data->peripheral_type;
211 	pair->dma_data.priority = tmp_data->priority;
212 	dma_release_channel(tmp_chan);
213 
214 	pair->dma_chan[dir] = dma_request_channel(mask, filter, &pair->dma_data);
215 	if (!pair->dma_chan[dir]) {
216 		dev_err(dev, "failed to request DMA channel for Back-End\n");
217 		return -EINVAL;
218 	}
219 
220 	if (asrc_priv->asrc_width == 16)
221 		buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
222 	else
223 		buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
224 
225 	config_be.direction = DMA_DEV_TO_DEV;
226 	config_be.src_addr_width = buswidth;
227 	config_be.src_maxburst = dma_params_be->maxburst;
228 	config_be.dst_addr_width = buswidth;
229 	config_be.dst_maxburst = dma_params_be->maxburst;
230 
231 	if (tx) {
232 		config_be.src_addr = asrc_priv->paddr + REG_ASRDO(index);
233 		config_be.dst_addr = dma_params_be->addr;
234 	} else {
235 		config_be.dst_addr = asrc_priv->paddr + REG_ASRDI(index);
236 		config_be.src_addr = dma_params_be->addr;
237 	}
238 
239 	ret = dmaengine_slave_config(pair->dma_chan[dir], &config_be);
240 	if (ret) {
241 		dev_err(dev, "failed to config DMA channel for Back-End\n");
242 		return ret;
243 	}
244 
245 	snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
246 
247 	return 0;
248 }
249 
250 static int fsl_asrc_dma_hw_free(struct snd_soc_component *component,
251 				struct snd_pcm_substream *substream)
252 {
253 	struct snd_pcm_runtime *runtime = substream->runtime;
254 	struct fsl_asrc_pair *pair = runtime->private_data;
255 
256 	snd_pcm_set_runtime_buffer(substream, NULL);
257 
258 	if (pair->dma_chan[IN])
259 		dma_release_channel(pair->dma_chan[IN]);
260 
261 	if (pair->dma_chan[OUT])
262 		dma_release_channel(pair->dma_chan[OUT]);
263 
264 	pair->dma_chan[IN] = NULL;
265 	pair->dma_chan[OUT] = NULL;
266 
267 	return 0;
268 }
269 
270 static int fsl_asrc_dma_startup(struct snd_soc_component *component,
271 				struct snd_pcm_substream *substream)
272 {
273 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
274 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
275 	struct snd_pcm_runtime *runtime = substream->runtime;
276 	struct snd_dmaengine_dai_dma_data *dma_data;
277 	struct device *dev = component->dev;
278 	struct fsl_asrc *asrc_priv = dev_get_drvdata(dev);
279 	struct fsl_asrc_pair *pair;
280 	struct dma_chan *tmp_chan = NULL;
281 	u8 dir = tx ? OUT : IN;
282 	bool release_pair = true;
283 	int ret = 0;
284 
285 	ret = snd_pcm_hw_constraint_integer(substream->runtime,
286 					    SNDRV_PCM_HW_PARAM_PERIODS);
287 	if (ret < 0) {
288 		dev_err(dev, "failed to set pcm hw params periods\n");
289 		return ret;
290 	}
291 
292 	pair = kzalloc(sizeof(struct fsl_asrc_pair), GFP_KERNEL);
293 	if (!pair)
294 		return -ENOMEM;
295 
296 	pair->asrc_priv = asrc_priv;
297 
298 	runtime->private_data = pair;
299 
300 	/* Request a dummy pair, which will be released later.
301 	 * Request pair function needs channel num as input, for this
302 	 * dummy pair, we just request "1" channel temporarily.
303 	 */
304 	ret = fsl_asrc_request_pair(1, pair);
305 	if (ret < 0) {
306 		dev_err(dev, "failed to request asrc pair\n");
307 		goto req_pair_err;
308 	}
309 
310 	/* Request a dummy dma channel, which will be released later. */
311 	tmp_chan = fsl_asrc_get_dma_channel(pair, dir);
312 	if (!tmp_chan) {
313 		dev_err(dev, "failed to get dma channel\n");
314 		ret = -EINVAL;
315 		goto dma_chan_err;
316 	}
317 
318 	dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
319 
320 	/* Refine the snd_imx_hardware according to caps of DMA. */
321 	ret = snd_dmaengine_pcm_refine_runtime_hwparams(substream,
322 							dma_data,
323 							&snd_imx_hardware,
324 							tmp_chan);
325 	if (ret < 0) {
326 		dev_err(dev, "failed to refine runtime hwparams\n");
327 		goto out;
328 	}
329 
330 	release_pair = false;
331 	snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
332 
333 out:
334 	dma_release_channel(tmp_chan);
335 
336 dma_chan_err:
337 	fsl_asrc_release_pair(pair);
338 
339 req_pair_err:
340 	if (release_pair)
341 		kfree(pair);
342 
343 	return ret;
344 }
345 
346 static int fsl_asrc_dma_shutdown(struct snd_soc_component *component,
347 				 struct snd_pcm_substream *substream)
348 {
349 	struct snd_pcm_runtime *runtime = substream->runtime;
350 	struct fsl_asrc_pair *pair = runtime->private_data;
351 	struct fsl_asrc *asrc_priv;
352 
353 	if (!pair)
354 		return 0;
355 
356 	asrc_priv = pair->asrc_priv;
357 
358 	if (asrc_priv->pair[pair->index] == pair)
359 		asrc_priv->pair[pair->index] = NULL;
360 
361 	kfree(pair);
362 
363 	return 0;
364 }
365 
366 static snd_pcm_uframes_t
367 fsl_asrc_dma_pcm_pointer(struct snd_soc_component *component,
368 			 struct snd_pcm_substream *substream)
369 {
370 	struct snd_pcm_runtime *runtime = substream->runtime;
371 	struct fsl_asrc_pair *pair = runtime->private_data;
372 
373 	return bytes_to_frames(substream->runtime, pair->pos);
374 }
375 
376 static int fsl_asrc_dma_pcm_new(struct snd_soc_component *component,
377 				struct snd_soc_pcm_runtime *rtd)
378 {
379 	struct snd_card *card = rtd->card->snd_card;
380 	struct snd_pcm_substream *substream;
381 	struct snd_pcm *pcm = rtd->pcm;
382 	int ret, i;
383 
384 	ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
385 	if (ret) {
386 		dev_err(card->dev, "failed to set DMA mask\n");
387 		return ret;
388 	}
389 
390 	for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_LAST; i++) {
391 		substream = pcm->streams[i].substream;
392 		if (!substream)
393 			continue;
394 
395 		ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, pcm->card->dev,
396 				FSL_ASRC_DMABUF_SIZE, &substream->dma_buffer);
397 		if (ret) {
398 			dev_err(card->dev, "failed to allocate DMA buffer\n");
399 			goto err;
400 		}
401 	}
402 
403 	return 0;
404 
405 err:
406 	if (--i == 0 && pcm->streams[i].substream)
407 		snd_dma_free_pages(&pcm->streams[i].substream->dma_buffer);
408 
409 	return ret;
410 }
411 
412 static void fsl_asrc_dma_pcm_free(struct snd_soc_component *component,
413 				  struct snd_pcm *pcm)
414 {
415 	struct snd_pcm_substream *substream;
416 	int i;
417 
418 	for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_LAST; i++) {
419 		substream = pcm->streams[i].substream;
420 		if (!substream)
421 			continue;
422 
423 		snd_dma_free_pages(&substream->dma_buffer);
424 		substream->dma_buffer.area = NULL;
425 		substream->dma_buffer.addr = 0;
426 	}
427 }
428 
429 struct snd_soc_component_driver fsl_asrc_component = {
430 	.name		= DRV_NAME,
431 	.ioctl		= snd_soc_pcm_lib_ioctl,
432 	.hw_params	= fsl_asrc_dma_hw_params,
433 	.hw_free	= fsl_asrc_dma_hw_free,
434 	.trigger	= fsl_asrc_dma_trigger,
435 	.open		= fsl_asrc_dma_startup,
436 	.close		= fsl_asrc_dma_shutdown,
437 	.pointer	= fsl_asrc_dma_pcm_pointer,
438 	.pcm_construct	= fsl_asrc_dma_pcm_new,
439 	.pcm_destruct	= fsl_asrc_dma_pcm_free,
440 };
441 EXPORT_SYMBOL_GPL(fsl_asrc_component);
442