hda_bind.c 7.0 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. if (codec->card->registered) {
  98. err = snd_card_register(codec->card);
  99. if (err < 0)
  100. goto error_module;
  101. snd_hda_codec_register(codec);
  102. }
  103. codec->core.lazy_cache = true;
  104. return 0;
  105. error_module:
  106. module_put(owner);
  107. error:
  108. snd_hda_codec_cleanup_for_unbind(codec);
  109. return err;
  110. }
  111. static int hda_codec_driver_remove(struct device *dev)
  112. {
  113. struct hda_codec *codec = dev_to_hda_codec(dev);
  114. if (codec->patch_ops.free)
  115. codec->patch_ops.free(codec);
  116. snd_hda_codec_cleanup_for_unbind(codec);
  117. module_put(dev->driver->owner);
  118. return 0;
  119. }
  120. static void hda_codec_driver_shutdown(struct device *dev)
  121. {
  122. struct hda_codec *codec = dev_to_hda_codec(dev);
  123. if (!pm_runtime_suspended(dev) && codec->patch_ops.reboot_notify)
  124. codec->patch_ops.reboot_notify(codec);
  125. }
  126. int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
  127. struct module *owner)
  128. {
  129. drv->core.driver.name = name;
  130. drv->core.driver.owner = owner;
  131. drv->core.driver.bus = &snd_hda_bus_type;
  132. drv->core.driver.probe = hda_codec_driver_probe;
  133. drv->core.driver.remove = hda_codec_driver_remove;
  134. drv->core.driver.shutdown = hda_codec_driver_shutdown;
  135. drv->core.driver.pm = &hda_codec_driver_pm;
  136. drv->core.type = HDA_DEV_LEGACY;
  137. drv->core.match = hda_codec_match;
  138. drv->core.unsol_event = hda_codec_unsol_event;
  139. return driver_register(&drv->core.driver);
  140. }
  141. EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
  142. void hda_codec_driver_unregister(struct hda_codec_driver *drv)
  143. {
  144. driver_unregister(&drv->core.driver);
  145. }
  146. EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);
  147. static inline bool codec_probed(struct hda_codec *codec)
  148. {
  149. return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
  150. }
  151. /* try to auto-load codec module */
  152. static void request_codec_module(struct hda_codec *codec)
  153. {
  154. #ifdef MODULE
  155. char modalias[32];
  156. const char *mod = NULL;
  157. switch (codec->probe_id) {
  158. case HDA_CODEC_ID_GENERIC_HDMI:
  159. #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
  160. mod = "snd-hda-codec-hdmi";
  161. #endif
  162. break;
  163. case HDA_CODEC_ID_GENERIC:
  164. #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
  165. mod = "snd-hda-codec-generic";
  166. #endif
  167. break;
  168. default:
  169. snd_hdac_codec_modalias(&codec->core, modalias, sizeof(modalias));
  170. mod = modalias;
  171. break;
  172. }
  173. if (mod)
  174. request_module(mod);
  175. #endif /* MODULE */
  176. }
  177. /* try to auto-load and bind the codec module */
  178. static void codec_bind_module(struct hda_codec *codec)
  179. {
  180. #ifdef MODULE
  181. request_codec_module(codec);
  182. if (codec_probed(codec))
  183. return;
  184. #endif
  185. }
  186. #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
  187. /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
  188. static bool is_likely_hdmi_codec(struct hda_codec *codec)
  189. {
  190. hda_nid_t nid;
  191. for_each_hda_codec_node(nid, codec) {
  192. unsigned int wcaps = get_wcaps(codec, nid);
  193. switch (get_wcaps_type(wcaps)) {
  194. case AC_WID_AUD_IN:
  195. return false; /* HDMI parser supports only HDMI out */
  196. case AC_WID_AUD_OUT:
  197. if (!(wcaps & AC_WCAP_DIGITAL))
  198. return false;
  199. break;
  200. }
  201. }
  202. return true;
  203. }
  204. #else
  205. /* no HDMI codec parser support */
  206. #define is_likely_hdmi_codec(codec) false
  207. #endif /* CONFIG_SND_HDA_CODEC_HDMI */
  208. static int codec_bind_generic(struct hda_codec *codec)
  209. {
  210. if (codec->probe_id)
  211. return -ENODEV;
  212. if (is_likely_hdmi_codec(codec)) {
  213. codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
  214. request_codec_module(codec);
  215. if (codec_probed(codec))
  216. return 0;
  217. }
  218. codec->probe_id = HDA_CODEC_ID_GENERIC;
  219. request_codec_module(codec);
  220. if (codec_probed(codec))
  221. return 0;
  222. return -ENODEV;
  223. }
  224. #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
  225. #define is_generic_config(codec) \
  226. (codec->modelname && !strcmp(codec->modelname, "generic"))
  227. #else
  228. #define is_generic_config(codec) 0
  229. #endif
  230. /**
  231. * snd_hda_codec_configure - (Re-)configure the HD-audio codec
  232. * @codec: the HDA codec
  233. *
  234. * Start parsing of the given codec tree and (re-)initialize the whole
  235. * patch instance.
  236. *
  237. * Returns 0 if successful or a negative error code.
  238. */
  239. int snd_hda_codec_configure(struct hda_codec *codec)
  240. {
  241. int err;
  242. if (is_generic_config(codec))
  243. codec->probe_id = HDA_CODEC_ID_GENERIC;
  244. else
  245. codec->probe_id = 0;
  246. err = snd_hdac_device_register(&codec->core);
  247. if (err < 0)
  248. return err;
  249. if (!codec->preset)
  250. codec_bind_module(codec);
  251. if (!codec->preset) {
  252. err = codec_bind_generic(codec);
  253. if (err < 0) {
  254. codec_err(codec, "Unable to bind the codec\n");
  255. goto error;
  256. }
  257. }
  258. return 0;
  259. error:
  260. snd_hdac_device_unregister(&codec->core);
  261. return err;
  262. }
  263. EXPORT_SYMBOL_GPL(snd_hda_codec_configure);