hda-codec.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. //
  3. // This file is provided under a dual BSD/GPLv2 license. When using or
  4. // redistributing this file, you may do so under either license.
  5. //
  6. // Copyright(c) 2018 Intel Corporation. All rights reserved.
  7. //
  8. // Authors: Keyon Jie <[email protected]>
  9. //
  10. #include <linux/module.h>
  11. #include <sound/hdaudio_ext.h>
  12. #include <sound/hda_register.h>
  13. #include <sound/hda_codec.h>
  14. #include <sound/hda_i915.h>
  15. #include <sound/sof.h>
  16. #include "../ops.h"
  17. #include "hda.h"
  18. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
  19. #include "../../codecs/hdac_hda.h"
  20. #endif /* CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC */
  21. #define CODEC_PROBE_RETRIES 3
  22. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
  23. #define IDISP_VID_INTEL 0x80860000
  24. /* load the legacy HDA codec driver */
  25. static int request_codec_module(struct hda_codec *codec)
  26. {
  27. #ifdef MODULE
  28. char alias[MODULE_NAME_LEN];
  29. const char *mod = NULL;
  30. switch (codec->probe_id) {
  31. case HDA_CODEC_ID_GENERIC:
  32. #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
  33. mod = "snd-hda-codec-generic";
  34. #endif
  35. break;
  36. default:
  37. snd_hdac_codec_modalias(&codec->core, alias, sizeof(alias));
  38. mod = alias;
  39. break;
  40. }
  41. if (mod) {
  42. dev_dbg(&codec->core.dev, "loading codec module: %s\n", mod);
  43. request_module(mod);
  44. }
  45. #endif /* MODULE */
  46. return device_attach(hda_codec_dev(codec));
  47. }
  48. static int hda_codec_load_module(struct hda_codec *codec)
  49. {
  50. int ret = request_codec_module(codec);
  51. if (ret <= 0) {
  52. codec->probe_id = HDA_CODEC_ID_GENERIC;
  53. ret = request_codec_module(codec);
  54. }
  55. return ret;
  56. }
  57. /* enable controller wake up event for all codecs with jack connectors */
  58. void hda_codec_jack_wake_enable(struct snd_sof_dev *sdev, bool enable)
  59. {
  60. struct hda_bus *hbus = sof_to_hbus(sdev);
  61. struct hdac_bus *bus = sof_to_bus(sdev);
  62. struct hda_codec *codec;
  63. unsigned int mask = 0;
  64. if (enable) {
  65. list_for_each_codec(codec, hbus)
  66. if (codec->jacktbl.used)
  67. mask |= BIT(codec->core.addr);
  68. }
  69. snd_hdac_chip_updatew(bus, WAKEEN, STATESTS_INT_MASK, mask);
  70. }
  71. /* check jack status after resuming from suspend mode */
  72. void hda_codec_jack_check(struct snd_sof_dev *sdev)
  73. {
  74. struct hda_bus *hbus = sof_to_hbus(sdev);
  75. struct hda_codec *codec;
  76. list_for_each_codec(codec, hbus)
  77. /*
  78. * Wake up all jack-detecting codecs regardless whether an event
  79. * has been recorded in STATESTS
  80. */
  81. if (codec->jacktbl.used)
  82. pm_request_resume(&codec->core.dev);
  83. }
  84. #else
  85. void hda_codec_jack_wake_enable(struct snd_sof_dev *sdev, bool enable) {}
  86. void hda_codec_jack_check(struct snd_sof_dev *sdev) {}
  87. #endif /* CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC */
  88. EXPORT_SYMBOL_NS(hda_codec_jack_wake_enable, SND_SOC_SOF_HDA_AUDIO_CODEC);
  89. EXPORT_SYMBOL_NS(hda_codec_jack_check, SND_SOC_SOF_HDA_AUDIO_CODEC);
  90. #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
  91. #define is_generic_config(bus) \
  92. ((bus)->modelname && !strcmp((bus)->modelname, "generic"))
  93. #else
  94. #define is_generic_config(x) 0
  95. #endif
  96. static struct hda_codec *hda_codec_device_init(struct hdac_bus *bus, int addr, int type)
  97. {
  98. struct hda_codec *codec;
  99. int ret;
  100. codec = snd_hda_codec_device_init(to_hda_bus(bus), addr, "ehdaudio%dD%d", bus->idx, addr);
  101. if (IS_ERR(codec)) {
  102. dev_err(bus->dev, "device init failed for hdac device\n");
  103. return codec;
  104. }
  105. codec->core.type = type;
  106. ret = snd_hdac_device_register(&codec->core);
  107. if (ret) {
  108. dev_err(bus->dev, "failed to register hdac device\n");
  109. put_device(&codec->core.dev);
  110. return ERR_PTR(ret);
  111. }
  112. return codec;
  113. }
  114. /* probe individual codec */
  115. static int hda_codec_probe(struct snd_sof_dev *sdev, int address,
  116. bool hda_codec_use_common_hdmi)
  117. {
  118. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
  119. struct hdac_hda_priv *hda_priv;
  120. int type = HDA_DEV_LEGACY;
  121. #endif
  122. struct hda_bus *hbus = sof_to_hbus(sdev);
  123. struct hda_codec *codec;
  124. u32 hda_cmd = (address << 28) | (AC_NODE_ROOT << 20) |
  125. (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
  126. u32 resp = -1;
  127. int ret, retry = 0;
  128. do {
  129. mutex_lock(&hbus->core.cmd_mutex);
  130. snd_hdac_bus_send_cmd(&hbus->core, hda_cmd);
  131. snd_hdac_bus_get_response(&hbus->core, address, &resp);
  132. mutex_unlock(&hbus->core.cmd_mutex);
  133. } while (resp == -1 && retry++ < CODEC_PROBE_RETRIES);
  134. if (resp == -1)
  135. return -EIO;
  136. dev_dbg(sdev->dev, "HDA codec #%d probed OK: response: %x\n",
  137. address, resp);
  138. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
  139. hda_priv = devm_kzalloc(sdev->dev, sizeof(*hda_priv), GFP_KERNEL);
  140. if (!hda_priv)
  141. return -ENOMEM;
  142. /* only probe ASoC codec drivers for HDAC-HDMI */
  143. if (!hda_codec_use_common_hdmi && (resp & 0xFFFF0000) == IDISP_VID_INTEL)
  144. type = HDA_DEV_ASOC;
  145. codec = hda_codec_device_init(&hbus->core, address, type);
  146. ret = PTR_ERR_OR_ZERO(codec);
  147. if (ret < 0)
  148. return ret;
  149. hda_priv->codec = codec;
  150. dev_set_drvdata(&codec->core.dev, hda_priv);
  151. if ((resp & 0xFFFF0000) == IDISP_VID_INTEL) {
  152. if (!hbus->core.audio_component) {
  153. dev_dbg(sdev->dev,
  154. "iDisp hw present but no driver\n");
  155. ret = -ENOENT;
  156. goto out;
  157. }
  158. hda_priv->need_display_power = true;
  159. }
  160. if (is_generic_config(hbus))
  161. codec->probe_id = HDA_CODEC_ID_GENERIC;
  162. else
  163. codec->probe_id = 0;
  164. if (type == HDA_DEV_LEGACY) {
  165. ret = hda_codec_load_module(codec);
  166. /*
  167. * handle ret==0 (no driver bound) as an error, but pass
  168. * other return codes without modification
  169. */
  170. if (ret == 0)
  171. ret = -ENOENT;
  172. }
  173. out:
  174. if (ret < 0) {
  175. snd_hdac_device_unregister(&codec->core);
  176. put_device(&codec->core.dev);
  177. }
  178. #else
  179. codec = hda_codec_device_init(&hbus->core, address, HDA_DEV_ASOC);
  180. ret = PTR_ERR_OR_ZERO(codec);
  181. #endif
  182. return ret;
  183. }
  184. /* Codec initialization */
  185. void hda_codec_probe_bus(struct snd_sof_dev *sdev,
  186. bool hda_codec_use_common_hdmi)
  187. {
  188. struct hdac_bus *bus = sof_to_bus(sdev);
  189. int i, ret;
  190. /* probe codecs in avail slots */
  191. for (i = 0; i < HDA_MAX_CODECS; i++) {
  192. if (!(bus->codec_mask & (1 << i)))
  193. continue;
  194. ret = hda_codec_probe(sdev, i, hda_codec_use_common_hdmi);
  195. if (ret < 0) {
  196. dev_warn(bus->dev, "codec #%d probe error, ret: %d\n",
  197. i, ret);
  198. bus->codec_mask &= ~BIT(i);
  199. }
  200. }
  201. }
  202. EXPORT_SYMBOL_NS(hda_codec_probe_bus, SND_SOC_SOF_HDA_AUDIO_CODEC);
  203. #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI) || \
  204. IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)
  205. void hda_codec_i915_display_power(struct snd_sof_dev *sdev, bool enable)
  206. {
  207. struct hdac_bus *bus = sof_to_bus(sdev);
  208. if (HDA_IDISP_CODEC(bus->codec_mask)) {
  209. dev_dbg(bus->dev, "Turning i915 HDAC power %d\n", enable);
  210. snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, enable);
  211. }
  212. }
  213. EXPORT_SYMBOL_NS(hda_codec_i915_display_power, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
  214. int hda_codec_i915_init(struct snd_sof_dev *sdev)
  215. {
  216. struct hdac_bus *bus = sof_to_bus(sdev);
  217. int ret;
  218. /* i915 exposes a HDA codec for HDMI audio */
  219. ret = snd_hdac_i915_init(bus);
  220. if (ret < 0)
  221. return ret;
  222. /* codec_mask not yet known, power up for probe */
  223. snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, true);
  224. return 0;
  225. }
  226. EXPORT_SYMBOL_NS(hda_codec_i915_init, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
  227. int hda_codec_i915_exit(struct snd_sof_dev *sdev)
  228. {
  229. struct hdac_bus *bus = sof_to_bus(sdev);
  230. if (!bus->audio_component)
  231. return 0;
  232. /* power down unconditionally */
  233. snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
  234. return snd_hdac_i915_exit(bus);
  235. }
  236. EXPORT_SYMBOL_NS(hda_codec_i915_exit, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
  237. #endif
  238. MODULE_LICENSE("Dual BSD/GPL");