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