skl.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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 <sound/soc-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. ret = 0;
  301. } else {
  302. ret = _skl_resume(ebus);
  303. /* turn off the links which are off before suspend */
  304. list_for_each_entry(hlink, &ebus->hlink_list, list) {
  305. if (!hlink->ref_count)
  306. snd_hdac_ext_bus_link_power_down(hlink);
  307. }
  308. if (!ebus->cmd_dma_state)
  309. snd_hdac_bus_stop_cmd_io(&ebus->bus);
  310. }
  311. return ret;
  312. }
  313. #endif /* CONFIG_PM_SLEEP */
  314. #ifdef CONFIG_PM
  315. static int skl_runtime_suspend(struct device *dev)
  316. {
  317. struct pci_dev *pci = to_pci_dev(dev);
  318. struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
  319. struct hdac_bus *bus = ebus_to_hbus(ebus);
  320. dev_dbg(bus->dev, "in %s\n", __func__);
  321. return _skl_suspend(ebus);
  322. }
  323. static int skl_runtime_resume(struct device *dev)
  324. {
  325. struct pci_dev *pci = to_pci_dev(dev);
  326. struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
  327. struct hdac_bus *bus = ebus_to_hbus(ebus);
  328. dev_dbg(bus->dev, "in %s\n", __func__);
  329. return _skl_resume(ebus);
  330. }
  331. #endif /* CONFIG_PM */
  332. static const struct dev_pm_ops skl_pm = {
  333. SET_SYSTEM_SLEEP_PM_OPS(skl_suspend, skl_resume)
  334. SET_RUNTIME_PM_OPS(skl_runtime_suspend, skl_runtime_resume, NULL)
  335. .suspend_late = skl_suspend_late,
  336. };
  337. /*
  338. * destructor
  339. */
  340. static int skl_free(struct hdac_ext_bus *ebus)
  341. {
  342. struct skl *skl = ebus_to_skl(ebus);
  343. struct hdac_bus *bus = ebus_to_hbus(ebus);
  344. skl->init_done = 0; /* to be sure */
  345. snd_hdac_ext_stop_streams(ebus);
  346. if (bus->irq >= 0)
  347. free_irq(bus->irq, (void *)ebus);
  348. snd_hdac_bus_free_stream_pages(bus);
  349. snd_hdac_stream_free_all(ebus);
  350. snd_hdac_link_free_all(ebus);
  351. if (bus->remap_addr)
  352. iounmap(bus->remap_addr);
  353. pci_release_regions(skl->pci);
  354. pci_disable_device(skl->pci);
  355. snd_hdac_ext_bus_exit(ebus);
  356. cancel_work_sync(&skl->probe_work);
  357. if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI))
  358. snd_hdac_i915_exit(&ebus->bus);
  359. return 0;
  360. }
  361. /*
  362. * For each ssp there are 3 clocks (mclk/sclk/sclkfs).
  363. * e.g. for ssp0, clocks will be named as
  364. * "ssp0_mclk", "ssp0_sclk", "ssp0_sclkfs"
  365. * So for skl+, there are 6 ssps, so 18 clocks will be created.
  366. */
  367. static struct skl_ssp_clk skl_ssp_clks[] = {
  368. {.name = "ssp0_mclk"}, {.name = "ssp1_mclk"}, {.name = "ssp2_mclk"},
  369. {.name = "ssp3_mclk"}, {.name = "ssp4_mclk"}, {.name = "ssp5_mclk"},
  370. {.name = "ssp0_sclk"}, {.name = "ssp1_sclk"}, {.name = "ssp2_sclk"},
  371. {.name = "ssp3_sclk"}, {.name = "ssp4_sclk"}, {.name = "ssp5_sclk"},
  372. {.name = "ssp0_sclkfs"}, {.name = "ssp1_sclkfs"},
  373. {.name = "ssp2_sclkfs"},
  374. {.name = "ssp3_sclkfs"}, {.name = "ssp4_sclkfs"},
  375. {.name = "ssp5_sclkfs"},
  376. };
  377. static int skl_find_machine(struct skl *skl, void *driver_data)
  378. {
  379. struct snd_soc_acpi_mach *mach = driver_data;
  380. struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
  381. struct skl_machine_pdata *pdata;
  382. mach = snd_soc_acpi_find_machine(mach);
  383. if (mach == NULL) {
  384. dev_err(bus->dev, "No matching machine driver found\n");
  385. return -ENODEV;
  386. }
  387. skl->mach = mach;
  388. skl->fw_name = mach->fw_filename;
  389. pdata = skl->mach->pdata;
  390. if (mach->pdata)
  391. skl->use_tplg_pcm = pdata->use_tplg_pcm;
  392. return 0;
  393. }
  394. static int skl_machine_device_register(struct skl *skl)
  395. {
  396. struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
  397. struct snd_soc_acpi_mach *mach = skl->mach;
  398. struct platform_device *pdev;
  399. int ret;
  400. pdev = platform_device_alloc(mach->drv_name, -1);
  401. if (pdev == NULL) {
  402. dev_err(bus->dev, "platform device alloc failed\n");
  403. return -EIO;
  404. }
  405. ret = platform_device_add(pdev);
  406. if (ret) {
  407. dev_err(bus->dev, "failed to add machine device\n");
  408. platform_device_put(pdev);
  409. return -EIO;
  410. }
  411. if (mach->pdata)
  412. dev_set_drvdata(&pdev->dev, mach->pdata);
  413. skl->i2s_dev = pdev;
  414. return 0;
  415. }
  416. static void skl_machine_device_unregister(struct skl *skl)
  417. {
  418. if (skl->i2s_dev)
  419. platform_device_unregister(skl->i2s_dev);
  420. }
  421. static int skl_dmic_device_register(struct skl *skl)
  422. {
  423. struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
  424. struct platform_device *pdev;
  425. int ret;
  426. /* SKL has one dmic port, so allocate dmic device for this */
  427. pdev = platform_device_alloc("dmic-codec", -1);
  428. if (!pdev) {
  429. dev_err(bus->dev, "failed to allocate dmic device\n");
  430. return -ENOMEM;
  431. }
  432. ret = platform_device_add(pdev);
  433. if (ret) {
  434. dev_err(bus->dev, "failed to add dmic device: %d\n", ret);
  435. platform_device_put(pdev);
  436. return ret;
  437. }
  438. skl->dmic_dev = pdev;
  439. return 0;
  440. }
  441. static void skl_dmic_device_unregister(struct skl *skl)
  442. {
  443. if (skl->dmic_dev)
  444. platform_device_unregister(skl->dmic_dev);
  445. }
  446. static struct skl_clk_parent_src skl_clk_src[] = {
  447. { .clk_id = SKL_XTAL, .name = "xtal" },
  448. { .clk_id = SKL_CARDINAL, .name = "cardinal", .rate = 24576000 },
  449. { .clk_id = SKL_PLL, .name = "pll", .rate = 96000000 },
  450. };
  451. struct skl_clk_parent_src *skl_get_parent_clk(u8 clk_id)
  452. {
  453. unsigned int i;
  454. for (i = 0; i < ARRAY_SIZE(skl_clk_src); i++) {
  455. if (skl_clk_src[i].clk_id == clk_id)
  456. return &skl_clk_src[i];
  457. }
  458. return NULL;
  459. }
  460. static void init_skl_xtal_rate(int pci_id)
  461. {
  462. switch (pci_id) {
  463. case 0x9d70:
  464. case 0x9d71:
  465. skl_clk_src[0].rate = 24000000;
  466. return;
  467. default:
  468. skl_clk_src[0].rate = 19200000;
  469. return;
  470. }
  471. }
  472. static int skl_clock_device_register(struct skl *skl)
  473. {
  474. struct platform_device_info pdevinfo = {NULL};
  475. struct skl_clk_pdata *clk_pdata;
  476. clk_pdata = devm_kzalloc(&skl->pci->dev, sizeof(*clk_pdata),
  477. GFP_KERNEL);
  478. if (!clk_pdata)
  479. return -ENOMEM;
  480. init_skl_xtal_rate(skl->pci->device);
  481. clk_pdata->parent_clks = skl_clk_src;
  482. clk_pdata->ssp_clks = skl_ssp_clks;
  483. clk_pdata->num_clks = ARRAY_SIZE(skl_ssp_clks);
  484. /* Query NHLT to fill the rates and parent */
  485. skl_get_clks(skl, clk_pdata->ssp_clks);
  486. clk_pdata->pvt_data = skl;
  487. /* Register Platform device */
  488. pdevinfo.parent = &skl->pci->dev;
  489. pdevinfo.id = -1;
  490. pdevinfo.name = "skl-ssp-clk";
  491. pdevinfo.data = clk_pdata;
  492. pdevinfo.size_data = sizeof(*clk_pdata);
  493. skl->clk_dev = platform_device_register_full(&pdevinfo);
  494. return PTR_ERR_OR_ZERO(skl->clk_dev);
  495. }
  496. static void skl_clock_device_unregister(struct skl *skl)
  497. {
  498. if (skl->clk_dev)
  499. platform_device_unregister(skl->clk_dev);
  500. }
  501. /*
  502. * Probe the given codec address
  503. */
  504. static int probe_codec(struct hdac_ext_bus *ebus, int addr)
  505. {
  506. struct hdac_bus *bus = ebus_to_hbus(ebus);
  507. unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
  508. (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
  509. unsigned int res = -1;
  510. mutex_lock(&bus->cmd_mutex);
  511. snd_hdac_bus_send_cmd(bus, cmd);
  512. snd_hdac_bus_get_response(bus, addr, &res);
  513. mutex_unlock(&bus->cmd_mutex);
  514. if (res == -1)
  515. return -EIO;
  516. dev_dbg(bus->dev, "codec #%d probed OK\n", addr);
  517. return snd_hdac_ext_bus_device_init(ebus, addr);
  518. }
  519. /* Codec initialization */
  520. static void skl_codec_create(struct hdac_ext_bus *ebus)
  521. {
  522. struct hdac_bus *bus = ebus_to_hbus(ebus);
  523. int c, max_slots;
  524. max_slots = HDA_MAX_CODECS;
  525. /* First try to probe all given codec slots */
  526. for (c = 0; c < max_slots; c++) {
  527. if ((bus->codec_mask & (1 << c))) {
  528. if (probe_codec(ebus, c) < 0) {
  529. /*
  530. * Some BIOSen give you wrong codec addresses
  531. * that don't exist
  532. */
  533. dev_warn(bus->dev,
  534. "Codec #%d probe error; disabling it...\n", c);
  535. bus->codec_mask &= ~(1 << c);
  536. /*
  537. * More badly, accessing to a non-existing
  538. * codec often screws up the controller bus,
  539. * and disturbs the further communications.
  540. * Thus if an error occurs during probing,
  541. * better to reset the controller bus to get
  542. * back to the sanity state.
  543. */
  544. snd_hdac_bus_stop_chip(bus);
  545. skl_init_chip(bus, true);
  546. }
  547. }
  548. }
  549. }
  550. static const struct hdac_bus_ops bus_core_ops = {
  551. .command = snd_hdac_bus_send_cmd,
  552. .get_response = snd_hdac_bus_get_response,
  553. };
  554. static int skl_i915_init(struct hdac_bus *bus)
  555. {
  556. int err;
  557. /*
  558. * The HDMI codec is in GPU so we need to ensure that it is powered
  559. * up and ready for probe
  560. */
  561. err = snd_hdac_i915_init(bus);
  562. if (err < 0)
  563. return err;
  564. err = snd_hdac_display_power(bus, true);
  565. if (err < 0)
  566. dev_err(bus->dev, "Cannot turn on display power on i915\n");
  567. return err;
  568. }
  569. static void skl_probe_work(struct work_struct *work)
  570. {
  571. struct skl *skl = container_of(work, struct skl, probe_work);
  572. struct hdac_ext_bus *ebus = &skl->ebus;
  573. struct hdac_bus *bus = ebus_to_hbus(ebus);
  574. struct hdac_ext_link *hlink = NULL;
  575. int err;
  576. if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
  577. err = skl_i915_init(bus);
  578. if (err < 0)
  579. return;
  580. }
  581. err = skl_init_chip(bus, true);
  582. if (err < 0) {
  583. dev_err(bus->dev, "Init chip failed with err: %d\n", err);
  584. goto out_err;
  585. }
  586. /* codec detection */
  587. if (!bus->codec_mask)
  588. dev_info(bus->dev, "no hda codecs found!\n");
  589. /* create codec instances */
  590. skl_codec_create(ebus);
  591. /* register platform dai and controls */
  592. err = skl_platform_register(bus->dev);
  593. if (err < 0) {
  594. dev_err(bus->dev, "platform register failed: %d\n", err);
  595. return;
  596. }
  597. if (bus->ppcap) {
  598. err = skl_machine_device_register(skl);
  599. if (err < 0) {
  600. dev_err(bus->dev, "machine register failed: %d\n", err);
  601. goto out_err;
  602. }
  603. }
  604. if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
  605. err = snd_hdac_display_power(bus, false);
  606. if (err < 0) {
  607. dev_err(bus->dev, "Cannot turn off display power on i915\n");
  608. skl_machine_device_unregister(skl);
  609. return;
  610. }
  611. }
  612. /*
  613. * we are done probing so decrement link counts
  614. */
  615. list_for_each_entry(hlink, &ebus->hlink_list, list)
  616. snd_hdac_ext_bus_link_put(ebus, hlink);
  617. /* configure PM */
  618. pm_runtime_put_noidle(bus->dev);
  619. pm_runtime_allow(bus->dev);
  620. skl->init_done = 1;
  621. return;
  622. out_err:
  623. if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI))
  624. err = snd_hdac_display_power(bus, false);
  625. }
  626. /*
  627. * constructor
  628. */
  629. static int skl_create(struct pci_dev *pci,
  630. const struct hdac_io_ops *io_ops,
  631. struct skl **rskl)
  632. {
  633. struct skl *skl;
  634. struct hdac_ext_bus *ebus;
  635. int err;
  636. *rskl = NULL;
  637. err = pci_enable_device(pci);
  638. if (err < 0)
  639. return err;
  640. skl = devm_kzalloc(&pci->dev, sizeof(*skl), GFP_KERNEL);
  641. if (!skl) {
  642. pci_disable_device(pci);
  643. return -ENOMEM;
  644. }
  645. ebus = &skl->ebus;
  646. snd_hdac_ext_bus_init(ebus, &pci->dev, &bus_core_ops, io_ops);
  647. ebus->bus.use_posbuf = 1;
  648. skl->pci = pci;
  649. INIT_WORK(&skl->probe_work, skl_probe_work);
  650. ebus->bus.bdl_pos_adj = 0;
  651. *rskl = skl;
  652. return 0;
  653. }
  654. static int skl_first_init(struct hdac_ext_bus *ebus)
  655. {
  656. struct skl *skl = ebus_to_skl(ebus);
  657. struct hdac_bus *bus = ebus_to_hbus(ebus);
  658. struct pci_dev *pci = skl->pci;
  659. int err;
  660. unsigned short gcap;
  661. int cp_streams, pb_streams, start_idx;
  662. err = pci_request_regions(pci, "Skylake HD audio");
  663. if (err < 0)
  664. return err;
  665. bus->addr = pci_resource_start(pci, 0);
  666. bus->remap_addr = pci_ioremap_bar(pci, 0);
  667. if (bus->remap_addr == NULL) {
  668. dev_err(bus->dev, "ioremap error\n");
  669. return -ENXIO;
  670. }
  671. skl_init_chip(bus, true);
  672. snd_hdac_bus_parse_capabilities(bus);
  673. if (skl_acquire_irq(ebus, 0) < 0)
  674. return -EBUSY;
  675. pci_set_master(pci);
  676. synchronize_irq(bus->irq);
  677. gcap = snd_hdac_chip_readw(bus, GCAP);
  678. dev_dbg(bus->dev, "chipset global capabilities = 0x%x\n", gcap);
  679. /* allow 64bit DMA address if supported by H/W */
  680. if (!dma_set_mask(bus->dev, DMA_BIT_MASK(64))) {
  681. dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(64));
  682. } else {
  683. dma_set_mask(bus->dev, DMA_BIT_MASK(32));
  684. dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(32));
  685. }
  686. /* read number of streams from GCAP register */
  687. cp_streams = (gcap >> 8) & 0x0f;
  688. pb_streams = (gcap >> 12) & 0x0f;
  689. if (!pb_streams && !cp_streams)
  690. return -EIO;
  691. ebus->num_streams = cp_streams + pb_streams;
  692. /* initialize streams */
  693. snd_hdac_ext_stream_init_all
  694. (ebus, 0, cp_streams, SNDRV_PCM_STREAM_CAPTURE);
  695. start_idx = cp_streams;
  696. snd_hdac_ext_stream_init_all
  697. (ebus, start_idx, pb_streams, SNDRV_PCM_STREAM_PLAYBACK);
  698. err = snd_hdac_bus_alloc_stream_pages(bus);
  699. if (err < 0)
  700. return err;
  701. /* initialize chip */
  702. skl_init_pci(skl);
  703. return skl_init_chip(bus, true);
  704. }
  705. static int skl_probe(struct pci_dev *pci,
  706. const struct pci_device_id *pci_id)
  707. {
  708. struct skl *skl;
  709. struct hdac_ext_bus *ebus = NULL;
  710. struct hdac_bus *bus = NULL;
  711. int err;
  712. /* we use ext core ops, so provide NULL for ops here */
  713. err = skl_create(pci, NULL, &skl);
  714. if (err < 0)
  715. return err;
  716. ebus = &skl->ebus;
  717. bus = ebus_to_hbus(ebus);
  718. err = skl_first_init(ebus);
  719. if (err < 0)
  720. goto out_free;
  721. skl->pci_id = pci->device;
  722. device_disable_async_suspend(bus->dev);
  723. skl->nhlt = skl_nhlt_init(bus->dev);
  724. if (skl->nhlt == NULL) {
  725. err = -ENODEV;
  726. goto out_free;
  727. }
  728. err = skl_nhlt_create_sysfs(skl);
  729. if (err < 0)
  730. goto out_nhlt_free;
  731. skl_nhlt_update_topology_bin(skl);
  732. pci_set_drvdata(skl->pci, ebus);
  733. skl_dmic_data.dmic_num = skl_get_dmic_geo(skl);
  734. /* check if dsp is there */
  735. if (bus->ppcap) {
  736. /* create device for dsp clk */
  737. err = skl_clock_device_register(skl);
  738. if (err < 0)
  739. goto out_clk_free;
  740. err = skl_find_machine(skl, (void *)pci_id->driver_data);
  741. if (err < 0)
  742. goto out_nhlt_free;
  743. err = skl_init_dsp(skl);
  744. if (err < 0) {
  745. dev_dbg(bus->dev, "error failed to register dsp\n");
  746. goto out_nhlt_free;
  747. }
  748. skl->skl_sst->enable_miscbdcge = skl_enable_miscbdcge;
  749. }
  750. if (bus->mlcap)
  751. snd_hdac_ext_bus_get_ml_capabilities(ebus);
  752. snd_hdac_bus_stop_chip(bus);
  753. /* create device for soc dmic */
  754. err = skl_dmic_device_register(skl);
  755. if (err < 0)
  756. goto out_dsp_free;
  757. schedule_work(&skl->probe_work);
  758. return 0;
  759. out_dsp_free:
  760. skl_free_dsp(skl);
  761. out_clk_free:
  762. skl_clock_device_unregister(skl);
  763. out_nhlt_free:
  764. skl_nhlt_free(skl->nhlt);
  765. out_free:
  766. skl_free(ebus);
  767. return err;
  768. }
  769. static void skl_shutdown(struct pci_dev *pci)
  770. {
  771. struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
  772. struct hdac_bus *bus = ebus_to_hbus(ebus);
  773. struct hdac_stream *s;
  774. struct hdac_ext_stream *stream;
  775. struct skl *skl;
  776. if (ebus == NULL)
  777. return;
  778. skl = ebus_to_skl(ebus);
  779. if (!skl->init_done)
  780. return;
  781. snd_hdac_ext_stop_streams(ebus);
  782. list_for_each_entry(s, &bus->stream_list, list) {
  783. stream = stream_to_hdac_ext_stream(s);
  784. snd_hdac_ext_stream_decouple(ebus, stream, false);
  785. }
  786. snd_hdac_bus_stop_chip(bus);
  787. }
  788. static void skl_remove(struct pci_dev *pci)
  789. {
  790. struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
  791. struct skl *skl = ebus_to_skl(ebus);
  792. release_firmware(skl->tplg);
  793. pm_runtime_get_noresume(&pci->dev);
  794. /* codec removal, invoke bus_device_remove */
  795. snd_hdac_ext_bus_device_remove(ebus);
  796. skl->debugfs = NULL;
  797. skl_platform_unregister(&pci->dev);
  798. skl_free_dsp(skl);
  799. skl_machine_device_unregister(skl);
  800. skl_dmic_device_unregister(skl);
  801. skl_clock_device_unregister(skl);
  802. skl_nhlt_remove_sysfs(skl);
  803. skl_nhlt_free(skl->nhlt);
  804. skl_free(ebus);
  805. dev_set_drvdata(&pci->dev, NULL);
  806. }
  807. static struct snd_soc_acpi_codecs skl_codecs = {
  808. .num_codecs = 1,
  809. .codecs = {"10508825"}
  810. };
  811. static struct snd_soc_acpi_codecs kbl_codecs = {
  812. .num_codecs = 1,
  813. .codecs = {"10508825"}
  814. };
  815. static struct snd_soc_acpi_codecs bxt_codecs = {
  816. .num_codecs = 1,
  817. .codecs = {"MX98357A"}
  818. };
  819. static struct snd_soc_acpi_codecs kbl_poppy_codecs = {
  820. .num_codecs = 1,
  821. .codecs = {"10EC5663"}
  822. };
  823. static struct snd_soc_acpi_codecs kbl_5663_5514_codecs = {
  824. .num_codecs = 2,
  825. .codecs = {"10EC5663", "10EC5514"}
  826. };
  827. static struct skl_machine_pdata cnl_pdata = {
  828. .use_tplg_pcm = true,
  829. };
  830. static struct snd_soc_acpi_mach sst_skl_devdata[] = {
  831. {
  832. .id = "INT343A",
  833. .drv_name = "skl_alc286s_i2s",
  834. .fw_filename = "intel/dsp_fw_release.bin",
  835. },
  836. {
  837. .id = "INT343B",
  838. .drv_name = "skl_n88l25_s4567",
  839. .fw_filename = "intel/dsp_fw_release.bin",
  840. .machine_quirk = snd_soc_acpi_codec_list,
  841. .quirk_data = &skl_codecs,
  842. .pdata = &skl_dmic_data
  843. },
  844. {
  845. .id = "MX98357A",
  846. .drv_name = "skl_n88l25_m98357a",
  847. .fw_filename = "intel/dsp_fw_release.bin",
  848. .machine_quirk = snd_soc_acpi_codec_list,
  849. .quirk_data = &skl_codecs,
  850. .pdata = &skl_dmic_data
  851. },
  852. {}
  853. };
  854. static struct snd_soc_acpi_mach sst_bxtp_devdata[] = {
  855. {
  856. .id = "INT343A",
  857. .drv_name = "bxt_alc298s_i2s",
  858. .fw_filename = "intel/dsp_fw_bxtn.bin",
  859. },
  860. {
  861. .id = "DLGS7219",
  862. .drv_name = "bxt_da7219_max98357a_i2s",
  863. .fw_filename = "intel/dsp_fw_bxtn.bin",
  864. .machine_quirk = snd_soc_acpi_codec_list,
  865. .quirk_data = &bxt_codecs,
  866. },
  867. {}
  868. };
  869. static struct snd_soc_acpi_mach sst_kbl_devdata[] = {
  870. {
  871. .id = "INT343A",
  872. .drv_name = "kbl_alc286s_i2s",
  873. .fw_filename = "intel/dsp_fw_kbl.bin",
  874. },
  875. {
  876. .id = "INT343B",
  877. .drv_name = "kbl_n88l25_s4567",
  878. .fw_filename = "intel/dsp_fw_kbl.bin",
  879. .machine_quirk = snd_soc_acpi_codec_list,
  880. .quirk_data = &kbl_codecs,
  881. .pdata = &skl_dmic_data
  882. },
  883. {
  884. .id = "MX98357A",
  885. .drv_name = "kbl_n88l25_m98357a",
  886. .fw_filename = "intel/dsp_fw_kbl.bin",
  887. .machine_quirk = snd_soc_acpi_codec_list,
  888. .quirk_data = &kbl_codecs,
  889. .pdata = &skl_dmic_data
  890. },
  891. {
  892. .id = "MX98927",
  893. .drv_name = "kbl_r5514_5663_max",
  894. .fw_filename = "intel/dsp_fw_kbl.bin",
  895. .machine_quirk = snd_soc_acpi_codec_list,
  896. .quirk_data = &kbl_5663_5514_codecs,
  897. .pdata = &skl_dmic_data
  898. },
  899. {
  900. .id = "MX98927",
  901. .drv_name = "kbl_rt5663_m98927",
  902. .fw_filename = "intel/dsp_fw_kbl.bin",
  903. .machine_quirk = snd_soc_acpi_codec_list,
  904. .quirk_data = &kbl_poppy_codecs,
  905. .pdata = &skl_dmic_data
  906. },
  907. {
  908. .id = "10EC5663",
  909. .drv_name = "kbl_rt5663",
  910. .fw_filename = "intel/dsp_fw_kbl.bin",
  911. },
  912. {}
  913. };
  914. static struct snd_soc_acpi_mach sst_glk_devdata[] = {
  915. {
  916. .id = "INT343A",
  917. .drv_name = "glk_alc298s_i2s",
  918. .fw_filename = "intel/dsp_fw_glk.bin",
  919. },
  920. {}
  921. };
  922. static const struct snd_soc_acpi_mach sst_cnl_devdata[] = {
  923. {
  924. .id = "INT34C2",
  925. .drv_name = "cnl_rt274",
  926. .fw_filename = "intel/dsp_fw_cnl.bin",
  927. .pdata = &cnl_pdata,
  928. },
  929. {}
  930. };
  931. /* PCI IDs */
  932. static const struct pci_device_id skl_ids[] = {
  933. /* Sunrise Point-LP */
  934. { PCI_DEVICE(0x8086, 0x9d70),
  935. .driver_data = (unsigned long)&sst_skl_devdata},
  936. /* BXT-P */
  937. { PCI_DEVICE(0x8086, 0x5a98),
  938. .driver_data = (unsigned long)&sst_bxtp_devdata},
  939. /* KBL */
  940. { PCI_DEVICE(0x8086, 0x9D71),
  941. .driver_data = (unsigned long)&sst_kbl_devdata},
  942. /* GLK */
  943. { PCI_DEVICE(0x8086, 0x3198),
  944. .driver_data = (unsigned long)&sst_glk_devdata},
  945. /* CNL */
  946. { PCI_DEVICE(0x8086, 0x9dc8),
  947. .driver_data = (unsigned long)&sst_cnl_devdata},
  948. { 0, }
  949. };
  950. MODULE_DEVICE_TABLE(pci, skl_ids);
  951. /* pci_driver definition */
  952. static struct pci_driver skl_driver = {
  953. .name = KBUILD_MODNAME,
  954. .id_table = skl_ids,
  955. .probe = skl_probe,
  956. .remove = skl_remove,
  957. .shutdown = skl_shutdown,
  958. .driver = {
  959. .pm = &skl_pm,
  960. },
  961. };
  962. module_pci_driver(skl_driver);
  963. MODULE_LICENSE("GPL v2");
  964. MODULE_DESCRIPTION("Intel Skylake ASoC HDA driver");