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 #include "hda_jack.h"
18 
19 /*
20  * find a matching codec id
21  */
hda_codec_match(struct hdac_device * dev,struct hdac_driver * drv)22 static int hda_codec_match(struct hdac_device *dev, struct hdac_driver *drv)
23 {
24 	struct hda_codec *codec = container_of(dev, struct hda_codec, core);
25 	struct hda_codec_driver *driver =
26 		container_of(drv, struct hda_codec_driver, core);
27 	const struct hda_device_id *list;
28 	/* check probe_id instead of vendor_id if set */
29 	u32 id = codec->probe_id ? codec->probe_id : codec->core.vendor_id;
30 	u32 rev_id = codec->core.revision_id;
31 
32 	for (list = driver->id; list->vendor_id; list++) {
33 		if (list->vendor_id == id &&
34 		    (!list->rev_id || list->rev_id == rev_id)) {
35 			codec->preset = list;
36 			return 1;
37 		}
38 	}
39 	return 0;
40 }
41 
42 /* process an unsolicited event */
hda_codec_unsol_event(struct hdac_device * dev,unsigned int ev)43 static void hda_codec_unsol_event(struct hdac_device *dev, unsigned int ev)
44 {
45 	struct hda_codec *codec = container_of(dev, struct hda_codec, core);
46 
47 	/* ignore unsol events during shutdown */
48 	if (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 (codec->patch_ops.unsol_event)
56 		codec->patch_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  */
snd_hda_codec_set_name(struct hda_codec * codec,const char * name)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 
hda_codec_driver_probe(struct device * dev)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 	hda_codec_patch_t patch;
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 	patch = (hda_codec_patch_t)codec->preset->driver_data;
116 	if (patch) {
117 		err = patch(codec);
118 		if (err < 0)
119 			goto error_module_put;
120 	}
121 
122 	err = snd_hda_codec_build_pcms(codec);
123 	if (err < 0)
124 		goto error_module;
125 	err = snd_hda_codec_build_controls(codec);
126 	if (err < 0)
127 		goto error_module;
128 	/* only register after the bus probe finished; otherwise it's racy */
129 	if (!codec->bus->bus_probing && codec->card->registered) {
130 		err = snd_card_register(codec->card);
131 		if (err < 0)
132 			goto error_module;
133 		snd_hda_codec_register(codec);
134 	}
135 
136 	codec->core.lazy_cache = true;
137 	return 0;
138 
139  error_module:
140 	if (codec->patch_ops.free)
141 		codec->patch_ops.free(codec);
142  error_module_put:
143 	module_put(owner);
144 
145  error:
146 	snd_hda_codec_cleanup_for_unbind(codec);
147 	return err;
148 }
149 
hda_codec_driver_remove(struct device * dev)150 static int hda_codec_driver_remove(struct device *dev)
151 {
152 	struct hda_codec *codec = dev_to_hda_codec(dev);
153 
154 	if (codec->bus->core.ext_ops) {
155 		if (WARN_ON(!codec->bus->core.ext_ops->hdev_detach))
156 			return -EINVAL;
157 		return codec->bus->core.ext_ops->hdev_detach(&codec->core);
158 	}
159 
160 	refcount_dec(&codec->pcm_ref);
161 	snd_hda_codec_disconnect_pcms(codec);
162 	snd_hda_jack_tbl_disconnect(codec);
163 	wait_event(codec->remove_sleep, !refcount_read(&codec->pcm_ref));
164 	snd_power_sync_ref(codec->bus->card);
165 
166 	if (codec->patch_ops.free)
167 		codec->patch_ops.free(codec);
168 	snd_hda_codec_cleanup_for_unbind(codec);
169 	module_put(dev->driver->owner);
170 	return 0;
171 }
172 
hda_codec_driver_shutdown(struct device * dev)173 static void hda_codec_driver_shutdown(struct device *dev)
174 {
175 	snd_hda_codec_shutdown(dev_to_hda_codec(dev));
176 }
177 
__hda_codec_driver_register(struct hda_codec_driver * drv,const char * name,struct module * owner)178 int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
179 			       struct module *owner)
180 {
181 	drv->core.driver.name = name;
182 	drv->core.driver.owner = owner;
183 	drv->core.driver.bus = &snd_hda_bus_type;
184 	drv->core.driver.probe = hda_codec_driver_probe;
185 	drv->core.driver.remove = hda_codec_driver_remove;
186 	drv->core.driver.shutdown = hda_codec_driver_shutdown;
187 	drv->core.driver.pm = &hda_codec_driver_pm;
188 	drv->core.type = HDA_DEV_LEGACY;
189 	drv->core.match = hda_codec_match;
190 	drv->core.unsol_event = hda_codec_unsol_event;
191 	return driver_register(&drv->core.driver);
192 }
193 EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
194 
hda_codec_driver_unregister(struct hda_codec_driver * drv)195 void hda_codec_driver_unregister(struct hda_codec_driver *drv)
196 {
197 	driver_unregister(&drv->core.driver);
198 }
199 EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);
200 
codec_probed(struct hda_codec * codec)201 static inline bool codec_probed(struct hda_codec *codec)
202 {
203 	return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
204 }
205 
206 /* try to auto-load codec module */
request_codec_module(struct hda_codec * codec)207 static void request_codec_module(struct hda_codec *codec)
208 {
209 #ifdef MODULE
210 	char modalias[32];
211 	const char *mod = NULL;
212 
213 	switch (codec->probe_id) {
214 	case HDA_CODEC_ID_GENERIC_HDMI:
215 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
216 		mod = "snd-hda-codec-hdmi";
217 #endif
218 		break;
219 	case HDA_CODEC_ID_GENERIC:
220 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
221 		mod = "snd-hda-codec-generic";
222 #endif
223 		break;
224 	default:
225 		snd_hdac_codec_modalias(&codec->core, modalias, sizeof(modalias));
226 		mod = modalias;
227 		break;
228 	}
229 
230 	if (mod)
231 		request_module(mod);
232 #endif /* MODULE */
233 }
234 
235 /* try to auto-load and bind the codec module */
codec_bind_module(struct hda_codec * codec)236 static void codec_bind_module(struct hda_codec *codec)
237 {
238 #ifdef MODULE
239 	request_codec_module(codec);
240 	if (codec_probed(codec))
241 		return;
242 #endif
243 }
244 
245 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
246 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
is_likely_hdmi_codec(struct hda_codec * codec)247 static bool is_likely_hdmi_codec(struct hda_codec *codec)
248 {
249 	hda_nid_t nid;
250 
251 	for_each_hda_codec_node(nid, codec) {
252 		unsigned int wcaps = get_wcaps(codec, nid);
253 		switch (get_wcaps_type(wcaps)) {
254 		case AC_WID_AUD_IN:
255 			return false; /* HDMI parser supports only HDMI out */
256 		case AC_WID_AUD_OUT:
257 			if (!(wcaps & AC_WCAP_DIGITAL))
258 				return false;
259 			break;
260 		}
261 	}
262 	return true;
263 }
264 #else
265 /* no HDMI codec parser support */
266 #define is_likely_hdmi_codec(codec)	false
267 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
268 
codec_bind_generic(struct hda_codec * codec)269 static int codec_bind_generic(struct hda_codec *codec)
270 {
271 	if (codec->probe_id)
272 		return -ENODEV;
273 
274 	if (is_likely_hdmi_codec(codec)) {
275 		codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
276 		request_codec_module(codec);
277 		if (codec_probed(codec))
278 			return 0;
279 	}
280 
281 	codec->probe_id = HDA_CODEC_ID_GENERIC;
282 	request_codec_module(codec);
283 	if (codec_probed(codec))
284 		return 0;
285 	return -ENODEV;
286 }
287 
288 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
289 #define is_generic_config(codec) \
290 	(codec->modelname && !strcmp(codec->modelname, "generic"))
291 #else
292 #define is_generic_config(codec)	0
293 #endif
294 
295 /**
296  * snd_hda_codec_configure - (Re-)configure the HD-audio codec
297  * @codec: the HDA codec
298  *
299  * Start parsing of the given codec tree and (re-)initialize the whole
300  * patch instance.
301  *
302  * Returns 0 if successful or a negative error code.
303  */
snd_hda_codec_configure(struct hda_codec * codec)304 int snd_hda_codec_configure(struct hda_codec *codec)
305 {
306 	int err;
307 
308 	if (codec->configured)
309 		return 0;
310 
311 	if (is_generic_config(codec))
312 		codec->probe_id = HDA_CODEC_ID_GENERIC;
313 	else
314 		codec->probe_id = 0;
315 
316 	if (!device_is_registered(&codec->core.dev)) {
317 		err = snd_hdac_device_register(&codec->core);
318 		if (err < 0)
319 			return err;
320 	}
321 
322 	if (!codec->preset)
323 		codec_bind_module(codec);
324 	if (!codec->preset) {
325 		err = codec_bind_generic(codec);
326 		if (err < 0) {
327 			codec_dbg(codec, "Unable to bind the codec\n");
328 			return err;
329 		}
330 	}
331 
332 	codec->configured = 1;
333 	return 0;
334 }
335 EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
336