1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2012, Analog Devices Inc.
4 * Author: Lars-Peter Clausen <lars@metafoo.de>
7 * imx-pcm-dma-mx2.c, Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
8 * mxs-pcm.c, Copyright (C) 2011 Freescale Semiconductor, Inc.
9 * ep93xx-pcm.c, Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
10 * Copyright (C) 2006 Applied Data Systems
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/dmaengine.h>
15 #include <linux/slab.h>
16 #include <sound/pcm.h>
17 #include <sound/pcm_params.h>
18 #include <sound/soc.h>
20 #include <sound/dmaengine_pcm.h>
22 struct dmaengine_pcm_runtime_data {
23 struct dma_chan *dma_chan;
29 static inline struct dmaengine_pcm_runtime_data *substream_to_prtd(
30 const struct snd_pcm_substream *substream)
32 return substream->runtime->private_data;
35 struct dma_chan *snd_dmaengine_pcm_get_chan(struct snd_pcm_substream *substream)
37 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
39 return prtd->dma_chan;
41 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_chan);
44 * snd_hwparams_to_dma_slave_config - Convert hw_params to dma_slave_config
45 * @substream: PCM substream
47 * @slave_config: DMA slave config
49 * This function can be used to initialize a dma_slave_config from a substream
50 * and hw_params in a dmaengine based PCM driver implementation.
52 int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream *substream,
53 const struct snd_pcm_hw_params *params,
54 struct dma_slave_config *slave_config)
56 enum dma_slave_buswidth buswidth;
59 bits = params_physical_width(params);
60 if (bits < 8 || bits > 64)
63 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
65 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
67 buswidth = DMA_SLAVE_BUSWIDTH_3_BYTES;
69 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
71 buswidth = DMA_SLAVE_BUSWIDTH_8_BYTES;
73 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
74 slave_config->direction = DMA_MEM_TO_DEV;
75 slave_config->dst_addr_width = buswidth;
77 slave_config->direction = DMA_DEV_TO_MEM;
78 slave_config->src_addr_width = buswidth;
81 slave_config->device_fc = false;
85 EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
88 * snd_dmaengine_pcm_set_config_from_dai_data() - Initializes a dma slave config
90 * @substream: PCM substream
91 * @dma_data: DAI DMA data
92 * @slave_config: DMA slave configuration
94 * Initializes the {dst,src}_addr, {dst,src}_maxburst, {dst,src}_addr_width and
95 * slave_id fields of the DMA slave config from the same fields of the DAI DMA
96 * data struct. The src and dst fields will be initialized depending on the
97 * direction of the substream. If the substream is a playback stream the dst
98 * fields will be initialized, if it is a capture stream the src fields will be
99 * initialized. The {dst,src}_addr_width field will only be initialized if the
100 * SND_DMAENGINE_PCM_DAI_FLAG_PACK flag is set or if the addr_width field of
101 * the DAI DMA data struct is not equal to DMA_SLAVE_BUSWIDTH_UNDEFINED. If
102 * both conditions are met the latter takes priority.
104 void snd_dmaengine_pcm_set_config_from_dai_data(
105 const struct snd_pcm_substream *substream,
106 const struct snd_dmaengine_dai_dma_data *dma_data,
107 struct dma_slave_config *slave_config)
109 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
110 slave_config->dst_addr = dma_data->addr;
111 slave_config->dst_maxburst = dma_data->maxburst;
112 if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK)
113 slave_config->dst_addr_width =
114 DMA_SLAVE_BUSWIDTH_UNDEFINED;
115 if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
116 slave_config->dst_addr_width = dma_data->addr_width;
118 slave_config->src_addr = dma_data->addr;
119 slave_config->src_maxburst = dma_data->maxburst;
120 if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK)
121 slave_config->src_addr_width =
122 DMA_SLAVE_BUSWIDTH_UNDEFINED;
123 if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
124 slave_config->src_addr_width = dma_data->addr_width;
127 slave_config->slave_id = dma_data->slave_id;
129 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data);
131 static void dmaengine_pcm_dma_complete(void *arg)
133 struct snd_pcm_substream *substream = arg;
134 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
136 prtd->pos += snd_pcm_lib_period_bytes(substream);
137 if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
140 snd_pcm_period_elapsed(substream);
143 static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
145 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
146 struct dma_chan *chan = prtd->dma_chan;
147 struct dma_async_tx_descriptor *desc;
148 enum dma_transfer_direction direction;
149 unsigned long flags = DMA_CTRL_ACK;
151 direction = snd_pcm_substream_to_dma_direction(substream);
153 if (!substream->runtime->no_period_wakeup)
154 flags |= DMA_PREP_INTERRUPT;
157 desc = dmaengine_prep_dma_cyclic(chan,
158 substream->runtime->dma_addr,
159 snd_pcm_lib_buffer_bytes(substream),
160 snd_pcm_lib_period_bytes(substream), direction, flags);
165 desc->callback = dmaengine_pcm_dma_complete;
166 desc->callback_param = substream;
167 prtd->cookie = dmaengine_submit(desc);
173 * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
174 * @substream: PCM substream
175 * @cmd: Trigger command
177 * Returns 0 on success, a negative error code otherwise.
179 * This function can be used as the PCM trigger callback for dmaengine based PCM
180 * driver implementations.
182 int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
184 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
185 struct snd_pcm_runtime *runtime = substream->runtime;
189 case SNDRV_PCM_TRIGGER_START:
190 ret = dmaengine_pcm_prepare_and_submit(substream);
193 dma_async_issue_pending(prtd->dma_chan);
195 case SNDRV_PCM_TRIGGER_RESUME:
196 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
197 dmaengine_resume(prtd->dma_chan);
199 case SNDRV_PCM_TRIGGER_SUSPEND:
200 if (runtime->info & SNDRV_PCM_INFO_PAUSE)
201 dmaengine_pause(prtd->dma_chan);
203 dmaengine_terminate_async(prtd->dma_chan);
205 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
206 dmaengine_pause(prtd->dma_chan);
208 case SNDRV_PCM_TRIGGER_STOP:
209 dmaengine_terminate_async(prtd->dma_chan);
217 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
220 * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
221 * @substream: PCM substream
223 * This function is deprecated and should not be used by new drivers, as its
224 * results may be unreliable.
226 snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
228 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
229 return bytes_to_frames(substream->runtime, prtd->pos);
231 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
234 * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
235 * @substream: PCM substream
237 * This function can be used as the PCM pointer callback for dmaengine based PCM
238 * driver implementations.
240 snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
242 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
243 struct dma_tx_state state;
244 enum dma_status status;
245 unsigned int buf_size;
246 unsigned int pos = 0;
248 status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
249 if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
250 buf_size = snd_pcm_lib_buffer_bytes(substream);
251 if (state.residue > 0 && state.residue <= buf_size)
252 pos = buf_size - state.residue;
255 return bytes_to_frames(substream->runtime, pos);
257 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
260 * snd_dmaengine_pcm_request_channel - Request channel for the dmaengine PCM
261 * @filter_fn: Filter function used to request the DMA channel
262 * @filter_data: Data passed to the DMA filter function
264 * Returns NULL or the requested DMA channel.
266 * This function request a DMA channel for usage with dmaengine PCM.
268 struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn,
274 dma_cap_set(DMA_SLAVE, mask);
275 dma_cap_set(DMA_CYCLIC, mask);
277 return dma_request_channel(mask, filter_fn, filter_data);
279 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_request_channel);
282 * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
283 * @substream: PCM substream
284 * @chan: DMA channel to use for data transfers
286 * Returns 0 on success, a negative error code otherwise.
288 * The function should usually be called from the pcm open callback. Note that
289 * this function will use private_data field of the substream's runtime. So it
290 * is not available to your pcm driver implementation.
292 int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
293 struct dma_chan *chan)
295 struct dmaengine_pcm_runtime_data *prtd;
301 ret = snd_pcm_hw_constraint_integer(substream->runtime,
302 SNDRV_PCM_HW_PARAM_PERIODS);
306 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
310 prtd->dma_chan = chan;
312 substream->runtime->private_data = prtd;
316 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
319 * snd_dmaengine_pcm_open_request_chan - Open a dmaengine based PCM substream and request channel
320 * @substream: PCM substream
321 * @filter_fn: Filter function used to request the DMA channel
322 * @filter_data: Data passed to the DMA filter function
324 * Returns 0 on success, a negative error code otherwise.
326 * This function will request a DMA channel using the passed filter function and
327 * data. The function should usually be called from the pcm open callback. Note
328 * that this function will use private_data field of the substream's runtime. So
329 * it is not available to your pcm driver implementation.
331 int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream *substream,
332 dma_filter_fn filter_fn, void *filter_data)
334 return snd_dmaengine_pcm_open(substream,
335 snd_dmaengine_pcm_request_channel(filter_fn, filter_data));
337 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open_request_chan);
340 * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
341 * @substream: PCM substream
343 int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
345 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
347 dmaengine_synchronize(prtd->dma_chan);
352 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
355 * snd_dmaengine_pcm_release_chan_close - Close a dmaengine based PCM substream and release channel
356 * @substream: PCM substream
358 * Releases the DMA channel associated with the PCM substream.
360 int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream)
362 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
364 dmaengine_synchronize(prtd->dma_chan);
365 dma_release_channel(prtd->dma_chan);
370 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close_release_chan);
373 * snd_dmaengine_pcm_refine_runtime_hwparams - Refine runtime hw params
374 * @substream: PCM substream
375 * @dma_data: DAI DMA data
377 * @chan: DMA channel to use for data transfers
379 * Returns 0 on success, a negative error code otherwise.
381 * This function will query DMA capability, then refine the pcm hardware
384 int snd_dmaengine_pcm_refine_runtime_hwparams(
385 struct snd_pcm_substream *substream,
386 struct snd_dmaengine_dai_dma_data *dma_data,
387 struct snd_pcm_hardware *hw,
388 struct dma_chan *chan)
390 struct dma_slave_caps dma_caps;
391 u32 addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
392 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
393 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
397 if (!hw || !chan || !dma_data)
400 ret = dma_get_slave_caps(chan, &dma_caps);
402 if (dma_caps.cmd_pause && dma_caps.cmd_resume)
403 hw->info |= SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME;
404 if (dma_caps.residue_granularity <= DMA_RESIDUE_GRANULARITY_SEGMENT)
405 hw->info |= SNDRV_PCM_INFO_BATCH;
407 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
408 addr_widths = dma_caps.dst_addr_widths;
410 addr_widths = dma_caps.src_addr_widths;
414 * If SND_DMAENGINE_PCM_DAI_FLAG_PACK is set keep
415 * hw.formats set to 0, meaning no restrictions are in place.
416 * In this case it's the responsibility of the DAI driver to
417 * provide the supported format information.
419 if (!(dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK))
421 * Prepare formats mask for valid/allowed sample types. If the
422 * dma does not have support for the given physical word size,
423 * it needs to be masked out so user space can not use the
424 * format which produces corrupted audio.
425 * In case the dma driver does not implement the slave_caps the
426 * default assumption is that it supports 1, 2 and 4 bytes
429 for (i = SNDRV_PCM_FORMAT_FIRST; i <= SNDRV_PCM_FORMAT_LAST; i++) {
430 int bits = snd_pcm_format_physical_width(i);
433 * Enable only samples with DMA supported physical
442 if (addr_widths & (1 << (bits / 8)))
443 hw->formats |= pcm_format_to_bits(i);
446 /* Unsupported types */
453 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_refine_runtime_hwparams);
455 MODULE_LICENSE("GPL");