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