Bug 215531 - Lenovo charge limit feature stops 1% short of the configured limit and says it's still charging
Summary: Lenovo charge limit feature stops 1% short of the configured limit and says i...
Status: RESOLVED CODE_FIX
Alias: None
Product: Drivers
Classification: Unclassified
Component: Platform_x86 (show other bugs)
Hardware: All Linux
: P1 normal
Assignee: drivers_platform_x86@kernel-bugs.osdl.org
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-01-25 17:50 UTC by Nate Graham
Modified: 2022-02-14 21:17 UTC (History)
2 users (show)

See Also:
Kernel Version: 5.15.16
Subsystem:
Regression: No
Bisected commit-id:


Attachments

Description Nate Graham 2022-01-25 17:50:26 UTC
I have a Lenovo ThinkPad X1 Yoga that has kernel support for setting a charge limit.

However when I use this feature, charging stops 1% short of the specified limit, and the state is reported as "charging".

For example when I set a charge limit of 80%, charging stops at 79% but reports that it is still charging.

This was investigated in upower and determined to be a kernel bug. See https://gitlab.freedesktop.org/upower/upower/-/issues/142
Comment 1 Hans de Goede 2022-02-14 09:41:53 UTC
This has been fixed in kernels >= 5.16.3 with this commit:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e96c1197aca628f7d2480a1cc3214912b40b3414

With these kernels:

cat /sys/class/power_supply/BAT0/status

Will now output "not charging" under these conditions. This is what the device's firmware gives us, so this is the best the kernel can do. Translating "not charging" + the capacity in % being more or less equal to the charge_control_end_threshold(1) value to "full" is something for userspace (upower) to sort out. Since we are looking on adding UI options to set charge_control_end_threshold, it would be good if we can get upower reporting "full" under these conditions.

Note the upower issue should be re-opened to track this.
Comment 2 Nate Graham 2022-02-14 18:30:51 UTC
> the capacity in % being more or less equal to the
> charge_control_end_threshold(1) value

Does this mean that expect userspace software to say, "79% is close enough to 80%, consider them equal"?
Comment 3 Hans de Goede 2022-02-14 20:02:58 UTC
(In reply to Nate Graham from comment #2)
> > the capacity in % being more or less equal to the
> > charge_control_end_threshold(1) value
> 
> Does this mean that expect userspace software to say, "79% is close enough
> to 80%, consider them equal"?

If necessary, yes. It also depends where the 79% is coming from.

On my X1 carbon gen 8, there is both a "capacity" file as well as "energy_now" and "energy_full" files under /sys/class/power_supply/BAT0. IIRC upower prefers the "energy_now" and "energy_full" files and then calculates a % based on these itself.

It is possible that when the system stops charging /sys/class/power_supply/BAT0/capacity actually is 80% where as the calculation of the % from:

energy_now * 100 / energy_full

Results in 79% especially if that divide is down without rounding the result to the nearest whole percent (by default integer division in C rounds down).

So it might be better if for the "full" because of hitting the charge threshold case the check uses /sys/class/power_supply/BAT0/capacity rather then energy_now, this will all need to be tested as this gets implemented and will likely need some more tweaking while the feature has just landed.

Things like the above example can all lead to things looking to be 1% or sometimes even 2% of, so yes userspace will be expected to fuzz things a bit here if necessary.
Comment 4 Nate Graham 2022-02-14 20:11:31 UTC
OK, thank you for the information. I will adjust our code in KDE Plasma accordingly.
Comment 5 Hans de Goede 2022-02-14 20:17:38 UTC
(In reply to Nate Graham from comment #4)
> OK, thank you for the information. I will adjust our code in KDE Plasma
> accordingly.

Does KDE Plasma not use upower for this?  This sort of low-level raw kernel interface wrangling to make it easier to use for userspace ideally really should be shared between different desktop environments.
Comment 6 Nate Graham 2022-02-14 21:17:28 UTC
We do use upower, but upower is currently affected by the aforementioned bug that makes it not report that the battery is full when a charge threshold has been met, so we have code working around this until that gets fixed.

Once upower correctly reports "plugged in and fully charged" when the charge level is at the limit and charging has stopped, we can remove that code.

Note You need to log in before you can comment on or make changes to this bug.