mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/3] power: supply: smb347: Set the USB input current limit from APSD
@ 2026-11-21  9:30 Patrick Fischer via B4 Relay
  2026-11-21  9:30 ` [PATCH 1/3] dt-bindings: power: supply: summit,smb347: Clarify the USB current limit Patrick Fischer via B4 Relay
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Patrick Fischer via B4 Relay @ 2026-11-21  9:30 UTC (permalink / raw)
  To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, David Heidelberg, Dmitry Osipenko
  Cc: Svyatoslav Ryhel, Bruce Robertson, Martin Jücker,
	Mithil Bavishi, linux-pm, devicetree, linux-kernel,
	Patrick Fischer

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@gmail.com/

---
Patrick Fischer (3):
      dt-bindings: power: supply: summit,smb347: Clarify the USB current limit
      power: supply: smb347: Make the APSD and AICL registers readable
      power: supply: smb347: Set the USB input current limit from APSD

 .../power/supply/summit,smb347-charger.yaml        |   5 +-
 drivers/power/supply/smb347-charger.c              | 258 ++++++++++++++++++++-
 2 files changed, 260 insertions(+), 3 deletions(-)
---
base-commit: abbe0a96bdbadae9e29489a1528fa436c55aca25
change-id: 20261120-upstream-smb347-apsd-98b4edefb06f

Best regards,
--  
Patrick Fischer <admin@tsv-stahl-riesa.de>



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/3] dt-bindings: power: supply: summit,smb347: Clarify the USB current limit
  2026-11-21  9:30 [PATCH 0/3] power: supply: smb347: Set the USB input current limit from APSD Patrick Fischer via B4 Relay
@ 2026-11-21  9:30 ` Patrick Fischer via B4 Relay
  2026-11-21  9:30 ` [PATCH 2/3] power: supply: smb347: Make the APSD and AICL registers readable Patrick Fischer via B4 Relay
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Patrick Fischer via B4 Relay @ 2026-11-21  9:30 UTC (permalink / raw)
  To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, David Heidelberg, Dmitry Osipenko
  Cc: Svyatoslav Ryhel, Bruce Robertson, Martin Jücker,
	Mithil Bavishi, linux-pm, devicetree, linux-kernel,
	Patrick Fischer

From: Patrick Fischer <admin@tsv-stahl-riesa.de>

The SMB347 detects whether its USB input is connected to a standard
downstream port, a charging downstream port or a dedicated charger.
The input current limit then depends on the port type: a PC port must
not be loaded with the current a dedicated charger can deliver.

Describe "summit,usb-current-limit-microamp" as the limit with a
dedicated charger, which is what boards set it to today (1.8 A on
exynos4412-p4note and omap4-samsung-espresso10).

Signed-off-by: Patrick Fischer <admin@tsv-stahl-riesa.de>
---
 .../devicetree/bindings/power/supply/summit,smb347-charger.yaml      | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/Documentation/devicetree/bindings/power/supply/summit,smb347-charger.yaml b/Documentation/devicetree/bindings/power/supply/summit,smb347-charger.yaml
index 65ed92bb05f3..67e6d4055af0 100644
--- a/Documentation/devicetree/bindings/power/supply/summit,smb347-charger.yaml
+++ b/Documentation/devicetree/bindings/power/supply/summit,smb347-charger.yaml
@@ -54,7 +54,10 @@ properties:
     description: Maximum input current from AC/DC input (in uA)
 
   summit,usb-current-limit-microamp:
-    description: Maximum input current from USB input (in uA)
+    description:
+      Maximum input current from USB input (in uA). This is the limit with a
+      dedicated charger. A port detected as standard or charging downstream
+      port may be limited further.
 
   summit,charge-current-compensation-microamp:
     description: Charge current compensation (in uA)

-- 
2.56.1



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/3] power: supply: smb347: Make the APSD and AICL registers readable
  2026-11-21  9:30 [PATCH 0/3] power: supply: smb347: Set the USB input current limit from APSD Patrick Fischer via B4 Relay
  2026-11-21  9:30 ` [PATCH 1/3] dt-bindings: power: supply: summit,smb347: Clarify the USB current limit Patrick Fischer via B4 Relay
@ 2026-11-21  9:30 ` Patrick Fischer via B4 Relay
  2026-11-21  9:30 ` [PATCH 3/3] power: supply: smb347: Set the USB input current limit from APSD Patrick Fischer via B4 Relay
  2026-11-21 20:59 ` [PATCH 0/3] " David Heidelberg
  3 siblings, 0 replies; 5+ messages in thread
From: Patrick Fischer via B4 Relay @ 2026-11-21  9:30 UTC (permalink / raw)
  To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, David Heidelberg, Dmitry Osipenko
  Cc: Svyatoslav Ryhel, Bruce Robertson, Martin Jücker,
	Mithil Bavishi, linux-pm, devicetree, linux-kernel,
	Patrick Fischer

From: Patrick Fischer <admin@tsv-stahl-riesa.de>

The charger has a few registers that the driver does not list as
readable yet:

  0x02  CFG_VARIOUS_FUNCTION  includes the AICL enable bit
  0x04  CFG_CHARGE_CONTROL    includes the APSD enable bit
  0x3e  STAT_D                automatic power source detection result

The bit assignments are taken from the Android 3.1 kernel for the
Nexus 7 (2012), drivers/power/smb347-charger.c in the
android-tegra3-grouper-3.1-lollipop-release branch of
https://android.googlesource.com/kernel/tegra.

Add them to the readable registers. STAT_D reflects the state of the
input and is volatile. The other two are configuration registers like
the ones the driver already caches.

This makes them visible in the regmap debugfs and prepares for using
the detected charger type to set the USB input current limit.

Signed-off-by: Patrick Fischer <admin@tsv-stahl-riesa.de>
---
 drivers/power/supply/smb347-charger.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/power/supply/smb347-charger.c b/drivers/power/supply/smb347-charger.c
index 72dfa15b3d80..03d58b378a25 100644
--- a/drivers/power/supply/smb347-charger.c
+++ b/drivers/power/supply/smb347-charger.c
@@ -43,10 +43,12 @@
 #define CFG_CURRENT_LIMIT_DC_MASK		0xf0
 #define CFG_CURRENT_LIMIT_DC_SHIFT		4
 #define CFG_CURRENT_LIMIT_USB_MASK		0x0f
+#define CFG_VARIOUS_FUNCTION			0x02
 #define CFG_FLOAT_VOLTAGE			0x03
 #define CFG_FLOAT_VOLTAGE_FLOAT_MASK		0x3f
 #define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK	0xc0
 #define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT	6
+#define CFG_CHARGE_CONTROL			0x04
 #define CFG_STAT				0x05
 #define CFG_STAT_DISABLED			BIT(5)
 #define CFG_STAT_ACTIVE_HIGH			BIT(7)
@@ -128,6 +130,7 @@
 #define STAT_C_CHG_SHIFT			1
 #define STAT_C_CHG_TERM				BIT(5)
 #define STAT_C_CHARGER_ERROR			BIT(6)
+#define STAT_D					0x3e
 #define STAT_E					0x3f
 
 #define SMB347_MAX_REGISTER			0x3f
@@ -1199,6 +1202,7 @@ static bool smb347_volatile_reg(struct device *dev, unsigned int reg)
 	case STAT_A:
 	case STAT_B:
 	case STAT_C:
+	case STAT_D:
 	case STAT_E:
 		return true;
 	}
@@ -1211,7 +1215,9 @@ static bool smb347_readable_reg(struct device *dev, unsigned int reg)
 	switch (reg) {
 	case CFG_CHARGE_CURRENT:
 	case CFG_CURRENT_LIMIT:
+	case CFG_VARIOUS_FUNCTION:
 	case CFG_FLOAT_VOLTAGE:
+	case CFG_CHARGE_CONTROL:
 	case CFG_STAT:
 	case CFG_PIN:
 	case CFG_THERM:

-- 
2.56.1



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/3] power: supply: smb347: Set the USB input current limit from APSD
  2026-11-21  9:30 [PATCH 0/3] power: supply: smb347: Set the USB input current limit from APSD Patrick Fischer via B4 Relay
  2026-11-21  9:30 ` [PATCH 1/3] dt-bindings: power: supply: summit,smb347: Clarify the USB current limit Patrick Fischer via B4 Relay
  2026-11-21  9:30 ` [PATCH 2/3] power: supply: smb347: Make the APSD and AICL registers readable Patrick Fischer via B4 Relay
@ 2026-11-21  9:30 ` Patrick Fischer via B4 Relay
  2026-11-21 20:59 ` [PATCH 0/3] " David Heidelberg
  3 siblings, 0 replies; 5+ messages in thread
From: Patrick Fischer via B4 Relay @ 2026-11-21  9:30 UTC (permalink / raw)
  To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, David Heidelberg, Dmitry Osipenko
  Cc: Svyatoslav Ryhel, Bruce Robertson, Martin Jücker,
	Mithil Bavishi, linux-pm, devicetree, linux-kernel,
	Patrick Fischer

From: Patrick Fischer <admin@tsv-stahl-riesa.de>

The driver programs the USB input current limit only once at probe,
from "summit,usb-current-limit-microamp". Without that property the
limit stays at the value from the chip's non-volatile memory. On the
Nexus 7 (2012) this is 900 mA, so the original 2 A charger charges the
battery no faster than a PC port: about 540 mA into the battery with
the display on, against about 505 mA at a laptop port. With the limit
set to 1800 mA in the device tree, the charger gives about 790 mA, but
then the same 1800 mA also applies to PC ports. Once, directly after
boot at a laptop port, input current limiting (AICL) then ended at
the lowest step (STAT_E 0xc0) and the battery got about 120 mA.

The charger detects the port type itself (automatic power source
detection, APSD) and reports it in STAT_D. The Android kernel for the
Nexus 7 uses it: 900 mA for a standard downstream port, 1800 mA for a
charging downstream port, a dedicated charger or an unknown charger.
It turns AICL off while it changes the limit and on again afterwards.

Use the APSD result to set the limit and report it as
POWER_SUPPLY_PROP_USB_TYPE:

  SDP, other, unknown  500 mA (USB 2.0)
  CDP                  1500 mA (BC 1.2)
  DCP                  "summit,usb-current-limit-microamp", or 1500 mA

All values are capped by "summit,usb-current-limit-microamp" if it is
set. A delayed work reads STAT_D 500 ms after the USB input became valid
and polls for up to 2 s until APSD is done. The same delay applies at
probe: directly after probe the charger NAKs the write to
CFG_VARIOUS_FUNCTION, 500 ms later it accepts it. If APSD does not
finish, the limit for an unknown port is used. When the input goes away,
the work sets that limit as well, so that a new connection never starts
with the higher limit of the previous charger. The work runs after every
USB input interrupt, even if the input state looks unchanged: on the
Nexus 7, a quick move from a laptop port to the charger gave two
interrupts, but the driver saw the input as connected both times and
kept the limit for the laptop port. Nothing changes while the charger
supplies VBUS in host mode.

The detection relies on the interrupt for input changes, so it is only
used if the interrupt is available. The STAT_D and AICL bit layout is
only known for the SMB347, so SMB345 and SMB358 are left as they are.
APSD is turned on at probe, as in the Android driver.

On the Nexus 7 (2012) the original charger is detected as DCP and now
gives about 1160 mA into the battery at 37-39 % charge with the display
off, against 600 mA before (31-33 %), and about 970 mA at 74-82 % with
the display on, against 560 mA before (84-86 %). 1800 mA in the device
tree gave no further gain. A laptop port detected as CDP gives about
640 mA instead of 505 mA.

This changes the behaviour of boards that set the property to a value
above 500 mA, today exynos4412-p4note and omap4-samsung-espresso10
with 1800 mA: they keep 1800 mA with a dedicated charger, but draw
500 mA from a PC port.

The STAT_D, AICL and APSD bits and the 20 ms delay before turning AICL
back on come from drivers/power/smb347-charger.c in the
android-tegra3-grouper-3.1-lollipop-release branch of
https://android.googlesource.com/kernel/tegra.

Signed-off-by: Patrick Fischer <admin@tsv-stahl-riesa.de>
---
 drivers/power/supply/smb347-charger.c | 252 +++++++++++++++++++++++++++++++++-
 1 file changed, 250 insertions(+), 2 deletions(-)

diff --git a/drivers/power/supply/smb347-charger.c b/drivers/power/supply/smb347-charger.c
index 03d58b378a25..330c05cfddc3 100644
--- a/drivers/power/supply/smb347-charger.c
+++ b/drivers/power/supply/smb347-charger.c
@@ -8,7 +8,9 @@
  *          Mika Westerberg <mika.westerberg@linux.intel.com>
  */
 
+#include <linux/bitfield.h>
 #include <linux/delay.h>
+#include <linux/devm-helpers.h>
 #include <linux/err.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
@@ -19,6 +21,7 @@
 #include <linux/property.h>
 #include <linux/regmap.h>
 #include <linux/regulator/driver.h>
+#include <linux/workqueue.h>
 
 #include <dt-bindings/power/summit,smb347-charger.h>
 
@@ -44,11 +47,13 @@
 #define CFG_CURRENT_LIMIT_DC_SHIFT		4
 #define CFG_CURRENT_LIMIT_USB_MASK		0x0f
 #define CFG_VARIOUS_FUNCTION			0x02
+#define CFG_VARIOUS_FUNCTION_AICL		BIT(4)
 #define CFG_FLOAT_VOLTAGE			0x03
 #define CFG_FLOAT_VOLTAGE_FLOAT_MASK		0x3f
 #define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK	0xc0
 #define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT	6
 #define CFG_CHARGE_CONTROL			0x04
+#define CFG_CHARGE_CONTROL_APSD			BIT(2)
 #define CFG_STAT				0x05
 #define CFG_STAT_DISABLED			BIT(5)
 #define CFG_STAT_ACTIVE_HIGH			BIT(7)
@@ -131,10 +136,29 @@
 #define STAT_C_CHG_TERM				BIT(5)
 #define STAT_C_CHARGER_ERROR			BIT(6)
 #define STAT_D					0x3e
+#define STAT_D_APSD_RESULT_MASK			GENMASK(2, 0)
+#define STAT_D_APSD_RESULT_CDP			1
+#define STAT_D_APSD_RESULT_DCP			2
+#define STAT_D_APSD_RESULT_OTHER		3
+#define STAT_D_APSD_RESULT_SDP			4
+#define STAT_D_APSD_DONE			BIT(3)
 #define STAT_E					0x3f
 
 #define SMB347_MAX_REGISTER			0x3f
 
+/*
+ * USB input current limits (in uA) for the port types reported by APSD.
+ * A dedicated charger gets "summit,usb-current-limit-microamp" if given.
+ */
+#define SMB347_SDP_CURRENT_LIMIT		500000U		/* USB 2.0 */
+#define SMB347_CDP_CURRENT_LIMIT		1500000U	/* BC 1.2 */
+#define SMB347_DCP_CURRENT_LIMIT		1500000U	/* BC 1.2 */
+
+/* APSD takes a few hundred milliseconds after the input became valid. */
+#define SMB347_APSD_DELAY_MS			500
+#define SMB347_APSD_POLL_US			(100 * USEC_PER_MSEC)
+#define SMB347_APSD_TIMEOUT_US			(2 * USEC_PER_SEC)
+
 /**
  * struct smb347_charger - smb347 charger instance
  * @dev: pointer to device
@@ -142,11 +166,14 @@
  * @mains: power_supply instance for AC/DC power
  * @usb: power_supply instance for USB power
  * @usb_rdev: USB VBUS regulator device
+ * @apsd_work: sets the USB input current limit from the detected port type
  * @id: SMB charger ID
  * @mains_online: is AC/DC input connected
  * @usb_online: is USB input connected
  * @irq_unsupported: is interrupt unsupported by SMB hardware
  * @usb_vbus_enabled: is USB VBUS powered by SMB charger
+ * @use_apsd: USB input current limit follows the APSD result
+ * @usb_type: USB port type detected by APSD (%POWER_SUPPLY_USB_TYPE_*)
  * @max_charge_current: maximum current (in uA) the battery can be charged
  * @max_charge_voltage: maximum voltage (in uV) the battery can be charged
  * @pre_charge_current: current (in uA) to use in pre-charging phase
@@ -202,11 +229,14 @@ struct smb347_charger {
 	struct power_supply	*mains;
 	struct power_supply	*usb;
 	struct regulator_dev	*usb_rdev;
+	struct delayed_work	apsd_work;
 	unsigned int		id;
 	bool			mains_online;
 	bool			usb_online;
 	bool			irq_unsupported;
 	bool			usb_vbus_enabled;
+	bool			use_apsd;
+	int			usb_type;
 
 	unsigned int		max_charge_current;
 	unsigned int		max_charge_voltage;
@@ -280,6 +310,14 @@ static const unsigned int ccc_tbl[NUM_CHIP_TYPES][4] = {
 	[SMB358] = {  200000,  450000,  600000,  900000 },
 };
 
+/*
+ * Chips with automatic power source detection (APSD) results in STAT_D and
+ * the AICL enable bit in CFG_VARIOUS_FUNCTION as described above.
+ */
+static const bool apsd_tbl[NUM_CHIP_TYPES] = {
+	[SMB347] = true,
+};
+
 /* Convert register value to current using lookup table */
 static int hw_to_current(const unsigned int *tbl, size_t size, unsigned int val)
 {
@@ -497,6 +535,59 @@ static int smb347_set_current_limits(struct smb347_charger *smb)
 	return 0;
 }
 
+/*
+ * Sets the USB input current limit. Like the vendor driver, turn AICL off
+ * while the limit changes, so that the charger detects the input current
+ * again with the new limit once AICL is turned back on.
+ *
+ * Must be called with the configuration registers writable.
+ */
+static int smb347_set_usb_current_limit(struct smb347_charger *smb,
+					unsigned int limit)
+{
+	unsigned int id = smb->id;
+	unsigned int val;
+	bool aicl;
+	int hw, ret, ret2;
+
+	hw = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]), limit);
+	if (hw < 0)
+		return hw;
+
+	ret = regmap_read(smb->regmap, CFG_CURRENT_LIMIT, &val);
+	if (ret < 0)
+		return ret;
+
+	if ((val & CFG_CURRENT_LIMIT_USB_MASK) == hw)
+		return 0;
+
+	ret = regmap_read(smb->regmap, CFG_VARIOUS_FUNCTION, &val);
+	if (ret < 0)
+		return ret;
+
+	aicl = val & CFG_VARIOUS_FUNCTION_AICL;
+	if (aicl) {
+		ret = regmap_clear_bits(smb->regmap, CFG_VARIOUS_FUNCTION,
+					CFG_VARIOUS_FUNCTION_AICL);
+		if (ret < 0)
+			return ret;
+	}
+
+	ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
+				 CFG_CURRENT_LIMIT_USB_MASK, hw);
+
+	if (aicl) {
+		/* The vendor driver waits 20 ms before turning AICL back on. */
+		msleep(20);
+		ret2 = regmap_set_bits(smb->regmap, CFG_VARIOUS_FUNCTION,
+				       CFG_VARIOUS_FUNCTION_AICL);
+		if (!ret)
+			ret = ret2;
+	}
+
+	return ret;
+}
+
 static int smb347_set_voltage_limits(struct smb347_charger *smb)
 {
 	int ret;
@@ -794,6 +885,125 @@ static int smb347_hw_init(struct smb347_charger *smb)
 	return ret;
 }
 
+static int smb347_apsd_usb_type(unsigned int stat_d)
+{
+	switch (FIELD_GET(STAT_D_APSD_RESULT_MASK, stat_d)) {
+	case STAT_D_APSD_RESULT_CDP:
+		return POWER_SUPPLY_USB_TYPE_CDP;
+	case STAT_D_APSD_RESULT_DCP:
+		return POWER_SUPPLY_USB_TYPE_DCP;
+	case STAT_D_APSD_RESULT_SDP:
+		return POWER_SUPPLY_USB_TYPE_SDP;
+	default:
+		/* Includes STAT_D_APSD_RESULT_OTHER (non-BC 1.2 chargers). */
+		return POWER_SUPPLY_USB_TYPE_UNKNOWN;
+	}
+}
+
+static unsigned int smb347_usb_type_current_limit(struct smb347_charger *smb,
+						  int usb_type)
+{
+	unsigned int dcp = smb->usb_hc_current_limit ?: SMB347_DCP_CURRENT_LIMIT;
+
+	switch (usb_type) {
+	case POWER_SUPPLY_USB_TYPE_DCP:
+		return dcp;
+	case POWER_SUPPLY_USB_TYPE_CDP:
+		return min(dcp, SMB347_CDP_CURRENT_LIMIT);
+	default:
+		return min(dcp, SMB347_SDP_CURRENT_LIMIT);
+	}
+}
+
+static void smb347_apsd_work(struct work_struct *work)
+{
+	struct smb347_charger *smb = container_of(to_delayed_work(work),
+						  struct smb347_charger,
+						  apsd_work);
+	int usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
+	unsigned int limit, val;
+	int ret;
+
+	/* VBUS comes from the charger itself in USB host mode. */
+	if (smb->usb_vbus_enabled)
+		return;
+
+	/*
+	 * Without input, set the limit for an unknown port, so that the next
+	 * connection starts with it until APSD has detected the port type.
+	 */
+	if (smb->usb_online) {
+		ret = regmap_read_poll_timeout(smb->regmap, STAT_D, val,
+					       val & STAT_D_APSD_DONE,
+					       SMB347_APSD_POLL_US,
+					       SMB347_APSD_TIMEOUT_US);
+		if (!ret)
+			usb_type = smb347_apsd_usb_type(val);
+		else if (ret == -ETIMEDOUT)
+			dev_dbg(smb->dev, "APSD did not complete\n");
+		else
+			dev_err(smb->dev, "failed to read APSD result: %d\n", ret);
+	}
+
+	limit = smb347_usb_type_current_limit(smb, usb_type);
+
+	ret = smb347_set_writable(smb, true, true);
+	if (ret < 0)
+		goto out;
+
+	ret = smb347_set_usb_current_limit(smb, limit);
+	smb347_set_writable(smb, false, true);
+out:
+	if (ret < 0)
+		dev_err(smb->dev, "failed to set USB input current limit: %d\n",
+			ret);
+
+	WRITE_ONCE(smb->usb_type, usb_type);
+	power_supply_changed(smb->usb);
+}
+
+static void smb347_apsd_schedule(struct smb347_charger *smb)
+{
+	unsigned long delay = 0;
+
+	if (!smb->use_apsd)
+		return;
+
+	if (smb->usb_online)
+		delay = msecs_to_jiffies(SMB347_APSD_DELAY_MS);
+
+	mod_delayed_work(system_dfl_wq, &smb->apsd_work, delay);
+}
+
+static int smb347_apsd_init(struct smb347_charger *smb)
+{
+	int ret;
+
+	smb->use_apsd = apsd_tbl[smb->id] && smb->use_usb &&
+			!smb->irq_unsupported;
+	if (!smb->use_apsd)
+		return 0;
+
+	ret = smb347_set_writable(smb, true, true);
+	if (ret < 0)
+		return ret;
+
+	/* The vendor driver turns APSD on at probe as well. */
+	ret = regmap_set_bits(smb->regmap, CFG_CHARGE_CONTROL,
+			      CFG_CHARGE_CONTROL_APSD);
+	smb347_set_writable(smb, false, true);
+	if (ret < 0)
+		return ret;
+
+	/*
+	 * Directly after probe the charger NAKs the write to
+	 * CFG_VARIOUS_FUNCTION. Wait as after a new connection.
+	 */
+	smb347_apsd_schedule(smb);
+
+	return 0;
+}
+
 static irqreturn_t smb347_interrupt(int irq, void *data)
 {
 	struct smb347_charger *smb = data;
@@ -885,6 +1095,14 @@ static irqreturn_t smb347_interrupt(int irq, void *data)
 			if (smb->use_usb)
 				power_supply_changed(smb->usb);
 		}
+
+		/*
+		 * The USB input may have been disconnected and connected again
+		 * since the last interrupt, so detect the port type again even
+		 * if the input state looks unchanged.
+		 */
+		if (irqstat_e & IRQSTAT_E_USBIN_UV_IRQ)
+			smb347_apsd_schedule(smb);
 		handled = true;
 	}
 
@@ -1157,6 +1375,10 @@ static int smb347_get_property_locked(struct power_supply *psy,
 		val->intval = ret;
 		break;
 
+	case POWER_SUPPLY_PROP_USB_TYPE:
+		val->intval = READ_ONCE(smb->usb_type);
+		break;
+
 	default:
 		return -EINVAL;
 	}
@@ -1191,6 +1413,15 @@ static enum power_supply_property smb347_properties[] = {
 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
 };
 
+static enum power_supply_property smb347_usb_properties[] = {
+	POWER_SUPPLY_PROP_STATUS,
+	POWER_SUPPLY_PROP_CHARGE_TYPE,
+	POWER_SUPPLY_PROP_ONLINE,
+	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
+	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
+	POWER_SUPPLY_PROP_USB_TYPE,
+};
+
 static bool smb347_volatile_reg(struct device *dev, unsigned int reg)
 {
 	switch (reg) {
@@ -1516,9 +1747,13 @@ static const struct power_supply_desc smb347_mains_desc = {
 static const struct power_supply_desc smb347_usb_desc = {
 	.name		= "smb347-usb",
 	.type		= POWER_SUPPLY_TYPE_USB,
+	.usb_types	= BIT(POWER_SUPPLY_USB_TYPE_SDP) |
+			  BIT(POWER_SUPPLY_USB_TYPE_CDP) |
+			  BIT(POWER_SUPPLY_USB_TYPE_DCP) |
+			  BIT(POWER_SUPPLY_USB_TYPE_UNKNOWN),
 	.get_property	= smb347_get_property,
-	.properties	= smb347_properties,
-	.num_properties	= ARRAY_SIZE(smb347_properties),
+	.properties	= smb347_usb_properties,
+	.num_properties	= ARRAY_SIZE(smb347_usb_properties),
 };
 
 static const struct regulator_desc smb347_usb_vbus_regulator_desc = {
@@ -1548,6 +1783,7 @@ static int smb347_probe(struct i2c_client *client)
 		return -ENOMEM;
 	smb->dev = &client->dev;
 	smb->id = id->driver_data;
+	smb->usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
 	i2c_set_clientdata(client, smb);
 
 	smb347_dt_parse_dev_info(smb);
@@ -1582,10 +1818,22 @@ static int smb347_probe(struct i2c_client *client)
 	if (ret < 0)
 		return ret;
 
+	/* Before the IRQ, so that it is cancelled after the IRQ is freed. */
+	ret = devm_delayed_work_autocancel(dev, &smb->apsd_work,
+					   smb347_apsd_work);
+	if (ret)
+		return ret;
+
 	ret = smb347_irq_init(smb, client);
 	if (ret)
 		return ret;
 
+	ret = smb347_apsd_init(smb);
+	if (ret < 0) {
+		smb347_irq_disable(smb);
+		return dev_err_probe(dev, ret, "failed to enable APSD\n");
+	}
+
 	usb_rdev_cfg.dev = dev;
 	usb_rdev_cfg.driver_data = smb;
 	usb_rdev_cfg.regmap = smb->regmap;

-- 
2.56.1



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 0/3] power: supply: smb347: Set the USB input current limit from APSD
  2026-11-21  9:30 [PATCH 0/3] power: supply: smb347: Set the USB input current limit from APSD Patrick Fischer via B4 Relay
                   ` (2 preceding siblings ...)
  2026-11-21  9:30 ` [PATCH 3/3] power: supply: smb347: Set the USB input current limit from APSD Patrick Fischer via B4 Relay
@ 2026-11-21 20:59 ` David Heidelberg
  3 siblings, 0 replies; 5+ messages in thread
From: David Heidelberg @ 2026-11-21 20:59 UTC (permalink / raw)
  To: admin, Sebastian Reichel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Dmitry Osipenko
  Cc: Svyatoslav Ryhel, Bruce Robertson, Martin Jücker,
	Mithil Bavishi, linux-pm, devicetree, linux-kernel

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@gmail.com/
> 
> ---
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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-21 20:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-21  9:30 [PATCH 0/3] power: supply: smb347: Set the USB input current limit from APSD Patrick Fischer via B4 Relay
2026-11-21  9:30 ` [PATCH 1/3] dt-bindings: power: supply: summit,smb347: Clarify the USB current limit Patrick Fischer via B4 Relay
2026-11-21  9:30 ` [PATCH 2/3] power: supply: smb347: Make the APSD and AICL registers readable Patrick Fischer via B4 Relay
2026-11-21  9:30 ` [PATCH 3/3] power: supply: smb347: Set the USB input current limit from APSD Patrick Fischer via B4 Relay
2026-11-21 20:59 ` [PATCH 0/3] " David Heidelberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®