skl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 <sound/pcm.h>
  29. #include "skl.h"
  30. /*
  31. * initialize the PCI registers
  32. */
  33. static void skl_update_pci_byte(struct pci_dev *pci, unsigned int reg,
  34. unsigned char mask, unsigned char val)
  35. {
  36. unsigned char data;
  37. pci_read_config_byte(pci, reg, &data);
  38. data &= ~mask;
  39. data |= (val & mask);
  40. pci_write_config_byte(pci, reg, data);
  41. }
  42. static void skl_init_pci(struct skl *skl)
  43. {
  44. struct hdac_ext_bus *ebus = &skl->ebus;
  45. /*
  46. * Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
  47. * TCSEL == Traffic Class Select Register, which sets PCI express QOS
  48. * Ensuring these bits are 0 clears playback static on some HD Audio
  49. * codecs.
  50. * The PCI register TCSEL is defined in the Intel manuals.
  51. */
  52. dev_dbg(ebus_to_hbus(ebus)->dev, "Clearing TCSEL\n");
  53. skl_update_pci_byte(skl->pci, AZX_PCIREG_TCSEL, 0x07, 0);
  54. }
  55. /* called from IRQ */
  56. static void skl_stream_update(struct hdac_bus *bus, struct hdac_stream *hstr)
  57. {
  58. snd_pcm_period_elapsed(hstr->substream);
  59. }
  60. static irqreturn_t skl_interrupt(int irq, void *dev_id)
  61. {
  62. struct hdac_ext_bus *ebus = dev_id;
  63. struct hdac_bus *bus = ebus_to_hbus(ebus);
  64. u32 status;
  65. if (!pm_runtime_active(bus->dev))
  66. return IRQ_NONE;
  67. spin_lock(&bus->reg_lock);
  68. status = snd_hdac_chip_readl(bus, INTSTS);
  69. if (status == 0 || status == 0xffffffff) {
  70. spin_unlock(&bus->reg_lock);
  71. return IRQ_NONE;
  72. }
  73. /* clear rirb int */
  74. status = snd_hdac_chip_readb(bus, RIRBSTS);
  75. if (status & RIRB_INT_MASK) {
  76. if (status & RIRB_INT_RESPONSE)
  77. snd_hdac_bus_update_rirb(bus);
  78. snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
  79. }
  80. spin_unlock(&bus->reg_lock);
  81. return snd_hdac_chip_readl(bus, INTSTS) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
  82. }
  83. static irqreturn_t skl_threaded_handler(int irq, void *dev_id)
  84. {
  85. struct hdac_ext_bus *ebus = dev_id;
  86. struct hdac_bus *bus = ebus_to_hbus(ebus);
  87. u32 status;
  88. status = snd_hdac_chip_readl(bus, INTSTS);
  89. snd_hdac_bus_handle_stream_irq(bus, status, skl_stream_update);
  90. return IRQ_HANDLED;
  91. }
  92. static int skl_acquire_irq(struct hdac_ext_bus *ebus, int do_disconnect)
  93. {
  94. struct skl *skl = ebus_to_skl(ebus);
  95. struct hdac_bus *bus = ebus_to_hbus(ebus);
  96. int ret;
  97. ret = request_threaded_irq(skl->pci->irq, skl_interrupt,
  98. skl_threaded_handler,
  99. IRQF_SHARED,
  100. KBUILD_MODNAME, ebus);
  101. if (ret) {
  102. dev_err(bus->dev,
  103. "unable to grab IRQ %d, disabling device\n",
  104. skl->pci->irq);
  105. return ret;
  106. }
  107. bus->irq = skl->pci->irq;
  108. pci_intx(skl->pci, 1);
  109. return 0;
  110. }
  111. #ifdef CONFIG_PM_SLEEP
  112. /*
  113. * power management
  114. */
  115. static int skl_suspend(struct device *dev)
  116. {
  117. struct pci_dev *pci = to_pci_dev(dev);
  118. struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
  119. struct hdac_bus *bus = ebus_to_hbus(ebus);
  120. snd_hdac_bus_stop_chip(bus);
  121. snd_hdac_bus_enter_link_reset(bus);
  122. return 0;
  123. }
  124. static int skl_resume(struct device *dev)
  125. {
  126. struct pci_dev *pci = to_pci_dev(dev);
  127. struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
  128. struct hdac_bus *bus = ebus_to_hbus(ebus);
  129. struct skl *hda = ebus_to_skl(ebus);
  130. skl_init_pci(hda);
  131. snd_hdac_bus_init_chip(bus, 1);
  132. return 0;
  133. }
  134. #endif /* CONFIG_PM_SLEEP */
  135. #ifdef CONFIG_PM
  136. static int skl_runtime_suspend(struct device *dev)
  137. {
  138. struct pci_dev *pci = to_pci_dev(dev);
  139. struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
  140. struct hdac_bus *bus = ebus_to_hbus(ebus);
  141. struct skl *skl = ebus_to_skl(ebus);
  142. int ret;
  143. dev_dbg(bus->dev, "in %s\n", __func__);
  144. /* enable controller wake up event */
  145. snd_hdac_chip_updatew(bus, WAKEEN, 0, STATESTS_INT_MASK);
  146. snd_hdac_ext_bus_link_power_down_all(ebus);
  147. ret = skl_suspend_dsp(skl);
  148. if (ret < 0)
  149. return ret;
  150. snd_hdac_bus_stop_chip(bus);
  151. snd_hdac_bus_enter_link_reset(bus);
  152. return 0;
  153. }
  154. static int skl_runtime_resume(struct device *dev)
  155. {
  156. struct pci_dev *pci = to_pci_dev(dev);
  157. struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
  158. struct hdac_bus *bus = ebus_to_hbus(ebus);
  159. struct skl *skl = ebus_to_skl(ebus);
  160. int status;
  161. dev_dbg(bus->dev, "in %s\n", __func__);
  162. /* Read STATESTS before controller reset */
  163. status = snd_hdac_chip_readw(bus, STATESTS);
  164. skl_init_pci(skl);
  165. snd_hdac_bus_init_chip(bus, true);
  166. /* disable controller Wake Up event */
  167. snd_hdac_chip_updatew(bus, WAKEEN, STATESTS_INT_MASK, 0);
  168. return skl_resume_dsp(skl);
  169. }
  170. #endif /* CONFIG_PM */
  171. static const struct dev_pm_ops skl_pm = {
  172. SET_SYSTEM_SLEEP_PM_OPS(skl_suspend, skl_resume)
  173. SET_RUNTIME_PM_OPS(skl_runtime_suspend, skl_runtime_resume, NULL)
  174. };
  175. /*
  176. * destructor
  177. */
  178. static int skl_free(struct hdac_ext_bus *ebus)
  179. {
  180. struct skl *skl = ebus_to_skl(ebus);
  181. struct hdac_bus *bus = ebus_to_hbus(ebus);
  182. skl->init_failed = 1; /* to be sure */
  183. snd_hdac_ext_stop_streams(ebus);
  184. if (bus->irq >= 0)
  185. free_irq(bus->irq, (void *)bus);
  186. if (bus->remap_addr)
  187. iounmap(bus->remap_addr);
  188. snd_hdac_bus_free_stream_pages(bus);
  189. snd_hdac_stream_free_all(ebus);
  190. snd_hdac_link_free_all(ebus);
  191. pci_release_regions(skl->pci);
  192. pci_disable_device(skl->pci);
  193. snd_hdac_ext_bus_exit(ebus);
  194. return 0;
  195. }
  196. static int skl_dmic_device_register(struct skl *skl)
  197. {
  198. struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
  199. struct platform_device *pdev;
  200. int ret;
  201. /* SKL has one dmic port, so allocate dmic device for this */
  202. pdev = platform_device_alloc("dmic-codec", -1);
  203. if (!pdev) {
  204. dev_err(bus->dev, "failed to allocate dmic device\n");
  205. return -ENOMEM;
  206. }
  207. ret = platform_device_add(pdev);
  208. if (ret) {
  209. dev_err(bus->dev, "failed to add dmic device: %d\n", ret);
  210. platform_device_put(pdev);
  211. return ret;
  212. }
  213. skl->dmic_dev = pdev;
  214. return 0;
  215. }
  216. static void skl_dmic_device_unregister(struct skl *skl)
  217. {
  218. if (skl->dmic_dev)
  219. platform_device_unregister(skl->dmic_dev);
  220. }
  221. /*
  222. * Probe the given codec address
  223. */
  224. static int probe_codec(struct hdac_ext_bus *ebus, int addr)
  225. {
  226. struct hdac_bus *bus = ebus_to_hbus(ebus);
  227. unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
  228. (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
  229. unsigned int res = -1;
  230. mutex_lock(&bus->cmd_mutex);
  231. snd_hdac_bus_send_cmd(bus, cmd);
  232. snd_hdac_bus_get_response(bus, addr, &res);
  233. mutex_unlock(&bus->cmd_mutex);
  234. if (res == -1)
  235. return -EIO;
  236. dev_dbg(bus->dev, "codec #%d probed OK\n", addr);
  237. return snd_hdac_ext_bus_device_init(ebus, addr);
  238. }
  239. /* Codec initialization */
  240. static int skl_codec_create(struct hdac_ext_bus *ebus)
  241. {
  242. struct hdac_bus *bus = ebus_to_hbus(ebus);
  243. int c, max_slots;
  244. max_slots = HDA_MAX_CODECS;
  245. /* First try to probe all given codec slots */
  246. for (c = 0; c < max_slots; c++) {
  247. if ((bus->codec_mask & (1 << c))) {
  248. if (probe_codec(ebus, c) < 0) {
  249. /*
  250. * Some BIOSen give you wrong codec addresses
  251. * that don't exist
  252. */
  253. dev_warn(bus->dev,
  254. "Codec #%d probe error; disabling it...\n", c);
  255. bus->codec_mask &= ~(1 << c);
  256. /*
  257. * More badly, accessing to a non-existing
  258. * codec often screws up the controller bus,
  259. * and disturbs the further communications.
  260. * Thus if an error occurs during probing,
  261. * better to reset the controller bus to get
  262. * back to the sanity state.
  263. */
  264. snd_hdac_bus_stop_chip(bus);
  265. snd_hdac_bus_init_chip(bus, true);
  266. }
  267. }
  268. }
  269. return 0;
  270. }
  271. static const struct hdac_bus_ops bus_core_ops = {
  272. .command = snd_hdac_bus_send_cmd,
  273. .get_response = snd_hdac_bus_get_response,
  274. };
  275. /*
  276. * constructor
  277. */
  278. static int skl_create(struct pci_dev *pci,
  279. const struct hdac_io_ops *io_ops,
  280. struct skl **rskl)
  281. {
  282. struct skl *skl;
  283. struct hdac_ext_bus *ebus;
  284. int err;
  285. *rskl = NULL;
  286. err = pci_enable_device(pci);
  287. if (err < 0)
  288. return err;
  289. skl = devm_kzalloc(&pci->dev, sizeof(*skl), GFP_KERNEL);
  290. if (!skl) {
  291. pci_disable_device(pci);
  292. return -ENOMEM;
  293. }
  294. ebus = &skl->ebus;
  295. snd_hdac_ext_bus_init(ebus, &pci->dev, &bus_core_ops, io_ops);
  296. ebus->bus.use_posbuf = 1;
  297. skl->pci = pci;
  298. ebus->bus.bdl_pos_adj = 0;
  299. *rskl = skl;
  300. return 0;
  301. }
  302. static int skl_first_init(struct hdac_ext_bus *ebus)
  303. {
  304. struct skl *skl = ebus_to_skl(ebus);
  305. struct hdac_bus *bus = ebus_to_hbus(ebus);
  306. struct pci_dev *pci = skl->pci;
  307. int err;
  308. unsigned short gcap;
  309. int cp_streams, pb_streams, start_idx;
  310. err = pci_request_regions(pci, "Skylake HD audio");
  311. if (err < 0)
  312. return err;
  313. bus->addr = pci_resource_start(pci, 0);
  314. bus->remap_addr = pci_ioremap_bar(pci, 0);
  315. if (bus->remap_addr == NULL) {
  316. dev_err(bus->dev, "ioremap error\n");
  317. return -ENXIO;
  318. }
  319. snd_hdac_ext_bus_parse_capabilities(ebus);
  320. if (skl_acquire_irq(ebus, 0) < 0)
  321. return -EBUSY;
  322. pci_set_master(pci);
  323. synchronize_irq(bus->irq);
  324. gcap = snd_hdac_chip_readw(bus, GCAP);
  325. dev_dbg(bus->dev, "chipset global capabilities = 0x%x\n", gcap);
  326. /* allow 64bit DMA address if supported by H/W */
  327. if (!dma_set_mask(bus->dev, DMA_BIT_MASK(64))) {
  328. dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(64));
  329. } else {
  330. dma_set_mask(bus->dev, DMA_BIT_MASK(32));
  331. dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(32));
  332. }
  333. /* read number of streams from GCAP register */
  334. cp_streams = (gcap >> 8) & 0x0f;
  335. pb_streams = (gcap >> 12) & 0x0f;
  336. if (!pb_streams && !cp_streams)
  337. return -EIO;
  338. ebus->num_streams = cp_streams + pb_streams;
  339. /* initialize streams */
  340. snd_hdac_ext_stream_init_all
  341. (ebus, 0, cp_streams, SNDRV_PCM_STREAM_CAPTURE);
  342. start_idx = cp_streams;
  343. snd_hdac_ext_stream_init_all
  344. (ebus, start_idx, pb_streams, SNDRV_PCM_STREAM_PLAYBACK);
  345. err = snd_hdac_bus_alloc_stream_pages(bus);
  346. if (err < 0)
  347. return err;
  348. /* initialize chip */
  349. skl_init_pci(skl);
  350. snd_hdac_bus_init_chip(bus, true);
  351. /* codec detection */
  352. if (!bus->codec_mask) {
  353. dev_err(bus->dev, "no codecs found!\n");
  354. return -ENODEV;
  355. }
  356. return 0;
  357. }
  358. static int skl_probe(struct pci_dev *pci,
  359. const struct pci_device_id *pci_id)
  360. {
  361. struct skl *skl;
  362. struct hdac_ext_bus *ebus = NULL;
  363. struct hdac_bus *bus = NULL;
  364. int err;
  365. /* we use ext core ops, so provide NULL for ops here */
  366. err = skl_create(pci, NULL, &skl);
  367. if (err < 0)
  368. return err;
  369. ebus = &skl->ebus;
  370. bus = ebus_to_hbus(ebus);
  371. err = skl_first_init(ebus);
  372. if (err < 0)
  373. goto out_free;
  374. skl->nhlt = skl_nhlt_init(bus->dev);
  375. if (skl->nhlt == NULL) {
  376. err = -ENODEV;
  377. goto out_free;
  378. }
  379. pci_set_drvdata(skl->pci, ebus);
  380. /* check if dsp is there */
  381. if (ebus->ppcap) {
  382. err = skl_init_dsp(skl);
  383. if (err < 0) {
  384. dev_dbg(bus->dev, "error failed to register dsp\n");
  385. goto out_free;
  386. }
  387. }
  388. if (ebus->mlcap)
  389. snd_hdac_ext_bus_get_ml_capabilities(ebus);
  390. /* create device for soc dmic */
  391. err = skl_dmic_device_register(skl);
  392. if (err < 0)
  393. goto out_dsp_free;
  394. /* register platform dai and controls */
  395. err = skl_platform_register(bus->dev);
  396. if (err < 0)
  397. goto out_dmic_free;
  398. /* create codec instances */
  399. err = skl_codec_create(ebus);
  400. if (err < 0)
  401. goto out_unregister;
  402. /*configure PM */
  403. pm_runtime_set_autosuspend_delay(bus->dev, SKL_SUSPEND_DELAY);
  404. pm_runtime_use_autosuspend(bus->dev);
  405. pm_runtime_put_noidle(bus->dev);
  406. pm_runtime_allow(bus->dev);
  407. return 0;
  408. out_unregister:
  409. skl_platform_unregister(bus->dev);
  410. out_dmic_free:
  411. skl_dmic_device_unregister(skl);
  412. out_dsp_free:
  413. skl_free_dsp(skl);
  414. out_free:
  415. skl->init_failed = 1;
  416. skl_free(ebus);
  417. return err;
  418. }
  419. static void skl_remove(struct pci_dev *pci)
  420. {
  421. struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
  422. struct skl *skl = ebus_to_skl(ebus);
  423. if (skl->tplg)
  424. release_firmware(skl->tplg);
  425. if (pci_dev_run_wake(pci))
  426. pm_runtime_get_noresume(&pci->dev);
  427. pci_dev_put(pci);
  428. skl_platform_unregister(&pci->dev);
  429. skl_free_dsp(skl);
  430. skl_dmic_device_unregister(skl);
  431. skl_free(ebus);
  432. dev_set_drvdata(&pci->dev, NULL);
  433. }
  434. /* PCI IDs */
  435. static const struct pci_device_id skl_ids[] = {
  436. /* Sunrise Point-LP */
  437. { PCI_DEVICE(0x8086, 0x9d70), 0},
  438. { 0, }
  439. };
  440. MODULE_DEVICE_TABLE(pci, skl_ids);
  441. /* pci_driver definition */
  442. static struct pci_driver skl_driver = {
  443. .name = KBUILD_MODNAME,
  444. .id_table = skl_ids,
  445. .probe = skl_probe,
  446. .remove = skl_remove,
  447. .driver = {
  448. .pm = &skl_pm,
  449. },
  450. };
  451. module_pci_driver(skl_driver);
  452. MODULE_LICENSE("GPL v2");
  453. MODULE_DESCRIPTION("Intel Skylake ASoC HDA driver");