xref: /openbmc/linux/sound/soc/amd/acp/acp-pci.c (revision cd57272c4e686d4ad2d2e775a40a3eac9f96ec7c)
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 acp3x_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 		return -ENOMEM;
74 	}
75 
76 	pci_set_master(pci);
77 
78 	switch (pci->revision) {
79 	case 0x01:
80 		res_acp = acp3x_res;
81 		num_res = ARRAY_SIZE(acp3x_res);
82 		chip->name = "acp_asoc_renoir";
83 		chip->acp_rev = ACP3X_DEV;
84 		break;
85 	case 0x6f:
86 		res_acp = acp3x_res;
87 		num_res = ARRAY_SIZE(acp3x_res);
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 		return -EINVAL;
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 		return PTR_ERR(dmic_dev);
100 	}
101 
102 	addr = pci_resource_start(pci, 0);
103 	chip->base = devm_ioremap(&pci->dev, addr, pci_resource_len(pci, 0));
104 
105 	res = devm_kzalloc(&pci->dev, sizeof(struct resource) * num_res, GFP_KERNEL);
106 	if (!res) {
107 		platform_device_unregister(dmic_dev);
108 		return -ENOMEM;
109 	}
110 
111 	for (i = 0; i < num_res; i++, res_acp++) {
112 		res[i].name = res_acp->name;
113 		res[i].flags = res_acp->flags;
114 		res[i].start = addr + res_acp->start;
115 		res[i].end = addr + res_acp->end;
116 		if (res_acp->flags == IORESOURCE_IRQ) {
117 			res[i].start = pci->irq;
118 			res[i].end = res[i].start;
119 		}
120 	}
121 
122 	memset(&pdevinfo, 0, sizeof(pdevinfo));
123 
124 	pdevinfo.name = chip->name;
125 	pdevinfo.id = 0;
126 	pdevinfo.parent = &pci->dev;
127 	pdevinfo.num_res = num_res;
128 	pdevinfo.res = &res[0];
129 	pdevinfo.data = chip;
130 	pdevinfo.size_data = sizeof(*chip);
131 
132 	pdev = platform_device_register_full(&pdevinfo);
133 	if (IS_ERR(pdev)) {
134 		dev_err(&pci->dev, "cannot register %s device\n", pdevinfo.name);
135 		platform_device_unregister(dmic_dev);
136 		ret = PTR_ERR(pdev);
137 	}
138 
139 	return ret;
140 };
141 
142 static void acp_pci_remove(struct pci_dev *pci)
143 {
144 	if (dmic_dev)
145 		platform_device_unregister(dmic_dev);
146 	if (pdev)
147 		platform_device_unregister(pdev);
148 }
149 
150 /* PCI IDs */
151 static const struct pci_device_id acp_pci_ids[] = {
152 	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, ACP_PCI_DEV_ID)},
153 	{ 0, }
154 };
155 MODULE_DEVICE_TABLE(pci, acp_pci_ids);
156 
157 /* pci_driver definition */
158 static struct pci_driver snd_amd_acp_pci_driver = {
159 	.name = KBUILD_MODNAME,
160 	.id_table = acp_pci_ids,
161 	.probe = acp_pci_probe,
162 	.remove = acp_pci_remove,
163 };
164 module_pci_driver(snd_amd_acp_pci_driver);
165 
166 MODULE_LICENSE("Dual BSD/GPL");
167 MODULE_ALIAS(DRV_NAME);
168