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