hda_bind.c 7.4 KB

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