diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 0e0607929..01545e31d 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -122,6 +122,8 @@ jobs: run: sh tools/exynos-tee-der-test.sh - name: The rule that holds the Galaxy S9's M3s at 1794 MHz unless the over-current guard's stage two allows more (hakios.exynos_cpufreq_ceiling.h) (sanitizers) run: sh tools/exynos-cpufreq-ceiling-test.sh + - name: The Galaxy S9's charging rules, its charger guard and the fast-charge path's conditions (hakios.max77705_charge.h) (sanitizers) + run: sh tools/max77705-charge-test.sh - name: The USB gadget's CDC-ACM descriptors and control requests (hakios.usb_acm.h) hold together (sanitizers) run: sh tools/usb-acm-test.sh - name: The UFS driver's commands and the whitelist every one passes before it reaches the flash (hakios.ufshc_upiu.h) hold against mutated commands (sanitizers) diff --git a/docs/ios27/CHARGING-TEST.md b/docs/ios27/CHARGING-TEST.md new file mode 100644 index 000000000..c1568c161 --- /dev/null +++ b/docs/ios27/CHARGING-TEST.md @@ -0,0 +1,71 @@ +# Fast charging on the Galaxy S9: the owner's test + +What to do with a Samsung fast wall charger (Adaptive Fast Charging, 9 V; for example the EP-TA20 that came with the S9, +"Adaptive Fast Charging" printed on it) and the phone's cable. This is the one step a computer's port cannot test. +The facts and the rules are in [CHARGING.md](CHARGING.md). + +## Before you start + +- The phone runs a kernel with the charger guard and the fast-charge path on: `sweetchg=2 sweetafc=1` in its boot line + (phone 2 was left so on 24 November; on another phone flash such an image first, see "The images"). +- Battery under 90 % (under 98 % is required; lower shows the fast charge better). Between 18 and 35 C: indoors, not in + the sun, not under a pillow. +- Stay with the phone for the 15 minutes. Put it on a hard surface. + +## Steps + +1. Plug the phone into the Mac mini (its USB console), unlocked, screen on. On the Mac mini: + `~/s9/agents/scion-phoneB/con.sh "sysctl kern.hakios.chg_afc"` must log `1`, and + `con.sh "sysctl kern.hakios.chg_type"` must log `2` (a computer's charging port) or `1`. Write down the battery %. +2. Unplug from the Mac. Plug the cable into the fast wall charger, then into the wall. +3. Watch the phone for 15 minutes. Touch its back now and then. +4. Unplug from the wall charger and plug back into the Mac mini. On the Mac mini: + `~/s9/agents/scion-phoneB/con.sh log` (the kernel's whole log again), then from its log file + (`cat ~/s9/agents/scion-phoneB/logs/current`): + `grep -a "STARLTE CHG" $(cat ~/s9/agents/scion-phoneB/logs/current)` + +## What you should see + +On the phone: the battery's lightning bolt within a few seconds; the percentage going up about 1 % every 1-1.5 +minutes below 80 % (at 5 V it is about half that). The phone gets warm, not hot. + +In the log, in this order: + +``` +STARLTE CHG: ... a wall charger at 5 V (DCP) ... the charger seen as a DCP +STARLTE CHG: input limit: 0xc0 ... -> 0x13, read back 0x13 500 mA while asking +STARLTE CHG: AFC: 9 V request sent (0x46): ok +STARLTE CHG: AFC: answer opcode 0x21 result 0 VBADC 6; USBC_STATUS1 VBADC 6 +STARLTE CHG: AFC: 9 V; the input and charge current go to 1650/2150 mA at the next second +STARLTE CHG: input limit: 0xc0 0x13 -> 0x41, read back 0x41 1650 mA +STARLTE CHG: charge current: 0xb9 0x6a -> 0x6b, read back 0x6b 2150 mA +STARLTE CHG: charging allowed; a fast charger at 9 V (AFC); on, ... +``` + +The console after plugging back into the Mac: `con.sh "sysctl kern.hakios.chg_afc_good"` is 1 or more, +`chg_afc_drops` says how many times it went back to 5 V (0 is expected unless the battery reached 98 % or warmed past 39 C). + +If the charger is not an AFC charger, or the answer is not good: `AFC: no 9 V; 5 V limits, not asked again on this +cable`, and it charges at 5 V, 1800 mA in, 2100 mA charge. That is a safe result, not a failure of the phone. + +## Stop at once (unplug from the wall) if + +- the phone is too hot to hold, or the battery or the charger's thermistor in the log reads 41 C or more and it is still + at 9 V (it should have gone back to 5 V by itself at 39 C, and stopped charging at 41 C); +- the phone or the charger smells, swells, clicks or the screen flickers; +- the percentage goes down while plugged in for more than 2 minutes; +- the log shows `DIFFERS`, `A READ FAILED`, `a fault` or `45 C or more` (the guard has already switched charging off; + unplug and send the log). + +## The images + +On the Mac mini, in `~/s9/agents/fastcharge-1124/img/` (the fork's charger-guard commit, kernel k4): + +| image | boot line | use | +|---|---|---| +| `xnu-boot-fa.img` | phone 2's usual line + `sweetafc=1` (the guard comes from the kernel's default line) | this test; what phone 2 was left on | +| `xnu-boot-fg.img` | phone 2's usual line | the guard only, no fast charge | +| `~/s9/agents/smooth-1123/img/xnu-boot-L6btm.img` | phone 2's usual line, the kernel before this work | charging stops about 70 s after boot | + +To put one on phone 2: `~/s9/agents/scion-phoneB/run.sh IMAGE "SWEET LIMITS: SpringBoard" 420` (it goes to TWRP, +writes BOOT only, reads it back, boots). diff --git a/docs/ios27/CHARGING.md b/docs/ios27/CHARGING.md new file mode 100644 index 000000000..85ba1e6fa --- /dev/null +++ b/docs/ios27/CHARGING.md @@ -0,0 +1,190 @@ +# Charging the Galaxy S9 under iOS 27 (and fast charging) + +24 November 2026, phone 2 (23edfb2021027ece). What charges the S9, what Samsung's kernel does about it, what iOS 27 +did before this change, and what the charger guard (`sweetchg=`) and the fast-charge path (`sweetafc=`, off by +default) do now. The owner's test plan for a fast wall charger is [CHARGING-TEST.md](CHARGING-TEST.md). + +The facts come from Samsung's kernel for this phone (Scion-Project/android_kernel_samsung_exynos9810, lineage-24.0: +`drivers/battery_v2/max77705_charger.c`, `sec_battery.c`, `drivers/muic/max77705-muic.c`, `max77705-muic-afc.c`, +`include/linux/muic/max77705-muic.h`, `include/linux/ccic/max77705.h`, and the device tree +`exynos9810-starlte_eur_open_26.dts`). That code is GPL. It was read for numbers and register meanings only. Nothing of +it is copied here or in the code. + +"Measured" means read from phone 2. "Inferred" means taken from the source or from what was measured, but not seen. + +## The chips + +| part | what it does | where | +|---|---|---| +| MAX77705, charger block | the switching charger: input current limit, charge current, float voltage, top-off, the charger's own watchdog and safety timer | I2C 0x69 on CMGP USI03's HSI2C (0x14360000) | +| MAX77705, fuel gauge | ModelGauge: state of charge (RepSOC), cell voltage, current, capacity. It learns the battery by itself | I2C 0x36, same bus | +| MAX77705, MUIC / USB-C controller | VBUS detection, BC 1.2 charger type, USB-C CC and PD, the D+/D- switch, the AFC 9 V handshake. A firmware that takes opcodes | I2C 0x25, same bus | +| MAX77705, top (PMIC part) | the chip's ID, interrupts, haptic block | I2C 0x66, same bus | +| AP's ADC (CMGP, 0x14230000) | the thermistors: channel 4 the battery, channel 3 the charger, channel 1 the USB port | not the MAX77705 | +| S2MPS18/S2MPS19 | the SoC's main PMICs (CPU, GPU, memory rails). Not part of charging | SPMI/ACPM | +| wireless charger (mfc-charger) | Qi pad charging. Not used or touched here | its own I2C | + +The battery is 3000 mAh nominal (`battery_full_capacity`); the gauge's design value is 2967 mAh (`fuelgauge,capacity`). +Phone 2's gauge says 3104 mAh full now (measured). + +## How Samsung picks the charger type + +1. The MUIC sees VBUS and runs BC 1.2 detection by itself. The result is `BC_STATUS` (0x08): bit 7 VBUS, [1:0] ChgTyp + (1 a plain USB port, SDP; 2 a computer's charging port, CDP; 3 a dedicated charger, DCP), bit 2 DCD time-out, + [5:3] PrChgTyp (Samsung 2 A, Apple 0.5/1/2 A/12 W, 3 A DCP). +2. The USB-C side (CC pins, Rp current, PD) is read as well. A USB-C source with Rp 1.5/3 A, or PD, becomes cable + type `PDIC` and is charged by its power (15 W at most here, `pd_charging_charge_power`). +3. The MUIC driver turns this into an "attached device" (TA, USB, CDP, Unofficial TA, DCD time-out, AFC 5 V/9 V ...). + The battery driver turns that into a cable type and takes that type's current group from `cable-info`. +4. A TA (DCP) is a candidate for AFC. + +Measured on phone 2 at the Mac mini's port: `BC_STATUS` 0x82 (VBUS, CDP, no special type), USB-C CC0 0xb1. In TWRP, +Samsung's own kernel called it `PDIC` (cable 19) and set 3000 mA in, 2150 mA charge (15 W, the port's USB-C Rp 3 A). + +## The AFC (Samsung Adaptive Fast Charging) 9 V handshake + +Inferred from Samsung's MUIC driver; not seen (no fast charger here): + +1. A DCP is seen. The MUIC driver waits 500 ms ("Maxim's request"). +2. The battery driver lowers the input limit to `pre_afc_input_current`, 500 mA. +3. The MUIC driver sends opcode 0x21 (`COMMAND_AFC_RESULT_READ`) with one byte: 0x46 for 9 V (0x08 for 5 V). Opcodes + go to register 0x21 (`OPCODE_WRITE`), then 0x41 (`OPCODE_WRITE_END`). +4. The MUIC firmware talks to the charger on D- (the AFC ping protocol). The answer comes back at 0x51 + (`OPCODE_READ`): the opcode, a result, the VBUS band (VBADC), then up to 8 bytes the charger sent. +5. Result 0 with VBADC 5, 6 or 7 (7.5-10.5 V) means the charger is at 9 V. Other results: 1 no CHGIN, 2 not a + high-voltage DCP, 3 not a DCP, 4 no ping answer, 5 unsupported byte (retry with one the charger offered), 6/7 VBUS + did not change, 8-10 a short on the cable, 11 not Rp 56 k, 16-18 protocol errors. Samsung retries 3 times, then + resets AFC (`BC_CTRL2` 0x13) if VBUS is still high. +6. The battery driver then sets the 9 V limits: 1650 mA in, 2150 mA charge. +7. Samsung asks for 5 V again when the battery is full, when its thermal level asks for less, and in store mode. With + the charger thermistor at 46 C or more it holds 9 V at 1000 mA in, 1250 mA charge. +8. For a TA the MUIC's D+/D- switch is left open (`COM_OPEN`), not connected to the AP's USB. + +## Samsung's limits (exynos9810-starlte_eur_open_26.dts) + +| charger | cable type | input limit | charge current | session time | +|---|---|---|---|---| +| plain USB port (SDP) | USB (4), current_group_12 | 475 mA | 550 mA | 36000 s (`usb_charging_total_time`) | +| computer's charging port (CDP) | USB_CDP (5), current_group_3 | 1500 mA | 1500 mA | 18000 s | +| wall charger at 5 V (DCP) | TA (3), the default | 1800 mA | 2100 mA | 18000 s (`normal_charging_total_time`) | +| fast charger at 9 V | 9V_TA (6), current_group_4 | 1650 mA | 2150 mA | 10800 s (`hv_charging_total_time`) | +| DCD time-out, unknown | TIMEOUT (30), current_group_1 | 500 mA | 500 mA | | +| USB-C PD/Rp | PDIC (19) | by the source's power, 3000 mA at most | 2150 mA | | + +- Float voltage 4350 mV (`chg_float_voltage`; CNFG_04 0x13, measured). Top-off 300 then 150 mA. +- Full: charging switched off (CNFG_00 0x14, measured in TWRP's log). Recharge under 4280 mV or under 98 %. +- Temperatures (battery thermistor, tenths of a degree in the tree): + - 50.0 C and up, or 0.0 C and down: health "overheat"/"cold", charging stops. Back at 45.0 C / 5.0 C. + - 41.0 C and up, or 5.0 C and down ("swelling"): charging stops. Below 4000 mV it recharges at a 4150 mV float, + 1090 mA (hot) or 700 mA (cold). It ends at 39.0 C and below, 10.0 C and above. + - 15.0 C and down: 700 mA at most. Ends at 20.0 C. + - Charger thermistor 46.0 C and up with 9 V: 1000 mA in, 1250 mA charge. Ends at 44.0 C. +- Timers: Samsung's driver turns the charger's hardware fast-charge timer OFF (CNFG_01 FCHGTIME 0) and its + hardware restart off. It counts time itself (`expired_time` 10800 s, the total times above). +- The charger's watchdog (CNFG_00 bit 4) is turned on by Samsung's driver and kicked at every poll, every 10 s + (CNFG_06 WDTCLR = 01). If it is not kicked, the charger stops (CHG_DTLS 0xB). + +## What hardware enforces alone, and what needs software + +Inferred from the register meanings and from what Samsung's driver does: + +- **Hardware alone:** VBUS over-voltage (CHGIN OVLO), the input current limit once set, the charge current once set, the + float voltage once set, the charger's own die temperature (regulation, then thermal shutdown, CHG_DTLS 0xA), + battery-to-system over-current (B2SOVRC), battery over-voltage, the watchdog once enabled, top-off and done. +- **Software only:** the battery's temperature (the thermistor is on the AP's ADC, not the charger, so nothing in the + chip knows it), the charger type's limits (the chip takes whatever input limit it is given), the safety timer + (Samsung turns the hardware timer off), recharging after full (hardware restart is off), keeping the watchdog fed, + and everything about AFC. + +## What iOS 27 did before this change (measured on phone 2) + +The iOS 27 kernel (Scion-Project/xnu, `starlte_muic.c` and `SweetBattery.cpp`, `sweetfg=1`) only read the gauge and +`BC_STATUS`. It never wrote the charger. So: + +1. **Charging stopped about 70 s after every boot.** S-Boot leaves the charger's watchdog on (CNFG_00 0x15) and kicks it + last. Nobody kicked it in iOS. At 15:35:01, 69 s after the kernel started, DETAILS_01 went from 0x33 (top-off) to + 0x3b (CHG_DTLS 0xB, watchdog expired), and stayed there for the whole window. iOS showed "cable in, not charging". + With a full battery this hardly shows. With a half-empty one the phone would not have charged in iOS at all. +2. **No battery temperature at all.** iOS was given a fixed 25.0 C. Nothing stopped charging when hot or cold. +3. **No safety timer.** S-Boot and TWRP leave the hardware timer off (CNFG_01 0xb8, measured). +4. **"Charging" guessed from the current** (more than 20 mA into the battery), not from the charger. +5. **After a full charge in TWRP the charger is left off** (Samsung writes CNFG_00 0x14 at full). S-Boot turned it on + again at the next boot here (CNFG_00 0x15 in iOS, measured), so this one did not reach iOS. + +What S-Boot leaves (measured in iOS, before anything was written): CNFG_00 0x15, CNFG_01 0xb8, CNFG_02 0x5e (1500 mA), +CNFG_03 0x99, CNFG_04 0x13 (4350 mV), CNFG_06 0x0c (CHGPROT unlocked), CNFG_09 0x3b (1500 mA), CNFG_12 0x60. That is +Samsung's CDP current group, so S-Boot detects the type the same way. + +## What the kernel does now + +`bsd/dev/starlte_charger.c` in the fork, with the rules in `hakios.max77705_charge.h` (scion-kernel +`drivers/power/max77705/`, a copy in the fork's `bsd/dev/`; host tests `tools/max77705-charge-test.sh`, in CI). + +| boot argument | what happens | +|---|---| +| `sweetchg=0` | nothing new. SweetBattery as before. (`sweetchg=2` is in the kernel's built-in default line since this change: `sweetchg=0` on the loader's line turns it off) | +| `sweetchg=1` | read only. Every second: the charger, MUIC, gauge and both thermistors. iOS gets the real battery temperature, "charging" from the charger's own state, "full" from top-off/done, and adapter details (type, volts, amps). The log says what the guard would do ("would: ..."). Register dump at start and every 5 minutes | +| `sweetchg=2` | the guard. As 1, and it writes the charger (volatile registers only, each read back): watchdog kicked every 10 s and re-armed if it expired; charging on or off by the temperature bands, the session time, full and recharge; the input limit and charge current set to Samsung's for the type, or less | +| `sweetafc=1` | with `sweetchg=2`: the fast-charge path, OFF by default (below) | +| `sweetchg_drain=MIN` | a test: the first MIN minutes (30 at most) with the input at 100 mA and charging off, so the phone runs on its battery. Stops at 50 % or 40 C | + +The guard's rules (all from Samsung's numbers above; stricter where they differ): + +- Charger type from `BC_STATUS` only: SDP, CDP, DCP, or unknown (DCD time-out, Apple and other special chargers, ChgTyp + 0). USB-C Rp and PD are not used: a USB-C source is charged at its BC 1.2 type's limits, which are lower. +- Off at 41.0 C and up until 39.0 C and down; off at 5.0 C and down until 10.0 C and up; 700 mA at most at 15.0 C and + down until 20.0 C. No swelling recharge (stricter than Samsung). +- 45.0 C or more, the float voltage reading above 4350 mV, VBUS over-voltage, no or dead battery, the timer's fault, + thermal shutdown, or a read-back that differs: charging off and latched off until the cable is pulled. +- No temperature reading: charging off (not latched). +- Session time per type (table above): then off until the cable is pulled. +- Full when the charger says done; on again under 4280 mV or 98 %. +- Never written: the float voltage, the timers, CNFG_01/03/04/05/07/08/10-12, the gauge, the PMIC, anything + non-volatile. + +### The fast-charge path (`sweetafc=1`, off by default) + +9 V is asked for only when all of these hold: + +- the MUIC says DCP (ChgTyp 3 with VBUS, no special type), for at least a second; +- the battery thermistor is above 15.0 C and at most 39.0 C, the charger thermistor is under 44.0 C, the battery is + under 98 %, the chip reports no fault, VBUS reads 5 V; +- it has not failed or been dropped before on this cable. + +Then: the input limit goes to 500 mA, the D+/D- switch is opened (as Samsung does for a TA), and the 9 V request is +sent. 9 V counts only if the answer is result 0 with VBADC 5-7 and `USBC_STATUS1` also reads 7.5-10.5 V. Only then +the limits go to 1650 mA in, 2150 mA charge. + +Every second while at 9 V: if the battery leaves 15-39 C, the charger thermistor reaches 44 C, VBUS leaves 7.5-10.5 V, +the battery reaches 98 %, a fault shows, or a register reads back differently, the input goes to 500 mA, 5 V is asked +for, and the standard DCP limits (1800/2100 mA at 5 V) apply once VBUS reads 5 V. While VBUS is above 5.5 V outside a 9 V +session, the input stays at 500 mA. No new 9 V request on that cable. + +On a computer port (SDP or CDP) and on unknown chargers it never asks. + +## Measured on phone 2 (computer port: the Mac mini) + +The minute-by-minute record is the run's NOTES (`~/s9/agents/fastcharge-1124/NOTES.md` on the Mac mini). Summary: + +- **The port.** `BC_STATUS` 0x82: VBUS, ChgTyp 2 = CDP, no special type, no DCD time-out. VBUS band 2 (4.5-5.5 V). + The guard calls it "a computer's charging port (CDP)" (`kern.hakios.chg_type` = 2) and sets Samsung's CDP limits, + 1500 mA in, 1500 mA charge. S-Boot had already set the same (no write needed). +- **Read-only window** (`sweetchg=1`, 15:33-15:43): the watchdog expiry above (charging stopped 69 s after the kernel + started and never came back). Battery thermistor 25.8-29.2 C (TWRP read 25.6 C just before, through Samsung's own + driver: the same reading); charger thermistor 26-32 C. +- **Guard window** (`sweetchg=2 sweetafc=1 sweetchg_drain=20`, 15:44-16:09): watchdog kicked every 10 s, DETAILS_01 + never 0x3b again. 20 minutes on the battery (input 100 mA, charging off; light load): 100 % to 90 %, -0.9 A at the + start, battery 29.2-31.0 C. At the end the guard set the CDP limits back (CNFG_09 0x03 -> 0x3b) and charging on + (CNFG_00 0x14 -> 0x15), each read back the same. The charger went to fast charge (DETAILS_01 0x31), then CV + (0x32). iOS showed the charging bolt. No 9 V request: the port is a CDP (the AFC log lines: none). +- **End state** (the kernel's default line with `sweetchg=2`, plus `sweetafc=1`): the guard on from boot, CDP, + 1500/1500 mA, charging in CV at 92 %, +373-419 mA, battery 30.1-31.1 C, charger 33.7-35.8 C, 14 watchdog kicks in + the first two minutes, 0 writes beyond the kicks, 0 read-backs that differed, 0 9 V requests. + +Register writes made on phone 2 in this work, all volatile charger registers, each read back the same: +CNFG_06 WDTCLR kick (0x0c -> 0x0d), CNFG_00 bit 0 (0x15 <-> 0x14), CNFG_09 input limit (0x3b <-> 0x03 for the drain +test). Not written: CHGPROT (it read unlocked), CNFG_02 (S-Boot's 1500 mA was already Samsung's CDP value), anything of +the MUIC (no DCP), the gauge, the PMIC, any NVM or OTP. + +Not tested (needs a fast wall charger): the DCP limits, the 9 V request and its answer, the 9 V limits, the drops back to +5 V. [CHARGING-TEST.md](CHARGING-TEST.md) is that test. diff --git a/drivers/map b/drivers/map index 4da583c6d..88f111a08 100644 --- a/drivers/map +++ b/drivers/map @@ -74,6 +74,7 @@ audio/codec bsd/dev/hakios.cs47l93.c power bsd/dev/hakios.battery.c power iokit/Kernel/hakios.battery.cpp power/max77705 bsd/dev/hakios.max77705.c +power/max77705 bsd/dev/hakios.max77705_charge.h power/exynos bsd/dev/hakios.exynos_cpufreq.c power/exynos bsd/dev/hakios.exynos_cpufreq_ceiling.h power/exynos bsd/dev/hakios.scion_gov.c diff --git a/drivers/power/max77705/hakios.max77705_charge.h b/drivers/power/max77705/hakios.max77705_charge.h new file mode 100644 index 000000000..3c9a7e67b --- /dev/null +++ b/drivers/power/max77705/hakios.max77705_charge.h @@ -0,0 +1,488 @@ +/* SPDX-License-Identifier: MIT-0 */ +/* + * The Galaxy S9's charging rules: what the MAX77705's charger may be told, for each kind of charger and each battery + * temperature. Plain C, no kernel: the iOS 27 fork's charger guard (bsd/dev/starlte_muic.c, sweetchg= and sweetafc=) asks + * these functions, and tools/max77705_charge_test.c checks them on a computer (tools/max77705-charge-test.sh). + * docs/ios27/CHARGING.md has the facts and where each number comes from. + * + * Every number is Samsung's own for this phone (exynos9810-starlte_eur_open_26.dts of its kernel: cable-info's current + * groups, the battery node's temperature bands and timers; drivers/battery_v2/max77705_charger.c for the register + * encodings; drivers/muic/max77705-muic*.c for the charger types and the AFC answer). Nothing here goes above them: + * - the input limit and the charge current of each charger type, at most Samsung's for that type; + * - charging stopped at 41.0 C and above or 5.0 C and below (Samsung's "swelling" block; stricter than its 50/0 C + * health limits, and without its reduced-float recharging there), back at 39.0 C and below and 10.0 C and above; + * 700 mA at most at 15.0 C and below (back above it at 20.0 C); + * - a charging session's time limited (Samsung's software timers: its hardware one is switched off by its driver); + * - 9 V (Samsung Adaptive Fast Charging) only on a charger the MUIC calls a dedicated charger (DCP), with the battery + * between 15.0 C and 39.0 C, the charger chip's own thermistor under 44.0 C, the battery not full, the chip + * reporting no fault, and only after the charger answered the 9 V request with VBUS read in the 7.5-10.5 V band. + * On any doubt: 5 V and the standard limits, or less. + * The float voltage (CNFG_04) is never written: it is only read, and charging is stopped if it reads above 4350 mV. + * Nothing of the chip's non-volatile memory is touched by anything that uses this file. + */ +#ifndef HAKIOS_MAX77705_CHARGE_H +#define HAKIOS_MAX77705_CHARGE_H + +#include +#include + +/* ---- the chip's registers (charger at 0x69, MUIC at 0x25) ------------------------------------------------------- */ + +#define CHG_REG_INT_OK 0xB2 /* bit 6 CHGIN_OK */ +#define CHG_REG_DTLS_00 0xB3 /* [6:5] CHGIN_DTLS: 3 = VBUS valid, 2 = over-voltage */ +#define CHG_REG_DTLS_01 0xB4 /* [3:0] CHG_DTLS, [6:4] BAT_DTLS */ +#define CHG_REG_DTLS_02 0xB5 /* [3:0] BYP_DTLS */ +#define CHG_REG_CNFG_00 0xB7 /* [3:0] mode (bit 0 CHG, bit 2 BUCK), bit 4 WDTEN */ +#define CHG_REG_CNFG_01 0xB8 /* [2:0] FCHGTIME, [5:4] CHG_RSTRT (read only here) */ +#define CHG_REG_CNFG_02 0xB9 /* [5:0] the charge current, 50 mA a step; [7:6] OTG_ILIM (kept) */ +#define CHG_REG_CNFG_04 0xBB /* [5:0] the float voltage (read only here) */ +#define CHG_REG_CNFG_06 0xBD /* [1:0] WDTCLR, [3:2] CHGPROT (11: unlocked) */ +#define CHG_REG_CNFG_09 0xC0 /* [6:0] the input limit, 25 mA a step; bit 7 CHG_EN (kept) */ + +#define CHG_CNFG00_CHG 0x01u +#define CHG_CNFG00_BUCK 0x04u +#define CHG_CNFG00_WDTEN 0x10u +#define CHG_CNFG02_CC_MASK 0x3Fu +#define CHG_CNFG09_LIM_MASK 0x7Fu +#define CHG_CNFG06_WDTCLR_MASK 0x03u +#define CHG_CNFG06_WDTCLR 0x01u +#define CHG_CNFG06_CHGPROT_MASK 0x0Cu + +#define MUIC_REG_USBC_STATUS1 0x06 /* [7:4] VBADC: VBUS in bands (below) */ +#define MUIC_REG_BC_STATUS 0x08 /* bit 7 VBUSDet, [5:3] PrChgTyp, bit 2 DCDTmo, [1:0] ChgTyp */ +#define MUIC_OP_CONTROL1_W 0x06 /* the switch: COM_OPEN 0x00, COM_USB 0x09 */ +#define MUIC_OP_AFC_RESULT 0x21 /* COMMAND_AFC_RESULT_READ: the AFC request with its byte; the answer at 0x51 */ +#define AFC_TX_9V 0x46 /* Samsung's byte for 9 V (max77705_muic_afc_hv_set) */ +#define AFC_TX_5V 0x08 /* ... and for 5 V */ + +/* VBADC (USBC_STATUS1 [7:4], and the AFC answer's second byte): 2 = 4.5-5.5 V, 5 = 7.5-8.5, 6 = 8.5-9.5, 7 = 9.5-10.5 */ +#define VBADC_5V_MAX 2u +#define VBADC_9V_MIN 5u +#define VBADC_9V_MAX 7u + +/* ---- the encodings (max77705_charger.c's set_/get_ functions) ---------------------------------------------------- */ + +/* CNFG_09: (code + 1) x 25 mA, 100 mA at codes 0-3, 3200 mA at 0x7F */ +static inline uint32_t +chg_input_ma_of(uint8_t code) +{ + code &= CHG_CNFG09_LIM_MASK; + return code <= 3 ? 100u : code >= 0x7F ? 3200u : (uint32_t)(code + 1u) * 25u; +} + +/* the code for at most ma (rounded down; never above ma, never under 100 mA's code) */ +static inline uint8_t +chg_input_code(uint32_t ma) +{ + if (ma < 100u) { + return 0; + } + if (ma > 3200u) { + ma = 3200u; + } + return (uint8_t)(ma / 25u - 1u); +} + +/* CNFG_02: code x 50 mA, 100 mA at codes 0-2 */ +static inline uint32_t +chg_charge_ma_of(uint8_t code) +{ + code &= CHG_CNFG02_CC_MASK; + return code <= 2 ? 100u : (uint32_t)code * 50u; +} + +static inline uint8_t +chg_charge_code(uint32_t ma) +{ + if (ma < 150u) { + return 0; /* (100 mA: the least the chip does) */ + } + if (ma > 3150u) { + ma = 3150u; + } + return (uint8_t)(ma / 50u); +} + +/* CNFG_04 [5:0]: 4000 + 50 mV a step to 0x04, then 4200 + 10 mV a step */ +static inline uint32_t +chg_float_mv_of(uint8_t code) +{ + code &= 0x3F; + return code <= 4 ? 4000u + code * 50u : 4200u + (code - 4u) * 10u; +} + +/* ---- the charger types and Samsung's limits for each ------------------------------------------------------------- */ + +enum chg_type { + CHG_NONE = 0, /* no VBUS */ + CHG_SDP, /* a computer's plain USB port (BC 1.2 SDP): Samsung's USB */ + CHG_CDP, /* a computer's charging port (BC 1.2 CDP) */ + CHG_DCP, /* a dedicated charger (DCP): Samsung's TA, at 5 V */ + CHG_AFC_9V, /* a DCP that answered the 9 V request: Samsung's 9V_TA */ + CHG_UNKNOWN, /* VBUS with anything else: DCD time-out, an Apple or other special charger, ChgTyp 0 */ + CHG_TYPES +}; + +struct chg_limit { + uint32_t input_ma; + uint32_t charge_ma; + uint32_t session_s; /* the charging time allowed in one session (Samsung's *_charging_total_time) */ + const char *name; +}; + +/* cable-info (current_group_12: USB; _3: CDP; default: TA; _4: 9V_TA; _1: TIMEOUT), the battery node's total times */ +static const struct chg_limit chg_limits[CHG_TYPES] = { + [CHG_NONE] = { 0, 0, 0, "no charger" }, + [CHG_SDP] = { 475, 550, 36000, "a computer's USB port (SDP)" }, + [CHG_CDP] = { 1500, 1500, 18000, "a computer's charging port (CDP)" }, + [CHG_DCP] = { 1800, 2100, 18000, "a wall charger at 5 V (DCP)" }, + [CHG_AFC_9V] = { 1650, 2150, 10800, "a fast charger at 9 V (AFC)" }, + [CHG_UNKNOWN] = { 500, 500, 18000, "an unknown charger" }, +}; + +#define CHG_FLOAT_MAX_MV 4350u /* chg_float_voltage */ +#define CHG_PRE_AFC_INPUT_MA 500u /* pre_afc_input_current: the input limit while the voltage is asked for */ +#define CHG_LOW_TEMP_CHARGE_MA 700u /* swelling_low_temp_current */ + +/* BC_STATUS -> the type. ChgTyp 1 with DCDTmo, or ChgTyp 1/2 with a special PrChgTyp, is not a plain port (Samsung's + * max77705_muic_check_chgtyp: DCD time-out, "unofficial TA"): unknown, the lowest limits. */ +static inline enum chg_type +chg_type_of_bc(uint8_t bc) +{ + unsigned chgtyp = bc & 3u, dcdtmo = (bc >> 2) & 1u, prchgtyp = (bc >> 3) & 7u; + + if (!(bc & 0x80u)) { + return CHG_NONE; + } + if (chgtyp == 1 && dcdtmo) { + return CHG_UNKNOWN; + } + if ((chgtyp == 1 || chgtyp == 2) && prchgtyp >= 1 && prchgtyp <= 5) { + return CHG_UNKNOWN; + } + switch (chgtyp) { + case 1: return CHG_SDP; + case 2: return CHG_CDP; + case 3: return CHG_DCP; + default: return CHG_UNKNOWN; + } +} + +/* ---- the temperature bands (tenths of a degree, as Samsung's device tree) --------------------------------------- */ + +#define T_STOP_HIGH_DC 410 /* swelling_high_temp_block */ +#define T_RESUME_HIGH_DC 390 /* swelling_high_temp_recov */ +#define T_STOP_LOW_DC 50 /* swelling_low_temp_block_2nd */ +#define T_RESUME_LOW_DC 100 /* swelling_low_temp_recov_2nd */ +#define T_REDUCE_DC 150 /* swelling_low_temp_block_1st: 700 mA at most */ +#define T_UNREDUCE_DC 200 /* swelling_low_temp_recov_1st */ +#define T_FAST_MIN_DC 150 /* 9 V only above the reduced band ... */ +#define T_FAST_MAX_DC 390 /* ... and under the swelling recovery point */ +#define T_CHG_FAST_MAX_DC 440 /* the charger's thermistor: chg_high_temp_recovery (46.0 C limits Samsung's 9 V) */ +#define T_ALARM_DC 450 /* the brief's own: above 45 C every change is given up */ +#define T_PLAUSIBLE_MIN_DC (-300) +#define T_PLAUSIBLE_MAX_DC 900 + +/* charging recharged after a full charge (recharge_condition_vcell, _soc) */ +#define RECHARGE_MV 4280u +#define RECHARGE_SOC 98u + +/* ---- the decision ------------------------------------------------------------------------------------------------ */ + +/* what the guard read this time */ +struct chg_in { + bool read_ok; /* every register below read */ + uint8_t bc; /* MUIC BC_STATUS */ + uint8_t vbadc; /* MUIC USBC_STATUS1 [7:4] */ + uint8_t dtls00, dtls01; /* charger DETAILS_00, _01 */ + uint8_t cnfg00, cnfg02, cnfg04, cnfg09; + bool temp_ok; /* the battery's thermistor read */ + int32_t temp_dc; /* the battery, tenths of a degree */ + bool chg_temp_ok; /* the charger's thermistor read */ + int32_t chg_temp_dc; + uint32_t soc; /* percent */ + uint32_t vcell_mv; + uint32_t now_s; /* seconds, any monotonic origin */ + bool afc_wanted; /* sweetafc=1 */ +}; + +/* what the guard keeps from one time to the next */ +struct chg_state { + enum chg_type type; /* the type in force: CHG_AFC_9V only after a good answer */ + bool stopped_hot, stopped_cold, reduced; /* the bands' hysteresis */ + bool full; /* the charger said done; charging again at the recharge condition */ + bool expired; /* the session's time ran out: nothing until the cable goes */ + bool afc_failed; /* the 9 V request went wrong once: not again until the cable goes */ + bool latched_off; /* a fault (a read-back that differed, the float above 4350 mV, an implausible + * temperature, 45 C): charging off, nothing written but that, until the cable goes */ + uint32_t session_start_s, charging_s, last_s; + uint32_t dcp_since_s; /* when the DCP was first seen (9 V asked 1 s after: Samsung waits 500 ms) */ + bool dcp_seen; +}; + +enum chg_why { + WHY_OK = 0, /* charging allowed at the type's limits */ + WHY_NO_VBUS, + WHY_READ_FAILED, + WHY_NO_TEMP, + WHY_HOT, + WHY_COLD, + WHY_FULL, + WHY_EXPIRED, + WHY_FAULT, + WHY_ALARM, + WHY_FLOAT_HIGH, +}; + +/* what to do now */ +struct chg_out { + bool enable; /* CNFG_00 bit 0 */ + uint32_t input_ma; /* CNFG_09: what it is to be set to (0: leave it) */ + uint32_t charge_ma; /* CNFG_02: ditto */ + bool ask_9v; /* send the 9 V request now */ + bool ask_5v; /* send the 5 V request now (back from 9 V) */ + bool rearm; /* the charger's watchdog expired (CHG_DTLS 0xB) with charging wanted: kick it, and charging + * off and on again, as Samsung's max77705_get_charging_health does */ + enum chg_why why; +}; + +static inline const char * +chg_why_name(enum chg_why why) +{ + static const char *const names[] = { "charging allowed", "no VBUS", "a read failed", "no battery temperature", + "too hot", "too cold", "full", "the session's time ran out", "a fault", + "45 C or more", "the float voltage reads above 4350 mV" }; + + return (unsigned)why < sizeof(names) / sizeof(names[0]) ? names[why] : "?"; +} + +static inline bool +chg_temp_plausible(int32_t dc) +{ + return dc >= T_PLAUSIBLE_MIN_DC && dc <= T_PLAUSIBLE_MAX_DC; +} + +/* CHG_DTLS: 0-2 charging (pre-qualification, fast CC, fast CV), 3 top-off, 4 done, 6 the fast-charge timer's fault, 8 off + * (no valid input, or charging off), 0xA off at the junction's thermal shutdown, 0xB off because the charger's watchdog + * expired (Samsung's driver counts 8, 0xA and 0xB as discharging, and re-arms the charger on 8 and 0xB) */ +#define CHG_DTLS_DONE 0x4u +#define CHG_DTLS_WDT_EXPIRED 0xBu + +/* the chip says something is wrong: VBUS over-voltage, the battery not OK, the timer's fault, thermal shutdown */ +static inline bool +chg_chip_fault(const struct chg_in *in) +{ + unsigned chgin = (in->dtls00 >> 5) & 3u, bat = (in->dtls01 >> 4) & 7u, chg = in->dtls01 & 0xFu; + + if (chgin == 2) { + return true; /* CHGIN > OVLO */ + } + if (bat == 0 || bat == 2 || bat == 5) { + return true; /* no battery, battery dead, battery over-voltage */ + } + return chg == 0x6 || chg == 0xA; +} + +/* the 9 V conditions, apart from the type */ +static inline bool +chg_fast_band(const struct chg_in *in) +{ + return in->temp_ok && in->chg_temp_ok && in->temp_dc > T_FAST_MIN_DC && in->temp_dc <= T_FAST_MAX_DC && + in->chg_temp_dc < T_CHG_FAST_MAX_DC && in->soc < RECHARGE_SOC; +} + +/* a new cable (or none): everything of the last one forgotten */ +static inline void +chg_state_reset(struct chg_state *s, uint32_t now_s) +{ + *s = (struct chg_state){ .type = CHG_NONE, .session_start_s = now_s, .last_s = now_s }; +} + +/* + * The guard's one step: from what was read and what it remembers, what the charger is to be told. Every limit given is at + * most Samsung's for the type in force; charging is off unless every condition holds. + */ +static inline struct chg_out +chg_step(struct chg_state *s, const struct chg_in *in) +{ + struct chg_out o = { .enable = false, .input_ma = 0, .charge_ma = 0, .why = WHY_OK }; + enum chg_type seen; + const struct chg_limit *lim; + uint32_t dt; + + if (!in->read_ok) { + /* nothing known: off is asked, nothing else (the caller writes only CNFG_00 when it could read it) */ + o.why = WHY_READ_FAILED; + return o; + } + seen = chg_type_of_bc(in->bc); + if (seen == CHG_NONE) { + chg_state_reset(s, in->now_s); + o.why = WHY_NO_VBUS; + return o; + } + /* the type in force: a 9 V session stays one while the MUIC still says DCP; anything else starts again */ + if (s->type == CHG_NONE || (s->type != seen && !(s->type == CHG_AFC_9V && seen == CHG_DCP))) { + bool was_9v = s->type == CHG_AFC_9V; + + chg_state_reset(s, in->now_s); + s->type = seen; + if (was_9v) { + s->afc_failed = true; /* (the MUIC changed its mind under 9 V: no new request on this cable) */ + } + } + dt = in->now_s >= s->last_s ? in->now_s - s->last_s : 0; + s->last_s = in->now_s; + + /* the faults: off and latched until the cable goes */ + if (s->latched_off) { + o.why = WHY_FAULT; + goto drop_9v; + } + if (chg_float_mv_of(in->cnfg04) > CHG_FLOAT_MAX_MV) { + s->latched_off = true; + o.why = WHY_FLOAT_HIGH; + goto drop_9v; + } + if (!in->temp_ok || !chg_temp_plausible(in->temp_dc)) { + o.why = WHY_NO_TEMP; + goto drop_9v; + } + if (in->temp_dc >= T_ALARM_DC) { + s->latched_off = true; + o.why = WHY_ALARM; + goto drop_9v; + } + if (chg_chip_fault(in)) { + s->latched_off = true; + o.why = WHY_FAULT; + goto drop_9v; + } + + /* the bands, with Samsung's hysteresis */ + if (in->temp_dc >= T_STOP_HIGH_DC) { + s->stopped_hot = true; + } else if (in->temp_dc <= T_RESUME_HIGH_DC) { + s->stopped_hot = false; + } + if (in->temp_dc <= T_STOP_LOW_DC) { + s->stopped_cold = true; + } else if (in->temp_dc >= T_RESUME_LOW_DC) { + s->stopped_cold = false; + } + if (in->temp_dc <= T_REDUCE_DC) { + s->reduced = true; + } else if (in->temp_dc >= T_UNREDUCE_DC) { + s->reduced = false; + } + + /* full, and charging again at Samsung's recharge condition */ + if ((in->dtls01 & 0xFu) == CHG_DTLS_DONE) { + s->full = true; + } else if (s->full && (in->vcell_mv < RECHARGE_MV || in->soc < RECHARGE_SOC)) { + s->full = false; + s->session_start_s = in->now_s; + s->charging_s = 0; + } + + /* the session's time: counted while charging is allowed and the chip charges */ + if ((in->cnfg00 & CHG_CNFG00_CHG) && (in->dtls01 & 0xFu) <= 0x3) { + s->charging_s += dt; + } + lim = &chg_limits[s->type]; + if (s->charging_s >= lim->session_s) { + s->expired = true; + } + + if (s->stopped_hot) { + o.why = WHY_HOT; + goto drop_9v; + } + if (s->stopped_cold) { + o.why = WHY_COLD; + goto drop_9v; + } + if (s->expired) { + o.why = WHY_EXPIRED; + goto drop_9v; + } + + /* 9 V: kept only inside its band; asked for once a DCP has been seen for a second, inside it */ + if (s->type == CHG_AFC_9V && (!chg_fast_band(in) || in->vbadc < VBADC_9V_MIN || in->vbadc > VBADC_9V_MAX || + s->reduced || s->full)) { + s->type = CHG_DCP; + s->afc_failed = true; + o.ask_5v = true; + } + if (s->type == CHG_DCP && !s->dcp_seen) { + s->dcp_seen = true; + s->dcp_since_s = in->now_s; + } + if (s->type == CHG_DCP && in->afc_wanted && !s->afc_failed && !s->full && !s->reduced && chg_fast_band(in) && + in->vbadc <= VBADC_5V_MAX && in->now_s - s->dcp_since_s >= 1) { + o.ask_9v = true; + } + + lim = &chg_limits[s->type]; + o.enable = !s->full; + o.why = s->full ? WHY_FULL : WHY_OK; + o.rearm = o.enable && (in->dtls01 & 0xFu) == CHG_DTLS_WDT_EXPIRED; + o.input_ma = lim->input_ma; + o.charge_ma = lim->charge_ma; + if (s->reduced && o.charge_ma > CHG_LOW_TEMP_CHARGE_MA) { + o.charge_ma = CHG_LOW_TEMP_CHARGE_MA; + } + /* VBUS above 5.5 V without a 9 V session in force (a request on its way, or a charger that went up by itself): + * the input held at the pre-AFC limit, which is under every type's */ + if (s->type != CHG_AFC_9V && in->vbadc > VBADC_5V_MAX) { + o.input_ma = CHG_PRE_AFC_INPUT_MA; + } + if (o.ask_9v) { + o.input_ma = CHG_PRE_AFC_INPUT_MA; + } + return o; + +drop_9v: + /* charging off; a 9 V session asked back to 5 V, the input held at the pre-AFC limit meanwhile */ + if (s->type == CHG_AFC_9V || in->vbadc > VBADC_5V_MAX) { + if (s->type == CHG_AFC_9V) { + s->type = CHG_DCP; + o.ask_5v = true; + } + s->afc_failed = true; + o.input_ma = CHG_PRE_AFC_INPUT_MA; + } + return o; +} + +/* The answer to the 9 V request (OPCODE_READ: the opcode, the result, VBADC): true only for result 0 with VBUS in the + * 7.5-10.5 V bands, read again from USBC_STATUS1 as well (Samsung: AFC_CHARGER_9V for those bands). */ +static inline bool +chg_afc_answer_9v(const uint8_t answer[3], uint8_t vbadc_now) +{ + return answer[0] == MUIC_OP_AFC_RESULT && answer[1] == 0 && answer[2] >= VBADC_9V_MIN && answer[2] <= VBADC_9V_MAX && + vbadc_now >= VBADC_9V_MIN && vbadc_now <= VBADC_9V_MAX; +} + +/* After the answer: the state's type moves to 9 V only with a good one; a bad one is remembered for the cable. */ +static inline void +chg_afc_result(struct chg_state *s, bool good) +{ + if (s->type != CHG_DCP) { + return; + } + if (good) { + s->type = CHG_AFC_9V; + } else { + s->afc_failed = true; + } +} + +/* A written register read back: the bits written must read as written (the guard's watchdog). */ +static inline bool +chg_readback_ok(uint8_t wrote, uint8_t read, uint8_t mask) +{ + return (wrote & mask) == (read & mask); +} + +#endif /* HAKIOS_MAX77705_CHARGE_H */ diff --git a/tools/arch-layers.conf b/tools/arch-layers.conf index e3d2a0ee5..e2a502245 100644 --- a/tools/arch-layers.conf +++ b/tools/arch-layers.conf @@ -9,6 +9,7 @@ codec drivers/bluetooth/bcm4361/hakios.bcm_hci.h codec drivers/bluetooth/bcm4361/hakios.bt_vsc.h codec drivers/soc/exynos/hakios.exynos_tee_der.h codec drivers/power/exynos/hakios.exynos_cpufreq_ceiling.h +codec drivers/power/max77705/hakios.max77705_charge.h codec drivers/input/ssp/hakios.bcm_shub.h codec drivers/display/panel/hakios.s6e3ha8_gamma.h codec drivers/display/panel/hakios.s6e3ha8_star2.h diff --git a/tools/max77705-charge-test.sh b/tools/max77705-charge-test.sh new file mode 100755 index 000000000..4c84ed5f6 --- /dev/null +++ b/tools/max77705-charge-test.sh @@ -0,0 +1,13 @@ +#!/bin/sh +# SPDX-License-Identifier: MIT-0 +# The Galaxy S9's charging rules (drivers/power/max77705/hakios.max77705_charge.h: the iOS 27 charger guard, sweetchg=2, and +# its fast-charge path, sweetafc=1) on this machine: tools/max77705_charge_test.c built against them under the address and +# undefined-behaviour sanitizers, and run. The header is plain C. +# usage: tools/max77705-charge-test.sh +set -e +cd "$(dirname "$0")/.." +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT +cc -O1 -g -Wall -Wextra -Werror -fsanitize=address,undefined -fno-sanitize-recover=all tools/max77705_charge_test.c \ + -o "$tmp/max77705_charge_test" +"$tmp/max77705_charge_test" diff --git a/tools/max77705_charge_test.c b/tools/max77705_charge_test.c new file mode 100644 index 000000000..dc3bc7d4d --- /dev/null +++ b/tools/max77705_charge_test.c @@ -0,0 +1,606 @@ +/* SPDX-License-Identifier: MIT-0 */ +/* + * tools/max77705_charge_test.c: drivers/power/max77705/hakios.max77705_charge.h on a computer, under the address and + * undefined-behaviour sanitizers (tools/max77705-charge-test.sh). The rules the iOS 27 charger guard (sweetchg=2) and its + * fast-charge path (sweetafc=1) follow on the Galaxy S9 (docs/ios27/CHARGING.md). + * + * What is checked: + * - the register encodings: every code read back as Samsung's driver reads it, and a code asked for a current never + * meaning more than that current; + * - every BC_STATUS byte (256): the type, and that only ChgTyp 3 with VBUS is a DCP; + * - the limits: every type's input and charge current at most Samsung's (cable-info), 9 V's the only one past 5 V; + * - each charger type at every battery temperature from -30.0 to 90.0 C (both ways through the hysteresis): charging off + * at 41.0 C and up until 39.0 C, at 5.0 C and down until 10.0 C, 700 mA at 15.0 C and down; 45 C latches off; + * - the faults: a read failure, no temperature, an implausible one, the float above 4350 mV, VBUS over-voltage, no + * battery, the charger's timer fault or thermal shutdown: off, and latched until the cable goes; the charger's + * watchdog expired: re-armed when charging is wanted, never latched; + * - the session's time: off after Samsung's time for the type, and on again only with a new cable; + * - full and the recharge condition; + * - the 9 V path: never asked on a computer port (SDP, CDP), an unknown charger, without sweetafc=1, outside 15-39 C, + * with the charger's thermistor at 44 C, full, or with VBUS already above 5.5 V; asked on a DCP after a second inside + * the band, with the input at 500 mA meanwhile; a good answer only with result 0 and VBADC 5-7 both ways; 9 V dropped + * (5 V asked, 500 mA) when the battery or the charger's thermistor leaves the band, VBUS leaves 7.5-10.5 V, or a fault; + * after a drop or a bad answer not asked again on that cable; asked again after the cable goes and comes; + * - the read-back check; + * - a grid over every input that matters: the input limit never above the type's in force, never above 500 mA with + * VBUS above 5.5 V outside a 9 V session, 9 V never asked outside every one of its conditions. + */ +#include +#include + +#include "../drivers/power/max77705/hakios.max77705_charge.h" + +static unsigned long checks, failures; + +#define CHECK(cond, ...) do { checks++; if (!(cond)) { if (failures++ < 60) { printf("FAIL %s:%d: ", __FILE__, __LINE__); \ + printf(__VA_ARGS__); printf("\n"); } } } while (0) + +/* BC_STATUS for each type: VBUS and ChgTyp */ +#define BC_SDP 0x81 +#define BC_CDP 0x82 +#define BC_DCP 0x83 +#define BC_NONE 0x00 + +/* a healthy reading: VBUS valid (CHGIN_DTLS 3), battery OK (BAT_DTLS 3), fast charge CC (CHG_DTLS 1), charging on, 4350 mV + * float (code 0x13), 25.0 C battery, 30.0 C charger, 50 %, 3800 mV, VBUS 5 V */ +static struct chg_in +healthy(uint8_t bc, uint32_t now) +{ + struct chg_in in = { + .read_ok = true, .bc = bc, .vbadc = 2, .dtls00 = 3u << 5, .dtls01 = (3u << 4) | 1u, + .cnfg00 = CHG_CNFG00_BUCK | CHG_CNFG00_CHG | CHG_CNFG00_WDTEN, .cnfg02 = 0x2b, .cnfg04 = 0x13, .cnfg09 = 0x77, + .temp_ok = true, .temp_dc = 250, .chg_temp_ok = true, .chg_temp_dc = 300, .soc = 50, .vcell_mv = 3800, + .now_s = now, .afc_wanted = false, + }; + return in; +} + +static const uint32_t samsung_input[CHG_TYPES] = { 0, 475, 1500, 1800, 1650, 500 }; +static const uint32_t samsung_charge[CHG_TYPES] = { 0, 550, 1500, 2100, 2150, 500 }; + +static void +encodings(void) +{ + for (unsigned c = 0; c < 256; c++) { + uint32_t ma = chg_input_ma_of((uint8_t)c); + + CHECK(ma >= 100 && ma <= 3200, "input code %#x -> %u mA", c, ma); + ma = chg_charge_ma_of((uint8_t)c); + CHECK(ma >= 100 && ma <= 3150, "charge code %#x -> %u mA", c, ma); + } + CHECK(chg_input_ma_of(0x77) == 3000, "0x77 is 3000 mA (Samsung's log in TWRP)"); + CHECK(chg_charge_ma_of(0x2b) == 2150, "0x2b is 2150 mA (Samsung's log in TWRP)"); + CHECK(chg_float_mv_of(0x13) == 4350, "0x13 is 4350 mV (Samsung's log in TWRP)"); + CHECK(chg_float_mv_of(0x14) == 4360, "0x14 is 4360 mV"); + CHECK(chg_float_mv_of(0x04) == 4200 && chg_float_mv_of(0x00) == 4000, "the float's low codes"); + for (uint32_t ma = 0; ma <= 4000; ma++) { + uint8_t c = chg_input_code(ma); + + CHECK(ma < 100 || chg_input_ma_of(c) <= ma, "input %u mA -> code %#x = %u mA", ma, c, chg_input_ma_of(c)); + CHECK(ma < 125 || ma > 3200 || chg_input_ma_of(c) + 25 > ma, "input %u mA rounded down by less than a step", ma); + c = chg_charge_code(ma); + CHECK(ma < 150 || chg_charge_ma_of(c) <= ma, "charge %u mA -> code %#x = %u mA", ma, c, chg_charge_ma_of(c)); + CHECK(ma < 150 || ma > 3150 || chg_charge_ma_of(c) + 50 > ma, "charge %u mA rounded down by less than a step", ma); + } + for (int t = 0; t < CHG_TYPES; t++) { + CHECK(chg_input_ma_of(chg_input_code(chg_limits[t].input_ma)) == chg_limits[t].input_ma || chg_limits[t].input_ma < 100, + "type %d's input limit is exactly encodable", t); + CHECK(chg_charge_ma_of(chg_charge_code(chg_limits[t].charge_ma)) == chg_limits[t].charge_ma || + chg_limits[t].charge_ma < 150, "type %d's charge current is exactly encodable", t); + } +} + +static void +types(void) +{ + for (unsigned bc = 0; bc < 256; bc++) { + enum chg_type t = chg_type_of_bc((uint8_t)bc); + + CHECK(t < CHG_TYPES && t != CHG_AFC_9V, "BC %#x: never 9 V from the status alone", bc); + CHECK(!(bc & 0x80) == (t == CHG_NONE), "BC %#x: none exactly without VBUS", bc); + CHECK(t != CHG_DCP || ((bc & 0x80) && (bc & 3) == 3), "BC %#x: DCP only for ChgTyp 3", bc); + } + CHECK(chg_type_of_bc(0x82) == CHG_CDP, "phone 2's port on the Mac mini (BC_STATUS 0x82) is a CDP"); + CHECK(chg_type_of_bc(0x81) == CHG_SDP, "0x81 SDP"); + CHECK(chg_type_of_bc(0x85) == CHG_UNKNOWN, "0x85: SDP with DCD time-out"); + CHECK(chg_type_of_bc(0x81 | (2 << 3)) == CHG_UNKNOWN, "Apple 500 mA on an SDP: unknown"); + CHECK(chg_type_of_bc(0x80) == CHG_UNKNOWN, "VBUS without a type"); + for (int t = 0; t < CHG_TYPES; t++) { + CHECK(chg_limits[t].input_ma == samsung_input[t] && chg_limits[t].charge_ma == samsung_charge[t], + "type %d: %u/%u mA, Samsung's %u/%u", t, chg_limits[t].input_ma, chg_limits[t].charge_ma, samsung_input[t], + samsung_charge[t]); + CHECK(t == CHG_NONE || chg_limits[t].session_s > 0, "type %d has a session time", t); + } +} + +/* a cable in for `secs` seconds at a temperature, from a fresh state; the last output */ +static struct chg_out +run(struct chg_state *s, uint8_t bc, int32_t temp_dc, bool afc, unsigned secs) +{ + struct chg_out o = { 0 }; + + for (unsigned t = 0; t <= secs; t++) { + struct chg_in in = healthy(bc, 1000 + t); + + in.temp_dc = temp_dc; + in.afc_wanted = afc; + o = chg_step(s, &in); + } + return o; +} + +static void +bands(void) +{ + static const uint8_t bcs[] = { BC_SDP, BC_CDP, BC_DCP, 0x80 }; + + for (unsigned b = 0; b < sizeof(bcs); b++) { + enum chg_type ty = chg_type_of_bc(bcs[b]); + struct chg_state s; + struct chg_out o; + + /* upward from -30.0 C to 44.9 C, a tenth at a time, one second each */ + chg_state_reset(&s, 0); + bool hot = false, cold = true, red = true; + for (int32_t t = -300; t < 450; t++) { + struct chg_in in = healthy(bcs[b], 2000 + (uint32_t)(t + 300)); + + in.temp_dc = t; + o = chg_step(&s, &in); + if (t >= T_STOP_HIGH_DC) hot = true; + if (t >= T_RESUME_LOW_DC) cold = false; + if (t >= T_UNREDUCE_DC) red = false; + CHECK(o.enable == (!hot && !cold), "type %d up at %d: enable %d", ty, t, o.enable); + CHECK(!o.ask_9v, "type %d: no 9 V without sweetafc", ty); + if (o.enable) { + CHECK(o.input_ma == chg_limits[ty].input_ma, "type %d at %d: input %u", ty, t, o.input_ma); + CHECK(o.charge_ma == (red && chg_limits[ty].charge_ma > 700 ? 700 : chg_limits[ty].charge_ma), + "type %d at %d: charge %u (reduced %d)", ty, t, o.charge_ma, red); + CHECK(t > T_STOP_LOW_DC && t < T_STOP_HIGH_DC, "type %d: charging at %d", ty, t); + } + } + /* and down again from 44.9 C to -30.0 C */ + for (int32_t t = 449; t >= -300; t--) { + struct chg_in in = healthy(bcs[b], 3000 + (uint32_t)(449 - t)); + + in.temp_dc = t; + o = chg_step(&s, &in); + if (t <= T_RESUME_HIGH_DC) hot = false; + if (t <= T_STOP_LOW_DC) cold = true; + if (t <= T_REDUCE_DC) red = true; + CHECK(o.enable == (!hot && !cold), "type %d down at %d: enable %d", ty, t, o.enable); + if (o.enable) { + CHECK(o.charge_ma <= (red ? 700u : chg_limits[ty].charge_ma), "type %d down at %d: charge %u", ty, t, + o.charge_ma); + } + } + /* 45.0 C and above: off, latched while the cable stays, even back at 25 C */ + chg_state_reset(&s, 0); + o = run(&s, bcs[b], 450, false, 0); + CHECK(!o.enable && o.why == WHY_ALARM, "type %d at 45.0 C: off (%s)", ty, chg_why_name(o.why)); + o = run(&s, bcs[b], 250, false, 2); + CHECK(!o.enable && o.why == WHY_FAULT, "type %d: 45 C latched (%s)", ty, chg_why_name(o.why)); + { + struct chg_in gone = healthy(BC_NONE, 5000); + + o = chg_step(&s, &gone); + CHECK(!o.enable && o.why == WHY_NO_VBUS, "cable out"); + } + o = run(&s, bcs[b], 250, false, 2); + CHECK(o.enable, "type %d: a new cable charges again", ty); + } +} + +static void +faults(void) +{ + struct chg_state s; + struct chg_out o; + struct chg_in in; + + chg_state_reset(&s, 0); + in = healthy(BC_CDP, 10); + in.read_ok = false; + o = chg_step(&s, &in); + CHECK(!o.enable && o.why == WHY_READ_FAILED && o.input_ma == 0 && o.charge_ma == 0 && !o.ask_9v, "a read failed"); + + chg_state_reset(&s, 0); + in = healthy(BC_CDP, 10); + in.temp_ok = false; + o = chg_step(&s, &in); + CHECK(!o.enable && o.why == WHY_NO_TEMP, "no temperature: off"); + in.temp_ok = true; + in.now_s++; + o = chg_step(&s, &in); + CHECK(o.enable, "the temperature back: on (not latched)"); + + for (int32_t t = -1000; t <= 2000; t += 50) { + chg_state_reset(&s, 0); + in = healthy(BC_CDP, 10); + in.temp_dc = t; + o = chg_step(&s, &in); + CHECK(chg_temp_plausible(t) || (!o.enable && o.why == WHY_NO_TEMP), "an implausible %d: off", t); + } + + for (unsigned code = 0; code < 64; code++) { + chg_state_reset(&s, 0); + in = healthy(BC_CDP, 10); + in.cnfg04 = (uint8_t)code; + o = chg_step(&s, &in); + CHECK((chg_float_mv_of((uint8_t)code) > 4350) == (o.why == WHY_FLOAT_HIGH), "float code %#x (%u mV): %s", code, + chg_float_mv_of((uint8_t)code), chg_why_name(o.why)); + CHECK(chg_float_mv_of((uint8_t)code) <= 4350 || !o.enable, "float code %#x: off", code); + } + + { + static const struct { uint8_t d00, d01; const char *what; } bad[] = { + { 2u << 5, (3u << 4) | 1, "VBUS over-voltage" }, + { 3u << 5, (0u << 4) | 1, "no battery" }, + { 3u << 5, (2u << 4) | 1, "battery dead" }, + { 3u << 5, (5u << 4) | 1, "battery over-voltage" }, + { 3u << 5, (3u << 4) | 6, "timer fault" }, + { 3u << 5, (3u << 4) | 0xA, "thermal shutdown" }, + }; + + for (unsigned i = 0; i < sizeof(bad) / sizeof(bad[0]); i++) { + chg_state_reset(&s, 0); + in = healthy(BC_DCP, 10); + in.dtls00 = bad[i].d00; + in.dtls01 = bad[i].d01; + o = chg_step(&s, &in); + CHECK(!o.enable && o.why == WHY_FAULT, "%s: off", bad[i].what); + in = healthy(BC_DCP, 11); + o = chg_step(&s, &in); + CHECK(!o.enable && o.why == WHY_FAULT, "%s: latched", bad[i].what); + } + } + /* the watchdog expired (seen on phone 2 in iOS 70 s after S-Boot: DETAILS_01 0x3b): not a latched fault; re-armed + * when charging is wanted, not when it is not (hot, full) */ + chg_state_reset(&s, 0); + in = healthy(BC_CDP, 10); + in.dtls01 = (3u << 4) | 0xB; + o = chg_step(&s, &in); + CHECK(o.enable && o.rearm && o.why == WHY_OK, "watchdog expired: re-armed (%s)", chg_why_name(o.why)); + in.dtls01 = (3u << 4) | 1; + in.now_s++; + o = chg_step(&s, &in); + CHECK(o.enable && !o.rearm, "charging again: no re-arm"); + chg_state_reset(&s, 0); + in = healthy(BC_CDP, 10); + in.dtls01 = (3u << 4) | 0xB; + in.temp_dc = 420; + o = chg_step(&s, &in); + CHECK(!o.enable && !o.rearm && o.why == WHY_HOT, "watchdog expired at 42 C: left off"); + CHECK(chg_readback_ok(0x2b, 0xeb, CHG_CNFG02_CC_MASK), "read-back: only the bits written"); + CHECK(!chg_readback_ok(0x2b, 0x2a, CHG_CNFG02_CC_MASK), "read-back: a differing bit"); +} + +static void +session(void) +{ + static const uint8_t bcs[] = { BC_SDP, BC_CDP, BC_DCP }; + + for (unsigned b = 0; b < sizeof(bcs); b++) { + enum chg_type ty = chg_type_of_bc(bcs[b]); + struct chg_state s; + struct chg_out o = { 0 }; + uint32_t limit = chg_limits[ty].session_s; + + chg_state_reset(&s, 0); + for (uint32_t t = 0; t <= limit + 20; t += 5) { + struct chg_in in = healthy(bcs[b], t); + + o = chg_step(&s, &in); + if (t + 5 < limit) { + CHECK(o.enable, "type %d at %u s: on", ty, t); + } + } + CHECK(!o.enable && o.why == WHY_EXPIRED, "type %d after %u s: off (%s)", ty, limit, chg_why_name(o.why)); + { + struct chg_in gone = healthy(BC_NONE, limit + 30), back = healthy(bcs[b], limit + 35); + + chg_step(&s, &gone); + o = chg_step(&s, &back); + CHECK(o.enable, "type %d: a new cable, a new session", ty); + } + } + /* not counted while the chip does not charge (full, or charging off) */ + { + struct chg_state s; + struct chg_out o = { 0 }; + + chg_state_reset(&s, 0); + for (uint32_t t = 0; t <= 40000; t += 10) { + struct chg_in in = healthy(BC_CDP, t); + + in.dtls01 = (3u << 4) | 0x8; /* off */ + o = chg_step(&s, &in); + } + CHECK(o.why != WHY_EXPIRED, "time not counted while the chip is off"); + } +} + +static void +full(void) +{ + struct chg_state s; + struct chg_out o; + struct chg_in in; + + chg_state_reset(&s, 0); + in = healthy(BC_CDP, 1); + in.dtls01 = (3u << 4) | 0x4; /* done */ + in.soc = 100; + in.vcell_mv = 4340; + o = chg_step(&s, &in); + CHECK(!o.enable && o.why == WHY_FULL, "done: off (%s)", chg_why_name(o.why)); + in.dtls01 = (3u << 4) | 0x8; /* off, as asked */ + in.now_s = 2; + o = chg_step(&s, &in); + CHECK(!o.enable && o.why == WHY_FULL, "stays off above the recharge condition"); + in.vcell_mv = 4279; + in.now_s = 3; + o = chg_step(&s, &in); + CHECK(o.enable && o.why == WHY_OK, "on again under 4280 mV"); + chg_state_reset(&s, 0); + in = healthy(BC_CDP, 1); + in.dtls01 = (3u << 4) | 0x4; + in.soc = 100; + in.vcell_mv = 4340; + chg_step(&s, &in); + in.dtls01 = (3u << 4) | 0x8; + in.soc = 97; + in.now_s = 2; + o = chg_step(&s, &in); + CHECK(o.enable, "on again under 98 %%"); +} + +/* a DCP with sweetafc=1 until 9 V is asked; returns the step that asked (or the last) */ +static struct chg_out +to_ask(struct chg_state *s, struct chg_in *in, unsigned max_steps) +{ + struct chg_out o = { 0 }; + + for (unsigned i = 0; i < max_steps; i++) { + o = chg_step(s, in); + if (o.ask_9v) { + break; + } + in->now_s++; + } + return o; +} + +static void +afc(void) +{ + struct chg_state s; + struct chg_out o; + struct chg_in in; + + /* refused: computer ports, unknown, no sweetafc, the bands, full, VBUS already high */ + { + static const uint8_t bcs[] = { BC_SDP, BC_CDP, 0x80, 0x85, 0x81 | (4 << 3) }; + + for (unsigned b = 0; b < sizeof(bcs); b++) { + chg_state_reset(&s, 0); + in = healthy(bcs[b], 1); + in.afc_wanted = true; + o = to_ask(&s, &in, 30); + CHECK(!o.ask_9v, "BC %#x: never 9 V", bcs[b]); + chg_afc_result(&s, true); /* even a "good" answer out of the blue */ + CHECK(s.type != CHG_AFC_9V, "BC %#x: a stray answer makes no 9 V session", bcs[b]); + } + } + chg_state_reset(&s, 0); + in = healthy(BC_DCP, 1); + o = to_ask(&s, &in, 30); + CHECK(!o.ask_9v, "no 9 V without sweetafc=1"); + for (int32_t t = -100; t <= 440; t += 5) { + chg_state_reset(&s, 0); + in = healthy(BC_DCP, 1); + in.afc_wanted = true; + in.temp_dc = t; + o = to_ask(&s, &in, 5); + CHECK(o.ask_9v == (t > T_FAST_MIN_DC && t <= T_FAST_MAX_DC), "DCP at %d: ask %d", t, o.ask_9v); + } + for (int32_t t = 300; t <= 500; t += 5) { + chg_state_reset(&s, 0); + in = healthy(BC_DCP, 1); + in.afc_wanted = true; + in.chg_temp_dc = t; + o = to_ask(&s, &in, 5); + CHECK(o.ask_9v == (t < T_CHG_FAST_MAX_DC), "charger thermistor %d: ask %d", t, o.ask_9v); + } + chg_state_reset(&s, 0); + in = healthy(BC_DCP, 1); + in.afc_wanted = true; + in.chg_temp_ok = false; + CHECK(!to_ask(&s, &in, 5).ask_9v, "no charger thermistor: no 9 V"); + chg_state_reset(&s, 0); + in = healthy(BC_DCP, 1); + in.afc_wanted = true; + in.soc = 98; + CHECK(!to_ask(&s, &in, 5).ask_9v, "98 %%: no 9 V"); + chg_state_reset(&s, 0); + in = healthy(BC_DCP, 1); + in.afc_wanted = true; + in.vbadc = 3; + o = to_ask(&s, &in, 5); + CHECK(!o.ask_9v && o.input_ma == CHG_PRE_AFC_INPUT_MA, "VBUS above 5.5 V on a plain DCP: no request, 500 mA"); + + /* the good path */ + chg_state_reset(&s, 0); + in = healthy(BC_DCP, 1); + in.afc_wanted = true; + o = chg_step(&s, &in); + CHECK(!o.ask_9v && o.enable && o.input_ma == 1800 && o.charge_ma == 2100, "the first second of a DCP: 5 V limits"); + in.now_s++; + o = chg_step(&s, &in); + CHECK(o.ask_9v && o.input_ma == CHG_PRE_AFC_INPUT_MA, "after a second: 9 V asked, the input at 500 mA"); + { + uint8_t good[3] = { MUIC_OP_AFC_RESULT, 0, 6 }, wrong_op[3] = { 0x05, 0, 6 }, res1[3] = { MUIC_OP_AFC_RESULT, 2, 6 }, + low[3] = { MUIC_OP_AFC_RESULT, 0, 2 }, high[3] = { MUIC_OP_AFC_RESULT, 0, 8 }; + + CHECK(chg_afc_answer_9v(good, 6), "the good answer"); + CHECK(!chg_afc_answer_9v(good, 2), "the good answer but VBUS read at 5 V"); + CHECK(!chg_afc_answer_9v(wrong_op, 6), "another opcode's answer"); + CHECK(!chg_afc_answer_9v(res1, 6), "result 2 (not a high-voltage DCP)"); + CHECK(!chg_afc_answer_9v(low, 6), "result 0 at 5 V"); + CHECK(!chg_afc_answer_9v(high, 8), "result 0 at 10.5-11.5 V"); + for (unsigned r = 0; r < 256; r++) { + for (unsigned v = 0; v < 16; v++) { + uint8_t a[3] = { MUIC_OP_AFC_RESULT, (uint8_t)r, (uint8_t)v }; + + CHECK(chg_afc_answer_9v(a, (uint8_t)v) == (r == 0 && v >= 5 && v <= 7), "answer r %u v %u", r, v); + } + } + } + chg_afc_result(&s, true); + CHECK(s.type == CHG_AFC_9V, "a good answer: 9 V"); + in.vbadc = 6; + in.now_s++; + o = chg_step(&s, &in); + CHECK(o.enable && o.input_ma == 1650 && o.charge_ma == 2150 && !o.ask_9v && !o.ask_5v, "9 V: 1650/2150"); + + /* the watchdog's reasons to drop it */ + { + struct { const char *what; int32_t temp, chg_temp; uint8_t vbadc, d00; uint32_t soc; } drop[] = { + { "battery 39.1 C", 391, 300, 6, 3u << 5, 50 }, + { "battery 15.0 C", 150, 300, 6, 3u << 5, 50 }, + { "charger 44.0 C", 250, 440, 6, 3u << 5, 50 }, + { "VBUS 5 V", 250, 300, 2, 3u << 5, 50 }, + { "VBUS 10.5-11.5 V", 250, 300, 8, 3u << 5, 50 }, + { "98 %", 250, 300, 6, 3u << 5, 98 }, + { "over-voltage", 250, 300, 6, 2u << 5, 50 }, + { "45 C", 450, 300, 6, 3u << 5, 50 }, + }; + + for (unsigned i = 0; i < sizeof(drop) / sizeof(drop[0]); i++) { + struct chg_state s2 = s; + struct chg_in in2 = in; + + in2.temp_dc = drop[i].temp; + in2.chg_temp_dc = drop[i].chg_temp; + in2.vbadc = drop[i].vbadc; + in2.dtls00 = drop[i].d00; + in2.soc = drop[i].soc; + in2.now_s++; + o = chg_step(&s2, &in2); + CHECK(o.ask_5v && s2.type == CHG_DCP && s2.afc_failed, "%s: back to 5 V", drop[i].what); + CHECK(o.input_ma <= CHG_PRE_AFC_INPUT_MA || (in2.vbadc <= VBADC_5V_MAX && o.input_ma <= 1800), + "%s: input %u", drop[i].what, o.input_ma); + in2.temp_dc = 250; + in2.chg_temp_dc = 300; + in2.vbadc = 2; + in2.dtls00 = 3u << 5; + in2.soc = 50; + for (int k = 0; k < 10; k++) { + in2.now_s++; + o = chg_step(&s2, &in2); + CHECK(!o.ask_9v, "%s: not asked again on this cable", drop[i].what); + } + } + } + /* a bad answer: not again on this cable; again after a new cable */ + chg_state_reset(&s, 0); + in = healthy(BC_DCP, 1); + in.afc_wanted = true; + o = to_ask(&s, &in, 5); + CHECK(o.ask_9v, "asked"); + chg_afc_result(&s, false); + for (int k = 0; k < 10; k++) { + in.now_s++; + o = chg_step(&s, &in); + CHECK(!o.ask_9v && o.input_ma == 1800, "after a bad answer: 5 V limits, no new request"); + } + in.bc = BC_NONE; + in.now_s++; + chg_step(&s, &in); + in.bc = BC_DCP; + o = to_ask(&s, &in, 5); + CHECK(o.ask_9v, "a new cable: asked again"); + /* the MUIC changing its mind under 9 V */ + chg_afc_result(&s, true); + in.vbadc = 6; + in.bc = BC_CDP; + in.now_s++; + o = chg_step(&s, &in); + CHECK(s.type == CHG_CDP && o.input_ma <= 1500, "9 V, then the MUIC says CDP: CDP limits (%u)", o.input_ma); +} + +/* every input that matters, for every state reached from them: the invariants */ +static void +grid(void) +{ + static const uint8_t bcs[] = { BC_NONE, BC_SDP, BC_CDP, BC_DCP, 0x80, 0x85 }; + static const int32_t temps[] = { -300, 0, 50, 51, 100, 150, 151, 200, 250, 390, 391, 409, 410, 449, 450, 600 }; + static const int32_t ctemps[] = { 200, 439, 440, 500 }; + static const uint8_t vbadcs[] = { 0, 2, 3, 5, 6, 7, 8, 10 }; + static const uint32_t socs[] = { 5, 50, 97, 98, 100 }; + unsigned long combos = 0; + + for (unsigned b = 0; b < sizeof(bcs); b++) + for (unsigned t = 0; t < sizeof(temps) / sizeof(temps[0]); t++) + for (unsigned c = 0; c < sizeof(ctemps) / sizeof(ctemps[0]); c++) + for (unsigned v = 0; v < sizeof(vbadcs); v++) + for (unsigned q = 0; q < sizeof(socs) / sizeof(socs[0]); q++) + for (unsigned afc_on = 0; afc_on < 2; afc_on++) + for (unsigned start9 = 0; start9 < 2; start9++) { + struct chg_state s; + struct chg_in in = healthy(bcs[b], 100); + struct chg_out o; + enum chg_type before; + + chg_state_reset(&s, 0); + if (start9) { + /* a 9 V session already in force, reached the proper way */ + struct chg_in d = healthy(BC_DCP, 90); + + d.afc_wanted = true; + to_ask(&s, &d, 5); + chg_afc_result(&s, true); + } + in.temp_dc = temps[t]; + in.chg_temp_dc = ctemps[c]; + in.vbadc = vbadcs[v]; + in.soc = socs[q]; + in.afc_wanted = afc_on; + before = s.type; + o = chg_step(&s, &in); + combos++; + (void)before; + CHECK(s.type < CHG_TYPES, "state type"); + CHECK(o.input_ma <= chg_limits[s.type].input_ma || o.input_ma <= CHG_PRE_AFC_INPUT_MA, + "input %u above type %d's", o.input_ma, s.type); + CHECK(o.charge_ma <= chg_limits[s.type].charge_ma, "charge %u above type %d's", o.charge_ma, s.type); + CHECK(o.input_ma <= 1800 && o.charge_ma <= 2150, "never above Samsung's highest"); + CHECK(s.type == CHG_AFC_9V || in.vbadc <= VBADC_5V_MAX || o.input_ma <= CHG_PRE_AFC_INPUT_MA, + "VBUS %u outside a 9 V session: input %u", in.vbadc, o.input_ma); + CHECK(!o.ask_9v || (afc_on && chg_type_of_bc(in.bc) == CHG_DCP && in.temp_dc > T_FAST_MIN_DC && + in.temp_dc <= T_FAST_MAX_DC && in.chg_temp_dc < T_CHG_FAST_MAX_DC && in.soc < RECHARGE_SOC && + in.vbadc <= VBADC_5V_MAX), "9 V asked outside its conditions"); + CHECK(!o.enable || (in.temp_dc > T_STOP_LOW_DC && in.temp_dc < T_STOP_HIGH_DC), "charging at %d", in.temp_dc); + CHECK(s.type != CHG_AFC_9V || (in.temp_dc <= T_FAST_MAX_DC && in.temp_dc > T_FAST_MIN_DC && + in.chg_temp_dc < T_CHG_FAST_MAX_DC && in.vbadc >= VBADC_9V_MIN && in.vbadc <= VBADC_9V_MAX), + "a 9 V session kept outside its band (t %d, c %d, v %u)", in.temp_dc, in.chg_temp_dc, in.vbadc); + CHECK(!(o.ask_9v && o.ask_5v), "both requests at once"); + CHECK(!o.rearm || o.enable, "a re-arm only with charging wanted"); + } + printf("max77705_charge_test: grid of %lu states\n", combos); +} + +int +main(void) +{ + encodings(); + types(); + bands(); + faults(); + session(); + full(); + afc(); + grid(); + printf("max77705_charge_test: %lu checks, %lu failures\n", checks, failures); + return failures != 0; +}