Skip to content

iPhone 5: PMU, charger, backlight, battery, touch and wireless - #59

Draft
brkosebay wants to merge 9 commits into
OrchardLinux:armv7from
brkosebay:a6/n42-pr-periph
Draft

brkosebay wants to merge 9 commits into
OrchardLinux:armv7from
brkosebay:a6/n42-pr-periph

Conversation

@brkosebay

@brkosebay brkosebay commented Dec 1, 2026 •

Copy link
Copy Markdown

This PR depends on #58. Both PRs target armv7, so this head also contains its predecessors’ commits. The subsystem changes start after 0bdd40d.

Describe the D1972, HDQ gauge, LM3534/DWI backlight, N1 touch, buttons and wireless peripherals. Reuse the current P105 touchscreen boot code for the N42 HBPP packet and per-unit calibration. Preserve the D1946 charger default. D1972 writes default off. The DWI window is corrected to 0xf700000.

On iPhone5,2 on 29 November, evtest3-1357 delivered 7,708 events across two touch slots and bt-1430 brought hci0 up and scanned nearby LE devices. The gauge reported 4,229 mV and 25.9 °C. These measurements came from the development kernel. The integration with Orchard's newer P105 boot worker has only been build-tested.

The N42 binding gives the scan-start report its four tested bytes. Is the P105-derived value acceptable until an N42 trace establishes it? Should the other armv7 PMU, HDQ, backlight and wireless properties gain bindings in one platform pass?

Built this head with the N42 integration config, ARCH=arm, LLVM 19, olddefconfig, zImage and dtbs. Hardware observations above refer to the development flights. Each commit carries Assisted-by: LLM and the author’s Signed-off-by trailer.

The iPhone 5 (N42, ADT "pmu,d1972") has a Dialog D1972 PMU. Move the
GPIO count and the power-off register sequence into per-chip data, and
add "apple,pmu-d1972", from AppleD1972PMU (iPhone5,2 iOS 10.3.4):

- 8-bit register addresses, 12 GPIOs at 0x68 + n;
- power off (_shutdownGated): 0x7d = 0, clear 0x01..0x09, wake masks
  0x12..0x1a (88 ff ff ff ff ae ff ff ff), alarm control 0x62 = 0x01,
  0xa2 = 0, 0xa4 = 0, boot flags 0xb0 &= ~0xd0, clear the wake bit of
  the input GPIOs (>= 0x60), standby 0x1b |= 0x01.

The D1946, D1974, D2333 and Chestnut paths are unchanged.

Tested on an iPhone5,2: the PMU probes, and its GPIOs drive the
Bluetooth and Wi-Fi power lines.

Assisted-by: LLM
Signed-off-by: Berkay Kösebay <brkosebay@users.noreply.github.com>
Move the per-chip charger registers and the current encoding into
struct pmu_chg_chip and add "apple,pmu-d1972-charger" for the iPhone 5,
from AppleD1972PMUPowerSource (iPhone5,2 iOS 10.3.4):

- the USB input current limit is register 0x3f (0x3d on the D1946);
- the code is linear, 10 mA per step from 70 mA: (mA - 70) / 10;
- VBUS online is status register 0x0a bit 6; a VBUS sag drops 0x3f to
  0x03 (100 mA).

The battery temperature comes from the HDQ gauge, not a PMU ADC
channel, and corrupt gauge words once alternated "battery at 6272.0 C"
with good readings. On the D1972 a reading outside -20..+70 C counts as
no reading (the existing 100 mA fallback), and leaving the fallback
takes three good readings in a row. Entering it on a missing reading is
logged once. The D1946 path is unchanged.

The driver keeps its armed=0 default: on the iPhone 5 it reports VBUS
and the input limit, and logs the limit it would set without writing
it.

Tested on an iPhone5,2: VBUS online and the 100 mA limit read, no
write.

Assisted-by: LLM
Signed-off-by: Berkay Kösebay <brkosebay@users.noreply.github.com>
The iPhone 5's D1972 PMU has no WLED strings, unlike the A5's D1946
and the A5X's D1974. Its panel backlight is an external TI LM3534 on
I2C0 (0x63), whose brightness comes over the SoC's DWI block
(0x3f107000, version 1).

Add apple-lm3534, after AppleLMBacklight and AppleS5L8940XDWI (iPhone5,2
iOS 10.3.4):

- enable and disable with LM3534 register 0x10 bit 0, keeping the other
  bits iBoot set, and wait 600 us after enabling;
- send 11-bit brightness (0..2047) on the DWI sync channel: +0x14 =
  0xa0000000 | level, then +0x10 = 0x71 once busy (bit 0) is clear.

Tested on an iPhone5,2: "LM3534 reg 0x10=0x03 (on), DWI v1 attached",
and a brightness of 300 is written and read back.

Assisted-by: LLM
Signed-off-by: Berkay Kösebay <brkosebay@users.noreply.github.com>
The iPhone 5's backlight needs it.

Assisted-by: LLM
Signed-off-by: Berkay Kösebay <brkosebay@users.noreply.github.com>
…S5L8950X

On the iPhone 5 most bq27540 reads failed ("no answer for 0x14") and
the rest decoded to garbage: 65404 mV (0xff7c), and temperatures of
6272 C and -119 C (0xffab, 0x05ff) that the charger then acted on. The
driver took "the last 8 frames" after a fixed 4.5 ms sleep, never
checked the echo and read 16-bit values without a consistency check, so
a missing or extra frame came through as a valid reading.

Add "apple,s5l8950x-hdq-battery", which runs the transaction the way
the N42 iBEC (12H321) and iOS AppleHDQGasGaugeControl-109.30.4
(_sendBreak, _hdqRead8, hdqRead16) do:

- break: UCON bit 4 held for 200 us and cleared by hand, 40 us, drain;
- a 0 is sent as 0xC0 (not 0xE0), a 1 as 0xFE;
- each echoed frame is read back with a deadline and must equal what
  was sent; then 8 gauge frames, where a frame >= 0xF9 is a 1; frames
  with a UERSTAT error are dropped like iBEC's uart_getc does;
- 8-bit reads get 4 tries 70 us apart, and 16-bit values are read hi,
  lo, hi and taken only when both hi reads agree (10 tries).

A refresh reads into temporaries, rejects values no bq27540 reports
(2500..5000 mV, -40..+90 C, at most 100%, ...), and commits all of them
together. After a failure the bus rests for 5 s and the last good
values are served for up to 30 s, so one bad transaction never reaches
a consumer. Each failure or rejection logs one line with the raw frames
and their UERSTAT bytes.

The A5 boards ("apple,s5l8940x-hdq-battery") keep the original path.

Tested on an iPhone5,2: 100%, 4229 mV, -84 mA, 25.9 C, 1448/1450 mAh
(design 1440), 160 cycles.

Assisted-by: LLM
Signed-off-by: Berkay Kösebay <brkosebay@users.noreply.github.com>
The P105 already uploads its N1 firmware in the kernel. The N42
supplies a raw HBPP firmware packet and separate proximity and panel
calibration blobs. Load the blobs from the DT or firmware files, and
take the reference clock divider from apple,ref-clk-div-val.

The scan-start SET report (0xbf) takes its value from the per-board
apple,scan-start-report property.

Assisted-by: LLM
Signed-off-by: Berkay Kösebay <brkosebay@users.noreply.github.com>
Add what the N41 and N42 share to s5l8950x-5.dtsi, from the iPhone5,2
iOS 8.4.1 (12H321) ADT and the 10.3.4 (14G61) ADT and kernelcache:

- gpio-keys: Home, Hold, Volume Up and Down, and the ringer switch;
- I2C0: the D1972 PMU (0x74) with its charger, the touch power switches
  and WL_REG_ON; the LM3534 backlight (0x63) and the DWI block;
- I2C1: the LIS3DSH accelerometer and AK8963 compass (no driver binds
  them yet);
- SPI1: the Gen1 Z2 touch controller, its 32768 Hz PWM clock, and the
  firmware name and clock values that let the kernel boot it;
- UART3: the BCM4334 Bluetooth;
- UART5 as HDQ for the bq27540 gauge, with Apple's transaction;
- EHCI with the BCM4334 Wi-Fi on HSIC port 4;
- the CLCD and DSIM, disabled until there is a display driver.

Each consumer's PMGR gate is enabled next to it. The I2C controllers
take their gates as their clock, so the driver holds them from probe.

The wifi0, bluetooth0 and touchscreen0 aliases let the boot loader fill
in the unit's MAC and Bluetooth addresses and its touch calibration.
Without the Wi-Fi address brcmfmac would use the NVRAM placeholder
02:00:00:00:00:00.

Tested on an iPhone5,2: WPA2 on 5 GHz with DHCP, DNS and HTTP; the
Bluetooth patchram loads; the gauge, charger, backlight and touch work
as their driver commits describe; the buttons probe.

Assisted-by: LLM
Signed-off-by: Berkay Kösebay <brkosebay@users.noreply.github.com>
Assisted-by: LLM
Signed-off-by: Berkay Kösebay <brkosebay@users.noreply.github.com>
@henrikklee

Copy link
Copy Markdown

Hi @brkosebay, thank you for the whole iPhone 5 stack (#59–#69). We've reviewed every PR's own commits against today's armv7. They are drafts, so we haven't changed or merged anything. Here is what each needs; when you mark one ready for review, we take it from there. We're happy to do the smaller fixes on your branches if you prefer; just say so.

Base: #58's 11 commits are already on armv7 (via #88), so #59 applies cleanly today. The stack conflicts because of #60 (below).

Also: please use your full name in the sign-off on the commits that have only "Berkay". Thanks again; there's a lot of careful work in here.

@brkosebay

Copy link
Copy Markdown
Author

Thanks for the review. We checked every point against the code and on the phone. Almost all of it holds, and the fixes are now pushed to #59–#68, rebased on current armv7. #69 is held back for now (see below).

Tested on the iPhone 5: the whole stack ran as one kernel on the installed system (NAND root), launched from iOS:

  • storage: read-write root, write fences, summary audit clean;
  • NAND: geometry and LwVM partitions;
  • SGX: the first-open microkernel start; flips latched at vblank, 1102 flips, none late; rotation;
  • charger: stays disarmed, gauge fine;
  • sensors: all three IIO devices read, rotation;
  • touch and buttons;
  • camera: both cameras stream;
  • cpufreq: 600/840/1104 MHz, transitions clean;
  • USB: NCM at about 8 MB/s.

KUnit: 123/123.

#59–#68 fixes: done as you describe, except two that aren't safe on their own on the N42:

#60: dropping our decoder for #89's also needed a replacement for the N42 identity flag a620d6f added, which #69 uses in seven places; it is a profile flag now. #89's read-only scattered LwVM view now also runs on the writable N42: it came up with p1–p4 and reads the iOS partitions.

#65: besides your points, it still carried the two #64 watchdog commits; they're gone now.

#69: agreed, let's settle the shape first, split as you suggest. Three findings from testing bear on it:

  1. A writer bug that broke iOS. Foreground GC could save a checkpoint before a new landing block's first record, so the block's first summary entry had a nonzero relative sequence. iOS's GC asserts on that (0 == btoc->weaveSeqAdd) and panicked on every boot.

    • The fix assigns the BTOC base at the first record, checks each summary before it is programmed, and audits all published summaries at load; any bad one keeps the disk read-only.
    • It's covered by KUnit, including the foreground-GC regression, and was flown on the N42: foreground GC retired 128 victims and moved 207 pages over four checkpoints (32 donors each), the audit stayed at 0 flagged across 1026 closed blocks, and iOS booted afterwards with no new panic.

    This belongs in part 2, and the audit is its data gate.

  2. Out of space under sustained writes. A fragmented fill, then trim, then refill load ran the writer out of free blocks (critical space allocation error) while ext4 still had about 1.7 GB free, ending in a journal abort and a watchdog reset.

  3. The Data check. iOS 8 encrypts the Data partition at the block level, so Linux only ever sees the HFSX header as ciphertext. The old check refused every install, and the last commit fell back to the installer's iOS-side shrink verification. You're right that this leaves no kernel-side check on the N42. We'd like the kernel to verify something it can read before the LwVM entry is cut, and will propose it with part 3.

Also on #69:

  • install_ctl isn't the install trigger (the LwVM header writes are); it only pads and checkpoints. Our installer relies on it, so it can go once part 4 checkpoints on flush and shutdown by itself.
  • The parameter count is higher than 49: 66 (36 in apple-sftl, 30 in h2fmi). Only 13 are settings; the rest are counters, which we'll move to debugfs or drop.
  • CDMA as a dmaengine driver: we'll look at what that needs and come back on it.

Sign-offs are fixed on all commits.

Report the side switch the way the iPad mini (P105) and iPad 2 (K93) do: KEY_MUTE while on silent instead of the SW_MUTE_DEVICE switch event, so desktops that bind the mute key (Omarchy's mute switch handler) act on it.

Assisted-by: LLM
Signed-off-by: Berkay Kösebay <brkosebay@users.noreply.github.com>
@brkosebay

Copy link
Copy Markdown
Author

Small update: the side switch now reports KEY_MUTE while on silent, the same way the iPad mini (P105) and iPad 2 (K93) trees do, instead of the SW_MUTE_DEVICE switch event. Desktops that bind the mute key then act on it. Checked on the iPhone 5: the switch interrupts arrive and the desktop's mute handler follows it. The rest of the stack is restacked on top, with no other changes.

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