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"
19 * find a matching codec id
21 static int hda_codec_match(struct hdac_device *dev, struct hdac_driver *drv)
23 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
24 struct hda_codec_driver *driver =
25 container_of(drv, struct hda_codec_driver, core);
26 const struct hda_device_id *list;
27 /* check probe_id instead of vendor_id if set */
28 u32 id = codec->probe_id ? codec->probe_id : codec->core.vendor_id;
29 u32 rev_id = codec->core.revision_id;
31 for (list = driver->id; list->vendor_id; list++) {
32 if (list->vendor_id == id &&
33 (!list->rev_id || list->rev_id == rev_id)) {
41 /* process an unsolicited event */
42 static void hda_codec_unsol_event(struct hdac_device *dev, unsigned int ev)
44 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
46 /* ignore unsol events during shutdown */
47 if (codec->bus->shutdown)
50 /* ignore unsol events during system suspend/resume */
51 if (codec->core.dev.power.power_state.event != PM_EVENT_ON)
54 if (codec->patch_ops.unsol_event)
55 codec->patch_ops.unsol_event(codec, ev);
59 * snd_hda_codec_set_name - set the codec name
60 * @codec: the HDA codec
61 * @name: name string to set
63 int snd_hda_codec_set_name(struct hda_codec *codec, const char *name)
69 err = snd_hdac_device_set_chip_name(&codec->core, name);
73 /* update the mixer name */
74 if (!*codec->card->mixername ||
75 codec->bus->mixer_assigned >= codec->core.addr) {
76 snprintf(codec->card->mixername,
77 sizeof(codec->card->mixername), "%s %s",
78 codec->core.vendor_name, codec->core.chip_name);
79 codec->bus->mixer_assigned = codec->core.addr;
84 EXPORT_SYMBOL_GPL(snd_hda_codec_set_name);
86 static int hda_codec_driver_probe(struct device *dev)
88 struct hda_codec *codec = dev_to_hda_codec(dev);
89 struct module *owner = dev->driver->owner;
90 hda_codec_patch_t patch;
93 if (codec->bus->core.ext_ops) {
94 if (WARN_ON(!codec->bus->core.ext_ops->hdev_attach))
96 return codec->bus->core.ext_ops->hdev_attach(&codec->core);
99 if (WARN_ON(!codec->preset))
102 err = snd_hda_codec_set_name(codec, codec->preset->name);
105 err = snd_hdac_regmap_init(&codec->core);
109 if (!try_module_get(owner)) {
114 patch = (hda_codec_patch_t)codec->preset->driver_data;
118 goto error_module_put;
121 err = snd_hda_codec_build_pcms(codec);
124 err = snd_hda_codec_build_controls(codec);
127 /* only register after the bus probe finished; otherwise it's racy */
128 if (!codec->bus->bus_probing && codec->card->registered) {
129 err = snd_card_register(codec->card);
132 snd_hda_codec_register(codec);
135 codec->core.lazy_cache = true;
139 if (codec->patch_ops.free)
140 codec->patch_ops.free(codec);
145 snd_hda_codec_cleanup_for_unbind(codec);
146 codec->preset = NULL;
150 static int hda_codec_driver_remove(struct device *dev)
152 struct hda_codec *codec = dev_to_hda_codec(dev);
154 if (codec->bus->core.ext_ops) {
155 if (WARN_ON(!codec->bus->core.ext_ops->hdev_detach))
157 return codec->bus->core.ext_ops->hdev_detach(&codec->core);
160 if (codec->patch_ops.free)
161 codec->patch_ops.free(codec);
162 snd_hda_codec_cleanup_for_unbind(codec);
163 codec->preset = NULL;
164 module_put(dev->driver->owner);
168 static void hda_codec_driver_shutdown(struct device *dev)
170 struct hda_codec *codec = dev_to_hda_codec(dev);
172 if (!pm_runtime_suspended(dev) && codec->patch_ops.reboot_notify)
173 codec->patch_ops.reboot_notify(codec);
176 int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
177 struct module *owner)
179 drv->core.driver.name = name;
180 drv->core.driver.owner = owner;
181 drv->core.driver.bus = &snd_hda_bus_type;
182 drv->core.driver.probe = hda_codec_driver_probe;
183 drv->core.driver.remove = hda_codec_driver_remove;
184 drv->core.driver.shutdown = hda_codec_driver_shutdown;
185 drv->core.driver.pm = &hda_codec_driver_pm;
186 drv->core.type = HDA_DEV_LEGACY;
187 drv->core.match = hda_codec_match;
188 drv->core.unsol_event = hda_codec_unsol_event;
189 return driver_register(&drv->core.driver);
191 EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
193 void hda_codec_driver_unregister(struct hda_codec_driver *drv)
195 driver_unregister(&drv->core.driver);
197 EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);
199 static inline bool codec_probed(struct hda_codec *codec)
201 return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
204 /* try to auto-load codec module */
205 static void request_codec_module(struct hda_codec *codec)
209 const char *mod = NULL;
211 switch (codec->probe_id) {
212 case HDA_CODEC_ID_GENERIC_HDMI:
213 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
214 mod = "snd-hda-codec-hdmi";
217 case HDA_CODEC_ID_GENERIC:
218 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
219 mod = "snd-hda-codec-generic";
223 snd_hdac_codec_modalias(&codec->core, modalias, sizeof(modalias));
233 /* try to auto-load and bind the codec module */
234 static void codec_bind_module(struct hda_codec *codec)
237 request_codec_module(codec);
238 if (codec_probed(codec))
243 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
244 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
245 static bool is_likely_hdmi_codec(struct hda_codec *codec)
249 for_each_hda_codec_node(nid, codec) {
250 unsigned int wcaps = get_wcaps(codec, nid);
251 switch (get_wcaps_type(wcaps)) {
253 return false; /* HDMI parser supports only HDMI out */
255 if (!(wcaps & AC_WCAP_DIGITAL))
263 /* no HDMI codec parser support */
264 #define is_likely_hdmi_codec(codec) false
265 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
267 static int codec_bind_generic(struct hda_codec *codec)
272 if (is_likely_hdmi_codec(codec)) {
273 codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
274 request_codec_module(codec);
275 if (codec_probed(codec))
279 codec->probe_id = HDA_CODEC_ID_GENERIC;
280 request_codec_module(codec);
281 if (codec_probed(codec))
286 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
287 #define is_generic_config(codec) \
288 (codec->modelname && !strcmp(codec->modelname, "generic"))
290 #define is_generic_config(codec) 0
294 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
295 * @codec: the HDA codec
297 * Start parsing of the given codec tree and (re-)initialize the whole
300 * Returns 0 if successful or a negative error code.
302 int snd_hda_codec_configure(struct hda_codec *codec)
306 if (codec->configured)
309 if (is_generic_config(codec))
310 codec->probe_id = HDA_CODEC_ID_GENERIC;
314 if (!device_is_registered(&codec->core.dev)) {
315 err = snd_hdac_device_register(&codec->core);
321 codec_bind_module(codec);
322 if (!codec->preset) {
323 err = codec_bind_generic(codec);
325 codec_dbg(codec, "Unable to bind the codec\n");
330 codec->configured = 1;
333 EXPORT_SYMBOL_GPL(snd_hda_codec_configure);