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