hdac_component.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. * @idx: HDA codec address, pass HDA_CODEC_IDX_CONTROLLER for controller
  48. * @enable: power up or down
  49. *
  50. * This function is used by either HD-audio controller or codec driver that
  51. * needs the interaction with graphics driver.
  52. *
  53. * This function updates the power status, and calls the get_power() and
  54. * put_power() ops accordingly, toggling the codec wakeup, too.
  55. */
  56. void snd_hdac_display_power(struct hdac_bus *bus, unsigned int idx, bool enable)
  57. {
  58. struct drm_audio_component *acomp = bus->audio_component;
  59. dev_dbg(bus->dev, "display power %s\n",
  60. enable ? "enable" : "disable");
  61. mutex_lock(&bus->lock);
  62. if (enable)
  63. set_bit(idx, &bus->display_power_status);
  64. else
  65. clear_bit(idx, &bus->display_power_status);
  66. if (!acomp || !acomp->ops)
  67. goto unlock;
  68. if (bus->display_power_status) {
  69. if (!bus->display_power_active) {
  70. unsigned long cookie = -1;
  71. if (acomp->ops->get_power)
  72. cookie = acomp->ops->get_power(acomp->dev);
  73. snd_hdac_set_codec_wakeup(bus, true);
  74. snd_hdac_set_codec_wakeup(bus, false);
  75. bus->display_power_active = cookie;
  76. }
  77. } else {
  78. if (bus->display_power_active) {
  79. unsigned long cookie = bus->display_power_active;
  80. if (acomp->ops->put_power)
  81. acomp->ops->put_power(acomp->dev, cookie);
  82. bus->display_power_active = 0;
  83. }
  84. }
  85. unlock:
  86. mutex_unlock(&bus->lock);
  87. }
  88. EXPORT_SYMBOL_GPL(snd_hdac_display_power);
  89. /**
  90. * snd_hdac_sync_audio_rate - Set N/CTS based on the sample rate
  91. * @codec: HDA codec
  92. * @nid: the pin widget NID
  93. * @dev_id: device identifier
  94. * @rate: the sample rate to set
  95. *
  96. * This function is supposed to be used only by a HD-audio controller
  97. * driver that needs the interaction with graphics driver.
  98. *
  99. * This function sets N/CTS value based on the given sample rate.
  100. * Returns zero for success, or a negative error code.
  101. */
  102. int snd_hdac_sync_audio_rate(struct hdac_device *codec, hda_nid_t nid,
  103. int dev_id, int rate)
  104. {
  105. struct hdac_bus *bus = codec->bus;
  106. struct drm_audio_component *acomp = bus->audio_component;
  107. int port, pipe;
  108. if (!acomp || !acomp->ops || !acomp->ops->sync_audio_rate)
  109. return -ENODEV;
  110. port = nid;
  111. if (acomp->audio_ops && acomp->audio_ops->pin2port) {
  112. port = acomp->audio_ops->pin2port(codec, nid);
  113. if (port < 0)
  114. return -EINVAL;
  115. }
  116. pipe = dev_id;
  117. return acomp->ops->sync_audio_rate(acomp->dev, port, pipe, rate);
  118. }
  119. EXPORT_SYMBOL_GPL(snd_hdac_sync_audio_rate);
  120. /**
  121. * snd_hdac_acomp_get_eld - Get the audio state and ELD via component
  122. * @codec: HDA codec
  123. * @nid: the pin widget NID
  124. * @dev_id: device identifier
  125. * @audio_enabled: the pointer to store the current audio state
  126. * @buffer: the buffer pointer to store ELD bytes
  127. * @max_bytes: the max bytes to be stored on @buffer
  128. *
  129. * This function is supposed to be used only by a HD-audio controller
  130. * driver that needs the interaction with graphics driver.
  131. *
  132. * This function queries the current state of the audio on the given
  133. * digital port and fetches the ELD bytes onto the given buffer.
  134. * It returns the number of bytes for the total ELD data, zero for
  135. * invalid ELD, or a negative error code.
  136. *
  137. * The return size is the total bytes required for the whole ELD bytes,
  138. * thus it may be over @max_bytes. If it's over @max_bytes, it implies
  139. * that only a part of ELD bytes have been fetched.
  140. */
  141. int snd_hdac_acomp_get_eld(struct hdac_device *codec, hda_nid_t nid, int dev_id,
  142. bool *audio_enabled, char *buffer, int max_bytes)
  143. {
  144. struct hdac_bus *bus = codec->bus;
  145. struct drm_audio_component *acomp = bus->audio_component;
  146. int port, pipe;
  147. if (!acomp || !acomp->ops || !acomp->ops->get_eld)
  148. return -ENODEV;
  149. port = nid;
  150. if (acomp->audio_ops && acomp->audio_ops->pin2port) {
  151. port = acomp->audio_ops->pin2port(codec, nid);
  152. if (port < 0)
  153. return -EINVAL;
  154. }
  155. pipe = dev_id;
  156. return acomp->ops->get_eld(acomp->dev, port, pipe, audio_enabled,
  157. buffer, max_bytes);
  158. }
  159. EXPORT_SYMBOL_GPL(snd_hdac_acomp_get_eld);
  160. static int hdac_component_master_bind(struct device *dev)
  161. {
  162. struct drm_audio_component *acomp = hdac_get_acomp(dev);
  163. int ret;
  164. if (WARN_ON(!acomp))
  165. return -EINVAL;
  166. ret = component_bind_all(dev, acomp);
  167. if (ret < 0)
  168. return ret;
  169. if (WARN_ON(!(acomp->dev && acomp->ops))) {
  170. ret = -EINVAL;
  171. goto out_unbind;
  172. }
  173. /* pin the module to avoid dynamic unbinding, but only if given */
  174. if (!try_module_get(acomp->ops->owner)) {
  175. ret = -ENODEV;
  176. goto out_unbind;
  177. }
  178. if (acomp->audio_ops && acomp->audio_ops->master_bind) {
  179. ret = acomp->audio_ops->master_bind(dev, acomp);
  180. if (ret < 0)
  181. goto module_put;
  182. }
  183. complete_all(&acomp->master_bind_complete);
  184. return 0;
  185. module_put:
  186. module_put(acomp->ops->owner);
  187. out_unbind:
  188. component_unbind_all(dev, acomp);
  189. complete_all(&acomp->master_bind_complete);
  190. return ret;
  191. }
  192. static void hdac_component_master_unbind(struct device *dev)
  193. {
  194. struct drm_audio_component *acomp = hdac_get_acomp(dev);
  195. if (acomp->audio_ops && acomp->audio_ops->master_unbind)
  196. acomp->audio_ops->master_unbind(dev, acomp);
  197. module_put(acomp->ops->owner);
  198. component_unbind_all(dev, acomp);
  199. WARN_ON(acomp->ops || acomp->dev);
  200. }
  201. static const struct component_master_ops hdac_component_master_ops = {
  202. .bind = hdac_component_master_bind,
  203. .unbind = hdac_component_master_unbind,
  204. };
  205. /**
  206. * snd_hdac_acomp_register_notifier - Register audio component ops
  207. * @bus: HDA core bus
  208. * @aops: audio component ops
  209. *
  210. * This function is supposed to be used only by a HD-audio controller
  211. * driver that needs the interaction with graphics driver.
  212. *
  213. * This function sets the given ops to be called by the graphics driver.
  214. *
  215. * Returns zero for success or a negative error code.
  216. */
  217. int snd_hdac_acomp_register_notifier(struct hdac_bus *bus,
  218. const struct drm_audio_component_audio_ops *aops)
  219. {
  220. if (!bus->audio_component)
  221. return -ENODEV;
  222. bus->audio_component->audio_ops = aops;
  223. return 0;
  224. }
  225. EXPORT_SYMBOL_GPL(snd_hdac_acomp_register_notifier);
  226. /**
  227. * snd_hdac_acomp_init - Initialize audio component
  228. * @bus: HDA core bus
  229. * @aops: audio component ops
  230. * @match_master: match function for finding components
  231. * @extra_size: Extra bytes to allocate
  232. *
  233. * This function is supposed to be used only by a HD-audio controller
  234. * driver that needs the interaction with graphics driver.
  235. *
  236. * This function initializes and sets up the audio component to communicate
  237. * with graphics driver.
  238. *
  239. * Unlike snd_hdac_i915_init(), this function doesn't synchronize with the
  240. * binding with the DRM component. Each caller needs to sync via master_bind
  241. * audio_ops.
  242. *
  243. * Returns zero for success or a negative error code.
  244. */
  245. int snd_hdac_acomp_init(struct hdac_bus *bus,
  246. const struct drm_audio_component_audio_ops *aops,
  247. int (*match_master)(struct device *, int, void *),
  248. size_t extra_size)
  249. {
  250. struct component_match *match = NULL;
  251. struct device *dev = bus->dev;
  252. struct drm_audio_component *acomp;
  253. int ret;
  254. if (WARN_ON(hdac_get_acomp(dev)))
  255. return -EBUSY;
  256. acomp = devres_alloc(hdac_acomp_release, sizeof(*acomp) + extra_size,
  257. GFP_KERNEL);
  258. if (!acomp)
  259. return -ENOMEM;
  260. acomp->audio_ops = aops;
  261. init_completion(&acomp->master_bind_complete);
  262. bus->audio_component = acomp;
  263. devres_add(dev, acomp);
  264. component_match_add_typed(dev, &match, match_master, bus);
  265. ret = component_master_add_with_match(dev, &hdac_component_master_ops,
  266. match);
  267. if (ret < 0)
  268. goto out_err;
  269. return 0;
  270. out_err:
  271. bus->audio_component = NULL;
  272. devres_destroy(dev, hdac_acomp_release, NULL, NULL);
  273. dev_info(dev, "failed to add audio component master (%d)\n", ret);
  274. return ret;
  275. }
  276. EXPORT_SYMBOL_GPL(snd_hdac_acomp_init);
  277. /**
  278. * snd_hdac_acomp_exit - Finalize audio component
  279. * @bus: HDA core bus
  280. *
  281. * This function is supposed to be used only by a HD-audio controller
  282. * driver that needs the interaction with graphics driver.
  283. *
  284. * This function releases the audio component that has been used.
  285. *
  286. * Returns zero for success or a negative error code.
  287. */
  288. int snd_hdac_acomp_exit(struct hdac_bus *bus)
  289. {
  290. struct device *dev = bus->dev;
  291. struct drm_audio_component *acomp = bus->audio_component;
  292. if (!acomp)
  293. return 0;
  294. if (WARN_ON(bus->display_power_active) && acomp->ops)
  295. acomp->ops->put_power(acomp->dev, bus->display_power_active);
  296. bus->display_power_active = 0;
  297. bus->display_power_status = 0;
  298. component_master_del(dev, &hdac_component_master_ops);
  299. bus->audio_component = NULL;
  300. devres_destroy(dev, hdac_acomp_release, NULL, NULL);
  301. return 0;
  302. }
  303. EXPORT_SYMBOL_GPL(snd_hdac_acomp_exit);