blob: 7c24dedff971f2e20604fa100ef82a2e50003e03 [file] [log] [blame]
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +01001/*
2 * Copyright (C) 2012, Analog Devices Inc.
3 * Author: Lars-Peter Clausen <lars@metafoo.de>
4 *
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
10 *
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.
15 *
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.
19 *
20 */
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>
28
29#include <sound/dmaengine_pcm.h>
30
31struct dmaengine_pcm_runtime_data {
32 struct dma_chan *dma_chan;
Lars-Peter Clausen3528f272012-06-11 20:11:42 +020033 dma_cookie_t cookie;
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +010034
35 unsigned int pos;
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +010036};
37
38static inline struct dmaengine_pcm_runtime_data *substream_to_prtd(
39 const struct snd_pcm_substream *substream)
40{
41 return substream->runtime->private_data;
42}
43
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +010044struct dma_chan *snd_dmaengine_pcm_get_chan(struct snd_pcm_substream *substream)
45{
46 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
47
48 return prtd->dma_chan;
49}
50EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_chan);
51
52/**
53 * snd_hwparams_to_dma_slave_config - Convert hw_params to dma_slave_config
54 * @substream: PCM substream
55 * @params: hw_params
56 * @slave_config: DMA slave config
57 *
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.
60 */
61int 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)
64{
65 enum dma_slave_buswidth buswidth;
66
67 switch (params_format(params)) {
68 case SNDRV_PCM_FORMAT_S8:
69 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
70 break;
71 case SNDRV_PCM_FORMAT_S16_LE:
72 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
73 break;
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;
79 break;
80 default:
81 return -EINVAL;
82 }
83
84 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
85 slave_config->direction = DMA_MEM_TO_DEV;
86 slave_config->dst_addr_width = buswidth;
87 } else {
88 slave_config->direction = DMA_DEV_TO_MEM;
89 slave_config->src_addr_width = buswidth;
90 }
91
Lars-Peter Clausen5fa70f72013-04-03 11:02:56 +020092 slave_config->device_fc = false;
93
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +010094 return 0;
95}
96EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
97
98static void dmaengine_pcm_dma_complete(void *arg)
99{
100 struct snd_pcm_substream *substream = arg;
101 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
102
103 prtd->pos += snd_pcm_lib_period_bytes(substream);
104 if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
105 prtd->pos = 0;
106
107 snd_pcm_period_elapsed(substream);
108}
109
110static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
111{
112 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
113 struct dma_chan *chan = prtd->dma_chan;
114 struct dma_async_tx_descriptor *desc;
115 enum dma_transfer_direction direction;
Peter Ujfalusie7736cd2012-09-24 10:58:04 +0300116 unsigned long flags = DMA_CTRL_ACK;
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100117
118 direction = snd_pcm_substream_to_dma_direction(substream);
119
Peter Ujfalusie7736cd2012-09-24 10:58:04 +0300120 if (!substream->runtime->no_period_wakeup)
121 flags |= DMA_PREP_INTERRUPT;
122
Mika Westerberg7a08cf702012-03-05 14:02:14 +0100123 prtd->pos = 0;
Vinod Koul41ba6b72012-03-26 17:28:08 +0530124 desc = dmaengine_prep_dma_cyclic(chan,
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100125 substream->runtime->dma_addr,
126 snd_pcm_lib_buffer_bytes(substream),
Peter Ujfalusie7736cd2012-09-24 10:58:04 +0300127 snd_pcm_lib_period_bytes(substream), direction, flags);
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100128
129 if (!desc)
130 return -ENOMEM;
131
132 desc->callback = dmaengine_pcm_dma_complete;
133 desc->callback_param = substream;
Lars-Peter Clausen3528f272012-06-11 20:11:42 +0200134 prtd->cookie = dmaengine_submit(desc);
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100135
136 return 0;
137}
138
139/**
140 * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
141 * @substream: PCM substream
142 * @cmd: Trigger command
143 *
144 * Returns 0 on success, a negative error code otherwise.
145 *
146 * This function can be used as the PCM trigger callback for dmaengine based PCM
147 * driver implementations.
148 */
149int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
150{
151 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
152 int ret;
153
154 switch (cmd) {
155 case SNDRV_PCM_TRIGGER_START:
156 ret = dmaengine_pcm_prepare_and_submit(substream);
157 if (ret)
158 return ret;
159 dma_async_issue_pending(prtd->dma_chan);
160 break;
161 case SNDRV_PCM_TRIGGER_RESUME:
162 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
163 dmaengine_resume(prtd->dma_chan);
164 break;
165 case SNDRV_PCM_TRIGGER_SUSPEND:
166 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
167 dmaengine_pause(prtd->dma_chan);
168 break;
169 case SNDRV_PCM_TRIGGER_STOP:
170 dmaengine_terminate_all(prtd->dma_chan);
171 break;
172 default:
173 return -EINVAL;
174 }
175
176 return 0;
177}
178EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
179
180/**
Lars-Peter Clausen9883ab22012-06-11 20:11:41 +0200181 * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100182 * @substream: PCM substream
183 *
Lars-Peter Clausen9883ab22012-06-11 20:11:41 +0200184 * This function is deprecated and should not be used by new drivers, as its
185 * results may be unreliable.
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100186 */
Lars-Peter Clausen9883ab22012-06-11 20:11:41 +0200187snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100188{
189 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
190 return bytes_to_frames(substream->runtime, prtd->pos);
191}
Lars-Peter Clausen9883ab22012-06-11 20:11:41 +0200192EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100193
Lars-Peter Clausen3528f272012-06-11 20:11:42 +0200194/**
195 * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
196 * @substream: PCM substream
197 *
198 * This function can be used as the PCM pointer callback for dmaengine based PCM
199 * driver implementations.
200 */
201snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
202{
203 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
204 struct dma_tx_state state;
205 enum dma_status status;
206 unsigned int buf_size;
207 unsigned int pos = 0;
208
209 status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
210 if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
211 buf_size = snd_pcm_lib_buffer_bytes(substream);
212 if (state.residue > 0 && state.residue <= buf_size)
213 pos = buf_size - state.residue;
214 }
215
216 return bytes_to_frames(substream->runtime, pos);
217}
218EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
219
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100220static int dmaengine_pcm_request_channel(struct dmaengine_pcm_runtime_data *prtd,
221 dma_filter_fn filter_fn, void *filter_data)
222{
223 dma_cap_mask_t mask;
224
225 dma_cap_zero(mask);
226 dma_cap_set(DMA_SLAVE, mask);
227 dma_cap_set(DMA_CYCLIC, mask);
228 prtd->dma_chan = dma_request_channel(mask, filter_fn, filter_data);
229
230 if (!prtd->dma_chan)
231 return -ENXIO;
232
233 return 0;
234}
235
236/**
237 * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
238 * @substream: PCM substream
239 * @filter_fn: Filter function used to request the DMA channel
240 * @filter_data: Data passed to the DMA filter function
241 *
242 * Returns 0 on success, a negative error code otherwise.
243 *
244 * This function will request a DMA channel using the passed filter function and
245 * data. The function should usually be called from the pcm open callback.
246 *
247 * Note that this function will use private_data field of the substream's
248 * runtime. So it is not availabe to your pcm driver implementation. If you need
249 * to keep additional data attached to a substream use
Uwe Kleine-König1573ee82012-06-25 09:28:46 +0200250 * snd_dmaengine_pcm_{set,get}_data.
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100251 */
252int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
253 dma_filter_fn filter_fn, void *filter_data)
254{
255 struct dmaengine_pcm_runtime_data *prtd;
256 int ret;
257
258 ret = snd_pcm_hw_constraint_integer(substream->runtime,
259 SNDRV_PCM_HW_PARAM_PERIODS);
260 if (ret < 0)
261 return ret;
262
263 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
264 if (!prtd)
265 return -ENOMEM;
266
267 ret = dmaengine_pcm_request_channel(prtd, filter_fn, filter_data);
268 if (ret < 0) {
269 kfree(prtd);
270 return ret;
271 }
272
273 substream->runtime->private_data = prtd;
274
275 return 0;
276}
277EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
278
279/**
280 * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
281 * @substream: PCM substream
282 */
283int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
284{
285 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
286
287 dma_release_channel(prtd->dma_chan);
288 kfree(prtd);
289
290 return 0;
291}
292EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
Lothar Waßmanncba69b42012-11-22 13:31:10 +0100293
294MODULE_LICENSE("GPL");