blob: 94d08733cb388cdda361e509701da59650714eeb [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;
Takashi Iwaia655f752013-10-25 16:33:12 +020066 int bits;
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +010067
Takashi Iwaia655f752013-10-25 16:33:12 +020068 bits = snd_pcm_format_physical_width(params_format(params));
69 if (bits < 8 || bits > 64)
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +010070 return -EINVAL;
Takashi Iwaia655f752013-10-25 16:33:12 +020071 else if (bits == 8)
72 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
73 else if (bits == 16)
74 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
75 else if (bits <= 32)
76 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
77 else
78 buswidth = DMA_SLAVE_BUSWIDTH_8_BYTES;
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +010079
80 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
81 slave_config->direction = DMA_MEM_TO_DEV;
82 slave_config->dst_addr_width = buswidth;
83 } else {
84 slave_config->direction = DMA_DEV_TO_MEM;
85 slave_config->src_addr_width = buswidth;
86 }
87
Lars-Peter Clausen5fa70f72013-04-03 11:02:56 +020088 slave_config->device_fc = false;
89
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +010090 return 0;
91}
92EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
93
Lars-Peter Clausen85c9f9c2013-04-03 11:06:02 +020094/**
95 * snd_dmaengine_pcm_set_config_from_dai_data() - Initializes a dma slave config
96 * using DAI DMA data.
97 * @substream: PCM substream
98 * @dma_data: DAI DMA data
99 * @slave_config: DMA slave configuration
100 *
101 * Initializes the {dst,src}_addr, {dst,src}_maxburst, {dst,src}_addr_width and
102 * slave_id fields of the DMA slave config from the same fields of the DAI DMA
103 * data struct. The src and dst fields will be initialized depending on the
104 * direction of the substream. If the substream is a playback stream the dst
105 * fields will be initialized, if it is a capture stream the src fields will be
106 * initialized. The {dst,src}_addr_width field will only be initialized if the
107 * addr_width field of the DAI DMA data struct is not equal to
108 * DMA_SLAVE_BUSWIDTH_UNDEFINED.
109 */
110void snd_dmaengine_pcm_set_config_from_dai_data(
111 const struct snd_pcm_substream *substream,
112 const struct snd_dmaengine_dai_dma_data *dma_data,
113 struct dma_slave_config *slave_config)
114{
115 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
116 slave_config->dst_addr = dma_data->addr;
117 slave_config->dst_maxburst = dma_data->maxburst;
118 if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
119 slave_config->dst_addr_width = dma_data->addr_width;
120 } else {
121 slave_config->src_addr = dma_data->addr;
122 slave_config->src_maxburst = dma_data->maxburst;
123 if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
124 slave_config->src_addr_width = dma_data->addr_width;
125 }
126
127 slave_config->slave_id = dma_data->slave_id;
128}
129EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data);
130
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100131static void dmaengine_pcm_dma_complete(void *arg)
132{
133 struct snd_pcm_substream *substream = arg;
134 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
135
136 prtd->pos += snd_pcm_lib_period_bytes(substream);
137 if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
138 prtd->pos = 0;
139
140 snd_pcm_period_elapsed(substream);
141}
142
143static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
144{
145 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
146 struct dma_chan *chan = prtd->dma_chan;
147 struct dma_async_tx_descriptor *desc;
148 enum dma_transfer_direction direction;
Peter Ujfalusie7736cd2012-09-24 10:58:04 +0300149 unsigned long flags = DMA_CTRL_ACK;
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100150
151 direction = snd_pcm_substream_to_dma_direction(substream);
152
Peter Ujfalusie7736cd2012-09-24 10:58:04 +0300153 if (!substream->runtime->no_period_wakeup)
154 flags |= DMA_PREP_INTERRUPT;
155
Mika Westerberg7a08cf702012-03-05 14:02:14 +0100156 prtd->pos = 0;
Vinod Koul41ba6b72012-03-26 17:28:08 +0530157 desc = dmaengine_prep_dma_cyclic(chan,
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100158 substream->runtime->dma_addr,
159 snd_pcm_lib_buffer_bytes(substream),
Peter Ujfalusie7736cd2012-09-24 10:58:04 +0300160 snd_pcm_lib_period_bytes(substream), direction, flags);
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100161
162 if (!desc)
163 return -ENOMEM;
164
165 desc->callback = dmaengine_pcm_dma_complete;
166 desc->callback_param = substream;
Lars-Peter Clausen3528f272012-06-11 20:11:42 +0200167 prtd->cookie = dmaengine_submit(desc);
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100168
169 return 0;
170}
171
172/**
173 * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
174 * @substream: PCM substream
175 * @cmd: Trigger command
176 *
177 * Returns 0 on success, a negative error code otherwise.
178 *
179 * This function can be used as the PCM trigger callback for dmaengine based PCM
180 * driver implementations.
181 */
182int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
183{
184 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
185 int ret;
186
187 switch (cmd) {
188 case SNDRV_PCM_TRIGGER_START:
189 ret = dmaengine_pcm_prepare_and_submit(substream);
190 if (ret)
191 return ret;
192 dma_async_issue_pending(prtd->dma_chan);
193 break;
194 case SNDRV_PCM_TRIGGER_RESUME:
195 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
196 dmaengine_resume(prtd->dma_chan);
197 break;
198 case SNDRV_PCM_TRIGGER_SUSPEND:
199 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
200 dmaengine_pause(prtd->dma_chan);
201 break;
202 case SNDRV_PCM_TRIGGER_STOP:
203 dmaengine_terminate_all(prtd->dma_chan);
204 break;
205 default:
206 return -EINVAL;
207 }
208
209 return 0;
210}
211EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
212
213/**
Lars-Peter Clausen9883ab22012-06-11 20:11:41 +0200214 * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100215 * @substream: PCM substream
216 *
Lars-Peter Clausen9883ab22012-06-11 20:11:41 +0200217 * This function is deprecated and should not be used by new drivers, as its
218 * results may be unreliable.
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100219 */
Lars-Peter Clausen9883ab22012-06-11 20:11:41 +0200220snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100221{
222 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
223 return bytes_to_frames(substream->runtime, prtd->pos);
224}
Lars-Peter Clausen9883ab22012-06-11 20:11:41 +0200225EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100226
Lars-Peter Clausen3528f272012-06-11 20:11:42 +0200227/**
228 * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
229 * @substream: PCM substream
230 *
231 * This function can be used as the PCM pointer callback for dmaengine based PCM
232 * driver implementations.
233 */
234snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
235{
236 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
237 struct dma_tx_state state;
238 enum dma_status status;
239 unsigned int buf_size;
240 unsigned int pos = 0;
241
242 status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
243 if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
244 buf_size = snd_pcm_lib_buffer_bytes(substream);
245 if (state.residue > 0 && state.residue <= buf_size)
246 pos = buf_size - state.residue;
247 }
248
249 return bytes_to_frames(substream->runtime, pos);
250}
251EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
252
Lars-Peter Clausenc9998362013-04-15 19:19:51 +0200253/**
254 * snd_dmaengine_pcm_request_channel - Request channel for the dmaengine PCM
255 * @filter_fn: Filter function used to request the DMA channel
256 * @filter_data: Data passed to the DMA filter function
257 *
258 * Returns NULL or the requested DMA channel.
259 *
260 * This function request a DMA channel for usage with dmaengine PCM.
261 */
262struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn,
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +0200263 void *filter_data)
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100264{
265 dma_cap_mask_t mask;
266
267 dma_cap_zero(mask);
268 dma_cap_set(DMA_SLAVE, mask);
269 dma_cap_set(DMA_CYCLIC, mask);
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100270
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +0200271 return dma_request_channel(mask, filter_fn, filter_data);
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100272}
Lars-Peter Clausenc9998362013-04-15 19:19:51 +0200273EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_request_channel);
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100274
275/**
276 * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
277 * @substream: PCM substream
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +0200278 * @chan: DMA channel to use for data transfers
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100279 *
280 * Returns 0 on success, a negative error code otherwise.
281 *
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +0200282 * The function should usually be called from the pcm open callback. Note that
283 * this function will use private_data field of the substream's runtime. So it
284 * is not availabe to your pcm driver implementation.
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100285 */
286int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +0200287 struct dma_chan *chan)
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100288{
289 struct dmaengine_pcm_runtime_data *prtd;
290 int ret;
291
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +0200292 if (!chan)
293 return -ENXIO;
294
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100295 ret = snd_pcm_hw_constraint_integer(substream->runtime,
296 SNDRV_PCM_HW_PARAM_PERIODS);
297 if (ret < 0)
298 return ret;
299
300 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
301 if (!prtd)
302 return -ENOMEM;
303
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +0200304 prtd->dma_chan = chan;
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100305
306 substream->runtime->private_data = prtd;
307
308 return 0;
309}
310EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
311
312/**
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +0200313 * snd_dmaengine_pcm_open_request_chan - Open a dmaengine based PCM substream and request channel
314 * @substream: PCM substream
315 * @filter_fn: Filter function used to request the DMA channel
316 * @filter_data: Data passed to the DMA filter function
317 *
318 * Returns 0 on success, a negative error code otherwise.
319 *
320 * This function will request a DMA channel using the passed filter function and
321 * data. The function should usually be called from the pcm open callback. Note
322 * that this function will use private_data field of the substream's runtime. So
323 * it is not availabe to your pcm driver implementation.
324 */
325int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream *substream,
326 dma_filter_fn filter_fn, void *filter_data)
327{
328 return snd_dmaengine_pcm_open(substream,
Lars-Peter Clausenc9998362013-04-15 19:19:51 +0200329 snd_dmaengine_pcm_request_channel(filter_fn, filter_data));
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +0200330}
331EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open_request_chan);
332
333/**
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100334 * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
335 * @substream: PCM substream
336 */
337int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
338{
339 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
340
Lars-Peter Clausene7f73a12012-02-22 10:49:08 +0100341 kfree(prtd);
342
343 return 0;
344}
345EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
Lothar Waßmanncba69b42012-11-22 13:31:10 +0100346
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +0200347/**
348 * snd_dmaengine_pcm_release_chan_close - Close a dmaengine based PCM substream and release channel
349 * @substream: PCM substream
350 *
351 * Releases the DMA channel associated with the PCM substream.
352 */
353int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream)
354{
355 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
356
357 dma_release_channel(prtd->dma_chan);
358
359 return snd_dmaengine_pcm_close(substream);
360}
361EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close_release_chan);
362
Lothar Waßmanncba69b42012-11-22 13:31:10 +0100363MODULE_LICENSE("GPL");