Vijendar Mukunda | c62442bd | 2021-10-18 16:50:33 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * AMD Yellow Carp ACP PCI Driver |
| 4 | * |
| 5 | * Copyright 2021 Advanced Micro Devices, Inc. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/pci.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/io.h> |
Vijendar Mukunda | 8c7161f | 2021-10-18 16:50:34 +0530 | [diff] [blame] | 11 | #include <linux/delay.h> |
Vijendar Mukunda | fc329c1 | 2021-10-18 16:50:35 +0530 | [diff] [blame] | 12 | #include <linux/platform_device.h> |
Vijendar Mukunda | cc0deaa | 2021-10-18 16:50:37 +0530 | [diff] [blame] | 13 | #include <linux/interrupt.h> |
| 14 | #include <sound/pcm_params.h> |
Vijendar Mukunda | c8212df | 2021-10-18 16:50:39 +0530 | [diff] [blame] | 15 | #include <linux/pm_runtime.h> |
Vijendar Mukunda | c62442bd | 2021-10-18 16:50:33 +0530 | [diff] [blame] | 16 | |
| 17 | #include "acp6x.h" |
| 18 | |
| 19 | struct acp6x_dev_data { |
| 20 | void __iomem *acp6x_base; |
Vijendar Mukunda | fc329c1 | 2021-10-18 16:50:35 +0530 | [diff] [blame] | 21 | struct resource *res; |
| 22 | bool acp6x_audio_mode; |
| 23 | struct platform_device *pdev[ACP6x_DEVS]; |
Vijendar Mukunda | c62442bd | 2021-10-18 16:50:33 +0530 | [diff] [blame] | 24 | }; |
| 25 | |
Vijendar Mukunda | 8c7161f | 2021-10-18 16:50:34 +0530 | [diff] [blame] | 26 | static int acp6x_power_on(void __iomem *acp_base) |
| 27 | { |
| 28 | u32 val; |
| 29 | int timeout; |
| 30 | |
| 31 | val = acp6x_readl(acp_base + ACP_PGFSM_STATUS); |
| 32 | |
| 33 | if (!val) |
| 34 | return val; |
| 35 | |
| 36 | if ((val & ACP_PGFSM_STATUS_MASK) != ACP_POWER_ON_IN_PROGRESS) |
| 37 | acp6x_writel(ACP_PGFSM_CNTL_POWER_ON_MASK, acp_base + ACP_PGFSM_CONTROL); |
| 38 | timeout = 0; |
| 39 | while (++timeout < 500) { |
| 40 | val = acp6x_readl(acp_base + ACP_PGFSM_STATUS); |
| 41 | if (!val) |
| 42 | return 0; |
| 43 | udelay(1); |
| 44 | } |
| 45 | return -ETIMEDOUT; |
| 46 | } |
| 47 | |
| 48 | static int acp6x_reset(void __iomem *acp_base) |
| 49 | { |
| 50 | u32 val; |
| 51 | int timeout; |
| 52 | |
| 53 | acp6x_writel(1, acp_base + ACP_SOFT_RESET); |
| 54 | timeout = 0; |
| 55 | while (++timeout < 500) { |
| 56 | val = acp6x_readl(acp_base + ACP_SOFT_RESET); |
| 57 | if (val & ACP_SOFT_RESET_SOFTRESET_AUDDONE_MASK) |
| 58 | break; |
| 59 | cpu_relax(); |
| 60 | } |
| 61 | acp6x_writel(0, acp_base + ACP_SOFT_RESET); |
| 62 | timeout = 0; |
| 63 | while (++timeout < 500) { |
| 64 | val = acp6x_readl(acp_base + ACP_SOFT_RESET); |
| 65 | if (!val) |
| 66 | return 0; |
| 67 | cpu_relax(); |
| 68 | } |
| 69 | return -ETIMEDOUT; |
| 70 | } |
| 71 | |
| 72 | static void acp6x_enable_interrupts(void __iomem *acp_base) |
| 73 | { |
| 74 | acp6x_writel(0x01, acp_base + ACP_EXTERNAL_INTR_ENB); |
| 75 | } |
| 76 | |
| 77 | static void acp6x_disable_interrupts(void __iomem *acp_base) |
| 78 | { |
| 79 | acp6x_writel(ACP_EXT_INTR_STAT_CLEAR_MASK, acp_base + |
| 80 | ACP_EXTERNAL_INTR_STAT); |
| 81 | acp6x_writel(0x00, acp_base + ACP_EXTERNAL_INTR_CNTL); |
| 82 | acp6x_writel(0x00, acp_base + ACP_EXTERNAL_INTR_ENB); |
| 83 | } |
| 84 | |
| 85 | static int acp6x_init(void __iomem *acp_base) |
| 86 | { |
| 87 | int ret; |
| 88 | |
| 89 | /* power on */ |
| 90 | ret = acp6x_power_on(acp_base); |
| 91 | if (ret) { |
| 92 | pr_err("ACP power on failed\n"); |
| 93 | return ret; |
| 94 | } |
| 95 | acp6x_writel(0x01, acp_base + ACP_CONTROL); |
| 96 | /* Reset */ |
| 97 | ret = acp6x_reset(acp_base); |
| 98 | if (ret) { |
| 99 | pr_err("ACP reset failed\n"); |
| 100 | return ret; |
| 101 | } |
| 102 | acp6x_writel(0x03, acp_base + ACP_CLKMUX_SEL); |
| 103 | acp6x_enable_interrupts(acp_base); |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int acp6x_deinit(void __iomem *acp_base) |
| 108 | { |
| 109 | int ret; |
| 110 | |
| 111 | acp6x_disable_interrupts(acp_base); |
| 112 | /* Reset */ |
| 113 | ret = acp6x_reset(acp_base); |
| 114 | if (ret) { |
| 115 | pr_err("ACP reset failed\n"); |
| 116 | return ret; |
| 117 | } |
| 118 | acp6x_writel(0x00, acp_base + ACP_CLKMUX_SEL); |
| 119 | acp6x_writel(0x00, acp_base + ACP_CONTROL); |
| 120 | return 0; |
| 121 | } |
| 122 | |
Vijendar Mukunda | cc0deaa | 2021-10-18 16:50:37 +0530 | [diff] [blame] | 123 | static irqreturn_t acp6x_irq_handler(int irq, void *dev_id) |
| 124 | { |
| 125 | struct acp6x_dev_data *adata; |
| 126 | struct pdm_dev_data *yc_pdm_data; |
| 127 | u32 val; |
| 128 | |
| 129 | adata = dev_id; |
| 130 | if (!adata) |
| 131 | return IRQ_NONE; |
| 132 | |
| 133 | val = acp6x_readl(adata->acp6x_base + ACP_EXTERNAL_INTR_STAT); |
| 134 | if (val & BIT(PDM_DMA_STAT)) { |
| 135 | yc_pdm_data = dev_get_drvdata(&adata->pdev[0]->dev); |
| 136 | acp6x_writel(BIT(PDM_DMA_STAT), adata->acp6x_base + ACP_EXTERNAL_INTR_STAT); |
| 137 | if (yc_pdm_data->capture_stream) |
| 138 | snd_pcm_period_elapsed(yc_pdm_data->capture_stream); |
| 139 | return IRQ_HANDLED; |
| 140 | } |
| 141 | return IRQ_NONE; |
| 142 | } |
| 143 | |
Vijendar Mukunda | c62442bd | 2021-10-18 16:50:33 +0530 | [diff] [blame] | 144 | static int snd_acp6x_probe(struct pci_dev *pci, |
| 145 | const struct pci_device_id *pci_id) |
| 146 | { |
| 147 | struct acp6x_dev_data *adata; |
Vijendar Mukunda | fc329c1 | 2021-10-18 16:50:35 +0530 | [diff] [blame] | 148 | struct platform_device_info pdevinfo[ACP6x_DEVS]; |
| 149 | int ret, index; |
| 150 | int val = 0x00; |
Vijendar Mukunda | c62442bd | 2021-10-18 16:50:33 +0530 | [diff] [blame] | 151 | u32 addr; |
Vijendar Mukunda | cc0deaa | 2021-10-18 16:50:37 +0530 | [diff] [blame] | 152 | unsigned int irqflags; |
Vijendar Mukunda | c62442bd | 2021-10-18 16:50:33 +0530 | [diff] [blame] | 153 | |
Vijendar Mukunda | cc0deaa | 2021-10-18 16:50:37 +0530 | [diff] [blame] | 154 | irqflags = IRQF_SHARED; |
Vijendar Mukunda | c62442bd | 2021-10-18 16:50:33 +0530 | [diff] [blame] | 155 | /* Yellow Carp device check */ |
| 156 | if (pci->revision != 0x60) |
| 157 | return -ENODEV; |
| 158 | |
| 159 | if (pci_enable_device(pci)) { |
| 160 | dev_err(&pci->dev, "pci_enable_device failed\n"); |
| 161 | return -ENODEV; |
| 162 | } |
| 163 | |
| 164 | ret = pci_request_regions(pci, "AMD ACP3x audio"); |
| 165 | if (ret < 0) { |
| 166 | dev_err(&pci->dev, "pci_request_regions failed\n"); |
| 167 | goto disable_pci; |
| 168 | } |
| 169 | |
| 170 | adata = devm_kzalloc(&pci->dev, sizeof(struct acp6x_dev_data), |
| 171 | GFP_KERNEL); |
| 172 | if (!adata) { |
| 173 | ret = -ENOMEM; |
| 174 | goto release_regions; |
| 175 | } |
| 176 | |
| 177 | addr = pci_resource_start(pci, 0); |
| 178 | adata->acp6x_base = devm_ioremap(&pci->dev, addr, |
| 179 | pci_resource_len(pci, 0)); |
| 180 | if (!adata->acp6x_base) { |
| 181 | ret = -ENOMEM; |
| 182 | goto release_regions; |
| 183 | } |
| 184 | pci_set_master(pci); |
| 185 | pci_set_drvdata(pci, adata); |
Vijendar Mukunda | 8c7161f | 2021-10-18 16:50:34 +0530 | [diff] [blame] | 186 | ret = acp6x_init(adata->acp6x_base); |
| 187 | if (ret) |
| 188 | goto release_regions; |
Vijendar Mukunda | fc329c1 | 2021-10-18 16:50:35 +0530 | [diff] [blame] | 189 | val = acp6x_readl(adata->acp6x_base + ACP_PIN_CONFIG); |
| 190 | switch (val) { |
| 191 | case ACP_CONFIG_0: |
| 192 | case ACP_CONFIG_1: |
| 193 | case ACP_CONFIG_2: |
| 194 | case ACP_CONFIG_3: |
| 195 | case ACP_CONFIG_9: |
| 196 | case ACP_CONFIG_15: |
| 197 | dev_info(&pci->dev, "Audio Mode %d\n", val); |
| 198 | break; |
| 199 | default: |
| 200 | adata->res = devm_kzalloc(&pci->dev, |
| 201 | sizeof(struct resource), |
| 202 | GFP_KERNEL); |
| 203 | if (!adata->res) { |
| 204 | ret = -ENOMEM; |
| 205 | goto de_init; |
| 206 | } |
Vijendar Mukunda | 8c7161f | 2021-10-18 16:50:34 +0530 | [diff] [blame] | 207 | |
Vijendar Mukunda | fc329c1 | 2021-10-18 16:50:35 +0530 | [diff] [blame] | 208 | adata->res->name = "acp_iomem"; |
| 209 | adata->res->flags = IORESOURCE_MEM; |
| 210 | adata->res->start = addr; |
| 211 | adata->res->end = addr + (ACP6x_REG_END - ACP6x_REG_START); |
| 212 | |
| 213 | adata->acp6x_audio_mode = ACP6x_PDM_MODE; |
| 214 | |
| 215 | memset(&pdevinfo, 0, sizeof(pdevinfo)); |
| 216 | pdevinfo[0].name = "acp_yc_pdm_dma"; |
| 217 | pdevinfo[0].id = 0; |
| 218 | pdevinfo[0].parent = &pci->dev; |
| 219 | pdevinfo[0].num_res = 1; |
| 220 | pdevinfo[0].res = adata->res; |
| 221 | |
| 222 | pdevinfo[1].name = "dmic-codec"; |
| 223 | pdevinfo[1].id = 0; |
| 224 | pdevinfo[1].parent = &pci->dev; |
| 225 | |
Vijendar Mukunda | 058dfdf | 2021-10-18 16:50:42 +0530 | [diff] [blame] | 226 | pdevinfo[2].name = "acp_yc_mach"; |
| 227 | pdevinfo[2].id = 0; |
| 228 | pdevinfo[2].parent = &pci->dev; |
| 229 | |
Vijendar Mukunda | fc329c1 | 2021-10-18 16:50:35 +0530 | [diff] [blame] | 230 | for (index = 0; index < ACP6x_DEVS; index++) { |
| 231 | adata->pdev[index] = |
| 232 | platform_device_register_full(&pdevinfo[index]); |
| 233 | if (IS_ERR(adata->pdev[index])) { |
| 234 | dev_err(&pci->dev, "cannot register %s device\n", |
| 235 | pdevinfo[index].name); |
| 236 | ret = PTR_ERR(adata->pdev[index]); |
| 237 | goto unregister_devs; |
| 238 | } |
| 239 | } |
| 240 | break; |
| 241 | } |
Vijendar Mukunda | cc0deaa | 2021-10-18 16:50:37 +0530 | [diff] [blame] | 242 | ret = devm_request_irq(&pci->dev, pci->irq, acp6x_irq_handler, |
| 243 | irqflags, "ACP_PCI_IRQ", adata); |
| 244 | if (ret) { |
| 245 | dev_err(&pci->dev, "ACP PCI IRQ request failed\n"); |
| 246 | goto unregister_devs; |
| 247 | } |
Vijendar Mukunda | c8212df | 2021-10-18 16:50:39 +0530 | [diff] [blame] | 248 | pm_runtime_set_autosuspend_delay(&pci->dev, ACP_SUSPEND_DELAY_MS); |
| 249 | pm_runtime_use_autosuspend(&pci->dev); |
| 250 | pm_runtime_put_noidle(&pci->dev); |
| 251 | pm_runtime_allow(&pci->dev); |
| 252 | |
Vijendar Mukunda | c62442bd | 2021-10-18 16:50:33 +0530 | [diff] [blame] | 253 | return 0; |
Vijendar Mukunda | fc329c1 | 2021-10-18 16:50:35 +0530 | [diff] [blame] | 254 | unregister_devs: |
| 255 | for (--index; index >= 0; index--) |
| 256 | platform_device_unregister(adata->pdev[index]); |
| 257 | de_init: |
| 258 | if (acp6x_deinit(adata->acp6x_base)) |
| 259 | dev_err(&pci->dev, "ACP de-init failed\n"); |
Vijendar Mukunda | c62442bd | 2021-10-18 16:50:33 +0530 | [diff] [blame] | 260 | release_regions: |
| 261 | pci_release_regions(pci); |
| 262 | disable_pci: |
| 263 | pci_disable_device(pci); |
| 264 | |
| 265 | return ret; |
| 266 | } |
| 267 | |
Vijendar Mukunda | c8212df | 2021-10-18 16:50:39 +0530 | [diff] [blame] | 268 | static int __maybe_unused snd_acp6x_suspend(struct device *dev) |
| 269 | { |
| 270 | struct acp6x_dev_data *adata; |
| 271 | int ret; |
| 272 | |
| 273 | adata = dev_get_drvdata(dev); |
| 274 | ret = acp6x_deinit(adata->acp6x_base); |
| 275 | if (ret) |
| 276 | dev_err(dev, "ACP de-init failed\n"); |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | static int __maybe_unused snd_acp6x_resume(struct device *dev) |
| 281 | { |
| 282 | struct acp6x_dev_data *adata; |
| 283 | int ret; |
| 284 | |
| 285 | adata = dev_get_drvdata(dev); |
| 286 | ret = acp6x_init(adata->acp6x_base); |
| 287 | if (ret) |
| 288 | dev_err(dev, "ACP init failed\n"); |
| 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | static const struct dev_pm_ops acp6x_pm = { |
| 293 | SET_RUNTIME_PM_OPS(snd_acp6x_suspend, snd_acp6x_resume, NULL) |
| 294 | SET_SYSTEM_SLEEP_PM_OPS(snd_acp6x_suspend, snd_acp6x_resume) |
| 295 | }; |
| 296 | |
Vijendar Mukunda | c62442bd | 2021-10-18 16:50:33 +0530 | [diff] [blame] | 297 | static void snd_acp6x_remove(struct pci_dev *pci) |
| 298 | { |
Vijendar Mukunda | 8c7161f | 2021-10-18 16:50:34 +0530 | [diff] [blame] | 299 | struct acp6x_dev_data *adata; |
Vijendar Mukunda | fc329c1 | 2021-10-18 16:50:35 +0530 | [diff] [blame] | 300 | int ret, index; |
Vijendar Mukunda | 8c7161f | 2021-10-18 16:50:34 +0530 | [diff] [blame] | 301 | |
| 302 | adata = pci_get_drvdata(pci); |
Vijendar Mukunda | fc329c1 | 2021-10-18 16:50:35 +0530 | [diff] [blame] | 303 | if (adata->acp6x_audio_mode == ACP6x_PDM_MODE) { |
| 304 | for (index = 0; index < ACP6x_DEVS; index++) |
| 305 | platform_device_unregister(adata->pdev[index]); |
| 306 | } |
Vijendar Mukunda | 8c7161f | 2021-10-18 16:50:34 +0530 | [diff] [blame] | 307 | ret = acp6x_deinit(adata->acp6x_base); |
| 308 | if (ret) |
| 309 | dev_err(&pci->dev, "ACP de-init failed\n"); |
Vijendar Mukunda | c8212df | 2021-10-18 16:50:39 +0530 | [diff] [blame] | 310 | pm_runtime_forbid(&pci->dev); |
| 311 | pm_runtime_get_noresume(&pci->dev); |
Vijendar Mukunda | c62442bd | 2021-10-18 16:50:33 +0530 | [diff] [blame] | 312 | pci_release_regions(pci); |
| 313 | pci_disable_device(pci); |
| 314 | } |
| 315 | |
| 316 | static const struct pci_device_id snd_acp6x_ids[] = { |
| 317 | { PCI_DEVICE(PCI_VENDOR_ID_AMD, ACP_DEVICE_ID), |
| 318 | .class = PCI_CLASS_MULTIMEDIA_OTHER << 8, |
| 319 | .class_mask = 0xffffff }, |
| 320 | { 0, }, |
| 321 | }; |
| 322 | MODULE_DEVICE_TABLE(pci, snd_acp6x_ids); |
| 323 | |
| 324 | static struct pci_driver yc_acp6x_driver = { |
| 325 | .name = KBUILD_MODNAME, |
| 326 | .id_table = snd_acp6x_ids, |
| 327 | .probe = snd_acp6x_probe, |
| 328 | .remove = snd_acp6x_remove, |
Vijendar Mukunda | c8212df | 2021-10-18 16:50:39 +0530 | [diff] [blame] | 329 | .driver = { |
| 330 | .pm = &acp6x_pm, |
| 331 | } |
Vijendar Mukunda | c62442bd | 2021-10-18 16:50:33 +0530 | [diff] [blame] | 332 | }; |
| 333 | |
| 334 | module_pci_driver(yc_acp6x_driver); |
| 335 | |
| 336 | MODULE_AUTHOR("Vijendar.Mukunda@amd.com"); |
| 337 | MODULE_DESCRIPTION("AMD ACP Yellow Carp PCI driver"); |
| 338 | MODULE_LICENSE("GPL v2"); |