skl.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * skl.c - Implementation of ASoC Intel SKL HD Audio driver
  4. *
  5. * Copyright (C) 2014-2015 Intel Corp
  6. * Author: Jeeja KP <[email protected]>
  7. *
  8. * Derived mostly from Intel HDA driver with following copyrights:
  9. * Copyright (c) 2004 Takashi Iwai <[email protected]>
  10. * PeiSen Hou <[email protected]>
  11. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  12. *
  13. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  14. */
  15. #include <linux/module.h>
  16. #include <linux/pci.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/firmware.h>
  20. #include <linux/delay.h>
  21. #include <sound/pcm.h>
  22. #include <sound/soc-acpi.h>
  23. #include <sound/soc-acpi-intel-match.h>
  24. #include <sound/hda_register.h>
  25. #include <sound/hdaudio.h>
  26. #include <sound/hda_i915.h>
  27. #include <sound/hda_codec.h>
  28. #include <sound/intel-nhlt.h>
  29. #include <sound/intel-dsp-config.h>
  30. #include "skl.h"
  31. #include "skl-sst-dsp.h"
  32. #include "skl-sst-ipc.h"
  33. #if IS_ENABLED(CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC)
  34. #include "../../../soc/codecs/hdac_hda.h"
  35. #endif
  36. static int skl_pci_binding;
  37. module_param_named(pci_binding, skl_pci_binding, int, 0444);
  38. MODULE_PARM_DESC(pci_binding, "PCI binding (0=auto, 1=only legacy, 2=only asoc");
  39. /*
  40. * initialize the PCI registers
  41. */
  42. static void skl_update_pci_byte(struct pci_dev *pci, unsigned int reg,
  43. unsigned char mask, unsigned char val)
  44. {
  45. unsigned char data;
  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. static void skl_init_pci(struct skl_dev *skl)
  52. {
  53. struct hdac_bus *bus = skl_to_bus(skl);
  54. /*
  55. * Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
  56. * TCSEL == Traffic Class Select Register, which sets PCI express QOS
  57. * Ensuring these bits are 0 clears playback static on some HD Audio
  58. * codecs.
  59. * The PCI register TCSEL is defined in the Intel manuals.
  60. */
  61. dev_dbg(bus->dev, "Clearing TCSEL\n");
  62. skl_update_pci_byte(skl->pci, AZX_PCIREG_TCSEL, 0x07, 0);
  63. }
  64. static void update_pci_dword(struct pci_dev *pci,
  65. unsigned int reg, u32 mask, u32 val)
  66. {
  67. u32 data = 0;
  68. pci_read_config_dword(pci, reg, &data);
  69. data &= ~mask;
  70. data |= (val & mask);
  71. pci_write_config_dword(pci, reg, data);
  72. }
  73. /*
  74. * skl_enable_miscbdcge - enable/dsiable CGCTL.MISCBDCGE bits
  75. *
  76. * @dev: device pointer
  77. * @enable: enable/disable flag
  78. */
  79. static void skl_enable_miscbdcge(struct device *dev, bool enable)
  80. {
  81. struct pci_dev *pci = to_pci_dev(dev);
  82. u32 val;
  83. val = enable ? AZX_CGCTL_MISCBDCGE_MASK : 0;
  84. update_pci_dword(pci, AZX_PCIREG_CGCTL, AZX_CGCTL_MISCBDCGE_MASK, val);
  85. }
  86. /**
  87. * skl_clock_power_gating: Enable/Disable clock and power gating
  88. *
  89. * @dev: Device pointer
  90. * @enable: Enable/Disable flag
  91. */
  92. static void skl_clock_power_gating(struct device *dev, bool enable)
  93. {
  94. struct pci_dev *pci = to_pci_dev(dev);
  95. struct hdac_bus *bus = pci_get_drvdata(pci);
  96. u32 val;
  97. /* Update PDCGE bit of CGCTL register */
  98. val = enable ? AZX_CGCTL_ADSPDCGE : 0;
  99. update_pci_dword(pci, AZX_PCIREG_CGCTL, AZX_CGCTL_ADSPDCGE, val);
  100. /* Update L1SEN bit of EM2 register */
  101. val = enable ? AZX_REG_VS_EM2_L1SEN : 0;
  102. snd_hdac_chip_updatel(bus, VS_EM2, AZX_REG_VS_EM2_L1SEN, val);
  103. /* Update ADSPPGD bit of PGCTL register */
  104. val = enable ? 0 : AZX_PGCTL_ADSPPGD;
  105. update_pci_dword(pci, AZX_PCIREG_PGCTL, AZX_PGCTL_ADSPPGD, val);
  106. }
  107. /*
  108. * While performing reset, controller may not come back properly causing
  109. * issues, so recommendation is to set CGCTL.MISCBDCGE to 0 then do reset
  110. * (init chip) and then again set CGCTL.MISCBDCGE to 1
  111. */
  112. static int skl_init_chip(struct hdac_bus *bus, bool full_reset)
  113. {
  114. struct hdac_ext_link *hlink;
  115. int ret;
  116. snd_hdac_set_codec_wakeup(bus, true);
  117. skl_enable_miscbdcge(bus->dev, false);
  118. ret = snd_hdac_bus_init_chip(bus, full_reset);
  119. /* Reset stream-to-link mapping */
  120. list_for_each_entry(hlink, &bus->hlink_list, list)
  121. writel(0, hlink->ml_addr + AZX_REG_ML_LOSIDV);
  122. skl_enable_miscbdcge(bus->dev, true);
  123. snd_hdac_set_codec_wakeup(bus, false);
  124. return ret;
  125. }
  126. void skl_update_d0i3c(struct device *dev, bool enable)
  127. {
  128. struct pci_dev *pci = to_pci_dev(dev);
  129. struct hdac_bus *bus = pci_get_drvdata(pci);
  130. u8 reg;
  131. int timeout = 50;
  132. reg = snd_hdac_chip_readb(bus, VS_D0I3C);
  133. /* Do not write to D0I3C until command in progress bit is cleared */
  134. while ((reg & AZX_REG_VS_D0I3C_CIP) && --timeout) {
  135. udelay(10);
  136. reg = snd_hdac_chip_readb(bus, VS_D0I3C);
  137. }
  138. /* Highly unlikely. But if it happens, flag error explicitly */
  139. if (!timeout) {
  140. dev_err(bus->dev, "Before D0I3C update: D0I3C CIP timeout\n");
  141. return;
  142. }
  143. if (enable)
  144. reg = reg | AZX_REG_VS_D0I3C_I3;
  145. else
  146. reg = reg & (~AZX_REG_VS_D0I3C_I3);
  147. snd_hdac_chip_writeb(bus, VS_D0I3C, reg);
  148. timeout = 50;
  149. /* Wait for cmd in progress to be cleared before exiting the function */
  150. reg = snd_hdac_chip_readb(bus, VS_D0I3C);
  151. while ((reg & AZX_REG_VS_D0I3C_CIP) && --timeout) {
  152. udelay(10);
  153. reg = snd_hdac_chip_readb(bus, VS_D0I3C);
  154. }
  155. /* Highly unlikely. But if it happens, flag error explicitly */
  156. if (!timeout) {
  157. dev_err(bus->dev, "After D0I3C update: D0I3C CIP timeout\n");
  158. return;
  159. }
  160. dev_dbg(bus->dev, "D0I3C register = 0x%x\n",
  161. snd_hdac_chip_readb(bus, VS_D0I3C));
  162. }
  163. /**
  164. * skl_dum_set - set DUM bit in EM2 register
  165. * @bus: HD-audio core bus
  166. *
  167. * Addresses incorrect position reporting for capture streams.
  168. * Used on device power up.
  169. */
  170. static void skl_dum_set(struct hdac_bus *bus)
  171. {
  172. /* For the DUM bit to be set, CRST needs to be out of reset state */
  173. if (!(snd_hdac_chip_readb(bus, GCTL) & AZX_GCTL_RESET)) {
  174. skl_enable_miscbdcge(bus->dev, false);
  175. snd_hdac_bus_exit_link_reset(bus);
  176. skl_enable_miscbdcge(bus->dev, true);
  177. }
  178. snd_hdac_chip_updatel(bus, VS_EM2, AZX_VS_EM2_DUM, AZX_VS_EM2_DUM);
  179. }
  180. /* called from IRQ */
  181. static void skl_stream_update(struct hdac_bus *bus, struct hdac_stream *hstr)
  182. {
  183. snd_pcm_period_elapsed(hstr->substream);
  184. }
  185. static irqreturn_t skl_interrupt(int irq, void *dev_id)
  186. {
  187. struct hdac_bus *bus = dev_id;
  188. u32 status;
  189. if (!pm_runtime_active(bus->dev))
  190. return IRQ_NONE;
  191. spin_lock(&bus->reg_lock);
  192. status = snd_hdac_chip_readl(bus, INTSTS);
  193. if (status == 0 || status == 0xffffffff) {
  194. spin_unlock(&bus->reg_lock);
  195. return IRQ_NONE;
  196. }
  197. /* clear rirb int */
  198. status = snd_hdac_chip_readb(bus, RIRBSTS);
  199. if (status & RIRB_INT_MASK) {
  200. if (status & RIRB_INT_RESPONSE)
  201. snd_hdac_bus_update_rirb(bus);
  202. snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
  203. }
  204. spin_unlock(&bus->reg_lock);
  205. return snd_hdac_chip_readl(bus, INTSTS) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
  206. }
  207. static irqreturn_t skl_threaded_handler(int irq, void *dev_id)
  208. {
  209. struct hdac_bus *bus = dev_id;
  210. u32 status;
  211. status = snd_hdac_chip_readl(bus, INTSTS);
  212. snd_hdac_bus_handle_stream_irq(bus, status, skl_stream_update);
  213. return IRQ_HANDLED;
  214. }
  215. static int skl_acquire_irq(struct hdac_bus *bus, int do_disconnect)
  216. {
  217. struct skl_dev *skl = bus_to_skl(bus);
  218. int ret;
  219. ret = request_threaded_irq(skl->pci->irq, skl_interrupt,
  220. skl_threaded_handler,
  221. IRQF_SHARED,
  222. KBUILD_MODNAME, bus);
  223. if (ret) {
  224. dev_err(bus->dev,
  225. "unable to grab IRQ %d, disabling device\n",
  226. skl->pci->irq);
  227. return ret;
  228. }
  229. bus->irq = skl->pci->irq;
  230. pci_intx(skl->pci, 1);
  231. return 0;
  232. }
  233. static int skl_suspend_late(struct device *dev)
  234. {
  235. struct pci_dev *pci = to_pci_dev(dev);
  236. struct hdac_bus *bus = pci_get_drvdata(pci);
  237. struct skl_dev *skl = bus_to_skl(bus);
  238. return skl_suspend_late_dsp(skl);
  239. }
  240. #ifdef CONFIG_PM
  241. static int _skl_suspend(struct hdac_bus *bus)
  242. {
  243. struct skl_dev *skl = bus_to_skl(bus);
  244. struct pci_dev *pci = to_pci_dev(bus->dev);
  245. int ret;
  246. snd_hdac_ext_bus_link_power_down_all(bus);
  247. ret = skl_suspend_dsp(skl);
  248. if (ret < 0)
  249. return ret;
  250. snd_hdac_bus_stop_chip(bus);
  251. update_pci_dword(pci, AZX_PCIREG_PGCTL,
  252. AZX_PGCTL_LSRMD_MASK, AZX_PGCTL_LSRMD_MASK);
  253. skl_enable_miscbdcge(bus->dev, false);
  254. snd_hdac_bus_enter_link_reset(bus);
  255. skl_enable_miscbdcge(bus->dev, true);
  256. skl_cleanup_resources(skl);
  257. return 0;
  258. }
  259. static int _skl_resume(struct hdac_bus *bus)
  260. {
  261. struct skl_dev *skl = bus_to_skl(bus);
  262. skl_init_pci(skl);
  263. skl_dum_set(bus);
  264. skl_init_chip(bus, true);
  265. return skl_resume_dsp(skl);
  266. }
  267. #endif
  268. #ifdef CONFIG_PM_SLEEP
  269. /*
  270. * power management
  271. */
  272. static int skl_suspend(struct device *dev)
  273. {
  274. struct pci_dev *pci = to_pci_dev(dev);
  275. struct hdac_bus *bus = pci_get_drvdata(pci);
  276. struct skl_dev *skl = bus_to_skl(bus);
  277. int ret;
  278. /*
  279. * Do not suspend if streams which are marked ignore suspend are
  280. * running, we need to save the state for these and continue
  281. */
  282. if (skl->supend_active) {
  283. /* turn off the links and stop the CORB/RIRB DMA if it is On */
  284. snd_hdac_ext_bus_link_power_down_all(bus);
  285. if (bus->cmd_dma_state)
  286. snd_hdac_bus_stop_cmd_io(bus);
  287. enable_irq_wake(bus->irq);
  288. pci_save_state(pci);
  289. } else {
  290. ret = _skl_suspend(bus);
  291. if (ret < 0)
  292. return ret;
  293. skl->fw_loaded = false;
  294. }
  295. return 0;
  296. }
  297. static int skl_resume(struct device *dev)
  298. {
  299. struct pci_dev *pci = to_pci_dev(dev);
  300. struct hdac_bus *bus = pci_get_drvdata(pci);
  301. struct skl_dev *skl = bus_to_skl(bus);
  302. struct hdac_ext_link *hlink;
  303. int ret;
  304. /*
  305. * resume only when we are not in suspend active, otherwise need to
  306. * restore the device
  307. */
  308. if (skl->supend_active) {
  309. pci_restore_state(pci);
  310. snd_hdac_ext_bus_link_power_up_all(bus);
  311. disable_irq_wake(bus->irq);
  312. /*
  313. * turn On the links which are On before active suspend
  314. * and start the CORB/RIRB DMA if On before
  315. * active suspend.
  316. */
  317. list_for_each_entry(hlink, &bus->hlink_list, list) {
  318. if (hlink->ref_count)
  319. snd_hdac_ext_bus_link_power_up(hlink);
  320. }
  321. ret = 0;
  322. if (bus->cmd_dma_state)
  323. snd_hdac_bus_init_cmd_io(bus);
  324. } else {
  325. ret = _skl_resume(bus);
  326. /* turn off the links which are off before suspend */
  327. list_for_each_entry(hlink, &bus->hlink_list, list) {
  328. if (!hlink->ref_count)
  329. snd_hdac_ext_bus_link_power_down(hlink);
  330. }
  331. if (!bus->cmd_dma_state)
  332. snd_hdac_bus_stop_cmd_io(bus);
  333. }
  334. return ret;
  335. }
  336. #endif /* CONFIG_PM_SLEEP */
  337. #ifdef CONFIG_PM
  338. static int skl_runtime_suspend(struct device *dev)
  339. {
  340. struct pci_dev *pci = to_pci_dev(dev);
  341. struct hdac_bus *bus = pci_get_drvdata(pci);
  342. dev_dbg(bus->dev, "in %s\n", __func__);
  343. return _skl_suspend(bus);
  344. }
  345. static int skl_runtime_resume(struct device *dev)
  346. {
  347. struct pci_dev *pci = to_pci_dev(dev);
  348. struct hdac_bus *bus = pci_get_drvdata(pci);
  349. dev_dbg(bus->dev, "in %s\n", __func__);
  350. return _skl_resume(bus);
  351. }
  352. #endif /* CONFIG_PM */
  353. static const struct dev_pm_ops skl_pm = {
  354. SET_SYSTEM_SLEEP_PM_OPS(skl_suspend, skl_resume)
  355. SET_RUNTIME_PM_OPS(skl_runtime_suspend, skl_runtime_resume, NULL)
  356. .suspend_late = skl_suspend_late,
  357. };
  358. /*
  359. * destructor
  360. */
  361. static int skl_free(struct hdac_bus *bus)
  362. {
  363. struct skl_dev *skl = bus_to_skl(bus);
  364. skl->init_done = 0; /* to be sure */
  365. snd_hdac_stop_streams_and_chip(bus);
  366. if (bus->irq >= 0)
  367. free_irq(bus->irq, (void *)bus);
  368. snd_hdac_bus_free_stream_pages(bus);
  369. snd_hdac_ext_stream_free_all(bus);
  370. snd_hdac_link_free_all(bus);
  371. if (bus->remap_addr)
  372. iounmap(bus->remap_addr);
  373. pci_release_regions(skl->pci);
  374. pci_disable_device(skl->pci);
  375. snd_hdac_ext_bus_exit(bus);
  376. if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
  377. snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
  378. snd_hdac_i915_exit(bus);
  379. }
  380. return 0;
  381. }
  382. /*
  383. * For each ssp there are 3 clocks (mclk/sclk/sclkfs).
  384. * e.g. for ssp0, clocks will be named as
  385. * "ssp0_mclk", "ssp0_sclk", "ssp0_sclkfs"
  386. * So for skl+, there are 6 ssps, so 18 clocks will be created.
  387. */
  388. static struct skl_ssp_clk skl_ssp_clks[] = {
  389. {.name = "ssp0_mclk"}, {.name = "ssp1_mclk"}, {.name = "ssp2_mclk"},
  390. {.name = "ssp3_mclk"}, {.name = "ssp4_mclk"}, {.name = "ssp5_mclk"},
  391. {.name = "ssp0_sclk"}, {.name = "ssp1_sclk"}, {.name = "ssp2_sclk"},
  392. {.name = "ssp3_sclk"}, {.name = "ssp4_sclk"}, {.name = "ssp5_sclk"},
  393. {.name = "ssp0_sclkfs"}, {.name = "ssp1_sclkfs"},
  394. {.name = "ssp2_sclkfs"},
  395. {.name = "ssp3_sclkfs"}, {.name = "ssp4_sclkfs"},
  396. {.name = "ssp5_sclkfs"},
  397. };
  398. static struct snd_soc_acpi_mach *skl_find_hda_machine(struct skl_dev *skl,
  399. struct snd_soc_acpi_mach *machines)
  400. {
  401. struct snd_soc_acpi_mach *mach;
  402. /* point to common table */
  403. mach = snd_soc_acpi_intel_hda_machines;
  404. /* all entries in the machine table use the same firmware */
  405. mach->fw_filename = machines->fw_filename;
  406. return mach;
  407. }
  408. static int skl_find_machine(struct skl_dev *skl, void *driver_data)
  409. {
  410. struct hdac_bus *bus = skl_to_bus(skl);
  411. struct snd_soc_acpi_mach *mach = driver_data;
  412. struct skl_machine_pdata *pdata;
  413. mach = snd_soc_acpi_find_machine(mach);
  414. if (!mach) {
  415. dev_dbg(bus->dev, "No matching I2S machine driver found\n");
  416. mach = skl_find_hda_machine(skl, driver_data);
  417. if (!mach) {
  418. dev_err(bus->dev, "No matching machine driver found\n");
  419. return -ENODEV;
  420. }
  421. }
  422. skl->mach = mach;
  423. skl->fw_name = mach->fw_filename;
  424. pdata = mach->pdata;
  425. if (pdata) {
  426. skl->use_tplg_pcm = pdata->use_tplg_pcm;
  427. mach->mach_params.dmic_num =
  428. intel_nhlt_get_dmic_geo(&skl->pci->dev,
  429. skl->nhlt);
  430. }
  431. return 0;
  432. }
  433. static int skl_machine_device_register(struct skl_dev *skl)
  434. {
  435. struct snd_soc_acpi_mach *mach = skl->mach;
  436. struct hdac_bus *bus = skl_to_bus(skl);
  437. struct platform_device *pdev;
  438. int ret;
  439. pdev = platform_device_alloc(mach->drv_name, -1);
  440. if (pdev == NULL) {
  441. dev_err(bus->dev, "platform device alloc failed\n");
  442. return -EIO;
  443. }
  444. mach->mach_params.platform = dev_name(bus->dev);
  445. mach->mach_params.codec_mask = bus->codec_mask;
  446. ret = platform_device_add_data(pdev, (const void *)mach, sizeof(*mach));
  447. if (ret) {
  448. dev_err(bus->dev, "failed to add machine device platform data\n");
  449. platform_device_put(pdev);
  450. return ret;
  451. }
  452. ret = platform_device_add(pdev);
  453. if (ret) {
  454. dev_err(bus->dev, "failed to add machine device\n");
  455. platform_device_put(pdev);
  456. return -EIO;
  457. }
  458. skl->i2s_dev = pdev;
  459. return 0;
  460. }
  461. static void skl_machine_device_unregister(struct skl_dev *skl)
  462. {
  463. if (skl->i2s_dev)
  464. platform_device_unregister(skl->i2s_dev);
  465. }
  466. static int skl_dmic_device_register(struct skl_dev *skl)
  467. {
  468. struct hdac_bus *bus = skl_to_bus(skl);
  469. struct platform_device *pdev;
  470. int ret;
  471. /* SKL has one dmic port, so allocate dmic device for this */
  472. pdev = platform_device_alloc("dmic-codec", -1);
  473. if (!pdev) {
  474. dev_err(bus->dev, "failed to allocate dmic device\n");
  475. return -ENOMEM;
  476. }
  477. ret = platform_device_add(pdev);
  478. if (ret) {
  479. dev_err(bus->dev, "failed to add dmic device: %d\n", ret);
  480. platform_device_put(pdev);
  481. return ret;
  482. }
  483. skl->dmic_dev = pdev;
  484. return 0;
  485. }
  486. static void skl_dmic_device_unregister(struct skl_dev *skl)
  487. {
  488. if (skl->dmic_dev)
  489. platform_device_unregister(skl->dmic_dev);
  490. }
  491. static struct skl_clk_parent_src skl_clk_src[] = {
  492. { .clk_id = SKL_XTAL, .name = "xtal" },
  493. { .clk_id = SKL_CARDINAL, .name = "cardinal", .rate = 24576000 },
  494. { .clk_id = SKL_PLL, .name = "pll", .rate = 96000000 },
  495. };
  496. struct skl_clk_parent_src *skl_get_parent_clk(u8 clk_id)
  497. {
  498. unsigned int i;
  499. for (i = 0; i < ARRAY_SIZE(skl_clk_src); i++) {
  500. if (skl_clk_src[i].clk_id == clk_id)
  501. return &skl_clk_src[i];
  502. }
  503. return NULL;
  504. }
  505. static void init_skl_xtal_rate(int pci_id)
  506. {
  507. switch (pci_id) {
  508. case 0x9d70:
  509. case 0x9d71:
  510. skl_clk_src[0].rate = 24000000;
  511. return;
  512. default:
  513. skl_clk_src[0].rate = 19200000;
  514. return;
  515. }
  516. }
  517. static int skl_clock_device_register(struct skl_dev *skl)
  518. {
  519. struct platform_device_info pdevinfo = {NULL};
  520. struct skl_clk_pdata *clk_pdata;
  521. if (!skl->nhlt)
  522. return 0;
  523. clk_pdata = devm_kzalloc(&skl->pci->dev, sizeof(*clk_pdata),
  524. GFP_KERNEL);
  525. if (!clk_pdata)
  526. return -ENOMEM;
  527. init_skl_xtal_rate(skl->pci->device);
  528. clk_pdata->parent_clks = skl_clk_src;
  529. clk_pdata->ssp_clks = skl_ssp_clks;
  530. clk_pdata->num_clks = ARRAY_SIZE(skl_ssp_clks);
  531. /* Query NHLT to fill the rates and parent */
  532. skl_get_clks(skl, clk_pdata->ssp_clks);
  533. clk_pdata->pvt_data = skl;
  534. /* Register Platform device */
  535. pdevinfo.parent = &skl->pci->dev;
  536. pdevinfo.id = -1;
  537. pdevinfo.name = "skl-ssp-clk";
  538. pdevinfo.data = clk_pdata;
  539. pdevinfo.size_data = sizeof(*clk_pdata);
  540. skl->clk_dev = platform_device_register_full(&pdevinfo);
  541. return PTR_ERR_OR_ZERO(skl->clk_dev);
  542. }
  543. static void skl_clock_device_unregister(struct skl_dev *skl)
  544. {
  545. if (skl->clk_dev)
  546. platform_device_unregister(skl->clk_dev);
  547. }
  548. #if IS_ENABLED(CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC)
  549. #define IDISP_INTEL_VENDOR_ID 0x80860000
  550. /*
  551. * load the legacy codec driver
  552. */
  553. static void load_codec_module(struct hda_codec *codec)
  554. {
  555. #ifdef MODULE
  556. char modalias[MODULE_NAME_LEN];
  557. const char *mod = NULL;
  558. snd_hdac_codec_modalias(&codec->core, modalias, sizeof(modalias));
  559. mod = modalias;
  560. dev_dbg(&codec->core.dev, "loading %s codec module\n", mod);
  561. request_module(mod);
  562. #endif
  563. }
  564. #endif /* CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC */
  565. static struct hda_codec *skl_codec_device_init(struct hdac_bus *bus, int addr)
  566. {
  567. struct hda_codec *codec;
  568. int ret;
  569. codec = snd_hda_codec_device_init(to_hda_bus(bus), addr, "ehdaudio%dD%d", bus->idx, addr);
  570. if (IS_ERR(codec)) {
  571. dev_err(bus->dev, "device init failed for hdac device\n");
  572. return codec;
  573. }
  574. codec->core.type = HDA_DEV_ASOC;
  575. ret = snd_hdac_device_register(&codec->core);
  576. if (ret) {
  577. dev_err(bus->dev, "failed to register hdac device\n");
  578. put_device(&codec->core.dev);
  579. return ERR_PTR(ret);
  580. }
  581. return codec;
  582. }
  583. /*
  584. * Probe the given codec address
  585. */
  586. static int probe_codec(struct hdac_bus *bus, int addr)
  587. {
  588. unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
  589. (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
  590. unsigned int res = -1;
  591. #if IS_ENABLED(CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC)
  592. struct skl_dev *skl = bus_to_skl(bus);
  593. struct hdac_hda_priv *hda_codec;
  594. #endif
  595. struct hda_codec *codec;
  596. mutex_lock(&bus->cmd_mutex);
  597. snd_hdac_bus_send_cmd(bus, cmd);
  598. snd_hdac_bus_get_response(bus, addr, &res);
  599. mutex_unlock(&bus->cmd_mutex);
  600. if (res == -1)
  601. return -EIO;
  602. dev_dbg(bus->dev, "codec #%d probed OK: %x\n", addr, res);
  603. #if IS_ENABLED(CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC)
  604. hda_codec = devm_kzalloc(&skl->pci->dev, sizeof(*hda_codec),
  605. GFP_KERNEL);
  606. if (!hda_codec)
  607. return -ENOMEM;
  608. codec = skl_codec_device_init(bus, addr);
  609. if (IS_ERR(codec))
  610. return PTR_ERR(codec);
  611. hda_codec->codec = codec;
  612. dev_set_drvdata(&codec->core.dev, hda_codec);
  613. /* use legacy bus only for HDA codecs, idisp uses ext bus */
  614. if ((res & 0xFFFF0000) != IDISP_INTEL_VENDOR_ID) {
  615. codec->core.type = HDA_DEV_LEGACY;
  616. load_codec_module(hda_codec->codec);
  617. }
  618. return 0;
  619. #else
  620. codec = skl_codec_device_init(bus, addr);
  621. return PTR_ERR_OR_ZERO(codec);
  622. #endif /* CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC */
  623. }
  624. /* Codec initialization */
  625. static void skl_codec_create(struct hdac_bus *bus)
  626. {
  627. int c, max_slots;
  628. max_slots = HDA_MAX_CODECS;
  629. /* First try to probe all given codec slots */
  630. for (c = 0; c < max_slots; c++) {
  631. if ((bus->codec_mask & (1 << c))) {
  632. if (probe_codec(bus, c) < 0) {
  633. /*
  634. * Some BIOSen give you wrong codec addresses
  635. * that don't exist
  636. */
  637. dev_warn(bus->dev,
  638. "Codec #%d probe error; disabling it...\n", c);
  639. bus->codec_mask &= ~(1 << c);
  640. /*
  641. * More badly, accessing to a non-existing
  642. * codec often screws up the controller bus,
  643. * and disturbs the further communications.
  644. * Thus if an error occurs during probing,
  645. * better to reset the controller bus to get
  646. * back to the sanity state.
  647. */
  648. snd_hdac_bus_stop_chip(bus);
  649. skl_init_chip(bus, true);
  650. }
  651. }
  652. }
  653. }
  654. static int skl_i915_init(struct hdac_bus *bus)
  655. {
  656. int err;
  657. /*
  658. * The HDMI codec is in GPU so we need to ensure that it is powered
  659. * up and ready for probe
  660. */
  661. err = snd_hdac_i915_init(bus);
  662. if (err < 0)
  663. return err;
  664. snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, true);
  665. return 0;
  666. }
  667. static void skl_probe_work(struct work_struct *work)
  668. {
  669. struct skl_dev *skl = container_of(work, struct skl_dev, probe_work);
  670. struct hdac_bus *bus = skl_to_bus(skl);
  671. struct hdac_ext_link *hlink;
  672. int err;
  673. if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
  674. err = skl_i915_init(bus);
  675. if (err < 0)
  676. return;
  677. }
  678. skl_init_pci(skl);
  679. skl_dum_set(bus);
  680. err = skl_init_chip(bus, true);
  681. if (err < 0) {
  682. dev_err(bus->dev, "Init chip failed with err: %d\n", err);
  683. goto out_err;
  684. }
  685. /* codec detection */
  686. if (!bus->codec_mask)
  687. dev_info(bus->dev, "no hda codecs found!\n");
  688. /* create codec instances */
  689. skl_codec_create(bus);
  690. /* register platform dai and controls */
  691. err = skl_platform_register(bus->dev);
  692. if (err < 0) {
  693. dev_err(bus->dev, "platform register failed: %d\n", err);
  694. goto out_err;
  695. }
  696. err = skl_machine_device_register(skl);
  697. if (err < 0) {
  698. dev_err(bus->dev, "machine register failed: %d\n", err);
  699. goto out_err;
  700. }
  701. /*
  702. * we are done probing so decrement link counts
  703. */
  704. list_for_each_entry(hlink, &bus->hlink_list, list)
  705. snd_hdac_ext_bus_link_put(bus, hlink);
  706. if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI))
  707. snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
  708. /* configure PM */
  709. pm_runtime_put_noidle(bus->dev);
  710. pm_runtime_allow(bus->dev);
  711. skl->init_done = 1;
  712. return;
  713. out_err:
  714. if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI))
  715. snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
  716. }
  717. /*
  718. * constructor
  719. */
  720. static int skl_create(struct pci_dev *pci,
  721. struct skl_dev **rskl)
  722. {
  723. struct hdac_ext_bus_ops *ext_ops = NULL;
  724. struct skl_dev *skl;
  725. struct hdac_bus *bus;
  726. struct hda_bus *hbus;
  727. int err;
  728. *rskl = NULL;
  729. err = pci_enable_device(pci);
  730. if (err < 0)
  731. return err;
  732. skl = devm_kzalloc(&pci->dev, sizeof(*skl), GFP_KERNEL);
  733. if (!skl) {
  734. pci_disable_device(pci);
  735. return -ENOMEM;
  736. }
  737. hbus = skl_to_hbus(skl);
  738. bus = skl_to_bus(skl);
  739. INIT_LIST_HEAD(&skl->ppl_list);
  740. INIT_LIST_HEAD(&skl->bind_list);
  741. #if IS_ENABLED(CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC)
  742. ext_ops = snd_soc_hdac_hda_get_ops();
  743. #endif
  744. snd_hdac_ext_bus_init(bus, &pci->dev, NULL, ext_ops);
  745. bus->use_posbuf = 1;
  746. skl->pci = pci;
  747. INIT_WORK(&skl->probe_work, skl_probe_work);
  748. bus->bdl_pos_adj = 0;
  749. mutex_init(&hbus->prepare_mutex);
  750. hbus->pci = pci;
  751. hbus->mixer_assigned = -1;
  752. hbus->modelname = "sklbus";
  753. *rskl = skl;
  754. return 0;
  755. }
  756. static int skl_first_init(struct hdac_bus *bus)
  757. {
  758. struct skl_dev *skl = bus_to_skl(bus);
  759. struct pci_dev *pci = skl->pci;
  760. int err;
  761. unsigned short gcap;
  762. int cp_streams, pb_streams, start_idx;
  763. err = pci_request_regions(pci, "Skylake HD audio");
  764. if (err < 0)
  765. return err;
  766. bus->addr = pci_resource_start(pci, 0);
  767. bus->remap_addr = pci_ioremap_bar(pci, 0);
  768. if (bus->remap_addr == NULL) {
  769. dev_err(bus->dev, "ioremap error\n");
  770. return -ENXIO;
  771. }
  772. snd_hdac_bus_parse_capabilities(bus);
  773. /* check if PPCAP exists */
  774. if (!bus->ppcap) {
  775. dev_err(bus->dev, "bus ppcap not set, HDAudio or DSP not present?\n");
  776. return -ENODEV;
  777. }
  778. if (skl_acquire_irq(bus, 0) < 0)
  779. return -EBUSY;
  780. pci_set_master(pci);
  781. synchronize_irq(bus->irq);
  782. gcap = snd_hdac_chip_readw(bus, GCAP);
  783. dev_dbg(bus->dev, "chipset global capabilities = 0x%x\n", gcap);
  784. /* read number of streams from GCAP register */
  785. cp_streams = (gcap >> 8) & 0x0f;
  786. pb_streams = (gcap >> 12) & 0x0f;
  787. if (!pb_streams && !cp_streams) {
  788. dev_err(bus->dev, "no streams found in GCAP definitions?\n");
  789. return -EIO;
  790. }
  791. bus->num_streams = cp_streams + pb_streams;
  792. /* allow 64bit DMA address if supported by H/W */
  793. if (dma_set_mask_and_coherent(bus->dev, DMA_BIT_MASK(64)))
  794. dma_set_mask_and_coherent(bus->dev, DMA_BIT_MASK(32));
  795. dma_set_max_seg_size(bus->dev, UINT_MAX);
  796. /* initialize streams */
  797. snd_hdac_ext_stream_init_all
  798. (bus, 0, cp_streams, SNDRV_PCM_STREAM_CAPTURE);
  799. start_idx = cp_streams;
  800. snd_hdac_ext_stream_init_all
  801. (bus, start_idx, pb_streams, SNDRV_PCM_STREAM_PLAYBACK);
  802. err = snd_hdac_bus_alloc_stream_pages(bus);
  803. if (err < 0)
  804. return err;
  805. return 0;
  806. }
  807. static int skl_probe(struct pci_dev *pci,
  808. const struct pci_device_id *pci_id)
  809. {
  810. struct skl_dev *skl;
  811. struct hdac_bus *bus = NULL;
  812. int err;
  813. switch (skl_pci_binding) {
  814. case SND_SKL_PCI_BIND_AUTO:
  815. err = snd_intel_dsp_driver_probe(pci);
  816. if (err != SND_INTEL_DSP_DRIVER_ANY &&
  817. err != SND_INTEL_DSP_DRIVER_SST)
  818. return -ENODEV;
  819. break;
  820. case SND_SKL_PCI_BIND_LEGACY:
  821. dev_info(&pci->dev, "Module parameter forced binding with HDAudio legacy, aborting probe\n");
  822. return -ENODEV;
  823. case SND_SKL_PCI_BIND_ASOC:
  824. dev_info(&pci->dev, "Module parameter forced binding with SKL driver, bypassed detection logic\n");
  825. break;
  826. default:
  827. dev_err(&pci->dev, "invalid value for skl_pci_binding module parameter, ignored\n");
  828. break;
  829. }
  830. /* we use ext core ops, so provide NULL for ops here */
  831. err = skl_create(pci, &skl);
  832. if (err < 0)
  833. return err;
  834. bus = skl_to_bus(skl);
  835. err = skl_first_init(bus);
  836. if (err < 0) {
  837. dev_err(bus->dev, "skl_first_init failed with err: %d\n", err);
  838. goto out_free;
  839. }
  840. skl->pci_id = pci->device;
  841. device_disable_async_suspend(bus->dev);
  842. skl->nhlt = intel_nhlt_init(bus->dev);
  843. if (skl->nhlt == NULL) {
  844. #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC)
  845. dev_err(bus->dev, "no nhlt info found\n");
  846. err = -ENODEV;
  847. goto out_free;
  848. #else
  849. dev_warn(bus->dev, "no nhlt info found, continuing to try to enable HDAudio codec\n");
  850. #endif
  851. } else {
  852. err = skl_nhlt_create_sysfs(skl);
  853. if (err < 0) {
  854. dev_err(bus->dev, "skl_nhlt_create_sysfs failed with err: %d\n", err);
  855. goto out_nhlt_free;
  856. }
  857. skl_nhlt_update_topology_bin(skl);
  858. /* create device for dsp clk */
  859. err = skl_clock_device_register(skl);
  860. if (err < 0) {
  861. dev_err(bus->dev, "skl_clock_device_register failed with err: %d\n", err);
  862. goto out_clk_free;
  863. }
  864. }
  865. pci_set_drvdata(skl->pci, bus);
  866. err = skl_find_machine(skl, (void *)pci_id->driver_data);
  867. if (err < 0) {
  868. dev_err(bus->dev, "skl_find_machine failed with err: %d\n", err);
  869. goto out_nhlt_free;
  870. }
  871. err = skl_init_dsp(skl);
  872. if (err < 0) {
  873. dev_dbg(bus->dev, "error failed to register dsp\n");
  874. goto out_nhlt_free;
  875. }
  876. skl->enable_miscbdcge = skl_enable_miscbdcge;
  877. skl->clock_power_gating = skl_clock_power_gating;
  878. if (bus->mlcap)
  879. snd_hdac_ext_bus_get_ml_capabilities(bus);
  880. /* create device for soc dmic */
  881. err = skl_dmic_device_register(skl);
  882. if (err < 0) {
  883. dev_err(bus->dev, "skl_dmic_device_register failed with err: %d\n", err);
  884. goto out_dsp_free;
  885. }
  886. schedule_work(&skl->probe_work);
  887. return 0;
  888. out_dsp_free:
  889. skl_free_dsp(skl);
  890. out_clk_free:
  891. skl_clock_device_unregister(skl);
  892. out_nhlt_free:
  893. if (skl->nhlt)
  894. intel_nhlt_free(skl->nhlt);
  895. out_free:
  896. skl_free(bus);
  897. return err;
  898. }
  899. static void skl_shutdown(struct pci_dev *pci)
  900. {
  901. struct hdac_bus *bus = pci_get_drvdata(pci);
  902. struct hdac_stream *s;
  903. struct hdac_ext_stream *stream;
  904. struct skl_dev *skl;
  905. if (!bus)
  906. return;
  907. skl = bus_to_skl(bus);
  908. if (!skl->init_done)
  909. return;
  910. snd_hdac_stop_streams(bus);
  911. snd_hdac_ext_bus_link_power_down_all(bus);
  912. skl_dsp_sleep(skl->dsp);
  913. list_for_each_entry(s, &bus->stream_list, list) {
  914. stream = stream_to_hdac_ext_stream(s);
  915. snd_hdac_ext_stream_decouple(bus, stream, false);
  916. }
  917. snd_hdac_bus_stop_chip(bus);
  918. }
  919. static void skl_remove(struct pci_dev *pci)
  920. {
  921. struct hdac_bus *bus = pci_get_drvdata(pci);
  922. struct skl_dev *skl = bus_to_skl(bus);
  923. cancel_work_sync(&skl->probe_work);
  924. pm_runtime_get_noresume(&pci->dev);
  925. /* codec removal, invoke bus_device_remove */
  926. snd_hdac_ext_bus_device_remove(bus);
  927. skl_platform_unregister(&pci->dev);
  928. skl_free_dsp(skl);
  929. skl_machine_device_unregister(skl);
  930. skl_dmic_device_unregister(skl);
  931. skl_clock_device_unregister(skl);
  932. skl_nhlt_remove_sysfs(skl);
  933. if (skl->nhlt)
  934. intel_nhlt_free(skl->nhlt);
  935. skl_free(bus);
  936. }
  937. /* PCI IDs */
  938. static const struct pci_device_id skl_ids[] = {
  939. #if IS_ENABLED(CONFIG_SND_SOC_INTEL_SKL)
  940. /* Sunrise Point-LP */
  941. { PCI_DEVICE(0x8086, 0x9d70),
  942. .driver_data = (unsigned long)&snd_soc_acpi_intel_skl_machines},
  943. #endif
  944. #if IS_ENABLED(CONFIG_SND_SOC_INTEL_APL)
  945. /* BXT-P */
  946. { PCI_DEVICE(0x8086, 0x5a98),
  947. .driver_data = (unsigned long)&snd_soc_acpi_intel_bxt_machines},
  948. #endif
  949. #if IS_ENABLED(CONFIG_SND_SOC_INTEL_KBL)
  950. /* KBL */
  951. { PCI_DEVICE(0x8086, 0x9D71),
  952. .driver_data = (unsigned long)&snd_soc_acpi_intel_kbl_machines},
  953. #endif
  954. #if IS_ENABLED(CONFIG_SND_SOC_INTEL_GLK)
  955. /* GLK */
  956. { PCI_DEVICE(0x8086, 0x3198),
  957. .driver_data = (unsigned long)&snd_soc_acpi_intel_glk_machines},
  958. #endif
  959. #if IS_ENABLED(CONFIG_SND_SOC_INTEL_CNL)
  960. /* CNL */
  961. { PCI_DEVICE(0x8086, 0x9dc8),
  962. .driver_data = (unsigned long)&snd_soc_acpi_intel_cnl_machines},
  963. #endif
  964. #if IS_ENABLED(CONFIG_SND_SOC_INTEL_CFL)
  965. /* CFL */
  966. { PCI_DEVICE(0x8086, 0xa348),
  967. .driver_data = (unsigned long)&snd_soc_acpi_intel_cnl_machines},
  968. #endif
  969. #if IS_ENABLED(CONFIG_SND_SOC_INTEL_CML_LP)
  970. /* CML-LP */
  971. { PCI_DEVICE(0x8086, 0x02c8),
  972. .driver_data = (unsigned long)&snd_soc_acpi_intel_cnl_machines},
  973. #endif
  974. #if IS_ENABLED(CONFIG_SND_SOC_INTEL_CML_H)
  975. /* CML-H */
  976. { PCI_DEVICE(0x8086, 0x06c8),
  977. .driver_data = (unsigned long)&snd_soc_acpi_intel_cnl_machines},
  978. #endif
  979. { 0, }
  980. };
  981. MODULE_DEVICE_TABLE(pci, skl_ids);
  982. /* pci_driver definition */
  983. static struct pci_driver skl_driver = {
  984. .name = KBUILD_MODNAME,
  985. .id_table = skl_ids,
  986. .probe = skl_probe,
  987. .remove = skl_remove,
  988. .shutdown = skl_shutdown,
  989. .driver = {
  990. .pm = &skl_pm,
  991. },
  992. };
  993. module_pci_driver(skl_driver);
  994. MODULE_LICENSE("GPL v2");
  995. MODULE_DESCRIPTION("Intel Skylake ASoC HDA driver");