summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabio Baltieri <fabiobaltieri@google.com>2022-02-16 18:39:29 +0000
committerCommit Bot <commit-bot@chromium.org>2022-02-18 01:06:15 +0000
commit3639eb8ab632fb1f910f17059d181c3c65f9e119 (patch)
treebaa7fde1bcf1af3971c5a56234bcbf1426ea9543
parentfe2f6a9cc3f2a58316a511c69fe2f8d78d86a7af (diff)
downloadchrome-ec-3639eb8ab632fb1f910f17059d181c3c65f9e119.tar.gz
common: pwm: handle EC_PWM_TYPE_KB_LIGHT with the kblight API
Change the PWM host command handling of EC_PWM_TYPE_KB_LIGHT to use the kblight APIs directly rather than the PWM ones. This makes the host command work correctly with kblight drivers that do not use the ECOS PWM APIs. Tested snippet: localhost /home/chronos/user # ectool pwmgetkblight Keyboard backlight disabled. localhost /home/chronos/user # ectool pwmgetduty kb Current PWM duty: 0 <change the backlight on the device> localhost /home/chronos/user # ectool pwmgetkblight Current keyboard backlight percent: 40 localhost /home/chronos/user # ectool pwmgetduty kb Current PWM duty: 26214 localhost /home/chronos/user # ectool pwmsetkblight 50 Keyboard backlight set. localhost /home/chronos/user # ectool pwmgetduty kb Current PWM duty: 32767 localhost /home/chronos/user # ectool pwmsetduty kb 65535 PWM set. localhost /home/chronos/user # ectool pwmgetkblight Current keyboard backlight percent: 100 BRANCH=none BUG=b:217741090 TEST=ectool kblight and pwm commands TEST=make runhosttests -j8 TEST=zmake testall Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com> Change-Id: Ib73f321334ff63cd8c512d0985c3504133834ec4 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3468504 Reviewed-by: Aaron Massey <aaronmassey@google.com>
-rw-r--r--common/pwm.c30
1 files changed, 23 insertions, 7 deletions
diff --git a/common/pwm.c b/common/pwm.c
index 41989a94e0..858057946b 100644
--- a/common/pwm.c
+++ b/common/pwm.c
@@ -8,6 +8,7 @@
#include "gpio.h"
#include "hooks.h"
#include "host_command.h"
+#include "keyboard_backlight.h"
#include "pwm.h"
#include "util.h"
@@ -17,6 +18,10 @@
#ifdef CONFIG_PWM
+#define PWM_RAW_TO_PERCENT(v) \
+ DIV_ROUND_NEAREST((uint32_t)(v) * 100, UINT16_MAX)
+#define PWM_PERCENT_TO_RAW(v) ((uint32_t)(v) * UINT16_MAX / 100)
+
/*
* Get target channel based on type / index host command parameters.
* Returns 0 if a valid channel is selected, -1 on error.
@@ -27,11 +32,6 @@ static int get_target_channel(enum pwm_channel *channel, int type, int index)
case EC_PWM_TYPE_GENERIC:
*channel = index;
break;
-#ifdef CONFIG_PWM_KBLIGHT
- case EC_PWM_TYPE_KB_LIGHT:
- *channel = PWM_CH_KBLIGHT;
- break;
-#endif
#ifdef CONFIG_PWM_DISPLIGHT
case EC_PWM_TYPE_DISPLAY_LIGHT:
*channel = PWM_CH_DISPLIGHT;
@@ -49,13 +49,13 @@ __attribute__((weak)) void pwm_set_raw_duty(enum pwm_channel ch, uint16_t duty)
int percent;
/* Convert 16 bit duty to percent on [0, 100] */
- percent = DIV_ROUND_NEAREST((uint32_t)duty * 100, 65535);
+ percent = PWM_RAW_TO_PERCENT(duty);
pwm_set_duty(ch, percent);
}
__attribute__((weak)) uint16_t pwm_get_raw_duty(enum pwm_channel ch)
{
- return (pwm_get_duty(ch) * 65535) / 100;
+ return PWM_PERCENT_TO_RAW(pwm_get_duty(ch));
}
static enum ec_status
@@ -64,6 +64,14 @@ host_command_pwm_set_duty(struct host_cmd_handler_args *args)
const struct ec_params_pwm_set_duty *p = args->params;
enum pwm_channel channel;
+#ifdef CONFIG_PWM_KBLIGHT
+ if (p->pwm_type == EC_PWM_TYPE_KB_LIGHT) {
+ kblight_set(PWM_RAW_TO_PERCENT(p->duty));
+ kblight_enable(p->duty > 0);
+ return EC_RES_SUCCESS;
+ }
+#endif
+
if (get_target_channel(&channel, p->pwm_type, p->index))
return EC_RES_INVALID_PARAM;
@@ -84,6 +92,14 @@ host_command_pwm_get_duty(struct host_cmd_handler_args *args)
enum pwm_channel channel;
+#ifdef CONFIG_PWM_KBLIGHT
+ if (p->pwm_type == EC_PWM_TYPE_KB_LIGHT) {
+ r->duty = PWM_PERCENT_TO_RAW(kblight_get());
+ args->response_size = sizeof(*r);
+ return EC_RES_SUCCESS;
+ }
+#endif
+
if (get_target_channel(&channel, p->pwm_type, p->index))
return EC_RES_INVALID_PARAM;