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