hda_bind.c 7.1 KB

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