xref: /openbmc/linux/sound/soc/amd/yc/pci-acp6x.c (revision 9dd7c463)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * AMD Yellow Carp ACP PCI Driver
4  *
5  * Copyright 2021 Advanced Micro Devices, Inc.
6  */
7 
8 #include <linux/pci.h>
9 #include <linux/module.h>
10 #include <linux/io.h>
11 #include <linux/delay.h>
12 #include <linux/platform_device.h>
13 #include <linux/interrupt.h>
14 #include <sound/pcm_params.h>
15 #include <linux/pm_runtime.h>
16 
17 #include "acp6x.h"
18 
19 struct acp6x_dev_data {
20 	void __iomem *acp6x_base;
21 	struct resource *res;
22 	bool acp6x_audio_mode;
23 	struct platform_device *pdev[ACP6x_DEVS];
24 };
25 
26 static int acp6x_power_on(void __iomem *acp_base)
27 {
28 	u32 val;
29 	int timeout;
30 
31 	val = acp6x_readl(acp_base + ACP_PGFSM_STATUS);
32 
33 	if (!val)
34 		return val;
35 
36 	if ((val & ACP_PGFSM_STATUS_MASK) != ACP_POWER_ON_IN_PROGRESS)
37 		acp6x_writel(ACP_PGFSM_CNTL_POWER_ON_MASK, acp_base + ACP_PGFSM_CONTROL);
38 	timeout = 0;
39 	while (++timeout < 500) {
40 		val = acp6x_readl(acp_base + ACP_PGFSM_STATUS);
41 		if (!val)
42 			return 0;
43 		udelay(1);
44 	}
45 	return -ETIMEDOUT;
46 }
47 
48 static int acp6x_reset(void __iomem *acp_base)
49 {
50 	u32 val;
51 	int timeout;
52 
53 	acp6x_writel(1, acp_base + ACP_SOFT_RESET);
54 	timeout = 0;
55 	while (++timeout < 500) {
56 		val = acp6x_readl(acp_base + ACP_SOFT_RESET);
57 		if (val & ACP_SOFT_RESET_SOFTRESET_AUDDONE_MASK)
58 			break;
59 		cpu_relax();
60 	}
61 	acp6x_writel(0, acp_base + ACP_SOFT_RESET);
62 	timeout = 0;
63 	while (++timeout < 500) {
64 		val = acp6x_readl(acp_base + ACP_SOFT_RESET);
65 		if (!val)
66 			return 0;
67 		cpu_relax();
68 	}
69 	return -ETIMEDOUT;
70 }
71 
72 static void acp6x_enable_interrupts(void __iomem *acp_base)
73 {
74 	acp6x_writel(0x01, acp_base + ACP_EXTERNAL_INTR_ENB);
75 }
76 
77 static void acp6x_disable_interrupts(void __iomem *acp_base)
78 {
79 	acp6x_writel(ACP_EXT_INTR_STAT_CLEAR_MASK, acp_base +
80 		     ACP_EXTERNAL_INTR_STAT);
81 	acp6x_writel(0x00, acp_base + ACP_EXTERNAL_INTR_CNTL);
82 	acp6x_writel(0x00, acp_base + ACP_EXTERNAL_INTR_ENB);
83 }
84 
85 static int acp6x_init(void __iomem *acp_base)
86 {
87 	int ret;
88 
89 	/* power on */
90 	ret = acp6x_power_on(acp_base);
91 	if (ret) {
92 		pr_err("ACP power on failed\n");
93 		return ret;
94 	}
95 	acp6x_writel(0x01, acp_base + ACP_CONTROL);
96 	/* Reset */
97 	ret = acp6x_reset(acp_base);
98 	if (ret) {
99 		pr_err("ACP reset failed\n");
100 		return ret;
101 	}
102 	acp6x_writel(0x03, acp_base + ACP_CLKMUX_SEL);
103 	acp6x_enable_interrupts(acp_base);
104 	return 0;
105 }
106 
107 static int acp6x_deinit(void __iomem *acp_base)
108 {
109 	int ret;
110 
111 	acp6x_disable_interrupts(acp_base);
112 	/* Reset */
113 	ret = acp6x_reset(acp_base);
114 	if (ret) {
115 		pr_err("ACP reset failed\n");
116 		return ret;
117 	}
118 	acp6x_writel(0x00, acp_base + ACP_CLKMUX_SEL);
119 	acp6x_writel(0x00, acp_base + ACP_CONTROL);
120 	return 0;
121 }
122 
123 static irqreturn_t acp6x_irq_handler(int irq, void *dev_id)
124 {
125 	struct acp6x_dev_data *adata;
126 	struct pdm_dev_data *yc_pdm_data;
127 	u32 val;
128 
129 	adata = dev_id;
130 	if (!adata)
131 		return IRQ_NONE;
132 
133 	val = acp6x_readl(adata->acp6x_base + ACP_EXTERNAL_INTR_STAT);
134 	if (val & BIT(PDM_DMA_STAT)) {
135 		yc_pdm_data = dev_get_drvdata(&adata->pdev[0]->dev);
136 		acp6x_writel(BIT(PDM_DMA_STAT), adata->acp6x_base + ACP_EXTERNAL_INTR_STAT);
137 		if (yc_pdm_data->capture_stream)
138 			snd_pcm_period_elapsed(yc_pdm_data->capture_stream);
139 		return IRQ_HANDLED;
140 	}
141 	return IRQ_NONE;
142 }
143 
144 static int snd_acp6x_probe(struct pci_dev *pci,
145 			   const struct pci_device_id *pci_id)
146 {
147 	struct acp6x_dev_data *adata;
148 	struct platform_device_info pdevinfo[ACP6x_DEVS];
149 	int index = 0;
150 	int val = 0x00;
151 	u32 addr;
152 	unsigned int irqflags;
153 	int ret;
154 
155 	irqflags = IRQF_SHARED;
156 	/* Yellow Carp device check */
157 	if (pci->revision != 0x60)
158 		return -ENODEV;
159 
160 	if (pci_enable_device(pci)) {
161 		dev_err(&pci->dev, "pci_enable_device failed\n");
162 		return -ENODEV;
163 	}
164 
165 	ret = pci_request_regions(pci, "AMD ACP3x audio");
166 	if (ret < 0) {
167 		dev_err(&pci->dev, "pci_request_regions failed\n");
168 		goto disable_pci;
169 	}
170 
171 	adata = devm_kzalloc(&pci->dev, sizeof(struct acp6x_dev_data),
172 			     GFP_KERNEL);
173 	if (!adata) {
174 		ret = -ENOMEM;
175 		goto release_regions;
176 	}
177 
178 	addr = pci_resource_start(pci, 0);
179 	adata->acp6x_base = devm_ioremap(&pci->dev, addr,
180 					 pci_resource_len(pci, 0));
181 	if (!adata->acp6x_base) {
182 		ret = -ENOMEM;
183 		goto release_regions;
184 	}
185 	pci_set_master(pci);
186 	pci_set_drvdata(pci, adata);
187 	ret = acp6x_init(adata->acp6x_base);
188 	if (ret)
189 		goto release_regions;
190 	val = acp6x_readl(adata->acp6x_base + ACP_PIN_CONFIG);
191 	switch (val) {
192 	case ACP_CONFIG_0:
193 	case ACP_CONFIG_1:
194 	case ACP_CONFIG_2:
195 	case ACP_CONFIG_3:
196 	case ACP_CONFIG_9:
197 	case ACP_CONFIG_15:
198 		dev_info(&pci->dev, "Audio Mode %d\n", val);
199 		break;
200 	default:
201 		adata->res = devm_kzalloc(&pci->dev,
202 					  sizeof(struct resource),
203 					  GFP_KERNEL);
204 		if (!adata->res) {
205 			ret = -ENOMEM;
206 			goto de_init;
207 		}
208 
209 		adata->res->name = "acp_iomem";
210 		adata->res->flags = IORESOURCE_MEM;
211 		adata->res->start = addr;
212 		adata->res->end = addr + (ACP6x_REG_END - ACP6x_REG_START);
213 
214 		adata->acp6x_audio_mode = ACP6x_PDM_MODE;
215 
216 		memset(&pdevinfo, 0, sizeof(pdevinfo));
217 		pdevinfo[0].name = "acp_yc_pdm_dma";
218 		pdevinfo[0].id = 0;
219 		pdevinfo[0].parent = &pci->dev;
220 		pdevinfo[0].num_res = 1;
221 		pdevinfo[0].res = adata->res;
222 
223 		pdevinfo[1].name = "dmic-codec";
224 		pdevinfo[1].id = 0;
225 		pdevinfo[1].parent = &pci->dev;
226 
227 		pdevinfo[2].name = "acp_yc_mach";
228 		pdevinfo[2].id = 0;
229 		pdevinfo[2].parent = &pci->dev;
230 
231 		for (index = 0; index < ACP6x_DEVS; index++) {
232 			adata->pdev[index] =
233 				platform_device_register_full(&pdevinfo[index]);
234 			if (IS_ERR(adata->pdev[index])) {
235 				dev_err(&pci->dev, "cannot register %s device\n",
236 					pdevinfo[index].name);
237 				ret = PTR_ERR(adata->pdev[index]);
238 				goto unregister_devs;
239 			}
240 		}
241 		break;
242 	}
243 	ret = devm_request_irq(&pci->dev, pci->irq, acp6x_irq_handler,
244 			       irqflags, "ACP_PCI_IRQ", adata);
245 	if (ret) {
246 		dev_err(&pci->dev, "ACP PCI IRQ request failed\n");
247 		goto unregister_devs;
248 	}
249 	pm_runtime_set_autosuspend_delay(&pci->dev, ACP_SUSPEND_DELAY_MS);
250 	pm_runtime_use_autosuspend(&pci->dev);
251 	pm_runtime_put_noidle(&pci->dev);
252 	pm_runtime_allow(&pci->dev);
253 
254 	return 0;
255 unregister_devs:
256 	for (--index; index >= 0; index--)
257 		platform_device_unregister(adata->pdev[index]);
258 de_init:
259 	if (acp6x_deinit(adata->acp6x_base))
260 		dev_err(&pci->dev, "ACP de-init failed\n");
261 release_regions:
262 	pci_release_regions(pci);
263 disable_pci:
264 	pci_disable_device(pci);
265 
266 	return ret;
267 }
268 
269 static int __maybe_unused snd_acp6x_suspend(struct device *dev)
270 {
271 	struct acp6x_dev_data *adata;
272 	int ret;
273 
274 	adata = dev_get_drvdata(dev);
275 	ret = acp6x_deinit(adata->acp6x_base);
276 	if (ret)
277 		dev_err(dev, "ACP de-init failed\n");
278 	return ret;
279 }
280 
281 static int __maybe_unused snd_acp6x_resume(struct device *dev)
282 {
283 	struct acp6x_dev_data *adata;
284 	int ret;
285 
286 	adata = dev_get_drvdata(dev);
287 	ret = acp6x_init(adata->acp6x_base);
288 	if (ret)
289 		dev_err(dev, "ACP init failed\n");
290 	return ret;
291 }
292 
293 static const struct dev_pm_ops acp6x_pm = {
294 	SET_RUNTIME_PM_OPS(snd_acp6x_suspend, snd_acp6x_resume, NULL)
295 	SET_SYSTEM_SLEEP_PM_OPS(snd_acp6x_suspend, snd_acp6x_resume)
296 };
297 
298 static void snd_acp6x_remove(struct pci_dev *pci)
299 {
300 	struct acp6x_dev_data *adata;
301 	int ret, index;
302 
303 	adata = pci_get_drvdata(pci);
304 	if (adata->acp6x_audio_mode == ACP6x_PDM_MODE) {
305 		for (index = 0; index < ACP6x_DEVS; index++)
306 			platform_device_unregister(adata->pdev[index]);
307 	}
308 	ret = acp6x_deinit(adata->acp6x_base);
309 	if (ret)
310 		dev_err(&pci->dev, "ACP de-init failed\n");
311 	pm_runtime_forbid(&pci->dev);
312 	pm_runtime_get_noresume(&pci->dev);
313 	pci_release_regions(pci);
314 	pci_disable_device(pci);
315 }
316 
317 static const struct pci_device_id snd_acp6x_ids[] = {
318 	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, ACP_DEVICE_ID),
319 	.class = PCI_CLASS_MULTIMEDIA_OTHER << 8,
320 	.class_mask = 0xffffff },
321 	{ 0, },
322 };
323 MODULE_DEVICE_TABLE(pci, snd_acp6x_ids);
324 
325 static struct pci_driver yc_acp6x_driver  = {
326 	.name = KBUILD_MODNAME,
327 	.id_table = snd_acp6x_ids,
328 	.probe = snd_acp6x_probe,
329 	.remove = snd_acp6x_remove,
330 	.driver = {
331 		.pm = &acp6x_pm,
332 	}
333 };
334 
335 module_pci_driver(yc_acp6x_driver);
336 
337 MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
338 MODULE_DESCRIPTION("AMD ACP Yellow Carp PCI driver");
339 MODULE_LICENSE("GPL v2");
340