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"
17 /* codec vendor labels */
18 struct hda_vendor_id {
23 static struct hda_vendor_id hda_vendor_ids[] = {
25 { 0x1013, "Cirrus Logic" },
26 { 0x1057, "Motorola" },
27 { 0x1095, "Silicon Image" },
29 { 0x10ec, "Realtek" },
30 { 0x1102, "Creative" },
34 { 0x11d4, "Analog Devices" },
35 { 0x13f6, "C-Media" },
36 { 0x14f1, "Conexant" },
37 { 0x17e8, "Chrontel" },
39 { 0x1aec, "Wolfson Microelectronics" },
41 { 0x434d, "C-Media" },
43 { 0x8384, "SigmaTel" },
48 * find a matching codec preset
50 static int hda_codec_match(struct hdac_device *dev, struct hdac_driver *drv)
52 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
53 struct hda_codec_driver *driver =
54 container_of(drv, struct hda_codec_driver, core);
55 const struct hda_codec_preset *preset;
56 /* check probe_id instead of vendor_id if set */
57 u32 id = codec->probe_id ? codec->probe_id : codec->vendor_id;
59 for (preset = driver->preset; preset->id; preset++) {
60 u32 mask = preset->mask;
62 if (preset->afg && preset->afg != codec->afg)
64 if (preset->mfg && preset->mfg != codec->mfg)
68 if (preset->id == (id & mask) &&
69 (!preset->rev || preset->rev == codec->revision_id)) {
70 codec->preset = preset;
77 /* reset the codec name from the preset */
78 static int codec_refresh_name(struct hda_codec *codec, const char *name)
82 kfree(codec->chip_name);
84 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
87 codec->chip_name = kstrdup(name, GFP_KERNEL);
88 return codec->chip_name ? 0 : -ENOMEM;
91 static int hda_codec_driver_probe(struct device *dev)
93 struct hda_codec *codec = dev_to_hda_codec(dev);
94 struct module *owner = dev->driver->owner;
97 if (WARN_ON(!codec->preset))
100 err = codec_refresh_name(codec, codec->preset->name);
104 if (!try_module_get(owner)) {
109 err = codec->preset->patch(codec);
113 err = snd_hda_codec_build_pcms(codec);
116 err = snd_hda_codec_build_controls(codec);
119 if (codec->card->registered) {
120 err = snd_card_register(codec->card);
131 snd_hda_codec_cleanup_for_unbind(codec);
135 static int hda_codec_driver_remove(struct device *dev)
137 struct hda_codec *codec = dev_to_hda_codec(dev);
139 if (codec->patch_ops.free)
140 codec->patch_ops.free(codec);
141 snd_hda_codec_cleanup_for_unbind(codec);
142 module_put(dev->driver->owner);
146 static void hda_codec_driver_shutdown(struct device *dev)
148 struct hda_codec *codec = dev_to_hda_codec(dev);
150 if (!pm_runtime_suspended(dev) && codec->patch_ops.reboot_notify)
151 codec->patch_ops.reboot_notify(codec);
154 int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
155 struct module *owner)
157 drv->core.driver.name = name;
158 drv->core.driver.owner = owner;
159 drv->core.driver.bus = &snd_hda_bus_type;
160 drv->core.driver.probe = hda_codec_driver_probe;
161 drv->core.driver.remove = hda_codec_driver_remove;
162 drv->core.driver.shutdown = hda_codec_driver_shutdown;
163 drv->core.driver.pm = &hda_codec_driver_pm;
164 drv->core.type = HDA_DEV_LEGACY;
165 drv->core.match = hda_codec_match;
166 return driver_register(&drv->core.driver);
168 EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
170 void hda_codec_driver_unregister(struct hda_codec_driver *drv)
172 driver_unregister(&drv->core.driver);
174 EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);
176 static inline bool codec_probed(struct hda_codec *codec)
178 return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
181 /* try to auto-load and bind the codec module */
182 static void codec_bind_module(struct hda_codec *codec)
185 request_module("snd-hda-codec-id:%08x", codec->vendor_id);
186 if (codec_probed(codec))
188 request_module("snd-hda-codec-id:%04x*",
189 (codec->vendor_id >> 16) & 0xffff);
190 if (codec_probed(codec))
195 /* store the codec vendor name */
196 static int get_codec_vendor_name(struct hda_codec *codec)
198 const struct hda_vendor_id *c;
199 const char *vendor = NULL;
200 u16 vendor_id = codec->vendor_id >> 16;
203 for (c = hda_vendor_ids; c->id; c++) {
204 if (c->id == vendor_id) {
210 sprintf(tmp, "Generic %04x", vendor_id);
213 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
214 if (!codec->vendor_name)
219 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
220 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
221 static bool is_likely_hdmi_codec(struct hda_codec *codec)
223 hda_nid_t nid = codec->start_nid;
226 for (i = 0; i < codec->num_nodes; i++, nid++) {
227 unsigned int wcaps = get_wcaps(codec, nid);
228 switch (get_wcaps_type(wcaps)) {
230 return false; /* HDMI parser supports only HDMI out */
232 if (!(wcaps & AC_WCAP_DIGITAL))
240 /* no HDMI codec parser support */
241 #define is_likely_hdmi_codec(codec) false
242 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
244 static int codec_bind_generic(struct hda_codec *codec)
249 if (is_likely_hdmi_codec(codec)) {
250 codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
251 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
252 request_module("snd-hda-codec-hdmi");
254 if (codec_probed(codec))
258 codec->probe_id = HDA_CODEC_ID_GENERIC;
259 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
260 request_module("snd-hda-codec-generic");
262 if (codec_probed(codec))
267 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
268 #define is_generic_config(codec) \
269 (codec->modelname && !strcmp(codec->modelname, "generic"))
271 #define is_generic_config(codec) 0
275 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
276 * @codec: the HDA codec
278 * Start parsing of the given codec tree and (re-)initialize the whole
281 * Returns 0 if successful or a negative error code.
283 int snd_hda_codec_configure(struct hda_codec *codec)
287 if (!codec->vendor_name) {
288 err = get_codec_vendor_name(codec);
293 if (is_generic_config(codec))
294 codec->probe_id = HDA_CODEC_ID_GENERIC;
298 err = device_add(hda_codec_dev(codec));
303 codec_bind_module(codec);
304 if (!codec->preset) {
305 err = codec_bind_generic(codec);
307 codec_err(codec, "Unable to bind the codec\n");
312 /* audio codec should override the mixer name */
313 if (codec->afg || !*codec->card->mixername)
314 snprintf(codec->card->mixername,
315 sizeof(codec->card->mixername),
316 "%s %s", codec->vendor_name, codec->chip_name);
320 device_del(hda_codec_dev(codec));
323 EXPORT_SYMBOL_GPL(snd_hda_codec_configure);