hda-codec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Copyright(c) 2018 Intel Corporation
  4. //
  5. // Authors: Keyon Jie <yang.jie@linux.intel.com>
  6. //
  7. #include <linux/module.h>
  8. #include <sound/hdaudio_ext.h>
  9. #include <sound/hda_register.h>
  10. #include <sound/hda_codec.h>
  11. #include <sound/hda_i915.h>
  12. #include <sound/sof.h>
  13. #include "../ops.h"
  14. #include "hda.h"
  15. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
  16. #include "../../codecs/hdac_hda.h"
  17. #define CODEC_PROBE_RETRIES 3
  18. #define IDISP_VID_INTEL 0x80860000
  19. static int hda_codec_mask = -1;
  20. module_param_named(codec_mask, hda_codec_mask, int, 0444);
  21. MODULE_PARM_DESC(codec_mask, "SOF HDA codec mask for probing");
  22. /* load the legacy HDA codec driver */
  23. static int request_codec_module(struct hda_codec *codec)
  24. {
  25. #ifdef MODULE
  26. char alias[MODULE_NAME_LEN];
  27. const char *mod = NULL;
  28. switch (codec->probe_id) {
  29. case HDA_CODEC_ID_GENERIC:
  30. #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
  31. mod = "snd-hda-codec-generic";
  32. #endif
  33. break;
  34. default:
  35. snd_hdac_codec_modalias(&codec->core, alias, sizeof(alias));
  36. mod = alias;
  37. break;
  38. }
  39. if (mod) {
  40. dev_dbg(&codec->core.dev, "loading codec module: %s\n", mod);
  41. request_module(mod);
  42. }
  43. #endif /* MODULE */
  44. return device_attach(hda_codec_dev(codec));
  45. }
  46. static int hda_codec_load_module(struct hda_codec *codec)
  47. {
  48. int ret;
  49. ret = snd_hdac_device_register(&codec->core);
  50. if (ret) {
  51. dev_err(&codec->core.dev, "failed to register hdac device\n");
  52. put_device(&codec->core.dev);
  53. return ret;
  54. }
  55. ret = request_codec_module(codec);
  56. if (ret <= 0) {
  57. codec->probe_id = HDA_CODEC_ID_GENERIC;
  58. ret = request_codec_module(codec);
  59. }
  60. return ret;
  61. }
  62. /* enable controller wake up event for all codecs with jack connectors */
  63. void hda_codec_jack_wake_enable(struct snd_sof_dev *sdev, bool enable)
  64. {
  65. struct hda_bus *hbus = sof_to_hbus(sdev);
  66. struct hdac_bus *bus = sof_to_bus(sdev);
  67. struct hda_codec *codec;
  68. unsigned int mask = 0;
  69. unsigned int val = 0;
  70. if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
  71. sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
  72. return;
  73. if (enable) {
  74. list_for_each_codec(codec, hbus) {
  75. /* only set WAKEEN when needed for HDaudio codecs */
  76. mask |= BIT(codec->core.addr);
  77. if (codec->jacktbl.used)
  78. val |= BIT(codec->core.addr);
  79. }
  80. } else {
  81. list_for_each_codec(codec, hbus) {
  82. /* reset WAKEEN only HDaudio codecs */
  83. mask |= BIT(codec->core.addr);
  84. }
  85. }
  86. snd_hdac_chip_updatew(bus, WAKEEN, mask & STATESTS_INT_MASK, val);
  87. }
  88. EXPORT_SYMBOL_NS_GPL(hda_codec_jack_wake_enable, SND_SOC_SOF_HDA_AUDIO_CODEC);
  89. /* check jack status after resuming from suspend mode */
  90. void hda_codec_jack_check(struct snd_sof_dev *sdev)
  91. {
  92. struct hda_bus *hbus = sof_to_hbus(sdev);
  93. struct hda_codec *codec;
  94. if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
  95. sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
  96. return;
  97. list_for_each_codec(codec, hbus)
  98. /*
  99. * Wake up all jack-detecting codecs regardless whether an event
  100. * has been recorded in STATESTS
  101. */
  102. if (codec->jacktbl.used)
  103. pm_request_resume(&codec->core.dev);
  104. }
  105. EXPORT_SYMBOL_NS_GPL(hda_codec_jack_check, SND_SOC_SOF_HDA_AUDIO_CODEC);
  106. #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
  107. #define is_generic_config(bus) \
  108. ((bus)->modelname && !strcmp((bus)->modelname, "generic"))
  109. #else
  110. #define is_generic_config(x) 0
  111. #endif
  112. static struct hda_codec *hda_codec_device_init(struct hdac_bus *bus, int addr, int type)
  113. {
  114. struct hda_codec *codec;
  115. codec = snd_hda_codec_device_init(to_hda_bus(bus), addr, "ehdaudio%dD%d", bus->idx, addr);
  116. if (IS_ERR(codec)) {
  117. dev_err(bus->dev, "device init failed for hdac device\n");
  118. return codec;
  119. }
  120. codec->core.type = type;
  121. return codec;
  122. }
  123. /* probe individual codec */
  124. static int hda_codec_probe(struct snd_sof_dev *sdev, int address)
  125. {
  126. struct hdac_hda_priv *hda_priv;
  127. struct hda_bus *hbus = sof_to_hbus(sdev);
  128. struct hda_codec *codec;
  129. u32 hda_cmd = (address << 28) | (AC_NODE_ROOT << 20) |
  130. (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
  131. u32 resp = -1;
  132. int ret, retry = 0;
  133. do {
  134. mutex_lock(&hbus->core.cmd_mutex);
  135. snd_hdac_bus_send_cmd(&hbus->core, hda_cmd);
  136. snd_hdac_bus_get_response(&hbus->core, address, &resp);
  137. mutex_unlock(&hbus->core.cmd_mutex);
  138. } while (resp == -1 && retry++ < CODEC_PROBE_RETRIES);
  139. if (resp == -1)
  140. return -EIO;
  141. dev_dbg(sdev->dev, "HDA codec #%d probed OK: response: %x\n",
  142. address, resp);
  143. hda_priv = devm_kzalloc(sdev->dev, sizeof(*hda_priv), GFP_KERNEL);
  144. if (!hda_priv)
  145. return -ENOMEM;
  146. codec = hda_codec_device_init(&hbus->core, address, HDA_DEV_LEGACY);
  147. ret = PTR_ERR_OR_ZERO(codec);
  148. if (ret < 0)
  149. return ret;
  150. hda_priv->codec = codec;
  151. hda_priv->dev_index = address;
  152. dev_set_drvdata(&codec->core.dev, hda_priv);
  153. if ((resp & 0xFFFF0000) == IDISP_VID_INTEL) {
  154. if (!hbus->core.audio_component) {
  155. dev_dbg(sdev->dev,
  156. "iDisp hw present but no driver\n");
  157. ret = -ENOENT;
  158. goto out;
  159. }
  160. hda_priv->need_display_power = true;
  161. }
  162. if (is_generic_config(hbus))
  163. codec->probe_id = HDA_CODEC_ID_GENERIC;
  164. else
  165. codec->probe_id = 0;
  166. ret = hda_codec_load_module(codec);
  167. /*
  168. * handle ret==0 (no driver bound) as an error, but pass
  169. * other return codes without modification
  170. */
  171. if (ret == 0)
  172. ret = -ENOENT;
  173. out:
  174. if (ret < 0) {
  175. snd_hdac_device_unregister(&codec->core);
  176. put_device(&codec->core.dev);
  177. }
  178. return ret;
  179. }
  180. /* Codec initialization */
  181. void hda_codec_probe_bus(struct snd_sof_dev *sdev)
  182. {
  183. struct hdac_bus *bus = sof_to_bus(sdev);
  184. int i, ret;
  185. if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
  186. sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
  187. return;
  188. /* probe codecs in avail slots */
  189. for (i = 0; i < HDA_MAX_CODECS; i++) {
  190. if (!(bus->codec_mask & (1 << i)))
  191. continue;
  192. ret = hda_codec_probe(sdev, i);
  193. if (ret < 0) {
  194. dev_warn(bus->dev, "codec #%d probe error, ret: %d\n",
  195. i, ret);
  196. bus->codec_mask &= ~BIT(i);
  197. }
  198. }
  199. }
  200. EXPORT_SYMBOL_NS_GPL(hda_codec_probe_bus, SND_SOC_SOF_HDA_AUDIO_CODEC);
  201. void hda_codec_check_for_state_change(struct snd_sof_dev *sdev)
  202. {
  203. struct hdac_bus *bus = sof_to_bus(sdev);
  204. unsigned int codec_mask;
  205. codec_mask = snd_hdac_chip_readw(bus, STATESTS);
  206. if (codec_mask) {
  207. hda_codec_jack_check(sdev);
  208. snd_hdac_chip_writew(bus, STATESTS, codec_mask);
  209. }
  210. }
  211. EXPORT_SYMBOL_NS_GPL(hda_codec_check_for_state_change, SND_SOC_SOF_HDA_AUDIO_CODEC);
  212. void hda_codec_detect_mask(struct snd_sof_dev *sdev)
  213. {
  214. struct hdac_bus *bus = sof_to_bus(sdev);
  215. if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
  216. sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
  217. return;
  218. /* Accept unsolicited responses */
  219. snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_UNSOL, AZX_GCTL_UNSOL);
  220. /* detect codecs */
  221. if (!bus->codec_mask) {
  222. bus->codec_mask = snd_hdac_chip_readw(bus, STATESTS);
  223. dev_dbg(bus->dev, "codec_mask = 0x%lx\n", bus->codec_mask);
  224. }
  225. if (hda_codec_mask != -1) {
  226. bus->codec_mask &= hda_codec_mask;
  227. dev_dbg(bus->dev, "filtered codec_mask = 0x%lx\n",
  228. bus->codec_mask);
  229. }
  230. }
  231. EXPORT_SYMBOL_NS_GPL(hda_codec_detect_mask, SND_SOC_SOF_HDA_AUDIO_CODEC);
  232. void hda_codec_init_cmd_io(struct snd_sof_dev *sdev)
  233. {
  234. struct hdac_bus *bus = sof_to_bus(sdev);
  235. if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
  236. sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
  237. return;
  238. /* initialize the codec command I/O */
  239. snd_hdac_bus_init_cmd_io(bus);
  240. }
  241. EXPORT_SYMBOL_NS_GPL(hda_codec_init_cmd_io, SND_SOC_SOF_HDA_AUDIO_CODEC);
  242. void hda_codec_resume_cmd_io(struct snd_sof_dev *sdev)
  243. {
  244. struct hdac_bus *bus = sof_to_bus(sdev);
  245. if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
  246. sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
  247. return;
  248. /* set up CORB/RIRB buffers if was on before suspend */
  249. if (bus->cmd_dma_state)
  250. snd_hdac_bus_init_cmd_io(bus);
  251. }
  252. EXPORT_SYMBOL_NS_GPL(hda_codec_resume_cmd_io, SND_SOC_SOF_HDA_AUDIO_CODEC);
  253. void hda_codec_stop_cmd_io(struct snd_sof_dev *sdev)
  254. {
  255. struct hdac_bus *bus = sof_to_bus(sdev);
  256. if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
  257. sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
  258. return;
  259. /* initialize the codec command I/O */
  260. snd_hdac_bus_stop_cmd_io(bus);
  261. }
  262. EXPORT_SYMBOL_NS_GPL(hda_codec_stop_cmd_io, SND_SOC_SOF_HDA_AUDIO_CODEC);
  263. void hda_codec_suspend_cmd_io(struct snd_sof_dev *sdev)
  264. {
  265. struct hdac_bus *bus = sof_to_bus(sdev);
  266. if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
  267. sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
  268. return;
  269. /* stop the CORB/RIRB DMA if it is On */
  270. if (bus->cmd_dma_state)
  271. snd_hdac_bus_stop_cmd_io(bus);
  272. }
  273. EXPORT_SYMBOL_NS_GPL(hda_codec_suspend_cmd_io, SND_SOC_SOF_HDA_AUDIO_CODEC);
  274. void hda_codec_rirb_status_clear(struct snd_sof_dev *sdev)
  275. {
  276. struct hdac_bus *bus = sof_to_bus(sdev);
  277. if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
  278. sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
  279. return;
  280. /* clear rirb status */
  281. snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
  282. }
  283. EXPORT_SYMBOL_NS_GPL(hda_codec_rirb_status_clear, SND_SOC_SOF_HDA_AUDIO_CODEC);
  284. void hda_codec_set_codec_wakeup(struct snd_sof_dev *sdev, bool status)
  285. {
  286. struct hdac_bus *bus = sof_to_bus(sdev);
  287. if (sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
  288. return;
  289. snd_hdac_set_codec_wakeup(bus, status);
  290. }
  291. EXPORT_SYMBOL_NS_GPL(hda_codec_set_codec_wakeup, SND_SOC_SOF_HDA_AUDIO_CODEC);
  292. bool hda_codec_check_rirb_status(struct snd_sof_dev *sdev)
  293. {
  294. struct hdac_bus *bus = sof_to_bus(sdev);
  295. bool active = false;
  296. u32 rirb_status;
  297. if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
  298. sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
  299. return false;
  300. rirb_status = snd_hdac_chip_readb(bus, RIRBSTS);
  301. if (rirb_status & RIRB_INT_MASK) {
  302. /*
  303. * Clearing the interrupt status here ensures
  304. * that no interrupt gets masked after the RIRB
  305. * wp is read in snd_hdac_bus_update_rirb.
  306. */
  307. snd_hdac_chip_writeb(bus, RIRBSTS,
  308. RIRB_INT_MASK);
  309. active = true;
  310. if (rirb_status & RIRB_INT_RESPONSE)
  311. snd_hdac_bus_update_rirb(bus);
  312. }
  313. return active;
  314. }
  315. EXPORT_SYMBOL_NS_GPL(hda_codec_check_rirb_status, SND_SOC_SOF_HDA_AUDIO_CODEC);
  316. void hda_codec_device_remove(struct snd_sof_dev *sdev)
  317. {
  318. struct hdac_bus *bus = sof_to_bus(sdev);
  319. if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
  320. sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
  321. return;
  322. /* codec removal, invoke bus_device_remove */
  323. snd_hdac_ext_bus_device_remove(bus);
  324. }
  325. EXPORT_SYMBOL_NS_GPL(hda_codec_device_remove, SND_SOC_SOF_HDA_AUDIO_CODEC);
  326. #endif /* CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC */
  327. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC) && IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
  328. void hda_codec_i915_display_power(struct snd_sof_dev *sdev, bool enable)
  329. {
  330. struct hdac_bus *bus = sof_to_bus(sdev);
  331. if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
  332. sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
  333. return;
  334. if (HDA_IDISP_CODEC(bus->codec_mask)) {
  335. dev_dbg(bus->dev, "Turning i915 HDAC power %d\n", enable);
  336. snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, enable);
  337. }
  338. }
  339. EXPORT_SYMBOL_NS_GPL(hda_codec_i915_display_power, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
  340. int hda_codec_i915_init(struct snd_sof_dev *sdev)
  341. {
  342. struct hdac_bus *bus = sof_to_bus(sdev);
  343. int ret;
  344. if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
  345. sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
  346. return 0;
  347. /* i915 exposes a HDA codec for HDMI audio */
  348. ret = snd_hdac_i915_init(bus);
  349. if (ret < 0)
  350. return ret;
  351. /* codec_mask not yet known, power up for probe */
  352. snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, true);
  353. return 0;
  354. }
  355. EXPORT_SYMBOL_NS_GPL(hda_codec_i915_init, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
  356. int hda_codec_i915_exit(struct snd_sof_dev *sdev)
  357. {
  358. struct hdac_bus *bus = sof_to_bus(sdev);
  359. if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
  360. sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
  361. return 0;
  362. if (!bus->audio_component)
  363. return 0;
  364. /* power down unconditionally */
  365. snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
  366. return snd_hdac_i915_exit(bus);
  367. }
  368. EXPORT_SYMBOL_NS_GPL(hda_codec_i915_exit, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
  369. MODULE_SOFTDEP("pre: snd-hda-codec-hdmi");
  370. #endif
  371. MODULE_LICENSE("Dual BSD/GPL");
  372. MODULE_DESCRIPTION("SOF support for HDaudio codecs");