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
irq_handler(int irq,void * data)29 static irqreturn_t irq_handler(int irq, void *data)
30 {
31 struct acp_chip_info *chip = data;
32
33 if (chip && chip->acp_hw_ops && chip->acp_hw_ops->irq)
34 return chip->acp_hw_ops->irq(irq, chip);
35
36 return IRQ_NONE;
37 }
acp_fill_platform_dev_info(struct platform_device_info * pdevinfo,struct device * parent,struct fwnode_handle * fw_node,char * name,unsigned int id,const struct resource * res,unsigned int num_res,const void * data,size_t size_data)38 static void acp_fill_platform_dev_info(struct platform_device_info *pdevinfo,
39 struct device *parent,
40 struct fwnode_handle *fw_node,
41 char *name, unsigned int id,
42 const struct resource *res,
43 unsigned int num_res,
44 const void *data,
45 size_t size_data)
46 {
47 pdevinfo->name = name;
48 pdevinfo->id = id;
49 pdevinfo->parent = parent;
50 pdevinfo->num_res = num_res;
51 pdevinfo->res = res;
52 pdevinfo->data = data;
53 pdevinfo->size_data = size_data;
54 pdevinfo->fwnode = fw_node;
55 }
56
create_acp_platform_devs(struct pci_dev * pci,struct acp_chip_info * chip,u32 addr)57 static int create_acp_platform_devs(struct pci_dev *pci, struct acp_chip_info *chip, u32 addr)
58 {
59 struct platform_device_info pdevinfo;
60 struct device *parent;
61 int ret;
62
63 parent = &pci->dev;
64
65 if (chip->is_i2s_config || chip->is_pdm_dev) {
66 chip->res = devm_kzalloc(&pci->dev, sizeof(struct resource), GFP_KERNEL);
67 if (!chip->res) {
68 ret = -ENOMEM;
69 goto err;
70 }
71 chip->res->flags = IORESOURCE_MEM;
72 chip->res->start = addr;
73 chip->res->end = addr + (ACP3x_REG_END - ACP3x_REG_START);
74 memset(&pdevinfo, 0, sizeof(pdevinfo));
75 }
76
77 memset(&pdevinfo, 0, sizeof(pdevinfo));
78 acp_fill_platform_dev_info(&pdevinfo, parent, NULL, chip->name,
79 0, chip->res, 1, chip, sizeof(*chip));
80
81 chip->acp_plat_dev = platform_device_register_full(&pdevinfo);
82 if (IS_ERR(chip->acp_plat_dev)) {
83 dev_err(&pci->dev,
84 "cannot register %s device\n", pdevinfo.name);
85 ret = PTR_ERR(chip->acp_plat_dev);
86 goto err;
87 }
88 if (chip->is_pdm_dev && chip->is_pdm_config) {
89 chip->dmic_codec_dev = platform_device_register_data(&pci->dev,
90 "dmic-codec",
91 PLATFORM_DEVID_NONE,
92 NULL, 0);
93 if (IS_ERR(chip->dmic_codec_dev)) {
94 dev_err(&pci->dev, "failed to create DMIC device\n");
95 ret = PTR_ERR(chip->dmic_codec_dev);
96 goto unregister_acp_plat_dev;
97 }
98 }
99 return 0;
100 unregister_acp_plat_dev:
101 platform_device_unregister(chip->acp_plat_dev);
102 err:
103 return ret;
104 }
105
acp_pci_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)106 static int acp_pci_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
107 {
108 struct device *dev = &pci->dev;
109 struct acp_chip_info *chip;
110 unsigned int flag, addr;
111 int ret;
112
113 flag = snd_amd_acp_find_config(pci);
114 if (flag != FLAG_AMD_LEGACY && flag != FLAG_AMD_LEGACY_ONLY_DMIC)
115 return -ENODEV;
116
117 chip = devm_kzalloc(&pci->dev, sizeof(*chip), GFP_KERNEL);
118 if (!chip)
119 return -ENOMEM;
120
121 if (pci_enable_device(pci))
122 return dev_err_probe(&pci->dev, -ENODEV,
123 "pci_enable_device failed\n");
124
125 ret = pci_request_regions(pci, "AMD ACP3x audio");
126 if (ret < 0) {
127 dev_err(&pci->dev, "pci_request_regions failed\n");
128 ret = -ENOMEM;
129 goto disable_pci;
130 }
131
132 pci_set_master(pci);
133
134 chip->acp_rev = pci->revision;
135 switch (pci->revision) {
136 case 0x01:
137 chip->name = "acp_asoc_renoir";
138 chip->rsrc = &rn_rsrc;
139 chip->acp_hw_ops_init = acp31_hw_ops_init;
140 chip->machines = &snd_soc_acpi_amd_acp_machines;
141 break;
142 case 0x6f:
143 chip->name = "acp_asoc_rembrandt";
144 chip->rsrc = &rmb_rsrc;
145 chip->acp_hw_ops_init = acp6x_hw_ops_init;
146 chip->machines = &snd_soc_acpi_amd_rmb_acp_machines;
147 break;
148 case 0x63:
149 chip->name = "acp_asoc_acp63";
150 chip->rsrc = &acp63_rsrc;
151 chip->acp_hw_ops_init = acp63_hw_ops_init;
152 chip->machines = &snd_soc_acpi_amd_acp63_acp_machines;
153 break;
154 case 0x70:
155 case 0x71:
156 chip->name = "acp_asoc_acp70";
157 chip->rsrc = &acp70_rsrc;
158 chip->acp_hw_ops_init = acp70_hw_ops_init;
159 chip->machines = &snd_soc_acpi_amd_acp70_acp_machines;
160 break;
161 default:
162 dev_err(dev, "Unsupported device revision:0x%x\n", pci->revision);
163 ret = -EINVAL;
164 goto release_regions;
165 }
166 chip->flag = flag;
167
168 addr = pci_resource_start(pci, 0);
169 chip->base = devm_ioremap(&pci->dev, addr, pci_resource_len(pci, 0));
170 if (!chip->base) {
171 ret = -ENOMEM;
172 goto release_regions;
173 }
174
175 chip->addr = addr;
176
177 chip->acp_hw_ops_init(chip);
178 ret = acp_hw_init(chip);
179 if (ret)
180 goto release_regions;
181
182 ret = devm_request_irq(dev, pci->irq, irq_handler,
183 IRQF_SHARED, "ACP_I2S_IRQ", chip);
184 if (ret) {
185 dev_err(&pci->dev, "ACP I2S IRQ request failed %d\n", ret);
186 goto de_init;
187 }
188
189 check_acp_config(pci, chip);
190 if (!chip->is_pdm_dev && !chip->is_i2s_config)
191 goto skip_pdev_creation;
192
193 ret = create_acp_platform_devs(pci, chip, addr);
194 if (ret < 0) {
195 dev_err(&pci->dev, "ACP platform devices creation failed\n");
196 goto de_init;
197 }
198
199 chip->chip_pdev = chip->acp_plat_dev;
200 chip->dev = &chip->acp_plat_dev->dev;
201
202 acp_machine_select(chip);
203
204 INIT_LIST_HEAD(&chip->stream_list);
205 spin_lock_init(&chip->acp_lock);
206 skip_pdev_creation:
207 dev_set_drvdata(&pci->dev, chip);
208 pm_runtime_set_autosuspend_delay(&pci->dev, 2000);
209 pm_runtime_use_autosuspend(&pci->dev);
210 pm_runtime_put_noidle(&pci->dev);
211 pm_runtime_allow(&pci->dev);
212 return ret;
213
214 de_init:
215 acp_hw_deinit(chip);
216 release_regions:
217 pci_release_regions(pci);
218 disable_pci:
219 pci_disable_device(pci);
220
221 return ret;
222 };
223
snd_acp_suspend(struct device * dev)224 static int snd_acp_suspend(struct device *dev)
225 {
226 struct acp_chip_info *chip;
227 int ret;
228
229 chip = dev_get_drvdata(dev);
230 ret = acp_hw_deinit(chip);
231 if (ret)
232 dev_err(dev, "ACP de-init failed\n");
233 return ret;
234 }
235
snd_acp_resume(struct device * dev)236 static int snd_acp_resume(struct device *dev)
237 {
238 struct acp_chip_info *chip;
239 int ret;
240
241 chip = dev_get_drvdata(dev);
242 ret = acp_hw_init(chip);
243 if (ret)
244 dev_err(dev, "ACP init failed\n");
245
246 ret = acp_hw_en_interrupts(chip);
247 if (ret)
248 dev_err(dev, "ACP en-interrupts failed\n");
249
250 return ret;
251 }
252
253 static const struct dev_pm_ops acp_pm_ops = {
254 RUNTIME_PM_OPS(snd_acp_suspend, snd_acp_resume, NULL)
255 SYSTEM_SLEEP_PM_OPS(snd_acp_suspend, snd_acp_resume)
256 };
257
acp_pci_remove(struct pci_dev * pci)258 static void acp_pci_remove(struct pci_dev *pci)
259 {
260 struct acp_chip_info *chip;
261 int ret;
262
263 chip = pci_get_drvdata(pci);
264 pm_runtime_forbid(&pci->dev);
265 pm_runtime_get_noresume(&pci->dev);
266 if (chip->dmic_codec_dev)
267 platform_device_unregister(chip->dmic_codec_dev);
268 if (chip->acp_plat_dev)
269 platform_device_unregister(chip->acp_plat_dev);
270 if (chip->mach_dev)
271 platform_device_unregister(chip->mach_dev);
272
273 ret = acp_hw_deinit(chip);
274 if (ret)
275 dev_err(&pci->dev, "ACP de-init failed\n");
276 }
277
278 /* PCI IDs */
279 static const struct pci_device_id acp_pci_ids[] = {
280 { PCI_DEVICE(PCI_VENDOR_ID_AMD, ACP_PCI_DEV_ID)},
281 { 0, }
282 };
283 MODULE_DEVICE_TABLE(pci, acp_pci_ids);
284
285 /* pci_driver definition */
286 static struct pci_driver snd_amd_acp_pci_driver = {
287 .name = KBUILD_MODNAME,
288 .id_table = acp_pci_ids,
289 .probe = acp_pci_probe,
290 .remove = acp_pci_remove,
291 .driver = {
292 .pm = pm_ptr(&acp_pm_ops),
293 },
294 };
295 module_pci_driver(snd_amd_acp_pci_driver);
296
297 MODULE_DESCRIPTION("AMD ACP common PCI support");
298 MODULE_LICENSE("Dual BSD/GPL");
299 MODULE_IMPORT_NS("SND_SOC_ACP_COMMON");
300 MODULE_ALIAS(DRV_NAME);
301