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