xref: /openbmc/linux/sound/soc/amd/vangogh/pci-acp5x.c (revision 887069f4)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // AMD Vangogh ACP PCI Driver
4 //
5 // Copyright (C) 2021 Advanced Micro Devices, Inc. All rights reserved.
6 
7 #include <linux/pci.h>
8 #include <linux/module.h>
9 #include <linux/io.h>
10 #include <linux/delay.h>
11 #include <linux/platform_device.h>
12 #include <linux/interrupt.h>
13 #include <linux/pm_runtime.h>
14 
15 #include "acp5x.h"
16 
17 struct acp5x_dev_data {
18 	void __iomem *acp5x_base;
19 	bool acp5x_audio_mode;
20 	struct resource *res;
21 	struct platform_device *pdev[ACP5x_DEVS];
22 };
23 
24 static int acp5x_power_on(void __iomem *acp5x_base)
25 {
26 	u32 val;
27 	int timeout;
28 
29 	val = acp_readl(acp5x_base + ACP_PGFSM_STATUS);
30 
31 	if (val == 0)
32 		return val;
33 
34 	if ((val & ACP_PGFSM_STATUS_MASK) !=
35 				ACP_POWER_ON_IN_PROGRESS)
36 		acp_writel(ACP_PGFSM_CNTL_POWER_ON_MASK,
37 			   acp5x_base + ACP_PGFSM_CONTROL);
38 	timeout = 0;
39 	while (++timeout < 500) {
40 		val = acp_readl(acp5x_base + ACP_PGFSM_STATUS);
41 		if ((val & ACP_PGFSM_STATUS_MASK) == ACP_POWERED_ON)
42 			return 0;
43 		udelay(1);
44 	}
45 	return -ETIMEDOUT;
46 }
47 
48 static int acp5x_reset(void __iomem *acp5x_base)
49 {
50 	u32 val;
51 	int timeout;
52 
53 	acp_writel(1, acp5x_base + ACP_SOFT_RESET);
54 	timeout = 0;
55 	while (++timeout < 500) {
56 		val = acp_readl(acp5x_base + ACP_SOFT_RESET);
57 		if (val & ACP_SOFT_RESET_SOFTRESET_AUDDONE_MASK)
58 			break;
59 		cpu_relax();
60 	}
61 	acp_writel(0, acp5x_base + ACP_SOFT_RESET);
62 	timeout = 0;
63 	while (++timeout < 500) {
64 		val = acp_readl(acp5x_base + ACP_SOFT_RESET);
65 		if (!val)
66 			return 0;
67 		cpu_relax();
68 	}
69 	return -ETIMEDOUT;
70 }
71 
72 static void acp5x_enable_interrupts(void __iomem *acp5x_base)
73 {
74 	acp_writel(0x01, acp5x_base + ACP_EXTERNAL_INTR_ENB);
75 }
76 
77 static void acp5x_disable_interrupts(void __iomem *acp5x_base)
78 {
79 	acp_writel(ACP_EXT_INTR_STAT_CLEAR_MASK, acp5x_base +
80 		   ACP_EXTERNAL_INTR_STAT);
81 	acp_writel(0x00, acp5x_base + ACP_EXTERNAL_INTR_CNTL);
82 	acp_writel(0x00, acp5x_base + ACP_EXTERNAL_INTR_ENB);
83 }
84 
85 static int acp5x_init(void __iomem *acp5x_base)
86 {
87 	int ret;
88 
89 	/* power on */
90 	ret = acp5x_power_on(acp5x_base);
91 	if (ret) {
92 		pr_err("ACP5x power on failed\n");
93 		return ret;
94 	}
95 	/* Reset */
96 	ret = acp5x_reset(acp5x_base);
97 	if (ret) {
98 		pr_err("ACP5x reset failed\n");
99 		return ret;
100 	}
101 	acp5x_enable_interrupts(acp5x_base);
102 	return 0;
103 }
104 
105 static int acp5x_deinit(void __iomem *acp5x_base)
106 {
107 	int ret;
108 
109 	acp5x_disable_interrupts(acp5x_base);
110 	/* Reset */
111 	ret = acp5x_reset(acp5x_base);
112 	if (ret) {
113 		pr_err("ACP5x reset failed\n");
114 		return ret;
115 	}
116 	return 0;
117 }
118 
119 static int snd_acp5x_probe(struct pci_dev *pci,
120 			   const struct pci_device_id *pci_id)
121 {
122 	struct acp5x_dev_data *adata;
123 	struct platform_device_info pdevinfo[ACP5x_DEVS];
124 	unsigned int irqflags;
125 	int ret, i;
126 	u32 addr, val;
127 
128 	irqflags = IRQF_SHARED;
129 	if (pci->revision != 0x50)
130 		return -ENODEV;
131 
132 	if (pci_enable_device(pci)) {
133 		dev_err(&pci->dev, "pci_enable_device failed\n");
134 		return -ENODEV;
135 	}
136 
137 	ret = pci_request_regions(pci, "AMD ACP5x audio");
138 	if (ret < 0) {
139 		dev_err(&pci->dev, "pci_request_regions failed\n");
140 		goto disable_pci;
141 	}
142 
143 	adata = devm_kzalloc(&pci->dev, sizeof(struct acp5x_dev_data),
144 			     GFP_KERNEL);
145 	if (!adata) {
146 		ret = -ENOMEM;
147 		goto release_regions;
148 	}
149 	addr = pci_resource_start(pci, 0);
150 	adata->acp5x_base = devm_ioremap(&pci->dev, addr,
151 					 pci_resource_len(pci, 0));
152 	if (!adata->acp5x_base) {
153 		ret = -ENOMEM;
154 		goto release_regions;
155 	}
156 	pci_set_master(pci);
157 	pci_set_drvdata(pci, adata);
158 	ret = acp5x_init(adata->acp5x_base);
159 	if (ret)
160 		goto release_regions;
161 
162 	val = acp_readl(adata->acp5x_base + ACP_PIN_CONFIG);
163 	switch (val) {
164 	case I2S_MODE:
165 		adata->res = devm_kzalloc(&pci->dev,
166 					  sizeof(struct resource) * ACP5x_RES,
167 					  GFP_KERNEL);
168 		if (!adata->res) {
169 			ret = -ENOMEM;
170 			goto de_init;
171 		}
172 
173 		adata->res[0].name = "acp5x_i2s_iomem";
174 		adata->res[0].flags = IORESOURCE_MEM;
175 		adata->res[0].start = addr;
176 		adata->res[0].end = addr + (ACP5x_REG_END - ACP5x_REG_START);
177 
178 		adata->res[1].name = "acp5x_i2s_sp";
179 		adata->res[1].flags = IORESOURCE_MEM;
180 		adata->res[1].start = addr + ACP5x_I2STDM_REG_START;
181 		adata->res[1].end = addr + ACP5x_I2STDM_REG_END;
182 
183 		adata->res[2].name = "acp5x_i2s_hs";
184 		adata->res[2].flags = IORESOURCE_MEM;
185 		adata->res[2].start = addr + ACP5x_HS_TDM_REG_START;
186 		adata->res[2].end = addr + ACP5x_HS_TDM_REG_END;
187 
188 		adata->res[3].name = "acp5x_i2s_irq";
189 		adata->res[3].flags = IORESOURCE_IRQ;
190 		adata->res[3].start = pci->irq;
191 		adata->res[3].end = adata->res[3].start;
192 
193 		adata->acp5x_audio_mode = ACP5x_I2S_MODE;
194 
195 		memset(&pdevinfo, 0, sizeof(pdevinfo));
196 		pdevinfo[0].name = "acp5x_i2s_dma";
197 		pdevinfo[0].id = 0;
198 		pdevinfo[0].parent = &pci->dev;
199 		pdevinfo[0].num_res = 4;
200 		pdevinfo[0].res = &adata->res[0];
201 		pdevinfo[0].data = &irqflags;
202 		pdevinfo[0].size_data = sizeof(irqflags);
203 
204 		pdevinfo[1].name = "acp5x_i2s_playcap";
205 		pdevinfo[1].id = 0;
206 		pdevinfo[1].parent = &pci->dev;
207 		pdevinfo[1].num_res = 1;
208 		pdevinfo[1].res = &adata->res[1];
209 
210 		pdevinfo[2].name = "acp5x_i2s_playcap";
211 		pdevinfo[2].id = 1;
212 		pdevinfo[2].parent = &pci->dev;
213 		pdevinfo[2].num_res = 1;
214 		pdevinfo[2].res = &adata->res[2];
215 
216 		for (i = 0; i < ACP5x_DEVS; i++) {
217 			adata->pdev[i] =
218 				platform_device_register_full(&pdevinfo[i]);
219 			if (IS_ERR(adata->pdev[i])) {
220 				dev_err(&pci->dev, "cannot register %s device\n",
221 					pdevinfo[i].name);
222 				ret = PTR_ERR(adata->pdev[i]);
223 				goto unregister_devs;
224 			}
225 		}
226 		break;
227 	default:
228 		dev_info(&pci->dev, "ACP audio mode : %d\n", val);
229 	}
230 	pm_runtime_set_autosuspend_delay(&pci->dev, 2000);
231 	pm_runtime_use_autosuspend(&pci->dev);
232 	pm_runtime_put_noidle(&pci->dev);
233 	pm_runtime_allow(&pci->dev);
234 	return 0;
235 
236 unregister_devs:
237 	for (--i; i >= 0; i--)
238 		platform_device_unregister(adata->pdev[i]);
239 de_init:
240 	if (acp5x_deinit(adata->acp5x_base))
241 		dev_err(&pci->dev, "ACP de-init failed\n");
242 release_regions:
243 	pci_release_regions(pci);
244 disable_pci:
245 	pci_disable_device(pci);
246 
247 	return ret;
248 }
249 
250 static int __maybe_unused snd_acp5x_suspend(struct device *dev)
251 {
252 	int ret;
253 	struct acp5x_dev_data *adata;
254 
255 	adata = dev_get_drvdata(dev);
256 	ret = acp5x_deinit(adata->acp5x_base);
257 	if (ret)
258 		dev_err(dev, "ACP de-init failed\n");
259 	else
260 		dev_dbg(dev, "ACP de-initialized\n");
261 
262 	return ret;
263 }
264 
265 static int __maybe_unused snd_acp5x_resume(struct device *dev)
266 {
267 	int ret;
268 	struct acp5x_dev_data *adata;
269 
270 	adata = dev_get_drvdata(dev);
271 	ret = acp5x_init(adata->acp5x_base);
272 	if (ret) {
273 		dev_err(dev, "ACP init failed\n");
274 		return ret;
275 	}
276 	return 0;
277 }
278 
279 static const struct dev_pm_ops acp5x_pm = {
280 	SET_RUNTIME_PM_OPS(snd_acp5x_suspend,
281 			   snd_acp5x_resume, NULL)
282 	SET_SYSTEM_SLEEP_PM_OPS(snd_acp5x_suspend, snd_acp5x_resume)
283 };
284 
285 static void snd_acp5x_remove(struct pci_dev *pci)
286 {
287 	struct acp5x_dev_data *adata;
288 	int i, ret;
289 
290 	adata = pci_get_drvdata(pci);
291 	if (adata->acp5x_audio_mode == ACP5x_I2S_MODE) {
292 		for (i = 0; i < ACP5x_DEVS; i++)
293 			platform_device_unregister(adata->pdev[i]);
294 	}
295 	ret = acp5x_deinit(adata->acp5x_base);
296 	if (ret)
297 		dev_err(&pci->dev, "ACP de-init failed\n");
298 	pm_runtime_forbid(&pci->dev);
299 	pm_runtime_get_noresume(&pci->dev);
300 	pci_release_regions(pci);
301 	pci_disable_device(pci);
302 }
303 
304 static const struct pci_device_id snd_acp5x_ids[] = {
305 	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, ACP_DEVICE_ID),
306 	.class = PCI_CLASS_MULTIMEDIA_OTHER << 8,
307 	.class_mask = 0xffffff },
308 	{ 0, },
309 };
310 MODULE_DEVICE_TABLE(pci, snd_acp5x_ids);
311 
312 static struct pci_driver acp5x_driver  = {
313 	.name = KBUILD_MODNAME,
314 	.id_table = snd_acp5x_ids,
315 	.probe = snd_acp5x_probe,
316 	.remove = snd_acp5x_remove,
317 	.driver = {
318 		.pm = &acp5x_pm,
319 	}
320 };
321 
322 module_pci_driver(acp5x_driver);
323 
324 MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
325 MODULE_DESCRIPTION("AMD Vangogh ACP PCI driver");
326 MODULE_LICENSE("GPL v2");
327