summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShawn Nematbakhsh <shawnn@chromium.org>2016-05-26 14:38:38 -0700
committerchrome-bot <chrome-bot@chromium.org>2016-05-27 18:08:58 -0700
commitd1beddc463f488e527a90bf5adea92e4e9199a8f (patch)
tree606209a936928e338424abf219f6eddf933304f3
parent8f886b40f4335a43bde5da9096308ae91935c7eb (diff)
downloadchrome-ec-d1beddc463f488e527a90bf5adea92e4e9199a8f.tar.gz
pwm: Modify new PWM host commands to take 16-bit duty cycle
EC_CMD_PWM_SET_DUTY / EC_CMD_PWM_GET_DUTY were recently added and are not yet in use. Future-proof these commands by taking a 16-bit duty cycle parameter and converting it between the [0-100] percent used by internal EC functions. BUG=chromium:615109 BRANCH=None TEST=Manual on chell. `ectool pwmsetduty kb 65535` - Verify KB backlight goes to 100% `ectool pwmgetduty kb` - Prints 65535 `ectool pwmgetduty 0` - Prints 65535 `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) `ectool pwmsetduty kb 6550` + `ectool pwmgetduty kb` - Prints 6553 (round up) `ectool pwmsetduty kb 6560` + `ectool pwmgetduty kb` - Prints 6553 (round down) Signed-off-by: Shawn Nematbakhsh <shawnn@chromium.org> Change-Id: Ic6996fc6e1e69359274b2f9a1120ee7002db991c Reviewed-on: https://chromium-review.googlesource.com/347608 Commit-Ready: Shawn N <shawnn@chromium.org> Tested-by: Shawn N <shawnn@chromium.org> Tested-by: Brian Norris <briannorris@chromium.org> Reviewed-by: Randall Spangler <rspangler@chromium.org> Reviewed-by: Brian Norris <briannorris@chromium.org>
-rw-r--r--common/pwm.c17
-rw-r--r--include/ec_commands.h6
-rw-r--r--util/ectool.c12
3 files changed, 22 insertions, 13 deletions
diff --git a/common/pwm.c b/common/pwm.c
index c689b0acac..84e242abf4 100644
--- a/common/pwm.c
+++ b/common/pwm.c
@@ -40,19 +40,25 @@ static int get_target_channel(enum pwm_channel *channel, int type, int index)
return *channel >= PWM_CH_COUNT;
}
+/*
+ * TODO(crbug.com/615109): These host commands use 16 bit duty cycle, but
+ * all of our internal code uses percent on [0, 100]. Convert internal
+ * functions to use 16 bit duty and remove the conversions below.
+ */
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;
+ int percent;
- if (p->percent > 100)
- return EC_RES_INVALID_PARAM;
+ /* Convert 16 bit duty to percent on [0, 100] */
+ percent = DIV_ROUND_NEAREST(p->duty * 100, EC_PWM_MAX_DUTY);
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);
+ pwm_set_duty(channel, percent);
+ pwm_enable(channel, percent > 0);
return EC_RES_SUCCESS;
}
@@ -70,7 +76,8 @@ static int host_command_pwm_get_duty(struct host_cmd_handler_args *args)
if (get_target_channel(&channel, p->pwm_type, p->index))
return EC_RES_INVALID_PARAM;
- r->percent = pwm_get_duty(channel);
+ /* Convert percent on [0, 100] to 16 bit duty */
+ r->duty = pwm_get_duty(channel) * EC_PWM_MAX_DUTY / 100;
args->response_size = sizeof(*r);
return EC_RES_SUCCESS;
diff --git a/include/ec_commands.h b/include/ec_commands.h
index 27049eac5a..871dbf9662 100644
--- a/include/ec_commands.h
+++ b/include/ec_commands.h
@@ -1207,6 +1207,8 @@ struct ec_params_pwm_set_fan_duty_v1 {
} __packed;
#define EC_CMD_PWM_SET_DUTY 0x25
+/* 16 bit duty cycle, 65535 = 100% */
+#define EC_PWM_MAX_DUTY 65535
enum ec_pwm_type {
/* All types, indexed by board-specific enum pwm_channel */
@@ -1219,7 +1221,7 @@ enum ec_pwm_type {
};
struct ec_params_pwm_set_duty {
- uint8_t percent; /* Duty cycle percent on [0, 100] */
+ uint16_t duty; /* Duty cycle, EC_PWM_MAX_DUTY = 100% */
uint8_t pwm_type; /* ec_pwm_type */
uint8_t index; /* Type-specific index, or 0 if unique */
} __packed;
@@ -1232,7 +1234,7 @@ struct ec_params_pwm_get_duty {
} __packed;
struct ec_response_pwm_get_duty {
- uint8_t percent;
+ uint16_t duty; /* Duty cycle, EC_PWM_MAX_DUTY = 100% */
} __packed;
/*****************************************************************************/
diff --git a/util/ectool.c b/util/ectool.c
index a1210466f5..364ec2f99f 100644
--- a/util/ectool.c
+++ b/util/ectool.c
@@ -176,13 +176,13 @@ const char help_str[] =
" pwmgetnumfans\n"
" Prints the number of fans present\n"
" pwmgetduty\n"
- " Prints the current duty cycle for given PWM\n"
+ " Prints the current 16 bit duty cycle for given PWM\n"
" pwmsetfanrpm <targetrpm>\n"
" Set target fan RPM\n"
" pwmsetkblight <percent>\n"
" Set keyboard backlight in percent\n"
" pwmsetduty\n"
- " Set duty cycle of given PWM in percent\n"
+ " Set 16 bit duty cycle of given PWM\n"
" readtest <patternoffset> <size>\n"
" Reads a pattern from the EC via LPC\n"
" reboot_ec <RO|RW|cold|hibernate|disable-jump> [at-shutdown]\n"
@@ -1941,7 +1941,7 @@ int cmd_pwm_get_duty(int argc, char *argv[])
if (rv < 0)
return rv;
- printf("Current PWM duty: %d\n", r.percent);
+ printf("Current PWM duty: %d\n", r.duty);
return 0;
}
@@ -1953,7 +1953,7 @@ int cmd_pwm_set_duty(int argc, char *argv[])
int rv;
if (argc != 3) {
- fprintf(stderr, "Usage: %s <pwm_idx> | kb | disp <percent>\n",
+ fprintf(stderr, "Usage: %s <pwm_idx> | kb | disp <duty>\n",
argv[0]);
return -1;
}
@@ -1973,9 +1973,9 @@ int cmd_pwm_set_duty(int argc, char *argv[])
}
}
- p.percent = strtol(argv[2], &e, 0);
+ p.duty = strtol(argv[2], &e, 0);
if (e && *e) {
- fprintf(stderr, "Bad percent.\n");
+ fprintf(stderr, "Bad duty.\n");
return -1;
}