xref: /openbmc/linux/sound/soc/amd/ps/ps-pdm-dma.c (revision 66c98360)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * AMD ALSA SoC Pink Sardine PDM Driver
4  *
5  * Copyright 2022 Advanced Micro Devices, Inc.
6  */
7 
8 #include <linux/platform_device.h>
9 #include <linux/module.h>
10 #include <linux/bitfield.h>
11 #include <linux/err.h>
12 #include <linux/io.h>
13 #include <sound/pcm_params.h>
14 #include <sound/soc.h>
15 #include <sound/soc-dai.h>
16 #include <linux/pm_runtime.h>
17 
18 #include "acp63.h"
19 
20 #define DRV_NAME "acp_ps_pdm_dma"
21 
22 static int pdm_gain = 3;
23 module_param(pdm_gain, int, 0644);
24 MODULE_PARM_DESC(pdm_gain, "Gain control (0-3)");
25 
26 static const struct snd_pcm_hardware acp63_pdm_hardware_capture = {
27 	.info = SNDRV_PCM_INFO_INTERLEAVED |
28 		SNDRV_PCM_INFO_BLOCK_TRANSFER |
29 		SNDRV_PCM_INFO_MMAP |
30 		SNDRV_PCM_INFO_MMAP_VALID |
31 		SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
32 	.formats = SNDRV_PCM_FMTBIT_S32_LE,
33 	.channels_min = 2,
34 	.channels_max = 2,
35 	.rates = SNDRV_PCM_RATE_48000,
36 	.rate_min = 48000,
37 	.rate_max = 48000,
38 	.buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE,
39 	.period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
40 	.period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
41 	.periods_min = CAPTURE_MIN_NUM_PERIODS,
42 	.periods_max = CAPTURE_MAX_NUM_PERIODS,
43 };
44 
45 static void acp63_init_pdm_ring_buffer(u32 physical_addr, u32 buffer_size,
46 				       u32 watermark_size, void __iomem *acp_base)
47 {
48 	acp63_writel(physical_addr, acp_base + ACP_WOV_RX_RINGBUFADDR);
49 	acp63_writel(buffer_size, acp_base + ACP_WOV_RX_RINGBUFSIZE);
50 	acp63_writel(watermark_size, acp_base + ACP_WOV_RX_INTR_WATERMARK_SIZE);
51 	acp63_writel(0x01, acp_base + ACPAXI2AXI_ATU_CTRL);
52 }
53 
54 static void acp63_enable_pdm_clock(void __iomem *acp_base)
55 {
56 	u32 pdm_clk_enable, pdm_ctrl;
57 
58 	pdm_clk_enable = ACP_PDM_CLK_FREQ_MASK;
59 	pdm_ctrl = 0x00;
60 
61 	acp63_writel(pdm_clk_enable, acp_base + ACP_WOV_CLK_CTRL);
62 	pdm_ctrl = acp63_readl(acp_base + ACP_WOV_MISC_CTRL);
63 	pdm_ctrl &= ~ACP_WOV_GAIN_CONTROL;
64 	pdm_ctrl |= FIELD_PREP(ACP_WOV_GAIN_CONTROL, clamp(pdm_gain, 0, 3));
65 	acp63_writel(pdm_ctrl, acp_base + ACP_WOV_MISC_CTRL);
66 }
67 
68 static void acp63_enable_pdm_interrupts(struct pdm_dev_data *adata)
69 {
70 	u32 ext_int_ctrl;
71 
72 	mutex_lock(adata->acp_lock);
73 	ext_int_ctrl = acp63_readl(adata->acp63_base + ACP_EXTERNAL_INTR_CNTL);
74 	ext_int_ctrl |= PDM_DMA_INTR_MASK;
75 	acp63_writel(ext_int_ctrl, adata->acp63_base + ACP_EXTERNAL_INTR_CNTL);
76 	mutex_unlock(adata->acp_lock);
77 }
78 
79 static void acp63_disable_pdm_interrupts(struct pdm_dev_data *adata)
80 {
81 	u32 ext_int_ctrl;
82 
83 	mutex_lock(adata->acp_lock);
84 	ext_int_ctrl = acp63_readl(adata->acp63_base + ACP_EXTERNAL_INTR_CNTL);
85 	ext_int_ctrl &= ~PDM_DMA_INTR_MASK;
86 	acp63_writel(ext_int_ctrl, adata->acp63_base + ACP_EXTERNAL_INTR_CNTL);
87 	mutex_unlock(adata->acp_lock);
88 }
89 
90 static bool acp63_check_pdm_dma_status(void __iomem *acp_base)
91 {
92 	bool pdm_dma_status;
93 	u32 pdm_enable, pdm_dma_enable;
94 
95 	pdm_dma_status = false;
96 	pdm_enable = acp63_readl(acp_base + ACP_WOV_PDM_ENABLE);
97 	pdm_dma_enable = acp63_readl(acp_base + ACP_WOV_PDM_DMA_ENABLE);
98 	if ((pdm_enable & ACP_PDM_ENABLE) && (pdm_dma_enable & ACP_PDM_DMA_EN_STATUS))
99 		pdm_dma_status = true;
100 
101 	return pdm_dma_status;
102 }
103 
104 static int acp63_start_pdm_dma(void __iomem *acp_base)
105 {
106 	u32 pdm_enable;
107 	u32 pdm_dma_enable;
108 	int timeout;
109 
110 	pdm_enable = 0x01;
111 	pdm_dma_enable  = 0x01;
112 
113 	acp63_enable_pdm_clock(acp_base);
114 	acp63_writel(pdm_enable, acp_base + ACP_WOV_PDM_ENABLE);
115 	acp63_writel(pdm_dma_enable, acp_base + ACP_WOV_PDM_DMA_ENABLE);
116 	timeout = 0;
117 	while (++timeout < ACP_COUNTER) {
118 		pdm_dma_enable = acp63_readl(acp_base + ACP_WOV_PDM_DMA_ENABLE);
119 		if ((pdm_dma_enable & 0x02) == ACP_PDM_DMA_EN_STATUS)
120 			return 0;
121 		udelay(DELAY_US);
122 	}
123 	return -ETIMEDOUT;
124 }
125 
126 static int acp63_stop_pdm_dma(void __iomem *acp_base)
127 {
128 	u32 pdm_enable, pdm_dma_enable;
129 	int timeout;
130 
131 	pdm_enable = 0x00;
132 	pdm_dma_enable  = 0x00;
133 
134 	pdm_enable = acp63_readl(acp_base + ACP_WOV_PDM_ENABLE);
135 	pdm_dma_enable = acp63_readl(acp_base + ACP_WOV_PDM_DMA_ENABLE);
136 	if (pdm_dma_enable & 0x01) {
137 		pdm_dma_enable = 0x02;
138 		acp63_writel(pdm_dma_enable, acp_base + ACP_WOV_PDM_DMA_ENABLE);
139 		timeout = 0;
140 		while (++timeout < ACP_COUNTER) {
141 			pdm_dma_enable = acp63_readl(acp_base + ACP_WOV_PDM_DMA_ENABLE);
142 			if ((pdm_dma_enable & 0x02) == 0x00)
143 				break;
144 			udelay(DELAY_US);
145 		}
146 		if (timeout == ACP_COUNTER)
147 			return -ETIMEDOUT;
148 	}
149 	if (pdm_enable == ACP_PDM_ENABLE) {
150 		pdm_enable = ACP_PDM_DISABLE;
151 		acp63_writel(pdm_enable, acp_base + ACP_WOV_PDM_ENABLE);
152 	}
153 	acp63_writel(0x01, acp_base + ACP_WOV_PDM_FIFO_FLUSH);
154 	return 0;
155 }
156 
157 static void acp63_config_dma(struct pdm_stream_instance *rtd, int direction)
158 {
159 	u16 page_idx;
160 	u32 low, high, val;
161 	dma_addr_t addr;
162 
163 	addr = rtd->dma_addr;
164 	val = PDM_PTE_OFFSET;
165 
166 	/* Group Enable */
167 	acp63_writel(ACP_SRAM_PTE_OFFSET | BIT(31), rtd->acp63_base +
168 		     ACPAXI2AXI_ATU_BASE_ADDR_GRP_1);
169 	acp63_writel(PAGE_SIZE_4K_ENABLE, rtd->acp63_base +
170 		     ACPAXI2AXI_ATU_PAGE_SIZE_GRP_1);
171 	for (page_idx = 0; page_idx < rtd->num_pages; page_idx++) {
172 		/* Load the low address of page int ACP SRAM through SRBM */
173 		low = lower_32_bits(addr);
174 		high = upper_32_bits(addr);
175 
176 		acp63_writel(low, rtd->acp63_base + ACP_SCRATCH_REG_0 + val);
177 		high |= BIT(31);
178 		acp63_writel(high, rtd->acp63_base + ACP_SCRATCH_REG_0 + val + 4);
179 		val += 8;
180 		addr += PAGE_SIZE;
181 	}
182 }
183 
184 static int acp63_pdm_dma_open(struct snd_soc_component *component,
185 			      struct snd_pcm_substream *substream)
186 {
187 	struct snd_pcm_runtime *runtime;
188 	struct pdm_dev_data *adata;
189 	struct pdm_stream_instance *pdm_data;
190 	int ret;
191 
192 	runtime = substream->runtime;
193 	adata = dev_get_drvdata(component->dev);
194 	pdm_data = kzalloc(sizeof(*pdm_data), GFP_KERNEL);
195 	if (!pdm_data)
196 		return -EINVAL;
197 
198 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
199 		runtime->hw = acp63_pdm_hardware_capture;
200 
201 	ret = snd_pcm_hw_constraint_integer(runtime,
202 					    SNDRV_PCM_HW_PARAM_PERIODS);
203 	if (ret < 0) {
204 		dev_err(component->dev, "set integer constraint failed\n");
205 		kfree(pdm_data);
206 		return ret;
207 	}
208 
209 	acp63_enable_pdm_interrupts(adata);
210 
211 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
212 		adata->capture_stream = substream;
213 
214 	pdm_data->acp63_base = adata->acp63_base;
215 	runtime->private_data = pdm_data;
216 	return ret;
217 }
218 
219 static int acp63_pdm_dma_hw_params(struct snd_soc_component *component,
220 				   struct snd_pcm_substream *substream,
221 				   struct snd_pcm_hw_params *params)
222 {
223 	struct pdm_stream_instance *rtd;
224 	size_t size, period_bytes;
225 
226 	rtd = substream->runtime->private_data;
227 	if (!rtd)
228 		return -EINVAL;
229 	size = params_buffer_bytes(params);
230 	period_bytes = params_period_bytes(params);
231 	rtd->dma_addr = substream->runtime->dma_addr;
232 	rtd->num_pages = (PAGE_ALIGN(size) >> PAGE_SHIFT);
233 	acp63_config_dma(rtd, substream->stream);
234 	acp63_init_pdm_ring_buffer(PDM_MEM_WINDOW_START, size,
235 				   period_bytes, rtd->acp63_base);
236 	return 0;
237 }
238 
239 static u64 acp63_pdm_get_byte_count(struct pdm_stream_instance *rtd,
240 				    int direction)
241 {
242 	u32 high, low;
243 	u64 byte_count;
244 
245 	high = acp63_readl(rtd->acp63_base + ACP_WOV_RX_LINEARPOSITIONCNTR_HIGH);
246 	byte_count = high;
247 	low = acp63_readl(rtd->acp63_base + ACP_WOV_RX_LINEARPOSITIONCNTR_LOW);
248 	byte_count = (byte_count << 32) | low;
249 	return byte_count;
250 }
251 
252 static snd_pcm_uframes_t acp63_pdm_dma_pointer(struct snd_soc_component *comp,
253 					       struct snd_pcm_substream *stream)
254 {
255 	struct pdm_stream_instance *rtd;
256 	u32 pos, buffersize;
257 	u64 bytescount;
258 
259 	rtd = stream->runtime->private_data;
260 	buffersize = frames_to_bytes(stream->runtime,
261 				     stream->runtime->buffer_size);
262 	bytescount = acp63_pdm_get_byte_count(rtd, stream->stream);
263 	if (bytescount > rtd->bytescount)
264 		bytescount -= rtd->bytescount;
265 	pos = do_div(bytescount, buffersize);
266 	return bytes_to_frames(stream->runtime, pos);
267 }
268 
269 static int acp63_pdm_dma_new(struct snd_soc_component *component,
270 			     struct snd_soc_pcm_runtime *rtd)
271 {
272 	struct device *parent = component->dev->parent;
273 
274 	snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV,
275 				       parent, MIN_BUFFER, MAX_BUFFER);
276 	return 0;
277 }
278 
279 static int acp63_pdm_dma_close(struct snd_soc_component *component,
280 			       struct snd_pcm_substream *substream)
281 {
282 	struct pdm_dev_data *adata = dev_get_drvdata(component->dev);
283 	struct snd_pcm_runtime *runtime = substream->runtime;
284 
285 	acp63_disable_pdm_interrupts(adata);
286 	adata->capture_stream = NULL;
287 	kfree(runtime->private_data);
288 	return 0;
289 }
290 
291 static int acp63_pdm_dai_trigger(struct snd_pcm_substream *substream,
292 				 int cmd, struct snd_soc_dai *dai)
293 {
294 	struct pdm_stream_instance *rtd;
295 	int ret;
296 	bool pdm_status;
297 	unsigned int ch_mask;
298 
299 	rtd = substream->runtime->private_data;
300 	ret = 0;
301 	switch (substream->runtime->channels) {
302 	case TWO_CH:
303 		ch_mask = 0x00;
304 		break;
305 	default:
306 		return -EINVAL;
307 	}
308 	switch (cmd) {
309 	case SNDRV_PCM_TRIGGER_START:
310 	case SNDRV_PCM_TRIGGER_RESUME:
311 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
312 		acp63_writel(ch_mask, rtd->acp63_base + ACP_WOV_PDM_NO_OF_CHANNELS);
313 		acp63_writel(PDM_DECIMATION_FACTOR, rtd->acp63_base +
314 			     ACP_WOV_PDM_DECIMATION_FACTOR);
315 		rtd->bytescount = acp63_pdm_get_byte_count(rtd, substream->stream);
316 		pdm_status = acp63_check_pdm_dma_status(rtd->acp63_base);
317 		if (!pdm_status)
318 			ret = acp63_start_pdm_dma(rtd->acp63_base);
319 		break;
320 	case SNDRV_PCM_TRIGGER_STOP:
321 	case SNDRV_PCM_TRIGGER_SUSPEND:
322 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
323 		pdm_status = acp63_check_pdm_dma_status(rtd->acp63_base);
324 		if (pdm_status)
325 			ret = acp63_stop_pdm_dma(rtd->acp63_base);
326 		break;
327 	default:
328 		ret = -EINVAL;
329 		break;
330 	}
331 	return ret;
332 }
333 
334 static const struct snd_soc_dai_ops acp63_pdm_dai_ops = {
335 	.trigger   = acp63_pdm_dai_trigger,
336 };
337 
338 static struct snd_soc_dai_driver acp63_pdm_dai_driver = {
339 	.name = "acp_ps_pdm_dma.0",
340 	.capture = {
341 		.rates = SNDRV_PCM_RATE_48000,
342 		.formats = SNDRV_PCM_FMTBIT_S32_LE,
343 		.channels_min = 2,
344 		.channels_max = 2,
345 		.rate_min = 48000,
346 		.rate_max = 48000,
347 	},
348 	.ops = &acp63_pdm_dai_ops,
349 };
350 
351 static const struct snd_soc_component_driver acp63_pdm_component = {
352 	.name		= DRV_NAME,
353 	.open		= acp63_pdm_dma_open,
354 	.close		= acp63_pdm_dma_close,
355 	.hw_params	= acp63_pdm_dma_hw_params,
356 	.pointer	= acp63_pdm_dma_pointer,
357 	.pcm_construct	= acp63_pdm_dma_new,
358 };
359 
360 static int acp63_pdm_audio_probe(struct platform_device *pdev)
361 {
362 	struct resource *res;
363 	struct pdm_dev_data *adata;
364 	int status;
365 
366 	if (!pdev->dev.platform_data) {
367 		dev_err(&pdev->dev, "platform_data not retrieved\n");
368 		return -ENODEV;
369 	}
370 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
371 	if (!res) {
372 		dev_err(&pdev->dev, "IORESOURCE_MEM FAILED\n");
373 		return -ENODEV;
374 	}
375 
376 	adata = devm_kzalloc(&pdev->dev, sizeof(*adata), GFP_KERNEL);
377 	if (!adata)
378 		return -ENOMEM;
379 
380 	adata->acp63_base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
381 	if (!adata->acp63_base)
382 		return -ENOMEM;
383 
384 	adata->capture_stream = NULL;
385 	adata->acp_lock = pdev->dev.platform_data;
386 	dev_set_drvdata(&pdev->dev, adata);
387 	status = devm_snd_soc_register_component(&pdev->dev,
388 						 &acp63_pdm_component,
389 						 &acp63_pdm_dai_driver, 1);
390 	if (status) {
391 		dev_err(&pdev->dev, "Fail to register acp pdm dai\n");
392 
393 		return -ENODEV;
394 	}
395 	pm_runtime_set_autosuspend_delay(&pdev->dev, ACP_SUSPEND_DELAY_MS);
396 	pm_runtime_use_autosuspend(&pdev->dev);
397 	pm_runtime_enable(&pdev->dev);
398 	pm_runtime_allow(&pdev->dev);
399 	return 0;
400 }
401 
402 static void acp63_pdm_audio_remove(struct platform_device *pdev)
403 {
404 	pm_runtime_disable(&pdev->dev);
405 }
406 
407 static int __maybe_unused acp63_pdm_resume(struct device *dev)
408 {
409 	struct pdm_dev_data *adata;
410 	struct snd_pcm_runtime *runtime;
411 	struct pdm_stream_instance *rtd;
412 	u32 period_bytes, buffer_len;
413 
414 	adata = dev_get_drvdata(dev);
415 	if (adata->capture_stream && adata->capture_stream->runtime) {
416 		runtime = adata->capture_stream->runtime;
417 		rtd = runtime->private_data;
418 		period_bytes = frames_to_bytes(runtime, runtime->period_size);
419 		buffer_len = frames_to_bytes(runtime, runtime->buffer_size);
420 		acp63_config_dma(rtd, SNDRV_PCM_STREAM_CAPTURE);
421 		acp63_init_pdm_ring_buffer(PDM_MEM_WINDOW_START, buffer_len,
422 					   period_bytes, adata->acp63_base);
423 	}
424 	acp63_enable_pdm_interrupts(adata);
425 	return 0;
426 }
427 
428 static int __maybe_unused acp63_pdm_suspend(struct device *dev)
429 {
430 	struct pdm_dev_data *adata;
431 
432 	adata = dev_get_drvdata(dev);
433 	acp63_disable_pdm_interrupts(adata);
434 	return 0;
435 }
436 
437 static int __maybe_unused acp63_pdm_runtime_resume(struct device *dev)
438 {
439 	struct pdm_dev_data *adata;
440 
441 	adata = dev_get_drvdata(dev);
442 	acp63_enable_pdm_interrupts(adata);
443 	return 0;
444 }
445 
446 static const struct dev_pm_ops acp63_pdm_pm_ops = {
447 	SET_RUNTIME_PM_OPS(acp63_pdm_suspend, acp63_pdm_runtime_resume, NULL)
448 	SET_SYSTEM_SLEEP_PM_OPS(acp63_pdm_suspend, acp63_pdm_resume)
449 };
450 
451 static struct platform_driver acp63_pdm_dma_driver = {
452 	.probe = acp63_pdm_audio_probe,
453 	.remove_new = acp63_pdm_audio_remove,
454 	.driver = {
455 		.name = "acp_ps_pdm_dma",
456 		.pm = &acp63_pdm_pm_ops,
457 	},
458 };
459 
460 module_platform_driver(acp63_pdm_dma_driver);
461 
462 MODULE_AUTHOR("Syed.SabaKareem@amd.com");
463 MODULE_DESCRIPTION("AMD PINK SARDINE PDM Driver");
464 MODULE_LICENSE("GPL v2");
465 MODULE_ALIAS("platform:" DRV_NAME);
466