GNOME battery charge control

Posted on zo 28 januari 2024 in Linux

As someone who has to use a laptop for work, I keep my laptop plugged in 8 hours or more a day, 7 days a week. The laptop's battery during these days would discharge and charge, slowly degrading the battery because only the last ~ 20% would be charged and discharged. To remedy this issue some devices now have charge thresholds where a user or firmware can configure a charge limit to stop the battery from getting constantly charged.

The most commonly found solution is to set a stop charge limit at 80% of the battery capacity. (Green part is never charged)

stop threshold

Another approach is to set a start and stop charge threshold where the battery charges till for example 80% but will only start when below 60%. The 60-80% region marks a "charge free zone". This is ideal for users who keep their laptop plugged in 24/7.

start/stop threshold

The Linux kernel supports these kinds of charge thresholds via a sysfs interface /sys/class/power_supply/BAT*/charge_control_{start,end}_threshold depending on hardware and driver support. Users can set their own charge control thresholds by echoing a value between 0-100 to the sysfs entry via for example a systemd unit or using a tool such as TLP.

That is usable for power users who know what thresholds to set and who also remember to turn it off when you for travel for a longer period of time and want to make full use of your battery. As a GNOME user however I would like to have a visual indication that my battery is being charge limited and an easy switch to turn this on and off.

GNOME obtains devices battery information in the shell and GNOME settings via UPower, a daemon which runs in the background and exposes device information from sysfs to DBus. UPower is also used by other popular DE's such as Cinnamon, Phosh, Mate, Lxqt, Xfce and Deepin.

My merge request for UPower implements detecting if a battery supports charge limits (start and stop) and allows for enabling set limits via a DBus method on the battery object. The limits are configurable via hwdb as UPower sets known good values 60-80% by default, users and vendors can override these limits with a simple hwdb rule.

For GNOME's settings, this merge request implements the UI design from Allan Day as can be seen below. (note: the label still needs some changes)

GNOME Control Center

For those interested, I will be giving a presentation at FOSDEM in the Kernel devroom with more technical details. In the future I plan to work on a visual indicator in the GNOME Shell when charge limiting is enabled, implementing battery calibration and exposing charge thresholds on the Framework laptop.