From 76019bdb61e8211d17d590046520e79d3d4d1b90 Mon Sep 17 00:00:00 2001 From: Tim Crawford Date: Thu, 6 Oct 2022 13:02:15 -0600 Subject: [PATCH] Fix charger values, again (#303) * common: Add macros for min, max, clamp Simple macros, with no type checking. Signed-off-by: Tim Crawford * charger: Limit charger values to max valid value Use the maximum valid value instead of discarding bits. Fixes: 6295f601720b ("Fix smart charger values") Signed-off-by: Tim Crawford * charger/bq24780s: Fix charge current mask bq24780s uses bit 12 for 4096 a current weight. Fixes: 6295f601720b ("Fix smart charger values") Signed-off-by: Tim Crawford * oryp: Reduce charge current to 2A Signed-off-by: Tim Crawford * charger/bq24780s: Set RSENSE ratio option Signed-off-by: Tim Crawford Signed-off-by: Tim Crawford --- src/board/system76/common/charger/bq24780s.c | 71 +++++++++++++++----- src/board/system76/common/charger/oz26786.c | 14 ++-- src/board/system76/oryp5/board.mk | 2 +- src/board/system76/oryp6/board.mk | 2 +- src/board/system76/oryp7/board.mk | 2 +- src/board/system76/oryp8/board.mk | 2 +- src/common/include/common/macro.h | 4 ++ 7 files changed, 71 insertions(+), 26 deletions(-) diff --git a/src/board/system76/common/charger/bq24780s.c b/src/board/system76/common/charger/bq24780s.c index 29a4e2d3c..5ccf562c5 100644 --- a/src/board/system76/common/charger/bq24780s.c +++ b/src/board/system76/common/charger/bq24780s.c @@ -13,6 +13,10 @@ #define REG_CHARGE_VOLTAGE 0x15 #define REG_DISCHARGE_CURRENT 0x39 #define REG_INPUT_CURRENT 0x3F +#define REG_CHARGE_OPTION_0 0x12 +#define REG_CHARGE_OPTION_1 0x3B +#define REG_CHARGE_OPTION_2 0x38 +#define REG_CHARGE_OPTION_3 0x37 // ChargeOption0 flags // Low Power Mode Enable @@ -24,36 +28,63 @@ // IDCHG Amplifier Gain #define SBC_IDCHC_GAIN ((uint16_t)(BIT(3))) +// ChargeOption1 flags +// Independent Comparator Deglitch +#define SBC_CMP_DEG_1US (0b01 << 4) +// PMON Gain +#define SBC_PMON_RATIO BIT(9) +// Adapter:Battery RSENSE ratio +#define SBC_RSENSE_RATIO_1_1 (0b00 << 12) +#define SBC_RSENSE_RATIO_2_1 (0b01 << 12) +#define SBC_RSENSE_RATIO_1_2 (0b10 << 12) +// Battery depletion threshold +#define SBC_BAT_DEPL_VTH (0b11 << 14) + + // Bits 0-5 are ignored. Bits 13-15 must be 0. #define CHARGE_CURRENT_MASK 0x1FC0 #if CHARGER_BATTERY_RSENSE == 5 - #define CHARGE_CURRENT ((CHARGER_CHARGE_CURRENT >> 1) & CHARGE_CURRENT_MASK) + #define CHARGE_CURRENT (MIN((CHARGER_CHARGE_CURRENT >> 1), CHARGE_CURRENT_MASK) & CHARGE_CURRENT_MASK) #elif CHARGER_BATTERY_RSENSE == 10 - #define CHARGE_CURRENT (CHARGER_CHARGE_CURRENT & CHARGE_CURRENT_MASK) + #define CHARGE_CURRENT (MIN(CHARGER_CHARGE_CURRENT, CHARGE_CURRENT_MASK) & CHARGE_CURRENT_MASK) #elif CHARGER_BATTERY_RSENSE == 20 - #define CHARGE_CURRENT ((CHARGER_CHARGE_CURRENT << 1) & CHARGE_CURRENT_MASK) + #define CHARGE_CURRENT (MIN((CHARGER_CHARGE_CURRENT << 1), CHARGE_CURRENT_MASK) & CHARGE_CURRENT_MASK) #else #error Invalid battery RSENSE value #endif // Bits 0-3, 15 are ignored. #define CHARGE_VOLTAGE_MASK 0x7FF0 -#define CHARGE_VOLTAGE (CHARGER_CHARGE_VOLTAGE & CHARGE_VOLTAGE_MASK) +#define CHARGE_VOLTAGE (MIN(CHARGER_CHARGE_VOLTAGE, CHARGE_VOLTAGE_MASK) & CHARGE_VOLTAGE_MASK) -// Bits 0-6 are ignored. Bits 12-15 must be 0. -#define INPUT_CURRENT_MASK 0x0F80 +// Bits 0-6 are ignored. Bits 13-15 must be 0. +#define INPUT_CURRENT_MASK 0x1F80 #if CHARGER_ADAPTER_RSENSE == 5 - #define INPUT_CURRENT ((CHARGER_INPUT_CURRENT >> 1) & INPUT_CURRENT_MASK) + #define INPUT_CURRENT (MIN((CHARGER_INPUT_CURRENT >> 1), INPUT_CURRENT_MASK) & INPUT_CURRENT_MASK) #elif CHARGER_ADAPTER_RSENSE == 10 - #define INPUT_CURRENT (CHARGER_INPUT_CURRENT & INPUT_CURRENT_MASK) + #define INPUT_CURRENT (MIN(CHARGER_INPUT_CURRENT, INPUT_CURRENT_MASK) & INPUT_CURRENT_MASK) #elif CHARGER_ADAPTER_RSENSE == 20 - #define INPUT_CURRENT ((CHARGER_INPUT_CURRENT << 1) & INPUT_CURRENT_MASK) + #define INPUT_CURRENT (MIN((CHARGER_INPUT_CURRENT << 1), INPUT_CURRENT_MASK) & INPUT_CURRENT_MASK) #else #error Invalid adapter RSENSE value #endif +#if (CHARGER_ADAPTER_RSENSE == 5 && CHARGER_BATTERY_RSENSE == 5) || \ + (CHARGER_ADAPTER_RSENSE == 10 && CHARGER_BATTERY_RSENSE == 10) || \ + (CHARGER_ADAPTER_RSENSE == 20 && CHARGER_BATTERY_RSENSE == 20) +#define RSENSE_RATIO SBC_RSENSE_RATIO_1_1 +#elif (CHARGER_ADAPTER_RSENSE == 10 && CHARGER_BATTERY_RSENSE == 5) || \ + (CHARGER_ADAPTER_RSENSE == 20 && CHARGER_BATTERY_RSENSE == 10) +#define RSENSE_RATIO SBC_RSENSE_RATIO_2_1 +#elif (CHARGER_ADAPTER_RSENSE == 5 && CHARGER_BATTERY_RSENSE == 10) || \ + (CHARGER_ADAPTER_RSENSE == 10 && CHARGER_BATTERY_RSENSE == 20) +#define RSENSE_RATIO SBC_RSENSE_RATIO_1_2 +#else + #error Invalid adapter:battery RSENSE ratio +#endif + // XXX: Assumption: ac_last is initialized high. static bool charger_enabled = false; @@ -65,7 +96,7 @@ int16_t battery_charger_disable(void) { // Set charge option 0 with 175s watchdog res = smbus_write( CHARGER_ADDRESS, - 0x12, + REG_CHARGE_OPTION_0, SBC_EN_LWPWR | SBC_WDTMR_ADJ_175S | SBC_PWM_FREQ_800KHZ | @@ -112,12 +143,22 @@ int16_t battery_charger_enable(void) { // Set charge option 0 with watchdog disabled res = smbus_write( CHARGER_ADDRESS, - 0x12, + REG_CHARGE_OPTION_0, SBC_EN_LWPWR | SBC_PWM_FREQ_800KHZ | SBC_IDCHC_GAIN ); + // Set the RSENSE ratio + res = smbus_write( + CHARGER_ADDRESS, + REG_CHARGE_OPTION_1, + SBC_CMP_DEG_1US | + SBC_PMON_RATIO | + RSENSE_RATIO | + SBC_BAT_DEPL_VTH + ); + DEBUG("Charger enabled\n"); charger_enabled = true; return 0; @@ -149,10 +190,10 @@ void battery_debug(void) { command(Status, BATTERY_ADDRESS, 0x16); DEBUG("Charger (bq24780s):\n"); - command(ChargeOption0, CHARGER_ADDRESS, 0x12); - command(ChargeOption1, CHARGER_ADDRESS, 0x3B); - command(ChargeOption2, CHARGER_ADDRESS, 0x38); - command(ChargeOption3, CHARGER_ADDRESS, 0x37); + command(ChargeOption0, CHARGER_ADDRESS, REG_CHARGE_OPTION_0); + command(ChargeOption1, CHARGER_ADDRESS, REG_CHARGE_OPTION_1); + command(ChargeOption2, CHARGER_ADDRESS, REG_CHARGE_OPTION_2); + command(ChargeOption3, CHARGER_ADDRESS, REG_CHARGE_OPTION_3); command(ChargeCurrent, CHARGER_ADDRESS, REG_CHARGE_CURRENT); command(ChargeVoltage, CHARGER_ADDRESS, REG_CHARGE_VOLTAGE); command(DishargeCurrent, CHARGER_ADDRESS, REG_DISCHARGE_CURRENT); diff --git a/src/board/system76/common/charger/oz26786.c b/src/board/system76/common/charger/oz26786.c index c2db468f5..d03fb0c79 100644 --- a/src/board/system76/common/charger/oz26786.c +++ b/src/board/system76/common/charger/oz26786.c @@ -21,14 +21,14 @@ #define CHARGE_CURRENT_MASK 0x1FC0 #if CHARGER_BATTERY_RSENSE == 5 - #define CHARGE_CURRENT ((CHARGER_CHARGE_CURRENT >> 1) & CHARGE_CURRENT_MASK) + #define CHARGE_CURRENT (MIN((CHARGER_CHARGE_CURRENT >> 1), CHARGE_CURRENT_MASK) & CHARGE_CURRENT_MASK) // XXX: According to the datasheet, only 10 and 20 are valid. #define BATTERY_RSENSE (RSENSE_10 << 8) #elif CHARGER_BATTERY_RSENSE == 10 - #define CHARGE_CURRENT (CHARGER_CHARGE_CURRENT & CHARGE_CURRENT_MASK) + #define CHARGE_CURRENT (MIN(CHARGER_CHARGE_CURRENT, CHARGE_CURRENT_MASK) & CHARGE_CURRENT_MASK) #define BATTERY_RSENSE (RSENSE_10 << 8) #elif CHARGER_BATTERY_RSENSE == 20 - #define CHARGE_CURRENT ((CHARGER_CHARGE_CURRENT << 1) & CHARGE_CURRENT_MASK) + #define CHARGE_CURRENT (MIN((CHARGER_CHARGE_CURRENT << 1), CHARGE_CURRENT_MASK) & CHARGE_CURRENT_MASK) #define BATTERY_RSENSE (RSENSE_20 << 8) #else #error Invalid battery RSENSE value @@ -36,19 +36,19 @@ // Bits 0-3, 15 are ignored. #define CHARGE_VOLTAGE_MASK 0x7FF0 -#define CHARGE_VOLTAGE (CHARGER_CHARGE_VOLTAGE & CHARGE_VOLTAGE_MASK) +#define CHARGE_VOLTAGE (MIN(CHARGER_CHARGE_VOLTAGE, CHARGE_VOLTAGE_MASK) & CHARGE_VOLTAGE_MASK) // Bits 0-6 are ignored. Bits 12-15 must be 0. #define INPUT_CURRENT_MASK 0x0F80 #if CHARGER_ADAPTER_RSENSE == 5 - #define INPUT_CURRENT ((CHARGER_INPUT_CURRENT >> 2) & INPUT_CURRENT_MASK) + #define INPUT_CURRENT (MIN((CHARGER_INPUT_CURRENT >> 2), INPUT_CURRENT_MASK) & INPUT_CURRENT_MASK) #define ADAPTER_RSENSE RSENSE_5 #elif CHARGER_ADAPTER_RSENSE == 10 - #define INPUT_CURRENT ((CHARGER_INPUT_CURRENT >> 1) & INPUT_CURRENT_MASK) + #define INPUT_CURRENT (MIN((CHARGER_INPUT_CURRENT >> 1), INPUT_CURRENT_MASK) & INPUT_CURRENT_MASK) #define ADAPTER_RSENSE RSENSE_10 #elif CHARGER_ADAPTER_RSENSE == 20 - #define INPUT_CURRENT (CHARGER_INPUT_CURRENT & INPUT_CURRENT_MASK) + #define INPUT_CURRENT (MIN(CHARGER_INPUT_CURRENT, INPUT_CURRENT_MASK) & INPUT_CURRENT_MASK) #define ADAPTER_RSENSE RSENSE_20 #else #error Invalid adapter RSENSE value diff --git a/src/board/system76/oryp5/board.mk b/src/board/system76/oryp5/board.mk index abdda685d..a6e10f920 100644 --- a/src/board/system76/oryp5/board.mk +++ b/src/board/system76/oryp5/board.mk @@ -22,7 +22,7 @@ CFLAGS+=-DPS2_TOUCHPAD=PS2_3 CFLAGS+=\ -DCHARGER_ADAPTER_RSENSE=10 \ -DCHARGER_BATTERY_RSENSE=10 \ - -DCHARGER_CHARGE_CURRENT=3072 \ + -DCHARGER_CHARGE_CURRENT=2048 \ -DCHARGER_CHARGE_VOLTAGE=17120 \ -DCHARGER_INPUT_CURRENT=9230 diff --git a/src/board/system76/oryp6/board.mk b/src/board/system76/oryp6/board.mk index 277895187..c91fca682 100644 --- a/src/board/system76/oryp6/board.mk +++ b/src/board/system76/oryp6/board.mk @@ -21,7 +21,7 @@ CFLAGS+=-DPS2_TOUCHPAD=PS2_3 CFLAGS+=\ -DCHARGER_ADAPTER_RSENSE=10 \ -DCHARGER_BATTERY_RSENSE=10 \ - -DCHARGER_CHARGE_CURRENT=3072 \ + -DCHARGER_CHARGE_CURRENT=2048 \ -DCHARGER_CHARGE_VOLTAGE=13050 \ -DCHARGER_INPUT_CURRENT=9230 diff --git a/src/board/system76/oryp7/board.mk b/src/board/system76/oryp7/board.mk index 277895187..c91fca682 100644 --- a/src/board/system76/oryp7/board.mk +++ b/src/board/system76/oryp7/board.mk @@ -21,7 +21,7 @@ CFLAGS+=-DPS2_TOUCHPAD=PS2_3 CFLAGS+=\ -DCHARGER_ADAPTER_RSENSE=10 \ -DCHARGER_BATTERY_RSENSE=10 \ - -DCHARGER_CHARGE_CURRENT=3072 \ + -DCHARGER_CHARGE_CURRENT=2048 \ -DCHARGER_CHARGE_VOLTAGE=13050 \ -DCHARGER_INPUT_CURRENT=9230 diff --git a/src/board/system76/oryp8/board.mk b/src/board/system76/oryp8/board.mk index 29444c55a..16b09ee64 100644 --- a/src/board/system76/oryp8/board.mk +++ b/src/board/system76/oryp8/board.mk @@ -24,7 +24,7 @@ CFLAGS+=-DPS2_TOUCHPAD=PS2_3 CFLAGS+=\ -DCHARGER_ADAPTER_RSENSE=10 \ -DCHARGER_BATTERY_RSENSE=10 \ - -DCHARGER_CHARGE_CURRENT=3072 \ + -DCHARGER_CHARGE_CURRENT=2048 \ -DCHARGER_CHARGE_VOLTAGE=13050 \ -DCHARGER_INPUT_CURRENT=9230 diff --git a/src/common/include/common/macro.h b/src/common/include/common/macro.h index 5fe6fe159..d5e79b73e 100644 --- a/src/common/include/common/macro.h +++ b/src/common/include/common/macro.h @@ -13,4 +13,8 @@ #define BIT(X) (1UL << (X)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define CLAMP(val, min, max) (MIN(MAX(val, min), max)) + #endif // _COMMON_MACRO_H