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