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