1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 //
3 // This file is provided under a dual BSD/GPLv2 license. When using or
4 // redistributing this file, you may do so under either license.
5 //
6 // Copyright(c) 2022 Advanced Micro Devices, Inc. All rights reserved.
7 //
8 // Authors: Ajit Kumar Pandey <AjitKumar.Pandey@amd.com>
9
10 /*
11 * Generic PCI interface for ACP device
12 */
13
14 #include <linux/delay.h>
15 #include <linux/interrupt.h>
16 #include <linux/pci.h>
17 #include <linux/platform_device.h>
18 #include <linux/module.h>
19 #include <linux/pm_runtime.h>
20
21 #include "amd.h"
22 #include "../mach-config.h"
23
24 #define DRV_NAME "acp_pci"
25
26 #define ACP3x_REG_START 0x1240000
27 #define ACP3x_REG_END 0x125C000
28
29 static struct platform_device *dmic_dev;
30 static struct platform_device *pdev;
31
32 static const struct resource acp_res[] = {
33 {
34 .start = 0,
35 .end = ACP3x_REG_END - ACP3x_REG_START,
36 .name = "acp_mem",
37 .flags = IORESOURCE_MEM,
38 },
39 {
40 .start = 0,
41 .end = 0,
42 .name = "acp_dai_irq",
43 .flags = IORESOURCE_IRQ,
44 },
45 };
46
acp_pci_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)47 static int acp_pci_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
48 {
49 struct platform_device_info pdevinfo;
50 struct device *dev = &pci->dev;
51 const struct resource *res_acp;
52 struct acp_chip_info *chip;
53 struct resource *res;
54 unsigned int flag, addr, num_res, i;
55 int ret;
56
57 flag = snd_amd_acp_find_config(pci);
58 if (flag != FLAG_AMD_LEGACY && flag != FLAG_AMD_LEGACY_ONLY_DMIC)
59 return -ENODEV;
60
61 chip = devm_kzalloc(&pci->dev, sizeof(*chip), GFP_KERNEL);
62 if (!chip)
63 return -ENOMEM;
64
65 if (pci_enable_device(pci))
66 return dev_err_probe(&pci->dev, -ENODEV,
67 "pci_enable_device failed\n");
68
69 ret = pci_request_regions(pci, "AMD ACP3x audio");
70 if (ret < 0) {
71 dev_err(&pci->dev, "pci_request_regions failed\n");
72 ret = -ENOMEM;
73 goto disable_pci;
74 }
75
76 pci_set_master(pci);
77
78 res_acp = acp_res;
79 num_res = ARRAY_SIZE(acp_res);
80
81 switch (pci->revision) {
82 case 0x01:
83 chip->name = "acp_asoc_renoir";
84 chip->acp_rev = ACP3X_DEV;
85 break;
86 case 0x6f:
87 chip->name = "acp_asoc_rembrandt";
88 chip->acp_rev = ACP6X_DEV;
89 break;
90 case 0x63:
91 chip->name = "acp_asoc_acp63";
92 chip->acp_rev = ACP63_DEV;
93 break;
94 case 0x70:
95 chip->name = "acp_asoc_acp70";
96 chip->acp_rev = ACP70_DEV;
97 break;
98 case 0x71:
99 chip->name = "acp_asoc_acp70";
100 chip->acp_rev = ACP71_DEV;
101 break;
102 default:
103 dev_err(dev, "Unsupported device revision:0x%x\n", pci->revision);
104 ret = -EINVAL;
105 goto release_regions;
106 }
107 chip->flag = flag;
108 dmic_dev = platform_device_register_data(dev, "dmic-codec", PLATFORM_DEVID_NONE, NULL, 0);
109 if (IS_ERR(dmic_dev)) {
110 dev_err(dev, "failed to create DMIC device\n");
111 ret = PTR_ERR(dmic_dev);
112 goto release_regions;
113 }
114
115 addr = pci_resource_start(pci, 0);
116 chip->base = devm_ioremap(&pci->dev, addr, pci_resource_len(pci, 0));
117 if (!chip->base) {
118 ret = -ENOMEM;
119 goto unregister_dmic_dev;
120 }
121
122 ret = acp_init(chip);
123 if (ret)
124 goto unregister_dmic_dev;
125
126 check_acp_config(pci, chip);
127 if (!chip->is_pdm_dev && !chip->is_i2s_config)
128 goto skip_pdev_creation;
129
130 res = devm_kcalloc(&pci->dev, num_res, sizeof(struct resource), GFP_KERNEL);
131 if (!res) {
132 ret = -ENOMEM;
133 goto unregister_dmic_dev;
134 }
135
136 for (i = 0; i < num_res; i++, res_acp++) {
137 res[i].name = res_acp->name;
138 res[i].flags = res_acp->flags;
139 res[i].start = addr + res_acp->start;
140 res[i].end = addr + res_acp->end;
141 if (res_acp->flags == IORESOURCE_IRQ) {
142 res[i].start = pci->irq;
143 res[i].end = res[i].start;
144 }
145 }
146
147 memset(&pdevinfo, 0, sizeof(pdevinfo));
148
149 pdevinfo.name = chip->name;
150 pdevinfo.id = 0;
151 pdevinfo.parent = &pci->dev;
152 pdevinfo.num_res = num_res;
153 pdevinfo.res = &res[0];
154 pdevinfo.data = chip;
155 pdevinfo.size_data = sizeof(*chip);
156
157 pdev = platform_device_register_full(&pdevinfo);
158 if (IS_ERR(pdev)) {
159 dev_err(&pci->dev, "cannot register %s device\n", pdevinfo.name);
160 ret = PTR_ERR(pdev);
161 goto unregister_dmic_dev;
162 }
163
164 skip_pdev_creation:
165 chip->chip_pdev = pdev;
166 dev_set_drvdata(&pci->dev, chip);
167 pm_runtime_set_autosuspend_delay(&pci->dev, 2000);
168 pm_runtime_use_autosuspend(&pci->dev);
169 pm_runtime_put_noidle(&pci->dev);
170 pm_runtime_allow(&pci->dev);
171 return ret;
172
173 unregister_dmic_dev:
174 platform_device_unregister(dmic_dev);
175 release_regions:
176 pci_release_regions(pci);
177 disable_pci:
178 pci_disable_device(pci);
179
180 return ret;
181 };
182
snd_acp_suspend(struct device * dev)183 static int __maybe_unused snd_acp_suspend(struct device *dev)
184 {
185 struct acp_chip_info *chip;
186 int ret;
187
188 chip = dev_get_drvdata(dev);
189 ret = acp_deinit(chip);
190 if (ret)
191 dev_err(dev, "ACP de-init failed\n");
192 return ret;
193 }
194
snd_acp_resume(struct device * dev)195 static int __maybe_unused snd_acp_resume(struct device *dev)
196 {
197 struct acp_chip_info *chip;
198 struct acp_dev_data *adata;
199 struct device child;
200 int ret;
201
202 chip = dev_get_drvdata(dev);
203 ret = acp_init(chip);
204 if (ret)
205 dev_err(dev, "ACP init failed\n");
206 if (chip->chip_pdev) {
207 child = chip->chip_pdev->dev;
208 adata = dev_get_drvdata(&child);
209 if (adata)
210 acp_enable_interrupts(adata);
211 }
212 return ret;
213 }
214
215 static const struct dev_pm_ops acp_pm_ops = {
216 SET_RUNTIME_PM_OPS(snd_acp_suspend, snd_acp_resume, NULL)
217 SET_SYSTEM_SLEEP_PM_OPS(snd_acp_suspend, snd_acp_resume)
218 };
219
acp_pci_remove(struct pci_dev * pci)220 static void acp_pci_remove(struct pci_dev *pci)
221 {
222 struct acp_chip_info *chip;
223 int ret;
224
225 chip = pci_get_drvdata(pci);
226 pm_runtime_forbid(&pci->dev);
227 pm_runtime_get_noresume(&pci->dev);
228 if (dmic_dev)
229 platform_device_unregister(dmic_dev);
230 if (pdev)
231 platform_device_unregister(pdev);
232 ret = acp_deinit(chip);
233 if (ret)
234 dev_err(&pci->dev, "ACP de-init failed\n");
235 }
236
237 /* PCI IDs */
238 static const struct pci_device_id acp_pci_ids[] = {
239 { PCI_DEVICE(PCI_VENDOR_ID_AMD, ACP_PCI_DEV_ID)},
240 { 0, }
241 };
242 MODULE_DEVICE_TABLE(pci, acp_pci_ids);
243
244 /* pci_driver definition */
245 static struct pci_driver snd_amd_acp_pci_driver = {
246 .name = KBUILD_MODNAME,
247 .id_table = acp_pci_ids,
248 .probe = acp_pci_probe,
249 .remove = acp_pci_remove,
250 .driver = {
251 .pm = &acp_pm_ops,
252 },
253 };
254 module_pci_driver(snd_amd_acp_pci_driver);
255
256 MODULE_DESCRIPTION("AMD ACP common PCI support");
257 MODULE_LICENSE("Dual BSD/GPL");
258 MODULE_IMPORT_NS(SND_SOC_ACP_COMMON);
259 MODULE_ALIAS(DRV_NAME);
260