xref: /openbmc/linux/sound/pci/hda/thinkpad_helper.c (revision da2ef666)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Helper functions for Thinkpad LED control;
3  * to be included from codec driver
4  */
5 
6 #if IS_ENABLED(CONFIG_THINKPAD_ACPI)
7 
8 #include <linux/acpi.h>
9 #include <linux/thinkpad_acpi.h>
10 
11 static int (*led_set_func)(int, bool);
12 static void (*old_vmaster_hook)(void *, int);
13 
14 static bool is_thinkpad(struct hda_codec *codec)
15 {
16 	return (codec->core.subsystem_id >> 16 == 0x17aa) &&
17 	       (acpi_dev_found("LEN0068") || acpi_dev_found("LEN0268") ||
18 		acpi_dev_found("IBM0068"));
19 }
20 
21 static void update_tpacpi_mute_led(void *private_data, int enabled)
22 {
23 	if (old_vmaster_hook)
24 		old_vmaster_hook(private_data, enabled);
25 
26 	if (led_set_func)
27 		led_set_func(TPACPI_LED_MUTE, !enabled);
28 }
29 
30 static void update_tpacpi_micmute(struct hda_codec *codec)
31 {
32 	struct hda_gen_spec *spec = codec->spec;
33 
34 	led_set_func(TPACPI_LED_MICMUTE, spec->micmute_led.led_value);
35 }
36 
37 static void hda_fixup_thinkpad_acpi(struct hda_codec *codec,
38 				    const struct hda_fixup *fix, int action)
39 {
40 	struct hda_gen_spec *spec = codec->spec;
41 	bool removefunc = false;
42 
43 	if (action == HDA_FIXUP_ACT_PROBE) {
44 		if (!is_thinkpad(codec))
45 			return;
46 		if (!led_set_func)
47 			led_set_func = symbol_request(tpacpi_led_set);
48 		if (!led_set_func) {
49 			codec_warn(codec,
50 				   "Failed to find thinkpad-acpi symbol tpacpi_led_set\n");
51 			return;
52 		}
53 
54 		removefunc = true;
55 		if (led_set_func(TPACPI_LED_MUTE, false) >= 0) {
56 			old_vmaster_hook = spec->vmaster_mute.hook;
57 			spec->vmaster_mute.hook = update_tpacpi_mute_led;
58 			removefunc = false;
59 		}
60 		if (led_set_func(TPACPI_LED_MICMUTE, false) >= 0 &&
61 		    snd_hda_gen_add_micmute_led(codec,
62 						update_tpacpi_micmute) > 0)
63 			removefunc = false;
64 	}
65 
66 	if (led_set_func && (action == HDA_FIXUP_ACT_FREE || removefunc)) {
67 		symbol_put(tpacpi_led_set);
68 		led_set_func = NULL;
69 		old_vmaster_hook = NULL;
70 	}
71 }
72 
73 #else /* CONFIG_THINKPAD_ACPI */
74 
75 static void hda_fixup_thinkpad_acpi(struct hda_codec *codec,
76 				    const struct hda_fixup *fix, int action)
77 {
78 }
79 
80 #endif /* CONFIG_THINKPAD_ACPI */
81