projects/linux·blob master · refs

1792 lines · 48.1 KiB

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