7c24dedff971f2e20604fa100ef82a2e50003e03
[linux-block.git] / sound / soc / soc-dmaengine-pcm.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
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
92         slave_config->device_fc = false;
93
94         return 0;
95 }
96 EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
97
98 static 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
110 static 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;
116         unsigned long flags = DMA_CTRL_ACK;
117
118         direction = snd_pcm_substream_to_dma_direction(substream);
119
120         if (!substream->runtime->no_period_wakeup)
121                 flags |= DMA_PREP_INTERRUPT;
122
123         prtd->pos = 0;
124         desc = dmaengine_prep_dma_cyclic(chan,
125                 substream->runtime->dma_addr,
126                 snd_pcm_lib_buffer_bytes(substream),
127                 snd_pcm_lib_period_bytes(substream), direction, flags);
128
129         if (!desc)
130                 return -ENOMEM;
131
132         desc->callback = dmaengine_pcm_dma_complete;
133         desc->callback_param = substream;
134         prtd->cookie = dmaengine_submit(desc);
135
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  */
149 int 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 }
178 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
179
180 /**
181  * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
182  * @substream: PCM substream
183  *
184  * This function is deprecated and should not be used by new drivers, as its
185  * results may be unreliable.
186  */
187 snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
188 {
189         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
190         return bytes_to_frames(substream->runtime, prtd->pos);
191 }
192 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
193
194 /**
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  */
201 snd_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 }
218 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
219
220 static 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
250  * snd_dmaengine_pcm_{set,get}_data.
251  */
252 int 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 }
277 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
278
279 /**
280  * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
281  * @substream: PCM substream
282  */
283 int 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 }
292 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
293
294 MODULE_LICENSE("GPL");