Bug 234403 - [acpi_ibm] Support ThinkPad battery charge start/stop control
Summary: [acpi_ibm] Support ThinkPad battery charge start/stop control
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 12.0-STABLE
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-acpi mailing list
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2018-12-25 18:19 UTC by Kevin Zheng
Modified: 2019-09-20 20:10 UTC (History)
3 users (show)

See Also:


Attachments
Use consistent driver name (2.07 KB, patch)
2018-12-25 18:19 UTC, Kevin Zheng
no flags Details | Diff
Support ThinkPad battery charge start/stop control (5.78 KB, patch)
2018-12-25 18:19 UTC, Kevin Zheng
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Kevin Zheng 2018-12-25 18:19:11 UTC
Created attachment 200506 [details]
Use consistent driver name

Support ThinkPad battery charge start/stop control using new sysctls:

dev.acpi_ibm.0.bat0_charge_start
dev.acpi_ibm.0.bat0_charge_stop

Informed by the Linux driver: https://lore.kernel.org/patchwork/patch/858076/

And this driver: https://github.com/teleshoes/tpacpi-bat/blob/master/battery_asl

Two patches are attached:
- First one uses a consistent driver name in the source code and man pages "ThinkPad ACPI Extras" instead of references to IBM
- Second one contains the majority of the change
Comment 1 Kevin Zheng 2018-12-25 18:19:45 UTC
Created attachment 200507 [details]
Support ThinkPad battery charge start/stop control
Comment 2 Kevin Zheng 2019-07-03 17:54:30 UTC
Is anybody able to take a look?
Comment 3 rkoberman 2019-07-03 21:03:38 UTC
(In reply to Kevin Zheng from comment #2)

Submitting this on Christmas probably didn't help to get it noticed. Even though I have long wanted the ability to control charging, I totally missed it. A little explanation of why stopping and starting charging is desirable would probably be a desirable, as well.

Not sure about the naming convention change as I believe that this module is appropriate to more Lenovo models than just the Thinkpad. Maybe it is time to just bite the bullet and go to "Lenovo". Not that many IBM units around any more.

That said, any change might cause confusion for current users when acpi_ibm no longer loads.Is that worse than for newer users who no longer think of IBM and Thinkpad as related.
Comment 4 Kevin Zheng 2019-07-03 22:28:32 UTC
(In reply to rkoberman from comment #3)
Perhaps "Lenovo" is better than "ThinkPad" and "IBM". Note that this patch does not change the name of the module, so it is still "acpi_ibm" and loading "acpi_ibm" will still continue to work.

Controlling battery charge thresholds could be useful for prolonging battery life in machines that are used mostly on AC power. On Windows, battery charging thresholds can be controlled using the Lenovo Power Manager. This patch exposes the same functionality through the sysctl interface.
Comment 5 Manuel Stühn 2019-08-26 18:04:25 UTC
(In reply to Kevin Zheng from comment #0)
I've applied the patch on a FreeBSD 12.0p10 running on a Thinkpad T450. The sysctls appear but how can i test that the patch actually works as intended?
Comment 6 Kevin Zheng 2019-08-26 21:07:26 UTC
(In reply to Manuel Stühn from comment #5)
Set those sysctls to a non-zero number between 0-100.

If you start with a full battery, set start_thresh to something like 80; discharge your battery to 85%, plug in, battery shouldn't charge. Discharge to 75%, battery will charge until stop_thresh.
Comment 7 Manuel Stühn 2019-08-27 16:39:30 UTC
(In reply to Kevin Zheng from comment #6)
I've tried the following:

# sysctl hw.acpi.battery
hw.acpi.battery.info_expire: 5
hw.acpi.battery.units: 2
hw.acpi.battery.state: 1
hw.acpi.battery.time: 156
hw.acpi.battery.life: 57
# sysctl dev.acpi_ibm.0 | grep charge
dev.acpi_ibm.0.bat0_charge_stop: 90
dev.acpi_ibm.0.bat0_charge_start: 50

If i plug in the laptop then, it seems to charge anyway:

# sysctl hw.acpi.battery
hw.acpi.battery.info_expire: 5
hw.acpi.battery.units: 2
hw.acpi.battery.state: 2
hw.acpi.battery.time: -1
hw.acpi.battery.life: 58
Comment 8 Kevin Zheng 2019-08-27 16:50:42 UTC
(In reply to Manuel Stühn from comment #7)
Per Google (https://askubuntu.com/questions/34452/how-can-i-limit-battery-charging-to-80-capacity), this method might have stopped working in the T440s.

I've tested on T430s and T410, and this patch works on both.
Comment 9 rkoberman 2019-08-28 18:32:42 UTC
I'm using it on my T520 and it is working fine.

I read the old IBM whitepaper on this and they recommend keeping hte btter at or below 85% when a ystm is usually plugged in. Not sure just what they implemented, but the paper proposed that the percentage of time on battery be tracked and if the battery percentage was below a threshold, the maximum charge be reduced to 85%. If the charge dropped below come low value (20%?), the maximum charge would reset to 100% for some time. Statistics were to be retained across boots.

It's at least 20 years, so my memory of details is fuzzy, but I'm thinking of writing something to implement at least a bit of this scheme, but I have done little coding since kernels and drivers were written in assembly, so I'm hardly the ideal person for the job.
Comment 10 rkoberman 2019-09-20 20:03:26 UTC
I just hit a minor nit. I can't set dev.acpi_ibm.0.bat0_charge_stop to 100. I get "sysctl: dev.acpi_ibm.0.bat0_charge_stop=100: Invalid argument".  99 works fine. Since the range is really 1-100, perhaps you should subtract 0ne from the input value to pass the 09-99 value to ACPI. 

I also have noted the values are sticky, lasting through reboots and even power cycles, so are being stored in the BIOS configuration.

Still working well for me. I keep it at 85 when I'm home and bump it to 99 when traveling as I am more frequently not on AC and the extra 15% is very useful. It's really like to get this into the system, but I'd like to know more about it on newer systems. The Tx20s are over 8 years old and I expect to be upgrading mine before too much longer.
Comment 11 Kevin Zheng 2019-09-20 20:10:00 UTC
(In reply to rkoberman from comment #10)
Thanks for the comments.

The range is actually 0-99, where 0 means "battery controller default", and 1-99 is a custom setting.

The values are sticky, but they are not stored in the BIOS configuration. I believe the values are actually persisted on the battery controller chip in the battery pack and are reset when the battery pack is detached. Notice that if you set a value, shut down the computer, remove and reattach the battery, the values get reset.

I agree; it'd be great to figure out how to get this working on newer ThinkPads. Until I get one, however, I have no incentive to write a patch :)