diff options
author | Shawn Nematbakhsh <shawnn@chromium.org> | 2016-05-10 13:24:51 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2016-05-11 18:24:30 -0700 |
commit | 9494fc0dd10f1e6b59084c691f1f9f1499e401f1 (patch) | |
tree | 163ff88a7b731abfbdedc9852269ecd88b44a10a /common | |
parent | 3e9490031bbf5cd48c2acc314c38b201d50cb748 (diff) | |
download | chrome-ec-9494fc0dd10f1e6b59084c691f1f9f1499e401f1.tar.gz |
pwm: Add generic PWM control host commands
Add generic PWM host commands for setting + getting duty cycle. PWMs can
be controlled through index (board-specific meaning) or by type
(currently KB backlight and display backlight are supported, more can be
added as needed).
BUG=chrome-os-partner:52002
BRANCH=None
TEST=Manual on chell.
`ectool pwmsetduty kb 100` - Verify KB backlight goes to 100%
`ectool pwmgetduty kb` - Prints 100
`ectool pwmgetduty 0` - Prints 100
`ectool pwmsetduty 0 0` - Verify KB backlight goes to 0%
`ectool pwmgetduty kb` - Prints 0
`ectool pwmgetduty disp` - Error res 3 (unsupported PWM type)
`ectool pwmsetduty 1` - Error res 3 (non-existent PWM index)
Signed-off-by: Shawn Nematbakhsh <shawnn@chromium.org>
Change-Id: I607c92a291e6c2e3af8238eaf22ad2bb81ffc805
Reviewed-on: https://chromium-review.googlesource.com/344012
Commit-Ready: Shawn N <shawnn@chromium.org>
Tested-by: Shawn N <shawnn@chromium.org>
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Reviewed-by: Mary Ruthven <mruthven@chromium.org>
Diffstat (limited to 'common')
-rw-r--r-- | common/pwm.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/common/pwm.c b/common/pwm.c index 7377d1d170..c689b0acac 100644 --- a/common/pwm.c +++ b/common/pwm.c @@ -7,10 +7,78 @@ #include "console.h" #include "gpio.h" #include "hooks.h" +#include "host_command.h" #include "pwm.h" #include "util.h" #ifdef CONFIG_PWM + +/* + * Get target channel based on type / index host command parameters. + * Returns 0 if a valid channel is selected, -1 on error. + */ +static int get_target_channel(enum pwm_channel *channel, int type, int index) +{ + switch (type) { + 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; + break; +#endif + default: + return -1; + } + + return *channel >= PWM_CH_COUNT; +} + +static int 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; + + if (p->percent > 100) + return EC_RES_INVALID_PARAM; + + if (get_target_channel(&channel, p->pwm_type, p->index)) + return EC_RES_INVALID_PARAM; + + pwm_set_duty(channel, p->percent); + pwm_enable(channel, p->percent > 0); + + return EC_RES_SUCCESS; +} +DECLARE_HOST_COMMAND(EC_CMD_PWM_SET_DUTY, + host_command_pwm_set_duty, + EC_VER_MASK(0)); + +static int host_command_pwm_get_duty(struct host_cmd_handler_args *args) +{ + const struct ec_params_pwm_get_duty *p = args->params; + struct ec_response_pwm_get_duty *r = args->response; + + enum pwm_channel channel; + + if (get_target_channel(&channel, p->pwm_type, p->index)) + return EC_RES_INVALID_PARAM; + + r->percent = pwm_get_duty(channel); + args->response_size = sizeof(*r); + + return EC_RES_SUCCESS; +} +DECLARE_HOST_COMMAND(EC_CMD_PWM_GET_DUTY, + host_command_pwm_get_duty, + EC_VER_MASK(0)); + /** * Print status of a PWM channel. * |