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