summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVic Yang <victoryang@chromium.org>2013-04-02 15:52:31 +0800
committerVic Yang <victoryang@chromium.org>2013-04-08 02:25:40 -0700
commit61624c9da00a8a2c74786813c5219738393d970a (patch)
tree180c4b431d5ccbccb3d0892d270ced0b95f04626
parent744cada9d5a2edacf462c15febfc31fbf7f64a12 (diff)
downloadchrome-ec-61624c9da00a8a2c74786813c5219738393d970a.tar.gz
spring: Add host command to limit external power current
This is useful for debugging and the factory. BUG=chrome-os-partner:18530 TEST=On spring, check we can set PWM duty cycle and can go back to automatic control. BRANCH=spring Original-Change-Id: I3da75f0a356cc0f21d748bf135e3b95fbd9c465b Signed-off-by: Vic Yang <victoryang@chromium.org> Reviewed-on: https://gerrit.chromium.org/gerrit/47105 Reviewed-by: Vincent Palatin <vpalatin@chromium.org> (cherry picked from commit 13c74da5ade0b98c9c024bf72e0c78c5155a8d08) Change-Id: I15e5ff4158ddd96cae4fc52a21899493fbbfe9da Signed-off-by: Vic Yang <victoryang@chromium.org> Reviewed-on: https://gerrit.chromium.org/gerrit/47553
-rw-r--r--board/spring/usb_charging.c41
-rw-r--r--include/ec_commands.h11
-rw-r--r--util/ectool.c26
3 files changed, 74 insertions, 4 deletions
diff --git a/board/spring/usb_charging.c b/board/spring/usb_charging.c
index d7ba332047..5a853b5905 100644
--- a/board/spring/usb_charging.c
+++ b/board/spring/usb_charging.c
@@ -8,14 +8,16 @@
#include "adc.h"
#include "board.h"
#include "console.h"
-#include "hooks.h"
#include "gpio.h"
-#include "lp5562.h"
+#include "hooks.h"
+#include "host_command.h"
#include "keyboard_scan.h"
+#include "lp5562.h"
#include "pmu_tpschrome.h"
#include "registers.h"
#include "smart_battery.h"
#include "stm32_adc.h"
+#include "system.h"
#include "task.h"
#include "timer.h"
#include "tsu6721.h"
@@ -59,9 +61,17 @@
#define DELAY_USB_DP_DN_MS 20
#define DELAY_ID_MUX_MS 30
+/*
+ * Mapping from PWM duty to current:
+ * Current = A + B * PWM_Duty
+ */
+#define PWM_MAPPING_A 3012
+#define PWM_MAPPING_B (-29)
+
static int current_dev_type = TSU6721_TYPE_NONE;
static int nominal_pwm_duty;
static int current_pwm_duty;
+static int user_pwm_duty = -1;
static enum {
LIMIT_NORMAL,
@@ -262,6 +272,13 @@ static void board_pwm_tweak(void)
vbus = adc_read_channel(ADC_CH_USB_VBUS_SNS);
if (battery_current(&current))
return;
+
+ if (user_pwm_duty >= 0) {
+ if (current_pwm_duty != user_pwm_duty)
+ board_pwm_duty_cycle(user_pwm_duty);
+ return;
+ }
+
/*
* If VBUS voltage is too low:
* - If battery is discharging, throttling more is going to draw
@@ -463,7 +480,7 @@ int board_get_usb_dev_type(void)
int board_get_usb_current_limit(void)
{
/* Approximate value by PWM duty cycle */
- return 3012 - 29 * current_pwm_duty;
+ return PWM_MAPPING_A + PWM_MAPPING_B * current_pwm_duty;
}
/*
@@ -547,3 +564,21 @@ DECLARE_CONSOLE_COMMAND(limitmode, command_current_limit_mode,
"[normal | aggressive]",
"Set current limit mode",
NULL);
+
+/*****************************************************************************/
+/* Host commands */
+
+static int ext_power_command_current_limit(struct host_cmd_handler_args *args)
+{
+ const struct ec_params_ext_power_current_limit *p = args->params;
+
+ if (system_is_locked())
+ return EC_RES_ACCESS_DENIED;
+
+ user_pwm_duty = ((int)(p->limit) - PWM_MAPPING_A) / PWM_MAPPING_B;
+
+ return EC_SUCCESS;
+}
+DECLARE_HOST_COMMAND(EC_CMD_EXT_POWER_CURRENT_LIMIT,
+ ext_power_command_current_limit,
+ EC_VER_MASK(0));
diff --git a/include/ec_commands.h b/include/ec_commands.h
index cd714cc1db..84b5d5582a 100644
--- a/include/ec_commands.h
+++ b/include/ec_commands.h
@@ -1273,7 +1273,16 @@ struct ec_response_power_info {
#define EC_CMD_CHARGE_CURRENT_LIMIT 0xa1
struct ec_params_current_limit {
- uint32_t limit;
+ uint32_t limit; /* in mA */
+} __packed;
+
+/*
+ * Set maximum external power current.
+ */
+#define EC_CMD_EXT_POWER_CURRENT_LIMIT 0xa2
+
+struct ec_params_ext_power_current_limit {
+ uint32_t limit; /* in mA */
} __packed;
/*****************************************************************************/
diff --git a/util/ectool.c b/util/ectool.c
index 3804170b8d..ff40734529 100644
--- a/util/ectool.c
+++ b/util/ectool.c
@@ -28,6 +28,8 @@ static inline int MIN(int a, int b) { return a < b ? a : b; }
const char help_str[] =
"Commands:\n"
+ " extpwrcurrentlimit\n"
+ " Set the maximum external power current\n"
" autofanctrl <on>\n"
" Turn on automatic fan speed control.\n"
" backlight <enabled>\n"
@@ -2026,6 +2028,29 @@ int cmd_lcd_backlight(int argc, char *argv[])
}
+int cmd_ext_power_current_limit(int argc, char *argv[])
+{
+ struct ec_params_ext_power_current_limit p;
+ int rv;
+ char *e;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <max_current_mA>\n", argv[0]);
+ return -1;
+ }
+
+ p.limit = strtol(argv[1], &e, 0);
+ if (e && *e) {
+ fprintf(stderr, "Bad value.\n");
+ return -1;
+ }
+
+ rv = ec_command(EC_CMD_EXT_POWER_CURRENT_LIMIT, 0, &p, sizeof(p),
+ NULL, 0);
+ return rv;
+}
+
+
int cmd_charge_current_limit(int argc, char *argv[])
{
struct ec_params_current_limit p;
@@ -2732,6 +2757,7 @@ struct command {
/* NULL-terminated list of commands */
const struct command commands[] = {
+ {"extpwrcurrentlimit", cmd_ext_power_current_limit},
{"autofanctrl", cmd_thermal_auto_fan_ctrl},
{"backlight", cmd_lcd_backlight},
{"battery", cmd_battery},