ALSA: hda - Move codec suspend/resume to codec driver
[firefly-linux-kernel-4.4.55.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 <sound/core.h>
13 #include "hda_codec.h"
14 #include "hda_local.h"
15
16 /* codec vendor labels */
17 struct hda_vendor_id {
18         unsigned int id;
19         const char *name;
20 };
21
22 static 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  */
49 static 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 */
77 static 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
90 static 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);
109         if (err < 0) {
110                 module_put(owner);
111                 goto error;
112         }
113
114         return 0;
115
116  error:
117         codec->preset = NULL;
118         memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
119         return err;
120 }
121
122 static int hda_codec_driver_remove(struct device *dev)
123 {
124         struct hda_codec *codec = dev_to_hda_codec(dev);
125
126         if (codec->patch_ops.free)
127                 codec->patch_ops.free(codec);
128         codec->preset = NULL;
129         memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
130         module_put(dev->driver->owner);
131         return 0;
132 }
133
134 int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
135                                struct module *owner)
136 {
137         drv->driver.name = name;
138         drv->driver.owner = owner;
139         drv->driver.bus = &snd_hda_bus_type;
140         drv->driver.probe = hda_codec_driver_probe;
141         drv->driver.remove = hda_codec_driver_remove;
142         drv->driver.pm = &hda_codec_driver_pm;
143         return driver_register(&drv->driver);
144 }
145 EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
146
147 void hda_codec_driver_unregister(struct hda_codec_driver *drv)
148 {
149         driver_unregister(&drv->driver);
150 }
151 EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);
152
153 static inline bool codec_probed(struct hda_codec *codec)
154 {
155         return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
156 }
157
158 /* try to auto-load and bind the codec module */
159 static void codec_bind_module(struct hda_codec *codec)
160 {
161 #ifdef MODULE
162         request_module("snd-hda-codec-id:%08x", codec->vendor_id);
163         if (codec_probed(codec))
164                 return;
165         request_module("snd-hda-codec-id:%04x*",
166                        (codec->vendor_id >> 16) & 0xffff);
167         if (codec_probed(codec))
168                 return;
169 #endif
170 }
171
172 /* store the codec vendor name */
173 static int get_codec_vendor_name(struct hda_codec *codec)
174 {
175         const struct hda_vendor_id *c;
176         const char *vendor = NULL;
177         u16 vendor_id = codec->vendor_id >> 16;
178         char tmp[16];
179
180         for (c = hda_vendor_ids; c->id; c++) {
181                 if (c->id == vendor_id) {
182                         vendor = c->name;
183                         break;
184                 }
185         }
186         if (!vendor) {
187                 sprintf(tmp, "Generic %04x", vendor_id);
188                 vendor = tmp;
189         }
190         codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
191         if (!codec->vendor_name)
192                 return -ENOMEM;
193         return 0;
194 }
195
196 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
197 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
198 static bool is_likely_hdmi_codec(struct hda_codec *codec)
199 {
200         hda_nid_t nid = codec->start_nid;
201         int i;
202
203         for (i = 0; i < codec->num_nodes; i++, nid++) {
204                 unsigned int wcaps = get_wcaps(codec, nid);
205                 switch (get_wcaps_type(wcaps)) {
206                 case AC_WID_AUD_IN:
207                         return false; /* HDMI parser supports only HDMI out */
208                 case AC_WID_AUD_OUT:
209                         if (!(wcaps & AC_WCAP_DIGITAL))
210                                 return false;
211                         break;
212                 }
213         }
214         return true;
215 }
216 #else
217 /* no HDMI codec parser support */
218 #define is_likely_hdmi_codec(codec)     false
219 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
220
221 static int codec_bind_generic(struct hda_codec *codec)
222 {
223         if (codec->probe_id)
224                 return -ENODEV;
225
226         if (is_likely_hdmi_codec(codec)) {
227                 codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
228 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
229                 request_module("snd-hda-codec-hdmi");
230 #endif
231                 if (codec_probed(codec))
232                         return 0;
233         }
234
235         codec->probe_id = HDA_CODEC_ID_GENERIC;
236 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
237         request_module("snd-hda-codec-generic");
238 #endif
239         if (codec_probed(codec))
240                 return 0;
241         return -ENODEV;
242 }
243
244 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
245 #define is_generic_config(codec) \
246         (codec->modelname && !strcmp(codec->modelname, "generic"))
247 #else
248 #define is_generic_config(codec)        0
249 #endif
250
251 /**
252  * snd_hda_codec_configure - (Re-)configure the HD-audio codec
253  * @codec: the HDA codec
254  *
255  * Start parsing of the given codec tree and (re-)initialize the whole
256  * patch instance.
257  *
258  * Returns 0 if successful or a negative error code.
259  */
260 int snd_hda_codec_configure(struct hda_codec *codec)
261 {
262         int err;
263
264         if (!codec->vendor_name) {
265                 err = get_codec_vendor_name(codec);
266                 if (err < 0)
267                         return err;
268         }
269
270         if (is_generic_config(codec))
271                 codec->probe_id = HDA_CODEC_ID_GENERIC;
272         else
273                 codec->probe_id = 0;
274
275         err = device_add(hda_codec_dev(codec));
276         if (err < 0)
277                 return err;
278
279         if (!codec->preset)
280                 codec_bind_module(codec);
281         if (!codec->preset) {
282                 err = codec_bind_generic(codec);
283                 if (err < 0) {
284                         codec_err(codec, "Unable to bind the codec\n");
285                         goto error;
286                 }
287         }
288
289         /* audio codec should override the mixer name */
290         if (codec->afg || !*codec->bus->card->mixername)
291                 snprintf(codec->bus->card->mixername,
292                          sizeof(codec->bus->card->mixername),
293                          "%s %s", codec->vendor_name, codec->chip_name);
294         return 0;
295
296  error:
297         device_del(hda_codec_dev(codec));
298         return err;
299 }
300 EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
301
302 /*
303  * bus registration
304  */
305 struct bus_type snd_hda_bus_type = {
306         .name = "hdaudio",
307         .match = hda_bus_match,
308 };
309
310 static int __init hda_codec_init(void)
311 {
312         return bus_register(&snd_hda_bus_type);
313 }
314
315 static void __exit hda_codec_exit(void)
316 {
317         bus_unregister(&snd_hda_bus_type);
318 }
319
320 module_init(hda_codec_init);
321 module_exit(hda_codec_exit);