1 // SPDX-License-Identifier: GPL-2.0+
3 // Copyright (C) 2013, Analog Devices Inc.
4 // Author: Lars-Peter Clausen <lars@metafoo.de>
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/dmaengine.h>
9 #include <linux/slab.h>
10 #include <sound/pcm.h>
11 #include <sound/pcm_params.h>
12 #include <sound/soc.h>
13 #include <linux/dma-mapping.h>
16 #include <sound/dmaengine_pcm.h>
18 static unsigned int prealloc_buffer_size_kbytes = 512;
19 module_param(prealloc_buffer_size_kbytes, uint, 0444);
20 MODULE_PARM_DESC(prealloc_buffer_size_kbytes, "Preallocate DMA buffer size (KB).");
23 * The platforms dmaengine driver does not support reporting the amount of
24 * bytes that are still left to transfer.
26 #define SND_DMAENGINE_PCM_FLAG_NO_RESIDUE BIT(31)
28 static struct device *dmaengine_dma_dev(struct dmaengine_pcm *pcm,
29 struct snd_pcm_substream *substream)
31 if (!pcm->chan[substream->stream])
34 return pcm->chan[substream->stream]->device->dev;
38 * snd_dmaengine_pcm_prepare_slave_config() - Generic prepare_slave_config callback
39 * @substream: PCM substream
41 * @slave_config: DMA slave config to prepare
43 * This function can be used as a generic prepare_slave_config callback for
44 * platforms which make use of the snd_dmaengine_dai_dma_data struct for their
45 * DAI DMA data. Internally the function will first call
46 * snd_hwparams_to_dma_slave_config to fill in the slave config based on the
47 * hw_params, followed by snd_dmaengine_set_config_from_dai_data to fill in the
48 * remaining fields based on the DAI DMA data.
50 int snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream *substream,
51 struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config)
53 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
54 struct snd_dmaengine_dai_dma_data *dma_data;
57 if (rtd->num_cpus > 1) {
59 "%s doesn't support Multi CPU yet\n", __func__);
63 dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
65 ret = snd_hwparams_to_dma_slave_config(substream, params, slave_config);
69 snd_dmaengine_pcm_set_config_from_dai_data(substream, dma_data,
74 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_prepare_slave_config);
76 static int dmaengine_pcm_hw_params(struct snd_soc_component *component,
77 struct snd_pcm_substream *substream,
78 struct snd_pcm_hw_params *params)
80 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
81 struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
82 int (*prepare_slave_config)(struct snd_pcm_substream *substream,
83 struct snd_pcm_hw_params *params,
84 struct dma_slave_config *slave_config);
85 struct dma_slave_config slave_config;
87 memset(&slave_config, 0, sizeof(slave_config));
89 if (pcm->config && pcm->config->prepare_slave_config)
90 prepare_slave_config = pcm->config->prepare_slave_config;
92 prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config;
94 if (prepare_slave_config) {
95 int ret = prepare_slave_config(substream, params, &slave_config);
99 ret = dmaengine_slave_config(chan, &slave_config);
108 dmaengine_pcm_set_runtime_hwparams(struct snd_soc_component *component,
109 struct snd_pcm_substream *substream)
111 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
112 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
113 struct device *dma_dev = dmaengine_dma_dev(pcm, substream);
114 struct dma_chan *chan = pcm->chan[substream->stream];
115 struct snd_dmaengine_dai_dma_data *dma_data;
116 struct snd_pcm_hardware hw;
118 if (rtd->num_cpus > 1) {
120 "%s doesn't support Multi CPU yet\n", __func__);
124 if (pcm->config && pcm->config->pcm_hardware)
125 return snd_soc_set_runtime_hwparams(substream,
126 pcm->config->pcm_hardware);
128 dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
130 memset(&hw, 0, sizeof(hw));
131 hw.info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
132 SNDRV_PCM_INFO_INTERLEAVED;
134 hw.periods_max = UINT_MAX;
135 hw.period_bytes_min = dma_data->maxburst * DMA_SLAVE_BUSWIDTH_8_BYTES;
136 if (!hw.period_bytes_min)
137 hw.period_bytes_min = 256;
138 hw.period_bytes_max = dma_get_max_seg_size(dma_dev);
139 hw.buffer_bytes_max = SIZE_MAX;
140 hw.fifo_size = dma_data->fifo_size;
142 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
143 hw.info |= SNDRV_PCM_INFO_BATCH;
146 * FIXME: Remove the return value check to align with the code
147 * before adding snd_dmaengine_pcm_refine_runtime_hwparams
150 snd_dmaengine_pcm_refine_runtime_hwparams(substream,
155 return snd_soc_set_runtime_hwparams(substream, &hw);
158 static int dmaengine_pcm_open(struct snd_soc_component *component,
159 struct snd_pcm_substream *substream)
161 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
162 struct dma_chan *chan = pcm->chan[substream->stream];
165 ret = dmaengine_pcm_set_runtime_hwparams(component, substream);
169 return snd_dmaengine_pcm_open(substream, chan);
172 static int dmaengine_pcm_close(struct snd_soc_component *component,
173 struct snd_pcm_substream *substream)
175 return snd_dmaengine_pcm_close(substream);
178 static int dmaengine_pcm_trigger(struct snd_soc_component *component,
179 struct snd_pcm_substream *substream, int cmd)
181 return snd_dmaengine_pcm_trigger(substream, cmd);
184 static struct dma_chan *dmaengine_pcm_compat_request_channel(
185 struct snd_soc_component *component,
186 struct snd_soc_pcm_runtime *rtd,
187 struct snd_pcm_substream *substream)
189 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
190 struct snd_dmaengine_dai_dma_data *dma_data;
191 dma_filter_fn fn = NULL;
193 if (rtd->num_cpus > 1) {
195 "%s doesn't support Multi CPU yet\n", __func__);
199 dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
201 if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) && pcm->chan[0])
204 if (pcm->config && pcm->config->compat_request_channel)
205 return pcm->config->compat_request_channel(rtd, substream);
208 fn = pcm->config->compat_filter_fn;
210 return snd_dmaengine_pcm_request_channel(fn, dma_data->filter_data);
213 static bool dmaengine_pcm_can_report_residue(struct device *dev,
214 struct dma_chan *chan)
216 struct dma_slave_caps dma_caps;
219 ret = dma_get_slave_caps(chan, &dma_caps);
221 dev_warn(dev, "Failed to get DMA channel capabilities, falling back to period counting: %d\n",
226 if (dma_caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR)
232 static int dmaengine_pcm_new(struct snd_soc_component *component,
233 struct snd_soc_pcm_runtime *rtd)
235 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
236 const struct snd_dmaengine_pcm_config *config = pcm->config;
237 struct device *dev = component->dev;
238 size_t prealloc_buffer_size;
239 size_t max_buffer_size;
242 if (config && config->prealloc_buffer_size)
243 prealloc_buffer_size = config->prealloc_buffer_size;
245 prealloc_buffer_size = prealloc_buffer_size_kbytes * 1024;
247 if (config && config->pcm_hardware && config->pcm_hardware->buffer_bytes_max)
248 max_buffer_size = config->pcm_hardware->buffer_bytes_max;
250 max_buffer_size = SIZE_MAX;
252 for_each_pcm_streams(i) {
253 struct snd_pcm_substream *substream = rtd->pcm->streams[i].substream;
257 if (!pcm->chan[i] && config && config->chan_names[i])
258 pcm->chan[i] = dma_request_slave_channel(dev,
259 config->chan_names[i]);
261 if (!pcm->chan[i] && (pcm->flags & SND_DMAENGINE_PCM_FLAG_COMPAT)) {
262 pcm->chan[i] = dmaengine_pcm_compat_request_channel(
263 component, rtd, substream);
267 dev_err(component->dev,
268 "Missing dma channel for stream: %d\n", i);
272 snd_pcm_set_managed_buffer(substream,
273 SNDRV_DMA_TYPE_DEV_IRAM,
274 dmaengine_dma_dev(pcm, substream),
275 prealloc_buffer_size,
278 if (!dmaengine_pcm_can_report_residue(dev, pcm->chan[i]))
279 pcm->flags |= SND_DMAENGINE_PCM_FLAG_NO_RESIDUE;
281 if (rtd->pcm->streams[i].pcm->name[0] == '\0') {
282 strscpy_pad(rtd->pcm->streams[i].pcm->name,
283 rtd->pcm->streams[i].pcm->id,
284 sizeof(rtd->pcm->streams[i].pcm->name));
291 static snd_pcm_uframes_t dmaengine_pcm_pointer(
292 struct snd_soc_component *component,
293 struct snd_pcm_substream *substream)
295 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
297 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
298 return snd_dmaengine_pcm_pointer_no_residue(substream);
300 return snd_dmaengine_pcm_pointer(substream);
303 static int dmaengine_copy_user(struct snd_soc_component *component,
304 struct snd_pcm_substream *substream,
305 int channel, unsigned long hwoff,
306 void __user *buf, unsigned long bytes)
308 struct snd_pcm_runtime *runtime = substream->runtime;
309 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
310 int (*process)(struct snd_pcm_substream *substream,
311 int channel, unsigned long hwoff,
312 void *buf, unsigned long bytes) = pcm->config->process;
313 bool is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
314 void *dma_ptr = runtime->dma_area + hwoff +
315 channel * (runtime->dma_bytes / runtime->channels);
318 if (copy_from_user(dma_ptr, buf, bytes))
322 int ret = process(substream, channel, hwoff, (__force void *)buf, bytes);
328 if (copy_to_user(buf, dma_ptr, bytes))
334 static const struct snd_soc_component_driver dmaengine_pcm_component = {
335 .name = SND_DMAENGINE_PCM_DRV_NAME,
336 .probe_order = SND_SOC_COMP_ORDER_LATE,
337 .open = dmaengine_pcm_open,
338 .close = dmaengine_pcm_close,
339 .hw_params = dmaengine_pcm_hw_params,
340 .trigger = dmaengine_pcm_trigger,
341 .pointer = dmaengine_pcm_pointer,
342 .pcm_construct = dmaengine_pcm_new,
345 static const struct snd_soc_component_driver dmaengine_pcm_component_process = {
346 .name = SND_DMAENGINE_PCM_DRV_NAME,
347 .probe_order = SND_SOC_COMP_ORDER_LATE,
348 .open = dmaengine_pcm_open,
349 .close = dmaengine_pcm_close,
350 .hw_params = dmaengine_pcm_hw_params,
351 .trigger = dmaengine_pcm_trigger,
352 .pointer = dmaengine_pcm_pointer,
353 .copy_user = dmaengine_copy_user,
354 .pcm_construct = dmaengine_pcm_new,
357 static const char * const dmaengine_pcm_dma_channel_names[] = {
358 [SNDRV_PCM_STREAM_PLAYBACK] = "tx",
359 [SNDRV_PCM_STREAM_CAPTURE] = "rx",
362 static int dmaengine_pcm_request_chan_of(struct dmaengine_pcm *pcm,
363 struct device *dev, const struct snd_dmaengine_pcm_config *config)
367 struct dma_chan *chan;
369 if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_DT) || (!dev->of_node &&
370 !(config && config->dma_dev && config->dma_dev->of_node)))
373 if (config && config->dma_dev) {
375 * If this warning is seen, it probably means that your Linux
376 * device structure does not match your HW device structure.
377 * It would be best to refactor the Linux device structure to
378 * correctly match the HW structure.
380 dev_warn(dev, "DMA channels sourced from device %s",
381 dev_name(config->dma_dev));
382 dev = config->dma_dev;
385 for_each_pcm_streams(i) {
386 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
389 name = dmaengine_pcm_dma_channel_names[i];
390 if (config && config->chan_names[i])
391 name = config->chan_names[i];
392 chan = dma_request_chan(dev, name);
395 * Only report probe deferral errors, channels
396 * might not be present for devices that
397 * support only TX or only RX.
399 if (PTR_ERR(chan) == -EPROBE_DEFER)
400 return -EPROBE_DEFER;
405 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
409 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
410 pcm->chan[1] = pcm->chan[0];
415 static void dmaengine_pcm_release_chan(struct dmaengine_pcm *pcm)
419 for_each_pcm_streams(i) {
422 dma_release_channel(pcm->chan[i]);
423 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
429 * snd_dmaengine_pcm_register - Register a dmaengine based PCM device
430 * @dev: The parent device for the PCM device
431 * @config: Platform specific PCM configuration
432 * @flags: Platform specific quirks
434 int snd_dmaengine_pcm_register(struct device *dev,
435 const struct snd_dmaengine_pcm_config *config, unsigned int flags)
437 const struct snd_soc_component_driver *driver;
438 struct dmaengine_pcm *pcm;
441 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
445 #ifdef CONFIG_DEBUG_FS
446 pcm->component.debugfs_prefix = "dma";
448 pcm->config = config;
451 ret = dmaengine_pcm_request_chan_of(pcm, dev, config);
455 if (config && config->process)
456 driver = &dmaengine_pcm_component_process;
458 driver = &dmaengine_pcm_component;
460 ret = snd_soc_component_initialize(&pcm->component, driver, dev);
464 ret = snd_soc_add_component(&pcm->component, NULL, 0);
471 dmaengine_pcm_release_chan(pcm);
475 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_register);
478 * snd_dmaengine_pcm_unregister - Removes a dmaengine based PCM device
479 * @dev: Parent device the PCM was register with
481 * Removes a dmaengine based PCM device previously registered with
482 * snd_dmaengine_pcm_register.
484 void snd_dmaengine_pcm_unregister(struct device *dev)
486 struct snd_soc_component *component;
487 struct dmaengine_pcm *pcm;
489 component = snd_soc_lookup_component(dev, SND_DMAENGINE_PCM_DRV_NAME);
493 pcm = soc_component_to_pcm(component);
495 snd_soc_unregister_component_by_driver(dev, component->driver);
496 dmaengine_pcm_release_chan(pcm);
499 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_unregister);
501 MODULE_LICENSE("GPL");