power: supply: Add Qualcomm SMB1351 charger driver
Subsystems (3)
v3 cover power: supply: Add Qualcomm SMB1351 charger driver
The Vsmart Active 1 (SDM660, PM660) has a Qualcomm SMB1351 parallel charger at 0x1d on blsp_i2c2, next to the PM660 charger. Its version register reads 0xb2, which the downstream driver identifies as an SMB1350. Without a driver it runs on its OTP defaults: charging is enabled through an active-low pin that the PM660 holds low, with a 4.5 V float voltage on a 4.4 V battery. It is powered from VBUS alone, so it does not answer on I2C without VBUS and starts over from those defaults whenever VBUS comes back. As a result, whenever the PM660 suspends its USB input, the SMB1351 charges the battery on its own. Since commit 98d68b74ebb9 ("power: supply: qcom_smbx: allow disabling charging"), qcom_smbx does that when user space writes 0 to the status property of the PM660 charger to stop charging.
Patch 1 documents the chip with the battery it charges, its STAT output as an optional interrupt and, on a parallel charger, the PMIC charger that shares its USB input. The SMB1350 gets its own compatible with qcom,smb1351 as the fallback. Patch 2 adds a driver that keeps the SMB1351 from charging: each time the PMIC charger reports a change, it hands the charge enable control to I2C with charging disabled and sets the float voltage from the battery. A status or charge_type read that finds the chip reset, or not answering, makes it configure the chip again. Its power supply reports whether the chip has VBUS, the charging status, the charge type and the float voltage set in the chip. Parallel charging is left for later, since it also needs the PMIC charger to split the current and to drive the enable pin. Patch 2 also explains why this is a new driver rather than part of smb347-charger.
Making qcom_smbx drive the enable pin high would also keep the chip off, but only because of the pin polarity in its OTP, which the downstream driver does not rely on: it programs the polarity over I2C. A driver that programs the enable control over I2C works whatever the PMIC does with the pin, gets the chip described in DT, and gives parallel charging something to build on.
Tested on the Vsmart Active 1 with a 7.2-based kernel from the sdm660-mainline tree [1] (its qcom-sdm660-7.2.y branch plus a few pending pull requests), with the same driver as this series, CONFIG_CHARGER_QCOM_SMB1351=m and a DT node [2] that also passes dtbs_check against this binding:
- With the PM660 USB input suspended (on that kernel through charge_behaviour=force-discharge, which the sdm660-mainline tree adds to qcom_smbx and which sets the same USBIN_SUSPEND bit as writing 0 to status), the battery discharges at 250 to 470 mA and the SMB1351 reports "Not charging". Without the driver it charged the battery at about 390 mA in that state.
- After an unplug, smb1351-charger goes offline 2.35 s after the PM660 charger reports the change (2.37 s in a second run), when the driver's retries run out.
- After a replug, the chip is back on its OTP defaults (0x03 = 0xf2, 0x06 = 0x6b) and is configured again (0x03 = 0xed, 0x06 = 0x0b) within about 0.4 s of VBUS settling. This also works while the PM660 has its USB input suspended, because the PM660 charger still reports plug events then. In that run nothing read the status, so the PM660 events alone were enough.
- Writing the OTP enable setting (0x06 = 0x6b) behind the driver's back makes the next status read return "Unknown", and the chip is configured again within a second.
- Probing without VBUS, unbind/bind, and unloading the module inside the retry windows work without warnings.
While the PM660 has its USB input suspended, smb1351-charger still reports online = 1, because VBUS is present, so user space such as UPower sees external power in that state.
On sre/for-next (7071d4750856), the driver builds without warnings with ARCH=arm64 LLVM=1 W=1 (clang 23.1.5), sparse (C=2) and the coccinelle report scripts find nothing in it, dt_binding_check and dt-check-style --mode=strict pass for the new schema, and checkpatch.pl --strict only asks whether MAINTAINERS needs updating for the new files.
The Vsmart Active 1 DT is only in the sdm660-mainline tree so far, and the SMB1351 node for it is in [2]. sdm630-sony-xperia-nile.dtsi also mentions an SMB1351 on blsp_i2c2, and msm8996pro-xiaomi-scorpio.dts names a STAT_SMB1351 line, so the binding may find more users.
How this series was made: with an LLM coding assistant over longer interactive sessions. I asked it to find out why the battery kept charging while the PM660 had its USB input suspended, and later to fix that in the kernel. It read the SMB1351 registers over i2c-dev and the PM660 registers through regmap debugfs on the phone, compared them with the stock DT and the downstream driver, and laid out the options. After I chose a separate I2C driver, it wrote the binding, the driver, the commit messages and this cover letter, and ran the build checks and the device test scripts above. It also ran sparse and the coccinelle report scripts on the driver; they found nothing. For v3 it wrote the locking fix below and re-ran the checks and the device tests. I did the cable tests on the phone and reviewed the result.
[1] https://github.com/sdm660-mainline/linux
[2] https://github.com/sdm660-mainline/linux/pull/241
Changes in v3:
- Patch 2: set retry_until and queue the work under a spinlock, so
that a status read cannot replace the longer deadline set by external_power_changed() with its shorter one (found by the Sashiko review bot).
- Drop the Assisted-by tags: since commit 1e035297ffda ("docs: remove Assisted-by references") the tool use is described in this cover letter instead.
- Rebase onto sre/for-next (7071d4750856, on v7.4-rc1).
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Patch 1: drop monitored-battery from properties, power-supply.yaml brings it (Krzysztof). Add Krzysztof's Reviewed-by.
- Patch 2: unchanged.
- Link to v1: https://lore.kernel.org/r/[email protected]
Adrian Nguyen (2):
dt-bindings: power: supply: Add Qualcomm SMB1351 charger
power: supply: Add Qualcomm SMB1351 charger driver
.../bindings/power/supply/qcom,smb1351.yaml | 69 +++ drivers/power/supply/Kconfig | 14 + drivers/power/supply/Makefile | 1 + drivers/power/supply/qcom_smb1351.c | 439 ++++++++++++++++++ 4 files changed, 523 insertions(+) create mode 100644 Documentation/devicetree/bindings/power/supply/qcom,smb1351.yaml create mode 100644 drivers/power/supply/qcom_smb1351.c
base-commit: 7071d4750856abbc68dfe431acc58a1d45dd42e8
Signature · 1 line
2.43.0
PATCH v3 1/2 dt-bindings: power: supply: Add Qualcomm SMB1351 charger
The Qualcomm SMB1351 is a switch-mode battery charger with an I2C interface. Some phones use it as a parallel charger next to the charger in their PMIC: both take power from the same USB input and charge the same battery. On the Vsmart Active 1 (SDM660, PM660) the SMB1351 is powered from that input alone, so it does not respond on I2C while the input is absent.
The SMB1350 has the same programming interface, and the two can be told apart by their version register, so the SMB1350 gets its own compatible with qcom,smb1351 as the fallback. The version register of the chip on the Vsmart Active 1 identifies it as an SMB1350.
Document the chip with the battery it charges and its STAT output, which it can drive as an interrupt line. On a parallel charger, power-supplies refers to the PMIC charger whose USB input also powers the SMB1351. The SMB1351 can also be the only charger of a board, with no PMIC charger to refer to, so that property is optional.
---
Notes:
Changes in v3:
- Drop the Assisted-by tag: since commit 1e035297ffda ("docs: remove Assisted-by references") the cover letter carries that information.
.../bindings/power/supply/qcom,smb1351.yaml | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Documentation/devicetree/bindings/power/supply/qcom,smb1351.yaml
Documentation/devicetree/bindings/power/supply/qcom,smb1351.yaml new file +69 -0
@@ -0,0 +1,69 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/power/supply/qcom,smb1351.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Qualcomm SMB1351 battery charger + +maintainers: + - Adrian Nguyen <[email protected]> + +description: + The Qualcomm SMB1351 is a switch-mode single-cell battery charger with an + I2C interface. Some boards use it as a parallel charger next to the + charger in a PMIC, taking power from the same USB input and charging the + same battery. On some of them it is powered from that input only, so it + does not respond on I2C and loses its configuration while the input is + absent. The SMB1350 has the same programming interface, and the two can be + told apart by their version register. + +allOf: + - $ref: power-supply.yaml# + +properties: + compatible: + oneOf: + - const: qcom,smb1351 + - items: + - const: qcom,smb1350 + - const: qcom,smb1351 + + reg: + maxItems: 1 + + interrupts: + description: + The STAT output, which the chip can drive as an interrupt line. + maxItems: 1 + + power-supplies: + description: + The PMIC charger whose USB input (VBUS) also powers this charger, on + boards that use it as a parallel charger. + maxItems: 1 + +required: + - compatible + - reg + - monitored-battery + +unevaluatedProperties: false + +examples: + - | + #include <dt-bindings/interrupt-controller/irq.h> + + i2c { + #address-cells = <1>; + #size-cells = <0>; + + charger@1d { + compatible = "qcom,smb1350", "qcom,smb1351"; + reg = <0x1d>; + interrupts = <61 IRQ_TYPE_LEVEL_LOW>; + + monitored-battery = <&battery>; + power-supplies = <&pm660_charger>; + }; + };
Signature · 1 line
2.43.0
PATCH v3 2/2 power: supply: Add Qualcomm SMB1351 charger driver
In reply to Adrian NguyenThe Qualcomm SMB1351 is a switch-mode battery charger that some phones use in parallel with the charger in their PMIC.
On the Vsmart Active 1 (SDM660, PM660) it is powered from VBUS alone:
without VBUS it does not answer on I2C, and whenever VBUS comes back it starts over from its OTP defaults. Those defaults enable charging through an active-low pin that the PM660 holds low, with a 4.5 V float voltage on a 4.4 V battery. So whenever the PM660 suspends its USB input, the SMB1351 charges the battery on its own. Since commit 98d68b74ebb9 ("power: supply: qcom_smbx: allow disabling charging"), qcom_smbx does that when user space writes 0 to the status property of the PM660 charger to stop charging.
Add a driver that keeps the charger disabled. Each time the PMIC charger reports a change, the driver hands the charge enable control to I2C with charging disabled, which does not depend on how the PMIC drives the enable pin, and sets the float voltage from the monitored battery. Making qcom_smbx drive the pin high instead would keep the chip off only because of the pin polarity in its OTP, which the downstream driver does not rely on: it programs the polarity over I2C. The float voltage only matters if charging gets enabled anyway, for example through i2c-dev: charging then stops at the battery's maximum voltage instead of 4.5 V.
While the chip does not answer, the driver retries for about two seconds and then treats VBUS as absent. A chip without VBUS NACKs its address, so only other I2C errors are logged. A power supply reports whether the chip answers, which with this wiring means that it has VBUS. It also reports the charging status, the charge type and the float voltage set in the chip.
The chip can also lose VBUS and reset without the PMIC charger reporting a change. Until the PMIC suspends its input, a reset chip looks the same as a configured one in the status register: neither charges. So reading the status or the charge type also checks that the enable control is still set to I2C. If it is not, or if the chip does not answer, they read as unknown and the driver tries to configure the chip again.
Charging in parallel also needs the PMIC charger to split the current and to drive the enable pin, so it is left for later.
The SMB1351 shares its float voltage and enable control registers (0x03 and 0x06) with the Summit SMB347, but not its command and status
registers: configuration writes are allowed by bit 6 of 0x30, not by bit 7, which the downstream driver names a reload bit; charging is enabled in 0x32, not 0x30; and the status is at 0x3a, not 0x3d. smb347-charger also applies its charging configuration only once, in probe, and fails to probe when the chip does not answer, so it cannot handle a chip that answers only while it has VBUS and has to be configured again each time VBUS returns.
The version register of the chip on this board reads 0xb2, which the downstream driver identifies as an SMB1350. The two chips share the registers used here. The register definitions and the enable control come from that driver:
https://github.com/LineageOS/android_kernel_xiaomi_sdm660/blob/49688d8750f2fca28271f936cfcfec5d97c0d7a4/drivers/power/supply/qcom/smb1351-charger.c
The cause was found with an LLM coding assistant, which read the SMB1351 and PM660 registers on the phone and compared them with the stock devicetree and the downstream driver.
---
Notes:
Changes in v3:
- Set retry_until and queue the work under a spinlock, in the status read and in external_power_changed(). Without it, a status read could find no work pending, get preempted while external_power_changed() set the longer deadline and queued the work, and then replace that deadline with its shorter one (found by the Sashiko review bot).
- Drop the Assisted-by tag, as in patch 1.
drivers/power/supply/Kconfig | 14 + drivers/power/supply/Makefile | 1 + drivers/power/supply/qcom_smb1351.c | 439 ++++++++++++++++++++++++++++ 3 files changed, 454 insertions(+) create mode 100644 drivers/power/supply/qcom_smb1351.c
drivers/power/supply/Kconfig +14 -0
@@ -1182,6 +1182,20 @@ config CHARGER_QCOM_SMB2 adds support for the SMB2 switch mode battery charger found in PMI8998 and related PMICs. +config CHARGER_QCOM_SMB1351 + tristate "Qualcomm SMB1351 parallel charger driver" + depends on I2C + select REGMAP_I2C + help + Say Y here to enable the driver for the Qualcomm SMB1351 charger + and the register-compatible SMB1350, which some phones use in + parallel with the charger in their PMIC. The driver keeps the + charger from charging the battery on its own and reports whether + it has input power. + + This driver can also be built as a module. If so, the module will + be called qcom_smb1351. + config FUEL_GAUGE_MM8013 tristate "Mitsumi MM8013 fuel gauge driver" depends on I2C
drivers/power/supply/Makefile +1 -0
@@ -132,6 +132,7 @@ obj-$(CONFIG_CHARGER_SURFACE) += surface_charger.o obj-$(CONFIG_BATTERY_CHARGER_SURFACE_RT) += surface-rt-ec.o obj-$(CONFIG_BATTERY_UG3105) += ug3105_battery.o obj-$(CONFIG_CHARGER_QCOM_SMB2) += qcom_smbx.o +obj-$(CONFIG_CHARGER_QCOM_SMB1351) += qcom_smb1351.o obj-$(CONFIG_FUEL_GAUGE_MM8013) += mm8013.o obj-$(CONFIG_MACSMC_POWER) += macsmc-power.o obj-$(CONFIG_CHARGER_MAX77759) += max77759_charger.o
drivers/power/supply/qcom_smb1351.c new file +439 -0
439 lines not on this page · raw message
Signature · 1 line
2.43.0
…
Quoted 2 lines
+++
b/drivers/power/supply/qcom_smb1351.c(quoted diff) +0
-0@@ -0,0 +1,439 @@
…
Quoted 2 lines
(quoted diff) +2
-0+static void smb1351_external_power_changed(struct power_supply *psy) +{
…
drivers/power/supply/qcom_smb1351.c line 309
Quoted 8 lines
drivers/power/supply/qcom_smb1351.c new file +8
-0+spin_lock (&chip->lock); +WRITE_ONCE (chip->retry_until, + jiffies +msecs_to_jiffies (SMB1351_SETTLE_MS + +SMB1351_RETRY_TIMEOUT_MS )); +mod_delayed_work (system_freezable_wq, &chip->work, +msecs_to_jiffies (SMB1351_SETTLE_MS )); +spin_unlock (&chip->lock); +}
…
Under which circumstances would you become interested to apply a statement like “guard(spinlock)(&chip->lock);”?
https://elixir.bootlin.com/linux/v7.3.1/source/include/linux/spinlock.h#L610-L612
Regards,
Markus
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] power: supply: qcom_smb1351: race on retry_until while smb1351_work() runs - [Low] power: supply…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] power: supply: qcom_smb1351: race on retry_until while smb1351_work() runs
- [Low] power: supply: qcom_smb1351: unlocked work scheduling in smb1351_probe()
--
commit 51205e0d5cfa98bce2fcb03aa43f9fb231439ca3
Author: Adrian Nguyen <[email protected]>
power: supply: Add Qualcomm SMB1351 charger driver
This commit adds a power supply driver for the Qualcomm SMB1351/SMB1350 parallel charger that keeps charging disabled over I2C and reports VBUS presence and charger status.
Quoted 5 lines
drivers/power/supply/qcom_smb1351.c new file +0
-0
[ ... ]
Quoted 8 lines
drivers/power/supply/qcom_smb1351.c new file +7
-0@@ -0,0 +1,439 @@ +/* +* The chip can lose VBUS, and with it its configuration, without the +* PMIC charger reporting a change. If it has been reset or does not +* answer, run the work again, unless a run with its own deadline is +* already pending. Report "Unknown" rather than an error, which would +* drop the whole uevent. +*/
drivers/power/supply/qcom_smb1351.c line 17
Quoted 10 lines
drivers/power/supply/qcom_smb1351.c new file +10
-0+ ret =smb1351_read_status (chip, &stat); +if (ret) { +spin_lock (&chip->lock); +if (!delayed_work_pending (&chip->work)) { +WRITE_ONCE (chip->retry_until, jiffies + +msecs_to_jiffies (SMB1351_RETRY_TIMEOUT_MS )); +queue_delayed_work (system_freezable_wq, &chip->work,0 ); + } +spin_unlock (&chip->lock); + }
[Severity: Medium]
Can smb1351_get_property() still overwrite chip->retry_until and bypass the retry backoff while smb1351_work() is running?
The workqueue core clears WORK_STRUCT_PENDING_BIT before calling smb1351_work(), so delayed_work_pending(&chip->work) returns false while smb1351_work() is executing I2C transfers:
CPU0 (kworker) CPU1smb1351_work()
/* pending bit cleared */
regmap_read(..., &version)
/* fails when VBUS is lost */
smb1351_get_property()
smb1351_read_status() -> -ENXIO
spin_lock(&chip->lock)
!delayed_work_pending(&chip->work)
chip->retry_until = jiffies + 2s
queue_delayed_work(..., 0)
spin_unlock(&chip->lock) time_before(jiffies, retry_until)
queue_delayed_work(..., 200ms) <-- ignored, already queued with delay 0
When userspace reads status or charge_type while smb1351_work() is running, smb1351_get_property() extends chip->retry_until by another 2 seconds and queues chip->work with a 0 ms delay. smb1351_work() then sees the extended deadline instead of expiring and setting chip->online to false, and its own queue_delayed_work() with SMB1351_RETRY_MS becomes a no-op because the work is already pending, re-running smb1351_work() immediately without the 200 ms backoff.
In addition, because smb1351_get_property() reads chip->online before smb1351_read_status() and does not recheck it under chip->lock (and smb1351_work() updates chip->online outside chip->lock), a status read that races with the final retry in smb1351_work() setting chip->online to false will find !delayed_work_pending(&chip->work) after smb1351_work() finishes and start a new 2-second retry loop.
[ ... ]
drivers/power/supply/qcom_smb1351.c line 413
Quoted 8 lines
drivers/power/supply/qcom_smb1351.c new file +8
-0+/* Leave the chip to the work, it does not answer without VBUS */ +WRITE_ONCE (chip->retry_until, + jiffies +msecs_to_jiffies (SMB1351_RETRY_TIMEOUT_MS )); +enable_delayed_work (&chip->work); +mod_delayed_work (system_freezable_wq, &chip->work,0 ); + +return 0 ; +}
[Severity: Low]
Can smb1351_probe() race with smb1351_external_power_changed() when enabling and scheduling chip->work without holding chip->lock?
chip->psy is already live after devm_power_supply_register(), so smb1351_external_power_changed() can run on another CPU while probe finishes:
CPU0 CPU1smb1351_probe()
devm_power_supply_register()
...
chip->retry_until = jiffies + 2s
enable_delayed_work(&chip->work)
smb1351_external_power_changed()
spin_lock(&chip->lock)
chip->retry_until = jiffies + 2.3s
mod_delayed_work(..., 300ms)
spin_unlock(&chip->lock) mod_delayed_work(..., 0) <-- overwrites 300ms settle delay with 0
If smb1351_external_power_changed() runs between enable_delayed_work() and mod_delayed_work(), smb1351_probe() replaces the 300 ms VBUS settle delay (SMB1351_SETTLE_MS) with 0, running smb1351_work() before VBUS has settled. If smb1351_external_power_changed() runs just before enable_delayed_work(), its mod_delayed_work() is dropped while the work is disabled and smb1351_probe() overwrites the longer retry_until deadline and schedules the work without the settle delay.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2