From 34dd9ccbac362e7c7586db6a9732ad3838215a58 Mon Sep 17 00:00:00 2001 From: Tim Crawford Date: Thu, 17 Aug 2023 12:36:26 -0600 Subject: [PATCH] battery: Disable charger if full or not on AC Signed-off-by: Tim Crawford --- src/board/system76/common/battery.c | 9 ++++++++- src/board/system76/common/include/board/battery.h | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/board/system76/common/battery.c b/src/board/system76/common/battery.c index 4074707..06d2abf 100644 --- a/src/board/system76/common/battery.c +++ b/src/board/system76/common/battery.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-3.0-only #include +#include #include #include @@ -55,8 +56,14 @@ bool battery_set_end_threshold(uint8_t value) { */ int16_t battery_charger_configure(void) { static bool should_charge = true; + bool ac_present = !gpio_get(&ACIN_N); - if (battery_get_end_threshold() == BATTERY_END_DEFAULT) { + if (!ac_present || (battery_info.status & BATTERY_FULLY_CHARGED)) { + // Always disable charger if: + // - AC is not plugged in, or + // - Battery is fully charged + should_charge = false; + } else if (battery_get_end_threshold() == BATTERY_END_DEFAULT) { // Stop threshold not configured: Always charge on AC. should_charge = true; } else if (battery_info.charge > battery_get_end_threshold()) { diff --git a/src/board/system76/common/include/board/battery.h b/src/board/system76/common/include/board/battery.h index 170a365..b1523e7 100644 --- a/src/board/system76/common/include/board/battery.h +++ b/src/board/system76/common/include/board/battery.h @@ -16,6 +16,9 @@ #define CHARGER_ADDRESS 0x09 #endif +#define BATTERY_FULLY_DISCHARGED BIT(4) +#define BATTERY_FULLY_CHARGED BIT(5) +#define BATTERY_DISCHARGING BIT(6) #define BATTERY_INITIALIZED BIT(7) struct battery_info {