2 * Copyright (C) 2012, Analog Devices Inc.
3 * Author: Lars-Peter Clausen <lars@metafoo.de>
6 * imx-pcm-dma-mx2.c, Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
7 * mxs-pcm.c, Copyright (C) 2011 Freescale Semiconductor, Inc.
8 * ep93xx-pcm.c, Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
9 * Copyright (C) 2006 Applied Data Systems
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/dmaengine.h>
24 #include <linux/slab.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
29 #include <sound/dmaengine_pcm.h>
31 struct dmaengine_pcm_runtime_data
{
32 struct dma_chan
*dma_chan
;
38 static inline struct dmaengine_pcm_runtime_data
*substream_to_prtd(
39 const struct snd_pcm_substream
*substream
)
41 return substream
->runtime
->private_data
;
44 struct dma_chan
*snd_dmaengine_pcm_get_chan(struct snd_pcm_substream
*substream
)
46 struct dmaengine_pcm_runtime_data
*prtd
= substream_to_prtd(substream
);
48 return prtd
->dma_chan
;
50 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_chan
);
53 * snd_hwparams_to_dma_slave_config - Convert hw_params to dma_slave_config
54 * @substream: PCM substream
56 * @slave_config: DMA slave config
58 * This function can be used to initialize a dma_slave_config from a substream
59 * and hw_params in a dmaengine based PCM driver implementation.
61 int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream
*substream
,
62 const struct snd_pcm_hw_params
*params
,
63 struct dma_slave_config
*slave_config
)
65 enum dma_slave_buswidth buswidth
;
68 bits
= params_physical_width(params
);
69 if (bits
< 8 || bits
> 64)
72 buswidth
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
74 buswidth
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
76 buswidth
= DMA_SLAVE_BUSWIDTH_3_BYTES
;
78 buswidth
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
80 buswidth
= DMA_SLAVE_BUSWIDTH_8_BYTES
;
82 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
83 slave_config
->direction
= DMA_MEM_TO_DEV
;
84 slave_config
->dst_addr_width
= buswidth
;
86 slave_config
->direction
= DMA_DEV_TO_MEM
;
87 slave_config
->src_addr_width
= buswidth
;
90 slave_config
->device_fc
= false;
94 EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config
);
97 * snd_dmaengine_pcm_set_config_from_dai_data() - Initializes a dma slave config
99 * @substream: PCM substream
100 * @dma_data: DAI DMA data
101 * @slave_config: DMA slave configuration
103 * Initializes the {dst,src}_addr, {dst,src}_maxburst, {dst,src}_addr_width and
104 * slave_id fields of the DMA slave config from the same fields of the DAI DMA
105 * data struct. The src and dst fields will be initialized depending on the
106 * direction of the substream. If the substream is a playback stream the dst
107 * fields will be initialized, if it is a capture stream the src fields will be
108 * initialized. The {dst,src}_addr_width field will only be initialized if the
109 * addr_width field of the DAI DMA data struct is not equal to
110 * DMA_SLAVE_BUSWIDTH_UNDEFINED.
112 void snd_dmaengine_pcm_set_config_from_dai_data(
113 const struct snd_pcm_substream
*substream
,
114 const struct snd_dmaengine_dai_dma_data
*dma_data
,
115 struct dma_slave_config
*slave_config
)
117 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
118 slave_config
->dst_addr
= dma_data
->addr
;
119 slave_config
->dst_maxburst
= dma_data
->maxburst
;
120 if (dma_data
->addr_width
!= DMA_SLAVE_BUSWIDTH_UNDEFINED
)
121 slave_config
->dst_addr_width
= dma_data
->addr_width
;
123 slave_config
->src_addr
= dma_data
->addr
;
124 slave_config
->src_maxburst
= dma_data
->maxburst
;
125 if (dma_data
->addr_width
!= DMA_SLAVE_BUSWIDTH_UNDEFINED
)
126 slave_config
->src_addr_width
= dma_data
->addr_width
;
129 slave_config
->slave_id
= dma_data
->slave_id
;
131 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data
);
133 static void dmaengine_pcm_dma_complete(void *arg
)
135 struct snd_pcm_substream
*substream
= arg
;
136 struct dmaengine_pcm_runtime_data
*prtd
= substream_to_prtd(substream
);
138 prtd
->pos
+= snd_pcm_lib_period_bytes(substream
);
139 if (prtd
->pos
>= snd_pcm_lib_buffer_bytes(substream
))
142 snd_pcm_period_elapsed(substream
);
145 static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream
*substream
)
147 struct dmaengine_pcm_runtime_data
*prtd
= substream_to_prtd(substream
);
148 struct dma_chan
*chan
= prtd
->dma_chan
;
149 struct dma_async_tx_descriptor
*desc
;
150 enum dma_transfer_direction direction
;
151 unsigned long flags
= DMA_CTRL_ACK
;
153 direction
= snd_pcm_substream_to_dma_direction(substream
);
155 if (!substream
->runtime
->no_period_wakeup
)
156 flags
|= DMA_PREP_INTERRUPT
;
159 desc
= dmaengine_prep_dma_cyclic(chan
,
160 substream
->runtime
->dma_addr
,
161 snd_pcm_lib_buffer_bytes(substream
),
162 snd_pcm_lib_period_bytes(substream
), direction
, flags
);
167 desc
->callback
= dmaengine_pcm_dma_complete
;
168 desc
->callback_param
= substream
;
169 prtd
->cookie
= dmaengine_submit(desc
);
175 * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
176 * @substream: PCM substream
177 * @cmd: Trigger command
179 * Returns 0 on success, a negative error code otherwise.
181 * This function can be used as the PCM trigger callback for dmaengine based PCM
182 * driver implementations.
184 int snd_dmaengine_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
186 struct dmaengine_pcm_runtime_data
*prtd
= substream_to_prtd(substream
);
187 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
191 case SNDRV_PCM_TRIGGER_START
:
192 ret
= dmaengine_pcm_prepare_and_submit(substream
);
195 dma_async_issue_pending(prtd
->dma_chan
);
197 case SNDRV_PCM_TRIGGER_RESUME
:
198 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
199 dmaengine_resume(prtd
->dma_chan
);
201 case SNDRV_PCM_TRIGGER_SUSPEND
:
202 if (runtime
->info
& SNDRV_PCM_INFO_PAUSE
)
203 dmaengine_pause(prtd
->dma_chan
);
205 dmaengine_terminate_all(prtd
->dma_chan
);
207 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
208 dmaengine_pause(prtd
->dma_chan
);
210 case SNDRV_PCM_TRIGGER_STOP
:
211 dmaengine_terminate_all(prtd
->dma_chan
);
219 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger
);
222 * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
223 * @substream: PCM substream
225 * This function is deprecated and should not be used by new drivers, as its
226 * results may be unreliable.
228 snd_pcm_uframes_t
snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream
*substream
)
230 struct dmaengine_pcm_runtime_data
*prtd
= substream_to_prtd(substream
);
231 return bytes_to_frames(substream
->runtime
, prtd
->pos
);
233 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue
);
236 * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
237 * @substream: PCM substream
239 * This function can be used as the PCM pointer callback for dmaengine based PCM
240 * driver implementations.
242 snd_pcm_uframes_t
snd_dmaengine_pcm_pointer(struct snd_pcm_substream
*substream
)
244 struct dmaengine_pcm_runtime_data
*prtd
= substream_to_prtd(substream
);
245 struct dma_tx_state state
;
246 enum dma_status status
;
247 unsigned int buf_size
;
248 unsigned int pos
= 0;
250 status
= dmaengine_tx_status(prtd
->dma_chan
, prtd
->cookie
, &state
);
251 if (status
== DMA_IN_PROGRESS
|| status
== DMA_PAUSED
) {
252 buf_size
= snd_pcm_lib_buffer_bytes(substream
);
253 if (state
.residue
> 0 && state
.residue
<= buf_size
)
254 pos
= buf_size
- state
.residue
;
257 return bytes_to_frames(substream
->runtime
, pos
);
259 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer
);
262 * snd_dmaengine_pcm_request_channel - Request channel for the dmaengine PCM
263 * @filter_fn: Filter function used to request the DMA channel
264 * @filter_data: Data passed to the DMA filter function
266 * Returns NULL or the requested DMA channel.
268 * This function request a DMA channel for usage with dmaengine PCM.
270 struct dma_chan
*snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn
,
276 dma_cap_set(DMA_SLAVE
, mask
);
277 dma_cap_set(DMA_CYCLIC
, mask
);
279 return dma_request_channel(mask
, filter_fn
, filter_data
);
281 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_request_channel
);
284 * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
285 * @substream: PCM substream
286 * @chan: DMA channel to use for data transfers
288 * Returns 0 on success, a negative error code otherwise.
290 * The function should usually be called from the pcm open callback. Note that
291 * this function will use private_data field of the substream's runtime. So it
292 * is not availabe to your pcm driver implementation.
294 int snd_dmaengine_pcm_open(struct snd_pcm_substream
*substream
,
295 struct dma_chan
*chan
)
297 struct dmaengine_pcm_runtime_data
*prtd
;
303 ret
= snd_pcm_hw_constraint_integer(substream
->runtime
,
304 SNDRV_PCM_HW_PARAM_PERIODS
);
308 prtd
= kzalloc(sizeof(*prtd
), GFP_KERNEL
);
312 prtd
->dma_chan
= chan
;
314 substream
->runtime
->private_data
= prtd
;
318 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open
);
321 * snd_dmaengine_pcm_open_request_chan - Open a dmaengine based PCM substream and request channel
322 * @substream: PCM substream
323 * @filter_fn: Filter function used to request the DMA channel
324 * @filter_data: Data passed to the DMA filter function
326 * Returns 0 on success, a negative error code otherwise.
328 * This function will request a DMA channel using the passed filter function and
329 * data. The function should usually be called from the pcm open callback. Note
330 * that this function will use private_data field of the substream's runtime. So
331 * it is not availabe to your pcm driver implementation.
333 int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream
*substream
,
334 dma_filter_fn filter_fn
, void *filter_data
)
336 return snd_dmaengine_pcm_open(substream
,
337 snd_dmaengine_pcm_request_channel(filter_fn
, filter_data
));
339 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open_request_chan
);
342 * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
343 * @substream: PCM substream
345 int snd_dmaengine_pcm_close(struct snd_pcm_substream
*substream
)
347 struct dmaengine_pcm_runtime_data
*prtd
= substream_to_prtd(substream
);
353 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close
);
356 * snd_dmaengine_pcm_release_chan_close - Close a dmaengine based PCM substream and release channel
357 * @substream: PCM substream
359 * Releases the DMA channel associated with the PCM substream.
361 int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream
*substream
)
363 struct dmaengine_pcm_runtime_data
*prtd
= substream_to_prtd(substream
);
365 dma_release_channel(prtd
->dma_chan
);
367 return snd_dmaengine_pcm_close(substream
);
369 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close_release_chan
);
371 MODULE_LICENSE("GPL");