at v6.19 1768 lines 48 kB view raw
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/* 15 * Hardware interface for generic Intel audio DSP HDA IP 16 */ 17 18#include <sound/hdaudio_ext.h> 19#include <sound/hda_register.h> 20 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/soc_sdw_utils.h> 30#include <sound/sof.h> 31#include <sound/sof/xtensa.h> 32#include <sound/hda-mlink.h> 33#include "../sof-audio.h" 34#include "../sof-pci-dev.h" 35#include "../ops.h" 36#include "../ipc4-topology.h" 37#include "../../intel/common/sof-function-topology-lib.h" 38#include "hda.h" 39 40#include <trace/events/sof_intel.h> 41 42#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA) 43#include <sound/soc-acpi-intel-match.h> 44#endif 45 46/* platform specific devices */ 47#include "shim.h" 48 49#if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) 50 51/* 52 * The default for SoundWire clock stop quirks is to power gate the IP 53 * and do a Bus Reset, this will need to be modified when the DSP 54 * needs to remain in D0i3 so that the Master does not lose context 55 * and enumeration is not required on clock restart 56 */ 57static int sdw_clock_stop_quirks = SDW_INTEL_CLK_STOP_BUS_RESET; 58module_param(sdw_clock_stop_quirks, int, 0444); 59MODULE_PARM_DESC(sdw_clock_stop_quirks, "SOF SoundWire clock stop quirks"); 60 61static int sdw_params_stream(struct device *dev, 62 struct sdw_intel_stream_params_data *params_data) 63{ 64 struct snd_soc_dai *d = params_data->dai; 65 struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(d, params_data->substream->stream); 66 struct snd_sof_dai_config_data data = { 0 }; 67 68 if (!w) { 69 dev_err(dev, "%s widget not found, check amp link num in the topology\n", 70 d->name); 71 return -EINVAL; 72 } 73 data.dai_index = (params_data->link_id << 8) | d->id; 74 data.dai_data = params_data->alh_stream_id; 75 data.dai_node_id = data.dai_data; 76 77 return hda_dai_config(w, SOF_DAI_CONFIG_FLAGS_HW_PARAMS, &data); 78} 79 80static int sdw_params_free(struct device *dev, struct sdw_intel_stream_free_data *free_data) 81{ 82 struct snd_soc_dai *d = free_data->dai; 83 struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(d, free_data->substream->stream); 84 struct snd_sof_dev *sdev = widget_to_sdev(w); 85 86 if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) { 87 struct snd_sof_widget *swidget = w->dobj.private; 88 struct snd_sof_dai *dai = swidget->private; 89 struct sof_ipc4_copier_data *copier_data; 90 struct sof_ipc4_copier *ipc4_copier; 91 92 ipc4_copier = dai->private; 93 ipc4_copier->dai_index = 0; 94 copier_data = &ipc4_copier->data; 95 96 /* clear the node ID */ 97 copier_data->gtw_cfg.node_id &= ~SOF_IPC4_NODE_INDEX_MASK; 98 } 99 100 return 0; 101} 102 103struct sdw_intel_ops sdw_callback = { 104 .params_stream = sdw_params_stream, 105 .free_stream = sdw_params_free, 106}; 107 108static int sdw_ace2x_params_stream(struct device *dev, 109 struct sdw_intel_stream_params_data *params_data) 110{ 111 return sdw_hda_dai_hw_params(params_data->substream, 112 params_data->hw_params, 113 params_data->dai, 114 params_data->link_id, 115 params_data->alh_stream_id); 116} 117 118static int sdw_ace2x_free_stream(struct device *dev, 119 struct sdw_intel_stream_free_data *free_data) 120{ 121 return sdw_hda_dai_hw_free(free_data->substream, 122 free_data->dai, 123 free_data->link_id); 124} 125 126static int sdw_ace2x_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai) 127{ 128 return sdw_hda_dai_trigger(substream, cmd, dai); 129} 130 131static struct sdw_intel_ops sdw_ace2x_callback = { 132 .params_stream = sdw_ace2x_params_stream, 133 .free_stream = sdw_ace2x_free_stream, 134 .trigger = sdw_ace2x_trigger, 135}; 136 137static int hda_sdw_acpi_scan(struct snd_sof_dev *sdev) 138{ 139 u32 interface_mask = hda_get_interface_mask(sdev); 140 struct sof_intel_hda_dev *hdev; 141 acpi_handle handle; 142 int ret; 143 144 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH))) 145 return -EINVAL; 146 147 handle = ACPI_HANDLE(sdev->dev); 148 149 /* save ACPI info for the probe step */ 150 hdev = sdev->pdata->hw_pdata; 151 152 ret = sdw_intel_acpi_scan(handle, &hdev->info); 153 if (ret < 0) 154 return -EINVAL; 155 156 return 0; 157} 158 159static int hda_sdw_probe(struct snd_sof_dev *sdev) 160{ 161 const struct sof_intel_dsp_desc *chip; 162 struct sof_intel_hda_dev *hdev; 163 struct sdw_intel_res res; 164 void *sdw; 165 166 hdev = sdev->pdata->hw_pdata; 167 168 memset(&res, 0, sizeof(res)); 169 170 chip = get_chip_info(sdev->pdata); 171 if (chip->hw_ip_version < SOF_INTEL_ACE_2_0) { 172 res.mmio_base = sdev->bar[HDA_DSP_BAR]; 173 res.hw_ops = &sdw_intel_cnl_hw_ops; 174 res.shim_base = hdev->desc->sdw_shim_base; 175 res.alh_base = hdev->desc->sdw_alh_base; 176 res.ext = false; 177 res.ops = &sdw_callback; 178 } else { 179 /* 180 * retrieve eml_lock needed to protect shared registers 181 * in the HDaudio multi-link areas 182 */ 183 res.eml_lock = hdac_bus_eml_get_mutex(sof_to_bus(sdev), true, 184 AZX_REG_ML_LEPTR_ID_SDW); 185 if (!res.eml_lock) 186 return -ENODEV; 187 188 res.mmio_base = sdev->bar[HDA_DSP_HDA_BAR]; 189 /* 190 * the SHIM and SoundWire register offsets are link-specific 191 * and will be determined when adding auxiliary devices 192 */ 193 res.hw_ops = &sdw_intel_lnl_hw_ops; 194 res.ext = true; 195 res.ops = &sdw_ace2x_callback; 196 197 /* ACE3+ supports microphone privacy */ 198 if (chip->hw_ip_version >= SOF_INTEL_ACE_3_0) 199 res.mic_privacy = true; 200 } 201 res.irq = sdev->ipc_irq; 202 res.handle = hdev->info.handle; 203 res.parent = sdev->dev; 204 205 res.dev = sdev->dev; 206 res.clock_stop_quirks = sdw_clock_stop_quirks; 207 res.hbus = sof_to_bus(sdev); 208 209 /* 210 * ops and arg fields are not populated for now, 211 * they will be needed when the DAI callbacks are 212 * provided 213 */ 214 215 /* we could filter links here if needed, e.g for quirks */ 216 res.count = hdev->info.count; 217 res.link_mask = hdev->info.link_mask; 218 219 sdw = sdw_intel_probe(&res); 220 if (!sdw) { 221 dev_err(sdev->dev, "error: SoundWire probe failed\n"); 222 return -EINVAL; 223 } 224 225 /* save context */ 226 hdev->sdw = sdw; 227 228 return 0; 229} 230 231int hda_sdw_startup(struct snd_sof_dev *sdev) 232{ 233 struct sof_intel_hda_dev *hdev; 234 struct snd_sof_pdata *pdata = sdev->pdata; 235 int ret; 236 237 hdev = sdev->pdata->hw_pdata; 238 239 if (!hdev->sdw) 240 return 0; 241 242 if (pdata->machine && !pdata->machine->mach_params.link_mask) 243 return 0; 244 245 ret = hda_sdw_check_lcount(sdev); 246 if (ret < 0) 247 return ret; 248 249 return sdw_intel_startup(hdev->sdw); 250} 251EXPORT_SYMBOL_NS(hda_sdw_startup, "SND_SOC_SOF_INTEL_HDA_GENERIC"); 252 253static int hda_sdw_exit(struct snd_sof_dev *sdev) 254{ 255 struct sof_intel_hda_dev *hdev; 256 257 hdev = sdev->pdata->hw_pdata; 258 259 if (hdev->sdw) 260 sdw_intel_exit(hdev->sdw); 261 hdev->sdw = NULL; 262 263 hda_sdw_int_enable(sdev, false); 264 265 return 0; 266} 267 268bool hda_common_check_sdw_irq(struct snd_sof_dev *sdev) 269{ 270 struct sof_intel_hda_dev *hdev; 271 bool ret = false; 272 u32 irq_status; 273 274 hdev = sdev->pdata->hw_pdata; 275 276 if (!hdev->sdw) 277 return ret; 278 279 /* store status */ 280 irq_status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS2); 281 282 /* invalid message ? */ 283 if (irq_status == 0xffffffff) 284 goto out; 285 286 /* SDW message ? */ 287 if (irq_status & HDA_DSP_REG_ADSPIS2_SNDW) 288 ret = true; 289 290out: 291 return ret; 292} 293EXPORT_SYMBOL_NS(hda_common_check_sdw_irq, "SND_SOC_SOF_INTEL_HDA_GENERIC"); 294 295static bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev) 296{ 297 u32 interface_mask = hda_get_interface_mask(sdev); 298 const struct sof_intel_dsp_desc *chip; 299 300 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH))) 301 return false; 302 303 chip = get_chip_info(sdev->pdata); 304 if (chip && chip->check_sdw_irq) 305 return chip->check_sdw_irq(sdev); 306 307 return false; 308} 309 310static irqreturn_t hda_dsp_sdw_thread(int irq, void *context) 311{ 312 return sdw_intel_thread(irq, context); 313} 314 315bool hda_sdw_check_wakeen_irq_common(struct snd_sof_dev *sdev) 316{ 317 struct sof_intel_hda_dev *hdev; 318 319 hdev = sdev->pdata->hw_pdata; 320 if (hdev->sdw && 321 snd_sof_dsp_read(sdev, HDA_DSP_BAR, 322 hdev->desc->sdw_shim_base + SDW_SHIM_WAKESTS)) 323 return true; 324 325 return false; 326} 327EXPORT_SYMBOL_NS(hda_sdw_check_wakeen_irq_common, "SND_SOC_SOF_INTEL_HDA_GENERIC"); 328 329static bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev) 330{ 331 u32 interface_mask = hda_get_interface_mask(sdev); 332 const struct sof_intel_dsp_desc *chip; 333 334 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH))) 335 return false; 336 337 chip = get_chip_info(sdev->pdata); 338 if (chip && chip->check_sdw_wakeen_irq) 339 return chip->check_sdw_wakeen_irq(sdev); 340 341 return false; 342} 343 344void hda_sdw_process_wakeen_common(struct snd_sof_dev *sdev) 345{ 346 u32 interface_mask = hda_get_interface_mask(sdev); 347 struct sof_intel_hda_dev *hdev; 348 349 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH))) 350 return; 351 352 hdev = sdev->pdata->hw_pdata; 353 if (!hdev->sdw) 354 return; 355 356 sdw_intel_process_wakeen_event(hdev->sdw); 357} 358EXPORT_SYMBOL_NS(hda_sdw_process_wakeen_common, "SND_SOC_SOF_INTEL_HDA_GENERIC"); 359 360static bool hda_dsp_sdw_check_mic_privacy_irq(struct snd_sof_dev *sdev) 361{ 362 const struct sof_intel_dsp_desc *chip; 363 364 chip = get_chip_info(sdev->pdata); 365 if (chip && chip->check_mic_privacy_irq) 366 return chip->check_mic_privacy_irq(sdev, true, 367 AZX_REG_ML_LEPTR_ID_SDW); 368 369 return false; 370} 371 372static void hda_dsp_sdw_process_mic_privacy(struct snd_sof_dev *sdev) 373{ 374 const struct sof_intel_dsp_desc *chip; 375 376 chip = get_chip_info(sdev->pdata); 377 if (chip && chip->process_mic_privacy) 378 chip->process_mic_privacy(sdev, true, AZX_REG_ML_LEPTR_ID_SDW); 379} 380 381#else /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) */ 382static inline int hda_sdw_acpi_scan(struct snd_sof_dev *sdev) 383{ 384 return 0; 385} 386 387static inline int hda_sdw_probe(struct snd_sof_dev *sdev) 388{ 389 return 0; 390} 391 392static inline int hda_sdw_exit(struct snd_sof_dev *sdev) 393{ 394 return 0; 395} 396 397static inline bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev) 398{ 399 return false; 400} 401 402static inline irqreturn_t hda_dsp_sdw_thread(int irq, void *context) 403{ 404 return IRQ_HANDLED; 405} 406 407static inline bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev) 408{ 409 return false; 410} 411 412static inline bool hda_dsp_sdw_check_mic_privacy_irq(struct snd_sof_dev *sdev) 413{ 414 return false; 415} 416 417static inline void hda_dsp_sdw_process_mic_privacy(struct snd_sof_dev *sdev) { } 418 419#endif /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) */ 420 421/* pre fw run operations */ 422int hda_dsp_pre_fw_run(struct snd_sof_dev *sdev) 423{ 424 /* disable clock gating and power gating */ 425 return hda_dsp_ctrl_clock_power_gating(sdev, false); 426} 427 428/* post fw run operations */ 429int hda_dsp_post_fw_run(struct snd_sof_dev *sdev) 430{ 431 int ret; 432 433 if (sdev->first_boot) { 434 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; 435 436 ret = hda_sdw_startup(sdev); 437 if (ret < 0) { 438 dev_err(sdev->dev, 439 "error: could not startup SoundWire links\n"); 440 return ret; 441 } 442 443 /* Check if IMR boot is usable */ 444 if (!sof_debug_check_flag(SOF_DBG_IGNORE_D3_PERSISTENT) && 445 (sdev->fw_ready.flags & SOF_IPC_INFO_D3_PERSISTENT || 446 sdev->pdata->ipc_type == SOF_IPC_TYPE_4)) { 447 hdev->imrboot_supported = true; 448 debugfs_create_bool("skip_imr_boot", 449 0644, sdev->debugfs_root, 450 &hdev->skip_imr_boot); 451 } 452 } 453 454 hda_sdw_int_enable(sdev, true); 455 456 /* re-enable clock gating and power gating */ 457 return hda_dsp_ctrl_clock_power_gating(sdev, true); 458} 459EXPORT_SYMBOL_NS(hda_dsp_post_fw_run, "SND_SOC_SOF_INTEL_HDA_GENERIC"); 460 461/* 462 * Debug 463 */ 464 465#if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG) 466static bool hda_use_msi = true; 467module_param_named(use_msi, hda_use_msi, bool, 0444); 468MODULE_PARM_DESC(use_msi, "SOF HDA use PCI MSI mode"); 469#else 470#define hda_use_msi (1) 471#endif 472 473static char *hda_model; 474module_param(hda_model, charp, 0444); 475MODULE_PARM_DESC(hda_model, "Use the given HDA board model."); 476 477static int dmic_num_override = -1; 478module_param_named(dmic_num, dmic_num_override, int, 0444); 479MODULE_PARM_DESC(dmic_num, "SOF HDA DMIC number"); 480 481static int mclk_id_override = -1; 482module_param_named(mclk_id, mclk_id_override, int, 0444); 483MODULE_PARM_DESC(mclk_id, "SOF SSP mclk_id"); 484 485static int bt_link_mask_override; 486module_param_named(bt_link_mask, bt_link_mask_override, int, 0444); 487MODULE_PARM_DESC(bt_link_mask, "SOF BT offload link mask"); 488 489static int hda_init(struct snd_sof_dev *sdev) 490{ 491 struct hda_bus *hbus; 492 struct hdac_bus *bus; 493 struct pci_dev *pci = to_pci_dev(sdev->dev); 494 int ret; 495 496 hbus = sof_to_hbus(sdev); 497 bus = sof_to_bus(sdev); 498 499 /* HDA bus init */ 500 sof_hda_bus_init(sdev, &pci->dev); 501 502 if (sof_hda_position_quirk == SOF_HDA_POSITION_QUIRK_USE_DPIB_REGISTERS) 503 bus->use_posbuf = 0; 504 else 505 bus->use_posbuf = 1; 506 bus->bdl_pos_adj = 0; 507 bus->sync_write = 1; 508 509 mutex_init(&hbus->prepare_mutex); 510 hbus->pci = pci; 511 hbus->mixer_assigned = -1; 512 hbus->modelname = hda_model; 513 514 /* initialise hdac bus */ 515 bus->addr = pci_resource_start(pci, 0); 516 bus->remap_addr = pci_ioremap_bar(pci, 0); 517 if (!bus->remap_addr) { 518 dev_err(bus->dev, "error: ioremap error\n"); 519 return -ENXIO; 520 } 521 522 /* HDA base */ 523 sdev->bar[HDA_DSP_HDA_BAR] = bus->remap_addr; 524 525 /* init i915 and HDMI codecs */ 526 ret = hda_codec_i915_init(sdev); 527 if (ret < 0 && ret != -ENODEV) { 528 dev_err_probe(sdev->dev, ret, "init of i915 and HDMI codec failed\n"); 529 goto out; 530 } 531 532 /* get controller capabilities */ 533 ret = hda_dsp_ctrl_get_caps(sdev); 534 if (ret < 0) { 535 dev_err(sdev->dev, "error: get caps error\n"); 536 hda_codec_i915_exit(sdev); 537 } 538 539out: 540 if (ret < 0) 541 iounmap(sof_to_bus(sdev)->remap_addr); 542 543 return ret; 544} 545 546static int check_dmic_num(struct snd_sof_dev *sdev) 547{ 548 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; 549 struct nhlt_acpi_table *nhlt; 550 int dmic_num = 0; 551 552 nhlt = hdev->nhlt; 553 if (nhlt) 554 dmic_num = intel_nhlt_get_dmic_geo(sdev->dev, nhlt); 555 556 dev_info(sdev->dev, "DMICs detected in NHLT tables: %d\n", dmic_num); 557 558 /* allow for module parameter override */ 559 if (dmic_num_override != -1) { 560 dev_dbg(sdev->dev, 561 "overriding DMICs detected in NHLT tables %d by kernel param %d\n", 562 dmic_num, dmic_num_override); 563 dmic_num = dmic_num_override; 564 } 565 566 if (dmic_num < 0 || dmic_num > 4) { 567 dev_dbg(sdev->dev, "invalid dmic_number %d\n", dmic_num); 568 dmic_num = 0; 569 } 570 571 return dmic_num; 572} 573 574static int check_nhlt_ssp_mask(struct snd_sof_dev *sdev, u8 device_type) 575{ 576 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; 577 struct nhlt_acpi_table *nhlt; 578 int ssp_mask = 0; 579 580 nhlt = hdev->nhlt; 581 if (!nhlt) 582 return ssp_mask; 583 584 if (intel_nhlt_has_endpoint_type(nhlt, NHLT_LINK_SSP)) { 585 ssp_mask = intel_nhlt_ssp_endpoint_mask(nhlt, device_type); 586 if (ssp_mask) 587 dev_info(sdev->dev, "NHLT device %s(%d) detected, ssp_mask %#x\n", 588 device_type == NHLT_DEVICE_BT ? "BT" : "I2S", 589 device_type, ssp_mask); 590 } 591 592 return ssp_mask; 593} 594 595static int check_nhlt_ssp_mclk_mask(struct snd_sof_dev *sdev, int ssp_num) 596{ 597 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; 598 struct nhlt_acpi_table *nhlt; 599 600 nhlt = hdev->nhlt; 601 if (!nhlt) 602 return 0; 603 604 return intel_nhlt_ssp_mclk_mask(nhlt, ssp_num); 605} 606 607static int hda_init_caps(struct snd_sof_dev *sdev) 608{ 609 u32 interface_mask = hda_get_interface_mask(sdev); 610 struct hdac_bus *bus = sof_to_bus(sdev); 611 struct snd_sof_pdata *pdata = sdev->pdata; 612 struct sof_intel_hda_dev *hdev = pdata->hw_pdata; 613 u32 link_mask; 614 int ret = 0; 615 616 /* check if dsp is there */ 617 if (bus->ppcap) 618 dev_dbg(sdev->dev, "PP capability, will probe DSP later.\n"); 619 620 /* Init HDA controller after i915 init */ 621 ret = hda_dsp_ctrl_init_chip(sdev, true); 622 if (ret < 0) { 623 dev_err(bus->dev, "error: init chip failed with ret: %d\n", 624 ret); 625 return ret; 626 } 627 628 hda_bus_ml_init(bus); 629 630 /* Skip SoundWire if it is not supported */ 631 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH))) 632 goto skip_soundwire; 633 634 /* Skip SoundWire in nocodec mode */ 635 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) && 636 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC)) 637 goto skip_soundwire; 638 639 /* scan SoundWire capabilities exposed by DSDT */ 640 ret = hda_sdw_acpi_scan(sdev); 641 if (ret < 0) { 642 dev_dbg(sdev->dev, "skipping SoundWire, not detected with ACPI scan\n"); 643 goto skip_soundwire; 644 } 645 646 link_mask = hdev->info.link_mask; 647 if (!link_mask) { 648 dev_dbg(sdev->dev, "skipping SoundWire, no links enabled\n"); 649 goto skip_soundwire; 650 } 651 652 /* 653 * probe/allocate SoundWire resources. 654 * The hardware configuration takes place in hda_sdw_startup 655 * after power rails are enabled. 656 * It's entirely possible to have a mix of I2S/DMIC/SoundWire 657 * devices, so we allocate the resources in all cases. 658 */ 659 ret = hda_sdw_probe(sdev); 660 if (ret < 0) { 661 dev_err(sdev->dev, "error: SoundWire probe error\n"); 662 return ret; 663 } 664 665skip_soundwire: 666 667 /* create codec instances */ 668 hda_codec_probe_bus(sdev); 669 670 if (!HDA_IDISP_CODEC(bus->codec_mask)) 671 hda_codec_i915_display_power(sdev, false); 672 673 hda_bus_ml_put_all(bus); 674 675 return 0; 676} 677 678static irqreturn_t hda_dsp_interrupt_handler(int irq, void *context) 679{ 680 struct snd_sof_dev *sdev = context; 681 682 /* 683 * Get global interrupt status. It includes all hardware interrupt 684 * sources in the Intel HD Audio controller. 685 */ 686 if (snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS) & 687 SOF_HDA_INTSTS_GIS) { 688 689 /* disable GIE interrupt */ 690 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, 691 SOF_HDA_INTCTL, 692 SOF_HDA_INT_GLOBAL_EN, 693 0); 694 695 return IRQ_WAKE_THREAD; 696 } 697 698 return IRQ_NONE; 699} 700 701static irqreturn_t hda_dsp_interrupt_thread(int irq, void *context) 702{ 703 struct snd_sof_dev *sdev = context; 704 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; 705 706 /* deal with streams and controller first */ 707 if (hda_dsp_check_stream_irq(sdev)) { 708 trace_sof_intel_hda_irq(sdev, "stream"); 709 hda_dsp_stream_threaded_handler(irq, sdev); 710 } 711 712 if (hda_check_ipc_irq(sdev)) { 713 trace_sof_intel_hda_irq(sdev, "ipc"); 714 sof_ops(sdev)->irq_thread(irq, sdev); 715 } 716 717 if (hda_dsp_check_sdw_irq(sdev)) { 718 trace_sof_intel_hda_irq(sdev, "sdw"); 719 720 hda_dsp_sdw_thread(irq, hdev->sdw); 721 722 if (hda_dsp_sdw_check_mic_privacy_irq(sdev)) { 723 trace_sof_intel_hda_irq(sdev, "mic privacy"); 724 hda_dsp_sdw_process_mic_privacy(sdev); 725 } 726 } 727 728 if (hda_sdw_check_wakeen_irq(sdev)) { 729 trace_sof_intel_hda_irq(sdev, "wakeen"); 730 hda_sdw_process_wakeen(sdev); 731 } 732 733 hda_codec_check_for_state_change(sdev); 734 735 /* enable GIE interrupt */ 736 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, 737 SOF_HDA_INTCTL, 738 SOF_HDA_INT_GLOBAL_EN, 739 SOF_HDA_INT_GLOBAL_EN); 740 741 return IRQ_HANDLED; 742} 743 744int hda_dsp_probe_early(struct snd_sof_dev *sdev) 745{ 746 struct pci_dev *pci = to_pci_dev(sdev->dev); 747 struct sof_intel_hda_dev *hdev; 748 const struct sof_intel_dsp_desc *chip; 749 int ret = 0; 750 751 if (!sdev->dspless_mode_selected) { 752 /* 753 * detect DSP by checking class/subclass/prog-id information 754 * class=04 subclass 03 prog-if 00: no DSP, legacy driver is required 755 * class=04 subclass 01 prog-if 00: DSP is present 756 * (and may be required e.g. for DMIC or SSP support) 757 * class=04 subclass 03 prog-if 80: either of DSP or legacy mode works 758 */ 759 if (pci->class == 0x040300) { 760 dev_err(sdev->dev, "the DSP is not enabled on this platform, aborting probe\n"); 761 return -ENODEV; 762 } else if (pci->class != 0x040100 && pci->class != 0x040380) { 763 dev_err(sdev->dev, "unknown PCI class/subclass/prog-if 0x%06x found, aborting probe\n", 764 pci->class); 765 return -ENODEV; 766 } 767 dev_info_once(sdev->dev, "DSP detected with PCI class/subclass/prog-if 0x%06x\n", 768 pci->class); 769 } 770 771 chip = get_chip_info(sdev->pdata); 772 if (!chip) { 773 dev_err(sdev->dev, "error: no such device supported, chip id:%x\n", 774 pci->device); 775 ret = -EIO; 776 goto err; 777 } 778 779 sdev->num_cores = chip->cores_num; 780 781 hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL); 782 if (!hdev) 783 return -ENOMEM; 784 sdev->pdata->hw_pdata = hdev; 785 hdev->desc = chip; 786 ret = hda_init(sdev); 787 788err: 789 return ret; 790} 791EXPORT_SYMBOL_NS(hda_dsp_probe_early, "SND_SOC_SOF_INTEL_HDA_GENERIC"); 792 793int hda_dsp_probe(struct snd_sof_dev *sdev) 794{ 795 struct pci_dev *pci = to_pci_dev(sdev->dev); 796 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; 797 const struct sof_intel_dsp_desc *chip; 798 int ret = 0; 799 800 hdev->dmic_dev = platform_device_register_data(sdev->dev, "dmic-codec", 801 PLATFORM_DEVID_NONE, 802 NULL, 0); 803 if (IS_ERR(hdev->dmic_dev)) { 804 dev_err(sdev->dev, "error: failed to create DMIC device\n"); 805 return PTR_ERR(hdev->dmic_dev); 806 } 807 808 /* 809 * use position update IPC if either it is forced 810 * or we don't have other choice 811 */ 812#if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_FORCE_IPC_POSITION) 813 hdev->no_ipc_position = 0; 814#else 815 hdev->no_ipc_position = sof_ops(sdev)->pcm_pointer ? 1 : 0; 816#endif 817 818 if (sdev->dspless_mode_selected) 819 hdev->no_ipc_position = 1; 820 821 if (sdev->dspless_mode_selected) 822 goto skip_dsp_setup; 823 824 /* DSP base */ 825 sdev->bar[HDA_DSP_BAR] = pci_ioremap_bar(pci, HDA_DSP_BAR); 826 if (!sdev->bar[HDA_DSP_BAR]) { 827 dev_err(sdev->dev, "error: ioremap error\n"); 828 ret = -ENXIO; 829 goto hdac_bus_unmap; 830 } 831 832 sdev->mmio_bar = HDA_DSP_BAR; 833 sdev->mailbox_bar = HDA_DSP_BAR; 834skip_dsp_setup: 835 836 /* allow 64bit DMA address if supported by H/W */ 837 if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(64))) { 838 dev_dbg(sdev->dev, "DMA mask is 32 bit\n"); 839 dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32)); 840 } 841 dma_set_max_seg_size(&pci->dev, UINT_MAX); 842 843 /* init streams */ 844 ret = hda_dsp_stream_init(sdev); 845 if (ret < 0) { 846 dev_err(sdev->dev, "error: failed to init streams\n"); 847 /* 848 * not all errors are due to memory issues, but trying 849 * to free everything does not harm 850 */ 851 goto free_streams; 852 } 853 854 /* 855 * register our IRQ 856 * let's try to enable msi firstly 857 * if it fails, use legacy interrupt mode 858 * TODO: support msi multiple vectors 859 */ 860 if (hda_use_msi && pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI) > 0) { 861 dev_info(sdev->dev, "use msi interrupt mode\n"); 862 sdev->ipc_irq = pci_irq_vector(pci, 0); 863 /* initialised to "false" by kzalloc() */ 864 sdev->msi_enabled = true; 865 } 866 867 if (!sdev->msi_enabled) { 868 dev_info(sdev->dev, "use legacy interrupt mode\n"); 869 /* 870 * in IO-APIC mode, hda->irq and ipc_irq are using the same 871 * irq number of pci->irq 872 */ 873 sdev->ipc_irq = pci->irq; 874 } 875 876 dev_dbg(sdev->dev, "using IPC IRQ %d\n", sdev->ipc_irq); 877 ret = request_threaded_irq(sdev->ipc_irq, hda_dsp_interrupt_handler, 878 hda_dsp_interrupt_thread, 879 IRQF_SHARED, "AudioDSP", sdev); 880 if (ret < 0) { 881 dev_err(sdev->dev, "error: failed to register IPC IRQ %d\n", 882 sdev->ipc_irq); 883 goto free_irq_vector; 884 } 885 886 pci_set_master(pci); 887 synchronize_irq(pci->irq); 888 889 /* 890 * clear TCSEL to clear playback on some HD Audio 891 * codecs. PCI TCSEL is defined in the Intel manuals. 892 */ 893 snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0); 894 895 /* init HDA capabilities */ 896 ret = hda_init_caps(sdev); 897 if (ret < 0) 898 goto free_ipc_irq; 899 900 if (!sdev->dspless_mode_selected) { 901 /* enable ppcap interrupt */ 902 hda_dsp_ctrl_ppcap_enable(sdev, true); 903 hda_dsp_ctrl_ppcap_int_enable(sdev, true); 904 905 /* set default mailbox offset for FW ready message */ 906 sdev->dsp_box.offset = HDA_DSP_MBOX_UPLINK_OFFSET; 907 908 INIT_DELAYED_WORK(&hdev->d0i3_work, hda_dsp_d0i3_work); 909 } 910 911 chip = get_chip_info(sdev->pdata); 912 if (chip && chip->hw_ip_version >= SOF_INTEL_ACE_2_0) { 913 ret = hda_sdw_startup(sdev); 914 if (ret < 0) { 915 dev_err(sdev->dev, "could not startup SoundWire links\n"); 916 goto disable_pp_cap; 917 } 918 } 919 920 init_waitqueue_head(&hdev->waitq); 921 922 hdev->nhlt = intel_nhlt_init(sdev->dev); 923 924 return 0; 925 926disable_pp_cap: 927 if (!sdev->dspless_mode_selected) { 928 hda_dsp_ctrl_ppcap_int_enable(sdev, false); 929 hda_dsp_ctrl_ppcap_enable(sdev, false); 930 } 931free_ipc_irq: 932 free_irq(sdev->ipc_irq, sdev); 933free_irq_vector: 934 if (sdev->msi_enabled) 935 pci_free_irq_vectors(pci); 936free_streams: 937 hda_dsp_stream_free(sdev); 938/* dsp_unmap: not currently used */ 939 if (!sdev->dspless_mode_selected) 940 iounmap(sdev->bar[HDA_DSP_BAR]); 941hdac_bus_unmap: 942 platform_device_unregister(hdev->dmic_dev); 943 944 return ret; 945} 946EXPORT_SYMBOL_NS(hda_dsp_probe, "SND_SOC_SOF_INTEL_HDA_GENERIC"); 947 948void hda_dsp_remove(struct snd_sof_dev *sdev) 949{ 950 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata; 951 const struct sof_intel_dsp_desc *chip = hda->desc; 952 struct pci_dev *pci = to_pci_dev(sdev->dev); 953 struct nhlt_acpi_table *nhlt = hda->nhlt; 954 955 if (nhlt) 956 intel_nhlt_free(nhlt); 957 958 if (!sdev->dspless_mode_selected) 959 /* cancel any attempt for DSP D0I3 */ 960 cancel_delayed_work_sync(&hda->d0i3_work); 961 962 hda_codec_device_remove(sdev); 963 964 hda_sdw_exit(sdev); 965 966 if (!IS_ERR_OR_NULL(hda->dmic_dev)) 967 platform_device_unregister(hda->dmic_dev); 968 969 if (!sdev->dspless_mode_selected) { 970 /* disable DSP IRQ */ 971 hda_dsp_ctrl_ppcap_int_enable(sdev, false); 972 } 973 974 /* disable CIE and GIE interrupts */ 975 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL, 976 SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN, 0); 977 978 if (sdev->dspless_mode_selected) 979 goto skip_disable_dsp; 980 981 /* Cancel the microphone privacy work if mic privacy is active */ 982 if (hda->mic_privacy.active) 983 cancel_work_sync(&hda->mic_privacy.work); 984 985 /* no need to check for error as the DSP will be disabled anyway */ 986 if (chip && chip->power_down_dsp) 987 chip->power_down_dsp(sdev); 988 989 /* disable DSP */ 990 hda_dsp_ctrl_ppcap_enable(sdev, false); 991 992 /* Free the persistent DMA buffers used for base firmware download */ 993 if (hda->cl_dmab.area) 994 snd_dma_free_pages(&hda->cl_dmab); 995 if (hda->iccmax_dmab.area) 996 snd_dma_free_pages(&hda->iccmax_dmab); 997 998skip_disable_dsp: 999 free_irq(sdev->ipc_irq, sdev); 1000 if (sdev->msi_enabled) 1001 pci_free_irq_vectors(pci); 1002 1003 hda_dsp_stream_free(sdev); 1004 1005 hda_bus_ml_free(sof_to_bus(sdev)); 1006 1007 if (!sdev->dspless_mode_selected) 1008 iounmap(sdev->bar[HDA_DSP_BAR]); 1009} 1010EXPORT_SYMBOL_NS(hda_dsp_remove, "SND_SOC_SOF_INTEL_HDA_GENERIC"); 1011 1012void hda_dsp_remove_late(struct snd_sof_dev *sdev) 1013{ 1014 iounmap(sof_to_bus(sdev)->remap_addr); 1015 sof_hda_bus_exit(sdev); 1016 hda_codec_i915_exit(sdev); 1017} 1018 1019int hda_power_down_dsp(struct snd_sof_dev *sdev) 1020{ 1021 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata; 1022 const struct sof_intel_dsp_desc *chip = hda->desc; 1023 1024 return hda_dsp_core_reset_power_down(sdev, chip->host_managed_cores_mask); 1025} 1026EXPORT_SYMBOL_NS(hda_power_down_dsp, "SND_SOC_SOF_INTEL_HDA_GENERIC"); 1027 1028#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC) 1029static void hda_generic_machine_select(struct snd_sof_dev *sdev, 1030 struct snd_soc_acpi_mach **mach) 1031{ 1032 struct hdac_bus *bus = sof_to_bus(sdev); 1033 struct snd_soc_acpi_mach_params *mach_params; 1034 struct snd_soc_acpi_mach *hda_mach; 1035 struct snd_sof_pdata *pdata = sdev->pdata; 1036 const char *tplg_filename; 1037 int codec_num = 0; 1038 int i; 1039 1040 /* codec detection */ 1041 if (!bus->codec_mask) { 1042 dev_info(bus->dev, "no hda codecs found!\n"); 1043 } else { 1044 dev_info(bus->dev, "hda codecs found, mask %lx\n", 1045 bus->codec_mask); 1046 1047 for (i = 0; i < HDA_MAX_CODECS; i++) { 1048 if (bus->codec_mask & (1 << i)) 1049 codec_num++; 1050 } 1051 1052 /* 1053 * If no machine driver is found, then: 1054 * 1055 * generic hda machine driver can handle: 1056 * - one HDMI codec, and/or 1057 * - one external HDAudio codec 1058 */ 1059 if (!*mach && codec_num <= 2) { 1060 bool tplg_fixup = false; 1061 1062 /* 1063 * make a local copy of the match array since we might 1064 * be modifying it 1065 */ 1066 hda_mach = devm_kmemdup_array(sdev->dev, 1067 snd_soc_acpi_intel_hda_machines, 1068 2, /* we have one entry + sentinel in the array */ 1069 sizeof(snd_soc_acpi_intel_hda_machines[0]), 1070 GFP_KERNEL); 1071 if (!hda_mach) { 1072 dev_err(bus->dev, 1073 "%s: failed to duplicate the HDA match table\n", 1074 __func__); 1075 return; 1076 } 1077 1078 dev_info(bus->dev, "using HDA machine driver %s now\n", 1079 hda_mach->drv_name); 1080 1081 /* 1082 * topology: use the info from hda_machines since tplg file name 1083 * is not overwritten 1084 */ 1085 if (!pdata->tplg_filename) 1086 tplg_fixup = true; 1087 1088 if (tplg_fixup && 1089 codec_num == 1 && HDA_IDISP_CODEC(bus->codec_mask)) { 1090 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL, 1091 "%s-idisp", 1092 hda_mach->sof_tplg_filename); 1093 if (!tplg_filename) 1094 return; 1095 1096 hda_mach->sof_tplg_filename = tplg_filename; 1097 } 1098 1099 if (codec_num == 2 || 1100 (codec_num == 1 && !HDA_IDISP_CODEC(bus->codec_mask))) { 1101 /* 1102 * Prevent SoundWire links from starting when an external 1103 * HDaudio codec is used 1104 */ 1105 hda_mach->mach_params.link_mask = 0; 1106 } else { 1107 /* 1108 * Allow SoundWire links to start when no external HDaudio codec 1109 * was detected. This will not create a SoundWire card but 1110 * will help detect if any SoundWire codec reports as ATTACHED. 1111 */ 1112 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; 1113 1114 hda_mach->mach_params.link_mask = hdev->info.link_mask; 1115 } 1116 1117 *mach = hda_mach; 1118 } 1119 } 1120 1121 /* used by hda machine driver to create dai links */ 1122 if (*mach) { 1123 mach_params = &(*mach)->mach_params; 1124 mach_params->codec_mask = bus->codec_mask; 1125 } 1126} 1127#else 1128static void hda_generic_machine_select(struct snd_sof_dev *sdev, 1129 struct snd_soc_acpi_mach **mach) 1130{ 1131} 1132#endif 1133 1134#if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) 1135 1136static bool is_endpoint_present(struct sdw_slave *sdw_device, 1137 struct asoc_sdw_codec_info *dai_info, int dai_type) 1138{ 1139 int i; 1140 1141 for (i = 0; i < sdw_device->sdca_data.num_functions; i++) { 1142 if (dai_type == dai_info->dais[i].dai_type) 1143 return true; 1144 } 1145 dev_dbg(&sdw_device->dev, "Endpoint DAI type %d not found\n", dai_type); 1146 return false; 1147} 1148 1149static struct snd_soc_acpi_adr_device *find_acpi_adr_device(struct device *dev, 1150 struct sdw_slave *sdw_device, 1151 struct snd_soc_acpi_link_adr *link, 1152 int *amp_index) 1153{ 1154 struct snd_soc_acpi_adr_device *adr_dev; 1155 const char *name_prefix = ""; 1156 int index = link->num_adr; 1157 bool is_amp = true; /* Set it to false if the codec wiah any NON-AMP DAI type */ 1158 int ep_index = 0; 1159 int i, j; 1160 1161 link->mask = BIT(sdw_device->bus->link_id); 1162 /* index is 0 based, we need allocate index + 1 for the array size */ 1163 if (!index) 1164 adr_dev = devm_kzalloc(dev, sizeof(*adr_dev), GFP_KERNEL); 1165 else 1166 adr_dev = devm_krealloc(dev, (struct snd_soc_acpi_adr_device *)link->adr_d, 1167 (index + 1) * sizeof(*adr_dev), GFP_KERNEL); 1168 1169 if (!adr_dev) 1170 return NULL; 1171 1172 for (i = 0; i < asoc_sdw_get_codec_info_list_count(); i++) { 1173 struct snd_soc_acpi_endpoint *endpoints; 1174 int amp_group_id = 1; 1175 1176 if (sdw_device->id.part_id != codec_info_list[i].part_id) 1177 continue; 1178 1179 endpoints = devm_kcalloc(dev, codec_info_list[i].dai_num, 1180 sizeof(struct snd_soc_acpi_endpoint), GFP_KERNEL); 1181 if (!endpoints) 1182 return NULL; 1183 1184 name_prefix = codec_info_list[i].name_prefix; 1185 /* 1186 * This should not happen, but add a paranoid check to avoid NULL pointer 1187 * dereference 1188 */ 1189 if (!name_prefix) { 1190 dev_err(dev, "codec_info_list name_prefix of part id %#x is missing\n", 1191 codec_info_list[i].part_id); 1192 return NULL; 1193 } 1194 for (j = 0; j < codec_info_list[i].dai_num; j++) { 1195 /* Check if the endpoint is present by the SDCA DisCo table */ 1196 if (!is_endpoint_present(sdw_device, &codec_info_list[i], 1197 codec_info_list[i].dais[j].dai_type)) 1198 continue; 1199 1200 endpoints[ep_index].num = ep_index; 1201 if (codec_info_list[i].dais[j].dai_type == SOC_SDW_DAI_TYPE_AMP) { 1202 /* Assume all amp are aggregated */ 1203 endpoints[ep_index].aggregated = 1; 1204 endpoints[ep_index].group_id = amp_group_id; 1205 endpoints[ep_index].group_position = *amp_index; 1206 /* Set group id = 2 for feedback capture endpoint */ 1207 amp_group_id++; 1208 } else { 1209 endpoints[ep_index].aggregated = 0; 1210 endpoints[ep_index].group_id = 0; 1211 endpoints[ep_index].group_position = 0; 1212 is_amp = false; 1213 } 1214 ep_index++; 1215 } 1216 adr_dev[index].endpoints = endpoints; 1217 adr_dev[index].num_endpoints = ep_index; 1218 break; 1219 } 1220 1221 if (i == asoc_sdw_get_codec_info_list_count()) { 1222 dev_err(dev, "part id %#x is not supported\n", sdw_device->id.part_id); 1223 return NULL; 1224 } 1225 1226 adr_dev[index].adr = ((u64)sdw_device->id.class_id & 0xFF) | 1227 ((u64)sdw_device->id.part_id & 0xFFFF) << 8 | 1228 ((u64)sdw_device->id.mfg_id & 0xFFFF) << 24 | 1229 ((u64)(sdw_device->id.unique_id & 0xF) << 40) | 1230 ((u64)(sdw_device->id.sdw_version & 0xF) << 44) | 1231 ((u64)(sdw_device->bus->link_id & 0xF) << 48); 1232 1233 if (!is_amp) { 1234 /* For non-amp codecs, get name_prefix from codec_info_list[] */ 1235 adr_dev[index].name_prefix = devm_kasprintf(dev, GFP_KERNEL, "%s", name_prefix); 1236 goto done_name_prefix; 1237 } 1238 1239 /* 1240 * The name_prefix comes from codec_info_list which has a name_prefix per codec. 1241 * And we need to give a unique name_prefix for each amp and should be backwards 1242 * compatible to the existing acpi match tables to not break existing UCMs. 1243 * For the common name_prefix, we append the amp index to it. However, for the 1244 * "Left" name_prefix, we convert the second amp name_prefix to "Right" and 1245 * for the third and further amps, we set the name_prefix to "AMP<amp_index>". 1246 */ 1247 if (!strcmp(name_prefix, "Left")) { 1248 switch (*amp_index) { 1249 case 1: 1250 adr_dev[index].name_prefix = devm_kasprintf(dev, GFP_KERNEL, 1251 "%s", "Left"); 1252 break; 1253 case 2: 1254 adr_dev[index].name_prefix = devm_kasprintf(dev, GFP_KERNEL, 1255 "%s", "Right"); 1256 break; 1257 default: 1258 /* Set the name_fix to AMP<amp_index> if there are more than 2 amps */ 1259 adr_dev[index].name_prefix = devm_kasprintf(dev, GFP_KERNEL, "%s%d", 1260 "AMP", *amp_index); 1261 break; 1262 } 1263 } else if (!strcmp(name_prefix, "AMP")) { 1264 adr_dev[index].name_prefix = devm_kasprintf(dev, GFP_KERNEL, "%s%d", 1265 name_prefix, 1266 *amp_index); 1267 } else { 1268 /* 1269 * The name_prefix will be the amp name if it is not "Left" or "AMP", set it to 1270 * <name_prefix>-<amp_index> format. Like rt1320-1 1271 */ 1272 adr_dev[index].name_prefix = devm_kasprintf(dev, GFP_KERNEL, "%s-%d", 1273 name_prefix, 1274 *amp_index); 1275 } 1276 (*amp_index)++; 1277 1278done_name_prefix: 1279 if (!adr_dev[index].name_prefix) { 1280 dev_err(dev, "failed to allocate memory for name_prefix\n"); 1281 return NULL; 1282 } 1283 1284 dev_dbg(dev, "adr[%d] 0x%llx link id %d name_prefix \"%s\" is found\n", 1285 index, adr_dev[index].adr, sdw_device->bus->link_id, adr_dev[index].name_prefix); 1286 1287 link->num_adr++; 1288 1289 return adr_dev; 1290} 1291 1292static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev) 1293{ 1294 struct snd_sof_pdata *pdata = sdev->pdata; 1295 const struct snd_soc_acpi_link_adr *link; 1296 const struct sof_intel_dsp_desc *chip; 1297 struct snd_soc_acpi_link_adr *links; 1298 struct sdw_peripherals *peripherals; 1299 struct snd_soc_acpi_mach *mach; 1300 struct sof_intel_hda_dev *hdev; 1301 int link_index, link_num; 1302 int amp_index = 1; 1303 u32 link_mask = 0; 1304 int i; 1305 1306 hdev = pdata->hw_pdata; 1307 1308 if (!hdev->info.link_mask) { 1309 dev_info(sdev->dev, "SoundWire links not enabled\n"); 1310 return NULL; 1311 } 1312 1313 if (!hdev->sdw) { 1314 dev_dbg(sdev->dev, "SoundWire context not allocated\n"); 1315 return NULL; 1316 } 1317 1318 if (!hdev->sdw->peripherals || !hdev->sdw->peripherals->num_peripherals) { 1319 dev_warn(sdev->dev, "No SoundWire peripheral detected in ACPI tables\n"); 1320 return NULL; 1321 } 1322 1323 /* 1324 * Select SoundWire machine driver if needed using the 1325 * alternate tables. This case deals with SoundWire-only 1326 * machines, for mixed cases with I2C/I2S the detection relies 1327 * on the HID list. 1328 */ 1329 for (mach = pdata->desc->alt_machines; 1330 mach && mach->link_mask; mach++) { 1331 /* 1332 * On some platforms such as Up Extreme all links 1333 * are enabled but only one link can be used by 1334 * external codec. Instead of exact match of two masks, 1335 * first check whether link_mask of mach is subset of 1336 * link_mask supported by hw and then go on searching 1337 * link_adr 1338 */ 1339 if (~hdev->info.link_mask & mach->link_mask) 1340 continue; 1341 1342 /* No need to match adr if there is no links defined */ 1343 if (!mach->links) 1344 break; 1345 1346 link = mach->links; 1347 for (i = 0; i < hdev->info.count && link->num_adr; 1348 i++, link++) { 1349 /* 1350 * Try next machine if any expected Slaves 1351 * are not found on this link. 1352 */ 1353 if (!snd_soc_acpi_sdw_link_slaves_found(sdev->dev, link, 1354 hdev->sdw->peripherals)) 1355 break; 1356 } 1357 /* Found if all Slaves are checked */ 1358 if (i == hdev->info.count || !link->num_adr) 1359 if (!mach->machine_check || mach->machine_check(hdev->sdw)) 1360 break; 1361 } 1362 if (mach && mach->link_mask) { 1363 mach->mach_params.links = mach->links; 1364 mach->mach_params.link_mask = mach->link_mask; 1365 mach->mach_params.platform = dev_name(sdev->dev); 1366 1367 return mach; 1368 } 1369 1370 dev_info(sdev->dev, "No SoundWire machine driver found for the ACPI-reported configuration:\n"); 1371 peripherals = hdev->sdw->peripherals; 1372 for (i = 0; i < peripherals->num_peripherals; i++) 1373 dev_info(sdev->dev, "link %d mfg_id 0x%04x part_id 0x%04x version %#x\n", 1374 peripherals->array[i]->bus->link_id, 1375 peripherals->array[i]->id.mfg_id, 1376 peripherals->array[i]->id.part_id, 1377 peripherals->array[i]->id.sdw_version); 1378 1379 chip = get_chip_info(sdev->pdata); 1380 1381 /* SDCA was not well supported in the BIOS before ACE2.0 */ 1382 if (chip->hw_ip_version < SOF_INTEL_ACE_2_0) 1383 return NULL; 1384 1385 if (!peripherals->num_peripherals) 1386 return NULL; 1387 1388 /* Create default SDW mach */ 1389 mach = devm_kzalloc(sdev->dev, sizeof(*mach), GFP_KERNEL); 1390 if (!mach) 1391 return NULL; 1392 1393 /* Get link mask and link number */ 1394 for (i = 0; i < peripherals->num_peripherals; i++) 1395 link_mask |= BIT(peripherals->array[i]->bus->link_id); 1396 1397 link_num = hweight32(link_mask); 1398 links = devm_kcalloc(sdev->dev, link_num, sizeof(*links), GFP_KERNEL); 1399 if (!links) 1400 return NULL; 1401 1402 /* Generate snd_soc_acpi_link_adr struct for each peripheral reported by the ACPI table */ 1403 for (i = 0; i < peripherals->num_peripherals; i++) { 1404 /* link_index = the number of used links below the current link */ 1405 link_index = hweight32(link_mask & (BIT(peripherals->array[i]->bus->link_id) - 1)); 1406 links[link_index].adr_d = find_acpi_adr_device(sdev->dev, peripherals->array[i], 1407 &links[link_index], &amp_index); 1408 if (!links[link_index].adr_d) 1409 return NULL; 1410 } 1411 1412 mach->drv_name = "sof_sdw"; 1413 mach->mach_params.links = links; 1414 mach->mach_params.link_mask = link_mask; 1415 mach->mach_params.platform = dev_name(sdev->dev); 1416 mach->get_function_tplg_files = sof_sdw_get_tplg_files; 1417 /* 1418 * Set mach->sof_tplg_filename as a dummy topology to avoid tplg file checking 1419 * and being used. 1420 */ 1421 mach->sof_tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL, 1422 "sof-%s-dummy.tplg", chip->platform); 1423 1424 dev_info(sdev->dev, "Use SoundWire default machine driver with function topologies\n"); 1425 return mach; 1426} 1427#else 1428static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev) 1429{ 1430 return NULL; 1431} 1432#endif 1433 1434void hda_set_mach_params(struct snd_soc_acpi_mach *mach, 1435 struct snd_sof_dev *sdev) 1436{ 1437 struct snd_sof_pdata *pdata = sdev->pdata; 1438 const struct sof_dev_desc *desc = pdata->desc; 1439 struct snd_soc_acpi_mach_params *mach_params; 1440 1441 mach_params = &mach->mach_params; 1442 mach_params->platform = dev_name(sdev->dev); 1443 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) && 1444 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC)) 1445 mach_params->num_dai_drivers = SOF_SKL_NUM_DAIS_NOCODEC; 1446 else 1447 mach_params->num_dai_drivers = desc->ops->num_drv; 1448 mach_params->dai_drivers = desc->ops->drv; 1449} 1450 1451static int check_tplg_quirk_mask(struct snd_soc_acpi_mach *mach) 1452{ 1453 u32 dmic_ssp_quirk; 1454 u32 codec_amp_name_quirk; 1455 1456 /* 1457 * In current implementation dmic and ssp quirks are designed for es8336 1458 * machine driver and could not be mixed with codec name and amp name 1459 * quirks. 1460 */ 1461 dmic_ssp_quirk = mach->tplg_quirk_mask & 1462 (SND_SOC_ACPI_TPLG_INTEL_DMIC_NUMBER | SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER); 1463 codec_amp_name_quirk = mach->tplg_quirk_mask & 1464 (SND_SOC_ACPI_TPLG_INTEL_AMP_NAME | SND_SOC_ACPI_TPLG_INTEL_CODEC_NAME); 1465 1466 if (dmic_ssp_quirk && codec_amp_name_quirk) 1467 return -EINVAL; 1468 1469 return 0; 1470} 1471 1472static char *remove_file_ext(struct device *dev, const char *tplg_filename) 1473{ 1474 char *filename, *tmp; 1475 1476 filename = devm_kstrdup(dev, tplg_filename, GFP_KERNEL); 1477 if (!filename) 1478 return NULL; 1479 1480 /* remove file extension if exist */ 1481 tmp = filename; 1482 return strsep(&tmp, "."); 1483} 1484 1485struct snd_soc_acpi_mach *hda_machine_select(struct snd_sof_dev *sdev) 1486{ 1487 u32 interface_mask = hda_get_interface_mask(sdev); 1488 struct snd_sof_pdata *sof_pdata = sdev->pdata; 1489 const struct sof_dev_desc *desc = sof_pdata->desc; 1490 struct hdac_bus *bus = sof_to_bus(sdev); 1491 struct snd_soc_acpi_mach *mach = NULL; 1492 enum snd_soc_acpi_intel_codec codec_type, amp_type; 1493 const char *tplg_filename; 1494 const char *tplg_suffix; 1495 bool amp_name_valid; 1496 bool i2s_mach_found = false; 1497 bool sdw_mach_found = false; 1498 1499 /* Try I2S or DMIC if it is supported */ 1500 if (interface_mask & (BIT(SOF_DAI_INTEL_SSP) | BIT(SOF_DAI_INTEL_DMIC))) { 1501 mach = snd_soc_acpi_find_machine(desc->machines); 1502 if (mach) 1503 i2s_mach_found = true; 1504 } 1505 1506 /* 1507 * If I2S fails and no external HDaudio codec is detected, 1508 * try SoundWire if it is supported 1509 */ 1510 if (!mach && !HDA_EXT_CODEC(bus->codec_mask) && 1511 (interface_mask & BIT(SOF_DAI_INTEL_ALH))) { 1512 mach = hda_sdw_machine_select(sdev); 1513 if (mach) 1514 sdw_mach_found = true; 1515 } 1516 1517 /* 1518 * Choose HDA generic machine driver if mach is NULL. 1519 * Otherwise, set certain mach params. 1520 */ 1521 hda_generic_machine_select(sdev, &mach); 1522 if (!mach) { 1523 dev_warn(sdev->dev, "warning: No matching ASoC machine driver found\n"); 1524 return NULL; 1525 } 1526 1527 /* report BT offload link mask to machine driver */ 1528 mach->mach_params.bt_link_mask = check_nhlt_ssp_mask(sdev, NHLT_DEVICE_BT); 1529 1530 dev_info(sdev->dev, "BT link detected in NHLT tables: %#x\n", 1531 mach->mach_params.bt_link_mask); 1532 1533 /* allow for module parameter override */ 1534 if (bt_link_mask_override) { 1535 dev_dbg(sdev->dev, "overriding BT link detected in NHLT tables %#x by kernel param %#x\n", 1536 mach->mach_params.bt_link_mask, bt_link_mask_override); 1537 mach->mach_params.bt_link_mask = bt_link_mask_override; 1538 } 1539 1540 if (hweight_long(mach->mach_params.bt_link_mask) > 1) { 1541 dev_warn(sdev->dev, "invalid BT link mask %#x found, reset the mask\n", 1542 mach->mach_params.bt_link_mask); 1543 mach->mach_params.bt_link_mask = 0; 1544 } 1545 1546 /* 1547 * Fixup tplg file name by appending dmic num, ssp num, codec/amplifier 1548 * name string if quirk flag is set. 1549 */ 1550 if (mach) { 1551 const struct sof_intel_dsp_desc *chip = get_chip_info(sdev->pdata); 1552 bool tplg_fixup = false; 1553 bool dmic_fixup = false; 1554 1555 /* 1556 * If tplg file name is overridden, use it instead of 1557 * the one set in mach table 1558 */ 1559 if (!sof_pdata->tplg_filename) { 1560 /* remove file extension if it exists */ 1561 tplg_filename = remove_file_ext(sdev->dev, mach->sof_tplg_filename); 1562 if (!tplg_filename) 1563 return NULL; 1564 1565 sof_pdata->tplg_filename = tplg_filename; 1566 tplg_fixup = true; 1567 } 1568 1569 /* 1570 * Checking quirk mask integrity; some quirk flags could not be 1571 * set concurrently. 1572 */ 1573 if (tplg_fixup && 1574 check_tplg_quirk_mask(mach)) { 1575 dev_err(sdev->dev, "Invalid tplg quirk mask 0x%x\n", 1576 mach->tplg_quirk_mask); 1577 return NULL; 1578 } 1579 1580 /* report to machine driver if any DMICs are found */ 1581 mach->mach_params.dmic_num = check_dmic_num(sdev); 1582 1583 if (sdw_mach_found || mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_DMIC_NUMBER) 1584 dmic_fixup = true; 1585 1586 if (tplg_fixup && 1587 dmic_fixup && 1588 mach->mach_params.dmic_num) { 1589 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL, 1590 "%s%s%d%s", 1591 sof_pdata->tplg_filename, 1592 i2s_mach_found ? "-dmic" : "-", 1593 mach->mach_params.dmic_num, 1594 "ch"); 1595 if (!tplg_filename) 1596 return NULL; 1597 1598 sof_pdata->tplg_filename = tplg_filename; 1599 } 1600 1601 if (tplg_fixup && mach->mach_params.bt_link_mask && 1602 chip->hw_ip_version >= SOF_INTEL_ACE_4_0) { 1603 int bt_port = fls(mach->mach_params.bt_link_mask) - 1; 1604 1605 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL, "%s-ssp%d-bt", 1606 sof_pdata->tplg_filename, bt_port); 1607 if (!tplg_filename) 1608 return NULL; 1609 1610 sof_pdata->tplg_filename = tplg_filename; 1611 } 1612 1613 if (mach->link_mask) { 1614 mach->mach_params.links = mach->links; 1615 mach->mach_params.link_mask = mach->link_mask; 1616 } 1617 1618 /* report SSP link mask to machine driver */ 1619 mach->mach_params.i2s_link_mask = check_nhlt_ssp_mask(sdev, NHLT_DEVICE_I2S); 1620 1621 if (tplg_fixup && 1622 mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER && 1623 mach->mach_params.i2s_link_mask) { 1624 int ssp_num; 1625 int mclk_mask; 1626 1627 if (hweight_long(mach->mach_params.i2s_link_mask) > 1 && 1628 !(mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_MSB)) 1629 dev_warn(sdev->dev, "More than one SSP exposed by NHLT, choosing MSB\n"); 1630 1631 /* fls returns 1-based results, SSPs indices are 0-based */ 1632 ssp_num = fls(mach->mach_params.i2s_link_mask) - 1; 1633 1634 if (ssp_num >= chip->ssp_count) { 1635 dev_err(sdev->dev, "Invalid SSP %d, max on this platform is %d\n", 1636 ssp_num, chip->ssp_count); 1637 return NULL; 1638 } 1639 1640 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL, 1641 "%s%s%d", 1642 sof_pdata->tplg_filename, 1643 "-ssp", 1644 ssp_num); 1645 if (!tplg_filename) 1646 return NULL; 1647 1648 sof_pdata->tplg_filename = tplg_filename; 1649 1650 mclk_mask = check_nhlt_ssp_mclk_mask(sdev, ssp_num); 1651 1652 if (mclk_mask < 0) { 1653 dev_err(sdev->dev, "Invalid MCLK configuration\n"); 1654 return NULL; 1655 } 1656 1657 dev_dbg(sdev->dev, "MCLK mask %#x found in NHLT\n", mclk_mask); 1658 1659 if (mclk_mask) { 1660 dev_info(sdev->dev, "Overriding topology with MCLK mask %#x from NHLT\n", mclk_mask); 1661 sdev->mclk_id_override = true; 1662 sdev->mclk_id_quirk = (mclk_mask & BIT(0)) ? 0 : 1; 1663 } 1664 } 1665 1666 amp_type = snd_soc_acpi_intel_detect_amp_type(sdev->dev); 1667 codec_type = snd_soc_acpi_intel_detect_codec_type(sdev->dev); 1668 amp_name_valid = amp_type != CODEC_NONE && amp_type != codec_type; 1669 1670 if (tplg_fixup && amp_name_valid && 1671 mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_AMP_NAME) { 1672 tplg_suffix = snd_soc_acpi_intel_get_amp_tplg_suffix(amp_type); 1673 if (!tplg_suffix) { 1674 dev_err(sdev->dev, "no tplg suffix found, amp %d\n", 1675 amp_type); 1676 return NULL; 1677 } 1678 1679 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL, 1680 "%s-%s", 1681 sof_pdata->tplg_filename, 1682 tplg_suffix); 1683 if (!tplg_filename) 1684 return NULL; 1685 1686 sof_pdata->tplg_filename = tplg_filename; 1687 } 1688 1689 1690 if (tplg_fixup && 1691 mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_CODEC_NAME && 1692 codec_type != CODEC_NONE) { 1693 tplg_suffix = snd_soc_acpi_intel_get_codec_tplg_suffix(codec_type); 1694 if (!tplg_suffix) { 1695 dev_err(sdev->dev, "no tplg suffix found, codec %d\n", 1696 codec_type); 1697 return NULL; 1698 } 1699 1700 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL, 1701 "%s-%s", 1702 sof_pdata->tplg_filename, 1703 tplg_suffix); 1704 if (!tplg_filename) 1705 return NULL; 1706 1707 sof_pdata->tplg_filename = tplg_filename; 1708 } 1709 1710 if (tplg_fixup) { 1711 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL, 1712 "%s%s", 1713 sof_pdata->tplg_filename, 1714 ".tplg"); 1715 if (!tplg_filename) 1716 return NULL; 1717 1718 sof_pdata->tplg_filename = tplg_filename; 1719 } 1720 1721 /* check if mclk_id should be modified from topology defaults */ 1722 if (mclk_id_override >= 0) { 1723 dev_info(sdev->dev, "Overriding topology with MCLK %d from kernel_parameter\n", mclk_id_override); 1724 sdev->mclk_id_override = true; 1725 sdev->mclk_id_quirk = mclk_id_override; 1726 } 1727 } 1728 1729 return mach; 1730} 1731 1732int hda_pci_intel_probe(struct pci_dev *pci, const struct pci_device_id *pci_id) 1733{ 1734 int ret; 1735 1736 ret = snd_intel_dsp_driver_probe(pci); 1737 if (ret != SND_INTEL_DSP_DRIVER_ANY && ret != SND_INTEL_DSP_DRIVER_SOF) { 1738 dev_dbg(&pci->dev, "SOF PCI driver not selected, aborting probe\n"); 1739 return -ENODEV; 1740 } 1741 1742 return sof_pci_probe(pci, pci_id); 1743} 1744EXPORT_SYMBOL_NS(hda_pci_intel_probe, "SND_SOC_SOF_INTEL_HDA_GENERIC"); 1745 1746int hda_register_clients(struct snd_sof_dev *sdev) 1747{ 1748 return hda_probes_register(sdev); 1749} 1750 1751void hda_unregister_clients(struct snd_sof_dev *sdev) 1752{ 1753 hda_probes_unregister(sdev); 1754} 1755 1756MODULE_LICENSE("Dual BSD/GPL"); 1757MODULE_DESCRIPTION("SOF support for HDaudio platforms"); 1758MODULE_IMPORT_NS("SND_SOC_SOF_PCI_DEV"); 1759MODULE_IMPORT_NS("SND_SOC_SOF_HDA_AUDIO_CODEC"); 1760MODULE_IMPORT_NS("SND_SOC_SOF_HDA_AUDIO_CODEC_I915"); 1761MODULE_IMPORT_NS("SND_SOC_SOF_XTENSA"); 1762MODULE_IMPORT_NS("SND_INTEL_SOUNDWIRE_ACPI"); 1763MODULE_IMPORT_NS("SOUNDWIRE_INTEL_INIT"); 1764MODULE_IMPORT_NS("SOUNDWIRE_INTEL"); 1765MODULE_IMPORT_NS("SND_SOC_SDW_UTILS"); 1766MODULE_IMPORT_NS("SND_SOC_SOF_HDA_MLINK"); 1767MODULE_IMPORT_NS("SND_SOC_SOF_INTEL_HDA_COMMON"); 1768MODULE_IMPORT_NS("SND_SOC_ACPI_INTEL_MATCH");