xref: /openbmc/linux/sound/soc/amd/acp/acp-pci.c (revision 6c8c1406)
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/pm_runtime.h>
19 #include <linux/module.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 
29 static struct platform_device *dmic_dev;
30 static struct platform_device *pdev;
31 
32 static 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 
47 static 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 	res = devm_kcalloc(&pci->dev, num_res, sizeof(struct resource), GFP_KERNEL);
111 	if (!res) {
112 		ret = -ENOMEM;
113 		goto unregister_dmic_dev;
114 	}
115 
116 	for (i = 0; i < num_res; i++, res_acp++) {
117 		res[i].name = res_acp->name;
118 		res[i].flags = res_acp->flags;
119 		res[i].start = addr + res_acp->start;
120 		res[i].end = addr + res_acp->end;
121 		if (res_acp->flags == IORESOURCE_IRQ) {
122 			res[i].start = pci->irq;
123 			res[i].end = res[i].start;
124 		}
125 	}
126 
127 	memset(&pdevinfo, 0, sizeof(pdevinfo));
128 
129 	pdevinfo.name = chip->name;
130 	pdevinfo.id = 0;
131 	pdevinfo.parent = &pci->dev;
132 	pdevinfo.num_res = num_res;
133 	pdevinfo.res = &res[0];
134 	pdevinfo.data = chip;
135 	pdevinfo.size_data = sizeof(*chip);
136 
137 	pdev = platform_device_register_full(&pdevinfo);
138 	if (IS_ERR(pdev)) {
139 		dev_err(&pci->dev, "cannot register %s device\n", pdevinfo.name);
140 		ret = PTR_ERR(pdev);
141 		goto unregister_dmic_dev;
142 	}
143 
144 	return ret;
145 
146 unregister_dmic_dev:
147 	platform_device_unregister(dmic_dev);
148 release_regions:
149 	pci_release_regions(pci);
150 disable_pci:
151 	pci_disable_device(pci);
152 
153 	return ret;
154 };
155 
156 static void acp_pci_remove(struct pci_dev *pci)
157 {
158 	if (dmic_dev)
159 		platform_device_unregister(dmic_dev);
160 	if (pdev)
161 		platform_device_unregister(pdev);
162 }
163 
164 /* PCI IDs */
165 static const struct pci_device_id acp_pci_ids[] = {
166 	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, ACP_PCI_DEV_ID)},
167 	{ 0, }
168 };
169 MODULE_DEVICE_TABLE(pci, acp_pci_ids);
170 
171 /* pci_driver definition */
172 static struct pci_driver snd_amd_acp_pci_driver = {
173 	.name = KBUILD_MODNAME,
174 	.id_table = acp_pci_ids,
175 	.probe = acp_pci_probe,
176 	.remove = acp_pci_remove,
177 };
178 module_pci_driver(snd_amd_acp_pci_driver);
179 
180 MODULE_LICENSE("Dual BSD/GPL");
181 MODULE_ALIAS(DRV_NAME);
182