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;
67 switch (params_format(params)) {
68 case SNDRV_PCM_FORMAT_S8:
69 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
71 case SNDRV_PCM_FORMAT_S16_LE:
72 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
74 case SNDRV_PCM_FORMAT_S18_3LE:
75 case SNDRV_PCM_FORMAT_S20_3LE:
76 case SNDRV_PCM_FORMAT_S24_LE:
77 case SNDRV_PCM_FORMAT_S32_LE:
78 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
84 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
85 slave_config->direction = DMA_MEM_TO_DEV;
86 slave_config->dst_addr_width = buswidth;
88 slave_config->direction = DMA_DEV_TO_MEM;
89 slave_config->src_addr_width = buswidth;
92 slave_config->device_fc = false;
96 EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
99 * snd_dmaengine_pcm_set_config_from_dai_data() - Initializes a dma slave config
100 * using DAI DMA data.
101 * @substream: PCM substream
102 * @dma_data: DAI DMA data
103 * @slave_config: DMA slave configuration
105 * Initializes the {dst,src}_addr, {dst,src}_maxburst, {dst,src}_addr_width and
106 * slave_id fields of the DMA slave config from the same fields of the DAI DMA
107 * data struct. The src and dst fields will be initialized depending on the
108 * direction of the substream. If the substream is a playback stream the dst
109 * fields will be initialized, if it is a capture stream the src fields will be
110 * initialized. The {dst,src}_addr_width field will only be initialized if the
111 * addr_width field of the DAI DMA data struct is not equal to
112 * DMA_SLAVE_BUSWIDTH_UNDEFINED.
114 void snd_dmaengine_pcm_set_config_from_dai_data(
115 const struct snd_pcm_substream *substream,
116 const struct snd_dmaengine_dai_dma_data *dma_data,
117 struct dma_slave_config *slave_config)
119 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
120 slave_config->dst_addr = dma_data->addr;
121 slave_config->dst_maxburst = dma_data->maxburst;
122 if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
123 slave_config->dst_addr_width = dma_data->addr_width;
125 slave_config->src_addr = dma_data->addr;
126 slave_config->src_maxburst = dma_data->maxburst;
127 if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
128 slave_config->src_addr_width = dma_data->addr_width;
131 slave_config->slave_id = dma_data->slave_id;
133 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data);
135 #ifdef CONFIG_ARCH_ROCKCHIP
136 static int debug_audio_timeout = 0;
137 module_param(debug_audio_timeout, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
138 MODULE_PARM_DESC(debug_audio_timeout, "Debug interface Audio DMA buffdone time out");
140 static void dmaengine_pcm_dma_complete(void *arg)
142 struct snd_pcm_substream *substream = arg;
143 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
145 #ifdef CONFIG_ARCH_ROCKCHIP
146 if(debug_audio_timeout){
147 struct snd_pcm_runtime *runtime = substream->runtime;
148 static ktime_t before = {0},after = {0};
152 t = ktime_to_us(ktime_sub(after, before));
154 if(t > (snd_pcm_lib_period_bytes(substream)/4+32)*1000*1000/runtime->rate
155 && t != ktime_to_us(after)) // (23220)4096/4/44100 + 32/44100
157 printk(KERN_DEBUG "Time out:: Audio DMA buffdone time out!!! the time = %lld!\n", t);
159 //printk(KERN_DEBUG "audio DMA callback time = %lld\n", t);
162 prtd->pos += snd_pcm_lib_period_bytes(substream);
163 if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
166 snd_pcm_period_elapsed(substream);
169 static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
171 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
172 struct dma_chan *chan = prtd->dma_chan;
173 struct dma_async_tx_descriptor *desc;
174 enum dma_transfer_direction direction;
175 unsigned long flags = DMA_CTRL_ACK;
177 direction = snd_pcm_substream_to_dma_direction(substream);
179 if (!substream->runtime->no_period_wakeup)
180 flags |= DMA_PREP_INTERRUPT;
183 #ifdef CONFIG_ARCH_ROCKCHIP
184 //printk("soc dma buffersize = %d , periodsize=%d, periods=%d\n",
185 // snd_pcm_lib_buffer_bytes(substream),
186 // snd_pcm_lib_period_bytes(substream),
187 // snd_pcm_lib_buffer_bytes(substream)/snd_pcm_lib_period_bytes(substream));
188 desc = dmaengine_prep_dma_infiniteloop(chan,
189 substream->runtime->dma_addr,
190 snd_pcm_lib_buffer_bytes(substream),
191 snd_pcm_lib_period_bytes(substream),
193 snd_pcm_lib_buffer_bytes(substream)/snd_pcm_lib_period_bytes(substream));
195 desc = dmaengine_prep_dma_cyclic(chan,
196 substream->runtime->dma_addr,
197 snd_pcm_lib_buffer_bytes(substream),
198 snd_pcm_lib_period_bytes(substream), direction, flags);
204 desc->callback = dmaengine_pcm_dma_complete;
205 desc->callback_param = substream;
206 prtd->cookie = dmaengine_submit(desc);
212 * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
213 * @substream: PCM substream
214 * @cmd: Trigger command
216 * Returns 0 on success, a negative error code otherwise.
218 * This function can be used as the PCM trigger callback for dmaengine based PCM
219 * driver implementations.
221 int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
223 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
227 case SNDRV_PCM_TRIGGER_START:
228 ret = dmaengine_pcm_prepare_and_submit(substream);
231 dma_async_issue_pending(prtd->dma_chan);
233 case SNDRV_PCM_TRIGGER_RESUME:
234 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
235 dmaengine_resume(prtd->dma_chan);
237 case SNDRV_PCM_TRIGGER_SUSPEND:
238 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
239 dmaengine_pause(prtd->dma_chan);
241 case SNDRV_PCM_TRIGGER_STOP:
242 dmaengine_terminate_all(prtd->dma_chan);
250 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
253 * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
254 * @substream: PCM substream
256 * This function is deprecated and should not be used by new drivers, as its
257 * results may be unreliable.
259 snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
261 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
262 #ifdef CONFIG_ARCH_ROCKCHIP
265 prtd->dma_chan->device->dma_getposition(prtd->dma_chan, &src, &dst);
266 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
267 prtd->pos = dst - substream->runtime->dma_addr;
269 prtd->pos = src - substream->runtime->dma_addr;
271 return bytes_to_frames(substream->runtime, prtd->pos);
273 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
276 * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
277 * @substream: PCM substream
279 * This function can be used as the PCM pointer callback for dmaengine based PCM
280 * driver implementations.
282 snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
284 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
285 struct dma_tx_state state;
286 enum dma_status status;
287 unsigned int buf_size;
288 unsigned int pos = 0;
290 status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
291 if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
292 buf_size = snd_pcm_lib_buffer_bytes(substream);
293 if (state.residue > 0 && state.residue <= buf_size)
294 pos = buf_size - state.residue;
297 return bytes_to_frames(substream->runtime, pos);
299 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
302 * snd_dmaengine_pcm_request_channel - Request channel for the dmaengine PCM
303 * @filter_fn: Filter function used to request the DMA channel
304 * @filter_data: Data passed to the DMA filter function
306 * Returns NULL or the requested DMA channel.
308 * This function request a DMA channel for usage with dmaengine PCM.
310 struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn,
316 dma_cap_set(DMA_SLAVE, mask);
317 dma_cap_set(DMA_CYCLIC, mask);
319 return dma_request_channel(mask, filter_fn, filter_data);
321 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_request_channel);
324 * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
325 * @substream: PCM substream
326 * @chan: DMA channel to use for data transfers
328 * Returns 0 on success, a negative error code otherwise.
330 * The function should usually be called from the pcm open callback. Note that
331 * this function will use private_data field of the substream's runtime. So it
332 * is not availabe to your pcm driver implementation.
334 int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
335 struct dma_chan *chan)
337 struct dmaengine_pcm_runtime_data *prtd;
343 ret = snd_pcm_hw_constraint_integer(substream->runtime,
344 SNDRV_PCM_HW_PARAM_PERIODS);
348 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
352 prtd->dma_chan = chan;
354 substream->runtime->private_data = prtd;
358 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
361 * snd_dmaengine_pcm_open_request_chan - Open a dmaengine based PCM substream and request channel
362 * @substream: PCM substream
363 * @filter_fn: Filter function used to request the DMA channel
364 * @filter_data: Data passed to the DMA filter function
366 * Returns 0 on success, a negative error code otherwise.
368 * This function will request a DMA channel using the passed filter function and
369 * data. The function should usually be called from the pcm open callback. Note
370 * that this function will use private_data field of the substream's runtime. So
371 * it is not availabe to your pcm driver implementation.
373 int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream *substream,
374 dma_filter_fn filter_fn, void *filter_data)
376 return snd_dmaengine_pcm_open(substream,
377 snd_dmaengine_pcm_request_channel(filter_fn, filter_data));
379 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open_request_chan);
382 * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
383 * @substream: PCM substream
385 int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
387 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
393 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
396 * snd_dmaengine_pcm_release_chan_close - Close a dmaengine based PCM substream and release channel
397 * @substream: PCM substream
399 * Releases the DMA channel associated with the PCM substream.
401 int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream)
403 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
405 dma_release_channel(prtd->dma_chan);
407 return snd_dmaengine_pcm_close(substream);
409 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close_release_chan);
411 MODULE_LICENSE("GPL");