summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDevin Lu <Devin.Lu@quantatw.com>2014-04-15 09:44:08 +0800
committerchrome-internal-fetch <chrome-internal-fetch@google.com>2014-04-17 13:23:50 +0000
commit4481150f012f70f98ef22f8fd9a5983d0dc09379 (patch)
tree814b17d3600f74cc1c0fc9bc317e039cd8f1c635
parentf4f7e3f42c265bb2d9dfbd2fe442f149d84bd6b0 (diff)
downloadchrome-ec-4481150f012f70f98ef22f8fd9a5983d0dc09379.tar.gz
Kip: identify adapters and adjust the charger input current
identify adapters and adjust the charger limit for each adapter. re-factored for adapter over rating. re-factored for battery over current. BUG=chrome-os-partner:27798 BRANCH=rambi TEST=manual build ec and flash to kip board. verify adapters can be identified. check AP can be throttle when battery over current. check AP can be throttle when adapter over rating. Change-Id: I1339f55d8ed523acf9543ee8acd7c7e3c9c99e9c Signed-off-by: Devin Lu <Devin.Lu@quantatw.com> Reviewed-by: Dave Parker <dparker@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/194891
-rw-r--r--board/kip/board.c4
-rw-r--r--board/kip/board.h4
-rw-r--r--common/build.mk1
-rw-r--r--common/charge_state.c5
-rw-r--r--common/extpower_kip.c259
-rw-r--r--include/extpower.h5
-rw-r--r--include/extpower_kip.h45
7 files changed, 323 insertions, 0 deletions
diff --git a/board/kip/board.c b/board/kip/board.c
index 3be8adf0ad..2b4de748fd 100644
--- a/board/kip/board.c
+++ b/board/kip/board.c
@@ -165,6 +165,10 @@ const struct adc_t adc_channels[] = {
*/
{"ChargerCurrent", LM4_ADC_SEQ1, 33000, ADC_READ_MAX * 4, 0,
LM4_AIN(0), 0x06 /* IE0 | END0 */, LM4_GPIO_E, (1<<3)},
+
+ /* AC Adapter ID voltage (mv) */
+ {"AdapterIDVoltage", LM4_ADC_SEQ2, 3300, ADC_READ_MAX, 0,
+ LM4_AIN(10), 0x06 /* IE0 | END0 */, LM4_GPIO_B, (1<<4)},
};
BUILD_ASSERT(ARRAY_SIZE(adc_channels) == ADC_CH_COUNT);
diff --git a/board/kip/board.h b/board/kip/board.h
index 3541b6cc5a..d192d7a289 100644
--- a/board/kip/board.h
+++ b/board/kip/board.h
@@ -23,6 +23,7 @@
#define CONFIG_CHIPSET_CAN_THROTTLE
#define CONFIG_CHIPSET_X86
#define CONFIG_CMD_GSV
+#define CONFIG_EXTPOWER_KIP
#define CONFIG_EXTPOWER_GPIO
#define CONFIG_I2C_PASSTHRU_RESTRICTED
#define CONFIG_KEYBOARD_COL2_INVERTED
@@ -162,6 +163,9 @@ enum adc_channel {
/* Charger current in mA. */
ADC_CH_CHARGER_CURRENT,
+ /* AC Adapter ID voltage in mV */
+ ADC_AC_ADAPTER_ID_VOLTAGE,
+
ADC_CH_COUNT
};
diff --git a/common/build.mk b/common/build.mk
index e4695804b7..62f6750933 100644
--- a/common/build.mk
+++ b/common/build.mk
@@ -33,6 +33,7 @@ common-$(CONFIG_PMU_POWERINFO)+=pmu_tps65090_powerinfo.o
common-$(CONFIG_PMU_TPS65090)+=pmu_tps65090.o
common-$(CONFIG_EOPTION)+=eoption.o
common-$(CONFIG_EXTPOWER_FALCO)+=extpower_falco.o
+common-$(CONFIG_EXTPOWER_KIP)+=extpower_kip.o
common-$(CONFIG_EXTPOWER_GPIO)+=extpower_gpio.o
common-$(CONFIG_EXTPOWER_SNOW)+=extpower_snow.o
common-$(CONFIG_EXTPOWER_SPRING)+=extpower_spring.o
diff --git a/common/charge_state.c b/common/charge_state.c
index 7711dfebb0..7cfb6d847f 100644
--- a/common/charge_state.c
+++ b/common/charge_state.c
@@ -851,6 +851,11 @@ void charger_task(void)
sleep_usec = EXTPOWER_FALCO_POLL_PERIOD;
#endif
+#ifdef CONFIG_EXTPOWER_KIP
+ watch_adapter_closely(ctx);
+ sleep_usec = EXTPOWER_KIP_POLL_PERIOD;
+#endif
+
/* Show charging progress in console */
charging_progress(ctx);
diff --git a/common/extpower_kip.c b/common/extpower_kip.c
new file mode 100644
index 0000000000..5da5ea2e35
--- /dev/null
+++ b/common/extpower_kip.c
@@ -0,0 +1,259 @@
+/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/*
+ * The limits vary depending on each adapter's power rating, so we
+ * need to watch for changes and adjust the limits and high-current thresholds
+ * accordingly. If we go over, the AP needs to throttle itself. The EC's
+ * charging state logic isn't affected, just the AP's P-State. We try to save
+ * PROCHOT as a last resort.
+ */
+
+#include <limits.h> /* part of the compiler */
+
+#include "adc.h"
+#include "battery_smart.h"
+#include "charge_state.h"
+#include "charger.h"
+#include "chipset.h"
+#include "common.h"
+#include "console.h"
+#include "driver/charger/bq24715.h"
+#include "extpower.h"
+#include "extpower_kip.h"
+#include "hooks.h"
+#include "host_command.h"
+#include "throttle_ap.h"
+#include "util.h"
+
+/* Console output macros */
+#define CPUTS(outstr) cputs(CC_CHARGER, outstr)
+#define CPRINTF(format, args...) cprintf(CC_CHARGER, format, ## args)
+
+/* Values for our supported adapters */
+static const char * const ad_name[] = {
+ "unknown",
+ "45W",
+ "65W",
+ "90W"
+};
+BUILD_ASSERT(ARRAY_SIZE(ad_name) == NUM_ADAPTER_TYPES);
+
+test_export_static
+struct adapter_id_vals ad_id_vals[] = {
+ /* mV low, mV high */
+ {INT_MIN, INT_MAX}, /* anything = ADAPTER_UNKNOWN */
+ {434, 554}, /* ADAPTER_45W */
+ {561, 717}, /* ADAPTER_65W */
+ {725, 925} /* ADAPTER_90W */
+};
+BUILD_ASSERT(ARRAY_SIZE(ad_id_vals) == NUM_ADAPTER_TYPES);
+
+test_export_static
+int ad_input_current[] = {
+ /*
+ * Current limits in mA for each adapter.
+ * Values are in hex to avoid roundoff, because the BQ24715 Input
+ * Current Register masks off bits 6-0.
+ *
+ * Note that this is very specific to the combinations of adapters and
+ * BQ24715 charger chip on Kip.
+ */
+
+ 0x0800, /* ADAPTER_UNKNOWN ~ 2.0 A */
+ 0x0800, /* ADAPTER_45W ~ 2.0 A */
+ 0x0c00, /* ADAPTER_65W ~ 3.0 A */
+ 0x1100 /* ADAPTER_90W ~ 4.3 A */
+};
+BUILD_ASSERT(ARRAY_SIZE(ad_input_current) == NUM_ADAPTER_TYPES);
+
+test_export_static
+struct adapter_limits ad_limits[][NUM_AC_THRESHOLDS] = {
+ /* ADAPTER_UNKNOWN - treat as 45W */
+ {
+ { 2310, 1960, 16, 80, },
+ { 2560, 2210, 1, 80, }
+ },
+ /* ADAPTER_45W */
+ {
+ { 2310, 1960, 16, 80, },
+ { 2560, 2210, 1, 80, }
+ },
+ /* ADAPTER_65W */
+ {
+ { 3330, 2980, 16, 80, },
+ { 3590, 3240, 1, 80, }
+ },
+ /* ADAPTER_90W */
+ {
+ { 4620, 4270, 16, 80, },
+ { 4870, 4520, 1, 80, }
+ }
+};
+BUILD_ASSERT(ARRAY_SIZE(ad_limits) == NUM_ADAPTER_TYPES);
+
+/* The battery current limits are independent of adapter rating.
+ * hi_val and lo_val are DISCHARGE current in mA.
+ */
+test_export_static
+struct adapter_limits batt_limits[] = {
+ { 5500, 5000, 16, 50, },
+ { 6000, 5500, 1, 50, },
+};
+BUILD_ASSERT(ARRAY_SIZE(batt_limits) == NUM_BATT_THRESHOLDS);
+
+static int last_mv;
+static enum adapter_type identify_adapter(void)
+{
+ int i;
+ last_mv = adc_read_channel(ADC_AC_ADAPTER_ID_VOLTAGE);
+
+ /* ADAPTER_UNKNOWN matches everything, so search backwards */
+ for (i = NUM_ADAPTER_TYPES - 1; i >= 0; i--)
+ if (last_mv >= ad_id_vals[i].lo && last_mv <= ad_id_vals[i].hi)
+ return i;
+
+ return ADAPTER_UNKNOWN; /* should never get here */
+}
+
+test_export_static enum adapter_type ac_adapter;
+static void ac_change_callback(void)
+{
+ if (extpower_is_present()) {
+ ac_adapter = identify_adapter();
+ CPRINTF("[%T AC Adapter is %s (%dmv)]\n",
+ ad_name[ac_adapter], last_mv);
+ } else {
+ ac_adapter = ADAPTER_UNKNOWN;
+ CPRINTF("[%T AC Adapter is not present]\n");
+ /* Charger unavailable. Clear local flags */
+ }
+}
+DECLARE_HOOK(HOOK_AC_CHANGE, ac_change_callback, HOOK_PRIO_DEFAULT);
+
+static void set_ad_input_current(void)
+{
+ int r;
+
+ /* Set allowed Io based on adapter. The charger will sometimes change
+ * this setting all by itself due to inrush current limiting, so we
+ * can't assume it stays where we put it. */
+ r = charger_set_input_current(ad_input_current[ac_adapter]);
+ if (r != EC_SUCCESS)
+ goto bad;
+
+ return;
+bad:
+ CPRINTF("[%T ERROR: can't talk to charger: %d]\n", r);
+}
+
+
+/* We need to OR all the possible reasons to throttle in order to decide
+ * whether it should happen or not. Use one bit per reason.
+ */
+#define BATT_REASON_OFFSET 0
+#define AC_REASON_OFFSET NUM_BATT_THRESHOLDS
+BUILD_ASSERT(NUM_BATT_THRESHOLDS + NUM_AC_THRESHOLDS < 32);
+
+test_export_static uint32_t ap_is_throttled;
+static void set_throttle(int on, int whosays)
+{
+ if (on)
+ ap_is_throttled |= (1 << whosays);
+ else
+ ap_is_throttled &= ~(1 << whosays);
+
+ throttle_ap(ap_is_throttled ? THROTTLE_ON : THROTTLE_OFF,
+ THROTTLE_SOFT, THROTTLE_SRC_POWER);
+}
+
+test_export_static
+void check_threshold(int current, struct adapter_limits *lim, int whoami)
+{
+ if (lim->triggered) {
+ /* watching for current to drop */
+ if (current < lim->lo_val) {
+ if (++lim->count >= lim->lo_cnt) {
+ set_throttle(0, whoami);
+ lim->count = 0;
+ lim->triggered = 0;
+ }
+ } else {
+ lim->count = 0;
+ }
+ } else {
+ /* watching for current to rise */
+ if (current > lim->hi_val) {
+ if (++lim->count >= lim->hi_cnt) {
+ set_throttle(1, whoami);
+ lim->count = 0;
+ lim->triggered = 1;
+ }
+ } else {
+ lim->count = 0;
+ }
+ }
+}
+
+
+test_export_static
+void watch_battery_closely(struct charge_state_context *ctx)
+{
+ int i;
+ int current = ctx->curr.batt.current;
+
+ /* NB: The values in batt_limits[] indicate DISCHARGE current (mA).
+ * However, the value returned from battery_current() is CHARGE
+ * current: postive for charging and negative for discharging.
+ *
+ * Turbo mode can discharge the battery even while connected to the
+ * charger. The spec says not to turn throttling off until the battery
+ * drain has been below the threshold for 5 seconds. That means we
+ * still need to check while on AC, or else just plugging the adapter
+ * in and out would mess up that 5-second timeout. Since the threshold
+ * logic uses signed numbers to compare the limits, everything Just
+ * Works.
+ */
+
+ /* Check limits against DISCHARGE current, not CHARGE current! */
+ for (i = 0; i < NUM_BATT_THRESHOLDS; i++)
+ check_threshold(-current, &batt_limits[i], /* invert sign! */
+ i + BATT_REASON_OFFSET);
+}
+
+void watch_adapter_closely(struct charge_state_context *ctx)
+{
+ int current, i;
+
+ /* We always watch the battery current drain, even when on AC. */
+ watch_battery_closely(ctx);
+
+ /* If AC present, we can set adpter input current for each adpater. */
+ if (extpower_is_present())
+ set_ad_input_current();
+
+ /* If the AP is off, we won't need to throttle it. */
+ if (chipset_in_state(CHIPSET_STATE_ANY_OFF |
+ CHIPSET_STATE_SUSPEND))
+ return;
+
+ /* Check all the thresholds. */
+ current = adc_read_channel(ADC_CH_CHARGER_CURRENT);
+ for (i = 0; i < NUM_AC_THRESHOLDS; i++)
+ check_threshold(current, &ad_limits[ac_adapter][i],
+ i + AC_REASON_OFFSET);
+}
+
+static int command_adapter(int argc, char **argv)
+{
+ enum adapter_type v = identify_adapter();
+ ccprintf("Adapter %s (%dmv), ap_is_throttled 0x%08x\n",
+ ad_name[v], last_mv, ap_is_throttled);
+ return EC_SUCCESS;
+}
+DECLARE_CONSOLE_COMMAND(adapter, command_adapter,
+ NULL,
+ "Display AC adapter information",
+ NULL);
diff --git a/include/extpower.h b/include/extpower.h
index f75a1a5c06..05346a9405 100644
--- a/include/extpower.h
+++ b/include/extpower.h
@@ -32,4 +32,9 @@ void extpower_interrupt(enum gpio_signal signal);
#include "extpower_falco.h"
#endif
+#ifdef CONFIG_EXTPOWER_KIP
+/* Adapter-specific logic */
+#include "extpower_kip.h"
+#endif
+
#endif /* __CROS_EC_EXTPOWER_H */
diff --git a/include/extpower_kip.h b/include/extpower_kip.h
new file mode 100644
index 0000000000..faa7a26b26
--- /dev/null
+++ b/include/extpower_kip.h
@@ -0,0 +1,45 @@
+/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/* We can do even smarter charging if we can identify the AC adapter */
+
+#ifndef __CROS_EC_EXTPOWER_KIP_H
+#define __CROS_EC_EXTPOWER_KIP_H
+
+#include "charge_state.h"
+
+/* Supported adapters */
+enum adapter_type {
+ ADAPTER_UNKNOWN = 0,
+ ADAPTER_45W,
+ ADAPTER_65W,
+ ADAPTER_90W,
+ NUM_ADAPTER_TYPES
+};
+
+/* Adapter identification values */
+struct adapter_id_vals {
+ int lo, hi;
+};
+
+/* Adapter-specific parameters. */
+struct adapter_limits {
+ int hi_val, lo_val; /* current thresholds (mA) */
+ int hi_cnt, lo_cnt; /* count needed to trigger */
+ int count; /* samples past the limit */
+ int triggered; /* threshold reached */
+};
+
+/* Rate at which adapter samples are collected. */
+#define EXTPOWER_KIP_POLL_PERIOD (MSEC * 100)
+
+/* Number of special states */
+#define NUM_AC_THRESHOLDS 2
+#define NUM_BATT_THRESHOLDS 2
+
+/* Change turbo mode or throttle the AP depending on the adapter state. */
+void watch_adapter_closely(struct charge_state_context *ctx);
+
+#endif /* __CROS_EC_EXTPOWER_KIP_H */