From dbd467bcd48a5be2cab7ac67976dc59e5c9fa03a Mon Sep 17 00:00:00 2001 From: Werner Sembach Date: Wed, 24 Aug 2022 10:57:20 +0200 Subject: [PATCH] Rework to switch from old platform sysfs interface to standardised leds sysfs interface --- Makefile | 4 +- src/clevo_interfaces.h | 14 + src/clevo_keyboard.h | 800 ++++------------------------------- src/clevo_leds.c | 285 +++++++++++++ src/clevo_leds.h | 39 ++ src/tuxedo_keyboard_common.h | 3 +- src/uniwill_keyboard.h | 1 + 7 files changed, 431 insertions(+), 715 deletions(-) create mode 100644 src/clevo_leds.c create mode 100644 src/clevo_leds.h diff --git a/Makefile b/Makefile index c60cf4d..75dc1a1 100644 --- a/Makefile +++ b/Makefile @@ -16,12 +16,14 @@ # You should have received a copy of the GNU General Public License # along with this software. If not, see . # -obj-m := ./src/tuxedo_keyboard.o \ +obj-m := tuxedo_keyboard.o \ ./src/clevo_wmi.o \ ./src/clevo_acpi.o \ ./src/tuxedo_io/tuxedo_io.o \ ./src/uniwill_wmi.o +tuxedo_keyboard-y := ./src/tuxedo_keyboard.o ./src/clevo_leds.o + PWD := $(shell pwd) KDIR := /lib/modules/$(shell uname -r)/build diff --git a/src/clevo_interfaces.h b/src/clevo_interfaces.h index c3226cd..a624258 100644 --- a/src/clevo_interfaces.h +++ b/src/clevo_interfaces.h @@ -38,6 +38,10 @@ #define CLEVO_CMD_GET_FLIGHTMODE_SW 0x07 #define CLEVO_CMD_GET_TOUCHPAD_SW 0x09 +#define CLEVO_CMD_GET_EVENT 0x01 + +#define CLEVO_CMD_GET_SPECS 0x0D // Returns buffer -> only works with clevo_evaluate_method2 + // The clevo set commands expect a parameter #define CLEVO_CMD_SET_FANSPEED_VALUE 0x68 #define CLEVO_CMD_SET_FANSPEED_AUTO 0x69 @@ -46,6 +50,16 @@ #define CLEVO_CMD_SET_FLIGHTMODE_SW 0x20 #define CLEVO_CMD_SET_TOUCHPAD_SW 0x2a +#define CLEVO_CMD_SET_KB_LEDS 0x67 // used to set color, brightness, blinking pattern, etc. + +#define CLEVO_CMD_SET_EVENTS_ENABLED 0x46 + +#define CLEVO_CMD_SET_KB_LEDS_SUB_RGB_ZONE_0 0xF0000000 // 1-zone RGB and 3-zone RGB left +#define CLEVO_CMD_SET_KB_LEDS_SUB_RGB_ZONE_1 0xF1000000 // 3-zone RGB center +#define CLEVO_CMD_SET_KB_LEDS_SUB_RGB_ZONE_2 0xF2000000 // 3-Zone RGB right +#define CLEVO_CMD_SET_KB_LEDS_SUB_RGB_ZONE_3 0xF3000000 // Unused on all known Clevo devices +#define CLEVO_CMD_SET_KB_LEDS_SUB_BRIGHTNESS 0xF4000000 + #define CLEVO_CMD_OPT 0x79 #define CLEVO_OPT_SUBCMD_SET_PERF_PROF 0x19 diff --git a/src/clevo_keyboard.h b/src/clevo_keyboard.h index 09631b8..80701f4 100644 --- a/src/clevo_keyboard.h +++ b/src/clevo_keyboard.h @@ -21,45 +21,23 @@ #include "tuxedo_keyboard_common.h" #include "clevo_interfaces.h" - -#include -#include - -#define BRIGHTNESS_MIN 0 -#define BRIGHTNESS_MAX 255 -#define BRIGHTNESS_DEFAULT BRIGHTNESS_MAX - -#define REGION_LEFT 0xF0000000 -#define REGION_CENTER 0xF1000000 -#define REGION_RIGHT 0xF2000000 -#define REGION_EXTRA 0xF3000000 - -#define KEYBOARD_BRIGHTNESS 0xF4000000 - -#define KB_COLOR_DEFAULT 0xFFFFFF // White -#define DEFAULT_BLINKING_PATTERN 0 - -// Submethod IDs for the CLEVO_GET interface method -#define CLEVO_METHOD_ID_GET_EVENT 0x01 -#define CLEVO_METHOD_ID_GET_AP 0x46 -#define CLEVO_METHOD_ID_SET_KB_LEDS 0x67 /* used to set color, brightness, - blinking pattern, etc. */ -#define CLEVO_METHOD_ID_GET_SPECS 0x0D // only works with clevo_evaluate_method2 - +#include "clevo_leds.h" // Clevo event codes -#define CLEVO_EVENT_DECREASE_BACKLIGHT 0x81 -#define CLEVO_EVENT_INCREASE_BACKLIGHT 0x82 -#define CLEVO_EVENT_NEXT_BLINKING_PATTERN 0x83 -#define CLEVO_EVENT_TOGGLE_STATE 0x9F +#define CLEVO_EVENT_KB_LEDS_DECREASE 0x81 +#define CLEVO_EVENT_KB_LEDS_INCREASE 0x82 +#define CLEVO_EVENT_KB_LEDS_CYCLE_MODE 0x83 +#define CLEVO_EVENT_KB_LEDS_CYCLE_BRIGHTNESS 0x8A +#define CLEVO_EVENT_KB_LEDS_TOGGLE 0x9F -#define CLEVO_EVENT_CYCLE_BRIGHTNESS 0x8A -#define CLEVO_EVENT_TOUCHPAD_TOGGLE 0x5D -#define CLEVO_EVENT_TOUCHPAD_OFF 0xFC -#define CLEVO_EVENT_TOUCHPAD_ON 0xFD +#define CLEVO_EVENT_TOUCHPAD_TOGGLE 0x5D +#define CLEVO_EVENT_TOUCHPAD_OFF 0xFC +#define CLEVO_EVENT_TOUCHPAD_ON 0xFD -#define CLEVO_EVENT_RFKILL1 0x85 -#define CLEVO_EVENT_RFKILL2 0x86 +#define CLEVO_EVENT_RFKILL1 0x85 +#define CLEVO_EVENT_RFKILL2 0x86 + +#define CLEVO_KB_MODE_DEFAULT 0 // "CUSTOM"/Static Color struct tuxedo_keyboard_driver clevo_keyboard_driver; @@ -68,18 +46,9 @@ static struct clevo_interfaces_t { struct clevo_interface_t *acpi; } clevo_interfaces; -enum clevo_kb_backlight_type { - CLEVO_KB_BACKLIGHT_TYPE_NONE = 0x00, - CLEVO_KB_BACKLIGHT_TYPE_FIXED_COLOR = 0x01, - CLEVO_KB_BACKLIGHT_TYPE_3_ZONE_RGB = 0x02, - CLEVO_KB_BACKLIGHT_TYPE_1_ZONE_RGB = 0x06, - CLEVO_KB_BACKLIGHT_TYPE_PER_KEY_RGB = 0xf3 -}; - struct clevo_interface_t *active_clevo_interface; -void clevo_keyboard_write_state(void); -void clevo_keyboard_event_callb(u32 event); +void clevo_keyboard_event_callb(u32 event); // TODO Reorder to not require forward declaration? static DEFINE_MUTEX(clevo_keyboard_interface_modification_lock); @@ -94,7 +63,7 @@ u32 clevo_keyboard_add_interface(struct clevo_interface_t *new_interface) // Only use wmi if there is no other current interface if (ZERO_OR_NULL_PTR(active_clevo_interface)) { pr_debug("enable wmi events\n"); - clevo_interfaces.wmi->method_call(0x46, 0, NULL); + clevo_interfaces.wmi->method_call(CLEVO_CMD_SET_EVENTS_ENABLED, 0, NULL); active_clevo_interface = clevo_interfaces.wmi; } @@ -104,7 +73,7 @@ u32 clevo_keyboard_add_interface(struct clevo_interface_t *new_interface) clevo_interfaces.acpi->event_callb = clevo_keyboard_event_callb; pr_debug("enable acpi events (takes priority)\n"); - clevo_interfaces.acpi->method_call(0x46, 0, NULL); + clevo_interfaces.acpi->method_call(CLEVO_CMD_SET_EVENTS_ENABLED, 0, NULL); active_clevo_interface = clevo_interfaces.acpi; } else { // Not recognized interface @@ -149,12 +118,12 @@ EXPORT_SYMBOL(clevo_keyboard_remove_interface); static struct key_entry clevo_keymap[] = { // Keyboard backlight (RGB versions) - { KE_KEY, CLEVO_EVENT_DECREASE_BACKLIGHT, { KEY_KBDILLUMDOWN } }, - { KE_KEY, CLEVO_EVENT_INCREASE_BACKLIGHT, { KEY_KBDILLUMUP } }, - { KE_KEY, CLEVO_EVENT_TOGGLE_STATE, { KEY_KBDILLUMTOGGLE } }, - { KE_KEY, CLEVO_EVENT_NEXT_BLINKING_PATTERN, { KEY_LIGHTS_TOGGLE } }, + { KE_KEY, CLEVO_EVENT_KB_LEDS_DECREASE, { KEY_KBDILLUMDOWN } }, + { KE_KEY, CLEVO_EVENT_KB_LEDS_INCREASE, { KEY_KBDILLUMUP } }, + { KE_KEY, CLEVO_EVENT_KB_LEDS_TOGGLE, { KEY_KBDILLUMTOGGLE } }, + { KE_KEY, CLEVO_EVENT_KB_LEDS_CYCLE_MODE, { KEY_LIGHTS_TOGGLE } }, // Single cycle key (white only versions) - { KE_KEY, CLEVO_EVENT_CYCLE_BRIGHTNESS, { KEY_KBDILLUMUP } }, + { KE_KEY, CLEVO_EVENT_KB_LEDS_CYCLE_BRIGHTNESS, { KEY_KBDILLUMTOGGLE } }, //FIXME Cycle in driver or toggle in desktop environment? // Touchpad // The weirdly named touchpad toggle key that is implemented as KEY_F21 "everywhere" @@ -183,23 +152,8 @@ static struct key_entry clevo_keymap[] = { // Keyboard struct struct kbd_led_state_t { - u8 has_left; - u8 has_center; - u8 has_right; - u8 has_extra; u8 has_mode; - u8 has_brightness; - u8 enabled; - - struct { - u32 left; - u32 center; - u32 right; - u32 extra; - } color; - - u8 brightness; - u8 blinking_pattern; + u8 mode; u8 whole_kbd_color; }; @@ -209,122 +163,28 @@ struct blinking_pattern_t { const char *const name; }; - -static int blinking_pattern_id_validator(const char *value, - const struct kernel_param *blinking_pattern_param); -static const struct kernel_param_ops param_ops_mode_ops = { - .set = blinking_pattern_id_validator, - .get = param_get_int, +static struct blinking_pattern_t kbd_backlight_modes[] = { + { .key = 0, .value = 0x00000000, .name = "CUSTOM"}, + { .key = 1, .value = 0x1002a000, .name = "BREATHE"}, + { .key = 2, .value = 0x33010000, .name = "CYCLE"}, + { .key = 3, .value = 0x80000000, .name = "DANCE"}, + { .key = 4, .value = 0xA0000000, .name = "FLASH"}, + { .key = 5, .value = 0x70000000, .name = "RANDOM_COLOR"}, + { .key = 6, .value = 0x90000000, .name = "TEMPO"}, + { .key = 7, .value = 0xB0000000, .name = "WAVE"} }; -static uint param_color_left = KB_COLOR_DEFAULT; -module_param_named(color_left, param_color_left, uint, S_IRUSR); -MODULE_PARM_DESC(color_left, "Color for the Left Region"); - -static uint param_color_center = KB_COLOR_DEFAULT; -module_param_named(color_center, param_color_center, uint, S_IRUSR); -MODULE_PARM_DESC(color_center, "Color for the Center Region"); - -static uint param_color_right = KB_COLOR_DEFAULT; -module_param_named(color_right, param_color_right, uint, S_IRUSR); -MODULE_PARM_DESC(color_right, "Color for the Right Region"); - -static uint param_color_extra = KB_COLOR_DEFAULT; -module_param_named(color_extra, param_color_extra, uint, S_IRUSR); -MODULE_PARM_DESC(color_extra, "Color for the Extra Region"); - -static ushort param_blinking_pattern = DEFAULT_BLINKING_PATTERN; -module_param_cb(mode, ¶m_ops_mode_ops, ¶m_blinking_pattern, S_IRUSR); -MODULE_PARM_DESC(mode, "Set the keyboard backlight blinking pattern"); - -static bool param_state = true; -module_param_named(state, param_state, bool, S_IRUSR); -MODULE_PARM_DESC(state, - "Set the State of the Keyboard TRUE = ON | FALSE = OFF"); - static struct kbd_led_state_t kbd_led_state = { - .has_left = 0, - .has_center = 0, - .has_right = 0, - .has_extra = 0, - .has_mode = 0, - .has_brightness = 0, - .enabled = 1, - .color = { - .left = KB_COLOR_DEFAULT, .center = KB_COLOR_DEFAULT, - .right = KB_COLOR_DEFAULT, .extra = KB_COLOR_DEFAULT - }, - .brightness = BRIGHTNESS_DEFAULT, - .blinking_pattern = DEFAULT_BLINKING_PATTERN, - .whole_kbd_color = 7 + .has_mode = 1, + .mode = CLEVO_KB_MODE_DEFAULT, + .whole_kbd_color = 7, }; -static struct blinking_pattern_t blinking_patterns[] = { - { .key = 0,.value = 0,.name = "CUSTOM"}, - { .key = 1,.value = 0x1002a000,.name = "BREATHE"}, - { .key = 2,.value = 0x33010000,.name = "CYCLE"}, - { .key = 3,.value = 0x80000000,.name = "DANCE"}, - { .key = 4,.value = 0xA0000000,.name = "FLASH"}, - { .key = 5,.value = 0x70000000,.name = "RANDOM_COLOR"}, - { .key = 6,.value = 0x90000000,.name = "TEMPO"}, - { .key = 7,.value = 0xB0000000,.name = "WAVE"} -}; - -// Sysfs Interface Methods -// Sysfs Interface for the keyboard state (ON / OFF) -static ssize_t show_state_fs(struct device *child, - struct device_attribute *attr, char *buffer) -{ - return sprintf(buffer, "%d\n", kbd_led_state.enabled); -} - -// Sysfs Interface for the color of the left side (Color as hexvalue) -static ssize_t show_color_left_fs(struct device *child, - struct device_attribute *attr, char *buffer) -{ - return sprintf(buffer, "%06x\n", kbd_led_state.color.left); -} - -// Sysfs Interface for the color of the center (Color as hexvalue) -static ssize_t show_color_center_fs(struct device *child, - struct device_attribute *attr, char *buffer) -{ - return sprintf(buffer, "%06x\n", kbd_led_state.color.center); -} - -// Sysfs Interface for the color of the right side (Color as hexvalue) -static ssize_t show_color_right_fs(struct device *child, - struct device_attribute *attr, char *buffer) -{ - return sprintf(buffer, "%06x\n", kbd_led_state.color.right); -} - -// Sysfs Interface for the color of the extra region (Color as hexvalue) -static ssize_t show_color_extra_fs(struct device *child, - struct device_attribute *attr, char *buffer) -{ - return sprintf(buffer, "%06x\n", kbd_led_state.color.extra); -} - -// Sysfs Interface for the keyboard brightness (unsigned int) -static ssize_t show_brightness_fs(struct device *child, - struct device_attribute *attr, char *buffer) -{ - return sprintf(buffer, "%d\n", kbd_led_state.brightness); -} - // Sysfs Interface for the backlight blinking pattern -static ssize_t show_blinking_patterns_fs(struct device *child, struct device_attribute *attr, +static ssize_t show_kbd_backlight_modes_fs(struct device *child, struct device_attribute *attr, char *buffer) { - return sprintf(buffer, "%d\n", kbd_led_state.blinking_pattern); -} - -// Sysfs Interface for if the keyboard has extra region -static ssize_t show_hasextra_fs(struct device *child, - struct device_attribute *attr, char *buffer) -{ - return sprintf(buffer, "%d\n", kbd_led_state.has_extra); + return sprintf(buffer, "%d\n", kbd_led_state.mode); } u32 clevo_evaluate_method2(u8 cmd, u32 arg, union acpi_object **result) @@ -373,33 +233,6 @@ u32 clevo_get_active_interface_id(char **id_str) } EXPORT_SYMBOL(clevo_get_active_interface_id); -static void set_brightness(u8 brightness) -{ - TUXEDO_INFO("Set brightness on %d", brightness); - if (!clevo_evaluate_method - (CLEVO_METHOD_ID_SET_KB_LEDS, 0xF4000000 | brightness, NULL)) { - kbd_led_state.brightness = brightness; - } -} - -static ssize_t set_brightness_fs(struct device *child, - struct device_attribute *attr, - const char *buffer, size_t size) -{ - unsigned int val; - // hier unsigned? - - int err = kstrtouint(buffer, 0, &val); - if (err) { - return err; - } - - val = clamp_t(u8, val, BRIGHTNESS_MIN, BRIGHTNESS_MAX); - set_brightness(val); - - return size; -} - static int set_enabled_cmd(u8 state) { u32 cmd = 0xE0000000; @@ -411,126 +244,7 @@ static int set_enabled_cmd(u8 state) cmd |= 0x07F001; } - return clevo_evaluate_method(CLEVO_METHOD_ID_SET_KB_LEDS, cmd, NULL); -} - -static void set_enabled(u8 state) -{ - if (!set_enabled_cmd(state)) { - kbd_led_state.enabled = state; - } -} - -static ssize_t set_state_fs(struct device *child, struct device_attribute *attr, - const char *buffer, size_t size) -{ - unsigned int state; - - int err = kstrtouint(buffer, 0, &state); - if (err) { - return err; - } - - state = clamp_t(u8, state, 0, 1); - - set_enabled(state); - - return size; -} - -static int set_color(u32 region, u32 color) -{ - u32 cset = - ((color & 0x0000FF) << 16) | ((color & 0xFF0000) >> 8) | - ((color & 0x00FF00) >> 8); - u32 clevo_submethod_arg = region | cset; - - TUXEDO_DEBUG("Set Color '%08x' for region '%08x'", color, region); - - return clevo_evaluate_method(CLEVO_METHOD_ID_SET_KB_LEDS, clevo_submethod_arg, NULL); -} -static int set_color_code_region(u32 region, u32 colorcode) -{ - int err; - if (0 == (err = set_color(region, colorcode))) { - // after succesfully setting color, update our state struct - // depending on which region was changed - switch (region) { - case REGION_LEFT: - kbd_led_state.color.left = colorcode; - break; - case REGION_CENTER: - kbd_led_state.color.center = colorcode; - break; - case REGION_RIGHT: - kbd_led_state.color.right = colorcode; - break; - case REGION_EXTRA: - kbd_led_state.color.extra = colorcode; - break; - } - } - - return err; -} - -static int set_color_string_region(const char *color_string, size_t size, u32 region) -{ - u32 colorcode; - int err = kstrtouint(color_string, 0, &colorcode); - - if (err) { - return err; - } - - if (!set_color(region, colorcode)) { - // after succesfully setting color, update our state struct - // depending on which region was changed - switch (region) { - case REGION_LEFT: - kbd_led_state.color.left = colorcode; - break; - case REGION_CENTER: - kbd_led_state.color.center = colorcode; - break; - case REGION_RIGHT: - kbd_led_state.color.right = colorcode; - break; - case REGION_EXTRA: - kbd_led_state.color.extra = colorcode; - break; - } - } - - return size; -} - -static ssize_t set_color_left_fs(struct device *child, - struct device_attribute *attr, - const char *color_string, size_t size) -{ - return set_color_string_region(color_string, size, REGION_LEFT); -} - -static ssize_t set_color_center_fs(struct device *child, - struct device_attribute *attr, - const char *color_string, size_t size) -{ - return set_color_string_region(color_string, size, REGION_CENTER); -} - -static ssize_t set_color_right_fs(struct device *child, - struct device_attribute *attr, - const char *color_string, size_t size) -{ - return set_color_string_region(color_string, size, REGION_RIGHT); -} - -static ssize_t set_color_extra_fs(struct device *child, - struct device_attribute *attr, - const char *color_string, size_t size) -{ - return set_color_string_region(color_string, size, REGION_EXTRA); + return clevo_evaluate_method(CLEVO_CMD_SET_KB_LEDS, cmd, NULL); } static int set_next_color_whole_kb(void) @@ -549,77 +263,71 @@ static int set_next_color_whole_kb(void) new_color_id, new_color_code); /* Set color on all four regions*/ - set_color_code_region(REGION_LEFT, new_color_code); - set_color_code_region(REGION_CENTER, new_color_code); - set_color_code_region(REGION_RIGHT, new_color_code); - set_color_code_region(REGION_EXTRA, new_color_code); - + clevo_leds_set_color_extern(new_color_code); kbd_led_state.whole_kbd_color = new_color_id; return 0; } -static void set_blinking_pattern(u8 blinkling_pattern) +static void set_kbd_backlight_mode(u8 kbd_backlight_mode) { - TUXEDO_INFO("set_mode on %s", blinking_patterns[blinkling_pattern].name); + TUXEDO_INFO("Set keyboard backlight mode on %s", kbd_backlight_modes[kbd_backlight_mode].name); - if (!clevo_evaluate_method(CLEVO_METHOD_ID_SET_KB_LEDS, blinking_patterns[blinkling_pattern].value, NULL)) { + if (!clevo_evaluate_method(CLEVO_CMD_SET_KB_LEDS, kbd_backlight_modes[kbd_backlight_mode].value, NULL)) { // method was succesfull so update ur internal state struct - kbd_led_state.blinking_pattern = blinkling_pattern; - } - - if (blinkling_pattern == 0) { // 0 is the "custom" blinking pattern - - // so just set all regions to the stored colors - set_color(REGION_LEFT, kbd_led_state.color.left); - set_color(REGION_CENTER, kbd_led_state.color.center); - set_color(REGION_RIGHT, kbd_led_state.color.right); - - if (kbd_led_state.has_extra == 1) { - set_color(REGION_EXTRA, kbd_led_state.color.extra); - } + kbd_led_state.mode = kbd_backlight_mode; } } -static ssize_t set_blinking_pattern_fs(struct device *child, +static ssize_t set_kbd_backlight_mode_fs(struct device *child, struct device_attribute *attr, const char *buffer, size_t size) { - unsigned int blinking_pattern; + unsigned int kbd_backlight_mode; - int err = kstrtouint(buffer, 0, &blinking_pattern); + int err = kstrtouint(buffer, 0, &kbd_backlight_mode); if (err) { return err; } - blinking_pattern = clamp_t(u8, blinking_pattern, 0, ARRAY_SIZE(blinking_patterns) - 1); - set_blinking_pattern(blinking_pattern); + kbd_backlight_mode = clamp_t(u8, kbd_backlight_mode, 0, ARRAY_SIZE(kbd_backlight_modes) - 1); + set_kbd_backlight_mode(kbd_backlight_mode); return size; } -static int blinking_pattern_id_validator(const char *value, - const struct kernel_param *blinking_pattern_param) +static int kbd_backlight_mode_id_validator(const char *value, + const struct kernel_param *kbd_backlight_mode_param) { - int blinking_pattern = 0; + int kbd_backlight_mode = 0; - if (kstrtoint(value, 10, &blinking_pattern) != 0 - || blinking_pattern < 0 - || blinking_pattern > (ARRAY_SIZE(blinking_patterns) - 1)) { + if (kstrtoint(value, 10, &kbd_backlight_mode) != 0 + || kbd_backlight_mode < 0 + || kbd_backlight_mode > (ARRAY_SIZE(kbd_backlight_modes) - 1)) { return -EINVAL; } - return param_set_int(value, blinking_pattern_param); + return param_set_int(value, kbd_backlight_mode_param); } +static const struct kernel_param_ops param_ops_mode_ops = { + .set = kbd_backlight_mode_id_validator, + .get = param_get_int, +}; + +static u8 param_kbd_backlight_mode = CLEVO_KB_MODE_DEFAULT; +module_param_cb(kbd_backlight_mode, ¶m_ops_mode_ops, ¶m_kbd_backlight_mode, S_IRUSR); +MODULE_PARM_DESC(kbd_backlight_mode, "Set the keyboard backlight blinking pattern"); + +// TODO remove static int brightness_validator(const char *value, const struct kernel_param *brightness_param) { int brightness = 0; if (kstrtoint(value, 10, &brightness) != 0 - || brightness < BRIGHTNESS_MIN - || brightness > BRIGHTNESS_MAX) { + || brightness < 0 + || brightness > 255) { return -EINVAL; } @@ -630,338 +338,37 @@ void clevo_keyboard_event_callb(u32 event) { u32 key_event = event; - // TUXEDO_DEBUG("clevo event: %0#6x\n", event); + TUXEDO_DEBUG("Clevo event: %0#6x\n", event); switch (key_event) { - case CLEVO_EVENT_DECREASE_BACKLIGHT: - if (kbd_led_state.brightness == BRIGHTNESS_MIN - || (kbd_led_state.brightness - BRIGHTNESS_STEP) < BRIGHTNESS_MIN) { - set_brightness(BRIGHTNESS_MIN); - } else { - set_brightness(kbd_led_state.brightness - BRIGHTNESS_STEP); - } - - break; - - case CLEVO_EVENT_INCREASE_BACKLIGHT: - if (kbd_led_state.brightness == BRIGHTNESS_MAX - || (kbd_led_state.brightness + BRIGHTNESS_STEP) > BRIGHTNESS_MAX) { - set_brightness(BRIGHTNESS_MAX); - } else { - set_brightness(kbd_led_state.brightness + BRIGHTNESS_STEP); - } - - break; - -// case CLEVO_EVENT_NEXT_BLINKING_PATTERN: -// set_blinking_pattern((kbd_led_state.blinking_pattern + 1) > -// (ARRAY_SIZE(blinking_patterns) - 1) ? 0 : (kbd_led_state.blinking_pattern + 1)); -// break; - - case CLEVO_EVENT_NEXT_BLINKING_PATTERN: - set_next_color_whole_kb(); - break; - - case CLEVO_EVENT_TOGGLE_STATE: - set_enabled(kbd_led_state.enabled == 0 ? 1 : 0); - break; - - default: - break; + case CLEVO_EVENT_KB_LEDS_CYCLE_MODE: + set_next_color_whole_kb(); + break; + default: + break; } if (current_driver != NULL && current_driver->input_device != NULL) { - if (!sparse_keymap_report_known_event( - current_driver->input_device, key_event, 1, true)) { - TUXEDO_DEBUG("Unknown key - %d (%0#6x)\n", key_event, - key_event); + if (!sparse_keymap_report_known_event(current_driver->input_device, key_event, 1, true)) { + TUXEDO_DEBUG("Unknown key - %d (%0#6x)\n", key_event, key_event); } } } // Sysfs attribute file permissions and method linking -static DEVICE_ATTR(state, 0644, show_state_fs, set_state_fs); -static DEVICE_ATTR(color_left, 0644, show_color_left_fs, set_color_left_fs); -static DEVICE_ATTR(color_center, 0644, show_color_center_fs, - set_color_center_fs); -static DEVICE_ATTR(color_right, 0644, show_color_right_fs, set_color_right_fs); -static DEVICE_ATTR(color_extra, 0644, show_color_extra_fs, set_color_extra_fs); -static DEVICE_ATTR(brightness, 0644, show_brightness_fs, set_brightness_fs); -static DEVICE_ATTR(mode, 0644, show_blinking_patterns_fs, set_blinking_pattern_fs); -static DEVICE_ATTR(extra, 0444, show_hasextra_fs, NULL); - -static int ledcdev_set_blocking(struct led_classdev *led_cdev, enum led_brightness brightness) { - set_brightness(brightness); - return 0; -} - -static int ledcdev_set_blocking_mc(struct led_classdev *led_cdev, enum led_brightness brightness) { - u32 region, colorcode; - struct led_classdev_mc *led_cdev_mc = lcdev_to_mccdev(led_cdev); - - pr_debug("ledcdev_set_blocking_mc() brightness 0x%02x\n", brightness); - pr_debug("ledcdev_set_blocking_mc() led_cdev_mc->subled_info[0].intensity 0x%02x\n", led_cdev_mc->subled_info[0].intensity); - pr_debug("ledcdev_set_blocking_mc() led_cdev_mc->subled_info[1].intensity 0x%02x\n", led_cdev_mc->subled_info[1].intensity); - pr_debug("ledcdev_set_blocking_mc() led_cdev_mc->subled_info[2].intensity 0x%02x\n", led_cdev_mc->subled_info[2].intensity); - pr_debug("ledcdev_set_blocking_mc() led_cdev_mc->subled_info[0].channel 0x%02x\n", led_cdev_mc->subled_info[0].channel); - pr_debug("ledcdev_set_blocking_mc() led_cdev_mc->subled_info[1].channel 0x%02x\n", led_cdev_mc->subled_info[1].channel); - pr_debug("ledcdev_set_blocking_mc() led_cdev_mc->subled_info[2].channel 0x%02x\n", led_cdev_mc->subled_info[2].channel); - - if (led_cdev_mc->subled_info[0].channel == 0) { - region = REGION_LEFT; - } - else if (led_cdev_mc->subled_info[0].channel == 1) { - region = REGION_CENTER; - } - else if (led_cdev_mc->subled_info[0].channel == 2) { - region = REGION_RIGHT; - } - else { - region = REGION_EXTRA; - } - - colorcode = (led_cdev_mc->subled_info[0].intensity << 16) + - (led_cdev_mc->subled_info[1].intensity << 8) + - led_cdev_mc->subled_info[2].intensity; - - if (!set_color(region, colorcode)) { - // after succesfully setting color, update our state struct - // depending on which region was changed - switch (region) { - case REGION_LEFT: - kbd_led_state.color.left = colorcode; - break; - case REGION_CENTER: - kbd_led_state.color.center = colorcode; - break; - case REGION_RIGHT: - kbd_led_state.color.right = colorcode; - break; - case REGION_EXTRA: - kbd_led_state.color.extra = colorcode; - break; - } - } - - set_brightness(brightness); - return 0; -} - -static enum led_brightness ledcdev_get(struct led_classdev *led_cdev) { - return kbd_led_state.brightness; -} - -static struct led_classdev cdev_kb = { - .name = KBUILD_MODNAME "::kbd_backlight", - .max_brightness = BRIGHTNESS_MAX, - .brightness_set_blocking = &ledcdev_set_blocking, - .brightness_get = &ledcdev_get, - .brightness = BRIGHTNESS_DEFAULT, -}; - -static struct led_classdev_mc cdev_kb_mc[3]; - -static struct mc_subled cdev_kb_mc_subled[3][3]; +static DEVICE_ATTR(kbd_backlight_mode, 0644, show_kbd_backlight_modes_fs, set_kbd_backlight_mode_fs); static void clevo_keyboard_init_device_interface(struct platform_device *dev) { - u32 status; - union acpi_object *result; - // The very first Clevos with keyboard backlight did have fixed color, but not yet the CLEVO_METHOD_ID_GET_SPECS. To - // not break these, we set this as default for the time being, better having an extra sysfs entry without function than - // a missing one. This is a temporary fix until we find a way to identify these early keyboard backlight devices. - enum clevo_kb_backlight_type kb_backlight_type = CLEVO_KB_BACKLIGHT_TYPE_FIXED_COLOR; - - status = clevo_evaluate_method2(CLEVO_METHOD_ID_GET_SPECS, 0, &result); - if (!status) { - if (result->type == ACPI_TYPE_BUFFER) { - pr_debug("CLEVO_METHOD_ID_GET_SPECS successful\n"); - kb_backlight_type = result->buffer.pointer[0x0f]; - } - else { - pr_debug("CLEVO_METHOD_ID_GET_SPECS return value has wrong type\n"); - } - ACPI_FREE(result); - } - else { - pr_debug("CLEVO_METHOD_ID_GET_SPECS failed\n"); - } - pr_debug("Keyboard Backlight Type: 0x%02x\n", kb_backlight_type); - // Setup sysfs - if (device_create_file(&dev->dev, &dev_attr_state) != 0) { - TUXEDO_ERROR("Sysfs attribute file creation failed for state\n"); - } - - if (kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_1_ZONE_RGB || - kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_3_ZONE_RGB) { - if (device_create_file - (&dev->dev, &dev_attr_color_left) != 0) { - TUXEDO_ERROR - ("Sysfs attribute file creation failed for color left\n"); - } - else { - kbd_led_state.has_left = 1; - } - } - - if (kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_3_ZONE_RGB) { - if (device_create_file - (&dev->dev, &dev_attr_color_center) != 0) { - TUXEDO_ERROR - ("Sysfs attribute file creation failed for color center\n"); - } - else { - kbd_led_state.has_center = 1; - } - } - - if (kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_3_ZONE_RGB) { - if (device_create_file - (&dev->dev, &dev_attr_color_right) != 0) { - TUXEDO_ERROR - ("Sysfs attribute file creation failed for color right\n"); - } - else { - kbd_led_state.has_right = 1; - } - } - - /*if (set_color(REGION_EXTRA, KB_COLOR_DEFAULT) != 0) { - TUXEDO_DEBUG("Keyboard does not support EXTRA Color"); - kbd_led_state.has_extra = 0; - } else { - kbd_led_state.has_extra = 1; - if (device_create_file - (&dev->dev, - &dev_attr_color_extra) != 0) { - TUXEDO_ERROR - ("Sysfs attribute file creation failed for color extra\n"); - } - - set_color(REGION_EXTRA, param_color_extra); - }*/ - kbd_led_state.has_extra = 0; // No known Clevo device using this region, set color check doesn't work. - - if (device_create_file(&dev->dev, &dev_attr_extra) != - 0) { - TUXEDO_ERROR - ("Sysfs attribute file creation failed for extra information flag\n"); - } - - if (kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_3_ZONE_RGB) { - if (device_create_file(&dev->dev, &dev_attr_mode) != - 0) { + if (clevo_leds_get_backlight_type() == CLEVO_KB_BACKLIGHT_TYPE_3_ZONE_RGB) { + if (device_create_file(&dev->dev, &dev_attr_kbd_backlight_mode) != 0) { TUXEDO_ERROR("Sysfs attribute file creation failed for blinking pattern\n"); } else { kbd_led_state.has_mode = 1; } } - - if (kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_FIXED_COLOR || - kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_1_ZONE_RGB || - kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_3_ZONE_RGB) { - if (device_create_file - (&dev->dev, &dev_attr_brightness) != 0) { - TUXEDO_ERROR - ("Sysfs attribute file creation failed for brightness\n"); - } - else { - kbd_led_state.has_brightness = 1; - } - } - - if (kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_FIXED_COLOR) { - led_classdev_register(&dev->dev, &cdev_kb); - } - - if (kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_1_ZONE_RGB) { - cdev_kb_mc[0].led_cdev.name = KBUILD_MODNAME "::kbd_backlight"; - cdev_kb_mc[0].led_cdev.max_brightness = BRIGHTNESS_MAX; - cdev_kb_mc[0].led_cdev.brightness_set_blocking = &ledcdev_set_blocking_mc; - cdev_kb_mc[0].led_cdev.brightness_get = &ledcdev_get; - cdev_kb_mc[0].led_cdev.brightness = BRIGHTNESS_DEFAULT; - cdev_kb_mc[0].num_colors = 3; - cdev_kb_mc[0].subled_info = cdev_kb_mc_subled[0]; - cdev_kb_mc[0].subled_info[0].color_index = LED_COLOR_ID_RED; - cdev_kb_mc[0].subled_info[0].intensity = BRIGHTNESS_DEFAULT; - cdev_kb_mc[0].subled_info[0].channel = 0; - cdev_kb_mc[0].subled_info[1].color_index = LED_COLOR_ID_GREEN; - cdev_kb_mc[0].subled_info[1].intensity = BRIGHTNESS_DEFAULT; - cdev_kb_mc[0].subled_info[1].channel = 0; - cdev_kb_mc[0].subled_info[2].color_index = LED_COLOR_ID_BLUE; - cdev_kb_mc[0].subled_info[2].intensity = BRIGHTNESS_DEFAULT; - cdev_kb_mc[0].subled_info[2].channel = 0; - devm_led_classdev_multicolor_register(&dev->dev, &cdev_kb_mc[0]); - } - - if (kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_3_ZONE_RGB) { - cdev_kb_mc[0].led_cdev.name = KBUILD_MODNAME "::kbd_backlight"; - cdev_kb_mc[0].led_cdev.max_brightness = BRIGHTNESS_MAX; - cdev_kb_mc[0].led_cdev.brightness_set_blocking = &ledcdev_set_blocking_mc; - cdev_kb_mc[0].led_cdev.brightness_get = &ledcdev_get; - cdev_kb_mc[0].led_cdev.brightness = BRIGHTNESS_DEFAULT; - cdev_kb_mc[0].num_colors = 3; - cdev_kb_mc[0].subled_info = cdev_kb_mc_subled[0]; - cdev_kb_mc[0].subled_info[0].color_index = LED_COLOR_ID_RED; - cdev_kb_mc[0].subled_info[0].intensity = BRIGHTNESS_DEFAULT; - cdev_kb_mc[0].subled_info[0].channel = 0; - cdev_kb_mc[0].subled_info[1].color_index = LED_COLOR_ID_GREEN; - cdev_kb_mc[0].subled_info[1].intensity = BRIGHTNESS_DEFAULT; - cdev_kb_mc[0].subled_info[1].channel = 0; - cdev_kb_mc[0].subled_info[2].color_index = LED_COLOR_ID_BLUE; - cdev_kb_mc[0].subled_info[2].intensity = BRIGHTNESS_DEFAULT; - cdev_kb_mc[0].subled_info[2].channel = 0; - devm_led_classdev_multicolor_register(&dev->dev, &cdev_kb_mc[0]); - - cdev_kb_mc[1].led_cdev.name = KBUILD_MODNAME "::kbd_backlight"; - cdev_kb_mc[1].led_cdev.max_brightness = BRIGHTNESS_MAX; - cdev_kb_mc[1].led_cdev.brightness_set_blocking = &ledcdev_set_blocking_mc; - cdev_kb_mc[1].led_cdev.brightness_get = &ledcdev_get; - cdev_kb_mc[1].led_cdev.brightness = BRIGHTNESS_DEFAULT; - cdev_kb_mc[1].num_colors = 3; - cdev_kb_mc[1].subled_info = cdev_kb_mc_subled[1]; - cdev_kb_mc[1].subled_info[0].color_index = LED_COLOR_ID_RED; - cdev_kb_mc[1].subled_info[0].intensity = BRIGHTNESS_DEFAULT; - cdev_kb_mc[1].subled_info[0].channel = 1; - cdev_kb_mc[1].subled_info[1].color_index = LED_COLOR_ID_GREEN; - cdev_kb_mc[1].subled_info[1].intensity = BRIGHTNESS_DEFAULT; - cdev_kb_mc[1].subled_info[1].channel = 1; - cdev_kb_mc[1].subled_info[2].color_index = LED_COLOR_ID_BLUE; - cdev_kb_mc[1].subled_info[2].intensity = BRIGHTNESS_DEFAULT; - cdev_kb_mc[1].subled_info[2].channel = 1; - devm_led_classdev_multicolor_register(&dev->dev, &cdev_kb_mc[1]); - - cdev_kb_mc[2].led_cdev.name = KBUILD_MODNAME "::kbd_backlight"; - cdev_kb_mc[2].led_cdev.max_brightness = BRIGHTNESS_MAX; - cdev_kb_mc[2].led_cdev.brightness_set_blocking = &ledcdev_set_blocking_mc; - cdev_kb_mc[2].led_cdev.brightness_get = &ledcdev_get; - cdev_kb_mc[2].led_cdev.brightness = BRIGHTNESS_DEFAULT; - cdev_kb_mc[2].num_colors = 3; - cdev_kb_mc[2].subled_info = cdev_kb_mc_subled[2]; - cdev_kb_mc[2].subled_info[0].color_index = LED_COLOR_ID_RED; - cdev_kb_mc[2].subled_info[0].intensity = BRIGHTNESS_DEFAULT; - cdev_kb_mc[2].subled_info[0].channel = 2; - cdev_kb_mc[2].subled_info[1].color_index = LED_COLOR_ID_GREEN; - cdev_kb_mc[2].subled_info[1].intensity = BRIGHTNESS_DEFAULT; - cdev_kb_mc[2].subled_info[1].channel = 2; - cdev_kb_mc[2].subled_info[2].color_index = LED_COLOR_ID_BLUE; - cdev_kb_mc[2].subled_info[2].intensity = BRIGHTNESS_DEFAULT; - cdev_kb_mc[2].subled_info[2].channel = 2; - devm_led_classdev_multicolor_register(&dev->dev, &cdev_kb_mc[2]); - } -} - -void clevo_keyboard_write_state(void) -{ - // Note: - // - set_blinking_pattern also writes colors - // - set_brightness, set_enabled, set_blinking_pattern - // still also update state - set_blinking_pattern(kbd_led_state.blinking_pattern); - set_brightness(kbd_led_state.brightness); - set_enabled(kbd_led_state.enabled); } /** @@ -981,20 +388,11 @@ int clevo_keyboard_init(void) { bool performance_profile_set_workaround; - // Init state from params - kbd_led_state.color.left = param_color_left; - kbd_led_state.color.center = param_color_center; - kbd_led_state.color.right = param_color_right; - kbd_led_state.color.extra = param_color_extra; + kbd_led_state.mode = param_kbd_backlight_mode; + set_kbd_backlight_mode(kbd_led_state.mode); - kbd_led_state.blinking_pattern = param_blinking_pattern; - - if (param_brightness > BRIGHTNESS_MAX) param_brightness = BRIGHTNESS_DEFAULT; - kbd_led_state.brightness = param_brightness; - - kbd_led_state.enabled = param_state; - - clevo_keyboard_write_state(); + clevo_evaluate_method(CLEVO_CMD_SET_EVENTS_ENABLED, 0, NULL); + set_enabled_cmd(1); // Workaround for firmware issue not setting selected performance profile. // Explicitly set "performance" perf. profile on init regardless of what is chosen @@ -1013,7 +411,7 @@ int clevo_keyboard_init(void) ; if (performance_profile_set_workaround) { TUXEDO_INFO("Performance profile 'performance' set workaround applied\n"); - clevo_evaluate_method(0x79, 0x19000002, NULL); + clevo_evaluate_method(CLEVO_CMD_OPT, 0x19000002, NULL); } return 0; @@ -1022,6 +420,7 @@ int clevo_keyboard_init(void) static int clevo_keyboard_probe(struct platform_device *dev) { clevo_keyboard_init_device_interface(dev); + clevo_leds_init(dev); clevo_keyboard_init(); return 0; @@ -1029,38 +428,15 @@ static int clevo_keyboard_probe(struct platform_device *dev) static void clevo_keyboard_remove_device_interface(struct platform_device *dev) { - device_remove_file(&dev->dev, &dev_attr_state); - - if (kbd_led_state.has_left == 1) { - device_remove_file(&dev->dev, &dev_attr_color_left); - } - - if (kbd_led_state.has_center == 1) { - device_remove_file(&dev->dev, &dev_attr_color_center); - } - - if (kbd_led_state.has_right == 1) { - device_remove_file(&dev->dev, &dev_attr_color_right); - } - - device_remove_file(&dev->dev, &dev_attr_extra); - if (kbd_led_state.has_mode == 1) { - device_remove_file(&dev->dev, &dev_attr_mode); - } - - if (kbd_led_state.has_brightness == 1) { - device_remove_file(&dev->dev, &dev_attr_brightness); - } - - if (kbd_led_state.has_extra == 1) { - device_remove_file(&dev->dev, &dev_attr_color_extra); + device_remove_file(&dev->dev, &dev_attr_kbd_backlight_mode); } } static int clevo_keyboard_remove(struct platform_device *dev) { clevo_keyboard_remove_device_interface(dev); + clevo_leds_remove(dev); return 0; } @@ -1073,10 +449,8 @@ static int clevo_keyboard_suspend(struct platform_device *dev, pm_message_t stat static int clevo_keyboard_resume(struct platform_device *dev) { - clevo_evaluate_method(CLEVO_METHOD_ID_GET_AP, 0, NULL); - - clevo_keyboard_write_state(); - + clevo_evaluate_method(CLEVO_CMD_SET_EVENTS_ENABLED, 0, NULL); + set_enabled_cmd(1); return 0; } diff --git a/src/clevo_leds.c b/src/clevo_leds.c new file mode 100644 index 0000000..7cbd3bf --- /dev/null +++ b/src/clevo_leds.c @@ -0,0 +1,285 @@ +/*! + * Copyright (c) 2018-2020 TUXEDO Computers GmbH + * + * This file is part of tuxedo-keyboard. + * + * tuxedo-keyboard is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software. If not, see . + */ + +#include "clevo_leds.h" + +#include "clevo_interfaces.h" + +#include +#include + +#define CLEVO_KBD_BRIGHTNESS_MIN 0 +#define CLEVO_KBD_BRIGHTNESS_MAX 255 +#define CLEVO_KBD_BRIGHTNESS_DEFAULT (CLEVO_KBD_BRIGHTNESS_MAX * 0.5) + +#define CLEVO_KB_COLOR_DEFAULT_RED 0xFF +#define CLEVO_KB_COLOR_DEFAULT_GREEN 0xFF +#define CLEVO_KB_COLOR_DEFAULT_BLUE 0xFF +#define CLEVO_KB_COLOR_DEFAULT ((CLEVO_KB_COLOR_DEFAULT_RED << 16) + (CLEVO_KB_COLOR_DEFAULT_GREEN << 8) + CLEVO_KB_COLOR_DEFAULT_BLUE) + +// The very first Clevos with keyboard backlight did have fixed color, but not yet the CLEVO_METHOD_ID_GET_SPECS. To +// not break these, we set this as default for the time being, better having an extra sysfs entry without function than +// a missing one. This is a temporary fix until we find a way to identify these early keyboard backlight devices. +static enum clevo_kb_backlight_types clevo_kb_backlight_type = CLEVO_KB_BACKLIGHT_TYPE_FIXED_COLOR; + +static int clevo_evaluate_set_brightness(u8 brightness) +{ + pr_debug("Set brightness on %d", brightness); + + return clevo_evaluate_method (CLEVO_CMD_SET_KB_LEDS, 0xF4000000 | brightness, NULL); +} + +static int clevo_evaluate_set_color(u32 zone, u32 color) +{ + u32 cset = ((color & 0x0000FF) << 16) | ((color & 0xFF0000) >> 8) | ((color & 0x00FF00) >> 8); + u32 clevo_submethod_arg = zone | cset; + + pr_debug("Set Color '%08x' for region '%08x'", color, zone); + + return clevo_evaluate_method(CLEVO_CMD_SET_KB_LEDS, clevo_submethod_arg, NULL); +} + +static void clevo_leds_set_brightness(struct led_classdev *led_cdev __always_unused, enum led_brightness brightness) { + int ret = clevo_evaluate_set_brightness(brightness); + if (ret) { + pr_debug("clevo_leds_set_brightness(): clevo_evaluate_set_brightness() failed"); + return; + } + led_cdev->brightness = brightness; +} + +static void clevo_leds_set_brightness_mc(struct led_classdev *led_cdev, enum led_brightness brightness) { + int ret; + u32 zone, color; + struct led_classdev_mc *mcled_cdev = lcdev_to_mccdev(led_cdev); + + ret = clevo_evaluate_set_brightness(CLEVO_KBD_BRIGHTNESS_MAX); + if (ret) { + pr_debug("clevo_leds_set_brightness_mc(): clevo_evaluate_set_brightness() failed"); + return; + } + + zone = mcled_cdev->subled_info[0].channel; + + led_mc_calc_color_components(mcled_cdev, brightness); + color = (mcled_cdev->subled_info[0].brightness << 16) + + (mcled_cdev->subled_info[1].brightness << 8) + + mcled_cdev->subled_info[2].brightness; + + ret = clevo_evaluate_set_color(zone, color); + if (ret) { + pr_debug("clevo_leds_set_brightness_mc(): clevo_evaluate_set_color() failed"); + return; + } + led_cdev->brightness = brightness; +} + +static struct led_classdev clevo_led_cdev = { + .name = "rgb:kbd_backlight", + .max_brightness = CLEVO_KBD_BRIGHTNESS_MAX, + .brightness_set = &clevo_leds_set_brightness, + .brightness = CLEVO_KBD_BRIGHTNESS_DEFAULT +}; + +static struct mc_subled clevo_mcled_cdevs_subleds[3][3] = { + { + { + .color_index = LED_COLOR_ID_RED, + .brightness = CLEVO_KBD_BRIGHTNESS_MAX, + .intensity = CLEVO_KB_COLOR_DEFAULT_RED, + .channel = CLEVO_CMD_SET_KB_LEDS_SUB_RGB_ZONE_0 + }, + { + .color_index = LED_COLOR_ID_GREEN, + .brightness = CLEVO_KBD_BRIGHTNESS_MAX, + .intensity = CLEVO_KB_COLOR_DEFAULT_GREEN, + .channel = CLEVO_CMD_SET_KB_LEDS_SUB_RGB_ZONE_0 + }, + { + .color_index = LED_COLOR_ID_BLUE, + .brightness = CLEVO_KBD_BRIGHTNESS_MAX, + .intensity = CLEVO_KB_COLOR_DEFAULT_BLUE, + .channel = CLEVO_CMD_SET_KB_LEDS_SUB_RGB_ZONE_0 + } + }, + { + { + .color_index = LED_COLOR_ID_RED, + .brightness = CLEVO_KBD_BRIGHTNESS_MAX, + .intensity = CLEVO_KB_COLOR_DEFAULT_RED, + .channel = CLEVO_CMD_SET_KB_LEDS_SUB_RGB_ZONE_1 + }, + { + .color_index = LED_COLOR_ID_GREEN, + .brightness = CLEVO_KBD_BRIGHTNESS_MAX, + .intensity = CLEVO_KB_COLOR_DEFAULT_GREEN, + .channel = CLEVO_CMD_SET_KB_LEDS_SUB_RGB_ZONE_1 + }, + { + .color_index = LED_COLOR_ID_BLUE, + .brightness = CLEVO_KBD_BRIGHTNESS_MAX, + .intensity = CLEVO_KB_COLOR_DEFAULT_BLUE, + .channel = CLEVO_CMD_SET_KB_LEDS_SUB_RGB_ZONE_1 + } + }, + { + { + .color_index = LED_COLOR_ID_RED, + .brightness = CLEVO_KBD_BRIGHTNESS_MAX, + .intensity = CLEVO_KB_COLOR_DEFAULT_RED, + .channel = CLEVO_CMD_SET_KB_LEDS_SUB_RGB_ZONE_2 + }, + { + .color_index = LED_COLOR_ID_GREEN, + .brightness = CLEVO_KBD_BRIGHTNESS_MAX, + .intensity = CLEVO_KB_COLOR_DEFAULT_GREEN, + .channel = CLEVO_CMD_SET_KB_LEDS_SUB_RGB_ZONE_2 + }, + { + .color_index = LED_COLOR_ID_BLUE, + .brightness = CLEVO_KBD_BRIGHTNESS_MAX, + .intensity = CLEVO_KB_COLOR_DEFAULT_BLUE, + .channel = CLEVO_CMD_SET_KB_LEDS_SUB_RGB_ZONE_2 + } + } +}; + +static struct led_classdev_mc clevo_mcled_cdevs[3] = { + { + .led_cdev.name = "rgb:kbd_backlight", + .led_cdev.max_brightness = CLEVO_KBD_BRIGHTNESS_MAX, + .led_cdev.brightness_set = &clevo_leds_set_brightness_mc, + .led_cdev.brightness = CLEVO_KBD_BRIGHTNESS_DEFAULT, + .subled_info = clevo_mcled_cdevs_subleds[0] + }, + { + .led_cdev.name = "rgb:kbd_backlight", + .led_cdev.max_brightness = CLEVO_KBD_BRIGHTNESS_MAX, + .led_cdev.brightness_set = &clevo_leds_set_brightness_mc, + .led_cdev.brightness = CLEVO_KBD_BRIGHTNESS_DEFAULT, + .subled_info = clevo_mcled_cdevs_subleds[1] + }, + { + .led_cdev.name = "rgb:kbd_backlight", + .led_cdev.max_brightness = CLEVO_KBD_BRIGHTNESS_MAX, + .led_cdev.brightness_set = &clevo_leds_set_brightness_mc, + .led_cdev.brightness = CLEVO_KBD_BRIGHTNESS_DEFAULT, + .subled_info = clevo_mcled_cdevs_subleds[2] + } +}; + +int clevo_leds_init(struct platform_device *dev) +{ + u32 status; + union acpi_object *result; + + status = clevo_evaluate_method2(CLEVO_CMD_GET_SPECS, 0, &result); + if (!status) { + if (result->type == ACPI_TYPE_BUFFER) { + pr_debug("CLEVO_CMD_GET_SPECS successful\n"); + clevo_kb_backlight_type = result->buffer.pointer[0x0f]; + } + else { + pr_debug("CLEVO_CMD_GET_SPECS return value has wrong type\n"); + } + ACPI_FREE(result); + } + else { + pr_debug("CLEVO_CMD_GET_SPECS failed\n"); + } + pr_debug("Keyboard backlight type: 0x%02x\n", clevo_kb_backlight_type); + + if (clevo_kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_FIXED_COLOR) { + led_classdev_register(&dev->dev, &clevo_led_cdev); + } + else if (clevo_kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_1_ZONE_RGB) { + devm_led_classdev_multicolor_register(&dev->dev, &clevo_mcled_cdevs[0]); + } + else if (clevo_kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_3_ZONE_RGB) { + devm_led_classdev_multicolor_register(&dev->dev, &clevo_mcled_cdevs[0]); + devm_led_classdev_multicolor_register(&dev->dev, &clevo_mcled_cdevs[1]); + devm_led_classdev_multicolor_register(&dev->dev, &clevo_mcled_cdevs[2]); + } + + return 0; +} +EXPORT_SYMBOL(clevo_leds_init); + +int clevo_leds_remove(struct platform_device *dev) { + if (clevo_kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_FIXED_COLOR) { + led_classdev_unregister(&clevo_led_cdev); + } + else if (clevo_kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_1_ZONE_RGB) { + devm_led_classdev_multicolor_unregister(&dev->dev, &clevo_mcled_cdevs[0]); + } + else if (clevo_kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_3_ZONE_RGB) { + devm_led_classdev_multicolor_unregister(&dev->dev, &clevo_mcled_cdevs[0]); + devm_led_classdev_multicolor_unregister(&dev->dev, &clevo_mcled_cdevs[1]); + devm_led_classdev_multicolor_unregister(&dev->dev, &clevo_mcled_cdevs[2]); + } + + return 0; +} +EXPORT_SYMBOL(clevo_leds_remove); + +enum clevo_kb_backlight_types clevo_leds_get_backlight_type() { + return clevo_kb_backlight_type; +} +EXPORT_SYMBOL(clevo_leds_get_backlight_type); + +void clevo_leds_set_brightness_extern(u32 brightness) { + if (clevo_kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_FIXED_COLOR) { + clevo_led_cdev.brightness_set(&clevo_led_cdev, brightness); + } + else if (clevo_kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_1_ZONE_RGB) { + clevo_mcled_cdevs[0].led_cdev.brightness_set(&clevo_mcled_cdevs[0].led_cdev, brightness); + } + else if (clevo_kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_3_ZONE_RGB) { + clevo_mcled_cdevs[0].led_cdev.brightness_set(&clevo_mcled_cdevs[0].led_cdev, brightness); + clevo_mcled_cdevs[1].led_cdev.brightness_set(&clevo_mcled_cdevs[1].led_cdev, brightness); + clevo_mcled_cdevs[2].led_cdev.brightness_set(&clevo_mcled_cdevs[2].led_cdev, brightness); + } +} +EXPORT_SYMBOL(clevo_leds_set_brightness_extern); + +void clevo_leds_set_color_extern(u32 color) { + if (clevo_kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_1_ZONE_RGB) { + clevo_mcled_cdevs[0].subled_info[0].intensity = (color >> 16) & 0xff; + clevo_mcled_cdevs[0].subled_info[1].intensity = (color >> 8) & 0xff; + clevo_mcled_cdevs[0].subled_info[2].intensity = color & 0xff; + clevo_mcled_cdevs[0].led_cdev.brightness_set(&clevo_mcled_cdevs[0].led_cdev, clevo_mcled_cdevs[0].led_cdev.brightness); + } + else if (clevo_kb_backlight_type == CLEVO_KB_BACKLIGHT_TYPE_3_ZONE_RGB) { + clevo_mcled_cdevs[0].subled_info[0].intensity = (color >> 16) & 0xff; + clevo_mcled_cdevs[0].subled_info[1].intensity = (color >> 8) & 0xff; + clevo_mcled_cdevs[0].subled_info[2].intensity = color & 0xff; + clevo_mcled_cdevs[0].led_cdev.brightness_set(&clevo_mcled_cdevs[0].led_cdev, clevo_mcled_cdevs[0].led_cdev.brightness); + clevo_mcled_cdevs[1].subled_info[0].intensity = (color >> 16) & 0xff; + clevo_mcled_cdevs[1].subled_info[1].intensity = (color >> 8) & 0xff; + clevo_mcled_cdevs[1].subled_info[2].intensity = color & 0xff; + clevo_mcled_cdevs[1].led_cdev.brightness_set(&clevo_mcled_cdevs[1].led_cdev, clevo_mcled_cdevs[0].led_cdev.brightness); + clevo_mcled_cdevs[2].subled_info[0].intensity = (color >> 16) & 0xff; + clevo_mcled_cdevs[2].subled_info[1].intensity = (color >> 8) & 0xff; + clevo_mcled_cdevs[2].subled_info[2].intensity = color & 0xff; + clevo_mcled_cdevs[2].led_cdev.brightness_set(&clevo_mcled_cdevs[2].led_cdev, clevo_mcled_cdevs[0].led_cdev.brightness); + } +} +EXPORT_SYMBOL(clevo_leds_set_color_extern); + +MODULE_LICENSE("GPL"); diff --git a/src/clevo_leds.h b/src/clevo_leds.h new file mode 100644 index 0000000..e6bf721 --- /dev/null +++ b/src/clevo_leds.h @@ -0,0 +1,39 @@ +/*! + * Copyright (c) 2018-2020 TUXEDO Computers GmbH + * + * This file is part of tuxedo-keyboard. + * + * tuxedo-keyboard is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software. If not, see . + */ + +#ifndef CLEVO_LEDS_H +#define CLEVO_LEDS_H + +#include + +enum clevo_kb_backlight_types { + CLEVO_KB_BACKLIGHT_TYPE_NONE = 0x00, + CLEVO_KB_BACKLIGHT_TYPE_FIXED_COLOR = 0x01, + CLEVO_KB_BACKLIGHT_TYPE_3_ZONE_RGB = 0x02, + CLEVO_KB_BACKLIGHT_TYPE_1_ZONE_RGB = 0x06, + CLEVO_KB_BACKLIGHT_TYPE_PER_KEY_RGB = 0xf3 +}; + +int clevo_leds_init(struct platform_device *dev); +int clevo_leds_remove(struct platform_device *dev); +enum clevo_kb_backlight_types clevo_leds_get_backlight_type(void); +void clevo_leds_set_brightness_extern(u32 brightness); +void clevo_leds_set_color_extern(u32 color); + +#endif // CLEVO_LEDS_H diff --git a/src/tuxedo_keyboard_common.h b/src/tuxedo_keyboard_common.h index 84f6059..60e62d8 100644 --- a/src/tuxedo_keyboard_common.h +++ b/src/tuxedo_keyboard_common.h @@ -129,6 +129,7 @@ static const struct kernel_param_ops param_ops_brightness_ops = { .get = param_get_int, }; +// TODO remove static ushort param_brightness = 0xffff; // Default unset value (higher than max) module_param_cb(brightness, ¶m_ops_brightness_ops, ¶m_brightness, S_IRUSR); @@ -139,4 +140,4 @@ static char param_color[COLOR_STRING_LEN]; module_param_string(color, param_color, COLOR_STRING_LEN, S_IRUSR); MODULE_PARM_DESC(color, "Preset color for the keyboard backlight as string"); -#endif \ No newline at end of file +#endif diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index 46de633..0e03e61 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include "uniwill_interfaces.h"