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 /* process an unsolicited event */
78 static void hda_codec_unsol_event(struct hdac_device *dev, unsigned int ev)
80 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
82 if (codec->patch_ops.unsol_event)
83 codec->patch_ops.unsol_event(codec, ev);
86 /* reset the codec name from the preset */
87 static int codec_refresh_name(struct hda_codec *codec, const char *name)
91 kfree(codec->chip_name);
93 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
96 codec->chip_name = kstrdup(name, GFP_KERNEL);
97 return codec->chip_name ? 0 : -ENOMEM;
100 static int hda_codec_driver_probe(struct device *dev)
102 struct hda_codec *codec = dev_to_hda_codec(dev);
103 struct module *owner = dev->driver->owner;
106 if (WARN_ON(!codec->preset))
109 err = codec_refresh_name(codec, codec->preset->name);
113 if (!try_module_get(owner)) {
118 err = codec->preset->patch(codec);
122 err = snd_hda_codec_build_pcms(codec);
125 err = snd_hda_codec_build_controls(codec);
128 if (codec->card->registered) {
129 err = snd_card_register(codec->card);
140 snd_hda_codec_cleanup_for_unbind(codec);
144 static int hda_codec_driver_remove(struct device *dev)
146 struct hda_codec *codec = dev_to_hda_codec(dev);
148 if (codec->patch_ops.free)
149 codec->patch_ops.free(codec);
150 snd_hda_codec_cleanup_for_unbind(codec);
151 module_put(dev->driver->owner);
155 static void hda_codec_driver_shutdown(struct device *dev)
157 struct hda_codec *codec = dev_to_hda_codec(dev);
159 if (!pm_runtime_suspended(dev) && codec->patch_ops.reboot_notify)
160 codec->patch_ops.reboot_notify(codec);
163 int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
164 struct module *owner)
166 drv->core.driver.name = name;
167 drv->core.driver.owner = owner;
168 drv->core.driver.bus = &snd_hda_bus_type;
169 drv->core.driver.probe = hda_codec_driver_probe;
170 drv->core.driver.remove = hda_codec_driver_remove;
171 drv->core.driver.shutdown = hda_codec_driver_shutdown;
172 drv->core.driver.pm = &hda_codec_driver_pm;
173 drv->core.type = HDA_DEV_LEGACY;
174 drv->core.match = hda_codec_match;
175 drv->core.unsol_event = hda_codec_unsol_event;
176 return driver_register(&drv->core.driver);
178 EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
180 void hda_codec_driver_unregister(struct hda_codec_driver *drv)
182 driver_unregister(&drv->core.driver);
184 EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);
186 static inline bool codec_probed(struct hda_codec *codec)
188 return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
191 /* try to auto-load and bind the codec module */
192 static void codec_bind_module(struct hda_codec *codec)
195 request_module("snd-hda-codec-id:%08x", codec->vendor_id);
196 if (codec_probed(codec))
198 request_module("snd-hda-codec-id:%04x*",
199 (codec->vendor_id >> 16) & 0xffff);
200 if (codec_probed(codec))
205 /* store the codec vendor name */
206 static int get_codec_vendor_name(struct hda_codec *codec)
208 const struct hda_vendor_id *c;
209 const char *vendor = NULL;
210 u16 vendor_id = codec->vendor_id >> 16;
213 for (c = hda_vendor_ids; c->id; c++) {
214 if (c->id == vendor_id) {
220 sprintf(tmp, "Generic %04x", vendor_id);
223 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
224 if (!codec->vendor_name)
229 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
230 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
231 static bool is_likely_hdmi_codec(struct hda_codec *codec)
233 hda_nid_t nid = codec->start_nid;
236 for (i = 0; i < codec->num_nodes; i++, nid++) {
237 unsigned int wcaps = get_wcaps(codec, nid);
238 switch (get_wcaps_type(wcaps)) {
240 return false; /* HDMI parser supports only HDMI out */
242 if (!(wcaps & AC_WCAP_DIGITAL))
250 /* no HDMI codec parser support */
251 #define is_likely_hdmi_codec(codec) false
252 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
254 static int codec_bind_generic(struct hda_codec *codec)
259 if (is_likely_hdmi_codec(codec)) {
260 codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
261 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
262 request_module("snd-hda-codec-hdmi");
264 if (codec_probed(codec))
268 codec->probe_id = HDA_CODEC_ID_GENERIC;
269 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
270 request_module("snd-hda-codec-generic");
272 if (codec_probed(codec))
277 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
278 #define is_generic_config(codec) \
279 (codec->modelname && !strcmp(codec->modelname, "generic"))
281 #define is_generic_config(codec) 0
285 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
286 * @codec: the HDA codec
288 * Start parsing of the given codec tree and (re-)initialize the whole
291 * Returns 0 if successful or a negative error code.
293 int snd_hda_codec_configure(struct hda_codec *codec)
297 if (!codec->vendor_name) {
298 err = get_codec_vendor_name(codec);
303 if (is_generic_config(codec))
304 codec->probe_id = HDA_CODEC_ID_GENERIC;
308 err = device_add(hda_codec_dev(codec));
313 codec_bind_module(codec);
314 if (!codec->preset) {
315 err = codec_bind_generic(codec);
317 codec_err(codec, "Unable to bind the codec\n");
322 /* audio codec should override the mixer name */
323 if (codec->afg || !*codec->card->mixername)
324 snprintf(codec->card->mixername,
325 sizeof(codec->card->mixername),
326 "%s %s", codec->vendor_name, codec->chip_name);
330 device_del(hda_codec_dev(codec));
333 EXPORT_SYMBOL_GPL(snd_hda_codec_configure);