Commit | Line | Data |
---|---|---|
a912e80b | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
e7f73a16 LPC |
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 | |
e7f73a16 LPC |
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; | |
3528f27a | 24 | dma_cookie_t cookie; |
e7f73a16 LPC |
25 | |
26 | unsigned int pos; | |
e7f73a16 LPC |
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 | ||
e7f73a16 LPC |
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; | |
a655f75c | 57 | int bits; |
e7f73a16 | 58 | |
732814c8 | 59 | bits = params_physical_width(params); |
a655f75c TI |
60 | if (bits < 8 || bits > 64) |
61 | return -EINVAL; | |
62 | else if (bits == 8) | |
e7f73a16 | 63 | buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE; |
a655f75c | 64 | else if (bits == 16) |
e7f73a16 | 65 | buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES; |
75f850fc PU |
66 | else if (bits == 24) |
67 | buswidth = DMA_SLAVE_BUSWIDTH_3_BYTES; | |
a655f75c | 68 | else if (bits <= 32) |
e7f73a16 | 69 | buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES; |
a655f75c TI |
70 | else |
71 | buswidth = DMA_SLAVE_BUSWIDTH_8_BYTES; | |
e7f73a16 LPC |
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 | ||
5fa70f71 LPC |
81 | slave_config->device_fc = false; |
82 | ||
e7f73a16 LPC |
83 | return 0; |
84 | } | |
85 | EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config); | |
86 | ||
85c9f9c5 LPC |
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 | |
73fe01cf MR |
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. | |
85c9f9c5 LPC |
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; | |
73fe01cf MR |
112 | if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK) |
113 | slave_config->dst_addr_width = | |
114 | DMA_SLAVE_BUSWIDTH_UNDEFINED; | |
85c9f9c5 LPC |
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; | |
73fe01cf MR |
120 | if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK) |
121 | slave_config->src_addr_width = | |
122 | DMA_SLAVE_BUSWIDTH_UNDEFINED; | |
85c9f9c5 LPC |
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 | } | |
129 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data); | |
130 | ||
e7f73a16 LPC |
131 | static void dmaengine_pcm_dma_complete(void *arg) |
132 | { | |
133 | struct snd_pcm_substream *substream = arg; | |
134 | struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream); | |
135 | ||
136 | prtd->pos += snd_pcm_lib_period_bytes(substream); | |
137 | if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream)) | |
138 | prtd->pos = 0; | |
139 | ||
140 | snd_pcm_period_elapsed(substream); | |
141 | } | |
142 | ||
143 | static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream) | |
144 | { | |
145 | struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream); | |
146 | struct dma_chan *chan = prtd->dma_chan; | |
147 | struct dma_async_tx_descriptor *desc; | |
148 | enum dma_transfer_direction direction; | |
e7736cde | 149 | unsigned long flags = DMA_CTRL_ACK; |
e7f73a16 LPC |
150 | |
151 | direction = snd_pcm_substream_to_dma_direction(substream); | |
152 | ||
e7736cde PU |
153 | if (!substream->runtime->no_period_wakeup) |
154 | flags |= DMA_PREP_INTERRUPT; | |
155 | ||
7a08cf70 | 156 | prtd->pos = 0; |
41ba6b71 | 157 | desc = dmaengine_prep_dma_cyclic(chan, |
e7f73a16 LPC |
158 | substream->runtime->dma_addr, |
159 | snd_pcm_lib_buffer_bytes(substream), | |
e7736cde | 160 | snd_pcm_lib_period_bytes(substream), direction, flags); |
e7f73a16 LPC |
161 | |
162 | if (!desc) | |
163 | return -ENOMEM; | |
164 | ||
165 | desc->callback = dmaengine_pcm_dma_complete; | |
166 | desc->callback_param = substream; | |
3528f27a | 167 | prtd->cookie = dmaengine_submit(desc); |
e7f73a16 LPC |
168 | |
169 | return 0; | |
170 | } | |
171 | ||
172 | /** | |
173 | * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation | |
174 | * @substream: PCM substream | |
175 | * @cmd: Trigger command | |
176 | * | |
177 | * Returns 0 on success, a negative error code otherwise. | |
178 | * | |
179 | * This function can be used as the PCM trigger callback for dmaengine based PCM | |
180 | * driver implementations. | |
181 | */ | |
182 | int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd) | |
183 | { | |
184 | struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream); | |
02fb05a5 | 185 | struct snd_pcm_runtime *runtime = substream->runtime; |
e7f73a16 LPC |
186 | int ret; |
187 | ||
188 | switch (cmd) { | |
189 | case SNDRV_PCM_TRIGGER_START: | |
190 | ret = dmaengine_pcm_prepare_and_submit(substream); | |
191 | if (ret) | |
192 | return ret; | |
193 | dma_async_issue_pending(prtd->dma_chan); | |
194 | break; | |
195 | case SNDRV_PCM_TRIGGER_RESUME: | |
196 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
197 | dmaengine_resume(prtd->dma_chan); | |
198 | break; | |
199 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
02fb05a5 TB |
200 | if (runtime->info & SNDRV_PCM_INFO_PAUSE) |
201 | dmaengine_pause(prtd->dma_chan); | |
202 | else | |
bc0e7345 | 203 | dmaengine_terminate_async(prtd->dma_chan); |
02fb05a5 | 204 | break; |
e7f73a16 LPC |
205 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
206 | dmaengine_pause(prtd->dma_chan); | |
207 | break; | |
208 | case SNDRV_PCM_TRIGGER_STOP: | |
bc0e7345 | 209 | dmaengine_terminate_async(prtd->dma_chan); |
e7f73a16 LPC |
210 | break; |
211 | default: | |
212 | return -EINVAL; | |
213 | } | |
214 | ||
215 | return 0; | |
216 | } | |
217 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger); | |
218 | ||
219 | /** | |
9883ab22 | 220 | * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation |
e7f73a16 LPC |
221 | * @substream: PCM substream |
222 | * | |
9883ab22 LPC |
223 | * This function is deprecated and should not be used by new drivers, as its |
224 | * results may be unreliable. | |
e7f73a16 | 225 | */ |
9883ab22 | 226 | snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream) |
e7f73a16 LPC |
227 | { |
228 | struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream); | |
229 | return bytes_to_frames(substream->runtime, prtd->pos); | |
230 | } | |
9883ab22 | 231 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue); |
e7f73a16 | 232 | |
3528f27a LPC |
233 | /** |
234 | * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation | |
235 | * @substream: PCM substream | |
236 | * | |
237 | * This function can be used as the PCM pointer callback for dmaengine based PCM | |
238 | * driver implementations. | |
239 | */ | |
240 | snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream) | |
241 | { | |
242 | struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream); | |
9d789dc0 | 243 | struct snd_pcm_runtime *runtime = substream->runtime; |
3528f27a LPC |
244 | struct dma_tx_state state; |
245 | enum dma_status status; | |
246 | unsigned int buf_size; | |
247 | unsigned int pos = 0; | |
248 | ||
249 | status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state); | |
250 | if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) { | |
251 | buf_size = snd_pcm_lib_buffer_bytes(substream); | |
252 | if (state.residue > 0 && state.residue <= buf_size) | |
253 | pos = buf_size - state.residue; | |
fa1f875c PU |
254 | |
255 | runtime->delay = bytes_to_frames(runtime, | |
256 | state.in_flight_bytes); | |
3528f27a LPC |
257 | } |
258 | ||
9d789dc0 | 259 | return bytes_to_frames(runtime, pos); |
3528f27a LPC |
260 | } |
261 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer); | |
262 | ||
c999836d LPC |
263 | /** |
264 | * snd_dmaengine_pcm_request_channel - Request channel for the dmaengine PCM | |
265 | * @filter_fn: Filter function used to request the DMA channel | |
266 | * @filter_data: Data passed to the DMA filter function | |
267 | * | |
268 | * Returns NULL or the requested DMA channel. | |
269 | * | |
270 | * This function request a DMA channel for usage with dmaengine PCM. | |
271 | */ | |
272 | struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn, | |
7c1c1d4a | 273 | void *filter_data) |
e7f73a16 LPC |
274 | { |
275 | dma_cap_mask_t mask; | |
276 | ||
277 | dma_cap_zero(mask); | |
278 | dma_cap_set(DMA_SLAVE, mask); | |
279 | dma_cap_set(DMA_CYCLIC, mask); | |
e7f73a16 | 280 | |
7c1c1d4a | 281 | return dma_request_channel(mask, filter_fn, filter_data); |
e7f73a16 | 282 | } |
c999836d | 283 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_request_channel); |
e7f73a16 LPC |
284 | |
285 | /** | |
286 | * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream | |
287 | * @substream: PCM substream | |
7c1c1d4a | 288 | * @chan: DMA channel to use for data transfers |
e7f73a16 LPC |
289 | * |
290 | * Returns 0 on success, a negative error code otherwise. | |
291 | * | |
7c1c1d4a LPC |
292 | * The function should usually be called from the pcm open callback. Note that |
293 | * this function will use private_data field of the substream's runtime. So it | |
1a6ab46f | 294 | * is not available to your pcm driver implementation. |
e7f73a16 LPC |
295 | */ |
296 | int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream, | |
7c1c1d4a | 297 | struct dma_chan *chan) |
e7f73a16 LPC |
298 | { |
299 | struct dmaengine_pcm_runtime_data *prtd; | |
300 | int ret; | |
301 | ||
7c1c1d4a LPC |
302 | if (!chan) |
303 | return -ENXIO; | |
304 | ||
e7f73a16 LPC |
305 | ret = snd_pcm_hw_constraint_integer(substream->runtime, |
306 | SNDRV_PCM_HW_PARAM_PERIODS); | |
307 | if (ret < 0) | |
308 | return ret; | |
309 | ||
310 | prtd = kzalloc(sizeof(*prtd), GFP_KERNEL); | |
311 | if (!prtd) | |
312 | return -ENOMEM; | |
313 | ||
7c1c1d4a | 314 | prtd->dma_chan = chan; |
e7f73a16 LPC |
315 | |
316 | substream->runtime->private_data = prtd; | |
317 | ||
318 | return 0; | |
319 | } | |
320 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open); | |
321 | ||
7c1c1d4a LPC |
322 | /** |
323 | * snd_dmaengine_pcm_open_request_chan - Open a dmaengine based PCM substream and request channel | |
324 | * @substream: PCM substream | |
325 | * @filter_fn: Filter function used to request the DMA channel | |
326 | * @filter_data: Data passed to the DMA filter function | |
327 | * | |
328 | * Returns 0 on success, a negative error code otherwise. | |
329 | * | |
330 | * This function will request a DMA channel using the passed filter function and | |
331 | * data. The function should usually be called from the pcm open callback. Note | |
332 | * that this function will use private_data field of the substream's runtime. So | |
1a6ab46f | 333 | * it is not available to your pcm driver implementation. |
7c1c1d4a LPC |
334 | */ |
335 | int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream *substream, | |
336 | dma_filter_fn filter_fn, void *filter_data) | |
337 | { | |
338 | return snd_dmaengine_pcm_open(substream, | |
c999836d | 339 | snd_dmaengine_pcm_request_channel(filter_fn, filter_data)); |
7c1c1d4a LPC |
340 | } |
341 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open_request_chan); | |
342 | ||
e7f73a16 LPC |
343 | /** |
344 | * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream | |
345 | * @substream: PCM substream | |
346 | */ | |
347 | int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream) | |
348 | { | |
349 | struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream); | |
350 | ||
bc0e7345 | 351 | dmaengine_synchronize(prtd->dma_chan); |
e7f73a16 LPC |
352 | kfree(prtd); |
353 | ||
354 | return 0; | |
355 | } | |
356 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close); | |
cba69b4d | 357 | |
7c1c1d4a LPC |
358 | /** |
359 | * snd_dmaengine_pcm_release_chan_close - Close a dmaengine based PCM substream and release channel | |
360 | * @substream: PCM substream | |
361 | * | |
362 | * Releases the DMA channel associated with the PCM substream. | |
363 | */ | |
364 | int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream) | |
365 | { | |
366 | struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream); | |
367 | ||
bc0e7345 | 368 | dmaengine_synchronize(prtd->dma_chan); |
7c1c1d4a | 369 | dma_release_channel(prtd->dma_chan); |
bc0e7345 | 370 | kfree(prtd); |
7c1c1d4a | 371 | |
bc0e7345 | 372 | return 0; |
7c1c1d4a LPC |
373 | } |
374 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close_release_chan); | |
375 | ||
e957204e SW |
376 | /** |
377 | * snd_dmaengine_pcm_refine_runtime_hwparams - Refine runtime hw params | |
378 | * @substream: PCM substream | |
379 | * @dma_data: DAI DMA data | |
380 | * @hw: PCM hw params | |
381 | * @chan: DMA channel to use for data transfers | |
382 | * | |
383 | * Returns 0 on success, a negative error code otherwise. | |
384 | * | |
385 | * This function will query DMA capability, then refine the pcm hardware | |
386 | * parameters. | |
387 | */ | |
388 | int snd_dmaengine_pcm_refine_runtime_hwparams( | |
389 | struct snd_pcm_substream *substream, | |
390 | struct snd_dmaengine_dai_dma_data *dma_data, | |
391 | struct snd_pcm_hardware *hw, | |
392 | struct dma_chan *chan) | |
393 | { | |
394 | struct dma_slave_caps dma_caps; | |
395 | u32 addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | | |
396 | BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | | |
397 | BIT(DMA_SLAVE_BUSWIDTH_4_BYTES); | |
398 | snd_pcm_format_t i; | |
399 | int ret = 0; | |
400 | ||
401 | if (!hw || !chan || !dma_data) | |
402 | return -EINVAL; | |
403 | ||
404 | ret = dma_get_slave_caps(chan, &dma_caps); | |
405 | if (ret == 0) { | |
406 | if (dma_caps.cmd_pause && dma_caps.cmd_resume) | |
407 | hw->info |= SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME; | |
408 | if (dma_caps.residue_granularity <= DMA_RESIDUE_GRANULARITY_SEGMENT) | |
409 | hw->info |= SNDRV_PCM_INFO_BATCH; | |
410 | ||
411 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
412 | addr_widths = dma_caps.dst_addr_widths; | |
413 | else | |
414 | addr_widths = dma_caps.src_addr_widths; | |
415 | } | |
416 | ||
417 | /* | |
418 | * If SND_DMAENGINE_PCM_DAI_FLAG_PACK is set keep | |
419 | * hw.formats set to 0, meaning no restrictions are in place. | |
420 | * In this case it's the responsibility of the DAI driver to | |
421 | * provide the supported format information. | |
422 | */ | |
423 | if (!(dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK)) | |
424 | /* | |
425 | * Prepare formats mask for valid/allowed sample types. If the | |
426 | * dma does not have support for the given physical word size, | |
427 | * it needs to be masked out so user space can not use the | |
428 | * format which produces corrupted audio. | |
429 | * In case the dma driver does not implement the slave_caps the | |
430 | * default assumption is that it supports 1, 2 and 4 bytes | |
431 | * widths. | |
432 | */ | |
89e0b9a0 | 433 | pcm_for_each_format(i) { |
e957204e SW |
434 | int bits = snd_pcm_format_physical_width(i); |
435 | ||
436 | /* | |
437 | * Enable only samples with DMA supported physical | |
438 | * widths | |
439 | */ | |
440 | switch (bits) { | |
441 | case 8: | |
442 | case 16: | |
443 | case 24: | |
444 | case 32: | |
445 | case 64: | |
446 | if (addr_widths & (1 << (bits / 8))) | |
447 | hw->formats |= pcm_format_to_bits(i); | |
448 | break; | |
449 | default: | |
450 | /* Unsupported types */ | |
451 | break; | |
452 | } | |
453 | } | |
454 | ||
455 | return ret; | |
456 | } | |
457 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_refine_runtime_hwparams); | |
458 | ||
cba69b4d | 459 | MODULE_LICENSE("GPL"); |