xref: /openbmc/linux/sound/soc/amd/acp/acp-pci.c (revision 6726d552)
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 		dev_err(&pci->dev, "pci_enable_device failed\n");
67 		return -ENODEV;
68 	}
69 
70 	ret = pci_request_regions(pci, "AMD ACP3x audio");
71 	if (ret < 0) {
72 		dev_err(&pci->dev, "pci_request_regions failed\n");
73 		ret = -ENOMEM;
74 		goto disable_pci;
75 	}
76 
77 	pci_set_master(pci);
78 
79 	res_acp = acp_res;
80 	num_res = ARRAY_SIZE(acp_res);
81 
82 	switch (pci->revision) {
83 	case 0x01:
84 		chip->name = "acp_asoc_renoir";
85 		chip->acp_rev = ACP3X_DEV;
86 		break;
87 	case 0x6f:
88 		chip->name = "acp_asoc_rembrandt";
89 		chip->acp_rev = ACP6X_DEV;
90 		break;
91 	default:
92 		dev_err(dev, "Unsupported device revision:0x%x\n", pci->revision);
93 		ret = -EINVAL;
94 		goto release_regions;
95 	}
96 
97 	dmic_dev = platform_device_register_data(dev, "dmic-codec", PLATFORM_DEVID_NONE, NULL, 0);
98 	if (IS_ERR(dmic_dev)) {
99 		dev_err(dev, "failed to create DMIC device\n");
100 		ret = PTR_ERR(dmic_dev);
101 		goto release_regions;
102 	}
103 
104 	addr = pci_resource_start(pci, 0);
105 	chip->base = devm_ioremap(&pci->dev, addr, pci_resource_len(pci, 0));
106 	if (!chip->base) {
107 		ret = -ENOMEM;
108 		goto release_regions;
109 	}
110 
111 	res = devm_kzalloc(&pci->dev, sizeof(struct resource) * num_res, GFP_KERNEL);
112 	if (!res) {
113 		platform_device_unregister(dmic_dev);
114 		ret = -ENOMEM;
115 		goto release_regions;
116 	}
117 
118 	for (i = 0; i < num_res; i++, res_acp++) {
119 		res[i].name = res_acp->name;
120 		res[i].flags = res_acp->flags;
121 		res[i].start = addr + res_acp->start;
122 		res[i].end = addr + res_acp->end;
123 		if (res_acp->flags == IORESOURCE_IRQ) {
124 			res[i].start = pci->irq;
125 			res[i].end = res[i].start;
126 		}
127 	}
128 
129 	memset(&pdevinfo, 0, sizeof(pdevinfo));
130 
131 	pdevinfo.name = chip->name;
132 	pdevinfo.id = 0;
133 	pdevinfo.parent = &pci->dev;
134 	pdevinfo.num_res = num_res;
135 	pdevinfo.res = &res[0];
136 	pdevinfo.data = chip;
137 	pdevinfo.size_data = sizeof(*chip);
138 
139 	pdev = platform_device_register_full(&pdevinfo);
140 	if (IS_ERR(pdev)) {
141 		dev_err(&pci->dev, "cannot register %s device\n", pdevinfo.name);
142 		platform_device_unregister(dmic_dev);
143 		ret = PTR_ERR(pdev);
144 		goto release_regions;
145 	}
146 
147 	return ret;
148 
149 release_regions:
150 	pci_release_regions(pci);
151 disable_pci:
152 	pci_disable_device(pci);
153 
154 	return ret;
155 };
156 
157 static void acp_pci_remove(struct pci_dev *pci)
158 {
159 	if (dmic_dev)
160 		platform_device_unregister(dmic_dev);
161 	if (pdev)
162 		platform_device_unregister(pdev);
163 }
164 
165 /* PCI IDs */
166 static const struct pci_device_id acp_pci_ids[] = {
167 	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, ACP_PCI_DEV_ID)},
168 	{ 0, }
169 };
170 MODULE_DEVICE_TABLE(pci, acp_pci_ids);
171 
172 /* pci_driver definition */
173 static struct pci_driver snd_amd_acp_pci_driver = {
174 	.name = KBUILD_MODNAME,
175 	.id_table = acp_pci_ids,
176 	.probe = acp_pci_probe,
177 	.remove = acp_pci_remove,
178 };
179 module_pci_driver(snd_amd_acp_pci_driver);
180 
181 MODULE_LICENSE("Dual BSD/GPL");
182 MODULE_ALIAS(DRV_NAME);
183