Skip to content

S8001 (A9X): Add Lightning charging for the 9.7-inch iPad Pro - #136

Open
danlovaj wants to merge 7 commits into
OrchardLinux:mainfrom
danlovaj:s8001-charging
Open

danlovaj wants to merge 7 commits into
OrchardLinux:mainfrom
danlovaj:s8001-charging

Conversation

@danlovaj

@danlovaj danlovaj commented Dec 4, 2026

Copy link
Copy Markdown

Adds Lightning charging to the 9.7-inch iPad Pro (J127, J128). It charges at up to 1.5 A from a USB-A charger and 500 mA from a computer.

Based on main; it does not need #118 or #134. Charging needs the m1n1 pull request that copies the 24-byte calibration table (OrchardLinux/m1n1#23). With an older m1n1 the charger does not start, and USB connects through dwc2's fallback.

Changes

  • PMIC events: D2257 (Aria) support in gpio-apple-d2365. The charger needs its ADC-done event.
  • Calibration: the D2355 driver accepts the 24-byte low-voltage table.
  • Cable identification: on J128 the Tristar's response window stays empty after a cable is plugged in, which held wall chargers at 80 mA. The driver now sends one GetID when the frame is invalid.
  • Device tree: the charger is on for J127 and J128. J98a and J99a have a D2231 charger, which has no driver yet.

Affects other boards: the GetID also runs on other Tristar boards without apple,usb-role-switch, but only when their cable frame is invalid, and only on CBTL1610 revision 0x8a.

Tested on J128

Source Result
Anker USB-A power bank BrickID 5, ramps to 1.5 A in about 8 s, battery charges at about 830 mA
Computer USB port 500 mA
Unplug and replug between the two identified again each time

Tested on this branch, booted on J128. One Anker attempt dropped out about 6 s after it was plugged in and stayed off; the cable was probably not fully seated. The repeat held 1.5 A for about 60 s, until unplugged.

Not tested

  • J127. Its ADT lists the same charger, PMIC and interrupts.
  • A long charge, or charging up to 100%.

For reviewers

  • The iPad Pro 2 allows 1.5 A only while Lightning VBUS reads 4.75-5.5 V. J128 has no VBUS reading yet, so that guard is not active here. Should the limit be lower until it is?
  • On the Anker power bank, dwc2 logs "new device is full-speed" about 12 times a second. tristar_accept() leaves USB data on for BrickID chargers, so this is existing behaviour.

Assisted-by: LLM

🤖 Generated with Claude Code

@henrikklee

Copy link
Copy Markdown

Thanks a lot for this, @danlovaj. The commits are well split, the Aria event controller and the 24-byte calibration table are useful on their own, and the test table with the Anker and the computer port is exactly what we need to review a charger change.

Our direction for charging on every Lightning board is adaptive charging: the input limit follows what the source on the other end says it can supply, through the same shared code on every board. Since this PR reuses apple_tristar_charger, most of that is already in place for J127/J128. Below is what "adaptive" has to cover, what main already does, and what we'd like changed here. Line links are to main at 02287e7.

What adaptive covers, and where it already is on main

Source Evidence Grant On main
USB-C charger (Lightning to USB-C cable) Type-C Rp on CC, on the lane the orientation selects 1.5 A or 3 A; default Rp falls through to BC1.2 tristar_identify_usbc()
Apple USB-A charger BrickID D+/D- divider, both mux banks, the lower one wins if they disagree 0.5 / 1 / 2.1 / 2.4 A per ID tristar_identify_usba(), tristar_brick_ma()
Generic USB-A charger BC1.2 primary/secondary detection (DCP or CDP) 1.5 A tristar_identify_bc12()
Computer or hub port USB enumeration through dwc2 vbus_draw and usb-psy-name = "lightning" 100 mA unconfigured, bMaxPower once configured, 2 mA suspended tristar_set_property()
Apple host (Mac) Apple vendor request 0x40 on the gadget 500 mA plus the host's extra composite.c
Apple accessory (camera adapter and the like) Digital ID over the ID bus, then the accessory power handshake the profile agreed in the handshake (2.1 A on the camera adapter) apple_tristar_accessory.c, only on boards with apple,usb-role-switch
Nothing identified 500 mA fallback tristar_identify()
A source that sags under load VBUS or the PMIC's input-collapse signal back off and keep the lower limit until unplug only partly: see 2 below

Every grant is capped at the board maximum and ramps the way iOS does, inside the battery window. docs/bring-up/charging/adaptive-charging.md in orchard describes the rules ("no fixed limits: a grant is the minimum of the source's evidence and the board maximum").

So the PR is not a hard-coded 1.5 A: 1.5 A is the ceiling the DT gives, and 500 mA from your computer is the USB 2.0 rule working as intended. What we need changed:

1. Board maximum from the ADT: 2.4 A, not 1.5 A

I checked the iOS 16.7.16 device trees (20H392, DeviceTree.j127ap and DeviceTree.j128ap): /charger has usb-input-limit-max = 2400, as on J71b, J71t, the Air 2 and the mini 4. Please use input-current-limit-microamp = <2400000>, with a comment citing the ADT, so a 2.4 A brick or a 3 A Type-C charger gets 2.4 A and a BC1.2 port still gets 1.5 A.

On your reviewer question about the VBUS window: the 1.5 A and VBUS guard on t8011-pro2.dtsi belong to J120's high-voltage path. Its ADT has function-vbus_voltage on an orion,bellatrix and the 84-byte -hv calibration table. J127/J128 have neither: only the 24-byte low-voltage table, and their orion-ic is an orion,micro at 0x70 with no vbus_voltage function. So J127/J128 charge at 5 V only, the pro2 window does not apply, and lowering the ceiling until it does would be the wrong fix. (The same goes for USB PD: on Lightning, iOS runs PD over the ID bus, and it only matters with a high-voltage path, so it's not something we ask of this PR.)

2. Back off a source that cannot deliver what it advertises

A cheap brick or a tired power bank can advertise 2.4 A and then sag. Your Anker attempt that dropped out after about 6 s and stayed off might be one. An adaptive charger lowers its limit and keeps the lower value until unplug. It doesn't trip to off or hold a limit the source can't give. Main does this in two other drivers, but not yet in the Tristar policy:

  • SN2400 (iPhones): VBUS droop foldback, sn2400_vbus_drooped();
  • D1946 (iPad mini 1, armv7 branch, apple-pmu-charger.c): when the PMU drops its own limit because VBUS sagged, back off 8 codes, as iBEC and iOS do.

Since J127/J128 have no VBUS ADC in the ADT, the signal has to come from the D2355 itself: a status bit or one of the events the charger already subscribes to (40, 42, 53), or the D2355 lowering its own limit. Could you look in the J128 kernelcache for what AppleD2355PMUPowerSource (or its A9X equivalent) acts on when the input collapses? Please build it as an optional per-chip callback in struct tristar_chip_info that the shared ramp and guard use, not as D2355-only logic in the poll loop. Then every Tristar board can plug its own signal in. If the D2355 turns out not to expose one, please write in the PR what you checked, and we'll treat the shared foldback as a separate piece of work.

3. The ID-bus request: on every attach, for every board, not as a retry on a bad frame

You're right that the cable has to be asked for its ID. iOS asks on every attach, before it touches the data lines, and then samples only the route the ID selects (see the header comment of apple_tristar_accessory.c). On main that request only happens on boards with the accessory policy, which also needs the USB role switch for host mode. The new fallback in cbtl_capability() fixes J128, but:

  • it runs on every Tristar board without apple,usb-role-switch (Air 2, mini 4, the A7 iPads, the 12.9-inch 2017), and only when a frame happens to be invalid. A half-inserted plug, a frame that changed between reads and a CRC error each lead to a different path;
  • cbtl_capability() is also called after source events during a USB session. A GetID there wasn't part of the design: the session-time path deliberately re-reads only the frame.

The way we'd like it built: split the accessory code into the ID read, which every Lightning board needs for charging and which the charger runs once per attach before identification (it builds with the charger, as your Makefile change already does), and the host-mode policy, which stays behind the role switch. Identification then starts from the digital ID on every board, and the capability frame is read after it. That replaces the retry, and J71b/J71t keep their current behaviour because their policy already does exactly this.

4. The Aria event controller in s8001-pro.dtsi: correct, keep it there

I checked this against the J98a ADT (same IPSW family, DeviceTree.j98aap): its pmu is also pmu,d2257 with interrupts = <215 1>, and its Tristar is also a tristar,cbtl1610 at 0x1a. The event controller and the Tristar node therefore describe J98a/J99a correctly too, and the shared file is the right place for them, even though their D2231 charger has no driver yet. A one-line comment citing the ADT would help the next reader.

5. Not yours to fix, just so you know

The new device is full-speed spam on a BrickID charger comes from tristar_accept() leaving USB data on for BrickID sources. That's shared behaviour on main and we'll track it separately.

Tests we'd like to see on J128 after the changes

For each one, the values of usb_type, current_max and input_current_limit from /sys/class/power_supply/lightning/, plus the gauge current:

  • Apple 12 W (BrickID 2.4 A) and 5 W (1 A) USB-A bricks;
  • a USB-C charger through the Lightning to USB-C cable (3 A Rp), and one that only advertises default USB power;
  • a generic USB-A charger (DCP) and your Anker;
  • a computer port, a bus-powered hub, and a Mac if you have one (Apple host grant);
  • unplugging, and swapping brick to computer in under a second (no grant may carry over).

Thanks again. This gets the 9.7-inch Pro most of the way there, and the shared parts (the ID read on every attach, the sag back-off) will benefit every Lightning iPad.

@grierj

grierj commented Dec 4, 2026

Copy link
Copy Markdown

Data point from iPad Pro (10.5-inch, Wi-Fi) (J207 / T8011): we hit the exact same bug on J207's CBTL1610 rev 0x8a (0x20..27 stayed 75 00 00 00 00 00 00 00, logging unidentified cable (capability frame -74): up to 80 mA on every plug-in; tracked in OrchardLinux/orchard#552).

Cherry-picking 2928ee76 (power: supply: apple_tristar_charger: Ask the cable for its ID if needed) onto main fixed cable identification immediately on J207 across:

  • Anker Zolo 50 W USB-A brick -> Apple USB-A charger, BrickID 5 (2.4 A)
  • Apple MacBook Air USB-C brick + USB-C-to-Lightning -> USB-C 3.0 A
  • Apple brick + USB-A adapter -> BC1.2 DCP
  • Computer USB-A port -> SDP

(Paired with #164 for t8011-pro2's usb-psy-name and VBUS droop backoff, J207 now ramps cleanly to 1.5 A on all three wall bricks and 500 mA on a computer USB port.)

henrikklee pushed a commit that referenced this pull request Dec 4, 2026
…of faulting at 4.75 V

The Orion Bellatrix VBUS ADC measures Lightning VBUS at the iPad's
connector, after USB cable and adapter IR drop. On real 5 V USB-A bricks
and cables, drawing 1.0-1.5 A routinely drops connector VBUS below
4.75 V (the host-port minimum before cable drop): on J207 we measured
4736 mV at 1.48 A on an Anker Zolo 50 W USB-A brick (BrickID 5) and
4688 mV at 1.48 A (4736 mV at 1.28 A) on an Apple MacBook Air brick
through a USB-A adapter (BC1.2 DCP). Previously, any single sample below
4750 mV latched TRISTAR_FAULT and withdrew input current to the 80 mA
fallback until unplug.

Separate hard VBUS faults from normal cable droop:
- Keep immediate TRISTAR_FAULT withdrawal to fallback on IIO read error,
  overvoltage (> 5500 mV, protecting low-voltage range 0 against
  high-voltage USB-PD rails), or severe brownout (< 4350 mV, below the
  USB 2.0 4.40 V device-side minimum), checked whenever input is above
  fallback or targeting > 1 A.
- On droop below 4500 mV, step back by one ramp step (100 mA), cap
  vbus_limit_ma for the current attachment, and hold steady instead of
  collapsing to 80 mA. Reset vbus_limit_ma on disconnect/new attachment.

Tested on J207 (combined with #136's GetID retry):
- Computer USB-A (SDP): ramps cleanly to 500 mA (VBUS 5040 -> 4880 mV)
- Anker Zolo 50 W USB-A (BrickID 5): ramps to 1500 mA, VBUS 4736 mV,
  +725 mA net into battery
- Apple MacBook Air USB-C brick + USB-C-to-Lightning (3.0 A CC): ramps
  to 1500 mA, VBUS 4832 mV, +749 mA net into battery
- Apple brick + USB-A adapter + USB-A-to-Lightning (BC1.2 DCP): ramps to
  1500 mA, VBUS 4688 mV, +688 mA net into battery

Ref: OrchardLinux/orchard#552
Assisted-by: LLM
Signed-off-by: Grier Johnson <grier@scooterfarm.com>
danlovaj and others added 7 commits December 5, 2026 00:01
The Dialog D2257 (Aria) is the PMIC of the A9X iPad Pros. It has 22
GPIOs, whose input levels are read as on the D2365, and latches 248 PMU
events.

Assisted-by: LLM
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The D2257 of the A9X iPad Pros has 31 event bytes at 0x140 (status) and
0x1c0 (mask), with GPIO line N being event 0x30 + N. Its 22 GPIOs are
inputs for now, as the boot firmware set them up.

The Lightning charger needs the events: the PMIC's ADC reports a finished
conversion as event 184.

Assisted-by: LLM
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…bration

The 9.7-inch iPad Pro has the same D2355 charger as the iPad Pro 2, but
its ADT carries a 24-byte low-voltage calibration table in place of the
84-byte one, and the ADC is in the Aria PMIC.

Assisted-by: LLM
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The 9.7-inch iPad Pro's ADT has a 24-byte usb-input-limit-calibration
table: five Q16.16 points for range 0 and a selector word. Use it when the
84-byte high-voltage table of the iPad Pro 2 is absent.

Assisted-by: LLM
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
iOS asks the cable for its digital ID over the ID bus on every attach
before touching the USB data lines, and then reads the capability frame
from the Tristar response window. Until now, only boards with
apple,usb-role-switch ran GetID through the accessory policy; on other
CBTL1610 rev 0x8a boards (such as J128 and J207), 0x20..0x27 stayed
75 00 00 00 00 00 00 00 and wall chargers fell back to 80 mA.

Build apple_tristar_idbus.o unconditionally with apple-tristar-charger
(keeping the host-mode accessory policy behind CONFIG_APPLE_TRISTAR_ACCESSORY)
and run tristar_idbus_identify() once per attachment in
tristar_run_identification() before tristar_identify() on boards without
chg->acc. Boards with chg->acc keep their existing accessory-policy flow,
non-0x8a Tristars return -ENODEV without register writes, and session-time
tristar_reconfirm() continues reading only the capability frame.

Assisted-by: LLM
On J127/J128 there is no VBUS ADC in the ADT, so input sag has to come
from the D2355 itself. In iOS 16.7.16 (AppleD2355PMUPowerSource::
handleInterrupt), when a verified grant >= 200 mA (raw >= 0x14) collapses
a weak source, the D2355 drops control_base + 0xb (0x44e) to 0x0a (100 mA)
while status[1] (0x81) bit 7 stays set, and AppleARMPMUCharger backs the
input current limit off by 100 mA.

Wire tristar_d2355_input_sag() into tristar_chip_info.input_sag:
- Keep tristar_vbus_sag() first for boards that carry a VBUS ADC.
- Otherwise report TRISTAR_SAG_DROOP when the D2355 has dropped
  control_base + 0xb to 0x0a while online.
- Because D2355's line/source IRQ handlers withdraw to the fallback before
  tristar_poll() runs, also check for that collapse right before
  tristar_d2355_write_limit() overwrites 0x44e on withdrawal and pass the
  collapsing current through input_sag() so the shared sag backoff caps
  the attachment 100 mA below it instead of looping back into collapse.
- Keep the faster poll interval while awaiting sag confirmation.

Assisted-by: LLM
…Pad Pro

Describe the Aria PMIC's event controller and the CBTL1610 Tristar in
s8001-pro.dtsi (matching the J98a/J99a and J127/J128 ADTs), and the D2355
charger of J127 and J128 in s8001-j127-j128.dtsi with the ADT's 2.4 A
board maximum (usb-input-limit-max = 2400). J98a and J99a have a D2231
instead, which has no driver yet.

Assisted-by: LLM
@danlovaj

danlovaj commented Dec 5, 2026

Copy link
Copy Markdown
Author

Thanks for the detailed review @henrikklee, and great to hear the ID-bus read also helped J207 @grierj!

Rebased onto origin/main (33fa48a3e, taking in the new input_sag() hook) and updated the branch (990d8f44e) for all 4 items:

1. Board max raised to 2.4 A

  • Updated s8001-j127-j128.dtsi to input-current-limit-microamp = <2400000> with a comment citing the iPadOS 16.7.16 (20H392) J127/J128 ADT (usb-input-limit-max = 2400).

2. D2355 input-collapse check wired into tristar_chip_info.input_sag

  • Inspected iPadOS 16.7.16 (20H392) j128 kernelcache (AppleD2355PMUPowerSource::handleInterrupt and AppleARMPMUCharger):
    • AppleD2355PMUPowerSource subscribes events 40 (0x501), 42 (0x504), and 53 (0x620).
    • On events 40/42, when the last written raw code in reg 0x44e (control_base + 0xb) is >= 0x14 (>= 200 mA) and status[1] (0x81) bit 7 is set, it reads reg 0x44e back; if the D2355 hardware dropped it to 0x0a (100 mA), it notifies AppleARMPMUCharger with bit 2 (0x4), which backs off the input current limit by 100 mA.
  • Wired tristar_d2355_input_sag() into tristar_d2355_chip.input_sag: keeps tristar_vbus_sag() first when a VBUS ADC is present, otherwise reports TRISTAR_SAG_DROOP if 0x44e collapsed to 0x0a while online (and also checks right before tristar_d2355_write_limit() overwrites 0x44e on IRQ withdrawal so the shared sag cap backs off 100 mA below the collapsing level).
  • Live observation on a sagging source (PC USB-C port): Under load, a Linux PC USB-C port advertising 3.0 A (2400 mA) repeatedly triggered D2355 PMU event 53 (0x620, status[6] bit 5) right at 2080 mA instead of resetting 0x44e to 0x0a, causing 80 -> 2080 mA -> event 53 -> 80 mA cycles (and on the 6th cycle a write race hit input-limit write failed). Happy to wire event 53 into input_sag as well if you think that belongs here or as a follow-up.

3. Per-attach ID-bus read before identification

  • Replaced the cbtl_capability() CRC-error retry with a single per-attach tristar_idbus_identify() in tristar_run_identification() before tristar_identify() on boards without chg->acc (apple_tristar_idbus.o built unconditionally with apple-tristar-charger, host-mode apple_tristar_accessory.o stays behind CONFIG_APPLE_TRISTAR_ACCESSORY).
  • cbtl_capability() is pure read-only again, chg->acc boards (J71b/J71t) keep their existing path, non-0x8a Tristars return -ENODEV without register writes, and session-time tristar_reconfirm() never touches the ID bus.

4. ADT comments in s8001-pro.dtsi

  • Kept tristar (cbtl1610 at 0x1a) and aria_gpio (pmu,d2257, AP GPIO 215) in s8001-pro.dtsi with comments citing the J98a/J99a/J127/J128 ADTs.

Live J128 hardware test matrix (990d8f44e + m1n1#23, hardware-check RAM boot)

Every source identified on the first try with zero -EBADMSG / ID-bus errors:

Source Kernel identification usb_type current_max input_current_limit Gauge current / V
PC USB-A port (idle, 95% SOC) USB enumeration (500 mA) SDP 500 mA 500 mA (steady) +70 mA / 4.17 V
Anker PowerPort 4 (A2142) (under 1-core load) Apple USB-A charger, BrickID 5 (2.4 A) BrickID 2400 mA 2400 mA (held 32 s) +374 mA / 4.17→4.20 V
Apple 10 W USB-A (95% SOC) Apple USB-A charger, BrickID 4 (2.1 A) BrickID 2100 mA 880 mA* +328 mA / 4.23 V
Oculus 15 W USB-C (C→Lightning, 95% SOC) USB-C 3.0 A (CC 1692 mV) C 2400 mA 880 mA* +315 mA / 4.23 V
Samsung 10 W USB-A (under 1-core load) USB charging port (DCP, BC1.2 667/667 mV) DCP 1500 mA 1500 mA (held 48 s) -222 mA / 4.12→4.15 V
USB-A-to-C adapter (C→Lightning, 95% SOC) USB-C 3.0 A (CC 1645 mV) C 2400 mA 880 mA* +309 mA / 4.23 V
PC USB-C port (C→Lightning, under load) USB-C 3.0 A (CC 1697 mV) + USB 500 mA C 2400 mA 2080 mA (event 53 at 2080 mA; see #2) -171 mA / 4.16 V
Brick → PC USB-A swap Disconnect → 80 mA → SDP (500 mA) SDP 500 mA 500 mA (zero carryover) -1315 mA / 4.05 V

*At 95% SOC (4.16 V resting), ramping past 880 mA raised pack voltage above the conservative 4.20 V limit (4.21–4.23 V), tripping the pack window guard cleanly. Under a 1-core system load (keeping pack voltage below 4.20 V), Anker held steady at 2400 mA and Samsung DCP held steady at 1500 mA.


Note on Signed-off-by (regarding your requests here and on #118/#133):
To be transparent, I am steering an AI coding assistant and performing the live DFU and hardware testing on my J128 — I did not write or audit all of the C/DTS code myself, so I am not comfortable certifying a Signed-off-by DCO for code I did not personally author/review, though I am happy to provide Tested-by: for the hardware verification. Let me know how you would prefer to handle that for these PRs.

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