hda_bind.c 6.6 KB

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