1 // SPDX-License-Identifier: GPL-2.0-only
3 * HD-audio codec driver binding
4 * Copyright (c) Takashi Iwai <tiwai@suse.de>
7 #include <linux/init.h>
8 #include <linux/slab.h>
9 #include <linux/mutex.h>
10 #include <linux/module.h>
11 #include <linux/export.h>
13 #include <linux/pm_runtime.h>
14 #include <sound/core.h>
15 #include <sound/hda_codec.h>
16 #include "hda_local.h"
20 * find a matching codec id
22 static int hda_codec_match(struct hdac_device *dev, struct hdac_driver *drv)
24 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
25 struct hda_codec_driver *driver =
26 container_of(drv, struct hda_codec_driver, core);
27 const struct hda_device_id *list;
28 /* check probe_id instead of vendor_id if set */
29 u32 id = codec->probe_id ? codec->probe_id : codec->core.vendor_id;
30 u32 rev_id = codec->core.revision_id;
32 for (list = driver->id; list->vendor_id; list++) {
33 if (list->vendor_id == id &&
34 (!list->rev_id || list->rev_id == rev_id)) {
42 /* process an unsolicited event */
43 static void hda_codec_unsol_event(struct hdac_device *dev, unsigned int ev)
45 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
47 /* ignore unsol events during shutdown */
48 if (codec->bus->shutdown)
51 /* ignore unsol events during system suspend/resume */
52 if (codec->core.dev.power.power_state.event != PM_EVENT_ON)
55 if (codec->patch_ops.unsol_event)
56 codec->patch_ops.unsol_event(codec, ev);
60 * snd_hda_codec_set_name - set the codec name
61 * @codec: the HDA codec
62 * @name: name string to set
64 int snd_hda_codec_set_name(struct hda_codec *codec, const char *name)
70 err = snd_hdac_device_set_chip_name(&codec->core, name);
74 /* update the mixer name */
75 if (!*codec->card->mixername ||
76 codec->bus->mixer_assigned >= codec->core.addr) {
77 snprintf(codec->card->mixername,
78 sizeof(codec->card->mixername), "%s %s",
79 codec->core.vendor_name, codec->core.chip_name);
80 codec->bus->mixer_assigned = codec->core.addr;
85 EXPORT_SYMBOL_GPL(snd_hda_codec_set_name);
87 static int hda_codec_driver_probe(struct device *dev)
89 struct hda_codec *codec = dev_to_hda_codec(dev);
90 struct module *owner = dev->driver->owner;
91 hda_codec_patch_t patch;
94 if (codec->bus->core.ext_ops) {
95 if (WARN_ON(!codec->bus->core.ext_ops->hdev_attach))
97 return codec->bus->core.ext_ops->hdev_attach(&codec->core);
100 if (WARN_ON(!codec->preset))
103 err = snd_hda_codec_set_name(codec, codec->preset->name);
106 err = snd_hdac_regmap_init(&codec->core);
110 if (!try_module_get(owner)) {
115 patch = (hda_codec_patch_t)codec->preset->driver_data;
119 goto error_module_put;
122 err = snd_hda_codec_build_pcms(codec);
125 err = snd_hda_codec_build_controls(codec);
128 /* only register after the bus probe finished; otherwise it's racy */
129 if (!codec->bus->bus_probing && codec->card->registered) {
130 err = snd_card_register(codec->card);
133 snd_hda_codec_register(codec);
136 codec->core.lazy_cache = true;
140 if (codec->patch_ops.free)
141 codec->patch_ops.free(codec);
146 snd_hda_codec_cleanup_for_unbind(codec);
147 codec->preset = NULL;
151 static int hda_codec_driver_remove(struct device *dev)
153 struct hda_codec *codec = dev_to_hda_codec(dev);
155 if (codec->bus->core.ext_ops) {
156 if (WARN_ON(!codec->bus->core.ext_ops->hdev_detach))
158 return codec->bus->core.ext_ops->hdev_detach(&codec->core);
161 snd_hda_codec_disconnect_pcms(codec);
162 snd_hda_jack_tbl_disconnect(codec);
163 if (!refcount_dec_and_test(&codec->pcm_ref))
164 wait_event(codec->remove_sleep, !refcount_read(&codec->pcm_ref));
165 snd_power_sync_ref(codec->bus->card);
167 if (codec->patch_ops.free)
168 codec->patch_ops.free(codec);
169 snd_hda_codec_cleanup_for_unbind(codec);
170 codec->preset = NULL;
171 module_put(dev->driver->owner);
175 static void hda_codec_driver_shutdown(struct device *dev)
177 snd_hda_codec_shutdown(dev_to_hda_codec(dev));
180 int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
181 struct module *owner)
183 drv->core.driver.name = name;
184 drv->core.driver.owner = owner;
185 drv->core.driver.bus = &snd_hda_bus_type;
186 drv->core.driver.probe = hda_codec_driver_probe;
187 drv->core.driver.remove = hda_codec_driver_remove;
188 drv->core.driver.shutdown = hda_codec_driver_shutdown;
189 drv->core.driver.pm = &hda_codec_driver_pm;
190 drv->core.type = HDA_DEV_LEGACY;
191 drv->core.match = hda_codec_match;
192 drv->core.unsol_event = hda_codec_unsol_event;
193 return driver_register(&drv->core.driver);
195 EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
197 void hda_codec_driver_unregister(struct hda_codec_driver *drv)
199 driver_unregister(&drv->core.driver);
201 EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);
203 static inline bool codec_probed(struct hda_codec *codec)
205 return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
208 /* try to auto-load codec module */
209 static void request_codec_module(struct hda_codec *codec)
213 const char *mod = NULL;
215 switch (codec->probe_id) {
216 case HDA_CODEC_ID_GENERIC_HDMI:
217 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
218 mod = "snd-hda-codec-hdmi";
221 case HDA_CODEC_ID_GENERIC:
222 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
223 mod = "snd-hda-codec-generic";
227 snd_hdac_codec_modalias(&codec->core, modalias, sizeof(modalias));
237 /* try to auto-load and bind the codec module */
238 static void codec_bind_module(struct hda_codec *codec)
241 request_codec_module(codec);
242 if (codec_probed(codec))
247 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
248 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
249 static bool is_likely_hdmi_codec(struct hda_codec *codec)
253 for_each_hda_codec_node(nid, codec) {
254 unsigned int wcaps = get_wcaps(codec, nid);
255 switch (get_wcaps_type(wcaps)) {
257 return false; /* HDMI parser supports only HDMI out */
259 if (!(wcaps & AC_WCAP_DIGITAL))
267 /* no HDMI codec parser support */
268 #define is_likely_hdmi_codec(codec) false
269 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
271 static int codec_bind_generic(struct hda_codec *codec)
276 if (is_likely_hdmi_codec(codec)) {
277 codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
278 request_codec_module(codec);
279 if (codec_probed(codec))
283 codec->probe_id = HDA_CODEC_ID_GENERIC;
284 request_codec_module(codec);
285 if (codec_probed(codec))
290 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
291 #define is_generic_config(codec) \
292 (codec->modelname && !strcmp(codec->modelname, "generic"))
294 #define is_generic_config(codec) 0
298 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
299 * @codec: the HDA codec
301 * Start parsing of the given codec tree and (re-)initialize the whole
304 * Returns 0 if successful or a negative error code.
306 int snd_hda_codec_configure(struct hda_codec *codec)
310 if (codec->configured)
313 if (is_generic_config(codec))
314 codec->probe_id = HDA_CODEC_ID_GENERIC;
318 if (!device_is_registered(&codec->core.dev)) {
319 err = snd_hdac_device_register(&codec->core);
325 codec_bind_module(codec);
326 if (!codec->preset) {
327 err = codec_bind_generic(codec);
329 codec_dbg(codec, "Unable to bind the codec\n");
334 codec->configured = 1;
337 EXPORT_SYMBOL_GPL(snd_hda_codec_configure);