treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / sound / soc / intel / skylake / skl.c
blobf755ca2484cff75b87d9eff67d99676ab22cc987
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * skl.c - Implementation of ASoC Intel SKL HD Audio driver
5 * Copyright (C) 2014-2015 Intel Corp
6 * Author: Jeeja KP <jeeja.kp@intel.com>
8 * Derived mostly from Intel HDA driver with following copyrights:
9 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
10 * PeiSen Hou <pshou@realtek.com.tw>
11 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16 #include <linux/module.h>
17 #include <linux/pci.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/platform_device.h>
20 #include <linux/firmware.h>
21 #include <linux/delay.h>
22 #include <sound/pcm.h>
23 #include <sound/soc-acpi.h>
24 #include <sound/soc-acpi-intel-match.h>
25 #include <sound/hda_register.h>
26 #include <sound/hdaudio.h>
27 #include <sound/hda_i915.h>
28 #include <sound/hda_codec.h>
29 #include <sound/intel-nhlt.h>
30 #include <sound/intel-dsp-config.h>
31 #include "skl.h"
32 #include "skl-sst-dsp.h"
33 #include "skl-sst-ipc.h"
35 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC)
36 #include "../../../soc/codecs/hdac_hda.h"
37 #endif
38 static int skl_pci_binding;
39 module_param_named(pci_binding, skl_pci_binding, int, 0444);
40 MODULE_PARM_DESC(pci_binding, "PCI binding (0=auto, 1=only legacy, 2=only asoc");
43 * initialize the PCI registers
45 static void skl_update_pci_byte(struct pci_dev *pci, unsigned int reg,
46 unsigned char mask, unsigned char val)
48 unsigned char data;
50 pci_read_config_byte(pci, reg, &data);
51 data &= ~mask;
52 data |= (val & mask);
53 pci_write_config_byte(pci, reg, data);
56 static void skl_init_pci(struct skl_dev *skl)
58 struct hdac_bus *bus = skl_to_bus(skl);
61 * Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
62 * TCSEL == Traffic Class Select Register, which sets PCI express QOS
63 * Ensuring these bits are 0 clears playback static on some HD Audio
64 * codecs.
65 * The PCI register TCSEL is defined in the Intel manuals.
67 dev_dbg(bus->dev, "Clearing TCSEL\n");
68 skl_update_pci_byte(skl->pci, AZX_PCIREG_TCSEL, 0x07, 0);
71 static void update_pci_dword(struct pci_dev *pci,
72 unsigned int reg, u32 mask, u32 val)
74 u32 data = 0;
76 pci_read_config_dword(pci, reg, &data);
77 data &= ~mask;
78 data |= (val & mask);
79 pci_write_config_dword(pci, reg, data);
83 * skl_enable_miscbdcge - enable/dsiable CGCTL.MISCBDCGE bits
85 * @dev: device pointer
86 * @enable: enable/disable flag
88 static void skl_enable_miscbdcge(struct device *dev, bool enable)
90 struct pci_dev *pci = to_pci_dev(dev);
91 u32 val;
93 val = enable ? AZX_CGCTL_MISCBDCGE_MASK : 0;
95 update_pci_dword(pci, AZX_PCIREG_CGCTL, AZX_CGCTL_MISCBDCGE_MASK, val);
98 /**
99 * skl_clock_power_gating: Enable/Disable clock and power gating
101 * @dev: Device pointer
102 * @enable: Enable/Disable flag
104 static void skl_clock_power_gating(struct device *dev, bool enable)
106 struct pci_dev *pci = to_pci_dev(dev);
107 struct hdac_bus *bus = pci_get_drvdata(pci);
108 u32 val;
110 /* Update PDCGE bit of CGCTL register */
111 val = enable ? AZX_CGCTL_ADSPDCGE : 0;
112 update_pci_dword(pci, AZX_PCIREG_CGCTL, AZX_CGCTL_ADSPDCGE, val);
114 /* Update L1SEN bit of EM2 register */
115 val = enable ? AZX_REG_VS_EM2_L1SEN : 0;
116 snd_hdac_chip_updatel(bus, VS_EM2, AZX_REG_VS_EM2_L1SEN, val);
118 /* Update ADSPPGD bit of PGCTL register */
119 val = enable ? 0 : AZX_PGCTL_ADSPPGD;
120 update_pci_dword(pci, AZX_PCIREG_PGCTL, AZX_PGCTL_ADSPPGD, val);
124 * While performing reset, controller may not come back properly causing
125 * issues, so recommendation is to set CGCTL.MISCBDCGE to 0 then do reset
126 * (init chip) and then again set CGCTL.MISCBDCGE to 1
128 static int skl_init_chip(struct hdac_bus *bus, bool full_reset)
130 struct hdac_ext_link *hlink;
131 int ret;
133 skl_enable_miscbdcge(bus->dev, false);
134 ret = snd_hdac_bus_init_chip(bus, full_reset);
136 /* Reset stream-to-link mapping */
137 list_for_each_entry(hlink, &bus->hlink_list, list)
138 writel(0, hlink->ml_addr + AZX_REG_ML_LOSIDV);
140 skl_enable_miscbdcge(bus->dev, true);
142 return ret;
145 void skl_update_d0i3c(struct device *dev, bool enable)
147 struct pci_dev *pci = to_pci_dev(dev);
148 struct hdac_bus *bus = pci_get_drvdata(pci);
149 u8 reg;
150 int timeout = 50;
152 reg = snd_hdac_chip_readb(bus, VS_D0I3C);
153 /* Do not write to D0I3C until command in progress bit is cleared */
154 while ((reg & AZX_REG_VS_D0I3C_CIP) && --timeout) {
155 udelay(10);
156 reg = snd_hdac_chip_readb(bus, VS_D0I3C);
159 /* Highly unlikely. But if it happens, flag error explicitly */
160 if (!timeout) {
161 dev_err(bus->dev, "Before D0I3C update: D0I3C CIP timeout\n");
162 return;
165 if (enable)
166 reg = reg | AZX_REG_VS_D0I3C_I3;
167 else
168 reg = reg & (~AZX_REG_VS_D0I3C_I3);
170 snd_hdac_chip_writeb(bus, VS_D0I3C, reg);
172 timeout = 50;
173 /* Wait for cmd in progress to be cleared before exiting the function */
174 reg = snd_hdac_chip_readb(bus, VS_D0I3C);
175 while ((reg & AZX_REG_VS_D0I3C_CIP) && --timeout) {
176 udelay(10);
177 reg = snd_hdac_chip_readb(bus, VS_D0I3C);
180 /* Highly unlikely. But if it happens, flag error explicitly */
181 if (!timeout) {
182 dev_err(bus->dev, "After D0I3C update: D0I3C CIP timeout\n");
183 return;
186 dev_dbg(bus->dev, "D0I3C register = 0x%x\n",
187 snd_hdac_chip_readb(bus, VS_D0I3C));
191 * skl_dum_set - set DUM bit in EM2 register
192 * @bus: HD-audio core bus
194 * Addresses incorrect position reporting for capture streams.
195 * Used on device power up.
197 static void skl_dum_set(struct hdac_bus *bus)
199 /* For the DUM bit to be set, CRST needs to be out of reset state */
200 if (!(snd_hdac_chip_readb(bus, GCTL) & AZX_GCTL_RESET)) {
201 skl_enable_miscbdcge(bus->dev, false);
202 snd_hdac_bus_exit_link_reset(bus);
203 skl_enable_miscbdcge(bus->dev, true);
206 snd_hdac_chip_updatel(bus, VS_EM2, AZX_VS_EM2_DUM, AZX_VS_EM2_DUM);
209 /* called from IRQ */
210 static void skl_stream_update(struct hdac_bus *bus, struct hdac_stream *hstr)
212 snd_pcm_period_elapsed(hstr->substream);
215 static irqreturn_t skl_interrupt(int irq, void *dev_id)
217 struct hdac_bus *bus = dev_id;
218 u32 status;
220 if (!pm_runtime_active(bus->dev))
221 return IRQ_NONE;
223 spin_lock(&bus->reg_lock);
225 status = snd_hdac_chip_readl(bus, INTSTS);
226 if (status == 0 || status == 0xffffffff) {
227 spin_unlock(&bus->reg_lock);
228 return IRQ_NONE;
231 /* clear rirb int */
232 status = snd_hdac_chip_readb(bus, RIRBSTS);
233 if (status & RIRB_INT_MASK) {
234 if (status & RIRB_INT_RESPONSE)
235 snd_hdac_bus_update_rirb(bus);
236 snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
239 spin_unlock(&bus->reg_lock);
241 return snd_hdac_chip_readl(bus, INTSTS) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
244 static irqreturn_t skl_threaded_handler(int irq, void *dev_id)
246 struct hdac_bus *bus = dev_id;
247 u32 status;
249 status = snd_hdac_chip_readl(bus, INTSTS);
251 snd_hdac_bus_handle_stream_irq(bus, status, skl_stream_update);
253 return IRQ_HANDLED;
256 static int skl_acquire_irq(struct hdac_bus *bus, int do_disconnect)
258 struct skl_dev *skl = bus_to_skl(bus);
259 int ret;
261 ret = request_threaded_irq(skl->pci->irq, skl_interrupt,
262 skl_threaded_handler,
263 IRQF_SHARED,
264 KBUILD_MODNAME, bus);
265 if (ret) {
266 dev_err(bus->dev,
267 "unable to grab IRQ %d, disabling device\n",
268 skl->pci->irq);
269 return ret;
272 bus->irq = skl->pci->irq;
273 pci_intx(skl->pci, 1);
275 return 0;
278 static int skl_suspend_late(struct device *dev)
280 struct pci_dev *pci = to_pci_dev(dev);
281 struct hdac_bus *bus = pci_get_drvdata(pci);
282 struct skl_dev *skl = bus_to_skl(bus);
284 return skl_suspend_late_dsp(skl);
287 #ifdef CONFIG_PM
288 static int _skl_suspend(struct hdac_bus *bus)
290 struct skl_dev *skl = bus_to_skl(bus);
291 struct pci_dev *pci = to_pci_dev(bus->dev);
292 int ret;
294 snd_hdac_ext_bus_link_power_down_all(bus);
296 ret = skl_suspend_dsp(skl);
297 if (ret < 0)
298 return ret;
300 snd_hdac_bus_stop_chip(bus);
301 update_pci_dword(pci, AZX_PCIREG_PGCTL,
302 AZX_PGCTL_LSRMD_MASK, AZX_PGCTL_LSRMD_MASK);
303 skl_enable_miscbdcge(bus->dev, false);
304 snd_hdac_bus_enter_link_reset(bus);
305 skl_enable_miscbdcge(bus->dev, true);
306 skl_cleanup_resources(skl);
308 return 0;
311 static int _skl_resume(struct hdac_bus *bus)
313 struct skl_dev *skl = bus_to_skl(bus);
315 skl_init_pci(skl);
316 skl_dum_set(bus);
317 skl_init_chip(bus, true);
319 return skl_resume_dsp(skl);
321 #endif
323 #ifdef CONFIG_PM_SLEEP
325 * power management
327 static int skl_suspend(struct device *dev)
329 struct pci_dev *pci = to_pci_dev(dev);
330 struct hdac_bus *bus = pci_get_drvdata(pci);
331 struct skl_dev *skl = bus_to_skl(bus);
332 int ret;
335 * Do not suspend if streams which are marked ignore suspend are
336 * running, we need to save the state for these and continue
338 if (skl->supend_active) {
339 /* turn off the links and stop the CORB/RIRB DMA if it is On */
340 snd_hdac_ext_bus_link_power_down_all(bus);
342 if (bus->cmd_dma_state)
343 snd_hdac_bus_stop_cmd_io(bus);
345 enable_irq_wake(bus->irq);
346 pci_save_state(pci);
347 } else {
348 ret = _skl_suspend(bus);
349 if (ret < 0)
350 return ret;
351 skl->fw_loaded = false;
354 return 0;
357 static int skl_resume(struct device *dev)
359 struct pci_dev *pci = to_pci_dev(dev);
360 struct hdac_bus *bus = pci_get_drvdata(pci);
361 struct skl_dev *skl = bus_to_skl(bus);
362 struct hdac_ext_link *hlink = NULL;
363 int ret;
366 * resume only when we are not in suspend active, otherwise need to
367 * restore the device
369 if (skl->supend_active) {
370 pci_restore_state(pci);
371 snd_hdac_ext_bus_link_power_up_all(bus);
372 disable_irq_wake(bus->irq);
374 * turn On the links which are On before active suspend
375 * and start the CORB/RIRB DMA if On before
376 * active suspend.
378 list_for_each_entry(hlink, &bus->hlink_list, list) {
379 if (hlink->ref_count)
380 snd_hdac_ext_bus_link_power_up(hlink);
383 ret = 0;
384 if (bus->cmd_dma_state)
385 snd_hdac_bus_init_cmd_io(bus);
386 } else {
387 ret = _skl_resume(bus);
389 /* turn off the links which are off before suspend */
390 list_for_each_entry(hlink, &bus->hlink_list, list) {
391 if (!hlink->ref_count)
392 snd_hdac_ext_bus_link_power_down(hlink);
395 if (!bus->cmd_dma_state)
396 snd_hdac_bus_stop_cmd_io(bus);
399 return ret;
401 #endif /* CONFIG_PM_SLEEP */
403 #ifdef CONFIG_PM
404 static int skl_runtime_suspend(struct device *dev)
406 struct pci_dev *pci = to_pci_dev(dev);
407 struct hdac_bus *bus = pci_get_drvdata(pci);
409 dev_dbg(bus->dev, "in %s\n", __func__);
411 return _skl_suspend(bus);
414 static int skl_runtime_resume(struct device *dev)
416 struct pci_dev *pci = to_pci_dev(dev);
417 struct hdac_bus *bus = pci_get_drvdata(pci);
419 dev_dbg(bus->dev, "in %s\n", __func__);
421 return _skl_resume(bus);
423 #endif /* CONFIG_PM */
425 static const struct dev_pm_ops skl_pm = {
426 SET_SYSTEM_SLEEP_PM_OPS(skl_suspend, skl_resume)
427 SET_RUNTIME_PM_OPS(skl_runtime_suspend, skl_runtime_resume, NULL)
428 .suspend_late = skl_suspend_late,
432 * destructor
434 static int skl_free(struct hdac_bus *bus)
436 struct skl_dev *skl = bus_to_skl(bus);
438 skl->init_done = 0; /* to be sure */
440 snd_hdac_ext_stop_streams(bus);
442 if (bus->irq >= 0)
443 free_irq(bus->irq, (void *)bus);
444 snd_hdac_bus_free_stream_pages(bus);
445 snd_hdac_stream_free_all(bus);
446 snd_hdac_link_free_all(bus);
448 if (bus->remap_addr)
449 iounmap(bus->remap_addr);
451 pci_release_regions(skl->pci);
452 pci_disable_device(skl->pci);
454 snd_hdac_ext_bus_exit(bus);
456 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
457 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
458 snd_hdac_i915_exit(bus);
461 return 0;
465 * For each ssp there are 3 clocks (mclk/sclk/sclkfs).
466 * e.g. for ssp0, clocks will be named as
467 * "ssp0_mclk", "ssp0_sclk", "ssp0_sclkfs"
468 * So for skl+, there are 6 ssps, so 18 clocks will be created.
470 static struct skl_ssp_clk skl_ssp_clks[] = {
471 {.name = "ssp0_mclk"}, {.name = "ssp1_mclk"}, {.name = "ssp2_mclk"},
472 {.name = "ssp3_mclk"}, {.name = "ssp4_mclk"}, {.name = "ssp5_mclk"},
473 {.name = "ssp0_sclk"}, {.name = "ssp1_sclk"}, {.name = "ssp2_sclk"},
474 {.name = "ssp3_sclk"}, {.name = "ssp4_sclk"}, {.name = "ssp5_sclk"},
475 {.name = "ssp0_sclkfs"}, {.name = "ssp1_sclkfs"},
476 {.name = "ssp2_sclkfs"},
477 {.name = "ssp3_sclkfs"}, {.name = "ssp4_sclkfs"},
478 {.name = "ssp5_sclkfs"},
481 static struct snd_soc_acpi_mach *skl_find_hda_machine(struct skl_dev *skl,
482 struct snd_soc_acpi_mach *machines)
484 struct hdac_bus *bus = skl_to_bus(skl);
485 struct snd_soc_acpi_mach *mach;
487 /* check if we have any codecs detected on bus */
488 if (bus->codec_mask == 0)
489 return NULL;
491 /* point to common table */
492 mach = snd_soc_acpi_intel_hda_machines;
494 /* all entries in the machine table use the same firmware */
495 mach->fw_filename = machines->fw_filename;
497 return mach;
500 static int skl_find_machine(struct skl_dev *skl, void *driver_data)
502 struct hdac_bus *bus = skl_to_bus(skl);
503 struct snd_soc_acpi_mach *mach = driver_data;
504 struct skl_machine_pdata *pdata;
506 mach = snd_soc_acpi_find_machine(mach);
507 if (!mach) {
508 dev_dbg(bus->dev, "No matching I2S machine driver found\n");
509 mach = skl_find_hda_machine(skl, driver_data);
510 if (!mach) {
511 dev_err(bus->dev, "No matching machine driver found\n");
512 return -ENODEV;
516 skl->mach = mach;
517 skl->fw_name = mach->fw_filename;
518 pdata = mach->pdata;
520 if (pdata) {
521 skl->use_tplg_pcm = pdata->use_tplg_pcm;
522 mach->mach_params.dmic_num =
523 intel_nhlt_get_dmic_geo(&skl->pci->dev,
524 skl->nhlt);
527 return 0;
530 static int skl_machine_device_register(struct skl_dev *skl)
532 struct snd_soc_acpi_mach *mach = skl->mach;
533 struct hdac_bus *bus = skl_to_bus(skl);
534 struct platform_device *pdev;
535 int ret;
537 pdev = platform_device_alloc(mach->drv_name, -1);
538 if (pdev == NULL) {
539 dev_err(bus->dev, "platform device alloc failed\n");
540 return -EIO;
543 mach->mach_params.platform = dev_name(bus->dev);
544 mach->mach_params.codec_mask = bus->codec_mask;
546 ret = platform_device_add_data(pdev, (const void *)mach, sizeof(*mach));
547 if (ret) {
548 dev_err(bus->dev, "failed to add machine device platform data\n");
549 platform_device_put(pdev);
550 return ret;
553 ret = platform_device_add(pdev);
554 if (ret) {
555 dev_err(bus->dev, "failed to add machine device\n");
556 platform_device_put(pdev);
557 return -EIO;
561 skl->i2s_dev = pdev;
563 return 0;
566 static void skl_machine_device_unregister(struct skl_dev *skl)
568 if (skl->i2s_dev)
569 platform_device_unregister(skl->i2s_dev);
572 static int skl_dmic_device_register(struct skl_dev *skl)
574 struct hdac_bus *bus = skl_to_bus(skl);
575 struct platform_device *pdev;
576 int ret;
578 /* SKL has one dmic port, so allocate dmic device for this */
579 pdev = platform_device_alloc("dmic-codec", -1);
580 if (!pdev) {
581 dev_err(bus->dev, "failed to allocate dmic device\n");
582 return -ENOMEM;
585 ret = platform_device_add(pdev);
586 if (ret) {
587 dev_err(bus->dev, "failed to add dmic device: %d\n", ret);
588 platform_device_put(pdev);
589 return ret;
591 skl->dmic_dev = pdev;
593 return 0;
596 static void skl_dmic_device_unregister(struct skl_dev *skl)
598 if (skl->dmic_dev)
599 platform_device_unregister(skl->dmic_dev);
602 static struct skl_clk_parent_src skl_clk_src[] = {
603 { .clk_id = SKL_XTAL, .name = "xtal" },
604 { .clk_id = SKL_CARDINAL, .name = "cardinal", .rate = 24576000 },
605 { .clk_id = SKL_PLL, .name = "pll", .rate = 96000000 },
608 struct skl_clk_parent_src *skl_get_parent_clk(u8 clk_id)
610 unsigned int i;
612 for (i = 0; i < ARRAY_SIZE(skl_clk_src); i++) {
613 if (skl_clk_src[i].clk_id == clk_id)
614 return &skl_clk_src[i];
617 return NULL;
620 static void init_skl_xtal_rate(int pci_id)
622 switch (pci_id) {
623 case 0x9d70:
624 case 0x9d71:
625 skl_clk_src[0].rate = 24000000;
626 return;
628 default:
629 skl_clk_src[0].rate = 19200000;
630 return;
634 static int skl_clock_device_register(struct skl_dev *skl)
636 struct platform_device_info pdevinfo = {NULL};
637 struct skl_clk_pdata *clk_pdata;
639 clk_pdata = devm_kzalloc(&skl->pci->dev, sizeof(*clk_pdata),
640 GFP_KERNEL);
641 if (!clk_pdata)
642 return -ENOMEM;
644 init_skl_xtal_rate(skl->pci->device);
646 clk_pdata->parent_clks = skl_clk_src;
647 clk_pdata->ssp_clks = skl_ssp_clks;
648 clk_pdata->num_clks = ARRAY_SIZE(skl_ssp_clks);
650 /* Query NHLT to fill the rates and parent */
651 skl_get_clks(skl, clk_pdata->ssp_clks);
652 clk_pdata->pvt_data = skl;
654 /* Register Platform device */
655 pdevinfo.parent = &skl->pci->dev;
656 pdevinfo.id = -1;
657 pdevinfo.name = "skl-ssp-clk";
658 pdevinfo.data = clk_pdata;
659 pdevinfo.size_data = sizeof(*clk_pdata);
660 skl->clk_dev = platform_device_register_full(&pdevinfo);
661 return PTR_ERR_OR_ZERO(skl->clk_dev);
664 static void skl_clock_device_unregister(struct skl_dev *skl)
666 if (skl->clk_dev)
667 platform_device_unregister(skl->clk_dev);
670 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC)
672 #define IDISP_INTEL_VENDOR_ID 0x80860000
675 * load the legacy codec driver
677 static void load_codec_module(struct hda_codec *codec)
679 #ifdef MODULE
680 char modalias[MODULE_NAME_LEN];
681 const char *mod = NULL;
683 snd_hdac_codec_modalias(&codec->core, modalias, sizeof(modalias));
684 mod = modalias;
685 dev_dbg(&codec->core.dev, "loading %s codec module\n", mod);
686 request_module(mod);
687 #endif
690 #endif /* CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC */
693 * Probe the given codec address
695 static int probe_codec(struct hdac_bus *bus, int addr)
697 unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
698 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
699 unsigned int res = -1;
700 struct skl_dev *skl = bus_to_skl(bus);
701 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC)
702 struct hdac_hda_priv *hda_codec;
703 int err;
704 #endif
705 struct hdac_device *hdev;
707 mutex_lock(&bus->cmd_mutex);
708 snd_hdac_bus_send_cmd(bus, cmd);
709 snd_hdac_bus_get_response(bus, addr, &res);
710 mutex_unlock(&bus->cmd_mutex);
711 if (res == -1)
712 return -EIO;
713 dev_dbg(bus->dev, "codec #%d probed OK: %x\n", addr, res);
715 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC)
716 hda_codec = devm_kzalloc(&skl->pci->dev, sizeof(*hda_codec),
717 GFP_KERNEL);
718 if (!hda_codec)
719 return -ENOMEM;
721 hda_codec->codec.bus = skl_to_hbus(skl);
722 hdev = &hda_codec->codec.core;
724 err = snd_hdac_ext_bus_device_init(bus, addr, hdev);
725 if (err < 0)
726 return err;
728 /* use legacy bus only for HDA codecs, idisp uses ext bus */
729 if ((res & 0xFFFF0000) != IDISP_INTEL_VENDOR_ID) {
730 hdev->type = HDA_DEV_LEGACY;
731 load_codec_module(&hda_codec->codec);
733 return 0;
734 #else
735 hdev = devm_kzalloc(&skl->pci->dev, sizeof(*hdev), GFP_KERNEL);
736 if (!hdev)
737 return -ENOMEM;
739 return snd_hdac_ext_bus_device_init(bus, addr, hdev);
740 #endif /* CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC */
743 /* Codec initialization */
744 static void skl_codec_create(struct hdac_bus *bus)
746 int c, max_slots;
748 max_slots = HDA_MAX_CODECS;
750 /* First try to probe all given codec slots */
751 for (c = 0; c < max_slots; c++) {
752 if ((bus->codec_mask & (1 << c))) {
753 if (probe_codec(bus, c) < 0) {
755 * Some BIOSen give you wrong codec addresses
756 * that don't exist
758 dev_warn(bus->dev,
759 "Codec #%d probe error; disabling it...\n", c);
760 bus->codec_mask &= ~(1 << c);
762 * More badly, accessing to a non-existing
763 * codec often screws up the controller bus,
764 * and disturbs the further communications.
765 * Thus if an error occurs during probing,
766 * better to reset the controller bus to get
767 * back to the sanity state.
769 snd_hdac_bus_stop_chip(bus);
770 skl_init_chip(bus, true);
776 static int skl_i915_init(struct hdac_bus *bus)
778 int err;
781 * The HDMI codec is in GPU so we need to ensure that it is powered
782 * up and ready for probe
784 err = snd_hdac_i915_init(bus);
785 if (err < 0)
786 return err;
788 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, true);
790 return 0;
793 static void skl_probe_work(struct work_struct *work)
795 struct skl_dev *skl = container_of(work, struct skl_dev, probe_work);
796 struct hdac_bus *bus = skl_to_bus(skl);
797 struct hdac_ext_link *hlink = NULL;
798 int err;
800 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
801 err = skl_i915_init(bus);
802 if (err < 0)
803 return;
806 err = skl_init_chip(bus, true);
807 if (err < 0) {
808 dev_err(bus->dev, "Init chip failed with err: %d\n", err);
809 goto out_err;
812 /* codec detection */
813 if (!bus->codec_mask)
814 dev_info(bus->dev, "no hda codecs found!\n");
816 /* create codec instances */
817 skl_codec_create(bus);
819 /* register platform dai and controls */
820 err = skl_platform_register(bus->dev);
821 if (err < 0) {
822 dev_err(bus->dev, "platform register failed: %d\n", err);
823 goto out_err;
826 err = skl_machine_device_register(skl);
827 if (err < 0) {
828 dev_err(bus->dev, "machine register failed: %d\n", err);
829 goto out_err;
833 * we are done probing so decrement link counts
835 list_for_each_entry(hlink, &bus->hlink_list, list)
836 snd_hdac_ext_bus_link_put(bus, hlink);
838 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI))
839 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
841 /* configure PM */
842 pm_runtime_put_noidle(bus->dev);
843 pm_runtime_allow(bus->dev);
844 skl->init_done = 1;
846 return;
848 out_err:
849 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI))
850 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
854 * constructor
856 static int skl_create(struct pci_dev *pci,
857 struct skl_dev **rskl)
859 struct hdac_ext_bus_ops *ext_ops = NULL;
860 struct skl_dev *skl;
861 struct hdac_bus *bus;
862 struct hda_bus *hbus;
863 int err;
865 *rskl = NULL;
867 err = pci_enable_device(pci);
868 if (err < 0)
869 return err;
871 skl = devm_kzalloc(&pci->dev, sizeof(*skl), GFP_KERNEL);
872 if (!skl) {
873 pci_disable_device(pci);
874 return -ENOMEM;
877 hbus = skl_to_hbus(skl);
878 bus = skl_to_bus(skl);
880 INIT_LIST_HEAD(&skl->ppl_list);
881 INIT_LIST_HEAD(&skl->bind_list);
883 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC)
884 ext_ops = snd_soc_hdac_hda_get_ops();
885 #endif
886 snd_hdac_ext_bus_init(bus, &pci->dev, NULL, ext_ops);
887 bus->use_posbuf = 1;
888 skl->pci = pci;
889 INIT_WORK(&skl->probe_work, skl_probe_work);
890 bus->bdl_pos_adj = 0;
892 mutex_init(&hbus->prepare_mutex);
893 hbus->pci = pci;
894 hbus->mixer_assigned = -1;
895 hbus->modelname = "sklbus";
897 *rskl = skl;
899 return 0;
902 static int skl_first_init(struct hdac_bus *bus)
904 struct skl_dev *skl = bus_to_skl(bus);
905 struct pci_dev *pci = skl->pci;
906 int err;
907 unsigned short gcap;
908 int cp_streams, pb_streams, start_idx;
910 err = pci_request_regions(pci, "Skylake HD audio");
911 if (err < 0)
912 return err;
914 bus->addr = pci_resource_start(pci, 0);
915 bus->remap_addr = pci_ioremap_bar(pci, 0);
916 if (bus->remap_addr == NULL) {
917 dev_err(bus->dev, "ioremap error\n");
918 return -ENXIO;
921 snd_hdac_bus_reset_link(bus, true);
923 snd_hdac_bus_parse_capabilities(bus);
925 /* check if PPCAP exists */
926 if (!bus->ppcap) {
927 dev_err(bus->dev, "bus ppcap not set, HDaudio or DSP not present?\n");
928 return -ENODEV;
931 if (skl_acquire_irq(bus, 0) < 0)
932 return -EBUSY;
934 pci_set_master(pci);
935 synchronize_irq(bus->irq);
937 gcap = snd_hdac_chip_readw(bus, GCAP);
938 dev_dbg(bus->dev, "chipset global capabilities = 0x%x\n", gcap);
940 /* read number of streams from GCAP register */
941 cp_streams = (gcap >> 8) & 0x0f;
942 pb_streams = (gcap >> 12) & 0x0f;
944 if (!pb_streams && !cp_streams) {
945 dev_err(bus->dev, "no streams found in GCAP definitions?\n");
946 return -EIO;
949 bus->num_streams = cp_streams + pb_streams;
951 /* allow 64bit DMA address if supported by H/W */
952 if (!dma_set_mask(bus->dev, DMA_BIT_MASK(64))) {
953 dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(64));
954 } else {
955 dma_set_mask(bus->dev, DMA_BIT_MASK(32));
956 dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(32));
959 /* initialize streams */
960 snd_hdac_ext_stream_init_all
961 (bus, 0, cp_streams, SNDRV_PCM_STREAM_CAPTURE);
962 start_idx = cp_streams;
963 snd_hdac_ext_stream_init_all
964 (bus, start_idx, pb_streams, SNDRV_PCM_STREAM_PLAYBACK);
966 err = snd_hdac_bus_alloc_stream_pages(bus);
967 if (err < 0)
968 return err;
970 /* initialize chip */
971 skl_init_pci(skl);
972 skl_dum_set(bus);
974 return skl_init_chip(bus, true);
977 static int skl_probe(struct pci_dev *pci,
978 const struct pci_device_id *pci_id)
980 struct skl_dev *skl;
981 struct hdac_bus *bus = NULL;
982 int err;
984 switch (skl_pci_binding) {
985 case SND_SKL_PCI_BIND_AUTO:
986 err = snd_intel_dsp_driver_probe(pci);
987 if (err != SND_INTEL_DSP_DRIVER_ANY &&
988 err != SND_INTEL_DSP_DRIVER_SST)
989 return -ENODEV;
990 break;
991 case SND_SKL_PCI_BIND_LEGACY:
992 dev_info(&pci->dev, "Module parameter forced binding with HDaudio legacy, aborting probe\n");
993 return -ENODEV;
994 case SND_SKL_PCI_BIND_ASOC:
995 dev_info(&pci->dev, "Module parameter forced binding with SKL driver, bypassed detection logic\n");
996 break;
997 default:
998 dev_err(&pci->dev, "invalid value for skl_pci_binding module parameter, ignored\n");
999 break;
1002 /* we use ext core ops, so provide NULL for ops here */
1003 err = skl_create(pci, &skl);
1004 if (err < 0)
1005 return err;
1007 bus = skl_to_bus(skl);
1009 err = skl_first_init(bus);
1010 if (err < 0) {
1011 dev_err(bus->dev, "skl_first_init failed with err: %d\n", err);
1012 goto out_free;
1015 skl->pci_id = pci->device;
1017 device_disable_async_suspend(bus->dev);
1019 skl->nhlt = intel_nhlt_init(bus->dev);
1021 if (skl->nhlt == NULL) {
1022 #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC)
1023 dev_err(bus->dev, "no nhlt info found\n");
1024 err = -ENODEV;
1025 goto out_free;
1026 #else
1027 dev_warn(bus->dev, "no nhlt info found, continuing to try to enable HDaudio codec\n");
1028 #endif
1029 } else {
1031 err = skl_nhlt_create_sysfs(skl);
1032 if (err < 0) {
1033 dev_err(bus->dev, "skl_nhlt_create_sysfs failed with err: %d\n", err);
1034 goto out_nhlt_free;
1037 skl_nhlt_update_topology_bin(skl);
1039 /* create device for dsp clk */
1040 err = skl_clock_device_register(skl);
1041 if (err < 0) {
1042 dev_err(bus->dev, "skl_clock_device_register failed with err: %d\n", err);
1043 goto out_clk_free;
1047 pci_set_drvdata(skl->pci, bus);
1050 err = skl_find_machine(skl, (void *)pci_id->driver_data);
1051 if (err < 0) {
1052 dev_err(bus->dev, "skl_find_machine failed with err: %d\n", err);
1053 goto out_nhlt_free;
1056 err = skl_init_dsp(skl);
1057 if (err < 0) {
1058 dev_dbg(bus->dev, "error failed to register dsp\n");
1059 goto out_nhlt_free;
1061 skl->enable_miscbdcge = skl_enable_miscbdcge;
1062 skl->clock_power_gating = skl_clock_power_gating;
1064 if (bus->mlcap)
1065 snd_hdac_ext_bus_get_ml_capabilities(bus);
1067 snd_hdac_bus_stop_chip(bus);
1069 /* create device for soc dmic */
1070 err = skl_dmic_device_register(skl);
1071 if (err < 0) {
1072 dev_err(bus->dev, "skl_dmic_device_register failed with err: %d\n", err);
1073 goto out_dsp_free;
1076 schedule_work(&skl->probe_work);
1078 return 0;
1080 out_dsp_free:
1081 skl_free_dsp(skl);
1082 out_clk_free:
1083 skl_clock_device_unregister(skl);
1084 out_nhlt_free:
1085 intel_nhlt_free(skl->nhlt);
1086 out_free:
1087 skl_free(bus);
1089 return err;
1092 static void skl_shutdown(struct pci_dev *pci)
1094 struct hdac_bus *bus = pci_get_drvdata(pci);
1095 struct hdac_stream *s;
1096 struct hdac_ext_stream *stream;
1097 struct skl_dev *skl;
1099 if (!bus)
1100 return;
1102 skl = bus_to_skl(bus);
1104 if (!skl->init_done)
1105 return;
1107 snd_hdac_ext_stop_streams(bus);
1108 list_for_each_entry(s, &bus->stream_list, list) {
1109 stream = stream_to_hdac_ext_stream(s);
1110 snd_hdac_ext_stream_decouple(bus, stream, false);
1113 snd_hdac_bus_stop_chip(bus);
1116 static void skl_remove(struct pci_dev *pci)
1118 struct hdac_bus *bus = pci_get_drvdata(pci);
1119 struct skl_dev *skl = bus_to_skl(bus);
1121 cancel_work_sync(&skl->probe_work);
1123 pm_runtime_get_noresume(&pci->dev);
1125 /* codec removal, invoke bus_device_remove */
1126 snd_hdac_ext_bus_device_remove(bus);
1128 skl_platform_unregister(&pci->dev);
1129 skl_free_dsp(skl);
1130 skl_machine_device_unregister(skl);
1131 skl_dmic_device_unregister(skl);
1132 skl_clock_device_unregister(skl);
1133 skl_nhlt_remove_sysfs(skl);
1134 intel_nhlt_free(skl->nhlt);
1135 skl_free(bus);
1136 dev_set_drvdata(&pci->dev, NULL);
1139 /* PCI IDs */
1140 static const struct pci_device_id skl_ids[] = {
1141 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_SKL)
1142 /* Sunrise Point-LP */
1143 { PCI_DEVICE(0x8086, 0x9d70),
1144 .driver_data = (unsigned long)&snd_soc_acpi_intel_skl_machines},
1145 #endif
1146 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_APL)
1147 /* BXT-P */
1148 { PCI_DEVICE(0x8086, 0x5a98),
1149 .driver_data = (unsigned long)&snd_soc_acpi_intel_bxt_machines},
1150 #endif
1151 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_KBL)
1152 /* KBL */
1153 { PCI_DEVICE(0x8086, 0x9D71),
1154 .driver_data = (unsigned long)&snd_soc_acpi_intel_kbl_machines},
1155 #endif
1156 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_GLK)
1157 /* GLK */
1158 { PCI_DEVICE(0x8086, 0x3198),
1159 .driver_data = (unsigned long)&snd_soc_acpi_intel_glk_machines},
1160 #endif
1161 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_CNL)
1162 /* CNL */
1163 { PCI_DEVICE(0x8086, 0x9dc8),
1164 .driver_data = (unsigned long)&snd_soc_acpi_intel_cnl_machines},
1165 #endif
1166 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_CFL)
1167 /* CFL */
1168 { PCI_DEVICE(0x8086, 0xa348),
1169 .driver_data = (unsigned long)&snd_soc_acpi_intel_cnl_machines},
1170 #endif
1171 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_CML_LP)
1172 /* CML-LP */
1173 { PCI_DEVICE(0x8086, 0x02c8),
1174 .driver_data = (unsigned long)&snd_soc_acpi_intel_cnl_machines},
1175 #endif
1176 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_CML_H)
1177 /* CML-H */
1178 { PCI_DEVICE(0x8086, 0x06c8),
1179 .driver_data = (unsigned long)&snd_soc_acpi_intel_cnl_machines},
1180 #endif
1181 { 0, }
1183 MODULE_DEVICE_TABLE(pci, skl_ids);
1185 /* pci_driver definition */
1186 static struct pci_driver skl_driver = {
1187 .name = KBUILD_MODNAME,
1188 .id_table = skl_ids,
1189 .probe = skl_probe,
1190 .remove = skl_remove,
1191 .shutdown = skl_shutdown,
1192 .driver = {
1193 .pm = &skl_pm,
1196 module_pci_driver(skl_driver);
1198 MODULE_LICENSE("GPL v2");
1199 MODULE_DESCRIPTION("Intel Skylake ASoC HDA driver");