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>
11 #include <sound/core.h>
12 #include "hda_codec.h"
13 #include "hda_local.h"
15 /* codec vendor labels */
16 struct hda_vendor_id {
21 static struct hda_vendor_id hda_vendor_ids[] = {
23 { 0x1013, "Cirrus Logic" },
24 { 0x1057, "Motorola" },
25 { 0x1095, "Silicon Image" },
27 { 0x10ec, "Realtek" },
28 { 0x1102, "Creative" },
32 { 0x11d4, "Analog Devices" },
33 { 0x13f6, "C-Media" },
34 { 0x14f1, "Conexant" },
35 { 0x17e8, "Chrontel" },
37 { 0x1aec, "Wolfson Microelectronics" },
39 { 0x434d, "C-Media" },
41 { 0x8384, "SigmaTel" },
46 * find a matching codec preset
48 static int hda_bus_match(struct device *dev, struct device_driver *drv)
50 struct hda_codec *codec = container_of(dev, struct hda_codec, dev);
51 struct hda_codec_driver *driver =
52 container_of(drv, struct hda_codec_driver, driver);
53 const struct hda_codec_preset *preset;
54 /* check probe_id instead of vendor_id if set */
55 u32 id = codec->probe_id ? codec->probe_id : codec->vendor_id;
57 for (preset = driver->preset; preset->id; preset++) {
58 u32 mask = preset->mask;
60 if (preset->afg && preset->afg != codec->afg)
62 if (preset->mfg && preset->mfg != codec->mfg)
66 if (preset->id == (id & mask) &&
67 (!preset->rev || preset->rev == codec->revision_id)) {
68 codec->preset = preset;
75 /* reset the codec name from the preset */
76 static int codec_refresh_name(struct hda_codec *codec, const char *name)
80 kfree(codec->chip_name);
82 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
85 codec->chip_name = kstrdup(name, GFP_KERNEL);
86 return codec->chip_name ? 0 : -ENOMEM;
89 static int hda_codec_driver_probe(struct device *dev)
91 struct hda_codec *codec = dev_to_hda_codec(dev);
92 struct module *owner = dev->driver->owner;
95 if (WARN_ON(!codec->preset))
98 err = codec_refresh_name(codec, codec->preset->name);
102 if (!try_module_get(owner)) {
107 err = codec->preset->patch(codec);
116 codec->preset = NULL;
117 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
121 static int hda_codec_driver_remove(struct device *dev)
123 struct hda_codec *codec = dev_to_hda_codec(dev);
125 if (codec->patch_ops.free)
126 codec->patch_ops.free(codec);
127 codec->preset = NULL;
128 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
129 module_put(dev->driver->owner);
133 int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
134 struct module *owner)
136 drv->driver.name = name;
137 drv->driver.owner = owner;
138 drv->driver.bus = &snd_hda_bus_type;
139 drv->driver.probe = hda_codec_driver_probe;
140 drv->driver.remove = hda_codec_driver_remove;
141 /* TODO: PM and others */
142 return driver_register(&drv->driver);
144 EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
146 void hda_codec_driver_unregister(struct hda_codec_driver *drv)
148 driver_unregister(&drv->driver);
150 EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);
152 static inline bool codec_probed(struct hda_codec *codec)
154 return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
157 /* try to auto-load and bind the codec module */
158 static void codec_bind_module(struct hda_codec *codec)
161 request_module("snd-hda-codec-id:%08x", codec->vendor_id);
162 if (codec_probed(codec))
164 request_module("snd-hda-codec-id:%04x*",
165 (codec->vendor_id >> 16) & 0xffff);
166 if (codec_probed(codec))
171 /* store the codec vendor name */
172 static int get_codec_vendor_name(struct hda_codec *codec)
174 const struct hda_vendor_id *c;
175 const char *vendor = NULL;
176 u16 vendor_id = codec->vendor_id >> 16;
179 for (c = hda_vendor_ids; c->id; c++) {
180 if (c->id == vendor_id) {
186 sprintf(tmp, "Generic %04x", vendor_id);
189 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
190 if (!codec->vendor_name)
195 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
196 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
197 static bool is_likely_hdmi_codec(struct hda_codec *codec)
199 hda_nid_t nid = codec->start_nid;
202 for (i = 0; i < codec->num_nodes; i++, nid++) {
203 unsigned int wcaps = get_wcaps(codec, nid);
204 switch (get_wcaps_type(wcaps)) {
206 return false; /* HDMI parser supports only HDMI out */
208 if (!(wcaps & AC_WCAP_DIGITAL))
216 /* no HDMI codec parser support */
217 #define is_likely_hdmi_codec(codec) false
218 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
220 static int codec_bind_generic(struct hda_codec *codec)
225 if (is_likely_hdmi_codec(codec)) {
226 codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
227 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
228 request_module("snd-hda-codec-hdmi");
230 if (codec_probed(codec))
234 codec->probe_id = HDA_CODEC_ID_GENERIC;
235 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
236 request_module("snd-hda-codec-generic");
238 if (codec_probed(codec))
243 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
244 #define is_generic_config(codec) \
245 (codec->modelname && !strcmp(codec->modelname, "generic"))
247 #define is_generic_config(codec) 0
251 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
252 * @codec: the HDA codec
254 * Start parsing of the given codec tree and (re-)initialize the whole
257 * Returns 0 if successful or a negative error code.
259 int snd_hda_codec_configure(struct hda_codec *codec)
263 if (!codec->vendor_name) {
264 err = get_codec_vendor_name(codec);
269 if (is_generic_config(codec))
270 codec->probe_id = HDA_CODEC_ID_GENERIC;
274 err = device_add(hda_codec_dev(codec));
279 codec_bind_module(codec);
280 if (!codec->preset) {
281 err = codec_bind_generic(codec);
283 codec_err(codec, "Unable to bind the codec\n");
288 /* audio codec should override the mixer name */
289 if (codec->afg || !*codec->bus->card->mixername)
290 snprintf(codec->bus->card->mixername,
291 sizeof(codec->bus->card->mixername),
292 "%s %s", codec->vendor_name, codec->chip_name);
296 device_del(hda_codec_dev(codec));
299 EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
304 struct bus_type snd_hda_bus_type = {
306 .match = hda_bus_match,
309 static int __init hda_codec_init(void)
311 return bus_register(&snd_hda_bus_type);
314 static void __exit hda_codec_exit(void)
316 bus_unregister(&snd_hda_bus_type);
319 module_init(hda_codec_init);
320 module_exit(hda_codec_exit);