1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
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.
6 // Copyright(c) 2018 Intel Corporation
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>
15 * Hardware interface for generic Intel audio DSP HDA IP
18 #include <sound/hdaudio_ext.h>
19 #include <sound/hda_register.h>
21 #include <linux/acpi.h>
22 #include <linux/debugfs.h>
23 #include <linux/module.h>
24 #include <linux/soundwire/sdw.h>
25 #include <linux/soundwire/sdw_intel.h>
26 #include <sound/intel-dsp-config.h>
27 #include <sound/intel-nhlt.h>
28 #include <sound/soc-acpi-intel-ssp-common.h>
29 #include <sound/sof.h>
30 #include <sound/sof/xtensa.h>
31 #include <sound/hda-mlink.h>
32 #include "../sof-audio.h"
33 #include "../sof-pci-dev.h"
35 #include "../ipc4-topology.h"
38 #include <trace/events/sof_intel.h>
40 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
41 #include <sound/soc-acpi-intel-match.h>
44 /* platform specific devices */
47 #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
50 * The default for SoundWire clock stop quirks is to power gate the IP
51 * and do a Bus Reset, this will need to be modified when the DSP
52 * needs to remain in D0i3 so that the Master does not lose context
53 * and enumeration is not required on clock restart
55 static int sdw_clock_stop_quirks = SDW_INTEL_CLK_STOP_BUS_RESET;
56 module_param(sdw_clock_stop_quirks, int, 0444);
57 MODULE_PARM_DESC(sdw_clock_stop_quirks, "SOF SoundWire clock stop quirks");
59 static int sdw_params_stream(struct device *dev,
60 struct sdw_intel_stream_params_data *params_data)
62 struct snd_soc_dai *d = params_data->dai;
63 struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(d, params_data->substream->stream);
64 struct snd_sof_dai_config_data data = { 0 };
66 data.dai_index = (params_data->link_id << 8) | d->id;
67 data.dai_data = params_data->alh_stream_id;
68 data.dai_node_id = data.dai_data;
70 return hda_dai_config(w, SOF_DAI_CONFIG_FLAGS_HW_PARAMS, &data);
73 static int sdw_params_free(struct device *dev, struct sdw_intel_stream_free_data *free_data)
75 struct snd_soc_dai *d = free_data->dai;
76 struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(d, free_data->substream->stream);
77 struct snd_sof_dev *sdev = widget_to_sdev(w);
79 if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) {
80 struct snd_sof_widget *swidget = w->dobj.private;
81 struct snd_sof_dai *dai = swidget->private;
82 struct sof_ipc4_copier_data *copier_data;
83 struct sof_ipc4_copier *ipc4_copier;
85 ipc4_copier = dai->private;
86 ipc4_copier->dai_index = 0;
87 copier_data = &ipc4_copier->data;
89 /* clear the node ID */
90 copier_data->gtw_cfg.node_id &= ~SOF_IPC4_NODE_INDEX_MASK;
96 struct sdw_intel_ops sdw_callback = {
97 .params_stream = sdw_params_stream,
98 .free_stream = sdw_params_free,
101 static int sdw_ace2x_params_stream(struct device *dev,
102 struct sdw_intel_stream_params_data *params_data)
104 return sdw_hda_dai_hw_params(params_data->substream,
105 params_data->hw_params,
107 params_data->link_id,
108 params_data->alh_stream_id);
111 static int sdw_ace2x_free_stream(struct device *dev,
112 struct sdw_intel_stream_free_data *free_data)
114 return sdw_hda_dai_hw_free(free_data->substream,
119 static int sdw_ace2x_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
121 return sdw_hda_dai_trigger(substream, cmd, dai);
124 static struct sdw_intel_ops sdw_ace2x_callback = {
125 .params_stream = sdw_ace2x_params_stream,
126 .free_stream = sdw_ace2x_free_stream,
127 .trigger = sdw_ace2x_trigger,
130 static int hda_sdw_acpi_scan(struct snd_sof_dev *sdev)
132 u32 interface_mask = hda_get_interface_mask(sdev);
133 struct sof_intel_hda_dev *hdev;
137 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
140 handle = ACPI_HANDLE(sdev->dev);
142 /* save ACPI info for the probe step */
143 hdev = sdev->pdata->hw_pdata;
145 ret = sdw_intel_acpi_scan(handle, &hdev->info);
152 static int hda_sdw_probe(struct snd_sof_dev *sdev)
154 const struct sof_intel_dsp_desc *chip;
155 struct sof_intel_hda_dev *hdev;
156 struct sdw_intel_res res;
159 hdev = sdev->pdata->hw_pdata;
161 memset(&res, 0, sizeof(res));
163 chip = get_chip_info(sdev->pdata);
164 if (chip->hw_ip_version < SOF_INTEL_ACE_2_0) {
165 res.mmio_base = sdev->bar[HDA_DSP_BAR];
166 res.hw_ops = &sdw_intel_cnl_hw_ops;
167 res.shim_base = hdev->desc->sdw_shim_base;
168 res.alh_base = hdev->desc->sdw_alh_base;
170 res.ops = &sdw_callback;
173 * retrieve eml_lock needed to protect shared registers
174 * in the HDaudio multi-link areas
176 res.eml_lock = hdac_bus_eml_get_mutex(sof_to_bus(sdev), true,
177 AZX_REG_ML_LEPTR_ID_SDW);
181 res.mmio_base = sdev->bar[HDA_DSP_HDA_BAR];
183 * the SHIM and SoundWire register offsets are link-specific
184 * and will be determined when adding auxiliary devices
186 res.hw_ops = &sdw_intel_lnl_hw_ops;
188 res.ops = &sdw_ace2x_callback;
191 res.irq = sdev->ipc_irq;
192 res.handle = hdev->info.handle;
193 res.parent = sdev->dev;
196 res.clock_stop_quirks = sdw_clock_stop_quirks;
197 res.hbus = sof_to_bus(sdev);
200 * ops and arg fields are not populated for now,
201 * they will be needed when the DAI callbacks are
205 /* we could filter links here if needed, e.g for quirks */
206 res.count = hdev->info.count;
207 res.link_mask = hdev->info.link_mask;
209 sdw = sdw_intel_probe(&res);
211 dev_err(sdev->dev, "error: SoundWire probe failed\n");
221 int hda_sdw_startup(struct snd_sof_dev *sdev)
223 struct sof_intel_hda_dev *hdev;
224 struct snd_sof_pdata *pdata = sdev->pdata;
227 hdev = sdev->pdata->hw_pdata;
232 if (pdata->machine && !pdata->machine->mach_params.link_mask)
235 ret = hda_sdw_check_lcount(sdev);
239 return sdw_intel_startup(hdev->sdw);
241 EXPORT_SYMBOL_NS(hda_sdw_startup, SND_SOC_SOF_INTEL_HDA_GENERIC);
243 static int hda_sdw_exit(struct snd_sof_dev *sdev)
245 struct sof_intel_hda_dev *hdev;
247 hdev = sdev->pdata->hw_pdata;
250 sdw_intel_exit(hdev->sdw);
253 hda_sdw_int_enable(sdev, false);
258 bool hda_common_check_sdw_irq(struct snd_sof_dev *sdev)
260 struct sof_intel_hda_dev *hdev;
264 hdev = sdev->pdata->hw_pdata;
270 irq_status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS2);
272 /* invalid message ? */
273 if (irq_status == 0xffffffff)
277 if (irq_status & HDA_DSP_REG_ADSPIS2_SNDW)
283 EXPORT_SYMBOL_NS(hda_common_check_sdw_irq, SND_SOC_SOF_INTEL_HDA_GENERIC);
285 static bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev)
287 u32 interface_mask = hda_get_interface_mask(sdev);
288 const struct sof_intel_dsp_desc *chip;
290 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
293 chip = get_chip_info(sdev->pdata);
294 if (chip && chip->check_sdw_irq)
295 return chip->check_sdw_irq(sdev);
300 static irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
302 return sdw_intel_thread(irq, context);
305 bool hda_sdw_check_wakeen_irq_common(struct snd_sof_dev *sdev)
307 struct sof_intel_hda_dev *hdev;
309 hdev = sdev->pdata->hw_pdata;
311 snd_sof_dsp_read(sdev, HDA_DSP_BAR,
312 hdev->desc->sdw_shim_base + SDW_SHIM_WAKESTS))
317 EXPORT_SYMBOL_NS(hda_sdw_check_wakeen_irq_common, SND_SOC_SOF_INTEL_HDA_GENERIC);
319 static bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev)
321 u32 interface_mask = hda_get_interface_mask(sdev);
322 const struct sof_intel_dsp_desc *chip;
324 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
327 chip = get_chip_info(sdev->pdata);
328 if (chip && chip->check_sdw_wakeen_irq)
329 return chip->check_sdw_wakeen_irq(sdev);
334 void hda_sdw_process_wakeen_common(struct snd_sof_dev *sdev)
336 u32 interface_mask = hda_get_interface_mask(sdev);
337 struct sof_intel_hda_dev *hdev;
339 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
342 hdev = sdev->pdata->hw_pdata;
346 sdw_intel_process_wakeen_event(hdev->sdw);
348 EXPORT_SYMBOL_NS(hda_sdw_process_wakeen_common, SND_SOC_SOF_INTEL_HDA_GENERIC);
350 #else /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) */
351 static inline int hda_sdw_acpi_scan(struct snd_sof_dev *sdev)
356 static inline int hda_sdw_probe(struct snd_sof_dev *sdev)
361 static inline int hda_sdw_exit(struct snd_sof_dev *sdev)
366 static inline bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev)
371 static inline irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
376 static inline bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev)
381 #endif /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) */
383 /* pre fw run operations */
384 int hda_dsp_pre_fw_run(struct snd_sof_dev *sdev)
386 /* disable clock gating and power gating */
387 return hda_dsp_ctrl_clock_power_gating(sdev, false);
390 /* post fw run operations */
391 int hda_dsp_post_fw_run(struct snd_sof_dev *sdev)
395 if (sdev->first_boot) {
396 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
398 ret = hda_sdw_startup(sdev);
401 "error: could not startup SoundWire links\n");
405 /* Check if IMR boot is usable */
406 if (!sof_debug_check_flag(SOF_DBG_IGNORE_D3_PERSISTENT) &&
407 (sdev->fw_ready.flags & SOF_IPC_INFO_D3_PERSISTENT ||
408 sdev->pdata->ipc_type == SOF_IPC_TYPE_4)) {
409 hdev->imrboot_supported = true;
410 debugfs_create_bool("skip_imr_boot",
411 0644, sdev->debugfs_root,
412 &hdev->skip_imr_boot);
416 hda_sdw_int_enable(sdev, true);
418 /* re-enable clock gating and power gating */
419 return hda_dsp_ctrl_clock_power_gating(sdev, true);
421 EXPORT_SYMBOL_NS(hda_dsp_post_fw_run, SND_SOC_SOF_INTEL_HDA_GENERIC);
427 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG)
428 static bool hda_use_msi = true;
429 module_param_named(use_msi, hda_use_msi, bool, 0444);
430 MODULE_PARM_DESC(use_msi, "SOF HDA use PCI MSI mode");
432 #define hda_use_msi (1)
435 static char *hda_model;
436 module_param(hda_model, charp, 0444);
437 MODULE_PARM_DESC(hda_model, "Use the given HDA board model.");
439 static int dmic_num_override = -1;
440 module_param_named(dmic_num, dmic_num_override, int, 0444);
441 MODULE_PARM_DESC(dmic_num, "SOF HDA DMIC number");
443 static int mclk_id_override = -1;
444 module_param_named(mclk_id, mclk_id_override, int, 0444);
445 MODULE_PARM_DESC(mclk_id, "SOF SSP mclk_id");
447 static int hda_init(struct snd_sof_dev *sdev)
449 struct hda_bus *hbus;
450 struct hdac_bus *bus;
451 struct pci_dev *pci = to_pci_dev(sdev->dev);
454 hbus = sof_to_hbus(sdev);
455 bus = sof_to_bus(sdev);
458 sof_hda_bus_init(sdev, &pci->dev);
460 if (sof_hda_position_quirk == SOF_HDA_POSITION_QUIRK_USE_DPIB_REGISTERS)
464 bus->bdl_pos_adj = 0;
467 mutex_init(&hbus->prepare_mutex);
469 hbus->mixer_assigned = -1;
470 hbus->modelname = hda_model;
472 /* initialise hdac bus */
473 bus->addr = pci_resource_start(pci, 0);
474 bus->remap_addr = pci_ioremap_bar(pci, 0);
475 if (!bus->remap_addr) {
476 dev_err(bus->dev, "error: ioremap error\n");
481 sdev->bar[HDA_DSP_HDA_BAR] = bus->remap_addr;
483 /* init i915 and HDMI codecs */
484 ret = hda_codec_i915_init(sdev);
485 if (ret < 0 && ret != -ENODEV) {
486 dev_err_probe(sdev->dev, ret, "init of i915 and HDMI codec failed\n");
490 /* get controller capabilities */
491 ret = hda_dsp_ctrl_get_caps(sdev);
493 dev_err(sdev->dev, "error: get caps error\n");
494 hda_codec_i915_exit(sdev);
499 iounmap(sof_to_bus(sdev)->remap_addr);
504 static int check_dmic_num(struct snd_sof_dev *sdev)
506 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
507 struct nhlt_acpi_table *nhlt;
512 dmic_num = intel_nhlt_get_dmic_geo(sdev->dev, nhlt);
514 /* allow for module parameter override */
515 if (dmic_num_override != -1) {
517 "overriding DMICs detected in NHLT tables %d by kernel param %d\n",
518 dmic_num, dmic_num_override);
519 dmic_num = dmic_num_override;
522 if (dmic_num < 0 || dmic_num > 4) {
523 dev_dbg(sdev->dev, "invalid dmic_number %d\n", dmic_num);
530 static int check_nhlt_ssp_mask(struct snd_sof_dev *sdev)
532 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
533 struct nhlt_acpi_table *nhlt;
540 if (intel_nhlt_has_endpoint_type(nhlt, NHLT_LINK_SSP)) {
541 ssp_mask = intel_nhlt_ssp_endpoint_mask(nhlt, NHLT_DEVICE_I2S);
543 dev_info(sdev->dev, "NHLT_DEVICE_I2S detected, ssp_mask %#x\n", ssp_mask);
549 static int check_nhlt_ssp_mclk_mask(struct snd_sof_dev *sdev, int ssp_num)
551 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
552 struct nhlt_acpi_table *nhlt;
558 return intel_nhlt_ssp_mclk_mask(nhlt, ssp_num);
561 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC) || IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
563 static const char *fixup_tplg_name(struct snd_sof_dev *sdev,
564 const char *sof_tplg_filename,
565 const char *idisp_str,
566 const char *dmic_str)
568 const char *tplg_filename = NULL;
569 char *filename, *tmp;
570 const char *split_ext;
572 filename = kstrdup(sof_tplg_filename, GFP_KERNEL);
576 /* this assumes a .tplg extension */
578 split_ext = strsep(&tmp, ".");
580 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
582 split_ext, idisp_str, dmic_str);
585 return tplg_filename;
588 static int dmic_detect_topology_fixup(struct snd_sof_dev *sdev,
589 const char **tplg_filename,
590 const char *idisp_str,
594 const char *dmic_str;
597 /* first check for DMICs (using NHLT or module parameter) */
598 dmic_num = check_dmic_num(sdev);
620 const char *default_tplg_filename = *tplg_filename;
621 const char *fixed_tplg_filename;
623 fixed_tplg_filename = fixup_tplg_name(sdev, default_tplg_filename,
624 idisp_str, dmic_str);
625 if (!fixed_tplg_filename)
627 *tplg_filename = fixed_tplg_filename;
630 dev_info(sdev->dev, "DMICs detected in NHLT tables: %d\n", dmic_num);
631 *dmic_found = dmic_num;
637 static int hda_init_caps(struct snd_sof_dev *sdev)
639 u32 interface_mask = hda_get_interface_mask(sdev);
640 struct hdac_bus *bus = sof_to_bus(sdev);
641 struct snd_sof_pdata *pdata = sdev->pdata;
642 struct sof_intel_hda_dev *hdev = pdata->hw_pdata;
646 /* check if dsp is there */
648 dev_dbg(sdev->dev, "PP capability, will probe DSP later.\n");
650 /* Init HDA controller after i915 init */
651 ret = hda_dsp_ctrl_init_chip(sdev);
653 dev_err(bus->dev, "error: init chip failed with ret: %d\n",
658 hda_bus_ml_init(bus);
660 /* Skip SoundWire if it is not supported */
661 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
664 /* scan SoundWire capabilities exposed by DSDT */
665 ret = hda_sdw_acpi_scan(sdev);
667 dev_dbg(sdev->dev, "skipping SoundWire, not detected with ACPI scan\n");
671 link_mask = hdev->info.link_mask;
673 dev_dbg(sdev->dev, "skipping SoundWire, no links enabled\n");
678 * probe/allocate SoundWire resources.
679 * The hardware configuration takes place in hda_sdw_startup
680 * after power rails are enabled.
681 * It's entirely possible to have a mix of I2S/DMIC/SoundWire
682 * devices, so we allocate the resources in all cases.
684 ret = hda_sdw_probe(sdev);
686 dev_err(sdev->dev, "error: SoundWire probe error\n");
692 /* create codec instances */
693 hda_codec_probe_bus(sdev);
695 if (!HDA_IDISP_CODEC(bus->codec_mask))
696 hda_codec_i915_display_power(sdev, false);
698 hda_bus_ml_put_all(bus);
703 static irqreturn_t hda_dsp_interrupt_handler(int irq, void *context)
705 struct snd_sof_dev *sdev = context;
708 * Get global interrupt status. It includes all hardware interrupt
709 * sources in the Intel HD Audio controller.
711 if (snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS) &
712 SOF_HDA_INTSTS_GIS) {
714 /* disable GIE interrupt */
715 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
717 SOF_HDA_INT_GLOBAL_EN,
720 return IRQ_WAKE_THREAD;
726 static irqreturn_t hda_dsp_interrupt_thread(int irq, void *context)
728 struct snd_sof_dev *sdev = context;
729 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
731 /* deal with streams and controller first */
732 if (hda_dsp_check_stream_irq(sdev)) {
733 trace_sof_intel_hda_irq(sdev, "stream");
734 hda_dsp_stream_threaded_handler(irq, sdev);
737 if (hda_check_ipc_irq(sdev)) {
738 trace_sof_intel_hda_irq(sdev, "ipc");
739 sof_ops(sdev)->irq_thread(irq, sdev);
742 if (hda_dsp_check_sdw_irq(sdev)) {
743 trace_sof_intel_hda_irq(sdev, "sdw");
744 hda_dsp_sdw_thread(irq, hdev->sdw);
747 if (hda_sdw_check_wakeen_irq(sdev)) {
748 trace_sof_intel_hda_irq(sdev, "wakeen");
749 hda_sdw_process_wakeen(sdev);
752 hda_codec_check_for_state_change(sdev);
754 /* enable GIE interrupt */
755 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
757 SOF_HDA_INT_GLOBAL_EN,
758 SOF_HDA_INT_GLOBAL_EN);
763 int hda_dsp_probe_early(struct snd_sof_dev *sdev)
765 struct pci_dev *pci = to_pci_dev(sdev->dev);
766 struct sof_intel_hda_dev *hdev;
767 const struct sof_intel_dsp_desc *chip;
770 if (!sdev->dspless_mode_selected) {
772 * detect DSP by checking class/subclass/prog-id information
773 * class=04 subclass 03 prog-if 00: no DSP, legacy driver is required
774 * class=04 subclass 01 prog-if 00: DSP is present
775 * (and may be required e.g. for DMIC or SSP support)
776 * class=04 subclass 03 prog-if 80: either of DSP or legacy mode works
778 if (pci->class == 0x040300) {
779 dev_err(sdev->dev, "the DSP is not enabled on this platform, aborting probe\n");
781 } else if (pci->class != 0x040100 && pci->class != 0x040380) {
782 dev_err(sdev->dev, "unknown PCI class/subclass/prog-if 0x%06x found, aborting probe\n",
786 dev_info(sdev->dev, "DSP detected with PCI class/subclass/prog-if 0x%06x\n",
790 chip = get_chip_info(sdev->pdata);
792 dev_err(sdev->dev, "error: no such device supported, chip id:%x\n",
798 sdev->num_cores = chip->cores_num;
800 hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
803 sdev->pdata->hw_pdata = hdev;
805 ret = hda_init(sdev);
810 EXPORT_SYMBOL_NS(hda_dsp_probe_early, SND_SOC_SOF_INTEL_HDA_GENERIC);
812 int hda_dsp_probe(struct snd_sof_dev *sdev)
814 struct pci_dev *pci = to_pci_dev(sdev->dev);
815 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
816 const struct sof_intel_dsp_desc *chip;
819 hdev->dmic_dev = platform_device_register_data(sdev->dev, "dmic-codec",
822 if (IS_ERR(hdev->dmic_dev)) {
823 dev_err(sdev->dev, "error: failed to create DMIC device\n");
824 return PTR_ERR(hdev->dmic_dev);
828 * use position update IPC if either it is forced
829 * or we don't have other choice
831 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_FORCE_IPC_POSITION)
832 hdev->no_ipc_position = 0;
834 hdev->no_ipc_position = sof_ops(sdev)->pcm_pointer ? 1 : 0;
837 if (sdev->dspless_mode_selected)
838 hdev->no_ipc_position = 1;
840 if (sdev->dspless_mode_selected)
844 sdev->bar[HDA_DSP_BAR] = pci_ioremap_bar(pci, HDA_DSP_BAR);
845 if (!sdev->bar[HDA_DSP_BAR]) {
846 dev_err(sdev->dev, "error: ioremap error\n");
851 sdev->mmio_bar = HDA_DSP_BAR;
852 sdev->mailbox_bar = HDA_DSP_BAR;
855 /* allow 64bit DMA address if supported by H/W */
856 if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(64))) {
857 dev_dbg(sdev->dev, "DMA mask is 32 bit\n");
858 dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32));
860 dma_set_max_seg_size(&pci->dev, UINT_MAX);
863 ret = hda_dsp_stream_init(sdev);
865 dev_err(sdev->dev, "error: failed to init streams\n");
867 * not all errors are due to memory issues, but trying
868 * to free everything does not harm
875 * let's try to enable msi firstly
876 * if it fails, use legacy interrupt mode
877 * TODO: support msi multiple vectors
879 if (hda_use_msi && pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI) > 0) {
880 dev_info(sdev->dev, "use msi interrupt mode\n");
881 sdev->ipc_irq = pci_irq_vector(pci, 0);
882 /* initialised to "false" by kzalloc() */
883 sdev->msi_enabled = true;
886 if (!sdev->msi_enabled) {
887 dev_info(sdev->dev, "use legacy interrupt mode\n");
889 * in IO-APIC mode, hda->irq and ipc_irq are using the same
890 * irq number of pci->irq
892 sdev->ipc_irq = pci->irq;
895 dev_dbg(sdev->dev, "using IPC IRQ %d\n", sdev->ipc_irq);
896 ret = request_threaded_irq(sdev->ipc_irq, hda_dsp_interrupt_handler,
897 hda_dsp_interrupt_thread,
898 IRQF_SHARED, "AudioDSP", sdev);
900 dev_err(sdev->dev, "error: failed to register IPC IRQ %d\n",
902 goto free_irq_vector;
906 synchronize_irq(pci->irq);
909 * clear TCSEL to clear playback on some HD Audio
910 * codecs. PCI TCSEL is defined in the Intel manuals.
912 snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
914 /* init HDA capabilities */
915 ret = hda_init_caps(sdev);
919 if (!sdev->dspless_mode_selected) {
920 /* enable ppcap interrupt */
921 hda_dsp_ctrl_ppcap_enable(sdev, true);
922 hda_dsp_ctrl_ppcap_int_enable(sdev, true);
924 /* set default mailbox offset for FW ready message */
925 sdev->dsp_box.offset = HDA_DSP_MBOX_UPLINK_OFFSET;
927 INIT_DELAYED_WORK(&hdev->d0i3_work, hda_dsp_d0i3_work);
930 chip = get_chip_info(sdev->pdata);
931 if (chip && chip->hw_ip_version >= SOF_INTEL_ACE_2_0) {
932 ret = hda_sdw_startup(sdev);
934 dev_err(sdev->dev, "could not startup SoundWire links\n");
938 hda_sdw_int_enable(sdev, true);
941 init_waitqueue_head(&hdev->waitq);
943 hdev->nhlt = intel_nhlt_init(sdev->dev);
948 if (!sdev->dspless_mode_selected) {
949 hda_dsp_ctrl_ppcap_int_enable(sdev, false);
950 hda_dsp_ctrl_ppcap_enable(sdev, false);
953 free_irq(sdev->ipc_irq, sdev);
955 if (sdev->msi_enabled)
956 pci_free_irq_vectors(pci);
958 hda_dsp_stream_free(sdev);
959 /* dsp_unmap: not currently used */
960 if (!sdev->dspless_mode_selected)
961 iounmap(sdev->bar[HDA_DSP_BAR]);
963 platform_device_unregister(hdev->dmic_dev);
967 EXPORT_SYMBOL_NS(hda_dsp_probe, SND_SOC_SOF_INTEL_HDA_GENERIC);
969 void hda_dsp_remove(struct snd_sof_dev *sdev)
971 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
972 const struct sof_intel_dsp_desc *chip = hda->desc;
973 struct pci_dev *pci = to_pci_dev(sdev->dev);
974 struct nhlt_acpi_table *nhlt = hda->nhlt;
977 intel_nhlt_free(nhlt);
979 if (!sdev->dspless_mode_selected)
980 /* cancel any attempt for DSP D0I3 */
981 cancel_delayed_work_sync(&hda->d0i3_work);
983 hda_codec_device_remove(sdev);
987 if (!IS_ERR_OR_NULL(hda->dmic_dev))
988 platform_device_unregister(hda->dmic_dev);
990 if (!sdev->dspless_mode_selected) {
991 /* disable DSP IRQ */
992 hda_dsp_ctrl_ppcap_int_enable(sdev, false);
995 /* disable CIE and GIE interrupts */
996 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
997 SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN, 0);
999 if (sdev->dspless_mode_selected)
1000 goto skip_disable_dsp;
1002 /* no need to check for error as the DSP will be disabled anyway */
1003 if (chip && chip->power_down_dsp)
1004 chip->power_down_dsp(sdev);
1007 hda_dsp_ctrl_ppcap_enable(sdev, false);
1010 free_irq(sdev->ipc_irq, sdev);
1011 if (sdev->msi_enabled)
1012 pci_free_irq_vectors(pci);
1014 hda_dsp_stream_free(sdev);
1016 hda_bus_ml_free(sof_to_bus(sdev));
1018 if (!sdev->dspless_mode_selected)
1019 iounmap(sdev->bar[HDA_DSP_BAR]);
1021 EXPORT_SYMBOL_NS(hda_dsp_remove, SND_SOC_SOF_INTEL_HDA_GENERIC);
1023 void hda_dsp_remove_late(struct snd_sof_dev *sdev)
1025 iounmap(sof_to_bus(sdev)->remap_addr);
1026 sof_hda_bus_exit(sdev);
1027 hda_codec_i915_exit(sdev);
1030 int hda_power_down_dsp(struct snd_sof_dev *sdev)
1032 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
1033 const struct sof_intel_dsp_desc *chip = hda->desc;
1035 return hda_dsp_core_reset_power_down(sdev, chip->host_managed_cores_mask);
1037 EXPORT_SYMBOL_NS(hda_power_down_dsp, SND_SOC_SOF_INTEL_HDA_GENERIC);
1039 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
1040 static void hda_generic_machine_select(struct snd_sof_dev *sdev,
1041 struct snd_soc_acpi_mach **mach)
1043 struct hdac_bus *bus = sof_to_bus(sdev);
1044 struct snd_soc_acpi_mach_params *mach_params;
1045 struct snd_soc_acpi_mach *hda_mach;
1046 struct snd_sof_pdata *pdata = sdev->pdata;
1047 const char *tplg_filename;
1048 const char *idisp_str;
1054 /* codec detection */
1055 if (!bus->codec_mask) {
1056 dev_info(bus->dev, "no hda codecs found!\n");
1058 dev_info(bus->dev, "hda codecs found, mask %lx\n",
1061 for (i = 0; i < HDA_MAX_CODECS; i++) {
1062 if (bus->codec_mask & (1 << i))
1067 * If no machine driver is found, then:
1069 * generic hda machine driver can handle:
1070 * - one HDMI codec, and/or
1071 * - one external HDAudio codec
1073 if (!*mach && codec_num <= 2) {
1076 hda_mach = snd_soc_acpi_intel_hda_machines;
1078 dev_info(bus->dev, "using HDA machine driver %s now\n",
1079 hda_mach->drv_name);
1081 if (codec_num == 1 && HDA_IDISP_CODEC(bus->codec_mask))
1082 idisp_str = "-idisp";
1086 /* topology: use the info from hda_machines */
1087 if (pdata->tplg_filename) {
1089 tplg_filename = pdata->tplg_filename;
1092 tplg_filename = hda_mach->sof_tplg_filename;
1094 ret = dmic_detect_topology_fixup(sdev, &tplg_filename, idisp_str, &dmic_num,
1099 hda_mach->mach_params.dmic_num = dmic_num;
1100 pdata->tplg_filename = tplg_filename;
1102 if (codec_num == 2 ||
1103 (codec_num == 1 && !HDA_IDISP_CODEC(bus->codec_mask))) {
1105 * Prevent SoundWire links from starting when an external
1106 * HDaudio codec is used
1108 hda_mach->mach_params.link_mask = 0;
1111 * Allow SoundWire links to start when no external HDaudio codec
1112 * was detected. This will not create a SoundWire card but
1113 * will help detect if any SoundWire codec reports as ATTACHED.
1115 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
1117 hda_mach->mach_params.link_mask = hdev->info.link_mask;
1124 /* used by hda machine driver to create dai links */
1126 mach_params = &(*mach)->mach_params;
1127 mach_params->codec_mask = bus->codec_mask;
1128 mach_params->common_hdmi_codec_drv = true;
1132 static void hda_generic_machine_select(struct snd_sof_dev *sdev,
1133 struct snd_soc_acpi_mach **mach)
1138 #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
1140 static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
1142 struct snd_sof_pdata *pdata = sdev->pdata;
1143 const struct snd_soc_acpi_link_adr *link;
1144 struct sdw_extended_slave_id *ids;
1145 struct snd_soc_acpi_mach *mach;
1146 struct sof_intel_hda_dev *hdev;
1150 hdev = pdata->hw_pdata;
1151 link_mask = hdev->info.link_mask;
1154 dev_info(sdev->dev, "SoundWire links not enabled\n");
1159 dev_dbg(sdev->dev, "SoundWire context not allocated\n");
1163 if (!hdev->sdw->num_slaves) {
1164 dev_warn(sdev->dev, "No SoundWire peripheral detected in ACPI tables\n");
1169 * Select SoundWire machine driver if needed using the
1170 * alternate tables. This case deals with SoundWire-only
1171 * machines, for mixed cases with I2C/I2S the detection relies
1174 for (mach = pdata->desc->alt_machines;
1175 mach && mach->link_mask; mach++) {
1177 * On some platforms such as Up Extreme all links
1178 * are enabled but only one link can be used by
1179 * external codec. Instead of exact match of two masks,
1180 * first check whether link_mask of mach is subset of
1181 * link_mask supported by hw and then go on searching
1184 if (~link_mask & mach->link_mask)
1187 /* No need to match adr if there is no links defined */
1192 for (i = 0; i < hdev->info.count && link->num_adr;
1195 * Try next machine if any expected Slaves
1196 * are not found on this link.
1198 if (!snd_soc_acpi_sdw_link_slaves_found(sdev->dev, link,
1200 hdev->sdw->num_slaves))
1203 /* Found if all Slaves are checked */
1204 if (i == hdev->info.count || !link->num_adr)
1207 if (mach && mach->link_mask) {
1210 const char *tplg_filename;
1212 mach->mach_params.links = mach->links;
1213 mach->mach_params.link_mask = mach->link_mask;
1214 mach->mach_params.platform = dev_name(sdev->dev);
1216 if (pdata->tplg_filename) {
1220 tplg_filename = mach->sof_tplg_filename;
1224 * DMICs use up to 4 pins and are typically pin-muxed with SoundWire
1225 * link 2 and 3, or link 1 and 2, thus we only try to enable dmics
1226 * if all conditions are true:
1227 * a) 2 or fewer links are used by SoundWire
1228 * b) the NHLT table reports the presence of microphones
1230 if (hweight_long(mach->link_mask) <= 2) {
1233 ret = dmic_detect_topology_fixup(sdev, &tplg_filename, "",
1234 &dmic_num, tplg_fixup);
1239 pdata->tplg_filename = tplg_filename;
1240 mach->mach_params.dmic_num = dmic_num;
1243 "SoundWire machine driver %s topology %s\n",
1245 pdata->tplg_filename);
1250 dev_info(sdev->dev, "No SoundWire machine driver found for the ACPI-reported configuration:\n");
1251 ids = hdev->sdw->ids;
1252 for (i = 0; i < hdev->sdw->num_slaves; i++)
1253 dev_info(sdev->dev, "link %d mfg_id 0x%04x part_id 0x%04x version %#x\n",
1254 ids[i].link_id, ids[i].id.mfg_id, ids[i].id.part_id, ids[i].id.sdw_version);
1259 static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
1265 void hda_set_mach_params(struct snd_soc_acpi_mach *mach,
1266 struct snd_sof_dev *sdev)
1268 struct snd_sof_pdata *pdata = sdev->pdata;
1269 const struct sof_dev_desc *desc = pdata->desc;
1270 struct snd_soc_acpi_mach_params *mach_params;
1272 mach_params = &mach->mach_params;
1273 mach_params->platform = dev_name(sdev->dev);
1274 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
1275 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
1276 mach_params->num_dai_drivers = SOF_SKL_NUM_DAIS_NOCODEC;
1278 mach_params->num_dai_drivers = desc->ops->num_drv;
1279 mach_params->dai_drivers = desc->ops->drv;
1282 static int check_tplg_quirk_mask(struct snd_soc_acpi_mach *mach)
1285 u32 codec_amp_name_quirk;
1288 * In current implementation dmic and ssp quirks are designed for es8336
1289 * machine driver and could not be mixed with codec name and amp name
1292 dmic_ssp_quirk = mach->tplg_quirk_mask &
1293 (SND_SOC_ACPI_TPLG_INTEL_DMIC_NUMBER | SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER);
1294 codec_amp_name_quirk = mach->tplg_quirk_mask &
1295 (SND_SOC_ACPI_TPLG_INTEL_AMP_NAME | SND_SOC_ACPI_TPLG_INTEL_CODEC_NAME);
1297 if (dmic_ssp_quirk && codec_amp_name_quirk)
1303 struct snd_soc_acpi_mach *hda_machine_select(struct snd_sof_dev *sdev)
1305 u32 interface_mask = hda_get_interface_mask(sdev);
1306 struct snd_sof_pdata *sof_pdata = sdev->pdata;
1307 const struct sof_dev_desc *desc = sof_pdata->desc;
1308 struct hdac_bus *bus = sof_to_bus(sdev);
1309 struct snd_soc_acpi_mach *mach = NULL;
1310 enum snd_soc_acpi_intel_codec codec_type;
1311 const char *tplg_filename;
1312 const char *tplg_suffix;
1314 /* Try I2S or DMIC if it is supported */
1315 if (interface_mask & (BIT(SOF_DAI_INTEL_SSP) | BIT(SOF_DAI_INTEL_DMIC)))
1316 mach = snd_soc_acpi_find_machine(desc->machines);
1319 bool add_extension = false;
1320 bool tplg_fixup = false;
1323 * If tplg file name is overridden, use it instead of
1324 * the one set in mach table
1326 if (!sof_pdata->tplg_filename) {
1327 sof_pdata->tplg_filename = mach->sof_tplg_filename;
1332 * Checking quirk mask integrity; some quirk flags could not be
1336 check_tplg_quirk_mask(mach)) {
1337 dev_err(sdev->dev, "Invalid tplg quirk mask 0x%x\n",
1338 mach->tplg_quirk_mask);
1342 /* report to machine driver if any DMICs are found */
1343 mach->mach_params.dmic_num = check_dmic_num(sdev);
1346 mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_DMIC_NUMBER &&
1347 mach->mach_params.dmic_num) {
1348 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1350 sof_pdata->tplg_filename,
1352 mach->mach_params.dmic_num,
1357 sof_pdata->tplg_filename = tplg_filename;
1358 add_extension = true;
1361 if (mach->link_mask) {
1362 mach->mach_params.links = mach->links;
1363 mach->mach_params.link_mask = mach->link_mask;
1366 /* report SSP link mask to machine driver */
1367 mach->mach_params.i2s_link_mask = check_nhlt_ssp_mask(sdev);
1370 mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER &&
1371 mach->mach_params.i2s_link_mask) {
1372 const struct sof_intel_dsp_desc *chip = get_chip_info(sdev->pdata);
1376 if (hweight_long(mach->mach_params.i2s_link_mask) > 1 &&
1377 !(mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_MSB))
1378 dev_warn(sdev->dev, "More than one SSP exposed by NHLT, choosing MSB\n");
1380 /* fls returns 1-based results, SSPs indices are 0-based */
1381 ssp_num = fls(mach->mach_params.i2s_link_mask) - 1;
1383 if (ssp_num >= chip->ssp_count) {
1384 dev_err(sdev->dev, "Invalid SSP %d, max on this platform is %d\n",
1385 ssp_num, chip->ssp_count);
1389 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1391 sof_pdata->tplg_filename,
1397 sof_pdata->tplg_filename = tplg_filename;
1398 add_extension = true;
1400 mclk_mask = check_nhlt_ssp_mclk_mask(sdev, ssp_num);
1402 if (mclk_mask < 0) {
1403 dev_err(sdev->dev, "Invalid MCLK configuration\n");
1407 dev_dbg(sdev->dev, "MCLK mask %#x found in NHLT\n", mclk_mask);
1410 dev_info(sdev->dev, "Overriding topology with MCLK mask %#x from NHLT\n", mclk_mask);
1411 sdev->mclk_id_override = true;
1412 sdev->mclk_id_quirk = (mclk_mask & BIT(0)) ? 0 : 1;
1416 codec_type = snd_soc_acpi_intel_detect_amp_type(sdev->dev);
1419 mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_AMP_NAME &&
1420 codec_type != CODEC_NONE) {
1421 tplg_suffix = snd_soc_acpi_intel_get_amp_tplg_suffix(codec_type);
1423 dev_err(sdev->dev, "no tplg suffix found, amp %d\n",
1428 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1430 sof_pdata->tplg_filename,
1435 sof_pdata->tplg_filename = tplg_filename;
1436 add_extension = true;
1439 codec_type = snd_soc_acpi_intel_detect_codec_type(sdev->dev);
1442 mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_CODEC_NAME &&
1443 codec_type != CODEC_NONE) {
1444 tplg_suffix = snd_soc_acpi_intel_get_codec_tplg_suffix(codec_type);
1446 dev_err(sdev->dev, "no tplg suffix found, codec %d\n",
1451 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1453 sof_pdata->tplg_filename,
1458 sof_pdata->tplg_filename = tplg_filename;
1459 add_extension = true;
1462 if (tplg_fixup && add_extension) {
1463 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1465 sof_pdata->tplg_filename,
1470 sof_pdata->tplg_filename = tplg_filename;
1473 /* check if mclk_id should be modified from topology defaults */
1474 if (mclk_id_override >= 0) {
1475 dev_info(sdev->dev, "Overriding topology with MCLK %d from kernel_parameter\n", mclk_id_override);
1476 sdev->mclk_id_override = true;
1477 sdev->mclk_id_quirk = mclk_id_override;
1482 * If I2S fails and no external HDaudio codec is detected,
1483 * try SoundWire if it is supported
1485 if (!mach && !HDA_EXT_CODEC(bus->codec_mask) &&
1486 (interface_mask & BIT(SOF_DAI_INTEL_ALH)))
1487 mach = hda_sdw_machine_select(sdev);
1490 * Choose HDA generic machine driver if mach is NULL.
1491 * Otherwise, set certain mach params.
1493 hda_generic_machine_select(sdev, &mach);
1495 dev_warn(sdev->dev, "warning: No matching ASoC machine driver found\n");
1500 int hda_pci_intel_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
1504 ret = snd_intel_dsp_driver_probe(pci);
1505 if (ret != SND_INTEL_DSP_DRIVER_ANY && ret != SND_INTEL_DSP_DRIVER_SOF) {
1506 dev_dbg(&pci->dev, "SOF PCI driver not selected, aborting probe\n");
1510 return sof_pci_probe(pci, pci_id);
1512 EXPORT_SYMBOL_NS(hda_pci_intel_probe, SND_SOC_SOF_INTEL_HDA_GENERIC);
1514 int hda_register_clients(struct snd_sof_dev *sdev)
1516 return hda_probes_register(sdev);
1519 void hda_unregister_clients(struct snd_sof_dev *sdev)
1521 hda_probes_unregister(sdev);
1524 MODULE_LICENSE("Dual BSD/GPL");
1525 MODULE_IMPORT_NS(SND_SOC_SOF_PCI_DEV);
1526 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC);
1527 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
1528 MODULE_IMPORT_NS(SND_SOC_SOF_XTENSA);
1529 MODULE_IMPORT_NS(SND_INTEL_SOUNDWIRE_ACPI);
1530 MODULE_IMPORT_NS(SOUNDWIRE_INTEL_INIT);
1531 MODULE_IMPORT_NS(SOUNDWIRE_INTEL);
1532 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_MLINK);
1533 MODULE_IMPORT_NS(SND_SOC_SOF_INTEL_HDA_COMMON);
1534 MODULE_IMPORT_NS(SND_SOC_ACPI_INTEL_MATCH);