ef6b8f836a873e6b1f4b9da5ac980f6414f20d90
[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 <linux/pm_runtime.h>
13 #include <sound/core.h>
14 #include "hda_codec.h"
15 #include "hda_local.h"
16
17 /*
18  * find a matching codec preset
19  */
20 static int hda_codec_match(struct hdac_device *dev, struct hdac_driver *drv)
21 {
22         struct hda_codec *codec = container_of(dev, struct hda_codec, core);
23         struct hda_codec_driver *driver =
24                 container_of(drv, struct hda_codec_driver, core);
25         const struct hda_codec_preset *preset;
26         /* check probe_id instead of vendor_id if set */
27         u32 id = codec->probe_id ? codec->probe_id : codec->core.vendor_id;
28
29         for (preset = driver->preset; preset->id; preset++) {
30                 if (preset->id == id &&
31                     (!preset->rev || preset->rev == codec->core.revision_id)) {
32                         codec->preset = preset;
33                         return 1;
34                 }
35         }
36         return 0;
37 }
38
39 /* process an unsolicited event */
40 static void hda_codec_unsol_event(struct hdac_device *dev, unsigned int ev)
41 {
42         struct hda_codec *codec = container_of(dev, struct hda_codec, core);
43
44         if (codec->patch_ops.unsol_event)
45                 codec->patch_ops.unsol_event(codec, ev);
46 }
47
48 /**
49  * snd_hda_codec_set_name - set the codec name
50  * @codec: the HDA codec
51  * @name: name string to set
52  */
53 int snd_hda_codec_set_name(struct hda_codec *codec, const char *name)
54 {
55         int err;
56
57         if (!name)
58                 return 0;
59         err = snd_hdac_device_set_chip_name(&codec->core, name);
60         if (err < 0)
61                 return err;
62
63         /* update the mixer name */
64         if (!*codec->card->mixername) {
65                 snprintf(codec->card->mixername,
66                          sizeof(codec->card->mixername), "%s %s",
67                          codec->core.vendor_name, codec->core.chip_name);
68         }
69
70         return 0;
71 }
72 EXPORT_SYMBOL_GPL(snd_hda_codec_set_name);
73
74 static int hda_codec_driver_probe(struct device *dev)
75 {
76         struct hda_codec *codec = dev_to_hda_codec(dev);
77         struct module *owner = dev->driver->owner;
78         int err;
79
80         if (WARN_ON(!codec->preset))
81                 return -EINVAL;
82
83         err = snd_hda_codec_set_name(codec, codec->preset->name);
84         if (err < 0)
85                 goto error;
86         err = snd_hdac_regmap_init(&codec->core);
87         if (err < 0)
88                 goto error;
89
90         if (!try_module_get(owner)) {
91                 err = -EINVAL;
92                 goto error;
93         }
94
95         err = codec->preset->patch(codec);
96         if (err < 0)
97                 goto error_module;
98
99         err = snd_hda_codec_build_pcms(codec);
100         if (err < 0)
101                 goto error_module;
102         err = snd_hda_codec_build_controls(codec);
103         if (err < 0)
104                 goto error_module;
105         if (codec->card->registered) {
106                 err = snd_card_register(codec->card);
107                 if (err < 0)
108                         goto error_module;
109                 snd_hda_codec_register(codec);
110         }
111
112         codec->core.lazy_cache = true;
113         return 0;
114
115  error_module:
116         module_put(owner);
117
118  error:
119         snd_hda_codec_cleanup_for_unbind(codec);
120         return err;
121 }
122
123 static int hda_codec_driver_remove(struct device *dev)
124 {
125         struct hda_codec *codec = dev_to_hda_codec(dev);
126
127         if (codec->patch_ops.free)
128                 codec->patch_ops.free(codec);
129         snd_hda_codec_cleanup_for_unbind(codec);
130         module_put(dev->driver->owner);
131         return 0;
132 }
133
134 static void hda_codec_driver_shutdown(struct device *dev)
135 {
136         struct hda_codec *codec = dev_to_hda_codec(dev);
137
138         if (!pm_runtime_suspended(dev) && codec->patch_ops.reboot_notify)
139                 codec->patch_ops.reboot_notify(codec);
140 }
141
142 int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
143                                struct module *owner)
144 {
145         drv->core.driver.name = name;
146         drv->core.driver.owner = owner;
147         drv->core.driver.bus = &snd_hda_bus_type;
148         drv->core.driver.probe = hda_codec_driver_probe;
149         drv->core.driver.remove = hda_codec_driver_remove;
150         drv->core.driver.shutdown = hda_codec_driver_shutdown;
151         drv->core.driver.pm = &hda_codec_driver_pm;
152         drv->core.type = HDA_DEV_LEGACY;
153         drv->core.match = hda_codec_match;
154         drv->core.unsol_event = hda_codec_unsol_event;
155         return driver_register(&drv->core.driver);
156 }
157 EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
158
159 void hda_codec_driver_unregister(struct hda_codec_driver *drv)
160 {
161         driver_unregister(&drv->core.driver);
162 }
163 EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);
164
165 static inline bool codec_probed(struct hda_codec *codec)
166 {
167         return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
168 }
169
170 /* try to auto-load and bind the codec module */
171 static void codec_bind_module(struct hda_codec *codec)
172 {
173 #ifdef MODULE
174         request_module("snd-hda-codec-id:%08x", codec->core.vendor_id);
175         if (codec_probed(codec))
176                 return;
177         request_module("snd-hda-codec-id:%04x*",
178                        (codec->core.vendor_id >> 16) & 0xffff);
179         if (codec_probed(codec))
180                 return;
181 #endif
182 }
183
184 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
185 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
186 static bool is_likely_hdmi_codec(struct hda_codec *codec)
187 {
188         hda_nid_t nid;
189
190         for_each_hda_codec_node(nid, codec) {
191                 unsigned int wcaps = get_wcaps(codec, nid);
192                 switch (get_wcaps_type(wcaps)) {
193                 case AC_WID_AUD_IN:
194                         return false; /* HDMI parser supports only HDMI out */
195                 case AC_WID_AUD_OUT:
196                         if (!(wcaps & AC_WCAP_DIGITAL))
197                                 return false;
198                         break;
199                 }
200         }
201         return true;
202 }
203 #else
204 /* no HDMI codec parser support */
205 #define is_likely_hdmi_codec(codec)     false
206 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
207
208 static int codec_bind_generic(struct hda_codec *codec)
209 {
210         if (codec->probe_id)
211                 return -ENODEV;
212
213         if (is_likely_hdmi_codec(codec)) {
214                 codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
215 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
216                 request_module("snd-hda-codec-hdmi");
217 #endif
218                 if (codec_probed(codec))
219                         return 0;
220         }
221
222         codec->probe_id = HDA_CODEC_ID_GENERIC;
223 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
224         request_module("snd-hda-codec-generic");
225 #endif
226         if (codec_probed(codec))
227                 return 0;
228         return -ENODEV;
229 }
230
231 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
232 #define is_generic_config(codec) \
233         (codec->modelname && !strcmp(codec->modelname, "generic"))
234 #else
235 #define is_generic_config(codec)        0
236 #endif
237
238 /**
239  * snd_hda_codec_configure - (Re-)configure the HD-audio codec
240  * @codec: the HDA codec
241  *
242  * Start parsing of the given codec tree and (re-)initialize the whole
243  * patch instance.
244  *
245  * Returns 0 if successful or a negative error code.
246  */
247 int snd_hda_codec_configure(struct hda_codec *codec)
248 {
249         int err;
250
251         if (is_generic_config(codec))
252                 codec->probe_id = HDA_CODEC_ID_GENERIC;
253         else
254                 codec->probe_id = 0;
255
256         err = snd_hdac_device_register(&codec->core);
257         if (err < 0)
258                 return err;
259
260         if (!codec->preset)
261                 codec_bind_module(codec);
262         if (!codec->preset) {
263                 err = codec_bind_generic(codec);
264                 if (err < 0) {
265                         codec_err(codec, "Unable to bind the codec\n");
266                         goto error;
267                 }
268         }
269
270         return 0;
271
272  error:
273         snd_hdac_device_unregister(&codec->core);
274         return err;
275 }
276 EXPORT_SYMBOL_GPL(snd_hda_codec_configure);