From ea97c08fa62b246941d05dae27ba80e9fe998a2e Mon Sep 17 00:00:00 2001 From: "Sicelo A. Mhlongo" Date: Mon, 2 Dec 2024 19:59:24 +0200 Subject: [PATCH] device-nokia-n900: implement udev-based battery monitoring (MR 5868) The bq27200 fuel gauge depends on its battery-backed memory for capacity reporting. If the supply is off for some time, the fuel gauge's last known capacity is reset, after which a capacity value can only be read after a full charge cycle. Thus, most of the time, as used on the Nokia N900, the reported percentage cannot be relied on for making power-off decisions on battery low conditions, e.g. through UPower. However the fuel gauge can very accurately detect and report a low battery condition internally, even if the reported capacity is incorrect, e.g. when it has been reset. The low battery condition is reported by the kernel via capacity_level in sysfs, and also generates uevents. So we can implement safe battery shutdown by requesting the kernel to more regularly polling for status changes, then performing an immediate shutdown when the capacity_level is reported as Low and the device is not charging. --- device/community/device-nokia-n900/APKBUILD | 14 +++++++++++++- .../community/device-nokia-n900/bq27k-monitor.sh | 16 ++++++++++++++++ .../device-nokia-n900/bq27k-options.conf | 1 + .../device-nokia-n900/udev/15-bq27k.rules | 1 + 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 device/community/device-nokia-n900/bq27k-monitor.sh create mode 100644 device/community/device-nokia-n900/bq27k-options.conf create mode 100644 device/community/device-nokia-n900/udev/15-bq27k.rules diff --git a/device/community/device-nokia-n900/APKBUILD b/device/community/device-nokia-n900/APKBUILD index b679d4212..1fca960f4 100644 --- a/device/community/device-nokia-n900/APKBUILD +++ b/device/community/device-nokia-n900/APKBUILD @@ -2,7 +2,7 @@ # Maintainer: Sicelo # Co-Maintainer: Danct12 pkgname=device-nokia-n900 -pkgver=33 +pkgver=34 pkgrel=0 pkgdesc="Nokia N900" url="https://postmarketos.org" @@ -38,6 +38,8 @@ source=" asound.state.headset asound.state.speakers backlight-enable.sh + bq27k-options.conf + bq27k-monitor.sh device-nokia-n900.start deviceinfo modules-initfs @@ -62,6 +64,7 @@ source=" proxishot.sh uboot-script.cmd udev/10-nokia-modem.rules + udev/15-bq27k.rules udev/80-feedbackd-twl4030.rules udev/90-touchscreen-dev.rules upower.conf @@ -101,12 +104,16 @@ package() { "$pkgdir"/usr/share/mkinitfs/hooks/10-initfs-keymap.sh install -Dm755 "$srcdir"/$pkgname.start \ "$pkgdir"/etc/local.d/$pkgname.start + install -Dm755 "$srcdir"/bq27k-monitor.sh \ + "$pkgdir"/usr/bin/bq27k-monitor # Udev rules install -D -m644 "$srcdir"/90-touchscreen-dev.rules \ "$pkgdir"/etc/udev/rules.d/90-touchscreen-dev.rules install -D -m644 "$srcdir"/10-nokia-modem.rules \ "$pkgdir"/etc/udev/rules.d/10-nokia-modem.rules + install -D -m644 "$srcdir"/15-bq27k.rules \ + "$pkgdir"/usr/lib/udev/rules.d/15-bq27xxx-battery.rules install -Dm644 "$srcdir"/80-feedbackd-twl4030.rules \ "$pkgdir"/usr/lib/udev/rules.d/80-feedbackd-twl4030.rules @@ -133,6 +140,8 @@ package() { "$pkgdir"/etc/modprobe.d/nokia-modem.conf install -D -m644 "$srcdir"/modules.blocklist \ "$pkgdir"/etc/modprobe.d/n900-module-blocklist.conf + install -D -m644 "$srcdir"/bq27k-options.conf \ + "$pkgdir"/etc/modprobe.d/bq27xxx-battery.conf postmarketos-mvcfg-package "$pkgdir" "$pkgname" } @@ -205,6 +214,8 @@ ff51909873160c2d4c6f0c0119b6d4a02751df29f571b8596b4bc8b04ce2541913c11a583b0e5dab 5b87071834313a389f83d29f86f6e3ea108d0921f17dc918be7a46de7e74c73a4418044ef600a0fbf72c8fad22b7730dcd5bfe3d6d01d76bf01556f8540f0b32 asound.state.headset 67acc17a33bc75113300393b4a5bba15319014032407e774d079abf310368e75f4f697dbb4856ba2d2d8ea184b056bb33561f42918de3ee91ab6a77df89a133c asound.state.speakers 3d55e34b95791636e44a5f41754f3d0de039dbba41f7a556d43a95c9e64afcfa930046b4b96b40020b6f196096ffba93514682927e32fa4488686fdd19c6da5a backlight-enable.sh +344ec50cee2a4d864debad12810a83070ab8257f9f07dcfa9be3eea1d8b26f3ab4fbe322fe05a384152b40fc5e1481df51d191624c7062186dd70f95a0784ea8 bq27k-options.conf +c5b8e03ca9f720a794f2593a3e938640cbd91ac44dd761079bbf4614a0d288a9d7b72de8ed2493c062dc9f37be580bea529f454d2335f55b7a9f4d14024b42a9 bq27k-monitor.sh e6bbac8de1a198ba716f44ade76606e1e0adcc574156b855ac41be5eb5308389d3277fd89cc34b119fbc33d9801ab9f79a6ff213392596bff40bc17e931c05ef device-nokia-n900.start 40aaeb44941a988ab8ec58d331bc6cd42c4a23c6b6e8fc302f9216a38b46baab38d0a0137606a5b7959a51326983c03d684615c5ba9c58b0e36314ec8c49cfbb deviceinfo f48b8dd7297d03008f73e1ecd55b77ace535ecb03f9bdf021123b96fb5f4fb491ff4c532e226b835c8118fa8d505cc4c635a2b604a3e42a162746021552a551c modules-initfs @@ -229,6 +240,7 @@ e440ed7a3070c17e003b86b72dbe6d8194d01b577ca8dd56dd066f216b6dda32bb965c780950f178 134f51d20decc0c63ab08010931a0b410b70d2f85bf6899f2ff137b1680f59f706a6287d01f5fbb41fa5309125df75a430d0783149bd1a6a1bca07d22f4f6a21 proxishot.sh 42f916d87dd305111028992a1107bf6fe108bebbd782ae8edd065e76505db2b3a75e57515970c3e40cb0d1b7e83097a1bba92785be13df7c3fd80959a8d9c5c1 uboot-script.cmd 465b9a0d2b46d0981c64b47cb1f656c2456a90b91b1751a2efad10af38a96030dd99108b1fd76ce31bf1ea0f236d55bed38b114d6bd712f09f8968de034c946c 10-nokia-modem.rules +1acc441b9f83dc91fd65fa3f89b8205bfd72a465286bc181b4e7e92dd4b8c0bc5c20a18dab46ff3ee8ae70940efd7d767b3dfb920edb4c74377bd418b838693d 15-bq27k.rules 03079030237440080e833dbfeff80327c1ad6a8129c6c730b401a3052cd00a986783ec3473a526b14ae73ba1f20653b4c7b2a6590839bfe6cc156bbbc3b7bb74 80-feedbackd-twl4030.rules c6012aef28b096141b924fced226ec99fb93eb53b69c064a8887d8fecdf8dc08a3ba3db399e18d88374c6ef4c59013a30699c7e4d76e5cb771040582573a0527 90-touchscreen-dev.rules d46adf47194c02b434cb46751003e67ee008d60978458e69b1f59dc709a7135a70f542918c29e359ba8308bdeda58a21e8efa446d06625511af05403db90e455 upower.conf diff --git a/device/community/device-nokia-n900/bq27k-monitor.sh b/device/community/device-nokia-n900/bq27k-monitor.sh new file mode 100644 index 000000000..384fb5578 --- /dev/null +++ b/device/community/device-nokia-n900/bq27k-monitor.sh @@ -0,0 +1,16 @@ +#!/bin/sh +set -e +echo "Capacity: $POWER_SUPPLY_CAPACITY; Voltage: $POWER_SUPPLY_VOLTAGE_NOW; Level: $POWER_SUPPLY_CAPACITY_LEVEL" >> /tmp/bq27k.log +if [ "$POWER_SUPPLY_CAPACITY_LEVEL" = "Low" ] || [ "$POWER_SUPPLY_CAPACITY_LEVEL" = "Critical" ]; then + if [ "$POWER_SUPPLY_STATUS" != "Charging" ]; then + /sbin/poweroff + fi +fi +# Adjust polling rate based on reported voltage. As voltage drops to near +# critical levels, poll more frequently, in order to catch status changes +# early +if [ "$POWER_SUPPLY_VOLTAGE_NOW" -le 3350000 ]; then + echo 5 > /sys/module/bq27xxx_battery/parameters/poll_interval +elif [ "$POWER_SUPPLY_VOLTAGE_NOW" -ge 3500000 ]; then + echo 45 > /sys/module/bq27xxx_battery/parameters/poll_interval +fi diff --git a/device/community/device-nokia-n900/bq27k-options.conf b/device/community/device-nokia-n900/bq27k-options.conf new file mode 100644 index 000000000..5248009be --- /dev/null +++ b/device/community/device-nokia-n900/bq27k-options.conf @@ -0,0 +1 @@ +options bq27xxx-battery poll_interval=30 diff --git a/device/community/device-nokia-n900/udev/15-bq27k.rules b/device/community/device-nokia-n900/udev/15-bq27k.rules new file mode 100644 index 000000000..6e091a5a8 --- /dev/null +++ b/device/community/device-nokia-n900/udev/15-bq27k.rules @@ -0,0 +1 @@ +KERNEL=="bq27200-0", SUBSYSTEM=="power_supply", ACTION=="change", RUN+="/usr/bin/bq27k-monitor"