From 4b54fc7f83f9e08e2a80bbeb6a97f871748bf7c6 Mon Sep 17 00:00:00 2001 From: "Sicelo A. Mhlongo" Date: Sun, 10 Sep 2023 00:57:26 +0200 Subject: [PATCH] linux-postmarketos-omap: add N900 battery capacity estimation (MR 4388) Provide a rough battery capacity estimation for the bq27200 fuel gauge inside Nokia N900. This is useful when the fuel gauge is out of calibration and a learning cycle cannot take place due to broken USB/charging port on the device. [ci:skip-build]: Already built successfully in CI --- ...ery-map-edv1-to-low-battery-capacity.patch | 42 ++++++++++++ ...add-voltage-based-capacity-estimatio.patch | 65 +++++++++++++++++++ .../linux-postmarketos-omap/APKBUILD | 6 +- 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 device/community/linux-postmarketos-omap/0011-bq27xxx_battery-map-edv1-to-low-battery-capacity.patch create mode 100644 device/community/linux-postmarketos-omap/0012-bq27xxx_battery-add-voltage-based-capacity-estimatio.patch diff --git a/device/community/linux-postmarketos-omap/0011-bq27xxx_battery-map-edv1-to-low-battery-capacity.patch b/device/community/linux-postmarketos-omap/0011-bq27xxx_battery-map-edv1-to-low-battery-capacity.patch new file mode 100644 index 000000000..a8a89217c --- /dev/null +++ b/device/community/linux-postmarketos-omap/0011-bq27xxx_battery-map-edv1-to-low-battery-capacity.patch @@ -0,0 +1,42 @@ +From 350df21c4f01137e3d29f445e36aeb6c82b85e34 Mon Sep 17 00:00:00 2001 +From: "Sicelo A. Mhlongo" +Date: Mon, 11 Sep 2023 10:34:54 +0200 +Subject: [PATCH 1/2] bq27xxx_battery: map edv1 to low battery capacity + +EDV1 is the most reliable indicator for battery low state on the N900/bq27200, +and the driver already exposes it in the POWER_SUPPLY_PROP_CAPACITY_LEVEL (Low) +and POWER_SUPPLY_PROP_HEALTH (Dead) properties. However, most userspace, +including upower, does not use these properties for decision-making. Map EDV1 +state to very low battery capacity to force userspace to act on EDV1. + +Signed-off-by: Sicelo A. Mhlongo +--- + drivers/power/supply/bq27xxx_battery.c | 12 +++++++++++- + 1 file changed, 11 insertions(+), 1 deletion(-) + +diff --git a/drivers/power/supply/bq27xxx_battery.c b/drivers/power/supply/bq27xxx_battery.c +index 4296600e8912..0789061ae9a0 100644 +--- a/drivers/power/supply/bq27xxx_battery.c ++++ b/drivers/power/supply/bq27xxx_battery.c +@@ -1851,7 +1851,17 @@ static void bq27xxx_battery_update_unlocked(struct bq27xxx_device_info *di) + cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF); + + cache.charge_full = bq27xxx_battery_read_fcc(di); +- cache.capacity = bq27xxx_battery_read_soc(di); ++ ++ /* HACK: If EDV1 flag is set, force capacity to report 1%, even though ++ * SEDV1 actually represents about 6.25%, per datasheet. This is to ensure ++ * that userspace, such as upower, can readily react to the true battery low ++ * state (EDV1 set) whether or not the reported SoC value is reliable. ++ */ ++ if (cache.flags & BQ27000_FLAG_EDV1) ++ cache.capacity = 1; ++ else ++ cache.capacity = bq27xxx_battery_read_soc(di); ++ + if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR) + cache.energy = bq27xxx_battery_read_energy(di); + di->cache.flags = cache.flags; +-- +2.40.1 + diff --git a/device/community/linux-postmarketos-omap/0012-bq27xxx_battery-add-voltage-based-capacity-estimatio.patch b/device/community/linux-postmarketos-omap/0012-bq27xxx_battery-add-voltage-based-capacity-estimatio.patch new file mode 100644 index 000000000..6a3398bf0 --- /dev/null +++ b/device/community/linux-postmarketos-omap/0012-bq27xxx_battery-add-voltage-based-capacity-estimatio.patch @@ -0,0 +1,65 @@ +From 456b5d133e026811fd735096ff3884823279879c Mon Sep 17 00:00:00 2001 +From: "Sicelo A. Mhlongo" +Date: Mon, 11 Sep 2023 11:14:56 +0200 +Subject: [PATCH] bq27xxx_battery: add voltage-based capacity estimation + +Use battery voltage to approximate the battery capacity, if the fuel gauge does +not have valid data. + +Signed-off-by: Sicelo A. Mhlongo +--- + drivers/power/supply/bq27xxx_battery.c | 28 ++++++++++++++++++++++++-- + 1 file changed, 26 insertions(+), 2 deletions(-) + +diff --git a/drivers/power/supply/bq27xxx_battery.c b/drivers/power/supply/bq27xxx_battery.c +index 0789061ae9a0..9476caed0ef4 100644 +--- a/drivers/power/supply/bq27xxx_battery.c ++++ b/drivers/power/supply/bq27xxx_battery.c +@@ -1017,6 +1017,8 @@ static struct { + + static DEFINE_MUTEX(bq27xxx_list_lock); + static LIST_HEAD(bq27xxx_battery_devices); ++static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di, ++ union power_supply_propval *val); + + #define BQ27XXX_MSLEEP(i) usleep_range((i)*1000, (i)*1000+500) + +@@ -1857,11 +1859,33 @@ static void bq27xxx_battery_update_unlocked(struct bq27xxx_device_info *di) + * that userspace, such as upower, can readily react to the true battery low + * state (EDV1 set) whether or not the reported SoC value is reliable. + */ +- if (cache.flags & BQ27000_FLAG_EDV1) ++ if (cache.flags & BQ27000_FLAG_EDV1) { + cache.capacity = 1; +- else ++ } else { + cache.capacity = bq27xxx_battery_read_soc(di); + ++ /* Use voltage to estimate capacity if valid data is not available in the ++ * fuel gauge. ++ */ ++ if ((cache.capacity == 0) && ++ bq27xxx_battery_capacity_inaccurate(di, cache.flags)) { ++ union power_supply_propval bqval; ++ bq27xxx_battery_voltage(di, &bqval); ++ if (bqval.intval > 4000000) ++ cache.capacity = 90; ++ else if (bqval.intval > 3840000) ++ cache.capacity = 80; ++ else if (bqval.intval > 3680000) ++ cache.capacity = 50; ++ else if (bqval.intval > 3550000) ++ cache.capacity = 30; ++ else if (bqval.intval > 3450000) ++ cache.capacity = 20; ++ else ++ cache.capacity = 10; ++ } ++ } ++ + if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR) + cache.energy = bq27xxx_battery_read_energy(di); + di->cache.flags = cache.flags; +-- +2.40.1 + diff --git a/device/community/linux-postmarketos-omap/APKBUILD b/device/community/linux-postmarketos-omap/APKBUILD index ed57c541b..e370005e4 100644 --- a/device/community/linux-postmarketos-omap/APKBUILD +++ b/device/community/linux-postmarketos-omap/APKBUILD @@ -4,7 +4,7 @@ pkgname=linux-postmarketos-omap pkgver=6.5 -pkgrel=0 +pkgrel=1 pkgdesc="Mainline kernel fork for OMAP devices" arch="armv7" url="https://kernel.org/" @@ -41,6 +41,8 @@ source=" 0008-hsi-ssi_port-force-pio-path.patch 0009-n900-dts-volume-keys.patch 0010-ARM-dts-disable-twl-off-idle-configuration-for-N900.patch + 0011-bq27xxx_battery-map-edv1-to-low-battery-capacity.patch + 0012-bq27xxx_battery-add-voltage-based-capacity-estimatio.patch " builddir="$srcdir/linux-$pkgver" replaces="linux-samsung-espresso3g linux-nokia-n900" @@ -81,4 +83,6 @@ f0bb41630d8e08adcffa433f3c82047e8787c96b7cc7301eb80bf5e5298010c136cd10c756278fb2 c3af9715b3559c2d593f4fcfa078730722c7deeec132c5b83e085ff4d9815d85ef349384097c580efe1bbc37c60f42e18ef559f0abccfe236080670e4403fa77 0008-hsi-ssi_port-force-pio-path.patch b98ce806b3d5a0122086e4c9670639174470ff6d29851c60258cc5d699ce9a479dbf4996b24299fc075d25e9fe8f6b1250fafdff742deea0ddeaf53d342a9d72 0009-n900-dts-volume-keys.patch 66abb5548910ad369608b08200f5835d5a8526c04cc3617221ef546f3e3d22cd944db91dc6727a5c26a102b24d8ef1306ea01254c9c382759afced91b31747ef 0010-ARM-dts-disable-twl-off-idle-configuration-for-N900.patch +6680b264db0c992826f9da0078256ba20ffae3de26a77978106b94499d96e72ed90d9a35bb42be38bae1e798e7ddf068e3c5e7fe63462b3bbad5031195622192 0011-bq27xxx_battery-map-edv1-to-low-battery-capacity.patch +cd91f0bb59e466c517b498a549357325be884bb1884986e593fef905a6210872024e0cab179df03833dfc6e8d1221441fa384466ae8227d82f6e70b22fce655c 0012-bq27xxx_battery-add-voltage-based-capacity-estimatio.patch "