skl.c 21 KB

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