hdac_i915.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 SNB, IVY, HSW, BSW, SKL, BXT, KBL:
  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. * on VLV, ILK:
  111. * Pin Widget 4 - PORT B (port = 1 in i915 driver)
  112. * Pin Widget 5 - PORT C (port = 2 in i915 driver)
  113. * Pin Widget 6 - PORT D (port = 3 in i915 driver)
  114. */
  115. static int pin2port(struct hdac_device *codec, hda_nid_t pin_nid)
  116. {
  117. int base_nid;
  118. switch (codec->vendor_id) {
  119. case 0x80860054: /* ILK */
  120. case 0x80862804: /* ILK */
  121. case 0x80862882: /* VLV */
  122. base_nid = 3;
  123. break;
  124. default:
  125. base_nid = 4;
  126. break;
  127. }
  128. if (WARN_ON(pin_nid <= base_nid || pin_nid > base_nid + 3))
  129. return -1;
  130. return pin_nid - base_nid;
  131. }
  132. /**
  133. * snd_hdac_sync_audio_rate - Set N/CTS based on the sample rate
  134. * @codec: HDA codec
  135. * @nid: the pin widget NID
  136. * @rate: the sample rate to set
  137. *
  138. * This function is supposed to be used only by a HD-audio controller
  139. * driver that needs the interaction with i915 graphics.
  140. *
  141. * This function sets N/CTS value based on the given sample rate.
  142. * Returns zero for success, or a negative error code.
  143. */
  144. int snd_hdac_sync_audio_rate(struct hdac_device *codec, hda_nid_t nid, int rate)
  145. {
  146. struct hdac_bus *bus = codec->bus;
  147. struct i915_audio_component *acomp = bus->audio_component;
  148. int port;
  149. if (!acomp || !acomp->ops || !acomp->ops->sync_audio_rate)
  150. return -ENODEV;
  151. port = pin2port(codec, nid);
  152. if (port < 0)
  153. return -EINVAL;
  154. return acomp->ops->sync_audio_rate(acomp->dev, port, rate);
  155. }
  156. EXPORT_SYMBOL_GPL(snd_hdac_sync_audio_rate);
  157. /**
  158. * snd_hdac_acomp_get_eld - Get the audio state and ELD via component
  159. * @codec: HDA codec
  160. * @nid: the pin widget NID
  161. * @audio_enabled: the pointer to store the current audio state
  162. * @buffer: the buffer pointer to store ELD bytes
  163. * @max_bytes: the max bytes to be stored on @buffer
  164. *
  165. * This function is supposed to be used only by a HD-audio controller
  166. * driver that needs the interaction with i915 graphics.
  167. *
  168. * This function queries the current state of the audio on the given
  169. * digital port and fetches the ELD bytes onto the given buffer.
  170. * It returns the number of bytes for the total ELD data, zero for
  171. * invalid ELD, or a negative error code.
  172. *
  173. * The return size is the total bytes required for the whole ELD bytes,
  174. * thus it may be over @max_bytes. If it's over @max_bytes, it implies
  175. * that only a part of ELD bytes have been fetched.
  176. */
  177. int snd_hdac_acomp_get_eld(struct hdac_device *codec, hda_nid_t nid,
  178. bool *audio_enabled, char *buffer, int max_bytes)
  179. {
  180. struct hdac_bus *bus = codec->bus;
  181. struct i915_audio_component *acomp = bus->audio_component;
  182. int port;
  183. if (!acomp || !acomp->ops || !acomp->ops->get_eld)
  184. return -ENODEV;
  185. port = pin2port(codec, nid);
  186. if (port < 0)
  187. return -EINVAL;
  188. return acomp->ops->get_eld(acomp->dev, port, audio_enabled,
  189. buffer, max_bytes);
  190. }
  191. EXPORT_SYMBOL_GPL(snd_hdac_acomp_get_eld);
  192. static int hdac_component_master_bind(struct device *dev)
  193. {
  194. struct i915_audio_component *acomp = hdac_acomp;
  195. int ret;
  196. ret = component_bind_all(dev, acomp);
  197. if (ret < 0)
  198. return ret;
  199. if (WARN_ON(!(acomp->dev && acomp->ops && acomp->ops->get_power &&
  200. acomp->ops->put_power && acomp->ops->get_cdclk_freq))) {
  201. ret = -EINVAL;
  202. goto out_unbind;
  203. }
  204. /*
  205. * Atm, we don't support dynamic unbinding initiated by the child
  206. * component, so pin its containing module until we unbind.
  207. */
  208. if (!try_module_get(acomp->ops->owner)) {
  209. ret = -ENODEV;
  210. goto out_unbind;
  211. }
  212. return 0;
  213. out_unbind:
  214. component_unbind_all(dev, acomp);
  215. return ret;
  216. }
  217. static void hdac_component_master_unbind(struct device *dev)
  218. {
  219. struct i915_audio_component *acomp = hdac_acomp;
  220. module_put(acomp->ops->owner);
  221. component_unbind_all(dev, acomp);
  222. WARN_ON(acomp->ops || acomp->dev);
  223. }
  224. static const struct component_master_ops hdac_component_master_ops = {
  225. .bind = hdac_component_master_bind,
  226. .unbind = hdac_component_master_unbind,
  227. };
  228. static int hdac_component_master_match(struct device *dev, void *data)
  229. {
  230. /* i915 is the only supported component */
  231. return !strcmp(dev->driver->name, "i915");
  232. }
  233. /**
  234. * snd_hdac_i915_register_notifier - Register i915 audio component ops
  235. * @aops: i915 audio component ops
  236. *
  237. * This function is supposed to be used only by a HD-audio controller
  238. * driver that needs the interaction with i915 graphics.
  239. *
  240. * This function sets the given ops to be called by the i915 graphics driver.
  241. *
  242. * Returns zero for success or a negative error code.
  243. */
  244. int snd_hdac_i915_register_notifier(const struct i915_audio_component_audio_ops *aops)
  245. {
  246. if (WARN_ON(!hdac_acomp))
  247. return -ENODEV;
  248. hdac_acomp->audio_ops = aops;
  249. return 0;
  250. }
  251. EXPORT_SYMBOL_GPL(snd_hdac_i915_register_notifier);
  252. /**
  253. * snd_hdac_i915_init - Initialize i915 audio component
  254. * @bus: HDA core bus
  255. *
  256. * This function is supposed to be used only by a HD-audio controller
  257. * driver that needs the interaction with i915 graphics.
  258. *
  259. * This function initializes and sets up the audio component to communicate
  260. * with i915 graphics driver.
  261. *
  262. * Returns zero for success or a negative error code.
  263. */
  264. int snd_hdac_i915_init(struct hdac_bus *bus)
  265. {
  266. struct component_match *match = NULL;
  267. struct device *dev = bus->dev;
  268. struct i915_audio_component *acomp;
  269. int ret;
  270. if (WARN_ON(hdac_acomp))
  271. return -EBUSY;
  272. acomp = kzalloc(sizeof(*acomp), GFP_KERNEL);
  273. if (!acomp)
  274. return -ENOMEM;
  275. bus->audio_component = acomp;
  276. hdac_acomp = acomp;
  277. component_match_add(dev, &match, hdac_component_master_match, bus);
  278. ret = component_master_add_with_match(dev, &hdac_component_master_ops,
  279. match);
  280. if (ret < 0)
  281. goto out_err;
  282. /*
  283. * Atm, we don't support deferring the component binding, so make sure
  284. * i915 is loaded and that the binding successfully completes.
  285. */
  286. request_module("i915");
  287. if (!acomp->ops) {
  288. ret = -ENODEV;
  289. goto out_master_del;
  290. }
  291. dev_dbg(dev, "bound to i915 component master\n");
  292. return 0;
  293. out_master_del:
  294. component_master_del(dev, &hdac_component_master_ops);
  295. out_err:
  296. kfree(acomp);
  297. bus->audio_component = NULL;
  298. hdac_acomp = NULL;
  299. dev_info(dev, "failed to add i915 component master (%d)\n", ret);
  300. return ret;
  301. }
  302. EXPORT_SYMBOL_GPL(snd_hdac_i915_init);
  303. /**
  304. * snd_hdac_i915_exit - Finalize 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 releases the i915 audio component that has been used.
  311. *
  312. * Returns zero for success or a negative error code.
  313. */
  314. int snd_hdac_i915_exit(struct hdac_bus *bus)
  315. {
  316. struct device *dev = bus->dev;
  317. struct i915_audio_component *acomp = bus->audio_component;
  318. if (!acomp)
  319. return 0;
  320. WARN_ON(bus->i915_power_refcount);
  321. if (bus->i915_power_refcount > 0 && acomp->ops)
  322. acomp->ops->put_power(acomp->dev);
  323. component_master_del(dev, &hdac_component_master_ops);
  324. kfree(acomp);
  325. bus->audio_component = NULL;
  326. hdac_acomp = NULL;
  327. return 0;
  328. }
  329. EXPORT_SYMBOL_GPL(snd_hdac_i915_exit);