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