hda_bind.c 7.1 KB

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