1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // AMD Renoir ACP PCI Driver 4 // 5 //Copyright 2020 Advanced Micro Devices, Inc. 6 7 #include <linux/pci.h> 8 #include <linux/acpi.h> 9 #include <linux/dmi.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/interrupt.h> 15 #include <linux/pm_runtime.h> 16 17 #include "rn_acp3x.h" 18 19 static int acp_power_gating; 20 module_param(acp_power_gating, int, 0644); 21 MODULE_PARM_DESC(acp_power_gating, "Enable acp power gating"); 22 23 /** 24 * dmic_acpi_check = -1 - Use ACPI/DMI method to detect the DMIC hardware presence at runtime 25 * = 0 - Skip the DMIC device creation and return probe failure 26 * = 1 - Force DMIC support 27 */ 28 static int dmic_acpi_check = ACP_DMIC_AUTO; 29 module_param(dmic_acpi_check, bint, 0644); 30 MODULE_PARM_DESC(dmic_acpi_check, "Digital microphone presence (-1=auto, 0=none, 1=force)"); 31 32 struct acp_dev_data { 33 void __iomem *acp_base; 34 struct resource *res; 35 struct platform_device *pdev[ACP_DEVS]; 36 }; 37 38 static int rn_acp_power_on(void __iomem *acp_base) 39 { 40 u32 val; 41 int timeout; 42 43 val = rn_readl(acp_base + ACP_PGFSM_STATUS); 44 45 if (val == 0) 46 return val; 47 48 if ((val & ACP_PGFSM_STATUS_MASK) != 49 ACP_POWER_ON_IN_PROGRESS) 50 rn_writel(ACP_PGFSM_CNTL_POWER_ON_MASK, 51 acp_base + ACP_PGFSM_CONTROL); 52 timeout = 0; 53 while (++timeout < 500) { 54 val = rn_readl(acp_base + ACP_PGFSM_STATUS); 55 if (!val) 56 return 0; 57 udelay(1); 58 } 59 return -ETIMEDOUT; 60 } 61 62 static int rn_acp_power_off(void __iomem *acp_base) 63 { 64 u32 val; 65 int timeout; 66 67 rn_writel(ACP_PGFSM_CNTL_POWER_OFF_MASK, 68 acp_base + ACP_PGFSM_CONTROL); 69 timeout = 0; 70 while (++timeout < 500) { 71 val = rn_readl(acp_base + ACP_PGFSM_STATUS); 72 if ((val & ACP_PGFSM_STATUS_MASK) == ACP_POWERED_OFF) 73 return 0; 74 udelay(1); 75 } 76 return -ETIMEDOUT; 77 } 78 79 static int rn_acp_reset(void __iomem *acp_base) 80 { 81 u32 val; 82 int timeout; 83 84 rn_writel(1, acp_base + ACP_SOFT_RESET); 85 timeout = 0; 86 while (++timeout < 500) { 87 val = rn_readl(acp_base + ACP_SOFT_RESET); 88 if (val & ACP_SOFT_RESET_SOFTRESET_AUDDONE_MASK) 89 break; 90 cpu_relax(); 91 } 92 rn_writel(0, acp_base + ACP_SOFT_RESET); 93 timeout = 0; 94 while (++timeout < 500) { 95 val = rn_readl(acp_base + ACP_SOFT_RESET); 96 if (!val) 97 return 0; 98 cpu_relax(); 99 } 100 return -ETIMEDOUT; 101 } 102 103 static void rn_acp_enable_interrupts(void __iomem *acp_base) 104 { 105 u32 ext_intr_ctrl; 106 107 rn_writel(0x01, acp_base + ACP_EXTERNAL_INTR_ENB); 108 ext_intr_ctrl = rn_readl(acp_base + ACP_EXTERNAL_INTR_CNTL); 109 ext_intr_ctrl |= ACP_ERROR_MASK; 110 rn_writel(ext_intr_ctrl, acp_base + ACP_EXTERNAL_INTR_CNTL); 111 } 112 113 static void rn_acp_disable_interrupts(void __iomem *acp_base) 114 { 115 rn_writel(ACP_EXT_INTR_STAT_CLEAR_MASK, acp_base + 116 ACP_EXTERNAL_INTR_STAT); 117 rn_writel(0x00, acp_base + ACP_EXTERNAL_INTR_ENB); 118 } 119 120 static int rn_acp_init(void __iomem *acp_base) 121 { 122 int ret; 123 124 /* power on */ 125 ret = rn_acp_power_on(acp_base); 126 if (ret) { 127 pr_err("ACP power on failed\n"); 128 return ret; 129 } 130 rn_writel(0x01, acp_base + ACP_CONTROL); 131 /* Reset */ 132 ret = rn_acp_reset(acp_base); 133 if (ret) { 134 pr_err("ACP reset failed\n"); 135 return ret; 136 } 137 rn_writel(0x03, acp_base + ACP_CLKMUX_SEL); 138 rn_acp_enable_interrupts(acp_base); 139 return 0; 140 } 141 142 static int rn_acp_deinit(void __iomem *acp_base) 143 { 144 int ret; 145 146 rn_acp_disable_interrupts(acp_base); 147 /* Reset */ 148 ret = rn_acp_reset(acp_base); 149 if (ret) { 150 pr_err("ACP reset failed\n"); 151 return ret; 152 } 153 rn_writel(0x00, acp_base + ACP_CLKMUX_SEL); 154 rn_writel(0x00, acp_base + ACP_CONTROL); 155 /* power off */ 156 if (acp_power_gating) { 157 ret = rn_acp_power_off(acp_base); 158 if (ret) { 159 pr_err("ACP power off failed\n"); 160 return ret; 161 } 162 } 163 return 0; 164 } 165 166 static const struct dmi_system_id rn_acp_quirk_table[] = { 167 { 168 /* Lenovo IdeaPad Flex 5 14ARE05, IdeaPad 5 15ARE05 */ 169 .matches = { 170 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "LENOVO"), 171 DMI_EXACT_MATCH(DMI_BOARD_NAME, "LNVNB161216"), 172 } 173 }, 174 { 175 /* Lenovo ThinkPad E14 Gen 2 */ 176 .matches = { 177 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "LENOVO"), 178 DMI_EXACT_MATCH(DMI_BOARD_NAME, "20T6CTO1WW"), 179 } 180 }, 181 { 182 /* Lenovo ThinkPad X395 */ 183 .matches = { 184 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "LENOVO"), 185 DMI_EXACT_MATCH(DMI_BOARD_NAME, "20NLCTO1WW"), 186 } 187 }, 188 {} 189 }; 190 191 static int snd_rn_acp_probe(struct pci_dev *pci, 192 const struct pci_device_id *pci_id) 193 { 194 struct acp_dev_data *adata; 195 struct platform_device_info pdevinfo[ACP_DEVS]; 196 #if defined(CONFIG_ACPI) 197 acpi_handle handle; 198 acpi_integer dmic_status; 199 #endif 200 const struct dmi_system_id *dmi_id; 201 unsigned int irqflags; 202 int ret, index; 203 u32 addr; 204 205 /* Renoir device check */ 206 if (pci->revision != 0x01) 207 return -ENODEV; 208 209 if (pci_enable_device(pci)) { 210 dev_err(&pci->dev, "pci_enable_device failed\n"); 211 return -ENODEV; 212 } 213 214 ret = pci_request_regions(pci, "AMD ACP3x audio"); 215 if (ret < 0) { 216 dev_err(&pci->dev, "pci_request_regions failed\n"); 217 goto disable_pci; 218 } 219 220 adata = devm_kzalloc(&pci->dev, sizeof(struct acp_dev_data), 221 GFP_KERNEL); 222 if (!adata) { 223 ret = -ENOMEM; 224 goto release_regions; 225 } 226 227 /* check for msi interrupt support */ 228 ret = pci_enable_msi(pci); 229 if (ret) 230 /* msi is not enabled */ 231 irqflags = IRQF_SHARED; 232 else 233 /* msi is enabled */ 234 irqflags = 0; 235 236 addr = pci_resource_start(pci, 0); 237 adata->acp_base = devm_ioremap(&pci->dev, addr, 238 pci_resource_len(pci, 0)); 239 if (!adata->acp_base) { 240 ret = -ENOMEM; 241 goto disable_msi; 242 } 243 pci_set_master(pci); 244 pci_set_drvdata(pci, adata); 245 ret = rn_acp_init(adata->acp_base); 246 if (ret) 247 goto disable_msi; 248 249 if (!dmic_acpi_check) { 250 ret = -ENODEV; 251 goto de_init; 252 } else if (dmic_acpi_check == ACP_DMIC_AUTO) { 253 #if defined(CONFIG_ACPI) 254 handle = ACPI_HANDLE(&pci->dev); 255 ret = acpi_evaluate_integer(handle, "_WOV", NULL, &dmic_status); 256 if (ACPI_FAILURE(ret)) { 257 ret = -ENODEV; 258 goto de_init; 259 } 260 if (!dmic_status) { 261 ret = -ENODEV; 262 goto de_init; 263 } 264 #endif 265 dmi_id = dmi_first_match(rn_acp_quirk_table); 266 if (dmi_id && !dmi_id->driver_data) { 267 dev_info(&pci->dev, "ACPI settings override using DMI (ACP mic is not present)"); 268 ret = -ENODEV; 269 goto de_init; 270 } 271 } 272 273 adata->res = devm_kzalloc(&pci->dev, 274 sizeof(struct resource) * 2, 275 GFP_KERNEL); 276 if (!adata->res) { 277 ret = -ENOMEM; 278 goto de_init; 279 } 280 281 adata->res[0].name = "acp_pdm_iomem"; 282 adata->res[0].flags = IORESOURCE_MEM; 283 adata->res[0].start = addr; 284 adata->res[0].end = addr + (ACP_REG_END - ACP_REG_START); 285 adata->res[1].name = "acp_pdm_irq"; 286 adata->res[1].flags = IORESOURCE_IRQ; 287 adata->res[1].start = pci->irq; 288 adata->res[1].end = pci->irq; 289 290 memset(&pdevinfo, 0, sizeof(pdevinfo)); 291 pdevinfo[0].name = "acp_rn_pdm_dma"; 292 pdevinfo[0].id = 0; 293 pdevinfo[0].parent = &pci->dev; 294 pdevinfo[0].num_res = 2; 295 pdevinfo[0].res = adata->res; 296 pdevinfo[0].data = &irqflags; 297 pdevinfo[0].size_data = sizeof(irqflags); 298 299 pdevinfo[1].name = "dmic-codec"; 300 pdevinfo[1].id = 0; 301 pdevinfo[1].parent = &pci->dev; 302 pdevinfo[2].name = "acp_pdm_mach"; 303 pdevinfo[2].id = 0; 304 pdevinfo[2].parent = &pci->dev; 305 for (index = 0; index < ACP_DEVS; index++) { 306 adata->pdev[index] = 307 platform_device_register_full(&pdevinfo[index]); 308 if (IS_ERR(adata->pdev[index])) { 309 dev_err(&pci->dev, "cannot register %s device\n", 310 pdevinfo[index].name); 311 ret = PTR_ERR(adata->pdev[index]); 312 goto unregister_devs; 313 } 314 } 315 pm_runtime_set_autosuspend_delay(&pci->dev, ACP_SUSPEND_DELAY_MS); 316 pm_runtime_use_autosuspend(&pci->dev); 317 pm_runtime_put_noidle(&pci->dev); 318 pm_runtime_allow(&pci->dev); 319 return 0; 320 321 unregister_devs: 322 for (index = 0; index < ACP_DEVS; index++) 323 platform_device_unregister(adata->pdev[index]); 324 de_init: 325 if (rn_acp_deinit(adata->acp_base)) 326 dev_err(&pci->dev, "ACP de-init failed\n"); 327 disable_msi: 328 pci_disable_msi(pci); 329 release_regions: 330 pci_release_regions(pci); 331 disable_pci: 332 pci_disable_device(pci); 333 334 return ret; 335 } 336 337 static int snd_rn_acp_suspend(struct device *dev) 338 { 339 int ret; 340 struct acp_dev_data *adata; 341 342 adata = dev_get_drvdata(dev); 343 ret = rn_acp_deinit(adata->acp_base); 344 if (ret) 345 dev_err(dev, "ACP de-init failed\n"); 346 else 347 dev_dbg(dev, "ACP de-initialized\n"); 348 349 return ret; 350 } 351 352 static int snd_rn_acp_resume(struct device *dev) 353 { 354 int ret; 355 struct acp_dev_data *adata; 356 357 adata = dev_get_drvdata(dev); 358 ret = rn_acp_init(adata->acp_base); 359 if (ret) { 360 dev_err(dev, "ACP init failed\n"); 361 return ret; 362 } 363 return 0; 364 } 365 366 static const struct dev_pm_ops rn_acp_pm = { 367 .runtime_suspend = snd_rn_acp_suspend, 368 .runtime_resume = snd_rn_acp_resume, 369 .suspend = snd_rn_acp_suspend, 370 .resume = snd_rn_acp_resume, 371 }; 372 373 static void snd_rn_acp_remove(struct pci_dev *pci) 374 { 375 struct acp_dev_data *adata; 376 int ret, index; 377 378 adata = pci_get_drvdata(pci); 379 for (index = 0; index < ACP_DEVS; index++) 380 platform_device_unregister(adata->pdev[index]); 381 ret = rn_acp_deinit(adata->acp_base); 382 if (ret) 383 dev_err(&pci->dev, "ACP de-init failed\n"); 384 pm_runtime_forbid(&pci->dev); 385 pm_runtime_get_noresume(&pci->dev); 386 pci_disable_msi(pci); 387 pci_release_regions(pci); 388 pci_disable_device(pci); 389 } 390 391 static const struct pci_device_id snd_rn_acp_ids[] = { 392 { PCI_DEVICE(PCI_VENDOR_ID_AMD, ACP_DEVICE_ID), 393 .class = PCI_CLASS_MULTIMEDIA_OTHER << 8, 394 .class_mask = 0xffffff }, 395 { 0, }, 396 }; 397 MODULE_DEVICE_TABLE(pci, snd_rn_acp_ids); 398 399 static struct pci_driver rn_acp_driver = { 400 .name = KBUILD_MODNAME, 401 .id_table = snd_rn_acp_ids, 402 .probe = snd_rn_acp_probe, 403 .remove = snd_rn_acp_remove, 404 .driver = { 405 .pm = &rn_acp_pm, 406 } 407 }; 408 409 module_pci_driver(rn_acp_driver); 410 411 MODULE_AUTHOR("Vijendar.Mukunda@amd.com"); 412 MODULE_DESCRIPTION("AMD ACP Renoir PCI driver"); 413 MODULE_LICENSE("GPL v2"); 414