xref: /linux/sound/hda/common/bind.c (revision 9611c0ce215a66770ccbe5c126bf57ba8c31bcad)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * HD-audio codec driver binding
4  * Copyright (c) Takashi Iwai <tiwai@suse.de>
5  */
6 
7 #include <linux/init.h>
8 #include <linux/slab.h>
9 #include <linux/mutex.h>
10 #include <linux/module.h>
11 #include <linux/export.h>
12 #include <linux/pm.h>
13 #include <sound/core.h>
14 #include <sound/hda_codec.h>
15 #include "hda_local.h"
16 #include "hda_jack.h"
17 
18 /*
19  * find a matching codec id
20  */
21 static int hda_codec_match(struct hdac_device *dev, const struct hdac_driver *drv)
22 {
23 	struct hda_codec *codec = container_of(dev, struct hda_codec, core);
24 	const struct hda_codec_driver *driver =
25 		container_of(drv, struct hda_codec_driver, core);
26 	const struct hda_device_id *list;
27 	/* check probe_id instead of vendor_id if set */
28 	u32 id = codec->probe_id ? codec->probe_id : codec->core.vendor_id;
29 	u32 rev_id = codec->core.revision_id;
30 
31 	for (list = driver->id; list->vendor_id; list++) {
32 		if (list->vendor_id == id &&
33 		    (!list->rev_id || list->rev_id == rev_id)) {
34 			codec->preset = list;
35 			return 1;
36 		}
37 	}
38 	return 0;
39 }
40 
41 /* process an unsolicited event */
42 static void hda_codec_unsol_event(struct hdac_device *dev, unsigned int ev)
43 {
44 	struct hda_codec *codec = container_of(dev, struct hda_codec, core);
45 	struct hda_codec_driver *driver = hda_codec_to_driver(codec);
46 
47 	/* ignore unsol events during shutdown */
48 	if (codec->card->shutdown || codec->bus->shutdown)
49 		return;
50 
51 	/* ignore unsol events during system suspend/resume */
52 	if (codec->core.dev.power.power_state.event != PM_EVENT_ON)
53 		return;
54 
55 	if (driver->ops->unsol_event)
56 		driver->ops->unsol_event(codec, ev);
57 }
58 
59 /**
60  * snd_hda_codec_set_name - set the codec name
61  * @codec: the HDA codec
62  * @name: name string to set
63  */
64 int snd_hda_codec_set_name(struct hda_codec *codec, const char *name)
65 {
66 	int err;
67 
68 	if (!name)
69 		return 0;
70 	err = snd_hdac_device_set_chip_name(&codec->core, name);
71 	if (err < 0)
72 		return err;
73 
74 	/* update the mixer name */
75 	if (!*codec->card->mixername ||
76 	    codec->bus->mixer_assigned >= codec->core.addr) {
77 		snprintf(codec->card->mixername,
78 			 sizeof(codec->card->mixername), "%s %s",
79 			 codec->core.vendor_name, codec->core.chip_name);
80 		codec->bus->mixer_assigned = codec->core.addr;
81 	}
82 
83 	return 0;
84 }
85 EXPORT_SYMBOL_GPL(snd_hda_codec_set_name);
86 
87 static int hda_codec_driver_probe(struct device *dev)
88 {
89 	struct hda_codec *codec = dev_to_hda_codec(dev);
90 	struct module *owner = dev->driver->owner;
91 	struct hda_codec_driver *driver = hda_codec_to_driver(codec);
92 	int err;
93 
94 	if (codec->bus->core.ext_ops) {
95 		if (WARN_ON(!codec->bus->core.ext_ops->hdev_attach))
96 			return -EINVAL;
97 		return codec->bus->core.ext_ops->hdev_attach(&codec->core);
98 	}
99 
100 	if (WARN_ON(!codec->preset))
101 		return -EINVAL;
102 
103 	err = snd_hda_codec_set_name(codec, codec->preset->name);
104 	if (err < 0)
105 		goto error;
106 	err = snd_hdac_regmap_init(&codec->core);
107 	if (err < 0)
108 		goto error;
109 
110 	if (!try_module_get(owner)) {
111 		err = -EINVAL;
112 		goto error;
113 	}
114 
115 	if (WARN_ON(!(driver->ops && driver->ops->probe))) {
116 		err = -EINVAL;
117 		goto error_module_put;
118 	}
119 
120 	err = driver->ops->probe(codec, codec->preset);
121 	if (err < 0)
122 		goto error_module_put;
123 	err = snd_hda_codec_build_pcms(codec);
124 	if (err < 0)
125 		goto error_module;
126 	err = snd_hda_codec_build_controls(codec);
127 	if (err < 0)
128 		goto error_module;
129 	/* only register after the bus probe finished; otherwise it's racy */
130 	if (!codec->bus->bus_probing && codec->card->registered) {
131 		err = snd_card_register(codec->card);
132 		if (err < 0)
133 			goto error_module;
134 		snd_hda_codec_register(codec);
135 	}
136 
137 	codec->core.lazy_cache = true;
138 	return 0;
139 
140  error_module:
141 	if (driver->ops->remove)
142 		driver->ops->remove(codec);
143  error_module_put:
144 	module_put(owner);
145 
146  error:
147 	snd_hda_codec_cleanup_for_unbind(codec);
148 	codec->preset = NULL;
149 	return err;
150 }
151 
152 static int hda_codec_driver_remove(struct device *dev)
153 {
154 	struct hda_codec *codec = dev_to_hda_codec(dev);
155 	struct hda_codec_driver *driver = hda_codec_to_driver(codec);
156 
157 	if (codec->bus->core.ext_ops) {
158 		if (WARN_ON(!codec->bus->core.ext_ops->hdev_detach))
159 			return -EINVAL;
160 		return codec->bus->core.ext_ops->hdev_detach(&codec->core);
161 	}
162 
163 	snd_hda_codec_disconnect_pcms(codec);
164 	snd_hda_jack_tbl_disconnect(codec);
165 	snd_refcount_sync(&codec->pcm_ref);
166 	snd_power_sync_ref(codec->bus->card);
167 
168 	if (driver->ops->remove)
169 		driver->ops->remove(codec);
170 	snd_hda_codec_cleanup_for_unbind(codec);
171 	codec->preset = NULL;
172 	module_put(dev->driver->owner);
173 	return 0;
174 }
175 
176 static void hda_codec_driver_shutdown(struct device *dev)
177 {
178 	snd_hda_codec_shutdown(dev_to_hda_codec(dev));
179 }
180 
181 int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
182 			       struct module *owner)
183 {
184 	drv->core.driver.name = name;
185 	drv->core.driver.owner = owner;
186 	drv->core.driver.bus = &snd_hda_bus_type;
187 	drv->core.driver.probe = hda_codec_driver_probe;
188 	drv->core.driver.remove = hda_codec_driver_remove;
189 	drv->core.driver.shutdown = hda_codec_driver_shutdown;
190 	drv->core.driver.pm = pm_ptr(&hda_codec_driver_pm);
191 	drv->core.type = HDA_DEV_LEGACY;
192 	drv->core.match = hda_codec_match;
193 	drv->core.unsol_event = hda_codec_unsol_event;
194 	return driver_register(&drv->core.driver);
195 }
196 EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
197 
198 void hda_codec_driver_unregister(struct hda_codec_driver *drv)
199 {
200 	driver_unregister(&drv->core.driver);
201 }
202 EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);
203 
204 static inline bool codec_probed(struct hda_codec *codec)
205 {
206 	return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
207 }
208 
209 /* try to auto-load codec module */
210 static void request_codec_module(struct hda_codec *codec)
211 {
212 #ifdef MODULE
213 	char modalias[32];
214 	const char *mod = NULL;
215 
216 	switch (codec->probe_id) {
217 	case HDA_CODEC_ID_GENERIC_HDMI:
218 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
219 		mod = "snd-hda-codec-hdmi";
220 #endif
221 		break;
222 	case HDA_CODEC_ID_GENERIC:
223 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
224 		mod = "snd-hda-codec-generic";
225 #endif
226 		break;
227 	default:
228 		snd_hdac_codec_modalias(&codec->core, modalias, sizeof(modalias));
229 		mod = modalias;
230 		break;
231 	}
232 
233 	if (mod)
234 		request_module(mod);
235 #endif /* MODULE */
236 }
237 
238 /* try to auto-load and bind the codec module */
239 static void codec_bind_module(struct hda_codec *codec)
240 {
241 #ifdef MODULE
242 	request_codec_module(codec);
243 	if (codec_probed(codec))
244 		return;
245 #endif
246 }
247 
248 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
249 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
250 static bool is_likely_hdmi_codec(struct hda_codec *codec)
251 {
252 	hda_nid_t nid;
253 
254 	/*
255 	 * For ASoC users, if snd_hda_hdmi_codec module is denylisted and any
256 	 * event causes i915 enumeration to fail, ->wcaps remains uninitialized.
257 	 */
258 	if (!codec->wcaps)
259 		return true;
260 
261 	for_each_hda_codec_node(nid, codec) {
262 		unsigned int wcaps = get_wcaps(codec, nid);
263 		switch (get_wcaps_type(wcaps)) {
264 		case AC_WID_AUD_IN:
265 			return false; /* HDMI parser supports only HDMI out */
266 		case AC_WID_AUD_OUT:
267 			if (!(wcaps & AC_WCAP_DIGITAL))
268 				return false;
269 			break;
270 		}
271 	}
272 	return true;
273 }
274 #else
275 /* no HDMI codec parser support */
276 #define is_likely_hdmi_codec(codec)	false
277 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
278 
279 static int codec_bind_generic(struct hda_codec *codec)
280 {
281 	if (codec->probe_id)
282 		return -ENODEV;
283 
284 	if (is_likely_hdmi_codec(codec)) {
285 		codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
286 		request_codec_module(codec);
287 		if (codec_probed(codec))
288 			return 0;
289 	}
290 
291 	codec->probe_id = HDA_CODEC_ID_GENERIC;
292 	request_codec_module(codec);
293 	if (codec_probed(codec))
294 		return 0;
295 	return -ENODEV;
296 }
297 
298 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
299 #define is_generic_config(codec) \
300 	(codec->modelname && !strcmp(codec->modelname, "generic"))
301 #else
302 #define is_generic_config(codec)	0
303 #endif
304 
305 /**
306  * snd_hda_codec_configure - (Re-)configure the HD-audio codec
307  * @codec: the HDA codec
308  *
309  * Start parsing of the given codec tree and (re-)initialize the whole
310  * codec driver binding.
311  *
312  * Returns 0 if successful or a negative error code.
313  */
314 int snd_hda_codec_configure(struct hda_codec *codec)
315 {
316 	int err;
317 
318 	if (codec->configured)
319 		return 0;
320 
321 	if (is_generic_config(codec))
322 		codec->probe_id = HDA_CODEC_ID_GENERIC;
323 	else
324 		codec->probe_id = 0;
325 
326 	if (!device_is_registered(&codec->core.dev)) {
327 		err = snd_hdac_device_register(&codec->core);
328 		if (err < 0)
329 			return err;
330 	}
331 
332 	if (!codec->preset)
333 		codec_bind_module(codec);
334 	if (!codec->preset) {
335 		err = codec_bind_generic(codec);
336 		if (err < 0) {
337 			codec_dbg(codec, "Unable to bind the codec\n");
338 			return err;
339 		}
340 	}
341 
342 	codec->configured = 1;
343 	return 0;
344 }
345 EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
346