1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * linux/sound/soc/pxa/mmp-pcm.c
4 *
5 * Copyright (C) 2011 Marvell International Ltd.
6 */
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/platform_device.h>
10 #include <linux/slab.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/dmaengine.h>
13 #include <linux/platform_data/dma-mmp_tdma.h>
14 #include <linux/platform_data/mmp_audio.h>
15
16 #include <sound/pxa2xx-lib.h>
17 #include <sound/core.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
21 #include <sound/dmaengine_pcm.h>
22
23 #define DRV_NAME "mmp-pcm"
24
25 struct mmp_dma_data {
26 int ssp_id;
27 struct resource *dma_res;
28 };
29
30 #define MMP_PCM_INFO (SNDRV_PCM_INFO_MMAP | \
31 SNDRV_PCM_INFO_MMAP_VALID | \
32 SNDRV_PCM_INFO_INTERLEAVED | \
33 SNDRV_PCM_INFO_PAUSE | \
34 SNDRV_PCM_INFO_RESUME | \
35 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP)
36
37 static struct snd_pcm_hardware mmp_pcm_hardware[] = {
38 {
39 .info = MMP_PCM_INFO,
40 .period_bytes_min = 1024,
41 .period_bytes_max = 2048,
42 .periods_min = 2,
43 .periods_max = 32,
44 .buffer_bytes_max = 4096,
45 .fifo_size = 32,
46 },
47 {
48 .info = MMP_PCM_INFO,
49 .period_bytes_min = 1024,
50 .period_bytes_max = 2048,
51 .periods_min = 2,
52 .periods_max = 32,
53 .buffer_bytes_max = 4096,
54 .fifo_size = 32,
55 },
56 };
57
mmp_pcm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)58 static int mmp_pcm_hw_params(struct snd_pcm_substream *substream,
59 struct snd_pcm_hw_params *params)
60 {
61 struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
62 struct dma_slave_config slave_config;
63 int ret;
64
65 ret =
66 snd_dmaengine_pcm_prepare_slave_config(substream, params,
67 &slave_config);
68 if (ret)
69 return ret;
70
71 ret = dmaengine_slave_config(chan, &slave_config);
72 if (ret)
73 return ret;
74
75 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
76
77 return 0;
78 }
79
filter(struct dma_chan * chan,void * param)80 static bool filter(struct dma_chan *chan, void *param)
81 {
82 struct mmp_dma_data *dma_data = param;
83 bool found = false;
84 char *devname;
85
86 devname = kasprintf(GFP_KERNEL, "%s.%d", dma_data->dma_res->name,
87 dma_data->ssp_id);
88 if ((strcmp(dev_name(chan->device->dev), devname) == 0) &&
89 (chan->chan_id == dma_data->dma_res->start)) {
90 found = true;
91 }
92
93 kfree(devname);
94 return found;
95 }
96
mmp_pcm_open(struct snd_pcm_substream * substream)97 static int mmp_pcm_open(struct snd_pcm_substream *substream)
98 {
99 struct snd_soc_pcm_runtime *rtd = substream->private_data;
100 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
101 struct platform_device *pdev = to_platform_device(component->dev);
102 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
103 struct mmp_dma_data dma_data;
104 struct resource *r;
105
106 r = platform_get_resource(pdev, IORESOURCE_DMA, substream->stream);
107 if (!r)
108 return -EBUSY;
109
110 snd_soc_set_runtime_hwparams(substream,
111 &mmp_pcm_hardware[substream->stream]);
112
113 dma_data.dma_res = r;
114 dma_data.ssp_id = cpu_dai->id;
115
116 return snd_dmaengine_pcm_open_request_chan(substream, filter,
117 &dma_data);
118 }
119
mmp_pcm_mmap(struct snd_pcm_substream * substream,struct vm_area_struct * vma)120 static int mmp_pcm_mmap(struct snd_pcm_substream *substream,
121 struct vm_area_struct *vma)
122 {
123 struct snd_pcm_runtime *runtime = substream->runtime;
124 unsigned long off = vma->vm_pgoff;
125
126 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
127 return remap_pfn_range(vma, vma->vm_start,
128 __phys_to_pfn(runtime->dma_addr) + off,
129 vma->vm_end - vma->vm_start, vma->vm_page_prot);
130 }
131
132 static const struct snd_pcm_ops mmp_pcm_ops = {
133 .open = mmp_pcm_open,
134 .close = snd_dmaengine_pcm_close_release_chan,
135 .ioctl = snd_pcm_lib_ioctl,
136 .hw_params = mmp_pcm_hw_params,
137 .trigger = snd_dmaengine_pcm_trigger,
138 .pointer = snd_dmaengine_pcm_pointer,
139 .mmap = mmp_pcm_mmap,
140 };
141
mmp_pcm_free_dma_buffers(struct snd_pcm * pcm)142 static void mmp_pcm_free_dma_buffers(struct snd_pcm *pcm)
143 {
144 struct snd_pcm_substream *substream;
145 struct snd_dma_buffer *buf;
146 int stream;
147 struct gen_pool *gpool;
148
149 gpool = sram_get_gpool("asram");
150 if (!gpool)
151 return;
152
153 for (stream = 0; stream < 2; stream++) {
154 size_t size = mmp_pcm_hardware[stream].buffer_bytes_max;
155
156 substream = pcm->streams[stream].substream;
157 if (!substream)
158 continue;
159
160 buf = &substream->dma_buffer;
161 if (!buf->area)
162 continue;
163 gen_pool_free(gpool, (unsigned long)buf->area, size);
164 buf->area = NULL;
165 }
166
167 }
168
mmp_pcm_preallocate_dma_buffer(struct snd_pcm_substream * substream,int stream)169 static int mmp_pcm_preallocate_dma_buffer(struct snd_pcm_substream *substream,
170 int stream)
171 {
172 struct snd_dma_buffer *buf = &substream->dma_buffer;
173 size_t size = mmp_pcm_hardware[stream].buffer_bytes_max;
174 struct gen_pool *gpool;
175
176 buf->dev.type = SNDRV_DMA_TYPE_DEV;
177 buf->dev.dev = substream->pcm->card->dev;
178 buf->private_data = NULL;
179
180 gpool = sram_get_gpool("asram");
181 if (!gpool)
182 return -ENOMEM;
183
184 buf->area = gen_pool_dma_alloc(gpool, size, &buf->addr);
185 if (!buf->area)
186 return -ENOMEM;
187 buf->bytes = size;
188 return 0;
189 }
190
mmp_pcm_new(struct snd_soc_pcm_runtime * rtd)191 static int mmp_pcm_new(struct snd_soc_pcm_runtime *rtd)
192 {
193 struct snd_pcm_substream *substream;
194 struct snd_pcm *pcm = rtd->pcm;
195 int ret = 0, stream;
196
197 for (stream = 0; stream < 2; stream++) {
198 substream = pcm->streams[stream].substream;
199
200 ret = mmp_pcm_preallocate_dma_buffer(substream, stream);
201 if (ret)
202 goto err;
203 }
204
205 return 0;
206
207 err:
208 mmp_pcm_free_dma_buffers(pcm);
209 return ret;
210 }
211
212 static const struct snd_soc_component_driver mmp_soc_component = {
213 .name = DRV_NAME,
214 .ops = &mmp_pcm_ops,
215 .pcm_new = mmp_pcm_new,
216 .pcm_free = mmp_pcm_free_dma_buffers,
217 };
218
mmp_pcm_probe(struct platform_device * pdev)219 static int mmp_pcm_probe(struct platform_device *pdev)
220 {
221 struct mmp_audio_platdata *pdata = pdev->dev.platform_data;
222
223 if (pdata) {
224 mmp_pcm_hardware[SNDRV_PCM_STREAM_PLAYBACK].buffer_bytes_max =
225 pdata->buffer_max_playback;
226 mmp_pcm_hardware[SNDRV_PCM_STREAM_PLAYBACK].period_bytes_max =
227 pdata->period_max_playback;
228 mmp_pcm_hardware[SNDRV_PCM_STREAM_CAPTURE].buffer_bytes_max =
229 pdata->buffer_max_capture;
230 mmp_pcm_hardware[SNDRV_PCM_STREAM_CAPTURE].period_bytes_max =
231 pdata->period_max_capture;
232 }
233 return devm_snd_soc_register_component(&pdev->dev, &mmp_soc_component,
234 NULL, 0);
235 }
236
237 static struct platform_driver mmp_pcm_driver = {
238 .driver = {
239 .name = "mmp-pcm-audio",
240 },
241
242 .probe = mmp_pcm_probe,
243 };
244
245 module_platform_driver(mmp_pcm_driver);
246
247 MODULE_AUTHOR("Leo Yan <leoy@marvell.com>");
248 MODULE_DESCRIPTION("MMP Soc Audio DMA module");
249 MODULE_LICENSE("GPL");
250 MODULE_ALIAS("platform:mmp-pcm-audio");
251