xref: /linux/sound/soc/sof/intel/hda.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
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 	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 
sdw_params_free(struct device * dev,struct sdw_intel_stream_free_data * free_data)73 static 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 
96 struct sdw_intel_ops sdw_callback = {
97 	.params_stream = sdw_params_stream,
98 	.free_stream = sdw_params_free,
99 };
100 
sdw_ace2x_params_stream(struct device * dev,struct sdw_intel_stream_params_data * params_data)101 static 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 
sdw_ace2x_free_stream(struct device * dev,struct sdw_intel_stream_free_data * free_data)111 static 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 
sdw_ace2x_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)119 static 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 
124 static struct sdw_intel_ops sdw_ace2x_callback = {
125 	.params_stream = sdw_ace2x_params_stream,
126 	.free_stream = sdw_ace2x_free_stream,
127 	.trigger = sdw_ace2x_trigger,
128 };
129 
hda_sdw_acpi_scan(struct snd_sof_dev * sdev)130 static 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 
hda_sdw_probe(struct snd_sof_dev * sdev)152 static 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 
hda_sdw_startup(struct snd_sof_dev * sdev)221 int 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 }
241 EXPORT_SYMBOL_NS(hda_sdw_startup, SND_SOC_SOF_INTEL_HDA_GENERIC);
242 
hda_sdw_exit(struct snd_sof_dev * sdev)243 static 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 
hda_common_check_sdw_irq(struct snd_sof_dev * sdev)258 bool 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 
280 out:
281 	return ret;
282 }
283 EXPORT_SYMBOL_NS(hda_common_check_sdw_irq, SND_SOC_SOF_INTEL_HDA_GENERIC);
284 
hda_dsp_check_sdw_irq(struct snd_sof_dev * sdev)285 static 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 
hda_dsp_sdw_thread(int irq,void * context)300 static irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
301 {
302 	return sdw_intel_thread(irq, context);
303 }
304 
hda_sdw_check_wakeen_irq_common(struct snd_sof_dev * sdev)305 bool 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 }
317 EXPORT_SYMBOL_NS(hda_sdw_check_wakeen_irq_common, SND_SOC_SOF_INTEL_HDA_GENERIC);
318 
hda_sdw_check_wakeen_irq(struct snd_sof_dev * sdev)319 static 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 
hda_sdw_process_wakeen_common(struct snd_sof_dev * sdev)334 void 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 }
348 EXPORT_SYMBOL_NS(hda_sdw_process_wakeen_common, SND_SOC_SOF_INTEL_HDA_GENERIC);
349 
350 #else /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) */
hda_sdw_acpi_scan(struct snd_sof_dev * sdev)351 static inline int hda_sdw_acpi_scan(struct snd_sof_dev *sdev)
352 {
353 	return 0;
354 }
355 
hda_sdw_probe(struct snd_sof_dev * sdev)356 static inline int hda_sdw_probe(struct snd_sof_dev *sdev)
357 {
358 	return 0;
359 }
360 
hda_sdw_exit(struct snd_sof_dev * sdev)361 static inline int hda_sdw_exit(struct snd_sof_dev *sdev)
362 {
363 	return 0;
364 }
365 
hda_dsp_check_sdw_irq(struct snd_sof_dev * sdev)366 static inline bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev)
367 {
368 	return false;
369 }
370 
hda_dsp_sdw_thread(int irq,void * context)371 static inline irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
372 {
373 	return IRQ_HANDLED;
374 }
375 
hda_sdw_check_wakeen_irq(struct snd_sof_dev * sdev)376 static 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 */
hda_dsp_pre_fw_run(struct snd_sof_dev * sdev)384 int 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 */
hda_dsp_post_fw_run(struct snd_sof_dev * sdev)391 int 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 }
421 EXPORT_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)
428 static bool hda_use_msi = true;
429 module_param_named(use_msi, hda_use_msi, bool, 0444);
430 MODULE_PARM_DESC(use_msi, "SOF HDA use PCI MSI mode");
431 #else
432 #define hda_use_msi	(1)
433 #endif
434 
435 static char *hda_model;
436 module_param(hda_model, charp, 0444);
437 MODULE_PARM_DESC(hda_model, "Use the given HDA board model.");
438 
439 static int dmic_num_override = -1;
440 module_param_named(dmic_num, dmic_num_override, int, 0444);
441 MODULE_PARM_DESC(dmic_num, "SOF HDA DMIC number");
442 
443 static int mclk_id_override = -1;
444 module_param_named(mclk_id, mclk_id_override, int, 0444);
445 MODULE_PARM_DESC(mclk_id, "SOF SSP mclk_id");
446 
447 static int bt_link_mask_override;
448 module_param_named(bt_link_mask, bt_link_mask_override, int, 0444);
449 MODULE_PARM_DESC(bt_link_mask, "SOF BT offload link mask");
450 
hda_init(struct snd_sof_dev * sdev)451 static 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 
501 out:
502 	if (ret < 0)
503 		iounmap(sof_to_bus(sdev)->remap_addr);
504 
505 	return ret;
506 }
507 
check_dmic_num(struct snd_sof_dev * sdev)508 static 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 
check_nhlt_ssp_mask(struct snd_sof_dev * sdev,u8 device_type)536 static 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 
check_nhlt_ssp_mclk_mask(struct snd_sof_dev * sdev,int ssp_num)557 static 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 
hda_init_caps(struct snd_sof_dev * sdev)569 static 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 
622 skip_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 
hda_dsp_interrupt_handler(int irq,void * context)635 static 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 
hda_dsp_interrupt_thread(int irq,void * context)658 static 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 
hda_dsp_probe_early(struct snd_sof_dev * sdev)695 int 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 
739 err:
740 	return ret;
741 }
742 EXPORT_SYMBOL_NS(hda_dsp_probe_early, SND_SOC_SOF_INTEL_HDA_GENERIC);
743 
hda_dsp_probe(struct snd_sof_dev * sdev)744 int 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;
785 skip_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 		hda_sdw_int_enable(sdev, true);
871 	}
872 
873 	init_waitqueue_head(&hdev->waitq);
874 
875 	hdev->nhlt = intel_nhlt_init(sdev->dev);
876 
877 	return 0;
878 
879 disable_pp_cap:
880 	if (!sdev->dspless_mode_selected) {
881 		hda_dsp_ctrl_ppcap_int_enable(sdev, false);
882 		hda_dsp_ctrl_ppcap_enable(sdev, false);
883 	}
884 free_ipc_irq:
885 	free_irq(sdev->ipc_irq, sdev);
886 free_irq_vector:
887 	if (sdev->msi_enabled)
888 		pci_free_irq_vectors(pci);
889 free_streams:
890 	hda_dsp_stream_free(sdev);
891 /* dsp_unmap: not currently used */
892 	if (!sdev->dspless_mode_selected)
893 		iounmap(sdev->bar[HDA_DSP_BAR]);
894 hdac_bus_unmap:
895 	platform_device_unregister(hdev->dmic_dev);
896 
897 	return ret;
898 }
899 EXPORT_SYMBOL_NS(hda_dsp_probe, SND_SOC_SOF_INTEL_HDA_GENERIC);
900 
hda_dsp_remove(struct snd_sof_dev * sdev)901 void hda_dsp_remove(struct snd_sof_dev *sdev)
902 {
903 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
904 	const struct sof_intel_dsp_desc *chip = hda->desc;
905 	struct pci_dev *pci = to_pci_dev(sdev->dev);
906 	struct nhlt_acpi_table *nhlt = hda->nhlt;
907 
908 	if (nhlt)
909 		intel_nhlt_free(nhlt);
910 
911 	if (!sdev->dspless_mode_selected)
912 		/* cancel any attempt for DSP D0I3 */
913 		cancel_delayed_work_sync(&hda->d0i3_work);
914 
915 	hda_codec_device_remove(sdev);
916 
917 	hda_sdw_exit(sdev);
918 
919 	if (!IS_ERR_OR_NULL(hda->dmic_dev))
920 		platform_device_unregister(hda->dmic_dev);
921 
922 	if (!sdev->dspless_mode_selected) {
923 		/* disable DSP IRQ */
924 		hda_dsp_ctrl_ppcap_int_enable(sdev, false);
925 	}
926 
927 	/* disable CIE and GIE interrupts */
928 	snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
929 				SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN, 0);
930 
931 	if (sdev->dspless_mode_selected)
932 		goto skip_disable_dsp;
933 
934 	/* no need to check for error as the DSP will be disabled anyway */
935 	if (chip && chip->power_down_dsp)
936 		chip->power_down_dsp(sdev);
937 
938 	/* disable DSP */
939 	hda_dsp_ctrl_ppcap_enable(sdev, false);
940 
941 skip_disable_dsp:
942 	free_irq(sdev->ipc_irq, sdev);
943 	if (sdev->msi_enabled)
944 		pci_free_irq_vectors(pci);
945 
946 	hda_dsp_stream_free(sdev);
947 
948 	hda_bus_ml_free(sof_to_bus(sdev));
949 
950 	if (!sdev->dspless_mode_selected)
951 		iounmap(sdev->bar[HDA_DSP_BAR]);
952 }
953 EXPORT_SYMBOL_NS(hda_dsp_remove, SND_SOC_SOF_INTEL_HDA_GENERIC);
954 
hda_dsp_remove_late(struct snd_sof_dev * sdev)955 void hda_dsp_remove_late(struct snd_sof_dev *sdev)
956 {
957 	iounmap(sof_to_bus(sdev)->remap_addr);
958 	sof_hda_bus_exit(sdev);
959 	hda_codec_i915_exit(sdev);
960 }
961 
hda_power_down_dsp(struct snd_sof_dev * sdev)962 int hda_power_down_dsp(struct snd_sof_dev *sdev)
963 {
964 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
965 	const struct sof_intel_dsp_desc *chip = hda->desc;
966 
967 	return hda_dsp_core_reset_power_down(sdev, chip->host_managed_cores_mask);
968 }
969 EXPORT_SYMBOL_NS(hda_power_down_dsp, SND_SOC_SOF_INTEL_HDA_GENERIC);
970 
971 #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)972 static void hda_generic_machine_select(struct snd_sof_dev *sdev,
973 				       struct snd_soc_acpi_mach **mach)
974 {
975 	struct hdac_bus *bus = sof_to_bus(sdev);
976 	struct snd_soc_acpi_mach_params *mach_params;
977 	struct snd_soc_acpi_mach *hda_mach;
978 	struct snd_sof_pdata *pdata = sdev->pdata;
979 	const char *tplg_filename;
980 	int codec_num = 0;
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 (!*mach && codec_num <= 2) {
1003 			bool tplg_fixup = false;
1004 
1005 			hda_mach = snd_soc_acpi_intel_hda_machines;
1006 
1007 			dev_info(bus->dev, "using HDA machine driver %s now\n",
1008 				 hda_mach->drv_name);
1009 
1010 			/*
1011 			 * topology: use the info from hda_machines since tplg file name
1012 			 * is not overwritten
1013 			 */
1014 			if (!pdata->tplg_filename)
1015 				tplg_fixup = true;
1016 
1017 			if (tplg_fixup &&
1018 			    codec_num == 1 && HDA_IDISP_CODEC(bus->codec_mask)) {
1019 				tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1020 							       "%s-idisp",
1021 							       hda_mach->sof_tplg_filename);
1022 				if (!tplg_filename)
1023 					return;
1024 
1025 				hda_mach->sof_tplg_filename = tplg_filename;
1026 			}
1027 
1028 			if (codec_num == 2 ||
1029 			    (codec_num == 1 && !HDA_IDISP_CODEC(bus->codec_mask))) {
1030 				/*
1031 				 * Prevent SoundWire links from starting when an external
1032 				 * HDaudio codec is used
1033 				 */
1034 				hda_mach->mach_params.link_mask = 0;
1035 			} else {
1036 				/*
1037 				 * Allow SoundWire links to start when no external HDaudio codec
1038 				 * was detected. This will not create a SoundWire card but
1039 				 * will help detect if any SoundWire codec reports as ATTACHED.
1040 				 */
1041 				struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
1042 
1043 				hda_mach->mach_params.link_mask = hdev->info.link_mask;
1044 			}
1045 
1046 			*mach = hda_mach;
1047 		}
1048 	}
1049 
1050 	/* used by hda machine driver to create dai links */
1051 	if (*mach) {
1052 		mach_params = &(*mach)->mach_params;
1053 		mach_params->codec_mask = bus->codec_mask;
1054 	}
1055 }
1056 #else
hda_generic_machine_select(struct snd_sof_dev * sdev,struct snd_soc_acpi_mach ** mach)1057 static void hda_generic_machine_select(struct snd_sof_dev *sdev,
1058 				       struct snd_soc_acpi_mach **mach)
1059 {
1060 }
1061 #endif
1062 
1063 #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
1064 
hda_sdw_machine_select(struct snd_sof_dev * sdev)1065 static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
1066 {
1067 	struct snd_sof_pdata *pdata = sdev->pdata;
1068 	const struct snd_soc_acpi_link_adr *link;
1069 	struct sdw_extended_slave_id *ids;
1070 	struct snd_soc_acpi_mach *mach;
1071 	struct sof_intel_hda_dev *hdev;
1072 	u32 link_mask;
1073 	int i;
1074 
1075 	hdev = pdata->hw_pdata;
1076 	link_mask = hdev->info.link_mask;
1077 
1078 	if (!link_mask) {
1079 		dev_info(sdev->dev, "SoundWire links not enabled\n");
1080 		return NULL;
1081 	}
1082 
1083 	if (!hdev->sdw) {
1084 		dev_dbg(sdev->dev, "SoundWire context not allocated\n");
1085 		return NULL;
1086 	}
1087 
1088 	if (!hdev->sdw->num_slaves) {
1089 		dev_warn(sdev->dev, "No SoundWire peripheral detected in ACPI tables\n");
1090 		return NULL;
1091 	}
1092 
1093 	/*
1094 	 * Select SoundWire machine driver if needed using the
1095 	 * alternate tables. This case deals with SoundWire-only
1096 	 * machines, for mixed cases with I2C/I2S the detection relies
1097 	 * on the HID list.
1098 	 */
1099 	for (mach = pdata->desc->alt_machines;
1100 	     mach && mach->link_mask; mach++) {
1101 		/*
1102 		 * On some platforms such as Up Extreme all links
1103 		 * are enabled but only one link can be used by
1104 		 * external codec. Instead of exact match of two masks,
1105 		 * first check whether link_mask of mach is subset of
1106 		 * link_mask supported by hw and then go on searching
1107 		 * link_adr
1108 		 */
1109 		if (~link_mask & mach->link_mask)
1110 			continue;
1111 
1112 		/* No need to match adr if there is no links defined */
1113 		if (!mach->links)
1114 			break;
1115 
1116 		link = mach->links;
1117 		for (i = 0; i < hdev->info.count && link->num_adr;
1118 		     i++, link++) {
1119 			/*
1120 			 * Try next machine if any expected Slaves
1121 			 * are not found on this link.
1122 			 */
1123 			if (!snd_soc_acpi_sdw_link_slaves_found(sdev->dev, link,
1124 								hdev->sdw->ids,
1125 								hdev->sdw->num_slaves))
1126 				break;
1127 		}
1128 		/* Found if all Slaves are checked */
1129 		if (i == hdev->info.count || !link->num_adr)
1130 			break;
1131 	}
1132 	if (mach && mach->link_mask) {
1133 		mach->mach_params.links = mach->links;
1134 		mach->mach_params.link_mask = mach->link_mask;
1135 		mach->mach_params.platform = dev_name(sdev->dev);
1136 
1137 		return mach;
1138 	}
1139 
1140 	dev_info(sdev->dev, "No SoundWire machine driver found for the ACPI-reported configuration:\n");
1141 	ids = hdev->sdw->ids;
1142 	for (i = 0; i < hdev->sdw->num_slaves; i++)
1143 		dev_info(sdev->dev, "link %d mfg_id 0x%04x part_id 0x%04x version %#x\n",
1144 			 ids[i].link_id, ids[i].id.mfg_id, ids[i].id.part_id, ids[i].id.sdw_version);
1145 
1146 	return NULL;
1147 }
1148 #else
hda_sdw_machine_select(struct snd_sof_dev * sdev)1149 static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
1150 {
1151 	return NULL;
1152 }
1153 #endif
1154 
hda_set_mach_params(struct snd_soc_acpi_mach * mach,struct snd_sof_dev * sdev)1155 void hda_set_mach_params(struct snd_soc_acpi_mach *mach,
1156 			 struct snd_sof_dev *sdev)
1157 {
1158 	struct snd_sof_pdata *pdata = sdev->pdata;
1159 	const struct sof_dev_desc *desc = pdata->desc;
1160 	struct snd_soc_acpi_mach_params *mach_params;
1161 
1162 	mach_params = &mach->mach_params;
1163 	mach_params->platform = dev_name(sdev->dev);
1164 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
1165 	    sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
1166 		mach_params->num_dai_drivers = SOF_SKL_NUM_DAIS_NOCODEC;
1167 	else
1168 		mach_params->num_dai_drivers = desc->ops->num_drv;
1169 	mach_params->dai_drivers = desc->ops->drv;
1170 }
1171 
check_tplg_quirk_mask(struct snd_soc_acpi_mach * mach)1172 static int check_tplg_quirk_mask(struct snd_soc_acpi_mach *mach)
1173 {
1174 	u32 dmic_ssp_quirk;
1175 	u32 codec_amp_name_quirk;
1176 
1177 	/*
1178 	 * In current implementation dmic and ssp quirks are designed for es8336
1179 	 * machine driver and could not be mixed with codec name and amp name
1180 	 * quirks.
1181 	 */
1182 	dmic_ssp_quirk = mach->tplg_quirk_mask &
1183 			 (SND_SOC_ACPI_TPLG_INTEL_DMIC_NUMBER | SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER);
1184 	codec_amp_name_quirk = mach->tplg_quirk_mask &
1185 			 (SND_SOC_ACPI_TPLG_INTEL_AMP_NAME | SND_SOC_ACPI_TPLG_INTEL_CODEC_NAME);
1186 
1187 	if (dmic_ssp_quirk && codec_amp_name_quirk)
1188 		return -EINVAL;
1189 
1190 	return 0;
1191 }
1192 
remove_file_ext(const char * tplg_filename)1193 static char *remove_file_ext(const char *tplg_filename)
1194 {
1195 	char *filename, *tmp;
1196 
1197 	filename = kstrdup(tplg_filename, GFP_KERNEL);
1198 	if (!filename)
1199 		return NULL;
1200 
1201 	/* remove file extension if exist */
1202 	tmp = filename;
1203 	return strsep(&tmp, ".");
1204 }
1205 
hda_machine_select(struct snd_sof_dev * sdev)1206 struct snd_soc_acpi_mach *hda_machine_select(struct snd_sof_dev *sdev)
1207 {
1208 	u32 interface_mask = hda_get_interface_mask(sdev);
1209 	struct snd_sof_pdata *sof_pdata = sdev->pdata;
1210 	const struct sof_dev_desc *desc = sof_pdata->desc;
1211 	struct hdac_bus *bus = sof_to_bus(sdev);
1212 	struct snd_soc_acpi_mach *mach = NULL;
1213 	enum snd_soc_acpi_intel_codec codec_type, amp_type;
1214 	const char *tplg_filename;
1215 	const char *tplg_suffix;
1216 	bool amp_name_valid;
1217 	bool i2s_mach_found = false;
1218 	bool sdw_mach_found = false;
1219 
1220 	/* Try I2S or DMIC if it is supported */
1221 	if (interface_mask & (BIT(SOF_DAI_INTEL_SSP) | BIT(SOF_DAI_INTEL_DMIC))) {
1222 		mach = snd_soc_acpi_find_machine(desc->machines);
1223 		if (mach)
1224 			i2s_mach_found = true;
1225 	}
1226 
1227 	/*
1228 	 * If I2S fails and no external HDaudio codec is detected,
1229 	 * try SoundWire if it is supported
1230 	 */
1231 	if (!mach && !HDA_EXT_CODEC(bus->codec_mask) &&
1232 	    (interface_mask & BIT(SOF_DAI_INTEL_ALH))) {
1233 		mach = hda_sdw_machine_select(sdev);
1234 		if (mach)
1235 			sdw_mach_found = true;
1236 	}
1237 
1238 	/*
1239 	 * Choose HDA generic machine driver if mach is NULL.
1240 	 * Otherwise, set certain mach params.
1241 	 */
1242 	hda_generic_machine_select(sdev, &mach);
1243 	if (!mach) {
1244 		dev_warn(sdev->dev, "warning: No matching ASoC machine driver found\n");
1245 		return NULL;
1246 	}
1247 
1248 	/* report BT offload link mask to machine driver */
1249 	mach->mach_params.bt_link_mask = check_nhlt_ssp_mask(sdev, NHLT_DEVICE_BT);
1250 
1251 	dev_info(sdev->dev, "BT link detected in NHLT tables: %#x\n",
1252 		 mach->mach_params.bt_link_mask);
1253 
1254 	/* allow for module parameter override */
1255 	if (bt_link_mask_override) {
1256 		dev_dbg(sdev->dev, "overriding BT link detected in NHLT tables %#x by kernel param %#x\n",
1257 			mach->mach_params.bt_link_mask, bt_link_mask_override);
1258 		mach->mach_params.bt_link_mask = bt_link_mask_override;
1259 	}
1260 
1261 	if (hweight_long(mach->mach_params.bt_link_mask) > 1) {
1262 		dev_warn(sdev->dev, "invalid BT link mask %#x found, reset the mask\n",
1263 			mach->mach_params.bt_link_mask);
1264 		mach->mach_params.bt_link_mask = 0;
1265 	}
1266 
1267 	/*
1268 	 * Fixup tplg file name by appending dmic num, ssp num, codec/amplifier
1269 	 * name string if quirk flag is set.
1270 	 */
1271 	if (mach) {
1272 		bool tplg_fixup = false;
1273 		bool dmic_fixup = false;
1274 
1275 		/*
1276 		 * If tplg file name is overridden, use it instead of
1277 		 * the one set in mach table
1278 		 */
1279 		if (!sof_pdata->tplg_filename) {
1280 			/* remove file extension if it exists */
1281 			tplg_filename = remove_file_ext(mach->sof_tplg_filename);
1282 			if (!tplg_filename)
1283 				return NULL;
1284 
1285 			sof_pdata->tplg_filename = tplg_filename;
1286 			tplg_fixup = true;
1287 		}
1288 
1289 		/*
1290 		 * Checking quirk mask integrity; some quirk flags could not be
1291 		 * set concurrently.
1292 		 */
1293 		if (tplg_fixup &&
1294 		    check_tplg_quirk_mask(mach)) {
1295 			dev_err(sdev->dev, "Invalid tplg quirk mask 0x%x\n",
1296 				mach->tplg_quirk_mask);
1297 			return NULL;
1298 		}
1299 
1300 		/* report to machine driver if any DMICs are found */
1301 		mach->mach_params.dmic_num = check_dmic_num(sdev);
1302 
1303 		if (sdw_mach_found) {
1304 			/*
1305 			 * DMICs use up to 4 pins and are typically pin-muxed with SoundWire
1306 			 * link 2 and 3, or link 1 and 2, thus we only try to enable dmics
1307 			 * if all conditions are true:
1308 			 * a) 2 or fewer links are used by SoundWire
1309 			 * b) the NHLT table reports the presence of microphones
1310 			 */
1311 			if (hweight_long(mach->link_mask) <= 2)
1312 				dmic_fixup = true;
1313 			else
1314 				mach->mach_params.dmic_num = 0;
1315 		} else {
1316 			if (mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_DMIC_NUMBER)
1317 				dmic_fixup = true;
1318 		}
1319 
1320 		if (tplg_fixup &&
1321 		    dmic_fixup &&
1322 		    mach->mach_params.dmic_num) {
1323 			tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1324 						       "%s%s%d%s",
1325 						       sof_pdata->tplg_filename,
1326 						       i2s_mach_found ? "-dmic" : "-",
1327 						       mach->mach_params.dmic_num,
1328 						       "ch");
1329 			if (!tplg_filename)
1330 				return NULL;
1331 
1332 			sof_pdata->tplg_filename = tplg_filename;
1333 		}
1334 
1335 		if (mach->link_mask) {
1336 			mach->mach_params.links = mach->links;
1337 			mach->mach_params.link_mask = mach->link_mask;
1338 		}
1339 
1340 		/* report SSP link mask to machine driver */
1341 		mach->mach_params.i2s_link_mask = check_nhlt_ssp_mask(sdev, NHLT_DEVICE_I2S);
1342 
1343 		if (tplg_fixup &&
1344 		    mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER &&
1345 		    mach->mach_params.i2s_link_mask) {
1346 			const struct sof_intel_dsp_desc *chip = get_chip_info(sdev->pdata);
1347 			int ssp_num;
1348 			int mclk_mask;
1349 
1350 			if (hweight_long(mach->mach_params.i2s_link_mask) > 1 &&
1351 			    !(mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_MSB))
1352 				dev_warn(sdev->dev, "More than one SSP exposed by NHLT, choosing MSB\n");
1353 
1354 			/* fls returns 1-based results, SSPs indices are 0-based */
1355 			ssp_num = fls(mach->mach_params.i2s_link_mask) - 1;
1356 
1357 			if (ssp_num >= chip->ssp_count) {
1358 				dev_err(sdev->dev, "Invalid SSP %d, max on this platform is %d\n",
1359 					ssp_num, chip->ssp_count);
1360 				return NULL;
1361 			}
1362 
1363 			tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1364 						       "%s%s%d",
1365 						       sof_pdata->tplg_filename,
1366 						       "-ssp",
1367 						       ssp_num);
1368 			if (!tplg_filename)
1369 				return NULL;
1370 
1371 			sof_pdata->tplg_filename = tplg_filename;
1372 
1373 			mclk_mask = check_nhlt_ssp_mclk_mask(sdev, ssp_num);
1374 
1375 			if (mclk_mask < 0) {
1376 				dev_err(sdev->dev, "Invalid MCLK configuration\n");
1377 				return NULL;
1378 			}
1379 
1380 			dev_dbg(sdev->dev, "MCLK mask %#x found in NHLT\n", mclk_mask);
1381 
1382 			if (mclk_mask) {
1383 				dev_info(sdev->dev, "Overriding topology with MCLK mask %#x from NHLT\n", mclk_mask);
1384 				sdev->mclk_id_override = true;
1385 				sdev->mclk_id_quirk = (mclk_mask & BIT(0)) ? 0 : 1;
1386 			}
1387 		}
1388 
1389 		amp_type = snd_soc_acpi_intel_detect_amp_type(sdev->dev);
1390 		codec_type = snd_soc_acpi_intel_detect_codec_type(sdev->dev);
1391 		amp_name_valid = amp_type != CODEC_NONE && amp_type != codec_type;
1392 
1393 		if (tplg_fixup && amp_name_valid &&
1394 		    mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_AMP_NAME) {
1395 			tplg_suffix = snd_soc_acpi_intel_get_amp_tplg_suffix(amp_type);
1396 			if (!tplg_suffix) {
1397 				dev_err(sdev->dev, "no tplg suffix found, amp %d\n",
1398 					amp_type);
1399 				return NULL;
1400 			}
1401 
1402 			tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1403 						       "%s-%s",
1404 						       sof_pdata->tplg_filename,
1405 						       tplg_suffix);
1406 			if (!tplg_filename)
1407 				return NULL;
1408 
1409 			sof_pdata->tplg_filename = tplg_filename;
1410 		}
1411 
1412 
1413 		if (tplg_fixup &&
1414 		    mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_CODEC_NAME &&
1415 		    codec_type != CODEC_NONE) {
1416 			tplg_suffix = snd_soc_acpi_intel_get_codec_tplg_suffix(codec_type);
1417 			if (!tplg_suffix) {
1418 				dev_err(sdev->dev, "no tplg suffix found, codec %d\n",
1419 					codec_type);
1420 				return NULL;
1421 			}
1422 
1423 			tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1424 						       "%s-%s",
1425 						       sof_pdata->tplg_filename,
1426 						       tplg_suffix);
1427 			if (!tplg_filename)
1428 				return NULL;
1429 
1430 			sof_pdata->tplg_filename = tplg_filename;
1431 		}
1432 
1433 		if (tplg_fixup) {
1434 			tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1435 						       "%s%s",
1436 						       sof_pdata->tplg_filename,
1437 						       ".tplg");
1438 			if (!tplg_filename)
1439 				return NULL;
1440 
1441 			sof_pdata->tplg_filename = tplg_filename;
1442 		}
1443 
1444 		/* check if mclk_id should be modified from topology defaults */
1445 		if (mclk_id_override >= 0) {
1446 			dev_info(sdev->dev, "Overriding topology with MCLK %d from kernel_parameter\n", mclk_id_override);
1447 			sdev->mclk_id_override = true;
1448 			sdev->mclk_id_quirk = mclk_id_override;
1449 		}
1450 	}
1451 
1452 	return mach;
1453 }
1454 
hda_pci_intel_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)1455 int hda_pci_intel_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
1456 {
1457 	int ret;
1458 
1459 	ret = snd_intel_dsp_driver_probe(pci);
1460 	if (ret != SND_INTEL_DSP_DRIVER_ANY && ret != SND_INTEL_DSP_DRIVER_SOF) {
1461 		dev_dbg(&pci->dev, "SOF PCI driver not selected, aborting probe\n");
1462 		return -ENODEV;
1463 	}
1464 
1465 	return sof_pci_probe(pci, pci_id);
1466 }
1467 EXPORT_SYMBOL_NS(hda_pci_intel_probe, SND_SOC_SOF_INTEL_HDA_GENERIC);
1468 
hda_register_clients(struct snd_sof_dev * sdev)1469 int hda_register_clients(struct snd_sof_dev *sdev)
1470 {
1471 	return hda_probes_register(sdev);
1472 }
1473 
hda_unregister_clients(struct snd_sof_dev * sdev)1474 void hda_unregister_clients(struct snd_sof_dev *sdev)
1475 {
1476 	hda_probes_unregister(sdev);
1477 }
1478 
1479 MODULE_LICENSE("Dual BSD/GPL");
1480 MODULE_DESCRIPTION("SOF support for HDaudio platforms");
1481 MODULE_IMPORT_NS(SND_SOC_SOF_PCI_DEV);
1482 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC);
1483 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
1484 MODULE_IMPORT_NS(SND_SOC_SOF_XTENSA);
1485 MODULE_IMPORT_NS(SND_INTEL_SOUNDWIRE_ACPI);
1486 MODULE_IMPORT_NS(SOUNDWIRE_INTEL_INIT);
1487 MODULE_IMPORT_NS(SOUNDWIRE_INTEL);
1488 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_MLINK);
1489 MODULE_IMPORT_NS(SND_SOC_SOF_INTEL_HDA_COMMON);
1490 MODULE_IMPORT_NS(SND_SOC_ACPI_INTEL_MATCH);
1491