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