hdac_i915.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. static int hdac_component_master_bind(struct device *dev)
  105. {
  106. struct i915_audio_component *acomp = hdac_acomp;
  107. int ret;
  108. ret = component_bind_all(dev, acomp);
  109. if (ret < 0)
  110. return ret;
  111. if (WARN_ON(!(acomp->dev && acomp->ops && acomp->ops->get_power &&
  112. acomp->ops->put_power && acomp->ops->get_cdclk_freq))) {
  113. ret = -EINVAL;
  114. goto out_unbind;
  115. }
  116. /*
  117. * Atm, we don't support dynamic unbinding initiated by the child
  118. * component, so pin its containing module until we unbind.
  119. */
  120. if (!try_module_get(acomp->ops->owner)) {
  121. ret = -ENODEV;
  122. goto out_unbind;
  123. }
  124. return 0;
  125. out_unbind:
  126. component_unbind_all(dev, acomp);
  127. return ret;
  128. }
  129. static void hdac_component_master_unbind(struct device *dev)
  130. {
  131. struct i915_audio_component *acomp = hdac_acomp;
  132. module_put(acomp->ops->owner);
  133. component_unbind_all(dev, acomp);
  134. WARN_ON(acomp->ops || acomp->dev);
  135. }
  136. static const struct component_master_ops hdac_component_master_ops = {
  137. .bind = hdac_component_master_bind,
  138. .unbind = hdac_component_master_unbind,
  139. };
  140. static int hdac_component_master_match(struct device *dev, void *data)
  141. {
  142. /* i915 is the only supported component */
  143. return !strcmp(dev->driver->name, "i915");
  144. }
  145. /**
  146. * snd_hdac_i915_register_notifier - Register i915 audio component ops
  147. * @aops: i915 audio component ops
  148. *
  149. * This function is supposed to be used only by a HD-audio controller
  150. * driver that needs the interaction with i915 graphics.
  151. *
  152. * This function sets the given ops to be called by the i915 graphics driver.
  153. *
  154. * Returns zero for success or a negative error code.
  155. */
  156. int snd_hdac_i915_register_notifier(const struct i915_audio_component_audio_ops *aops)
  157. {
  158. if (!hdac_acomp)
  159. return -ENODEV;
  160. hdac_acomp->audio_ops = aops;
  161. return 0;
  162. }
  163. EXPORT_SYMBOL_GPL(snd_hdac_i915_register_notifier);
  164. /**
  165. * snd_hdac_i915_init - Initialize i915 audio component
  166. * @bus: HDA core bus
  167. *
  168. * This function is supposed to be used only by a HD-audio controller
  169. * driver that needs the interaction with i915 graphics.
  170. *
  171. * This function initializes and sets up the audio component to communicate
  172. * with i915 graphics driver.
  173. *
  174. * Returns zero for success or a negative error code.
  175. */
  176. int snd_hdac_i915_init(struct hdac_bus *bus)
  177. {
  178. struct component_match *match = NULL;
  179. struct device *dev = bus->dev;
  180. struct i915_audio_component *acomp;
  181. int ret;
  182. acomp = kzalloc(sizeof(*acomp), GFP_KERNEL);
  183. if (!acomp)
  184. return -ENOMEM;
  185. bus->audio_component = acomp;
  186. hdac_acomp = acomp;
  187. component_match_add(dev, &match, hdac_component_master_match, bus);
  188. ret = component_master_add_with_match(dev, &hdac_component_master_ops,
  189. match);
  190. if (ret < 0)
  191. goto out_err;
  192. /*
  193. * Atm, we don't support deferring the component binding, so make sure
  194. * i915 is loaded and that the binding successfully completes.
  195. */
  196. request_module("i915");
  197. if (!acomp->ops) {
  198. ret = -ENODEV;
  199. goto out_master_del;
  200. }
  201. dev_dbg(dev, "bound to i915 component master\n");
  202. return 0;
  203. out_master_del:
  204. component_master_del(dev, &hdac_component_master_ops);
  205. out_err:
  206. kfree(acomp);
  207. bus->audio_component = NULL;
  208. hdac_acomp = NULL;
  209. dev_info(dev, "failed to add i915 component master (%d)\n", ret);
  210. return ret;
  211. }
  212. EXPORT_SYMBOL_GPL(snd_hdac_i915_init);
  213. /**
  214. * snd_hdac_i915_exit - Finalize i915 audio component
  215. * @bus: HDA core bus
  216. *
  217. * This function is supposed to be used only by a HD-audio controller
  218. * driver that needs the interaction with i915 graphics.
  219. *
  220. * This function releases the i915 audio component that has been used.
  221. *
  222. * Returns zero for success or a negative error code.
  223. */
  224. int snd_hdac_i915_exit(struct hdac_bus *bus)
  225. {
  226. struct device *dev = bus->dev;
  227. struct i915_audio_component *acomp = bus->audio_component;
  228. if (!acomp)
  229. return 0;
  230. WARN_ON(bus->i915_power_refcount);
  231. if (bus->i915_power_refcount > 0 && acomp->ops)
  232. acomp->ops->put_power(acomp->dev);
  233. component_master_del(dev, &hdac_component_master_ops);
  234. kfree(acomp);
  235. bus->audio_component = NULL;
  236. hdac_acomp = NULL;
  237. return 0;
  238. }
  239. EXPORT_SYMBOL_GPL(snd_hdac_i915_exit);