blob: e3bd2807b64469ca2e03e5f11dd208a4c41fbd87 [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 Iwaib2a0baf2015-03-05 17:21:32 +010012#include <linux/pm_runtime.h>
Takashi Iwaid8a766a2015-02-17 15:25:37 +010013#include <sound/core.h>
14#include "hda_codec.h"
15#include "hda_local.h"
16
17/* codec vendor labels */
18struct hda_vendor_id {
19 unsigned int id;
20 const char *name;
21};
22
23static 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 */
Takashi Iwaie3d280f2015-02-17 21:46:37 +010050static int hda_codec_match(struct hdac_device *dev, struct hdac_driver *drv)
Takashi Iwaid8a766a2015-02-17 15:25:37 +010051{
Takashi Iwaie3d280f2015-02-17 21:46:37 +010052 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +010053 struct hda_codec_driver *driver =
Takashi Iwaie3d280f2015-02-17 21:46:37 +010054 container_of(drv, struct hda_codec_driver, core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +010055 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 */
78static 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
91static 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);
Takashi Iwaibcd96552015-02-27 20:44:54 +0100110 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;
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100123 }
124
125 return 0;
126
Takashi Iwaibcd96552015-02-27 20:44:54 +0100127 error_module:
128 module_put(owner);
129
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100130 error:
Takashi Iwaibcd96552015-02-27 20:44:54 +0100131 snd_hda_codec_cleanup_for_unbind(codec);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100132 return err;
133}
134
135static 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);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100141 snd_hda_codec_cleanup_for_unbind(codec);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100142 module_put(dev->driver->owner);
143 return 0;
144}
145
Takashi Iwaib2a0baf2015-03-05 17:21:32 +0100146static 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
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100154int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
155 struct module *owner)
156{
Takashi Iwaie3d280f2015-02-17 21:46:37 +0100157 drv->core.driver.name = name;
158 drv->core.driver.owner = owner;
159 drv->core.driver.bus = &snd_hda_bus_type;
160 drv->core.driver.probe = hda_codec_driver_probe;
161 drv->core.driver.remove = hda_codec_driver_remove;
162 drv->core.driver.shutdown = hda_codec_driver_shutdown;
163 drv->core.driver.pm = &hda_codec_driver_pm;
164 drv->core.type = HDA_DEV_LEGACY;
165 drv->core.match = hda_codec_match;
166 return driver_register(&drv->core.driver);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100167}
168EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
169
170void hda_codec_driver_unregister(struct hda_codec_driver *drv)
171{
Takashi Iwaie3d280f2015-02-17 21:46:37 +0100172 driver_unregister(&drv->core.driver);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100173}
174EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);
175
176static inline bool codec_probed(struct hda_codec *codec)
177{
178 return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
179}
180
181/* try to auto-load and bind the codec module */
182static void codec_bind_module(struct hda_codec *codec)
183{
184#ifdef MODULE
185 request_module("snd-hda-codec-id:%08x", codec->vendor_id);
186 if (codec_probed(codec))
187 return;
188 request_module("snd-hda-codec-id:%04x*",
189 (codec->vendor_id >> 16) & 0xffff);
190 if (codec_probed(codec))
191 return;
192#endif
193}
194
195/* store the codec vendor name */
196static int get_codec_vendor_name(struct hda_codec *codec)
197{
198 const struct hda_vendor_id *c;
199 const char *vendor = NULL;
200 u16 vendor_id = codec->vendor_id >> 16;
201 char tmp[16];
202
203 for (c = hda_vendor_ids; c->id; c++) {
204 if (c->id == vendor_id) {
205 vendor = c->name;
206 break;
207 }
208 }
209 if (!vendor) {
210 sprintf(tmp, "Generic %04x", vendor_id);
211 vendor = tmp;
212 }
213 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
214 if (!codec->vendor_name)
215 return -ENOMEM;
216 return 0;
217}
218
219#if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
220/* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
221static bool is_likely_hdmi_codec(struct hda_codec *codec)
222{
223 hda_nid_t nid = codec->start_nid;
224 int i;
225
226 for (i = 0; i < codec->num_nodes; i++, nid++) {
227 unsigned int wcaps = get_wcaps(codec, nid);
228 switch (get_wcaps_type(wcaps)) {
229 case AC_WID_AUD_IN:
230 return false; /* HDMI parser supports only HDMI out */
231 case AC_WID_AUD_OUT:
232 if (!(wcaps & AC_WCAP_DIGITAL))
233 return false;
234 break;
235 }
236 }
237 return true;
238}
239#else
240/* no HDMI codec parser support */
241#define is_likely_hdmi_codec(codec) false
242#endif /* CONFIG_SND_HDA_CODEC_HDMI */
243
244static int codec_bind_generic(struct hda_codec *codec)
245{
246 if (codec->probe_id)
247 return -ENODEV;
248
249 if (is_likely_hdmi_codec(codec)) {
250 codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
251#if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
252 request_module("snd-hda-codec-hdmi");
253#endif
254 if (codec_probed(codec))
255 return 0;
256 }
257
258 codec->probe_id = HDA_CODEC_ID_GENERIC;
259#if IS_MODULE(CONFIG_SND_HDA_GENERIC)
260 request_module("snd-hda-codec-generic");
261#endif
262 if (codec_probed(codec))
263 return 0;
264 return -ENODEV;
265}
266
267#if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
268#define is_generic_config(codec) \
269 (codec->modelname && !strcmp(codec->modelname, "generic"))
270#else
271#define is_generic_config(codec) 0
272#endif
273
274/**
275 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
276 * @codec: the HDA codec
277 *
278 * Start parsing of the given codec tree and (re-)initialize the whole
279 * patch instance.
280 *
281 * Returns 0 if successful or a negative error code.
282 */
283int snd_hda_codec_configure(struct hda_codec *codec)
284{
285 int err;
286
287 if (!codec->vendor_name) {
288 err = get_codec_vendor_name(codec);
289 if (err < 0)
290 return err;
291 }
292
293 if (is_generic_config(codec))
294 codec->probe_id = HDA_CODEC_ID_GENERIC;
295 else
296 codec->probe_id = 0;
297
298 err = device_add(hda_codec_dev(codec));
299 if (err < 0)
300 return err;
301
302 if (!codec->preset)
303 codec_bind_module(codec);
304 if (!codec->preset) {
305 err = codec_bind_generic(codec);
306 if (err < 0) {
307 codec_err(codec, "Unable to bind the codec\n");
308 goto error;
309 }
310 }
311
312 /* audio codec should override the mixer name */
Takashi Iwai6efdd852015-02-27 16:09:22 +0100313 if (codec->afg || !*codec->card->mixername)
314 snprintf(codec->card->mixername,
315 sizeof(codec->card->mixername),
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100316 "%s %s", codec->vendor_name, codec->chip_name);
317 return 0;
318
319 error:
320 device_del(hda_codec_dev(codec));
321 return err;
322}
323EXPORT_SYMBOL_GPL(snd_hda_codec_configure);