xref: /linux/sound/soc/sof/intel/hda.c (revision 9c2f970518c900821acdac47bbd681b99a325e3d)
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 	/* scan SoundWire capabilities exposed by DSDT */
633 	ret = hda_sdw_acpi_scan(sdev);
634 	if (ret < 0) {
635 		dev_dbg(sdev->dev, "skipping SoundWire, not detected with ACPI scan\n");
636 		goto skip_soundwire;
637 	}
638 
639 	link_mask = hdev->info.link_mask;
640 	if (!link_mask) {
641 		dev_dbg(sdev->dev, "skipping SoundWire, no links enabled\n");
642 		goto skip_soundwire;
643 	}
644 
645 	/*
646 	 * probe/allocate SoundWire resources.
647 	 * The hardware configuration takes place in hda_sdw_startup
648 	 * after power rails are enabled.
649 	 * It's entirely possible to have a mix of I2S/DMIC/SoundWire
650 	 * devices, so we allocate the resources in all cases.
651 	 */
652 	ret = hda_sdw_probe(sdev);
653 	if (ret < 0) {
654 		dev_err(sdev->dev, "error: SoundWire probe error\n");
655 		return ret;
656 	}
657 
658 skip_soundwire:
659 
660 	/* create codec instances */
661 	hda_codec_probe_bus(sdev);
662 
663 	if (!HDA_IDISP_CODEC(bus->codec_mask))
664 		hda_codec_i915_display_power(sdev, false);
665 
666 	hda_bus_ml_put_all(bus);
667 
668 	return 0;
669 }
670 
hda_dsp_interrupt_handler(int irq,void * context)671 static irqreturn_t hda_dsp_interrupt_handler(int irq, void *context)
672 {
673 	struct snd_sof_dev *sdev = context;
674 
675 	/*
676 	 * Get global interrupt status. It includes all hardware interrupt
677 	 * sources in the Intel HD Audio controller.
678 	 */
679 	if (snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS) &
680 	    SOF_HDA_INTSTS_GIS) {
681 
682 		/* disable GIE interrupt */
683 		snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
684 					SOF_HDA_INTCTL,
685 					SOF_HDA_INT_GLOBAL_EN,
686 					0);
687 
688 		return IRQ_WAKE_THREAD;
689 	}
690 
691 	return IRQ_NONE;
692 }
693 
hda_dsp_interrupt_thread(int irq,void * context)694 static irqreturn_t hda_dsp_interrupt_thread(int irq, void *context)
695 {
696 	struct snd_sof_dev *sdev = context;
697 	struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
698 
699 	/* deal with streams and controller first */
700 	if (hda_dsp_check_stream_irq(sdev)) {
701 		trace_sof_intel_hda_irq(sdev, "stream");
702 		hda_dsp_stream_threaded_handler(irq, sdev);
703 	}
704 
705 	if (hda_check_ipc_irq(sdev)) {
706 		trace_sof_intel_hda_irq(sdev, "ipc");
707 		sof_ops(sdev)->irq_thread(irq, sdev);
708 	}
709 
710 	if (hda_dsp_check_sdw_irq(sdev)) {
711 		trace_sof_intel_hda_irq(sdev, "sdw");
712 
713 		hda_dsp_sdw_thread(irq, hdev->sdw);
714 
715 		if (hda_dsp_sdw_check_mic_privacy_irq(sdev)) {
716 			trace_sof_intel_hda_irq(sdev, "mic privacy");
717 			hda_dsp_sdw_process_mic_privacy(sdev);
718 		}
719 	}
720 
721 	if (hda_sdw_check_wakeen_irq(sdev)) {
722 		trace_sof_intel_hda_irq(sdev, "wakeen");
723 		hda_sdw_process_wakeen(sdev);
724 	}
725 
726 	hda_codec_check_for_state_change(sdev);
727 
728 	/* enable GIE interrupt */
729 	snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
730 				SOF_HDA_INTCTL,
731 				SOF_HDA_INT_GLOBAL_EN,
732 				SOF_HDA_INT_GLOBAL_EN);
733 
734 	return IRQ_HANDLED;
735 }
736 
hda_dsp_probe_early(struct snd_sof_dev * sdev)737 int hda_dsp_probe_early(struct snd_sof_dev *sdev)
738 {
739 	struct pci_dev *pci = to_pci_dev(sdev->dev);
740 	struct sof_intel_hda_dev *hdev;
741 	const struct sof_intel_dsp_desc *chip;
742 	int ret = 0;
743 
744 	if (!sdev->dspless_mode_selected) {
745 		/*
746 		 * detect DSP by checking class/subclass/prog-id information
747 		 * class=04 subclass 03 prog-if 00: no DSP, legacy driver is required
748 		 * class=04 subclass 01 prog-if 00: DSP is present
749 		 *   (and may be required e.g. for DMIC or SSP support)
750 		 * class=04 subclass 03 prog-if 80: either of DSP or legacy mode works
751 		 */
752 		if (pci->class == 0x040300) {
753 			dev_err(sdev->dev, "the DSP is not enabled on this platform, aborting probe\n");
754 			return -ENODEV;
755 		} else if (pci->class != 0x040100 && pci->class != 0x040380) {
756 			dev_err(sdev->dev, "unknown PCI class/subclass/prog-if 0x%06x found, aborting probe\n",
757 				pci->class);
758 			return -ENODEV;
759 		}
760 		dev_info_once(sdev->dev, "DSP detected with PCI class/subclass/prog-if 0x%06x\n",
761 			      pci->class);
762 	}
763 
764 	chip = get_chip_info(sdev->pdata);
765 	if (!chip) {
766 		dev_err(sdev->dev, "error: no such device supported, chip id:%x\n",
767 			pci->device);
768 		ret = -EIO;
769 		goto err;
770 	}
771 
772 	sdev->num_cores = chip->cores_num;
773 
774 	hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
775 	if (!hdev)
776 		return -ENOMEM;
777 	sdev->pdata->hw_pdata = hdev;
778 	hdev->desc = chip;
779 	ret = hda_init(sdev);
780 
781 err:
782 	return ret;
783 }
784 EXPORT_SYMBOL_NS(hda_dsp_probe_early, "SND_SOC_SOF_INTEL_HDA_GENERIC");
785 
hda_dsp_probe(struct snd_sof_dev * sdev)786 int hda_dsp_probe(struct snd_sof_dev *sdev)
787 {
788 	struct pci_dev *pci = to_pci_dev(sdev->dev);
789 	struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
790 	const struct sof_intel_dsp_desc *chip;
791 	int ret = 0;
792 
793 	hdev->dmic_dev = platform_device_register_data(sdev->dev, "dmic-codec",
794 						       PLATFORM_DEVID_NONE,
795 						       NULL, 0);
796 	if (IS_ERR(hdev->dmic_dev)) {
797 		dev_err(sdev->dev, "error: failed to create DMIC device\n");
798 		return PTR_ERR(hdev->dmic_dev);
799 	}
800 
801 	/*
802 	 * use position update IPC if either it is forced
803 	 * or we don't have other choice
804 	 */
805 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_FORCE_IPC_POSITION)
806 	hdev->no_ipc_position = 0;
807 #else
808 	hdev->no_ipc_position = sof_ops(sdev)->pcm_pointer ? 1 : 0;
809 #endif
810 
811 	if (sdev->dspless_mode_selected)
812 		hdev->no_ipc_position = 1;
813 
814 	if (sdev->dspless_mode_selected)
815 		goto skip_dsp_setup;
816 
817 	/* DSP base */
818 	sdev->bar[HDA_DSP_BAR] = pci_ioremap_bar(pci, HDA_DSP_BAR);
819 	if (!sdev->bar[HDA_DSP_BAR]) {
820 		dev_err(sdev->dev, "error: ioremap error\n");
821 		ret = -ENXIO;
822 		goto hdac_bus_unmap;
823 	}
824 
825 	sdev->mmio_bar = HDA_DSP_BAR;
826 	sdev->mailbox_bar = HDA_DSP_BAR;
827 skip_dsp_setup:
828 
829 	/* allow 64bit DMA address if supported by H/W */
830 	if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(64))) {
831 		dev_dbg(sdev->dev, "DMA mask is 32 bit\n");
832 		dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32));
833 	}
834 	dma_set_max_seg_size(&pci->dev, UINT_MAX);
835 
836 	/* init streams */
837 	ret = hda_dsp_stream_init(sdev);
838 	if (ret < 0) {
839 		dev_err(sdev->dev, "error: failed to init streams\n");
840 		/*
841 		 * not all errors are due to memory issues, but trying
842 		 * to free everything does not harm
843 		 */
844 		goto free_streams;
845 	}
846 
847 	/*
848 	 * register our IRQ
849 	 * let's try to enable msi firstly
850 	 * if it fails, use legacy interrupt mode
851 	 * TODO: support msi multiple vectors
852 	 */
853 	if (hda_use_msi && pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI) > 0) {
854 		dev_info(sdev->dev, "use msi interrupt mode\n");
855 		sdev->ipc_irq = pci_irq_vector(pci, 0);
856 		/* initialised to "false" by kzalloc() */
857 		sdev->msi_enabled = true;
858 	}
859 
860 	if (!sdev->msi_enabled) {
861 		dev_info(sdev->dev, "use legacy interrupt mode\n");
862 		/*
863 		 * in IO-APIC mode, hda->irq and ipc_irq are using the same
864 		 * irq number of pci->irq
865 		 */
866 		sdev->ipc_irq = pci->irq;
867 	}
868 
869 	dev_dbg(sdev->dev, "using IPC IRQ %d\n", sdev->ipc_irq);
870 	ret = request_threaded_irq(sdev->ipc_irq, hda_dsp_interrupt_handler,
871 				   hda_dsp_interrupt_thread,
872 				   IRQF_SHARED, "AudioDSP", sdev);
873 	if (ret < 0) {
874 		dev_err(sdev->dev, "error: failed to register IPC IRQ %d\n",
875 			sdev->ipc_irq);
876 		goto free_irq_vector;
877 	}
878 
879 	pci_set_master(pci);
880 	synchronize_irq(pci->irq);
881 
882 	/*
883 	 * clear TCSEL to clear playback on some HD Audio
884 	 * codecs. PCI TCSEL is defined in the Intel manuals.
885 	 */
886 	snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
887 
888 	/* init HDA capabilities */
889 	ret = hda_init_caps(sdev);
890 	if (ret < 0)
891 		goto free_ipc_irq;
892 
893 	if (!sdev->dspless_mode_selected) {
894 		/* enable ppcap interrupt */
895 		hda_dsp_ctrl_ppcap_enable(sdev, true);
896 		hda_dsp_ctrl_ppcap_int_enable(sdev, true);
897 
898 		/* set default mailbox offset for FW ready message */
899 		sdev->dsp_box.offset = HDA_DSP_MBOX_UPLINK_OFFSET;
900 
901 		INIT_DELAYED_WORK(&hdev->d0i3_work, hda_dsp_d0i3_work);
902 	}
903 
904 	chip = get_chip_info(sdev->pdata);
905 	if (chip && chip->hw_ip_version >= SOF_INTEL_ACE_2_0) {
906 		ret = hda_sdw_startup(sdev);
907 		if (ret < 0) {
908 			dev_err(sdev->dev, "could not startup SoundWire links\n");
909 			goto disable_pp_cap;
910 		}
911 	}
912 
913 	init_waitqueue_head(&hdev->waitq);
914 
915 	hdev->nhlt = intel_nhlt_init(sdev->dev);
916 
917 	return 0;
918 
919 disable_pp_cap:
920 	if (!sdev->dspless_mode_selected) {
921 		hda_dsp_ctrl_ppcap_int_enable(sdev, false);
922 		hda_dsp_ctrl_ppcap_enable(sdev, false);
923 	}
924 free_ipc_irq:
925 	free_irq(sdev->ipc_irq, sdev);
926 free_irq_vector:
927 	if (sdev->msi_enabled)
928 		pci_free_irq_vectors(pci);
929 free_streams:
930 	hda_dsp_stream_free(sdev);
931 /* dsp_unmap: not currently used */
932 	if (!sdev->dspless_mode_selected)
933 		iounmap(sdev->bar[HDA_DSP_BAR]);
934 hdac_bus_unmap:
935 	platform_device_unregister(hdev->dmic_dev);
936 
937 	return ret;
938 }
939 EXPORT_SYMBOL_NS(hda_dsp_probe, "SND_SOC_SOF_INTEL_HDA_GENERIC");
940 
hda_dsp_remove(struct snd_sof_dev * sdev)941 void hda_dsp_remove(struct snd_sof_dev *sdev)
942 {
943 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
944 	const struct sof_intel_dsp_desc *chip = hda->desc;
945 	struct pci_dev *pci = to_pci_dev(sdev->dev);
946 	struct nhlt_acpi_table *nhlt = hda->nhlt;
947 
948 	if (nhlt)
949 		intel_nhlt_free(nhlt);
950 
951 	if (!sdev->dspless_mode_selected)
952 		/* cancel any attempt for DSP D0I3 */
953 		cancel_delayed_work_sync(&hda->d0i3_work);
954 
955 	hda_codec_device_remove(sdev);
956 
957 	hda_sdw_exit(sdev);
958 
959 	if (!IS_ERR_OR_NULL(hda->dmic_dev))
960 		platform_device_unregister(hda->dmic_dev);
961 
962 	if (!sdev->dspless_mode_selected) {
963 		/* disable DSP IRQ */
964 		hda_dsp_ctrl_ppcap_int_enable(sdev, false);
965 	}
966 
967 	/* disable CIE and GIE interrupts */
968 	snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
969 				SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN, 0);
970 
971 	if (sdev->dspless_mode_selected)
972 		goto skip_disable_dsp;
973 
974 	/* Cancel the microphone privacy work if mic privacy is active */
975 	if (hda->mic_privacy.active)
976 		cancel_work_sync(&hda->mic_privacy.work);
977 
978 	/* no need to check for error as the DSP will be disabled anyway */
979 	if (chip && chip->power_down_dsp)
980 		chip->power_down_dsp(sdev);
981 
982 	/* disable DSP */
983 	hda_dsp_ctrl_ppcap_enable(sdev, false);
984 
985 	/* Free the persistent DMA buffers used for base firmware download */
986 	if (hda->cl_dmab.area)
987 		snd_dma_free_pages(&hda->cl_dmab);
988 	if (hda->iccmax_dmab.area)
989 		snd_dma_free_pages(&hda->iccmax_dmab);
990 
991 skip_disable_dsp:
992 	free_irq(sdev->ipc_irq, sdev);
993 	if (sdev->msi_enabled)
994 		pci_free_irq_vectors(pci);
995 
996 	hda_dsp_stream_free(sdev);
997 
998 	hda_bus_ml_free(sof_to_bus(sdev));
999 
1000 	if (!sdev->dspless_mode_selected)
1001 		iounmap(sdev->bar[HDA_DSP_BAR]);
1002 }
1003 EXPORT_SYMBOL_NS(hda_dsp_remove, "SND_SOC_SOF_INTEL_HDA_GENERIC");
1004 
hda_dsp_remove_late(struct snd_sof_dev * sdev)1005 void hda_dsp_remove_late(struct snd_sof_dev *sdev)
1006 {
1007 	iounmap(sof_to_bus(sdev)->remap_addr);
1008 	sof_hda_bus_exit(sdev);
1009 	hda_codec_i915_exit(sdev);
1010 }
1011 
hda_power_down_dsp(struct snd_sof_dev * sdev)1012 int hda_power_down_dsp(struct snd_sof_dev *sdev)
1013 {
1014 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
1015 	const struct sof_intel_dsp_desc *chip = hda->desc;
1016 
1017 	return hda_dsp_core_reset_power_down(sdev, chip->host_managed_cores_mask);
1018 }
1019 EXPORT_SYMBOL_NS(hda_power_down_dsp, "SND_SOC_SOF_INTEL_HDA_GENERIC");
1020 
1021 #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)1022 static void hda_generic_machine_select(struct snd_sof_dev *sdev,
1023 				       struct snd_soc_acpi_mach **mach)
1024 {
1025 	struct hdac_bus *bus = sof_to_bus(sdev);
1026 	struct snd_soc_acpi_mach_params *mach_params;
1027 	struct snd_soc_acpi_mach *hda_mach;
1028 	struct snd_sof_pdata *pdata = sdev->pdata;
1029 	const char *tplg_filename;
1030 	int codec_num = 0;
1031 	int i;
1032 
1033 	/* codec detection */
1034 	if (!bus->codec_mask) {
1035 		dev_info(bus->dev, "no hda codecs found!\n");
1036 	} else {
1037 		dev_info(bus->dev, "hda codecs found, mask %lx\n",
1038 			 bus->codec_mask);
1039 
1040 		for (i = 0; i < HDA_MAX_CODECS; i++) {
1041 			if (bus->codec_mask & (1 << i))
1042 				codec_num++;
1043 		}
1044 
1045 		/*
1046 		 * If no machine driver is found, then:
1047 		 *
1048 		 * generic hda machine driver can handle:
1049 		 *  - one HDMI codec, and/or
1050 		 *  - one external HDAudio codec
1051 		 */
1052 		if (!*mach && codec_num <= 2) {
1053 			bool tplg_fixup = false;
1054 
1055 			/*
1056 			 * make a local copy of the match array since we might
1057 			 * be modifying it
1058 			 */
1059 			hda_mach = devm_kmemdup_array(sdev->dev,
1060 					snd_soc_acpi_intel_hda_machines,
1061 					2, /* we have one entry + sentinel in the array */
1062 					sizeof(snd_soc_acpi_intel_hda_machines[0]),
1063 					GFP_KERNEL);
1064 			if (!hda_mach) {
1065 				dev_err(bus->dev,
1066 					"%s: failed to duplicate the HDA match table\n",
1067 					__func__);
1068 				return;
1069 			}
1070 
1071 			dev_info(bus->dev, "using HDA machine driver %s now\n",
1072 				 hda_mach->drv_name);
1073 
1074 			/*
1075 			 * topology: use the info from hda_machines since tplg file name
1076 			 * is not overwritten
1077 			 */
1078 			if (!pdata->tplg_filename)
1079 				tplg_fixup = true;
1080 
1081 			if (tplg_fixup &&
1082 			    codec_num == 1 && HDA_IDISP_CODEC(bus->codec_mask)) {
1083 				tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1084 							       "%s-idisp",
1085 							       hda_mach->sof_tplg_filename);
1086 				if (!tplg_filename)
1087 					return;
1088 
1089 				hda_mach->sof_tplg_filename = tplg_filename;
1090 			}
1091 
1092 			if (codec_num == 2 ||
1093 			    (codec_num == 1 && !HDA_IDISP_CODEC(bus->codec_mask))) {
1094 				/*
1095 				 * Prevent SoundWire links from starting when an external
1096 				 * HDaudio codec is used
1097 				 */
1098 				hda_mach->mach_params.link_mask = 0;
1099 			} else {
1100 				/*
1101 				 * Allow SoundWire links to start when no external HDaudio codec
1102 				 * was detected. This will not create a SoundWire card but
1103 				 * will help detect if any SoundWire codec reports as ATTACHED.
1104 				 */
1105 				struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
1106 
1107 				hda_mach->mach_params.link_mask = hdev->info.link_mask;
1108 			}
1109 
1110 			*mach = hda_mach;
1111 		}
1112 	}
1113 
1114 	/* used by hda machine driver to create dai links */
1115 	if (*mach) {
1116 		mach_params = &(*mach)->mach_params;
1117 		mach_params->codec_mask = bus->codec_mask;
1118 	}
1119 }
1120 #else
hda_generic_machine_select(struct snd_sof_dev * sdev,struct snd_soc_acpi_mach ** mach)1121 static void hda_generic_machine_select(struct snd_sof_dev *sdev,
1122 				       struct snd_soc_acpi_mach **mach)
1123 {
1124 }
1125 #endif
1126 
1127 #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
1128 
hda_sdw_machine_select(struct snd_sof_dev * sdev)1129 static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
1130 {
1131 	struct snd_sof_pdata *pdata = sdev->pdata;
1132 	const struct snd_soc_acpi_link_adr *link;
1133 	struct sdw_peripherals *peripherals;
1134 	struct snd_soc_acpi_mach *mach;
1135 	struct sof_intel_hda_dev *hdev;
1136 	u32 link_mask;
1137 	int i;
1138 
1139 	hdev = pdata->hw_pdata;
1140 	link_mask = hdev->info.link_mask;
1141 
1142 	if (!link_mask) {
1143 		dev_info(sdev->dev, "SoundWire links not enabled\n");
1144 		return NULL;
1145 	}
1146 
1147 	if (!hdev->sdw) {
1148 		dev_dbg(sdev->dev, "SoundWire context not allocated\n");
1149 		return NULL;
1150 	}
1151 
1152 	if (!hdev->sdw->peripherals || !hdev->sdw->peripherals->num_peripherals) {
1153 		dev_warn(sdev->dev, "No SoundWire peripheral detected in ACPI tables\n");
1154 		return NULL;
1155 	}
1156 
1157 	/*
1158 	 * Select SoundWire machine driver if needed using the
1159 	 * alternate tables. This case deals with SoundWire-only
1160 	 * machines, for mixed cases with I2C/I2S the detection relies
1161 	 * on the HID list.
1162 	 */
1163 	for (mach = pdata->desc->alt_machines;
1164 	     mach && mach->link_mask; mach++) {
1165 		/*
1166 		 * On some platforms such as Up Extreme all links
1167 		 * are enabled but only one link can be used by
1168 		 * external codec. Instead of exact match of two masks,
1169 		 * first check whether link_mask of mach is subset of
1170 		 * link_mask supported by hw and then go on searching
1171 		 * link_adr
1172 		 */
1173 		if (~link_mask & mach->link_mask)
1174 			continue;
1175 
1176 		/* No need to match adr if there is no links defined */
1177 		if (!mach->links)
1178 			break;
1179 
1180 		link = mach->links;
1181 		for (i = 0; i < hdev->info.count && link->num_adr;
1182 		     i++, link++) {
1183 			/*
1184 			 * Try next machine if any expected Slaves
1185 			 * are not found on this link.
1186 			 */
1187 			if (!snd_soc_acpi_sdw_link_slaves_found(sdev->dev, link,
1188 								hdev->sdw->peripherals))
1189 				break;
1190 		}
1191 		/* Found if all Slaves are checked */
1192 		if (i == hdev->info.count || !link->num_adr)
1193 			if (!mach->machine_check || mach->machine_check(hdev->sdw))
1194 				break;
1195 	}
1196 	if (mach && mach->link_mask) {
1197 		mach->mach_params.links = mach->links;
1198 		mach->mach_params.link_mask = mach->link_mask;
1199 		mach->mach_params.platform = dev_name(sdev->dev);
1200 
1201 		return mach;
1202 	}
1203 
1204 	dev_info(sdev->dev, "No SoundWire machine driver found for the ACPI-reported configuration:\n");
1205 	peripherals = hdev->sdw->peripherals;
1206 	for (i = 0; i < peripherals->num_peripherals; i++)
1207 		dev_info(sdev->dev, "link %d mfg_id 0x%04x part_id 0x%04x version %#x\n",
1208 			 peripherals->array[i]->bus->link_id,
1209 			 peripherals->array[i]->id.mfg_id,
1210 			 peripherals->array[i]->id.part_id,
1211 			 peripherals->array[i]->id.sdw_version);
1212 
1213 	return NULL;
1214 }
1215 #else
hda_sdw_machine_select(struct snd_sof_dev * sdev)1216 static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
1217 {
1218 	return NULL;
1219 }
1220 #endif
1221 
hda_set_mach_params(struct snd_soc_acpi_mach * mach,struct snd_sof_dev * sdev)1222 void hda_set_mach_params(struct snd_soc_acpi_mach *mach,
1223 			 struct snd_sof_dev *sdev)
1224 {
1225 	struct snd_sof_pdata *pdata = sdev->pdata;
1226 	const struct sof_dev_desc *desc = pdata->desc;
1227 	struct snd_soc_acpi_mach_params *mach_params;
1228 
1229 	mach_params = &mach->mach_params;
1230 	mach_params->platform = dev_name(sdev->dev);
1231 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
1232 	    sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
1233 		mach_params->num_dai_drivers = SOF_SKL_NUM_DAIS_NOCODEC;
1234 	else
1235 		mach_params->num_dai_drivers = desc->ops->num_drv;
1236 	mach_params->dai_drivers = desc->ops->drv;
1237 }
1238 
check_tplg_quirk_mask(struct snd_soc_acpi_mach * mach)1239 static int check_tplg_quirk_mask(struct snd_soc_acpi_mach *mach)
1240 {
1241 	u32 dmic_ssp_quirk;
1242 	u32 codec_amp_name_quirk;
1243 
1244 	/*
1245 	 * In current implementation dmic and ssp quirks are designed for es8336
1246 	 * machine driver and could not be mixed with codec name and amp name
1247 	 * quirks.
1248 	 */
1249 	dmic_ssp_quirk = mach->tplg_quirk_mask &
1250 			 (SND_SOC_ACPI_TPLG_INTEL_DMIC_NUMBER | SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER);
1251 	codec_amp_name_quirk = mach->tplg_quirk_mask &
1252 			 (SND_SOC_ACPI_TPLG_INTEL_AMP_NAME | SND_SOC_ACPI_TPLG_INTEL_CODEC_NAME);
1253 
1254 	if (dmic_ssp_quirk && codec_amp_name_quirk)
1255 		return -EINVAL;
1256 
1257 	return 0;
1258 }
1259 
remove_file_ext(struct device * dev,const char * tplg_filename)1260 static char *remove_file_ext(struct device *dev, const char *tplg_filename)
1261 {
1262 	char *filename, *tmp;
1263 
1264 	filename = devm_kstrdup(dev, tplg_filename, GFP_KERNEL);
1265 	if (!filename)
1266 		return NULL;
1267 
1268 	/* remove file extension if exist */
1269 	tmp = filename;
1270 	return strsep(&tmp, ".");
1271 }
1272 
hda_machine_select(struct snd_sof_dev * sdev)1273 struct snd_soc_acpi_mach *hda_machine_select(struct snd_sof_dev *sdev)
1274 {
1275 	u32 interface_mask = hda_get_interface_mask(sdev);
1276 	struct snd_sof_pdata *sof_pdata = sdev->pdata;
1277 	const struct sof_dev_desc *desc = sof_pdata->desc;
1278 	struct hdac_bus *bus = sof_to_bus(sdev);
1279 	struct snd_soc_acpi_mach *mach = NULL;
1280 	enum snd_soc_acpi_intel_codec codec_type, amp_type;
1281 	const char *tplg_filename;
1282 	const char *tplg_suffix;
1283 	bool amp_name_valid;
1284 	bool i2s_mach_found = false;
1285 	bool sdw_mach_found = false;
1286 
1287 	/* Try I2S or DMIC if it is supported */
1288 	if (interface_mask & (BIT(SOF_DAI_INTEL_SSP) | BIT(SOF_DAI_INTEL_DMIC))) {
1289 		mach = snd_soc_acpi_find_machine(desc->machines);
1290 		if (mach)
1291 			i2s_mach_found = true;
1292 	}
1293 
1294 	/*
1295 	 * If I2S fails and no external HDaudio codec is detected,
1296 	 * try SoundWire if it is supported
1297 	 */
1298 	if (!mach && !HDA_EXT_CODEC(bus->codec_mask) &&
1299 	    (interface_mask & BIT(SOF_DAI_INTEL_ALH))) {
1300 		mach = hda_sdw_machine_select(sdev);
1301 		if (mach)
1302 			sdw_mach_found = true;
1303 	}
1304 
1305 	/*
1306 	 * Choose HDA generic machine driver if mach is NULL.
1307 	 * Otherwise, set certain mach params.
1308 	 */
1309 	hda_generic_machine_select(sdev, &mach);
1310 	if (!mach) {
1311 		dev_warn(sdev->dev, "warning: No matching ASoC machine driver found\n");
1312 		return NULL;
1313 	}
1314 
1315 	/* report BT offload link mask to machine driver */
1316 	mach->mach_params.bt_link_mask = check_nhlt_ssp_mask(sdev, NHLT_DEVICE_BT);
1317 
1318 	dev_info(sdev->dev, "BT link detected in NHLT tables: %#x\n",
1319 		 mach->mach_params.bt_link_mask);
1320 
1321 	/* allow for module parameter override */
1322 	if (bt_link_mask_override) {
1323 		dev_dbg(sdev->dev, "overriding BT link detected in NHLT tables %#x by kernel param %#x\n",
1324 			mach->mach_params.bt_link_mask, bt_link_mask_override);
1325 		mach->mach_params.bt_link_mask = bt_link_mask_override;
1326 	}
1327 
1328 	if (hweight_long(mach->mach_params.bt_link_mask) > 1) {
1329 		dev_warn(sdev->dev, "invalid BT link mask %#x found, reset the mask\n",
1330 			mach->mach_params.bt_link_mask);
1331 		mach->mach_params.bt_link_mask = 0;
1332 	}
1333 
1334 	/*
1335 	 * Fixup tplg file name by appending dmic num, ssp num, codec/amplifier
1336 	 * name string if quirk flag is set.
1337 	 */
1338 	if (mach) {
1339 		bool tplg_fixup = false;
1340 		bool dmic_fixup = false;
1341 
1342 		/*
1343 		 * If tplg file name is overridden, use it instead of
1344 		 * the one set in mach table
1345 		 */
1346 		if (!sof_pdata->tplg_filename) {
1347 			/* remove file extension if it exists */
1348 			tplg_filename = remove_file_ext(sdev->dev, mach->sof_tplg_filename);
1349 			if (!tplg_filename)
1350 				return NULL;
1351 
1352 			sof_pdata->tplg_filename = tplg_filename;
1353 			tplg_fixup = true;
1354 		}
1355 
1356 		/*
1357 		 * Checking quirk mask integrity; some quirk flags could not be
1358 		 * set concurrently.
1359 		 */
1360 		if (tplg_fixup &&
1361 		    check_tplg_quirk_mask(mach)) {
1362 			dev_err(sdev->dev, "Invalid tplg quirk mask 0x%x\n",
1363 				mach->tplg_quirk_mask);
1364 			return NULL;
1365 		}
1366 
1367 		/* report to machine driver if any DMICs are found */
1368 		mach->mach_params.dmic_num = check_dmic_num(sdev);
1369 
1370 		if (sdw_mach_found || mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_DMIC_NUMBER)
1371 			dmic_fixup = true;
1372 
1373 		if (tplg_fixup &&
1374 		    dmic_fixup &&
1375 		    mach->mach_params.dmic_num) {
1376 			tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1377 						       "%s%s%d%s",
1378 						       sof_pdata->tplg_filename,
1379 						       i2s_mach_found ? "-dmic" : "-",
1380 						       mach->mach_params.dmic_num,
1381 						       "ch");
1382 			if (!tplg_filename)
1383 				return NULL;
1384 
1385 			sof_pdata->tplg_filename = tplg_filename;
1386 		}
1387 
1388 		if (mach->link_mask) {
1389 			mach->mach_params.links = mach->links;
1390 			mach->mach_params.link_mask = mach->link_mask;
1391 		}
1392 
1393 		/* report SSP link mask to machine driver */
1394 		mach->mach_params.i2s_link_mask = check_nhlt_ssp_mask(sdev, NHLT_DEVICE_I2S);
1395 
1396 		if (tplg_fixup &&
1397 		    mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER &&
1398 		    mach->mach_params.i2s_link_mask) {
1399 			const struct sof_intel_dsp_desc *chip = get_chip_info(sdev->pdata);
1400 			int ssp_num;
1401 			int mclk_mask;
1402 
1403 			if (hweight_long(mach->mach_params.i2s_link_mask) > 1 &&
1404 			    !(mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_MSB))
1405 				dev_warn(sdev->dev, "More than one SSP exposed by NHLT, choosing MSB\n");
1406 
1407 			/* fls returns 1-based results, SSPs indices are 0-based */
1408 			ssp_num = fls(mach->mach_params.i2s_link_mask) - 1;
1409 
1410 			if (ssp_num >= chip->ssp_count) {
1411 				dev_err(sdev->dev, "Invalid SSP %d, max on this platform is %d\n",
1412 					ssp_num, chip->ssp_count);
1413 				return NULL;
1414 			}
1415 
1416 			tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1417 						       "%s%s%d",
1418 						       sof_pdata->tplg_filename,
1419 						       "-ssp",
1420 						       ssp_num);
1421 			if (!tplg_filename)
1422 				return NULL;
1423 
1424 			sof_pdata->tplg_filename = tplg_filename;
1425 
1426 			mclk_mask = check_nhlt_ssp_mclk_mask(sdev, ssp_num);
1427 
1428 			if (mclk_mask < 0) {
1429 				dev_err(sdev->dev, "Invalid MCLK configuration\n");
1430 				return NULL;
1431 			}
1432 
1433 			dev_dbg(sdev->dev, "MCLK mask %#x found in NHLT\n", mclk_mask);
1434 
1435 			if (mclk_mask) {
1436 				dev_info(sdev->dev, "Overriding topology with MCLK mask %#x from NHLT\n", mclk_mask);
1437 				sdev->mclk_id_override = true;
1438 				sdev->mclk_id_quirk = (mclk_mask & BIT(0)) ? 0 : 1;
1439 			}
1440 		}
1441 
1442 		amp_type = snd_soc_acpi_intel_detect_amp_type(sdev->dev);
1443 		codec_type = snd_soc_acpi_intel_detect_codec_type(sdev->dev);
1444 		amp_name_valid = amp_type != CODEC_NONE && amp_type != codec_type;
1445 
1446 		if (tplg_fixup && amp_name_valid &&
1447 		    mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_AMP_NAME) {
1448 			tplg_suffix = snd_soc_acpi_intel_get_amp_tplg_suffix(amp_type);
1449 			if (!tplg_suffix) {
1450 				dev_err(sdev->dev, "no tplg suffix found, amp %d\n",
1451 					amp_type);
1452 				return NULL;
1453 			}
1454 
1455 			tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1456 						       "%s-%s",
1457 						       sof_pdata->tplg_filename,
1458 						       tplg_suffix);
1459 			if (!tplg_filename)
1460 				return NULL;
1461 
1462 			sof_pdata->tplg_filename = tplg_filename;
1463 		}
1464 
1465 
1466 		if (tplg_fixup &&
1467 		    mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_CODEC_NAME &&
1468 		    codec_type != CODEC_NONE) {
1469 			tplg_suffix = snd_soc_acpi_intel_get_codec_tplg_suffix(codec_type);
1470 			if (!tplg_suffix) {
1471 				dev_err(sdev->dev, "no tplg suffix found, codec %d\n",
1472 					codec_type);
1473 				return NULL;
1474 			}
1475 
1476 			tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1477 						       "%s-%s",
1478 						       sof_pdata->tplg_filename,
1479 						       tplg_suffix);
1480 			if (!tplg_filename)
1481 				return NULL;
1482 
1483 			sof_pdata->tplg_filename = tplg_filename;
1484 		}
1485 
1486 		if (tplg_fixup) {
1487 			tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1488 						       "%s%s",
1489 						       sof_pdata->tplg_filename,
1490 						       ".tplg");
1491 			if (!tplg_filename)
1492 				return NULL;
1493 
1494 			sof_pdata->tplg_filename = tplg_filename;
1495 		}
1496 
1497 		/* check if mclk_id should be modified from topology defaults */
1498 		if (mclk_id_override >= 0) {
1499 			dev_info(sdev->dev, "Overriding topology with MCLK %d from kernel_parameter\n", mclk_id_override);
1500 			sdev->mclk_id_override = true;
1501 			sdev->mclk_id_quirk = mclk_id_override;
1502 		}
1503 	}
1504 
1505 	return mach;
1506 }
1507 
hda_pci_intel_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)1508 int hda_pci_intel_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
1509 {
1510 	int ret;
1511 
1512 	ret = snd_intel_dsp_driver_probe(pci);
1513 	if (ret != SND_INTEL_DSP_DRIVER_ANY && ret != SND_INTEL_DSP_DRIVER_SOF) {
1514 		dev_dbg(&pci->dev, "SOF PCI driver not selected, aborting probe\n");
1515 		return -ENODEV;
1516 	}
1517 
1518 	return sof_pci_probe(pci, pci_id);
1519 }
1520 EXPORT_SYMBOL_NS(hda_pci_intel_probe, "SND_SOC_SOF_INTEL_HDA_GENERIC");
1521 
hda_register_clients(struct snd_sof_dev * sdev)1522 int hda_register_clients(struct snd_sof_dev *sdev)
1523 {
1524 	return hda_probes_register(sdev);
1525 }
1526 
hda_unregister_clients(struct snd_sof_dev * sdev)1527 void hda_unregister_clients(struct snd_sof_dev *sdev)
1528 {
1529 	hda_probes_unregister(sdev);
1530 }
1531 
1532 MODULE_LICENSE("Dual BSD/GPL");
1533 MODULE_DESCRIPTION("SOF support for HDaudio platforms");
1534 MODULE_IMPORT_NS("SND_SOC_SOF_PCI_DEV");
1535 MODULE_IMPORT_NS("SND_SOC_SOF_HDA_AUDIO_CODEC");
1536 MODULE_IMPORT_NS("SND_SOC_SOF_HDA_AUDIO_CODEC_I915");
1537 MODULE_IMPORT_NS("SND_SOC_SOF_XTENSA");
1538 MODULE_IMPORT_NS("SND_INTEL_SOUNDWIRE_ACPI");
1539 MODULE_IMPORT_NS("SOUNDWIRE_INTEL_INIT");
1540 MODULE_IMPORT_NS("SOUNDWIRE_INTEL");
1541 MODULE_IMPORT_NS("SND_SOC_SOF_HDA_MLINK");
1542 MODULE_IMPORT_NS("SND_SOC_SOF_INTEL_HDA_COMMON");
1543 MODULE_IMPORT_NS("SND_SOC_ACPI_INTEL_MATCH");
1544