GNU Linux-libre 4.14.266-gnu1
[releases.git] / sound / pci / hda / dell_wmi_helper.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Helper functions for Dell Mic Mute LED control;
3  * to be included from codec driver
4  */
5
6 #if IS_ENABLED(CONFIG_DELL_LAPTOP)
7 #include <linux/dell-led.h>
8
9 enum {
10         MICMUTE_LED_ON,
11         MICMUTE_LED_OFF,
12         MICMUTE_LED_FOLLOW_CAPTURE,
13         MICMUTE_LED_FOLLOW_MUTE,
14 };
15
16 static int dell_led_mode = MICMUTE_LED_FOLLOW_MUTE;
17 static int dell_capture;
18 static int dell_led_value;
19 static int (*dell_micmute_led_set_func)(int);
20 static void (*dell_old_cap_hook)(struct hda_codec *,
21                                  struct snd_kcontrol *,
22                                  struct snd_ctl_elem_value *);
23
24 static 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
50 static 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
57         if (!ucontrol || !dell_micmute_led_set_func)
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? */
61                 dell_capture = (ucontrol->value.integer.value[0] ||
62                                 ucontrol->value.integer.value[1]);
63                 call_micmute_led_update();
64         }
65 }
66
67 static 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
77 static 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
84 static 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
99 static 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 };
109
110 static 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) {
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) {
120                         codec_warn(codec, "Failed to find dell wmi symbol dell_micmute_led_set\n");
121                         return;
122                 }
123
124                 removefunc = true;
125                 if (dell_micmute_led_set_func(false) >= 0) {
126                         dell_led_value = 0;
127                         if (spec->gen.num_adc_nids > 1 && !spec->gen.dyn_adc_switch)
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;
133                                 add_mixer(spec, dell_mic_mute_mode_ctls);
134                         }
135                 }
136
137         }
138
139         if (dell_micmute_led_set_func && (action == HDA_FIXUP_ACT_FREE || removefunc)) {
140                 symbol_put(dell_micmute_led_set);
141                 dell_micmute_led_set_func = NULL;
142                 dell_old_cap_hook = NULL;
143         }
144 }
145
146 #else /* CONFIG_DELL_LAPTOP */
147 static void alc_fixup_dell_wmi(struct hda_codec *codec,
148                                const struct hda_fixup *fix, int action)
149 {
150 }
151
152 #endif /* CONFIG_DELL_LAPTOP */