ASoC: cros_ec_codec: Convert to the common vmalloc memalloc
[linux-2.6-block.git] / sound / soc / soc-generic-dmaengine-pcm.c
CommitLineData
1356a607
KM
1// SPDX-License-Identifier: GPL-2.0+
2//
3// Copyright (C) 2013, Analog Devices Inc.
4// Author: Lars-Peter Clausen <lars@metafoo.de>
5
28c4468b
LPC
6#include <linux/module.h>
7#include <linux/init.h>
8#include <linux/dmaengine.h>
9#include <linux/slab.h>
10#include <sound/pcm.h>
11#include <sound/pcm_params.h>
12#include <sound/soc.h>
13#include <linux/dma-mapping.h>
14#include <linux/of.h>
28c4468b
LPC
15
16#include <sound/dmaengine_pcm.h>
17
acde50a7
LPC
18/*
19 * The platforms dmaengine driver does not support reporting the amount of
20 * bytes that are still left to transfer.
21 */
22#define SND_DMAENGINE_PCM_FLAG_NO_RESIDUE BIT(31)
23
28c4468b 24struct dmaengine_pcm {
f82bf8e2 25 struct dma_chan *chan[SNDRV_PCM_STREAM_LAST + 1];
28c4468b 26 const struct snd_dmaengine_pcm_config *config;
be7ee5f3 27 struct snd_soc_component component;
d1e1406c 28 unsigned int flags;
28c4468b
LPC
29};
30
be7ee5f3 31static struct dmaengine_pcm *soc_component_to_pcm(struct snd_soc_component *p)
28c4468b 32{
be7ee5f3 33 return container_of(p, struct dmaengine_pcm, component);
28c4468b
LPC
34}
35
c0de42bf
LPC
36static struct device *dmaengine_dma_dev(struct dmaengine_pcm *pcm,
37 struct snd_pcm_substream *substream)
38{
39 if (!pcm->chan[substream->stream])
40 return NULL;
41
42 return pcm->chan[substream->stream]->device->dev;
43}
44
28c4468b
LPC
45/**
46 * snd_dmaengine_pcm_prepare_slave_config() - Generic prepare_slave_config callback
47 * @substream: PCM substream
48 * @params: hw_params
49 * @slave_config: DMA slave config to prepare
50 *
51 * This function can be used as a generic prepare_slave_config callback for
52 * platforms which make use of the snd_dmaengine_dai_dma_data struct for their
53 * DAI DMA data. Internally the function will first call
54 * snd_hwparams_to_dma_slave_config to fill in the slave config based on the
55 * hw_params, followed by snd_dmaengine_set_config_from_dai_data to fill in the
56 * remaining fields based on the DAI DMA data.
57 */
58int snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream *substream,
59 struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config)
60{
61 struct snd_soc_pcm_runtime *rtd = substream->private_data;
62 struct snd_dmaengine_dai_dma_data *dma_data;
63 int ret;
64
65 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
66
67 ret = snd_hwparams_to_dma_slave_config(substream, params, slave_config);
68 if (ret)
69 return ret;
70
71 snd_dmaengine_pcm_set_config_from_dai_data(substream, dma_data,
72 slave_config);
73
74 return 0;
75}
76EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_prepare_slave_config);
77
ece23171
KM
78static int dmaengine_pcm_hw_params(struct snd_soc_component *component,
79 struct snd_pcm_substream *substream,
80 struct snd_pcm_hw_params *params)
28c4468b 81{
be7ee5f3 82 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
28c4468b 83 struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
fa654e08
LPC
84 int (*prepare_slave_config)(struct snd_pcm_substream *substream,
85 struct snd_pcm_hw_params *params,
86 struct dma_slave_config *slave_config);
28c4468b
LPC
87 struct dma_slave_config slave_config;
88 int ret;
89
a894bd7f
LJ
90 memset(&slave_config, 0, sizeof(slave_config));
91
fa654e08
LPC
92 if (!pcm->config)
93 prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config;
94 else
95 prepare_slave_config = pcm->config->prepare_slave_config;
96
97 if (prepare_slave_config) {
98 ret = prepare_slave_config(substream, params, &slave_config);
28c4468b
LPC
99 if (ret)
100 return ret;
101
102 ret = dmaengine_slave_config(chan, &slave_config);
103 if (ret)
104 return ret;
105 }
106
107 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
108}
109
ece23171
KM
110static int
111dmaengine_pcm_set_runtime_hwparams(struct snd_soc_component *component,
112 struct snd_pcm_substream *substream)
28c4468b
LPC
113{
114 struct snd_soc_pcm_runtime *rtd = substream->private_data;
be7ee5f3 115 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
c0de42bf 116 struct device *dma_dev = dmaengine_dma_dev(pcm, substream);
28c4468b 117 struct dma_chan *chan = pcm->chan[substream->stream];
c0de42bf 118 struct snd_dmaengine_dai_dma_data *dma_data;
c0de42bf 119 struct snd_pcm_hardware hw;
7ed310bd 120 int ret;
28c4468b 121
fa654e08 122 if (pcm->config && pcm->config->pcm_hardware)
c0de42bf 123 return snd_soc_set_runtime_hwparams(substream,
28c4468b 124 pcm->config->pcm_hardware);
28c4468b 125
c0de42bf
LPC
126 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
127
128 memset(&hw, 0, sizeof(hw));
129 hw.info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
130 SNDRV_PCM_INFO_INTERLEAVED;
131 hw.periods_min = 2;
132 hw.periods_max = UINT_MAX;
133 hw.period_bytes_min = 256;
134 hw.period_bytes_max = dma_get_max_seg_size(dma_dev);
135 hw.buffer_bytes_max = SIZE_MAX;
136 hw.fifo_size = dma_data->fifo_size;
137
a22f33b0
LPC
138 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
139 hw.info |= SNDRV_PCM_INFO_BATCH;
140
e957204e
SW
141 ret = snd_dmaengine_pcm_refine_runtime_hwparams(substream,
142 dma_data,
143 &hw,
144 chan);
145 if (ret)
146 return ret;
c0de42bf
LPC
147
148 return snd_soc_set_runtime_hwparams(substream, &hw);
28c4468b
LPC
149}
150
ece23171
KM
151static int dmaengine_pcm_open(struct snd_soc_component *component,
152 struct snd_pcm_substream *substream)
28c4468b 153{
be7ee5f3 154 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
c0de42bf
LPC
155 struct dma_chan *chan = pcm->chan[substream->stream];
156 int ret;
28c4468b 157
ece23171 158 ret = dmaengine_pcm_set_runtime_hwparams(component, substream);
c0de42bf
LPC
159 if (ret)
160 return ret;
161
162 return snd_dmaengine_pcm_open(substream, chan);
28c4468b
LPC
163}
164
ece23171
KM
165static int dmaengine_pcm_close(struct snd_soc_component *component,
166 struct snd_pcm_substream *substream)
167{
168 return snd_dmaengine_pcm_close(substream);
169}
170
171static int dmaengine_pcm_hw_free(struct snd_soc_component *component,
172 struct snd_pcm_substream *substream)
173{
174 return snd_pcm_lib_free_pages(substream);
175}
176
177static int dmaengine_pcm_trigger(struct snd_soc_component *component,
178 struct snd_pcm_substream *substream, int cmd)
179{
180 return snd_dmaengine_pcm_trigger(substream, cmd);
181}
182
c999836d 183static struct dma_chan *dmaengine_pcm_compat_request_channel(
ece23171 184 struct snd_soc_component *component,
c999836d
LPC
185 struct snd_soc_pcm_runtime *rtd,
186 struct snd_pcm_substream *substream)
187{
be7ee5f3 188 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
90130d2e 189 struct snd_dmaengine_dai_dma_data *dma_data;
ec4f2857 190 dma_filter_fn fn = NULL;
90130d2e
MB
191
192 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
c999836d 193
d1e1406c
LPC
194 if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) && pcm->chan[0])
195 return pcm->chan[0];
196
ec4f2857 197 if (pcm->config && pcm->config->compat_request_channel)
c999836d
LPC
198 return pcm->config->compat_request_channel(rtd, substream);
199
ec4f2857
XL
200 if (pcm->config)
201 fn = pcm->config->compat_filter_fn;
202
203 return snd_dmaengine_pcm_request_channel(fn, dma_data->filter_data);
c999836d
LPC
204}
205
acde50a7
LPC
206static bool dmaengine_pcm_can_report_residue(struct device *dev,
207 struct dma_chan *chan)
478028e0
LPC
208{
209 struct dma_slave_caps dma_caps;
210 int ret;
211
212 ret = dma_get_slave_caps(chan, &dma_caps);
acde50a7
LPC
213 if (ret != 0) {
214 dev_warn(dev, "Failed to get DMA channel capabilities, falling back to period counting: %d\n",
215 ret);
216 return false;
217 }
478028e0
LPC
218
219 if (dma_caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR)
220 return false;
221
222 return true;
223}
224
ece23171
KM
225static int dmaengine_pcm_new(struct snd_soc_component *component,
226 struct snd_soc_pcm_runtime *rtd)
28c4468b 227{
be7ee5f3 228 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
28c4468b 229 const struct snd_dmaengine_pcm_config *config = pcm->config;
be7ee5f3 230 struct device *dev = component->dev;
28c4468b 231 struct snd_pcm_substream *substream;
fa654e08
LPC
232 size_t prealloc_buffer_size;
233 size_t max_buffer_size;
28c4468b 234 unsigned int i;
28c4468b 235
fa654e08
LPC
236 if (config && config->prealloc_buffer_size) {
237 prealloc_buffer_size = config->prealloc_buffer_size;
238 max_buffer_size = config->pcm_hardware->buffer_bytes_max;
239 } else {
240 prealloc_buffer_size = 512 * 1024;
241 max_buffer_size = SIZE_MAX;
242 }
243
28c4468b
LPC
244 for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE; i++) {
245 substream = rtd->pcm->streams[i].substream;
246 if (!substream)
247 continue;
248
76d9c68b 249 if (!pcm->chan[i] && config && config->chan_names[i])
9bfa24e9 250 pcm->chan[i] = dma_request_slave_channel(dev,
76d9c68b 251 config->chan_names[i]);
9bfa24e9 252
d1e1406c 253 if (!pcm->chan[i] && (pcm->flags & SND_DMAENGINE_PCM_FLAG_COMPAT)) {
ece23171
KM
254 pcm->chan[i] = dmaengine_pcm_compat_request_channel(
255 component, rtd, substream);
c999836d
LPC
256 }
257
28c4468b 258 if (!pcm->chan[i]) {
be7ee5f3 259 dev_err(component->dev,
28c4468b 260 "Missing dma channel for stream: %d\n", i);
de7621e8 261 return -EINVAL;
28c4468b
LPC
262 }
263
6c422436 264 snd_pcm_lib_preallocate_pages(substream,
ca2b0295 265 SNDRV_DMA_TYPE_DEV_IRAM,
28c4468b 266 dmaengine_dma_dev(pcm, substream),
fa654e08
LPC
267 prealloc_buffer_size,
268 max_buffer_size);
478028e0 269
acde50a7 270 if (!dmaengine_pcm_can_report_residue(dev, pcm->chan[i]))
478028e0 271 pcm->flags |= SND_DMAENGINE_PCM_FLAG_NO_RESIDUE;
2ec42f31
PU
272
273 if (rtd->pcm->streams[i].pcm->name[0] == '\0') {
48118a93
PU
274 strscpy_pad(rtd->pcm->streams[i].pcm->name,
275 rtd->pcm->streams[i].pcm->id,
276 sizeof(rtd->pcm->streams[i].pcm->name));
2ec42f31 277 }
28c4468b
LPC
278 }
279
280 return 0;
28c4468b
LPC
281}
282
93b943ed 283static snd_pcm_uframes_t dmaengine_pcm_pointer(
ece23171 284 struct snd_soc_component *component,
93b943ed
LPC
285 struct snd_pcm_substream *substream)
286{
be7ee5f3 287 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
93b943ed
LPC
288
289 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
290 return snd_dmaengine_pcm_pointer_no_residue(substream);
291 else
292 return snd_dmaengine_pcm_pointer(substream);
293}
294
ece23171
KM
295static int dmaengine_copy_user(struct snd_soc_component *component,
296 struct snd_pcm_substream *substream,
78648092 297 int channel, unsigned long hwoff,
40d1299f 298 void __user *buf, unsigned long bytes)
78648092 299{
78648092
OM
300 struct snd_pcm_runtime *runtime = substream->runtime;
301 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
302 int (*process)(struct snd_pcm_substream *substream,
303 int channel, unsigned long hwoff,
304 void *buf, unsigned long bytes) = pcm->config->process;
305 bool is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
306 void *dma_ptr = runtime->dma_area + hwoff +
307 channel * (runtime->dma_bytes / runtime->channels);
308 int ret;
309
310 if (is_playback)
40d1299f 311 if (copy_from_user(dma_ptr, buf, bytes))
78648092
OM
312 return -EFAULT;
313
314 if (process) {
40d1299f 315 ret = process(substream, channel, hwoff, (__force void *)buf, bytes);
78648092
OM
316 if (ret < 0)
317 return ret;
318 }
319
320 if (!is_playback)
40d1299f 321 if (copy_to_user(buf, dma_ptr, bytes))
78648092
OM
322 return -EFAULT;
323
324 return 0;
325}
326
ece23171
KM
327static const struct snd_soc_component_driver dmaengine_pcm_component = {
328 .name = SND_DMAENGINE_PCM_DRV_NAME,
329 .probe_order = SND_SOC_COMP_ORDER_LATE,
28c4468b 330 .open = dmaengine_pcm_open,
ece23171
KM
331 .close = dmaengine_pcm_close,
332 .ioctl = snd_soc_pcm_lib_ioctl,
28c4468b 333 .hw_params = dmaengine_pcm_hw_params,
ece23171
KM
334 .hw_free = dmaengine_pcm_hw_free,
335 .trigger = dmaengine_pcm_trigger,
93b943ed 336 .pointer = dmaengine_pcm_pointer,
ece23171 337 .pcm_construct = dmaengine_pcm_new,
28c4468b
LPC
338};
339
ece23171
KM
340static const struct snd_soc_component_driver dmaengine_pcm_component_process = {
341 .name = SND_DMAENGINE_PCM_DRV_NAME,
342 .probe_order = SND_SOC_COMP_ORDER_LATE,
78648092 343 .open = dmaengine_pcm_open,
ece23171
KM
344 .close = dmaengine_pcm_close,
345 .ioctl = snd_soc_pcm_lib_ioctl,
78648092 346 .hw_params = dmaengine_pcm_hw_params,
ece23171
KM
347 .hw_free = dmaengine_pcm_hw_free,
348 .trigger = dmaengine_pcm_trigger,
78648092
OM
349 .pointer = dmaengine_pcm_pointer,
350 .copy_user = dmaengine_copy_user,
ece23171 351 .pcm_construct = dmaengine_pcm_new,
78648092
OM
352};
353
28c4468b
LPC
354static const char * const dmaengine_pcm_dma_channel_names[] = {
355 [SNDRV_PCM_STREAM_PLAYBACK] = "tx",
356 [SNDRV_PCM_STREAM_CAPTURE] = "rx",
357};
358
5eda87b8 359static int dmaengine_pcm_request_chan_of(struct dmaengine_pcm *pcm,
194c7dea 360 struct device *dev, const struct snd_dmaengine_pcm_config *config)
d1e1406c
LPC
361{
362 unsigned int i;
11b3a7ad 363 const char *name;
5eda87b8 364 struct dma_chan *chan;
d1e1406c 365
76d9c68b
SN
366 if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_DT) || (!dev->of_node &&
367 !(config && config->dma_dev && config->dma_dev->of_node)))
5eda87b8 368 return 0;
d1e1406c 369
2b67f8ba 370 if (config && config->dma_dev) {
194c7dea
SW
371 /*
372 * If this warning is seen, it probably means that your Linux
373 * device structure does not match your HW device structure.
374 * It would be best to refactor the Linux device structure to
375 * correctly match the HW structure.
376 */
377 dev_warn(dev, "DMA channels sourced from device %s",
378 dev_name(config->dma_dev));
379 dev = config->dma_dev;
380 }
381
11b3a7ad
SW
382 for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE;
383 i++) {
384 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
385 name = "rx-tx";
386 else
387 name = dmaengine_pcm_dma_channel_names[i];
2b67f8ba 388 if (config && config->chan_names[i])
194c7dea 389 name = config->chan_names[i];
5eda87b8
SW
390 chan = dma_request_slave_channel_reason(dev, name);
391 if (IS_ERR(chan)) {
e9036c2a 392 if (PTR_ERR(chan) == -EPROBE_DEFER)
5eda87b8
SW
393 return -EPROBE_DEFER;
394 pcm->chan[i] = NULL;
395 } else {
396 pcm->chan[i] = chan;
397 }
11b3a7ad
SW
398 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
399 break;
d1e1406c 400 }
11b3a7ad
SW
401
402 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
403 pcm->chan[1] = pcm->chan[0];
5eda87b8
SW
404
405 return 0;
d1e1406c
LPC
406}
407
6b9f3e65
SW
408static void dmaengine_pcm_release_chan(struct dmaengine_pcm *pcm)
409{
410 unsigned int i;
411
412 for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE;
413 i++) {
414 if (!pcm->chan[i])
415 continue;
416 dma_release_channel(pcm->chan[i]);
417 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
418 break;
419 }
420}
421
28c4468b
LPC
422/**
423 * snd_dmaengine_pcm_register - Register a dmaengine based PCM device
424 * @dev: The parent device for the PCM device
425 * @config: Platform specific PCM configuration
426 * @flags: Platform specific quirks
427 */
428int snd_dmaengine_pcm_register(struct device *dev,
429 const struct snd_dmaengine_pcm_config *config, unsigned int flags)
430{
431 struct dmaengine_pcm *pcm;
6b9f3e65 432 int ret;
28c4468b 433
28c4468b
LPC
434 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
435 if (!pcm)
436 return -ENOMEM;
437
f0b3bdbd
FE
438#ifdef CONFIG_DEBUG_FS
439 pcm->component.debugfs_prefix = "dma";
440#endif
28c4468b 441 pcm->config = config;
d1e1406c 442 pcm->flags = flags;
28c4468b 443
5eda87b8
SW
444 ret = dmaengine_pcm_request_chan_of(pcm, dev, config);
445 if (ret)
b84acf44 446 goto err_free_dma;
28c4468b 447
78648092
OM
448 if (config && config->process)
449 ret = snd_soc_add_component(dev, &pcm->component,
450 &dmaengine_pcm_component_process,
451 NULL, 0);
452 else
453 ret = snd_soc_add_component(dev, &pcm->component,
454 &dmaengine_pcm_component, NULL, 0);
6b9f3e65
SW
455 if (ret)
456 goto err_free_dma;
457
458 return 0;
459
460err_free_dma:
461 dmaengine_pcm_release_chan(pcm);
462 kfree(pcm);
463 return ret;
28c4468b
LPC
464}
465EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_register);
466
467/**
468 * snd_dmaengine_pcm_unregister - Removes a dmaengine based PCM device
469 * @dev: Parent device the PCM was register with
470 *
471 * Removes a dmaengine based PCM device previously registered with
472 * snd_dmaengine_pcm_register.
473 */
474void snd_dmaengine_pcm_unregister(struct device *dev)
475{
be7ee5f3 476 struct snd_soc_component *component;
28c4468b 477 struct dmaengine_pcm *pcm;
28c4468b 478
be7ee5f3
KM
479 component = snd_soc_lookup_component(dev, SND_DMAENGINE_PCM_DRV_NAME);
480 if (!component)
28c4468b
LPC
481 return;
482
be7ee5f3 483 pcm = soc_component_to_pcm(component);
28c4468b 484
be7ee5f3 485 snd_soc_unregister_component(dev);
6b9f3e65 486 dmaengine_pcm_release_chan(pcm);
28c4468b
LPC
487 kfree(pcm);
488}
489EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_unregister);
490
491MODULE_LICENSE("GPL");