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>
19 * The platforms dmaengine driver does not support reporting the amount of
20 * bytes that are still left to transfer.
22 #define SND_DMAENGINE_PCM_FLAG_NO_RESIDUE BIT(31)
24 struct dmaengine_pcm {
25 struct dma_chan *chan[SNDRV_PCM_STREAM_LAST + 1];
26 const struct snd_dmaengine_pcm_config *config;
27 struct snd_soc_component component;
31 static struct dmaengine_pcm *soc_component_to_pcm(struct snd_soc_component *p)
33 return container_of(p, struct dmaengine_pcm, component);
36 static struct device *dmaengine_dma_dev(struct dmaengine_pcm *pcm,
37 struct snd_pcm_substream *substream)
39 if (!pcm->chan[substream->stream])
42 return pcm->chan[substream->stream]->device->dev;
46 * snd_dmaengine_pcm_prepare_slave_config() - Generic prepare_slave_config callback
47 * @substream: PCM substream
49 * @slave_config: DMA slave config to prepare
51 * This function can be used as a generic prepare_slave_config callback for
52 * platforms which make use of the snd_dmaengine_dai_dma_data struct for their
53 * DAI DMA data. Internally the function will first call
54 * snd_hwparams_to_dma_slave_config to fill in the slave config based on the
55 * hw_params, followed by snd_dmaengine_set_config_from_dai_data to fill in the
56 * remaining fields based on the DAI DMA data.
58 int snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream *substream,
59 struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config)
61 struct snd_soc_pcm_runtime *rtd = substream->private_data;
62 struct snd_dmaengine_dai_dma_data *dma_data;
65 if (rtd->num_cpus > 1) {
67 "%s doesn't support Multi CPU yet\n", __func__);
71 dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
73 ret = snd_hwparams_to_dma_slave_config(substream, params, slave_config);
77 snd_dmaengine_pcm_set_config_from_dai_data(substream, dma_data,
82 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_prepare_slave_config);
84 static int dmaengine_pcm_hw_params(struct snd_soc_component *component,
85 struct snd_pcm_substream *substream,
86 struct snd_pcm_hw_params *params)
88 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
89 struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
90 int (*prepare_slave_config)(struct snd_pcm_substream *substream,
91 struct snd_pcm_hw_params *params,
92 struct dma_slave_config *slave_config);
93 struct dma_slave_config slave_config;
96 memset(&slave_config, 0, sizeof(slave_config));
99 prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config;
101 prepare_slave_config = pcm->config->prepare_slave_config;
103 if (prepare_slave_config) {
104 ret = prepare_slave_config(substream, params, &slave_config);
108 ret = dmaengine_slave_config(chan, &slave_config);
117 dmaengine_pcm_set_runtime_hwparams(struct snd_soc_component *component,
118 struct snd_pcm_substream *substream)
120 struct snd_soc_pcm_runtime *rtd = substream->private_data;
121 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
122 struct device *dma_dev = dmaengine_dma_dev(pcm, substream);
123 struct dma_chan *chan = pcm->chan[substream->stream];
124 struct snd_dmaengine_dai_dma_data *dma_data;
125 struct snd_pcm_hardware hw;
127 if (rtd->num_cpus > 1) {
129 "%s doesn't support Multi CPU yet\n", __func__);
133 if (pcm->config && pcm->config->pcm_hardware)
134 return snd_soc_set_runtime_hwparams(substream,
135 pcm->config->pcm_hardware);
137 dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
139 memset(&hw, 0, sizeof(hw));
140 hw.info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
141 SNDRV_PCM_INFO_INTERLEAVED;
143 hw.periods_max = UINT_MAX;
144 hw.period_bytes_min = 256;
145 hw.period_bytes_max = dma_get_max_seg_size(dma_dev);
146 hw.buffer_bytes_max = SIZE_MAX;
147 hw.fifo_size = dma_data->fifo_size;
149 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
150 hw.info |= SNDRV_PCM_INFO_BATCH;
153 * FIXME: Remove the return value check to align with the code
154 * before adding snd_dmaengine_pcm_refine_runtime_hwparams
157 snd_dmaengine_pcm_refine_runtime_hwparams(substream,
162 return snd_soc_set_runtime_hwparams(substream, &hw);
165 static int dmaengine_pcm_open(struct snd_soc_component *component,
166 struct snd_pcm_substream *substream)
168 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
169 struct dma_chan *chan = pcm->chan[substream->stream];
172 ret = dmaengine_pcm_set_runtime_hwparams(component, substream);
176 return snd_dmaengine_pcm_open(substream, chan);
179 static int dmaengine_pcm_close(struct snd_soc_component *component,
180 struct snd_pcm_substream *substream)
182 return snd_dmaengine_pcm_close(substream);
185 static int dmaengine_pcm_trigger(struct snd_soc_component *component,
186 struct snd_pcm_substream *substream, int cmd)
188 return snd_dmaengine_pcm_trigger(substream, cmd);
191 static struct dma_chan *dmaengine_pcm_compat_request_channel(
192 struct snd_soc_component *component,
193 struct snd_soc_pcm_runtime *rtd,
194 struct snd_pcm_substream *substream)
196 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
197 struct snd_dmaengine_dai_dma_data *dma_data;
198 dma_filter_fn fn = NULL;
200 if (rtd->num_cpus > 1) {
202 "%s doesn't support Multi CPU yet\n", __func__);
206 dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
208 if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) && pcm->chan[0])
211 if (pcm->config && pcm->config->compat_request_channel)
212 return pcm->config->compat_request_channel(rtd, substream);
215 fn = pcm->config->compat_filter_fn;
217 return snd_dmaengine_pcm_request_channel(fn, dma_data->filter_data);
220 static bool dmaengine_pcm_can_report_residue(struct device *dev,
221 struct dma_chan *chan)
223 struct dma_slave_caps dma_caps;
226 ret = dma_get_slave_caps(chan, &dma_caps);
228 dev_warn(dev, "Failed to get DMA channel capabilities, falling back to period counting: %d\n",
233 if (dma_caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR)
239 static int dmaengine_pcm_new(struct snd_soc_component *component,
240 struct snd_soc_pcm_runtime *rtd)
242 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
243 const struct snd_dmaengine_pcm_config *config = pcm->config;
244 struct device *dev = component->dev;
245 struct snd_pcm_substream *substream;
246 size_t prealloc_buffer_size;
247 size_t max_buffer_size;
250 if (config && config->prealloc_buffer_size) {
251 prealloc_buffer_size = config->prealloc_buffer_size;
252 max_buffer_size = config->pcm_hardware->buffer_bytes_max;
254 prealloc_buffer_size = 512 * 1024;
255 max_buffer_size = SIZE_MAX;
258 for_each_pcm_streams(i) {
259 substream = rtd->pcm->streams[i].substream;
263 if (!pcm->chan[i] && config && config->chan_names[i])
264 pcm->chan[i] = dma_request_slave_channel(dev,
265 config->chan_names[i]);
267 if (!pcm->chan[i] && (pcm->flags & SND_DMAENGINE_PCM_FLAG_COMPAT)) {
268 pcm->chan[i] = dmaengine_pcm_compat_request_channel(
269 component, rtd, substream);
273 dev_err(component->dev,
274 "Missing dma channel for stream: %d\n", i);
278 snd_pcm_set_managed_buffer(substream,
279 SNDRV_DMA_TYPE_DEV_IRAM,
280 dmaengine_dma_dev(pcm, substream),
281 prealloc_buffer_size,
284 if (!dmaengine_pcm_can_report_residue(dev, pcm->chan[i]))
285 pcm->flags |= SND_DMAENGINE_PCM_FLAG_NO_RESIDUE;
287 if (rtd->pcm->streams[i].pcm->name[0] == '\0') {
288 strscpy_pad(rtd->pcm->streams[i].pcm->name,
289 rtd->pcm->streams[i].pcm->id,
290 sizeof(rtd->pcm->streams[i].pcm->name));
297 static snd_pcm_uframes_t dmaengine_pcm_pointer(
298 struct snd_soc_component *component,
299 struct snd_pcm_substream *substream)
301 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
303 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
304 return snd_dmaengine_pcm_pointer_no_residue(substream);
306 return snd_dmaengine_pcm_pointer(substream);
309 static int dmaengine_copy_user(struct snd_soc_component *component,
310 struct snd_pcm_substream *substream,
311 int channel, unsigned long hwoff,
312 void __user *buf, unsigned long bytes)
314 struct snd_pcm_runtime *runtime = substream->runtime;
315 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
316 int (*process)(struct snd_pcm_substream *substream,
317 int channel, unsigned long hwoff,
318 void *buf, unsigned long bytes) = pcm->config->process;
319 bool is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
320 void *dma_ptr = runtime->dma_area + hwoff +
321 channel * (runtime->dma_bytes / runtime->channels);
325 if (copy_from_user(dma_ptr, buf, bytes))
329 ret = process(substream, channel, hwoff, (__force void *)buf, bytes);
335 if (copy_to_user(buf, dma_ptr, bytes))
341 static const struct snd_soc_component_driver dmaengine_pcm_component = {
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 .pcm_construct = dmaengine_pcm_new,
352 static const struct snd_soc_component_driver dmaengine_pcm_component_process = {
353 .name = SND_DMAENGINE_PCM_DRV_NAME,
354 .probe_order = SND_SOC_COMP_ORDER_LATE,
355 .open = dmaengine_pcm_open,
356 .close = dmaengine_pcm_close,
357 .hw_params = dmaengine_pcm_hw_params,
358 .trigger = dmaengine_pcm_trigger,
359 .pointer = dmaengine_pcm_pointer,
360 .copy_user = dmaengine_copy_user,
361 .pcm_construct = dmaengine_pcm_new,
364 static const char * const dmaengine_pcm_dma_channel_names[] = {
365 [SNDRV_PCM_STREAM_PLAYBACK] = "tx",
366 [SNDRV_PCM_STREAM_CAPTURE] = "rx",
369 static int dmaengine_pcm_request_chan_of(struct dmaengine_pcm *pcm,
370 struct device *dev, const struct snd_dmaengine_pcm_config *config)
374 struct dma_chan *chan;
376 if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_DT) || (!dev->of_node &&
377 !(config && config->dma_dev && config->dma_dev->of_node)))
380 if (config && config->dma_dev) {
382 * If this warning is seen, it probably means that your Linux
383 * device structure does not match your HW device structure.
384 * It would be best to refactor the Linux device structure to
385 * correctly match the HW structure.
387 dev_warn(dev, "DMA channels sourced from device %s",
388 dev_name(config->dma_dev));
389 dev = config->dma_dev;
392 for_each_pcm_streams(i) {
393 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
396 name = dmaengine_pcm_dma_channel_names[i];
397 if (config && config->chan_names[i])
398 name = config->chan_names[i];
399 chan = dma_request_chan(dev, name);
401 if (PTR_ERR(chan) == -EPROBE_DEFER)
402 return -EPROBE_DEFER;
407 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
411 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
412 pcm->chan[1] = pcm->chan[0];
417 static void dmaengine_pcm_release_chan(struct dmaengine_pcm *pcm)
421 for_each_pcm_streams(i) {
424 dma_release_channel(pcm->chan[i]);
425 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
431 * snd_dmaengine_pcm_register - Register a dmaengine based PCM device
432 * @dev: The parent device for the PCM device
433 * @config: Platform specific PCM configuration
434 * @flags: Platform specific quirks
436 int snd_dmaengine_pcm_register(struct device *dev,
437 const struct snd_dmaengine_pcm_config *config, unsigned int flags)
439 struct dmaengine_pcm *pcm;
442 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
446 #ifdef CONFIG_DEBUG_FS
447 pcm->component.debugfs_prefix = "dma";
449 pcm->config = config;
452 ret = dmaengine_pcm_request_chan_of(pcm, dev, config);
456 if (config && config->process)
457 ret = snd_soc_add_component(dev, &pcm->component,
458 &dmaengine_pcm_component_process,
461 ret = snd_soc_add_component(dev, &pcm->component,
462 &dmaengine_pcm_component, NULL, 0);
469 dmaengine_pcm_release_chan(pcm);
473 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_register);
476 * snd_dmaengine_pcm_unregister - Removes a dmaengine based PCM device
477 * @dev: Parent device the PCM was register with
479 * Removes a dmaengine based PCM device previously registered with
480 * snd_dmaengine_pcm_register.
482 void snd_dmaengine_pcm_unregister(struct device *dev)
484 struct snd_soc_component *component;
485 struct dmaengine_pcm *pcm;
487 component = snd_soc_lookup_component(dev, SND_DMAENGINE_PCM_DRV_NAME);
491 pcm = soc_component_to_pcm(component);
493 snd_soc_unregister_component(dev);
494 dmaengine_pcm_release_chan(pcm);
497 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_unregister);
499 MODULE_LICENSE("GPL");