rk: restore file mode
[firefly-linux-kernel-4.4.55.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 /**
99  * snd_dmaengine_pcm_set_config_from_dai_data() - Initializes a dma slave config
100  *  using DAI DMA data.
101  * @substream: PCM substream
102  * @dma_data: DAI DMA data
103  * @slave_config: DMA slave configuration
104  *
105  * Initializes the {dst,src}_addr, {dst,src}_maxburst, {dst,src}_addr_width and
106  * slave_id fields of the DMA slave config from the same fields of the DAI DMA
107  * data struct. The src and dst fields will be initialized depending on the
108  * direction of the substream. If the substream is a playback stream the dst
109  * fields will be initialized, if it is a capture stream the src fields will be
110  * initialized. The {dst,src}_addr_width field will only be initialized if the
111  * addr_width field of the DAI DMA data struct is not equal to
112  * DMA_SLAVE_BUSWIDTH_UNDEFINED.
113  */
114 void snd_dmaengine_pcm_set_config_from_dai_data(
115         const struct snd_pcm_substream *substream,
116         const struct snd_dmaengine_dai_dma_data *dma_data,
117         struct dma_slave_config *slave_config)
118 {
119         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
120                 slave_config->dst_addr = dma_data->addr;
121                 slave_config->dst_maxburst = dma_data->maxburst;
122                 if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
123                         slave_config->dst_addr_width = dma_data->addr_width;
124         } else {
125                 slave_config->src_addr = dma_data->addr;
126                 slave_config->src_maxburst = dma_data->maxburst;
127                 if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
128                         slave_config->src_addr_width = dma_data->addr_width;
129         }
130
131         slave_config->slave_id = dma_data->slave_id;
132 }
133 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data);
134
135 #ifdef CONFIG_ARCH_ROCKCHIP
136 static int debug_audio_timeout = 0;
137 module_param(debug_audio_timeout, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
138 MODULE_PARM_DESC(debug_audio_timeout, "Debug interface Audio DMA buffdone time out");
139 #endif
140 static void dmaengine_pcm_dma_complete(void *arg)
141 {
142         struct snd_pcm_substream *substream = arg;
143         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
144
145 #ifdef CONFIG_ARCH_ROCKCHIP
146         if(debug_audio_timeout){
147                 struct snd_pcm_runtime *runtime = substream->runtime;
148                 static ktime_t before = {0},after = {0};
149                 s64 t;
150                 before = after;
151                 after = ktime_get();
152                 t = ktime_to_us(ktime_sub(after, before));
153
154                 if(t > (snd_pcm_lib_period_bytes(substream)/4+32)*1000*1000/runtime->rate
155                         && t != ktime_to_us(after)) // (23220)4096/4/44100 + 32/44100
156                 {
157                                 printk(KERN_DEBUG "Time out:: Audio DMA buffdone time out!!! the time = %lld!\n", t);
158                 }
159                 //printk(KERN_DEBUG "audio DMA callback time = %lld\n", t);
160         }
161 #endif
162         prtd->pos += snd_pcm_lib_period_bytes(substream);
163         if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
164                 prtd->pos = 0;
165
166         snd_pcm_period_elapsed(substream);
167 }
168
169 static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
170 {
171         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
172         struct dma_chan *chan = prtd->dma_chan;
173         struct dma_async_tx_descriptor *desc;
174         enum dma_transfer_direction direction;
175         unsigned long flags = DMA_CTRL_ACK;
176
177         direction = snd_pcm_substream_to_dma_direction(substream);
178
179         if (!substream->runtime->no_period_wakeup)
180                 flags |= DMA_PREP_INTERRUPT;
181
182         prtd->pos = 0;
183 #ifdef CONFIG_ARCH_ROCKCHIP
184         //printk("soc dma buffersize = %d , periodsize=%d, periods=%d\n",
185         //      snd_pcm_lib_buffer_bytes(substream),
186         //      snd_pcm_lib_period_bytes(substream),
187         //      snd_pcm_lib_buffer_bytes(substream)/snd_pcm_lib_period_bytes(substream));
188         desc = dmaengine_prep_dma_infiniteloop(chan,
189                 substream->runtime->dma_addr,
190                 snd_pcm_lib_buffer_bytes(substream),
191                 snd_pcm_lib_period_bytes(substream),
192                 direction, flags,
193                 snd_pcm_lib_buffer_bytes(substream)/snd_pcm_lib_period_bytes(substream));
194 #else
195         desc = dmaengine_prep_dma_cyclic(chan,
196                 substream->runtime->dma_addr,
197                 snd_pcm_lib_buffer_bytes(substream),
198                 snd_pcm_lib_period_bytes(substream), direction, flags);
199 #endif
200
201         if (!desc)
202                 return -ENOMEM;
203
204         desc->callback = dmaengine_pcm_dma_complete;
205         desc->callback_param = substream;
206         prtd->cookie = dmaengine_submit(desc);
207
208         return 0;
209 }
210
211 /**
212  * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
213  * @substream: PCM substream
214  * @cmd: Trigger command
215  *
216  * Returns 0 on success, a negative error code otherwise.
217  *
218  * This function can be used as the PCM trigger callback for dmaengine based PCM
219  * driver implementations.
220  */
221 int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
222 {
223         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
224         int ret;
225
226         switch (cmd) {
227         case SNDRV_PCM_TRIGGER_START:
228                 ret = dmaengine_pcm_prepare_and_submit(substream);
229                 if (ret)
230                         return ret;
231                 dma_async_issue_pending(prtd->dma_chan);
232                 break;
233         case SNDRV_PCM_TRIGGER_RESUME:
234         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
235                 dmaengine_resume(prtd->dma_chan);
236                 break;
237         case SNDRV_PCM_TRIGGER_SUSPEND:
238         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
239                 dmaengine_pause(prtd->dma_chan);
240                 break;
241         case SNDRV_PCM_TRIGGER_STOP:
242                 dmaengine_terminate_all(prtd->dma_chan);
243                 break;
244         default:
245                 return -EINVAL;
246         }
247
248         return 0;
249 }
250 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
251
252 /**
253  * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
254  * @substream: PCM substream
255  *
256  * This function is deprecated and should not be used by new drivers, as its
257  * results may be unreliable.
258  */
259 snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
260 {
261         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
262 #ifdef CONFIG_ARCH_ROCKCHIP
263         dma_addr_t src, dst;
264
265         prtd->dma_chan->device->dma_getposition(prtd->dma_chan, &src, &dst);
266         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
267                 prtd->pos = dst - substream->runtime->dma_addr;
268         else
269                 prtd->pos = src - substream->runtime->dma_addr;
270 #endif
271         return bytes_to_frames(substream->runtime, prtd->pos);
272 }
273 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
274
275 /**
276  * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
277  * @substream: PCM substream
278  *
279  * This function can be used as the PCM pointer callback for dmaengine based PCM
280  * driver implementations.
281  */
282 snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
283 {
284         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
285         struct dma_tx_state state;
286         enum dma_status status;
287         unsigned int buf_size;
288         unsigned int pos = 0;
289
290         status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
291         if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
292                 buf_size = snd_pcm_lib_buffer_bytes(substream);
293                 if (state.residue > 0 && state.residue <= buf_size)
294                         pos = buf_size - state.residue;
295         }
296
297         return bytes_to_frames(substream->runtime, pos);
298 }
299 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
300
301 /**
302  * snd_dmaengine_pcm_request_channel - Request channel for the dmaengine PCM
303  * @filter_fn: Filter function used to request the DMA channel
304  * @filter_data: Data passed to the DMA filter function
305  *
306  * Returns NULL or the requested DMA channel.
307  *
308  * This function request a DMA channel for usage with dmaengine PCM.
309  */
310 struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn,
311         void *filter_data)
312 {
313         dma_cap_mask_t mask;
314
315         dma_cap_zero(mask);
316         dma_cap_set(DMA_SLAVE, mask);
317         dma_cap_set(DMA_CYCLIC, mask);
318
319         return dma_request_channel(mask, filter_fn, filter_data);
320 }
321 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_request_channel);
322
323 /**
324  * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
325  * @substream: PCM substream
326  * @chan: DMA channel to use for data transfers
327  *
328  * Returns 0 on success, a negative error code otherwise.
329  *
330  * The function should usually be called from the pcm open callback. Note that
331  * this function will use private_data field of the substream's runtime. So it
332  * is not availabe to your pcm driver implementation.
333  */
334 int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
335         struct dma_chan *chan)
336 {
337         struct dmaengine_pcm_runtime_data *prtd;
338         int ret;
339
340         if (!chan)
341                 return -ENXIO;
342
343         ret = snd_pcm_hw_constraint_integer(substream->runtime,
344                                             SNDRV_PCM_HW_PARAM_PERIODS);
345         if (ret < 0)
346                 return ret;
347
348         prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
349         if (!prtd)
350                 return -ENOMEM;
351
352         prtd->dma_chan = chan;
353
354         substream->runtime->private_data = prtd;
355
356         return 0;
357 }
358 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
359
360 /**
361  * snd_dmaengine_pcm_open_request_chan - Open a dmaengine based PCM substream and request channel
362  * @substream: PCM substream
363  * @filter_fn: Filter function used to request the DMA channel
364  * @filter_data: Data passed to the DMA filter function
365  *
366  * Returns 0 on success, a negative error code otherwise.
367  *
368  * This function will request a DMA channel using the passed filter function and
369  * data. The function should usually be called from the pcm open callback. Note
370  * that this function will use private_data field of the substream's runtime. So
371  * it is not availabe to your pcm driver implementation.
372  */
373 int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream *substream,
374         dma_filter_fn filter_fn, void *filter_data)
375 {
376         return snd_dmaengine_pcm_open(substream,
377                     snd_dmaengine_pcm_request_channel(filter_fn, filter_data));
378 }
379 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open_request_chan);
380
381 /**
382  * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
383  * @substream: PCM substream
384  */
385 int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
386 {
387         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
388
389         kfree(prtd);
390
391         return 0;
392 }
393 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
394
395 /**
396  * snd_dmaengine_pcm_release_chan_close - Close a dmaengine based PCM substream and release channel
397  * @substream: PCM substream
398  *
399  * Releases the DMA channel associated with the PCM substream.
400  */
401 int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream)
402 {
403         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
404
405         dma_release_channel(prtd->dma_chan);
406
407         return snd_dmaengine_pcm_close(substream);
408 }
409 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close_release_chan);
410
411 MODULE_LICENSE("GPL");