dell_wmi_helper.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #if IS_ENABLED(CONFIG_DELL_LAPTOP)
  6. #include <linux/dell-led.h>
  7. enum {
  8. MICMUTE_LED_ON,
  9. MICMUTE_LED_OFF,
  10. MICMUTE_LED_FOLLOW_CAPTURE,
  11. MICMUTE_LED_FOLLOW_MUTE,
  12. };
  13. static int dell_led_mode = MICMUTE_LED_FOLLOW_MUTE;
  14. static int dell_capture;
  15. static int dell_led_value;
  16. static int (*dell_micmute_led_set_func)(int);
  17. static void (*dell_old_cap_hook)(struct hda_codec *,
  18. struct snd_kcontrol *,
  19. struct snd_ctl_elem_value *);
  20. static void call_micmute_led_update(void)
  21. {
  22. int val;
  23. switch (dell_led_mode) {
  24. case MICMUTE_LED_ON:
  25. val = 1;
  26. break;
  27. case MICMUTE_LED_OFF:
  28. val = 0;
  29. break;
  30. case MICMUTE_LED_FOLLOW_CAPTURE:
  31. val = dell_capture;
  32. break;
  33. case MICMUTE_LED_FOLLOW_MUTE:
  34. default:
  35. val = !dell_capture;
  36. break;
  37. }
  38. if (val == dell_led_value)
  39. return;
  40. dell_led_value = val;
  41. dell_micmute_led_set_func(dell_led_value);
  42. }
  43. static void update_dell_wmi_micmute_led(struct hda_codec *codec,
  44. struct snd_kcontrol *kcontrol,
  45. struct snd_ctl_elem_value *ucontrol)
  46. {
  47. if (dell_old_cap_hook)
  48. dell_old_cap_hook(codec, kcontrol, ucontrol);
  49. if (!ucontrol || !dell_micmute_led_set_func)
  50. return;
  51. if (strcmp("Capture Switch", ucontrol->id.name) == 0 && ucontrol->id.index == 0) {
  52. /* TODO: How do I verify if it's a mono or stereo here? */
  53. dell_capture = (ucontrol->value.integer.value[0] ||
  54. ucontrol->value.integer.value[1]);
  55. call_micmute_led_update();
  56. }
  57. }
  58. static int dell_mic_mute_led_mode_info(struct snd_kcontrol *kcontrol,
  59. struct snd_ctl_elem_info *uinfo)
  60. {
  61. static const char * const texts[] = {
  62. "On", "Off", "Follow Capture", "Follow Mute",
  63. };
  64. return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
  65. }
  66. static int dell_mic_mute_led_mode_get(struct snd_kcontrol *kcontrol,
  67. struct snd_ctl_elem_value *ucontrol)
  68. {
  69. ucontrol->value.enumerated.item[0] = dell_led_mode;
  70. return 0;
  71. }
  72. static int dell_mic_mute_led_mode_put(struct snd_kcontrol *kcontrol,
  73. struct snd_ctl_elem_value *ucontrol)
  74. {
  75. unsigned int mode;
  76. mode = ucontrol->value.enumerated.item[0];
  77. if (mode > MICMUTE_LED_FOLLOW_MUTE)
  78. mode = MICMUTE_LED_FOLLOW_MUTE;
  79. if (mode == dell_led_mode)
  80. return 0;
  81. dell_led_mode = mode;
  82. call_micmute_led_update();
  83. return 1;
  84. }
  85. static const struct snd_kcontrol_new dell_mic_mute_mode_ctls[] = {
  86. {
  87. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  88. .name = "Mic Mute-LED Mode",
  89. .info = dell_mic_mute_led_mode_info,
  90. .get = dell_mic_mute_led_mode_get,
  91. .put = dell_mic_mute_led_mode_put,
  92. },
  93. {}
  94. };
  95. static void alc_fixup_dell_wmi(struct hda_codec *codec,
  96. const struct hda_fixup *fix, int action)
  97. {
  98. struct alc_spec *spec = codec->spec;
  99. bool removefunc = false;
  100. if (action == HDA_FIXUP_ACT_PROBE) {
  101. if (!dell_micmute_led_set_func)
  102. dell_micmute_led_set_func = symbol_request(dell_micmute_led_set);
  103. if (!dell_micmute_led_set_func) {
  104. codec_warn(codec, "Failed to find dell wmi symbol dell_micmute_led_set\n");
  105. return;
  106. }
  107. removefunc = true;
  108. if (dell_micmute_led_set_func(false) >= 0) {
  109. dell_led_value = 0;
  110. if (spec->gen.num_adc_nids > 1 && !spec->gen.dyn_adc_switch)
  111. codec_dbg(codec, "Skipping micmute LED control due to several ADCs");
  112. else {
  113. dell_old_cap_hook = spec->gen.cap_sync_hook;
  114. spec->gen.cap_sync_hook = update_dell_wmi_micmute_led;
  115. removefunc = false;
  116. add_mixer(spec, dell_mic_mute_mode_ctls);
  117. }
  118. }
  119. }
  120. if (dell_micmute_led_set_func && (action == HDA_FIXUP_ACT_FREE || removefunc)) {
  121. symbol_put(dell_micmute_led_set);
  122. dell_micmute_led_set_func = NULL;
  123. dell_old_cap_hook = NULL;
  124. }
  125. }
  126. #else /* CONFIG_DELL_LAPTOP */
  127. static void alc_fixup_dell_wmi(struct hda_codec *codec,
  128. const struct hda_fixup *fix, int action)
  129. {
  130. }
  131. #endif /* CONFIG_DELL_LAPTOP */