hda_bind.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 preset
  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_codec_preset *preset;
  24. /* check probe_id instead of vendor_id if set */
  25. u32 id = codec->probe_id ? codec->probe_id : codec->core.vendor_id;
  26. for (preset = driver->preset; preset->id; preset++) {
  27. if (preset->id == id &&
  28. (!preset->rev || preset->rev == codec->core.revision_id)) {
  29. codec->preset = preset;
  30. return 1;
  31. }
  32. }
  33. return 0;
  34. }
  35. /* process an unsolicited event */
  36. static void hda_codec_unsol_event(struct hdac_device *dev, unsigned int ev)
  37. {
  38. struct hda_codec *codec = container_of(dev, struct hda_codec, core);
  39. if (codec->patch_ops.unsol_event)
  40. codec->patch_ops.unsol_event(codec, ev);
  41. }
  42. /* reset the codec name from the preset */
  43. static int codec_refresh_name(struct hda_codec *codec, const char *name)
  44. {
  45. if (name) {
  46. kfree(codec->core.chip_name);
  47. codec->core.chip_name = kstrdup(name, GFP_KERNEL);
  48. }
  49. return codec->core.chip_name ? 0 : -ENOMEM;
  50. }
  51. static int hda_codec_driver_probe(struct device *dev)
  52. {
  53. struct hda_codec *codec = dev_to_hda_codec(dev);
  54. struct module *owner = dev->driver->owner;
  55. int err;
  56. if (WARN_ON(!codec->preset))
  57. return -EINVAL;
  58. err = codec_refresh_name(codec, codec->preset->name);
  59. if (err < 0)
  60. goto error;
  61. err = snd_hdac_regmap_init(&codec->core);
  62. if (err < 0)
  63. goto error;
  64. if (!try_module_get(owner)) {
  65. err = -EINVAL;
  66. goto error;
  67. }
  68. err = codec->preset->patch(codec);
  69. if (err < 0)
  70. goto error_module;
  71. err = snd_hda_codec_build_pcms(codec);
  72. if (err < 0)
  73. goto error_module;
  74. err = snd_hda_codec_build_controls(codec);
  75. if (err < 0)
  76. goto error_module;
  77. if (codec->card->registered) {
  78. err = snd_card_register(codec->card);
  79. if (err < 0)
  80. goto error_module;
  81. snd_hda_codec_register(codec);
  82. }
  83. codec->core.lazy_cache = true;
  84. return 0;
  85. error_module:
  86. module_put(owner);
  87. error:
  88. snd_hda_codec_cleanup_for_unbind(codec);
  89. return err;
  90. }
  91. static int hda_codec_driver_remove(struct device *dev)
  92. {
  93. struct hda_codec *codec = dev_to_hda_codec(dev);
  94. if (codec->patch_ops.free)
  95. codec->patch_ops.free(codec);
  96. snd_hda_codec_cleanup_for_unbind(codec);
  97. module_put(dev->driver->owner);
  98. return 0;
  99. }
  100. static void hda_codec_driver_shutdown(struct device *dev)
  101. {
  102. struct hda_codec *codec = dev_to_hda_codec(dev);
  103. if (!pm_runtime_suspended(dev) && codec->patch_ops.reboot_notify)
  104. codec->patch_ops.reboot_notify(codec);
  105. }
  106. int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
  107. struct module *owner)
  108. {
  109. drv->core.driver.name = name;
  110. drv->core.driver.owner = owner;
  111. drv->core.driver.bus = &snd_hda_bus_type;
  112. drv->core.driver.probe = hda_codec_driver_probe;
  113. drv->core.driver.remove = hda_codec_driver_remove;
  114. drv->core.driver.shutdown = hda_codec_driver_shutdown;
  115. drv->core.driver.pm = &hda_codec_driver_pm;
  116. drv->core.type = HDA_DEV_LEGACY;
  117. drv->core.match = hda_codec_match;
  118. drv->core.unsol_event = hda_codec_unsol_event;
  119. return driver_register(&drv->core.driver);
  120. }
  121. EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
  122. void hda_codec_driver_unregister(struct hda_codec_driver *drv)
  123. {
  124. driver_unregister(&drv->core.driver);
  125. }
  126. EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);
  127. static inline bool codec_probed(struct hda_codec *codec)
  128. {
  129. return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
  130. }
  131. /* try to auto-load and bind the codec module */
  132. static void codec_bind_module(struct hda_codec *codec)
  133. {
  134. #ifdef MODULE
  135. request_module("snd-hda-codec-id:%08x", codec->core.vendor_id);
  136. if (codec_probed(codec))
  137. return;
  138. request_module("snd-hda-codec-id:%04x*",
  139. (codec->core.vendor_id >> 16) & 0xffff);
  140. if (codec_probed(codec))
  141. return;
  142. #endif
  143. }
  144. #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
  145. /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
  146. static bool is_likely_hdmi_codec(struct hda_codec *codec)
  147. {
  148. hda_nid_t nid;
  149. for_each_hda_codec_node(nid, codec) {
  150. unsigned int wcaps = get_wcaps(codec, nid);
  151. switch (get_wcaps_type(wcaps)) {
  152. case AC_WID_AUD_IN:
  153. return false; /* HDMI parser supports only HDMI out */
  154. case AC_WID_AUD_OUT:
  155. if (!(wcaps & AC_WCAP_DIGITAL))
  156. return false;
  157. break;
  158. }
  159. }
  160. return true;
  161. }
  162. #else
  163. /* no HDMI codec parser support */
  164. #define is_likely_hdmi_codec(codec) false
  165. #endif /* CONFIG_SND_HDA_CODEC_HDMI */
  166. static int codec_bind_generic(struct hda_codec *codec)
  167. {
  168. if (codec->probe_id)
  169. return -ENODEV;
  170. if (is_likely_hdmi_codec(codec)) {
  171. codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
  172. #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
  173. request_module("snd-hda-codec-hdmi");
  174. #endif
  175. if (codec_probed(codec))
  176. return 0;
  177. }
  178. codec->probe_id = HDA_CODEC_ID_GENERIC;
  179. #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
  180. request_module("snd-hda-codec-generic");
  181. #endif
  182. if (codec_probed(codec))
  183. return 0;
  184. return -ENODEV;
  185. }
  186. #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
  187. #define is_generic_config(codec) \
  188. (codec->modelname && !strcmp(codec->modelname, "generic"))
  189. #else
  190. #define is_generic_config(codec) 0
  191. #endif
  192. /**
  193. * snd_hda_codec_configure - (Re-)configure the HD-audio codec
  194. * @codec: the HDA codec
  195. *
  196. * Start parsing of the given codec tree and (re-)initialize the whole
  197. * patch instance.
  198. *
  199. * Returns 0 if successful or a negative error code.
  200. */
  201. int snd_hda_codec_configure(struct hda_codec *codec)
  202. {
  203. int err;
  204. if (is_generic_config(codec))
  205. codec->probe_id = HDA_CODEC_ID_GENERIC;
  206. else
  207. codec->probe_id = 0;
  208. err = snd_hdac_device_register(&codec->core);
  209. if (err < 0)
  210. return err;
  211. if (!codec->preset)
  212. codec_bind_module(codec);
  213. if (!codec->preset) {
  214. err = codec_bind_generic(codec);
  215. if (err < 0) {
  216. codec_err(codec, "Unable to bind the codec\n");
  217. goto error;
  218. }
  219. }
  220. /* audio codec should override the mixer name */
  221. if (codec->core.afg || !*codec->card->mixername)
  222. snprintf(codec->card->mixername,
  223. sizeof(codec->card->mixername), "%s %s",
  224. codec->core.vendor_name, codec->core.chip_name);
  225. return 0;
  226. error:
  227. snd_hdac_device_unregister(&codec->core);
  228. return err;
  229. }
  230. EXPORT_SYMBOL_GPL(snd_hda_codec_configure);