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;
39 static inline struct dmaengine_pcm_runtime_data *substream_to_prtd(
40 const struct snd_pcm_substream *substream)
42 return substream->runtime->private_data;
46 * snd_dmaengine_pcm_set_data - Set dmaengine substream private data
47 * @substream: PCM substream
50 void snd_dmaengine_pcm_set_data(struct snd_pcm_substream *substream, void *data)
52 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
56 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_data);
59 * snd_dmaengine_pcm_get_data - Get dmaeinge substream private data
60 * @substream: PCM substream
62 * Returns the data previously set with snd_dmaengine_pcm_set_data
64 void *snd_dmaengine_pcm_get_data(struct snd_pcm_substream *substream)
66 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
70 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_data);
72 struct dma_chan *snd_dmaengine_pcm_get_chan(struct snd_pcm_substream *substream)
74 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
76 return prtd->dma_chan;
78 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_chan);
81 * snd_hwparams_to_dma_slave_config - Convert hw_params to dma_slave_config
82 * @substream: PCM substream
84 * @slave_config: DMA slave config
86 * This function can be used to initialize a dma_slave_config from a substream
87 * and hw_params in a dmaengine based PCM driver implementation.
89 int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream *substream,
90 const struct snd_pcm_hw_params *params,
91 struct dma_slave_config *slave_config)
93 enum dma_slave_buswidth buswidth;
95 switch (params_format(params)) {
96 case SNDRV_PCM_FORMAT_S8:
97 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
99 case SNDRV_PCM_FORMAT_S16_LE:
100 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
102 case SNDRV_PCM_FORMAT_S18_3LE:
103 case SNDRV_PCM_FORMAT_S20_3LE:
104 case SNDRV_PCM_FORMAT_S24_LE:
105 case SNDRV_PCM_FORMAT_S32_LE:
106 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
112 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
113 slave_config->direction = DMA_MEM_TO_DEV;
114 slave_config->dst_addr_width = buswidth;
116 slave_config->direction = DMA_DEV_TO_MEM;
117 slave_config->src_addr_width = buswidth;
122 EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
124 static void dmaengine_pcm_dma_complete(void *arg)
126 struct snd_pcm_substream *substream = arg;
127 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
129 prtd->pos += snd_pcm_lib_period_bytes(substream);
130 if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
133 snd_pcm_period_elapsed(substream);
136 static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
138 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
139 struct dma_chan *chan = prtd->dma_chan;
140 struct dma_async_tx_descriptor *desc;
141 enum dma_transfer_direction direction;
143 direction = snd_pcm_substream_to_dma_direction(substream);
146 desc = dmaengine_prep_dma_cyclic(chan,
147 substream->runtime->dma_addr,
148 snd_pcm_lib_buffer_bytes(substream),
149 snd_pcm_lib_period_bytes(substream), direction);
154 desc->callback = dmaengine_pcm_dma_complete;
155 desc->callback_param = substream;
156 dmaengine_submit(desc);
162 * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
163 * @substream: PCM substream
164 * @cmd: Trigger command
166 * Returns 0 on success, a negative error code otherwise.
168 * This function can be used as the PCM trigger callback for dmaengine based PCM
169 * driver implementations.
171 int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
173 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
177 case SNDRV_PCM_TRIGGER_START:
178 ret = dmaengine_pcm_prepare_and_submit(substream);
181 dma_async_issue_pending(prtd->dma_chan);
183 case SNDRV_PCM_TRIGGER_RESUME:
184 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
185 dmaengine_resume(prtd->dma_chan);
187 case SNDRV_PCM_TRIGGER_SUSPEND:
188 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
189 dmaengine_pause(prtd->dma_chan);
191 case SNDRV_PCM_TRIGGER_STOP:
192 dmaengine_terminate_all(prtd->dma_chan);
200 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
203 * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
204 * @substream: PCM substream
206 * This function can be used as the PCM pointer callback for dmaengine based PCM
207 * driver implementations.
209 snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
211 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
212 return bytes_to_frames(substream->runtime, prtd->pos);
214 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
216 static int dmaengine_pcm_request_channel(struct dmaengine_pcm_runtime_data *prtd,
217 dma_filter_fn filter_fn, void *filter_data)
222 dma_cap_set(DMA_SLAVE, mask);
223 dma_cap_set(DMA_CYCLIC, mask);
224 prtd->dma_chan = dma_request_channel(mask, filter_fn, filter_data);
233 * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
234 * @substream: PCM substream
235 * @filter_fn: Filter function used to request the DMA channel
236 * @filter_data: Data passed to the DMA filter function
238 * Returns 0 on success, a negative error code otherwise.
240 * This function will request a DMA channel using the passed filter function and
241 * data. The function should usually be called from the pcm open callback.
243 * Note that this function will use private_data field of the substream's
244 * runtime. So it is not availabe to your pcm driver implementation. If you need
245 * to keep additional data attached to a substream use
246 * snd_dmaeinge_pcm_{set,get}_data.
248 int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
249 dma_filter_fn filter_fn, void *filter_data)
251 struct dmaengine_pcm_runtime_data *prtd;
254 ret = snd_pcm_hw_constraint_integer(substream->runtime,
255 SNDRV_PCM_HW_PARAM_PERIODS);
259 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
263 ret = dmaengine_pcm_request_channel(prtd, filter_fn, filter_data);
269 substream->runtime->private_data = prtd;
273 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
276 * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
277 * @substream: PCM substream
279 int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
281 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
283 dma_release_channel(prtd->dma_chan);
288 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);