Skip to content

N66: userspace charge control on the SN2400 charger - #112

Draft
ricktav wants to merge 1 commit into
OrchardLinux:mainfrom
ricktav:n66/charge-control
Draft

ricktav wants to merge 1 commit into
OrchardLinux:mainfrom
ricktav:n66/charge-control

Conversation

@ricktav

@ricktav ricktav commented Dec 2, 2026

Copy link
Copy Markdown

Lets userspace limit charging on the SN2400 charger, the way iOS does it: a charge-current cap, "don't charge" while the phone keeps running from the cable, and a charge-level hold (end and start thresholds) where UPower, GNOME, KDE and TLP look for it.

Draft: four on-device checks are still open (see below).

What

On the usb supply (SN2400), in control mode without apple,keep-charge-limit:

  • constant_charge_current (rw): 100 mA up to the monitored battery's constant-charge-current-max, in 10 mA steps, written to 0x0d. That maximum is iOS's own ceiling (ADT charge-limits[0]), so a write can never exceed what iOS asks.
  • charge_behaviour (rw): auto, inhibit-charge (0x0d = 0, input on: the phone runs from the cable), force-discharge (also turns the input off, through the temperature guard's path).

On the gauge's battery, as a power-supply extension:

  • charge_control_end_threshold (20..100, 100 = off) and charge_control_start_threshold: charging stops at the end and resumes below the start (or 5 % below the end).
  • charge_behaviour: the same state as on usb.

The driver applies the thresholds itself, on the temperature guard's existing 5 s gauge poll, so no daemon is needed and a crashed daemon cannot leave charging off.

Why this way

iOS 15.8.8's AppleSN2400ChargerFunction writes 0x0d = min(mA, 2550) / 10 from AppleARMPMUCharger's chargeLimit. A chargeLimit of 0 (IsCharging false, PredictiveChargingInhibit, i.e. Optimized Battery Charging at 80 %, and the PoSM state-of-charge hold) writes 0x0d = 0 and leaves the input on. Turning the input off is iOS's separate inflow inhibit. This driver maps inhibit-charge to the first and force-discharge to the second.

Safety

  • Every mode other than normal charging needs a gauge capacity no older than 60 s and above 10 %. Without one the charger charges as normal.
  • The temperature guard is unchanged and still turns the input off or reduces it.
  • Remove and shutdown restore 0x0d. A panic or an emergency restart skips shutdown, so probe restores a leftover 0 (also with apple,keep-charge-limit) and warns.
  • The suspend handler stops charging from the start threshold up, so a night on a charger cannot run through the band.
  • Read-only and apple,keep-charge-limit nodes: no writable attribute, no extension.

Which boards

The driver change applies to every board whose SN2400 node is in control mode: the A9 iPhones (s800x-iphone.dtsi: N66, N66m, N71, N71m, N69, N69u), the iPhone 6 (N61, t7000-n61.dts, since #18) and the iPhone 7 / 7 Plus (t8010-7.dtsi). Each one's ceiling is its own DT battery's constant-charge-current-max. Only the N66 was tested.

Tested

On an iPhone 6s Plus (N66), RAM-booted, on a Mac's USB port, on 2026-11-25. The kernel was built from the pre-migration Orchard patch tree, with this patch as drivers/power/0074. The driver file on current main is unchanged since that tree (#18 changed only the N61 device tree), and this commit applies without conflicts, byte-identical to the tested patch.

  • T0, probe: the extension is on battery, 0x0d = 0xc0 (1920000 µA), thresholds 0/100.
  • T2/T3, inhibit and resume:
    • inhibit-charge sets 0x0d = 0x00 and 0x0e = 0x00. The battery holds at 0 A, IBUS is 270–350 mA from the cable, usb/online is 1 and usb/status is "Not charging".
    • auto charges again within about 1 s (0x0d 0xc0, +0.18 A).
  • T4, current cap:
    • 500000 → 0x0d = 0x32.
    • 123456 → 120000 (0x0c).
    • 2000000, 1925000 (above the 1920000 maximum) and 50000 → EINVAL.
  • T6, end threshold: end 90 at 100 % → "charge inhibited at 100 %" within 7 s, 0x0d = 0, IBUS about 330 mA.
  • T7, force-discharge:
    • Input off (IBUS 0, current_max 0, 0x10 = 0x0c), battery −0.36 A, usb/status "Discharging".
    • auto with end 90 at 100 % stays held; end 100 charges again.
  • T10, replug while held: 0x0d stays 0x00 across an 8 s unplug, with no rewrite warning.
  • T12, clean reboot while held: reboot -f into iOS, which shows the charging bolt.
  • T13, partial:
    • A DFU entry from a held Linux cleared the 0: the next probe read a non-zero 0x0d.
    • A real crash that ends in recovery was not reproduced. The first attempt rebooted straight into iOS, which rewrites 0x0d.

Local checks on this branch:

  • scripts/checkpatch.pl --strict: 0 problems in the code. It flags only the missing Signed-off-by and the Co-Authored-By trailer spelling.
  • drivers/power/supply/apple_sn2400_charger.o builds with W=1 (orchard_apple16k_defconfig, aarch64 gcc 12) without warnings.

Not tested (why this is a draft)

  • T5: the current cap at a wall adapter. On the Mac's port, the 500 mA input limit binds before the cap.
  • T8: the thresholds across suspend inside the band, 10 min.
  • T9: suspend below the start threshold, 20 min. The expected overshoot is documented in the code.
  • T13: what a real crash while held leaves for iBoot and recovery.

These need a boot at 60 % charge or more. Also not tested:

  • the N61, the iPhone 7 / 7 Plus and the other A9 iPhones;
  • UPower/desktop discovery of the extension;
  • the 10 % floor.

Known gaps

  • A phone that suspends below the start threshold keeps charging while asleep: there is no timed wake. It can pass the end threshold by a few percent before it wakes.
  • 0x0e and 0x0f (applied current and voltage) are only logged. Their meaning is inferred.

Commits

  • power: supply: apple_sn2400_charger: Let userspace limit charging: constant_charge_current and charge_behaviour on the USB supply, end and start thresholds on the gauge's battery, applied in the driver, with the crash and suspend safeguards above.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SFMaVf5sMxs7xEyPFZWGEj

The charger charges whenever the input allows it; userspace can neither
stop at a charge level nor lower the charge current.

iOS 15.8.8's AppleSN2400ChargerFunction (iPhone 6s Plus kernelcache)
writes 0x0d = min(mA, 2550) / 10 from AppleARMPMUCharger's chargeLimit,
which is capped at the ADT charge-limits[0] (1924 mA on the n66; the
monitored battery's constant-charge-current-max on all six A9 iPhones).
A chargeLimit of 0 writes 0x0d = 0 and leaves the input on: that is how
iOS stops charging for IsCharging false, PredictiveChargingInhibit
(optimized charging at 80 %) and its PoSM state-of-charge hold, which
stops at an upper threshold until the charge falls to a lower one.
Turning the input off (0x10 OFF, 0x09 = 0) is iOS's separate inflow
inhibit.

In control mode without apple,keep-charge-limit:
- constant_charge_current (100 mA up to the battery's
  constant-charge-current-max, 10 mA steps) caps 0x0d;
- charge_behaviour inhibit-charge writes 0x0d = 0, force-discharge also
  turns the input off as the temperature guard does;
- the gauge's battery gets charge_control_end_threshold and
  start_threshold (and charge_behaviour) as a power-supply extension:
  charging stops at the end threshold and resumes below the start
  threshold, or 5 % below the end without one.

The driver reads the gauge's capacity on the temperature guard's 5 s
poll, only while charge control is in use. Without a capacity younger
than a minute, or at 10 % and below, it charges as normal. The
temperature guard still turns the input off or reduces it. Asleep the
work does not run, so the suspend handler also stops charging from the
start threshold up; force-discharge keeps the input off. jiffies stand
still in s2idle, so resume reads the gauge again before deciding.
0x0d is written again after every cable change and read back while
charging is held. Remove and shutdown restore the full limit: a 0 would
survive into iBoot and the next boot. A panic or an emergency restart
skips shutdown, and the I2C controller cannot write from a restart
handler, so probe restores a 0 it finds, also with
apple,keep-charge-limit, and warns that a crashed kernel left it.

The charger's status bit stays set while 0x0d is 0, so the USB supply
reports Not charging while the charge is held and Discharging with
force-discharge.

Every board whose SN2400 node is in control mode gets this: the A9
iPhones (s800x-iphone.dtsi), the iPhone 6 (N61) and the iPhone 7 and
7 Plus (t8010-7.dtsi). Read-only and apple,keep-charge-limit nodes
keep their attributes read-only and register no extension.

Tested on an iPhone 6s Plus (N66), RAM-booted, on a Mac's USB port
(2026-11-25): inhibit-charge writes 0x0d = 0 and holds the battery at
0 A while the phone runs from the cable (IBUS 270-350 mA), with
usb/status Not charging; auto charges again within a second.
constant_charge_current 500000 gives 0x0d = 0x32, 123456 rounds to
120000, and values above the battery's limit or below 100 mA are
refused. An end threshold below the charge stops charging within
7 s; force-discharge turns the input off (IBUS 0, battery -0.36 A).
0x0d stays 0 across an unplug and replug, and a clean reboot while
held hands iOS a charger that charges. The current cap at a wall
adapter, the thresholds across suspend and a crash while held were
not tested, nor were the N61 and the iPhone 7.

Signed-off-by: ricktav <82380971+ricktav@users.noreply.github.com>
Assisted-by: LLM
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SFMaVf5sMxs7xEyPFZWGEj
@ricktav
ricktav force-pushed the n66/charge-control branch from 94e9c6d to 5c502af Compare December 2, 2026 21:32
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.

1 participant