acpi: battery: Query BCT method and steamdeck
v2 cover acpi: battery: Query BCT method and steamdeck
Short series to query Battery charge time ("_BCT") from ACPI tables (1/2).
Patch 2/2 handles the case where steamdeck devices can report "Charging" even when low power PSU is connected which is discharging the device.
Changes in v2:
- comment improvements
- split off BCT method handling into separate patch
v1:
https://lore.kernel.org/all/[email protected]/
Umang Jain (2):
acpi: battery: Query Battery charge time ("_BCT") method
acpi: battery: Handle Valve steamdeck devices quirk
drivers/acpi/battery.c | 45 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-)
Signature · 1 line
2.55.0
PATCH v2 1/2 acpi: battery: Query Battery charge time ("_BCT") method
Add plumbing for querying the Battery Charge Time ("_BCT") method. If present, set ACPI_BATTERY_BCT_PRESENT in battery flags to denote that the method is supported in ACPI.
--- drivers/acpi/battery.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
drivers/acpi/battery.c +17 -0
@@ -76,6 +76,7 @@ MODULE_DEVICE_TABLE(acpi, battery_device_ids);enum {ACPI_BATTERY_ALARM_PRESENT ,ACPI_BATTERY_XINFO_PRESENT , +ACPI_BATTERY_BCT_PRESENT ,ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY ,/* On Lenovo Thinkpad models from 2010 and 2011, the power unit * switches between mWh and mAh depending on whether the system
@@ -110,6 +111,7 @@ struct acpi_battery {struct mutex property_lock;/* Protects properties below. */ unsigned long update_time; +unsigned long long charge_time;int revision;int rate_now;int capacity_now;
@@ -613,6 +615,10 @@ static int acpi_battery_get_state(struct acpi_battery *battery)int result =0 ;acpi_status status =0 ;struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER ,NULL }; +union acpi_object arg0 = {ACPI_TYPE_INTEGER }; +struct acpi_object_list args = {1 , &arg0 }; + + arg0.integer.value =100 ;lockdep_assert_held (&battery->property_lock);
@@ -638,6 +644,15 @@ static int acpi_battery_get_state(struct acpi_battery *battery) battery->update_time = jiffies;kfree (buffer.pointer); +if (test_bit (ACPI_BATTERY_BCT_PRESENT , &battery->flags)) { + status =acpi_evaluate_integer (battery->device->handle,"_BCT" , + &args, &battery->charge_time); +if (ACPI_FAILURE (status)) +acpi_handle_info (battery->device->handle, +"_BCT evaluation failed: %s" , +acpi_format_exception (status)); + } +/* For buggy DSDTs that report negative 16-bit values for either * charging or discharging current and/or report 0 as 65536 * due to bad math.
@@ -1083,6 +1098,8 @@ static int acpi_battery_update(struct acpi_battery *battery, bool resume)if (result)return result;acpi_battery_init_alarm (battery); +if (acpi_has_method (battery->device->handle,"_BCT" )) +set_bit (ACPI_BATTERY_BCT_PRESENT , &battery->flags); } result =acpi_battery_get_state (battery);
Signature · 1 line
2.55.0
Rafael J. Wysocki (Intel)
maintainerRe: [PATCH v2 1/2] acpi: battery: Query Battery charge time ("_BCT") method
Quoted 23 lines · Umang Jain
Add plumbing for querying the Battery Charge Time ("
_BCT") method. If present, setACPI_BATTERY_BCT_PRESENTin battery flags to denote that the method is supported in ACPI.Signed-off-by Umang Jain <[email protected]>--- drivers/acpi/battery.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)drivers/acpi/battery.c +1
-0@@ -76,6 +76,7 @@ MODULE_DEVICE_TABLE(acpi, battery_device_ids);enum {ACPI_BATTERY_ALARM_PRESENT ,ACPI_BATTERY_XINFO_PRESENT , +ACPI_BATTERY_BCT_PRESENT ,ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY ,/* On Lenovo Thinkpad models from 2010 and 2011, the power unit * switches between mWh and mAh depending on whether the system
drivers/acpi/battery.c struct acpi_battery { · line 114
Quoted 4 lines
drivers/acpi/battery.c +1
-0@@ -110,6 +111,7 @@ struct acpi_battery {struct mutex property_lock;/* Protects properties below. */ unsigned long update_time; +unsigned long long charge_time;
This doesn't need to be unsigned long long. As per the spec, unsigned int should be sufficient.
drivers/acpi/battery.c static int acpi_battery_get_state(struct acpi_battery *battery) · line 621
Quoted 11 lines
drivers/acpi/battery.c +4
-0int revision;int rate_now;int capacity_now;@@ -613,6 +615,10 @@ static int acpi_battery_get_state(struct acpi_battery *battery)int result =0 ;acpi_status status =0 ;struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER ,NULL }; +union acpi_object arg0 = {ACPI_TYPE_INTEGER }; +struct acpi_object_list args = {1 , &arg0 }; + + arg0.integer.value =100 ;
Please move the definitions of the variables related to _BCT evaluation to the code block where it gets evaluated.
drivers/acpi/battery.c static int acpi_battery_get_state(struct acpi_battery *battery) · line 649
Quoted 10 lines
drivers/acpi/battery.c +3
-0lockdep_assert_held (&battery->property_lock);@@ -638,6 +644,15 @@ static int acpi_battery_get_state(struct acpi_battery *battery) battery->update_time = jiffies;kfree (buffer.pointer); +if (test_bit (ACPI_BATTERY_BCT_PRESENT , &battery->flags)) { + status =acpi_evaluate_integer (battery->device->handle,"_BCT" , + &args, &battery->charge_time);
Currently, you only need to evaluate _BCT to implement the quirk in the second patch AFAICS, so it is not useful to evaluate _BCT if the quirk is not going to be used. Any chance to rearrange the code to avoid evaluating _BCT when it is not needed?
drivers/acpi/battery.c static int acpi_battery_get_state(struct acpi_battery *battery) · line 653
Quoted 4 lines
drivers/acpi/battery.c +4
-0+if (ACPI_FAILURE (status)) +acpi_handle_info (battery->device->handle, +"_BCT evaluation failed: %s" , +acpi_format_exception (status));
Missing indentation.
Quoted 15 lines
drivers/acpi/battery.c +4
-1+ } +/* For buggy DSDTs that report negative 16-bit values for either * charging or discharging current and/or report 0 as 65536 * due to bad math. @@ -1083,6 +1098,8 @@ static int acpi_battery_update(struct acpi_battery *battery, bool resume)if (result)return result;acpi_battery_init_alarm (battery); +if (acpi_has_method (battery->device->handle,"_BCT" )) +set_bit (ACPI_BATTERY_BCT_PRESENT , &battery->flags); } result =acpi_battery_get_state (battery); --
Re: [PATCH v2 1/2] acpi: battery: Query Battery charge time ("_BCT") method
Hi Rafael,
Thanks for the review.
Quoted 54 lines · Rafael J. Wysocki (Intel)
Quoted 23 lines · Umang Jain
Add plumbing for querying the Battery Charge Time ("
_BCT") method. If present, setACPI_BATTERY_BCT_PRESENTin battery flags to denote that the method is supported in ACPI.Signed-off-by Umang Jain <[email protected]>--- drivers/acpi/battery.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)drivers/acpi/battery.c +1
-0@@ -76,6 +76,7 @@ MODULE_DEVICE_TABLE(acpi, battery_device_ids);enum {ACPI_BATTERY_ALARM_PRESENT ,ACPI_BATTERY_XINFO_PRESENT , +ACPI_BATTERY_BCT_PRESENT ,ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY ,/* On Lenovo Thinkpad models from 2010 and 2011, the power unit * switches between mWh and mAh depending on whether the system drivers/acpi/battery.c struct acpi_battery { · line 114
Quoted 4 lines
drivers/acpi/battery.c +1
-0@@ -110,6 +111,7 @@ struct acpi_battery {struct mutex property_lock;/* Protects properties below. */ unsigned long update_time; +unsigned long long charge_time;This doesn't need to be unsigned long long. As per the spec, unsigned int should be sufficient.
drivers/acpi/battery.c static int acpi_battery_get_state(struct acpi_battery *battery) · line 621
Quoted 11 lines
drivers/acpi/battery.c +4
-0int revision;int rate_now;int capacity_now;@@ -613,6 +615,10 @@ static int acpi_battery_get_state(struct acpi_battery *battery)int result =0 ;acpi_status status =0 ;struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER ,NULL }; +union acpi_object arg0 = {ACPI_TYPE_INTEGER }; +struct acpi_object_list args = {1 , &arg0 }; + + arg0.integer.value =100 ;Please move the definitions of the variables related to
_BCTevaluation to the code block where it gets evaluated.Quoted 7 lines
drivers/acpi/battery.c +0
-0lockdep_assert_held (&battery->property_lock);@@ -638,6 +644,15 @@ static int acpi_battery_get_state(struct acpi_battery *battery) battery->update_time = jiffies;kfree (buffer.pointer);
Quoted 8 lines
drivers/acpi/battery.c static int acpi_battery_get_state(struct acpi_battery *battery) · line 649
Quoted 3 lines
drivers/acpi/battery.c +3
-0+if (test_bit (ACPI_BATTERY_BCT_PRESENT , &battery->flags)) { + status =acpi_evaluate_integer (battery->device->handle,"_BCT" , + &args, &battery->charge_time);Currently, you only need to evaluate
_BCTto implement the quirk in the second patch AFAICS, so it is not useful to evaluate_BCTif the quirk is not going to be used. Any chance to rearrange the code to avoid evaluating_BCTwhen it is not needed?
Currently yes, _BCT is not useful in all cases, but since it is a ACPI specification component, I didn't keep it behind the quirk and read as a global param to read out.
I've re-arranged the code to evaluate _BCT if the quirk is present, but want to double sure if that's the design we need to go ahead with?
Quoted 7 lines
drivers/acpi/battery.c static int acpi_battery_get_state(struct acpi_battery *battery) · line 653
Quoted 5 lines
drivers/acpi/battery.c +4
-0+if (ACPI_FAILURE (status)) +acpi_handle_info (battery->device->handle, +"_BCT evaluation failed: %s" , +acpi_format_exception (status));Missing indentation.
Looks fine to me, what's wrong? The patches sent are sanitized with ./scripts/checkpatch.pl, unless I am missing something?
Quoted 16 lines
Quoted 16 lines
drivers/acpi/battery.c +4
-1+ } +/* For buggy DSDTs that report negative 16-bit values for either * charging or discharging current and/or report 0 as 65536 * due to bad math. @@ -1083,6 +1098,8 @@ static int acpi_battery_update(struct acpi_battery *battery, bool resume)if (result)return result;acpi_battery_init_alarm (battery); +if (acpi_has_method (battery->device->handle,"_BCT" )) +set_bit (ACPI_BATTERY_BCT_PRESENT , &battery->flags); } result =acpi_battery_get_state (battery); --
Re: [PATCH v2 1/2] acpi: battery: Query Battery charge time ("_BCT") method
In reply to Rafael J. Wysocki (Intel)Quoted 25 lines · Rafael J. Wysocki (Intel)
Quoted 24 lines · Umang Jain
Add plumbing for querying the Battery Charge Time ("
_BCT") method. If present, setACPI_BATTERY_BCT_PRESENTin battery flags to denote that the method is supported in ACPI.Signed-off-by Umang Jain <[email protected]>--- drivers/acpi/battery.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)drivers/acpi/battery.c +1
-0@@ -76,6 +76,7 @@ MODULE_DEVICE_TABLE(acpi, battery_device_ids);enum {ACPI_BATTERY_ALARM_PRESENT ,ACPI_BATTERY_XINFO_PRESENT , +ACPI_BATTERY_BCT_PRESENT ,ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY ,/* On Lenovo Thinkpad models from 2010 and 2011, the power unit * switches between mWh and mAh depending on whether the system @@ -110,6 +111,7 @@ struct acpi_battery {
Quoted 6 lines
drivers/acpi/battery.c struct acpi_battery { · line 114
Quoted 3 lines
drivers/acpi/battery.c +1
-0struct mutex property_lock;/* Protects properties below. */ unsigned long update_time; +unsigned long long charge_time;This doesn't need to be unsigned long long. As per the spec, unsigned int should be sufficient.
Last thing before I send out v3, this is the function param-type for the acpi_evaluate_integer()
In include/acpi/acpi_bus.h,
acpi_status
acpi_evaluate_integer(acpi_handle handle,
acpi_string pathname, struct acpi_object_list *arguments, unsigned long long *data);
Thanks,
Umang
Rafael J. Wysocki (Intel)
maintainerRe: [PATCH v2 1/2] acpi: battery: Query Battery charge time ("_BCT") method
Quoted 37 lines · Umang Jain
Quoted 25 lines · Rafael J. Wysocki (Intel)
Quoted 24 lines · Umang Jain
Add plumbing for querying the Battery Charge Time ("
_BCT") method. If present, setACPI_BATTERY_BCT_PRESENTin battery flags to denote that the method is supported in ACPI.Signed-off-by Umang Jain <[email protected]>--- drivers/acpi/battery.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)drivers/acpi/battery.c +1
-0@@ -76,6 +76,7 @@ MODULE_DEVICE_TABLE(acpi, battery_device_ids);enum {ACPI_BATTERY_ALARM_PRESENT ,ACPI_BATTERY_XINFO_PRESENT , +ACPI_BATTERY_BCT_PRESENT ,ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY ,/* On Lenovo Thinkpad models from 2010 and 2011, the power unit * switches between mWh and mAh depending on whether the system @@ -110,6 +111,7 @@ struct acpi_battery {Quoted 6 lines
drivers/acpi/battery.c struct acpi_battery { · line 114
Quoted 3 lines
drivers/acpi/battery.c +1
-0struct mutex property_lock;/* Protects properties below. */ unsigned long update_time; +unsigned long long charge_time;This doesn't need to be unsigned long long. As per the spec, unsigned int should be sufficient.
Last thing before I send out v3, this is the function param-type for the
acpi_evaluate_integer()
Quoted 6 lines
In
include/acpi/acpi_bus.h,
acpi_status
acpi_evaluate_integer(acpi_handle handle,acpi_string pathname, struct acpi_object_list *arguments, unsigned long long *data);
Yes, so you need an unsigned long long for calling that function, but then you can copy the value to a variable of a different type (a u32, say) if the given object is defined to return a value within the scope of that type. Of course, you should validate the returned value before doing so, but it generally needs to be validated before being used anyway.
PATCH v2 2/2 acpi: battery: Handle Valve steamdeck devices quirk
In reply to Umang JainValve steamdeck devices can report battery status as charging, even when low-powered power supply is connected to the device. The battery status is reported as charging through sysfs, even though in reality, it is discharging.
To handle this case, we introduce a battery quirk for steamdeck devices ACPI_BATTERY_QUIRK_VALVE_STEAMDECK. Additionally, it has been found that on such devices, Battery Charge Time ("_BCT") is reported accurately, hence use that information for report charging vs discharging status. Battery Charge Time("_BCT") with an eligible power supply unit will have valid values whereas with a low-powered supply unit, it shall report ACPI_BATTERY_VALUE_UNKNOWN. Use this as a differentiator to determine, whether the battery of steamdeck devices is really charging or not.
--- drivers/acpi/battery.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-)
drivers/acpi/battery.c +27 -1
@@ -95,6 +95,14 @@ enum {* on a full charge, but showing degradation in full charge cap. */ ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE , +/* +* Valve steamdeck devices can get discharged even when battery status +* is charging. This happens typically when low power supply unit is +* connected. To accurately determine this scenario, we shall read +* battery charge time ("_BCT"). This is the key differentiator to +* determine whether the steamdeck is actually charging or not. +*/ + ACPI_BATTERY_QUIRK_VALVE_STEAMDECK, }; struct acpi_battery {
@@ -236,7 +244,7 @@ static int acpi_battery_get_property(struct power_supply *psy, : POWER_SUPPLY_STATUS_DISCHARGING;return 0 ; } -else if (battery->state &ACPI_BATTERY_STATE_CHARGING ) +else if (battery->state &ACPI_BATTERY_STATE_CHARGING ) {/* Check the rate and capacity to validate the status. */ if (!acpi_battery_is_full(battery) || (battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
@@ -246,6 +254,12 @@ static int acpi_battery_get_property(struct power_supply *psy,/* Full and zero rate. */ val->intval =POWER_SUPPLY_STATUS_NOT_CHARGING ; } + +if (test_bit (ACPI_BATTERY_QUIRK_VALVE_STEAMDECK , &battery->flags) && +test_bit (ACPI_BATTERY_BCT_PRESENT , &battery->flags) && + battery->charge_time ==ACPI_BATTERY_VALUE_UNKNOWN ) + val->intval =POWER_SUPPLY_STATUS_DISCHARGING ; + }else if (battery->state &ACPI_BATTERY_STATE_CHARGE_LIMITING ) val->intval =POWER_SUPPLY_STATUS_NOT_CHARGING ;else if (acpi_battery_is_charged (battery))
@@ -1072,6 +1086,18 @@ static void acpi_battery_quirks(struct acpi_battery *battery)set_bit (ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE , &battery->flags); battery->capacity_now = battery->full_charge_capacity; } + +if (test_bit (ACPI_BATTERY_QUIRK_VALVE_STEAMDECK , &battery->flags)) +return ; + +if (dmi_name_in_vendors ("Valve" )) { +const char *name; + + name =dmi_get_system_info (DMI_PRODUCT_NAME ); +if ((name && (!strncmp ("Jupiter" , name,7 ))) || + (name && (!strncmp ("Galileo" , name,7 )))) +set_bit (ACPI_BATTERY_QUIRK_VALVE_STEAMDECK , &battery->flags); + } }static int acpi_battery_update (struct acpi_battery *battery,bool resume)
Signature · 1 line
2.55.0
Rafael J. Wysocki (Intel)
maintainerRe: [PATCH v2 2/2] acpi: battery: Handle Valve steamdeck devices quirk
Quoted 50 lines · Umang Jain
Valve steamdeck devices can report battery status as charging, even when low-powered power supply is connected to the device. The battery status is reported as charging through sysfs, even though in reality, it is discharging.
To handle this case, we introduce a battery quirk for steamdeck devices
ACPI_BATTERY_QUIRK_VALVE_STEAMDECK. Additionally, it has been found that on such devices, Battery Charge Time ("_BCT") is reported accurately, hence use that information for report charging vs discharging status. Battery ChargeTime("_BCT")with an eligible power supply unit will have valid values whereas with a low-powered supply unit, it shall reportACPI_BATTERY_VALUE_UNKNOWN. Use this as a differentiator to determine, whether the battery of steamdeck devices is really charging or not.Signed-off-by Umang Jain <[email protected]>--- drivers/acpi/battery.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-)drivers/acpi/battery.c +9
-1@@ -95,6 +95,14 @@ enum {* on a full charge, but showing degradation in full charge cap. */ ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE , +/* +* Valve steamdeck devices can get discharged even when battery status +* is charging. This happens typically when low power supply unit is +* connected. To accurately determine this scenario, we shall read +* battery charge time ("_BCT"). This is the key differentiator to +* determine whether the steamdeck is actually charging or not. +*/ + ACPI_BATTERY_QUIRK_VALVE_STEAMDECK, }; struct acpi_battery {@@ -236,7 +244,7 @@ static int acpi_battery_get_property(struct power_supply *psy, : POWER_SUPPLY_STATUS_DISCHARGING;return 0 ; } -else if (battery->state &ACPI_BATTERY_STATE_CHARGING ) +else if (battery->state &ACPI_BATTERY_STATE_CHARGING ) {/* Check the rate and capacity to validate the status. */ if (!acpi_battery_is_full(battery) || (battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
drivers/acpi/battery.c static int acpi_battery_get_property(struct power_supply *psy, · line 260
Quoted 7 lines
drivers/acpi/battery.c +4
-0@@ -246,6 +254,12 @@ static int acpi_battery_get_property(struct power_supply *psy,/* Full and zero rate. */ val->intval =POWER_SUPPLY_STATUS_NOT_CHARGING ; } + +if (test_bit (ACPI_BATTERY_QUIRK_VALVE_STEAMDECK , &battery->flags) && +test_bit (ACPI_BATTERY_BCT_PRESENT , &battery->flags) && + battery->charge_time ==ACPI_BATTERY_VALUE_UNKNOWN )
Would it be practical to evaluate _BCT here without storing charge_time in the battery object?
Quoted 25 lines
drivers/acpi/battery.c +14
-1+ val->intval =POWER_SUPPLY_STATUS_DISCHARGING ; + }else if (battery->state &ACPI_BATTERY_STATE_CHARGE_LIMITING ) val->intval =POWER_SUPPLY_STATUS_NOT_CHARGING ;else if (acpi_battery_is_charged (battery))@@ -1072,6 +1086,18 @@ static void acpi_battery_quirks(struct acpi_battery *battery)set_bit (ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE , &battery->flags); battery->capacity_now = battery->full_charge_capacity; } + +if (test_bit (ACPI_BATTERY_QUIRK_VALVE_STEAMDECK , &battery->flags)) +return ; + +if (dmi_name_in_vendors ("Valve" )) { +const char *name; + + name =dmi_get_system_info (DMI_PRODUCT_NAME ); +if ((name && (!strncmp ("Jupiter" , name,7 ))) || + (name && (!strncmp ("Galileo" , name,7 )))) +set_bit (ACPI_BATTERY_QUIRK_VALVE_STEAMDECK , &battery->flags); + } }static int acpi_battery_update (struct acpi_battery *battery,bool resume) --
Hello,
Gentle ping for some review?
Thanks,
Umang
Quoted 19 lines · Umang Jain
Short series to query Battery charge time ("
_BCT") from ACPI tables (1/2).Patch 2/2 handles the case where steamdeck devices can report "Charging" even when low power PSU is connected which is discharging the device.
Changes in v2:
- comment improvements
- split off BCT method handling into separate patchv1:
https://lore.kernel.org/all/[email protected]/Umang Jain (2):
acpi: battery: Query Battery charge time ("_BCT") method
acpi: battery: Handle Valve steamdeck devices quirkdrivers/acpi/battery.c | 45 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-)