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
rn_acp_power_on(void __iomem * acp_base)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
rn_acp_power_off(void __iomem * acp_base)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
rn_acp_reset(void __iomem * acp_base)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
rn_acp_enable_interrupts(void __iomem * acp_base)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
rn_acp_disable_interrupts(void __iomem * acp_base)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
rn_acp_init(void __iomem * acp_base)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
rn_acp_deinit(void __iomem * acp_base)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 S340-14API */
169 .matches = {
170 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
171 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "81NB"),
172 }
173 },
174 {
175 /* Lenovo IdeaPad Flex 5 14ARE05 */
176 .matches = {
177 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
178 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "81X2"),
179 }
180 },
181 {
182 /* Lenovo IdeaPad 5 15ARE05 */
183 .matches = {
184 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
185 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "81YQ"),
186 }
187 },
188 {
189 /* Lenovo ThinkPad X395 */
190 .matches = {
191 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
192 DMI_EXACT_MATCH(DMI_BOARD_NAME, "20NLCTO1WW"),
193 }
194 },
195 {}
196 };
197
snd_rn_acp_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)198 static int snd_rn_acp_probe(struct pci_dev *pci,
199 const struct pci_device_id *pci_id)
200 {
201 struct acp_dev_data *adata;
202 struct platform_device_info pdevinfo[ACP_DEVS];
203 #if defined(CONFIG_ACPI)
204 acpi_handle handle;
205 acpi_integer dmic_status;
206 #endif
207 const struct dmi_system_id *dmi_id;
208 unsigned int irqflags;
209 int ret, index;
210 u32 addr;
211
212 /* Renoir device check */
213 if (pci->revision != 0x01)
214 return -ENODEV;
215
216 if (pci_enable_device(pci)) {
217 dev_err(&pci->dev, "pci_enable_device failed\n");
218 return -ENODEV;
219 }
220
221 ret = pci_request_regions(pci, "AMD ACP3x audio");
222 if (ret < 0) {
223 dev_err(&pci->dev, "pci_request_regions failed\n");
224 goto disable_pci;
225 }
226
227 adata = devm_kzalloc(&pci->dev, sizeof(struct acp_dev_data),
228 GFP_KERNEL);
229 if (!adata) {
230 ret = -ENOMEM;
231 goto release_regions;
232 }
233
234 /* check for msi interrupt support */
235 ret = pci_enable_msi(pci);
236 if (ret)
237 /* msi is not enabled */
238 irqflags = IRQF_SHARED;
239 else
240 /* msi is enabled */
241 irqflags = 0;
242
243 addr = pci_resource_start(pci, 0);
244 adata->acp_base = devm_ioremap(&pci->dev, addr,
245 pci_resource_len(pci, 0));
246 if (!adata->acp_base) {
247 ret = -ENOMEM;
248 goto disable_msi;
249 }
250 pci_set_master(pci);
251 pci_set_drvdata(pci, adata);
252 ret = rn_acp_init(adata->acp_base);
253 if (ret)
254 goto disable_msi;
255
256 if (!dmic_acpi_check) {
257 ret = -ENODEV;
258 goto de_init;
259 } else if (dmic_acpi_check == ACP_DMIC_AUTO) {
260 #if defined(CONFIG_ACPI)
261 handle = ACPI_HANDLE(&pci->dev);
262 ret = acpi_evaluate_integer(handle, "_WOV", NULL, &dmic_status);
263 if (ACPI_FAILURE(ret)) {
264 ret = -EINVAL;
265 goto de_init;
266 }
267 if (!dmic_status) {
268 ret = -ENODEV;
269 goto de_init;
270 }
271 #endif
272 dmi_id = dmi_first_match(rn_acp_quirk_table);
273 if (dmi_id && !dmi_id->driver_data) {
274 dev_info(&pci->dev, "ACPI settings override using DMI (ACP mic is not present)");
275 ret = -ENODEV;
276 goto de_init;
277 }
278 }
279
280 adata->res = devm_kzalloc(&pci->dev,
281 sizeof(struct resource) * 2,
282 GFP_KERNEL);
283 if (!adata->res) {
284 ret = -ENOMEM;
285 goto de_init;
286 }
287
288 adata->res[0].name = "acp_pdm_iomem";
289 adata->res[0].flags = IORESOURCE_MEM;
290 adata->res[0].start = addr;
291 adata->res[0].end = addr + (ACP_REG_END - ACP_REG_START);
292 adata->res[1].name = "acp_pdm_irq";
293 adata->res[1].flags = IORESOURCE_IRQ;
294 adata->res[1].start = pci->irq;
295 adata->res[1].end = pci->irq;
296
297 memset(&pdevinfo, 0, sizeof(pdevinfo));
298 pdevinfo[0].name = "acp_rn_pdm_dma";
299 pdevinfo[0].id = 0;
300 pdevinfo[0].parent = &pci->dev;
301 pdevinfo[0].num_res = 2;
302 pdevinfo[0].res = adata->res;
303 pdevinfo[0].data = &irqflags;
304 pdevinfo[0].size_data = sizeof(irqflags);
305
306 pdevinfo[1].name = "dmic-codec";
307 pdevinfo[1].id = 0;
308 pdevinfo[1].parent = &pci->dev;
309 pdevinfo[2].name = "acp_pdm_mach";
310 pdevinfo[2].id = 0;
311 pdevinfo[2].parent = &pci->dev;
312 for (index = 0; index < ACP_DEVS; index++) {
313 adata->pdev[index] =
314 platform_device_register_full(&pdevinfo[index]);
315 if (IS_ERR(adata->pdev[index])) {
316 dev_err(&pci->dev, "cannot register %s device\n",
317 pdevinfo[index].name);
318 ret = PTR_ERR(adata->pdev[index]);
319 goto unregister_devs;
320 }
321 }
322 pm_runtime_set_autosuspend_delay(&pci->dev, ACP_SUSPEND_DELAY_MS);
323 pm_runtime_use_autosuspend(&pci->dev);
324 pm_runtime_put_noidle(&pci->dev);
325 pm_runtime_allow(&pci->dev);
326 return 0;
327
328 unregister_devs:
329 for (index = 0; index < ACP_DEVS; index++)
330 platform_device_unregister(adata->pdev[index]);
331 de_init:
332 if (rn_acp_deinit(adata->acp_base))
333 dev_err(&pci->dev, "ACP de-init failed\n");
334 disable_msi:
335 pci_disable_msi(pci);
336 release_regions:
337 pci_release_regions(pci);
338 disable_pci:
339 pci_disable_device(pci);
340
341 return ret;
342 }
343
snd_rn_acp_suspend(struct device * dev)344 static int snd_rn_acp_suspend(struct device *dev)
345 {
346 int ret;
347 struct acp_dev_data *adata;
348
349 adata = dev_get_drvdata(dev);
350 ret = rn_acp_deinit(adata->acp_base);
351 if (ret)
352 dev_err(dev, "ACP de-init failed\n");
353 else
354 dev_dbg(dev, "ACP de-initialized\n");
355
356 return ret;
357 }
358
snd_rn_acp_resume(struct device * dev)359 static int snd_rn_acp_resume(struct device *dev)
360 {
361 int ret;
362 struct acp_dev_data *adata;
363
364 adata = dev_get_drvdata(dev);
365 ret = rn_acp_init(adata->acp_base);
366 if (ret) {
367 dev_err(dev, "ACP init failed\n");
368 return ret;
369 }
370 return 0;
371 }
372
373 static const struct dev_pm_ops rn_acp_pm = {
374 .runtime_suspend = snd_rn_acp_suspend,
375 .runtime_resume = snd_rn_acp_resume,
376 .suspend = snd_rn_acp_suspend,
377 .resume = snd_rn_acp_resume,
378 };
379
snd_rn_acp_remove(struct pci_dev * pci)380 static void snd_rn_acp_remove(struct pci_dev *pci)
381 {
382 struct acp_dev_data *adata;
383 int ret, index;
384
385 adata = pci_get_drvdata(pci);
386 for (index = 0; index < ACP_DEVS; index++)
387 platform_device_unregister(adata->pdev[index]);
388 ret = rn_acp_deinit(adata->acp_base);
389 if (ret)
390 dev_err(&pci->dev, "ACP de-init failed\n");
391 pm_runtime_forbid(&pci->dev);
392 pm_runtime_get_noresume(&pci->dev);
393 pci_disable_msi(pci);
394 pci_release_regions(pci);
395 pci_disable_device(pci);
396 }
397
398 static const struct pci_device_id snd_rn_acp_ids[] = {
399 { PCI_DEVICE(PCI_VENDOR_ID_AMD, ACP_DEVICE_ID),
400 .class = PCI_CLASS_MULTIMEDIA_OTHER << 8,
401 .class_mask = 0xffffff },
402 { 0, },
403 };
404 MODULE_DEVICE_TABLE(pci, snd_rn_acp_ids);
405
406 static struct pci_driver rn_acp_driver = {
407 .name = KBUILD_MODNAME,
408 .id_table = snd_rn_acp_ids,
409 .probe = snd_rn_acp_probe,
410 .remove = snd_rn_acp_remove,
411 .driver = {
412 .pm = &rn_acp_pm,
413 }
414 };
415
416 module_pci_driver(rn_acp_driver);
417
418 MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
419 MODULE_DESCRIPTION("AMD ACP Renoir PCI driver");
420 MODULE_LICENSE("GPL v2");
421