Takashi Iwai | b317b03 | 2014-01-08 11:44:21 +0100 | [diff] [blame^] | 1 | /* Helper functions for Thinkpad LED control; |
| 2 | * to be included from codec driver |
| 3 | */ |
| 4 | |
| 5 | #if IS_ENABLED(CONFIG_THINKPAD_ACPI) |
| 6 | |
| 7 | #include <linux/acpi.h> |
| 8 | #include <linux/thinkpad_acpi.h> |
| 9 | |
| 10 | static int (*led_set_func)(int, bool); |
| 11 | |
| 12 | static acpi_status acpi_check_cb(acpi_handle handle, u32 lvl, void *context, |
| 13 | void **rv) |
| 14 | { |
| 15 | bool *found = context; |
| 16 | *found = true; |
| 17 | return AE_OK; |
| 18 | } |
| 19 | |
| 20 | static bool is_thinkpad(struct hda_codec *codec) |
| 21 | { |
| 22 | bool found = false; |
| 23 | if (codec->subsystem_id >> 16 != 0x17aa) |
| 24 | return false; |
| 25 | if (ACPI_SUCCESS(acpi_get_devices("LEN0068", acpi_check_cb, &found, NULL)) && found) |
| 26 | return true; |
| 27 | found = false; |
| 28 | return ACPI_SUCCESS(acpi_get_devices("IBM0068", acpi_check_cb, &found, NULL)) && found; |
| 29 | } |
| 30 | |
| 31 | static void update_tpacpi_mute_led(void *private_data, int enabled) |
| 32 | { |
| 33 | struct hda_codec *codec = private_data; |
| 34 | struct hda_gen_spec *spec = codec->spec; |
| 35 | |
| 36 | if (spec->vmaster_mute.hook) |
| 37 | spec->vmaster_mute.hook(private_data, enabled); |
| 38 | |
| 39 | if (led_set_func) |
| 40 | led_set_func(TPACPI_LED_MUTE, !enabled); |
| 41 | } |
| 42 | |
| 43 | static void update_tpacpi_micmute_led(struct hda_codec *codec, |
| 44 | struct snd_ctl_elem_value *ucontrol) |
| 45 | { |
| 46 | if (!ucontrol || !led_set_func) |
| 47 | return; |
| 48 | if (strcmp("Capture Switch", ucontrol->id.name) == 0 && ucontrol->id.index == 0) { |
| 49 | /* TODO: How do I verify if it's a mono or stereo here? */ |
| 50 | bool val = ucontrol->value.integer.value[0] || ucontrol->value.integer.value[1]; |
| 51 | led_set_func(TPACPI_LED_MICMUTE, !val); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | static void hda_fixup_thinkpad_acpi(struct hda_codec *codec, |
| 56 | const struct hda_fixup *fix, int action) |
| 57 | { |
| 58 | struct hda_gen_spec *spec = codec->spec; |
| 59 | bool removefunc = false; |
| 60 | |
| 61 | if (action == HDA_FIXUP_ACT_PROBE) { |
| 62 | if (!is_thinkpad(codec)) |
| 63 | return; |
| 64 | if (!led_set_func) |
| 65 | led_set_func = symbol_request(tpacpi_led_set); |
| 66 | if (!led_set_func) { |
| 67 | snd_printk(KERN_WARNING "Failed to find thinkpad-acpi symbol tpacpi_led_set\n"); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | removefunc = true; |
| 72 | if (led_set_func(TPACPI_LED_MUTE, false) >= 0) { |
| 73 | spec->vmaster_mute.hook = update_tpacpi_mute_led; |
| 74 | removefunc = false; |
| 75 | } |
| 76 | if (led_set_func(TPACPI_LED_MICMUTE, false) >= 0) { |
| 77 | if (spec->num_adc_nids > 1) |
| 78 | snd_printdd("Skipping micmute LED control due to several ADCs"); |
| 79 | else { |
| 80 | spec->cap_sync_hook = update_tpacpi_micmute_led; |
| 81 | removefunc = false; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if (led_set_func && (action == HDA_FIXUP_ACT_FREE || removefunc)) { |
| 87 | symbol_put(tpacpi_led_set); |
| 88 | led_set_func = NULL; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | #else /* CONFIG_THINKPAD_ACPI */ |
| 93 | |
| 94 | static void hda_fixup_thinkpad_acpi(struct hda_codec *codec, |
| 95 | const struct hda_fixup *fix, int action) |
| 96 | { |
| 97 | } |
| 98 | |
| 99 | #endif /* CONFIG_THINKPAD_ACPI */ |