at91: add kernel support for sama7g5 soc
[openwrt/staging/dedeckeh.git] / target / linux / at91 / patches-5.10 / 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 diff --git a/sound/soc/atmel/atmel-pcm-pdc.c b/sound/soc/atmel/atmel-pcm-pdc.c
24 index 704f700013d3..3e7ea2021b46 100644
25 --- a/sound/soc/atmel/atmel-pcm-pdc.c
26 +++ b/sound/soc/atmel/atmel-pcm-pdc.c
27 @@ -34,86 +34,21 @@
28 #include "atmel-pcm.h"
29
30
31 -static int atmel_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
32 - int stream)
33 -{
34 - struct snd_pcm_substream *substream = pcm->streams[stream].substream;
35 - struct snd_dma_buffer *buf = &substream->dma_buffer;
36 - size_t size = ATMEL_SSC_DMABUF_SIZE;
37 -
38 - buf->dev.type = SNDRV_DMA_TYPE_DEV;
39 - buf->dev.dev = pcm->card->dev;
40 - buf->private_data = NULL;
41 - buf->area = dma_alloc_coherent(pcm->card->dev, size,
42 - &buf->addr, GFP_KERNEL);
43 - pr_debug("atmel-pcm: alloc dma buffer: area=%p, addr=%p, size=%zu\n",
44 - (void *)buf->area, (void *)(long)buf->addr, size);
45 -
46 - if (!buf->area)
47 - return -ENOMEM;
48 -
49 - buf->bytes = size;
50 - return 0;
51 -}
52 -
53 -static int atmel_pcm_mmap(struct snd_soc_component *component,
54 - struct snd_pcm_substream *substream,
55 - struct vm_area_struct *vma)
56 -{
57 - return remap_pfn_range(vma, vma->vm_start,
58 - substream->dma_buffer.addr >> PAGE_SHIFT,
59 - vma->vm_end - vma->vm_start, vma->vm_page_prot);
60 -}
61 -
62 static int atmel_pcm_new(struct snd_soc_component *component,
63 struct snd_soc_pcm_runtime *rtd)
64 {
65 struct snd_card *card = rtd->card->snd_card;
66 - struct snd_pcm *pcm = rtd->pcm;
67 int ret;
68
69 ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
70 if (ret)
71 return ret;
72
73 - if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
74 - pr_debug("atmel-pcm: allocating PCM playback DMA buffer\n");
75 - ret = atmel_pcm_preallocate_dma_buffer(pcm,
76 - SNDRV_PCM_STREAM_PLAYBACK);
77 - if (ret)
78 - goto out;
79 - }
80 + snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV,
81 + card->dev, ATMEL_SSC_DMABUF_SIZE,
82 + ATMEL_SSC_DMABUF_SIZE);
83
84 - if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
85 - pr_debug("atmel-pcm: allocating PCM capture DMA buffer\n");
86 - ret = atmel_pcm_preallocate_dma_buffer(pcm,
87 - SNDRV_PCM_STREAM_CAPTURE);
88 - if (ret)
89 - goto out;
90 - }
91 - out:
92 - return ret;
93 -}
94 -
95 -static void atmel_pcm_free(struct snd_soc_component *component,
96 - struct snd_pcm *pcm)
97 -{
98 - struct snd_pcm_substream *substream;
99 - struct snd_dma_buffer *buf;
100 - int stream;
101 -
102 - for (stream = 0; stream < 2; stream++) {
103 - substream = pcm->streams[stream].substream;
104 - if (!substream)
105 - continue;
106 -
107 - buf = &substream->dma_buffer;
108 - if (!buf->area)
109 - continue;
110 - dma_free_coherent(pcm->card->dev, buf->bytes,
111 - buf->area, buf->addr);
112 - buf->area = NULL;
113 - }
114 + return 0;
115 }
116
117 /*--------------------------------------------------------------------------*\
118 @@ -210,9 +145,6 @@ static int atmel_pcm_hw_params(struct snd_soc_component *component,
119 /* this may get called several times by oss emulation
120 * with different params */
121
122 - snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
123 - runtime->dma_bytes = params_buffer_bytes(params);
124 -
125 prtd->params = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
126 prtd->params->dma_intr_handler = atmel_pcm_dma_irq;
127
128 @@ -384,9 +316,7 @@ static const struct snd_soc_component_driver atmel_soc_platform = {
129 .prepare = atmel_pcm_prepare,
130 .trigger = atmel_pcm_trigger,
131 .pointer = atmel_pcm_pointer,
132 - .mmap = atmel_pcm_mmap,
133 .pcm_construct = atmel_pcm_new,
134 - .pcm_destruct = atmel_pcm_free,
135 };
136
137 int atmel_pcm_pdc_platform_register(struct device *dev)
138 --
139 2.32.0
140