hda.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  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
  7. //
  8. // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
  9. // Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
  10. // Rander Wang <rander.wang@intel.com>
  11. // Keyon Jie <yang.jie@linux.intel.com>
  12. //
  13. /*
  14. * Hardware interface for generic Intel audio DSP HDA IP
  15. */
  16. #include <sound/hdaudio_ext.h>
  17. #include <sound/hda_register.h>
  18. #include <linux/acpi.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/module.h>
  21. #include <linux/soundwire/sdw.h>
  22. #include <linux/soundwire/sdw_intel.h>
  23. #include <sound/intel-dsp-config.h>
  24. #include <sound/intel-nhlt.h>
  25. #include <sound/soc-acpi-intel-ssp-common.h>
  26. #include <sound/sof.h>
  27. #include <sound/sof/xtensa.h>
  28. #include <sound/hda-mlink.h>
  29. #include "../sof-audio.h"
  30. #include "../sof-pci-dev.h"
  31. #include "../ops.h"
  32. #include "../ipc4-topology.h"
  33. #include "hda.h"
  34. #include <trace/events/sof_intel.h>
  35. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
  36. #include <sound/soc-acpi-intel-match.h>
  37. #endif
  38. /* platform specific devices */
  39. #include "shim.h"
  40. #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
  41. /*
  42. * The default for SoundWire clock stop quirks is to power gate the IP
  43. * and do a Bus Reset, this will need to be modified when the DSP
  44. * needs to remain in D0i3 so that the Master does not lose context
  45. * and enumeration is not required on clock restart
  46. */
  47. static int sdw_clock_stop_quirks = SDW_INTEL_CLK_STOP_BUS_RESET;
  48. module_param(sdw_clock_stop_quirks, int, 0444);
  49. MODULE_PARM_DESC(sdw_clock_stop_quirks, "SOF SoundWire clock stop quirks");
  50. static int sdw_params_stream(struct device *dev,
  51. struct sdw_intel_stream_params_data *params_data)
  52. {
  53. struct snd_soc_dai *d = params_data->dai;
  54. struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(d, params_data->substream->stream);
  55. struct snd_sof_dai_config_data data = { 0 };
  56. if (!w) {
  57. dev_err(dev, "%s widget not found, check amp link num in the topology\n",
  58. d->name);
  59. return -EINVAL;
  60. }
  61. data.dai_index = (params_data->link_id << 8) | d->id;
  62. data.dai_data = params_data->alh_stream_id;
  63. data.dai_node_id = data.dai_data;
  64. return hda_dai_config(w, SOF_DAI_CONFIG_FLAGS_HW_PARAMS, &data);
  65. }
  66. static int sdw_params_free(struct device *dev, struct sdw_intel_stream_free_data *free_data)
  67. {
  68. struct snd_soc_dai *d = free_data->dai;
  69. struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(d, free_data->substream->stream);
  70. struct snd_sof_dev *sdev = widget_to_sdev(w);
  71. if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) {
  72. struct snd_sof_widget *swidget = w->dobj.private;
  73. struct snd_sof_dai *dai = swidget->private;
  74. struct sof_ipc4_copier_data *copier_data;
  75. struct sof_ipc4_copier *ipc4_copier;
  76. ipc4_copier = dai->private;
  77. ipc4_copier->dai_index = 0;
  78. copier_data = &ipc4_copier->data;
  79. /* clear the node ID */
  80. copier_data->gtw_cfg.node_id &= ~SOF_IPC4_NODE_INDEX_MASK;
  81. }
  82. return 0;
  83. }
  84. struct sdw_intel_ops sdw_callback = {
  85. .params_stream = sdw_params_stream,
  86. .free_stream = sdw_params_free,
  87. };
  88. static int sdw_ace2x_params_stream(struct device *dev,
  89. struct sdw_intel_stream_params_data *params_data)
  90. {
  91. return sdw_hda_dai_hw_params(params_data->substream,
  92. params_data->hw_params,
  93. params_data->dai,
  94. params_data->link_id,
  95. params_data->alh_stream_id);
  96. }
  97. static int sdw_ace2x_free_stream(struct device *dev,
  98. struct sdw_intel_stream_free_data *free_data)
  99. {
  100. return sdw_hda_dai_hw_free(free_data->substream,
  101. free_data->dai,
  102. free_data->link_id);
  103. }
  104. static int sdw_ace2x_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
  105. {
  106. return sdw_hda_dai_trigger(substream, cmd, dai);
  107. }
  108. static struct sdw_intel_ops sdw_ace2x_callback = {
  109. .params_stream = sdw_ace2x_params_stream,
  110. .free_stream = sdw_ace2x_free_stream,
  111. .trigger = sdw_ace2x_trigger,
  112. };
  113. static int hda_sdw_acpi_scan(struct snd_sof_dev *sdev)
  114. {
  115. u32 interface_mask = hda_get_interface_mask(sdev);
  116. struct sof_intel_hda_dev *hdev;
  117. acpi_handle handle;
  118. int ret;
  119. if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
  120. return -EINVAL;
  121. handle = ACPI_HANDLE(sdev->dev);
  122. /* save ACPI info for the probe step */
  123. hdev = sdev->pdata->hw_pdata;
  124. ret = sdw_intel_acpi_scan(handle, &hdev->info);
  125. if (ret < 0)
  126. return -EINVAL;
  127. return 0;
  128. }
  129. static int hda_sdw_probe(struct snd_sof_dev *sdev)
  130. {
  131. const struct sof_intel_dsp_desc *chip;
  132. struct sof_intel_hda_dev *hdev;
  133. struct sdw_intel_res res;
  134. void *sdw;
  135. hdev = sdev->pdata->hw_pdata;
  136. memset(&res, 0, sizeof(res));
  137. chip = get_chip_info(sdev->pdata);
  138. if (chip->hw_ip_version < SOF_INTEL_ACE_2_0) {
  139. res.mmio_base = sdev->bar[HDA_DSP_BAR];
  140. res.hw_ops = &sdw_intel_cnl_hw_ops;
  141. res.shim_base = hdev->desc->sdw_shim_base;
  142. res.alh_base = hdev->desc->sdw_alh_base;
  143. res.ext = false;
  144. res.ops = &sdw_callback;
  145. } else {
  146. /*
  147. * retrieve eml_lock needed to protect shared registers
  148. * in the HDaudio multi-link areas
  149. */
  150. res.eml_lock = hdac_bus_eml_get_mutex(sof_to_bus(sdev), true,
  151. AZX_REG_ML_LEPTR_ID_SDW);
  152. if (!res.eml_lock)
  153. return -ENODEV;
  154. res.mmio_base = sdev->bar[HDA_DSP_HDA_BAR];
  155. /*
  156. * the SHIM and SoundWire register offsets are link-specific
  157. * and will be determined when adding auxiliary devices
  158. */
  159. res.hw_ops = &sdw_intel_lnl_hw_ops;
  160. res.ext = true;
  161. res.ops = &sdw_ace2x_callback;
  162. }
  163. res.irq = sdev->ipc_irq;
  164. res.handle = hdev->info.handle;
  165. res.parent = sdev->dev;
  166. res.dev = sdev->dev;
  167. res.clock_stop_quirks = sdw_clock_stop_quirks;
  168. res.hbus = sof_to_bus(sdev);
  169. /*
  170. * ops and arg fields are not populated for now,
  171. * they will be needed when the DAI callbacks are
  172. * provided
  173. */
  174. /* we could filter links here if needed, e.g for quirks */
  175. res.count = hdev->info.count;
  176. res.link_mask = hdev->info.link_mask;
  177. sdw = sdw_intel_probe(&res);
  178. if (!sdw) {
  179. dev_err(sdev->dev, "error: SoundWire probe failed\n");
  180. return -EINVAL;
  181. }
  182. /* save context */
  183. hdev->sdw = sdw;
  184. return 0;
  185. }
  186. int hda_sdw_startup(struct snd_sof_dev *sdev)
  187. {
  188. struct sof_intel_hda_dev *hdev;
  189. struct snd_sof_pdata *pdata = sdev->pdata;
  190. int ret;
  191. hdev = sdev->pdata->hw_pdata;
  192. if (!hdev->sdw)
  193. return 0;
  194. if (pdata->machine && !pdata->machine->mach_params.link_mask)
  195. return 0;
  196. ret = hda_sdw_check_lcount(sdev);
  197. if (ret < 0)
  198. return ret;
  199. return sdw_intel_startup(hdev->sdw);
  200. }
  201. EXPORT_SYMBOL_NS(hda_sdw_startup, SND_SOC_SOF_INTEL_HDA_GENERIC);
  202. static int hda_sdw_exit(struct snd_sof_dev *sdev)
  203. {
  204. struct sof_intel_hda_dev *hdev;
  205. hdev = sdev->pdata->hw_pdata;
  206. if (hdev->sdw)
  207. sdw_intel_exit(hdev->sdw);
  208. hdev->sdw = NULL;
  209. hda_sdw_int_enable(sdev, false);
  210. return 0;
  211. }
  212. bool hda_common_check_sdw_irq(struct snd_sof_dev *sdev)
  213. {
  214. struct sof_intel_hda_dev *hdev;
  215. bool ret = false;
  216. u32 irq_status;
  217. hdev = sdev->pdata->hw_pdata;
  218. if (!hdev->sdw)
  219. return ret;
  220. /* store status */
  221. irq_status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS2);
  222. /* invalid message ? */
  223. if (irq_status == 0xffffffff)
  224. goto out;
  225. /* SDW message ? */
  226. if (irq_status & HDA_DSP_REG_ADSPIS2_SNDW)
  227. ret = true;
  228. out:
  229. return ret;
  230. }
  231. EXPORT_SYMBOL_NS(hda_common_check_sdw_irq, SND_SOC_SOF_INTEL_HDA_GENERIC);
  232. static bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev)
  233. {
  234. u32 interface_mask = hda_get_interface_mask(sdev);
  235. const struct sof_intel_dsp_desc *chip;
  236. if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
  237. return false;
  238. chip = get_chip_info(sdev->pdata);
  239. if (chip && chip->check_sdw_irq)
  240. return chip->check_sdw_irq(sdev);
  241. return false;
  242. }
  243. static irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
  244. {
  245. return sdw_intel_thread(irq, context);
  246. }
  247. bool hda_sdw_check_wakeen_irq_common(struct snd_sof_dev *sdev)
  248. {
  249. struct sof_intel_hda_dev *hdev;
  250. hdev = sdev->pdata->hw_pdata;
  251. if (hdev->sdw &&
  252. snd_sof_dsp_read(sdev, HDA_DSP_BAR,
  253. hdev->desc->sdw_shim_base + SDW_SHIM_WAKESTS))
  254. return true;
  255. return false;
  256. }
  257. EXPORT_SYMBOL_NS(hda_sdw_check_wakeen_irq_common, SND_SOC_SOF_INTEL_HDA_GENERIC);
  258. static bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev)
  259. {
  260. u32 interface_mask = hda_get_interface_mask(sdev);
  261. const struct sof_intel_dsp_desc *chip;
  262. if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
  263. return false;
  264. chip = get_chip_info(sdev->pdata);
  265. if (chip && chip->check_sdw_wakeen_irq)
  266. return chip->check_sdw_wakeen_irq(sdev);
  267. return false;
  268. }
  269. void hda_sdw_process_wakeen_common(struct snd_sof_dev *sdev)
  270. {
  271. u32 interface_mask = hda_get_interface_mask(sdev);
  272. struct sof_intel_hda_dev *hdev;
  273. if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
  274. return;
  275. hdev = sdev->pdata->hw_pdata;
  276. if (!hdev->sdw)
  277. return;
  278. sdw_intel_process_wakeen_event(hdev->sdw);
  279. }
  280. EXPORT_SYMBOL_NS(hda_sdw_process_wakeen_common, SND_SOC_SOF_INTEL_HDA_GENERIC);
  281. #else /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) */
  282. static inline int hda_sdw_acpi_scan(struct snd_sof_dev *sdev)
  283. {
  284. return 0;
  285. }
  286. static inline int hda_sdw_probe(struct snd_sof_dev *sdev)
  287. {
  288. return 0;
  289. }
  290. static inline int hda_sdw_exit(struct snd_sof_dev *sdev)
  291. {
  292. return 0;
  293. }
  294. static inline bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev)
  295. {
  296. return false;
  297. }
  298. static inline irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
  299. {
  300. return IRQ_HANDLED;
  301. }
  302. static inline bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev)
  303. {
  304. return false;
  305. }
  306. #endif /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) */
  307. /* pre fw run operations */
  308. int hda_dsp_pre_fw_run(struct snd_sof_dev *sdev)
  309. {
  310. /* disable clock gating and power gating */
  311. return hda_dsp_ctrl_clock_power_gating(sdev, false);
  312. }
  313. /* post fw run operations */
  314. int hda_dsp_post_fw_run(struct snd_sof_dev *sdev)
  315. {
  316. int ret;
  317. if (sdev->first_boot) {
  318. struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
  319. ret = hda_sdw_startup(sdev);
  320. if (ret < 0) {
  321. dev_err(sdev->dev,
  322. "error: could not startup SoundWire links\n");
  323. return ret;
  324. }
  325. /* Check if IMR boot is usable */
  326. if (!sof_debug_check_flag(SOF_DBG_IGNORE_D3_PERSISTENT) &&
  327. (sdev->fw_ready.flags & SOF_IPC_INFO_D3_PERSISTENT ||
  328. sdev->pdata->ipc_type == SOF_IPC_TYPE_4)) {
  329. hdev->imrboot_supported = true;
  330. debugfs_create_bool("skip_imr_boot",
  331. 0644, sdev->debugfs_root,
  332. &hdev->skip_imr_boot);
  333. }
  334. }
  335. hda_sdw_int_enable(sdev, true);
  336. /* re-enable clock gating and power gating */
  337. return hda_dsp_ctrl_clock_power_gating(sdev, true);
  338. }
  339. EXPORT_SYMBOL_NS(hda_dsp_post_fw_run, SND_SOC_SOF_INTEL_HDA_GENERIC);
  340. /*
  341. * Debug
  342. */
  343. #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG)
  344. static bool hda_use_msi = true;
  345. module_param_named(use_msi, hda_use_msi, bool, 0444);
  346. MODULE_PARM_DESC(use_msi, "SOF HDA use PCI MSI mode");
  347. #else
  348. #define hda_use_msi (1)
  349. #endif
  350. static char *hda_model;
  351. module_param(hda_model, charp, 0444);
  352. MODULE_PARM_DESC(hda_model, "Use the given HDA board model.");
  353. static int dmic_num_override = -1;
  354. module_param_named(dmic_num, dmic_num_override, int, 0444);
  355. MODULE_PARM_DESC(dmic_num, "SOF HDA DMIC number");
  356. static int mclk_id_override = -1;
  357. module_param_named(mclk_id, mclk_id_override, int, 0444);
  358. MODULE_PARM_DESC(mclk_id, "SOF SSP mclk_id");
  359. static int bt_link_mask_override;
  360. module_param_named(bt_link_mask, bt_link_mask_override, int, 0444);
  361. MODULE_PARM_DESC(bt_link_mask, "SOF BT offload link mask");
  362. static int hda_init(struct snd_sof_dev *sdev)
  363. {
  364. struct hda_bus *hbus;
  365. struct hdac_bus *bus;
  366. struct pci_dev *pci = to_pci_dev(sdev->dev);
  367. int ret;
  368. hbus = sof_to_hbus(sdev);
  369. bus = sof_to_bus(sdev);
  370. /* HDA bus init */
  371. sof_hda_bus_init(sdev, &pci->dev);
  372. if (sof_hda_position_quirk == SOF_HDA_POSITION_QUIRK_USE_DPIB_REGISTERS)
  373. bus->use_posbuf = 0;
  374. else
  375. bus->use_posbuf = 1;
  376. bus->bdl_pos_adj = 0;
  377. bus->sync_write = 1;
  378. mutex_init(&hbus->prepare_mutex);
  379. hbus->pci = pci;
  380. hbus->mixer_assigned = -1;
  381. hbus->modelname = hda_model;
  382. /* initialise hdac bus */
  383. bus->addr = pci_resource_start(pci, 0);
  384. bus->remap_addr = pci_ioremap_bar(pci, 0);
  385. if (!bus->remap_addr) {
  386. dev_err(bus->dev, "error: ioremap error\n");
  387. return -ENXIO;
  388. }
  389. /* HDA base */
  390. sdev->bar[HDA_DSP_HDA_BAR] = bus->remap_addr;
  391. /* init i915 and HDMI codecs */
  392. ret = hda_codec_i915_init(sdev);
  393. if (ret < 0 && ret != -ENODEV) {
  394. dev_err_probe(sdev->dev, ret, "init of i915 and HDMI codec failed\n");
  395. goto out;
  396. }
  397. /* get controller capabilities */
  398. ret = hda_dsp_ctrl_get_caps(sdev);
  399. if (ret < 0) {
  400. dev_err(sdev->dev, "error: get caps error\n");
  401. hda_codec_i915_exit(sdev);
  402. }
  403. out:
  404. if (ret < 0)
  405. iounmap(sof_to_bus(sdev)->remap_addr);
  406. return ret;
  407. }
  408. static int check_dmic_num(struct snd_sof_dev *sdev)
  409. {
  410. struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
  411. struct nhlt_acpi_table *nhlt;
  412. int dmic_num = 0;
  413. nhlt = hdev->nhlt;
  414. if (nhlt)
  415. dmic_num = intel_nhlt_get_dmic_geo(sdev->dev, nhlt);
  416. dev_info(sdev->dev, "DMICs detected in NHLT tables: %d\n", dmic_num);
  417. /* allow for module parameter override */
  418. if (dmic_num_override != -1) {
  419. dev_dbg(sdev->dev,
  420. "overriding DMICs detected in NHLT tables %d by kernel param %d\n",
  421. dmic_num, dmic_num_override);
  422. dmic_num = dmic_num_override;
  423. }
  424. if (dmic_num < 0 || dmic_num > 4) {
  425. dev_dbg(sdev->dev, "invalid dmic_number %d\n", dmic_num);
  426. dmic_num = 0;
  427. }
  428. return dmic_num;
  429. }
  430. static int check_nhlt_ssp_mask(struct snd_sof_dev *sdev, u8 device_type)
  431. {
  432. struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
  433. struct nhlt_acpi_table *nhlt;
  434. int ssp_mask = 0;
  435. nhlt = hdev->nhlt;
  436. if (!nhlt)
  437. return ssp_mask;
  438. if (intel_nhlt_has_endpoint_type(nhlt, NHLT_LINK_SSP)) {
  439. ssp_mask = intel_nhlt_ssp_endpoint_mask(nhlt, device_type);
  440. if (ssp_mask)
  441. dev_info(sdev->dev, "NHLT device %s(%d) detected, ssp_mask %#x\n",
  442. device_type == NHLT_DEVICE_BT ? "BT" : "I2S",
  443. device_type, ssp_mask);
  444. }
  445. return ssp_mask;
  446. }
  447. static int check_nhlt_ssp_mclk_mask(struct snd_sof_dev *sdev, int ssp_num)
  448. {
  449. struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
  450. struct nhlt_acpi_table *nhlt;
  451. nhlt = hdev->nhlt;
  452. if (!nhlt)
  453. return 0;
  454. return intel_nhlt_ssp_mclk_mask(nhlt, ssp_num);
  455. }
  456. static int hda_init_caps(struct snd_sof_dev *sdev)
  457. {
  458. u32 interface_mask = hda_get_interface_mask(sdev);
  459. struct hdac_bus *bus = sof_to_bus(sdev);
  460. struct snd_sof_pdata *pdata = sdev->pdata;
  461. struct sof_intel_hda_dev *hdev = pdata->hw_pdata;
  462. u32 link_mask;
  463. int ret = 0;
  464. /* check if dsp is there */
  465. if (bus->ppcap)
  466. dev_dbg(sdev->dev, "PP capability, will probe DSP later.\n");
  467. /* Init HDA controller after i915 init */
  468. ret = hda_dsp_ctrl_init_chip(sdev);
  469. if (ret < 0) {
  470. dev_err(bus->dev, "error: init chip failed with ret: %d\n",
  471. ret);
  472. return ret;
  473. }
  474. hda_bus_ml_init(bus);
  475. /* Skip SoundWire if it is not supported */
  476. if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
  477. goto skip_soundwire;
  478. /* scan SoundWire capabilities exposed by DSDT */
  479. ret = hda_sdw_acpi_scan(sdev);
  480. if (ret < 0) {
  481. dev_dbg(sdev->dev, "skipping SoundWire, not detected with ACPI scan\n");
  482. goto skip_soundwire;
  483. }
  484. link_mask = hdev->info.link_mask;
  485. if (!link_mask) {
  486. dev_dbg(sdev->dev, "skipping SoundWire, no links enabled\n");
  487. goto skip_soundwire;
  488. }
  489. /*
  490. * probe/allocate SoundWire resources.
  491. * The hardware configuration takes place in hda_sdw_startup
  492. * after power rails are enabled.
  493. * It's entirely possible to have a mix of I2S/DMIC/SoundWire
  494. * devices, so we allocate the resources in all cases.
  495. */
  496. ret = hda_sdw_probe(sdev);
  497. if (ret < 0) {
  498. dev_err(sdev->dev, "error: SoundWire probe error\n");
  499. return ret;
  500. }
  501. skip_soundwire:
  502. /* create codec instances */
  503. hda_codec_probe_bus(sdev);
  504. if (!HDA_IDISP_CODEC(bus->codec_mask))
  505. hda_codec_i915_display_power(sdev, false);
  506. hda_bus_ml_put_all(bus);
  507. return 0;
  508. }
  509. static irqreturn_t hda_dsp_interrupt_handler(int irq, void *context)
  510. {
  511. struct snd_sof_dev *sdev = context;
  512. /*
  513. * Get global interrupt status. It includes all hardware interrupt
  514. * sources in the Intel HD Audio controller.
  515. */
  516. if (snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS) &
  517. SOF_HDA_INTSTS_GIS) {
  518. /* disable GIE interrupt */
  519. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
  520. SOF_HDA_INTCTL,
  521. SOF_HDA_INT_GLOBAL_EN,
  522. 0);
  523. return IRQ_WAKE_THREAD;
  524. }
  525. return IRQ_NONE;
  526. }
  527. static irqreturn_t hda_dsp_interrupt_thread(int irq, void *context)
  528. {
  529. struct snd_sof_dev *sdev = context;
  530. struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
  531. /* deal with streams and controller first */
  532. if (hda_dsp_check_stream_irq(sdev)) {
  533. trace_sof_intel_hda_irq(sdev, "stream");
  534. hda_dsp_stream_threaded_handler(irq, sdev);
  535. }
  536. if (hda_check_ipc_irq(sdev)) {
  537. trace_sof_intel_hda_irq(sdev, "ipc");
  538. sof_ops(sdev)->irq_thread(irq, sdev);
  539. }
  540. if (hda_dsp_check_sdw_irq(sdev)) {
  541. trace_sof_intel_hda_irq(sdev, "sdw");
  542. hda_dsp_sdw_thread(irq, hdev->sdw);
  543. }
  544. if (hda_sdw_check_wakeen_irq(sdev)) {
  545. trace_sof_intel_hda_irq(sdev, "wakeen");
  546. hda_sdw_process_wakeen(sdev);
  547. }
  548. hda_codec_check_for_state_change(sdev);
  549. /* enable GIE interrupt */
  550. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
  551. SOF_HDA_INTCTL,
  552. SOF_HDA_INT_GLOBAL_EN,
  553. SOF_HDA_INT_GLOBAL_EN);
  554. return IRQ_HANDLED;
  555. }
  556. int hda_dsp_probe_early(struct snd_sof_dev *sdev)
  557. {
  558. struct pci_dev *pci = to_pci_dev(sdev->dev);
  559. struct sof_intel_hda_dev *hdev;
  560. const struct sof_intel_dsp_desc *chip;
  561. int ret = 0;
  562. if (!sdev->dspless_mode_selected) {
  563. /*
  564. * detect DSP by checking class/subclass/prog-id information
  565. * class=04 subclass 03 prog-if 00: no DSP, legacy driver is required
  566. * class=04 subclass 01 prog-if 00: DSP is present
  567. * (and may be required e.g. for DMIC or SSP support)
  568. * class=04 subclass 03 prog-if 80: either of DSP or legacy mode works
  569. */
  570. if (pci->class == 0x040300) {
  571. dev_err(sdev->dev, "the DSP is not enabled on this platform, aborting probe\n");
  572. return -ENODEV;
  573. } else if (pci->class != 0x040100 && pci->class != 0x040380) {
  574. dev_err(sdev->dev, "unknown PCI class/subclass/prog-if 0x%06x found, aborting probe\n",
  575. pci->class);
  576. return -ENODEV;
  577. }
  578. dev_info_once(sdev->dev, "DSP detected with PCI class/subclass/prog-if 0x%06x\n",
  579. pci->class);
  580. }
  581. chip = get_chip_info(sdev->pdata);
  582. if (!chip) {
  583. dev_err(sdev->dev, "error: no such device supported, chip id:%x\n",
  584. pci->device);
  585. ret = -EIO;
  586. goto err;
  587. }
  588. sdev->num_cores = chip->cores_num;
  589. hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
  590. if (!hdev)
  591. return -ENOMEM;
  592. sdev->pdata->hw_pdata = hdev;
  593. hdev->desc = chip;
  594. ret = hda_init(sdev);
  595. err:
  596. return ret;
  597. }
  598. EXPORT_SYMBOL_NS(hda_dsp_probe_early, SND_SOC_SOF_INTEL_HDA_GENERIC);
  599. int hda_dsp_probe(struct snd_sof_dev *sdev)
  600. {
  601. struct pci_dev *pci = to_pci_dev(sdev->dev);
  602. struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
  603. const struct sof_intel_dsp_desc *chip;
  604. int ret = 0;
  605. hdev->dmic_dev = platform_device_register_data(sdev->dev, "dmic-codec",
  606. PLATFORM_DEVID_NONE,
  607. NULL, 0);
  608. if (IS_ERR(hdev->dmic_dev)) {
  609. dev_err(sdev->dev, "error: failed to create DMIC device\n");
  610. return PTR_ERR(hdev->dmic_dev);
  611. }
  612. /*
  613. * use position update IPC if either it is forced
  614. * or we don't have other choice
  615. */
  616. #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_FORCE_IPC_POSITION)
  617. hdev->no_ipc_position = 0;
  618. #else
  619. hdev->no_ipc_position = sof_ops(sdev)->pcm_pointer ? 1 : 0;
  620. #endif
  621. if (sdev->dspless_mode_selected)
  622. hdev->no_ipc_position = 1;
  623. if (sdev->dspless_mode_selected)
  624. goto skip_dsp_setup;
  625. /* DSP base */
  626. sdev->bar[HDA_DSP_BAR] = pci_ioremap_bar(pci, HDA_DSP_BAR);
  627. if (!sdev->bar[HDA_DSP_BAR]) {
  628. dev_err(sdev->dev, "error: ioremap error\n");
  629. ret = -ENXIO;
  630. goto hdac_bus_unmap;
  631. }
  632. sdev->mmio_bar = HDA_DSP_BAR;
  633. sdev->mailbox_bar = HDA_DSP_BAR;
  634. skip_dsp_setup:
  635. /* allow 64bit DMA address if supported by H/W */
  636. if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(64))) {
  637. dev_dbg(sdev->dev, "DMA mask is 32 bit\n");
  638. dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32));
  639. }
  640. dma_set_max_seg_size(&pci->dev, UINT_MAX);
  641. /* init streams */
  642. ret = hda_dsp_stream_init(sdev);
  643. if (ret < 0) {
  644. dev_err(sdev->dev, "error: failed to init streams\n");
  645. /*
  646. * not all errors are due to memory issues, but trying
  647. * to free everything does not harm
  648. */
  649. goto free_streams;
  650. }
  651. /*
  652. * register our IRQ
  653. * let's try to enable msi firstly
  654. * if it fails, use legacy interrupt mode
  655. * TODO: support msi multiple vectors
  656. */
  657. if (hda_use_msi && pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI) > 0) {
  658. dev_info(sdev->dev, "use msi interrupt mode\n");
  659. sdev->ipc_irq = pci_irq_vector(pci, 0);
  660. /* initialised to "false" by kzalloc() */
  661. sdev->msi_enabled = true;
  662. }
  663. if (!sdev->msi_enabled) {
  664. dev_info(sdev->dev, "use legacy interrupt mode\n");
  665. /*
  666. * in IO-APIC mode, hda->irq and ipc_irq are using the same
  667. * irq number of pci->irq
  668. */
  669. sdev->ipc_irq = pci->irq;
  670. }
  671. dev_dbg(sdev->dev, "using IPC IRQ %d\n", sdev->ipc_irq);
  672. ret = request_threaded_irq(sdev->ipc_irq, hda_dsp_interrupt_handler,
  673. hda_dsp_interrupt_thread,
  674. IRQF_SHARED, "AudioDSP", sdev);
  675. if (ret < 0) {
  676. dev_err(sdev->dev, "error: failed to register IPC IRQ %d\n",
  677. sdev->ipc_irq);
  678. goto free_irq_vector;
  679. }
  680. pci_set_master(pci);
  681. synchronize_irq(pci->irq);
  682. /*
  683. * clear TCSEL to clear playback on some HD Audio
  684. * codecs. PCI TCSEL is defined in the Intel manuals.
  685. */
  686. snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
  687. /* init HDA capabilities */
  688. ret = hda_init_caps(sdev);
  689. if (ret < 0)
  690. goto free_ipc_irq;
  691. if (!sdev->dspless_mode_selected) {
  692. /* enable ppcap interrupt */
  693. hda_dsp_ctrl_ppcap_enable(sdev, true);
  694. hda_dsp_ctrl_ppcap_int_enable(sdev, true);
  695. /* set default mailbox offset for FW ready message */
  696. sdev->dsp_box.offset = HDA_DSP_MBOX_UPLINK_OFFSET;
  697. INIT_DELAYED_WORK(&hdev->d0i3_work, hda_dsp_d0i3_work);
  698. }
  699. chip = get_chip_info(sdev->pdata);
  700. if (chip && chip->hw_ip_version >= SOF_INTEL_ACE_2_0) {
  701. ret = hda_sdw_startup(sdev);
  702. if (ret < 0) {
  703. dev_err(sdev->dev, "could not startup SoundWire links\n");
  704. goto disable_pp_cap;
  705. }
  706. hda_sdw_int_enable(sdev, true);
  707. }
  708. init_waitqueue_head(&hdev->waitq);
  709. hdev->nhlt = intel_nhlt_init(sdev->dev);
  710. return 0;
  711. disable_pp_cap:
  712. if (!sdev->dspless_mode_selected) {
  713. hda_dsp_ctrl_ppcap_int_enable(sdev, false);
  714. hda_dsp_ctrl_ppcap_enable(sdev, false);
  715. }
  716. free_ipc_irq:
  717. free_irq(sdev->ipc_irq, sdev);
  718. free_irq_vector:
  719. if (sdev->msi_enabled)
  720. pci_free_irq_vectors(pci);
  721. free_streams:
  722. hda_dsp_stream_free(sdev);
  723. /* dsp_unmap: not currently used */
  724. if (!sdev->dspless_mode_selected)
  725. iounmap(sdev->bar[HDA_DSP_BAR]);
  726. hdac_bus_unmap:
  727. platform_device_unregister(hdev->dmic_dev);
  728. return ret;
  729. }
  730. EXPORT_SYMBOL_NS(hda_dsp_probe, SND_SOC_SOF_INTEL_HDA_GENERIC);
  731. void hda_dsp_remove(struct snd_sof_dev *sdev)
  732. {
  733. struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
  734. const struct sof_intel_dsp_desc *chip = hda->desc;
  735. struct pci_dev *pci = to_pci_dev(sdev->dev);
  736. struct nhlt_acpi_table *nhlt = hda->nhlt;
  737. if (nhlt)
  738. intel_nhlt_free(nhlt);
  739. if (!sdev->dspless_mode_selected)
  740. /* cancel any attempt for DSP D0I3 */
  741. cancel_delayed_work_sync(&hda->d0i3_work);
  742. hda_codec_device_remove(sdev);
  743. hda_sdw_exit(sdev);
  744. if (!IS_ERR_OR_NULL(hda->dmic_dev))
  745. platform_device_unregister(hda->dmic_dev);
  746. if (!sdev->dspless_mode_selected) {
  747. /* disable DSP IRQ */
  748. hda_dsp_ctrl_ppcap_int_enable(sdev, false);
  749. }
  750. /* disable CIE and GIE interrupts */
  751. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
  752. SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN, 0);
  753. if (sdev->dspless_mode_selected)
  754. goto skip_disable_dsp;
  755. /* no need to check for error as the DSP will be disabled anyway */
  756. if (chip && chip->power_down_dsp)
  757. chip->power_down_dsp(sdev);
  758. /* disable DSP */
  759. hda_dsp_ctrl_ppcap_enable(sdev, false);
  760. skip_disable_dsp:
  761. free_irq(sdev->ipc_irq, sdev);
  762. if (sdev->msi_enabled)
  763. pci_free_irq_vectors(pci);
  764. hda_dsp_stream_free(sdev);
  765. hda_bus_ml_free(sof_to_bus(sdev));
  766. if (!sdev->dspless_mode_selected)
  767. iounmap(sdev->bar[HDA_DSP_BAR]);
  768. }
  769. EXPORT_SYMBOL_NS(hda_dsp_remove, SND_SOC_SOF_INTEL_HDA_GENERIC);
  770. void hda_dsp_remove_late(struct snd_sof_dev *sdev)
  771. {
  772. iounmap(sof_to_bus(sdev)->remap_addr);
  773. sof_hda_bus_exit(sdev);
  774. hda_codec_i915_exit(sdev);
  775. }
  776. int hda_power_down_dsp(struct snd_sof_dev *sdev)
  777. {
  778. struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
  779. const struct sof_intel_dsp_desc *chip = hda->desc;
  780. return hda_dsp_core_reset_power_down(sdev, chip->host_managed_cores_mask);
  781. }
  782. EXPORT_SYMBOL_NS(hda_power_down_dsp, SND_SOC_SOF_INTEL_HDA_GENERIC);
  783. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
  784. static void hda_generic_machine_select(struct snd_sof_dev *sdev,
  785. struct snd_soc_acpi_mach **mach)
  786. {
  787. struct hdac_bus *bus = sof_to_bus(sdev);
  788. struct snd_soc_acpi_mach_params *mach_params;
  789. struct snd_soc_acpi_mach *hda_mach;
  790. struct snd_sof_pdata *pdata = sdev->pdata;
  791. const char *tplg_filename;
  792. int codec_num = 0;
  793. int i;
  794. /* codec detection */
  795. if (!bus->codec_mask) {
  796. dev_info(bus->dev, "no hda codecs found!\n");
  797. } else {
  798. dev_info(bus->dev, "hda codecs found, mask %lx\n",
  799. bus->codec_mask);
  800. for (i = 0; i < HDA_MAX_CODECS; i++) {
  801. if (bus->codec_mask & (1 << i))
  802. codec_num++;
  803. }
  804. /*
  805. * If no machine driver is found, then:
  806. *
  807. * generic hda machine driver can handle:
  808. * - one HDMI codec, and/or
  809. * - one external HDAudio codec
  810. */
  811. if (!*mach && codec_num <= 2) {
  812. bool tplg_fixup = false;
  813. /*
  814. * make a local copy of the match array since we might
  815. * be modifying it
  816. */
  817. hda_mach = devm_kmemdup_array(sdev->dev,
  818. snd_soc_acpi_intel_hda_machines,
  819. 2, /* we have one entry + sentinel in the array */
  820. sizeof(snd_soc_acpi_intel_hda_machines[0]),
  821. GFP_KERNEL);
  822. if (!hda_mach) {
  823. dev_err(bus->dev,
  824. "%s: failed to duplicate the HDA match table\n",
  825. __func__);
  826. return;
  827. }
  828. dev_info(bus->dev, "using HDA machine driver %s now\n",
  829. hda_mach->drv_name);
  830. /*
  831. * topology: use the info from hda_machines since tplg file name
  832. * is not overwritten
  833. */
  834. if (!pdata->tplg_filename)
  835. tplg_fixup = true;
  836. if (tplg_fixup &&
  837. codec_num == 1 && HDA_IDISP_CODEC(bus->codec_mask)) {
  838. tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
  839. "%s-idisp",
  840. hda_mach->sof_tplg_filename);
  841. if (!tplg_filename)
  842. return;
  843. hda_mach->sof_tplg_filename = tplg_filename;
  844. }
  845. if (codec_num == 2 ||
  846. (codec_num == 1 && !HDA_IDISP_CODEC(bus->codec_mask))) {
  847. /*
  848. * Prevent SoundWire links from starting when an external
  849. * HDaudio codec is used
  850. */
  851. hda_mach->mach_params.link_mask = 0;
  852. } else {
  853. /*
  854. * Allow SoundWire links to start when no external HDaudio codec
  855. * was detected. This will not create a SoundWire card but
  856. * will help detect if any SoundWire codec reports as ATTACHED.
  857. */
  858. struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
  859. hda_mach->mach_params.link_mask = hdev->info.link_mask;
  860. }
  861. *mach = hda_mach;
  862. }
  863. }
  864. /* used by hda machine driver to create dai links */
  865. if (*mach) {
  866. mach_params = &(*mach)->mach_params;
  867. mach_params->codec_mask = bus->codec_mask;
  868. }
  869. }
  870. #else
  871. static void hda_generic_machine_select(struct snd_sof_dev *sdev,
  872. struct snd_soc_acpi_mach **mach)
  873. {
  874. }
  875. #endif
  876. #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
  877. static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
  878. {
  879. struct snd_sof_pdata *pdata = sdev->pdata;
  880. const struct snd_soc_acpi_link_adr *link;
  881. struct sdw_extended_slave_id *ids;
  882. struct snd_soc_acpi_mach *mach;
  883. struct sof_intel_hda_dev *hdev;
  884. u32 link_mask;
  885. int i;
  886. hdev = pdata->hw_pdata;
  887. link_mask = hdev->info.link_mask;
  888. if (!link_mask) {
  889. dev_info(sdev->dev, "SoundWire links not enabled\n");
  890. return NULL;
  891. }
  892. if (!hdev->sdw) {
  893. dev_dbg(sdev->dev, "SoundWire context not allocated\n");
  894. return NULL;
  895. }
  896. if (!hdev->sdw->num_slaves) {
  897. dev_warn(sdev->dev, "No SoundWire peripheral detected in ACPI tables\n");
  898. return NULL;
  899. }
  900. /*
  901. * Select SoundWire machine driver if needed using the
  902. * alternate tables. This case deals with SoundWire-only
  903. * machines, for mixed cases with I2C/I2S the detection relies
  904. * on the HID list.
  905. */
  906. for (mach = pdata->desc->alt_machines;
  907. mach && mach->link_mask; mach++) {
  908. /*
  909. * On some platforms such as Up Extreme all links
  910. * are enabled but only one link can be used by
  911. * external codec. Instead of exact match of two masks,
  912. * first check whether link_mask of mach is subset of
  913. * link_mask supported by hw and then go on searching
  914. * link_adr
  915. */
  916. if (~link_mask & mach->link_mask)
  917. continue;
  918. /* No need to match adr if there is no links defined */
  919. if (!mach->links)
  920. break;
  921. link = mach->links;
  922. for (i = 0; i < hdev->info.count && link->num_adr;
  923. i++, link++) {
  924. /*
  925. * Try next machine if any expected Slaves
  926. * are not found on this link.
  927. */
  928. if (!snd_soc_acpi_sdw_link_slaves_found(sdev->dev, link,
  929. hdev->sdw->ids,
  930. hdev->sdw->num_slaves))
  931. break;
  932. }
  933. /* Found if all Slaves are checked */
  934. if (i == hdev->info.count || !link->num_adr)
  935. break;
  936. }
  937. if (mach && mach->link_mask) {
  938. mach->mach_params.links = mach->links;
  939. mach->mach_params.link_mask = mach->link_mask;
  940. mach->mach_params.platform = dev_name(sdev->dev);
  941. return mach;
  942. }
  943. dev_info(sdev->dev, "No SoundWire machine driver found for the ACPI-reported configuration:\n");
  944. ids = hdev->sdw->ids;
  945. for (i = 0; i < hdev->sdw->num_slaves; i++)
  946. dev_info(sdev->dev, "link %d mfg_id 0x%04x part_id 0x%04x version %#x\n",
  947. ids[i].link_id, ids[i].id.mfg_id, ids[i].id.part_id, ids[i].id.sdw_version);
  948. return NULL;
  949. }
  950. #else
  951. static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
  952. {
  953. return NULL;
  954. }
  955. #endif
  956. void hda_set_mach_params(struct snd_soc_acpi_mach *mach,
  957. struct snd_sof_dev *sdev)
  958. {
  959. struct snd_sof_pdata *pdata = sdev->pdata;
  960. const struct sof_dev_desc *desc = pdata->desc;
  961. struct snd_soc_acpi_mach_params *mach_params;
  962. mach_params = &mach->mach_params;
  963. mach_params->platform = dev_name(sdev->dev);
  964. if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
  965. sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
  966. mach_params->num_dai_drivers = SOF_SKL_NUM_DAIS_NOCODEC;
  967. else
  968. mach_params->num_dai_drivers = desc->ops->num_drv;
  969. mach_params->dai_drivers = desc->ops->drv;
  970. }
  971. static int check_tplg_quirk_mask(struct snd_soc_acpi_mach *mach)
  972. {
  973. u32 dmic_ssp_quirk;
  974. u32 codec_amp_name_quirk;
  975. /*
  976. * In current implementation dmic and ssp quirks are designed for es8336
  977. * machine driver and could not be mixed with codec name and amp name
  978. * quirks.
  979. */
  980. dmic_ssp_quirk = mach->tplg_quirk_mask &
  981. (SND_SOC_ACPI_TPLG_INTEL_DMIC_NUMBER | SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER);
  982. codec_amp_name_quirk = mach->tplg_quirk_mask &
  983. (SND_SOC_ACPI_TPLG_INTEL_AMP_NAME | SND_SOC_ACPI_TPLG_INTEL_CODEC_NAME);
  984. if (dmic_ssp_quirk && codec_amp_name_quirk)
  985. return -EINVAL;
  986. return 0;
  987. }
  988. static char *remove_file_ext(struct device *dev, const char *tplg_filename)
  989. {
  990. char *filename, *tmp;
  991. filename = devm_kstrdup(dev, tplg_filename, GFP_KERNEL);
  992. if (!filename)
  993. return NULL;
  994. /* remove file extension if exist */
  995. tmp = filename;
  996. return strsep(&tmp, ".");
  997. }
  998. struct snd_soc_acpi_mach *hda_machine_select(struct snd_sof_dev *sdev)
  999. {
  1000. u32 interface_mask = hda_get_interface_mask(sdev);
  1001. struct snd_sof_pdata *sof_pdata = sdev->pdata;
  1002. const struct sof_dev_desc *desc = sof_pdata->desc;
  1003. struct hdac_bus *bus = sof_to_bus(sdev);
  1004. struct snd_soc_acpi_mach *mach = NULL;
  1005. enum snd_soc_acpi_intel_codec codec_type, amp_type;
  1006. const char *tplg_filename;
  1007. const char *tplg_suffix;
  1008. bool amp_name_valid;
  1009. bool i2s_mach_found = false;
  1010. bool sdw_mach_found = false;
  1011. /* Try I2S or DMIC if it is supported */
  1012. if (interface_mask & (BIT(SOF_DAI_INTEL_SSP) | BIT(SOF_DAI_INTEL_DMIC))) {
  1013. mach = snd_soc_acpi_find_machine(desc->machines);
  1014. if (mach)
  1015. i2s_mach_found = true;
  1016. }
  1017. /*
  1018. * If I2S fails and no external HDaudio codec is detected,
  1019. * try SoundWire if it is supported
  1020. */
  1021. if (!mach && !HDA_EXT_CODEC(bus->codec_mask) &&
  1022. (interface_mask & BIT(SOF_DAI_INTEL_ALH))) {
  1023. mach = hda_sdw_machine_select(sdev);
  1024. if (mach)
  1025. sdw_mach_found = true;
  1026. }
  1027. /*
  1028. * Choose HDA generic machine driver if mach is NULL.
  1029. * Otherwise, set certain mach params.
  1030. */
  1031. hda_generic_machine_select(sdev, &mach);
  1032. if (!mach) {
  1033. dev_warn(sdev->dev, "warning: No matching ASoC machine driver found\n");
  1034. return NULL;
  1035. }
  1036. /* report BT offload link mask to machine driver */
  1037. mach->mach_params.bt_link_mask = check_nhlt_ssp_mask(sdev, NHLT_DEVICE_BT);
  1038. dev_info(sdev->dev, "BT link detected in NHLT tables: %#x\n",
  1039. mach->mach_params.bt_link_mask);
  1040. /* allow for module parameter override */
  1041. if (bt_link_mask_override) {
  1042. dev_dbg(sdev->dev, "overriding BT link detected in NHLT tables %#x by kernel param %#x\n",
  1043. mach->mach_params.bt_link_mask, bt_link_mask_override);
  1044. mach->mach_params.bt_link_mask = bt_link_mask_override;
  1045. }
  1046. if (hweight_long(mach->mach_params.bt_link_mask) > 1) {
  1047. dev_warn(sdev->dev, "invalid BT link mask %#x found, reset the mask\n",
  1048. mach->mach_params.bt_link_mask);
  1049. mach->mach_params.bt_link_mask = 0;
  1050. }
  1051. /*
  1052. * Fixup tplg file name by appending dmic num, ssp num, codec/amplifier
  1053. * name string if quirk flag is set.
  1054. */
  1055. if (mach) {
  1056. bool tplg_fixup = false;
  1057. bool dmic_fixup = false;
  1058. /*
  1059. * If tplg file name is overridden, use it instead of
  1060. * the one set in mach table
  1061. */
  1062. if (!sof_pdata->tplg_filename) {
  1063. /* remove file extension if it exists */
  1064. tplg_filename = remove_file_ext(sdev->dev, mach->sof_tplg_filename);
  1065. if (!tplg_filename)
  1066. return NULL;
  1067. sof_pdata->tplg_filename = tplg_filename;
  1068. tplg_fixup = true;
  1069. }
  1070. /*
  1071. * Checking quirk mask integrity; some quirk flags could not be
  1072. * set concurrently.
  1073. */
  1074. if (tplg_fixup &&
  1075. check_tplg_quirk_mask(mach)) {
  1076. dev_err(sdev->dev, "Invalid tplg quirk mask 0x%x\n",
  1077. mach->tplg_quirk_mask);
  1078. return NULL;
  1079. }
  1080. /* report to machine driver if any DMICs are found */
  1081. mach->mach_params.dmic_num = check_dmic_num(sdev);
  1082. if (sdw_mach_found || mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_DMIC_NUMBER)
  1083. dmic_fixup = true;
  1084. if (tplg_fixup &&
  1085. dmic_fixup &&
  1086. mach->mach_params.dmic_num) {
  1087. tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
  1088. "%s%s%d%s",
  1089. sof_pdata->tplg_filename,
  1090. i2s_mach_found ? "-dmic" : "-",
  1091. mach->mach_params.dmic_num,
  1092. "ch");
  1093. if (!tplg_filename)
  1094. return NULL;
  1095. sof_pdata->tplg_filename = tplg_filename;
  1096. }
  1097. if (mach->link_mask) {
  1098. mach->mach_params.links = mach->links;
  1099. mach->mach_params.link_mask = mach->link_mask;
  1100. }
  1101. /* report SSP link mask to machine driver */
  1102. mach->mach_params.i2s_link_mask = check_nhlt_ssp_mask(sdev, NHLT_DEVICE_I2S);
  1103. if (tplg_fixup &&
  1104. mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER &&
  1105. mach->mach_params.i2s_link_mask) {
  1106. const struct sof_intel_dsp_desc *chip = get_chip_info(sdev->pdata);
  1107. int ssp_num;
  1108. int mclk_mask;
  1109. if (hweight_long(mach->mach_params.i2s_link_mask) > 1 &&
  1110. !(mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_MSB))
  1111. dev_warn(sdev->dev, "More than one SSP exposed by NHLT, choosing MSB\n");
  1112. /* fls returns 1-based results, SSPs indices are 0-based */
  1113. ssp_num = fls(mach->mach_params.i2s_link_mask) - 1;
  1114. if (ssp_num >= chip->ssp_count) {
  1115. dev_err(sdev->dev, "Invalid SSP %d, max on this platform is %d\n",
  1116. ssp_num, chip->ssp_count);
  1117. return NULL;
  1118. }
  1119. tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
  1120. "%s%s%d",
  1121. sof_pdata->tplg_filename,
  1122. "-ssp",
  1123. ssp_num);
  1124. if (!tplg_filename)
  1125. return NULL;
  1126. sof_pdata->tplg_filename = tplg_filename;
  1127. mclk_mask = check_nhlt_ssp_mclk_mask(sdev, ssp_num);
  1128. if (mclk_mask < 0) {
  1129. dev_err(sdev->dev, "Invalid MCLK configuration\n");
  1130. return NULL;
  1131. }
  1132. dev_dbg(sdev->dev, "MCLK mask %#x found in NHLT\n", mclk_mask);
  1133. if (mclk_mask) {
  1134. dev_info(sdev->dev, "Overriding topology with MCLK mask %#x from NHLT\n", mclk_mask);
  1135. sdev->mclk_id_override = true;
  1136. sdev->mclk_id_quirk = (mclk_mask & BIT(0)) ? 0 : 1;
  1137. }
  1138. }
  1139. amp_type = snd_soc_acpi_intel_detect_amp_type(sdev->dev);
  1140. codec_type = snd_soc_acpi_intel_detect_codec_type(sdev->dev);
  1141. amp_name_valid = amp_type != CODEC_NONE && amp_type != codec_type;
  1142. if (tplg_fixup && amp_name_valid &&
  1143. mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_AMP_NAME) {
  1144. tplg_suffix = snd_soc_acpi_intel_get_amp_tplg_suffix(amp_type);
  1145. if (!tplg_suffix) {
  1146. dev_err(sdev->dev, "no tplg suffix found, amp %d\n",
  1147. amp_type);
  1148. return NULL;
  1149. }
  1150. tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
  1151. "%s-%s",
  1152. sof_pdata->tplg_filename,
  1153. tplg_suffix);
  1154. if (!tplg_filename)
  1155. return NULL;
  1156. sof_pdata->tplg_filename = tplg_filename;
  1157. }
  1158. if (tplg_fixup &&
  1159. mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_CODEC_NAME &&
  1160. codec_type != CODEC_NONE) {
  1161. tplg_suffix = snd_soc_acpi_intel_get_codec_tplg_suffix(codec_type);
  1162. if (!tplg_suffix) {
  1163. dev_err(sdev->dev, "no tplg suffix found, codec %d\n",
  1164. codec_type);
  1165. return NULL;
  1166. }
  1167. tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
  1168. "%s-%s",
  1169. sof_pdata->tplg_filename,
  1170. tplg_suffix);
  1171. if (!tplg_filename)
  1172. return NULL;
  1173. sof_pdata->tplg_filename = tplg_filename;
  1174. }
  1175. if (tplg_fixup) {
  1176. tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
  1177. "%s%s",
  1178. sof_pdata->tplg_filename,
  1179. ".tplg");
  1180. if (!tplg_filename)
  1181. return NULL;
  1182. sof_pdata->tplg_filename = tplg_filename;
  1183. }
  1184. /* check if mclk_id should be modified from topology defaults */
  1185. if (mclk_id_override >= 0) {
  1186. dev_info(sdev->dev, "Overriding topology with MCLK %d from kernel_parameter\n", mclk_id_override);
  1187. sdev->mclk_id_override = true;
  1188. sdev->mclk_id_quirk = mclk_id_override;
  1189. }
  1190. }
  1191. return mach;
  1192. }
  1193. int hda_pci_intel_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
  1194. {
  1195. int ret;
  1196. ret = snd_intel_dsp_driver_probe(pci);
  1197. if (ret != SND_INTEL_DSP_DRIVER_ANY && ret != SND_INTEL_DSP_DRIVER_SOF) {
  1198. dev_dbg(&pci->dev, "SOF PCI driver not selected, aborting probe\n");
  1199. return -ENODEV;
  1200. }
  1201. return sof_pci_probe(pci, pci_id);
  1202. }
  1203. EXPORT_SYMBOL_NS(hda_pci_intel_probe, SND_SOC_SOF_INTEL_HDA_GENERIC);
  1204. int hda_register_clients(struct snd_sof_dev *sdev)
  1205. {
  1206. return hda_probes_register(sdev);
  1207. }
  1208. void hda_unregister_clients(struct snd_sof_dev *sdev)
  1209. {
  1210. hda_probes_unregister(sdev);
  1211. }
  1212. MODULE_LICENSE("Dual BSD/GPL");
  1213. MODULE_DESCRIPTION("SOF support for HDaudio platforms");
  1214. MODULE_IMPORT_NS(SND_SOC_SOF_PCI_DEV);
  1215. MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC);
  1216. MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
  1217. MODULE_IMPORT_NS(SND_SOC_SOF_XTENSA);
  1218. MODULE_IMPORT_NS(SND_INTEL_SOUNDWIRE_ACPI);
  1219. MODULE_IMPORT_NS(SOUNDWIRE_INTEL_INIT);
  1220. MODULE_IMPORT_NS(SOUNDWIRE_INTEL);
  1221. MODULE_IMPORT_NS(SND_SOC_SOF_HDA_MLINK);
  1222. MODULE_IMPORT_NS(SND_SOC_SOF_INTEL_HDA_COMMON);
  1223. MODULE_IMPORT_NS(SND_SOC_ACPI_INTEL_MATCH);