blob: 1b48a8c19d28e36ba6cf27da78a8c5dc33fe3ebe [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Hui Wang00ef9942014-07-31 11:52:38 +08002/* Helper functions for Dell Mic Mute LED control;
3 * to be included from codec driver
4 */
5
Michał Kępień44319ab2017-02-17 08:57:50 +01006#if IS_ENABLED(CONFIG_DELL_LAPTOP)
Hui Wang00ef9942014-07-31 11:52:38 +08007#include <linux/dell-led.h>
8
Takashi Iwai62a93942017-08-22 16:52:10 +02009enum {
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;
Hui Wang00ef9942014-07-31 11:52:38 +080018static int dell_led_value;
Michał Kępień5dba8802017-02-17 08:57:49 +010019static int (*dell_micmute_led_set_func)(int);
Hui Wang00ef9942014-07-31 11:52:38 +080020static void (*dell_old_cap_hook)(struct hda_codec *,
21 struct snd_kcontrol *,
22 struct snd_ctl_elem_value *);
23
Takashi Iwai62a93942017-08-22 16:52:10 +020024static 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
Hui Wang00ef9942014-07-31 11:52:38 +080050static 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
Michał Kępień5dba8802017-02-17 08:57:49 +010057 if (!ucontrol || !dell_micmute_led_set_func)
Hui Wang00ef9942014-07-31 11:52:38 +080058 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? */
Takashi Iwai62a93942017-08-22 16:52:10 +020061 dell_capture = (ucontrol->value.integer.value[0] ||
62 ucontrol->value.integer.value[1]);
63 call_micmute_led_update();
Hui Wang00ef9942014-07-31 11:52:38 +080064 }
65}
66
Takashi Iwai62a93942017-08-22 16:52:10 +020067static 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};
Hui Wang00ef9942014-07-31 11:52:38 +0800109
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) {
Michał Kępień5dba8802017-02-17 08:57:49 +0100117 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) {
Michał Kępieńfa5923c2017-02-17 08:57:48 +0100120 codec_warn(codec, "Failed to find dell wmi symbol dell_micmute_led_set\n");
Hui Wang00ef9942014-07-31 11:52:38 +0800121 return;
122 }
123
124 removefunc = true;
Michał Kępień5dba8802017-02-17 08:57:49 +0100125 if (dell_micmute_led_set_func(false) >= 0) {
Hui Wang00ef9942014-07-31 11:52:38 +0800126 dell_led_value = 0;
Hui Wang4875a5f2016-10-11 10:48:58 +0800127 if (spec->gen.num_adc_nids > 1 && !spec->gen.dyn_adc_switch)
Hui Wang00ef9942014-07-31 11:52:38 +0800128 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;
Takashi Iwai62a93942017-08-22 16:52:10 +0200133 add_mixer(spec, dell_mic_mute_mode_ctls);
Hui Wang00ef9942014-07-31 11:52:38 +0800134 }
135 }
136
137 }
138
Michał Kępień5dba8802017-02-17 08:57:49 +0100139 if (dell_micmute_led_set_func && (action == HDA_FIXUP_ACT_FREE || removefunc)) {
Michał Kępieńfa5923c2017-02-17 08:57:48 +0100140 symbol_put(dell_micmute_led_set);
Michał Kępień5dba8802017-02-17 08:57:49 +0100141 dell_micmute_led_set_func = NULL;
Hui Wang00ef9942014-07-31 11:52:38 +0800142 dell_old_cap_hook = NULL;
143 }
144}
145
Michał Kępień44319ab2017-02-17 08:57:50 +0100146#else /* CONFIG_DELL_LAPTOP */
Hui Wang00ef9942014-07-31 11:52:38 +0800147static void alc_fixup_dell_wmi(struct hda_codec *codec,
148 const struct hda_fixup *fix, int action)
149{
150}
151
Michał Kępień44319ab2017-02-17 08:57:50 +0100152#endif /* CONFIG_DELL_LAPTOP */