dell_wmi_helper.c 3.7 KB

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