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