xref: /openbmc/linux/sound/soc/sof/intel/hda.c (revision 25b892b5)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 //
3 // This file is provided under a dual BSD/GPLv2 license.  When using or
4 // redistributing this file, you may do so under either license.
5 //
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
7 //
8 // 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/module.h>
23 #include <linux/soundwire/sdw.h>
24 #include <linux/soundwire/sdw_intel.h>
25 #include <sound/intel-dsp-config.h>
26 #include <sound/intel-nhlt.h>
27 #include <sound/sof.h>
28 #include <sound/sof/xtensa.h>
29 #include "../sof-audio.h"
30 #include "../sof-pci-dev.h"
31 #include "../ops.h"
32 #include "hda.h"
33 
34 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
35 #include <sound/soc-acpi-intel-match.h>
36 #endif
37 
38 /* platform specific devices */
39 #include "shim.h"
40 
41 #define EXCEPT_MAX_HDR_SIZE	0x400
42 #define HDA_EXT_ROM_STATUS_SIZE 8
43 
44 static const struct sof_intel_dsp_desc
45 	*get_chip_info(struct snd_sof_pdata *pdata)
46 {
47 	const struct sof_dev_desc *desc = pdata->desc;
48 	const struct sof_intel_dsp_desc *chip_info;
49 
50 	chip_info = desc->chip_info;
51 
52 	return chip_info;
53 }
54 
55 #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
56 
57 /*
58  * The default for SoundWire clock stop quirks is to power gate the IP
59  * and do a Bus Reset, this will need to be modified when the DSP
60  * needs to remain in D0i3 so that the Master does not lose context
61  * and enumeration is not required on clock restart
62  */
63 static int sdw_clock_stop_quirks = SDW_INTEL_CLK_STOP_BUS_RESET;
64 module_param(sdw_clock_stop_quirks, int, 0444);
65 MODULE_PARM_DESC(sdw_clock_stop_quirks, "SOF SoundWire clock stop quirks");
66 
67 static int sdw_params_stream(struct device *dev,
68 			     struct sdw_intel_stream_params_data *params_data)
69 {
70 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
71 	struct snd_soc_dai *d = params_data->dai;
72 	struct sof_ipc_dai_config config;
73 	struct sof_ipc_reply reply;
74 	int link_id = params_data->link_id;
75 	int alh_stream_id = params_data->alh_stream_id;
76 	int ret;
77 	u32 size = sizeof(config);
78 
79 	memset(&config, 0, size);
80 	config.hdr.size = size;
81 	config.hdr.cmd = SOF_IPC_GLB_DAI_MSG | SOF_IPC_DAI_CONFIG;
82 	config.type = SOF_DAI_INTEL_ALH;
83 	config.dai_index = (link_id << 8) | (d->id);
84 	config.alh.stream_id = alh_stream_id;
85 
86 	/* send message to DSP */
87 	ret = sof_ipc_tx_message(sdev->ipc,
88 				 config.hdr.cmd, &config, size, &reply,
89 				 sizeof(reply));
90 	if (ret < 0) {
91 		dev_err(sdev->dev,
92 			"error: failed to set DAI hw_params for link %d dai->id %d ALH %d\n",
93 			link_id, d->id, alh_stream_id);
94 	}
95 
96 	return ret;
97 }
98 
99 static int sdw_free_stream(struct device *dev,
100 			   struct sdw_intel_stream_free_data *free_data)
101 {
102 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
103 	struct snd_soc_dai *d = free_data->dai;
104 	struct sof_ipc_dai_config config;
105 	struct sof_ipc_reply reply;
106 	int link_id = free_data->link_id;
107 	int ret;
108 	u32 size = sizeof(config);
109 
110 	memset(&config, 0, size);
111 	config.hdr.size = size;
112 	config.hdr.cmd = SOF_IPC_GLB_DAI_MSG | SOF_IPC_DAI_CONFIG;
113 	config.type = SOF_DAI_INTEL_ALH;
114 	config.dai_index = (link_id << 8) | d->id;
115 	config.alh.stream_id = 0xFFFF; /* invalid value on purpose */
116 
117 	/* send message to DSP */
118 	ret = sof_ipc_tx_message(sdev->ipc,
119 				 config.hdr.cmd, &config, size, &reply,
120 				 sizeof(reply));
121 	if (ret < 0) {
122 		dev_err(sdev->dev,
123 			"error: failed to free stream for link %d dai->id %d\n",
124 			link_id, d->id);
125 	}
126 
127 	return ret;
128 }
129 
130 static const struct sdw_intel_ops sdw_callback = {
131 	.params_stream = sdw_params_stream,
132 	.free_stream = sdw_free_stream,
133 };
134 
135 void hda_sdw_int_enable(struct snd_sof_dev *sdev, bool enable)
136 {
137 	sdw_intel_enable_irq(sdev->bar[HDA_DSP_BAR], enable);
138 }
139 
140 static int hda_sdw_acpi_scan(struct snd_sof_dev *sdev)
141 {
142 	struct sof_intel_hda_dev *hdev;
143 	acpi_handle handle;
144 	int ret;
145 
146 	handle = ACPI_HANDLE(sdev->dev);
147 
148 	/* save ACPI info for the probe step */
149 	hdev = sdev->pdata->hw_pdata;
150 
151 	ret = sdw_intel_acpi_scan(handle, &hdev->info);
152 	if (ret < 0)
153 		return -EINVAL;
154 
155 	return 0;
156 }
157 
158 static int hda_sdw_probe(struct snd_sof_dev *sdev)
159 {
160 	struct sof_intel_hda_dev *hdev;
161 	struct sdw_intel_res res;
162 	void *sdw;
163 
164 	hdev = sdev->pdata->hw_pdata;
165 
166 	memset(&res, 0, sizeof(res));
167 
168 	res.mmio_base = sdev->bar[HDA_DSP_BAR];
169 	res.shim_base = hdev->desc->sdw_shim_base;
170 	res.alh_base = hdev->desc->sdw_alh_base;
171 	res.irq = sdev->ipc_irq;
172 	res.handle = hdev->info.handle;
173 	res.parent = sdev->dev;
174 	res.ops = &sdw_callback;
175 	res.dev = sdev->dev;
176 	res.clock_stop_quirks = sdw_clock_stop_quirks;
177 
178 	/*
179 	 * ops and arg fields are not populated for now,
180 	 * they will be needed when the DAI callbacks are
181 	 * provided
182 	 */
183 
184 	/* we could filter links here if needed, e.g for quirks */
185 	res.count = hdev->info.count;
186 	res.link_mask = hdev->info.link_mask;
187 
188 	sdw = sdw_intel_probe(&res);
189 	if (!sdw) {
190 		dev_err(sdev->dev, "error: SoundWire probe failed\n");
191 		return -EINVAL;
192 	}
193 
194 	/* save context */
195 	hdev->sdw = sdw;
196 
197 	return 0;
198 }
199 
200 int hda_sdw_startup(struct snd_sof_dev *sdev)
201 {
202 	struct sof_intel_hda_dev *hdev;
203 	struct snd_sof_pdata *pdata = sdev->pdata;
204 
205 	hdev = sdev->pdata->hw_pdata;
206 
207 	if (!hdev->sdw)
208 		return 0;
209 
210 	if (pdata->machine && !pdata->machine->mach_params.link_mask)
211 		return 0;
212 
213 	return sdw_intel_startup(hdev->sdw);
214 }
215 
216 static int hda_sdw_exit(struct snd_sof_dev *sdev)
217 {
218 	struct sof_intel_hda_dev *hdev;
219 
220 	hdev = sdev->pdata->hw_pdata;
221 
222 	hda_sdw_int_enable(sdev, false);
223 
224 	if (hdev->sdw)
225 		sdw_intel_exit(hdev->sdw);
226 	hdev->sdw = NULL;
227 
228 	return 0;
229 }
230 
231 bool hda_common_check_sdw_irq(struct snd_sof_dev *sdev)
232 {
233 	struct sof_intel_hda_dev *hdev;
234 	bool ret = false;
235 	u32 irq_status;
236 
237 	hdev = sdev->pdata->hw_pdata;
238 
239 	if (!hdev->sdw)
240 		return ret;
241 
242 	/* store status */
243 	irq_status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS2);
244 
245 	/* invalid message ? */
246 	if (irq_status == 0xffffffff)
247 		goto out;
248 
249 	/* SDW message ? */
250 	if (irq_status & HDA_DSP_REG_ADSPIS2_SNDW)
251 		ret = true;
252 
253 out:
254 	return ret;
255 }
256 
257 static bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev)
258 {
259 	const struct sof_intel_dsp_desc *chip;
260 
261 	chip = get_chip_info(sdev->pdata);
262 	if (chip && chip->check_sdw_irq)
263 		return chip->check_sdw_irq(sdev);
264 
265 	return false;
266 }
267 
268 static irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
269 {
270 	return sdw_intel_thread(irq, context);
271 }
272 
273 static bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev)
274 {
275 	struct sof_intel_hda_dev *hdev;
276 
277 	hdev = sdev->pdata->hw_pdata;
278 	if (hdev->sdw &&
279 	    snd_sof_dsp_read(sdev, HDA_DSP_BAR,
280 			     hdev->desc->sdw_shim_base + SDW_SHIM_WAKESTS))
281 		return true;
282 
283 	return false;
284 }
285 
286 void hda_sdw_process_wakeen(struct snd_sof_dev *sdev)
287 {
288 	struct sof_intel_hda_dev *hdev;
289 
290 	hdev = sdev->pdata->hw_pdata;
291 	if (!hdev->sdw)
292 		return;
293 
294 	sdw_intel_process_wakeen_event(hdev->sdw);
295 }
296 
297 #endif
298 
299 /*
300  * Debug
301  */
302 
303 struct hda_dsp_msg_code {
304 	u32 code;
305 	const char *msg;
306 };
307 
308 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG)
309 static bool hda_use_msi = true;
310 module_param_named(use_msi, hda_use_msi, bool, 0444);
311 MODULE_PARM_DESC(use_msi, "SOF HDA use PCI MSI mode");
312 #else
313 #define hda_use_msi	(1)
314 #endif
315 
316 static char *hda_model;
317 module_param(hda_model, charp, 0444);
318 MODULE_PARM_DESC(hda_model, "Use the given HDA board model.");
319 
320 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA) || IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
321 static int hda_dmic_num = -1;
322 module_param_named(dmic_num, hda_dmic_num, int, 0444);
323 MODULE_PARM_DESC(dmic_num, "SOF HDA DMIC number");
324 #endif
325 
326 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
327 static bool hda_codec_use_common_hdmi = IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI);
328 module_param_named(use_common_hdmi, hda_codec_use_common_hdmi, bool, 0444);
329 MODULE_PARM_DESC(use_common_hdmi, "SOF HDA use common HDMI codec driver");
330 #endif
331 
332 static const struct hda_dsp_msg_code hda_dsp_rom_msg[] = {
333 	{HDA_DSP_ROM_FW_MANIFEST_LOADED, "status: manifest loaded"},
334 	{HDA_DSP_ROM_FW_FW_LOADED, "status: fw loaded"},
335 	{HDA_DSP_ROM_FW_ENTERED, "status: fw entered"},
336 	{HDA_DSP_ROM_CSE_ERROR, "error: cse error"},
337 	{HDA_DSP_ROM_CSE_WRONG_RESPONSE, "error: cse wrong response"},
338 	{HDA_DSP_ROM_IMR_TO_SMALL, "error: IMR too small"},
339 	{HDA_DSP_ROM_BASE_FW_NOT_FOUND, "error: base fw not found"},
340 	{HDA_DSP_ROM_CSE_VALIDATION_FAILED, "error: signature verification failed"},
341 	{HDA_DSP_ROM_IPC_FATAL_ERROR, "error: ipc fatal error"},
342 	{HDA_DSP_ROM_L2_CACHE_ERROR, "error: L2 cache error"},
343 	{HDA_DSP_ROM_LOAD_OFFSET_TO_SMALL, "error: load offset too small"},
344 	{HDA_DSP_ROM_API_PTR_INVALID, "error: API ptr invalid"},
345 	{HDA_DSP_ROM_BASEFW_INCOMPAT, "error: base fw incompatible"},
346 	{HDA_DSP_ROM_UNHANDLED_INTERRUPT, "error: unhandled interrupt"},
347 	{HDA_DSP_ROM_MEMORY_HOLE_ECC, "error: ECC memory hole"},
348 	{HDA_DSP_ROM_KERNEL_EXCEPTION, "error: kernel exception"},
349 	{HDA_DSP_ROM_USER_EXCEPTION, "error: user exception"},
350 	{HDA_DSP_ROM_UNEXPECTED_RESET, "error: unexpected reset"},
351 	{HDA_DSP_ROM_NULL_FW_ENTRY,	"error: null FW entry point"},
352 };
353 
354 static void hda_dsp_get_status(struct snd_sof_dev *sdev)
355 {
356 	u32 status;
357 	int i;
358 
359 	status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
360 				  HDA_DSP_SRAM_REG_ROM_STATUS);
361 
362 	for (i = 0; i < ARRAY_SIZE(hda_dsp_rom_msg); i++) {
363 		if (status == hda_dsp_rom_msg[i].code) {
364 			dev_err(sdev->dev, "%s - code %8.8x\n",
365 				hda_dsp_rom_msg[i].msg, status);
366 			return;
367 		}
368 	}
369 
370 	/* not for us, must be generic sof message */
371 	dev_dbg(sdev->dev, "unknown ROM status value %8.8x\n", status);
372 }
373 
374 static void hda_dsp_get_registers(struct snd_sof_dev *sdev,
375 				  struct sof_ipc_dsp_oops_xtensa *xoops,
376 				  struct sof_ipc_panic_info *panic_info,
377 				  u32 *stack, size_t stack_words)
378 {
379 	u32 offset = sdev->dsp_oops_offset;
380 
381 	/* first read registers */
382 	sof_mailbox_read(sdev, offset, xoops, sizeof(*xoops));
383 
384 	/* note: variable AR register array is not read */
385 
386 	/* then get panic info */
387 	if (xoops->arch_hdr.totalsize > EXCEPT_MAX_HDR_SIZE) {
388 		dev_err(sdev->dev, "invalid header size 0x%x. FW oops is bogus\n",
389 			xoops->arch_hdr.totalsize);
390 		return;
391 	}
392 	offset += xoops->arch_hdr.totalsize;
393 	sof_block_read(sdev, sdev->mmio_bar, offset,
394 		       panic_info, sizeof(*panic_info));
395 
396 	/* then get the stack */
397 	offset += sizeof(*panic_info);
398 	sof_block_read(sdev, sdev->mmio_bar, offset, stack,
399 		       stack_words * sizeof(u32));
400 }
401 
402 /* dump the first 8 dwords representing the extended ROM status */
403 static void hda_dsp_dump_ext_rom_status(struct snd_sof_dev *sdev, u32 flags)
404 {
405 	char msg[128];
406 	int len = 0;
407 	u32 value;
408 	int i;
409 
410 	for (i = 0; i < HDA_EXT_ROM_STATUS_SIZE; i++) {
411 		value = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_ROM_STATUS + i * 0x4);
412 		len += snprintf(msg + len, sizeof(msg) - len, " 0x%x", value);
413 	}
414 
415 	sof_dev_dbg_or_err(sdev->dev, flags & SOF_DBG_DUMP_FORCE_ERR_LEVEL,
416 			   "extended rom status: %s", msg);
417 
418 }
419 
420 void hda_dsp_dump(struct snd_sof_dev *sdev, u32 flags)
421 {
422 	struct sof_ipc_dsp_oops_xtensa xoops;
423 	struct sof_ipc_panic_info panic_info;
424 	u32 stack[HDA_DSP_STACK_DUMP_SIZE];
425 
426 	/* print ROM/FW status */
427 	hda_dsp_get_status(sdev);
428 
429 	/* print panic info if FW boot is complete. Otherwise, print the extended ROM status */
430 	if (sdev->fw_state == SOF_FW_BOOT_COMPLETE) {
431 		u32 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_FW_STATUS);
432 		u32 panic = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_FW_TRACEP);
433 
434 		hda_dsp_get_registers(sdev, &xoops, &panic_info, stack,
435 				      HDA_DSP_STACK_DUMP_SIZE);
436 		snd_sof_get_status(sdev, status, panic, &xoops, &panic_info,
437 				   stack, HDA_DSP_STACK_DUMP_SIZE);
438 	} else {
439 		hda_dsp_dump_ext_rom_status(sdev, flags);
440 	}
441 }
442 
443 void hda_ipc_irq_dump(struct snd_sof_dev *sdev)
444 {
445 	struct hdac_bus *bus = sof_to_bus(sdev);
446 	u32 adspis;
447 	u32 intsts;
448 	u32 intctl;
449 	u32 ppsts;
450 	u8 rirbsts;
451 
452 	/* read key IRQ stats and config registers */
453 	adspis = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS);
454 	intsts = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS);
455 	intctl = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL);
456 	ppsts = snd_sof_dsp_read(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPSTS);
457 	rirbsts = snd_hdac_chip_readb(bus, RIRBSTS);
458 
459 	dev_err(sdev->dev,
460 		"error: hda irq intsts 0x%8.8x intlctl 0x%8.8x rirb %2.2x\n",
461 		intsts, intctl, rirbsts);
462 	dev_err(sdev->dev,
463 		"error: dsp irq ppsts 0x%8.8x adspis 0x%8.8x\n",
464 		ppsts, adspis);
465 }
466 
467 void hda_ipc_dump(struct snd_sof_dev *sdev)
468 {
469 	u32 hipcie;
470 	u32 hipct;
471 	u32 hipcctl;
472 
473 	hda_ipc_irq_dump(sdev);
474 
475 	/* read IPC status */
476 	hipcie = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCIE);
477 	hipct = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCT);
478 	hipcctl = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCCTL);
479 
480 	/* dump the IPC regs */
481 	/* TODO: parse the raw msg */
482 	dev_err(sdev->dev,
483 		"error: host status 0x%8.8x dsp status 0x%8.8x mask 0x%8.8x\n",
484 		hipcie, hipct, hipcctl);
485 }
486 
487 static int hda_init(struct snd_sof_dev *sdev)
488 {
489 	struct hda_bus *hbus;
490 	struct hdac_bus *bus;
491 	struct pci_dev *pci = to_pci_dev(sdev->dev);
492 	int ret;
493 
494 	hbus = sof_to_hbus(sdev);
495 	bus = sof_to_bus(sdev);
496 
497 	/* HDA bus init */
498 	sof_hda_bus_init(bus, &pci->dev);
499 
500 	bus->use_posbuf = 1;
501 	bus->bdl_pos_adj = 0;
502 	bus->sync_write = 1;
503 
504 	mutex_init(&hbus->prepare_mutex);
505 	hbus->pci = pci;
506 	hbus->mixer_assigned = -1;
507 	hbus->modelname = hda_model;
508 
509 	/* initialise hdac bus */
510 	bus->addr = pci_resource_start(pci, 0);
511 	bus->remap_addr = pci_ioremap_bar(pci, 0);
512 	if (!bus->remap_addr) {
513 		dev_err(bus->dev, "error: ioremap error\n");
514 		return -ENXIO;
515 	}
516 
517 	/* HDA base */
518 	sdev->bar[HDA_DSP_HDA_BAR] = bus->remap_addr;
519 
520 	/* init i915 and HDMI codecs */
521 	ret = hda_codec_i915_init(sdev);
522 	if (ret < 0)
523 		dev_warn(sdev->dev, "init of i915 and HDMI codec failed\n");
524 
525 	/* get controller capabilities */
526 	ret = hda_dsp_ctrl_get_caps(sdev);
527 	if (ret < 0)
528 		dev_err(sdev->dev, "error: get caps error\n");
529 
530 	return ret;
531 }
532 
533 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA) || IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
534 
535 static int check_nhlt_dmic(struct snd_sof_dev *sdev)
536 {
537 	struct nhlt_acpi_table *nhlt;
538 	int dmic_num;
539 
540 	nhlt = intel_nhlt_init(sdev->dev);
541 	if (nhlt) {
542 		dmic_num = intel_nhlt_get_dmic_geo(sdev->dev, nhlt);
543 		intel_nhlt_free(nhlt);
544 		if (dmic_num >= 1 && dmic_num <= 4)
545 			return dmic_num;
546 	}
547 
548 	return 0;
549 }
550 
551 static const char *fixup_tplg_name(struct snd_sof_dev *sdev,
552 				   const char *sof_tplg_filename,
553 				   const char *idisp_str,
554 				   const char *dmic_str)
555 {
556 	const char *tplg_filename = NULL;
557 	char *filename, *tmp;
558 	const char *split_ext;
559 
560 	filename = kstrdup(sof_tplg_filename, GFP_KERNEL);
561 	if (!filename)
562 		return NULL;
563 
564 	/* this assumes a .tplg extension */
565 	tmp = filename;
566 	split_ext = strsep(&tmp, ".");
567 	if (split_ext)
568 		tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
569 					       "%s%s%s.tplg",
570 					       split_ext, idisp_str, dmic_str);
571 	kfree(filename);
572 
573 	return tplg_filename;
574 }
575 
576 static int dmic_topology_fixup(struct snd_sof_dev *sdev,
577 			       const char **tplg_filename,
578 			       const char *idisp_str,
579 			       int *dmic_found)
580 {
581 	const char *default_tplg_filename = *tplg_filename;
582 	const char *fixed_tplg_filename;
583 	const char *dmic_str;
584 	int dmic_num;
585 
586 	/* first check NHLT for DMICs */
587 	dmic_num = check_nhlt_dmic(sdev);
588 
589 	/* allow for module parameter override */
590 	if (hda_dmic_num != -1) {
591 		dev_dbg(sdev->dev,
592 			"overriding DMICs detected in NHLT tables %d by kernel param %d\n",
593 			dmic_num, hda_dmic_num);
594 		dmic_num = hda_dmic_num;
595 	}
596 
597 	switch (dmic_num) {
598 	case 1:
599 		dmic_str = "-1ch";
600 		break;
601 	case 2:
602 		dmic_str = "-2ch";
603 		break;
604 	case 3:
605 		dmic_str = "-3ch";
606 		break;
607 	case 4:
608 		dmic_str = "-4ch";
609 		break;
610 	default:
611 		dmic_num = 0;
612 		dmic_str = "";
613 		break;
614 	}
615 
616 	fixed_tplg_filename = fixup_tplg_name(sdev, default_tplg_filename,
617 					      idisp_str, dmic_str);
618 	if (!fixed_tplg_filename)
619 		return -ENOMEM;
620 
621 	dev_info(sdev->dev, "DMICs detected in NHLT tables: %d\n", dmic_num);
622 	*dmic_found = dmic_num;
623 	*tplg_filename = fixed_tplg_filename;
624 
625 	return 0;
626 }
627 #endif
628 
629 static int hda_init_caps(struct snd_sof_dev *sdev)
630 {
631 	struct hdac_bus *bus = sof_to_bus(sdev);
632 	struct snd_sof_pdata *pdata = sdev->pdata;
633 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
634 	struct hdac_ext_link *hlink;
635 #endif
636 	struct sof_intel_hda_dev *hdev = pdata->hw_pdata;
637 	u32 link_mask;
638 	int ret = 0;
639 
640 	/* check if dsp is there */
641 	if (bus->ppcap)
642 		dev_dbg(sdev->dev, "PP capability, will probe DSP later.\n");
643 
644 	/* Init HDA controller after i915 init */
645 	ret = hda_dsp_ctrl_init_chip(sdev, true);
646 	if (ret < 0) {
647 		dev_err(bus->dev, "error: init chip failed with ret: %d\n",
648 			ret);
649 		return ret;
650 	}
651 
652 	/* scan SoundWire capabilities exposed by DSDT */
653 	ret = hda_sdw_acpi_scan(sdev);
654 	if (ret < 0) {
655 		dev_dbg(sdev->dev, "skipping SoundWire, not detected with ACPI scan\n");
656 		goto skip_soundwire;
657 	}
658 
659 	link_mask = hdev->info.link_mask;
660 	if (!link_mask) {
661 		dev_dbg(sdev->dev, "skipping SoundWire, no links enabled\n");
662 		goto skip_soundwire;
663 	}
664 
665 	/*
666 	 * probe/allocate SoundWire resources.
667 	 * The hardware configuration takes place in hda_sdw_startup
668 	 * after power rails are enabled.
669 	 * It's entirely possible to have a mix of I2S/DMIC/SoundWire
670 	 * devices, so we allocate the resources in all cases.
671 	 */
672 	ret = hda_sdw_probe(sdev);
673 	if (ret < 0) {
674 		dev_err(sdev->dev, "error: SoundWire probe error\n");
675 		return ret;
676 	}
677 
678 skip_soundwire:
679 
680 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
681 	if (bus->mlcap)
682 		snd_hdac_ext_bus_get_ml_capabilities(bus);
683 
684 	/* create codec instances */
685 	hda_codec_probe_bus(sdev, hda_codec_use_common_hdmi);
686 
687 	if (!HDA_IDISP_CODEC(bus->codec_mask))
688 		hda_codec_i915_display_power(sdev, false);
689 
690 	/*
691 	 * we are done probing so decrement link counts
692 	 */
693 	list_for_each_entry(hlink, &bus->hlink_list, list)
694 		snd_hdac_ext_bus_link_put(bus, hlink);
695 #endif
696 	return 0;
697 }
698 
699 static irqreturn_t hda_dsp_interrupt_handler(int irq, void *context)
700 {
701 	struct snd_sof_dev *sdev = context;
702 
703 	/*
704 	 * Get global interrupt status. It includes all hardware interrupt
705 	 * sources in the Intel HD Audio controller.
706 	 */
707 	if (snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS) &
708 	    SOF_HDA_INTSTS_GIS) {
709 
710 		/* disable GIE interrupt */
711 		snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
712 					SOF_HDA_INTCTL,
713 					SOF_HDA_INT_GLOBAL_EN,
714 					0);
715 
716 		return IRQ_WAKE_THREAD;
717 	}
718 
719 	return IRQ_NONE;
720 }
721 
722 static irqreturn_t hda_dsp_interrupt_thread(int irq, void *context)
723 {
724 	struct snd_sof_dev *sdev = context;
725 	struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
726 
727 	/* deal with streams and controller first */
728 	if (hda_dsp_check_stream_irq(sdev))
729 		hda_dsp_stream_threaded_handler(irq, sdev);
730 
731 	if (hda_dsp_check_ipc_irq(sdev))
732 		sof_ops(sdev)->irq_thread(irq, sdev);
733 
734 	if (hda_dsp_check_sdw_irq(sdev))
735 		hda_dsp_sdw_thread(irq, hdev->sdw);
736 
737 	if (hda_sdw_check_wakeen_irq(sdev))
738 		hda_sdw_process_wakeen(sdev);
739 
740 	/* enable GIE interrupt */
741 	snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
742 				SOF_HDA_INTCTL,
743 				SOF_HDA_INT_GLOBAL_EN,
744 				SOF_HDA_INT_GLOBAL_EN);
745 
746 	return IRQ_HANDLED;
747 }
748 
749 int hda_dsp_probe(struct snd_sof_dev *sdev)
750 {
751 	struct pci_dev *pci = to_pci_dev(sdev->dev);
752 	struct sof_intel_hda_dev *hdev;
753 	struct hdac_bus *bus;
754 	const struct sof_intel_dsp_desc *chip;
755 	int ret = 0;
756 
757 	/*
758 	 * detect DSP by checking class/subclass/prog-id information
759 	 * class=04 subclass 03 prog-if 00: no DSP, legacy driver is required
760 	 * class=04 subclass 01 prog-if 00: DSP is present
761 	 *   (and may be required e.g. for DMIC or SSP support)
762 	 * class=04 subclass 03 prog-if 80: either of DSP or legacy mode works
763 	 */
764 	if (pci->class == 0x040300) {
765 		dev_err(sdev->dev, "error: the DSP is not enabled on this platform, aborting probe\n");
766 		return -ENODEV;
767 	} else if (pci->class != 0x040100 && pci->class != 0x040380) {
768 		dev_err(sdev->dev, "error: unknown PCI class/subclass/prog-if 0x%06x found, aborting probe\n", pci->class);
769 		return -ENODEV;
770 	}
771 	dev_info(sdev->dev, "DSP detected with PCI class/subclass/prog-if 0x%06x\n", pci->class);
772 
773 	chip = get_chip_info(sdev->pdata);
774 	if (!chip) {
775 		dev_err(sdev->dev, "error: no such device supported, chip id:%x\n",
776 			pci->device);
777 		ret = -EIO;
778 		goto err;
779 	}
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 
787 	hdev->dmic_dev = platform_device_register_data(sdev->dev, "dmic-codec",
788 						       PLATFORM_DEVID_NONE,
789 						       NULL, 0);
790 	if (IS_ERR(hdev->dmic_dev)) {
791 		dev_err(sdev->dev, "error: failed to create DMIC device\n");
792 		return PTR_ERR(hdev->dmic_dev);
793 	}
794 
795 	/*
796 	 * use position update IPC if either it is forced
797 	 * or we don't have other choice
798 	 */
799 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_FORCE_IPC_POSITION)
800 	hdev->no_ipc_position = 0;
801 #else
802 	hdev->no_ipc_position = sof_ops(sdev)->pcm_pointer ? 1 : 0;
803 #endif
804 
805 	/* set up HDA base */
806 	bus = sof_to_bus(sdev);
807 	ret = hda_init(sdev);
808 	if (ret < 0)
809 		goto hdac_bus_unmap;
810 
811 	/* DSP base */
812 	sdev->bar[HDA_DSP_BAR] = pci_ioremap_bar(pci, HDA_DSP_BAR);
813 	if (!sdev->bar[HDA_DSP_BAR]) {
814 		dev_err(sdev->dev, "error: ioremap error\n");
815 		ret = -ENXIO;
816 		goto hdac_bus_unmap;
817 	}
818 
819 	sdev->mmio_bar = HDA_DSP_BAR;
820 	sdev->mailbox_bar = HDA_DSP_BAR;
821 
822 	/* allow 64bit DMA address if supported by H/W */
823 	if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(64))) {
824 		dev_dbg(sdev->dev, "DMA mask is 32 bit\n");
825 		dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32));
826 	}
827 
828 	/* init streams */
829 	ret = hda_dsp_stream_init(sdev);
830 	if (ret < 0) {
831 		dev_err(sdev->dev, "error: failed to init streams\n");
832 		/*
833 		 * not all errors are due to memory issues, but trying
834 		 * to free everything does not harm
835 		 */
836 		goto free_streams;
837 	}
838 
839 	/*
840 	 * register our IRQ
841 	 * let's try to enable msi firstly
842 	 * if it fails, use legacy interrupt mode
843 	 * TODO: support msi multiple vectors
844 	 */
845 	if (hda_use_msi && pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI) > 0) {
846 		dev_info(sdev->dev, "use msi interrupt mode\n");
847 		sdev->ipc_irq = pci_irq_vector(pci, 0);
848 		/* initialised to "false" by kzalloc() */
849 		sdev->msi_enabled = true;
850 	}
851 
852 	if (!sdev->msi_enabled) {
853 		dev_info(sdev->dev, "use legacy interrupt mode\n");
854 		/*
855 		 * in IO-APIC mode, hda->irq and ipc_irq are using the same
856 		 * irq number of pci->irq
857 		 */
858 		sdev->ipc_irq = pci->irq;
859 	}
860 
861 	dev_dbg(sdev->dev, "using IPC IRQ %d\n", sdev->ipc_irq);
862 	ret = request_threaded_irq(sdev->ipc_irq, hda_dsp_interrupt_handler,
863 				   hda_dsp_interrupt_thread,
864 				   IRQF_SHARED, "AudioDSP", sdev);
865 	if (ret < 0) {
866 		dev_err(sdev->dev, "error: failed to register IPC IRQ %d\n",
867 			sdev->ipc_irq);
868 		goto free_irq_vector;
869 	}
870 
871 	pci_set_master(pci);
872 	synchronize_irq(pci->irq);
873 
874 	/*
875 	 * clear TCSEL to clear playback on some HD Audio
876 	 * codecs. PCI TCSEL is defined in the Intel manuals.
877 	 */
878 	snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
879 
880 	/* init HDA capabilities */
881 	ret = hda_init_caps(sdev);
882 	if (ret < 0)
883 		goto free_ipc_irq;
884 
885 	/* enable ppcap interrupt */
886 	hda_dsp_ctrl_ppcap_enable(sdev, true);
887 	hda_dsp_ctrl_ppcap_int_enable(sdev, true);
888 
889 	/* set default mailbox offset for FW ready message */
890 	sdev->dsp_box.offset = HDA_DSP_MBOX_UPLINK_OFFSET;
891 
892 	INIT_DELAYED_WORK(&hdev->d0i3_work, hda_dsp_d0i3_work);
893 
894 	return 0;
895 
896 free_ipc_irq:
897 	free_irq(sdev->ipc_irq, sdev);
898 free_irq_vector:
899 	if (sdev->msi_enabled)
900 		pci_free_irq_vectors(pci);
901 free_streams:
902 	hda_dsp_stream_free(sdev);
903 /* dsp_unmap: not currently used */
904 	iounmap(sdev->bar[HDA_DSP_BAR]);
905 hdac_bus_unmap:
906 	platform_device_unregister(hdev->dmic_dev);
907 	iounmap(bus->remap_addr);
908 	hda_codec_i915_exit(sdev);
909 err:
910 	return ret;
911 }
912 
913 int hda_dsp_remove(struct snd_sof_dev *sdev)
914 {
915 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
916 	struct hdac_bus *bus = sof_to_bus(sdev);
917 	struct pci_dev *pci = to_pci_dev(sdev->dev);
918 	const struct sof_intel_dsp_desc *chip = hda->desc;
919 
920 	/* cancel any attempt for DSP D0I3 */
921 	cancel_delayed_work_sync(&hda->d0i3_work);
922 
923 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
924 	/* codec removal, invoke bus_device_remove */
925 	snd_hdac_ext_bus_device_remove(bus);
926 #endif
927 
928 	hda_sdw_exit(sdev);
929 
930 	if (!IS_ERR_OR_NULL(hda->dmic_dev))
931 		platform_device_unregister(hda->dmic_dev);
932 
933 	/* disable DSP IRQ */
934 	snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
935 				SOF_HDA_PPCTL_PIE, 0);
936 
937 	/* disable CIE and GIE interrupts */
938 	snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
939 				SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN, 0);
940 
941 	/* disable cores */
942 	if (chip)
943 		snd_sof_dsp_core_power_down(sdev, chip->host_managed_cores_mask);
944 
945 	/* disable DSP */
946 	snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
947 				SOF_HDA_PPCTL_GPROCEN, 0);
948 
949 	free_irq(sdev->ipc_irq, sdev);
950 	if (sdev->msi_enabled)
951 		pci_free_irq_vectors(pci);
952 
953 	hda_dsp_stream_free(sdev);
954 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
955 	snd_hdac_link_free_all(bus);
956 #endif
957 
958 	iounmap(sdev->bar[HDA_DSP_BAR]);
959 	iounmap(bus->remap_addr);
960 
961 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
962 	snd_hdac_ext_bus_exit(bus);
963 #endif
964 	hda_codec_i915_exit(sdev);
965 
966 	return 0;
967 }
968 
969 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
970 static int hda_generic_machine_select(struct snd_sof_dev *sdev)
971 {
972 	struct hdac_bus *bus = sof_to_bus(sdev);
973 	struct snd_soc_acpi_mach_params *mach_params;
974 	struct snd_soc_acpi_mach *hda_mach;
975 	struct snd_sof_pdata *pdata = sdev->pdata;
976 	const char *tplg_filename;
977 	const char *idisp_str;
978 	int dmic_num = 0;
979 	int codec_num = 0;
980 	int ret;
981 	int i;
982 
983 	/* codec detection */
984 	if (!bus->codec_mask) {
985 		dev_info(bus->dev, "no hda codecs found!\n");
986 	} else {
987 		dev_info(bus->dev, "hda codecs found, mask %lx\n",
988 			 bus->codec_mask);
989 
990 		for (i = 0; i < HDA_MAX_CODECS; i++) {
991 			if (bus->codec_mask & (1 << i))
992 				codec_num++;
993 		}
994 
995 		/*
996 		 * If no machine driver is found, then:
997 		 *
998 		 * generic hda machine driver can handle:
999 		 *  - one HDMI codec, and/or
1000 		 *  - one external HDAudio codec
1001 		 */
1002 		if (!pdata->machine && codec_num <= 2) {
1003 			hda_mach = snd_soc_acpi_intel_hda_machines;
1004 
1005 			dev_info(bus->dev, "using HDA machine driver %s now\n",
1006 				 hda_mach->drv_name);
1007 
1008 			if (codec_num == 1 && HDA_IDISP_CODEC(bus->codec_mask))
1009 				idisp_str = "-idisp";
1010 			else
1011 				idisp_str = "";
1012 
1013 			/* topology: use the info from hda_machines */
1014 			tplg_filename = hda_mach->sof_tplg_filename;
1015 			ret = dmic_topology_fixup(sdev, &tplg_filename, idisp_str, &dmic_num);
1016 			if (ret < 0)
1017 				return ret;
1018 
1019 			hda_mach->mach_params.dmic_num = dmic_num;
1020 			pdata->machine = hda_mach;
1021 			pdata->tplg_filename = tplg_filename;
1022 
1023 			if (codec_num == 2) {
1024 				/*
1025 				 * Prevent SoundWire links from starting when an external
1026 				 * HDaudio codec is used
1027 				 */
1028 				hda_mach->mach_params.link_mask = 0;
1029 			}
1030 		}
1031 	}
1032 
1033 	/* used by hda machine driver to create dai links */
1034 	if (pdata->machine) {
1035 		mach_params = (struct snd_soc_acpi_mach_params *)
1036 			&pdata->machine->mach_params;
1037 		mach_params->codec_mask = bus->codec_mask;
1038 		mach_params->common_hdmi_codec_drv = hda_codec_use_common_hdmi;
1039 	}
1040 
1041 	return 0;
1042 }
1043 #else
1044 static int hda_generic_machine_select(struct snd_sof_dev *sdev)
1045 {
1046 	return 0;
1047 }
1048 #endif
1049 
1050 #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
1051 /* Check if all Slaves defined on the link can be found */
1052 static bool link_slaves_found(struct snd_sof_dev *sdev,
1053 			      const struct snd_soc_acpi_link_adr *link,
1054 			      struct sdw_intel_ctx *sdw)
1055 {
1056 	struct hdac_bus *bus = sof_to_bus(sdev);
1057 	struct sdw_intel_slave_id *ids = sdw->ids;
1058 	int num_slaves = sdw->num_slaves;
1059 	unsigned int part_id, link_id, unique_id, mfg_id;
1060 	int i, j, k;
1061 
1062 	for (i = 0; i < link->num_adr; i++) {
1063 		u64 adr = link->adr_d[i].adr;
1064 		int reported_part_count = 0;
1065 
1066 		mfg_id = SDW_MFG_ID(adr);
1067 		part_id = SDW_PART_ID(adr);
1068 		link_id = SDW_DISCO_LINK_ID(adr);
1069 
1070 		for (j = 0; j < num_slaves; j++) {
1071 			/* find out how many identical parts were reported on that link */
1072 			if (ids[j].link_id == link_id &&
1073 			    ids[j].id.part_id == part_id &&
1074 			    ids[j].id.mfg_id == mfg_id)
1075 				reported_part_count++;
1076 		}
1077 
1078 		for (j = 0; j < num_slaves; j++) {
1079 			int expected_part_count = 0;
1080 
1081 			if (ids[j].link_id != link_id ||
1082 			    ids[j].id.part_id != part_id ||
1083 			    ids[j].id.mfg_id != mfg_id)
1084 				continue;
1085 
1086 			/* find out how many identical parts are expected */
1087 			for (k = 0; k < link->num_adr; k++) {
1088 				u64 adr2 = link->adr_d[k].adr;
1089 				unsigned int part_id2, link_id2, mfg_id2;
1090 
1091 				mfg_id2 = SDW_MFG_ID(adr2);
1092 				part_id2 = SDW_PART_ID(adr2);
1093 				link_id2 = SDW_DISCO_LINK_ID(adr2);
1094 
1095 				if (link_id2 == link_id &&
1096 				    part_id2 == part_id &&
1097 				    mfg_id2 == mfg_id)
1098 					expected_part_count++;
1099 			}
1100 
1101 			if (reported_part_count == expected_part_count) {
1102 				/*
1103 				 * we have to check unique id
1104 				 * if there is more than one
1105 				 * Slave on the link
1106 				 */
1107 				unique_id = SDW_UNIQUE_ID(adr);
1108 				if (reported_part_count == 1 ||
1109 				    ids[j].id.unique_id == unique_id) {
1110 					dev_dbg(bus->dev, "found %x at link %d\n",
1111 						part_id, link_id);
1112 					break;
1113 				}
1114 			} else {
1115 				dev_dbg(bus->dev, "part %x reported %d expected %d on link %d, skipping\n",
1116 					part_id, reported_part_count, expected_part_count, link_id);
1117 			}
1118 		}
1119 		if (j == num_slaves) {
1120 			dev_dbg(bus->dev,
1121 				"Slave %x not found\n",
1122 				part_id);
1123 			return false;
1124 		}
1125 	}
1126 	return true;
1127 }
1128 
1129 static int hda_sdw_machine_select(struct snd_sof_dev *sdev)
1130 {
1131 	struct snd_sof_pdata *pdata = sdev->pdata;
1132 	const struct snd_soc_acpi_link_adr *link;
1133 	struct snd_soc_acpi_mach *mach;
1134 	struct sof_intel_hda_dev *hdev;
1135 	u32 link_mask;
1136 	int i;
1137 
1138 	hdev = pdata->hw_pdata;
1139 	link_mask = hdev->info.link_mask;
1140 
1141 	/*
1142 	 * Select SoundWire machine driver if needed using the
1143 	 * alternate tables. This case deals with SoundWire-only
1144 	 * machines, for mixed cases with I2C/I2S the detection relies
1145 	 * on the HID list.
1146 	 */
1147 	if (link_mask && !pdata->machine) {
1148 		for (mach = pdata->desc->alt_machines;
1149 		     mach && mach->link_mask; mach++) {
1150 			/*
1151 			 * On some platforms such as Up Extreme all links
1152 			 * are enabled but only one link can be used by
1153 			 * external codec. Instead of exact match of two masks,
1154 			 * first check whether link_mask of mach is subset of
1155 			 * link_mask supported by hw and then go on searching
1156 			 * link_adr
1157 			 */
1158 			if (~link_mask & mach->link_mask)
1159 				continue;
1160 
1161 			/* No need to match adr if there is no links defined */
1162 			if (!mach->links)
1163 				break;
1164 
1165 			link = mach->links;
1166 			for (i = 0; i < hdev->info.count && link->num_adr;
1167 			     i++, link++) {
1168 				/*
1169 				 * Try next machine if any expected Slaves
1170 				 * are not found on this link.
1171 				 */
1172 				if (!link_slaves_found(sdev, link, hdev->sdw))
1173 					break;
1174 			}
1175 			/* Found if all Slaves are checked */
1176 			if (i == hdev->info.count || !link->num_adr)
1177 				break;
1178 		}
1179 		if (mach && mach->link_mask) {
1180 			int dmic_num = 0;
1181 
1182 			pdata->machine = mach;
1183 			mach->mach_params.links = mach->links;
1184 			mach->mach_params.link_mask = mach->link_mask;
1185 			mach->mach_params.platform = dev_name(sdev->dev);
1186 			if (mach->sof_fw_filename)
1187 				pdata->fw_filename = mach->sof_fw_filename;
1188 			else
1189 				pdata->fw_filename = pdata->desc->default_fw_filename;
1190 			pdata->tplg_filename = mach->sof_tplg_filename;
1191 
1192 			/*
1193 			 * DMICs use up to 4 pins and are typically pin-muxed with SoundWire
1194 			 * link 2 and 3, thus we only try to enable dmics if all conditions
1195 			 * are true:
1196 			 * a) link 2 and 3 are not used by SoundWire
1197 			 * b) the NHLT table reports the presence of microphones
1198 			 */
1199 			if (!(mach->link_mask & GENMASK(3, 2))) {
1200 				const char *tplg_filename = mach->sof_tplg_filename;
1201 				int ret;
1202 
1203 				ret = dmic_topology_fixup(sdev, &tplg_filename, "", &dmic_num);
1204 
1205 				if (ret < 0)
1206 					return ret;
1207 
1208 				pdata->tplg_filename = tplg_filename;
1209 			}
1210 			mach->mach_params.dmic_num = dmic_num;
1211 
1212 			dev_dbg(sdev->dev,
1213 				"SoundWire machine driver %s topology %s\n",
1214 				mach->drv_name,
1215 				pdata->tplg_filename);
1216 		} else {
1217 			dev_info(sdev->dev,
1218 				 "No SoundWire machine driver found\n");
1219 		}
1220 	}
1221 
1222 	return 0;
1223 }
1224 #else
1225 static int hda_sdw_machine_select(struct snd_sof_dev *sdev)
1226 {
1227 	return 0;
1228 }
1229 #endif
1230 
1231 void hda_set_mach_params(const struct snd_soc_acpi_mach *mach,
1232 			 struct snd_sof_dev *sdev)
1233 {
1234 	struct snd_sof_pdata *pdata = sdev->pdata;
1235 	const struct sof_dev_desc *desc = pdata->desc;
1236 	struct snd_soc_acpi_mach_params *mach_params;
1237 
1238 	mach_params = (struct snd_soc_acpi_mach_params *)&mach->mach_params;
1239 	mach_params->platform = dev_name(sdev->dev);
1240 	mach_params->num_dai_drivers = desc->ops->num_drv;
1241 	mach_params->dai_drivers = desc->ops->drv;
1242 }
1243 
1244 void hda_machine_select(struct snd_sof_dev *sdev)
1245 {
1246 	struct snd_sof_pdata *sof_pdata = sdev->pdata;
1247 	const struct sof_dev_desc *desc = sof_pdata->desc;
1248 	struct snd_soc_acpi_mach *mach;
1249 
1250 	mach = snd_soc_acpi_find_machine(desc->machines);
1251 	if (mach) {
1252 		/*
1253 		 * If tplg file name is overridden, use it instead of
1254 		 * the one set in mach table
1255 		 */
1256 		if (!sof_pdata->tplg_filename)
1257 			sof_pdata->tplg_filename = mach->sof_tplg_filename;
1258 
1259 		sof_pdata->machine = mach;
1260 
1261 		if (mach->link_mask) {
1262 			mach->mach_params.links = mach->links;
1263 			mach->mach_params.link_mask = mach->link_mask;
1264 		}
1265 	}
1266 
1267 	/*
1268 	 * If I2S fails, try SoundWire
1269 	 */
1270 	hda_sdw_machine_select(sdev);
1271 
1272 	/*
1273 	 * Choose HDA generic machine driver if mach is NULL.
1274 	 * Otherwise, set certain mach params.
1275 	 */
1276 	hda_generic_machine_select(sdev);
1277 
1278 	if (!sof_pdata->machine)
1279 		dev_warn(sdev->dev, "warning: No matching ASoC machine driver found\n");
1280 }
1281 
1282 int hda_pci_intel_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
1283 {
1284 	int ret;
1285 
1286 	ret = snd_intel_dsp_driver_probe(pci);
1287 	if (ret != SND_INTEL_DSP_DRIVER_ANY && ret != SND_INTEL_DSP_DRIVER_SOF) {
1288 		dev_dbg(&pci->dev, "SOF PCI driver not selected, aborting probe\n");
1289 		return -ENODEV;
1290 	}
1291 
1292 	return sof_pci_probe(pci, pci_id);
1293 }
1294 EXPORT_SYMBOL_NS(hda_pci_intel_probe, SND_SOC_SOF_INTEL_HDA_COMMON);
1295 
1296 MODULE_LICENSE("Dual BSD/GPL");
1297 MODULE_IMPORT_NS(SND_SOC_SOF_PCI_DEV);
1298 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC);
1299 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
1300 MODULE_IMPORT_NS(SND_SOC_SOF_XTENSA);
1301 MODULE_IMPORT_NS(SND_INTEL_SOUNDWIRE_ACPI);
1302 MODULE_IMPORT_NS(SOUNDWIRE_INTEL_INIT);
1303