2 * HD-audio codec driver binding
3 * Copyright (c) Takashi Iwai <tiwai@suse.de>
6 #include <linux/init.h>
7 #include <linux/slab.h>
8 #include <linux/mutex.h>
9 #include <linux/module.h>
10 #include <linux/export.h>
12 #include <linux/pm_runtime.h>
13 #include <sound/core.h>
14 #include "hda_codec.h"
15 #include "hda_local.h"
18 * find a matching codec id
20 static int hda_codec_match(struct hdac_device *dev, struct hdac_driver *drv)
22 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
23 struct hda_codec_driver *driver =
24 container_of(drv, struct hda_codec_driver, core);
25 const struct hda_device_id *list;
26 /* check probe_id instead of vendor_id if set */
27 u32 id = codec->probe_id ? codec->probe_id : codec->core.vendor_id;
28 u32 rev_id = codec->core.revision_id;
30 for (list = driver->id; list->vendor_id; list++) {
31 if (list->vendor_id == id &&
32 (!list->rev_id || list->rev_id == rev_id)) {
40 /* process an unsolicited event */
41 static void hda_codec_unsol_event(struct hdac_device *dev, unsigned int ev)
43 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
45 /* ignore unsol events during shutdown */
46 if (codec->bus->shutdown)
49 /* ignore unsol events during system suspend/resume */
50 if (codec->core.dev.power.power_state.event != PM_EVENT_ON)
53 if (codec->patch_ops.unsol_event)
54 codec->patch_ops.unsol_event(codec, ev);
58 * snd_hda_codec_set_name - set the codec name
59 * @codec: the HDA codec
60 * @name: name string to set
62 int snd_hda_codec_set_name(struct hda_codec *codec, const char *name)
68 err = snd_hdac_device_set_chip_name(&codec->core, name);
72 /* update the mixer name */
73 if (!*codec->card->mixername ||
74 codec->bus->mixer_assigned >= codec->core.addr) {
75 snprintf(codec->card->mixername,
76 sizeof(codec->card->mixername), "%s %s",
77 codec->core.vendor_name, codec->core.chip_name);
78 codec->bus->mixer_assigned = codec->core.addr;
83 EXPORT_SYMBOL_GPL(snd_hda_codec_set_name);
85 static int hda_codec_driver_probe(struct device *dev)
87 struct hda_codec *codec = dev_to_hda_codec(dev);
88 struct module *owner = dev->driver->owner;
89 hda_codec_patch_t patch;
92 if (WARN_ON(!codec->preset))
95 err = snd_hda_codec_set_name(codec, codec->preset->name);
98 err = snd_hdac_regmap_init(&codec->core);
102 if (!try_module_get(owner)) {
107 patch = (hda_codec_patch_t)codec->preset->driver_data;
114 err = snd_hda_codec_build_pcms(codec);
117 err = snd_hda_codec_build_controls(codec);
120 /* only register after the bus probe finished; otherwise it's racy */
121 if (!codec->bus->bus_probing && codec->card->registered) {
122 err = snd_card_register(codec->card);
125 snd_hda_codec_register(codec);
128 codec->core.lazy_cache = true;
135 snd_hda_codec_cleanup_for_unbind(codec);
139 static int hda_codec_driver_remove(struct device *dev)
141 struct hda_codec *codec = dev_to_hda_codec(dev);
143 if (codec->patch_ops.free)
144 codec->patch_ops.free(codec);
145 snd_hda_codec_cleanup_for_unbind(codec);
146 module_put(dev->driver->owner);
150 static void hda_codec_driver_shutdown(struct device *dev)
152 struct hda_codec *codec = dev_to_hda_codec(dev);
154 if (!pm_runtime_suspended(dev) && codec->patch_ops.reboot_notify)
155 codec->patch_ops.reboot_notify(codec);
158 int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
159 struct module *owner)
161 drv->core.driver.name = name;
162 drv->core.driver.owner = owner;
163 drv->core.driver.bus = &snd_hda_bus_type;
164 drv->core.driver.probe = hda_codec_driver_probe;
165 drv->core.driver.remove = hda_codec_driver_remove;
166 drv->core.driver.shutdown = hda_codec_driver_shutdown;
167 drv->core.driver.pm = &hda_codec_driver_pm;
168 drv->core.type = HDA_DEV_LEGACY;
169 drv->core.match = hda_codec_match;
170 drv->core.unsol_event = hda_codec_unsol_event;
171 return driver_register(&drv->core.driver);
173 EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
175 void hda_codec_driver_unregister(struct hda_codec_driver *drv)
177 driver_unregister(&drv->core.driver);
179 EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);
181 static inline bool codec_probed(struct hda_codec *codec)
183 return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
186 /* try to auto-load codec module */
187 static void request_codec_module(struct hda_codec *codec)
191 const char *mod = NULL;
193 switch (codec->probe_id) {
194 case HDA_CODEC_ID_GENERIC_HDMI:
195 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
196 mod = "snd-hda-codec-hdmi";
199 case HDA_CODEC_ID_GENERIC:
200 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
201 mod = "snd-hda-codec-generic";
205 snd_hdac_codec_modalias(&codec->core, modalias, sizeof(modalias));
215 /* try to auto-load and bind the codec module */
216 static void codec_bind_module(struct hda_codec *codec)
219 request_codec_module(codec);
220 if (codec_probed(codec))
225 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
226 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
227 static bool is_likely_hdmi_codec(struct hda_codec *codec)
231 for_each_hda_codec_node(nid, codec) {
232 unsigned int wcaps = get_wcaps(codec, nid);
233 switch (get_wcaps_type(wcaps)) {
235 return false; /* HDMI parser supports only HDMI out */
237 if (!(wcaps & AC_WCAP_DIGITAL))
245 /* no HDMI codec parser support */
246 #define is_likely_hdmi_codec(codec) false
247 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
249 static int codec_bind_generic(struct hda_codec *codec)
254 if (is_likely_hdmi_codec(codec)) {
255 codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
256 request_codec_module(codec);
257 if (codec_probed(codec))
261 codec->probe_id = HDA_CODEC_ID_GENERIC;
262 request_codec_module(codec);
263 if (codec_probed(codec))
268 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
269 #define is_generic_config(codec) \
270 (codec->modelname && !strcmp(codec->modelname, "generic"))
272 #define is_generic_config(codec) 0
276 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
277 * @codec: the HDA codec
279 * Start parsing of the given codec tree and (re-)initialize the whole
282 * Returns 0 if successful or a negative error code.
284 int snd_hda_codec_configure(struct hda_codec *codec)
288 if (is_generic_config(codec))
289 codec->probe_id = HDA_CODEC_ID_GENERIC;
293 err = snd_hdac_device_register(&codec->core);
298 codec_bind_module(codec);
299 if (!codec->preset) {
300 err = codec_bind_generic(codec);
302 codec_err(codec, "Unable to bind the codec\n");
310 snd_hdac_device_unregister(&codec->core);
313 EXPORT_SYMBOL_GPL(snd_hda_codec_configure);