sof-pci-dev.c 9.3 KB

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