]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
ACPI: battery: negate current when discharging
authorPeter Marheine <pmarheine@chromium.org>
Thu, 8 May 2025 02:41:45 +0000 (12:41 +1000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 27 Jun 2025 10:05:28 +0000 (11:05 +0100)
[ Upstream commit 234f71555019d308c6bc6f98c78c5551cb8cd56a ]

The ACPI specification requires that battery rate is always positive,
but the kernel ABI for POWER_SUPPLY_PROP_CURRENT_NOW
(Documentation/ABI/testing/sysfs-class-power) specifies that it should
be negative when a battery is discharging. When reporting CURRENT_NOW,
massage the value to match the documented ABI.

This only changes the sign of `current_now` and not `power_now` because
documentation doesn't describe any particular meaning for `power_now` so
leaving `power_now` unchanged is less likely to confuse userspace
unnecessarily, whereas becoming consistent with the documented ABI is
worth potentially confusing clients that read `current_now`.

Signed-off-by: Peter Marheine <pmarheine@chromium.org>
Link: https://patch.msgid.link/20250508024146.1436129-1-pmarheine@chromium.org
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/acpi/battery.c

index 8bb0f4d06adc01ce897d60085339d6fcc0fd637e..b0a5d077db90548b8643439da8249f6df2e5bced 100644 (file)
@@ -250,10 +250,23 @@ static int acpi_battery_get_property(struct power_supply *psy,
                break;
        case POWER_SUPPLY_PROP_CURRENT_NOW:
        case POWER_SUPPLY_PROP_POWER_NOW:
-               if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
+               if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN) {
                        ret = -ENODEV;
-               else
-                       val->intval = battery->rate_now * 1000;
+                       break;
+               }
+
+               val->intval = battery->rate_now * 1000;
+               /*
+                * When discharging, the current should be reported as a
+                * negative number as per the power supply class interface
+                * definition.
+                */
+               if (psp == POWER_SUPPLY_PROP_CURRENT_NOW &&
+                   (battery->state & ACPI_BATTERY_STATE_DISCHARGING) &&
+                   acpi_battery_handle_discharging(battery)
+                               == POWER_SUPPLY_STATUS_DISCHARGING)
+                       val->intval = -val->intval;
+
                break;
        case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
        case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: