hdac_i915.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. * @dev_id: device identifier
  172. * @rate: the sample rate to set
  173. *
  174. * This function is supposed to be used only by a HD-audio controller
  175. * driver that needs the interaction with i915 graphics.
  176. *
  177. * This function sets N/CTS value based on the given sample rate.
  178. * Returns zero for success, or a negative error code.
  179. */
  180. int snd_hdac_sync_audio_rate(struct hdac_device *codec, hda_nid_t nid,
  181. int dev_id, int rate)
  182. {
  183. struct hdac_bus *bus = codec->bus;
  184. struct i915_audio_component *acomp = bus->audio_component;
  185. int port, pipe;
  186. if (!acomp || !acomp->ops || !acomp->ops->sync_audio_rate)
  187. return -ENODEV;
  188. port = pin2port(codec, nid);
  189. if (port < 0)
  190. return -EINVAL;
  191. pipe = dev_id;
  192. return acomp->ops->sync_audio_rate(acomp->dev, port, pipe, rate);
  193. }
  194. EXPORT_SYMBOL_GPL(snd_hdac_sync_audio_rate);
  195. /**
  196. * snd_hdac_acomp_get_eld - Get the audio state and ELD via component
  197. * @codec: HDA codec
  198. * @nid: the pin widget NID
  199. * @dev_id: device identifier
  200. * @audio_enabled: the pointer to store the current audio state
  201. * @buffer: the buffer pointer to store ELD bytes
  202. * @max_bytes: the max bytes to be stored on @buffer
  203. *
  204. * This function is supposed to be used only by a HD-audio controller
  205. * driver that needs the interaction with i915 graphics.
  206. *
  207. * This function queries the current state of the audio on the given
  208. * digital port and fetches the ELD bytes onto the given buffer.
  209. * It returns the number of bytes for the total ELD data, zero for
  210. * invalid ELD, or a negative error code.
  211. *
  212. * The return size is the total bytes required for the whole ELD bytes,
  213. * thus it may be over @max_bytes. If it's over @max_bytes, it implies
  214. * that only a part of ELD bytes have been fetched.
  215. */
  216. int snd_hdac_acomp_get_eld(struct hdac_device *codec, hda_nid_t nid, int dev_id,
  217. bool *audio_enabled, char *buffer, int max_bytes)
  218. {
  219. struct hdac_bus *bus = codec->bus;
  220. struct i915_audio_component *acomp = bus->audio_component;
  221. int port, pipe;
  222. if (!acomp || !acomp->ops || !acomp->ops->get_eld)
  223. return -ENODEV;
  224. port = pin2port(codec, nid);
  225. if (port < 0)
  226. return -EINVAL;
  227. pipe = dev_id;
  228. return acomp->ops->get_eld(acomp->dev, port, pipe, audio_enabled,
  229. buffer, max_bytes);
  230. }
  231. EXPORT_SYMBOL_GPL(snd_hdac_acomp_get_eld);
  232. static int hdac_component_master_bind(struct device *dev)
  233. {
  234. struct i915_audio_component *acomp = hdac_acomp;
  235. int ret;
  236. ret = component_bind_all(dev, acomp);
  237. if (ret < 0)
  238. return ret;
  239. if (WARN_ON(!(acomp->dev && acomp->ops && acomp->ops->get_power &&
  240. acomp->ops->put_power && acomp->ops->get_cdclk_freq))) {
  241. ret = -EINVAL;
  242. goto out_unbind;
  243. }
  244. /*
  245. * Atm, we don't support dynamic unbinding initiated by the child
  246. * component, so pin its containing module until we unbind.
  247. */
  248. if (!try_module_get(acomp->ops->owner)) {
  249. ret = -ENODEV;
  250. goto out_unbind;
  251. }
  252. return 0;
  253. out_unbind:
  254. component_unbind_all(dev, acomp);
  255. return ret;
  256. }
  257. static void hdac_component_master_unbind(struct device *dev)
  258. {
  259. struct i915_audio_component *acomp = hdac_acomp;
  260. module_put(acomp->ops->owner);
  261. component_unbind_all(dev, acomp);
  262. WARN_ON(acomp->ops || acomp->dev);
  263. }
  264. static const struct component_master_ops hdac_component_master_ops = {
  265. .bind = hdac_component_master_bind,
  266. .unbind = hdac_component_master_unbind,
  267. };
  268. static int hdac_component_master_match(struct device *dev, void *data)
  269. {
  270. /* i915 is the only supported component */
  271. return !strcmp(dev->driver->name, "i915");
  272. }
  273. /**
  274. * snd_hdac_i915_register_notifier - Register i915 audio component ops
  275. * @aops: i915 audio component ops
  276. *
  277. * This function is supposed to be used only by a HD-audio controller
  278. * driver that needs the interaction with i915 graphics.
  279. *
  280. * This function sets the given ops to be called by the i915 graphics driver.
  281. *
  282. * Returns zero for success or a negative error code.
  283. */
  284. int snd_hdac_i915_register_notifier(const struct i915_audio_component_audio_ops *aops)
  285. {
  286. if (!hdac_acomp)
  287. return -ENODEV;
  288. hdac_acomp->audio_ops = aops;
  289. return 0;
  290. }
  291. EXPORT_SYMBOL_GPL(snd_hdac_i915_register_notifier);
  292. /* check whether intel graphics is present */
  293. static bool i915_gfx_present(void)
  294. {
  295. static const struct pci_device_id ids[] = {
  296. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_ANY_ID),
  297. .class = PCI_BASE_CLASS_DISPLAY << 16,
  298. .class_mask = 0xff << 16 },
  299. {}
  300. };
  301. return pci_dev_present(ids);
  302. }
  303. /**
  304. * snd_hdac_i915_init - Initialize i915 audio component
  305. * @bus: HDA core bus
  306. *
  307. * This function is supposed to be used only by a HD-audio controller
  308. * driver that needs the interaction with i915 graphics.
  309. *
  310. * This function initializes and sets up the audio component to communicate
  311. * with i915 graphics driver.
  312. *
  313. * Returns zero for success or a negative error code.
  314. */
  315. int snd_hdac_i915_init(struct hdac_bus *bus)
  316. {
  317. struct component_match *match = NULL;
  318. struct device *dev = bus->dev;
  319. struct i915_audio_component *acomp;
  320. int ret;
  321. if (WARN_ON(hdac_acomp))
  322. return -EBUSY;
  323. if (!i915_gfx_present())
  324. return -ENODEV;
  325. acomp = kzalloc(sizeof(*acomp), GFP_KERNEL);
  326. if (!acomp)
  327. return -ENOMEM;
  328. bus->audio_component = acomp;
  329. hdac_acomp = acomp;
  330. component_match_add(dev, &match, hdac_component_master_match, bus);
  331. ret = component_master_add_with_match(dev, &hdac_component_master_ops,
  332. match);
  333. if (ret < 0)
  334. goto out_err;
  335. /*
  336. * Atm, we don't support deferring the component binding, so make sure
  337. * i915 is loaded and that the binding successfully completes.
  338. */
  339. request_module("i915");
  340. if (!acomp->ops) {
  341. ret = -ENODEV;
  342. goto out_master_del;
  343. }
  344. dev_dbg(dev, "bound to i915 component master\n");
  345. return 0;
  346. out_master_del:
  347. component_master_del(dev, &hdac_component_master_ops);
  348. out_err:
  349. kfree(acomp);
  350. bus->audio_component = NULL;
  351. hdac_acomp = NULL;
  352. dev_info(dev, "failed to add i915 component master (%d)\n", ret);
  353. return ret;
  354. }
  355. EXPORT_SYMBOL_GPL(snd_hdac_i915_init);
  356. /**
  357. * snd_hdac_i915_exit - Finalize i915 audio component
  358. * @bus: HDA core bus
  359. *
  360. * This function is supposed to be used only by a HD-audio controller
  361. * driver that needs the interaction with i915 graphics.
  362. *
  363. * This function releases the i915 audio component that has been used.
  364. *
  365. * Returns zero for success or a negative error code.
  366. */
  367. int snd_hdac_i915_exit(struct hdac_bus *bus)
  368. {
  369. struct device *dev = bus->dev;
  370. struct i915_audio_component *acomp = bus->audio_component;
  371. if (!acomp)
  372. return 0;
  373. WARN_ON(bus->i915_power_refcount);
  374. if (bus->i915_power_refcount > 0 && acomp->ops)
  375. acomp->ops->put_power(acomp->dev);
  376. component_master_del(dev, &hdac_component_master_ops);
  377. kfree(acomp);
  378. bus->audio_component = NULL;
  379. hdac_acomp = NULL;
  380. return 0;
  381. }
  382. EXPORT_SYMBOL_GPL(snd_hdac_i915_exit);