summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--board/reef/battery.c9
-rw-r--r--driver/charger/bd9995x.c11
-rw-r--r--include/common.h6
3 files changed, 18 insertions, 8 deletions
diff --git a/board/reef/battery.c b/board/reef/battery.c
index 602834257a..a76bd92d1d 100644
--- a/board/reef/battery.c
+++ b/board/reef/battery.c
@@ -11,6 +11,7 @@
#include "charge_ramp.h"
#include "charge_state.h"
#include "charger_profile_override.h"
+#include "common.h"
#include "console.h"
#include "ec_commands.h"
#include "extpower.h"
@@ -118,7 +119,7 @@ static const struct fast_charge_params fast_chg_params_smp_cos4870 = {
};
const struct battery_info batt_info_smp_cos4870 = {
- .voltage_max = 8700, /* mV */
+ .voltage_max = TARGET_WITH_MARGIN(8700, 5),
.voltage_normal = 7600,
/*
* Actual value 6000mV, added 100mV for charger accuracy so that
@@ -162,7 +163,7 @@ static const struct fast_charge_params fast_chg_params_sonycorp = {
};
const struct battery_info batt_info_sonycorp = {
- .voltage_max = 8700, /* mV */
+ .voltage_max = TARGET_WITH_MARGIN(8700, 5),
.voltage_normal = 7600,
/*
@@ -216,7 +217,7 @@ static const struct fast_charge_params fast_chg_params_panasonic = {
};
const struct battery_info batt_info_panasoic = {
- .voltage_max = 8800, /* mV */
+ .voltage_max = TARGET_WITH_MARGIN(8800, 5),
.voltage_normal = 7700,
/*
@@ -324,7 +325,7 @@ static const struct fast_charge_params fast_chg_params_cpt_c22n1626 = {
};
const struct battery_info batt_info_c22n1626 = {
- .voltage_max = 8800, /* mV */
+ .voltage_max = TARGET_WITH_MARGIN(8800, 5),
.voltage_normal = 7700,
/*
diff --git a/driver/charger/bd9995x.c b/driver/charger/bd9995x.c
index 607d0b81f6..c668aa5260 100644
--- a/driver/charger/bd9995x.c
+++ b/driver/charger/bd9995x.c
@@ -607,6 +607,7 @@ int charger_get_voltage(int *voltage)
int charger_set_voltage(int voltage)
{
+ const int battery_voltage_max = battery_get_info()->voltage_max;
int rv;
int reg;
@@ -620,14 +621,16 @@ int charger_set_voltage(int voltage)
return rv;
if (voltage == 0 ||
- reg & BD9995X_CMD_CHGOP_SET2_BATT_LEARN ||
- battery_is_present() != BP_YES ||
- battery_is_cut_off())
- voltage = battery_get_info()->voltage_max;
+ reg & BD9995X_CMD_CHGOP_SET2_BATT_LEARN ||
+ battery_is_present() != BP_YES ||
+ battery_is_cut_off() ||
+ voltage > battery_voltage_max)
+ voltage = battery_voltage_max;
/* Charge voltage step 16 mV */
voltage &= ~0x0F;
+ /* Assumes charger's voltage_min < battery's voltagte_max */
if (voltage < bd9995x_charger_info.voltage_min)
voltage = bd9995x_charger_info.voltage_min;
diff --git a/include/common.h b/include/common.h
index 0bdea8965d..f35d543ce8 100644
--- a/include/common.h
+++ b/include/common.h
@@ -84,6 +84,12 @@
#define CELSIUS_TO_DECI_KELVIN(temp_c) ((temp_c) * 10 + 2731)
#define DECI_KELVIN_TO_CELSIUS(temp_dk) ((temp_dk - 2731) / 10)
+/* Calculate a value with error margin considered. For example,
+ * TARGET_WITH_MARGIN(X, 5) returns X' where X' * 100.5% is almost equal to
+ * but does not exceed X. */
+#define TARGET_WITH_MARGIN(target, tenths_percent) \
+ (((target) * 1000) / (1000 + (tenths_percent)))
+
/* Include top-level configuration file */
#include "config.h"