ALSA: hda - Bind codecs via standard bus
[linux-2.6-block.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 <sound/core.h>
12 #include "hda_codec.h"
13 #include "hda_local.h"
14
15 /* codec vendor labels */
16 struct hda_vendor_id {
17         unsigned int id;
18         const char *name;
19 };
20
21 static struct hda_vendor_id hda_vendor_ids[] = {
22         { 0x1002, "ATI" },
23         { 0x1013, "Cirrus Logic" },
24         { 0x1057, "Motorola" },
25         { 0x1095, "Silicon Image" },
26         { 0x10de, "Nvidia" },
27         { 0x10ec, "Realtek" },
28         { 0x1102, "Creative" },
29         { 0x1106, "VIA" },
30         { 0x111d, "IDT" },
31         { 0x11c1, "LSI" },
32         { 0x11d4, "Analog Devices" },
33         { 0x13f6, "C-Media" },
34         { 0x14f1, "Conexant" },
35         { 0x17e8, "Chrontel" },
36         { 0x1854, "LG" },
37         { 0x1aec, "Wolfson Microelectronics" },
38         { 0x1af4, "QEMU" },
39         { 0x434d, "C-Media" },
40         { 0x8086, "Intel" },
41         { 0x8384, "SigmaTel" },
42         {} /* terminator */
43 };
44
45 /*
46  * find a matching codec preset
47  */
48 static int hda_bus_match(struct device *dev, struct device_driver *drv)
49 {
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;
56
57         for (preset = driver->preset; preset->id; preset++) {
58                 u32 mask = preset->mask;
59
60                 if (preset->afg && preset->afg != codec->afg)
61                         continue;
62                 if (preset->mfg && preset->mfg != codec->mfg)
63                         continue;
64                 if (!mask)
65                         mask = ~0;
66                 if (preset->id == (id & mask) &&
67                     (!preset->rev || preset->rev == codec->revision_id)) {
68                         codec->preset = preset;
69                         return 1;
70                 }
71         }
72         return 0;
73 }
74
75 /* reset the codec name from the preset */
76 static int codec_refresh_name(struct hda_codec *codec, const char *name)
77 {
78         char tmp[16];
79
80         kfree(codec->chip_name);
81         if (!name) {
82                 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
83                 name = tmp;
84         }
85         codec->chip_name = kstrdup(name, GFP_KERNEL);
86         return codec->chip_name ? 0 : -ENOMEM;
87 }
88
89 static int hda_codec_driver_probe(struct device *dev)
90 {
91         struct hda_codec *codec = dev_to_hda_codec(dev);
92         struct module *owner = dev->driver->owner;
93         int err;
94
95         if (WARN_ON(!codec->preset))
96                 return -EINVAL;
97
98         err = codec_refresh_name(codec, codec->preset->name);
99         if (err < 0)
100                 goto error;
101
102         if (!try_module_get(owner)) {
103                 err = -EINVAL;
104                 goto error;
105         }
106
107         err = codec->preset->patch(codec);
108         if (err < 0) {
109                 module_put(owner);
110                 goto error;
111         }
112
113         return 0;
114
115  error:
116         codec->preset = NULL;
117         memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
118         return err;
119 }
120
121 static int hda_codec_driver_remove(struct device *dev)
122 {
123         struct hda_codec *codec = dev_to_hda_codec(dev);
124
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);
130         return 0;
131 }
132
133 int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
134                                struct module *owner)
135 {
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);
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->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->vendor_id);
162         if (codec_probed(codec))
163                 return;
164         request_module("snd-hda-codec-id:%04x*",
165                        (codec->vendor_id >> 16) & 0xffff);
166         if (codec_probed(codec))
167                 return;
168 #endif
169 }
170
171 /* store the codec vendor name */
172 static int get_codec_vendor_name(struct hda_codec *codec)
173 {
174         const struct hda_vendor_id *c;
175         const char *vendor = NULL;
176         u16 vendor_id = codec->vendor_id >> 16;
177         char tmp[16];
178
179         for (c = hda_vendor_ids; c->id; c++) {
180                 if (c->id == vendor_id) {
181                         vendor = c->name;
182                         break;
183                 }
184         }
185         if (!vendor) {
186                 sprintf(tmp, "Generic %04x", vendor_id);
187                 vendor = tmp;
188         }
189         codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
190         if (!codec->vendor_name)
191                 return -ENOMEM;
192         return 0;
193 }
194
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)
198 {
199         hda_nid_t nid = codec->start_nid;
200         int i;
201
202         for (i = 0; i < codec->num_nodes; i++, nid++) {
203                 unsigned int wcaps = get_wcaps(codec, nid);
204                 switch (get_wcaps_type(wcaps)) {
205                 case AC_WID_AUD_IN:
206                         return false; /* HDMI parser supports only HDMI out */
207                 case AC_WID_AUD_OUT:
208                         if (!(wcaps & AC_WCAP_DIGITAL))
209                                 return false;
210                         break;
211                 }
212         }
213         return true;
214 }
215 #else
216 /* no HDMI codec parser support */
217 #define is_likely_hdmi_codec(codec)     false
218 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
219
220 static int codec_bind_generic(struct hda_codec *codec)
221 {
222         if (codec->probe_id)
223                 return -ENODEV;
224
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");
229 #endif
230                 if (codec_probed(codec))
231                         return 0;
232         }
233
234         codec->probe_id = HDA_CODEC_ID_GENERIC;
235 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
236         request_module("snd-hda-codec-generic");
237 #endif
238         if (codec_probed(codec))
239                 return 0;
240         return -ENODEV;
241 }
242
243 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
244 #define is_generic_config(codec) \
245         (codec->modelname && !strcmp(codec->modelname, "generic"))
246 #else
247 #define is_generic_config(codec)        0
248 #endif
249
250 /**
251  * snd_hda_codec_configure - (Re-)configure the HD-audio codec
252  * @codec: the HDA codec
253  *
254  * Start parsing of the given codec tree and (re-)initialize the whole
255  * patch instance.
256  *
257  * Returns 0 if successful or a negative error code.
258  */
259 int snd_hda_codec_configure(struct hda_codec *codec)
260 {
261         int err;
262
263         if (!codec->vendor_name) {
264                 err = get_codec_vendor_name(codec);
265                 if (err < 0)
266                         return err;
267         }
268
269         if (is_generic_config(codec))
270                 codec->probe_id = HDA_CODEC_ID_GENERIC;
271         else
272                 codec->probe_id = 0;
273
274         err = device_add(hda_codec_dev(codec));
275         if (err < 0)
276                 return err;
277
278         if (!codec->preset)
279                 codec_bind_module(codec);
280         if (!codec->preset) {
281                 err = codec_bind_generic(codec);
282                 if (err < 0) {
283                         codec_err(codec, "Unable to bind the codec\n");
284                         goto error;
285                 }
286         }
287
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);
293         return 0;
294
295  error:
296         device_del(hda_codec_dev(codec));
297         return err;
298 }
299 EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
300
301 /*
302  * bus registration
303  */
304 struct bus_type snd_hda_bus_type = {
305         .name = "hdaudio",
306         .match = hda_bus_match,
307 };
308
309 static int __init hda_codec_init(void)
310 {
311         return bus_register(&snd_hda_bus_type);
312 }
313
314 static void __exit hda_codec_exit(void)
315 {
316         bus_unregister(&snd_hda_bus_type);
317 }
318
319 module_init(hda_codec_init);
320 module_exit(hda_codec_exit);