1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // AMD ALSA SoC PDM Driver
4 //
5 //Copyright 2020 Advanced Micro Devices, Inc.
6 
7 #include <linux/platform_device.h>
8 #include <linux/module.h>
9 #include <linux/err.h>
10 #include <linux/io.h>
11 #include <linux/pm_runtime.h>
12 #include <sound/pcm_params.h>
13 #include <sound/soc.h>
14 #include <sound/soc-dai.h>
15 
16 #include "rn_acp3x.h"
17 
18 #define DRV_NAME "acp_rn_pdm_dma"
19 
20 static const struct snd_pcm_hardware acp_pdm_hardware_capture = {
21 	.info = SNDRV_PCM_INFO_INTERLEAVED |
22 		SNDRV_PCM_INFO_BLOCK_TRANSFER |
23 		SNDRV_PCM_INFO_MMAP |
24 		SNDRV_PCM_INFO_MMAP_VALID |
25 		SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
26 	.formats = SNDRV_PCM_FMTBIT_S32_LE,
27 	.channels_min = 2,
28 	.channels_max = 2,
29 	.rates = SNDRV_PCM_RATE_48000,
30 	.rate_min = 48000,
31 	.rate_max = 48000,
32 	.buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE,
33 	.period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
34 	.period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
35 	.periods_min = CAPTURE_MIN_NUM_PERIODS,
36 	.periods_max = CAPTURE_MAX_NUM_PERIODS,
37 };
38 
39 static irqreturn_t pdm_irq_handler(int irq, void *dev_id)
40 {
41 	struct pdm_dev_data *rn_pdm_data;
42 	u16 cap_flag;
43 	u32 val;
44 
45 	rn_pdm_data = dev_id;
46 	if (!rn_pdm_data)
47 		return IRQ_NONE;
48 
49 	cap_flag = 0;
50 	val = rn_readl(rn_pdm_data->acp_base + ACP_EXTERNAL_INTR_STAT);
51 	if ((val & BIT(PDM_DMA_STAT)) && rn_pdm_data->capture_stream) {
52 		rn_writel(BIT(PDM_DMA_STAT), rn_pdm_data->acp_base +
53 			  ACP_EXTERNAL_INTR_STAT);
54 		snd_pcm_period_elapsed(rn_pdm_data->capture_stream);
55 		cap_flag = 1;
56 	}
57 
58 	if (cap_flag)
59 		return IRQ_HANDLED;
60 	else
61 		return IRQ_NONE;
62 }
63 
64 static void init_pdm_ring_buffer(u32 physical_addr,
65 				 u32 buffer_size,
66 				 u32 watermark_size,
67 				 void __iomem *acp_base)
68 {
69 	rn_writel(physical_addr, acp_base + ACP_WOV_RX_RINGBUFADDR);
70 	rn_writel(buffer_size, acp_base + ACP_WOV_RX_RINGBUFSIZE);
71 	rn_writel(watermark_size, acp_base + ACP_WOV_RX_INTR_WATERMARK_SIZE);
72 	rn_writel(0x01, acp_base + ACPAXI2AXI_ATU_CTRL);
73 }
74 
75 static void enable_pdm_clock(void __iomem *acp_base)
76 {
77 	u32 pdm_clk_enable, pdm_ctrl;
78 
79 	pdm_clk_enable = ACP_PDM_CLK_FREQ_MASK;
80 	pdm_ctrl = 0x00;
81 
82 	rn_writel(pdm_clk_enable, acp_base + ACP_WOV_CLK_CTRL);
83 	pdm_ctrl = rn_readl(acp_base + ACP_WOV_MISC_CTRL);
84 	pdm_ctrl |= ACP_WOV_MISC_CTRL_MASK;
85 	rn_writel(pdm_ctrl, acp_base + ACP_WOV_MISC_CTRL);
86 }
87 
88 static void enable_pdm_interrupts(void __iomem *acp_base)
89 {
90 	u32 ext_int_ctrl;
91 
92 	ext_int_ctrl = rn_readl(acp_base + ACP_EXTERNAL_INTR_CNTL);
93 	ext_int_ctrl |= PDM_DMA_INTR_MASK;
94 	rn_writel(ext_int_ctrl, acp_base + ACP_EXTERNAL_INTR_CNTL);
95 }
96 
97 static void disable_pdm_interrupts(void __iomem *acp_base)
98 {
99 	u32 ext_int_ctrl;
100 
101 	ext_int_ctrl = rn_readl(acp_base + ACP_EXTERNAL_INTR_CNTL);
102 	ext_int_ctrl |= ~PDM_DMA_INTR_MASK;
103 	rn_writel(ext_int_ctrl, acp_base + ACP_EXTERNAL_INTR_CNTL);
104 }
105 
106 static bool check_pdm_dma_status(void __iomem *acp_base)
107 {
108 	bool pdm_dma_status;
109 	u32 pdm_enable, pdm_dma_enable;
110 
111 	pdm_dma_status = false;
112 	pdm_enable = rn_readl(acp_base + ACP_WOV_PDM_ENABLE);
113 	pdm_dma_enable = rn_readl(acp_base + ACP_WOV_PDM_DMA_ENABLE);
114 	if ((pdm_enable & ACP_PDM_ENABLE) && (pdm_dma_enable &
115 	     ACP_PDM_DMA_EN_STATUS))
116 		pdm_dma_status = true;
117 	return pdm_dma_status;
118 }
119 
120 static int start_pdm_dma(void __iomem *acp_base)
121 {
122 	u32 pdm_enable;
123 	u32 pdm_dma_enable;
124 	int timeout;
125 
126 	pdm_enable = 0x01;
127 	pdm_dma_enable  = 0x01;
128 
129 	enable_pdm_clock(acp_base);
130 	rn_writel(pdm_enable, acp_base + ACP_WOV_PDM_ENABLE);
131 	rn_writel(pdm_dma_enable, acp_base + ACP_WOV_PDM_DMA_ENABLE);
132 	pdm_dma_enable = 0x00;
133 	timeout = 0;
134 	while (++timeout < ACP_COUNTER) {
135 		pdm_dma_enable = rn_readl(acp_base + ACP_WOV_PDM_DMA_ENABLE);
136 		if ((pdm_dma_enable & 0x02) == ACP_PDM_DMA_EN_STATUS)
137 			return 0;
138 		udelay(DELAY_US);
139 	}
140 	return -ETIMEDOUT;
141 }
142 
143 static int stop_pdm_dma(void __iomem *acp_base)
144 {
145 	u32 pdm_enable, pdm_dma_enable;
146 	int timeout;
147 
148 	pdm_enable = 0x00;
149 	pdm_dma_enable  = 0x00;
150 
151 	pdm_enable = rn_readl(acp_base + ACP_WOV_PDM_ENABLE);
152 	pdm_dma_enable = rn_readl(acp_base + ACP_WOV_PDM_DMA_ENABLE);
153 	if (pdm_dma_enable & 0x01) {
154 		pdm_dma_enable = 0x02;
155 		rn_writel(pdm_dma_enable, acp_base + ACP_WOV_PDM_DMA_ENABLE);
156 		pdm_dma_enable = 0x00;
157 		timeout = 0;
158 		while (++timeout < ACP_COUNTER) {
159 			pdm_dma_enable = rn_readl(acp_base +
160 						  ACP_WOV_PDM_DMA_ENABLE);
161 			if ((pdm_dma_enable & 0x02) == 0x00)
162 				break;
163 			udelay(DELAY_US);
164 		}
165 		if (timeout == ACP_COUNTER)
166 			return -ETIMEDOUT;
167 	}
168 	if (pdm_enable == ACP_PDM_ENABLE) {
169 		pdm_enable = ACP_PDM_DISABLE;
170 		rn_writel(pdm_enable, acp_base + ACP_WOV_PDM_ENABLE);
171 	}
172 	rn_writel(0x01, acp_base + ACP_WOV_PDM_FIFO_FLUSH);
173 	return 0;
174 }
175 
176 static void config_acp_dma(struct pdm_stream_instance *rtd, int direction)
177 {
178 	u16 page_idx;
179 	u32 low, high, val;
180 	dma_addr_t addr;
181 
182 	addr = rtd->dma_addr;
183 	val = 0;
184 
185 	/* Group Enable */
186 	rn_writel(ACP_SRAM_PTE_OFFSET | BIT(31), rtd->acp_base +
187 		  ACPAXI2AXI_ATU_BASE_ADDR_GRP_1);
188 	rn_writel(PAGE_SIZE_4K_ENABLE, rtd->acp_base +
189 		  ACPAXI2AXI_ATU_PAGE_SIZE_GRP_1);
190 
191 	for (page_idx = 0; page_idx < rtd->num_pages; page_idx++) {
192 		/* Load the low address of page int ACP SRAM through SRBM */
193 		low = lower_32_bits(addr);
194 		high = upper_32_bits(addr);
195 
196 		rn_writel(low, rtd->acp_base + ACP_SCRATCH_REG_0 + val);
197 		high |= BIT(31);
198 		rn_writel(high, rtd->acp_base + ACP_SCRATCH_REG_0 + val + 4);
199 		val += 8;
200 		addr += PAGE_SIZE;
201 	}
202 }
203 
204 static int acp_pdm_dma_open(struct snd_soc_component *component,
205 			    struct snd_pcm_substream *substream)
206 {
207 	struct snd_pcm_runtime *runtime;
208 	struct pdm_dev_data *adata;
209 	struct pdm_stream_instance *pdm_data;
210 	int ret;
211 
212 	runtime = substream->runtime;
213 	adata = dev_get_drvdata(component->dev);
214 	pdm_data = kzalloc(sizeof(*pdm_data), GFP_KERNEL);
215 	if (!pdm_data)
216 		return -EINVAL;
217 
218 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
219 		runtime->hw = acp_pdm_hardware_capture;
220 
221 	ret = snd_pcm_hw_constraint_integer(runtime,
222 					    SNDRV_PCM_HW_PARAM_PERIODS);
223 	if (ret < 0) {
224 		dev_err(component->dev, "set integer constraint failed\n");
225 		kfree(pdm_data);
226 		return ret;
227 	}
228 
229 	enable_pdm_interrupts(adata->acp_base);
230 
231 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
232 		adata->capture_stream = substream;
233 
234 	pdm_data->acp_base = adata->acp_base;
235 	runtime->private_data = pdm_data;
236 	return ret;
237 }
238 
239 static int acp_pdm_dma_hw_params(struct snd_soc_component *component,
240 				 struct snd_pcm_substream *substream,
241 				 struct snd_pcm_hw_params *params)
242 {
243 	struct pdm_stream_instance *rtd;
244 	size_t size, period_bytes;
245 
246 	rtd = substream->runtime->private_data;
247 	if (!rtd)
248 		return -EINVAL;
249 	size = params_buffer_bytes(params);
250 	period_bytes = params_period_bytes(params);
251 	rtd->dma_addr = substream->dma_buffer.addr;
252 	rtd->num_pages = (PAGE_ALIGN(size) >> PAGE_SHIFT);
253 	config_acp_dma(rtd, substream->stream);
254 	init_pdm_ring_buffer(MEM_WINDOW_START, size, period_bytes,
255 			     rtd->acp_base);
256 	return 0;
257 }
258 
259 static u64 acp_pdm_get_byte_count(struct pdm_stream_instance *rtd,
260 				  int direction)
261 {
262 	union acp_pdm_dma_count byte_count;
263 
264 	byte_count.bcount.high =
265 			rn_readl(rtd->acp_base +
266 				 ACP_WOV_RX_LINEARPOSITIONCNTR_HIGH);
267 	byte_count.bcount.low =
268 			rn_readl(rtd->acp_base +
269 				 ACP_WOV_RX_LINEARPOSITIONCNTR_LOW);
270 	return byte_count.bytescount;
271 }
272 
273 static snd_pcm_uframes_t acp_pdm_dma_pointer(struct snd_soc_component *comp,
274 					     struct snd_pcm_substream *stream)
275 {
276 	struct pdm_stream_instance *rtd;
277 	u32 pos, buffersize;
278 	u64 bytescount;
279 
280 	rtd = stream->runtime->private_data;
281 	buffersize = frames_to_bytes(stream->runtime,
282 				     stream->runtime->buffer_size);
283 	bytescount = acp_pdm_get_byte_count(rtd, stream->stream);
284 	if (bytescount > rtd->bytescount)
285 		bytescount -= rtd->bytescount;
286 	pos = do_div(bytescount, buffersize);
287 	return bytes_to_frames(stream->runtime, pos);
288 }
289 
290 static int acp_pdm_dma_new(struct snd_soc_component *component,
291 			   struct snd_soc_pcm_runtime *rtd)
292 {
293 	struct device *parent = component->dev->parent;
294 
295 	snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV,
296 				       parent, MIN_BUFFER, MAX_BUFFER);
297 	return 0;
298 }
299 
300 static int acp_pdm_dma_mmap(struct snd_soc_component *component,
301 			    struct snd_pcm_substream *substream,
302 			    struct vm_area_struct *vma)
303 {
304 	return snd_pcm_lib_default_mmap(substream, vma);
305 }
306 
307 static int acp_pdm_dma_close(struct snd_soc_component *component,
308 			     struct snd_pcm_substream *substream)
309 {
310 	struct pdm_dev_data *adata = dev_get_drvdata(component->dev);
311 
312 	disable_pdm_interrupts(adata->acp_base);
313 	adata->capture_stream = NULL;
314 	return 0;
315 }
316 
317 static int acp_pdm_dai_hw_params(struct snd_pcm_substream *substream,
318 				 struct snd_pcm_hw_params *params,
319 				 struct snd_soc_dai *dai)
320 {
321 	struct pdm_stream_instance *rtd;
322 	unsigned int ch_mask;
323 
324 	rtd = substream->runtime->private_data;
325 	switch (params_channels(params)) {
326 	case TWO_CH:
327 		ch_mask = 0x00;
328 		break;
329 	default:
330 		return -EINVAL;
331 	}
332 	rn_writel(ch_mask, rtd->acp_base + ACP_WOV_PDM_NO_OF_CHANNELS);
333 	rn_writel(PDM_DECIMATION_FACTOR, rtd->acp_base +
334 		  ACP_WOV_PDM_DECIMATION_FACTOR);
335 	return 0;
336 }
337 
338 static int acp_pdm_dai_trigger(struct snd_pcm_substream *substream,
339 			       int cmd, struct snd_soc_dai *dai)
340 {
341 	struct pdm_stream_instance *rtd;
342 	int ret;
343 	bool pdm_status;
344 
345 	rtd = substream->runtime->private_data;
346 	ret = 0;
347 	switch (cmd) {
348 	case SNDRV_PCM_TRIGGER_START:
349 	case SNDRV_PCM_TRIGGER_RESUME:
350 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
351 		rtd->bytescount = acp_pdm_get_byte_count(rtd,
352 							 substream->stream);
353 		pdm_status = check_pdm_dma_status(rtd->acp_base);
354 		if (!pdm_status)
355 			ret = start_pdm_dma(rtd->acp_base);
356 		break;
357 	case SNDRV_PCM_TRIGGER_STOP:
358 	case SNDRV_PCM_TRIGGER_SUSPEND:
359 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
360 		pdm_status = check_pdm_dma_status(rtd->acp_base);
361 		if (pdm_status)
362 			ret = stop_pdm_dma(rtd->acp_base);
363 		break;
364 	default:
365 		ret = -EINVAL;
366 		break;
367 	}
368 	return ret;
369 }
370 
371 static struct snd_soc_dai_ops acp_pdm_dai_ops = {
372 	.hw_params = acp_pdm_dai_hw_params,
373 	.trigger   = acp_pdm_dai_trigger,
374 };
375 
376 static struct snd_soc_dai_driver acp_pdm_dai_driver = {
377 	.capture = {
378 		.rates = SNDRV_PCM_RATE_48000,
379 		.formats = SNDRV_PCM_FMTBIT_S24_LE |
380 			   SNDRV_PCM_FMTBIT_S32_LE,
381 		.channels_min = 2,
382 		.channels_max = 2,
383 		.rate_min = 48000,
384 		.rate_max = 48000,
385 	},
386 	.ops = &acp_pdm_dai_ops,
387 };
388 
389 static const struct snd_soc_component_driver acp_pdm_component = {
390 	.name		= DRV_NAME,
391 	.open		= acp_pdm_dma_open,
392 	.close		= acp_pdm_dma_close,
393 	.hw_params	= acp_pdm_dma_hw_params,
394 	.pointer	= acp_pdm_dma_pointer,
395 	.mmap		= acp_pdm_dma_mmap,
396 	.pcm_construct	= acp_pdm_dma_new,
397 };
398 
399 static int acp_pdm_audio_probe(struct platform_device *pdev)
400 {
401 	struct resource *res;
402 	struct pdm_dev_data *adata;
403 	unsigned int irqflags;
404 	int status;
405 
406 	if (!pdev->dev.platform_data) {
407 		dev_err(&pdev->dev, "platform_data not retrieved\n");
408 		return -ENODEV;
409 	}
410 	irqflags = *((unsigned int *)(pdev->dev.platform_data));
411 
412 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
413 	if (!res) {
414 		dev_err(&pdev->dev, "IORESOURCE_MEM FAILED\n");
415 		return -ENODEV;
416 	}
417 
418 	adata = devm_kzalloc(&pdev->dev, sizeof(*adata), GFP_KERNEL);
419 	if (!adata)
420 		return -ENOMEM;
421 
422 	adata->acp_base = devm_ioremap(&pdev->dev, res->start,
423 				       resource_size(res));
424 	if (!adata->acp_base)
425 		return -ENOMEM;
426 
427 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
428 	if (!res) {
429 		dev_err(&pdev->dev, "IORESOURCE_IRQ FAILED\n");
430 		return -ENODEV;
431 	}
432 
433 	adata->pdm_irq = res->start;
434 	adata->capture_stream = NULL;
435 
436 	dev_set_drvdata(&pdev->dev, adata);
437 	status = devm_snd_soc_register_component(&pdev->dev,
438 						 &acp_pdm_component,
439 						 &acp_pdm_dai_driver, 1);
440 	if (status) {
441 		dev_err(&pdev->dev, "Fail to register acp pdm dai\n");
442 
443 		return -ENODEV;
444 	}
445 	status = devm_request_irq(&pdev->dev, adata->pdm_irq, pdm_irq_handler,
446 				  irqflags, "ACP_PDM_IRQ", adata);
447 	if (status) {
448 		dev_err(&pdev->dev, "ACP PDM IRQ request failed\n");
449 		return -ENODEV;
450 	}
451 	pm_runtime_set_autosuspend_delay(&pdev->dev, ACP_SUSPEND_DELAY_MS);
452 	pm_runtime_use_autosuspend(&pdev->dev);
453 	pm_runtime_enable(&pdev->dev);
454 	pm_runtime_allow(&pdev->dev);
455 	return 0;
456 }
457 
458 static int acp_pdm_audio_remove(struct platform_device *pdev)
459 {
460 	pm_runtime_disable(&pdev->dev);
461 	return 0;
462 }
463 
464 static int acp_pdm_resume(struct device *dev)
465 {
466 	struct pdm_dev_data *adata;
467 	struct snd_pcm_runtime *runtime;
468 	struct pdm_stream_instance *rtd;
469 	u32 period_bytes, buffer_len;
470 
471 	adata = dev_get_drvdata(dev);
472 	if (adata->capture_stream && adata->capture_stream->runtime) {
473 		runtime = adata->capture_stream->runtime;
474 		rtd = runtime->private_data;
475 		period_bytes = frames_to_bytes(runtime, runtime->period_size);
476 		buffer_len = frames_to_bytes(runtime, runtime->buffer_size);
477 		config_acp_dma(rtd, SNDRV_PCM_STREAM_CAPTURE);
478 		init_pdm_ring_buffer(MEM_WINDOW_START, buffer_len, period_bytes,
479 				     adata->acp_base);
480 	}
481 	enable_pdm_interrupts(adata->acp_base);
482 	return 0;
483 }
484 
485 static int acp_pdm_runtime_suspend(struct device *dev)
486 {
487 	struct pdm_dev_data *adata;
488 
489 	adata = dev_get_drvdata(dev);
490 	disable_pdm_interrupts(adata->acp_base);
491 
492 	return 0;
493 }
494 
495 static int acp_pdm_runtime_resume(struct device *dev)
496 {
497 	struct pdm_dev_data *adata;
498 
499 	adata = dev_get_drvdata(dev);
500 	enable_pdm_interrupts(adata->acp_base);
501 	return 0;
502 }
503 
504 static const struct dev_pm_ops acp_pdm_pm_ops = {
505 	.runtime_suspend = acp_pdm_runtime_suspend,
506 	.runtime_resume = acp_pdm_runtime_resume,
507 	.resume = acp_pdm_resume,
508 };
509 
510 static struct platform_driver acp_pdm_dma_driver = {
511 	.probe = acp_pdm_audio_probe,
512 	.remove = acp_pdm_audio_remove,
513 	.driver = {
514 		.name = "acp_rn_pdm_dma",
515 		.pm = &acp_pdm_pm_ops,
516 	},
517 };
518 
519 module_platform_driver(acp_pdm_dma_driver);
520 
521 MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
522 MODULE_DESCRIPTION("AMD ACP3x Renior PDM Driver");
523 MODULE_LICENSE("GPL v2");
524 MODULE_ALIAS("platform:" DRV_NAME);
525