hdac_i915.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * hdac_i915.c - routines for sync between HD-A core and i915 display driver
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/pci.h>
  17. #include <linux/component.h>
  18. #include <drm/i915_component.h>
  19. #include <sound/core.h>
  20. #include <sound/hdaudio.h>
  21. #include <sound/hda_i915.h>
  22. #include <sound/hda_register.h>
  23. static struct i915_audio_component *hdac_acomp;
  24. /**
  25. * snd_hdac_set_codec_wakeup - Enable / disable HDMI/DP codec wakeup
  26. * @bus: HDA core bus
  27. * @enable: enable or disable the wakeup
  28. *
  29. * This function is supposed to be used only by a HD-audio controller
  30. * driver that needs the interaction with i915 graphics.
  31. *
  32. * This function should be called during the chip reset, also called at
  33. * resume for updating STATESTS register read.
  34. *
  35. * Returns zero for success or a negative error code.
  36. */
  37. int snd_hdac_set_codec_wakeup(struct hdac_bus *bus, bool enable)
  38. {
  39. struct i915_audio_component *acomp = bus->audio_component;
  40. if (!acomp || !acomp->ops)
  41. return -ENODEV;
  42. if (!acomp->ops->codec_wake_override) {
  43. dev_warn(bus->dev,
  44. "Invalid codec wake callback\n");
  45. return 0;
  46. }
  47. dev_dbg(bus->dev, "%s codec wakeup\n",
  48. enable ? "enable" : "disable");
  49. acomp->ops->codec_wake_override(acomp->dev, enable);
  50. return 0;
  51. }
  52. EXPORT_SYMBOL_GPL(snd_hdac_set_codec_wakeup);
  53. /**
  54. * snd_hdac_display_power - Power up / down the power refcount
  55. * @bus: HDA core bus
  56. * @enable: power up or down
  57. *
  58. * This function is supposed to be used only by a HD-audio controller
  59. * driver that needs the interaction with i915 graphics.
  60. *
  61. * This function manages a refcount and calls the i915 get_power() and
  62. * put_power() ops accordingly, toggling the codec wakeup, too.
  63. *
  64. * Returns zero for success or a negative error code.
  65. */
  66. int snd_hdac_display_power(struct hdac_bus *bus, bool enable)
  67. {
  68. struct i915_audio_component *acomp = bus->audio_component;
  69. if (!acomp || !acomp->ops)
  70. return -ENODEV;
  71. dev_dbg(bus->dev, "display power %s\n",
  72. enable ? "enable" : "disable");
  73. if (enable) {
  74. if (!bus->i915_power_refcount++) {
  75. acomp->ops->get_power(acomp->dev);
  76. snd_hdac_set_codec_wakeup(bus, true);
  77. snd_hdac_set_codec_wakeup(bus, false);
  78. }
  79. } else {
  80. WARN_ON(!bus->i915_power_refcount);
  81. if (!--bus->i915_power_refcount)
  82. acomp->ops->put_power(acomp->dev);
  83. }
  84. return 0;
  85. }
  86. EXPORT_SYMBOL_GPL(snd_hdac_display_power);
  87. #define CONTROLLER_IN_GPU(pci) (((pci)->device == 0x0a0c) || \
  88. ((pci)->device == 0x0c0c) || \
  89. ((pci)->device == 0x0d0c) || \
  90. ((pci)->device == 0x160c))
  91. /**
  92. * snd_hdac_i915_set_bclk - Reprogram BCLK for HSW/BDW
  93. * @bus: HDA core bus
  94. *
  95. * Intel HSW/BDW display HDA controller is in GPU. Both its power and link BCLK
  96. * depends on GPU. Two Extended Mode registers EM4 (M value) and EM5 (N Value)
  97. * are used to convert CDClk (Core Display Clock) to 24MHz BCLK:
  98. * BCLK = CDCLK * M / N
  99. * The values will be lost when the display power well is disabled and need to
  100. * be restored to avoid abnormal playback speed.
  101. *
  102. * Call this function at initializing and changing power well, as well as
  103. * at ELD notifier for the hotplug.
  104. */
  105. void snd_hdac_i915_set_bclk(struct hdac_bus *bus)
  106. {
  107. struct i915_audio_component *acomp = bus->audio_component;
  108. struct pci_dev *pci = to_pci_dev(bus->dev);
  109. int cdclk_freq;
  110. unsigned int bclk_m, bclk_n;
  111. if (!acomp || !acomp->ops || !acomp->ops->get_cdclk_freq)
  112. return; /* only for i915 binding */
  113. if (!CONTROLLER_IN_GPU(pci))
  114. return; /* only HSW/BDW */
  115. cdclk_freq = acomp->ops->get_cdclk_freq(acomp->dev);
  116. switch (cdclk_freq) {
  117. case 337500:
  118. bclk_m = 16;
  119. bclk_n = 225;
  120. break;
  121. case 450000:
  122. default: /* default CDCLK 450MHz */
  123. bclk_m = 4;
  124. bclk_n = 75;
  125. break;
  126. case 540000:
  127. bclk_m = 4;
  128. bclk_n = 90;
  129. break;
  130. case 675000:
  131. bclk_m = 8;
  132. bclk_n = 225;
  133. break;
  134. }
  135. snd_hdac_chip_writew(bus, HSW_EM4, bclk_m);
  136. snd_hdac_chip_writew(bus, HSW_EM5, bclk_n);
  137. }
  138. EXPORT_SYMBOL_GPL(snd_hdac_i915_set_bclk);
  139. /* There is a fixed mapping between audio pin node and display port.
  140. * on SNB, IVY, HSW, BSW, SKL, BXT, KBL:
  141. * Pin Widget 5 - PORT B (port = 1 in i915 driver)
  142. * Pin Widget 6 - PORT C (port = 2 in i915 driver)
  143. * Pin Widget 7 - PORT D (port = 3 in i915 driver)
  144. *
  145. * on VLV, ILK:
  146. * Pin Widget 4 - PORT B (port = 1 in i915 driver)
  147. * Pin Widget 5 - PORT C (port = 2 in i915 driver)
  148. * Pin Widget 6 - PORT D (port = 3 in i915 driver)
  149. */
  150. static int pin2port(struct hdac_device *codec, hda_nid_t pin_nid)
  151. {
  152. int base_nid;
  153. switch (codec->vendor_id) {
  154. case 0x80860054: /* ILK */
  155. case 0x80862804: /* ILK */
  156. case 0x80862882: /* VLV */
  157. base_nid = 3;
  158. break;
  159. default:
  160. base_nid = 4;
  161. break;
  162. }
  163. if (WARN_ON(pin_nid <= base_nid || pin_nid > base_nid + 3))
  164. return -1;
  165. return pin_nid - base_nid;
  166. }
  167. /**
  168. * snd_hdac_sync_audio_rate - Set N/CTS based on the sample rate
  169. * @codec: HDA codec
  170. * @nid: the pin widget NID
  171. * @rate: the sample rate to set
  172. *
  173. * This function is supposed to be used only by a HD-audio controller
  174. * driver that needs the interaction with i915 graphics.
  175. *
  176. * This function sets N/CTS value based on the given sample rate.
  177. * Returns zero for success, or a negative error code.
  178. */
  179. int snd_hdac_sync_audio_rate(struct hdac_device *codec, hda_nid_t nid, int rate)
  180. {
  181. struct hdac_bus *bus = codec->bus;
  182. struct i915_audio_component *acomp = bus->audio_component;
  183. int port;
  184. if (!acomp || !acomp->ops || !acomp->ops->sync_audio_rate)
  185. return -ENODEV;
  186. port = pin2port(codec, nid);
  187. if (port < 0)
  188. return -EINVAL;
  189. return acomp->ops->sync_audio_rate(acomp->dev, port, rate);
  190. }
  191. EXPORT_SYMBOL_GPL(snd_hdac_sync_audio_rate);
  192. /**
  193. * snd_hdac_acomp_get_eld - Get the audio state and ELD via component
  194. * @codec: HDA codec
  195. * @nid: the pin widget NID
  196. * @audio_enabled: the pointer to store the current audio state
  197. * @buffer: the buffer pointer to store ELD bytes
  198. * @max_bytes: the max bytes to be stored on @buffer
  199. *
  200. * This function is supposed to be used only by a HD-audio controller
  201. * driver that needs the interaction with i915 graphics.
  202. *
  203. * This function queries the current state of the audio on the given
  204. * digital port and fetches the ELD bytes onto the given buffer.
  205. * It returns the number of bytes for the total ELD data, zero for
  206. * invalid ELD, or a negative error code.
  207. *
  208. * The return size is the total bytes required for the whole ELD bytes,
  209. * thus it may be over @max_bytes. If it's over @max_bytes, it implies
  210. * that only a part of ELD bytes have been fetched.
  211. */
  212. int snd_hdac_acomp_get_eld(struct hdac_device *codec, hda_nid_t nid,
  213. bool *audio_enabled, char *buffer, int max_bytes)
  214. {
  215. struct hdac_bus *bus = codec->bus;
  216. struct i915_audio_component *acomp = bus->audio_component;
  217. int port;
  218. if (!acomp || !acomp->ops || !acomp->ops->get_eld)
  219. return -ENODEV;
  220. port = pin2port(codec, nid);
  221. if (port < 0)
  222. return -EINVAL;
  223. return acomp->ops->get_eld(acomp->dev, port, audio_enabled,
  224. buffer, max_bytes);
  225. }
  226. EXPORT_SYMBOL_GPL(snd_hdac_acomp_get_eld);
  227. static int hdac_component_master_bind(struct device *dev)
  228. {
  229. struct i915_audio_component *acomp = hdac_acomp;
  230. int ret;
  231. ret = component_bind_all(dev, acomp);
  232. if (ret < 0)
  233. return ret;
  234. if (WARN_ON(!(acomp->dev && acomp->ops && acomp->ops->get_power &&
  235. acomp->ops->put_power && acomp->ops->get_cdclk_freq))) {
  236. ret = -EINVAL;
  237. goto out_unbind;
  238. }
  239. /*
  240. * Atm, we don't support dynamic unbinding initiated by the child
  241. * component, so pin its containing module until we unbind.
  242. */
  243. if (!try_module_get(acomp->ops->owner)) {
  244. ret = -ENODEV;
  245. goto out_unbind;
  246. }
  247. return 0;
  248. out_unbind:
  249. component_unbind_all(dev, acomp);
  250. return ret;
  251. }
  252. static void hdac_component_master_unbind(struct device *dev)
  253. {
  254. struct i915_audio_component *acomp = hdac_acomp;
  255. module_put(acomp->ops->owner);
  256. component_unbind_all(dev, acomp);
  257. WARN_ON(acomp->ops || acomp->dev);
  258. }
  259. static const struct component_master_ops hdac_component_master_ops = {
  260. .bind = hdac_component_master_bind,
  261. .unbind = hdac_component_master_unbind,
  262. };
  263. static int hdac_component_master_match(struct device *dev, void *data)
  264. {
  265. /* i915 is the only supported component */
  266. return !strcmp(dev->driver->name, "i915");
  267. }
  268. /**
  269. * snd_hdac_i915_register_notifier - Register i915 audio component ops
  270. * @aops: i915 audio component ops
  271. *
  272. * This function is supposed to be used only by a HD-audio controller
  273. * driver that needs the interaction with i915 graphics.
  274. *
  275. * This function sets the given ops to be called by the i915 graphics driver.
  276. *
  277. * Returns zero for success or a negative error code.
  278. */
  279. int snd_hdac_i915_register_notifier(const struct i915_audio_component_audio_ops *aops)
  280. {
  281. if (!hdac_acomp)
  282. return -ENODEV;
  283. hdac_acomp->audio_ops = aops;
  284. return 0;
  285. }
  286. EXPORT_SYMBOL_GPL(snd_hdac_i915_register_notifier);
  287. /* check whether intel graphics is present */
  288. static bool i915_gfx_present(void)
  289. {
  290. static struct pci_device_id ids[] = {
  291. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_ANY_ID),
  292. .class = PCI_BASE_CLASS_DISPLAY << 16,
  293. .class_mask = 0xff << 16 },
  294. {}
  295. };
  296. return pci_dev_present(ids);
  297. }
  298. /**
  299. * snd_hdac_i915_init - Initialize i915 audio component
  300. * @bus: HDA core bus
  301. *
  302. * This function is supposed to be used only by a HD-audio controller
  303. * driver that needs the interaction with i915 graphics.
  304. *
  305. * This function initializes and sets up the audio component to communicate
  306. * with i915 graphics driver.
  307. *
  308. * Returns zero for success or a negative error code.
  309. */
  310. int snd_hdac_i915_init(struct hdac_bus *bus)
  311. {
  312. struct component_match *match = NULL;
  313. struct device *dev = bus->dev;
  314. struct i915_audio_component *acomp;
  315. int ret;
  316. if (WARN_ON(hdac_acomp))
  317. return -EBUSY;
  318. if (!i915_gfx_present())
  319. return -ENODEV;
  320. acomp = kzalloc(sizeof(*acomp), GFP_KERNEL);
  321. if (!acomp)
  322. return -ENOMEM;
  323. bus->audio_component = acomp;
  324. hdac_acomp = acomp;
  325. component_match_add(dev, &match, hdac_component_master_match, bus);
  326. ret = component_master_add_with_match(dev, &hdac_component_master_ops,
  327. match);
  328. if (ret < 0)
  329. goto out_err;
  330. /*
  331. * Atm, we don't support deferring the component binding, so make sure
  332. * i915 is loaded and that the binding successfully completes.
  333. */
  334. request_module("i915");
  335. if (!acomp->ops) {
  336. ret = -ENODEV;
  337. goto out_master_del;
  338. }
  339. dev_dbg(dev, "bound to i915 component master\n");
  340. return 0;
  341. out_master_del:
  342. component_master_del(dev, &hdac_component_master_ops);
  343. out_err:
  344. kfree(acomp);
  345. bus->audio_component = NULL;
  346. hdac_acomp = NULL;
  347. dev_info(dev, "failed to add i915 component master (%d)\n", ret);
  348. return ret;
  349. }
  350. EXPORT_SYMBOL_GPL(snd_hdac_i915_init);
  351. /**
  352. * snd_hdac_i915_exit - Finalize i915 audio component
  353. * @bus: HDA core bus
  354. *
  355. * This function is supposed to be used only by a HD-audio controller
  356. * driver that needs the interaction with i915 graphics.
  357. *
  358. * This function releases the i915 audio component that has been used.
  359. *
  360. * Returns zero for success or a negative error code.
  361. */
  362. int snd_hdac_i915_exit(struct hdac_bus *bus)
  363. {
  364. struct device *dev = bus->dev;
  365. struct i915_audio_component *acomp = bus->audio_component;
  366. if (!acomp)
  367. return 0;
  368. WARN_ON(bus->i915_power_refcount);
  369. if (bus->i915_power_refcount > 0 && acomp->ops)
  370. acomp->ops->put_power(acomp->dev);
  371. component_master_del(dev, &hdac_component_master_ops);
  372. kfree(acomp);
  373. bus->audio_component = NULL;
  374. hdac_acomp = NULL;
  375. return 0;
  376. }
  377. EXPORT_SYMBOL_GPL(snd_hdac_i915_exit);