1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * AMD Pink Sardine ACP PCI Driver 4 * 5 * Copyright 2022 Advanced Micro Devices, Inc. 6 */ 7 8 #include <linux/pci.h> 9 #include <linux/bitops.h> 10 #include <linux/module.h> 11 #include <linux/io.h> 12 #include <linux/delay.h> 13 #include <linux/platform_device.h> 14 #include <linux/acpi.h> 15 #include <linux/interrupt.h> 16 #include <sound/pcm_params.h> 17 #include <linux/pm_runtime.h> 18 #include <linux/iopoll.h> 19 #include <linux/soundwire/sdw_amd.h> 20 #include "../mach-config.h" 21 22 #include "acp63.h" 23 24 static int acp63_power_on(void __iomem *acp_base) 25 { 26 u32 val; 27 28 val = readl(acp_base + ACP_PGFSM_STATUS); 29 30 if (!val) 31 return val; 32 33 if ((val & ACP_PGFSM_STATUS_MASK) != ACP_POWER_ON_IN_PROGRESS) 34 writel(ACP_PGFSM_CNTL_POWER_ON_MASK, acp_base + ACP_PGFSM_CONTROL); 35 36 return readl_poll_timeout(acp_base + ACP_PGFSM_STATUS, val, !val, DELAY_US, ACP_TIMEOUT); 37 } 38 39 static int acp63_reset(void __iomem *acp_base) 40 { 41 u32 val; 42 int ret; 43 44 writel(1, acp_base + ACP_SOFT_RESET); 45 46 ret = readl_poll_timeout(acp_base + ACP_SOFT_RESET, val, 47 val & ACP_SOFT_RESET_SOFTRESET_AUDDONE_MASK, 48 DELAY_US, ACP_TIMEOUT); 49 if (ret) 50 return ret; 51 52 writel(0, acp_base + ACP_SOFT_RESET); 53 54 return readl_poll_timeout(acp_base + ACP_SOFT_RESET, val, !val, DELAY_US, ACP_TIMEOUT); 55 } 56 57 static void acp63_enable_interrupts(void __iomem *acp_base) 58 { 59 writel(1, acp_base + ACP_EXTERNAL_INTR_ENB); 60 writel(ACP_ERROR_IRQ, acp_base + ACP_EXTERNAL_INTR_CNTL); 61 } 62 63 static void acp63_disable_interrupts(void __iomem *acp_base) 64 { 65 writel(ACP_EXT_INTR_STAT_CLEAR_MASK, acp_base + ACP_EXTERNAL_INTR_STAT); 66 writel(0, acp_base + ACP_EXTERNAL_INTR_CNTL); 67 writel(0, acp_base + ACP_EXTERNAL_INTR_ENB); 68 } 69 70 static int acp63_init(void __iomem *acp_base, struct device *dev) 71 { 72 int ret; 73 74 ret = acp63_power_on(acp_base); 75 if (ret) { 76 dev_err(dev, "ACP power on failed\n"); 77 return ret; 78 } 79 writel(0x01, acp_base + ACP_CONTROL); 80 ret = acp63_reset(acp_base); 81 if (ret) { 82 dev_err(dev, "ACP reset failed\n"); 83 return ret; 84 } 85 acp63_enable_interrupts(acp_base); 86 return 0; 87 } 88 89 static int acp63_deinit(void __iomem *acp_base, struct device *dev) 90 { 91 int ret; 92 93 acp63_disable_interrupts(acp_base); 94 ret = acp63_reset(acp_base); 95 if (ret) { 96 dev_err(dev, "ACP reset failed\n"); 97 return ret; 98 } 99 writel(0, acp_base + ACP_CONTROL); 100 return 0; 101 } 102 103 static irqreturn_t acp63_irq_thread(int irq, void *context) 104 { 105 struct sdw_dma_dev_data *sdw_dma_data; 106 struct acp63_dev_data *adata = context; 107 u32 stream_index; 108 109 sdw_dma_data = dev_get_drvdata(&adata->sdw_dma_dev->dev); 110 111 for (stream_index = 0; stream_index < ACP63_SDW0_DMA_MAX_STREAMS; stream_index++) { 112 if (adata->sdw0_dma_intr_stat[stream_index]) { 113 if (sdw_dma_data->sdw0_dma_stream[stream_index]) 114 snd_pcm_period_elapsed(sdw_dma_data->sdw0_dma_stream[stream_index]); 115 adata->sdw0_dma_intr_stat[stream_index] = 0; 116 } 117 } 118 for (stream_index = 0; stream_index < ACP63_SDW1_DMA_MAX_STREAMS; stream_index++) { 119 if (adata->sdw1_dma_intr_stat[stream_index]) { 120 if (sdw_dma_data->sdw1_dma_stream[stream_index]) 121 snd_pcm_period_elapsed(sdw_dma_data->sdw1_dma_stream[stream_index]); 122 adata->sdw1_dma_intr_stat[stream_index] = 0; 123 } 124 } 125 return IRQ_HANDLED; 126 } 127 128 static irqreturn_t acp63_irq_handler(int irq, void *dev_id) 129 { 130 struct acp63_dev_data *adata; 131 struct pdm_dev_data *ps_pdm_data; 132 struct amd_sdw_manager *amd_manager; 133 u32 ext_intr_stat, ext_intr_stat1; 134 u32 stream_id = 0; 135 u16 irq_flag = 0; 136 u16 sdw_dma_irq_flag = 0; 137 u16 index; 138 139 adata = dev_id; 140 if (!adata) 141 return IRQ_NONE; 142 /* ACP interrupts will be cleared by reading particular bit and writing 143 * same value to the status register. writing zero's doesn't have any 144 * effect. 145 * Bit by bit checking of IRQ field is implemented. 146 */ 147 ext_intr_stat = readl(adata->acp63_base + ACP_EXTERNAL_INTR_STAT); 148 if (ext_intr_stat & ACP_SDW0_STAT) { 149 writel(ACP_SDW0_STAT, adata->acp63_base + ACP_EXTERNAL_INTR_STAT); 150 amd_manager = dev_get_drvdata(&adata->sdw->pdev[0]->dev); 151 if (amd_manager) 152 schedule_work(&amd_manager->amd_sdw_irq_thread); 153 irq_flag = 1; 154 } 155 156 ext_intr_stat1 = readl(adata->acp63_base + ACP_EXTERNAL_INTR_STAT1); 157 if (ext_intr_stat1 & ACP_SDW1_STAT) { 158 writel(ACP_SDW1_STAT, adata->acp63_base + ACP_EXTERNAL_INTR_STAT1); 159 amd_manager = dev_get_drvdata(&adata->sdw->pdev[1]->dev); 160 if (amd_manager) 161 schedule_work(&amd_manager->amd_sdw_irq_thread); 162 irq_flag = 1; 163 } 164 165 if (ext_intr_stat & ACP_ERROR_IRQ) { 166 writel(ACP_ERROR_IRQ, adata->acp63_base + ACP_EXTERNAL_INTR_STAT); 167 /* TODO: Report SoundWire Manager instance errors */ 168 writel(0, adata->acp63_base + ACP_SW0_I2S_ERROR_REASON); 169 writel(0, adata->acp63_base + ACP_SW1_I2S_ERROR_REASON); 170 writel(0, adata->acp63_base + ACP_ERROR_STATUS); 171 irq_flag = 1; 172 } 173 174 if (ext_intr_stat & BIT(PDM_DMA_STAT)) { 175 ps_pdm_data = dev_get_drvdata(&adata->pdm_dev->dev); 176 writel(BIT(PDM_DMA_STAT), adata->acp63_base + ACP_EXTERNAL_INTR_STAT); 177 if (ps_pdm_data->capture_stream) 178 snd_pcm_period_elapsed(ps_pdm_data->capture_stream); 179 irq_flag = 1; 180 } 181 if (ext_intr_stat & ACP_SDW_DMA_IRQ_MASK) { 182 for (index = ACP_AUDIO2_RX_THRESHOLD; index <= ACP_AUDIO0_TX_THRESHOLD; index++) { 183 if (ext_intr_stat & BIT(index)) { 184 writel(BIT(index), adata->acp63_base + ACP_EXTERNAL_INTR_STAT); 185 switch (index) { 186 case ACP_AUDIO0_TX_THRESHOLD: 187 stream_id = ACP_SDW0_AUDIO0_TX; 188 break; 189 case ACP_AUDIO1_TX_THRESHOLD: 190 stream_id = ACP_SDW0_AUDIO1_TX; 191 break; 192 case ACP_AUDIO2_TX_THRESHOLD: 193 stream_id = ACP_SDW0_AUDIO2_TX; 194 break; 195 case ACP_AUDIO0_RX_THRESHOLD: 196 stream_id = ACP_SDW0_AUDIO0_RX; 197 break; 198 case ACP_AUDIO1_RX_THRESHOLD: 199 stream_id = ACP_SDW0_AUDIO1_RX; 200 break; 201 case ACP_AUDIO2_RX_THRESHOLD: 202 stream_id = ACP_SDW0_AUDIO2_RX; 203 break; 204 } 205 206 adata->sdw0_dma_intr_stat[stream_id] = 1; 207 sdw_dma_irq_flag = 1; 208 } 209 } 210 } 211 212 if (ext_intr_stat1 & ACP_P1_AUDIO1_RX_THRESHOLD) { 213 writel(ACP_P1_AUDIO1_RX_THRESHOLD, 214 adata->acp63_base + ACP_EXTERNAL_INTR_STAT1); 215 adata->sdw1_dma_intr_stat[ACP_SDW1_AUDIO1_RX] = 1; 216 sdw_dma_irq_flag = 1; 217 } 218 219 if (ext_intr_stat1 & ACP_P1_AUDIO1_TX_THRESHOLD) { 220 writel(ACP_P1_AUDIO1_TX_THRESHOLD, 221 adata->acp63_base + ACP_EXTERNAL_INTR_STAT1); 222 adata->sdw1_dma_intr_stat[ACP_SDW1_AUDIO1_TX] = 1; 223 sdw_dma_irq_flag = 1; 224 } 225 226 if (sdw_dma_irq_flag) 227 return IRQ_WAKE_THREAD; 228 229 if (irq_flag) 230 return IRQ_HANDLED; 231 else 232 return IRQ_NONE; 233 } 234 235 #if IS_ENABLED(CONFIG_SND_SOC_AMD_SOUNDWIRE) 236 static int acp_scan_sdw_devices(struct device *dev, u64 addr) 237 { 238 struct acpi_device *sdw_dev; 239 struct acp63_dev_data *acp_data; 240 241 acp_data = dev_get_drvdata(dev); 242 if (!addr) 243 return -ENODEV; 244 245 sdw_dev = acpi_find_child_device(ACPI_COMPANION(dev), addr, 0); 246 if (!sdw_dev) 247 return -ENODEV; 248 249 acp_data->info.handle = sdw_dev->handle; 250 acp_data->info.count = AMD_SDW_MAX_MANAGERS; 251 return amd_sdw_scan_controller(&acp_data->info); 252 } 253 254 static int amd_sdw_probe(struct device *dev) 255 { 256 struct acp63_dev_data *acp_data; 257 struct sdw_amd_res sdw_res; 258 int ret; 259 260 acp_data = dev_get_drvdata(dev); 261 memset(&sdw_res, 0, sizeof(sdw_res)); 262 sdw_res.addr = acp_data->addr; 263 sdw_res.reg_range = acp_data->reg_range; 264 sdw_res.handle = acp_data->info.handle; 265 sdw_res.parent = dev; 266 sdw_res.dev = dev; 267 sdw_res.acp_lock = &acp_data->acp_lock; 268 sdw_res.count = acp_data->info.count; 269 sdw_res.mmio_base = acp_data->acp63_base; 270 sdw_res.acp_rev = acp_data->acp_rev; 271 sdw_res.link_mask = acp_data->info.link_mask; 272 ret = sdw_amd_probe(&sdw_res, &acp_data->sdw); 273 if (ret) 274 dev_err(dev, "error: SoundWire probe failed\n"); 275 return ret; 276 } 277 278 static int amd_sdw_exit(struct acp63_dev_data *acp_data) 279 { 280 if (acp_data->sdw) 281 sdw_amd_exit(acp_data->sdw); 282 acp_data->sdw = NULL; 283 284 return 0; 285 } 286 287 static struct snd_soc_acpi_mach *acp63_sdw_machine_select(struct device *dev) 288 { 289 struct snd_soc_acpi_mach *mach; 290 const struct snd_soc_acpi_link_adr *link; 291 struct acp63_dev_data *acp_data = dev_get_drvdata(dev); 292 int ret, i; 293 294 if (acp_data->info.count) { 295 ret = sdw_amd_get_slave_info(acp_data->sdw); 296 if (ret) { 297 dev_dbg(dev, "failed to read slave information\n"); 298 return NULL; 299 } 300 for (mach = acp_data->machines; mach; mach++) { 301 if (!mach->links) 302 break; 303 link = mach->links; 304 for (i = 0; i < acp_data->info.count && link->num_adr; link++, i++) { 305 if (!snd_soc_acpi_sdw_link_slaves_found(dev, link, 306 acp_data->sdw->peripherals)) 307 break; 308 } 309 if (i == acp_data->info.count || !link->num_adr) 310 break; 311 } 312 if (mach && mach->link_mask) { 313 mach->mach_params.links = mach->links; 314 mach->mach_params.link_mask = mach->link_mask; 315 return mach; 316 } 317 } 318 dev_dbg(dev, "No SoundWire machine driver found\n"); 319 return NULL; 320 } 321 #else 322 static int acp_scan_sdw_devices(struct device *dev, u64 addr) 323 { 324 return 0; 325 } 326 327 static int amd_sdw_probe(struct device *dev) 328 { 329 return 0; 330 } 331 332 static int amd_sdw_exit(struct acp63_dev_data *acp_data) 333 { 334 return 0; 335 } 336 337 static struct snd_soc_acpi_mach *acp63_sdw_machine_select(struct device *dev) 338 { 339 return NULL; 340 } 341 #endif 342 343 static int acp63_machine_register(struct device *dev) 344 { 345 struct snd_soc_acpi_mach *mach; 346 struct acp63_dev_data *adata = dev_get_drvdata(dev); 347 int size; 348 349 if (adata->is_sdw_dev && adata->is_sdw_config) { 350 size = sizeof(*adata->machines); 351 mach = acp63_sdw_machine_select(dev); 352 if (mach) { 353 adata->mach_dev = platform_device_register_data(dev, mach->drv_name, 354 PLATFORM_DEVID_NONE, mach, 355 size); 356 if (IS_ERR(adata->mach_dev)) { 357 dev_err(dev, 358 "cannot register Machine device for SoundWire Interface\n"); 359 return PTR_ERR(adata->mach_dev); 360 } 361 } 362 363 } else if (adata->is_pdm_dev && !adata->is_sdw_dev && adata->is_pdm_config) { 364 adata->mach_dev = platform_device_register_data(dev, "acp_ps_mach", 365 PLATFORM_DEVID_NONE, NULL, 0); 366 if (IS_ERR(adata->mach_dev)) { 367 dev_err(dev, "cannot register amd_ps_mach device\n"); 368 return PTR_ERR(adata->mach_dev); 369 } 370 } 371 return 0; 372 } 373 374 static int get_acp63_device_config(struct pci_dev *pci, struct acp63_dev_data *acp_data) 375 { 376 struct acpi_device *pdm_dev; 377 const union acpi_object *obj; 378 acpi_handle handle; 379 acpi_integer dmic_status; 380 u32 config; 381 bool is_dmic_dev = false; 382 bool is_sdw_dev = false; 383 bool wov_en, dmic_en; 384 int ret; 385 386 /* IF WOV entry not found, enable dmic based on acp-audio-device-type entry*/ 387 wov_en = true; 388 dmic_en = false; 389 390 config = readl(acp_data->acp63_base + ACP_PIN_CONFIG); 391 switch (config) { 392 case ACP_CONFIG_4: 393 case ACP_CONFIG_5: 394 case ACP_CONFIG_10: 395 case ACP_CONFIG_11: 396 acp_data->is_pdm_config = true; 397 break; 398 case ACP_CONFIG_2: 399 case ACP_CONFIG_3: 400 acp_data->is_sdw_config = true; 401 break; 402 case ACP_CONFIG_6: 403 case ACP_CONFIG_7: 404 case ACP_CONFIG_12: 405 case ACP_CONFIG_8: 406 case ACP_CONFIG_13: 407 case ACP_CONFIG_14: 408 acp_data->is_pdm_config = true; 409 acp_data->is_sdw_config = true; 410 break; 411 default: 412 break; 413 } 414 415 if (acp_data->is_pdm_config) { 416 pdm_dev = acpi_find_child_device(ACPI_COMPANION(&pci->dev), ACP63_DMIC_ADDR, 0); 417 if (pdm_dev) { 418 /* is_dmic_dev flag will be set when ACP PDM controller device exists */ 419 if (!acpi_dev_get_property(pdm_dev, "acp-audio-device-type", 420 ACPI_TYPE_INTEGER, &obj) && 421 obj->integer.value == ACP_DMIC_DEV) 422 dmic_en = true; 423 } 424 425 handle = ACPI_HANDLE(&pci->dev); 426 ret = acpi_evaluate_integer(handle, "_WOV", NULL, &dmic_status); 427 if (!ACPI_FAILURE(ret)) 428 wov_en = dmic_status; 429 } 430 431 if (dmic_en && wov_en) 432 is_dmic_dev = true; 433 434 if (acp_data->is_sdw_config) { 435 ret = acp_scan_sdw_devices(&pci->dev, ACP63_SDW_ADDR); 436 if (!ret && acp_data->info.link_mask) 437 is_sdw_dev = true; 438 } 439 440 acp_data->is_pdm_dev = is_dmic_dev; 441 acp_data->is_sdw_dev = is_sdw_dev; 442 if (!is_dmic_dev && !is_sdw_dev) { 443 dev_dbg(&pci->dev, "No PDM or SoundWire manager devices found\n"); 444 return -ENODEV; 445 } 446 return 0; 447 } 448 449 static void acp63_fill_platform_dev_info(struct platform_device_info *pdevinfo, 450 struct device *parent, 451 struct fwnode_handle *fw_node, 452 char *name, unsigned int id, 453 const struct resource *res, 454 unsigned int num_res, 455 const void *data, 456 size_t size_data) 457 { 458 pdevinfo->name = name; 459 pdevinfo->id = id; 460 pdevinfo->parent = parent; 461 pdevinfo->num_res = num_res; 462 pdevinfo->res = res; 463 pdevinfo->data = data; 464 pdevinfo->size_data = size_data; 465 pdevinfo->fwnode = fw_node; 466 } 467 468 static int create_acp63_platform_devs(struct pci_dev *pci, struct acp63_dev_data *adata, u32 addr) 469 { 470 struct platform_device_info pdevinfo; 471 struct device *parent; 472 int ret; 473 474 parent = &pci->dev; 475 476 if (adata->is_sdw_dev || adata->is_pdm_dev) { 477 adata->res = devm_kzalloc(&pci->dev, sizeof(struct resource), GFP_KERNEL); 478 if (!adata->res) { 479 ret = -ENOMEM; 480 goto de_init; 481 } 482 adata->res->flags = IORESOURCE_MEM; 483 adata->res->start = addr; 484 adata->res->end = addr + (ACP63_REG_END - ACP63_REG_START); 485 memset(&pdevinfo, 0, sizeof(pdevinfo)); 486 } 487 488 if (adata->is_pdm_dev && adata->is_pdm_config) { 489 acp63_fill_platform_dev_info(&pdevinfo, parent, NULL, "acp_ps_pdm_dma", 490 0, adata->res, 1, NULL, 0); 491 492 adata->pdm_dev = platform_device_register_full(&pdevinfo); 493 if (IS_ERR(adata->pdm_dev)) { 494 dev_err(&pci->dev, 495 "cannot register %s device\n", pdevinfo.name); 496 ret = PTR_ERR(adata->pdm_dev); 497 goto de_init; 498 } 499 memset(&pdevinfo, 0, sizeof(pdevinfo)); 500 acp63_fill_platform_dev_info(&pdevinfo, parent, NULL, "dmic-codec", 501 0, NULL, 0, NULL, 0); 502 adata->dmic_codec_dev = platform_device_register_full(&pdevinfo); 503 if (IS_ERR(adata->dmic_codec_dev)) { 504 dev_err(&pci->dev, 505 "cannot register %s device\n", pdevinfo.name); 506 ret = PTR_ERR(adata->dmic_codec_dev); 507 goto unregister_pdm_dev; 508 } 509 } 510 if (adata->is_sdw_dev && adata->is_sdw_config) { 511 ret = amd_sdw_probe(&pci->dev); 512 if (ret) { 513 if (adata->is_pdm_dev) 514 goto unregister_dmic_codec_dev; 515 else 516 goto de_init; 517 } 518 memset(&pdevinfo, 0, sizeof(pdevinfo)); 519 acp63_fill_platform_dev_info(&pdevinfo, parent, NULL, "amd_ps_sdw_dma", 520 0, adata->res, 1, NULL, 0); 521 522 adata->sdw_dma_dev = platform_device_register_full(&pdevinfo); 523 if (IS_ERR(adata->sdw_dma_dev)) { 524 dev_err(&pci->dev, 525 "cannot register %s device\n", pdevinfo.name); 526 ret = PTR_ERR(adata->sdw_dma_dev); 527 if (adata->is_pdm_dev) 528 goto unregister_dmic_codec_dev; 529 else 530 goto de_init; 531 } 532 } 533 534 return 0; 535 unregister_dmic_codec_dev: 536 platform_device_unregister(adata->dmic_codec_dev); 537 unregister_pdm_dev: 538 platform_device_unregister(adata->pdm_dev); 539 de_init: 540 if (acp63_deinit(adata->acp63_base, &pci->dev)) 541 dev_err(&pci->dev, "ACP de-init failed\n"); 542 return ret; 543 } 544 545 static int snd_acp63_probe(struct pci_dev *pci, 546 const struct pci_device_id *pci_id) 547 { 548 struct acp63_dev_data *adata; 549 u32 addr; 550 u32 irqflags, flag; 551 int ret; 552 553 irqflags = IRQF_SHARED; 554 555 /* Return if acp config flag is defined */ 556 flag = snd_amd_acp_find_config(pci); 557 if (flag) 558 return -ENODEV; 559 560 /* Pink Sardine device check */ 561 switch (pci->revision) { 562 case 0x63: 563 break; 564 default: 565 dev_dbg(&pci->dev, "acp63 pci device not found\n"); 566 return -ENODEV; 567 } 568 if (pci_enable_device(pci)) { 569 dev_err(&pci->dev, "pci_enable_device failed\n"); 570 return -ENODEV; 571 } 572 573 ret = pci_request_regions(pci, "AMD ACP6.2 audio"); 574 if (ret < 0) { 575 dev_err(&pci->dev, "pci_request_regions failed\n"); 576 goto disable_pci; 577 } 578 adata = devm_kzalloc(&pci->dev, sizeof(struct acp63_dev_data), 579 GFP_KERNEL); 580 if (!adata) { 581 ret = -ENOMEM; 582 goto release_regions; 583 } 584 585 addr = pci_resource_start(pci, 0); 586 adata->acp63_base = devm_ioremap(&pci->dev, addr, 587 pci_resource_len(pci, 0)); 588 if (!adata->acp63_base) { 589 ret = -ENOMEM; 590 goto release_regions; 591 } 592 adata->addr = addr; 593 adata->reg_range = ACP63_REG_END - ACP63_REG_START; 594 adata->acp_rev = pci->revision; 595 pci_set_master(pci); 596 pci_set_drvdata(pci, adata); 597 mutex_init(&adata->acp_lock); 598 ret = acp63_init(adata->acp63_base, &pci->dev); 599 if (ret) 600 goto release_regions; 601 ret = devm_request_threaded_irq(&pci->dev, pci->irq, acp63_irq_handler, 602 acp63_irq_thread, irqflags, "ACP_PCI_IRQ", adata); 603 if (ret) { 604 dev_err(&pci->dev, "ACP PCI IRQ request failed\n"); 605 goto de_init; 606 } 607 ret = get_acp63_device_config(pci, adata); 608 /* ACP PCI driver probe should be continued even PDM or SoundWire Devices are not found */ 609 if (ret) { 610 dev_dbg(&pci->dev, "get acp device config failed:%d\n", ret); 611 goto skip_pdev_creation; 612 } 613 ret = create_acp63_platform_devs(pci, adata, addr); 614 if (ret < 0) { 615 dev_err(&pci->dev, "ACP platform devices creation failed\n"); 616 goto de_init; 617 } 618 adata->machines = snd_soc_acpi_amd_acp63_sdw_machines; 619 ret = acp63_machine_register(&pci->dev); 620 if (ret) { 621 dev_err(&pci->dev, "ACP machine register failed\n"); 622 goto de_init; 623 } 624 skip_pdev_creation: 625 device_set_wakeup_enable(&pci->dev, true); 626 pm_runtime_set_autosuspend_delay(&pci->dev, ACP_SUSPEND_DELAY_MS); 627 pm_runtime_use_autosuspend(&pci->dev); 628 pm_runtime_put_noidle(&pci->dev); 629 pm_runtime_allow(&pci->dev); 630 return 0; 631 de_init: 632 if (acp63_deinit(adata->acp63_base, &pci->dev)) 633 dev_err(&pci->dev, "ACP de-init failed\n"); 634 release_regions: 635 pci_release_regions(pci); 636 disable_pci: 637 pci_disable_device(pci); 638 639 return ret; 640 } 641 642 static bool check_acp_sdw_enable_status(struct acp63_dev_data *adata) 643 { 644 u32 sdw0_en, sdw1_en; 645 646 sdw0_en = readl(adata->acp63_base + ACP_SW0_EN); 647 sdw1_en = readl(adata->acp63_base + ACP_SW1_EN); 648 return (sdw0_en || sdw1_en); 649 } 650 651 static void handle_acp63_sdw_pme_event(struct acp63_dev_data *adata) 652 { 653 u32 val; 654 655 val = readl(adata->acp63_base + ACP_SW0_WAKE_EN); 656 if (val && adata->sdw->pdev[0]) 657 pm_request_resume(&adata->sdw->pdev[0]->dev); 658 659 val = readl(adata->acp63_base + ACP_SW1_WAKE_EN); 660 if (val && adata->sdw->pdev[1]) 661 pm_request_resume(&adata->sdw->pdev[1]->dev); 662 } 663 664 static int __maybe_unused snd_acp63_suspend(struct device *dev) 665 { 666 struct acp63_dev_data *adata; 667 int ret; 668 669 adata = dev_get_drvdata(dev); 670 if (adata->is_sdw_dev) { 671 adata->sdw_en_stat = check_acp_sdw_enable_status(adata); 672 if (adata->sdw_en_stat) 673 return 0; 674 } 675 ret = acp63_deinit(adata->acp63_base, dev); 676 if (ret) 677 dev_err(dev, "ACP de-init failed\n"); 678 679 return ret; 680 } 681 682 static int __maybe_unused snd_acp63_runtime_resume(struct device *dev) 683 { 684 struct acp63_dev_data *adata; 685 int ret; 686 687 adata = dev_get_drvdata(dev); 688 if (adata->sdw_en_stat) 689 return 0; 690 691 ret = acp63_init(adata->acp63_base, dev); 692 if (ret) { 693 dev_err(dev, "ACP init failed\n"); 694 return ret; 695 } 696 697 if (!adata->sdw_en_stat) 698 handle_acp63_sdw_pme_event(adata); 699 return 0; 700 } 701 702 static int __maybe_unused snd_acp63_resume(struct device *dev) 703 { 704 struct acp63_dev_data *adata; 705 int ret; 706 707 adata = dev_get_drvdata(dev); 708 if (adata->sdw_en_stat) 709 return 0; 710 711 ret = acp63_init(adata->acp63_base, dev); 712 if (ret) 713 dev_err(dev, "ACP init failed\n"); 714 715 return ret; 716 } 717 718 static const struct dev_pm_ops acp63_pm_ops = { 719 SET_RUNTIME_PM_OPS(snd_acp63_suspend, snd_acp63_runtime_resume, NULL) 720 SET_SYSTEM_SLEEP_PM_OPS(snd_acp63_suspend, snd_acp63_resume) 721 }; 722 723 static void snd_acp63_remove(struct pci_dev *pci) 724 { 725 struct acp63_dev_data *adata; 726 int ret; 727 728 adata = pci_get_drvdata(pci); 729 if (adata->sdw) { 730 amd_sdw_exit(adata); 731 platform_device_unregister(adata->sdw_dma_dev); 732 } 733 if (adata->is_pdm_dev) { 734 platform_device_unregister(adata->pdm_dev); 735 platform_device_unregister(adata->dmic_codec_dev); 736 } 737 if (adata->mach_dev) 738 platform_device_unregister(adata->mach_dev); 739 ret = acp63_deinit(adata->acp63_base, &pci->dev); 740 if (ret) 741 dev_err(&pci->dev, "ACP de-init failed\n"); 742 pm_runtime_forbid(&pci->dev); 743 pm_runtime_get_noresume(&pci->dev); 744 pci_release_regions(pci); 745 pci_disable_device(pci); 746 } 747 748 static const struct pci_device_id snd_acp63_ids[] = { 749 { PCI_DEVICE(PCI_VENDOR_ID_AMD, ACP_DEVICE_ID), 750 .class = PCI_CLASS_MULTIMEDIA_OTHER << 8, 751 .class_mask = 0xffffff }, 752 { 0, }, 753 }; 754 MODULE_DEVICE_TABLE(pci, snd_acp63_ids); 755 756 static struct pci_driver ps_acp63_driver = { 757 .name = KBUILD_MODNAME, 758 .id_table = snd_acp63_ids, 759 .probe = snd_acp63_probe, 760 .remove = snd_acp63_remove, 761 .driver = { 762 .pm = &acp63_pm_ops, 763 } 764 }; 765 766 module_pci_driver(ps_acp63_driver); 767 768 MODULE_AUTHOR("Vijendar.Mukunda@amd.com"); 769 MODULE_AUTHOR("Syed.SabaKareem@amd.com"); 770 MODULE_DESCRIPTION("AMD ACP Pink Sardine PCI driver"); 771 MODULE_IMPORT_NS("SOUNDWIRE_AMD_INIT"); 772 MODULE_IMPORT_NS("SND_AMD_SOUNDWIRE_ACPI"); 773 MODULE_LICENSE("GPL v2"); 774