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 /*
1053 * make a local copy of the match array since we might
1054 * be modifying it
1055 */
1056 hda_mach = devm_kmemdup_array(sdev->dev,
1057 snd_soc_acpi_intel_hda_machines,
1058 2, /* we have one entry + sentinel in the array */
1059 sizeof(snd_soc_acpi_intel_hda_machines[0]),
1060 GFP_KERNEL);
1061 if (!hda_mach) {
1062 dev_err(bus->dev,
1063 "%s: failed to duplicate the HDA match table\n",
1064 __func__);
1065 return;
1066 }
1067
1068 dev_info(bus->dev, "using HDA machine driver %s now\n",
1069 hda_mach->drv_name);
1070
1071 /*
1072 * topology: use the info from hda_machines since tplg file name
1073 * is not overwritten
1074 */
1075 if (!pdata->tplg_filename)
1076 tplg_fixup = true;
1077
1078 if (tplg_fixup &&
1079 codec_num == 1 && HDA_IDISP_CODEC(bus->codec_mask)) {
1080 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1081 "%s-idisp",
1082 hda_mach->sof_tplg_filename);
1083 if (!tplg_filename)
1084 return;
1085
1086 hda_mach->sof_tplg_filename = tplg_filename;
1087 }
1088
1089 if (codec_num == 2 ||
1090 (codec_num == 1 && !HDA_IDISP_CODEC(bus->codec_mask))) {
1091 /*
1092 * Prevent SoundWire links from starting when an external
1093 * HDaudio codec is used
1094 */
1095 hda_mach->mach_params.link_mask = 0;
1096 } else {
1097 /*
1098 * Allow SoundWire links to start when no external HDaudio codec
1099 * was detected. This will not create a SoundWire card but
1100 * will help detect if any SoundWire codec reports as ATTACHED.
1101 */
1102 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
1103
1104 hda_mach->mach_params.link_mask = hdev->info.link_mask;
1105 }
1106
1107 *mach = hda_mach;
1108 }
1109 }
1110
1111 /* used by hda machine driver to create dai links */
1112 if (*mach) {
1113 mach_params = &(*mach)->mach_params;
1114 mach_params->codec_mask = bus->codec_mask;
1115 }
1116 }
1117 #else
hda_generic_machine_select(struct snd_sof_dev * sdev,struct snd_soc_acpi_mach ** mach)1118 static void hda_generic_machine_select(struct snd_sof_dev *sdev,
1119 struct snd_soc_acpi_mach **mach)
1120 {
1121 }
1122 #endif
1123
1124 #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
1125
hda_sdw_machine_select(struct snd_sof_dev * sdev)1126 static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
1127 {
1128 struct snd_sof_pdata *pdata = sdev->pdata;
1129 const struct snd_soc_acpi_link_adr *link;
1130 struct sdw_peripherals *peripherals;
1131 struct snd_soc_acpi_mach *mach;
1132 struct sof_intel_hda_dev *hdev;
1133 u32 link_mask;
1134 int i;
1135
1136 hdev = pdata->hw_pdata;
1137 link_mask = hdev->info.link_mask;
1138
1139 if (!link_mask) {
1140 dev_info(sdev->dev, "SoundWire links not enabled\n");
1141 return NULL;
1142 }
1143
1144 if (!hdev->sdw) {
1145 dev_dbg(sdev->dev, "SoundWire context not allocated\n");
1146 return NULL;
1147 }
1148
1149 if (!hdev->sdw->peripherals || !hdev->sdw->peripherals->num_peripherals) {
1150 dev_warn(sdev->dev, "No SoundWire peripheral detected in ACPI tables\n");
1151 return NULL;
1152 }
1153
1154 /*
1155 * Select SoundWire machine driver if needed using the
1156 * alternate tables. This case deals with SoundWire-only
1157 * machines, for mixed cases with I2C/I2S the detection relies
1158 * on the HID list.
1159 */
1160 for (mach = pdata->desc->alt_machines;
1161 mach && mach->link_mask; mach++) {
1162 /*
1163 * On some platforms such as Up Extreme all links
1164 * are enabled but only one link can be used by
1165 * external codec. Instead of exact match of two masks,
1166 * first check whether link_mask of mach is subset of
1167 * link_mask supported by hw and then go on searching
1168 * link_adr
1169 */
1170 if (~link_mask & mach->link_mask)
1171 continue;
1172
1173 /* No need to match adr if there is no links defined */
1174 if (!mach->links)
1175 break;
1176
1177 link = mach->links;
1178 for (i = 0; i < hdev->info.count && link->num_adr;
1179 i++, link++) {
1180 /*
1181 * Try next machine if any expected Slaves
1182 * are not found on this link.
1183 */
1184 if (!snd_soc_acpi_sdw_link_slaves_found(sdev->dev, link,
1185 hdev->sdw->peripherals))
1186 break;
1187 }
1188 /* Found if all Slaves are checked */
1189 if (i == hdev->info.count || !link->num_adr)
1190 if (!mach->machine_check || mach->machine_check(hdev->sdw))
1191 break;
1192 }
1193 if (mach && mach->link_mask) {
1194 mach->mach_params.links = mach->links;
1195 mach->mach_params.link_mask = mach->link_mask;
1196 mach->mach_params.platform = dev_name(sdev->dev);
1197
1198 return mach;
1199 }
1200
1201 dev_info(sdev->dev, "No SoundWire machine driver found for the ACPI-reported configuration:\n");
1202 peripherals = hdev->sdw->peripherals;
1203 for (i = 0; i < peripherals->num_peripherals; i++)
1204 dev_info(sdev->dev, "link %d mfg_id 0x%04x part_id 0x%04x version %#x\n",
1205 peripherals->array[i]->bus->link_id,
1206 peripherals->array[i]->id.mfg_id,
1207 peripherals->array[i]->id.part_id,
1208 peripherals->array[i]->id.sdw_version);
1209
1210 return NULL;
1211 }
1212 #else
hda_sdw_machine_select(struct snd_sof_dev * sdev)1213 static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
1214 {
1215 return NULL;
1216 }
1217 #endif
1218
hda_set_mach_params(struct snd_soc_acpi_mach * mach,struct snd_sof_dev * sdev)1219 void hda_set_mach_params(struct snd_soc_acpi_mach *mach,
1220 struct snd_sof_dev *sdev)
1221 {
1222 struct snd_sof_pdata *pdata = sdev->pdata;
1223 const struct sof_dev_desc *desc = pdata->desc;
1224 struct snd_soc_acpi_mach_params *mach_params;
1225
1226 mach_params = &mach->mach_params;
1227 mach_params->platform = dev_name(sdev->dev);
1228 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
1229 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
1230 mach_params->num_dai_drivers = SOF_SKL_NUM_DAIS_NOCODEC;
1231 else
1232 mach_params->num_dai_drivers = desc->ops->num_drv;
1233 mach_params->dai_drivers = desc->ops->drv;
1234 }
1235
check_tplg_quirk_mask(struct snd_soc_acpi_mach * mach)1236 static int check_tplg_quirk_mask(struct snd_soc_acpi_mach *mach)
1237 {
1238 u32 dmic_ssp_quirk;
1239 u32 codec_amp_name_quirk;
1240
1241 /*
1242 * In current implementation dmic and ssp quirks are designed for es8336
1243 * machine driver and could not be mixed with codec name and amp name
1244 * quirks.
1245 */
1246 dmic_ssp_quirk = mach->tplg_quirk_mask &
1247 (SND_SOC_ACPI_TPLG_INTEL_DMIC_NUMBER | SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER);
1248 codec_amp_name_quirk = mach->tplg_quirk_mask &
1249 (SND_SOC_ACPI_TPLG_INTEL_AMP_NAME | SND_SOC_ACPI_TPLG_INTEL_CODEC_NAME);
1250
1251 if (dmic_ssp_quirk && codec_amp_name_quirk)
1252 return -EINVAL;
1253
1254 return 0;
1255 }
1256
remove_file_ext(const char * tplg_filename)1257 static char *remove_file_ext(const char *tplg_filename)
1258 {
1259 char *filename, *tmp;
1260
1261 filename = kstrdup(tplg_filename, GFP_KERNEL);
1262 if (!filename)
1263 return NULL;
1264
1265 /* remove file extension if exist */
1266 tmp = filename;
1267 return strsep(&tmp, ".");
1268 }
1269
hda_machine_select(struct snd_sof_dev * sdev)1270 struct snd_soc_acpi_mach *hda_machine_select(struct snd_sof_dev *sdev)
1271 {
1272 u32 interface_mask = hda_get_interface_mask(sdev);
1273 struct snd_sof_pdata *sof_pdata = sdev->pdata;
1274 const struct sof_dev_desc *desc = sof_pdata->desc;
1275 struct hdac_bus *bus = sof_to_bus(sdev);
1276 struct snd_soc_acpi_mach *mach = NULL;
1277 enum snd_soc_acpi_intel_codec codec_type, amp_type;
1278 const char *tplg_filename;
1279 const char *tplg_suffix;
1280 bool amp_name_valid;
1281 bool i2s_mach_found = false;
1282 bool sdw_mach_found = false;
1283
1284 /* Try I2S or DMIC if it is supported */
1285 if (interface_mask & (BIT(SOF_DAI_INTEL_SSP) | BIT(SOF_DAI_INTEL_DMIC))) {
1286 mach = snd_soc_acpi_find_machine(desc->machines);
1287 if (mach)
1288 i2s_mach_found = true;
1289 }
1290
1291 /*
1292 * If I2S fails and no external HDaudio codec is detected,
1293 * try SoundWire if it is supported
1294 */
1295 if (!mach && !HDA_EXT_CODEC(bus->codec_mask) &&
1296 (interface_mask & BIT(SOF_DAI_INTEL_ALH))) {
1297 mach = hda_sdw_machine_select(sdev);
1298 if (mach)
1299 sdw_mach_found = true;
1300 }
1301
1302 /*
1303 * Choose HDA generic machine driver if mach is NULL.
1304 * Otherwise, set certain mach params.
1305 */
1306 hda_generic_machine_select(sdev, &mach);
1307 if (!mach) {
1308 dev_warn(sdev->dev, "warning: No matching ASoC machine driver found\n");
1309 return NULL;
1310 }
1311
1312 /* report BT offload link mask to machine driver */
1313 mach->mach_params.bt_link_mask = check_nhlt_ssp_mask(sdev, NHLT_DEVICE_BT);
1314
1315 dev_info(sdev->dev, "BT link detected in NHLT tables: %#x\n",
1316 mach->mach_params.bt_link_mask);
1317
1318 /* allow for module parameter override */
1319 if (bt_link_mask_override) {
1320 dev_dbg(sdev->dev, "overriding BT link detected in NHLT tables %#x by kernel param %#x\n",
1321 mach->mach_params.bt_link_mask, bt_link_mask_override);
1322 mach->mach_params.bt_link_mask = bt_link_mask_override;
1323 }
1324
1325 if (hweight_long(mach->mach_params.bt_link_mask) > 1) {
1326 dev_warn(sdev->dev, "invalid BT link mask %#x found, reset the mask\n",
1327 mach->mach_params.bt_link_mask);
1328 mach->mach_params.bt_link_mask = 0;
1329 }
1330
1331 /*
1332 * Fixup tplg file name by appending dmic num, ssp num, codec/amplifier
1333 * name string if quirk flag is set.
1334 */
1335 if (mach) {
1336 bool tplg_fixup = false;
1337 bool dmic_fixup = false;
1338
1339 /*
1340 * If tplg file name is overridden, use it instead of
1341 * the one set in mach table
1342 */
1343 if (!sof_pdata->tplg_filename) {
1344 /* remove file extension if it exists */
1345 tplg_filename = remove_file_ext(mach->sof_tplg_filename);
1346 if (!tplg_filename)
1347 return NULL;
1348
1349 sof_pdata->tplg_filename = tplg_filename;
1350 tplg_fixup = true;
1351 }
1352
1353 /*
1354 * Checking quirk mask integrity; some quirk flags could not be
1355 * set concurrently.
1356 */
1357 if (tplg_fixup &&
1358 check_tplg_quirk_mask(mach)) {
1359 dev_err(sdev->dev, "Invalid tplg quirk mask 0x%x\n",
1360 mach->tplg_quirk_mask);
1361 return NULL;
1362 }
1363
1364 /* report to machine driver if any DMICs are found */
1365 mach->mach_params.dmic_num = check_dmic_num(sdev);
1366
1367 if (sdw_mach_found || mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_DMIC_NUMBER)
1368 dmic_fixup = true;
1369
1370 if (tplg_fixup &&
1371 dmic_fixup &&
1372 mach->mach_params.dmic_num) {
1373 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1374 "%s%s%d%s",
1375 sof_pdata->tplg_filename,
1376 i2s_mach_found ? "-dmic" : "-",
1377 mach->mach_params.dmic_num,
1378 "ch");
1379 if (!tplg_filename)
1380 return NULL;
1381
1382 sof_pdata->tplg_filename = tplg_filename;
1383 }
1384
1385 if (mach->link_mask) {
1386 mach->mach_params.links = mach->links;
1387 mach->mach_params.link_mask = mach->link_mask;
1388 }
1389
1390 /* report SSP link mask to machine driver */
1391 mach->mach_params.i2s_link_mask = check_nhlt_ssp_mask(sdev, NHLT_DEVICE_I2S);
1392
1393 if (tplg_fixup &&
1394 mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER &&
1395 mach->mach_params.i2s_link_mask) {
1396 const struct sof_intel_dsp_desc *chip = get_chip_info(sdev->pdata);
1397 int ssp_num;
1398 int mclk_mask;
1399
1400 if (hweight_long(mach->mach_params.i2s_link_mask) > 1 &&
1401 !(mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_MSB))
1402 dev_warn(sdev->dev, "More than one SSP exposed by NHLT, choosing MSB\n");
1403
1404 /* fls returns 1-based results, SSPs indices are 0-based */
1405 ssp_num = fls(mach->mach_params.i2s_link_mask) - 1;
1406
1407 if (ssp_num >= chip->ssp_count) {
1408 dev_err(sdev->dev, "Invalid SSP %d, max on this platform is %d\n",
1409 ssp_num, chip->ssp_count);
1410 return NULL;
1411 }
1412
1413 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1414 "%s%s%d",
1415 sof_pdata->tplg_filename,
1416 "-ssp",
1417 ssp_num);
1418 if (!tplg_filename)
1419 return NULL;
1420
1421 sof_pdata->tplg_filename = tplg_filename;
1422
1423 mclk_mask = check_nhlt_ssp_mclk_mask(sdev, ssp_num);
1424
1425 if (mclk_mask < 0) {
1426 dev_err(sdev->dev, "Invalid MCLK configuration\n");
1427 return NULL;
1428 }
1429
1430 dev_dbg(sdev->dev, "MCLK mask %#x found in NHLT\n", mclk_mask);
1431
1432 if (mclk_mask) {
1433 dev_info(sdev->dev, "Overriding topology with MCLK mask %#x from NHLT\n", mclk_mask);
1434 sdev->mclk_id_override = true;
1435 sdev->mclk_id_quirk = (mclk_mask & BIT(0)) ? 0 : 1;
1436 }
1437 }
1438
1439 amp_type = snd_soc_acpi_intel_detect_amp_type(sdev->dev);
1440 codec_type = snd_soc_acpi_intel_detect_codec_type(sdev->dev);
1441 amp_name_valid = amp_type != CODEC_NONE && amp_type != codec_type;
1442
1443 if (tplg_fixup && amp_name_valid &&
1444 mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_AMP_NAME) {
1445 tplg_suffix = snd_soc_acpi_intel_get_amp_tplg_suffix(amp_type);
1446 if (!tplg_suffix) {
1447 dev_err(sdev->dev, "no tplg suffix found, amp %d\n",
1448 amp_type);
1449 return NULL;
1450 }
1451
1452 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1453 "%s-%s",
1454 sof_pdata->tplg_filename,
1455 tplg_suffix);
1456 if (!tplg_filename)
1457 return NULL;
1458
1459 sof_pdata->tplg_filename = tplg_filename;
1460 }
1461
1462
1463 if (tplg_fixup &&
1464 mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_CODEC_NAME &&
1465 codec_type != CODEC_NONE) {
1466 tplg_suffix = snd_soc_acpi_intel_get_codec_tplg_suffix(codec_type);
1467 if (!tplg_suffix) {
1468 dev_err(sdev->dev, "no tplg suffix found, codec %d\n",
1469 codec_type);
1470 return NULL;
1471 }
1472
1473 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1474 "%s-%s",
1475 sof_pdata->tplg_filename,
1476 tplg_suffix);
1477 if (!tplg_filename)
1478 return NULL;
1479
1480 sof_pdata->tplg_filename = tplg_filename;
1481 }
1482
1483 if (tplg_fixup) {
1484 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1485 "%s%s",
1486 sof_pdata->tplg_filename,
1487 ".tplg");
1488 if (!tplg_filename)
1489 return NULL;
1490
1491 sof_pdata->tplg_filename = tplg_filename;
1492 }
1493
1494 /* check if mclk_id should be modified from topology defaults */
1495 if (mclk_id_override >= 0) {
1496 dev_info(sdev->dev, "Overriding topology with MCLK %d from kernel_parameter\n", mclk_id_override);
1497 sdev->mclk_id_override = true;
1498 sdev->mclk_id_quirk = mclk_id_override;
1499 }
1500 }
1501
1502 return mach;
1503 }
1504
hda_pci_intel_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)1505 int hda_pci_intel_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
1506 {
1507 int ret;
1508
1509 ret = snd_intel_dsp_driver_probe(pci);
1510 if (ret != SND_INTEL_DSP_DRIVER_ANY && ret != SND_INTEL_DSP_DRIVER_SOF) {
1511 dev_dbg(&pci->dev, "SOF PCI driver not selected, aborting probe\n");
1512 return -ENODEV;
1513 }
1514
1515 return sof_pci_probe(pci, pci_id);
1516 }
1517 EXPORT_SYMBOL_NS(hda_pci_intel_probe, "SND_SOC_SOF_INTEL_HDA_GENERIC");
1518
hda_register_clients(struct snd_sof_dev * sdev)1519 int hda_register_clients(struct snd_sof_dev *sdev)
1520 {
1521 return hda_probes_register(sdev);
1522 }
1523
hda_unregister_clients(struct snd_sof_dev * sdev)1524 void hda_unregister_clients(struct snd_sof_dev *sdev)
1525 {
1526 hda_probes_unregister(sdev);
1527 }
1528
1529 MODULE_LICENSE("Dual BSD/GPL");
1530 MODULE_DESCRIPTION("SOF support for HDaudio platforms");
1531 MODULE_IMPORT_NS("SND_SOC_SOF_PCI_DEV");
1532 MODULE_IMPORT_NS("SND_SOC_SOF_HDA_AUDIO_CODEC");
1533 MODULE_IMPORT_NS("SND_SOC_SOF_HDA_AUDIO_CODEC_I915");
1534 MODULE_IMPORT_NS("SND_SOC_SOF_XTENSA");
1535 MODULE_IMPORT_NS("SND_INTEL_SOUNDWIRE_ACPI");
1536 MODULE_IMPORT_NS("SOUNDWIRE_INTEL_INIT");
1537 MODULE_IMPORT_NS("SOUNDWIRE_INTEL");
1538 MODULE_IMPORT_NS("SND_SOC_SOF_HDA_MLINK");
1539 MODULE_IMPORT_NS("SND_SOC_SOF_INTEL_HDA_COMMON");
1540 MODULE_IMPORT_NS("SND_SOC_ACPI_INTEL_MATCH");
1541