xref: /openbmc/linux/sound/soc/amd/raven/pci-acp3x.c (revision 165f2d28)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // AMD ACP PCI Driver
4 //
5 //Copyright 2016 Advanced Micro Devices, Inc.
6 
7 #include <linux/pci.h>
8 #include <linux/module.h>
9 #include <linux/io.h>
10 #include <linux/platform_device.h>
11 #include <linux/interrupt.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/delay.h>
14 
15 #include "acp3x.h"
16 
17 struct acp3x_dev_data {
18 	void __iomem *acp3x_base;
19 	bool acp3x_audio_mode;
20 	struct resource *res;
21 	struct platform_device *pdev[ACP3x_DEVS];
22 };
23 
24 static int acp3x_power_on(void __iomem *acp3x_base)
25 {
26 	u32 val;
27 	int timeout;
28 
29 	val = rv_readl(acp3x_base + mmACP_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 		rv_writel(ACP_PGFSM_CNTL_POWER_ON_MASK,
37 			acp3x_base + mmACP_PGFSM_CONTROL);
38 	timeout = 0;
39 	while (++timeout < 500) {
40 		val = rv_readl(acp3x_base + mmACP_PGFSM_STATUS);
41 		if (!val) {
42 			/* Set PME_EN as after ACP power On,
43 			 * PME_EN gets cleared
44 			 */
45 			rv_writel(0x1, acp3x_base + mmACP_PME_EN);
46 			return 0;
47 		}
48 		udelay(1);
49 	}
50 	return -ETIMEDOUT;
51 }
52 
53 static int acp3x_reset(void __iomem *acp3x_base)
54 {
55 	u32 val;
56 	int timeout;
57 
58 	rv_writel(1, acp3x_base + mmACP_SOFT_RESET);
59 	timeout = 0;
60 	while (++timeout < 500) {
61 		val = rv_readl(acp3x_base + mmACP_SOFT_RESET);
62 		if (val & ACP3x_SOFT_RESET__SoftResetAudDone_MASK)
63 			break;
64 		cpu_relax();
65 	}
66 	rv_writel(0, acp3x_base + mmACP_SOFT_RESET);
67 	timeout = 0;
68 	while (++timeout < 500) {
69 		val = rv_readl(acp3x_base + mmACP_SOFT_RESET);
70 		if (!val)
71 			return 0;
72 		cpu_relax();
73 	}
74 	return -ETIMEDOUT;
75 }
76 
77 static int acp3x_init(void __iomem *acp3x_base)
78 {
79 	int ret;
80 
81 	/* power on */
82 	ret = acp3x_power_on(acp3x_base);
83 	if (ret) {
84 		pr_err("ACP3x power on failed\n");
85 		return ret;
86 	}
87 	/* Reset */
88 	ret = acp3x_reset(acp3x_base);
89 	if (ret) {
90 		pr_err("ACP3x reset failed\n");
91 		return ret;
92 	}
93 	return 0;
94 }
95 
96 static int acp3x_deinit(void __iomem *acp3x_base)
97 {
98 	int ret;
99 
100 	/* Reset */
101 	ret = acp3x_reset(acp3x_base);
102 	if (ret) {
103 		pr_err("ACP3x reset failed\n");
104 		return ret;
105 	}
106 	return 0;
107 }
108 
109 static int snd_acp3x_probe(struct pci_dev *pci,
110 			   const struct pci_device_id *pci_id)
111 {
112 	struct acp3x_dev_data *adata;
113 	struct platform_device_info pdevinfo[ACP3x_DEVS];
114 	unsigned int irqflags;
115 	int ret, i;
116 	u32 addr, val;
117 
118 	if (pci_enable_device(pci)) {
119 		dev_err(&pci->dev, "pci_enable_device failed\n");
120 		return -ENODEV;
121 	}
122 
123 	ret = pci_request_regions(pci, "AMD ACP3x audio");
124 	if (ret < 0) {
125 		dev_err(&pci->dev, "pci_request_regions failed\n");
126 		goto disable_pci;
127 	}
128 
129 	adata = devm_kzalloc(&pci->dev, sizeof(struct acp3x_dev_data),
130 			     GFP_KERNEL);
131 	if (!adata) {
132 		ret = -ENOMEM;
133 		goto release_regions;
134 	}
135 
136 	/* check for msi interrupt support */
137 	ret = pci_enable_msi(pci);
138 	if (ret)
139 		/* msi is not enabled */
140 		irqflags = IRQF_SHARED;
141 	else
142 		/* msi is enabled */
143 		irqflags = 0;
144 
145 	addr = pci_resource_start(pci, 0);
146 	adata->acp3x_base = devm_ioremap(&pci->dev, addr,
147 					pci_resource_len(pci, 0));
148 	if (!adata->acp3x_base) {
149 		ret = -ENOMEM;
150 		goto disable_msi;
151 	}
152 	pci_set_master(pci);
153 	pci_set_drvdata(pci, adata);
154 	ret = acp3x_init(adata->acp3x_base);
155 	if (ret)
156 		goto disable_msi;
157 
158 	val = rv_readl(adata->acp3x_base + mmACP_I2S_PIN_CONFIG);
159 	switch (val) {
160 	case I2S_MODE:
161 		adata->res = devm_kzalloc(&pci->dev,
162 					  sizeof(struct resource) * 4,
163 					  GFP_KERNEL);
164 		if (!adata->res) {
165 			ret = -ENOMEM;
166 			goto de_init;
167 		}
168 
169 		adata->res[0].name = "acp3x_i2s_iomem";
170 		adata->res[0].flags = IORESOURCE_MEM;
171 		adata->res[0].start = addr;
172 		adata->res[0].end = addr + (ACP3x_REG_END - ACP3x_REG_START);
173 
174 		adata->res[1].name = "acp3x_i2s_sp";
175 		adata->res[1].flags = IORESOURCE_MEM;
176 		adata->res[1].start = addr + ACP3x_I2STDM_REG_START;
177 		adata->res[1].end = addr + ACP3x_I2STDM_REG_END;
178 
179 		adata->res[2].name = "acp3x_i2s_bt";
180 		adata->res[2].flags = IORESOURCE_MEM;
181 		adata->res[2].start = addr + ACP3x_BT_TDM_REG_START;
182 		adata->res[2].end = addr + ACP3x_BT_TDM_REG_END;
183 
184 		adata->res[3].name = "acp3x_i2s_irq";
185 		adata->res[3].flags = IORESOURCE_IRQ;
186 		adata->res[3].start = pci->irq;
187 		adata->res[3].end = adata->res[3].start;
188 
189 		adata->acp3x_audio_mode = ACP3x_I2S_MODE;
190 
191 		memset(&pdevinfo, 0, sizeof(pdevinfo));
192 		pdevinfo[0].name = "acp3x_rv_i2s_dma";
193 		pdevinfo[0].id = 0;
194 		pdevinfo[0].parent = &pci->dev;
195 		pdevinfo[0].num_res = 4;
196 		pdevinfo[0].res = &adata->res[0];
197 		pdevinfo[0].data = &irqflags;
198 		pdevinfo[0].size_data = sizeof(irqflags);
199 
200 		pdevinfo[1].name = "acp3x_i2s_playcap";
201 		pdevinfo[1].id = 0;
202 		pdevinfo[1].parent = &pci->dev;
203 		pdevinfo[1].num_res = 1;
204 		pdevinfo[1].res = &adata->res[1];
205 
206 		pdevinfo[2].name = "acp3x_i2s_playcap";
207 		pdevinfo[2].id = 1;
208 		pdevinfo[2].parent = &pci->dev;
209 		pdevinfo[2].num_res = 1;
210 		pdevinfo[2].res = &adata->res[1];
211 
212 		pdevinfo[3].name = "acp3x_i2s_playcap";
213 		pdevinfo[3].id = 2;
214 		pdevinfo[3].parent = &pci->dev;
215 		pdevinfo[3].num_res = 1;
216 		pdevinfo[3].res = &adata->res[2];
217 		for (i = 0; i < ACP3x_DEVS; i++) {
218 			adata->pdev[i] =
219 				platform_device_register_full(&pdevinfo[i]);
220 			if (IS_ERR(adata->pdev[i])) {
221 				dev_err(&pci->dev, "cannot register %s device\n",
222 					pdevinfo[i].name);
223 				ret = PTR_ERR(adata->pdev[i]);
224 				goto unregister_devs;
225 			}
226 		}
227 		break;
228 	default:
229 		dev_err(&pci->dev, "Invalid ACP audio mode : %d\n", val);
230 		ret = -ENODEV;
231 		goto disable_msi;
232 	}
233 	pm_runtime_set_autosuspend_delay(&pci->dev, 2000);
234 	pm_runtime_use_autosuspend(&pci->dev);
235 	pm_runtime_set_active(&pci->dev);
236 	pm_runtime_put_noidle(&pci->dev);
237 	pm_runtime_enable(&pci->dev);
238 	pm_runtime_allow(&pci->dev);
239 	return 0;
240 
241 unregister_devs:
242 	if (val == I2S_MODE)
243 		for (i = 0; i < ACP3x_DEVS; i++)
244 			platform_device_unregister(adata->pdev[i]);
245 de_init:
246 	if (acp3x_deinit(adata->acp3x_base))
247 		dev_err(&pci->dev, "ACP de-init failed\n");
248 disable_msi:
249 	pci_disable_msi(pci);
250 release_regions:
251 	pci_release_regions(pci);
252 disable_pci:
253 	pci_disable_device(pci);
254 
255 	return ret;
256 }
257 
258 static int snd_acp3x_suspend(struct device *dev)
259 {
260 	int ret;
261 	struct acp3x_dev_data *adata;
262 
263 	adata = dev_get_drvdata(dev);
264 	ret = acp3x_deinit(adata->acp3x_base);
265 	if (ret)
266 		dev_err(dev, "ACP de-init failed\n");
267 	else
268 		dev_dbg(dev, "ACP de-initialized\n");
269 
270 	return 0;
271 }
272 
273 static int snd_acp3x_resume(struct device *dev)
274 {
275 	int ret;
276 	struct acp3x_dev_data *adata;
277 
278 	adata = dev_get_drvdata(dev);
279 	ret = acp3x_init(adata->acp3x_base);
280 	if (ret) {
281 		dev_err(dev, "ACP init failed\n");
282 		return ret;
283 	}
284 	return 0;
285 }
286 
287 static const struct dev_pm_ops acp3x_pm = {
288 	.runtime_suspend = snd_acp3x_suspend,
289 	.runtime_resume =  snd_acp3x_resume,
290 	.resume =	snd_acp3x_resume,
291 };
292 
293 static void snd_acp3x_remove(struct pci_dev *pci)
294 {
295 	struct acp3x_dev_data *adata;
296 	int i, ret;
297 
298 	adata = pci_get_drvdata(pci);
299 	if (adata->acp3x_audio_mode == ACP3x_I2S_MODE) {
300 		for (i = 0; i < ACP3x_DEVS; i++)
301 			platform_device_unregister(adata->pdev[i]);
302 	}
303 	ret = acp3x_deinit(adata->acp3x_base);
304 	if (ret)
305 		dev_err(&pci->dev, "ACP de-init failed\n");
306 	pm_runtime_disable(&pci->dev);
307 	pm_runtime_get_noresume(&pci->dev);
308 	pci_disable_msi(pci);
309 	pci_release_regions(pci);
310 	pci_disable_device(pci);
311 }
312 
313 static const struct pci_device_id snd_acp3x_ids[] = {
314 	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, 0x15e2),
315 	.class = PCI_CLASS_MULTIMEDIA_OTHER << 8,
316 	.class_mask = 0xffffff },
317 	{ 0, },
318 };
319 MODULE_DEVICE_TABLE(pci, snd_acp3x_ids);
320 
321 static struct pci_driver acp3x_driver  = {
322 	.name = KBUILD_MODNAME,
323 	.id_table = snd_acp3x_ids,
324 	.probe = snd_acp3x_probe,
325 	.remove = snd_acp3x_remove,
326 	.driver = {
327 		.pm = &acp3x_pm,
328 	}
329 };
330 
331 module_pci_driver(acp3x_driver);
332 
333 MODULE_AUTHOR("Vishnuvardhanrao.Ravulapati@amd.com");
334 MODULE_AUTHOR("Maruthi.Bayyavarapu@amd.com");
335 MODULE_DESCRIPTION("AMD ACP3x PCI driver");
336 MODULE_LICENSE("GPL v2");
337