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
filter(struct dma_chan * chan,void * param)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
fsl_asrc_dma_complete(void * arg)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
fsl_asrc_dma_prepare_and_submit(struct snd_pcm_substream * substream,struct snd_soc_component * component)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
fsl_asrc_dma_trigger(struct snd_soc_component * component,struct snd_pcm_substream * substream,int cmd)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
fsl_asrc_dma_hw_params(struct snd_soc_component * component,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)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 = snd_soc_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_cpu = snd_soc_rtd_to_cpu(be, 0);
160 struct snd_soc_dai *dai_codec = snd_soc_rtd_to_codec(be, 0);
161 struct snd_soc_dai *dai;
162
163 if (dpcm->fe != rtd)
164 continue;
165
166 /*
167 * With audio graph card, original cpu dai is changed to codec
168 * device in backend, so if cpu dai is dummy device in backend,
169 * get the codec dai device, which is the real hardware device
170 * connected.
171 */
172 if (!snd_soc_dai_is_dummy(dai_cpu))
173 dai = dai_cpu;
174 else
175 dai = dai_codec;
176
177 substream_be = snd_soc_dpcm_get_substream(be, stream);
178 dma_params_be = snd_soc_dai_get_dma_data(dai, substream_be);
179 dev_be = dai->dev;
180 break;
181 }
182
183 if (!dma_params_be) {
184 dev_err(dev, "failed to get the substream of Back-End\n");
185 return -EINVAL;
186 }
187
188 /* Override dma_data of the Front-End and config its dmaengine */
189 dma_params_fe = snd_soc_dai_get_dma_data(snd_soc_rtd_to_cpu(rtd, 0), substream);
190 dma_params_fe->addr = asrc->paddr + asrc->get_fifo_addr(!dir, index);
191 dma_params_fe->maxburst = dma_params_be->maxburst;
192
193 pair->dma_chan[!dir] = asrc->get_dma_channel(pair, !dir);
194 if (!pair->dma_chan[!dir]) {
195 dev_err(dev, "failed to request DMA channel\n");
196 return -EINVAL;
197 }
198
199 ret = snd_dmaengine_pcm_prepare_slave_config(substream, params, &config_fe);
200 if (ret) {
201 dev_err(dev, "failed to prepare DMA config for Front-End\n");
202 return ret;
203 }
204
205 ret = dmaengine_slave_config(pair->dma_chan[!dir], &config_fe);
206 if (ret) {
207 dev_err(dev, "failed to config DMA channel for Front-End\n");
208 return ret;
209 }
210
211 /* Request and config DMA channel for Back-End */
212 dma_cap_zero(mask);
213 dma_cap_set(DMA_SLAVE, mask);
214 dma_cap_set(DMA_CYCLIC, mask);
215
216 /*
217 * The Back-End device might have already requested a DMA channel,
218 * so try to reuse it first, and then request a new one upon NULL.
219 */
220 component_be = snd_soc_lookup_component_nolocked(dev_be, SND_DMAENGINE_PCM_DRV_NAME);
221 if (component_be) {
222 struct dmaengine_pcm *pcm = snd_soc_component_to_priv(component_be);
223
224 be_chan = pcm->chan[substream->stream];
225 tmp_chan = be_chan;
226 }
227 if (!tmp_chan) {
228 tmp_chan = dma_request_chan(dev_be, tx ? "tx" : "rx");
229 if (IS_ERR(tmp_chan)) {
230 dev_err(dev, "failed to request DMA channel for Back-End\n");
231 return -EINVAL;
232 }
233 }
234
235 /*
236 * An EDMA DEV_TO_DEV channel is fixed and bound with DMA event of each
237 * peripheral, unlike SDMA channel that is allocated dynamically. So no
238 * need to configure dma_request and dma_request2, but get dma_chan of
239 * Back-End device directly via dma_request_chan.
240 */
241 if (!asrc->use_edma) {
242 /* Get DMA request of Back-End */
243 tmp_data = tmp_chan->private;
244 pair->dma_data.dma_request = tmp_data->dma_request;
245 be_peripheral_type = tmp_data->peripheral_type;
246 if (!be_chan)
247 dma_release_channel(tmp_chan);
248
249 /* Get DMA request of Front-End */
250 tmp_chan = asrc->get_dma_channel(pair, dir);
251 tmp_data = tmp_chan->private;
252 pair->dma_data.dma_request2 = tmp_data->dma_request;
253 pair->dma_data.peripheral_type = tmp_data->peripheral_type;
254 pair->dma_data.priority = tmp_data->priority;
255 dma_release_channel(tmp_chan);
256
257 of_dma_node = pair->dma_chan[!dir]->device->dev->of_node;
258 pair->dma_chan[dir] =
259 __dma_request_channel(&mask, filter, &pair->dma_data,
260 of_dma_node);
261 pair->req_dma_chan = true;
262 } else {
263 pair->dma_chan[dir] = tmp_chan;
264 /* Do not flag to release if we are reusing the Back-End one */
265 pair->req_dma_chan = !be_chan;
266 }
267
268 if (!pair->dma_chan[dir]) {
269 dev_err(dev, "failed to request DMA channel for Back-End\n");
270 return -EINVAL;
271 }
272
273 width = snd_pcm_format_physical_width(asrc->asrc_format);
274 if (width < 8 || width > 64)
275 return -EINVAL;
276 else if (width == 8)
277 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
278 else if (width == 16)
279 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
280 else if (width == 24)
281 buswidth = DMA_SLAVE_BUSWIDTH_3_BYTES;
282 else if (width <= 32)
283 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
284 else
285 buswidth = DMA_SLAVE_BUSWIDTH_8_BYTES;
286
287 config_be.direction = DMA_DEV_TO_DEV;
288 config_be.src_addr_width = buswidth;
289 config_be.src_maxburst = dma_params_be->maxburst;
290 config_be.dst_addr_width = buswidth;
291 config_be.dst_maxburst = dma_params_be->maxburst;
292
293 /*
294 * For eDMA, the back-end may report a maxburst size that is not evenly
295 * divisible by the channel count. This causes the DMA transfer length
296 * to misalign with the FIFO boundary, resulting in wrong data and
297 * audible noise. Align maxburst to the nearest valid boundary:
298 * - If maxburst >= channel count, override to the channel count so
299 * each transfer equals exactly one audio frame.
300 * - If maxburst < channel count, override to 1 to avoid partial-frame
301 * transfers.
302 */
303 if (asrc->use_edma && (dma_params_be->maxburst % params_channels(params))) {
304 if (dma_params_be->maxburst >= params_channels(params)) {
305 config_be.src_maxburst = params_channels(params);
306 config_be.dst_maxburst = params_channels(params);
307 } else {
308 config_be.src_maxburst = 1;
309 config_be.dst_maxburst = 1;
310 }
311 }
312
313 memset(&audio_config, 0, sizeof(audio_config));
314 config_be.peripheral_config = &audio_config;
315 config_be.peripheral_size = sizeof(audio_config);
316
317 if (tx && (be_peripheral_type == IMX_DMATYPE_SSI_DUAL ||
318 be_peripheral_type == IMX_DMATYPE_SPDIF))
319 audio_config.n_fifos_dst = 2;
320 if (!tx && (be_peripheral_type == IMX_DMATYPE_SSI_DUAL ||
321 be_peripheral_type == IMX_DMATYPE_SPDIF))
322 audio_config.n_fifos_src = 2;
323
324 if (tx) {
325 config_be.src_addr = asrc->paddr + asrc->get_fifo_addr(OUT, index);
326 config_be.dst_addr = dma_params_be->addr;
327 } else {
328 config_be.dst_addr = asrc->paddr + asrc->get_fifo_addr(IN, index);
329 config_be.src_addr = dma_params_be->addr;
330 }
331
332 ret = dmaengine_slave_config(pair->dma_chan[dir], &config_be);
333 if (ret) {
334 dev_err(dev, "failed to config DMA channel for Back-End\n");
335 if (pair->req_dma_chan)
336 dma_release_channel(pair->dma_chan[dir]);
337 return ret;
338 }
339
340 return 0;
341 }
342
fsl_asrc_dma_hw_free(struct snd_soc_component * component,struct snd_pcm_substream * substream)343 static int fsl_asrc_dma_hw_free(struct snd_soc_component *component,
344 struct snd_pcm_substream *substream)
345 {
346 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
347 struct snd_pcm_runtime *runtime = substream->runtime;
348 struct fsl_asrc_pair *pair = runtime->private_data;
349 u8 dir = tx ? OUT : IN;
350
351 if (pair->dma_chan[!dir])
352 dma_release_channel(pair->dma_chan[!dir]);
353
354 /* release dev_to_dev chan if we aren't reusing the Back-End one */
355 if (pair->dma_chan[dir] && pair->req_dma_chan)
356 dma_release_channel(pair->dma_chan[dir]);
357
358 pair->dma_chan[!dir] = NULL;
359 pair->dma_chan[dir] = NULL;
360
361 return 0;
362 }
363
fsl_asrc_dma_startup(struct snd_soc_component * component,struct snd_pcm_substream * substream)364 static int fsl_asrc_dma_startup(struct snd_soc_component *component,
365 struct snd_pcm_substream *substream)
366 {
367 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
368 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
369 struct snd_pcm_runtime *runtime = substream->runtime;
370 struct snd_dmaengine_dai_dma_data *dma_data;
371 struct device *dev = component->dev;
372 struct fsl_asrc *asrc = dev_get_drvdata(dev);
373 struct fsl_asrc_pair *pair;
374 struct dma_chan *tmp_chan = NULL;
375 u8 dir = tx ? OUT : IN;
376 bool release_pair = true;
377 int ret = 0;
378
379 ret = snd_pcm_hw_constraint_integer(substream->runtime,
380 SNDRV_PCM_HW_PARAM_PERIODS);
381 if (ret < 0) {
382 dev_err(dev, "failed to set pcm hw params periods\n");
383 return ret;
384 }
385
386 pair = kzalloc(sizeof(*pair) + asrc->pair_priv_size, GFP_KERNEL);
387 if (!pair)
388 return -ENOMEM;
389
390 pair->asrc = asrc;
391 pair->private = (void *)pair + sizeof(struct fsl_asrc_pair);
392
393 runtime->private_data = pair;
394
395 /* Request a dummy pair, which will be released later.
396 * Request pair function needs channel num as input, for this
397 * dummy pair, we just request "1" channel temporarily.
398 */
399 ret = asrc->request_pair(1, pair);
400 if (ret < 0) {
401 dev_err(dev, "failed to request asrc pair\n");
402 goto req_pair_err;
403 }
404
405 /* Request a dummy dma channel, which will be released later. */
406 tmp_chan = asrc->get_dma_channel(pair, dir);
407 if (!tmp_chan) {
408 dev_err(dev, "failed to get dma channel\n");
409 ret = -EINVAL;
410 goto dma_chan_err;
411 }
412
413 dma_data = snd_soc_dai_get_dma_data(snd_soc_rtd_to_cpu(rtd, 0), substream);
414
415 /* Refine the snd_imx_hardware according to caps of DMA. */
416 ret = snd_dmaengine_pcm_refine_runtime_hwparams(substream,
417 dma_data,
418 &snd_imx_hardware,
419 tmp_chan);
420 if (ret < 0) {
421 dev_err(dev, "failed to refine runtime hwparams\n");
422 goto out;
423 }
424
425 release_pair = false;
426 snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
427
428 out:
429 dma_release_channel(tmp_chan);
430
431 dma_chan_err:
432 asrc->release_pair(pair);
433
434 req_pair_err:
435 if (release_pair)
436 kfree(pair);
437
438 return ret;
439 }
440
fsl_asrc_dma_shutdown(struct snd_soc_component * component,struct snd_pcm_substream * substream)441 static int fsl_asrc_dma_shutdown(struct snd_soc_component *component,
442 struct snd_pcm_substream *substream)
443 {
444 struct snd_pcm_runtime *runtime = substream->runtime;
445 struct fsl_asrc_pair *pair = runtime->private_data;
446 struct fsl_asrc *asrc;
447
448 if (!pair)
449 return 0;
450
451 asrc = pair->asrc;
452
453 if (asrc->pair[pair->index] == pair)
454 asrc->pair[pair->index] = NULL;
455
456 kfree(pair);
457
458 return 0;
459 }
460
461 static snd_pcm_uframes_t
fsl_asrc_dma_pcm_pointer(struct snd_soc_component * component,struct snd_pcm_substream * substream)462 fsl_asrc_dma_pcm_pointer(struct snd_soc_component *component,
463 struct snd_pcm_substream *substream)
464 {
465 struct snd_pcm_runtime *runtime = substream->runtime;
466 struct fsl_asrc_pair *pair = runtime->private_data;
467
468 return bytes_to_frames(substream->runtime, pair->pos);
469 }
470
fsl_asrc_dma_pcm_new(struct snd_soc_component * component,struct snd_soc_pcm_runtime * rtd)471 static int fsl_asrc_dma_pcm_new(struct snd_soc_component *component,
472 struct snd_soc_pcm_runtime *rtd)
473 {
474 struct device *dev = component->dev;
475 struct fsl_asrc *asrc = dev_get_drvdata(dev);
476 struct fsl_asrc_pair *pair;
477 struct snd_pcm *pcm = rtd->pcm;
478 struct dma_chan *chan;
479 int ret;
480
481 pair = kzalloc(size_add(sizeof(*pair), asrc->pair_priv_size), GFP_KERNEL);
482 if (!pair)
483 return -ENOMEM;
484
485 pair->asrc = asrc;
486 pair->private = (void *)pair + sizeof(struct fsl_asrc_pair);
487
488 /* Request a pair, which will be released later.
489 * Request pair function needs channel num as input, for this
490 * pair, we just request "1" channel temporarily.
491 */
492 ret = asrc->request_pair(1, pair);
493 if (ret < 0) {
494 dev_err(dev, "failed to request asrc pair\n");
495 goto req_pair_err;
496 }
497
498 /* Request a dma channel, which will be released later. */
499 chan = asrc->get_dma_channel(pair, IN);
500 if (!chan) {
501 dev_err(dev, "failed to get dma channel\n");
502 ret = -EINVAL;
503 goto dma_chan_err;
504 }
505
506 ret = snd_pcm_set_fixed_buffer_all(pcm,
507 SNDRV_DMA_TYPE_DEV,
508 chan->device->dev,
509 FSL_ASRC_DMABUF_SIZE);
510
511 dma_release_channel(chan);
512
513 dma_chan_err:
514 asrc->release_pair(pair);
515
516 req_pair_err:
517 kfree(pair);
518
519 return ret;
520 }
521
522 struct snd_soc_component_driver fsl_asrc_component = {
523 .name = DRV_NAME,
524 .hw_params = fsl_asrc_dma_hw_params,
525 .hw_free = fsl_asrc_dma_hw_free,
526 .trigger = fsl_asrc_dma_trigger,
527 .open = fsl_asrc_dma_startup,
528 .close = fsl_asrc_dma_shutdown,
529 .pointer = fsl_asrc_dma_pcm_pointer,
530 .pcm_new = fsl_asrc_dma_pcm_new,
531 .legacy_dai_naming = 1,
532 #ifdef CONFIG_DEBUG_FS
533 .debugfs_prefix = "asrc",
534 #endif
535 };
536 EXPORT_SYMBOL_GPL(fsl_asrc_component);
537