Skip to content

power: supply: max17042_battery: wait out the MAX77759's alert - #170

Merged
derekross merged 1 commit into
derekross:omamobilefrom
nesmeck:omamobile-max17042-alert-wait
Nov 1, 2026
Merged

derekross merged 1 commit into
derekross:omamobilefrom
nesmeck:omamobile-max17042-alert-wait

Conversation

@nesmeck

@nesmeck nesmeck commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

On the MAX77759, every gauge alert turns into a burst of hundreds of interrupts. The gauge keeps ALRT asserted until its next task period after Status is cleared: up to 351 ms with the TaskPeriod (0x2d00) its battery models set. The interrupt is level-low and oneshot, so as soon as max17042_thread_handler() returns, it runs again, about once a millisecond, until the gauge lets go.

The fix. On the MAX77759 only, the thread sleeps one task period after clearing Status. The period is computed from chip->task_period, in units of 1/32768 s: 351 ms with a model, 176 ms at the default 0x1680. An alert that arrives during the sleep isn't lost: the level interrupt stays pending and is handled on the next run. Nothing new is written to the gauge.

Google's GPL driver for the same gauge does the same. max1720x_fg_irq_thread_fn() in kernel/google-modules/bms, max1720x_battery.c, branch android-gs-pantah-6.1-android16, ends with msleep(MAX1720X_TICLR_MS) (500 ms) and the comment "oneshot w/o filter will unmask on return but gauge will take up to 351 ms to clear ALRM1".

Tested on a Pixel 7 (slot B, 7.3.0-rc6), with stock's battery model loaded (the battery PR, to follow) and a logger reading the interrupt count with Status and the gauge's SOC registers every 250 ms:

Alerts Interrupts per alert
Before (…-ge654d41e556f) RepSOC 92 → 91, 91 → 90, 90 → 89 %, with the cable out 424, 429, 453
After (…-g51074200c999, only this change added) RepSOC reaching 90 % on the cable, then 90 → 89 … 87 → 86 %, with the cable out 1 each

Before the fix, Status already read 0 after each burst, and nothing fired between steps. After it, SALRT_Th moved with each step, and the gauge's readings (capacity, voltage, current, temperature) were as before.

The Pixel 7 Pro uses the same gauge and driver, so it should see the same bursts. I haven't tested it there.

Checks: tools/kernel-pr-check --build OUT --merge origin/omamobile passes everything:

  • a clean merge onto caf5f8a;
  • the sign-off;
  • checkpatch --strict with no warnings;
  • the W=1 build of the driver.

🤖 Generated with Claude Code

The MAX77759's gauge keeps ALRT asserted until its next task period
after Status is cleared, up to 351 ms with the TaskPeriod its battery
models set. The interrupt is level-low and oneshot, so as soon as the
thread returns it runs again, and each alert becomes a burst: on a
Pixel 7, every 1 % step of RepSOC fired the gauge's interrupt about 420
times within half a second, with Status already clear on each later
run.

Sleep one task period at the end of the thread on the MAX77759, before
the interrupt is unmasked. Google's driver for the same gauge does the
same (max1720x_fg_irq_thread_fn() sleeps 500 ms, "gauge will take up to
351 ms to clear ALRM1"). Alerts arriving meanwhile are kept by the
level interrupt and handled on the next run.

Tested on a Pixel 7 (7.3.0-rc6) with its battery model loaded: before
this change, unplugged and discharging, three 1 % steps of RepSOC fired
the interrupt 424, 429 and 453 times. With it, five alerts in 18
minutes (RepSOC reaching 90 % on the cable, then four steps down to
86 % unplugged) fired it once each, the thresholds moved with each step,
and the gauge reported as before.

Assisted-by: Claude Opus 5.5
Signed-off-by: Alejandro Garcia <skipper.pelygro@gmail.com>
@Somnius

Somnius commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Tested on a Pixel 8 Pro (husky), and I see the same burst you describe, and your patch removes it.

Setup: 7.3.0-rc6 (@greenbrettmichael's c6 plus the four commits of #163), the MAX77759 gauge with Google's default-a1-0k model loaded through a DT overlay, so TaskPeriod is 0x2d00 and your sleep is the 351 ms. A small logger on the phone reads the gauge's line in /proc/interrupts (summed over the CPUs) every 30 seconds together with the battery level, and I forced a few 1% steps with a CPU load (about 450 to 650 mA).

Without your patch: the count jumped by 466 for the 75 to 74 step (803 to 1269) and by 402 for 74 to 73 (1269 to 1671), so roughly 400 to 470 interrupts per 1% step, the same size as on your Pixel 7.

With your patch: it applied cleanly to our tree, I rebuilt only max17042_battery.ko with the release name pinned and swapped it on the running phone (rmmod, insmod, the model stayed loaded, Status POR stayed clear). The 73 to 72 step added 1 interrupt (1671 to 1672) and 72 to 71 added 1 (1672 to 1673).

What I did not test: the Pixel 7 Pro, a different pack or model, more than two steps with the patch, or anything else the patch might change (the percentage and temperature kept reading normally, nothing else I looked at changed). Thanks for this one.

@derekross

Copy link
Copy Markdown
Owner

Thank you, @nesmeck, for tracking this down, and for the before/after counts per 1 % step; that table makes the problem and the fix easy to see. Thanks also to @Somnius for the independent run on the Pixel 8 Pro.

The change itself looks right to me:

  • Google's max1720x_fg_irq_thread_fn() on android-gs-pantah-6.1-android16 does end with the 500 ms sleep and the "up to 351 ms to clear ALRM1" comment you quote, so the mechanism is the vendor's.
  • Sleeping one task period (351 ms with TaskPeriod 0x2d00, 176 ms at the default 0x1680) before the oneshot thread returns covers the window the gauge needs, and a level interrupt that arrives meanwhile stays pending, as you say.
  • It's gated on MAXIM_DEVICE_TYPE_MAX77759, so the other gauges this driver serves don't change.

Our gate agrees with yours on today's omamobile: a clean merge, the sign-off and prefix pass, checkpatch has no warnings, and the driver builds clean with W=1. CI is green too.

The one thing left is on my side. The Pixel 7 Pro has the same gauge on the same interrupt (gpa9-3, level low), with a battery model that sets the same TaskPeriod, so this changes the reference phone on purpose. Our rule for shared drivers (CONTRIBUTING §6.2) is that a deliberate Pixel 7 Pro change merges after it has run there. The driver file is identical on omamobile and on the integration branch my phone runs, so I can test exactly this commit. I'll count the gauge's interrupts in /proc/interrupts across a couple of 1 % steps, before and after, check that capacity, voltage, current and temperature still read as before, and that suspend still behaves. If it matches your numbers, I'll merge it.

One small thing you could tidy if you touch it again, not a blocker: the early return IRQ_HANDLED when the Status read fails skips the sleep, so an I2C error during an alert would still burst. Moving the sleep to a common exit would cover that path too.

@nesmeck

nesmeck commented Nov 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for checking it against Google's handler, and for running it on the Pixel 7 Pro before merging.

You're right about the early return: when the Status read fails, return IRQ_HANDLED skips the sleep, so an I2C error during an alert would still burst. I have the fix ready locally. The read failure now goes to a common exit (goto out), and the MAX77759 sleep sits there, so both paths wait one task period. Nothing else changes.

How would you like it, given that you're testing exactly this commit on your phone?

  • (a) I fold it into this PR's single commit now and force-push, so your Pixel 7 Pro run covers the final version. That means repeating it if you've already started.
  • (b) You test and merge this one as it is, and I send the change as a small follow-up PR right after.

Either is fine with me. On the Pixel 7 I'd check the follow-up the same way as before: one interrupt per 1 % step, with the gauge readings unchanged. The error path itself I can't trigger safely on the MAX77759's bus.

🤖 Generated with Claude Code

@Somnius

Somnius commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

@nesmeck thanks, the common exit sounds right to me. @derekross if it helps with your choice between (a) and (b): I can run the final version on my Pixel 8 Pro as well, it has the same MAX77759 gauge and I already ran this commit there before (one interrupt per 1 % step, gauge readings unchanged). I'd just rebuild max17042_battery with the change on my kernel and read /proc/interrupts across a few steps, same as last time, so nothing extra needed on your side. Tell me which version you end up with and I'll run it.

@derekross

Copy link
Copy Markdown
Owner

Thanks, I'm taking this one. I had an equivalent fix on my integration branch (a 1.5-period hold-off, plus an IRQ_NONE return when Status reads zero). I've swapped both of my commits out for yours, so the published kernel gets your version.

Tested on a Pixel 7 Pro (7.3.0-rc6-omamobile-push1-gd8c2d4f278bd, my integration kernel with my two commits reverted and this one on top): phone-smoke passed every row, and the 99 to 100 % step on the charger fired the gauge's interrupt once (max170xx_battery count 0 to 1), where without a wait it used to be 300-460 per step. Gate and CI are clean.

@derekross

Copy link
Copy Markdown
Owner

To your question, @nesmeck: option (b). This merges as it is, so please send the common exit as a small follow-up PR. @Somnius, a run of that one on the Pixel 8 Pro would be very welcome.

@derekross
derekross merged commit 92b2b5d into derekross:omamobile Nov 1, 2026
1 check passed
@Somnius

Somnius commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

@derekross thank you for taking it. @nesmeck when the common-exit follow-up PR is up, ping me and I'll run it on my Pixel 8 Pro the same way as before: the interrupt count per 1 % step, gauge readings unchanged. I can't trigger the I2C error path safely on this bus either.

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.

3 participants