GNU Linux-libre 4.19.268-gnu1
[releases.git] / sound / core / pcm_dmaengine.c
1 /*
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
31 struct dmaengine_pcm_runtime_data {
32         struct dma_chan *dma_chan;
33         dma_cookie_t cookie;
34
35         unsigned int pos;
36 };
37
38 static 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
44 struct 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 }
50 EXPORT_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  */
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)
64 {
65         enum dma_slave_buswidth buswidth;
66         int bits;
67
68         bits = params_physical_width(params);
69         if (bits < 8 || bits > 64)
70                 return -EINVAL;
71         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 == 24)
76                 buswidth = DMA_SLAVE_BUSWIDTH_3_BYTES;
77         else if (bits <= 32)
78                 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
79         else
80                 buswidth = DMA_SLAVE_BUSWIDTH_8_BYTES;
81
82         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
83                 slave_config->direction = DMA_MEM_TO_DEV;
84                 slave_config->dst_addr_width = buswidth;
85         } else {
86                 slave_config->direction = DMA_DEV_TO_MEM;
87                 slave_config->src_addr_width = buswidth;
88         }
89
90         slave_config->device_fc = false;
91
92         return 0;
93 }
94 EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
95
96 /**
97  * snd_dmaengine_pcm_set_config_from_dai_data() - Initializes a dma slave config
98  *  using DAI DMA data.
99  * @substream: PCM substream
100  * @dma_data: DAI DMA data
101  * @slave_config: DMA slave configuration
102  *
103  * Initializes the {dst,src}_addr, {dst,src}_maxburst, {dst,src}_addr_width and
104  * slave_id fields of the DMA slave config from the same fields of the DAI DMA
105  * data struct. The src and dst fields will be initialized depending on the
106  * direction of the substream. If the substream is a playback stream the dst
107  * fields will be initialized, if it is a capture stream the src fields will be
108  * initialized. The {dst,src}_addr_width field will only be initialized if the
109  * SND_DMAENGINE_PCM_DAI_FLAG_PACK flag is set or if the addr_width field of
110  * the DAI DMA data struct is not equal to DMA_SLAVE_BUSWIDTH_UNDEFINED. If
111  * both conditions are met the latter takes priority.
112  */
113 void snd_dmaengine_pcm_set_config_from_dai_data(
114         const struct snd_pcm_substream *substream,
115         const struct snd_dmaengine_dai_dma_data *dma_data,
116         struct dma_slave_config *slave_config)
117 {
118         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
119                 slave_config->dst_addr = dma_data->addr;
120                 slave_config->dst_maxburst = dma_data->maxburst;
121                 if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK)
122                         slave_config->dst_addr_width =
123                                 DMA_SLAVE_BUSWIDTH_UNDEFINED;
124                 if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
125                         slave_config->dst_addr_width = dma_data->addr_width;
126         } else {
127                 slave_config->src_addr = dma_data->addr;
128                 slave_config->src_maxburst = dma_data->maxburst;
129                 if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK)
130                         slave_config->src_addr_width =
131                                 DMA_SLAVE_BUSWIDTH_UNDEFINED;
132                 if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
133                         slave_config->src_addr_width = dma_data->addr_width;
134         }
135
136         slave_config->slave_id = dma_data->slave_id;
137 }
138 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data);
139
140 static void dmaengine_pcm_dma_complete(void *arg)
141 {
142         unsigned int new_pos;
143         struct snd_pcm_substream *substream = arg;
144         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
145
146         new_pos = prtd->pos + snd_pcm_lib_period_bytes(substream);
147         if (new_pos >= snd_pcm_lib_buffer_bytes(substream))
148                 new_pos = 0;
149         prtd->pos = new_pos;
150
151         snd_pcm_period_elapsed(substream);
152 }
153
154 static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
155 {
156         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
157         struct dma_chan *chan = prtd->dma_chan;
158         struct dma_async_tx_descriptor *desc;
159         enum dma_transfer_direction direction;
160         unsigned long flags = DMA_CTRL_ACK;
161
162         direction = snd_pcm_substream_to_dma_direction(substream);
163
164         if (!substream->runtime->no_period_wakeup)
165                 flags |= DMA_PREP_INTERRUPT;
166
167         prtd->pos = 0;
168         desc = dmaengine_prep_dma_cyclic(chan,
169                 substream->runtime->dma_addr,
170                 snd_pcm_lib_buffer_bytes(substream),
171                 snd_pcm_lib_period_bytes(substream), direction, flags);
172
173         if (!desc)
174                 return -ENOMEM;
175
176         desc->callback = dmaengine_pcm_dma_complete;
177         desc->callback_param = substream;
178         prtd->cookie = dmaengine_submit(desc);
179
180         return 0;
181 }
182
183 /**
184  * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
185  * @substream: PCM substream
186  * @cmd: Trigger command
187  *
188  * Returns 0 on success, a negative error code otherwise.
189  *
190  * This function can be used as the PCM trigger callback for dmaengine based PCM
191  * driver implementations.
192  */
193 int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
194 {
195         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
196         struct snd_pcm_runtime *runtime = substream->runtime;
197         int ret;
198
199         switch (cmd) {
200         case SNDRV_PCM_TRIGGER_START:
201                 ret = dmaengine_pcm_prepare_and_submit(substream);
202                 if (ret)
203                         return ret;
204                 dma_async_issue_pending(prtd->dma_chan);
205                 break;
206         case SNDRV_PCM_TRIGGER_RESUME:
207         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
208                 dmaengine_resume(prtd->dma_chan);
209                 break;
210         case SNDRV_PCM_TRIGGER_SUSPEND:
211                 if (runtime->info & SNDRV_PCM_INFO_PAUSE)
212                         dmaengine_pause(prtd->dma_chan);
213                 else
214                         dmaengine_terminate_async(prtd->dma_chan);
215                 break;
216         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
217                 dmaengine_pause(prtd->dma_chan);
218                 break;
219         case SNDRV_PCM_TRIGGER_STOP:
220                 dmaengine_terminate_async(prtd->dma_chan);
221                 break;
222         default:
223                 return -EINVAL;
224         }
225
226         return 0;
227 }
228 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
229
230 /**
231  * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
232  * @substream: PCM substream
233  *
234  * This function is deprecated and should not be used by new drivers, as its
235  * results may be unreliable.
236  */
237 snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
238 {
239         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
240         return bytes_to_frames(substream->runtime, prtd->pos);
241 }
242 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
243
244 /**
245  * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
246  * @substream: PCM substream
247  *
248  * This function can be used as the PCM pointer callback for dmaengine based PCM
249  * driver implementations.
250  */
251 snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
252 {
253         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
254         struct dma_tx_state state;
255         enum dma_status status;
256         unsigned int buf_size;
257         unsigned int pos = 0;
258
259         status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
260         if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
261                 buf_size = snd_pcm_lib_buffer_bytes(substream);
262                 if (state.residue > 0 && state.residue <= buf_size)
263                         pos = buf_size - state.residue;
264         }
265
266         return bytes_to_frames(substream->runtime, pos);
267 }
268 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
269
270 /**
271  * snd_dmaengine_pcm_request_channel - Request channel for the dmaengine PCM
272  * @filter_fn: Filter function used to request the DMA channel
273  * @filter_data: Data passed to the DMA filter function
274  *
275  * Returns NULL or the requested DMA channel.
276  *
277  * This function request a DMA channel for usage with dmaengine PCM.
278  */
279 struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn,
280         void *filter_data)
281 {
282         dma_cap_mask_t mask;
283
284         dma_cap_zero(mask);
285         dma_cap_set(DMA_SLAVE, mask);
286         dma_cap_set(DMA_CYCLIC, mask);
287
288         return dma_request_channel(mask, filter_fn, filter_data);
289 }
290 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_request_channel);
291
292 /**
293  * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
294  * @substream: PCM substream
295  * @chan: DMA channel to use for data transfers
296  *
297  * Returns 0 on success, a negative error code otherwise.
298  *
299  * The function should usually be called from the pcm open callback. Note that
300  * this function will use private_data field of the substream's runtime. So it
301  * is not available to your pcm driver implementation.
302  */
303 int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
304         struct dma_chan *chan)
305 {
306         struct dmaengine_pcm_runtime_data *prtd;
307         int ret;
308
309         if (!chan)
310                 return -ENXIO;
311
312         ret = snd_pcm_hw_constraint_integer(substream->runtime,
313                                             SNDRV_PCM_HW_PARAM_PERIODS);
314         if (ret < 0)
315                 return ret;
316
317         prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
318         if (!prtd)
319                 return -ENOMEM;
320
321         prtd->dma_chan = chan;
322
323         substream->runtime->private_data = prtd;
324
325         return 0;
326 }
327 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
328
329 /**
330  * snd_dmaengine_pcm_open_request_chan - Open a dmaengine based PCM substream and request channel
331  * @substream: PCM substream
332  * @filter_fn: Filter function used to request the DMA channel
333  * @filter_data: Data passed to the DMA filter function
334  *
335  * Returns 0 on success, a negative error code otherwise.
336  *
337  * This function will request a DMA channel using the passed filter function and
338  * data. The function should usually be called from the pcm open callback. Note
339  * that this function will use private_data field of the substream's runtime. So
340  * it is not available to your pcm driver implementation.
341  */
342 int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream *substream,
343         dma_filter_fn filter_fn, void *filter_data)
344 {
345         return snd_dmaengine_pcm_open(substream,
346                     snd_dmaengine_pcm_request_channel(filter_fn, filter_data));
347 }
348 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open_request_chan);
349
350 /**
351  * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
352  * @substream: PCM substream
353  */
354 int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
355 {
356         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
357
358         dmaengine_synchronize(prtd->dma_chan);
359         kfree(prtd);
360
361         return 0;
362 }
363 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
364
365 /**
366  * snd_dmaengine_pcm_release_chan_close - Close a dmaengine based PCM substream and release channel
367  * @substream: PCM substream
368  *
369  * Releases the DMA channel associated with the PCM substream.
370  */
371 int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream)
372 {
373         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
374
375         dmaengine_synchronize(prtd->dma_chan);
376         dma_release_channel(prtd->dma_chan);
377         kfree(prtd);
378
379         return 0;
380 }
381 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close_release_chan);
382
383 MODULE_LICENSE("GPL");