xref: /openbmc/linux/sound/soc/amd/ps/pci-ps.c (revision bbaf1ff0)
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_enable_interrupts(acp_base);
95 	return 0;
96 }
97 
98 static int acp63_deinit(void __iomem *acp_base, struct device *dev)
99 {
100 	int ret;
101 
102 	acp63_disable_interrupts(acp_base);
103 	ret = acp63_reset(acp_base);
104 	if (ret) {
105 		dev_err(dev, "ACP reset failed\n");
106 		return ret;
107 	}
108 	acp63_writel(0, acp_base + ACP_CONTROL);
109 	return 0;
110 }
111 
112 static irqreturn_t acp63_irq_handler(int irq, void *dev_id)
113 {
114 	struct acp63_dev_data *adata;
115 	struct pdm_dev_data *ps_pdm_data;
116 	u32 val;
117 	u16 pdev_index;
118 
119 	adata = dev_id;
120 	if (!adata)
121 		return IRQ_NONE;
122 
123 	val = acp63_readl(adata->acp63_base + ACP_EXTERNAL_INTR_STAT);
124 	if (val & BIT(PDM_DMA_STAT)) {
125 		pdev_index = adata->pdm_dev_index;
126 		ps_pdm_data = dev_get_drvdata(&adata->pdev[pdev_index]->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 void get_acp63_device_config(u32 config, struct pci_dev *pci,
136 				    struct acp63_dev_data *acp_data)
137 {
138 	struct acpi_device *dmic_dev;
139 	const union acpi_object *obj;
140 	bool is_dmic_dev = false;
141 
142 	dmic_dev = acpi_find_child_device(ACPI_COMPANION(&pci->dev), ACP63_DMIC_ADDR, 0);
143 	if (dmic_dev) {
144 		if (!acpi_dev_get_property(dmic_dev, "acp-audio-device-type",
145 					   ACPI_TYPE_INTEGER, &obj) &&
146 					   obj->integer.value == ACP_DMIC_DEV)
147 			is_dmic_dev = true;
148 	}
149 
150 	switch (config) {
151 	case ACP_CONFIG_0:
152 	case ACP_CONFIG_1:
153 	case ACP_CONFIG_2:
154 	case ACP_CONFIG_3:
155 	case ACP_CONFIG_9:
156 	case ACP_CONFIG_15:
157 		dev_dbg(&pci->dev, "Audio Mode %d\n", config);
158 		break;
159 	default:
160 		if (is_dmic_dev) {
161 			acp_data->pdev_mask = ACP63_PDM_DEV_MASK;
162 			acp_data->pdev_count = ACP63_PDM_MODE_DEVS;
163 		}
164 		break;
165 	}
166 }
167 
168 static void acp63_fill_platform_dev_info(struct platform_device_info *pdevinfo,
169 					 struct device *parent,
170 					 struct fwnode_handle *fw_node,
171 					 char *name, unsigned int id,
172 					 const struct resource *res,
173 					 unsigned int num_res,
174 					 const void *data,
175 					 size_t size_data)
176 {
177 	pdevinfo->name = name;
178 	pdevinfo->id = id;
179 	pdevinfo->parent = parent;
180 	pdevinfo->num_res = num_res;
181 	pdevinfo->res = res;
182 	pdevinfo->data = data;
183 	pdevinfo->size_data = size_data;
184 	pdevinfo->fwnode = fw_node;
185 }
186 
187 static int create_acp63_platform_devs(struct pci_dev *pci, struct acp63_dev_data *adata, u32 addr)
188 {
189 	struct platform_device_info pdevinfo[ACP63_DEVS];
190 	struct device *parent;
191 	int index;
192 	int ret;
193 
194 	parent = &pci->dev;
195 	dev_dbg(&pci->dev,
196 		"%s pdev_mask:0x%x pdev_count:0x%x\n", __func__, adata->pdev_mask,
197 		adata->pdev_count);
198 	if (adata->pdev_mask) {
199 		adata->res = devm_kzalloc(&pci->dev, sizeof(struct resource), GFP_KERNEL);
200 		if (!adata->res) {
201 			ret = -ENOMEM;
202 			goto de_init;
203 		}
204 		adata->res->flags = IORESOURCE_MEM;
205 		adata->res->start = addr;
206 		adata->res->end = addr + (ACP63_REG_END - ACP63_REG_START);
207 		memset(&pdevinfo, 0, sizeof(pdevinfo));
208 	}
209 
210 	switch (adata->pdev_mask) {
211 	case ACP63_PDM_DEV_MASK:
212 		adata->pdm_dev_index  = 0;
213 		acp63_fill_platform_dev_info(&pdevinfo[0], parent, NULL, "acp_ps_pdm_dma",
214 					     0, adata->res, 1, NULL, 0);
215 		acp63_fill_platform_dev_info(&pdevinfo[1], parent, NULL, "dmic-codec",
216 					     0, NULL, 0, NULL, 0);
217 		acp63_fill_platform_dev_info(&pdevinfo[2], parent, NULL, "acp_ps_mach",
218 					     0, NULL, 0, NULL, 0);
219 		break;
220 	default:
221 		dev_dbg(&pci->dev, "No PDM devices found\n");
222 		return 0;
223 	}
224 
225 	for (index = 0; index < adata->pdev_count; index++) {
226 		adata->pdev[index] = platform_device_register_full(&pdevinfo[index]);
227 		if (IS_ERR(adata->pdev[index])) {
228 			dev_err(&pci->dev,
229 				"cannot register %s device\n", pdevinfo[index].name);
230 			ret = PTR_ERR(adata->pdev[index]);
231 			goto unregister_devs;
232 		}
233 	}
234 	return 0;
235 unregister_devs:
236 	for (--index; index >= 0; index--)
237 		platform_device_unregister(adata->pdev[index]);
238 de_init:
239 	if (acp63_deinit(adata->acp63_base, &pci->dev))
240 		dev_err(&pci->dev, "ACP de-init failed\n");
241 	return ret;
242 }
243 
244 static int snd_acp63_probe(struct pci_dev *pci,
245 			   const struct pci_device_id *pci_id)
246 {
247 	struct acp63_dev_data *adata;
248 	u32 addr;
249 	u32 irqflags, flag;
250 	int val;
251 	int ret;
252 
253 	irqflags = IRQF_SHARED;
254 
255 	/* Return if acp config flag is defined */
256 	flag = snd_amd_acp_find_config(pci);
257 	if (flag)
258 		return -ENODEV;
259 
260 	/* Pink Sardine device check */
261 	switch (pci->revision) {
262 	case 0x63:
263 		break;
264 	default:
265 		dev_dbg(&pci->dev, "acp63 pci device not found\n");
266 		return -ENODEV;
267 	}
268 	if (pci_enable_device(pci)) {
269 		dev_err(&pci->dev, "pci_enable_device failed\n");
270 		return -ENODEV;
271 	}
272 
273 	ret = pci_request_regions(pci, "AMD ACP6.2 audio");
274 	if (ret < 0) {
275 		dev_err(&pci->dev, "pci_request_regions failed\n");
276 		goto disable_pci;
277 	}
278 	adata = devm_kzalloc(&pci->dev, sizeof(struct acp63_dev_data),
279 			     GFP_KERNEL);
280 	if (!adata) {
281 		ret = -ENOMEM;
282 		goto release_regions;
283 	}
284 
285 	addr = pci_resource_start(pci, 0);
286 	adata->acp63_base = devm_ioremap(&pci->dev, addr,
287 					 pci_resource_len(pci, 0));
288 	if (!adata->acp63_base) {
289 		ret = -ENOMEM;
290 		goto release_regions;
291 	}
292 	pci_set_master(pci);
293 	pci_set_drvdata(pci, adata);
294 	mutex_init(&adata->acp_lock);
295 	ret = acp63_init(adata->acp63_base, &pci->dev);
296 	if (ret)
297 		goto release_regions;
298 	ret = devm_request_irq(&pci->dev, pci->irq, acp63_irq_handler,
299 			       irqflags, "ACP_PCI_IRQ", adata);
300 	if (ret) {
301 		dev_err(&pci->dev, "ACP PCI IRQ request failed\n");
302 		goto de_init;
303 	}
304 	val = acp63_readl(adata->acp63_base + ACP_PIN_CONFIG);
305 	get_acp63_device_config(val, pci, adata);
306 	ret = create_acp63_platform_devs(pci, adata, addr);
307 	if (ret < 0) {
308 		dev_err(&pci->dev, "ACP platform devices creation failed\n");
309 		goto de_init;
310 	}
311 	pm_runtime_set_autosuspend_delay(&pci->dev, ACP_SUSPEND_DELAY_MS);
312 	pm_runtime_use_autosuspend(&pci->dev);
313 	pm_runtime_put_noidle(&pci->dev);
314 	pm_runtime_allow(&pci->dev);
315 	return 0;
316 de_init:
317 	if (acp63_deinit(adata->acp63_base, &pci->dev))
318 		dev_err(&pci->dev, "ACP de-init failed\n");
319 release_regions:
320 	pci_release_regions(pci);
321 disable_pci:
322 	pci_disable_device(pci);
323 
324 	return ret;
325 }
326 
327 static int __maybe_unused snd_acp63_suspend(struct device *dev)
328 {
329 	struct acp63_dev_data *adata;
330 	int ret;
331 
332 	adata = dev_get_drvdata(dev);
333 	ret = acp63_deinit(adata->acp63_base, dev);
334 	if (ret)
335 		dev_err(dev, "ACP de-init failed\n");
336 	return ret;
337 }
338 
339 static int __maybe_unused snd_acp63_resume(struct device *dev)
340 {
341 	struct acp63_dev_data *adata;
342 	int ret;
343 
344 	adata = dev_get_drvdata(dev);
345 	ret = acp63_init(adata->acp63_base, dev);
346 	if (ret)
347 		dev_err(dev, "ACP init failed\n");
348 	return ret;
349 }
350 
351 static const struct dev_pm_ops acp63_pm_ops = {
352 	SET_RUNTIME_PM_OPS(snd_acp63_suspend, snd_acp63_resume, NULL)
353 	SET_SYSTEM_SLEEP_PM_OPS(snd_acp63_suspend, snd_acp63_resume)
354 };
355 
356 static void snd_acp63_remove(struct pci_dev *pci)
357 {
358 	struct acp63_dev_data *adata;
359 	int ret, index;
360 
361 	adata = pci_get_drvdata(pci);
362 	for (index = 0; index < adata->pdev_count; index++)
363 		platform_device_unregister(adata->pdev[index]);
364 	ret = acp63_deinit(adata->acp63_base, &pci->dev);
365 	if (ret)
366 		dev_err(&pci->dev, "ACP de-init failed\n");
367 	pm_runtime_forbid(&pci->dev);
368 	pm_runtime_get_noresume(&pci->dev);
369 	pci_release_regions(pci);
370 	pci_disable_device(pci);
371 }
372 
373 static const struct pci_device_id snd_acp63_ids[] = {
374 	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, ACP_DEVICE_ID),
375 	.class = PCI_CLASS_MULTIMEDIA_OTHER << 8,
376 	.class_mask = 0xffffff },
377 	{ 0, },
378 };
379 MODULE_DEVICE_TABLE(pci, snd_acp63_ids);
380 
381 static struct pci_driver ps_acp63_driver  = {
382 	.name = KBUILD_MODNAME,
383 	.id_table = snd_acp63_ids,
384 	.probe = snd_acp63_probe,
385 	.remove = snd_acp63_remove,
386 	.driver = {
387 		.pm = &acp63_pm_ops,
388 	}
389 };
390 
391 module_pci_driver(ps_acp63_driver);
392 
393 MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
394 MODULE_AUTHOR("Syed.SabaKareem@amd.com");
395 MODULE_DESCRIPTION("AMD ACP Pink Sardine PCI driver");
396 MODULE_LICENSE("GPL v2");
397