xref: /linux-6.15/sound/soc/sof/sof-pci-dev.c (revision 58c520e2)
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) 2018 Intel Corporation. All rights reserved.
7 //
8 // Author: Liam Girdwood <[email protected]>
9 //
10 
11 #include <linux/firmware.h>
12 #include <linux/dmi.h>
13 #include <linux/module.h>
14 #include <linux/pci.h>
15 #include <linux/pm_runtime.h>
16 #include <sound/soc-acpi.h>
17 #include <sound/soc-acpi-intel-match.h>
18 #include <sound/sof.h>
19 #include "ops.h"
20 #include "sof-pci-dev.h"
21 
22 static char *fw_path;
23 module_param(fw_path, charp, 0444);
24 MODULE_PARM_DESC(fw_path, "alternate path for SOF firmware.");
25 
26 static char *fw_filename;
27 module_param(fw_filename, charp, 0444);
28 MODULE_PARM_DESC(fw_filename, "alternate filename for SOF firmware.");
29 
30 static char *tplg_path;
31 module_param(tplg_path, charp, 0444);
32 MODULE_PARM_DESC(tplg_path, "alternate path for SOF topology.");
33 
34 static int sof_pci_debug;
35 module_param_named(sof_pci_debug, sof_pci_debug, int, 0444);
36 MODULE_PARM_DESC(sof_pci_debug, "SOF PCI debug options (0x0 all off)");
37 
38 static const char *sof_override_tplg_name;
39 
40 #define SOF_PCI_DISABLE_PM_RUNTIME BIT(0)
41 
42 static int sof_tplg_cb(const struct dmi_system_id *id)
43 {
44 	sof_override_tplg_name = id->driver_data;
45 	return 1;
46 }
47 
48 static const struct dmi_system_id sof_tplg_table[] = {
49 	{
50 		.callback = sof_tplg_cb,
51 		.matches = {
52 			DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Volteer"),
53 			DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98373_ALC5682I_I2S_UP4"),
54 		},
55 		.driver_data = "sof-tgl-rt5682-ssp0-max98373-ssp2.tplg",
56 	},
57 	{
58 		.callback = sof_tplg_cb,
59 		.matches = {
60 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
61 			DMI_MATCH(DMI_PRODUCT_NAME, "Alder Lake Client Platform"),
62 			DMI_MATCH(DMI_OEM_STRING, "AUDIO-ADL_MAX98373_ALC5682I_I2S"),
63 		},
64 		.driver_data = "sof-adl-rt5682-ssp0-max98373-ssp2.tplg",
65 	},
66 	{
67 		.callback = sof_tplg_cb,
68 		.matches = {
69 			DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
70 			DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98390_ALC5682I_I2S"),
71 		},
72 		.driver_data = "sof-adl-max98390-ssp2-rt5682-ssp0.tplg",
73 	},
74 	{
75 		.callback = sof_tplg_cb,
76 		.matches = {
77 			DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
78 			DMI_MATCH(DMI_OEM_STRING, "AUDIO_AMP-MAX98360_ALC5682VS_I2S_2WAY"),
79 		},
80 		.driver_data = "sof-adl-max98360a-rt5682-2way.tplg",
81 	},
82 	{
83 		.callback = sof_tplg_cb,
84 		.matches = {
85 			DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
86 			DMI_MATCH(DMI_OEM_STRING, "AUDIO-AUDIO_MAX98357_ALC5682I_I2S_2WAY"),
87 		},
88 		.driver_data = "sof-adl-max98357a-rt5682-2way.tplg",
89 	},
90 	{
91 		.callback = sof_tplg_cb,
92 		.matches = {
93 			DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
94 			DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98360_ALC5682I_I2S_AMP_SSP2"),
95 		},
96 		.driver_data = "sof-adl-max98357a-rt5682.tplg",
97 	},
98 	{}
99 };
100 
101 static const struct dmi_system_id community_key_platforms[] = {
102 	{
103 		.ident = "Up boards",
104 		.matches = {
105 			DMI_MATCH(DMI_SYS_VENDOR, "AAEON"),
106 		}
107 	},
108 	{
109 		.ident = "Google Chromebooks",
110 		.matches = {
111 			DMI_MATCH(DMI_SYS_VENDOR, "Google"),
112 		}
113 	},
114 	{},
115 };
116 
117 const struct dev_pm_ops sof_pci_pm = {
118 	.prepare = snd_sof_prepare,
119 	.complete = snd_sof_complete,
120 	SET_SYSTEM_SLEEP_PM_OPS(snd_sof_suspend, snd_sof_resume)
121 	SET_RUNTIME_PM_OPS(snd_sof_runtime_suspend, snd_sof_runtime_resume,
122 			   snd_sof_runtime_idle)
123 };
124 EXPORT_SYMBOL_NS(sof_pci_pm, SND_SOC_SOF_PCI_DEV);
125 
126 static void sof_pci_probe_complete(struct device *dev)
127 {
128 	dev_dbg(dev, "Completing SOF PCI probe");
129 
130 	if (sof_pci_debug & SOF_PCI_DISABLE_PM_RUNTIME)
131 		return;
132 
133 	/* allow runtime_pm */
134 	pm_runtime_set_autosuspend_delay(dev, SND_SOF_SUSPEND_DELAY_MS);
135 	pm_runtime_use_autosuspend(dev);
136 
137 	/*
138 	 * runtime pm for pci device is "forbidden" by default.
139 	 * so call pm_runtime_allow() to enable it.
140 	 */
141 	pm_runtime_allow(dev);
142 
143 	/* mark last_busy for pm_runtime to make sure not suspend immediately */
144 	pm_runtime_mark_last_busy(dev);
145 
146 	/* follow recommendation in pci-driver.c to decrement usage counter */
147 	pm_runtime_put_noidle(dev);
148 }
149 
150 int sof_pci_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
151 {
152 	struct device *dev = &pci->dev;
153 	const struct sof_dev_desc *desc =
154 		(const struct sof_dev_desc *)pci_id->driver_data;
155 	struct snd_sof_pdata *sof_pdata;
156 	int ret;
157 
158 	dev_dbg(&pci->dev, "PCI DSP detected");
159 
160 	if (!desc->ops) {
161 		dev_err(dev, "error: no matching PCI descriptor ops\n");
162 		return -ENODEV;
163 	}
164 
165 	sof_pdata = devm_kzalloc(dev, sizeof(*sof_pdata), GFP_KERNEL);
166 	if (!sof_pdata)
167 		return -ENOMEM;
168 
169 	ret = pcim_enable_device(pci);
170 	if (ret < 0)
171 		return ret;
172 
173 	ret = pci_request_regions(pci, "Audio DSP");
174 	if (ret < 0)
175 		return ret;
176 
177 	sof_pdata->name = pci_name(pci);
178 	sof_pdata->desc = desc;
179 	sof_pdata->dev = dev;
180 
181 	sof_pdata->ipc_type = desc->ipc_default;
182 
183 	if (fw_filename) {
184 		sof_pdata->fw_filename = fw_filename;
185 
186 		dev_dbg(dev, "Module parameter used, changed fw filename to %s\n",
187 			sof_pdata->fw_filename);
188 	} else {
189 		sof_pdata->fw_filename = desc->default_fw_filename[sof_pdata->ipc_type];
190 	}
191 
192 	/*
193 	 * for platforms using the SOF community key, change the
194 	 * default path automatically to pick the right files from the
195 	 * linux-firmware tree. This can be overridden with the
196 	 * fw_path kernel parameter, e.g. for developers.
197 	 */
198 
199 	/* alternate fw and tplg filenames ? */
200 	if (fw_path) {
201 		sof_pdata->fw_filename_prefix = fw_path;
202 
203 		dev_dbg(dev,
204 			"Module parameter used, changed fw path to %s\n",
205 			sof_pdata->fw_filename_prefix);
206 
207 	} else if (dmi_check_system(community_key_platforms)) {
208 		sof_pdata->fw_filename_prefix =
209 			devm_kasprintf(dev, GFP_KERNEL, "%s/%s",
210 				       sof_pdata->desc->default_fw_path[sof_pdata->ipc_type],
211 				       "community");
212 
213 		dev_dbg(dev,
214 			"Platform uses community key, changed fw path to %s\n",
215 			sof_pdata->fw_filename_prefix);
216 	} else {
217 		sof_pdata->fw_filename_prefix =
218 			sof_pdata->desc->default_fw_path[sof_pdata->ipc_type];
219 	}
220 
221 	if (tplg_path)
222 		sof_pdata->tplg_filename_prefix = tplg_path;
223 	else
224 		sof_pdata->tplg_filename_prefix =
225 			sof_pdata->desc->default_tplg_path[sof_pdata->ipc_type];
226 
227 	dmi_check_system(sof_tplg_table);
228 	if (sof_override_tplg_name)
229 		sof_pdata->tplg_filename = sof_override_tplg_name;
230 
231 	/* set callback to be called on successful device probe to enable runtime_pm */
232 	sof_pdata->sof_probe_complete = sof_pci_probe_complete;
233 
234 	/* call sof helper for DSP hardware probe */
235 	ret = snd_sof_device_probe(dev, sof_pdata);
236 	if (ret)
237 		pci_release_regions(pci);
238 
239 	return ret;
240 }
241 EXPORT_SYMBOL_NS(sof_pci_probe, SND_SOC_SOF_PCI_DEV);
242 
243 void sof_pci_remove(struct pci_dev *pci)
244 {
245 	/* call sof helper for DSP hardware remove */
246 	snd_sof_device_remove(&pci->dev);
247 
248 	/* follow recommendation in pci-driver.c to increment usage counter */
249 	if (snd_sof_device_probe_completed(&pci->dev) &&
250 	    !(sof_pci_debug & SOF_PCI_DISABLE_PM_RUNTIME))
251 		pm_runtime_get_noresume(&pci->dev);
252 
253 	/* release pci regions and disable device */
254 	pci_release_regions(pci);
255 }
256 EXPORT_SYMBOL_NS(sof_pci_remove, SND_SOC_SOF_PCI_DEV);
257 
258 void sof_pci_shutdown(struct pci_dev *pci)
259 {
260 	snd_sof_device_shutdown(&pci->dev);
261 }
262 EXPORT_SYMBOL_NS(sof_pci_shutdown, SND_SOC_SOF_PCI_DEV);
263 
264 MODULE_LICENSE("Dual BSD/GPL");
265