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