hdac_i915.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. static struct i915_audio_component *hdac_acomp;
  23. /**
  24. * snd_hdac_set_codec_wakeup - Enable / disable HDMI/DP codec wakeup
  25. * @bus: HDA core bus
  26. * @enable: enable or disable the wakeup
  27. *
  28. * This function is supposed to be used only by a HD-audio controller
  29. * driver that needs the interaction with i915 graphics.
  30. *
  31. * This function should be called during the chip reset, also called at
  32. * resume for updating STATESTS register read.
  33. *
  34. * Returns zero for success or a negative error code.
  35. */
  36. int snd_hdac_set_codec_wakeup(struct hdac_bus *bus, bool enable)
  37. {
  38. struct i915_audio_component *acomp = bus->audio_component;
  39. if (!acomp || !acomp->ops)
  40. return -ENODEV;
  41. if (!acomp->ops->codec_wake_override) {
  42. dev_warn(bus->dev,
  43. "Invalid codec wake callback\n");
  44. return 0;
  45. }
  46. dev_dbg(bus->dev, "%s codec wakeup\n",
  47. enable ? "enable" : "disable");
  48. acomp->ops->codec_wake_override(acomp->dev, enable);
  49. return 0;
  50. }
  51. EXPORT_SYMBOL_GPL(snd_hdac_set_codec_wakeup);
  52. /**
  53. * snd_hdac_display_power - Power up / down the power refcount
  54. * @bus: HDA core bus
  55. * @enable: power up or down
  56. *
  57. * This function is supposed to be used only by a HD-audio controller
  58. * driver that needs the interaction with i915 graphics.
  59. *
  60. * This function manages a refcount and calls the i915 get_power() and
  61. * put_power() ops accordingly, toggling the codec wakeup, too.
  62. *
  63. * Returns zero for success or a negative error code.
  64. */
  65. int snd_hdac_display_power(struct hdac_bus *bus, bool enable)
  66. {
  67. struct i915_audio_component *acomp = bus->audio_component;
  68. if (!acomp || !acomp->ops)
  69. return -ENODEV;
  70. dev_dbg(bus->dev, "display power %s\n",
  71. enable ? "enable" : "disable");
  72. if (enable) {
  73. if (!bus->i915_power_refcount++) {
  74. acomp->ops->get_power(acomp->dev);
  75. snd_hdac_set_codec_wakeup(bus, true);
  76. snd_hdac_set_codec_wakeup(bus, false);
  77. }
  78. } else {
  79. WARN_ON(!bus->i915_power_refcount);
  80. if (!--bus->i915_power_refcount)
  81. acomp->ops->put_power(acomp->dev);
  82. }
  83. return 0;
  84. }
  85. EXPORT_SYMBOL_GPL(snd_hdac_display_power);
  86. /**
  87. * snd_hdac_get_display_clk - Get CDCLK in kHz
  88. * @bus: HDA core bus
  89. *
  90. * This function is supposed to be used only by a HD-audio controller
  91. * driver that needs the interaction with i915 graphics.
  92. *
  93. * This function queries CDCLK value in kHz from the graphics driver and
  94. * returns the value. A negative code is returned in error.
  95. */
  96. int snd_hdac_get_display_clk(struct hdac_bus *bus)
  97. {
  98. struct i915_audio_component *acomp = bus->audio_component;
  99. if (!acomp || !acomp->ops)
  100. return -ENODEV;
  101. return acomp->ops->get_cdclk_freq(acomp->dev);
  102. }
  103. EXPORT_SYMBOL_GPL(snd_hdac_get_display_clk);
  104. /* There is a fixed mapping between audio pin node and display port
  105. * on current Intel platforms:
  106. * Pin Widget 5 - PORT B (port = 1 in i915 driver)
  107. * Pin Widget 6 - PORT C (port = 2 in i915 driver)
  108. * Pin Widget 7 - PORT D (port = 3 in i915 driver)
  109. */
  110. static int pin2port(hda_nid_t pin_nid)
  111. {
  112. if (WARN_ON(pin_nid < 5 || pin_nid > 7))
  113. return -1;
  114. return pin_nid - 4;
  115. }
  116. /**
  117. * snd_hdac_sync_audio_rate - Set N/CTS based on the sample rate
  118. * @bus: HDA core bus
  119. * @nid: the pin widget NID
  120. * @rate: the sample rate to set
  121. *
  122. * This function is supposed to be used only by a HD-audio controller
  123. * driver that needs the interaction with i915 graphics.
  124. *
  125. * This function sets N/CTS value based on the given sample rate.
  126. * Returns zero for success, or a negative error code.
  127. */
  128. int snd_hdac_sync_audio_rate(struct hdac_bus *bus, hda_nid_t nid, int rate)
  129. {
  130. struct i915_audio_component *acomp = bus->audio_component;
  131. int port;
  132. if (!acomp || !acomp->ops || !acomp->ops->sync_audio_rate)
  133. return -ENODEV;
  134. port = pin2port(nid);
  135. if (port < 0)
  136. return -EINVAL;
  137. return acomp->ops->sync_audio_rate(acomp->dev, port, rate);
  138. }
  139. EXPORT_SYMBOL_GPL(snd_hdac_sync_audio_rate);
  140. /**
  141. * snd_hdac_acomp_get_eld - Get the audio state and ELD via component
  142. * @bus: HDA core bus
  143. * @nid: the pin widget NID
  144. * @audio_enabled: the pointer to store the current audio state
  145. * @buffer: the buffer pointer to store ELD bytes
  146. * @max_bytes: the max bytes to be stored on @buffer
  147. *
  148. * This function is supposed to be used only by a HD-audio controller
  149. * driver that needs the interaction with i915 graphics.
  150. *
  151. * This function queries the current state of the audio on the given
  152. * digital port and fetches the ELD bytes onto the given buffer.
  153. * It returns the number of bytes for the total ELD data, zero for
  154. * invalid ELD, or a negative error code.
  155. *
  156. * The return size is the total bytes required for the whole ELD bytes,
  157. * thus it may be over @max_bytes. If it's over @max_bytes, it implies
  158. * that only a part of ELD bytes have been fetched.
  159. */
  160. int snd_hdac_acomp_get_eld(struct hdac_bus *bus, hda_nid_t nid,
  161. bool *audio_enabled, char *buffer, int max_bytes)
  162. {
  163. struct i915_audio_component *acomp = bus->audio_component;
  164. int port;
  165. if (!acomp || !acomp->ops || !acomp->ops->get_eld)
  166. return -ENODEV;
  167. port = pin2port(nid);
  168. if (port < 0)
  169. return -EINVAL;
  170. return acomp->ops->get_eld(acomp->dev, port, audio_enabled,
  171. buffer, max_bytes);
  172. }
  173. EXPORT_SYMBOL_GPL(snd_hdac_acomp_get_eld);
  174. static int hdac_component_master_bind(struct device *dev)
  175. {
  176. struct i915_audio_component *acomp = hdac_acomp;
  177. int ret;
  178. ret = component_bind_all(dev, acomp);
  179. if (ret < 0)
  180. return ret;
  181. if (WARN_ON(!(acomp->dev && acomp->ops && acomp->ops->get_power &&
  182. acomp->ops->put_power && acomp->ops->get_cdclk_freq))) {
  183. ret = -EINVAL;
  184. goto out_unbind;
  185. }
  186. /*
  187. * Atm, we don't support dynamic unbinding initiated by the child
  188. * component, so pin its containing module until we unbind.
  189. */
  190. if (!try_module_get(acomp->ops->owner)) {
  191. ret = -ENODEV;
  192. goto out_unbind;
  193. }
  194. return 0;
  195. out_unbind:
  196. component_unbind_all(dev, acomp);
  197. return ret;
  198. }
  199. static void hdac_component_master_unbind(struct device *dev)
  200. {
  201. struct i915_audio_component *acomp = hdac_acomp;
  202. module_put(acomp->ops->owner);
  203. component_unbind_all(dev, acomp);
  204. WARN_ON(acomp->ops || acomp->dev);
  205. }
  206. static const struct component_master_ops hdac_component_master_ops = {
  207. .bind = hdac_component_master_bind,
  208. .unbind = hdac_component_master_unbind,
  209. };
  210. static int hdac_component_master_match(struct device *dev, void *data)
  211. {
  212. /* i915 is the only supported component */
  213. return !strcmp(dev->driver->name, "i915");
  214. }
  215. /**
  216. * snd_hdac_i915_register_notifier - Register i915 audio component ops
  217. * @aops: i915 audio component ops
  218. *
  219. * This function is supposed to be used only by a HD-audio controller
  220. * driver that needs the interaction with i915 graphics.
  221. *
  222. * This function sets the given ops to be called by the i915 graphics driver.
  223. *
  224. * Returns zero for success or a negative error code.
  225. */
  226. int snd_hdac_i915_register_notifier(const struct i915_audio_component_audio_ops *aops)
  227. {
  228. if (WARN_ON(!hdac_acomp))
  229. return -ENODEV;
  230. hdac_acomp->audio_ops = aops;
  231. return 0;
  232. }
  233. EXPORT_SYMBOL_GPL(snd_hdac_i915_register_notifier);
  234. /**
  235. * snd_hdac_i915_init - Initialize i915 audio component
  236. * @bus: HDA core bus
  237. *
  238. * This function is supposed to be used only by a HD-audio controller
  239. * driver that needs the interaction with i915 graphics.
  240. *
  241. * This function initializes and sets up the audio component to communicate
  242. * with i915 graphics driver.
  243. *
  244. * Returns zero for success or a negative error code.
  245. */
  246. int snd_hdac_i915_init(struct hdac_bus *bus)
  247. {
  248. struct component_match *match = NULL;
  249. struct device *dev = bus->dev;
  250. struct i915_audio_component *acomp;
  251. int ret;
  252. acomp = kzalloc(sizeof(*acomp), GFP_KERNEL);
  253. if (!acomp)
  254. return -ENOMEM;
  255. bus->audio_component = acomp;
  256. hdac_acomp = acomp;
  257. component_match_add(dev, &match, hdac_component_master_match, bus);
  258. ret = component_master_add_with_match(dev, &hdac_component_master_ops,
  259. match);
  260. if (ret < 0)
  261. goto out_err;
  262. /*
  263. * Atm, we don't support deferring the component binding, so make sure
  264. * i915 is loaded and that the binding successfully completes.
  265. */
  266. request_module("i915");
  267. if (!acomp->ops) {
  268. ret = -ENODEV;
  269. goto out_master_del;
  270. }
  271. dev_dbg(dev, "bound to i915 component master\n");
  272. return 0;
  273. out_master_del:
  274. component_master_del(dev, &hdac_component_master_ops);
  275. out_err:
  276. kfree(acomp);
  277. bus->audio_component = NULL;
  278. dev_info(dev, "failed to add i915 component master (%d)\n", ret);
  279. return ret;
  280. }
  281. EXPORT_SYMBOL_GPL(snd_hdac_i915_init);
  282. /**
  283. * snd_hdac_i915_exit - Finalize i915 audio component
  284. * @bus: HDA core bus
  285. *
  286. * This function is supposed to be used only by a HD-audio controller
  287. * driver that needs the interaction with i915 graphics.
  288. *
  289. * This function releases the i915 audio component that has been used.
  290. *
  291. * Returns zero for success or a negative error code.
  292. */
  293. int snd_hdac_i915_exit(struct hdac_bus *bus)
  294. {
  295. struct device *dev = bus->dev;
  296. struct i915_audio_component *acomp = bus->audio_component;
  297. if (!acomp)
  298. return 0;
  299. WARN_ON(bus->i915_power_refcount);
  300. if (bus->i915_power_refcount > 0 && acomp->ops)
  301. acomp->ops->put_power(acomp->dev);
  302. component_master_del(dev, &hdac_component_master_ops);
  303. kfree(acomp);
  304. bus->audio_component = NULL;
  305. return 0;
  306. }
  307. EXPORT_SYMBOL_GPL(snd_hdac_i915_exit);