xref: /openbmc/linux/sound/soc/sof/amd/pci-rn.c (revision 53f9cd5c)
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) 2021 Advanced Micro Devices, Inc. All rights reserved.
7 //
8 // Authors: Ajit Kumar Pandey <AjitKumar.Pandey@amd.com>
9 
10 /*
11  * PCI interface for Renoir ACP device
12  */
13 
14 #include <linux/module.h>
15 #include <linux/pci.h>
16 #include <linux/platform_device.h>
17 #include <sound/sof.h>
18 #include <sound/soc-acpi.h>
19 
20 #include "../ops.h"
21 #include "../sof-pci-dev.h"
22 #include "../../amd/mach-config.h"
23 #include "acp.h"
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 renoir_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 const struct sof_amd_acp_desc renoir_chip_info = {
47 	.host_bridge_id = HOST_BRIDGE_CZN,
48 };
49 
50 static const struct sof_dev_desc renoir_desc = {
51 	.machines		= snd_soc_acpi_amd_sof_machines,
52 	.use_acpi_target_states	= true,
53 	.resindex_lpe_base	= 0,
54 	.resindex_pcicfg_base	= -1,
55 	.resindex_imr_base	= -1,
56 	.irqindex_host_ipc	= -1,
57 	.chip_info		= &renoir_chip_info,
58 	.ipc_supported_mask	= BIT(SOF_IPC),
59 	.ipc_default		= SOF_IPC,
60 	.default_fw_path = {
61 		[SOF_IPC] = "amd/sof",
62 	},
63 	.default_tplg_path = {
64 		[SOF_IPC] = "amd/sof-tplg",
65 	},
66 	.default_fw_filename	= {
67 		[SOF_IPC] = "sof-rn.ri",
68 	},
69 	.nocodec_tplg_filename	= "sof-acp.tplg",
70 	.ops			= &sof_renoir_ops,
71 };
72 
73 static int acp_pci_rn_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
74 {
75 	struct platform_device_info pdevinfo;
76 	struct device *dev = &pci->dev;
77 	const struct resource *res_i2s;
78 	struct resource *res;
79 	unsigned int flag, i, addr;
80 	int ret;
81 
82 	flag = snd_amd_acp_find_config(pci);
83 	if (flag != FLAG_AMD_SOF && flag != FLAG_AMD_SOF_ONLY_DMIC)
84 		return -ENODEV;
85 
86 	ret = sof_pci_probe(pci, pci_id);
87 	if (ret != 0)
88 		return ret;
89 
90 	dmic_dev = platform_device_register_data(dev, "dmic-codec", PLATFORM_DEVID_NONE, NULL, 0);
91 	if (IS_ERR(dmic_dev)) {
92 		dev_err(dev, "failed to create DMIC device\n");
93 		sof_pci_remove(pci);
94 		return PTR_ERR(dmic_dev);
95 	}
96 
97 	/* Register platform device only if flag set to FLAG_AMD_SOF_ONLY_DMIC */
98 	if (flag != FLAG_AMD_SOF_ONLY_DMIC)
99 		return 0;
100 
101 	addr = pci_resource_start(pci, 0);
102 	res = devm_kzalloc(&pci->dev, sizeof(struct resource) * ARRAY_SIZE(renoir_res), GFP_KERNEL);
103 	if (!res) {
104 		sof_pci_remove(pci);
105 		platform_device_unregister(dmic_dev);
106 		return -ENOMEM;
107 	}
108 
109 	res_i2s = renoir_res;
110 	for (i = 0; i < ARRAY_SIZE(renoir_res); i++, res_i2s++) {
111 		res[i].name = res_i2s->name;
112 		res[i].flags = res_i2s->flags;
113 		res[i].start = addr + res_i2s->start;
114 		res[i].end = addr + res_i2s->end;
115 		if (res_i2s->flags == IORESOURCE_IRQ) {
116 			res[i].start = pci->irq;
117 			res[i].end = res[i].start;
118 		}
119 	}
120 
121 	memset(&pdevinfo, 0, sizeof(pdevinfo));
122 
123 	/*
124 	 * We have common PCI driver probe for ACP device but we have to support I2S without SOF
125 	 * for some distributions. Register platform device that will be used to support non dsp
126 	 * ACP's audio ends points on some machines.
127 	 */
128 
129 	pdevinfo.name = "acp_asoc_renoir";
130 	pdevinfo.id = 0;
131 	pdevinfo.parent = &pci->dev;
132 	pdevinfo.num_res = ARRAY_SIZE(renoir_res);
133 	pdevinfo.res = &res[0];
134 
135 	pdev = platform_device_register_full(&pdevinfo);
136 	if (IS_ERR(pdev)) {
137 		dev_err(&pci->dev, "cannot register %s device\n", pdevinfo.name);
138 		sof_pci_remove(pci);
139 		platform_device_unregister(dmic_dev);
140 		ret = PTR_ERR(pdev);
141 	}
142 
143 	return ret;
144 };
145 
146 static void acp_pci_rn_remove(struct pci_dev *pci)
147 {
148 	if (dmic_dev)
149 		platform_device_unregister(dmic_dev);
150 	if (pdev)
151 		platform_device_unregister(pdev);
152 
153 	return sof_pci_remove(pci);
154 }
155 
156 /* PCI IDs */
157 static const struct pci_device_id rn_pci_ids[] = {
158 	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, ACP_PCI_DEV_ID),
159 	.driver_data = (unsigned long)&renoir_desc},
160 	{ 0, }
161 };
162 MODULE_DEVICE_TABLE(pci, rn_pci_ids);
163 
164 /* pci_driver definition */
165 static struct pci_driver snd_sof_pci_amd_rn_driver = {
166 	.name = KBUILD_MODNAME,
167 	.id_table = rn_pci_ids,
168 	.probe = acp_pci_rn_probe,
169 	.remove = acp_pci_rn_remove,
170 	.driver = {
171 		.pm = &sof_pci_pm,
172 	},
173 };
174 module_pci_driver(snd_sof_pci_amd_rn_driver);
175 
176 MODULE_LICENSE("Dual BSD/GPL");
177 MODULE_IMPORT_NS(SND_SOC_SOF_AMD_COMMON);
178 MODULE_IMPORT_NS(SND_SOC_SOF_PCI_DEV);
179