ASoC: codecs: jz4725b: fix capture selector naming
[platform/kernel/linux-rpi.git] / sound / core / pcm_dmaengine.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (C) 2012, Analog Devices Inc.
4  *      Author: Lars-Peter Clausen <lars@metafoo.de>
5  *
6  *  Based on:
7  *      imx-pcm-dma-mx2.c, Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
8  *      mxs-pcm.c, Copyright (C) 2011 Freescale Semiconductor, Inc.
9  *      ep93xx-pcm.c, Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
10  *                    Copyright (C) 2006 Applied Data Systems
11  */
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/dmaengine.h>
15 #include <linux/slab.h>
16 #include <sound/pcm.h>
17 #include <sound/pcm_params.h>
18 #include <sound/soc.h>
19
20 #include <sound/dmaengine_pcm.h>
21
22 struct dmaengine_pcm_runtime_data {
23         struct dma_chan *dma_chan;
24         dma_cookie_t cookie;
25
26         unsigned int pos;
27 };
28
29 static inline struct dmaengine_pcm_runtime_data *substream_to_prtd(
30         const struct snd_pcm_substream *substream)
31 {
32         return substream->runtime->private_data;
33 }
34
35 struct dma_chan *snd_dmaengine_pcm_get_chan(struct snd_pcm_substream *substream)
36 {
37         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
38
39         return prtd->dma_chan;
40 }
41 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_chan);
42
43 /**
44  * snd_hwparams_to_dma_slave_config - Convert hw_params to dma_slave_config
45  * @substream: PCM substream
46  * @params: hw_params
47  * @slave_config: DMA slave config
48  *
49  * This function can be used to initialize a dma_slave_config from a substream
50  * and hw_params in a dmaengine based PCM driver implementation.
51  */
52 int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream *substream,
53         const struct snd_pcm_hw_params *params,
54         struct dma_slave_config *slave_config)
55 {
56         enum dma_slave_buswidth buswidth;
57         int bits;
58
59         bits = params_physical_width(params);
60         if (bits < 8 || bits > 64)
61                 return -EINVAL;
62         else if (bits == 8)
63                 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
64         else if (bits == 16)
65                 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
66         else if (bits == 24)
67                 buswidth = DMA_SLAVE_BUSWIDTH_3_BYTES;
68         else if (bits <= 32)
69                 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
70         else
71                 buswidth = DMA_SLAVE_BUSWIDTH_8_BYTES;
72
73         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
74                 slave_config->direction = DMA_MEM_TO_DEV;
75                 slave_config->dst_addr_width = buswidth;
76         } else {
77                 slave_config->direction = DMA_DEV_TO_MEM;
78                 slave_config->src_addr_width = buswidth;
79         }
80
81         slave_config->device_fc = false;
82
83         return 0;
84 }
85 EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
86
87 /**
88  * snd_dmaengine_pcm_set_config_from_dai_data() - Initializes a dma slave config
89  *  using DAI DMA data.
90  * @substream: PCM substream
91  * @dma_data: DAI DMA data
92  * @slave_config: DMA slave configuration
93  *
94  * Initializes the {dst,src}_addr, {dst,src}_maxburst, {dst,src}_addr_width and
95  * slave_id fields of the DMA slave config from the same fields of the DAI DMA
96  * data struct. The src and dst fields will be initialized depending on the
97  * direction of the substream. If the substream is a playback stream the dst
98  * fields will be initialized, if it is a capture stream the src fields will be
99  * initialized. The {dst,src}_addr_width field will only be initialized if the
100  * SND_DMAENGINE_PCM_DAI_FLAG_PACK flag is set or if the addr_width field of
101  * the DAI DMA data struct is not equal to DMA_SLAVE_BUSWIDTH_UNDEFINED. If
102  * both conditions are met the latter takes priority.
103  */
104 void snd_dmaengine_pcm_set_config_from_dai_data(
105         const struct snd_pcm_substream *substream,
106         const struct snd_dmaengine_dai_dma_data *dma_data,
107         struct dma_slave_config *slave_config)
108 {
109         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
110                 slave_config->dst_addr = dma_data->addr;
111                 slave_config->dst_maxburst = dma_data->maxburst;
112                 if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK)
113                         slave_config->dst_addr_width =
114                                 DMA_SLAVE_BUSWIDTH_UNDEFINED;
115                 if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
116                         slave_config->dst_addr_width = dma_data->addr_width;
117         } else {
118                 slave_config->src_addr = dma_data->addr;
119                 slave_config->src_maxburst = dma_data->maxburst;
120                 if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK)
121                         slave_config->src_addr_width =
122                                 DMA_SLAVE_BUSWIDTH_UNDEFINED;
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         slave_config->peripheral_config = dma_data->peripheral_config;
129         slave_config->peripheral_size = dma_data->peripheral_size;
130 }
131 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data);
132
133 static void dmaengine_pcm_dma_complete(void *arg)
134 {
135         unsigned int new_pos;
136         struct snd_pcm_substream *substream = arg;
137         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
138
139         new_pos = prtd->pos + snd_pcm_lib_period_bytes(substream);
140         if (new_pos >= snd_pcm_lib_buffer_bytes(substream))
141                 new_pos = 0;
142         prtd->pos = new_pos;
143
144         snd_pcm_period_elapsed(substream);
145 }
146
147 static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
148 {
149         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
150         struct dma_chan *chan = prtd->dma_chan;
151         struct dma_async_tx_descriptor *desc;
152         enum dma_transfer_direction direction;
153         unsigned long flags = DMA_CTRL_ACK;
154
155         direction = snd_pcm_substream_to_dma_direction(substream);
156
157         if (!substream->runtime->no_period_wakeup)
158                 flags |= DMA_PREP_INTERRUPT;
159
160         prtd->pos = 0;
161         desc = dmaengine_prep_dma_cyclic(chan,
162                 substream->runtime->dma_addr,
163                 snd_pcm_lib_buffer_bytes(substream),
164                 snd_pcm_lib_period_bytes(substream), direction, flags);
165
166         if (!desc)
167                 return -ENOMEM;
168
169         desc->callback = dmaengine_pcm_dma_complete;
170         desc->callback_param = substream;
171         prtd->cookie = dmaengine_submit(desc);
172
173         return 0;
174 }
175
176 /**
177  * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
178  * @substream: PCM substream
179  * @cmd: Trigger command
180  *
181  * Returns 0 on success, a negative error code otherwise.
182  *
183  * This function can be used as the PCM trigger callback for dmaengine based PCM
184  * driver implementations.
185  */
186 int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
187 {
188         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
189         struct snd_pcm_runtime *runtime = substream->runtime;
190         int ret;
191
192         switch (cmd) {
193         case SNDRV_PCM_TRIGGER_START:
194                 ret = dmaengine_pcm_prepare_and_submit(substream);
195                 if (ret)
196                         return ret;
197                 dma_async_issue_pending(prtd->dma_chan);
198                 break;
199         case SNDRV_PCM_TRIGGER_RESUME:
200         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
201                 dmaengine_resume(prtd->dma_chan);
202                 break;
203         case SNDRV_PCM_TRIGGER_SUSPEND:
204                 if (runtime->info & SNDRV_PCM_INFO_PAUSE)
205                         dmaengine_pause(prtd->dma_chan);
206                 else
207                         dmaengine_terminate_async(prtd->dma_chan);
208                 break;
209         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
210                 dmaengine_pause(prtd->dma_chan);
211                 break;
212         case SNDRV_PCM_TRIGGER_STOP:
213                 dmaengine_terminate_async(prtd->dma_chan);
214                 break;
215         default:
216                 return -EINVAL;
217         }
218
219         return 0;
220 }
221 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
222
223 /**
224  * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
225  * @substream: PCM substream
226  *
227  * This function is deprecated and should not be used by new drivers, as its
228  * results may be unreliable.
229  */
230 snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
231 {
232         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
233         return bytes_to_frames(substream->runtime, prtd->pos);
234 }
235 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
236
237 /**
238  * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
239  * @substream: PCM substream
240  *
241  * This function can be used as the PCM pointer callback for dmaengine based PCM
242  * driver implementations.
243  */
244 snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
245 {
246         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
247         struct snd_pcm_runtime *runtime = substream->runtime;
248         struct dma_tx_state state;
249         enum dma_status status;
250         unsigned int buf_size;
251         unsigned int pos = 0;
252
253         status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
254         if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
255                 buf_size = snd_pcm_lib_buffer_bytes(substream);
256                 if (state.residue > 0 && state.residue <= buf_size)
257                         pos = buf_size - state.residue;
258
259                 runtime->delay = bytes_to_frames(runtime,
260                                                  state.in_flight_bytes);
261         }
262
263         return bytes_to_frames(runtime, pos);
264 }
265 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
266
267 /**
268  * snd_dmaengine_pcm_request_channel - Request channel for the dmaengine PCM
269  * @filter_fn: Filter function used to request the DMA channel
270  * @filter_data: Data passed to the DMA filter function
271  *
272  * Returns NULL or the requested DMA channel.
273  *
274  * This function request a DMA channel for usage with dmaengine PCM.
275  */
276 struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn,
277         void *filter_data)
278 {
279         dma_cap_mask_t mask;
280
281         dma_cap_zero(mask);
282         dma_cap_set(DMA_SLAVE, mask);
283         dma_cap_set(DMA_CYCLIC, mask);
284
285         return dma_request_channel(mask, filter_fn, filter_data);
286 }
287 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_request_channel);
288
289 /**
290  * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
291  * @substream: PCM substream
292  * @chan: DMA channel to use for data transfers
293  *
294  * Returns 0 on success, a negative error code otherwise.
295  *
296  * The function should usually be called from the pcm open callback. Note that
297  * this function will use private_data field of the substream's runtime. So it
298  * is not available to your pcm driver implementation.
299  */
300 int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
301         struct dma_chan *chan)
302 {
303         struct dmaengine_pcm_runtime_data *prtd;
304         int ret;
305
306         if (!chan)
307                 return -ENXIO;
308
309         ret = snd_pcm_hw_constraint_integer(substream->runtime,
310                                             SNDRV_PCM_HW_PARAM_PERIODS);
311         if (ret < 0)
312                 return ret;
313
314         prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
315         if (!prtd)
316                 return -ENOMEM;
317
318         prtd->dma_chan = chan;
319
320         substream->runtime->private_data = prtd;
321
322         return 0;
323 }
324 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
325
326 /**
327  * snd_dmaengine_pcm_open_request_chan - Open a dmaengine based PCM substream and request channel
328  * @substream: PCM substream
329  * @filter_fn: Filter function used to request the DMA channel
330  * @filter_data: Data passed to the DMA filter function
331  *
332  * Returns 0 on success, a negative error code otherwise.
333  *
334  * This function will request a DMA channel using the passed filter function and
335  * data. The function should usually be called from the pcm open callback. Note
336  * that this function will use private_data field of the substream's runtime. So
337  * it is not available to your pcm driver implementation.
338  */
339 int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream *substream,
340         dma_filter_fn filter_fn, void *filter_data)
341 {
342         return snd_dmaengine_pcm_open(substream,
343                     snd_dmaengine_pcm_request_channel(filter_fn, filter_data));
344 }
345 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open_request_chan);
346
347 /**
348  * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
349  * @substream: PCM substream
350  */
351 int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
352 {
353         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
354
355         dmaengine_synchronize(prtd->dma_chan);
356         kfree(prtd);
357
358         return 0;
359 }
360 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
361
362 /**
363  * snd_dmaengine_pcm_close_release_chan - Close a dmaengine based PCM
364  *                                        substream and release channel
365  * @substream: PCM substream
366  *
367  * Releases the DMA channel associated with the PCM substream.
368  */
369 int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream)
370 {
371         struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
372
373         dmaengine_synchronize(prtd->dma_chan);
374         dma_release_channel(prtd->dma_chan);
375         kfree(prtd);
376
377         return 0;
378 }
379 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close_release_chan);
380
381 /**
382  * snd_dmaengine_pcm_refine_runtime_hwparams - Refine runtime hw params
383  * @substream: PCM substream
384  * @dma_data: DAI DMA data
385  * @hw: PCM hw params
386  * @chan: DMA channel to use for data transfers
387  *
388  * Returns 0 on success, a negative error code otherwise.
389  *
390  * This function will query DMA capability, then refine the pcm hardware
391  * parameters.
392  */
393 int snd_dmaengine_pcm_refine_runtime_hwparams(
394         struct snd_pcm_substream *substream,
395         struct snd_dmaengine_dai_dma_data *dma_data,
396         struct snd_pcm_hardware *hw,
397         struct dma_chan *chan)
398 {
399         struct dma_slave_caps dma_caps;
400         u32 addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
401                           BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
402                           BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
403         snd_pcm_format_t i;
404         int ret = 0;
405
406         if (!hw || !chan || !dma_data)
407                 return -EINVAL;
408
409         ret = dma_get_slave_caps(chan, &dma_caps);
410         if (ret == 0) {
411                 if (dma_caps.cmd_pause && dma_caps.cmd_resume)
412                         hw->info |= SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME;
413                 if (dma_caps.residue_granularity <= DMA_RESIDUE_GRANULARITY_SEGMENT)
414                         hw->info |= SNDRV_PCM_INFO_BATCH;
415
416                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
417                         addr_widths = dma_caps.dst_addr_widths;
418                 else
419                         addr_widths = dma_caps.src_addr_widths;
420         }
421
422         /*
423          * If SND_DMAENGINE_PCM_DAI_FLAG_PACK is set keep
424          * hw.formats set to 0, meaning no restrictions are in place.
425          * In this case it's the responsibility of the DAI driver to
426          * provide the supported format information.
427          */
428         if (!(dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK))
429                 /*
430                  * Prepare formats mask for valid/allowed sample types. If the
431                  * dma does not have support for the given physical word size,
432                  * it needs to be masked out so user space can not use the
433                  * format which produces corrupted audio.
434                  * In case the dma driver does not implement the slave_caps the
435                  * default assumption is that it supports 1, 2 and 4 bytes
436                  * widths.
437                  */
438                 pcm_for_each_format(i) {
439                         int bits = snd_pcm_format_physical_width(i);
440
441                         /*
442                          * Enable only samples with DMA supported physical
443                          * widths
444                          */
445                         switch (bits) {
446                         case 8:
447                         case 16:
448                         case 24:
449                         case 32:
450                         case 64:
451                                 if (addr_widths & (1 << (bits / 8)))
452                                         hw->formats |= pcm_format_to_bits(i);
453                                 break;
454                         default:
455                                 /* Unsupported types */
456                                 break;
457                         }
458                 }
459
460         return ret;
461 }
462 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_refine_runtime_hwparams);
463
464 MODULE_LICENSE("GPL");