1 // SPDX-License-Identifier: GPL-2.0+
3 * AMD Yellow Carp ACP PCI Driver
5 * Copyright 2021 Advanced Micro Devices, Inc.
9 #include <linux/module.h>
11 #include <linux/delay.h>
12 #include <linux/platform_device.h>
13 #include <linux/interrupt.h>
14 #include <sound/pcm_params.h>
15 #include <linux/pm_runtime.h>
19 struct acp6x_dev_data {
20 void __iomem *acp6x_base;
22 bool acp6x_audio_mode;
23 struct platform_device *pdev[ACP6x_DEVS];
26 static int acp6x_power_on(void __iomem *acp_base)
31 val = acp6x_readl(acp_base + ACP_PGFSM_STATUS);
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);
39 while (++timeout < 500) {
40 val = acp6x_readl(acp_base + ACP_PGFSM_STATUS);
48 static int acp6x_reset(void __iomem *acp_base)
53 acp6x_writel(1, acp_base + ACP_SOFT_RESET);
55 while (++timeout < 500) {
56 val = acp6x_readl(acp_base + ACP_SOFT_RESET);
57 if (val & ACP_SOFT_RESET_SOFTRESET_AUDDONE_MASK)
61 acp6x_writel(0, acp_base + ACP_SOFT_RESET);
63 while (++timeout < 500) {
64 val = acp6x_readl(acp_base + ACP_SOFT_RESET);
72 static void acp6x_enable_interrupts(void __iomem *acp_base)
74 acp6x_writel(0x01, acp_base + ACP_EXTERNAL_INTR_ENB);
77 static void acp6x_disable_interrupts(void __iomem *acp_base)
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);
85 static int acp6x_init(void __iomem *acp_base)
90 ret = acp6x_power_on(acp_base);
92 pr_err("ACP power on failed\n");
95 acp6x_writel(0x01, acp_base + ACP_CONTROL);
97 ret = acp6x_reset(acp_base);
99 pr_err("ACP reset failed\n");
102 acp6x_writel(0x03, acp_base + ACP_CLKMUX_SEL);
103 acp6x_enable_interrupts(acp_base);
107 static int acp6x_deinit(void __iomem *acp_base)
111 acp6x_disable_interrupts(acp_base);
113 ret = acp6x_reset(acp_base);
115 pr_err("ACP reset failed\n");
118 acp6x_writel(0x00, acp_base + ACP_CLKMUX_SEL);
119 acp6x_writel(0x00, acp_base + ACP_CONTROL);
123 static irqreturn_t acp6x_irq_handler(int irq, void *dev_id)
125 struct acp6x_dev_data *adata;
126 struct pdm_dev_data *yc_pdm_data;
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);
144 static int snd_acp6x_probe(struct pci_dev *pci,
145 const struct pci_device_id *pci_id)
147 struct acp6x_dev_data *adata;
148 struct platform_device_info pdevinfo[ACP6x_DEVS];
152 unsigned int irqflags;
155 irqflags = IRQF_SHARED;
156 /* Yellow Carp device check */
157 switch (pci->revision) {
162 dev_err(&pci->dev, "acp6x pci device not found\n");
165 if (pci_enable_device(pci)) {
166 dev_err(&pci->dev, "pci_enable_device failed\n");
170 ret = pci_request_regions(pci, "AMD ACP3x audio");
172 dev_err(&pci->dev, "pci_request_regions failed\n");
176 adata = devm_kzalloc(&pci->dev, sizeof(struct acp6x_dev_data),
180 goto release_regions;
183 addr = pci_resource_start(pci, 0);
184 adata->acp6x_base = devm_ioremap(&pci->dev, addr,
185 pci_resource_len(pci, 0));
186 if (!adata->acp6x_base) {
188 goto release_regions;
191 pci_set_drvdata(pci, adata);
192 ret = acp6x_init(adata->acp6x_base);
194 goto release_regions;
195 val = acp6x_readl(adata->acp6x_base + ACP_PIN_CONFIG);
203 dev_info(&pci->dev, "Audio Mode %d\n", val);
206 adata->res = devm_kzalloc(&pci->dev,
207 sizeof(struct resource),
214 adata->res->name = "acp_iomem";
215 adata->res->flags = IORESOURCE_MEM;
216 adata->res->start = addr;
217 adata->res->end = addr + (ACP6x_REG_END - ACP6x_REG_START);
219 adata->acp6x_audio_mode = ACP6x_PDM_MODE;
221 memset(&pdevinfo, 0, sizeof(pdevinfo));
222 pdevinfo[0].name = "acp_yc_pdm_dma";
224 pdevinfo[0].parent = &pci->dev;
225 pdevinfo[0].num_res = 1;
226 pdevinfo[0].res = adata->res;
228 pdevinfo[1].name = "dmic-codec";
230 pdevinfo[1].parent = &pci->dev;
232 pdevinfo[2].name = "acp_yc_mach";
234 pdevinfo[2].parent = &pci->dev;
236 for (index = 0; index < ACP6x_DEVS; index++) {
238 platform_device_register_full(&pdevinfo[index]);
239 if (IS_ERR(adata->pdev[index])) {
240 dev_err(&pci->dev, "cannot register %s device\n",
241 pdevinfo[index].name);
242 ret = PTR_ERR(adata->pdev[index]);
243 goto unregister_devs;
248 ret = devm_request_irq(&pci->dev, pci->irq, acp6x_irq_handler,
249 irqflags, "ACP_PCI_IRQ", adata);
251 dev_err(&pci->dev, "ACP PCI IRQ request failed\n");
252 goto unregister_devs;
254 pm_runtime_set_autosuspend_delay(&pci->dev, ACP_SUSPEND_DELAY_MS);
255 pm_runtime_use_autosuspend(&pci->dev);
256 pm_runtime_put_noidle(&pci->dev);
257 pm_runtime_allow(&pci->dev);
261 for (--index; index >= 0; index--)
262 platform_device_unregister(adata->pdev[index]);
264 if (acp6x_deinit(adata->acp6x_base))
265 dev_err(&pci->dev, "ACP de-init failed\n");
267 pci_release_regions(pci);
269 pci_disable_device(pci);
274 static int __maybe_unused snd_acp6x_suspend(struct device *dev)
276 struct acp6x_dev_data *adata;
279 adata = dev_get_drvdata(dev);
280 ret = acp6x_deinit(adata->acp6x_base);
282 dev_err(dev, "ACP de-init failed\n");
286 static int __maybe_unused snd_acp6x_resume(struct device *dev)
288 struct acp6x_dev_data *adata;
291 adata = dev_get_drvdata(dev);
292 ret = acp6x_init(adata->acp6x_base);
294 dev_err(dev, "ACP init failed\n");
298 static const struct dev_pm_ops acp6x_pm = {
299 SET_RUNTIME_PM_OPS(snd_acp6x_suspend, snd_acp6x_resume, NULL)
300 SET_SYSTEM_SLEEP_PM_OPS(snd_acp6x_suspend, snd_acp6x_resume)
303 static void snd_acp6x_remove(struct pci_dev *pci)
305 struct acp6x_dev_data *adata;
308 adata = pci_get_drvdata(pci);
309 if (adata->acp6x_audio_mode == ACP6x_PDM_MODE) {
310 for (index = 0; index < ACP6x_DEVS; index++)
311 platform_device_unregister(adata->pdev[index]);
313 ret = acp6x_deinit(adata->acp6x_base);
315 dev_err(&pci->dev, "ACP de-init failed\n");
316 pm_runtime_forbid(&pci->dev);
317 pm_runtime_get_noresume(&pci->dev);
318 pci_release_regions(pci);
319 pci_disable_device(pci);
322 static const struct pci_device_id snd_acp6x_ids[] = {
323 { PCI_DEVICE(PCI_VENDOR_ID_AMD, ACP_DEVICE_ID),
324 .class = PCI_CLASS_MULTIMEDIA_OTHER << 8,
325 .class_mask = 0xffffff },
328 MODULE_DEVICE_TABLE(pci, snd_acp6x_ids);
330 static struct pci_driver yc_acp6x_driver = {
331 .name = KBUILD_MODNAME,
332 .id_table = snd_acp6x_ids,
333 .probe = snd_acp6x_probe,
334 .remove = snd_acp6x_remove,
340 module_pci_driver(yc_acp6x_driver);
342 MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
343 MODULE_DESCRIPTION("AMD ACP Yellow Carp PCI driver");
344 MODULE_LICENSE("GPL v2");