hdac_component.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // SPDX-License-Identifier: GPL-2.0
  2. // hdac_component.c - routines for sync between HD-A core and DRM driver
  3. #include <linux/init.h>
  4. #include <linux/module.h>
  5. #include <linux/pci.h>
  6. #include <linux/component.h>
  7. #include <sound/core.h>
  8. #include <sound/hdaudio.h>
  9. #include <sound/hda_component.h>
  10. #include <sound/hda_register.h>
  11. static void hdac_acomp_release(struct device *dev, void *res)
  12. {
  13. }
  14. static struct drm_audio_component *hdac_get_acomp(struct device *dev)
  15. {
  16. return devres_find(dev, hdac_acomp_release, NULL, NULL);
  17. }
  18. /**
  19. * snd_hdac_set_codec_wakeup - Enable / disable HDMI/DP codec wakeup
  20. * @bus: HDA core bus
  21. * @enable: enable or disable the wakeup
  22. *
  23. * This function is supposed to be used only by a HD-audio controller
  24. * driver that needs the interaction with graphics driver.
  25. *
  26. * This function should be called during the chip reset, also called at
  27. * resume for updating STATESTS register read.
  28. *
  29. * Returns zero for success or a negative error code.
  30. */
  31. int snd_hdac_set_codec_wakeup(struct hdac_bus *bus, bool enable)
  32. {
  33. struct drm_audio_component *acomp = bus->audio_component;
  34. if (!acomp || !acomp->ops)
  35. return -ENODEV;
  36. if (!acomp->ops->codec_wake_override)
  37. return 0;
  38. dev_dbg(bus->dev, "%s codec wakeup\n",
  39. enable ? "enable" : "disable");
  40. acomp->ops->codec_wake_override(acomp->dev, enable);
  41. return 0;
  42. }
  43. EXPORT_SYMBOL_GPL(snd_hdac_set_codec_wakeup);
  44. /**
  45. * snd_hdac_display_power - Power up / down the power refcount
  46. * @bus: HDA core bus
  47. * @enable: power up or down
  48. *
  49. * This function is supposed to be used only by a HD-audio controller
  50. * driver that needs the interaction with graphics driver.
  51. *
  52. * This function manages a refcount and calls the get_power() and
  53. * put_power() ops accordingly, toggling the codec wakeup, too.
  54. *
  55. * Returns zero for success or a negative error code.
  56. */
  57. int snd_hdac_display_power(struct hdac_bus *bus, bool enable)
  58. {
  59. struct drm_audio_component *acomp = bus->audio_component;
  60. if (!acomp || !acomp->ops)
  61. return -ENODEV;
  62. dev_dbg(bus->dev, "display power %s\n",
  63. enable ? "enable" : "disable");
  64. if (enable) {
  65. if (!bus->drm_power_refcount++) {
  66. if (acomp->ops->get_power)
  67. acomp->ops->get_power(acomp->dev);
  68. snd_hdac_set_codec_wakeup(bus, true);
  69. snd_hdac_set_codec_wakeup(bus, false);
  70. }
  71. } else {
  72. WARN_ON(!bus->drm_power_refcount);
  73. if (!--bus->drm_power_refcount)
  74. if (acomp->ops->put_power)
  75. acomp->ops->put_power(acomp->dev);
  76. }
  77. return 0;
  78. }
  79. EXPORT_SYMBOL_GPL(snd_hdac_display_power);
  80. /**
  81. * snd_hdac_sync_audio_rate - Set N/CTS based on the sample rate
  82. * @codec: HDA codec
  83. * @nid: the pin widget NID
  84. * @dev_id: device identifier
  85. * @rate: the sample rate to set
  86. *
  87. * This function is supposed to be used only by a HD-audio controller
  88. * driver that needs the interaction with graphics driver.
  89. *
  90. * This function sets N/CTS value based on the given sample rate.
  91. * Returns zero for success, or a negative error code.
  92. */
  93. int snd_hdac_sync_audio_rate(struct hdac_device *codec, hda_nid_t nid,
  94. int dev_id, int rate)
  95. {
  96. struct hdac_bus *bus = codec->bus;
  97. struct drm_audio_component *acomp = bus->audio_component;
  98. int port, pipe;
  99. if (!acomp || !acomp->ops || !acomp->ops->sync_audio_rate)
  100. return -ENODEV;
  101. port = nid;
  102. if (acomp->audio_ops && acomp->audio_ops->pin2port) {
  103. port = acomp->audio_ops->pin2port(codec, nid);
  104. if (port < 0)
  105. return -EINVAL;
  106. }
  107. pipe = dev_id;
  108. return acomp->ops->sync_audio_rate(acomp->dev, port, pipe, rate);
  109. }
  110. EXPORT_SYMBOL_GPL(snd_hdac_sync_audio_rate);
  111. /**
  112. * snd_hdac_acomp_get_eld - Get the audio state and ELD via component
  113. * @codec: HDA codec
  114. * @nid: the pin widget NID
  115. * @dev_id: device identifier
  116. * @audio_enabled: the pointer to store the current audio state
  117. * @buffer: the buffer pointer to store ELD bytes
  118. * @max_bytes: the max bytes to be stored on @buffer
  119. *
  120. * This function is supposed to be used only by a HD-audio controller
  121. * driver that needs the interaction with graphics driver.
  122. *
  123. * This function queries the current state of the audio on the given
  124. * digital port and fetches the ELD bytes onto the given buffer.
  125. * It returns the number of bytes for the total ELD data, zero for
  126. * invalid ELD, or a negative error code.
  127. *
  128. * The return size is the total bytes required for the whole ELD bytes,
  129. * thus it may be over @max_bytes. If it's over @max_bytes, it implies
  130. * that only a part of ELD bytes have been fetched.
  131. */
  132. int snd_hdac_acomp_get_eld(struct hdac_device *codec, hda_nid_t nid, int dev_id,
  133. bool *audio_enabled, char *buffer, int max_bytes)
  134. {
  135. struct hdac_bus *bus = codec->bus;
  136. struct drm_audio_component *acomp = bus->audio_component;
  137. int port, pipe;
  138. if (!acomp || !acomp->ops || !acomp->ops->get_eld)
  139. return -ENODEV;
  140. port = nid;
  141. if (acomp->audio_ops && acomp->audio_ops->pin2port) {
  142. port = acomp->audio_ops->pin2port(codec, nid);
  143. if (port < 0)
  144. return -EINVAL;
  145. }
  146. pipe = dev_id;
  147. return acomp->ops->get_eld(acomp->dev, port, pipe, audio_enabled,
  148. buffer, max_bytes);
  149. }
  150. EXPORT_SYMBOL_GPL(snd_hdac_acomp_get_eld);
  151. static int hdac_component_master_bind(struct device *dev)
  152. {
  153. struct drm_audio_component *acomp = hdac_get_acomp(dev);
  154. int ret;
  155. if (WARN_ON(!acomp))
  156. return -EINVAL;
  157. ret = component_bind_all(dev, acomp);
  158. if (ret < 0)
  159. return ret;
  160. if (WARN_ON(!(acomp->dev && acomp->ops))) {
  161. ret = -EINVAL;
  162. goto out_unbind;
  163. }
  164. /* pin the module to avoid dynamic unbinding, but only if given */
  165. if (!try_module_get(acomp->ops->owner)) {
  166. ret = -ENODEV;
  167. goto out_unbind;
  168. }
  169. if (acomp->audio_ops && acomp->audio_ops->master_bind) {
  170. ret = acomp->audio_ops->master_bind(dev, acomp);
  171. if (ret < 0)
  172. goto module_put;
  173. }
  174. return 0;
  175. module_put:
  176. module_put(acomp->ops->owner);
  177. out_unbind:
  178. component_unbind_all(dev, acomp);
  179. return ret;
  180. }
  181. static void hdac_component_master_unbind(struct device *dev)
  182. {
  183. struct drm_audio_component *acomp = hdac_get_acomp(dev);
  184. if (acomp->audio_ops && acomp->audio_ops->master_unbind)
  185. acomp->audio_ops->master_unbind(dev, acomp);
  186. module_put(acomp->ops->owner);
  187. component_unbind_all(dev, acomp);
  188. WARN_ON(acomp->ops || acomp->dev);
  189. }
  190. static const struct component_master_ops hdac_component_master_ops = {
  191. .bind = hdac_component_master_bind,
  192. .unbind = hdac_component_master_unbind,
  193. };
  194. /**
  195. * snd_hdac_acomp_register_notifier - Register audio component ops
  196. * @bus: HDA core bus
  197. * @aops: audio component ops
  198. *
  199. * This function is supposed to be used only by a HD-audio controller
  200. * driver that needs the interaction with graphics driver.
  201. *
  202. * This function sets the given ops to be called by the graphics driver.
  203. *
  204. * Returns zero for success or a negative error code.
  205. */
  206. int snd_hdac_acomp_register_notifier(struct hdac_bus *bus,
  207. const struct drm_audio_component_audio_ops *aops)
  208. {
  209. if (!bus->audio_component)
  210. return -ENODEV;
  211. bus->audio_component->audio_ops = aops;
  212. return 0;
  213. }
  214. EXPORT_SYMBOL_GPL(snd_hdac_acomp_register_notifier);
  215. /**
  216. * snd_hdac_acomp_init - Initialize audio component
  217. * @bus: HDA core bus
  218. * @match_master: match function for finding components
  219. * @extra_size: Extra bytes to allocate
  220. *
  221. * This function is supposed to be used only by a HD-audio controller
  222. * driver that needs the interaction with graphics driver.
  223. *
  224. * This function initializes and sets up the audio component to communicate
  225. * with graphics driver.
  226. *
  227. * Unlike snd_hdac_i915_init(), this function doesn't synchronize with the
  228. * binding with the DRM component. Each caller needs to sync via master_bind
  229. * audio_ops.
  230. *
  231. * Returns zero for success or a negative error code.
  232. */
  233. int snd_hdac_acomp_init(struct hdac_bus *bus,
  234. const struct drm_audio_component_audio_ops *aops,
  235. int (*match_master)(struct device *, void *),
  236. size_t extra_size)
  237. {
  238. struct component_match *match = NULL;
  239. struct device *dev = bus->dev;
  240. struct drm_audio_component *acomp;
  241. int ret;
  242. if (WARN_ON(hdac_get_acomp(dev)))
  243. return -EBUSY;
  244. acomp = devres_alloc(hdac_acomp_release, sizeof(*acomp) + extra_size,
  245. GFP_KERNEL);
  246. if (!acomp)
  247. return -ENOMEM;
  248. acomp->audio_ops = aops;
  249. bus->audio_component = acomp;
  250. devres_add(dev, acomp);
  251. component_match_add(dev, &match, match_master, bus);
  252. ret = component_master_add_with_match(dev, &hdac_component_master_ops,
  253. match);
  254. if (ret < 0)
  255. goto out_err;
  256. return 0;
  257. out_err:
  258. bus->audio_component = NULL;
  259. devres_destroy(dev, hdac_acomp_release, NULL, NULL);
  260. dev_info(dev, "failed to add audio component master (%d)\n", ret);
  261. return ret;
  262. }
  263. EXPORT_SYMBOL_GPL(snd_hdac_acomp_init);
  264. /**
  265. * snd_hdac_acomp_exit - Finalize audio component
  266. * @bus: HDA core bus
  267. *
  268. * This function is supposed to be used only by a HD-audio controller
  269. * driver that needs the interaction with graphics driver.
  270. *
  271. * This function releases the audio component that has been used.
  272. *
  273. * Returns zero for success or a negative error code.
  274. */
  275. int snd_hdac_acomp_exit(struct hdac_bus *bus)
  276. {
  277. struct device *dev = bus->dev;
  278. struct drm_audio_component *acomp = bus->audio_component;
  279. if (!acomp)
  280. return 0;
  281. WARN_ON(bus->drm_power_refcount);
  282. if (bus->drm_power_refcount > 0 && acomp->ops)
  283. acomp->ops->put_power(acomp->dev);
  284. component_master_del(dev, &hdac_component_master_ops);
  285. bus->audio_component = NULL;
  286. devres_destroy(dev, hdac_acomp_release, NULL, NULL);
  287. return 0;
  288. }
  289. EXPORT_SYMBOL_GPL(snd_hdac_acomp_exit);