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