xref: /openbmc/linux/sound/soc/amd/ps/pci-ps.c (revision ffcdf473)
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, &adata->acp_lock,
215 					     sizeof(adata->acp_lock));
216 		acp63_fill_platform_dev_info(&pdevinfo[1], parent, NULL, "dmic-codec",
217 					     0, NULL, 0, NULL, 0);
218 		acp63_fill_platform_dev_info(&pdevinfo[2], parent, NULL, "acp_ps_mach",
219 					     0, NULL, 0, NULL, 0);
220 		break;
221 	default:
222 		dev_dbg(&pci->dev, "No PDM devices found\n");
223 		return 0;
224 	}
225 
226 	for (index = 0; index < adata->pdev_count; index++) {
227 		adata->pdev[index] = platform_device_register_full(&pdevinfo[index]);
228 		if (IS_ERR(adata->pdev[index])) {
229 			dev_err(&pci->dev,
230 				"cannot register %s device\n", pdevinfo[index].name);
231 			ret = PTR_ERR(adata->pdev[index]);
232 			goto unregister_devs;
233 		}
234 	}
235 	return 0;
236 unregister_devs:
237 	for (--index; index >= 0; index--)
238 		platform_device_unregister(adata->pdev[index]);
239 de_init:
240 	if (acp63_deinit(adata->acp63_base, &pci->dev))
241 		dev_err(&pci->dev, "ACP de-init failed\n");
242 	return ret;
243 }
244 
245 static int snd_acp63_probe(struct pci_dev *pci,
246 			   const struct pci_device_id *pci_id)
247 {
248 	struct acp63_dev_data *adata;
249 	u32 addr;
250 	u32 irqflags, flag;
251 	int val;
252 	int ret;
253 
254 	irqflags = IRQF_SHARED;
255 
256 	/* Return if acp config flag is defined */
257 	flag = snd_amd_acp_find_config(pci);
258 	if (flag)
259 		return -ENODEV;
260 
261 	/* Pink Sardine device check */
262 	switch (pci->revision) {
263 	case 0x63:
264 		break;
265 	default:
266 		dev_dbg(&pci->dev, "acp63 pci device not found\n");
267 		return -ENODEV;
268 	}
269 	if (pci_enable_device(pci)) {
270 		dev_err(&pci->dev, "pci_enable_device failed\n");
271 		return -ENODEV;
272 	}
273 
274 	ret = pci_request_regions(pci, "AMD ACP6.2 audio");
275 	if (ret < 0) {
276 		dev_err(&pci->dev, "pci_request_regions failed\n");
277 		goto disable_pci;
278 	}
279 	adata = devm_kzalloc(&pci->dev, sizeof(struct acp63_dev_data),
280 			     GFP_KERNEL);
281 	if (!adata) {
282 		ret = -ENOMEM;
283 		goto release_regions;
284 	}
285 
286 	addr = pci_resource_start(pci, 0);
287 	adata->acp63_base = devm_ioremap(&pci->dev, addr,
288 					 pci_resource_len(pci, 0));
289 	if (!adata->acp63_base) {
290 		ret = -ENOMEM;
291 		goto release_regions;
292 	}
293 	pci_set_master(pci);
294 	pci_set_drvdata(pci, adata);
295 	mutex_init(&adata->acp_lock);
296 	ret = acp63_init(adata->acp63_base, &pci->dev);
297 	if (ret)
298 		goto release_regions;
299 	ret = devm_request_irq(&pci->dev, pci->irq, acp63_irq_handler,
300 			       irqflags, "ACP_PCI_IRQ", adata);
301 	if (ret) {
302 		dev_err(&pci->dev, "ACP PCI IRQ request failed\n");
303 		goto de_init;
304 	}
305 	val = acp63_readl(adata->acp63_base + ACP_PIN_CONFIG);
306 	get_acp63_device_config(val, pci, adata);
307 	ret = create_acp63_platform_devs(pci, adata, addr);
308 	if (ret < 0) {
309 		dev_err(&pci->dev, "ACP platform devices creation failed\n");
310 		goto de_init;
311 	}
312 	pm_runtime_set_autosuspend_delay(&pci->dev, ACP_SUSPEND_DELAY_MS);
313 	pm_runtime_use_autosuspend(&pci->dev);
314 	pm_runtime_put_noidle(&pci->dev);
315 	pm_runtime_allow(&pci->dev);
316 	return 0;
317 de_init:
318 	if (acp63_deinit(adata->acp63_base, &pci->dev))
319 		dev_err(&pci->dev, "ACP de-init failed\n");
320 release_regions:
321 	pci_release_regions(pci);
322 disable_pci:
323 	pci_disable_device(pci);
324 
325 	return ret;
326 }
327 
328 static int __maybe_unused snd_acp63_suspend(struct device *dev)
329 {
330 	struct acp63_dev_data *adata;
331 	int ret;
332 
333 	adata = dev_get_drvdata(dev);
334 	ret = acp63_deinit(adata->acp63_base, dev);
335 	if (ret)
336 		dev_err(dev, "ACP de-init failed\n");
337 	return ret;
338 }
339 
340 static int __maybe_unused snd_acp63_resume(struct device *dev)
341 {
342 	struct acp63_dev_data *adata;
343 	int ret;
344 
345 	adata = dev_get_drvdata(dev);
346 	ret = acp63_init(adata->acp63_base, dev);
347 	if (ret)
348 		dev_err(dev, "ACP init failed\n");
349 	return ret;
350 }
351 
352 static const struct dev_pm_ops acp63_pm_ops = {
353 	SET_RUNTIME_PM_OPS(snd_acp63_suspend, snd_acp63_resume, NULL)
354 	SET_SYSTEM_SLEEP_PM_OPS(snd_acp63_suspend, snd_acp63_resume)
355 };
356 
357 static void snd_acp63_remove(struct pci_dev *pci)
358 {
359 	struct acp63_dev_data *adata;
360 	int ret, index;
361 
362 	adata = pci_get_drvdata(pci);
363 	for (index = 0; index < adata->pdev_count; index++)
364 		platform_device_unregister(adata->pdev[index]);
365 	ret = acp63_deinit(adata->acp63_base, &pci->dev);
366 	if (ret)
367 		dev_err(&pci->dev, "ACP de-init failed\n");
368 	pm_runtime_forbid(&pci->dev);
369 	pm_runtime_get_noresume(&pci->dev);
370 	pci_release_regions(pci);
371 	pci_disable_device(pci);
372 }
373 
374 static const struct pci_device_id snd_acp63_ids[] = {
375 	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, ACP_DEVICE_ID),
376 	.class = PCI_CLASS_MULTIMEDIA_OTHER << 8,
377 	.class_mask = 0xffffff },
378 	{ 0, },
379 };
380 MODULE_DEVICE_TABLE(pci, snd_acp63_ids);
381 
382 static struct pci_driver ps_acp63_driver  = {
383 	.name = KBUILD_MODNAME,
384 	.id_table = snd_acp63_ids,
385 	.probe = snd_acp63_probe,
386 	.remove = snd_acp63_remove,
387 	.driver = {
388 		.pm = &acp63_pm_ops,
389 	}
390 };
391 
392 module_pci_driver(ps_acp63_driver);
393 
394 MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
395 MODULE_AUTHOR("Syed.SabaKareem@amd.com");
396 MODULE_DESCRIPTION("AMD ACP Pink Sardine PCI driver");
397 MODULE_LICENSE("GPL v2");
398