Re: [PATCH 0/3] power: supply: smb347: Set the USB input current limit from APSD

From: David Heidelberg

Date: Sat Nov 21 2026 - 15:59:37 EST


On 21/11/2026 10:30, Patrick Fischer via B4 Relay wrote:
The SMB347 driver programs the USB input current limit once at probe
from "summit,usb-current-limit-microamp". Boards either leave it at the
chip's NVM default, which is 900 mA on the Nexus 7 (2012), or set it to
1800 mA (exynos4412-p4note, omap4-samsung-espresso10). The first makes
the original 2 A charger no faster than a PC port, the second loads PC
ports with 1.8 A.

The charger detects the port type itself (APSD). This series reads the
result after the input became valid, sets the limit for the port type
(SDP 500 mA, CDP 1.5 A, DCP the DT value or 1.5 A) and reports it as
POWER_SUPPLY_PROP_USB_TYPE. Patch 1 updates the binding text, patch 2
makes the registers readable, patch 3 does the detection.

Measured on a Nexus 7 (2012, E1565), median current into the battery
(bq27541) over 5-30 minutes, battery charge in brackets:

before (NVM 900 mA) this series
laptop port (CDP) 505 mA (84-89 %) 640 mA (85-90 %) 1.5 A
2 A charger (DCP) 561 mA (84-86 %) 968 mA (74-78 %) 1.5 A
972 mA (79-82 %) 1.8 A *
same, display off 596 mA (31-33 %) 1161 mA (37-39 %) 1.5 A
1147 mA (45-47 %) 1.8 A *

* with summit,usb-current-limit-microamp = <1800000> added to the
grouper DT for the test; it gives no gain over 1.5 A, so this
series does not touch the grouper DT.

The display was on unless noted.

The laptop port turned out to be a CDP. A PC port detected as SDP was
not available for testing. With 1800 mA in the device tree and without
this series, AICL ended once at the lowest step directly after boot at
the laptop port (the battery got 120 mA); with the series it did not
happen in seven boots and several reconnections. Directly after probe the
charger NAKs the write to CFG_VARIOUS_FUNCTION; patch 3 therefore does
the first detection 500 ms after probe, as after a new connection.
A quick move from the laptop port to the charger once left the driver
with the CDP limit (two interrupts, input state unchanged in the
driver), so patch 3 detects the port type after every USB input
interrupt; quick moves in both directions work with that.

There is no public datasheet with the register map that I could find.
The STAT_D, AICL and APSD bits are taken from the Android 3.1 kernel for
the Nexus 7 [1], which uses them the same way (900 mA for SDP, 1800 mA
for the other types). That is why only the SMB347 uses APSD, and
SMB345/SMB358 stay as they are.

Questions:

- Is 500 mA for an SDP acceptable? It is the USB 2.0 value, but less
than the NVM default of 900 mA on the Nexus 7 and than the 1800 mA
p4note and espresso10 use today at a PC port.
- APSD result "other" (non-BC 1.2 chargers) gets 500 mA here; the
Android driver uses the charger limit. Is someone able to check the
meaning of the remaining STAT_D values on SMB345/SMB358?

Bruce Robertson posted an RFC for a Rust SMB347 driver [2], which does
not handle interrupts or USB OTG yet. This series is for the existing C
driver; I am happy to test the Rust driver on the Nexus 7 as well.

Tested with v7.4-rc3 on a Nexus 7 (2012, E1565) at a laptop port (CDP)
and the original 2 A charger (DCP), including unplugging, replugging and
quick moves between them.
An SDP, chargers reported as "other" and USB host mode (OTG) were not
tested. The series is build tested on top of power-supply for-next.

Tool use (see Documentation/process/generated-content.rst): the patches,
their changelogs and this cover letter were prepared with an LLM coding
assistant (Claude Code). It compared the Android and mainline drivers,
wrote the code and drafted the texts from my measurements and
instructions. I reviewed the changes and ran all tests on the device.

[1] drivers/power/smb347-charger.c, branch
android-tegra3-grouper-3.1-lollipop-release of
https://android.googlesource.com/kernel/tegra
[2] https://lore.kernel.org/all/20260708214738.25008-1-brucer42@xxxxxxxxx/

---
https://sashiko.dev/#/patchset/20261121-upstream-smb347-apsd-v1-0-ca46b92bfd44%40tsv-stahl-riesa.de

It's partly matching with Sashiko, but here is second LLM review:

Commits Reviewed (909ef7a9a30fa..HEAD on branch test-smb347)
1. b5983679ca5a1 dt-bindings: power: supply: summit,smb347: Clarify the USB current limit
2. 94061e72ab31f power: supply: smb347: Make the APSD and AICL registers readable
3. * 375bda07de8ee power: supply: smb347: Set the USB input current limit from APSD
Build & Validation Checks

• dt_binding_check on Documentation/devicetree/bindings/power/supply/summit,smb347-charger.yaml: passed
• make ARCH=arm64 LLVM=1 W=1 drivers/power/supply/smb347-charger.o (in temporary worktree): passed (0 warnings)
• scripts/checkpatch.pl --strict --git 909ef7a9a30fa..HEAD: passed (0 errors, 0 warnings, 0 checks)
• Reachability / Kconfig / Fixes tag: REACHABILITY: confirmed; no Kconfig changes; not a bug-fix series (no Fixes: tag required).
──────
Per-Commit Summary
• Patch 1 (b5983679ca5a1) — Documentation/devicetree/bindings/power/supply/summit,smb347-charger.yaml:56-60: No issues found.
• Patch 2 (94061e72ab31f) — drivers/power/supply/smb347-charger.c:1425-1469: No issues found.
• Patch 3 (375bda07de8ee) — drivers/power/supply/smb347-charger.c: 5 issues found, written in mailing-list format to review-inline.txt (and metadata in
review-metadata.json):
1. Unsynchronized CMD_A_ALLOW_WRITE and shared state (smb->usb_vbus_enabled, smb->usb_online) (drivers/power/supply/smb347-charger.c:918-963,
drivers/power/supply/smb347-charger.c:545-589):
• smb347_set_writable() only toggles disable_irq()/enable_irq(), which blocks smb347_interrupt() but does not exclude other process-context threads.
Previously, runtime callers were all VBUS regulator ops serialized by the regulator core mutex.
• smb347_apsd_work() now holds CMD_A_ALLOW_WRITE across msleep(20) in smb347_set_usb_current_limit(), allowing a concurrent VBUS regulator callback (or
smb347_remove()/smb347_shutdown()) to clear CMD_A_ALLOW_WRITE mid-sequence, causing the subsequent CFG_VARIOUS_FUNCTION write to re-enable AICL (or the
regulator's CFG_SYSOK/CFG_OTG writes in the reverse interleaving) to be ignored by hardware.
• smb->usb_vbus_enabled is checked before the up-to-2 s regmap_read_poll_timeout() loop without locking (TOCTOU if host VBUS is enabled during polling),
and smb->usb_online is read without synchronization while smb347_interrupt() updates it.
2. Missing 500 ms post-probe delay when booting unplugged (+ regmap cache poisoning on NAK + use_apsd probe race)
(drivers/power/supply/smb347-charger.c:965-1005):
• smb347_apsd_init() calls smb347_apsd_schedule(smb) expecting the 500 ms delay to avoid the post-probe CFG_VARIOUS_FUNCTION write NAK, but
smb347_apsd_schedule() sets delay = 0 whenever !smb->usb_online. When booting on battery, smb347_apsd_work() runs immediately and still writes
CFG_VARIOUS_FUNCTION (to drop from 900 mA / 1800 mA to 500 mA).
• If that write NAKs, _regmap_write() in drivers/base/regmap/regmap.c:1988-1998 updates the maple cache before map->reg_write(), leaving
CFG_VARIOUS_FUNCTION_AICL cleared in cache while still set in hardware, so future smb347_set_usb_current_limit() calls read aicl == false from cache and
stop toggling AICL.
• smb->use_apsd is also assigned after smb347_irq_init() has enabled the IRQ and before CFG_CHARGE_CONTROL_APSD is written, allowing an early USBIN_UV_IRQ
to schedule apsd_work prematurely and race with smb347_apsd_init() on CMD_A_ALLOW_WRITE.
3. Teardown / shutdown bugs with apsd_work and free_irq() (drivers/power/supply/smb347-charger.c:1821-1863):
• smb347_shutdown() calls smb347_remove() without devres release (and i2c_device_shutdown() skips disable_irq() when .shutdown is defined), so a pending or
polling apsd_work is never cancelled on shutdown and can keep issuing I2C transfers after smb347_shutdown() and parent I2C adapter shutdown.
• On detach, devm_delayed_work_autocancel() is registered before devm_request_threaded_irq(), so free_irq() runs before cancel_delayed_work_sync(). An
already-running smb347_apsd_work() finishing its STAT_D poll then calls smb347_set_writable() -> disable_irq()/enable_irq() on a freed and deactivated IRQ.
4. POWER_SUPPLY_PROP_USB_TYPE exposed when APSD is disabled (drivers/power/supply/smb347-charger.c:1747-1757):
• smb347_usb_desc unconditionally exposes usb_type advertising SDP/CDP/DCP on SMB345, SMB358, and SMB347 boards without an interrupt (such as exynos4412-
p4note), where smb->use_apsd is false and the property stays Unknown permanently.
5. Commit message inaccuracy on exynos4412-p4note (arch/arm/boot/dts/samsung/exynos4412-p4note.dtsi:219-229):
• The commit message states that exynos4412-p4note changes behaviour to draw 500 mA from a PC port, but exynos4412-p4note.dtsi (charger@6) has no
interrupts property, so smb->irq_unsupported is true and smb->use_apsd remains false; only omap4-samsung-espresso10 is affected.


Ruled-Out Potential Regressions

• Ruled out regression 1: smb347_apsd_work() calling power_supply_changed(smb->usb) cannot dereference NULL because smb->use_apsd requires smb->use_usb, which
registers smb->usb unconditionally in smb347_probe().
• Ruled out regression 2: smb347_usb_type_current_limit() with low DT values (e.g. 300000 uA) or unset (0) always maps to a valid icl_tbl[SMB347] entry, and
invalid < 300000 uA DT values are already rejected in smb347_hw_init().
──────
FINAL REGRESSIONS FOUND: 5
FINAL TOKENS USED: not exposed by runtime
Assisted-by: Antigravity:Gemini 4 Neon