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