From: Mario Limonciello <superm1@kernel.org>
To: "Rafael J . Wysocki" <rafael@kernel.org>
Cc: Len Brown <lenb@kernel.org>, Pavel Machek <pavel@ucw.cz>,
linux-acpi@vger.kernel.org (open list:ACPI),
linux-pm@vger.kernel.org (open list:HIBERNATION (aka Software
Suspend, aka swsusp)),
Mario Limonciello <mario.limonciello@amd.com>
Subject: [PATCH 2/4] ACPI: battery: Save and report battery capacity over suspend
Date: Sat, 8 Feb 2025 10:22:08 -0600 [thread overview]
Message-ID: <20250208162210.3929473-3-superm1@kernel.org> (raw)
In-Reply-To: <20250208162210.3929473-1-superm1@kernel.org>
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>
---
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-08 16:22 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 ` Mario Limonciello [this message]
2025-02-10 15:23 ` [PATCH 2/4] ACPI: battery: Save and report battery capacity over suspend Sebastian Reichel
2025-02-10 21:24 ` Mario Limonciello
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=20250208162210.3929473-3-superm1@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 \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox