Bug 41062 - [PATCH]Incorrect battery capacity after power up/reboot on recent Thinkpads
Summary: [PATCH]Incorrect battery capacity after power up/reboot on recent Thinkpads
Status: CLOSED CODE_FIX
Alias: None
Product: ACPI
Classification: Unclassified
Component: Power-Battery (show other bugs)
Hardware: All Linux
: P1 normal
Assignee: Lan Tianyu
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-08-13 03:43 UTC by Kamil Iskra
Modified: 2013-02-08 18:28 UTC (History)
5 users (show)

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


Attachments
Output of dmidecode on Thinkpad X220 (13.41 KB, text/plain)
2011-08-27 23:41 UTC, Kamil Iskra
Details
Patch against 3.1.0-rc3's drivers/acpi/battery.c (2.58 KB, patch)
2011-08-27 23:58 UTC, Kamil Iskra
Details | Diff
Patch against 3.1.0-rc4's drivers/acpi/battery.c (4.31 KB, patch)
2011-09-04 04:41 UTC, Kamil Iskra
Details | Diff
Patch against 3.7-rc5's drivers/acpi/battery.c (4.52 KB, patch)
2012-11-12 04:44 UTC, Kamil Iskra
Details | Diff

Description Kamil Iskra 2011-08-13 03:43:36 UTC
Recent Thinkpads (I'm seeing this on last year's x201 as well as this year's x220, but not on older T61 or T42) display incorrect battery capacity in /proc/acpi/battery/*/*.

The bug is easily reproducible, but subtle.  These recent Thinkpad models have a weird "feature" that the battery charge in /proc/acpi/battery/*/* is initially (after power up/reboot) reported in mAh, but after a suspend/resume cycle while running on battery, it changes to mWh.

Here's /proc/acpi/battery/BAT0/info after reboot:

present:                 yes
design capacity:         2886 mAh
last full capacity:      2909 mAh
battery technology:      rechargeable
design voltage:          14800 mV
design capacity warning: 145 mAh
design capacity low:     13 mAh
cycle count:              0
capacity granularity 1:  1 mAh
capacity granularity 2:  1 mAh
model number:            42T4899
serial number:           21064
battery type:            LION
OEM info:                SANYO

After a suspend/resume cycle the output changes to:

present:                 yes
design capacity:         28860 mWh
last full capacity:      29090 mWh
battery technology:      rechargeable
design voltage:          14800 mV
design capacity warning: 1454 mWh
design capacity low:     200 mWh
cycle count:              0
capacity granularity 1:  1 mWh
capacity granularity 2:  1 mWh
model number:            42T4899
serial number:           21064
battery type:            LION
OEM info:                SANYO

Can you see how the values for "design capacity", etc. differ by a factor of 10, instead of 14.8 (the design voltage of this battery)?  On the battery itself it says: 14.8V, 1.95Ah, 29Wh, so clearly the values reported in mWh are correct and the ones in mAh (right after power up/reboot) are not.

Common Thinkpad batteries are rated at 10.8V, so the error with them is just 8% and probably that's why it hasn't been reported so far.  However, I got a smaller 4-cell battery rated at 14.8V, and with this battery there is a huge discrepancy in powertop between immediate vs long-term power estimates.
Comment 1 Lan Tianyu 2011-08-23 08:13:18 UTC
Does the problem happen before 3.1-rc1 ?
Comment 2 Kamil Iskra 2011-08-23 15:01:37 UTC
Oh yes, on my x201 I tested it back to 2.6.34 and saw the same error there.  So this is not a recent regression.  For all I know, it might have always been broken on these recent Thinkpad models.

I'm inclined to try to force the acpi battery module to always use mWh units on Thinkpads instead of mAh, since the values displayed for mAh are clearly incorrect.  On the older Thinkpads where I don't see this problem, the values are always in mWh.  So perhaps the battery driver somehow wrongly figures out that it should be using mAh units right after power up/reboot, and forcing it to use mWh instead will magically fix things?  Or will this just change the unit but the values will be too low by a factor of 10?

What would be the best way to try this?  Override battery->power_unit in acpi_battery_quirks()?
Comment 3 Lan Tianyu 2011-08-24 01:44:33 UTC
These information are got from method "_BIF" of DSDT in the bios. The battery driver just shows them. 

I also have seen some bug about the thinkpad x201 battery. The unit will change when returning from suspend or hibernation.(E.G bug 28192)
https://bugzilla.kernel.org/show_bug.cgi?id=28192

Bug 28192 reporter also uses thinkpad x201.
Is there newer bios version?
Comment 4 Kamil Iskra 2011-08-24 05:19:21 UTC
Yes, I've seen bug #28192 before.  I believe the issue I'm reporting is different.

The problem in bug #28192 was inconsistency in the driver as the battery changes units from mAh to mWh (I've never seen that problem on my laptop(s), BTW, though perhaps it's simply because I rarely look in /sys).  The problem I'm seeing is that the values reported are wrong *without* any changes of units -- they are wrong right from a power up/reboot.

I'm using the latest BIOS.  The issue is not x201-specific.  I'm seeing it on x220 as well.  Given that it affects more than one model, I'm guessing that many recent Thinkpad models may be affected.  I could check on a few more systems (x200, x300, t410) if you wish.

This problem is really easy to reproduce:

1. boot your Thinkpad
2. cat /proc/acpi/battery/BAT0/info
3. if the units for "design capacity", etc, are "mWh", the system is not affected
4. if the units are "mAh", check the value of "design capacity" against the battery specification (printed on the battery itself, available on Lenovo website, or in Lenovo Power Manager under Windows).  My experience is that the value under Linux is *always* wrong.  It is too high by a factor of "design voltage/10000".

Workaround: while running on battery, suspend/resume, or rmmod/modprobe battery.  This changes the unit to mWh and the values shown are what they should be.
Comment 5 Lan Tianyu 2011-08-25 06:58:43 UTC
You want to make a quirk. Exactly when the unit is "mAh", divide the value by 10 and multiply 14.8 to convert it to the correct value in the "mWh".

Please provide output dmicode and test with the kernel param "acpi_osi=linux".
Comment 6 Kamil Iskra 2011-08-27 23:41:45 UTC
Created attachment 70562 [details]
Output of dmidecode on Thinkpad X220
Comment 7 Kamil Iskra 2011-08-27 23:58:23 UTC
Created attachment 70572 [details]
Patch against 3.1.0-rc3's drivers/acpi/battery.c

Adding "acpi_osi=linux" to the kernel command line did not fix the problem.  I'm attaching a patch that does fix it.

The patch is straightforward, my only doubt is on which systems the quirk should be activated.  Currently it activates on all Lenovo Thinkpads.  According to my tests, this is safe, because older (2005-2010) systems never use mAh units and are thus unaffected, whereas all the newer systems I've tried always need the adjustment if the unit is mAh.  So at this moment the patch appears to be safe.

Of course, Lenovo could fix this problem at some point.  A more selective activation would be better.  I see in the output of dmidecode that there is (correct) information on battery design capacity in there.  Maybe we could scan the DMI list on battery add to see if the values read from ACPI need adjustment or not?  That could get tricky on systems with more than one battery, though...
Comment 8 Kamil Iskra 2011-09-04 04:41:50 UTC
Created attachment 71632 [details]
Patch against 3.1.0-rc4's drivers/acpi/battery.c

Here's an updated version of the patch.  The new version no longer crashes the kernel with a division by zero error when the battery is removed, and it performs the DMI scan I suggested earlier so that the quirk is only activated if the reported values are incorrect.

One thing that isn't entirely clear to me is the correct way of translating capacity_now when it's in mAh units into a charge value in mWh.  Should it be multiplied by voltage_now or design_voltage?  PowerTOP multiples by voltage_now and that's what I assumed in the previous version of the patch, but I noticed that UPower was showing an incorrect value then, and so were panel battery icons in KDE and Gnome. So in this new version I switched so that multiplying by design_voltage gives a correct value.  Is the code in PowerTOP wrong?
Comment 9 Kamil Iskra 2012-01-10 05:39:44 UTC
I just tested the newly released 3.2.0 kernel and I'm still seeing the same problem.  Any chance of my patch from comment #8 being merged?  I've been using it for months now without problems.
Comment 10 Zhang Rui 2012-01-18 05:33:05 UTC
It's great that the kernel bugzilla is back.

Can you please verify if the problem still exists in the latest upstream
kernel?
Comment 11 Kamil Iskra 2012-01-18 16:31:24 UTC
The bug still exists in 3.2.1.

See my patch at https://bugzilla.kernel.org/attachment.cgi?id=71632 for a possible fix.  I've been using it for months now without problems.
Comment 12 Lan Tianyu 2012-01-31 02:49:09 UTC
Did you send your patch to the acpi maillist?
Comment 13 Kamil Iskra 2012-01-31 04:38:46 UTC
No, I did not.  Are you suggesting that I do?  What mailing list should I use? linux-acpi? ibm-acpi-devel?
Comment 14 Lan Tianyu 2012-01-31 05:29:36 UTC
Yeah.
send to linux-acpi.
Follow this website.
http://www.lesswatts.org/projects/acpi/submitting-patches.php
Comment 15 Zhang Rui 2012-02-02 02:53:10 UTC
We can mark this bug as RESOLVED and PATCH_ALREADY_AVAILABLE once we have a fix for the problem.
Len will look at the patches in RESOLVED bugs and try to merge them.

Anyway, either Kamil or Tianyu can send the patch to the list for review first.
Comment 16 Lan Tianyu 2012-02-03 01:03:58 UTC
Hi kamll:
      Are you preparing to send the patch to linux-acpi maillist? If no, I can help you.
Comment 17 Kamil Iskra 2012-02-03 05:29:20 UTC
Sorry, I've had a crazy week at work.  Thankfully, the weekend is near, and I'm hoping to catch up then -- including sending the patch.
Comment 18 Len Brown 2012-06-05 03:16:53 UTC
This was addressed by
da8aeb92d4853f37e281f11fddf61f9c7d84c3cd
(ACPI / Battery: Update information on info notification and resume)

in linux 2.6.38
Comment 19 Kamil Iskra 2012-06-05 03:34:55 UTC
I'm sorry, but the problem reported here was *not* addressed in 2.6.38.  It is still present in kernel 3.4.  The problem fixed in 2.6.38 is a separate, though related, issue.

Allow me to repeat what I already wrote in comment #4 above:

"Yes, I've seen bug #28192 before.  I believe the issue I'm reporting is
different.

The problem in bug #28192 was inconsistency in the driver as the battery
changes units from mAh to mWh (I've never seen that problem on my laptop(s),
BTW, though perhaps it's simply because I rarely look in /sys).  The problem
I'm seeing is that the values reported are wrong *without* any changes of units
-- they are wrong right from a power up/reboot.

I'm using the latest BIOS.  The issue is not x201-specific.  I'm seeing it on
x220 as well.  Given that it affects more than one model, I'm guessing that
many recent Thinkpad models may be affected.  I could check on a few more
systems (x200, x300, t410) if you wish.

This problem is really easy to reproduce:

1. boot your Thinkpad, preferably plugged in
2. cat /proc/acpi/battery/BAT0/info
3. if the units for "design capacity", etc, are "mWh", the system is not
affected
4. if the units are "mAh", check the value of "design capacity" against the
battery specification (printed on the battery itself, available on Lenovo
website, or in Lenovo Power Manager under Windows).  My experience is that the
value under Linux is *always* wrong.  It is too high by a factor of "design
voltage/10000".

Workaround: while running on battery, suspend/resume, or rmmod/modprobe
battery.  This changes the unit to mWh and the values shown are what they
should be."

I've since checked every Thinkpad I could test and all 2011-2012 models I tried exhibit this problem.  The patch that fixes the problem for me is at https://bugzilla.kernel.org/attachment.cgi?id=71632 (comment #8 above).
Comment 20 Lan Tianyu 2012-11-05 00:55:04 UTC
Hi kamll:
         Can you resend your patch based on the newest upstream kernel to acpi maillist?
Comment 21 Kamil Iskra 2012-11-07 20:13:59 UTC
Lan,

There have been some external developments with respect to the problem addressed by this patch.

It looks like new (2012) Thinkpads have a fixed BIOS that does not switch between mAh and mWh, always using (correct) mWh units instead (the way 2009 and older Thinkpads did).  At least, that's what I saw on an x230.

Also, at least for the one 2011 Thinkpad that I have an easy access to, namely x220, Lenovo has released a BIOS update earlier this year (version 1.29 if I'm not mistaken) that fixes the bug.

Unfortunately, the 2010 models (tested with x201), even with the latest BIOS, still exhibit the problem and need my patch to correct the battery capacity when using mAh units.

The patch I've posted earlier does apply clearly to the 3.6.6 kernel, although I should adjust the comments in it with the above updated info before reposting.

I have been wondering if it would be helpful to also copy the Thinkpad ACPI people (ibm-acpi-devel@lists.sourceforge.net) on this patch to get their input/support?
Comment 22 Lan Tianyu 2012-11-09 01:12:54 UTC
You can Cc Thinkpad ACPI people when you send out the patch. Go ahead.
Comment 23 Kamil Iskra 2012-11-12 04:44:01 UTC
Created attachment 86141 [details]
 Patch against 3.7-rc5's drivers/acpi/battery.c 

Updated version of the patch.  Only the comment is changed.  I'll mail it to the ML shortly.
Comment 24 Lan Tianyu 2012-11-17 13:51:24 UTC
http://git.kernel.org/?p=linux/kernel/git/rafael/linux-pm.git;a=commit;h=4000e626156935dfb626321ce09cae2c833eabbb

The patch has been merged into linux-pm tree.
Comment 25 Len Brown 2013-02-08 18:28:14 UTC
Author: Kamil Iskra <kamil@iskra.name>  2012-11-16 16:28:58
Committer: Rafael J. Wysocki <rafael.j.wysocki@intel.com>  2012-11-16 16:28:58
Parent: 9069240480e24a2d6ce23404c9ad6cabf59b7258 (ACPI / PM: Fix unused function warnings for CONFIG_PM_SLEEP)
Branches: master, remotes/origin/master
Follows: v3.7-rc5
Precedes: v3.8-rc1

closed.

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