hda_bind.c 8.1 KB

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