udl: handle EDID failure properly.
[linux-2.6.git] / sound / soc / soc-dmaengine-pcm.c
blob111b7d921e890c0051444b1a28fb6edfe78fd837
1 /*
2 * Copyright (C) 2012, Analog Devices Inc.
3 * Author: Lars-Peter Clausen <lars@metafoo.de>
5 * Based on:
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;
33 dma_cookie_t cookie;
35 unsigned int pos;
37 void *data;
40 static inline struct dmaengine_pcm_runtime_data *substream_to_prtd(
41 const struct snd_pcm_substream *substream)
43 return substream->runtime->private_data;
46 /**
47 * snd_dmaengine_pcm_set_data - Set dmaengine substream private data
48 * @substream: PCM substream
49 * @data: Data to set
51 void snd_dmaengine_pcm_set_data(struct snd_pcm_substream *substream, void *data)
53 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
55 prtd->data = data;
57 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_data);
59 /**
60 * snd_dmaengine_pcm_get_data - Get dmaeinge substream private data
61 * @substream: PCM substream
63 * Returns the data previously set with snd_dmaengine_pcm_set_data
65 void *snd_dmaengine_pcm_get_data(struct snd_pcm_substream *substream)
67 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
69 return prtd->data;
71 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_data);
73 struct dma_chan *snd_dmaengine_pcm_get_chan(struct snd_pcm_substream *substream)
75 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
77 return prtd->dma_chan;
79 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_chan);
81 /**
82 * snd_hwparams_to_dma_slave_config - Convert hw_params to dma_slave_config
83 * @substream: PCM substream
84 * @params: hw_params
85 * @slave_config: DMA slave config
87 * This function can be used to initialize a dma_slave_config from a substream
88 * and hw_params in a dmaengine based PCM driver implementation.
90 int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream *substream,
91 const struct snd_pcm_hw_params *params,
92 struct dma_slave_config *slave_config)
94 enum dma_slave_buswidth buswidth;
96 switch (params_format(params)) {
97 case SNDRV_PCM_FORMAT_S8:
98 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
99 break;
100 case SNDRV_PCM_FORMAT_S16_LE:
101 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
102 break;
103 case SNDRV_PCM_FORMAT_S18_3LE:
104 case SNDRV_PCM_FORMAT_S20_3LE:
105 case SNDRV_PCM_FORMAT_S24_LE:
106 case SNDRV_PCM_FORMAT_S32_LE:
107 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
108 break;
109 default:
110 return -EINVAL;
113 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
114 slave_config->direction = DMA_MEM_TO_DEV;
115 slave_config->dst_addr_width = buswidth;
116 } else {
117 slave_config->direction = DMA_DEV_TO_MEM;
118 slave_config->src_addr_width = buswidth;
121 return 0;
123 EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
125 static void dmaengine_pcm_dma_complete(void *arg)
127 struct snd_pcm_substream *substream = arg;
128 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
130 prtd->pos += snd_pcm_lib_period_bytes(substream);
131 if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
132 prtd->pos = 0;
134 snd_pcm_period_elapsed(substream);
137 static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
139 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
140 struct dma_chan *chan = prtd->dma_chan;
141 struct dma_async_tx_descriptor *desc;
142 enum dma_transfer_direction direction;
143 unsigned long flags = DMA_CTRL_ACK;
145 direction = snd_pcm_substream_to_dma_direction(substream);
147 if (!substream->runtime->no_period_wakeup)
148 flags |= DMA_PREP_INTERRUPT;
150 prtd->pos = 0;
151 desc = dmaengine_prep_dma_cyclic(chan,
152 substream->runtime->dma_addr,
153 snd_pcm_lib_buffer_bytes(substream),
154 snd_pcm_lib_period_bytes(substream), direction, flags);
156 if (!desc)
157 return -ENOMEM;
159 desc->callback = dmaengine_pcm_dma_complete;
160 desc->callback_param = substream;
161 prtd->cookie = dmaengine_submit(desc);
163 return 0;
167 * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
168 * @substream: PCM substream
169 * @cmd: Trigger command
171 * Returns 0 on success, a negative error code otherwise.
173 * This function can be used as the PCM trigger callback for dmaengine based PCM
174 * driver implementations.
176 int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
178 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
179 int ret;
181 switch (cmd) {
182 case SNDRV_PCM_TRIGGER_START:
183 ret = dmaengine_pcm_prepare_and_submit(substream);
184 if (ret)
185 return ret;
186 dma_async_issue_pending(prtd->dma_chan);
187 break;
188 case SNDRV_PCM_TRIGGER_RESUME:
189 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
190 dmaengine_resume(prtd->dma_chan);
191 break;
192 case SNDRV_PCM_TRIGGER_SUSPEND:
193 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
194 dmaengine_pause(prtd->dma_chan);
195 break;
196 case SNDRV_PCM_TRIGGER_STOP:
197 dmaengine_terminate_all(prtd->dma_chan);
198 break;
199 default:
200 return -EINVAL;
203 return 0;
205 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
208 * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
209 * @substream: PCM substream
211 * This function is deprecated and should not be used by new drivers, as its
212 * results may be unreliable.
214 snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
216 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
217 return bytes_to_frames(substream->runtime, prtd->pos);
219 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
222 * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
223 * @substream: PCM substream
225 * This function can be used as the PCM pointer callback for dmaengine based PCM
226 * driver implementations.
228 snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
230 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
231 struct dma_tx_state state;
232 enum dma_status status;
233 unsigned int buf_size;
234 unsigned int pos = 0;
236 status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
237 if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
238 buf_size = snd_pcm_lib_buffer_bytes(substream);
239 if (state.residue > 0 && state.residue <= buf_size)
240 pos = buf_size - state.residue;
243 return bytes_to_frames(substream->runtime, pos);
245 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
247 static int dmaengine_pcm_request_channel(struct dmaengine_pcm_runtime_data *prtd,
248 dma_filter_fn filter_fn, void *filter_data)
250 dma_cap_mask_t mask;
252 dma_cap_zero(mask);
253 dma_cap_set(DMA_SLAVE, mask);
254 dma_cap_set(DMA_CYCLIC, mask);
255 prtd->dma_chan = dma_request_channel(mask, filter_fn, filter_data);
257 if (!prtd->dma_chan)
258 return -ENXIO;
260 return 0;
264 * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
265 * @substream: PCM substream
266 * @filter_fn: Filter function used to request the DMA channel
267 * @filter_data: Data passed to the DMA filter function
269 * Returns 0 on success, a negative error code otherwise.
271 * This function will request a DMA channel using the passed filter function and
272 * data. The function should usually be called from the pcm open callback.
274 * Note that this function will use private_data field of the substream's
275 * runtime. So it is not availabe to your pcm driver implementation. If you need
276 * to keep additional data attached to a substream use
277 * snd_dmaengine_pcm_{set,get}_data.
279 int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
280 dma_filter_fn filter_fn, void *filter_data)
282 struct dmaengine_pcm_runtime_data *prtd;
283 int ret;
285 ret = snd_pcm_hw_constraint_integer(substream->runtime,
286 SNDRV_PCM_HW_PARAM_PERIODS);
287 if (ret < 0)
288 return ret;
290 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
291 if (!prtd)
292 return -ENOMEM;
294 ret = dmaengine_pcm_request_channel(prtd, filter_fn, filter_data);
295 if (ret < 0) {
296 kfree(prtd);
297 return ret;
300 substream->runtime->private_data = prtd;
302 return 0;
304 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
307 * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
308 * @substream: PCM substream
310 int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
312 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
314 dma_release_channel(prtd->dma_chan);
315 kfree(prtd);
317 return 0;
319 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
321 MODULE_LICENSE("GPL");