From: Mario Limonciello <superm1@kernel.org>
To: Sebastian Reichel <sebastian.reichel@collabora.com>
Cc: "Rafael J . Wysocki" <rafael@kernel.org>,
Len Brown <lenb@kernel.org>, Pavel Machek <pavel@ucw.cz>,
"open list:ACPI" <linux-acpi@vger.kernel.org>,
"open list:HIBERNATION (aka Software Suspend,
aka swsusp)" <linux-pm@vger.kernel.org>,
Mario Limonciello <mario.limonciello@amd.com>
Subject: Re: [PATCH 2/4] ACPI: battery: Save and report battery capacity over suspend
Date: Mon, 10 Feb 2025 15:24:42 -0600 [thread overview]
Message-ID: <c8527274-fd84-4745-b996-d1c66694aba4@kernel.org> (raw)
In-Reply-To: <bzltxadthnef5c4xaidfcjuq7tt2h23znn76povptoxbb2iax6@xvuzfqbtomzb>
On 2/10/2025 09:23, Sebastian Reichel wrote:
> Hi,
>
> On Sat, Feb 08, 2025 at 10:22:08AM -0600, Mario Limonciello wrote:
>> From: Mario Limonciello <mario.limonciello@amd.com>
>>
>> To find out if a device is malfunctioning over suspend it's often
>> interesting to know how much battery was lost.
>>
>> At the start of the suspend cycle cache the amount of battery.
>> During resume, read the battery level again and if the battery
>> has decreased report the difference to the suspend stats using
>> pm_report_sleep_energy()
>>
>> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
>> ---
>
> This code assumes, that there is only a single battery, but there
> can be more than one battery supplying the system. For example
> Thinkpads used to have an internal battery and a user swappable
> one.
>
> Also it seems in almost all cases debugging this from userspace
> by dropping a script in /usr/lib/systemd/system-sleep is good
> enough, so I wonder if extending the kernel ABI makes sense at
> all.
>
Thanks for looking. I think it could be extended to add all the
batteries up and collectively look at how much each went down.
But that's a good point it's pretty easy to do the same thing from
userspace.
> Greetings,
>
> -- Sebastian
>
>> drivers/acpi/battery.c | 30 ++++++++++++++++++++++++++----
>> 1 file changed, 26 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/acpi/battery.c b/drivers/acpi/battery.c
>> index 6760330a8af55..f21bfd02a26d1 100644
>> --- a/drivers/acpi/battery.c
>> +++ b/drivers/acpi/battery.c
>> @@ -124,6 +124,7 @@ struct acpi_battery {
>> char oem_info[MAX_STRING_LENGTH];
>> int state;
>> int power_unit;
>> + int capacity_suspend;
>> unsigned long flags;
>> };
>>
>> @@ -1011,9 +1012,6 @@ static int acpi_battery_update(struct acpi_battery *battery, bool resume)
>> return 0;
>> }
>>
>> - if (resume)
>> - return 0;
>> -
>> if (!battery->update_time) {
>> result = acpi_battery_get_info(battery);
>> if (result)
>> @@ -1032,6 +1030,14 @@ static int acpi_battery_update(struct acpi_battery *battery, bool resume)
>> return result;
>> }
>>
>> + if (resume) {
>> + if (battery->capacity_suspend > battery->capacity_now)
>> + pm_report_sleep_energy(battery->capacity_suspend - battery->capacity_now);
>> + else
>> + pm_report_sleep_energy(0);
>> + return 0;
>> + }
>> +
>> /*
>> * Wakeup the system if battery is critical low
>> * or lower than the alarm level
>> @@ -1285,6 +1291,22 @@ static void acpi_battery_remove(struct acpi_device *device)
>> }
>>
>> /* this is needed to learn about changes made in suspended state */
>> +static int acpi_battery_suspend(struct device *dev)
>> +{
>> + struct acpi_battery *battery;
>> +
>> + if (!dev)
>> + return -EINVAL;
>> +
>> + battery = acpi_driver_data(to_acpi_device(dev));
>> + if (!battery)
>> + return -EINVAL;
>> +
>> + battery->capacity_suspend = battery->capacity_now;
>> +
>> + return 0;
>> +}
>> +
>> static int acpi_battery_resume(struct device *dev)
>> {
>> struct acpi_battery *battery;
>> @@ -1301,7 +1323,7 @@ static int acpi_battery_resume(struct device *dev)
>> return 0;
>> }
>>
>> -static DEFINE_SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
>> +static DEFINE_SIMPLE_DEV_PM_OPS(acpi_battery_pm, acpi_battery_suspend, acpi_battery_resume);
>>
>> static struct acpi_driver acpi_battery_driver = {
>> .name = "battery",
>> --
>> 2.43.0
>>
>>
next prev parent reply other threads:[~2025-02-10 21:24 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-08 16:22 [PATCH 0/4] Improvements to ACPI battery handling over s2idle Mario Limonciello
2025-02-08 16:22 ` [PATCH 1/4] PM: Add sysfs file for energy consumed over sleep cycle Mario Limonciello
2025-02-08 16:22 ` [PATCH 2/4] ACPI: battery: Save and report battery capacity over suspend Mario Limonciello
2025-02-10 15:23 ` Sebastian Reichel
2025-02-10 21:24 ` Mario Limonciello [this message]
2025-02-08 16:22 ` [PATCH 3/4] ACPI: battery: Refactor wakeup reasons in acpi_battery_update() Mario Limonciello
2025-02-08 16:22 ` [PATCH 4/4] ACPI: battery: Wake system on AC plug or unplug in over s2idle Mario Limonciello
2025-02-08 17:59 ` Rafael J. Wysocki
2025-02-09 13:14 ` Mario Limonciello
2025-02-12 13:49 ` Armin Wolf
2025-02-12 13:57 ` Rafael J. Wysocki
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c8527274-fd84-4745-b996-d1c66694aba4@kernel.org \
--to=superm1@kernel.org \
--cc=lenb@kernel.org \
--cc=linux-acpi@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=mario.limonciello@amd.com \
--cc=pavel@ucw.cz \
--cc=rafael@kernel.org \
--cc=sebastian.reichel@collabora.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.