From 731ec3feec00335b6e3176b5ee986266aa20d5c8 Mon Sep 17 00:00:00 2001 From: Quin Chou Date: Fri, 13 Mar 2026 15:19:20 +0800 Subject: [PATCH 1/2] --kblight: Fix on some early systems I tested on Laptop 13 Intel 13th gen and kblight didn't work. Signed-off-by: Daniel Schaefer --- framework_lib/src/chromium_ec/mod.rs | 35 +++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/framework_lib/src/chromium_ec/mod.rs b/framework_lib/src/chromium_ec/mod.rs index 055ee75..39e3612 100644 --- a/framework_lib/src/chromium_ec/mod.rs +++ b/framework_lib/src/chromium_ec/mod.rs @@ -770,22 +770,51 @@ impl CrosEc { /// * `percent` - An integer from 0 to 100. 0 being off, 100 being full brightness pub fn set_keyboard_backlight(&self, percent: u8) { debug_assert!(percent <= 100); + let duty = percent as u16 * (PWM_MAX_DUTY / 100); + let res = EcRequestPwmSetDuty { - duty: percent as u16 * (PWM_MAX_DUTY / 100), + duty, pwm_type: PwmType::KbLight as u8, index: 0, } .send_command(self); + + // Early systems (hx20/hx30) don't enable CONFIG_PWM_KBLIGHT in their EC firmware; + // keyboard backlight is at generic PWM channel index 1. + let res = match res { + Err(EcError::Response(EcResponseStatus::InvalidParameter)) => { + EcRequestPwmSetDuty { + duty, + pwm_type: PwmType::Generic as u8, + index: 1, + } + .send_command(self) + } + other => other, + }; debug_assert!(res.is_ok()); } /// Check the current brightness of the keyboard backlight pub fn get_keyboard_backlight(&self) -> EcResult { - let kblight = EcRequestPwmGetDuty { + let res = EcRequestPwmGetDuty { pwm_type: PwmType::KbLight as u8, index: 0, } - .send_command(self)?; + .send_command(self); + + // Early systems (hx20/hx30) don't enable CONFIG_PWM_KBLIGHT in their EC firmware; + // keyboard backlight is at generic PWM channel index 1. + let kblight = match res { + Err(EcError::Response(EcResponseStatus::InvalidParameter)) => { + EcRequestPwmGetDuty { + pwm_type: PwmType::Generic as u8, + index: 1, + } + .send_command(self)? + } + other => other?, + }; Ok((kblight.duty / (PWM_MAX_DUTY / 100)) as u8) } From 9b915bea4a3b329c7b3aa4bab5841a656d312e29 Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Mon, 30 Mar 2026 00:28:25 +0800 Subject: [PATCH 2/2] kblight: Fix duty/percentage scaling Signed-off-by: Daniel Schaefer --- framework_lib/src/chromium_ec/commands.rs | 10 ++++++++++ framework_lib/src/chromium_ec/mod.rs | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/framework_lib/src/chromium_ec/commands.rs b/framework_lib/src/chromium_ec/commands.rs index 2a22b89..1ebfff6 100644 --- a/framework_lib/src/chromium_ec/commands.rs +++ b/framework_lib/src/chromium_ec/commands.rs @@ -246,6 +246,16 @@ impl EcRequest<()> for EcRequestPwmSetFanDutyV1 { pub const PWM_MAX_DUTY: u16 = 0xFFFF; +pub fn percent_to_duty(percent: u8) -> u16 { + let duty = percent as u32 * PWM_MAX_DUTY as u32; + (duty / 100) as u16 +} + +pub fn duty_to_percent(duty: u16) -> u8 { + let percent = duty as u32 * 100; + (percent / PWM_MAX_DUTY as u32) as u8 +} + #[repr(C, packed)] pub struct EcRequestPwmSetDuty { /// Duty cycle, min 0, max 0xFFFF diff --git a/framework_lib/src/chromium_ec/mod.rs b/framework_lib/src/chromium_ec/mod.rs index 39e3612..f8d675f 100644 --- a/framework_lib/src/chromium_ec/mod.rs +++ b/framework_lib/src/chromium_ec/mod.rs @@ -770,7 +770,7 @@ impl CrosEc { /// * `percent` - An integer from 0 to 100. 0 being off, 100 being full brightness pub fn set_keyboard_backlight(&self, percent: u8) { debug_assert!(percent <= 100); - let duty = percent as u16 * (PWM_MAX_DUTY / 100); + let duty = percent_to_duty(percent); let res = EcRequestPwmSetDuty { duty, @@ -816,7 +816,7 @@ impl CrosEc { other => other?, }; - Ok((kblight.duty / (PWM_MAX_DUTY / 100)) as u8) + Ok(duty_to_percent(kblight.duty)) } pub fn ps2_emulation_enable(&self, enable: bool) -> EcResult<()> {