xref: /openbmc/linux/sound/soc/amd/acp/acp-pci.c (revision 657c45b3)
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 
20 #include "amd.h"
21 #include "../mach-config.h"
22 
23 #define DRV_NAME "acp_pci"
24 
25 #define ACP3x_REG_START	0x1240000
26 #define ACP3x_REG_END	0x125C000
27 
28 static struct platform_device *dmic_dev;
29 static struct platform_device *pdev;
30 
31 static const struct resource acp_res[] = {
32 	{
33 		.start = 0,
34 		.end = ACP3x_REG_END - ACP3x_REG_START,
35 		.name = "acp_mem",
36 		.flags = IORESOURCE_MEM,
37 	},
38 	{
39 		.start = 0,
40 		.end = 0,
41 		.name = "acp_dai_irq",
42 		.flags = IORESOURCE_IRQ,
43 	},
44 };
45 
46 static int acp_pci_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
47 {
48 	struct platform_device_info pdevinfo;
49 	struct device *dev = &pci->dev;
50 	const struct resource *res_acp;
51 	struct acp_chip_info *chip;
52 	struct resource *res;
53 	unsigned int flag, addr, num_res, i;
54 	int ret;
55 
56 	flag = snd_amd_acp_find_config(pci);
57 	if (flag != FLAG_AMD_LEGACY)
58 		return -ENODEV;
59 
60 	chip = devm_kzalloc(&pci->dev, sizeof(*chip), GFP_KERNEL);
61 	if (!chip)
62 		return -ENOMEM;
63 
64 	if (pci_enable_device(pci))
65 		return dev_err_probe(&pci->dev, -ENODEV,
66 				     "pci_enable_device failed\n");
67 
68 	ret = pci_request_regions(pci, "AMD ACP3x audio");
69 	if (ret < 0) {
70 		dev_err(&pci->dev, "pci_request_regions failed\n");
71 		ret = -ENOMEM;
72 		goto disable_pci;
73 	}
74 
75 	pci_set_master(pci);
76 
77 	res_acp = acp_res;
78 	num_res = ARRAY_SIZE(acp_res);
79 
80 	switch (pci->revision) {
81 	case 0x01:
82 		chip->name = "acp_asoc_renoir";
83 		chip->acp_rev = ACP3X_DEV;
84 		break;
85 	case 0x6f:
86 		chip->name = "acp_asoc_rembrandt";
87 		chip->acp_rev = ACP6X_DEV;
88 		break;
89 	default:
90 		dev_err(dev, "Unsupported device revision:0x%x\n", pci->revision);
91 		ret = -EINVAL;
92 		goto release_regions;
93 	}
94 
95 	dmic_dev = platform_device_register_data(dev, "dmic-codec", PLATFORM_DEVID_NONE, NULL, 0);
96 	if (IS_ERR(dmic_dev)) {
97 		dev_err(dev, "failed to create DMIC device\n");
98 		ret = PTR_ERR(dmic_dev);
99 		goto release_regions;
100 	}
101 
102 	addr = pci_resource_start(pci, 0);
103 	chip->base = devm_ioremap(&pci->dev, addr, pci_resource_len(pci, 0));
104 	if (!chip->base) {
105 		ret = -ENOMEM;
106 		goto unregister_dmic_dev;
107 	}
108 
109 	res = devm_kcalloc(&pci->dev, num_res, sizeof(struct resource), GFP_KERNEL);
110 	if (!res) {
111 		ret = -ENOMEM;
112 		goto unregister_dmic_dev;
113 	}
114 
115 	for (i = 0; i < num_res; i++, res_acp++) {
116 		res[i].name = res_acp->name;
117 		res[i].flags = res_acp->flags;
118 		res[i].start = addr + res_acp->start;
119 		res[i].end = addr + res_acp->end;
120 		if (res_acp->flags == IORESOURCE_IRQ) {
121 			res[i].start = pci->irq;
122 			res[i].end = res[i].start;
123 		}
124 	}
125 
126 	memset(&pdevinfo, 0, sizeof(pdevinfo));
127 
128 	pdevinfo.name = chip->name;
129 	pdevinfo.id = 0;
130 	pdevinfo.parent = &pci->dev;
131 	pdevinfo.num_res = num_res;
132 	pdevinfo.res = &res[0];
133 	pdevinfo.data = chip;
134 	pdevinfo.size_data = sizeof(*chip);
135 
136 	pdev = platform_device_register_full(&pdevinfo);
137 	if (IS_ERR(pdev)) {
138 		dev_err(&pci->dev, "cannot register %s device\n", pdevinfo.name);
139 		ret = PTR_ERR(pdev);
140 		goto unregister_dmic_dev;
141 	}
142 
143 	return ret;
144 
145 unregister_dmic_dev:
146 	platform_device_unregister(dmic_dev);
147 release_regions:
148 	pci_release_regions(pci);
149 disable_pci:
150 	pci_disable_device(pci);
151 
152 	return ret;
153 };
154 
155 static void acp_pci_remove(struct pci_dev *pci)
156 {
157 	if (dmic_dev)
158 		platform_device_unregister(dmic_dev);
159 	if (pdev)
160 		platform_device_unregister(pdev);
161 }
162 
163 /* PCI IDs */
164 static const struct pci_device_id acp_pci_ids[] = {
165 	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, ACP_PCI_DEV_ID)},
166 	{ 0, }
167 };
168 MODULE_DEVICE_TABLE(pci, acp_pci_ids);
169 
170 /* pci_driver definition */
171 static struct pci_driver snd_amd_acp_pci_driver = {
172 	.name = KBUILD_MODNAME,
173 	.id_table = acp_pci_ids,
174 	.probe = acp_pci_probe,
175 	.remove = acp_pci_remove,
176 };
177 module_pci_driver(snd_amd_acp_pci_driver);
178 
179 MODULE_LICENSE("Dual BSD/GPL");
180 MODULE_ALIAS(DRV_NAME);
181