Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 1 | /* |
| 2 | * skl.c - Implementation of ASoC Intel SKL HD Audio driver |
| 3 | * |
| 4 | * Copyright (C) 2014-2015 Intel Corp |
| 5 | * Author: Jeeja KP <jeeja.kp@intel.com> |
| 6 | * |
| 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 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 11 | * |
| 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. |
| 15 | * |
| 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. |
| 20 | * |
| 21 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/pci.h> |
| 26 | #include <linux/pm_runtime.h> |
| 27 | #include <linux/platform_device.h> |
Vinod Koul | d801836 | 2016-01-05 17:16:04 +0530 | [diff] [blame] | 28 | #include <linux/firmware.h> |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 29 | #include <sound/pcm.h> |
Vinod Koul | cc18c5f | 2015-11-05 21:34:13 +0530 | [diff] [blame] | 30 | #include "../common/sst-acpi.h" |
Vinod Koul | 6980c05 | 2016-02-17 21:34:06 +0530 | [diff] [blame] | 31 | #include <sound/hda_register.h> |
| 32 | #include <sound/hdaudio.h> |
| 33 | #include <sound/hda_i915.h> |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 34 | #include "skl.h" |
Jayachandran B | 0c8ba9d | 2015-12-18 15:12:03 +0530 | [diff] [blame] | 35 | #include "skl-sst-dsp.h" |
| 36 | #include "skl-sst-ipc.h" |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * initialize the PCI registers |
| 40 | */ |
| 41 | static void skl_update_pci_byte(struct pci_dev *pci, unsigned int reg, |
| 42 | unsigned char mask, unsigned char val) |
| 43 | { |
| 44 | unsigned char data; |
| 45 | |
| 46 | pci_read_config_byte(pci, reg, &data); |
| 47 | data &= ~mask; |
| 48 | data |= (val & mask); |
| 49 | pci_write_config_byte(pci, reg, data); |
| 50 | } |
| 51 | |
| 52 | static void skl_init_pci(struct skl *skl) |
| 53 | { |
| 54 | struct hdac_ext_bus *ebus = &skl->ebus; |
| 55 | |
| 56 | /* |
| 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 |
| 60 | * codecs. |
| 61 | * The PCI register TCSEL is defined in the Intel manuals. |
| 62 | */ |
| 63 | dev_dbg(ebus_to_hbus(ebus)->dev, "Clearing TCSEL\n"); |
| 64 | skl_update_pci_byte(skl->pci, AZX_PCIREG_TCSEL, 0x07, 0); |
| 65 | } |
| 66 | |
Jayachandran B | 0c8ba9d | 2015-12-18 15:12:03 +0530 | [diff] [blame] | 67 | static void update_pci_dword(struct pci_dev *pci, |
| 68 | unsigned int reg, u32 mask, u32 val) |
| 69 | { |
| 70 | u32 data = 0; |
| 71 | |
| 72 | pci_read_config_dword(pci, reg, &data); |
| 73 | data &= ~mask; |
| 74 | data |= (val & mask); |
| 75 | pci_write_config_dword(pci, reg, data); |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * skl_enable_miscbdcge - enable/dsiable CGCTL.MISCBDCGE bits |
| 80 | * |
| 81 | * @dev: device pointer |
| 82 | * @enable: enable/disable flag |
| 83 | */ |
| 84 | static void skl_enable_miscbdcge(struct device *dev, bool enable) |
| 85 | { |
| 86 | struct pci_dev *pci = to_pci_dev(dev); |
| 87 | u32 val; |
| 88 | |
| 89 | val = enable ? AZX_CGCTL_MISCBDCGE_MASK : 0; |
| 90 | |
| 91 | update_pci_dword(pci, AZX_PCIREG_CGCTL, AZX_CGCTL_MISCBDCGE_MASK, val); |
| 92 | } |
| 93 | |
| 94 | /* |
| 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 |
| 98 | */ |
| 99 | static int skl_init_chip(struct hdac_bus *bus, bool full_reset) |
| 100 | { |
| 101 | int ret; |
| 102 | |
| 103 | skl_enable_miscbdcge(bus->dev, false); |
| 104 | ret = snd_hdac_bus_init_chip(bus, full_reset); |
| 105 | skl_enable_miscbdcge(bus->dev, true); |
| 106 | |
| 107 | return ret; |
| 108 | } |
| 109 | |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 110 | /* called from IRQ */ |
| 111 | static void skl_stream_update(struct hdac_bus *bus, struct hdac_stream *hstr) |
| 112 | { |
| 113 | snd_pcm_period_elapsed(hstr->substream); |
| 114 | } |
| 115 | |
| 116 | static irqreturn_t skl_interrupt(int irq, void *dev_id) |
| 117 | { |
| 118 | struct hdac_ext_bus *ebus = dev_id; |
| 119 | struct hdac_bus *bus = ebus_to_hbus(ebus); |
| 120 | u32 status; |
| 121 | |
| 122 | if (!pm_runtime_active(bus->dev)) |
| 123 | return IRQ_NONE; |
| 124 | |
| 125 | spin_lock(&bus->reg_lock); |
| 126 | |
| 127 | status = snd_hdac_chip_readl(bus, INTSTS); |
| 128 | if (status == 0 || status == 0xffffffff) { |
| 129 | spin_unlock(&bus->reg_lock); |
| 130 | return IRQ_NONE; |
| 131 | } |
| 132 | |
| 133 | /* clear rirb int */ |
| 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); |
| 139 | } |
| 140 | |
| 141 | spin_unlock(&bus->reg_lock); |
| 142 | |
| 143 | return snd_hdac_chip_readl(bus, INTSTS) ? IRQ_WAKE_THREAD : IRQ_HANDLED; |
| 144 | } |
| 145 | |
| 146 | static irqreturn_t skl_threaded_handler(int irq, void *dev_id) |
| 147 | { |
| 148 | struct hdac_ext_bus *ebus = dev_id; |
| 149 | struct hdac_bus *bus = ebus_to_hbus(ebus); |
| 150 | u32 status; |
| 151 | |
| 152 | status = snd_hdac_chip_readl(bus, INTSTS); |
| 153 | |
| 154 | snd_hdac_bus_handle_stream_irq(bus, status, skl_stream_update); |
| 155 | |
| 156 | return IRQ_HANDLED; |
| 157 | } |
| 158 | |
| 159 | static int skl_acquire_irq(struct hdac_ext_bus *ebus, int do_disconnect) |
| 160 | { |
| 161 | struct skl *skl = ebus_to_skl(ebus); |
| 162 | struct hdac_bus *bus = ebus_to_hbus(ebus); |
| 163 | int ret; |
| 164 | |
| 165 | ret = request_threaded_irq(skl->pci->irq, skl_interrupt, |
| 166 | skl_threaded_handler, |
| 167 | IRQF_SHARED, |
| 168 | KBUILD_MODNAME, ebus); |
| 169 | if (ret) { |
| 170 | dev_err(bus->dev, |
| 171 | "unable to grab IRQ %d, disabling device\n", |
| 172 | skl->pci->irq); |
| 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | bus->irq = skl->pci->irq; |
| 177 | pci_intx(skl->pci, 1); |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 182 | #ifdef CONFIG_PM |
Jeeja KP | 61722f4 | 2015-10-27 09:23:00 +0900 | [diff] [blame] | 183 | static int _skl_suspend(struct hdac_ext_bus *ebus) |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 184 | { |
Jeeja KP | 2a29b20 | 2015-10-07 11:31:58 +0100 | [diff] [blame] | 185 | struct skl *skl = ebus_to_skl(ebus); |
Jeeja KP | 61722f4 | 2015-10-27 09:23:00 +0900 | [diff] [blame] | 186 | struct hdac_bus *bus = ebus_to_hbus(ebus); |
Jeeja KP | 2a29b20 | 2015-10-07 11:31:58 +0100 | [diff] [blame] | 187 | int ret; |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 188 | |
Jeeja KP | 01bb84b | 2015-10-09 09:01:51 +0100 | [diff] [blame] | 189 | snd_hdac_ext_bus_link_power_down_all(ebus); |
| 190 | |
Jeeja KP | 2a29b20 | 2015-10-07 11:31:58 +0100 | [diff] [blame] | 191 | ret = skl_suspend_dsp(skl); |
| 192 | if (ret < 0) |
| 193 | return ret; |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 194 | |
| 195 | snd_hdac_bus_stop_chip(bus); |
Jayachandran B | 0c8ba9d | 2015-12-18 15:12:03 +0530 | [diff] [blame] | 196 | skl_enable_miscbdcge(bus->dev, false); |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 197 | snd_hdac_bus_enter_link_reset(bus); |
Jayachandran B | 0c8ba9d | 2015-12-18 15:12:03 +0530 | [diff] [blame] | 198 | skl_enable_miscbdcge(bus->dev, true); |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Jeeja KP | 61722f4 | 2015-10-27 09:23:00 +0900 | [diff] [blame] | 203 | static int _skl_resume(struct hdac_ext_bus *ebus) |
| 204 | { |
| 205 | struct skl *skl = ebus_to_skl(ebus); |
| 206 | struct hdac_bus *bus = ebus_to_hbus(ebus); |
| 207 | |
| 208 | skl_init_pci(skl); |
Jayachandran B | 0c8ba9d | 2015-12-18 15:12:03 +0530 | [diff] [blame] | 209 | skl_init_chip(bus, true); |
Jeeja KP | 61722f4 | 2015-10-27 09:23:00 +0900 | [diff] [blame] | 210 | |
| 211 | return skl_resume_dsp(skl); |
| 212 | } |
| 213 | #endif |
| 214 | |
| 215 | #ifdef CONFIG_PM_SLEEP |
| 216 | /* |
| 217 | * power management |
| 218 | */ |
| 219 | static int skl_suspend(struct device *dev) |
| 220 | { |
| 221 | struct pci_dev *pci = to_pci_dev(dev); |
| 222 | struct hdac_ext_bus *ebus = pci_get_drvdata(pci); |
Jeeja KP | 4557c30 | 2015-12-03 23:30:00 +0530 | [diff] [blame] | 223 | struct skl *skl = ebus_to_skl(ebus); |
Jeeja KP | 1f4956f | 2015-12-18 15:12:06 +0530 | [diff] [blame] | 224 | struct hdac_bus *bus = ebus_to_hbus(ebus); |
Jeeja KP | 61722f4 | 2015-10-27 09:23:00 +0900 | [diff] [blame] | 225 | |
Jeeja KP | 4557c30 | 2015-12-03 23:30:00 +0530 | [diff] [blame] | 226 | /* |
| 227 | * Do not suspend if streams which are marked ignore suspend are |
| 228 | * running, we need to save the state for these and continue |
| 229 | */ |
| 230 | if (skl->supend_active) { |
Vinod Koul | c2e20cd | 2015-12-18 15:12:05 +0530 | [diff] [blame] | 231 | snd_hdac_ext_bus_link_power_down_all(ebus); |
Jeeja KP | 1f4956f | 2015-12-18 15:12:06 +0530 | [diff] [blame] | 232 | enable_irq_wake(bus->irq); |
Jeeja KP | 4557c30 | 2015-12-03 23:30:00 +0530 | [diff] [blame] | 233 | pci_save_state(pci); |
| 234 | pci_disable_device(pci); |
| 235 | return 0; |
| 236 | } else { |
| 237 | return _skl_suspend(ebus); |
| 238 | } |
Jeeja KP | 61722f4 | 2015-10-27 09:23:00 +0900 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static int skl_resume(struct device *dev) |
| 242 | { |
| 243 | struct pci_dev *pci = to_pci_dev(dev); |
| 244 | struct hdac_ext_bus *ebus = pci_get_drvdata(pci); |
Jeeja KP | 4557c30 | 2015-12-03 23:30:00 +0530 | [diff] [blame] | 245 | struct skl *skl = ebus_to_skl(ebus); |
Jeeja KP | 1f4956f | 2015-12-18 15:12:06 +0530 | [diff] [blame] | 246 | struct hdac_bus *bus = ebus_to_hbus(ebus); |
Jeeja KP | 4557c30 | 2015-12-03 23:30:00 +0530 | [diff] [blame] | 247 | int ret; |
Jeeja KP | 61722f4 | 2015-10-27 09:23:00 +0900 | [diff] [blame] | 248 | |
Vinod Koul | 6980c05 | 2016-02-17 21:34:06 +0530 | [diff] [blame] | 249 | /* Turned OFF in HDMI codec driver after codec reconfiguration */ |
| 250 | if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) { |
| 251 | ret = snd_hdac_display_power(bus, true); |
| 252 | if (ret < 0) { |
| 253 | dev_err(bus->dev, |
| 254 | "Cannot turn on display power on i915\n"); |
| 255 | return ret; |
| 256 | } |
| 257 | } |
| 258 | |
Jeeja KP | 4557c30 | 2015-12-03 23:30:00 +0530 | [diff] [blame] | 259 | /* |
| 260 | * resume only when we are not in suspend active, otherwise need to |
| 261 | * restore the device |
| 262 | */ |
| 263 | if (skl->supend_active) { |
| 264 | pci_restore_state(pci); |
| 265 | ret = pci_enable_device(pci); |
Vinod Koul | c2e20cd | 2015-12-18 15:12:05 +0530 | [diff] [blame] | 266 | snd_hdac_ext_bus_link_power_up_all(ebus); |
Jeeja KP | 1f4956f | 2015-12-18 15:12:06 +0530 | [diff] [blame] | 267 | disable_irq_wake(bus->irq); |
Jeeja KP | 4557c30 | 2015-12-03 23:30:00 +0530 | [diff] [blame] | 268 | } else { |
| 269 | ret = _skl_resume(ebus); |
| 270 | } |
| 271 | |
| 272 | return ret; |
Jeeja KP | 61722f4 | 2015-10-27 09:23:00 +0900 | [diff] [blame] | 273 | } |
| 274 | #endif /* CONFIG_PM_SLEEP */ |
| 275 | |
| 276 | #ifdef CONFIG_PM |
| 277 | static int skl_runtime_suspend(struct device *dev) |
| 278 | { |
| 279 | struct pci_dev *pci = to_pci_dev(dev); |
| 280 | struct hdac_ext_bus *ebus = pci_get_drvdata(pci); |
| 281 | struct hdac_bus *bus = ebus_to_hbus(ebus); |
| 282 | |
| 283 | dev_dbg(bus->dev, "in %s\n", __func__); |
| 284 | |
Jeeja KP | 61722f4 | 2015-10-27 09:23:00 +0900 | [diff] [blame] | 285 | return _skl_suspend(ebus); |
| 286 | } |
| 287 | |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 288 | static int skl_runtime_resume(struct device *dev) |
| 289 | { |
| 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); |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 293 | |
| 294 | dev_dbg(bus->dev, "in %s\n", __func__); |
| 295 | |
Jeeja KP | 61722f4 | 2015-10-27 09:23:00 +0900 | [diff] [blame] | 296 | return _skl_resume(ebus); |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 297 | } |
| 298 | #endif /* CONFIG_PM */ |
| 299 | |
| 300 | static const struct dev_pm_ops skl_pm = { |
| 301 | SET_SYSTEM_SLEEP_PM_OPS(skl_suspend, skl_resume) |
| 302 | SET_RUNTIME_PM_OPS(skl_runtime_suspend, skl_runtime_resume, NULL) |
| 303 | }; |
| 304 | |
| 305 | /* |
| 306 | * destructor |
| 307 | */ |
| 308 | static int skl_free(struct hdac_ext_bus *ebus) |
| 309 | { |
| 310 | struct skl *skl = ebus_to_skl(ebus); |
| 311 | struct hdac_bus *bus = ebus_to_hbus(ebus); |
| 312 | |
| 313 | skl->init_failed = 1; /* to be sure */ |
| 314 | |
| 315 | snd_hdac_ext_stop_streams(ebus); |
| 316 | |
| 317 | if (bus->irq >= 0) |
| 318 | free_irq(bus->irq, (void *)bus); |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 319 | snd_hdac_bus_free_stream_pages(bus); |
| 320 | snd_hdac_stream_free_all(ebus); |
| 321 | snd_hdac_link_free_all(ebus); |
Vinod Koul | 077411e | 2016-03-15 16:39:26 +0530 | [diff] [blame] | 322 | |
| 323 | if (bus->remap_addr) |
| 324 | iounmap(bus->remap_addr); |
| 325 | |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 326 | pci_release_regions(skl->pci); |
| 327 | pci_disable_device(skl->pci); |
| 328 | |
| 329 | snd_hdac_ext_bus_exit(ebus); |
| 330 | |
Vinod Koul | 5b2fe89 | 2016-03-15 16:39:27 +0530 | [diff] [blame^] | 331 | if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) |
| 332 | snd_hdac_i915_exit(&ebus->bus); |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 333 | return 0; |
| 334 | } |
| 335 | |
Vinod Koul | cc18c5f | 2015-11-05 21:34:13 +0530 | [diff] [blame] | 336 | static int skl_machine_device_register(struct skl *skl, void *driver_data) |
| 337 | { |
| 338 | struct hdac_bus *bus = ebus_to_hbus(&skl->ebus); |
| 339 | struct platform_device *pdev; |
| 340 | struct sst_acpi_mach *mach = driver_data; |
| 341 | int ret; |
| 342 | |
| 343 | mach = sst_acpi_find_machine(mach); |
| 344 | if (mach == NULL) { |
| 345 | dev_err(bus->dev, "No matching machine driver found\n"); |
| 346 | return -ENODEV; |
| 347 | } |
Vinod Koul | aecf6fd | 2015-11-05 21:34:15 +0530 | [diff] [blame] | 348 | skl->fw_name = mach->fw_filename; |
Vinod Koul | cc18c5f | 2015-11-05 21:34:13 +0530 | [diff] [blame] | 349 | |
| 350 | pdev = platform_device_alloc(mach->drv_name, -1); |
| 351 | if (pdev == NULL) { |
| 352 | dev_err(bus->dev, "platform device alloc failed\n"); |
| 353 | return -EIO; |
| 354 | } |
| 355 | |
| 356 | ret = platform_device_add(pdev); |
| 357 | if (ret) { |
| 358 | dev_err(bus->dev, "failed to add machine device\n"); |
| 359 | platform_device_put(pdev); |
| 360 | return -EIO; |
| 361 | } |
| 362 | skl->i2s_dev = pdev; |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | static void skl_machine_device_unregister(struct skl *skl) |
| 368 | { |
| 369 | if (skl->i2s_dev) |
| 370 | platform_device_unregister(skl->i2s_dev); |
| 371 | } |
| 372 | |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 373 | static int skl_dmic_device_register(struct skl *skl) |
| 374 | { |
| 375 | struct hdac_bus *bus = ebus_to_hbus(&skl->ebus); |
| 376 | struct platform_device *pdev; |
| 377 | int ret; |
| 378 | |
| 379 | /* SKL has one dmic port, so allocate dmic device for this */ |
| 380 | pdev = platform_device_alloc("dmic-codec", -1); |
| 381 | if (!pdev) { |
| 382 | dev_err(bus->dev, "failed to allocate dmic device\n"); |
| 383 | return -ENOMEM; |
| 384 | } |
| 385 | |
| 386 | ret = platform_device_add(pdev); |
| 387 | if (ret) { |
| 388 | dev_err(bus->dev, "failed to add dmic device: %d\n", ret); |
| 389 | platform_device_put(pdev); |
| 390 | return ret; |
| 391 | } |
| 392 | skl->dmic_dev = pdev; |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static void skl_dmic_device_unregister(struct skl *skl) |
| 398 | { |
| 399 | if (skl->dmic_dev) |
| 400 | platform_device_unregister(skl->dmic_dev); |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * Probe the given codec address |
| 405 | */ |
| 406 | static int probe_codec(struct hdac_ext_bus *ebus, int addr) |
| 407 | { |
| 408 | struct hdac_bus *bus = ebus_to_hbus(ebus); |
| 409 | unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) | |
| 410 | (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID; |
| 411 | unsigned int res; |
| 412 | |
| 413 | mutex_lock(&bus->cmd_mutex); |
| 414 | snd_hdac_bus_send_cmd(bus, cmd); |
| 415 | snd_hdac_bus_get_response(bus, addr, &res); |
| 416 | mutex_unlock(&bus->cmd_mutex); |
| 417 | if (res == -1) |
| 418 | return -EIO; |
| 419 | dev_dbg(bus->dev, "codec #%d probed OK\n", addr); |
| 420 | |
| 421 | return snd_hdac_ext_bus_device_init(ebus, addr); |
| 422 | } |
| 423 | |
| 424 | /* Codec initialization */ |
| 425 | static int skl_codec_create(struct hdac_ext_bus *ebus) |
| 426 | { |
| 427 | struct hdac_bus *bus = ebus_to_hbus(ebus); |
| 428 | int c, max_slots; |
| 429 | |
| 430 | max_slots = HDA_MAX_CODECS; |
| 431 | |
| 432 | /* First try to probe all given codec slots */ |
| 433 | for (c = 0; c < max_slots; c++) { |
| 434 | if ((bus->codec_mask & (1 << c))) { |
| 435 | if (probe_codec(ebus, c) < 0) { |
| 436 | /* |
| 437 | * Some BIOSen give you wrong codec addresses |
| 438 | * that don't exist |
| 439 | */ |
| 440 | dev_warn(bus->dev, |
| 441 | "Codec #%d probe error; disabling it...\n", c); |
| 442 | bus->codec_mask &= ~(1 << c); |
| 443 | /* |
| 444 | * More badly, accessing to a non-existing |
| 445 | * codec often screws up the controller bus, |
| 446 | * and disturbs the further communications. |
| 447 | * Thus if an error occurs during probing, |
| 448 | * better to reset the controller bus to get |
| 449 | * back to the sanity state. |
| 450 | */ |
| 451 | snd_hdac_bus_stop_chip(bus); |
Jayachandran B | 0c8ba9d | 2015-12-18 15:12:03 +0530 | [diff] [blame] | 452 | skl_init_chip(bus, true); |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | static const struct hdac_bus_ops bus_core_ops = { |
| 461 | .command = snd_hdac_bus_send_cmd, |
| 462 | .get_response = snd_hdac_bus_get_response, |
| 463 | }; |
| 464 | |
| 465 | /* |
| 466 | * constructor |
| 467 | */ |
| 468 | static int skl_create(struct pci_dev *pci, |
| 469 | const struct hdac_io_ops *io_ops, |
| 470 | struct skl **rskl) |
| 471 | { |
| 472 | struct skl *skl; |
| 473 | struct hdac_ext_bus *ebus; |
| 474 | |
| 475 | int err; |
| 476 | |
| 477 | *rskl = NULL; |
| 478 | |
| 479 | err = pci_enable_device(pci); |
| 480 | if (err < 0) |
| 481 | return err; |
| 482 | |
| 483 | skl = devm_kzalloc(&pci->dev, sizeof(*skl), GFP_KERNEL); |
| 484 | if (!skl) { |
| 485 | pci_disable_device(pci); |
| 486 | return -ENOMEM; |
| 487 | } |
| 488 | ebus = &skl->ebus; |
| 489 | snd_hdac_ext_bus_init(ebus, &pci->dev, &bus_core_ops, io_ops); |
| 490 | ebus->bus.use_posbuf = 1; |
| 491 | skl->pci = pci; |
| 492 | |
| 493 | ebus->bus.bdl_pos_adj = 0; |
| 494 | |
| 495 | *rskl = skl; |
| 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
Vinod Koul | 6980c05 | 2016-02-17 21:34:06 +0530 | [diff] [blame] | 500 | static int skl_i915_init(struct hdac_bus *bus) |
| 501 | { |
| 502 | int err; |
| 503 | |
| 504 | /* |
| 505 | * The HDMI codec is in GPU so we need to ensure that it is powered |
| 506 | * up and ready for probe |
| 507 | */ |
| 508 | err = snd_hdac_i915_init(bus); |
| 509 | if (err < 0) |
| 510 | return err; |
| 511 | |
| 512 | err = snd_hdac_display_power(bus, true); |
| 513 | if (err < 0) { |
| 514 | dev_err(bus->dev, "Cannot turn on display power on i915\n"); |
| 515 | return err; |
| 516 | } |
| 517 | |
| 518 | return err; |
| 519 | } |
| 520 | |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 521 | static int skl_first_init(struct hdac_ext_bus *ebus) |
| 522 | { |
| 523 | struct skl *skl = ebus_to_skl(ebus); |
| 524 | struct hdac_bus *bus = ebus_to_hbus(ebus); |
| 525 | struct pci_dev *pci = skl->pci; |
| 526 | int err; |
| 527 | unsigned short gcap; |
| 528 | int cp_streams, pb_streams, start_idx; |
| 529 | |
| 530 | err = pci_request_regions(pci, "Skylake HD audio"); |
| 531 | if (err < 0) |
| 532 | return err; |
| 533 | |
| 534 | bus->addr = pci_resource_start(pci, 0); |
| 535 | bus->remap_addr = pci_ioremap_bar(pci, 0); |
| 536 | if (bus->remap_addr == NULL) { |
| 537 | dev_err(bus->dev, "ioremap error\n"); |
| 538 | return -ENXIO; |
| 539 | } |
| 540 | |
Jeeja KP | 0505700 | 2015-07-09 15:20:11 +0530 | [diff] [blame] | 541 | snd_hdac_ext_bus_parse_capabilities(ebus); |
| 542 | |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 543 | if (skl_acquire_irq(ebus, 0) < 0) |
| 544 | return -EBUSY; |
| 545 | |
| 546 | pci_set_master(pci); |
| 547 | synchronize_irq(bus->irq); |
| 548 | |
| 549 | gcap = snd_hdac_chip_readw(bus, GCAP); |
| 550 | dev_dbg(bus->dev, "chipset global capabilities = 0x%x\n", gcap); |
| 551 | |
| 552 | /* allow 64bit DMA address if supported by H/W */ |
| 553 | if (!dma_set_mask(bus->dev, DMA_BIT_MASK(64))) { |
| 554 | dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(64)); |
| 555 | } else { |
| 556 | dma_set_mask(bus->dev, DMA_BIT_MASK(32)); |
| 557 | dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(32)); |
| 558 | } |
| 559 | |
| 560 | /* read number of streams from GCAP register */ |
| 561 | cp_streams = (gcap >> 8) & 0x0f; |
| 562 | pb_streams = (gcap >> 12) & 0x0f; |
| 563 | |
| 564 | if (!pb_streams && !cp_streams) |
| 565 | return -EIO; |
| 566 | |
| 567 | ebus->num_streams = cp_streams + pb_streams; |
| 568 | |
| 569 | /* initialize streams */ |
| 570 | snd_hdac_ext_stream_init_all |
| 571 | (ebus, 0, cp_streams, SNDRV_PCM_STREAM_CAPTURE); |
| 572 | start_idx = cp_streams; |
| 573 | snd_hdac_ext_stream_init_all |
| 574 | (ebus, start_idx, pb_streams, SNDRV_PCM_STREAM_PLAYBACK); |
| 575 | |
| 576 | err = snd_hdac_bus_alloc_stream_pages(bus); |
| 577 | if (err < 0) |
| 578 | return err; |
| 579 | |
| 580 | /* initialize chip */ |
| 581 | skl_init_pci(skl); |
| 582 | |
Vinod Koul | 6980c05 | 2016-02-17 21:34:06 +0530 | [diff] [blame] | 583 | if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) { |
| 584 | err = skl_i915_init(bus); |
| 585 | if (err < 0) |
| 586 | return err; |
| 587 | } |
| 588 | |
Jayachandran B | 0c8ba9d | 2015-12-18 15:12:03 +0530 | [diff] [blame] | 589 | skl_init_chip(bus, true); |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 590 | |
| 591 | /* codec detection */ |
| 592 | if (!bus->codec_mask) { |
Jeeja KP | 029890c | 2015-10-27 09:22:47 +0900 | [diff] [blame] | 593 | dev_info(bus->dev, "no hda codecs found!\n"); |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | static int skl_probe(struct pci_dev *pci, |
| 600 | const struct pci_device_id *pci_id) |
| 601 | { |
| 602 | struct skl *skl; |
| 603 | struct hdac_ext_bus *ebus = NULL; |
| 604 | struct hdac_bus *bus = NULL; |
| 605 | int err; |
| 606 | |
| 607 | /* we use ext core ops, so provide NULL for ops here */ |
| 608 | err = skl_create(pci, NULL, &skl); |
| 609 | if (err < 0) |
| 610 | return err; |
| 611 | |
| 612 | ebus = &skl->ebus; |
| 613 | bus = ebus_to_hbus(ebus); |
| 614 | |
| 615 | err = skl_first_init(ebus); |
| 616 | if (err < 0) |
| 617 | goto out_free; |
| 618 | |
Vinod Koul | 4b235c4 | 2016-02-19 11:42:34 +0530 | [diff] [blame] | 619 | skl->pci_id = pci->device; |
| 620 | |
Jeeja KP | 87b2bdf | 2015-10-07 11:31:59 +0100 | [diff] [blame] | 621 | skl->nhlt = skl_nhlt_init(bus->dev); |
| 622 | |
| 623 | if (skl->nhlt == NULL) |
| 624 | goto out_free; |
| 625 | |
Vinod Koul | 4b235c4 | 2016-02-19 11:42:34 +0530 | [diff] [blame] | 626 | skl_nhlt_update_topology_bin(skl); |
| 627 | |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 628 | pci_set_drvdata(skl->pci, ebus); |
| 629 | |
Jeeja KP | 0505700 | 2015-07-09 15:20:11 +0530 | [diff] [blame] | 630 | /* check if dsp is there */ |
| 631 | if (ebus->ppcap) { |
Vinod Koul | cc18c5f | 2015-11-05 21:34:13 +0530 | [diff] [blame] | 632 | err = skl_machine_device_register(skl, |
| 633 | (void *)pci_id->driver_data); |
| 634 | if (err < 0) |
| 635 | goto out_free; |
| 636 | |
Jeeja KP | 2a29b20 | 2015-10-07 11:31:58 +0100 | [diff] [blame] | 637 | err = skl_init_dsp(skl); |
| 638 | if (err < 0) { |
| 639 | dev_dbg(bus->dev, "error failed to register dsp\n"); |
Vinod Koul | cc18c5f | 2015-11-05 21:34:13 +0530 | [diff] [blame] | 640 | goto out_mach_free; |
Jeeja KP | 2a29b20 | 2015-10-07 11:31:58 +0100 | [diff] [blame] | 641 | } |
Jayachandran B | 0c8ba9d | 2015-12-18 15:12:03 +0530 | [diff] [blame] | 642 | skl->skl_sst->enable_miscbdcge = skl_enable_miscbdcge; |
| 643 | |
Jeeja KP | 0505700 | 2015-07-09 15:20:11 +0530 | [diff] [blame] | 644 | } |
Jeeja KP | 0505700 | 2015-07-09 15:20:11 +0530 | [diff] [blame] | 645 | if (ebus->mlcap) |
| 646 | snd_hdac_ext_bus_get_ml_capabilities(ebus); |
| 647 | |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 648 | /* create device for soc dmic */ |
| 649 | err = skl_dmic_device_register(skl); |
| 650 | if (err < 0) |
Jeeja KP | 2a29b20 | 2015-10-07 11:31:58 +0100 | [diff] [blame] | 651 | goto out_dsp_free; |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 652 | |
| 653 | /* register platform dai and controls */ |
| 654 | err = skl_platform_register(bus->dev); |
| 655 | if (err < 0) |
| 656 | goto out_dmic_free; |
| 657 | |
| 658 | /* create codec instances */ |
| 659 | err = skl_codec_create(ebus); |
| 660 | if (err < 0) |
| 661 | goto out_unregister; |
| 662 | |
Vinod Koul | 6980c05 | 2016-02-17 21:34:06 +0530 | [diff] [blame] | 663 | if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) { |
| 664 | err = snd_hdac_display_power(bus, false); |
| 665 | if (err < 0) { |
| 666 | dev_err(bus->dev, "Cannot turn off display power on i915\n"); |
| 667 | return err; |
| 668 | } |
| 669 | } |
| 670 | |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 671 | /*configure PM */ |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 672 | pm_runtime_put_noidle(bus->dev); |
| 673 | pm_runtime_allow(bus->dev); |
| 674 | |
| 675 | return 0; |
| 676 | |
| 677 | out_unregister: |
| 678 | skl_platform_unregister(bus->dev); |
| 679 | out_dmic_free: |
| 680 | skl_dmic_device_unregister(skl); |
Jeeja KP | 2a29b20 | 2015-10-07 11:31:58 +0100 | [diff] [blame] | 681 | out_dsp_free: |
| 682 | skl_free_dsp(skl); |
Vinod Koul | cc18c5f | 2015-11-05 21:34:13 +0530 | [diff] [blame] | 683 | out_mach_free: |
| 684 | skl_machine_device_unregister(skl); |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 685 | out_free: |
| 686 | skl->init_failed = 1; |
| 687 | skl_free(ebus); |
| 688 | |
| 689 | return err; |
| 690 | } |
| 691 | |
Jeeja KP | c5a76a2 | 2016-02-05 12:19:09 +0530 | [diff] [blame] | 692 | static void skl_shutdown(struct pci_dev *pci) |
| 693 | { |
| 694 | struct hdac_ext_bus *ebus = pci_get_drvdata(pci); |
| 695 | struct hdac_bus *bus = ebus_to_hbus(ebus); |
| 696 | struct hdac_stream *s; |
| 697 | struct hdac_ext_stream *stream; |
| 698 | struct skl *skl; |
| 699 | |
| 700 | if (ebus == NULL) |
| 701 | return; |
| 702 | |
| 703 | skl = ebus_to_skl(ebus); |
| 704 | |
| 705 | if (skl->init_failed) |
| 706 | return; |
| 707 | |
| 708 | snd_hdac_ext_stop_streams(ebus); |
| 709 | list_for_each_entry(s, &bus->stream_list, list) { |
| 710 | stream = stream_to_hdac_ext_stream(s); |
| 711 | snd_hdac_ext_stream_decouple(ebus, stream, false); |
| 712 | } |
| 713 | |
| 714 | snd_hdac_bus_stop_chip(bus); |
| 715 | } |
| 716 | |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 717 | static void skl_remove(struct pci_dev *pci) |
| 718 | { |
| 719 | struct hdac_ext_bus *ebus = pci_get_drvdata(pci); |
| 720 | struct skl *skl = ebus_to_skl(ebus); |
| 721 | |
Vinod Koul | d801836 | 2016-01-05 17:16:04 +0530 | [diff] [blame] | 722 | if (skl->tplg) |
| 723 | release_firmware(skl->tplg); |
| 724 | |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 725 | if (pci_dev_run_wake(pci)) |
| 726 | pm_runtime_get_noresume(&pci->dev); |
| 727 | pci_dev_put(pci); |
Vinod Koul | 7373f48 | 2016-03-15 16:39:24 +0530 | [diff] [blame] | 728 | |
| 729 | /* codec removal, invoke bus_device_remove */ |
| 730 | snd_hdac_ext_bus_device_remove(ebus); |
| 731 | |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 732 | skl_platform_unregister(&pci->dev); |
Jeeja KP | 2a29b20 | 2015-10-07 11:31:58 +0100 | [diff] [blame] | 733 | skl_free_dsp(skl); |
Vinod Koul | cc18c5f | 2015-11-05 21:34:13 +0530 | [diff] [blame] | 734 | skl_machine_device_unregister(skl); |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 735 | skl_dmic_device_unregister(skl); |
| 736 | skl_free(ebus); |
| 737 | dev_set_drvdata(&pci->dev, NULL); |
| 738 | } |
| 739 | |
Vinod Koul | cc18c5f | 2015-11-05 21:34:13 +0530 | [diff] [blame] | 740 | static struct sst_acpi_mach sst_skl_devdata[] = { |
| 741 | { "INT343A", "skl_alc286s_i2s", "intel/dsp_fw_release.bin", NULL, NULL, NULL }, |
Fang, Yang A | 02cc235 | 2015-11-05 22:53:08 +0530 | [diff] [blame] | 742 | { "INT343B", "skl_nau88l25_ssm4567_i2s", "intel/dsp_fw_release.bin", |
| 743 | NULL, NULL, NULL }, |
Rohit Ainapure | 69b7f9c | 2015-12-11 11:29:07 -0800 | [diff] [blame] | 744 | { "MX98357A", "skl_nau88l25_max98357a_i2s", "intel/dsp_fw_release.bin", |
| 745 | NULL, NULL, NULL }, |
Vinod Koul | cc18c5f | 2015-11-05 21:34:13 +0530 | [diff] [blame] | 746 | {} |
| 747 | }; |
| 748 | |
Senthilnathan Veppur | b379b1f | 2016-03-11 10:12:54 +0530 | [diff] [blame] | 749 | static struct sst_acpi_mach sst_bxtp_devdata[] = { |
| 750 | { "INT343A", "bxt_alc298s_i2s", "intel/dsp_fw_bxtn.bin", NULL, NULL, NULL }, |
| 751 | }; |
| 752 | |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 753 | /* PCI IDs */ |
| 754 | static const struct pci_device_id skl_ids[] = { |
| 755 | /* Sunrise Point-LP */ |
Vinod Koul | cc18c5f | 2015-11-05 21:34:13 +0530 | [diff] [blame] | 756 | { PCI_DEVICE(0x8086, 0x9d70), |
| 757 | .driver_data = (unsigned long)&sst_skl_devdata}, |
Senthilnathan Veppur | b379b1f | 2016-03-11 10:12:54 +0530 | [diff] [blame] | 758 | /* BXT-P */ |
| 759 | { PCI_DEVICE(0x8086, 0x5a98), |
| 760 | .driver_data = (unsigned long)&sst_bxtp_devdata}, |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 761 | { 0, } |
| 762 | }; |
| 763 | MODULE_DEVICE_TABLE(pci, skl_ids); |
| 764 | |
| 765 | /* pci_driver definition */ |
| 766 | static struct pci_driver skl_driver = { |
| 767 | .name = KBUILD_MODNAME, |
| 768 | .id_table = skl_ids, |
| 769 | .probe = skl_probe, |
| 770 | .remove = skl_remove, |
Jeeja KP | c5a76a2 | 2016-02-05 12:19:09 +0530 | [diff] [blame] | 771 | .shutdown = skl_shutdown, |
Jeeja KP | d8c2dab | 2015-07-09 15:20:09 +0530 | [diff] [blame] | 772 | .driver = { |
| 773 | .pm = &skl_pm, |
| 774 | }, |
| 775 | }; |
| 776 | module_pci_driver(skl_driver); |
| 777 | |
| 778 | MODULE_LICENSE("GPL v2"); |
| 779 | MODULE_DESCRIPTION("Intel Skylake ASoC HDA driver"); |