1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Copyright (C) 2013, Analog Devices Inc.
4 // Author: Lars-Peter Clausen <lars@metafoo.de>
5
6 #include <linux/acpi.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/dmaengine.h>
10 #include <linux/slab.h>
11 #include <sound/pcm.h>
12 #include <sound/pcm_params.h>
13 #include <sound/soc.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/of.h>
16
17 #include <sound/dmaengine_pcm.h>
18
19 static unsigned int prealloc_buffer_size_kbytes = 512;
20 module_param(prealloc_buffer_size_kbytes, uint, 0444);
21 MODULE_PARM_DESC(prealloc_buffer_size_kbytes, "Preallocate DMA buffer size (KB).");
22
23 /*
24 * The platforms dmaengine driver does not support reporting the amount of
25 * bytes that are still left to transfer.
26 */
27 #define SND_DMAENGINE_PCM_FLAG_NO_RESIDUE BIT(31)
28
dmaengine_dma_dev(struct dmaengine_pcm * pcm,struct snd_pcm_substream * substream)29 static struct device *dmaengine_dma_dev(struct dmaengine_pcm *pcm,
30 struct snd_pcm_substream *substream)
31 {
32 if (!pcm->chan[substream->stream])
33 return NULL;
34
35 return pcm->chan[substream->stream]->device->dev;
36 }
37
38 /**
39 * snd_dmaengine_pcm_prepare_slave_config() - Generic prepare_slave_config callback
40 * @substream: PCM substream
41 * @params: hw_params
42 * @slave_config: DMA slave config to prepare
43 *
44 * This function can be used as a generic prepare_slave_config callback for
45 * platforms which make use of the snd_dmaengine_dai_dma_data struct for their
46 * DAI DMA data. Internally the function will first call
47 * snd_hwparams_to_dma_slave_config to fill in the slave config based on the
48 * hw_params, followed by snd_dmaengine_pcm_set_config_from_dai_data to fill in
49 * the remaining fields based on the DAI DMA data.
50 */
snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct dma_slave_config * slave_config)51 int snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream *substream,
52 struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config)
53 {
54 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
55 struct snd_dmaengine_dai_dma_data *dma_data;
56 int ret;
57
58 if (rtd->dai_link->num_cpus > 1) {
59 dev_err(rtd->dev,
60 "%s doesn't support Multi CPU yet\n", __func__);
61 return -EINVAL;
62 }
63
64 dma_data = snd_soc_dai_get_dma_data(snd_soc_rtd_to_cpu(rtd, 0), substream);
65
66 ret = snd_hwparams_to_dma_slave_config(substream, params, slave_config);
67 if (ret)
68 return ret;
69
70 snd_dmaengine_pcm_set_config_from_dai_data(substream, dma_data,
71 slave_config);
72
73 return 0;
74 }
75 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_prepare_slave_config);
76
dmaengine_pcm_hw_params(struct snd_soc_component * component,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)77 static int dmaengine_pcm_hw_params(struct snd_soc_component *component,
78 struct snd_pcm_substream *substream,
79 struct snd_pcm_hw_params *params)
80 {
81 struct dmaengine_pcm *pcm = snd_soc_component_to_priv(component);
82 struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
83 struct dma_slave_config slave_config;
84 int ret;
85
86 if (!pcm->config->prepare_slave_config)
87 return 0;
88
89 memset(&slave_config, 0, sizeof(slave_config));
90
91 ret = pcm->config->prepare_slave_config(substream, params, &slave_config);
92 if (ret)
93 return ret;
94
95 return dmaengine_slave_config(chan, &slave_config);
96 }
97
98 static int
dmaengine_pcm_set_runtime_hwparams(struct snd_soc_component * component,struct snd_pcm_substream * substream)99 dmaengine_pcm_set_runtime_hwparams(struct snd_soc_component *component,
100 struct snd_pcm_substream *substream)
101 {
102 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
103 struct dmaengine_pcm *pcm = snd_soc_component_to_priv(component);
104 struct device *dma_dev = dmaengine_dma_dev(pcm, substream);
105 struct dma_chan *chan = pcm->chan[substream->stream];
106 struct snd_dmaengine_dai_dma_data *dma_data;
107 struct snd_pcm_hardware hw;
108
109 if (rtd->dai_link->num_cpus > 1) {
110 dev_err(rtd->dev,
111 "%s doesn't support Multi CPU yet\n", __func__);
112 return -EINVAL;
113 }
114
115 if (pcm->config->pcm_hardware)
116 return snd_soc_set_runtime_hwparams(substream,
117 pcm->config->pcm_hardware);
118
119 dma_data = snd_soc_dai_get_dma_data(snd_soc_rtd_to_cpu(rtd, 0), substream);
120
121 memset(&hw, 0, sizeof(hw));
122 hw.info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
123 SNDRV_PCM_INFO_INTERLEAVED;
124 hw.periods_min = 2;
125 hw.periods_max = UINT_MAX;
126 hw.period_bytes_min = dma_data->maxburst * DMA_SLAVE_BUSWIDTH_8_BYTES;
127 if (!hw.period_bytes_min)
128 hw.period_bytes_min = 256;
129 hw.period_bytes_max = dma_get_max_seg_size(dma_dev);
130 hw.buffer_bytes_max = SIZE_MAX;
131 hw.fifo_size = dma_data->fifo_size;
132
133 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
134 hw.info |= SNDRV_PCM_INFO_BATCH;
135
136 /**
137 * FIXME: Remove the return value check to align with the code
138 * before adding snd_dmaengine_pcm_refine_runtime_hwparams
139 * function.
140 */
141 snd_dmaengine_pcm_refine_runtime_hwparams(substream,
142 dma_data,
143 &hw,
144 chan);
145
146 return snd_soc_set_runtime_hwparams(substream, &hw);
147 }
148
dmaengine_pcm_open(struct snd_soc_component * component,struct snd_pcm_substream * substream)149 static int dmaengine_pcm_open(struct snd_soc_component *component,
150 struct snd_pcm_substream *substream)
151 {
152 struct dmaengine_pcm *pcm = snd_soc_component_to_priv(component);
153 struct dma_chan *chan = pcm->chan[substream->stream];
154 int ret;
155
156 ret = dmaengine_pcm_set_runtime_hwparams(component, substream);
157 if (ret)
158 return ret;
159
160 return snd_dmaengine_pcm_open(substream, chan);
161 }
162
dmaengine_pcm_close(struct snd_soc_component * component,struct snd_pcm_substream * substream)163 static int dmaengine_pcm_close(struct snd_soc_component *component,
164 struct snd_pcm_substream *substream)
165 {
166 return snd_dmaengine_pcm_close(substream);
167 }
168
dmaengine_pcm_trigger(struct snd_soc_component * component,struct snd_pcm_substream * substream,int cmd)169 static int dmaengine_pcm_trigger(struct snd_soc_component *component,
170 struct snd_pcm_substream *substream, int cmd)
171 {
172 return snd_dmaengine_pcm_trigger(substream, cmd);
173 }
174
dmaengine_pcm_compat_request_channel(struct snd_soc_component * component,struct snd_soc_pcm_runtime * rtd,struct snd_pcm_substream * substream)175 static struct dma_chan *dmaengine_pcm_compat_request_channel(
176 struct snd_soc_component *component,
177 struct snd_soc_pcm_runtime *rtd,
178 struct snd_pcm_substream *substream)
179 {
180 struct dmaengine_pcm *pcm = snd_soc_component_to_priv(component);
181 struct snd_dmaengine_dai_dma_data *dma_data;
182
183 if (rtd->dai_link->num_cpus > 1) {
184 dev_err(rtd->dev,
185 "%s doesn't support Multi CPU yet\n", __func__);
186 return NULL;
187 }
188
189 dma_data = snd_soc_dai_get_dma_data(snd_soc_rtd_to_cpu(rtd, 0), substream);
190
191 if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) && pcm->chan[0])
192 return pcm->chan[0];
193
194 if (pcm->config->compat_request_channel)
195 return pcm->config->compat_request_channel(rtd, substream);
196
197 return snd_dmaengine_pcm_request_channel(pcm->config->compat_filter_fn,
198 dma_data->filter_data);
199 }
200
dmaengine_pcm_can_report_residue(struct device * dev,struct dma_chan * chan)201 static bool dmaengine_pcm_can_report_residue(struct device *dev,
202 struct dma_chan *chan)
203 {
204 struct dma_slave_caps dma_caps;
205 int ret;
206
207 ret = dma_get_slave_caps(chan, &dma_caps);
208 if (ret != 0) {
209 dev_warn(dev, "Failed to get DMA channel capabilities, falling back to period counting: %d\n",
210 ret);
211 return false;
212 }
213
214 if (dma_caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR)
215 return false;
216
217 return true;
218 }
219
dmaengine_pcm_new(struct snd_soc_component * component,struct snd_soc_pcm_runtime * rtd)220 static int dmaengine_pcm_new(struct snd_soc_component *component,
221 struct snd_soc_pcm_runtime *rtd)
222 {
223 struct dmaengine_pcm *pcm = snd_soc_component_to_priv(component);
224 const struct snd_dmaengine_pcm_config *config = pcm->config;
225 struct device *dev = component->dev;
226 size_t prealloc_buffer_size;
227 size_t max_buffer_size;
228 unsigned int i;
229
230 if (config->prealloc_buffer_size)
231 prealloc_buffer_size = config->prealloc_buffer_size;
232 else
233 prealloc_buffer_size = prealloc_buffer_size_kbytes * 1024;
234
235 if (config->pcm_hardware && config->pcm_hardware->buffer_bytes_max)
236 max_buffer_size = config->pcm_hardware->buffer_bytes_max;
237 else
238 max_buffer_size = SIZE_MAX;
239
240 for_each_pcm_streams(i) {
241 struct snd_pcm_substream *substream = rtd->pcm->streams[i].substream;
242 if (!substream)
243 continue;
244
245 if (!pcm->chan[i] && config->chan_names[i])
246 pcm->chan[i] = dma_request_slave_channel(dev,
247 config->chan_names[i]);
248
249 if (!pcm->chan[i] && (pcm->flags & SND_DMAENGINE_PCM_FLAG_COMPAT)) {
250 pcm->chan[i] = dmaengine_pcm_compat_request_channel(
251 component, rtd, substream);
252 }
253
254 if (!pcm->chan[i]) {
255 dev_err(component->dev,
256 "Missing dma channel for stream: %d\n", i);
257 return -EINVAL;
258 }
259
260 snd_pcm_set_managed_buffer(substream,
261 SNDRV_DMA_TYPE_DEV_IRAM,
262 dmaengine_dma_dev(pcm, substream),
263 prealloc_buffer_size,
264 max_buffer_size);
265
266 if (!dmaengine_pcm_can_report_residue(dev, pcm->chan[i]))
267 pcm->flags |= SND_DMAENGINE_PCM_FLAG_NO_RESIDUE;
268
269 if (rtd->pcm->streams[i].pcm->name[0] == '\0') {
270 strscpy_pad(rtd->pcm->streams[i].pcm->name,
271 rtd->pcm->streams[i].pcm->id,
272 sizeof(rtd->pcm->streams[i].pcm->name));
273 }
274 }
275
276 return 0;
277 }
278
dmaengine_pcm_pointer(struct snd_soc_component * component,struct snd_pcm_substream * substream)279 static snd_pcm_uframes_t dmaengine_pcm_pointer(
280 struct snd_soc_component *component,
281 struct snd_pcm_substream *substream)
282 {
283 struct dmaengine_pcm *pcm = snd_soc_component_to_priv(component);
284
285 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
286 return snd_dmaengine_pcm_pointer_no_residue(substream);
287 else
288 return snd_dmaengine_pcm_pointer(substream);
289 }
290
dmaengine_copy(struct snd_soc_component * component,struct snd_pcm_substream * substream,int channel,unsigned long hwoff,struct iov_iter * iter,unsigned long bytes)291 static int dmaengine_copy(struct snd_soc_component *component,
292 struct snd_pcm_substream *substream,
293 int channel, unsigned long hwoff,
294 struct iov_iter *iter, unsigned long bytes)
295 {
296 struct snd_pcm_runtime *runtime = substream->runtime;
297 struct dmaengine_pcm *pcm = snd_soc_component_to_priv(component);
298 int (*process)(struct snd_pcm_substream *substream,
299 int channel, unsigned long hwoff,
300 unsigned long bytes) = pcm->config->process;
301 bool is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
302 void *dma_ptr = runtime->dma_area + hwoff +
303 channel * (runtime->dma_bytes / runtime->channels);
304
305 if (is_playback)
306 if (copy_from_iter(dma_ptr, bytes, iter) != bytes)
307 return -EFAULT;
308
309 if (process) {
310 int ret = process(substream, channel, hwoff, bytes);
311 if (ret < 0)
312 return ret;
313 }
314
315 if (!is_playback)
316 if (copy_to_iter(dma_ptr, bytes, iter) != bytes)
317 return -EFAULT;
318
319 return 0;
320 }
321
dmaengine_pcm_sync_stop(struct snd_soc_component * component,struct snd_pcm_substream * substream)322 static int dmaengine_pcm_sync_stop(struct snd_soc_component *component,
323 struct snd_pcm_substream *substream)
324 {
325 return snd_dmaengine_pcm_sync_stop(substream);
326 }
327
328 static const struct snd_soc_component_driver dmaengine_pcm_component = {
329 .name = SND_DMAENGINE_PCM_DRV_NAME,
330 .probe_order = SND_SOC_COMP_ORDER_LATE,
331 .open = dmaengine_pcm_open,
332 .close = dmaengine_pcm_close,
333 .hw_params = dmaengine_pcm_hw_params,
334 .trigger = dmaengine_pcm_trigger,
335 .pointer = dmaengine_pcm_pointer,
336 .pcm_new = dmaengine_pcm_new,
337 .sync_stop = dmaengine_pcm_sync_stop,
338 .debugfs_prefix = "dma",
339 };
340
341 static const struct snd_soc_component_driver dmaengine_pcm_component_process = {
342 .name = SND_DMAENGINE_PCM_DRV_NAME,
343 .probe_order = SND_SOC_COMP_ORDER_LATE,
344 .open = dmaengine_pcm_open,
345 .close = dmaengine_pcm_close,
346 .hw_params = dmaengine_pcm_hw_params,
347 .trigger = dmaengine_pcm_trigger,
348 .pointer = dmaengine_pcm_pointer,
349 .copy = dmaengine_copy,
350 .pcm_new = dmaengine_pcm_new,
351 .sync_stop = dmaengine_pcm_sync_stop,
352 .debugfs_prefix = "dma",
353 };
354
355 static const char * const dmaengine_pcm_dma_channel_names[] = {
356 [SNDRV_PCM_STREAM_PLAYBACK] = "tx",
357 [SNDRV_PCM_STREAM_CAPTURE] = "rx",
358 };
359
dmaengine_pcm_request_chan_of(struct dmaengine_pcm * pcm,struct device * dev,const struct snd_dmaengine_pcm_config * config)360 static int dmaengine_pcm_request_chan_of(struct dmaengine_pcm *pcm,
361 struct device *dev, const struct snd_dmaengine_pcm_config *config)
362 {
363 unsigned int i;
364 const char *name;
365 struct dma_chan *chan;
366
367 if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_DT) || (!dev->of_node &&
368 !(config->dma_dev && config->dma_dev->of_node)))
369 return 0;
370
371 if (config->dma_dev) {
372 /*
373 * If this warning is seen, it probably means that your Linux
374 * device structure does not match your HW device structure.
375 * It would be best to refactor the Linux device structure to
376 * correctly match the HW structure.
377 */
378 dev_warn(dev, "DMA channels sourced from device %s",
379 dev_name(config->dma_dev));
380 dev = config->dma_dev;
381 }
382
383 for_each_pcm_streams(i) {
384 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
385 name = "rx-tx";
386 else
387 name = dmaengine_pcm_dma_channel_names[i];
388 if (config->chan_names[i])
389 name = config->chan_names[i];
390 chan = dma_request_chan(dev, name);
391 if (IS_ERR(chan)) {
392 /*
393 * Only report probe deferral errors, channels
394 * might not be present for devices that
395 * support only TX or only RX.
396 */
397 if (PTR_ERR(chan) == -EPROBE_DEFER)
398 return -EPROBE_DEFER;
399
400 if (device_property_match_string(dev, "dma-names", name) >= 0)
401 dev_warn(dev, "dma-names has '%s' but request failed (%ld)\n",
402 name, PTR_ERR(chan));
403
404 pcm->chan[i] = NULL;
405 } else {
406 pcm->chan[i] = chan;
407 }
408 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
409 break;
410 }
411
412 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
413 pcm->chan[1] = pcm->chan[0];
414
415 if (!pcm->chan[0] &&
416 !pcm->chan[1]) {
417 dev_err(dev, "no DMA channel found for either playback or capture\n");
418 return -ENODEV;
419 }
420
421 return 0;
422 }
423
dmaengine_pcm_release_chan(struct dmaengine_pcm * pcm)424 static void dmaengine_pcm_release_chan(struct dmaengine_pcm *pcm)
425 {
426 unsigned int i;
427
428 for_each_pcm_streams(i) {
429 if (!pcm->chan[i])
430 continue;
431 dma_release_channel(pcm->chan[i]);
432 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
433 break;
434 }
435 }
436
437 static const struct snd_dmaengine_pcm_config snd_dmaengine_pcm_default_config = {
438 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
439 };
440
441 /**
442 * snd_dmaengine_pcm_register - Register a dmaengine based PCM device
443 * @dev: The parent device for the PCM device
444 * @config: Platform specific PCM configuration
445 * @flags: Platform specific quirks
446 */
snd_dmaengine_pcm_register(struct device * dev,const struct snd_dmaengine_pcm_config * config,unsigned int flags)447 int snd_dmaengine_pcm_register(struct device *dev,
448 const struct snd_dmaengine_pcm_config *config, unsigned int flags)
449 {
450 struct snd_soc_component *component;
451 const struct snd_soc_component_driver *driver;
452 struct dmaengine_pcm *pcm;
453 int ret;
454
455 component = snd_soc_component_alloc(dev);
456 if (!component)
457 return -ENOMEM;
458
459 pcm = kzalloc_obj(*pcm);
460 if (!pcm)
461 return -ENOMEM;
462
463 if (!config)
464 config = &snd_dmaengine_pcm_default_config;
465 pcm->config = config;
466 pcm->flags = flags;
467
468 if (config->name)
469 snd_soc_component_set_name(component, config->name);
470 snd_soc_component_set_priv(component, pcm);
471
472 ret = dmaengine_pcm_request_chan_of(pcm, dev, config);
473 if (ret)
474 goto err_free_dma;
475
476 if (config->process)
477 driver = &dmaengine_pcm_component_process;
478 else
479 driver = &dmaengine_pcm_component;
480
481 ret = snd_soc_register_component(component, driver, NULL, 0);
482 if (ret)
483 goto err_free_dma;
484
485 return 0;
486
487 err_free_dma:
488 dmaengine_pcm_release_chan(pcm);
489 kfree(pcm);
490 return ret;
491 }
492 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_register);
493
494 /**
495 * snd_dmaengine_pcm_unregister - Removes a dmaengine based PCM device
496 * @dev: Parent device the PCM was register with
497 *
498 * Removes a dmaengine based PCM device previously registered with
499 * snd_dmaengine_pcm_register.
500 */
snd_dmaengine_pcm_unregister(struct device * dev)501 void snd_dmaengine_pcm_unregister(struct device *dev)
502 {
503 struct snd_soc_component *component;
504 struct dmaengine_pcm *pcm;
505
506 component = snd_soc_lookup_component(dev, SND_DMAENGINE_PCM_DRV_NAME);
507 if (!component)
508 return;
509
510 pcm = snd_soc_component_to_priv(component);
511
512 snd_soc_unregister_component_by_driver(dev, component->driver);
513 dmaengine_pcm_release_chan(pcm);
514 kfree(pcm);
515 }
516 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_unregister);
517
518 MODULE_DESCRIPTION("ASoC helpers for generic PCM dmaengine API");
519 MODULE_LICENSE("GPL");
520