130f672e6f37b2a913471fbc69d3ae4d06d6e228
[deliverable/linux.git] / sound / pci / hda / hda_bind.c
1 /*
2 * HD-audio codec driver binding
3 * Copyright (c) Takashi Iwai <tiwai@suse.de>
4 */
5
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 <linux/pm.h>
12 #include <linux/pm_runtime.h>
13 #include <sound/core.h>
14 #include "hda_codec.h"
15 #include "hda_local.h"
16
17 /*
18 * find a matching codec preset
19 */
20 static int hda_codec_match(struct hdac_device *dev, struct hdac_driver *drv)
21 {
22 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
23 struct hda_codec_driver *driver =
24 container_of(drv, struct hda_codec_driver, core);
25 const struct hda_codec_preset *preset;
26 /* check probe_id instead of vendor_id if set */
27 u32 id = codec->probe_id ? codec->probe_id : codec->core.vendor_id;
28
29 for (preset = driver->preset; preset->id; preset++) {
30 u32 mask = preset->mask;
31
32 if (preset->afg && preset->afg != codec->core.afg)
33 continue;
34 if (preset->mfg && preset->mfg != codec->core.mfg)
35 continue;
36 if (!mask)
37 mask = ~0;
38 if (preset->id == (id & mask) &&
39 (!preset->rev || preset->rev == codec->core.revision_id)) {
40 codec->preset = preset;
41 return 1;
42 }
43 }
44 return 0;
45 }
46
47 /* process an unsolicited event */
48 static void hda_codec_unsol_event(struct hdac_device *dev, unsigned int ev)
49 {
50 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
51
52 if (codec->patch_ops.unsol_event)
53 codec->patch_ops.unsol_event(codec, ev);
54 }
55
56 /* reset the codec name from the preset */
57 static int codec_refresh_name(struct hda_codec *codec, const char *name)
58 {
59 if (name) {
60 kfree(codec->core.chip_name);
61 codec->core.chip_name = kstrdup(name, GFP_KERNEL);
62 }
63 return codec->core.chip_name ? 0 : -ENOMEM;
64 }
65
66 static int hda_codec_driver_probe(struct device *dev)
67 {
68 struct hda_codec *codec = dev_to_hda_codec(dev);
69 struct module *owner = dev->driver->owner;
70 int err;
71
72 if (WARN_ON(!codec->preset))
73 return -EINVAL;
74
75 err = codec_refresh_name(codec, codec->preset->name);
76 if (err < 0)
77 goto error;
78
79 if (!try_module_get(owner)) {
80 err = -EINVAL;
81 goto error;
82 }
83
84 err = codec->preset->patch(codec);
85 if (err < 0)
86 goto error_module;
87
88 err = snd_hda_codec_build_pcms(codec);
89 if (err < 0)
90 goto error_module;
91 err = snd_hda_codec_build_controls(codec);
92 if (err < 0)
93 goto error_module;
94 if (codec->card->registered) {
95 err = snd_card_register(codec->card);
96 if (err < 0)
97 goto error_module;
98 }
99
100 return 0;
101
102 error_module:
103 module_put(owner);
104
105 error:
106 snd_hda_codec_cleanup_for_unbind(codec);
107 return err;
108 }
109
110 static int hda_codec_driver_remove(struct device *dev)
111 {
112 struct hda_codec *codec = dev_to_hda_codec(dev);
113
114 if (codec->patch_ops.free)
115 codec->patch_ops.free(codec);
116 snd_hda_codec_cleanup_for_unbind(codec);
117 module_put(dev->driver->owner);
118 return 0;
119 }
120
121 static void hda_codec_driver_shutdown(struct device *dev)
122 {
123 struct hda_codec *codec = dev_to_hda_codec(dev);
124
125 if (!pm_runtime_suspended(dev) && codec->patch_ops.reboot_notify)
126 codec->patch_ops.reboot_notify(codec);
127 }
128
129 int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
130 struct module *owner)
131 {
132 drv->core.driver.name = name;
133 drv->core.driver.owner = owner;
134 drv->core.driver.bus = &snd_hda_bus_type;
135 drv->core.driver.probe = hda_codec_driver_probe;
136 drv->core.driver.remove = hda_codec_driver_remove;
137 drv->core.driver.shutdown = hda_codec_driver_shutdown;
138 drv->core.driver.pm = &hda_codec_driver_pm;
139 drv->core.type = HDA_DEV_LEGACY;
140 drv->core.match = hda_codec_match;
141 drv->core.unsol_event = hda_codec_unsol_event;
142 return driver_register(&drv->core.driver);
143 }
144 EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
145
146 void hda_codec_driver_unregister(struct hda_codec_driver *drv)
147 {
148 driver_unregister(&drv->core.driver);
149 }
150 EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);
151
152 static inline bool codec_probed(struct hda_codec *codec)
153 {
154 return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
155 }
156
157 /* try to auto-load and bind the codec module */
158 static void codec_bind_module(struct hda_codec *codec)
159 {
160 #ifdef MODULE
161 request_module("snd-hda-codec-id:%08x", codec->core.vendor_id);
162 if (codec_probed(codec))
163 return;
164 request_module("snd-hda-codec-id:%04x*",
165 (codec->core.vendor_id >> 16) & 0xffff);
166 if (codec_probed(codec))
167 return;
168 #endif
169 }
170
171 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
172 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
173 static bool is_likely_hdmi_codec(struct hda_codec *codec)
174 {
175 hda_nid_t nid;
176
177 for_each_hda_codec_node(nid, codec) {
178 unsigned int wcaps = get_wcaps(codec, nid);
179 switch (get_wcaps_type(wcaps)) {
180 case AC_WID_AUD_IN:
181 return false; /* HDMI parser supports only HDMI out */
182 case AC_WID_AUD_OUT:
183 if (!(wcaps & AC_WCAP_DIGITAL))
184 return false;
185 break;
186 }
187 }
188 return true;
189 }
190 #else
191 /* no HDMI codec parser support */
192 #define is_likely_hdmi_codec(codec) false
193 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
194
195 static int codec_bind_generic(struct hda_codec *codec)
196 {
197 if (codec->probe_id)
198 return -ENODEV;
199
200 if (is_likely_hdmi_codec(codec)) {
201 codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
202 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
203 request_module("snd-hda-codec-hdmi");
204 #endif
205 if (codec_probed(codec))
206 return 0;
207 }
208
209 codec->probe_id = HDA_CODEC_ID_GENERIC;
210 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
211 request_module("snd-hda-codec-generic");
212 #endif
213 if (codec_probed(codec))
214 return 0;
215 return -ENODEV;
216 }
217
218 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
219 #define is_generic_config(codec) \
220 (codec->modelname && !strcmp(codec->modelname, "generic"))
221 #else
222 #define is_generic_config(codec) 0
223 #endif
224
225 /**
226 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
227 * @codec: the HDA codec
228 *
229 * Start parsing of the given codec tree and (re-)initialize the whole
230 * patch instance.
231 *
232 * Returns 0 if successful or a negative error code.
233 */
234 int snd_hda_codec_configure(struct hda_codec *codec)
235 {
236 int err;
237
238 if (is_generic_config(codec))
239 codec->probe_id = HDA_CODEC_ID_GENERIC;
240 else
241 codec->probe_id = 0;
242
243 err = snd_hdac_device_register(&codec->core);
244 if (err < 0)
245 return err;
246
247 if (!codec->preset)
248 codec_bind_module(codec);
249 if (!codec->preset) {
250 err = codec_bind_generic(codec);
251 if (err < 0) {
252 codec_err(codec, "Unable to bind the codec\n");
253 goto error;
254 }
255 }
256
257 /* audio codec should override the mixer name */
258 if (codec->core.afg || !*codec->card->mixername)
259 snprintf(codec->card->mixername,
260 sizeof(codec->card->mixername), "%s %s",
261 codec->core.vendor_name, codec->core.chip_name);
262 return 0;
263
264 error:
265 snd_hdac_device_unregister(&codec->core);
266 return err;
267 }
268 EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
This page took 0.137839 seconds and 4 git commands to generate.