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