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