1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // AMD Vangogh ACP PCI Driver 4 // 5 // Copyright (C) 2021 Advanced Micro Devices, Inc. All rights reserved. 6 7 #include <linux/pci.h> 8 #include <linux/module.h> 9 #include <linux/io.h> 10 #include <linux/delay.h> 11 #include <linux/platform_device.h> 12 #include <linux/interrupt.h> 13 #include <linux/pm_runtime.h> 14 15 #include "acp5x.h" 16 17 struct acp5x_dev_data { 18 void __iomem *acp5x_base; 19 bool acp5x_audio_mode; 20 struct resource *res; 21 struct platform_device *pdev[ACP5x_DEVS]; 22 }; 23 24 static int acp5x_power_on(void __iomem *acp5x_base) 25 { 26 u32 val; 27 int timeout; 28 29 val = acp_readl(acp5x_base + ACP_PGFSM_STATUS); 30 31 if (val == 0) 32 return val; 33 34 if ((val & ACP_PGFSM_STATUS_MASK) != 35 ACP_POWER_ON_IN_PROGRESS) 36 acp_writel(ACP_PGFSM_CNTL_POWER_ON_MASK, 37 acp5x_base + ACP_PGFSM_CONTROL); 38 timeout = 0; 39 while (++timeout < 500) { 40 val = acp_readl(acp5x_base + ACP_PGFSM_STATUS); 41 if ((val & ACP_PGFSM_STATUS_MASK) == ACP_POWERED_ON) 42 return 0; 43 udelay(1); 44 } 45 return -ETIMEDOUT; 46 } 47 48 static int acp5x_reset(void __iomem *acp5x_base) 49 { 50 u32 val; 51 int timeout; 52 53 acp_writel(1, acp5x_base + ACP_SOFT_RESET); 54 timeout = 0; 55 while (++timeout < 500) { 56 val = acp_readl(acp5x_base + ACP_SOFT_RESET); 57 if (val & ACP_SOFT_RESET_SOFTRESET_AUDDONE_MASK) 58 break; 59 cpu_relax(); 60 } 61 acp_writel(0, acp5x_base + ACP_SOFT_RESET); 62 timeout = 0; 63 while (++timeout < 500) { 64 val = acp_readl(acp5x_base + ACP_SOFT_RESET); 65 if (!val) 66 return 0; 67 cpu_relax(); 68 } 69 return -ETIMEDOUT; 70 } 71 72 static void acp5x_enable_interrupts(void __iomem *acp5x_base) 73 { 74 acp_writel(0x01, acp5x_base + ACP_EXTERNAL_INTR_ENB); 75 } 76 77 static void acp5x_disable_interrupts(void __iomem *acp5x_base) 78 { 79 acp_writel(ACP_EXT_INTR_STAT_CLEAR_MASK, acp5x_base + 80 ACP_EXTERNAL_INTR_STAT); 81 acp_writel(0x00, acp5x_base + ACP_EXTERNAL_INTR_CNTL); 82 acp_writel(0x00, acp5x_base + ACP_EXTERNAL_INTR_ENB); 83 } 84 85 static int acp5x_init(void __iomem *acp5x_base) 86 { 87 int ret; 88 89 /* power on */ 90 ret = acp5x_power_on(acp5x_base); 91 if (ret) { 92 pr_err("ACP5x power on failed\n"); 93 return ret; 94 } 95 /* Reset */ 96 ret = acp5x_reset(acp5x_base); 97 if (ret) { 98 pr_err("ACP5x reset failed\n"); 99 return ret; 100 } 101 acp5x_enable_interrupts(acp5x_base); 102 return 0; 103 } 104 105 static int acp5x_deinit(void __iomem *acp5x_base) 106 { 107 int ret; 108 109 acp5x_disable_interrupts(acp5x_base); 110 /* Reset */ 111 ret = acp5x_reset(acp5x_base); 112 if (ret) { 113 pr_err("ACP5x reset failed\n"); 114 return ret; 115 } 116 return 0; 117 } 118 119 static int snd_acp5x_probe(struct pci_dev *pci, 120 const struct pci_device_id *pci_id) 121 { 122 struct acp5x_dev_data *adata; 123 struct platform_device_info pdevinfo[ACP5x_DEVS]; 124 unsigned int irqflags; 125 int ret, i; 126 u32 addr, val; 127 128 irqflags = IRQF_SHARED; 129 if (pci->revision != 0x50) 130 return -ENODEV; 131 132 if (pci_enable_device(pci)) { 133 dev_err(&pci->dev, "pci_enable_device failed\n"); 134 return -ENODEV; 135 } 136 137 ret = pci_request_regions(pci, "AMD ACP5x audio"); 138 if (ret < 0) { 139 dev_err(&pci->dev, "pci_request_regions failed\n"); 140 goto disable_pci; 141 } 142 143 adata = devm_kzalloc(&pci->dev, sizeof(struct acp5x_dev_data), 144 GFP_KERNEL); 145 if (!adata) { 146 ret = -ENOMEM; 147 goto release_regions; 148 } 149 addr = pci_resource_start(pci, 0); 150 adata->acp5x_base = devm_ioremap(&pci->dev, addr, 151 pci_resource_len(pci, 0)); 152 if (!adata->acp5x_base) { 153 ret = -ENOMEM; 154 goto release_regions; 155 } 156 pci_set_master(pci); 157 pci_set_drvdata(pci, adata); 158 ret = acp5x_init(adata->acp5x_base); 159 if (ret) 160 goto release_regions; 161 162 val = acp_readl(adata->acp5x_base + ACP_PIN_CONFIG); 163 switch (val) { 164 case I2S_MODE: 165 adata->res = devm_kzalloc(&pci->dev, 166 sizeof(struct resource) * ACP5x_RES, 167 GFP_KERNEL); 168 if (!adata->res) { 169 ret = -ENOMEM; 170 goto de_init; 171 } 172 173 adata->res[0].name = "acp5x_i2s_iomem"; 174 adata->res[0].flags = IORESOURCE_MEM; 175 adata->res[0].start = addr; 176 adata->res[0].end = addr + (ACP5x_REG_END - ACP5x_REG_START); 177 178 adata->res[1].name = "acp5x_i2s_sp"; 179 adata->res[1].flags = IORESOURCE_MEM; 180 adata->res[1].start = addr + ACP5x_I2STDM_REG_START; 181 adata->res[1].end = addr + ACP5x_I2STDM_REG_END; 182 183 adata->res[2].name = "acp5x_i2s_hs"; 184 adata->res[2].flags = IORESOURCE_MEM; 185 adata->res[2].start = addr + ACP5x_HS_TDM_REG_START; 186 adata->res[2].end = addr + ACP5x_HS_TDM_REG_END; 187 188 adata->res[3].name = "acp5x_i2s_irq"; 189 adata->res[3].flags = IORESOURCE_IRQ; 190 adata->res[3].start = pci->irq; 191 adata->res[3].end = adata->res[3].start; 192 193 adata->acp5x_audio_mode = ACP5x_I2S_MODE; 194 195 memset(&pdevinfo, 0, sizeof(pdevinfo)); 196 pdevinfo[0].name = "acp5x_i2s_dma"; 197 pdevinfo[0].id = 0; 198 pdevinfo[0].parent = &pci->dev; 199 pdevinfo[0].num_res = 4; 200 pdevinfo[0].res = &adata->res[0]; 201 pdevinfo[0].data = &irqflags; 202 pdevinfo[0].size_data = sizeof(irqflags); 203 204 pdevinfo[1].name = "acp5x_i2s_playcap"; 205 pdevinfo[1].id = 0; 206 pdevinfo[1].parent = &pci->dev; 207 pdevinfo[1].num_res = 1; 208 pdevinfo[1].res = &adata->res[1]; 209 210 pdevinfo[2].name = "acp5x_i2s_playcap"; 211 pdevinfo[2].id = 1; 212 pdevinfo[2].parent = &pci->dev; 213 pdevinfo[2].num_res = 1; 214 pdevinfo[2].res = &adata->res[2]; 215 216 pdevinfo[3].name = "acp5x_mach"; 217 pdevinfo[3].id = 0; 218 pdevinfo[3].parent = &pci->dev; 219 for (i = 0; i < ACP5x_DEVS; i++) { 220 adata->pdev[i] = 221 platform_device_register_full(&pdevinfo[i]); 222 if (IS_ERR(adata->pdev[i])) { 223 dev_err(&pci->dev, "cannot register %s device\n", 224 pdevinfo[i].name); 225 ret = PTR_ERR(adata->pdev[i]); 226 goto unregister_devs; 227 } 228 } 229 break; 230 default: 231 dev_info(&pci->dev, "ACP audio mode : %d\n", val); 232 } 233 pm_runtime_set_autosuspend_delay(&pci->dev, 2000); 234 pm_runtime_use_autosuspend(&pci->dev); 235 pm_runtime_put_noidle(&pci->dev); 236 pm_runtime_allow(&pci->dev); 237 return 0; 238 239 unregister_devs: 240 for (--i; i >= 0; i--) 241 platform_device_unregister(adata->pdev[i]); 242 de_init: 243 if (acp5x_deinit(adata->acp5x_base)) 244 dev_err(&pci->dev, "ACP de-init failed\n"); 245 release_regions: 246 pci_release_regions(pci); 247 disable_pci: 248 pci_disable_device(pci); 249 250 return ret; 251 } 252 253 static int __maybe_unused snd_acp5x_suspend(struct device *dev) 254 { 255 int ret; 256 struct acp5x_dev_data *adata; 257 258 adata = dev_get_drvdata(dev); 259 ret = acp5x_deinit(adata->acp5x_base); 260 if (ret) 261 dev_err(dev, "ACP de-init failed\n"); 262 else 263 dev_dbg(dev, "ACP de-initialized\n"); 264 265 return ret; 266 } 267 268 static int __maybe_unused snd_acp5x_resume(struct device *dev) 269 { 270 int ret; 271 struct acp5x_dev_data *adata; 272 273 adata = dev_get_drvdata(dev); 274 ret = acp5x_init(adata->acp5x_base); 275 if (ret) { 276 dev_err(dev, "ACP init failed\n"); 277 return ret; 278 } 279 return 0; 280 } 281 282 static const struct dev_pm_ops acp5x_pm = { 283 SET_RUNTIME_PM_OPS(snd_acp5x_suspend, 284 snd_acp5x_resume, NULL) 285 SET_SYSTEM_SLEEP_PM_OPS(snd_acp5x_suspend, snd_acp5x_resume) 286 }; 287 288 static void snd_acp5x_remove(struct pci_dev *pci) 289 { 290 struct acp5x_dev_data *adata; 291 int i, ret; 292 293 adata = pci_get_drvdata(pci); 294 if (adata->acp5x_audio_mode == ACP5x_I2S_MODE) { 295 for (i = 0; i < ACP5x_DEVS; i++) 296 platform_device_unregister(adata->pdev[i]); 297 } 298 ret = acp5x_deinit(adata->acp5x_base); 299 if (ret) 300 dev_err(&pci->dev, "ACP de-init failed\n"); 301 pm_runtime_forbid(&pci->dev); 302 pm_runtime_get_noresume(&pci->dev); 303 pci_release_regions(pci); 304 pci_disable_device(pci); 305 } 306 307 static const struct pci_device_id snd_acp5x_ids[] = { 308 { PCI_DEVICE(PCI_VENDOR_ID_AMD, ACP_DEVICE_ID), 309 .class = PCI_CLASS_MULTIMEDIA_OTHER << 8, 310 .class_mask = 0xffffff }, 311 { 0, }, 312 }; 313 MODULE_DEVICE_TABLE(pci, snd_acp5x_ids); 314 315 static struct pci_driver acp5x_driver = { 316 .name = KBUILD_MODNAME, 317 .id_table = snd_acp5x_ids, 318 .probe = snd_acp5x_probe, 319 .remove = snd_acp5x_remove, 320 .driver = { 321 .pm = &acp5x_pm, 322 } 323 }; 324 325 module_pci_driver(acp5x_driver); 326 327 MODULE_AUTHOR("Vijendar.Mukunda@amd.com"); 328 MODULE_DESCRIPTION("AMD Vangogh ACP PCI driver"); 329 MODULE_LICENSE("GPL v2"); 330