Skip to content

power: supply: max77759: charge thresholds, read-back and safer limits - #266

Open
Somnius wants to merge 5 commits into
derekross:omamobilefrom
Somnius:husky-charger-thresholds-1116
Open

Somnius wants to merge 5 commits into
derekross:omamobilefrom
Somnius:husky-charger-thresholds-1116

Conversation

@Somnius

@Somnius Somnius commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

The two husky commits (the Pixel 8 Pro charge defender and the husky float restore) moved to #277, where they wait for the gauge temperature fix. This PR has only the five shared commits.

@derekross second of three for the 92 % stop on my Pixel 8 Pro, on top of #265 (its commit is the first one here, so review that one there).

Three commits:

  • the fast-charge current now rounds down, so the driver never sets more than the device tree asks. Husky's 1485000 used to land on 1533400 uA, now 1466730. Cheetah and raven ask for 1466730, which is an exact step, so they don't change. Akita goes from 1333390 to 1266720 uA, which is the one change on another board.
  • 0xb9 to 0xbd are volatile in the MFD, so reads of mode, current and float come from the chip and not the regmap cache. Own commit because it touches the 7 Pro and 6 Pro too.
  • charge_control_end_threshold and charge_control_start_threshold (100 / 0 by default, which does nothing), a husky only charge_defender parameter like stock's (6 h plugged at 4.27 V and 35 C or more, then hold at 79-80 % until unplug, off by default), a read-back of float and current after every guard write that stops charging on a mismatch, and on husky the bootloader's float put back on unbind and shutdown, only ever downwards.

Tested: 28 KUnit cases pass under UML, the charger and MFD objects build for arm64 with W=1 and no warnings, checkpatch --strict is clean on all three, and kernel-pr-check is all ok with the fix from derekross/omarchy-mobile#851.

On the phone today, with this plus the third PR (float 4.45 V on husky) built into a test kernel, the charger loaded by hand with temp_guard=1 charge_defender=1 default_ilim_ua=500000, attended and with a guard script on the phone:

  • the end threshold stopped charging in 18 to 27 s (mode 4), and setting it back to 100 resumed it
  • unbinding put the float back from 4.45 V to 4.35 V (0xbd 0x2d to 0x23), rebinding raised it again
  • 90 % to 100 % on my desktop's USB port in about 38 minutes (18:41 to 19:18), battery at most 38.7 C. The SoC zones touched 60 C for about a minute around 19:02 with the screen on; my guard script only aborted above 60 C, I've changed it to abort at 60 C. A second run reached the charger's own DONE (0xb6 0xb4) at 4.43 V, 20 mV under the float.

charge runs at 4.45 V

Not tested: suspend while plugged in (the guard is delayed work and pauses in s2idle while the chip keeps charging, so for now I don't leave it sleeping on the cable), the defender actually firing (needs 6 h), and anything on a 7 Pro.

Questions for you:

  1. With temp_guard off by default, the next PR's device tree change alone would charge husky straight to 4.45 V with no steps. Keep the switches off in the driver and turn them on from the husky package like cheetah's charger conf, or keep the husky float at 4.20 V in the device tree and let the guard raise it?
  2. Volatile registers in the MFD, or a local cache bypass in the charger?
  3. Start 0 resumes at end - 5. The sysfs docs read as if start 0 means never resume while plugged. Which do you want?
  4. Is the read-back stopping charging on a mismatch fine on every board, or husky only?

This merges cleanly with #261 (checked with a test merge). Husky boots with 0xc2 = 0x13, NO_AUTOIBUS clear, so the charger already picks its own input current there; that fits what I saw, the battery took up to about 470 mA on top of the phone's own load with default_ilim_ua=500000.

The husky package still blacklists max77759_charger, so none of this runs on a stock install until that changes. I'll send that after the kernel side is in, since on today's kernel unblocking it would set the float to 4.20 V.

@derekross

Copy link
Copy Markdown
Owner

Thank you, @Somnius. This is careful work: the two inhibits kept apart, the restore that only ever lowers, the devm order, the tests on an array-backed regmap, and above all the plain list of what you didn't test. I checked the rounding against Google's driver (max77759_set_charger_current_max_ua() in kernel/google-modules/bms rounds down too) and the defender numbers against zuma-shusky-charging.dtsi: both match. Every commit builds here with and without the KUnit option at W=1. The red check is the gate building the test file on its own, as on #265.

It can't go in as it is, for these reasons:

  1. It does change the Pixel 7 Pro, the 6 Pro and the Pixel 7. Not at probe, where their 1466730 uA is an exact step, but under the temperature guard, which those phones run with temp_guard=1:

    • the rounding: at 0-10 C between 4.20 and 4.30 V the table asks for 10 % of the capacity, 492.6 mA on the 7 Pro, which was written as 533 mA and becomes 467 mA (on the Pixel 7, 467 becomes 400 mA). That's a fix the 7 Pro needs too, since stock rounds down there, so please say so in the message instead of "nothing changes there";
    • the volatile registers and the read-back: every guard run now talks to the chip, and a mismatch or a failed read stops charging where a failed write used to be ignored;
    • the new .shutdown disables the guard work on every board.

    None of that is wrong. But a change to what the 7 Pro does has to say so and has to run there before it merges, so I'll run the shared parts on mine: a charge from under 80 % with the guard on, a threshold stop and resume, a sleep on the charger, unplug and replug, a reboot.

  2. Please split the last commit into four: the thresholds, the read-back, the defender, and the float restore with .shutdown. Two of them reach every phone and two only yours, and I'd like to test and land them apart.

  3. The messages need the test. They land; the description doesn't. For each commit: what ran, on which phone and uname -r, and your "not tested" list as it stands. And the runs should be of this branch's head on the published base, with the guard switched on by the module parameters, rather than the stack plus a local commit on your own kernel.

  4. The limit doesn't hold while the phone sleeps. You say it yourself: the guard is delayed work, s2idle stops it, the chip carries on to the float. A phone on the charger overnight is the case a charge limit is for, and it's asleep. I need to decide how we handle that before omarchy-mobile#860 offers the limit to anyone: keep the phone awake on the cable as you do, wake for it, or say on the page that it only holds while the phone is awake. Please put the limitation in the commit message and the parameter text now. Note also that with at most 60 s counted per run, time asleep never counts toward the defender's six hours.

Your questions:

  1. Where the switches go: on max77759: husky DONE check, and the float raised only while the guard runs #267.
  2. Volatile in the MFD: yes, that's the right place. A bypass toggled around one read would race with everyone else on that regmap.
  3. Start 0: keep resuming at end - 5, but let charge_control_start_threshold read back the value in force (75 for an end of 80), so the file doesn't say 0 while the driver does something else.
  4. Read-back on every board: yes, every board, after the 7 Pro run.

Smaller things:

  • While a threshold or the guard holds the charge, status reads Discharging with the cable in (the chip is in buck-only mode). Not charging is the ABI's word for it, and the Battery page will want it.
  • The float still rounds up in set_float_voltage_limit() and fv_quantize(). Every value in use is a multiple of 10 mV, so nothing changes today, but "never above the request" should hold for the float too. Google's driver rounds it down.
  • .shutdown leaves a threshold stop in place (buck only) until the bootloader rewrites the mode. Worth a sentence.
  • Please cite zuma-shusky-charging.dtsi for the defender, and say that stock's resume conditions (bd-resume-*) are replaced here by "until unplugged".

Two things from omarchy-mobile#761 that bear on this:

  • gitColinX's 8 Pro on the published kernel read temp at 22.0 C in every one of 2450 samples, and its percent comes from the default gauge model. The defender's 35 C, the guard and the thresholds' percent all read that supply, so on a published kernel I can't yet tell what the husky parts would do. You wrote that you'd look at where the value comes from before leaning on it; I'd like that answer before any husky-only part merges. More on max77759: husky DONE check, and the float raised only while the guard runs #267.
  • You wrote that nothing would run at a raised float before the dead-man limits were reviewed, and then that you didn't wait. Thank you for saying it straight. Please do wait from here: no more runs at a raised float until I've been through the limits with you on omarchy-mobile#761.

The Pixel 7 is about to start running this guard (#259 and omarchy-mobile#827, #788), so I'll keep this out of the kernel its first attended run uses, or make sure it's in it: one change at a time there.

The temperature guard takes its voltage tiers and its charge currents
from three constant arrays, the step-charging table of the gs201 phones.
The Pixel 8 Pro (husky) has another battery and another table: four
tiers, 4.20/4.30/4.35/4.48 V, instead of three.

Put the tiers and the currents in a struct guard_table, with the number
of tiers in use, and let the driver pick the table at probe. The default
table holds the numbers the arrays held, so nothing changes for the
Pixel 7 Pro, the Pixel 6 Pro and every other board. A machine compatible
with "google,zuma-husky" gets the husky table: the chg-cv-limits and
chg-cc-limits of the google,battery node in Google's tree
(kernel/devices/google/shusky, branch android-gs-shusky-6.1-android16):
dts/zuma-shusky-battery.dtsi for the temperature and voltage limits,
dts/zuma-husky-battery.dtsi for the 4950 mAh and the six rows, in
percent of that design capacity. The temperature bands are the same in
both and stay as they are.

The tier and limit arithmetic of the guard work moves into two functions
without side effects, guard_next_tier() and guard_limits(), so that it
can be tested. The limits from the battery description still cap the
float voltage and the charge current, and temp_guard is still off by
default.

With the husky battery node as it is today, 4.20 V and 1.485 A, the
float stays 4.20 V in every band and the current 1.485 A, the lowest
row of the husky table being 30 % of 4950 mAh. One case differs from
the default table on that board: from 0 to 10 C with the battery at
4.20 V or more the current limit was 495 mA and is 1.485 A now, as the
Google table has it.

With the battery node's 1.485 A cap, every cell of the husky table
that charges comes out at the cap, so the current does not change from
band to band. What the table changes is the float per band (4.30 V at
0-10 C and 46-48 C, 4.20 V at 48-55 C), which only shows once the
battery node asks for more than 4.20 V, and the one cold cell above.

The same Google file also ages the table: google,aact-cc-limits lowers
the 20-42 C row with use, and google,aafv-ref-offset lowers the float
by up to 100 mV with cycles. That is not done here; it doesn't matter
at 4.20 V and 0.3 C.

Add KUnit tests, built into the driver with
CHARGER_MAX77759_KUNIT_TEST: each band edge, the hysteresis, the tier
steps of both tables, both tables against their numbers, and the
limits with and without the caps of the battery description. They pass
under UML (10 of 10), and the driver builds and links as an arm64
module, W=1 clean, with and without the tests.

The MAINTAINERS entry of the driver now matches the test file too.

Not run on a phone, neither a Pixel 8 Pro nor a Pixel 7 Pro. The choice
of the table by the machine compatible is not covered by the tests.

Signed-off-by: Lef <me@lefteros.com>
@Somnius
Somnius force-pushed the husky-charger-thresholds-1116 branch from bf42640 to c4d94a1 Compare November 18, 2026 08:52
@Somnius

Somnius commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

@derekross thanks for the careful read. Reworked and force-pushed (head c4d94a1), on #265's new head:

The last commit is split as you asked, so the series is now: the current/float rounding, the thresholds, the MFD volatile registers, the read-back, the defender, and the float restore with .shutdown. Every commit builds on its own at W=1 with and without the KUnit option, KUnit passes at each step (10 up to 36 cases), checkpatch --strict is clean, and tools/kernel-pr-check --merge passes in the build container.

From your list:

  • The 7 Pro, 6 Pro and Pixel 7 changes are in the messages now: the rounding (492.6 mA asked, 533 before, 467 now on the 7 Pro, 467 to 400 on the Pixel 7, and stock rounds down too), the read-back where a mismatch or a failed read now stops charging, .shutdown disabling the guard on every board, and that a failed mode write now rolls the inhibit back and retries next run (the old code kept the flag and never retried).
  • The sleep limit is in the thresholds and defender messages and in the temp_guard and charge_defender parameter text, including that time asleep never counts toward the six hours.
  • charge_control_start_threshold reads back the value in force (75 for an end of 80). While the defender is latched both files read 80 and 79, so the files never say something the driver isn't doing.
  • With a threshold or the guard holding and CHGIN valid, status reads Not charging. With no input it reads Discharging.
  • The float rounds down in set_float_voltage_limit() and fv_quantize() through one helper.
  • .shutdown leaving a threshold stop in place until the bootloader rewrites the mode has its sentence.
  • The defender cites zuma-shusky-charging.dtsi and says stock's bd-resume-* is replaced by "until unplugged". On husky the psy work never runs, so the guard run checks CHGIN for the unplug; an unplug and replug that both fall between two guard runs (30 s) is missed, and the message says so. It also says plainly that it stops charging at 80 % and resumes at 79 %, it doesn't drain.
  • The float restore: if the restore or its first read fails, it logs and leaves charging stopped instead of clearing the stop on a raised float. If the boot float can't be read, husky falls back to 4350 mV.

Two things I found reading Google's side that you may want to know: zuma-shusky-charging.dtsi sets google,bd-temp-dry-run, and in the google_charger.c I have, dry run returns before charging is turned off, so stock may only count unless Android clears it at run time. I don't know whether it does. The defender message says this.

What's still open, honestly: each message's "Tested" part still describes my runs on the earlier one-commit form plus a local test commit, not this head on the published base with the guard switched on by the module parameters. Those re-runs are next. On #266 alone the husky float stays at 4.20 V, so the defender can't latch on the phone and the float restore has nothing to restore; both are KUnit-only for now, and the messages say so. Nothing will run at a raised float until we've been through the limits on #761.

@Somnius
Somnius force-pushed the husky-charger-thresholds-1116 branch from c4d94a1 to 9c1af39 Compare November 18, 2026 10:13
@Somnius

Somnius commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

@derekross the re-run on this head, and the results are now in each commit's message (head 9c1af39, code unchanged from c4d94a1, only the Tested parts).

My 8 Pro, slot B, kernel 7.3.0-rc6-omamobile-push1-gc4d94a1c1fe5: this branch's head on the published base with nothing on top, the guard on through the module options (temp_guard=1 charge_defender=1 default_ilim_ua=500000), on a 500 mA desktop port.

  • probe: found: mode 0x5, float voltage 4350 mV, fast charge 1933420 uA, then probed: ... float voltage 4200 mV, fast charge 1466730 uA and the band line battery 38.6 C, 4308 mV: band 20-42 C, float 4200 mV, charge 1485000 uA
  • the saved limit (90) came back at boot, and 36 s in: charge stopped at 90 %, status Not charging with the cable in, the start threshold reading 85
  • unplug: online 0, status Discharging. Replug: Not charging again, held at 89 % since it resumes at 85
  • end to 100: start read 0, charge resumed: no limit, Charging. End back to 90 at 89 %: no stop, since it stops at 90
  • one odd reading worth knowing: status said Full at 89 %, because the battery sat at 4.26 V, above this base's 4.20 V float
  • systemctl reboot: the next boot found the bootloader's mode 0x5 and 4350 mV again, so a threshold stop doesn't survive a reboot, and the driver set 4200 mV and the saved 90 again
  • tools/phone-smoke on that boot: every required check ok, no sound card (expected, the published base has no AoC)

Not run, and the messages say so: the defender latch (4.20 V never reaches its 4.27 V), the float restore actually lowering something, a read-back mismatch on hardware, sleep on the cable, poweroff (the base doesn't have the S2MPG14 rtc fix yet, so reboots only), and of course nothing on a 7 Pro. Logs (30 s CSV, dmesg) are on my side if you want them attached.

@derekross

Copy link
Copy Markdown
Owner

Thank you, @Somnius. This is the rework I asked for, point by point: the split into four, the messages saying what changes on the 7 Pro, 6 Pro and Pixel 7, the "Tested" and "Not tested" parts, and a run of this branch's own tree on the published base with the guard switched on by the module options (I checked that c4d94a1 and today's head are the same tree). The restore-then-clear order on unbind and the stop that stays when the float can't be put back are right. Every commit builds here at W=1 with and without the tests.

One change, in the rounding commit:

  • A charge current above the top step is now written as the top step, 4.0 A, where the driver used to return -EINVAL. Your message says so, but look at where such a value comes from: a battery node without constant-charge-current-max-microamp leaves the field at -EINVAL, max77759_charger_init() copies it into a u32, and what used to be a failed probe becomes a 4 A fast-charge current with no cap for the guard either. No board on the branch lacks the property today, so nothing changes on a phone now, but the next board file would get no warning. You already refuse a float above the highest step "rather than clamping it"; please do the same for the current in set_fast_charge_current_limit() and cc_quantize() (-EINVAL above the last range's maximum), with a test for 4000190, 4000191 and (u32)-EINVAL.

Smaller:

  • When the read-back fails, the values the chip held are only in a dev_dbg. Please put them in the one dev_err; on the first phone where it fires they are what we'll want.
  • "this head" in the messages will mean nothing after a rebase merge. "the head of this series (linux-omarchy-mobile#266)" works from any commit.

What happens next is on my side:

  1. The red check is the gate again (omarchy-mobile#851).
  2. I'll run the shared commits on my 7 Pro: a charge from under 80 % with the guard on, a threshold stop and resume, unplug and replug, a sleep on the charger, a reboot.
  3. I still owe you the decision on a limit that doesn't hold while the phone sleeps. It comes before omarchy-mobile#860.
  4. The defender and the float restore wait for the gauge temperature (your notes on max77759: husky DONE check, and the float raised only while the guard runs #267). On a published kernel the defender would read 22.0 C for ever and never count. If you'd like the five shared commits to go in first, split the two husky ones into their own PR; otherwise the series waits as one.

About your note on omarchy-mobile#761 this morning: thank you for catching it and saying so within the hour. It wasn't a decision this time but a leftover kernel, which is the kind of thing a rule alone doesn't stop. Two requests. Until your daily kernel is rebuilt, read constant_charge_voltage_max before the cable goes in. And when you next build a test kernel with the float raised, make it one that can't be mistaken for the daily one (its own LOCALVERSION, and the float raised only by an option you set by hand, not by its device tree). The sleep you ran for omarchy-mobile#836 was inside that window, with your sleep blocker stopped, so for those two minutes the chip was charging toward 4.45 V with the guard paused. I'll go through the limits with you on #761 next; that review is overdue on my side.

@Somnius

Somnius commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

@derekross thanks, and point taken on the two minutes of sleep for #836, I hadn't connected that to the float.

The daily kernel is rebuilt without the raised float: 7.3.0-rc6-omamobile-push1-g0124dfcad569 (the same tree minus the 4.45 V commit). constant_charge_voltage_max reads 4200000 on the phone right now, and I'll keep reading it before the cable goes in. From now on a test kernel with a raised float gets its own LOCALVERSION and the float only through an option I set by hand, never from its device tree.

I'll do the current clamp (-EINVAL above the last range, with tests for 4000190, 4000191 and (u32)-EINVAL), the values in the one dev_err, and the "this head" wording next, and post here when it's pushed.

@Somnius
Somnius force-pushed the husky-charger-thresholds-1116 branch from 9c1af39 to 1244cde Compare November 18, 2026 16:47
@Somnius

Somnius commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

@derekross pushed as 1244cde (force-push, same base 0d68188c3):

  • Current above the top step: refused again, in the rounding commit (3987919). A new cc_selector(), like the float's fv_selector(), returns -EINVAL above the last range's maximum (4000190 uA) or below the first step. set_fast_charge_current_limit() uses it there, and cc_quantize() uses it from the read-back commit where it first exists. round_test_current checks 1485000 (0x16), 4000190 (0x3c), 4000191, (u32)-EINVAL and 133329. Without the max check the selector helper returns the top selector for anything above, so 4000191 and (u32)-EINVAL would both have become 4 A. The message now says so.
  • The one dev_err: it now prints what the chip holds: "chip did not take float %u mV, charge %u uA (%d), it holds %d mV, %d uA: charging inhibited". The dev_dbg is gone. charger_apply_limits() hands the read-back values out, each -1 when it wasn't read, and readback_test_mismatch checks them.
  • "this head": every message now says "an earlier head of this series (linux-omarchy-mobile#266)". It also says that the -EINVAL above the top step and the values in the dev_err came after the phone run and are covered by KUnit only.

KUnit under UML: 37 of 37 pass. tools/kernel-pr-check --build from today's main: sign-offs, checkpatch --strict (0 warnings), policy, and W=1 builds all ok, with the test file the SKIP that #851 added. No phone run on this head yet.

@derekross derekross left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @Somnius. All three are done as asked, and I checked each in the code. cc_selector() refuses a current above 4000190 uA or below the first step, max77759_charger_init() passes the error on, so a battery node without the property is a failed probe again; the test covers 4000190, 4000191 and (u32)-EINVAL, through charger_apply_limits() too. The dev_err now says what the chip holds. The messages name the series. CI is green, and since its W=1 step skips the test file I built every commit here with and without the KUnit option: 14 builds, no warnings. I haven't run the KUnit suite myself.

Thank you also for rebuilding the daily kernel and for the habit of reading the float before the cable goes in.

One small thing for whenever you next push, not worth a push on its own: in the rounding commit's "Tested" part the sentence about the earlier head breaks in mid-phrase ("… which KUnit covers, on the published / base, nothing else on top") and reads as if KUnit ran on the published base.

Nothing else is open on your side for the five shared commits. What's left is mine:

  1. The run on my 7 Pro: a charge from under 80 % with the guard on, a threshold stop and resume, unplug and replug, a sleep on the charger, a reboot.
  2. The decision on a limit that doesn't hold while the phone sleeps. Your omarchy-mobile#899 gives it a concrete form for husky; I've said there which way I lean.
  3. The defender and the float restore still wait for the gauge temperature. If you'd like the five shared commits in first, move those two to their own PR; otherwise the series waits as one.

I saw your question on omarchy-mobile#761 about 4.20 V against the bootloader's 4.35 V. I'll answer it there together with the limits.

@derekross

Copy link
Copy Markdown
Owner

Decided on point 3: please split it. The five shared commits stay here and can go in after my 7 Pro run; the defender and the float restore move to a PR of their own and wait there for the gauge temperature fix. On point 2, keeping the phone awake on the cable (omarchy-mobile#899) is the 8 Pro's answer for now.

set_fast_charge_current_limit() and set_float_voltage_limit() pick the
step at or above the requested value
(linear_range_get_selector_high_array()), so a request between two
steps is written as the next step up. On husky the battery node's
1485000 uA becomes 1533400 uA, 48 mA above the battery's limit.

Take the step at or below the request instead, for both, as Google's
driver does (max77759_set_charger_current_max_ua() in
google-modules/bms rounds down too).

The current, at probe: cheetah and raven ask for 1466730 uA, an exact
step, so probe writes the same value as before. akita's 1320000 uA goes
from 1333390 uA to 1266720 uA. husky's 1485000 uA becomes 1466730 uA.
A request above the highest step (4000190 uA) or below the lowest is
still -EINVAL, through a new cc_selector() like the float's: a battery
node without constant-charge-current-max-microamp leaves -EINVAL in the
field, and max77759_charger_init() copies it into a u32, which must stay
a failed probe and not become a 4 A current with no cap.

The current, under the temperature guard (temp_guard=1), this changes
the Pixel 7 Pro, the 6 Pro and the Pixel 7 too: every guard current
that falls between two steps is now one step lower. At 0-10 C between
4.20 and 4.30 V the default table asks for 10 % of the capacity, 492.6
mA on the 7 Pro, which was written as 533 mA and is now 467 mA (the 6
Pro's 490.5 mA the same); on the Pixel 7, 427.7 mA was 467 mA and is now
400 mA. Stock rounds down there, so this is a fix for those phones as
well.

The float: a new fv_selector() takes the step at or below the request,
3900 mV between the two ranges, and still refuses a request below the
lowest step or above the highest (4500 mV) with -EINVAL rather than
clamping it. Every float in use (4200, 4300, 4450 mV and the guard
table's) is a multiple of 10 mV, an exact step, so no written float
changes today on any board.

Tested: KUnit under UML (new cases for the float steps, the range gap
and the limits, and for the current: 1485000, 4000190, 4000191 and
(u32)-EINVAL); arm64 objects at W=1 with and without the KUnit option.
On a Pixel 8 Pro, 2026-11-18, slot B, kernel
7.3.0-rc6-omamobile-push1-gc4d94a1c1fe5 (an earlier head of this series,
linux-omarchy-mobile#266, which still had the charge defender and the
husky float restore, now in their own PR, before the -EINVAL above the
top current step and the read-back values in the dev_err, which KUnit
covers, on the published base, nothing else on top; the charger options
from /etc/modprobe.d, temp_guard=1 charge_defender=1
default_ilim_ua=500000; a 500 mA desktop USB port):
the probe logged "fast charge 1466730 uA" for husky's 1485000 uA (the
bootloader had left 1933420 uA). The float written at probe, 4200 mV, is
an exact step, so the float rounding is covered by KUnit only.

Not tested: anything on a Pixel 7 Pro, 6 Pro, Pixel 7 or akita; a guard
current between two steps on hardware.

Signed-off-by: Lef <me@lefteros.com>
Add charge_control_end_threshold and charge_control_start_threshold to
the charger. At 100 / 0, the default, they do nothing and the guard work
reads and writes nothing more than before. With a lower end, charging
stops (buck only, as the temperature guard does) once the battery's
capacity reaches it, and resumes at the start threshold, or 5 % below
the end when start is 0 or not below the end.

Each threshold is checked on its own, so the order of two writes does
not matter: an end at or below the start sets the start back to 0.
charge_control_start_threshold reads back the value in force, so with
an end of 80 and a start of 0 it reads 75, the level charging resumes
at. With the end at 100 it reads the stored start.

The two inhibits, the temperature guard's and the threshold's, are kept
apart so that neither clears the other. The thresholds run first in each
guard run, so a stop is in place before the guard clears its own
inhibit, and a flag only changes once the mode write has worked, so a
failed write is tried again at the next run. The thresholds are checked
by the guard work, at most 30 s after a change in capacity. On unbind,
after the guard work is cancelled, a threshold stop is cleared before
charging is left on.

While either inhibit holds the charge with the cable in (buck only, the
chip reports CHG_DTLS off and CHGIN valid), status now reads Not
charging instead of Discharging. With no input it reads Discharging,
whatever the flags and the last mode written. This reaches the Pixel 7
Pro, the 6 Pro and the Pixel 7 too when they run with temp_guard=1:
while the temperature guard inhibits charging with the cable in, their
status changes from Discharging to Not charging. One more change for
them with temp_guard=1: the guard's inhibit flag is now rolled back when
its mode write fails, and the write is tried again at the next run,
where before the flag stayed set with the chip still charging.

The guard work is delayed work and s2idle stops it, so the thresholds
are only checked while the phone is awake: asleep on the charger, the
chip carries on to the float, and a stop already in place stays in
place. temp_guard's description now says the guard work only runs while
the phone is awake.

The rules are side-effect free functions with KUnit tests; the two
inhibits, the status and the stop and resume on a fake capacity are
tested on a regmap backed by an array, and the tests read their own
fake battery and register their charger supply under a test name, so
they leave guard_psy and the real max77759-charger alone.

Tested: KUnit under UML; arm64 objects at W=1 with and without the KUnit
option.
On a Pixel 8 Pro, 2026-11-18, slot B, kernel
7.3.0-rc6-omamobile-push1-gc4d94a1c1fe5 (an earlier head of this series,
linux-omarchy-mobile#266, which still had the charge defender and the
husky float restore, now in their own PR, before the -EINVAL above the
top current step and the read-back values in the dev_err, which KUnit
covers, on the published base, nothing else on top; the charger options
from /etc/modprobe.d, temp_guard=1 charge_defender=1
default_ilim_ua=500000; a 500 mA desktop USB port):
an end of 90, saved by the Settings unit and applied at boot, stopped
charging 36 s after boot ("charge stopped at 90 %"), status read Not
charging with the cable in and the start read back 85. Unplugged,
online read 0 and status Discharging; plugged back in at 89 %, status
stayed Not charging (resume at 85 % or less). An end of 100 read the
start back as 0, logged "charge resumed: no limit" and status read
Charging; an end of 90 again at 89 % did not stop it (the stop is at
90 % or more) and the start read 85. After a reboot the bootloader had
put the charger mode back (a threshold stop does not survive a reboot)
and the saved end of 90 and start of 85 came back. Status read Full at
89 % once, with the battery at 4.26 V above the 4.20 V float.

Not tested: a stop that reaches the start and resumes on its own,
suspend while plugged in, poweroff (that base lacks the rtc fix, so
reboots only), anything on a Pixel 7 Pro, 6 Pro or Pixel 7.

Signed-off-by: Lef <me@lefteros.com>
…atile

The charger regmap caches every register but the interrupt and status
ones. CHG_CNFG_00 to CHG_CNFG_04 hold the charger mode, the fast charge
current and the float voltage. The charger driver is going to read
them back after it writes them, to make sure the chip holds what was
asked, and a cached read would only return the value it just wrote.

Mark 0xb9-0xbd volatile so reads go to the chip. Writes are the same as
before; regmap_update_bits() now reads the chip instead of the cache
first, one more i2c read per update.

This reaches every board with the MAX77759, the Pixel 7 Pro, the 6 Pro
and the Pixel 7 too: every read of these registers, the sysfs reads of
the current and the float and every update of them, now goes to the
chip, and with temp_guard=1 every guard run talks to the chip. The MFD
has no cache sync or cache-only path on suspend, so no resume path
changes.

Tested: arm64 objects at W=1 with and without the KUnit option; the
charger's KUnit tests under UML.
On a Pixel 8 Pro, 2026-11-18, slot B, kernel
7.3.0-rc6-omamobile-push1-gc4d94a1c1fe5 (an earlier head of this series,
linux-omarchy-mobile#266, which still had the charge defender and the
husky float restore, now in their own PR, before the -EINVAL above the
top current step and the read-back values in the dev_err, which KUnit
covers, on the published base, nothing else on top; the charger options
from /etc/modprobe.d, temp_guard=1 charge_defender=1
default_ilim_ua=500000; a 500 mA desktop USB port):
probe, the thresholds and the guard ran with no visible change from
this; read-only through regmap debugfs the charger registers matched
what the driver logged (a 4200 mV float written over the bootloader's
4350 mV, again after a reboot). On 2026-11-16, on an earlier form of
this series, 0xbd read 0x2d after probe and 0x23 after unbind (the float
restore, since moved to its own PR), the values the driver wrote.

Not tested: anything on a Pixel 7 Pro, 6 Pro or Pixel 7.

Signed-off-by: Lef <me@lefteros.com>
The temperature guard writes the float voltage and the charge current
and has so far ignored a failed write. Read both back from the chip
after writing them (the registers are volatile since the previous
patch) and keep charging inhibited when the chip does not hold the
step the request maps to, or when a write or a read fails. The next
guard run tries again; the error is logged once, when it starts, with
the float and current asked for and what the chip holds. The
release back to the battery's own limits does the same and stays
active until the chip is seen to hold them. The write order is
unchanged: float, then current.

This runs on every board with temp_guard=1, the Pixel 7 Pro, the 6 Pro
and the Pixel 7 too: each guard run now writes and reads the two
registers on the chip, and a mismatch or a failed read now stops
charging (buck only) where a failed write used to be ignored. With
temp_guard=0, the default, nothing changes.

The expected values come from the same step rounding the setters use,
at or below the request for both, so a request between two steps is
no mismatch.

Tested on a regmap backed by an array: the write order and values, a
wrong read-back of the float and of the current gives -EIO with the
values the chip holds and puts the chip in buck-only mode, a request
refused before the write reads nothing back, and a good one resumes
charging. KUnit under
UML; arm64 objects at W=1 with and without the KUnit option.
On a Pixel 8 Pro, 2026-11-18, slot B, kernel
7.3.0-rc6-omamobile-push1-gc4d94a1c1fe5 (an earlier head of this series,
linux-omarchy-mobile#266, which still had the charge defender and the
husky float restore, now in their own PR, before the -EINVAL above the
top current step and the read-back values in the dev_err, which KUnit
covers, on the published base, nothing else on top; the charger options
from /etc/modprobe.d, temp_guard=1 charge_defender=1
default_ilim_ua=500000; a 500 mA desktop USB port):
the guard logged "battery 38.6 C, 4308 mV: band 20-42 C, float 4200 mV,
charge 1485000 uA", and no read-back error or inhibit was logged through
the run and a reboot.

Not tested: a real mismatch or failed read on hardware (the failure path
is KUnit only), anything on a Pixel 7 Pro, 6 Pro or Pixel 7.

Signed-off-by: Lef <me@lefteros.com>
@Somnius
Somnius force-pushed the husky-charger-thresholds-1116 branch from 1244cde to 6a78515 Compare November 19, 2026 06:43
@Somnius Somnius changed the title power: supply: max77759: charge thresholds, a battery defender and safer limits power: supply: max77759: charge thresholds, read-back and safer limits Nov 19, 2026
@Somnius

Somnius commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

@derekross done, I split it. This PR now has only the five shared commits, head 6a78515 (it was 1244cde), so it's ready for your 7 Pro run.

What changed:

  • the defender and the husky float restore moved out to their own PR, stacked on this one, where they wait for the gauge temperature fix: power: supply: max77759: Pixel 8 Pro charge defender and husky float restore #277
  • the tested notes in the four later commits now say the earlier head I ran on the phone still had those two commits, and the 0xbd note says the unbind value came from the float restore
  • a test comment in the thresholds commit said "the defender's 80 / 79", now it says "start one below the end: resume at 79"

No code changed besides that comment. KUnit is 26/26 on this head and the kernel-pr-check gate passes. The defender PR head is 9a079c7 and #267 is now c4aca6f.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants