summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAseda Aboagye <aaboagye@google.com>2021-03-10 18:59:35 -0800
committerCommit Bot <commit-bot@chromium.org>2021-03-11 20:11:44 +0000
commit7ad578bfb93e995b59841417f98dc693fa81e9a3 (patch)
tree1945f29c920ee0680e8c8cd9853be162c2f2266c
parente8cb700579e12ddb0c0f2cda784f04adb6de40f8 (diff)
downloadchrome-ec-7ad578bfb93e995b59841417f98dc693fa81e9a3.tar.gz
charger: Add new APIs for measured charge values
Some of the charger ICs can provide measurements for the charge voltage and the charge current. This information is needed by the OCPC module. Previously, the charge_get_current() and charge_get_voltage() functions were modified to provide this information. However, those functions are intended to provide the set voltage and current targets for the charger IC. This commit adds a new set of APIs, charge_get_actual_current() and charge_get_actual_voltage() which provides the actual charge current and voltage if the charger IC is able to provide that information. BUG=b:182018616 BRANCH=dedede TEST=Build and flash madoo, verify that `charger` EC console command shows the set current and voltage targets instead of the measured values. Check that the `chgstate` command shows the measured values for use with the OCPC module. TEST=Verify that charging from the sub board works. TEST=Verify that resistances are still calculated and seem reasonable. Signed-off-by: Aseda Aboagye <aaboagye@google.com> Change-Id: I82565d18908d9ea0f54934787897937488e280e6 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2750866 Reviewed-by: Diana Z <dzigterman@chromium.org> Commit-Queue: Aseda Aboagye <aaboagye@chromium.org> Tested-by: Aseda Aboagye <aaboagye@chromium.org>
-rw-r--r--common/charger.c33
-rw-r--r--common/ocpc.c6
-rw-r--r--driver/charger/isl923x.c59
-rw-r--r--driver/charger/sm5803.c40
-rw-r--r--include/charger.h10
5 files changed, 123 insertions, 25 deletions
diff --git a/common/charger.c b/common/charger.c
index f9fd1c4640..e3d77976ae 100644
--- a/common/charger.c
+++ b/common/charger.c
@@ -359,6 +359,23 @@ int charger_is_sourcing_otg_power(int port)
return chg_chips[chgnum].drv->is_sourcing_otg_power(chgnum, port);
}
+enum ec_error_list charger_get_actual_current(int chgnum, int *current)
+{
+ /* Note: chgnum may be -1 if no active port is selected */
+ if (chgnum < 0)
+ return EC_ERROR_INVAL;
+
+ if (chgnum >= board_get_charger_chip_count()) {
+ CPRINTS("%s(%d) Invalid charger!", __func__, chgnum);
+ return EC_ERROR_INVAL;
+ }
+
+ if (!chg_chips[chgnum].drv->get_actual_current)
+ return EC_ERROR_UNIMPLEMENTED;
+
+ return chg_chips[chgnum].drv->get_actual_current(chgnum, current);
+}
+
enum ec_error_list charger_get_current(int chgnum, int *current)
{
/* Note: chgnum may be -1 if no active port is selected */
@@ -389,6 +406,22 @@ enum ec_error_list charger_set_current(int chgnum, int current)
return chg_chips[chgnum].drv->set_current(chgnum, current);
}
+enum ec_error_list charger_get_actual_voltage(int chgnum, int *voltage)
+{
+ if (chgnum < 0)
+ return EC_ERROR_INVAL;
+
+ if (chgnum >= board_get_charger_chip_count()) {
+ CPRINTS("%s(%d) Invalid charger!", __func__, chgnum);
+ return EC_ERROR_INVAL;
+ }
+
+ if (!chg_chips[chgnum].drv->get_actual_voltage)
+ return EC_ERROR_UNIMPLEMENTED;
+
+ return chg_chips[chgnum].drv->get_actual_voltage(chgnum, voltage);
+}
+
enum ec_error_list charger_get_voltage(int chgnum, int *voltage)
{
if (chgnum < 0)
diff --git a/common/ocpc.c b/common/ocpc.c
index ab7f8e74a3..cf0d3adedb 100644
--- a/common/ocpc.c
+++ b/common/ocpc.c
@@ -606,7 +606,7 @@ void ocpc_get_adcs(struct ocpc_data *ocpc)
ocpc->primary_ibus_ma = val;
val = 0;
- if (!charger_get_voltage(CHARGER_PRIMARY, &val))
+ if (!charger_get_actual_voltage(CHARGER_PRIMARY, &val))
ocpc->vsys_mv = val;
if (board_get_charger_chip_count() <= CHARGER_SECONDARY) {
@@ -626,11 +626,11 @@ void ocpc_get_adcs(struct ocpc_data *ocpc)
ocpc->secondary_ibus_ma = val;
val = 0;
- if (!charger_get_voltage(CHARGER_SECONDARY, &val))
+ if (!charger_get_actual_voltage(CHARGER_SECONDARY, &val))
ocpc->vsys_aux_mv = val;
val = 0;
- if (!charger_get_current(CHARGER_SECONDARY, &val))
+ if (!charger_get_actual_current(CHARGER_SECONDARY, &val))
ocpc->isys_ma = val;
}
diff --git a/driver/charger/isl923x.c b/driver/charger/isl923x.c
index 9cd6a2cbab..7d2b894f96 100644
--- a/driver/charger/isl923x.c
+++ b/driver/charger/isl923x.c
@@ -409,19 +409,28 @@ static enum ec_error_list isl923x_set_mode(int chgnum, int mode)
return rv;
}
+#ifdef CONFIG_CHARGER_RAA489000
+static enum ec_error_list raa489000_get_actual_current(int chgnum, int *current)
+{
+ int rv;
+ int reg;
+
+ rv = raw_read16(chgnum, RAA489000_REG_ADC_CHARGE_CURRENT, &reg);
+ /* The value is in 22.2mA increments. */
+ reg *= 222;
+ reg /= 10;
+
+ *current = REG_TO_CURRENT(reg);
+ return rv;
+}
+#endif /* CONFIG_CHARGER_RAA489000 */
+
static enum ec_error_list isl923x_get_current(int chgnum, int *current)
{
int rv;
int reg;
- if (IS_ENABLED(CONFIG_CHARGER_RAA489000)) {
- rv = raw_read16(chgnum, RAA489000_REG_ADC_CHARGE_CURRENT, &reg);
- /* The value is in 22.2mA increments. */
- reg *= 222;
- reg /= 10;
- } else {
- rv = raw_read16(chgnum, ISL923X_REG_CHG_CURRENT, &reg);
- }
+ rv = raw_read16(chgnum, ISL923X_REG_CHG_CURRENT, &reg);
if (rv)
return rv;
@@ -434,24 +443,28 @@ static enum ec_error_list isl923x_set_current(int chgnum, int current)
return isl9237_set_current(chgnum, current);
}
-static enum ec_error_list isl923x_get_voltage(int chgnum, int *voltage)
+#ifdef CONFIG_CHARGER_RAA489000
+static enum ec_error_list raa489000_get_actual_voltage(int chgnum, int *voltage)
{
int rv;
int reg;
- if (IS_ENABLED(CONFIG_CHARGER_RAA489000)) {
- rv = raw_read16(chgnum, RAA489000_REG_ADC_VSYS, &reg);
- if (rv)
- return rv;
+ rv = raw_read16(chgnum, RAA489000_REG_ADC_VSYS, &reg);
+ if (rv)
+ return rv;
- /* The voltage is returned in bits 13:6. LSB is 96mV. */
- reg &= GENMASK(13, 6);
- reg >>= 6;
- reg *= 96;
+ /* The voltage is returned in bits 13:6. LSB is 96mV. */
+ reg &= GENMASK(13, 6);
+ reg >>= 6;
+ reg *= 96;
- *voltage = reg;
- return EC_SUCCESS;
- }
+ *voltage = reg;
+ return EC_SUCCESS;
+}
+#endif /* CONFIG_CHARGER_RAA489000 */
+
+static enum ec_error_list isl923x_get_voltage(int chgnum, int *voltage)
+{
return raw_read16(chgnum, ISL923X_REG_SYS_VOLTAGE_MAX, voltage);
}
@@ -1376,8 +1389,14 @@ const struct charger_drv isl923x_drv = {
.enable_otg_power = &isl923x_enable_otg_power,
.set_otg_current_voltage = &isl923x_set_otg_current_voltage,
#endif
+#ifdef CONFIG_CHARGER_RAA489000
+ .get_actual_current = &raa489000_get_actual_current,
+#endif
.get_current = &isl923x_get_current,
.set_current = &isl923x_set_current,
+#ifdef CONFIG_CHARGER_RAA489000
+ .get_actual_voltage = &raa489000_get_actual_voltage,
+#endif
.get_voltage = &isl923x_get_voltage,
.set_voltage = &isl923x_set_voltage,
.discharge_on_ac = &isl923x_discharge_on_ac,
diff --git a/driver/charger/sm5803.c b/driver/charger/sm5803.c
index 7997dc09d8..7c635ddb93 100644
--- a/driver/charger/sm5803.c
+++ b/driver/charger/sm5803.c
@@ -1212,7 +1212,7 @@ static enum ec_error_list sm5803_set_mode(int chgnum, int mode)
return rv;
}
-static enum ec_error_list sm5803_get_current(int chgnum, int *current)
+static enum ec_error_list sm5803_get_actual_current(int chgnum, int *current)
{
enum ec_error_list rv;
int reg;
@@ -1233,6 +1233,21 @@ static enum ec_error_list sm5803_get_current(int chgnum, int *current)
return EC_SUCCESS;
}
+static enum ec_error_list sm5803_get_current(int chgnum, int *current)
+{
+ enum ec_error_list rv;
+ int reg;
+
+ rv = chg_read8(chgnum, SM5803_REG_FAST_CONF4, &reg);
+ if (rv)
+ return rv;
+
+ reg &= SM5803_CONF4_ICHG_FAST;
+ *current = SM5803_REG_TO_CURRENT(reg);
+
+ return EC_SUCCESS;
+}
+
static enum ec_error_list sm5803_set_current(int chgnum, int current)
{
enum ec_error_list rv;
@@ -1249,7 +1264,7 @@ static enum ec_error_list sm5803_set_current(int chgnum, int current)
return rv;
}
-static enum ec_error_list sm5803_get_voltage(int chgnum, int *voltage)
+static enum ec_error_list sm5803_get_actual_voltage(int chgnum, int *voltage)
{
enum ec_error_list rv;
int reg;
@@ -1271,6 +1286,25 @@ static enum ec_error_list sm5803_get_voltage(int chgnum, int *voltage)
return EC_SUCCESS;
}
+static enum ec_error_list sm5803_get_voltage(int chgnum, int *voltage)
+{
+ enum ec_error_list rv;
+ int regval;
+ int v;
+
+ rv = chg_read8(chgnum, SM5803_REG_VBAT_FAST_MSB, &regval);
+ v = regval << 3;
+ rv |= chg_read8(chgnum, SM5803_REG_VBAT_FAST_LSB, &regval);
+ v |= (regval & 0x3);
+
+ *voltage = SM5803_REG_TO_VOLTAGE(v);
+
+ if (rv)
+ return EC_ERROR_UNKNOWN;
+
+ return EC_SUCCESS;
+}
+
static enum ec_error_list sm5803_set_voltage(int chgnum, int voltage)
{
enum ec_error_list rv;
@@ -1716,8 +1750,10 @@ const struct charger_drv sm5803_drv = {
.get_info = &sm5803_get_info,
.get_status = &sm5803_get_status,
.set_mode = &sm5803_set_mode,
+ .get_actual_current = &sm5803_get_actual_current,
.get_current = &sm5803_get_current,
.set_current = &sm5803_set_current,
+ .get_actual_voltage = &sm5803_get_actual_voltage,
.get_voltage = &sm5803_get_voltage,
.set_voltage = &sm5803_set_voltage,
.discharge_on_ac = &sm5803_discharge_on_ac,
diff --git a/include/charger.h b/include/charger.h
index d8d62c1e9b..402f9eca73 100644
--- a/include/charger.h
+++ b/include/charger.h
@@ -85,6 +85,12 @@ struct charger_drv {
enum ec_error_list (*get_voltage)(int chgnum, int *voltage);
enum ec_error_list (*set_voltage)(int chgnum, int voltage);
+
+ /* Get the measured charge current and voltage in mA/mV */
+ enum ec_error_list (*get_actual_current)(int chgnum, int *current);
+ enum ec_error_list (*get_actual_voltage)(int chgnum, int *voltage);
+
+
/* Discharge battery when on AC power. */
enum ec_error_list (*discharge_on_ac)(int chgnum, int enable);
@@ -250,6 +256,10 @@ enum ec_error_list charger_set_current(int chgnum, int current);
enum ec_error_list charger_get_voltage(int chgnum, int *voltage);
enum ec_error_list charger_set_voltage(int chgnum, int voltage);
+/* Get the measured charge current and voltage in mA/mV */
+enum ec_error_list charger_get_actual_current(int chgnum, int *current);
+enum ec_error_list charger_get_actual_voltage(int chgnum, int *voltage);
+
/* Discharge battery when on AC power. */
enum ec_error_list charger_discharge_on_ac(int enable);