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"
32 #include "skl-sst-dsp.h"
33 #include "skl-sst-ipc.h"
36 * initialize the PCI registers
38 static void skl_update_pci_byte(struct pci_dev *pci, unsigned int reg,
39 unsigned char mask, unsigned char val)
43 pci_read_config_byte(pci, reg, &data);
46 pci_write_config_byte(pci, reg, data);
49 static void skl_init_pci(struct skl *skl)
51 struct hdac_ext_bus *ebus = &skl->ebus;
54 * Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
55 * TCSEL == Traffic Class Select Register, which sets PCI express QOS
56 * Ensuring these bits are 0 clears playback static on some HD Audio
58 * The PCI register TCSEL is defined in the Intel manuals.
60 dev_dbg(ebus_to_hbus(ebus)->dev, "Clearing TCSEL\n");
61 skl_update_pci_byte(skl->pci, AZX_PCIREG_TCSEL, 0x07, 0);
64 static void update_pci_dword(struct pci_dev *pci,
65 unsigned int reg, u32 mask, u32 val)
69 pci_read_config_dword(pci, reg, &data);
72 pci_write_config_dword(pci, reg, data);
76 * skl_enable_miscbdcge - enable/dsiable CGCTL.MISCBDCGE bits
78 * @dev: device pointer
79 * @enable: enable/disable flag
81 static void skl_enable_miscbdcge(struct device *dev, bool enable)
83 struct pci_dev *pci = to_pci_dev(dev);
86 val = enable ? AZX_CGCTL_MISCBDCGE_MASK : 0;
88 update_pci_dword(pci, AZX_PCIREG_CGCTL, AZX_CGCTL_MISCBDCGE_MASK, val);
92 * While performing reset, controller may not come back properly causing
93 * issues, so recommendation is to set CGCTL.MISCBDCGE to 0 then do reset
94 * (init chip) and then again set CGCTL.MISCBDCGE to 1
96 static int skl_init_chip(struct hdac_bus *bus, bool full_reset)
100 skl_enable_miscbdcge(bus->dev, false);
101 ret = snd_hdac_bus_init_chip(bus, full_reset);
102 skl_enable_miscbdcge(bus->dev, true);
107 /* called from IRQ */
108 static void skl_stream_update(struct hdac_bus *bus, struct hdac_stream *hstr)
110 snd_pcm_period_elapsed(hstr->substream);
113 static irqreturn_t skl_interrupt(int irq, void *dev_id)
115 struct hdac_ext_bus *ebus = dev_id;
116 struct hdac_bus *bus = ebus_to_hbus(ebus);
119 if (!pm_runtime_active(bus->dev))
122 spin_lock(&bus->reg_lock);
124 status = snd_hdac_chip_readl(bus, INTSTS);
125 if (status == 0 || status == 0xffffffff) {
126 spin_unlock(&bus->reg_lock);
131 status = snd_hdac_chip_readb(bus, RIRBSTS);
132 if (status & RIRB_INT_MASK) {
133 if (status & RIRB_INT_RESPONSE)
134 snd_hdac_bus_update_rirb(bus);
135 snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
138 spin_unlock(&bus->reg_lock);
140 return snd_hdac_chip_readl(bus, INTSTS) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
143 static irqreturn_t skl_threaded_handler(int irq, void *dev_id)
145 struct hdac_ext_bus *ebus = dev_id;
146 struct hdac_bus *bus = ebus_to_hbus(ebus);
149 status = snd_hdac_chip_readl(bus, INTSTS);
151 snd_hdac_bus_handle_stream_irq(bus, status, skl_stream_update);
156 static int skl_acquire_irq(struct hdac_ext_bus *ebus, int do_disconnect)
158 struct skl *skl = ebus_to_skl(ebus);
159 struct hdac_bus *bus = ebus_to_hbus(ebus);
162 ret = request_threaded_irq(skl->pci->irq, skl_interrupt,
163 skl_threaded_handler,
165 KBUILD_MODNAME, ebus);
168 "unable to grab IRQ %d, disabling device\n",
173 bus->irq = skl->pci->irq;
174 pci_intx(skl->pci, 1);
180 static int _skl_suspend(struct hdac_ext_bus *ebus)
182 struct skl *skl = ebus_to_skl(ebus);
183 struct hdac_bus *bus = ebus_to_hbus(ebus);
186 snd_hdac_ext_bus_link_power_down_all(ebus);
188 ret = skl_suspend_dsp(skl);
192 snd_hdac_bus_stop_chip(bus);
193 skl_enable_miscbdcge(bus->dev, false);
194 snd_hdac_bus_enter_link_reset(bus);
195 skl_enable_miscbdcge(bus->dev, true);
200 static int _skl_resume(struct hdac_ext_bus *ebus)
202 struct skl *skl = ebus_to_skl(ebus);
203 struct hdac_bus *bus = ebus_to_hbus(ebus);
206 skl_init_chip(bus, true);
208 return skl_resume_dsp(skl);
212 #ifdef CONFIG_PM_SLEEP
216 static int skl_suspend(struct device *dev)
218 struct pci_dev *pci = to_pci_dev(dev);
219 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
220 struct skl *skl = ebus_to_skl(ebus);
221 struct hdac_bus *bus = ebus_to_hbus(ebus);
224 * Do not suspend if streams which are marked ignore suspend are
225 * running, we need to save the state for these and continue
227 if (skl->supend_active) {
228 snd_hdac_ext_bus_link_power_down_all(ebus);
229 enable_irq_wake(bus->irq);
231 pci_disable_device(pci);
234 return _skl_suspend(ebus);
238 static int skl_resume(struct device *dev)
240 struct pci_dev *pci = to_pci_dev(dev);
241 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
242 struct skl *skl = ebus_to_skl(ebus);
243 struct hdac_bus *bus = ebus_to_hbus(ebus);
247 * resume only when we are not in suspend active, otherwise need to
250 if (skl->supend_active) {
251 pci_restore_state(pci);
252 ret = pci_enable_device(pci);
253 snd_hdac_ext_bus_link_power_up_all(ebus);
254 disable_irq_wake(bus->irq);
256 ret = _skl_resume(ebus);
261 #endif /* CONFIG_PM_SLEEP */
264 static int skl_runtime_suspend(struct device *dev)
266 struct pci_dev *pci = to_pci_dev(dev);
267 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
268 struct hdac_bus *bus = ebus_to_hbus(ebus);
270 dev_dbg(bus->dev, "in %s\n", __func__);
272 return _skl_suspend(ebus);
275 static int skl_runtime_resume(struct device *dev)
277 struct pci_dev *pci = to_pci_dev(dev);
278 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
279 struct hdac_bus *bus = ebus_to_hbus(ebus);
281 dev_dbg(bus->dev, "in %s\n", __func__);
283 return _skl_resume(ebus);
285 #endif /* CONFIG_PM */
287 static const struct dev_pm_ops skl_pm = {
288 SET_SYSTEM_SLEEP_PM_OPS(skl_suspend, skl_resume)
289 SET_RUNTIME_PM_OPS(skl_runtime_suspend, skl_runtime_resume, NULL)
295 static int skl_free(struct hdac_ext_bus *ebus)
297 struct skl *skl = ebus_to_skl(ebus);
298 struct hdac_bus *bus = ebus_to_hbus(ebus);
300 skl->init_failed = 1; /* to be sure */
302 snd_hdac_ext_stop_streams(ebus);
305 free_irq(bus->irq, (void *)bus);
307 iounmap(bus->remap_addr);
309 snd_hdac_bus_free_stream_pages(bus);
310 snd_hdac_stream_free_all(ebus);
311 snd_hdac_link_free_all(ebus);
312 pci_release_regions(skl->pci);
313 pci_disable_device(skl->pci);
315 snd_hdac_ext_bus_exit(ebus);
320 static int skl_machine_device_register(struct skl *skl, void *driver_data)
322 struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
323 struct platform_device *pdev;
324 struct sst_acpi_mach *mach = driver_data;
327 mach = sst_acpi_find_machine(mach);
329 dev_err(bus->dev, "No matching machine driver found\n");
332 skl->fw_name = mach->fw_filename;
334 pdev = platform_device_alloc(mach->drv_name, -1);
336 dev_err(bus->dev, "platform device alloc failed\n");
340 ret = platform_device_add(pdev);
342 dev_err(bus->dev, "failed to add machine device\n");
343 platform_device_put(pdev);
351 static void skl_machine_device_unregister(struct skl *skl)
354 platform_device_unregister(skl->i2s_dev);
357 static int skl_dmic_device_register(struct skl *skl)
359 struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
360 struct platform_device *pdev;
363 /* SKL has one dmic port, so allocate dmic device for this */
364 pdev = platform_device_alloc("dmic-codec", -1);
366 dev_err(bus->dev, "failed to allocate dmic device\n");
370 ret = platform_device_add(pdev);
372 dev_err(bus->dev, "failed to add dmic device: %d\n", ret);
373 platform_device_put(pdev);
376 skl->dmic_dev = pdev;
381 static void skl_dmic_device_unregister(struct skl *skl)
384 platform_device_unregister(skl->dmic_dev);
388 * Probe the given codec address
390 static int probe_codec(struct hdac_ext_bus *ebus, int addr)
392 struct hdac_bus *bus = ebus_to_hbus(ebus);
393 unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
394 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
397 mutex_lock(&bus->cmd_mutex);
398 snd_hdac_bus_send_cmd(bus, cmd);
399 snd_hdac_bus_get_response(bus, addr, &res);
400 mutex_unlock(&bus->cmd_mutex);
403 dev_dbg(bus->dev, "codec #%d probed OK\n", addr);
405 return snd_hdac_ext_bus_device_init(ebus, addr);
408 /* Codec initialization */
409 static int skl_codec_create(struct hdac_ext_bus *ebus)
411 struct hdac_bus *bus = ebus_to_hbus(ebus);
414 max_slots = HDA_MAX_CODECS;
416 /* First try to probe all given codec slots */
417 for (c = 0; c < max_slots; c++) {
418 if ((bus->codec_mask & (1 << c))) {
419 if (probe_codec(ebus, c) < 0) {
421 * Some BIOSen give you wrong codec addresses
425 "Codec #%d probe error; disabling it...\n", c);
426 bus->codec_mask &= ~(1 << c);
428 * More badly, accessing to a non-existing
429 * codec often screws up the controller bus,
430 * and disturbs the further communications.
431 * Thus if an error occurs during probing,
432 * better to reset the controller bus to get
433 * back to the sanity state.
435 snd_hdac_bus_stop_chip(bus);
436 skl_init_chip(bus, true);
444 static const struct hdac_bus_ops bus_core_ops = {
445 .command = snd_hdac_bus_send_cmd,
446 .get_response = snd_hdac_bus_get_response,
452 static int skl_create(struct pci_dev *pci,
453 const struct hdac_io_ops *io_ops,
457 struct hdac_ext_bus *ebus;
463 err = pci_enable_device(pci);
467 skl = devm_kzalloc(&pci->dev, sizeof(*skl), GFP_KERNEL);
469 pci_disable_device(pci);
473 snd_hdac_ext_bus_init(ebus, &pci->dev, &bus_core_ops, io_ops);
474 ebus->bus.use_posbuf = 1;
477 ebus->bus.bdl_pos_adj = 0;
484 static int skl_first_init(struct hdac_ext_bus *ebus)
486 struct skl *skl = ebus_to_skl(ebus);
487 struct hdac_bus *bus = ebus_to_hbus(ebus);
488 struct pci_dev *pci = skl->pci;
491 int cp_streams, pb_streams, start_idx;
493 err = pci_request_regions(pci, "Skylake HD audio");
497 bus->addr = pci_resource_start(pci, 0);
498 bus->remap_addr = pci_ioremap_bar(pci, 0);
499 if (bus->remap_addr == NULL) {
500 dev_err(bus->dev, "ioremap error\n");
504 snd_hdac_ext_bus_parse_capabilities(ebus);
506 if (skl_acquire_irq(ebus, 0) < 0)
510 synchronize_irq(bus->irq);
512 gcap = snd_hdac_chip_readw(bus, GCAP);
513 dev_dbg(bus->dev, "chipset global capabilities = 0x%x\n", gcap);
515 /* allow 64bit DMA address if supported by H/W */
516 if (!dma_set_mask(bus->dev, DMA_BIT_MASK(64))) {
517 dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(64));
519 dma_set_mask(bus->dev, DMA_BIT_MASK(32));
520 dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(32));
523 /* read number of streams from GCAP register */
524 cp_streams = (gcap >> 8) & 0x0f;
525 pb_streams = (gcap >> 12) & 0x0f;
527 if (!pb_streams && !cp_streams)
530 ebus->num_streams = cp_streams + pb_streams;
532 /* initialize streams */
533 snd_hdac_ext_stream_init_all
534 (ebus, 0, cp_streams, SNDRV_PCM_STREAM_CAPTURE);
535 start_idx = cp_streams;
536 snd_hdac_ext_stream_init_all
537 (ebus, start_idx, pb_streams, SNDRV_PCM_STREAM_PLAYBACK);
539 err = snd_hdac_bus_alloc_stream_pages(bus);
543 /* initialize chip */
546 skl_init_chip(bus, true);
548 /* codec detection */
549 if (!bus->codec_mask) {
550 dev_info(bus->dev, "no hda codecs found!\n");
556 static int skl_probe(struct pci_dev *pci,
557 const struct pci_device_id *pci_id)
560 struct hdac_ext_bus *ebus = NULL;
561 struct hdac_bus *bus = NULL;
564 /* we use ext core ops, so provide NULL for ops here */
565 err = skl_create(pci, NULL, &skl);
570 bus = ebus_to_hbus(ebus);
572 err = skl_first_init(ebus);
576 skl->nhlt = skl_nhlt_init(bus->dev);
578 if (skl->nhlt == NULL)
581 pci_set_drvdata(skl->pci, ebus);
583 /* check if dsp is there */
585 err = skl_machine_device_register(skl,
586 (void *)pci_id->driver_data);
590 err = skl_init_dsp(skl);
592 dev_dbg(bus->dev, "error failed to register dsp\n");
595 skl->skl_sst->enable_miscbdcge = skl_enable_miscbdcge;
599 snd_hdac_ext_bus_get_ml_capabilities(ebus);
601 /* create device for soc dmic */
602 err = skl_dmic_device_register(skl);
606 /* register platform dai and controls */
607 err = skl_platform_register(bus->dev);
611 /* create codec instances */
612 err = skl_codec_create(ebus);
617 pm_runtime_put_noidle(bus->dev);
618 pm_runtime_allow(bus->dev);
623 skl_platform_unregister(bus->dev);
625 skl_dmic_device_unregister(skl);
629 skl_machine_device_unregister(skl);
631 skl->init_failed = 1;
637 static void skl_shutdown(struct pci_dev *pci)
639 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
640 struct hdac_bus *bus = ebus_to_hbus(ebus);
641 struct hdac_stream *s;
642 struct hdac_ext_stream *stream;
648 skl = ebus_to_skl(ebus);
650 if (skl->init_failed)
653 snd_hdac_ext_stop_streams(ebus);
654 list_for_each_entry(s, &bus->stream_list, list) {
655 stream = stream_to_hdac_ext_stream(s);
656 snd_hdac_ext_stream_decouple(ebus, stream, false);
659 snd_hdac_bus_stop_chip(bus);
662 static void skl_remove(struct pci_dev *pci)
664 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
665 struct skl *skl = ebus_to_skl(ebus);
668 release_firmware(skl->tplg);
670 if (pci_dev_run_wake(pci))
671 pm_runtime_get_noresume(&pci->dev);
673 skl_platform_unregister(&pci->dev);
675 skl_machine_device_unregister(skl);
676 skl_dmic_device_unregister(skl);
678 dev_set_drvdata(&pci->dev, NULL);
681 static struct sst_acpi_mach sst_skl_devdata[] = {
682 { "INT343A", "skl_alc286s_i2s", "intel/dsp_fw_release.bin", NULL, NULL, NULL },
683 { "INT343B", "skl_nau88l25_ssm4567_i2s", "intel/dsp_fw_release.bin",
685 { "MX98357A", "skl_nau88l25_max98357a_i2s", "intel/dsp_fw_release.bin",
691 static const struct pci_device_id skl_ids[] = {
692 /* Sunrise Point-LP */
693 { PCI_DEVICE(0x8086, 0x9d70),
694 .driver_data = (unsigned long)&sst_skl_devdata},
697 MODULE_DEVICE_TABLE(pci, skl_ids);
699 /* pci_driver definition */
700 static struct pci_driver skl_driver = {
701 .name = KBUILD_MODNAME,
704 .remove = skl_remove,
705 .shutdown = skl_shutdown,
710 module_pci_driver(skl_driver);
712 MODULE_LICENSE("GPL v2");
713 MODULE_DESCRIPTION("Intel Skylake ASoC HDA driver");