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