License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-2.6-block.git] / sound / pci / hda / dell_wmi_helper.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
00ef9940
HW
2/* Helper functions for Dell Mic Mute LED control;
3 * to be included from codec driver
4 */
5
44319ab7 6#if IS_ENABLED(CONFIG_DELL_LAPTOP)
00ef9940
HW
7#include <linux/dell-led.h>
8
62a93947
TI
9enum {
10 MICMUTE_LED_ON,
11 MICMUTE_LED_OFF,
12 MICMUTE_LED_FOLLOW_CAPTURE,
13 MICMUTE_LED_FOLLOW_MUTE,
14};
15
16static int dell_led_mode = MICMUTE_LED_FOLLOW_MUTE;
17static int dell_capture;
00ef9940 18static int dell_led_value;
5dba8804 19static int (*dell_micmute_led_set_func)(int);
00ef9940
HW
20static void (*dell_old_cap_hook)(struct hda_codec *,
21 struct snd_kcontrol *,
22 struct snd_ctl_elem_value *);
23
62a93947
TI
24static void call_micmute_led_update(void)
25{
26 int val;
27
28 switch (dell_led_mode) {
29 case MICMUTE_LED_ON:
30 val = 1;
31 break;
32 case MICMUTE_LED_OFF:
33 val = 0;
34 break;
35 case MICMUTE_LED_FOLLOW_CAPTURE:
36 val = dell_capture;
37 break;
38 case MICMUTE_LED_FOLLOW_MUTE:
39 default:
40 val = !dell_capture;
41 break;
42 }
43
44 if (val == dell_led_value)
45 return;
46 dell_led_value = val;
47 dell_micmute_led_set_func(dell_led_value);
48}
49
00ef9940
HW
50static void update_dell_wmi_micmute_led(struct hda_codec *codec,
51 struct snd_kcontrol *kcontrol,
52 struct snd_ctl_elem_value *ucontrol)
53{
54 if (dell_old_cap_hook)
55 dell_old_cap_hook(codec, kcontrol, ucontrol);
56
5dba8804 57 if (!ucontrol || !dell_micmute_led_set_func)
00ef9940
HW
58 return;
59 if (strcmp("Capture Switch", ucontrol->id.name) == 0 && ucontrol->id.index == 0) {
60 /* TODO: How do I verify if it's a mono or stereo here? */
62a93947
TI
61 dell_capture = (ucontrol->value.integer.value[0] ||
62 ucontrol->value.integer.value[1]);
63 call_micmute_led_update();
00ef9940
HW
64 }
65}
66
62a93947
TI
67static int dell_mic_mute_led_mode_info(struct snd_kcontrol *kcontrol,
68 struct snd_ctl_elem_info *uinfo)
69{
70 static const char * const texts[] = {
71 "On", "Off", "Follow Capture", "Follow Mute",
72 };
73
74 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
75}
76
77static int dell_mic_mute_led_mode_get(struct snd_kcontrol *kcontrol,
78 struct snd_ctl_elem_value *ucontrol)
79{
80 ucontrol->value.enumerated.item[0] = dell_led_mode;
81 return 0;
82}
83
84static int dell_mic_mute_led_mode_put(struct snd_kcontrol *kcontrol,
85 struct snd_ctl_elem_value *ucontrol)
86{
87 unsigned int mode;
88
89 mode = ucontrol->value.enumerated.item[0];
90 if (mode > MICMUTE_LED_FOLLOW_MUTE)
91 mode = MICMUTE_LED_FOLLOW_MUTE;
92 if (mode == dell_led_mode)
93 return 0;
94 dell_led_mode = mode;
95 call_micmute_led_update();
96 return 1;
97}
98
99static const struct snd_kcontrol_new dell_mic_mute_mode_ctls[] = {
100 {
101 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
102 .name = "Mic Mute-LED Mode",
103 .info = dell_mic_mute_led_mode_info,
104 .get = dell_mic_mute_led_mode_get,
105 .put = dell_mic_mute_led_mode_put,
106 },
107 {}
108};
00ef9940
HW
109
110static void alc_fixup_dell_wmi(struct hda_codec *codec,
111 const struct hda_fixup *fix, int action)
112{
113 struct alc_spec *spec = codec->spec;
114 bool removefunc = false;
115
116 if (action == HDA_FIXUP_ACT_PROBE) {
5dba8804
MK
117 if (!dell_micmute_led_set_func)
118 dell_micmute_led_set_func = symbol_request(dell_micmute_led_set);
119 if (!dell_micmute_led_set_func) {
fa5923ce 120 codec_warn(codec, "Failed to find dell wmi symbol dell_micmute_led_set\n");
00ef9940
HW
121 return;
122 }
123
124 removefunc = true;
5dba8804 125 if (dell_micmute_led_set_func(false) >= 0) {
00ef9940 126 dell_led_value = 0;
4875a5f7 127 if (spec->gen.num_adc_nids > 1 && !spec->gen.dyn_adc_switch)
00ef9940
HW
128 codec_dbg(codec, "Skipping micmute LED control due to several ADCs");
129 else {
130 dell_old_cap_hook = spec->gen.cap_sync_hook;
131 spec->gen.cap_sync_hook = update_dell_wmi_micmute_led;
132 removefunc = false;
62a93947 133 add_mixer(spec, dell_mic_mute_mode_ctls);
00ef9940
HW
134 }
135 }
136
137 }
138
5dba8804 139 if (dell_micmute_led_set_func && (action == HDA_FIXUP_ACT_FREE || removefunc)) {
fa5923ce 140 symbol_put(dell_micmute_led_set);
5dba8804 141 dell_micmute_led_set_func = NULL;
00ef9940
HW
142 dell_old_cap_hook = NULL;
143 }
144}
145
44319ab7 146#else /* CONFIG_DELL_LAPTOP */
00ef9940
HW
147static void alc_fixup_dell_wmi(struct hda_codec *codec,
148 const struct hda_fixup *fix, int action)
149{
150}
151
44319ab7 152#endif /* CONFIG_DELL_LAPTOP */