]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - sound/pci/hda/hda_bind.c
Merge branch 'for-next' into topic/hda-core
[mirror_ubuntu-artful-kernel.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 /* codec vendor labels */
18 struct hda_vendor_id {
19 unsigned int id;
20 const char *name;
21 };
22
23 static struct hda_vendor_id hda_vendor_ids[] = {
24 { 0x1002, "ATI" },
25 { 0x1013, "Cirrus Logic" },
26 { 0x1057, "Motorola" },
27 { 0x1095, "Silicon Image" },
28 { 0x10de, "Nvidia" },
29 { 0x10ec, "Realtek" },
30 { 0x1102, "Creative" },
31 { 0x1106, "VIA" },
32 { 0x111d, "IDT" },
33 { 0x11c1, "LSI" },
34 { 0x11d4, "Analog Devices" },
35 { 0x13f6, "C-Media" },
36 { 0x14f1, "Conexant" },
37 { 0x17e8, "Chrontel" },
38 { 0x1854, "LG" },
39 { 0x1aec, "Wolfson Microelectronics" },
40 { 0x1af4, "QEMU" },
41 { 0x434d, "C-Media" },
42 { 0x8086, "Intel" },
43 { 0x8384, "SigmaTel" },
44 {} /* terminator */
45 };
46
47 /*
48 * find a matching codec preset
49 */
50 static int hda_bus_match(struct device *dev, struct device_driver *drv)
51 {
52 struct hda_codec *codec = container_of(dev, struct hda_codec, dev);
53 struct hda_codec_driver *driver =
54 container_of(drv, struct hda_codec_driver, driver);
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;
58
59 for (preset = driver->preset; preset->id; preset++) {
60 u32 mask = preset->mask;
61
62 if (preset->afg && preset->afg != codec->afg)
63 continue;
64 if (preset->mfg && preset->mfg != codec->mfg)
65 continue;
66 if (!mask)
67 mask = ~0;
68 if (preset->id == (id & mask) &&
69 (!preset->rev || preset->rev == codec->revision_id)) {
70 codec->preset = preset;
71 return 1;
72 }
73 }
74 return 0;
75 }
76
77 /* reset the codec name from the preset */
78 static int codec_refresh_name(struct hda_codec *codec, const char *name)
79 {
80 char tmp[16];
81
82 kfree(codec->chip_name);
83 if (!name) {
84 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
85 name = tmp;
86 }
87 codec->chip_name = kstrdup(name, GFP_KERNEL);
88 return codec->chip_name ? 0 : -ENOMEM;
89 }
90
91 static int hda_codec_driver_probe(struct device *dev)
92 {
93 struct hda_codec *codec = dev_to_hda_codec(dev);
94 struct module *owner = dev->driver->owner;
95 int err;
96
97 if (WARN_ON(!codec->preset))
98 return -EINVAL;
99
100 err = codec_refresh_name(codec, codec->preset->name);
101 if (err < 0)
102 goto error;
103
104 if (!try_module_get(owner)) {
105 err = -EINVAL;
106 goto error;
107 }
108
109 err = codec->preset->patch(codec);
110 if (err < 0)
111 goto error_module;
112
113 err = snd_hda_codec_build_pcms(codec);
114 if (err < 0)
115 goto error_module;
116 err = snd_hda_codec_build_controls(codec);
117 if (err < 0)
118 goto error_module;
119 if (codec->card->registered) {
120 err = snd_card_register(codec->card);
121 if (err < 0)
122 goto error_module;
123 }
124
125 return 0;
126
127 error_module:
128 module_put(owner);
129
130 error:
131 snd_hda_codec_cleanup_for_unbind(codec);
132 return err;
133 }
134
135 static int hda_codec_driver_remove(struct device *dev)
136 {
137 struct hda_codec *codec = dev_to_hda_codec(dev);
138
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);
143 return 0;
144 }
145
146 static void hda_codec_driver_shutdown(struct device *dev)
147 {
148 struct hda_codec *codec = dev_to_hda_codec(dev);
149
150 if (!pm_runtime_suspended(dev) && codec->patch_ops.reboot_notify)
151 codec->patch_ops.reboot_notify(codec);
152 }
153
154 int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
155 struct module *owner)
156 {
157 drv->driver.name = name;
158 drv->driver.owner = owner;
159 drv->driver.bus = &snd_hda_bus_type;
160 drv->driver.probe = hda_codec_driver_probe;
161 drv->driver.remove = hda_codec_driver_remove;
162 drv->driver.shutdown = hda_codec_driver_shutdown;
163 drv->driver.pm = &hda_codec_driver_pm;
164 return driver_register(&drv->driver);
165 }
166 EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
167
168 void hda_codec_driver_unregister(struct hda_codec_driver *drv)
169 {
170 driver_unregister(&drv->driver);
171 }
172 EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);
173
174 static inline bool codec_probed(struct hda_codec *codec)
175 {
176 return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
177 }
178
179 /* try to auto-load and bind the codec module */
180 static void codec_bind_module(struct hda_codec *codec)
181 {
182 #ifdef MODULE
183 request_module("snd-hda-codec-id:%08x", codec->vendor_id);
184 if (codec_probed(codec))
185 return;
186 request_module("snd-hda-codec-id:%04x*",
187 (codec->vendor_id >> 16) & 0xffff);
188 if (codec_probed(codec))
189 return;
190 #endif
191 }
192
193 /* store the codec vendor name */
194 static int get_codec_vendor_name(struct hda_codec *codec)
195 {
196 const struct hda_vendor_id *c;
197 const char *vendor = NULL;
198 u16 vendor_id = codec->vendor_id >> 16;
199 char tmp[16];
200
201 for (c = hda_vendor_ids; c->id; c++) {
202 if (c->id == vendor_id) {
203 vendor = c->name;
204 break;
205 }
206 }
207 if (!vendor) {
208 sprintf(tmp, "Generic %04x", vendor_id);
209 vendor = tmp;
210 }
211 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
212 if (!codec->vendor_name)
213 return -ENOMEM;
214 return 0;
215 }
216
217 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
218 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
219 static bool is_likely_hdmi_codec(struct hda_codec *codec)
220 {
221 hda_nid_t nid = codec->start_nid;
222 int i;
223
224 for (i = 0; i < codec->num_nodes; i++, nid++) {
225 unsigned int wcaps = get_wcaps(codec, nid);
226 switch (get_wcaps_type(wcaps)) {
227 case AC_WID_AUD_IN:
228 return false; /* HDMI parser supports only HDMI out */
229 case AC_WID_AUD_OUT:
230 if (!(wcaps & AC_WCAP_DIGITAL))
231 return false;
232 break;
233 }
234 }
235 return true;
236 }
237 #else
238 /* no HDMI codec parser support */
239 #define is_likely_hdmi_codec(codec) false
240 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
241
242 static int codec_bind_generic(struct hda_codec *codec)
243 {
244 if (codec->probe_id)
245 return -ENODEV;
246
247 if (is_likely_hdmi_codec(codec)) {
248 codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
249 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
250 request_module("snd-hda-codec-hdmi");
251 #endif
252 if (codec_probed(codec))
253 return 0;
254 }
255
256 codec->probe_id = HDA_CODEC_ID_GENERIC;
257 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
258 request_module("snd-hda-codec-generic");
259 #endif
260 if (codec_probed(codec))
261 return 0;
262 return -ENODEV;
263 }
264
265 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
266 #define is_generic_config(codec) \
267 (codec->modelname && !strcmp(codec->modelname, "generic"))
268 #else
269 #define is_generic_config(codec) 0
270 #endif
271
272 /**
273 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
274 * @codec: the HDA codec
275 *
276 * Start parsing of the given codec tree and (re-)initialize the whole
277 * patch instance.
278 *
279 * Returns 0 if successful or a negative error code.
280 */
281 int snd_hda_codec_configure(struct hda_codec *codec)
282 {
283 int err;
284
285 if (!codec->vendor_name) {
286 err = get_codec_vendor_name(codec);
287 if (err < 0)
288 return err;
289 }
290
291 if (is_generic_config(codec))
292 codec->probe_id = HDA_CODEC_ID_GENERIC;
293 else
294 codec->probe_id = 0;
295
296 err = device_add(hda_codec_dev(codec));
297 if (err < 0)
298 return err;
299
300 if (!codec->preset)
301 codec_bind_module(codec);
302 if (!codec->preset) {
303 err = codec_bind_generic(codec);
304 if (err < 0) {
305 codec_err(codec, "Unable to bind the codec\n");
306 goto error;
307 }
308 }
309
310 /* audio codec should override the mixer name */
311 if (codec->afg || !*codec->card->mixername)
312 snprintf(codec->card->mixername,
313 sizeof(codec->card->mixername),
314 "%s %s", codec->vendor_name, codec->chip_name);
315 return 0;
316
317 error:
318 device_del(hda_codec_dev(codec));
319 return err;
320 }
321 EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
322
323 /*
324 * bus registration
325 */
326 struct bus_type snd_hda_bus_type = {
327 .name = "hdaudio",
328 .match = hda_bus_match,
329 };
330
331 static int __init hda_codec_init(void)
332 {
333 return bus_register(&snd_hda_bus_type);
334 }
335
336 static void __exit hda_codec_exit(void)
337 {
338 bus_unregister(&snd_hda_bus_type);
339 }
340
341 module_init(hda_codec_init);
342 module_exit(hda_codec_exit);