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 <sound/core.h>
13 #include "hda_codec.h"
14 #include "hda_local.h"
16 /* codec vendor labels */
17 struct hda_vendor_id
{
22 static struct hda_vendor_id hda_vendor_ids
[] = {
24 { 0x1013, "Cirrus Logic" },
25 { 0x1057, "Motorola" },
26 { 0x1095, "Silicon Image" },
28 { 0x10ec, "Realtek" },
29 { 0x1102, "Creative" },
33 { 0x11d4, "Analog Devices" },
34 { 0x13f6, "C-Media" },
35 { 0x14f1, "Conexant" },
36 { 0x17e8, "Chrontel" },
38 { 0x1aec, "Wolfson Microelectronics" },
40 { 0x434d, "C-Media" },
42 { 0x8384, "SigmaTel" },
47 * find a matching codec preset
49 static int hda_bus_match(struct device
*dev
, struct device_driver
*drv
)
51 struct hda_codec
*codec
= container_of(dev
, struct hda_codec
, dev
);
52 struct hda_codec_driver
*driver
=
53 container_of(drv
, struct hda_codec_driver
, driver
);
54 const struct hda_codec_preset
*preset
;
55 /* check probe_id instead of vendor_id if set */
56 u32 id
= codec
->probe_id
? codec
->probe_id
: codec
->vendor_id
;
58 for (preset
= driver
->preset
; preset
->id
; preset
++) {
59 u32 mask
= preset
->mask
;
61 if (preset
->afg
&& preset
->afg
!= codec
->afg
)
63 if (preset
->mfg
&& preset
->mfg
!= codec
->mfg
)
67 if (preset
->id
== (id
& mask
) &&
68 (!preset
->rev
|| preset
->rev
== codec
->revision_id
)) {
69 codec
->preset
= preset
;
76 /* reset the codec name from the preset */
77 static int codec_refresh_name(struct hda_codec
*codec
, const char *name
)
81 kfree(codec
->chip_name
);
83 sprintf(tmp
, "ID %x", codec
->vendor_id
& 0xffff);
86 codec
->chip_name
= kstrdup(name
, GFP_KERNEL
);
87 return codec
->chip_name
? 0 : -ENOMEM
;
90 static int hda_codec_driver_probe(struct device
*dev
)
92 struct hda_codec
*codec
= dev_to_hda_codec(dev
);
93 struct module
*owner
= dev
->driver
->owner
;
96 if (WARN_ON(!codec
->preset
))
99 err
= codec_refresh_name(codec
, codec
->preset
->name
);
103 if (!try_module_get(owner
)) {
108 err
= codec
->preset
->patch(codec
);
117 codec
->preset
= NULL
;
118 memset(&codec
->patch_ops
, 0, sizeof(codec
->patch_ops
));
122 static int hda_codec_driver_remove(struct device
*dev
)
124 struct hda_codec
*codec
= dev_to_hda_codec(dev
);
126 if (codec
->patch_ops
.free
)
127 codec
->patch_ops
.free(codec
);
128 codec
->preset
= NULL
;
129 memset(&codec
->patch_ops
, 0, sizeof(codec
->patch_ops
));
130 module_put(dev
->driver
->owner
);
134 int __hda_codec_driver_register(struct hda_codec_driver
*drv
, const char *name
,
135 struct module
*owner
)
137 drv
->driver
.name
= name
;
138 drv
->driver
.owner
= owner
;
139 drv
->driver
.bus
= &snd_hda_bus_type
;
140 drv
->driver
.probe
= hda_codec_driver_probe
;
141 drv
->driver
.remove
= hda_codec_driver_remove
;
142 drv
->driver
.pm
= &hda_codec_driver_pm
;
143 return driver_register(&drv
->driver
);
145 EXPORT_SYMBOL_GPL(__hda_codec_driver_register
);
147 void hda_codec_driver_unregister(struct hda_codec_driver
*drv
)
149 driver_unregister(&drv
->driver
);
151 EXPORT_SYMBOL_GPL(hda_codec_driver_unregister
);
153 static inline bool codec_probed(struct hda_codec
*codec
)
155 return device_attach(hda_codec_dev(codec
)) > 0 && codec
->preset
;
158 /* try to auto-load and bind the codec module */
159 static void codec_bind_module(struct hda_codec
*codec
)
162 request_module("snd-hda-codec-id:%08x", codec
->vendor_id
);
163 if (codec_probed(codec
))
165 request_module("snd-hda-codec-id:%04x*",
166 (codec
->vendor_id
>> 16) & 0xffff);
167 if (codec_probed(codec
))
172 /* store the codec vendor name */
173 static int get_codec_vendor_name(struct hda_codec
*codec
)
175 const struct hda_vendor_id
*c
;
176 const char *vendor
= NULL
;
177 u16 vendor_id
= codec
->vendor_id
>> 16;
180 for (c
= hda_vendor_ids
; c
->id
; c
++) {
181 if (c
->id
== vendor_id
) {
187 sprintf(tmp
, "Generic %04x", vendor_id
);
190 codec
->vendor_name
= kstrdup(vendor
, GFP_KERNEL
);
191 if (!codec
->vendor_name
)
196 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
197 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
198 static bool is_likely_hdmi_codec(struct hda_codec
*codec
)
200 hda_nid_t nid
= codec
->start_nid
;
203 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
204 unsigned int wcaps
= get_wcaps(codec
, nid
);
205 switch (get_wcaps_type(wcaps
)) {
207 return false; /* HDMI parser supports only HDMI out */
209 if (!(wcaps
& AC_WCAP_DIGITAL
))
217 /* no HDMI codec parser support */
218 #define is_likely_hdmi_codec(codec) false
219 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
221 static int codec_bind_generic(struct hda_codec
*codec
)
226 if (is_likely_hdmi_codec(codec
)) {
227 codec
->probe_id
= HDA_CODEC_ID_GENERIC_HDMI
;
228 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
229 request_module("snd-hda-codec-hdmi");
231 if (codec_probed(codec
))
235 codec
->probe_id
= HDA_CODEC_ID_GENERIC
;
236 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
237 request_module("snd-hda-codec-generic");
239 if (codec_probed(codec
))
244 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
245 #define is_generic_config(codec) \
246 (codec->modelname && !strcmp(codec->modelname, "generic"))
248 #define is_generic_config(codec) 0
252 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
253 * @codec: the HDA codec
255 * Start parsing of the given codec tree and (re-)initialize the whole
258 * Returns 0 if successful or a negative error code.
260 int snd_hda_codec_configure(struct hda_codec
*codec
)
264 if (!codec
->vendor_name
) {
265 err
= get_codec_vendor_name(codec
);
270 if (is_generic_config(codec
))
271 codec
->probe_id
= HDA_CODEC_ID_GENERIC
;
275 err
= device_add(hda_codec_dev(codec
));
280 codec_bind_module(codec
);
281 if (!codec
->preset
) {
282 err
= codec_bind_generic(codec
);
284 codec_err(codec
, "Unable to bind the codec\n");
289 /* audio codec should override the mixer name */
290 if (codec
->afg
|| !*codec
->card
->mixername
)
291 snprintf(codec
->card
->mixername
,
292 sizeof(codec
->card
->mixername
),
293 "%s %s", codec
->vendor_name
, codec
->chip_name
);
297 device_del(hda_codec_dev(codec
));
300 EXPORT_SYMBOL_GPL(snd_hda_codec_configure
);
305 struct bus_type snd_hda_bus_type
= {
307 .match
= hda_bus_match
,
310 static int __init
hda_codec_init(void)
312 return bus_register(&snd_hda_bus_type
);
315 static void __exit
hda_codec_exit(void)
317 bus_unregister(&snd_hda_bus_type
);
320 module_init(hda_codec_init
);
321 module_exit(hda_codec_exit
);