hda_bind.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HD-audio codec driver binding
  4. * Copyright (c) Takashi Iwai <[email protected]>
  5. */
  6. #include <linux/init.h>
  7. #include <linux/slab.h>
  8. #include <linux/mutex.h>
  9. #include <linux/module.h>
  10. #include <linux/export.h>
  11. #include <linux/pm.h>
  12. #include <linux/pm_runtime.h>
  13. #include <sound/core.h>
  14. #include <sound/hda_codec.h>
  15. #include "hda_local.h"
  16. #include "hda_jack.h"
  17. /*
  18. * find a matching codec id
  19. */
  20. static int hda_codec_match(struct hdac_device *dev, struct hdac_driver *drv)
  21. {
  22. struct hda_codec *codec = container_of(dev, struct hda_codec, core);
  23. struct hda_codec_driver *driver =
  24. container_of(drv, struct hda_codec_driver, core);
  25. const struct hda_device_id *list;
  26. /* check probe_id instead of vendor_id if set */
  27. u32 id = codec->probe_id ? codec->probe_id : codec->core.vendor_id;
  28. u32 rev_id = codec->core.revision_id;
  29. for (list = driver->id; list->vendor_id; list++) {
  30. if (list->vendor_id == id &&
  31. (!list->rev_id || list->rev_id == rev_id)) {
  32. codec->preset = list;
  33. return 1;
  34. }
  35. }
  36. return 0;
  37. }
  38. /* process an unsolicited event */
  39. static void hda_codec_unsol_event(struct hdac_device *dev, unsigned int ev)
  40. {
  41. struct hda_codec *codec = container_of(dev, struct hda_codec, core);
  42. /* ignore unsol events during shutdown */
  43. if (codec->bus->shutdown)
  44. return;
  45. /* ignore unsol events during system suspend/resume */
  46. if (codec->core.dev.power.power_state.event != PM_EVENT_ON)
  47. return;
  48. if (codec->patch_ops.unsol_event)
  49. codec->patch_ops.unsol_event(codec, ev);
  50. }
  51. /**
  52. * snd_hda_codec_set_name - set the codec name
  53. * @codec: the HDA codec
  54. * @name: name string to set
  55. */
  56. int snd_hda_codec_set_name(struct hda_codec *codec, const char *name)
  57. {
  58. int err;
  59. if (!name)
  60. return 0;
  61. err = snd_hdac_device_set_chip_name(&codec->core, name);
  62. if (err < 0)
  63. return err;
  64. /* update the mixer name */
  65. if (!*codec->card->mixername ||
  66. codec->bus->mixer_assigned >= codec->core.addr) {
  67. snprintf(codec->card->mixername,
  68. sizeof(codec->card->mixername), "%s %s",
  69. codec->core.vendor_name, codec->core.chip_name);
  70. codec->bus->mixer_assigned = codec->core.addr;
  71. }
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(snd_hda_codec_set_name);
  75. static int hda_codec_driver_probe(struct device *dev)
  76. {
  77. struct hda_codec *codec = dev_to_hda_codec(dev);
  78. struct module *owner = dev->driver->owner;
  79. hda_codec_patch_t patch;
  80. int err;
  81. if (codec->bus->core.ext_ops) {
  82. if (WARN_ON(!codec->bus->core.ext_ops->hdev_attach))
  83. return -EINVAL;
  84. return codec->bus->core.ext_ops->hdev_attach(&codec->core);
  85. }
  86. if (WARN_ON(!codec->preset))
  87. return -EINVAL;
  88. err = snd_hda_codec_set_name(codec, codec->preset->name);
  89. if (err < 0)
  90. goto error;
  91. err = snd_hdac_regmap_init(&codec->core);
  92. if (err < 0)
  93. goto error;
  94. if (!try_module_get(owner)) {
  95. err = -EINVAL;
  96. goto error;
  97. }
  98. patch = (hda_codec_patch_t)codec->preset->driver_data;
  99. if (patch) {
  100. err = patch(codec);
  101. if (err < 0)
  102. goto error_module_put;
  103. }
  104. err = snd_hda_codec_build_pcms(codec);
  105. if (err < 0)
  106. goto error_module;
  107. err = snd_hda_codec_build_controls(codec);
  108. if (err < 0)
  109. goto error_module;
  110. /* only register after the bus probe finished; otherwise it's racy */
  111. if (!codec->bus->bus_probing && codec->card->registered) {
  112. err = snd_card_register(codec->card);
  113. if (err < 0)
  114. goto error_module;
  115. snd_hda_codec_register(codec);
  116. }
  117. codec->core.lazy_cache = true;
  118. return 0;
  119. error_module:
  120. if (codec->patch_ops.free)
  121. codec->patch_ops.free(codec);
  122. error_module_put:
  123. module_put(owner);
  124. error:
  125. snd_hda_codec_cleanup_for_unbind(codec);
  126. codec->preset = NULL;
  127. return err;
  128. }
  129. static int hda_codec_driver_remove(struct device *dev)
  130. {
  131. struct hda_codec *codec = dev_to_hda_codec(dev);
  132. if (codec->bus->core.ext_ops) {
  133. if (WARN_ON(!codec->bus->core.ext_ops->hdev_detach))
  134. return -EINVAL;
  135. return codec->bus->core.ext_ops->hdev_detach(&codec->core);
  136. }
  137. snd_hda_codec_disconnect_pcms(codec);
  138. snd_hda_jack_tbl_disconnect(codec);
  139. if (!refcount_dec_and_test(&codec->pcm_ref))
  140. wait_event(codec->remove_sleep, !refcount_read(&codec->pcm_ref));
  141. snd_power_sync_ref(codec->bus->card);
  142. if (codec->patch_ops.free)
  143. codec->patch_ops.free(codec);
  144. snd_hda_codec_cleanup_for_unbind(codec);
  145. codec->preset = NULL;
  146. module_put(dev->driver->owner);
  147. return 0;
  148. }
  149. static void hda_codec_driver_shutdown(struct device *dev)
  150. {
  151. snd_hda_codec_shutdown(dev_to_hda_codec(dev));
  152. }
  153. int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
  154. struct module *owner)
  155. {
  156. drv->core.driver.name = name;
  157. drv->core.driver.owner = owner;
  158. drv->core.driver.bus = &snd_hda_bus_type;
  159. drv->core.driver.probe = hda_codec_driver_probe;
  160. drv->core.driver.remove = hda_codec_driver_remove;
  161. drv->core.driver.shutdown = hda_codec_driver_shutdown;
  162. drv->core.driver.pm = &hda_codec_driver_pm;
  163. drv->core.type = HDA_DEV_LEGACY;
  164. drv->core.match = hda_codec_match;
  165. drv->core.unsol_event = hda_codec_unsol_event;
  166. return driver_register(&drv->core.driver);
  167. }
  168. EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
  169. void hda_codec_driver_unregister(struct hda_codec_driver *drv)
  170. {
  171. driver_unregister(&drv->core.driver);
  172. }
  173. EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);
  174. static inline bool codec_probed(struct hda_codec *codec)
  175. {
  176. return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
  177. }
  178. /* try to auto-load codec module */
  179. static void request_codec_module(struct hda_codec *codec)
  180. {
  181. #ifdef MODULE
  182. char modalias[32];
  183. const char *mod = NULL;
  184. switch (codec->probe_id) {
  185. case HDA_CODEC_ID_GENERIC_HDMI:
  186. #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
  187. mod = "snd-hda-codec-hdmi";
  188. #endif
  189. break;
  190. case HDA_CODEC_ID_GENERIC:
  191. #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
  192. mod = "snd-hda-codec-generic";
  193. #endif
  194. break;
  195. default:
  196. snd_hdac_codec_modalias(&codec->core, modalias, sizeof(modalias));
  197. mod = modalias;
  198. break;
  199. }
  200. if (mod)
  201. request_module(mod);
  202. #endif /* MODULE */
  203. }
  204. /* try to auto-load and bind the codec module */
  205. static void codec_bind_module(struct hda_codec *codec)
  206. {
  207. #ifdef MODULE
  208. request_codec_module(codec);
  209. if (codec_probed(codec))
  210. return;
  211. #endif
  212. }
  213. #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
  214. /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
  215. static bool is_likely_hdmi_codec(struct hda_codec *codec)
  216. {
  217. hda_nid_t nid;
  218. /*
  219. * For ASoC users, if snd_hda_hdmi_codec module is denylisted and any
  220. * event causes i915 enumeration to fail, ->wcaps remains uninitialized.
  221. */
  222. if (!codec->wcaps)
  223. return true;
  224. for_each_hda_codec_node(nid, codec) {
  225. unsigned int wcaps = get_wcaps(codec, nid);
  226. switch (get_wcaps_type(wcaps)) {
  227. case AC_WID_AUD_IN:
  228. return false; /* HDMI parser supports only HDMI out */
  229. case AC_WID_AUD_OUT:
  230. if (!(wcaps & AC_WCAP_DIGITAL))
  231. return false;
  232. break;
  233. }
  234. }
  235. return true;
  236. }
  237. #else
  238. /* no HDMI codec parser support */
  239. #define is_likely_hdmi_codec(codec) false
  240. #endif /* CONFIG_SND_HDA_CODEC_HDMI */
  241. static int codec_bind_generic(struct hda_codec *codec)
  242. {
  243. if (codec->probe_id)
  244. return -ENODEV;
  245. if (is_likely_hdmi_codec(codec)) {
  246. codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
  247. request_codec_module(codec);
  248. if (codec_probed(codec))
  249. return 0;
  250. }
  251. codec->probe_id = HDA_CODEC_ID_GENERIC;
  252. request_codec_module(codec);
  253. if (codec_probed(codec))
  254. return 0;
  255. return -ENODEV;
  256. }
  257. #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
  258. #define is_generic_config(codec) \
  259. (codec->modelname && !strcmp(codec->modelname, "generic"))
  260. #else
  261. #define is_generic_config(codec) 0
  262. #endif
  263. /**
  264. * snd_hda_codec_configure - (Re-)configure the HD-audio codec
  265. * @codec: the HDA codec
  266. *
  267. * Start parsing of the given codec tree and (re-)initialize the whole
  268. * patch instance.
  269. *
  270. * Returns 0 if successful or a negative error code.
  271. */
  272. int snd_hda_codec_configure(struct hda_codec *codec)
  273. {
  274. int err;
  275. if (codec->configured)
  276. return 0;
  277. if (is_generic_config(codec))
  278. codec->probe_id = HDA_CODEC_ID_GENERIC;
  279. else
  280. codec->probe_id = 0;
  281. if (!device_is_registered(&codec->core.dev)) {
  282. err = snd_hdac_device_register(&codec->core);
  283. if (err < 0)
  284. return err;
  285. }
  286. if (!codec->preset)
  287. codec_bind_module(codec);
  288. if (!codec->preset) {
  289. err = codec_bind_generic(codec);
  290. if (err < 0) {
  291. codec_dbg(codec, "Unable to bind the codec\n");
  292. return err;
  293. }
  294. }
  295. codec->configured = 1;
  296. return 0;
  297. }
  298. EXPORT_SYMBOL_GPL(snd_hda_codec_configure);