From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.180.11 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796190355; b=pd1dUbJEHTFkvaYOtfNrvWKiNtgPnNB2AUyJtWn9/2vwnRc64PbA/wJf0yPq0o38iSmq s/aOwQUjj7bRwhxFJV0P4uH4WEiKNn2Q1wsZYPLHbqF0om9/0FKUjzDheP+93mNux4DCp qGDWPnCpLHNqxAiIJ0qdULRiJOILFTeluq72Mvl4NV7jB2GWMqCQJVq2QBR1gU9fjCClh 2LeMV63AKBgCE69b3SUiMJQHk9czQvy2Q0+LCFC9+He5qYCIog+n7isk286MC9+tc3MBT BSaqLBE9hxRHubMkeFQy4+2BfMTILeupjIx5NGlikbjsfF5TMeznkER8l44rZDH4Eqw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796190355; h=DMARC-Filter:DKIM-Signature:Received:Received:Date:From:To:Cc: Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To:X-Zoho-Virus-Status:X-Zoho-AV-Stamp: X-ZohoMailClient; bh=izuOOu7bPd/hfj5qK0HbDt65L28fR638Wiix9uLmK+A=; b=woJfUwRbDcEjLlc0DkfbKsu4ghDykt8xVaA6bgHQ1awBjeaIUFQ5SDRG2l9rcdUeDuu5 89btdx+05SP31eI0rRp+JDOK+D90iizqGLKXFgYIsBKCG4eAN1ad0BkNbAIKXj1j4FaGQ Lnj+B3OP+Z9QFq8nh+VUZbNVrnoKPtdbpAzS0u+GfT7pVlx0Dxf0td53OogJoEgs7xvL6 b6o5oFxv4ao9leNYN/e6ytaUPcr3lQlCPRq3rzkeoPD0mTbbM9fu84jovNhtGHi5SrvjE nPD++Zyce1YaaupYdHP+V/LNKdNRx4MwUvy90Gq5HjVM/UsK5X9bR2Z7B33WK8Edf/w== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=zohomail header.b=KUM90HWx; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.180.11 Received: from sender6-op-o11.zoho.com (sender6-op-o11.zoho.com [165.173.180.11]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5CD66606A338; Wed, 2 Dec 2026 05:45:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5CD66606A338 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=zohomail header.b=KUM90HWx ARC-Seal: i=1; a=rsa-sha256; t=1796190348; cv=none; d=zohomail.com; s=zohoarc; b=MVjkuTPM400gei01tHuej14gLyUxuzlUPkbQEqUlGGsLb2REGa7GryD26YsNhe/z/fnOD27GcOVKXNBmGMw/LjJoYq+zDPGMAd9RURj2KxmeUAqX+m4oibhbtyhBT6uQTOel24SPV+WMUKonnC7FwndA70l5Afg4wtWr0d6KLBg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1796190348; h=Content-Type:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=izuOOu7bPd/hfj5qK0HbDt65L28fR638Wiix9uLmK+A=; b=gBpMOj2RtpMWExRH8T7h0XSqdLjQdm2nGfe+ggPsqcLJ+c3NxTvns4ctE4x6V/5G2dm/Hyno/V0+FapoX1juVEK/OLvlSVSd2BUp5hn9usJ5t30q92flLlA3MB6dPpUaDcs1mZEsFPbfIg8xMe5wNJxBn9X1mmcRcZSkXnjan04= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=sebastian.reichel@collabora.com; dmarc=pass header.from= DKIM-Signature: a=rsa-sha256; b=KUM90HWxaN+WBw3UmuC72a63rMOetzb9xXiQz6+w4Qbc/Tjrhy6wUXKwuILEb7skh3b3UNorI2f3PI8fDvDvJGH44P2tPCcYObcw/9MBH8D+Y2ORSXKaL/G37oAx1mesdalCrf9+z+jpvmFUvUe+zuJRAY5HBlYHdemiEEAcPTc=; c=relaxed/relaxed; s=zohomail; d=collabora.com; v=1; bh=izuOOu7bPd/hfj5qK0HbDt65L28fR638Wiix9uLmK+A=; h=Content-Description:Content-ID:Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Resent-Sender:Resent-Cc:Resent-Date:Resent-To:Reply-To:Resent-Message-ID:Resent-From:Sender:Subject:To:Date:From:Message-Id:Reply-To:Subject:To:Cc; Received: by smtp.zohomail.com with SMTPS id 1796190345702124.48984938579144; Tue, 1 Dec 2026 21:45:45 -0800 (PST) Received: by venus (Postfix, from userid 1000) id 4C8AD182058; Wed, 02 Dec 2026 02:38:29 +0100 (CET) Date: Wed, 2 Dec 2026 02:38:29 +0100 From: Sebastian Reichel To: Adrian Nguyen Cc: Rob Herring , Krzysztof Kozlowski , Conor Dooley , linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, Casey Connolly Subject: Re: [PATCH v3 2/2] power: supply: Add Qualcomm SMB1351 charger driver Message-ID: References: <20261114112155.85068-1-thenguyen1024@gmail.com> <20261114112155.85068-3-thenguyen1024@gmail.com> X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="fqgxf4stu2odqyiq" Content-Disposition: inline In-Reply-To: <20261114112155.85068-3-thenguyen1024@gmail.com> X-Zoho-Virus-Status: 1 X-Zoho-AV-Stamp: zmail-av-0.2.28.1.5.5/296.168.99 X-ZohoMailClient: External --fqgxf4stu2odqyiq Content-Type: text/plain; protected-headers=v1; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Subject: Re: [PATCH v3 2/2] power: supply: Add Qualcomm SMB1351 charger driver MIME-Version: 1.0 Hi, On Sat, Nov 14, 2026 at 06:21:55PM +0700, Adrian Nguyen wrote: > The Qualcomm SMB1351 is a switch-mode battery charger that some phones > use in parallel with the charger in their PMIC. >=20 > 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. >=20 > 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. >=20 > 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. >=20 > 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. >=20 > Charging in parallel also needs the PMIC charger to split the current > and to drive the enable pin, so it is left for later. >=20 > 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. >=20 > 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/49688d8750= f2fca28271f936cfcfec5d97c0d7a4/drivers/power/supply/qcom/smb1351-charger.c >=20 > 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. >=20 > Signed-off-by: Adrian Nguyen > --- >=20 > 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 fi= nd > no work pending, get preempted while external_power_changed() set t= he > 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. >=20 > 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 >=20 > diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig > index ee7dc5f2617a..d1dd39f47e38 100644 > --- a/drivers/power/supply/Kconfig > +++ b/drivers/power/supply/Kconfig > @@ -1182,6 +1182,20 @@ config CHARGER_QCOM_SMB2 > adds support for the SMB2 switch mode battery charger found > in PMI8998 and related PMICs. > =20 > +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 > diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile > index f3c0d1006c1a..07b5f0d02f2a 100644 > --- a/drivers/power/supply/Makefile > +++ b/drivers/power/supply/Makefile > @@ -132,6 +132,7 @@ obj-$(CONFIG_CHARGER_SURFACE) +=3D surface_charger.o > obj-$(CONFIG_BATTERY_CHARGER_SURFACE_RT) +=3D surface-rt-ec.o > obj-$(CONFIG_BATTERY_UG3105) +=3D ug3105_battery.o > obj-$(CONFIG_CHARGER_QCOM_SMB2) +=3D qcom_smbx.o > +obj-$(CONFIG_CHARGER_QCOM_SMB1351) +=3D qcom_smb1351.o > obj-$(CONFIG_FUEL_GAUGE_MM8013) +=3D mm8013.o > obj-$(CONFIG_MACSMC_POWER) +=3D macsmc-power.o > obj-$(CONFIG_CHARGER_MAX77759) +=3D max77759_charger.o > diff --git a/drivers/power/supply/qcom_smb1351.c b/drivers/power/supply/q= com_smb1351.c > new file mode 100644 > index 000000000000..33289c767bdb > --- /dev/null > +++ b/drivers/power/supply/qcom_smb1351.c > @@ -0,0 +1,439 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/* > + * Qualcomm SMB1351 charger driver > + * > + * The SMB1351 is a switch-mode battery charger that some phones use in > + * parallel with the charger in their PMIC. On some of them, such as the > + * Vsmart Active 1, it is powered from VBUS only: without VBUS it does n= ot > + * answer on I2C, and when VBUS returns it starts over from its OTP > + * configuration. This driver supports only that wiring and treats a chip > + * that answers as having VBUS. It keeps charging disabled, and reapplies > + * its configuration whenever the PMIC charger reports a change or a sta= tus > + * read finds the chip reset. Parallel charging, interrupts and OTG are = not > + * supported yet. > + * > + * Copyright (c) 2016-2020, The Linux Foundation. All rights reserved. > + * Copyright (c) 2026 Adrian Nguyen > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#define SMB1351_VFLOAT_REG 0x03 > +#define SMB1351_VFLOAT_MASK GENMASK(5, 0) > + > +#define SMB1351_CHG_PIN_EN_CTRL_REG 0x06 > +#define SMB1351_EN_PIN_CTRL_MASK GENMASK(6, 5) > +#define SMB1351_EN_BY_I2C_0_DISABLE 0x00 > + > +#define SMB1351_VERSION_REG 0x2e > +#define SMB1351_VERSION_SMB1350 BIT(1) > + > +#define SMB1351_CMD_I2C_REG 0x30 > +#define SMB1351_CMD_BQ_CFG_ACCESS BIT(6) > + > +#define SMB1351_CMD_CHG_REG 0x32 > +#define SMB1351_CMD_CHG_EN BIT(1) > + > +#define SMB1351_STATUS_4_REG 0x3a > +#define SMB1351_STATUS_HOLD_OFF BIT(3) > +#define SMB1351_STATUS_CHG_MASK GENMASK(2, 1) > +#define SMB1351_STATUS_CHG_NONE 0 > +#define SMB1351_STATUS_CHG_PRE 1 > +#define SMB1351_STATUS_CHG_FAST 2 > +#define SMB1351_STATUS_CHG_TAPER 3 > + > +#define SMB1351_CHG_REVISION_REG 0x3f > + > +#define SMB1351_VFLOAT_MIN_UV 3500000 > +#define SMB1351_VFLOAT_MAX_UV 4500000 > +#define SMB1351_VFLOAT_STEP_UV 20000 > + > +/* Let VBUS settle after the PMIC charger reports a change */ > +#define SMB1351_SETTLE_MS 300 > +/* Right after VBUS appears the chip may not answer yet */ > +#define SMB1351_RETRY_MS 200 > +#define SMB1351_RETRY_TIMEOUT_MS 2000 > + > +struct smb1351 { > + struct device *dev; > + struct regmap *regmap; > + struct power_supply *psy; > + struct delayed_work work; > + spinlock_t lock; /* sets retry_until with queueing the work */ > + unsigned long retry_until; /* jiffies, when to stop retrying */ > + unsigned int vfloat_code; /* set before the work is enabled */ > + bool online; /* only written by the work */ > + bool identified; /* only used by the work */ > +}; > + > +static const enum power_supply_property smb1351_properties[] =3D { > + POWER_SUPPLY_PROP_STATUS, > + POWER_SUPPLY_PROP_CHARGE_TYPE, > + POWER_SUPPLY_PROP_ONLINE, > + POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, > +}; > + > +static int smb1351_status(unsigned int stat) > +{ > + if (stat & SMB1351_STATUS_HOLD_OFF) > + return POWER_SUPPLY_STATUS_NOT_CHARGING; > + > + if (FIELD_GET(SMB1351_STATUS_CHG_MASK, stat) !=3D SMB1351_STATUS_CHG_NO= NE) > + return POWER_SUPPLY_STATUS_CHARGING; > + > + return POWER_SUPPLY_STATUS_NOT_CHARGING; > +} > + > +static int smb1351_charge_type(unsigned int stat) > +{ > + switch (FIELD_GET(SMB1351_STATUS_CHG_MASK, stat)) { > + case SMB1351_STATUS_CHG_PRE: > + return POWER_SUPPLY_CHARGE_TYPE_TRICKLE; > + case SMB1351_STATUS_CHG_FAST: > + case SMB1351_STATUS_CHG_TAPER: /* constant voltage phase */ > + return POWER_SUPPLY_CHARGE_TYPE_FAST; > + default: > + return POWER_SUPPLY_CHARGE_TYPE_NONE; > + } > +} > + > +/* > + * Read the charger status, provided the chip is still configured. The d= river > + * only ever selects the I2C enable control, so any other setting means = that > + * the chip has been reset and is no longer kept from charging. > + */ > +static int smb1351_read_status(struct smb1351 *chip, unsigned int *stat) > +{ > + unsigned int en; > + int ret; > + > + ret =3D regmap_read(chip->regmap, SMB1351_CHG_PIN_EN_CTRL_REG, &en); > + if (ret) > + return ret; > + > + if ((en & SMB1351_EN_PIN_CTRL_MASK) !=3D SMB1351_EN_BY_I2C_0_DISABLE) > + return -EIO; > + > + return regmap_read(chip->regmap, SMB1351_STATUS_4_REG, stat); > +} > + > +static int smb1351_get_property(struct power_supply *psy, > + enum power_supply_property psp, > + union power_supply_propval *val) > +{ > + struct smb1351 *chip =3D power_supply_get_drvdata(psy); > + bool online =3D READ_ONCE(chip->online); > + unsigned int stat, vfloat; > + int ret; > + > + switch (psp) { > + case POWER_SUPPLY_PROP_ONLINE: > + val->intval =3D online; > + return 0; > + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: > + /* -ENODATA leaves the property out of a uevent */ > + if (!online) > + return -ENODATA; > + ret =3D regmap_read(chip->regmap, SMB1351_VFLOAT_REG, &vfloat); > + if (ret) > + return -ENODATA; > + val->intval =3D SMB1351_VFLOAT_MIN_UV + > + FIELD_GET(SMB1351_VFLOAT_MASK, vfloat) * > + SMB1351_VFLOAT_STEP_UV; > + return 0; > + case POWER_SUPPLY_PROP_STATUS: > + if (!online) { > + val->intval =3D POWER_SUPPLY_STATUS_DISCHARGING; > + return 0; > + } > + break; > + case POWER_SUPPLY_PROP_CHARGE_TYPE: > + if (!online) { > + val->intval =3D POWER_SUPPLY_CHARGE_TYPE_NONE; > + return 0; > + } > + break; > + default: > + return -EINVAL; > + } > + > + /* > + * 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. > + */ > + ret =3D smb1351_read_status(chip, &stat); > + if (ret) { > + spin_lock(&chip->lock); guard(spinlock)(&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); > + } > + > + if (psp =3D=3D POWER_SUPPLY_PROP_STATUS) > + val->intval =3D ret ? POWER_SUPPLY_STATUS_UNKNOWN : > + smb1351_status(stat); > + else > + val->intval =3D ret ? POWER_SUPPLY_CHARGE_TYPE_UNKNOWN : > + smb1351_charge_type(stat); > + > + return 0; > +} > + > +static int smb1351_configure(struct smb1351 *chip) > +{ > + struct regmap *map =3D chip->regmap; > + unsigned int cmd, en, vfloat; > + int ret; > + > + /* The configuration registers can only be written with this bit set */ > + ret =3D regmap_set_bits(map, SMB1351_CMD_I2C_REG, > + SMB1351_CMD_BQ_CFG_ACCESS); > + if (ret) > + return ret; > + > + /* Clear the command first, so that I2C control starts out disabled */ > + ret =3D regmap_clear_bits(map, SMB1351_CMD_CHG_REG, SMB1351_CMD_CHG_EN); > + if (ret) > + return ret; > + > + ret =3D regmap_update_bits(map, SMB1351_CHG_PIN_EN_CTRL_REG, > + SMB1351_EN_PIN_CTRL_MASK, > + SMB1351_EN_BY_I2C_0_DISABLE); > + if (ret) > + return ret; > + > + ret =3D regmap_update_bits(map, SMB1351_VFLOAT_REG, SMB1351_VFLOAT_MASK, > + FIELD_PREP(SMB1351_VFLOAT_MASK, > + chip->vfloat_code)); > + if (ret) > + return ret; > + > + ret =3D regmap_read(map, SMB1351_CMD_CHG_REG, &cmd); > + if (ret) > + return ret; > + ret =3D regmap_read(map, SMB1351_CHG_PIN_EN_CTRL_REG, &en); > + if (ret) > + return ret; > + ret =3D regmap_read(map, SMB1351_VFLOAT_REG, &vfloat); > + if (ret) > + return ret; > + > + if ((cmd & SMB1351_CMD_CHG_EN) || > + (en & SMB1351_EN_PIN_CTRL_MASK) !=3D SMB1351_EN_BY_I2C_0_DISABLE || > + FIELD_GET(SMB1351_VFLOAT_MASK, vfloat) !=3D chip->vfloat_code) > + return -EIO; > + > + return 0; > +} > + > +static void smb1351_work(struct work_struct *work) > +{ > + struct smb1351 *chip =3D container_of(to_delayed_work(work), > + struct smb1351, work); > + unsigned int version; > + bool online; > + int ret; > + > + /* The chip only answers while it has VBUS */ > + ret =3D regmap_read(chip->regmap, SMB1351_VERSION_REG, &version); > + online =3D !ret; > + > + if (online && !chip->identified) { > + bool smb1350 =3D version & SMB1351_VERSION_SMB1350; > + > + dev_info(chip->dev, "found %s (version %#04x)\n", > + smb1350 ? "SMB1350" : "SMB1351", version); > + chip->identified =3D true; > + } > + > + if (online) > + ret =3D smb1351_configure(chip); > + > + if (ret && time_before(jiffies, READ_ONCE(chip->retry_until))) { > + queue_delayed_work(system_freezable_wq, &chip->work, > + msecs_to_jiffies(SMB1351_RETRY_MS)); > + return; > + } > + > + if (!online) { > + /* Without VBUS the chip does not acknowledge its address */ > + if (ret !=3D -ENXIO) > + dev_warn_ratelimited(chip->dev, > + "failed to read version: %pe\n", > + ERR_PTR(ret)); > + } else if (ret) { > + dev_err_ratelimited(chip->dev, > + "failed to disable charging: %pe\n", > + ERR_PTR(ret)); > + } > + > + if (online !=3D chip->online) { > + dev_dbg(chip->dev, "VBUS %s\n", online ? "present" : "absent"); > + WRITE_ONCE(chip->online, online); > + power_supply_changed(chip->psy); > + } > +} > + > +static void smb1351_external_power_changed(struct power_supply *psy) > +{ > + struct smb1351 *chip =3D power_supply_get_drvdata(psy); > + > + /* > + * Under the lock, so that a status read that finds no work pending > + * cannot then replace this deadline with its shorter one. > + */ > + spin_lock(&chip->lock); guard(spinlock)(&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); > +} > + > +static const struct power_supply_desc smb1351_psy_desc =3D { > + .name =3D "smb1351-charger", > + .type =3D POWER_SUPPLY_TYPE_USB, > + .properties =3D smb1351_properties, > + .num_properties =3D ARRAY_SIZE(smb1351_properties), > + .get_property =3D smb1351_get_property, > + .external_power_changed =3D smb1351_external_power_changed, > +}; > + > +static const struct regmap_config smb1351_regmap_config =3D { > + .reg_bits =3D 8, > + .val_bits =3D 8, > + /* 0x40-0x47 are interrupt status registers that clear on read */ > + .max_register =3D SMB1351_CHG_REVISION_REG, > + /* The chip forgets its configuration whenever it loses VBUS */ > + .cache_type =3D REGCACHE_NONE, > +}; > + > +static void smb1351_disable_work(void *data) > +{ > + struct smb1351 *chip =3D data; > + > + disable_delayed_work_sync(&chip->work); > +} > + > +static int smb1351_probe(struct i2c_client *client) > +{ > + struct device *dev =3D &client->dev; > + struct power_supply_config psy_cfg =3D {}; > + struct power_supply_battery_info *info; > + struct smb1351 *chip; > + int vfloat_uv; > + int ret; > + > + chip =3D devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); chip =3D devm_kzalloc_obj(dev, *chip); > + if (!chip) > + return -ENOMEM; > + > + chip->dev =3D dev; > + spin_lock_init(&chip->lock); > + > + chip->regmap =3D devm_regmap_init_i2c(client, &smb1351_regmap_config); > + if (IS_ERR(chip->regmap)) > + return dev_err_probe(dev, PTR_ERR(chip->regmap), > + "failed to initialize regmap\n"); > + > + /* > + * external_power_changed() can run as soon as the power supply is > + * registered, before devm_power_supply_register() returns. Keep the > + * work disabled until the rest of probe is done. > + */ > + INIT_DELAYED_WORK(&chip->work, smb1351_work); > + disable_delayed_work(&chip->work); > + > + psy_cfg.drv_data =3D chip; > + psy_cfg.fwnode =3D dev_fwnode(dev); > + > + chip->psy =3D devm_power_supply_register(dev, &smb1351_psy_desc, > + &psy_cfg); > + if (IS_ERR(chip->psy)) > + return dev_err_probe(dev, PTR_ERR(chip->psy), > + "failed to register power supply\n"); > + > + /* > + * Added after the power supply, so this runs before the power supply > + * is unregistered. Unlike devm_delayed_work_autocancel(), disabling > + * the work also turns later requeues into no-ops. > + */ > + ret =3D devm_add_action_or_reset(dev, smb1351_disable_work, chip); > + if (ret) > + return ret; everything from here: > + ret =3D power_supply_get_battery_info(chip->psy, &info); > + if (ret) > + return dev_err_probe(dev, ret, "failed to get battery info\n"); > + > + if (power_supply_battery_info_has_prop(info, > + POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX)) > + vfloat_uv =3D info->constant_charge_voltage_max_uv; > + else if (power_supply_battery_info_has_prop(info, > + POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN)) > + vfloat_uv =3D info->voltage_max_design_uv; > + else > + return dev_err_probe(dev, -EINVAL, > + "battery has no maximum voltage\n"); > + > + if (vfloat_uv < SMB1351_VFLOAT_MIN_UV || > + vfloat_uv > SMB1351_VFLOAT_MAX_UV) > + return dev_err_probe(dev, -EINVAL, > + "unsupported float voltage %d uV\n", > + vfloat_uv); > + > + chip->vfloat_code =3D (vfloat_uv - SMB1351_VFLOAT_MIN_UV) / > + SMB1351_VFLOAT_STEP_UV; > + > + /* 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)); to here, should probably go into a helper function called by smb1351_psy_desc.init, so that it is configured properly when the power-supply device becomes available. At this point it should be fine to have the delayed work enabled the whole time during probe. Greetings, -- Sebastian > + enable_delayed_work(&chip->work); > + mod_delayed_work(system_freezable_wq, &chip->work, 0); > + > + return 0; > +} > + > +static const struct of_device_id smb1351_of_match[] =3D { > + { .compatible =3D "qcom,smb1351" }, > + { } > +}; > +MODULE_DEVICE_TABLE(of, smb1351_of_match); > + > +static const struct i2c_device_id smb1351_i2c_id[] =3D { > + { .name =3D "smb1351" }, > + { } > +}; > +MODULE_DEVICE_TABLE(i2c, smb1351_i2c_id); > + > +static struct i2c_driver smb1351_driver =3D { > + .driver =3D { > + .name =3D "qcom-smb1351", > + .of_match_table =3D smb1351_of_match, > + }, > + .probe =3D smb1351_probe, > + .id_table =3D smb1351_i2c_id, > +}; > +module_i2c_driver(smb1351_driver); > + > +MODULE_AUTHOR("Adrian Nguyen "); > +MODULE_DESCRIPTION("Qualcomm SMB1351 charger driver"); > +MODULE_LICENSE("GPL"); > --=20 > 2.43.0 --fqgxf4stu2odqyiq Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEE72YNB0Y/i3JqeVQT2O7X88g7+poFAmsPdpEACgkQ2O7X88g7 +pqBbQ/+KgJ92lhGEm18aHyRngW9KI/qD9aYtDNeiLQTL7xkty6pir0X/9d5fAKA amE4UkoTvQjy9vw6UCCy7yo2xsYMdBPTW1WKqh3kEQ8ibu8mRvA60xerTY5XFe8b Atrcq35Y0K8jTuHJvNrCP+vCK1j23wP41A/evKI2ynhuaqsFqh9hscOKAlWXfVMt gp33Vtcps4ACU+wi1o6W1mVayY/tgjjZ4JRp+wH8q5loKzMw2AXoEqXXDFc7WR5k mL0/Rucz+muRrsRbPPpvg1FqMLD86N9IBNP1VvfkWPfeWZqwTn796EGODXsWq0Rg 9ihHWCVJJkinQuTnXmp2AvYXgmwVMD6IoujrUeN1czsdYb9unANc/hWp5v5QJD9B MLQuALuQRnQoIlh7T2nkRCQE4TzHy8y/a+GE3Nz7jIOIGNWl2Fhtp3if5IdxSpnF EVMCNSW50LghcfQ63AuSg5stBmmTqeo6qhoZM01oY27297RwdolasC6SOif+omla 2Y46dxQRL+WNiOZPRUx7qRl894DMh0ljkF52T1aRb2QwpcsxOZZHomGnlgtJASi9 xPdjV1J+iNj9qgXVxDqSpfdTl9LWGqSLlcc/2fI41T5tyGX1cdrh01yEcDX0GqrU +E92I8eqMwaLnWUmBhyl2ZzPnhr+dk4RnSYQyuO3f9j4LD9ArjI= =3Tz7 -----END PGP SIGNATURE----- --fqgxf4stu2odqyiq--