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