Skip to content

Commit

Permalink
Fix charger values, again (#303)
Browse files Browse the repository at this point in the history
* common: Add macros for min, max, clamp

Simple macros, with no type checking.

Signed-off-by: Tim Crawford <[email protected]>

* charger: Limit charger values to max valid value

Use the maximum valid value instead of discarding bits.

Fixes: 6295f60 ("Fix smart charger values")

Signed-off-by: Tim Crawford <[email protected]>

* charger/bq24780s: Fix charge current mask

bq24780s uses bit 12 for 4096 a current weight.

Fixes: 6295f60 ("Fix smart charger values")

Signed-off-by: Tim Crawford <[email protected]>

* oryp: Reduce charge current to 2A

Signed-off-by: Tim Crawford <[email protected]>

* charger/bq24780s: Set RSENSE ratio option

Signed-off-by: Tim Crawford <[email protected]>

Signed-off-by: Tim Crawford <[email protected]>
  • Loading branch information
crawfxrd authored Oct 6, 2022
1 parent a32a3e3 commit 76019bd
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 26 deletions.
71 changes: 56 additions & 15 deletions src/board/system76/common/charger/bq24780s.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand All @@ -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 |
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions src/board/system76/common/charger/oz26786.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,34 @@
#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
#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

#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
Expand Down
2 changes: 1 addition & 1 deletion src/board/system76/oryp5/board.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/board/system76/oryp6/board.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/board/system76/oryp7/board.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/board/system76/oryp8/board.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions src/common/include/common/macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 76019bd

Please sign in to comment.