2 * skl.c - Implementation of ASoC Intel SKL HD Audio driver
4 * Copyright (C) 2014-2015 Intel Corp
5 * Author: Jeeja KP <jeeja.kp@intel.com>
7 * Derived mostly from Intel HDA driver with following copyrights:
8 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
9 * PeiSen Hou <pshou@realtek.com.tw>
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; version 2 of the License.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 #include <linux/module.h>
25 #include <linux/pci.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/platform_device.h>
28 #include <linux/firmware.h>
29 #include <sound/pcm.h>
30 #include "../common/sst-acpi.h"
31 #include <sound/hda_register.h>
32 #include <sound/hdaudio.h>
33 #include <sound/hda_i915.h>
35 #include "skl-sst-dsp.h"
36 #include "skl-sst-ipc.h"
39 * initialize the PCI registers
41 static void skl_update_pci_byte(struct pci_dev *pci, unsigned int reg,
42 unsigned char mask, unsigned char val)
46 pci_read_config_byte(pci, reg, &data);
49 pci_write_config_byte(pci, reg, data);
52 static void skl_init_pci(struct skl *skl)
54 struct hdac_ext_bus *ebus = &skl->ebus;
57 * Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
58 * TCSEL == Traffic Class Select Register, which sets PCI express QOS
59 * Ensuring these bits are 0 clears playback static on some HD Audio
61 * The PCI register TCSEL is defined in the Intel manuals.
63 dev_dbg(ebus_to_hbus(ebus)->dev, "Clearing TCSEL\n");
64 skl_update_pci_byte(skl->pci, AZX_PCIREG_TCSEL, 0x07, 0);
67 static void update_pci_dword(struct pci_dev *pci,
68 unsigned int reg, u32 mask, u32 val)
72 pci_read_config_dword(pci, reg, &data);
75 pci_write_config_dword(pci, reg, data);
79 * skl_enable_miscbdcge - enable/dsiable CGCTL.MISCBDCGE bits
81 * @dev: device pointer
82 * @enable: enable/disable flag
84 static void skl_enable_miscbdcge(struct device *dev, bool enable)
86 struct pci_dev *pci = to_pci_dev(dev);
89 val = enable ? AZX_CGCTL_MISCBDCGE_MASK : 0;
91 update_pci_dword(pci, AZX_PCIREG_CGCTL, AZX_CGCTL_MISCBDCGE_MASK, val);
95 * While performing reset, controller may not come back properly causing
96 * issues, so recommendation is to set CGCTL.MISCBDCGE to 0 then do reset
97 * (init chip) and then again set CGCTL.MISCBDCGE to 1
99 static int skl_init_chip(struct hdac_bus *bus, bool full_reset)
103 skl_enable_miscbdcge(bus->dev, false);
104 ret = snd_hdac_bus_init_chip(bus, full_reset);
105 skl_enable_miscbdcge(bus->dev, true);
110 /* called from IRQ */
111 static void skl_stream_update(struct hdac_bus *bus, struct hdac_stream *hstr)
113 snd_pcm_period_elapsed(hstr->substream);
116 static irqreturn_t skl_interrupt(int irq, void *dev_id)
118 struct hdac_ext_bus *ebus = dev_id;
119 struct hdac_bus *bus = ebus_to_hbus(ebus);
122 if (!pm_runtime_active(bus->dev))
125 spin_lock(&bus->reg_lock);
127 status = snd_hdac_chip_readl(bus, INTSTS);
128 if (status == 0 || status == 0xffffffff) {
129 spin_unlock(&bus->reg_lock);
134 status = snd_hdac_chip_readb(bus, RIRBSTS);
135 if (status & RIRB_INT_MASK) {
136 if (status & RIRB_INT_RESPONSE)
137 snd_hdac_bus_update_rirb(bus);
138 snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
141 spin_unlock(&bus->reg_lock);
143 return snd_hdac_chip_readl(bus, INTSTS) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
146 static irqreturn_t skl_threaded_handler(int irq, void *dev_id)
148 struct hdac_ext_bus *ebus = dev_id;
149 struct hdac_bus *bus = ebus_to_hbus(ebus);
152 status = snd_hdac_chip_readl(bus, INTSTS);
154 snd_hdac_bus_handle_stream_irq(bus, status, skl_stream_update);
159 static int skl_acquire_irq(struct hdac_ext_bus *ebus, int do_disconnect)
161 struct skl *skl = ebus_to_skl(ebus);
162 struct hdac_bus *bus = ebus_to_hbus(ebus);
165 ret = request_threaded_irq(skl->pci->irq, skl_interrupt,
166 skl_threaded_handler,
168 KBUILD_MODNAME, ebus);
171 "unable to grab IRQ %d, disabling device\n",
176 bus->irq = skl->pci->irq;
177 pci_intx(skl->pci, 1);
183 static int _skl_suspend(struct hdac_ext_bus *ebus)
185 struct skl *skl = ebus_to_skl(ebus);
186 struct hdac_bus *bus = ebus_to_hbus(ebus);
189 snd_hdac_ext_bus_link_power_down_all(ebus);
191 ret = skl_suspend_dsp(skl);
195 snd_hdac_bus_stop_chip(bus);
196 skl_enable_miscbdcge(bus->dev, false);
197 snd_hdac_bus_enter_link_reset(bus);
198 skl_enable_miscbdcge(bus->dev, true);
203 static int _skl_resume(struct hdac_ext_bus *ebus)
205 struct skl *skl = ebus_to_skl(ebus);
206 struct hdac_bus *bus = ebus_to_hbus(ebus);
209 skl_init_chip(bus, true);
211 return skl_resume_dsp(skl);
215 #ifdef CONFIG_PM_SLEEP
219 static int skl_suspend(struct device *dev)
221 struct pci_dev *pci = to_pci_dev(dev);
222 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
223 struct skl *skl = ebus_to_skl(ebus);
224 struct hdac_bus *bus = ebus_to_hbus(ebus);
228 * Do not suspend if streams which are marked ignore suspend are
229 * running, we need to save the state for these and continue
231 if (skl->supend_active) {
232 snd_hdac_ext_bus_link_power_down_all(ebus);
233 enable_irq_wake(bus->irq);
235 pci_disable_device(pci);
237 ret = _skl_suspend(ebus);
242 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
243 ret = snd_hdac_display_power(bus, false);
246 "Cannot turn OFF display power on i915\n");
252 static int skl_resume(struct device *dev)
254 struct pci_dev *pci = to_pci_dev(dev);
255 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
256 struct skl *skl = ebus_to_skl(ebus);
257 struct hdac_bus *bus = ebus_to_hbus(ebus);
260 /* Turned OFF in HDMI codec driver after codec reconfiguration */
261 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
262 ret = snd_hdac_display_power(bus, true);
265 "Cannot turn on display power on i915\n");
271 * resume only when we are not in suspend active, otherwise need to
274 if (skl->supend_active) {
275 pci_restore_state(pci);
276 ret = pci_enable_device(pci);
277 snd_hdac_ext_bus_link_power_up_all(ebus);
278 disable_irq_wake(bus->irq);
280 ret = _skl_resume(ebus);
285 #endif /* CONFIG_PM_SLEEP */
288 static int skl_runtime_suspend(struct device *dev)
290 struct pci_dev *pci = to_pci_dev(dev);
291 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
292 struct hdac_bus *bus = ebus_to_hbus(ebus);
294 dev_dbg(bus->dev, "in %s\n", __func__);
296 return _skl_suspend(ebus);
299 static int skl_runtime_resume(struct device *dev)
301 struct pci_dev *pci = to_pci_dev(dev);
302 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
303 struct hdac_bus *bus = ebus_to_hbus(ebus);
305 dev_dbg(bus->dev, "in %s\n", __func__);
307 return _skl_resume(ebus);
309 #endif /* CONFIG_PM */
311 static const struct dev_pm_ops skl_pm = {
312 SET_SYSTEM_SLEEP_PM_OPS(skl_suspend, skl_resume)
313 SET_RUNTIME_PM_OPS(skl_runtime_suspend, skl_runtime_resume, NULL)
319 static int skl_free(struct hdac_ext_bus *ebus)
321 struct skl *skl = ebus_to_skl(ebus);
322 struct hdac_bus *bus = ebus_to_hbus(ebus);
324 skl->init_failed = 1; /* to be sure */
326 snd_hdac_ext_stop_streams(ebus);
329 free_irq(bus->irq, (void *)bus);
330 snd_hdac_bus_free_stream_pages(bus);
331 snd_hdac_stream_free_all(ebus);
332 snd_hdac_link_free_all(ebus);
335 iounmap(bus->remap_addr);
337 pci_release_regions(skl->pci);
338 pci_disable_device(skl->pci);
340 snd_hdac_ext_bus_exit(ebus);
342 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI))
343 snd_hdac_i915_exit(&ebus->bus);
347 static int skl_machine_device_register(struct skl *skl, void *driver_data)
349 struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
350 struct platform_device *pdev;
351 struct sst_acpi_mach *mach = driver_data;
354 mach = sst_acpi_find_machine(mach);
356 dev_err(bus->dev, "No matching machine driver found\n");
359 skl->fw_name = mach->fw_filename;
361 pdev = platform_device_alloc(mach->drv_name, -1);
363 dev_err(bus->dev, "platform device alloc failed\n");
367 ret = platform_device_add(pdev);
369 dev_err(bus->dev, "failed to add machine device\n");
370 platform_device_put(pdev);
378 static void skl_machine_device_unregister(struct skl *skl)
381 platform_device_unregister(skl->i2s_dev);
384 static int skl_dmic_device_register(struct skl *skl)
386 struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
387 struct platform_device *pdev;
390 /* SKL has one dmic port, so allocate dmic device for this */
391 pdev = platform_device_alloc("dmic-codec", -1);
393 dev_err(bus->dev, "failed to allocate dmic device\n");
397 ret = platform_device_add(pdev);
399 dev_err(bus->dev, "failed to add dmic device: %d\n", ret);
400 platform_device_put(pdev);
403 skl->dmic_dev = pdev;
408 static void skl_dmic_device_unregister(struct skl *skl)
411 platform_device_unregister(skl->dmic_dev);
415 * Probe the given codec address
417 static int probe_codec(struct hdac_ext_bus *ebus, int addr)
419 struct hdac_bus *bus = ebus_to_hbus(ebus);
420 unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
421 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
424 mutex_lock(&bus->cmd_mutex);
425 snd_hdac_bus_send_cmd(bus, cmd);
426 snd_hdac_bus_get_response(bus, addr, &res);
427 mutex_unlock(&bus->cmd_mutex);
430 dev_dbg(bus->dev, "codec #%d probed OK\n", addr);
432 return snd_hdac_ext_bus_device_init(ebus, addr);
435 /* Codec initialization */
436 static int skl_codec_create(struct hdac_ext_bus *ebus)
438 struct hdac_bus *bus = ebus_to_hbus(ebus);
441 max_slots = HDA_MAX_CODECS;
443 /* First try to probe all given codec slots */
444 for (c = 0; c < max_slots; c++) {
445 if ((bus->codec_mask & (1 << c))) {
446 if (probe_codec(ebus, c) < 0) {
448 * Some BIOSen give you wrong codec addresses
452 "Codec #%d probe error; disabling it...\n", c);
453 bus->codec_mask &= ~(1 << c);
455 * More badly, accessing to a non-existing
456 * codec often screws up the controller bus,
457 * and disturbs the further communications.
458 * Thus if an error occurs during probing,
459 * better to reset the controller bus to get
460 * back to the sanity state.
462 snd_hdac_bus_stop_chip(bus);
463 skl_init_chip(bus, true);
471 static const struct hdac_bus_ops bus_core_ops = {
472 .command = snd_hdac_bus_send_cmd,
473 .get_response = snd_hdac_bus_get_response,
479 static int skl_create(struct pci_dev *pci,
480 const struct hdac_io_ops *io_ops,
484 struct hdac_ext_bus *ebus;
490 err = pci_enable_device(pci);
494 skl = devm_kzalloc(&pci->dev, sizeof(*skl), GFP_KERNEL);
496 pci_disable_device(pci);
500 snd_hdac_ext_bus_init(ebus, &pci->dev, &bus_core_ops, io_ops);
501 ebus->bus.use_posbuf = 1;
504 ebus->bus.bdl_pos_adj = 0;
511 static int skl_i915_init(struct hdac_bus *bus)
516 * The HDMI codec is in GPU so we need to ensure that it is powered
517 * up and ready for probe
519 err = snd_hdac_i915_init(bus);
523 err = snd_hdac_display_power(bus, true);
525 dev_err(bus->dev, "Cannot turn on display power on i915\n");
532 static int skl_first_init(struct hdac_ext_bus *ebus)
534 struct skl *skl = ebus_to_skl(ebus);
535 struct hdac_bus *bus = ebus_to_hbus(ebus);
536 struct pci_dev *pci = skl->pci;
539 int cp_streams, pb_streams, start_idx;
541 err = pci_request_regions(pci, "Skylake HD audio");
545 bus->addr = pci_resource_start(pci, 0);
546 bus->remap_addr = pci_ioremap_bar(pci, 0);
547 if (bus->remap_addr == NULL) {
548 dev_err(bus->dev, "ioremap error\n");
552 snd_hdac_ext_bus_parse_capabilities(ebus);
554 if (skl_acquire_irq(ebus, 0) < 0)
558 synchronize_irq(bus->irq);
560 gcap = snd_hdac_chip_readw(bus, GCAP);
561 dev_dbg(bus->dev, "chipset global capabilities = 0x%x\n", gcap);
563 /* allow 64bit DMA address if supported by H/W */
564 if (!dma_set_mask(bus->dev, DMA_BIT_MASK(64))) {
565 dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(64));
567 dma_set_mask(bus->dev, DMA_BIT_MASK(32));
568 dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(32));
571 /* read number of streams from GCAP register */
572 cp_streams = (gcap >> 8) & 0x0f;
573 pb_streams = (gcap >> 12) & 0x0f;
575 if (!pb_streams && !cp_streams)
578 ebus->num_streams = cp_streams + pb_streams;
580 /* initialize streams */
581 snd_hdac_ext_stream_init_all
582 (ebus, 0, cp_streams, SNDRV_PCM_STREAM_CAPTURE);
583 start_idx = cp_streams;
584 snd_hdac_ext_stream_init_all
585 (ebus, start_idx, pb_streams, SNDRV_PCM_STREAM_PLAYBACK);
587 err = snd_hdac_bus_alloc_stream_pages(bus);
591 /* initialize chip */
594 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
595 err = skl_i915_init(bus);
600 skl_init_chip(bus, true);
602 /* codec detection */
603 if (!bus->codec_mask) {
604 dev_info(bus->dev, "no hda codecs found!\n");
610 static int skl_probe(struct pci_dev *pci,
611 const struct pci_device_id *pci_id)
614 struct hdac_ext_bus *ebus = NULL;
615 struct hdac_bus *bus = NULL;
618 /* we use ext core ops, so provide NULL for ops here */
619 err = skl_create(pci, NULL, &skl);
624 bus = ebus_to_hbus(ebus);
626 err = skl_first_init(ebus);
630 skl->pci_id = pci->device;
632 skl->nhlt = skl_nhlt_init(bus->dev);
634 if (skl->nhlt == NULL)
637 skl_nhlt_update_topology_bin(skl);
639 pci_set_drvdata(skl->pci, ebus);
641 /* check if dsp is there */
643 err = skl_machine_device_register(skl,
644 (void *)pci_id->driver_data);
648 err = skl_init_dsp(skl);
650 dev_dbg(bus->dev, "error failed to register dsp\n");
653 skl->skl_sst->enable_miscbdcge = skl_enable_miscbdcge;
657 snd_hdac_ext_bus_get_ml_capabilities(ebus);
659 /* create device for soc dmic */
660 err = skl_dmic_device_register(skl);
664 /* register platform dai and controls */
665 err = skl_platform_register(bus->dev);
669 /* create codec instances */
670 err = skl_codec_create(ebus);
674 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
675 err = snd_hdac_display_power(bus, false);
677 dev_err(bus->dev, "Cannot turn off display power on i915\n");
683 pm_runtime_put_noidle(bus->dev);
684 pm_runtime_allow(bus->dev);
689 skl_platform_unregister(bus->dev);
691 skl_dmic_device_unregister(skl);
695 skl_machine_device_unregister(skl);
697 skl->init_failed = 1;
703 static void skl_shutdown(struct pci_dev *pci)
705 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
706 struct hdac_bus *bus = ebus_to_hbus(ebus);
707 struct hdac_stream *s;
708 struct hdac_ext_stream *stream;
714 skl = ebus_to_skl(ebus);
716 if (skl->init_failed)
719 snd_hdac_ext_stop_streams(ebus);
720 list_for_each_entry(s, &bus->stream_list, list) {
721 stream = stream_to_hdac_ext_stream(s);
722 snd_hdac_ext_stream_decouple(ebus, stream, false);
725 snd_hdac_bus_stop_chip(bus);
728 static void skl_remove(struct pci_dev *pci)
730 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
731 struct skl *skl = ebus_to_skl(ebus);
734 release_firmware(skl->tplg);
736 if (pci_dev_run_wake(pci))
737 pm_runtime_get_noresume(&pci->dev);
739 /* codec removal, invoke bus_device_remove */
740 snd_hdac_ext_bus_device_remove(ebus);
742 skl_platform_unregister(&pci->dev);
744 skl_machine_device_unregister(skl);
745 skl_dmic_device_unregister(skl);
747 dev_set_drvdata(&pci->dev, NULL);
750 static struct sst_acpi_mach sst_skl_devdata[] = {
751 { "INT343A", "skl_alc286s_i2s", "intel/dsp_fw_release.bin", NULL, NULL, NULL },
752 { "INT343B", "skl_nau88l25_ssm4567_i2s", "intel/dsp_fw_release.bin",
754 { "MX98357A", "skl_nau88l25_max98357a_i2s", "intel/dsp_fw_release.bin",
759 static struct sst_acpi_mach sst_bxtp_devdata[] = {
760 { "INT343A", "bxt_alc298s_i2s", "intel/dsp_fw_bxtn.bin", NULL, NULL, NULL },
764 static const struct pci_device_id skl_ids[] = {
765 /* Sunrise Point-LP */
766 { PCI_DEVICE(0x8086, 0x9d70),
767 .driver_data = (unsigned long)&sst_skl_devdata},
769 { PCI_DEVICE(0x8086, 0x5a98),
770 .driver_data = (unsigned long)&sst_bxtp_devdata},
773 MODULE_DEVICE_TABLE(pci, skl_ids);
775 /* pci_driver definition */
776 static struct pci_driver skl_driver = {
777 .name = KBUILD_MODNAME,
780 .remove = skl_remove,
781 .shutdown = skl_shutdown,
786 module_pci_driver(skl_driver);
788 MODULE_LICENSE("GPL v2");
789 MODULE_DESCRIPTION("Intel Skylake ASoC HDA driver");