at91: kernel v5.15: copy config and patches from 5.10
[openwrt/openwrt.git] / target / linux / at91 / patches-5.15 / 144-ASoC-atmel-pdc-Use-managed-DMA-buffer-allocation.patch
1 From 0b20c174a17dcfa805ddac1301a5af7298877ec3 Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Wed, 6 Jan 2021 14:36:48 +0100
4 Subject: [PATCH 144/247] ASoC: atmel-pdc: Use managed DMA buffer allocation
5
6 Instead of manually managing its DMA buffers using
7 dma_{alloc,free}_coherent() lets the sound core take care of this using
8 managed buffers.
9
10 On one hand this reduces the amount of boiler plate code, but the main
11 motivation for the change is to use the shared code where possible. This
12 makes it easier to argue about correctness and that the code does not
13 contain subtle bugs like data leakage or similar.
14
15 Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
16 Reviewed-by: Codrin Ciubotariu <codrin.ciubotariu@microchip.com>
17 Link: https://lore.kernel.org/r/20210106133650.13509-1-lars@metafoo.de
18 Signed-off-by: Mark Brown <broonie@kernel.org>
19 ---
20 sound/soc/atmel/atmel-pcm-pdc.c | 78 ++-------------------------------
21 1 file changed, 4 insertions(+), 74 deletions(-)
22
23 --- a/sound/soc/atmel/atmel-pcm-pdc.c
24 +++ b/sound/soc/atmel/atmel-pcm-pdc.c
25 @@ -34,86 +34,21 @@
26 #include "atmel-pcm.h"
27
28
29 -static int atmel_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
30 - int stream)
31 -{
32 - struct snd_pcm_substream *substream = pcm->streams[stream].substream;
33 - struct snd_dma_buffer *buf = &substream->dma_buffer;
34 - size_t size = ATMEL_SSC_DMABUF_SIZE;
35 -
36 - buf->dev.type = SNDRV_DMA_TYPE_DEV;
37 - buf->dev.dev = pcm->card->dev;
38 - buf->private_data = NULL;
39 - buf->area = dma_alloc_coherent(pcm->card->dev, size,
40 - &buf->addr, GFP_KERNEL);
41 - pr_debug("atmel-pcm: alloc dma buffer: area=%p, addr=%p, size=%zu\n",
42 - (void *)buf->area, (void *)(long)buf->addr, size);
43 -
44 - if (!buf->area)
45 - return -ENOMEM;
46 -
47 - buf->bytes = size;
48 - return 0;
49 -}
50 -
51 -static int atmel_pcm_mmap(struct snd_soc_component *component,
52 - struct snd_pcm_substream *substream,
53 - struct vm_area_struct *vma)
54 -{
55 - return remap_pfn_range(vma, vma->vm_start,
56 - substream->dma_buffer.addr >> PAGE_SHIFT,
57 - vma->vm_end - vma->vm_start, vma->vm_page_prot);
58 -}
59 -
60 static int atmel_pcm_new(struct snd_soc_component *component,
61 struct snd_soc_pcm_runtime *rtd)
62 {
63 struct snd_card *card = rtd->card->snd_card;
64 - struct snd_pcm *pcm = rtd->pcm;
65 int ret;
66
67 ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
68 if (ret)
69 return ret;
70
71 - if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
72 - pr_debug("atmel-pcm: allocating PCM playback DMA buffer\n");
73 - ret = atmel_pcm_preallocate_dma_buffer(pcm,
74 - SNDRV_PCM_STREAM_PLAYBACK);
75 - if (ret)
76 - goto out;
77 - }
78 + snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV,
79 + card->dev, ATMEL_SSC_DMABUF_SIZE,
80 + ATMEL_SSC_DMABUF_SIZE);
81
82 - if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
83 - pr_debug("atmel-pcm: allocating PCM capture DMA buffer\n");
84 - ret = atmel_pcm_preallocate_dma_buffer(pcm,
85 - SNDRV_PCM_STREAM_CAPTURE);
86 - if (ret)
87 - goto out;
88 - }
89 - out:
90 - return ret;
91 -}
92 -
93 -static void atmel_pcm_free(struct snd_soc_component *component,
94 - struct snd_pcm *pcm)
95 -{
96 - struct snd_pcm_substream *substream;
97 - struct snd_dma_buffer *buf;
98 - int stream;
99 -
100 - for (stream = 0; stream < 2; stream++) {
101 - substream = pcm->streams[stream].substream;
102 - if (!substream)
103 - continue;
104 -
105 - buf = &substream->dma_buffer;
106 - if (!buf->area)
107 - continue;
108 - dma_free_coherent(pcm->card->dev, buf->bytes,
109 - buf->area, buf->addr);
110 - buf->area = NULL;
111 - }
112 + return 0;
113 }
114
115 /*--------------------------------------------------------------------------*\
116 @@ -210,9 +145,6 @@ static int atmel_pcm_hw_params(struct sn
117 /* this may get called several times by oss emulation
118 * with different params */
119
120 - snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
121 - runtime->dma_bytes = params_buffer_bytes(params);
122 -
123 prtd->params = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
124 prtd->params->dma_intr_handler = atmel_pcm_dma_irq;
125
126 @@ -384,9 +316,7 @@ static const struct snd_soc_component_dr
127 .prepare = atmel_pcm_prepare,
128 .trigger = atmel_pcm_trigger,
129 .pointer = atmel_pcm_pointer,
130 - .mmap = atmel_pcm_mmap,
131 .pcm_construct = atmel_pcm_new,
132 - .pcm_destruct = atmel_pcm_free,
133 };
134
135 int atmel_pcm_pdc_platform_register(struct device *dev)