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