xref: /kernel/linux/linux-6.6/sound/soc/amd/acp/acp-pci.c (revision 62306a36)
1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2//
3// This file is provided under a dual BSD/GPLv2 license. When using or
4// redistributing this file, you may do so under either license.
5//
6// Copyright(c) 2022 Advanced Micro Devices, Inc. All rights reserved.
7//
8// Authors: Ajit Kumar Pandey <AjitKumar.Pandey@amd.com>
9
10/*
11 * Generic PCI interface for ACP device
12 */
13
14#include <linux/delay.h>
15#include <linux/interrupt.h>
16#include <linux/pci.h>
17#include <linux/platform_device.h>
18#include <linux/module.h>
19#include <linux/pm_runtime.h>
20
21#include "amd.h"
22#include "../mach-config.h"
23
24#define DRV_NAME "acp_pci"
25
26#define ACP3x_REG_START	0x1240000
27#define ACP3x_REG_END	0x125C000
28
29static struct platform_device *dmic_dev;
30static struct platform_device *pdev;
31
32static const struct resource acp_res[] = {
33	{
34		.start = 0,
35		.end = ACP3x_REG_END - ACP3x_REG_START,
36		.name = "acp_mem",
37		.flags = IORESOURCE_MEM,
38	},
39	{
40		.start = 0,
41		.end = 0,
42		.name = "acp_dai_irq",
43		.flags = IORESOURCE_IRQ,
44	},
45};
46
47static int acp_pci_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
48{
49	struct platform_device_info pdevinfo;
50	struct device *dev = &pci->dev;
51	const struct resource *res_acp;
52	struct acp_chip_info *chip;
53	struct resource *res;
54	unsigned int flag, addr, num_res, i;
55	int ret;
56
57	flag = snd_amd_acp_find_config(pci);
58	if (flag != FLAG_AMD_LEGACY)
59		return -ENODEV;
60
61	chip = devm_kzalloc(&pci->dev, sizeof(*chip), GFP_KERNEL);
62	if (!chip)
63		return -ENOMEM;
64
65	if (pci_enable_device(pci))
66		return dev_err_probe(&pci->dev, -ENODEV,
67				     "pci_enable_device failed\n");
68
69	ret = pci_request_regions(pci, "AMD ACP3x audio");
70	if (ret < 0) {
71		dev_err(&pci->dev, "pci_request_regions failed\n");
72		ret = -ENOMEM;
73		goto disable_pci;
74	}
75
76	pci_set_master(pci);
77
78	res_acp = acp_res;
79	num_res = ARRAY_SIZE(acp_res);
80
81	switch (pci->revision) {
82	case 0x01:
83		chip->name = "acp_asoc_renoir";
84		chip->acp_rev = ACP3X_DEV;
85		break;
86	case 0x6f:
87		chip->name = "acp_asoc_rembrandt";
88		chip->acp_rev = ACP6X_DEV;
89		break;
90	default:
91		dev_err(dev, "Unsupported device revision:0x%x\n", pci->revision);
92		ret = -EINVAL;
93		goto release_regions;
94	}
95
96	dmic_dev = platform_device_register_data(dev, "dmic-codec", PLATFORM_DEVID_NONE, NULL, 0);
97	if (IS_ERR(dmic_dev)) {
98		dev_err(dev, "failed to create DMIC device\n");
99		ret = PTR_ERR(dmic_dev);
100		goto release_regions;
101	}
102
103	addr = pci_resource_start(pci, 0);
104	chip->base = devm_ioremap(&pci->dev, addr, pci_resource_len(pci, 0));
105	if (!chip->base) {
106		ret = -ENOMEM;
107		goto unregister_dmic_dev;
108	}
109
110	acp_init(chip);
111	res = devm_kcalloc(&pci->dev, num_res, sizeof(struct resource), GFP_KERNEL);
112	if (!res) {
113		ret = -ENOMEM;
114		goto unregister_dmic_dev;
115	}
116
117	for (i = 0; i < num_res; i++, res_acp++) {
118		res[i].name = res_acp->name;
119		res[i].flags = res_acp->flags;
120		res[i].start = addr + res_acp->start;
121		res[i].end = addr + res_acp->end;
122		if (res_acp->flags == IORESOURCE_IRQ) {
123			res[i].start = pci->irq;
124			res[i].end = res[i].start;
125		}
126	}
127
128	memset(&pdevinfo, 0, sizeof(pdevinfo));
129
130	pdevinfo.name = chip->name;
131	pdevinfo.id = 0;
132	pdevinfo.parent = &pci->dev;
133	pdevinfo.num_res = num_res;
134	pdevinfo.res = &res[0];
135	pdevinfo.data = chip;
136	pdevinfo.size_data = sizeof(*chip);
137
138	pdev = platform_device_register_full(&pdevinfo);
139	if (IS_ERR(pdev)) {
140		dev_err(&pci->dev, "cannot register %s device\n", pdevinfo.name);
141		ret = PTR_ERR(pdev);
142		goto unregister_dmic_dev;
143	}
144	chip->chip_pdev = pdev;
145	dev_set_drvdata(&pci->dev, chip);
146	pm_runtime_set_autosuspend_delay(&pci->dev, 2000);
147	pm_runtime_use_autosuspend(&pci->dev);
148	pm_runtime_put_noidle(&pci->dev);
149	pm_runtime_allow(&pci->dev);
150	return ret;
151
152unregister_dmic_dev:
153	platform_device_unregister(dmic_dev);
154release_regions:
155	pci_release_regions(pci);
156disable_pci:
157	pci_disable_device(pci);
158
159	return ret;
160};
161
162static int __maybe_unused snd_acp_suspend(struct device *dev)
163{
164	struct acp_chip_info *chip;
165	int ret;
166
167	chip = dev_get_drvdata(dev);
168	ret = acp_deinit(chip->base);
169	if (ret)
170		dev_err(dev, "ACP de-init failed\n");
171	return ret;
172}
173
174static int __maybe_unused snd_acp_resume(struct device *dev)
175{
176	struct acp_chip_info *chip;
177	struct acp_dev_data *adata;
178	struct device child;
179	int ret;
180
181	chip = dev_get_drvdata(dev);
182	ret = acp_init(chip);
183	if (ret)
184		dev_err(dev, "ACP init failed\n");
185	child = chip->chip_pdev->dev;
186	adata = dev_get_drvdata(&child);
187	if (adata)
188		acp_enable_interrupts(adata);
189	return ret;
190}
191
192static const struct dev_pm_ops acp_pm_ops = {
193	SET_RUNTIME_PM_OPS(snd_acp_suspend, snd_acp_resume, NULL)
194	SET_SYSTEM_SLEEP_PM_OPS(snd_acp_suspend, snd_acp_resume)
195};
196
197static void acp_pci_remove(struct pci_dev *pci)
198{
199	struct acp_chip_info *chip;
200	int ret;
201
202	chip = pci_get_drvdata(pci);
203	pm_runtime_forbid(&pci->dev);
204	pm_runtime_get_noresume(&pci->dev);
205	if (dmic_dev)
206		platform_device_unregister(dmic_dev);
207	if (pdev)
208		platform_device_unregister(pdev);
209	ret = acp_deinit(chip->base);
210	if (ret)
211		dev_err(&pci->dev, "ACP de-init failed\n");
212}
213
214/* PCI IDs */
215static const struct pci_device_id acp_pci_ids[] = {
216	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, ACP_PCI_DEV_ID)},
217	{ 0, }
218};
219MODULE_DEVICE_TABLE(pci, acp_pci_ids);
220
221/* pci_driver definition */
222static struct pci_driver snd_amd_acp_pci_driver = {
223	.name = KBUILD_MODNAME,
224	.id_table = acp_pci_ids,
225	.probe = acp_pci_probe,
226	.remove = acp_pci_remove,
227	.driver = {
228		.pm = &acp_pm_ops,
229	},
230};
231module_pci_driver(snd_amd_acp_pci_driver);
232
233MODULE_LICENSE("Dual BSD/GPL");
234MODULE_IMPORT_NS(SND_SOC_ACP_COMMON);
235MODULE_ALIAS(DRV_NAME);
236