mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] power: supply: qcom_battmgr: treat a start threshold of 0 as none
@ 2026-11-28  1:24 turbinebmw
  0 siblings, 0 replies; only message in thread
From: turbinebmw @ 2026-11-28  1:24 UTC (permalink / raw)
  To: Sebastian Reichel, linux-pm
  Cc: linux-arm-msm, linux-kernel, Brandon Williams, Val Packett,
	Fenglin Wu, Konrad Dybcio

From: Brandon Williams <turbine.bmw@gmail.com>

UPower turns a charge limit off by writing 0 to
charge_control_start_threshold and 100 to charge_control_end_threshold.
Since commit 8809980fdc8a ("power: supply: qcom_battmgr: clamp charge
control thresholds") the driver raises the 0 to
CHARGE_CTRL_START_THR_MIN, so the firmware is told to charge to 100%
but resume only below 50%. A battery that the limit had stopped at its
end threshold, say 80%, then stays at "Not charging" until it drains
below 50%, and turning the limit off appears to do nothing.

Disabling charge control through the request's enable bit was tried
and reverted, as the firmware did not behave as expected with it.
Instead, take a start threshold of 0 to mean that there is none: keep
the end threshold and use the smallest step, CHARGE_CTRL_DELTA_SOC, so
that charging resumes as soon as the level drops below it. The start
sent to the firmware stays within [50, 95], as the end threshold is at
least 55. The start is remembered as 0, so a following end threshold
write keeps the small step, whichever order userspace writes them in.

Tested on a Microsoft Surface Pro 11 (X1E80100) with UPower 1.91.5: with
the battery held at 80% on a 75-80 window, turning the limit off set
95-100 and the battery charged at once; turning it back on restored
75-80.

Link: https://gitlab.freedesktop.org/upower/upower/-/issues/327
Fixes: 8809980fdc8a ("power: supply: qcom_battmgr: clamp charge control thresholds")
Cc: Val Packett <val@packett.cool>
Cc: Fenglin Wu <fenglin.wu@oss.qualcomm.com>
Cc: Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Brandon Williams <turbine.bmw@gmail.com>
---
Based on sre/linux-power-supply for-next. Hardware-tested as a backport
to 7.3.6 (the same logic laid out with a goto, on top of a local change
that returns the request status like ffce9bb777f2); a userspace harness
running both versions' setters over 120000 random threshold writes gave
identical firmware requests and sysfs values. Compile-tested with W=1.

This keeps the 50% floor Fenglin explained in the thread on Val's series
and does not use the enable bit, whose removal is 8e8856396b54.

 drivers/power/supply/qcom_battmgr.c | 43 +++++++++++++++++++----------
 1 file changed, 29 insertions(+), 14 deletions(-)

diff --git a/drivers/power/supply/qcom_battmgr.c b/drivers/power/supply/qcom_battmgr.c
index 0789303..45bd1ff 100644
--- a/drivers/power/supply/qcom_battmgr.c
+++ b/drivers/power/supply/qcom_battmgr.c
@@ -762,21 +762,36 @@ static int qcom_battmgr_set_charge_start_threshold(struct qcom_battmgr *battmgr,
 	u32 target_soc, delta_soc;
 	int ret;
 
-	start_soc = clamp(start_soc, CHARGE_CTRL_START_THR_MIN, CHARGE_CTRL_START_THR_MAX);
-
-	/*
-	 * If the new start threshold is larger than the old end threshold,
-	 * move the end threshold one step (DELTA_SOC) after the new start
-	 * threshold.
-	 */
-	if (start_soc > battmgr->info.charge_ctrl_end) {
-		target_soc = start_soc + CHARGE_CTRL_DELTA_SOC;
-		target_soc = min_t(u32, target_soc, CHARGE_CTRL_END_THR_MAX);
-		delta_soc = target_soc - start_soc;
-		delta_soc = min_t(u32, delta_soc, CHARGE_CTRL_DELTA_SOC);
+	if (start_soc <= 0) {
+		/*
+		 * A start threshold of 0 means there is none: charging resumes
+		 * as soon as the level drops below the end threshold. Userspace
+		 * writes it, with an end threshold of 100, to turn the limit
+		 * off. Use the smallest step below the end threshold, which
+		 * keeps the start within the firmware's range, and remember 0
+		 * so that a following end threshold write keeps that step.
+		 */
+		start_soc = 0;
+		target_soc = battmgr->info.charge_ctrl_end ?: CHARGE_CTRL_END_THR_MAX;
+		delta_soc = CHARGE_CTRL_DELTA_SOC;
 	} else {
-		target_soc =  battmgr->info.charge_ctrl_end;
-		delta_soc = battmgr->info.charge_ctrl_end - start_soc;
+		start_soc = clamp(start_soc, CHARGE_CTRL_START_THR_MIN,
+				  CHARGE_CTRL_START_THR_MAX);
+
+		/*
+		 * If the new start threshold is larger than the old end
+		 * threshold, move the end threshold one step (DELTA_SOC)
+		 * after the new start threshold.
+		 */
+		if (start_soc > battmgr->info.charge_ctrl_end) {
+			target_soc = start_soc + CHARGE_CTRL_DELTA_SOC;
+			target_soc = min_t(u32, target_soc, CHARGE_CTRL_END_THR_MAX);
+			delta_soc = target_soc - start_soc;
+			delta_soc = min_t(u32, delta_soc, CHARGE_CTRL_DELTA_SOC);
+		} else {
+			target_soc =  battmgr->info.charge_ctrl_end;
+			delta_soc = battmgr->info.charge_ctrl_end - start_soc;
+		}
 	}
 
 	mutex_lock(&battmgr->lock);
base-commit: 4f9bf356cbfb8aa6e73b1d05e182445723a4bb49
-- 
2.56.1


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-28  1:24 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-28  1:24 [PATCH] power: supply: qcom_battmgr: treat a start threshold of 0 as none turbinebmw

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®