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.182.11 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796507869; b=imrZZ044MTb5Gxu1+S2yg7XJvXXxXXouK0rCbW6+oxuyUp4FtgO7yb+yWSe2q1hh8nAp Phucf9PpTj/Eh+HIahaiVY3UccK3rhOtiv/oSTUWnVHsU/uuvLZjo3btyM9x+py3HfLxY 0JlwNMSgJKI00+3kODNHAeTj/gAgNH4wD9hZVp4dMTtViuj5TwN3MDkwcJScgIukBU7js Oczv2AoUw4RDoN+CeMKbgglk7ZdhKtokZiSgBGKq2cDvziX19rA5jHbvZx9LM5U/b3DS6 9fBzogSr06T+YG9FUWYgHUutZG/CIh/jTfVyh11mcAk1osoRHTqfdCS9BY2HYfHzy5g== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796507869; 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=Ip+nKb1QRCEjZdoS9xZFSzcpENmIXQLkbDZhJ2zIX/w=; b=FB6u+kuVeh9ryjZsDpU/gLbXGtdZ0heoM+di7U3sm4ha7zWezlOfyqAfzUkhczKcUyBJ rVlrzsKBBpQe5lP22IpUTwJ6awX7inX3QzNu+ATHS0OtdNw8Q6BQIzfVojnTCeKx2C7Z7 gS33qR496sto88rCLmDzX/b08s3jdFeRcYKZ7A5PakAVyHPk/ACuhDTlfTnY9UvkG8v3N t9Xk9tqDT/By68UTO8WSJmjAquWHbt8w3NkDpfl69Gl0MQZixKShLYHoLu6R3zJ/ZbxTM TyenvD7WRdsPonMkAaxIvafMLdzd7UPJw0DkYhLPfRONDGU6l8OnsnS8/mFXfO7tr/Q== 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=UwLpGijQ; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.182.11 Received: from sender5-op-o11.zoho.com (sender5-op-o11.zoho.com [165.173.182.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 94A48606A31F; Sat, 5 Dec 2026 21:57:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 94A48606A31F 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) header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=zohomail header.b=UwLpGijQ ARC-Seal: i=1; a=rsa-sha256; t=1796507839; cv=none; d=zohomail.com; s=zohoarc; b=hvo0GqO6Y+xlHOlcmzJYFlfRjNNbdEHKiCGTaGSgR1yI4oCMY8GTfEd5TpqwGs/VrPfgKKPKllXOEiwKvq4u3Vka+P8vrjrjDhY1ZRl9K89pgAHVpe72n7hOyPRr8ipwJoUZOjeV/sr2P/xWKm/gT/59YP6qdk70sqx5lJO4WDg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1796507839; 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=Ip+nKb1QRCEjZdoS9xZFSzcpENmIXQLkbDZhJ2zIX/w=; b=ShJK9OOzpxS9MnvGMvlMC4It+AdVtuqtmD+WwsNqAx23IhGA5KYNDbD0EMsU0nAUpJrA+nFgeDL2wJ5H7Bmy2fGK5UpPiCI1FHAkh2gYusXSXScaLRYnHkavrLJiVz9Zth6D74EhZy7u5gKAOTYxH4Tk+Zxs1n2wIj2sRnjgOeU= 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=UwLpGijQulNRK3jenyFOXHi7eTL/f6m9gkDB2aNkHf4YBLfYgzHgp3S9NQOmpjHrP4IvovW97JtGFtPdtraf07inxXaoAl5efmjrQRXrXSuUaBLVQQp0i2YBY71dB2r+K6ScOjiZCHvqqaBf0Ig+prVMghmfjjF7Y1RYD8o4tzc=; c=relaxed/relaxed; s=zohomail; d=collabora.com; v=1; bh=Ip+nKb1QRCEjZdoS9xZFSzcpENmIXQLkbDZhJ2zIX/w=; 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 1796507837962518.1442488149846; Sat, 5 Dec 2026 13:57:17 -0800 (PST) Received: by venus (Postfix, from userid 1000) id C78E918205E; Sat, 05 Dec 2026 22:57:13 +0100 (CET) Date: Sat, 5 Dec 2026 22:57:13 +0100 From: Sebastian Reichel To: Chris Morgan Cc: linux-rockchip@lists.infradead.org, devicetree@vger.kernel.org, shawn.lin@rock-chips.com, linux-pm@vger.kernel.org, heiko@sntech.de, conor+dt@kernel.org, krzk+dt@kernel.org, robh@kernel.org, t.schramm@manjaro.org, xsf@rock-chips.com, Chris Morgan Subject: Re: [PATCH V5 2/3] power: supply: cw2217: Add support for Cellwise cw2217 Message-ID: References: <20261127192330.610452-1-macroalpha82@gmail.com> <20261127192330.610452-3-macroalpha82@gmail.com> X-Mailing-List: devicetree@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="z2x7shjdeklfd6pg" Content-Disposition: inline In-Reply-To: <20261127192330.610452-3-macroalpha82@gmail.com> X-Zoho-Virus-Status: 1 X-Zoho-AV-Stamp: zmail-av-0.2.28.1.5.5/296.487.71 X-ZohoMailClient: External --z2x7shjdeklfd6pg Content-Type: text/plain; protected-headers=v1; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Subject: Re: [PATCH V5 2/3] power: supply: cw2217: Add support for Cellwise cw2217 MIME-Version: 1.0 Hi, On Fri, Nov 27, 2026 at 01:23:29PM -0600, Chris Morgan wrote: > From: Chris Morgan >=20 > Add support for the CW2217 i2c battery gauge. The gauge driver was > adapted from the Rockchip BSP driver [1]. >=20 > This patch was adapted from work originally done by Xu Shengfei at > Rockchip . >=20 > [1] https://github.com/rockchip-linux/kernel/blob/develop-6.12/drivers/po= wer/supply/cw221x_battery.c >=20 > Signed-off-by: Chris Morgan > --- Looks mostly good to me. I have some small comments inline. > drivers/power/supply/Kconfig | 11 + > drivers/power/supply/Makefile | 1 + > drivers/power/supply/cw2217_battery.c | 878 ++++++++++++++++++++++++++ > 3 files changed, 890 insertions(+) > create mode 100644 drivers/power/supply/cw2217_battery.c >=20 > diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig > index ee7dc5f2617a..9a3592dc9767 100644 > --- a/drivers/power/supply/Kconfig > +++ b/drivers/power/supply/Kconfig > @@ -141,6 +141,17 @@ config BATTERY_CW2015 > This driver can also be built as a module. If so, the module will be > called cw2015_battery. > =20 > +config BATTERY_CW2217 > + tristate "CW2217 Battery driver" > + depends on I2C > + select REGMAP_I2C > + help > + Say Y here to enable support for the cellwise cw2217 > + battery fuel gauge (includes support for the cw2215 and cw2218). > + > + This driver can also be built as a module. If so, the module will be > + called cw2217_battery. > + > config BATTERY_DS2760 > tristate "DS2760 battery driver (HP iPAQ & others)" > depends on W1 > diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile > index f3c0d1006c1a..66e6303888c7 100644 > --- a/drivers/power/supply/Makefile > +++ b/drivers/power/supply/Makefile > @@ -27,6 +27,7 @@ obj-$(CONFIG_CHARGER_AXP20X) +=3D axp20x_ac_power.o > obj-$(CONFIG_BATTERY_CHAGALL) +=3D chagall-battery.o > obj-$(CONFIG_BATTERY_CPCAP) +=3D cpcap-battery.o > obj-$(CONFIG_BATTERY_CW2015) +=3D cw2015_battery.o > +obj-$(CONFIG_BATTERY_CW2217) +=3D cw2217_battery.o > obj-$(CONFIG_BATTERY_DS2760) +=3D ds2760_battery.o > obj-$(CONFIG_BATTERY_DS2780) +=3D ds2780_battery.o > obj-$(CONFIG_BATTERY_DS2781) +=3D ds2781_battery.o > diff --git a/drivers/power/supply/cw2217_battery.c b/drivers/power/supply= /cw2217_battery.c > new file mode 100644 > index 000000000000..becd3b5a1572 > --- /dev/null > +++ b/drivers/power/supply/cw2217_battery.c > @@ -0,0 +1,878 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Charger driver for cw2217 > + * > + * Copyright (c) 2026 Rockchip Electronics Co., Ltd. > + * I would expect either your copyright here (i.e. enough changes to be relevant) or at least these tags in the commit: Co-developed-by: Xu Shengfei Signed-off-by: Xu Shengfei > + * Author: Xu Shengfei > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include > + > +#define CW2217_VERSION 0x00 > +#define CW2217_CHIP_ID 0xA0 > + > +#define CW2217_VCELL_H 0x02 > +#define CW2217_VCELL_L 0x03 > + > +#define CW2217_SOC_H 0x04 > +#define CW2217_SOC_L 0x05 > + > +#define CW2217_TEMP 0x06 > + > +#define CW2217_CONFIG 0x08 > +#define CW2217_CONFIG_ACT_MSK GENMASK(7, 4) > +#define CW2217_CONFIG_SLEEP (0x03 << 6) > +#define CW2217_CONFIG_RESTART (0x03 << 4) > +#define CW2217_CONFIG_ACTIVE (0x00) > + > +#define CW2217_INT_CONF 0x0A > +#define CW2217_EN_SOC_INT BIT(6) > +#define CW2217_EN_TMX_INT BIT(5) > +#define CW2217_EN_TMN_INT BIT(4) > + > +#define CW2217_SOC_ALERT 0x0B > +#define CW2217_SOC_UPDATE_FLG BIT(7) > +#define CW2217_SOC_ALERT_MSK GENMASK(6, 0) > +#define CW2217_SOC_ALERT_OFF 0x0 > + > +#define CW2217_TEMP_MAX 0x0C > + > +#define CW2217_TEMP_MIN 0x0D > + > +#define CW2217_CURRENT_H 0x0E > +#define CW2217_CURRENT_L 0x0F > + > +#define CW2217_BAT_PROFILE 0x10 > +#define CW2217_PROFILE_SZ 80 > + > +#define CW2217_T_HOST_H 0xA0 > +#define CW2217_T_HOST_L 0xA1 > + > +#define CW2217_USER_CONF 0xA2 > + > +#define CW2217_CYCLECNT_H 0xA4 > +#define CW2217_CYCLECNT_L 0xA5 > + > +#define CW2217_SOH 0xA6 > + > +/* This register not documented in datasheet. */ > +#define CW2217_IC_STATE 0xA7 > +#define CW2217_IC_READY_MASK 0x0C > +#define CW2217_IC_READY 3 > + > +#define CW2217_STB_CUR_H 0xA8 > +#define CW2217_STB_CUR_L 0xA9 > + > +#define CW2217_FW_VERSION 0xAB > +#define CW2217_ID_MASK GENMASK(7, 6) > +#define CW2215_ID 2 > +#define CW2217_ID 1 > +#define CW2218_ID 0 > + > +/* Default values from BSP driver */ > +#define CW2217_USER_RSENSE 1500 > +#define CW2217_DEFAULT_POLL_INTERVAL_MS 5000 > + > +struct cw2217_battery { > + struct device *dev; > + struct regmap *regmap; > + struct power_supply *cw_bat; > + u8 *bat_profile; > + struct workqueue_struct *battery_workqueue; > + struct delayed_work battery_delay_work; > + int user_rsense; > + int chip_id; > + int voltage; > + int soc; > + int temp; > + long cw_current; > + int cycle; > + int soh; > + int fw_version; > + u32 poll_interval_ms; > + bool update_profile; > + bool battery_changed; > +}; > + > +/* > + * The CONFIG register is used for the host MCU to configure the fuel ga= uge IC. > + * The default value is 0xF0, SLEEP and RESTART bits are set. To power u= p the > + * IC, the host MCU needs to write 0x30 to exit shutdown mode, and then = write > + * 0x00 to restart the gauge to enter active mode. To reset the IC, the = host > + * MCU needs to write 0xF0, 0x30 and 0x00 in sequence to this register to > + * complete the restart procedure. The CW2217 will reload relevant param= eters > + * and settings and restart SOC calculation. Note that the SOC may be a > + * different value after reset operation since it is a brand-new calcula= tion > + * based on the latest battery status. The BSP driver slept 20ms after r= estart > + * and 10ms after setting active, though no delays are documented in the > + * datasheet. > + */ > +static int cw2217_active(struct cw2217_battery *cw_bat, bool active) > +{ > + int ret; > + > + if (active) { > + ret =3D regmap_write(cw_bat->regmap, CW2217_CONFIG, > + CW2217_CONFIG_RESTART); > + if (ret) > + return ret; > + > + fsleep(20000); > + > + ret =3D regmap_write(cw_bat->regmap, CW2217_CONFIG, > + CW2217_CONFIG_ACTIVE); > + if (ret) > + return ret; > + } else { > + ret =3D regmap_write(cw_bat->regmap, CW2217_CONFIG, > + CW2217_CONFIG_SLEEP | CW2217_CONFIG_RESTART); > + if (ret) > + return ret; > + } > + > + fsleep(10000); > + > + return 0; > +} > + > +/* > + * The 0x00 register is an UNSIGNED 8bit read-only register. Its value is > + * fixed to 0xA0 in shutdown mode and active mode. > + */ > +static int cw2217_get_chip_id(struct cw2217_battery *cw_bat) > +{ > + u32 reg; > + int ret; > + > + ret =3D regmap_read(cw_bat->regmap, CW2217_VERSION, ®); > + if (ret) > + return ret; > + > + cw_bat->chip_id =3D reg; > + > + return 0; > +} I think this can just be dropped. It's effectively a oneliner. > + > +/* > + * The VCELL registers (0x02 0x03) are an UNSIGNED 14bit read-only regis= ter > + * that updates the battery voltage continuously. Battery voltage is mea= sured > + * between the VCELL pin and VSS pin, which is the ground reference. A 1= 4bit > + * sigma-delta A/D converter is used and the voltage resolution is 312.5= uV > + * (0.3125mV is * 5/16). > + */ > +static int cw2217_get_voltage(struct cw2217_battery *cw_bat) > +{ > + int val =3D cw_bat->voltage; > + __be16 reg; > + int ret; > + > + ret =3D regmap_bulk_read(cw_bat->regmap, CW2217_VCELL_H, > + ®, sizeof(reg)); > + if (ret) > + return ret; > + > + cw_bat->voltage =3D ((be16_to_cpu(reg) * 5) / 16) * 1000; > + > + if (val !=3D cw_bat->voltage) > + cw_bat->battery_changed =3D true; > + > + return 0; > +} > + > +/* > + * The SOC registers (0x04 0x05) are an UNSIGNED 16bit read-only registe= r that > + * indicates the SOC of the battery. The SOC shows in % format, which me= ans > + * how much percent of the battery's total available capacity is remaini= ng in > + * the battery now. The SOC can intrinsically adjust itself to cater to = the > + * change of battery status; including load, temperature, and aging. > + * The high byte(0x04) contains the SOC in 1% unit which can be directly= used > + * if this resolution is good enough for the application. The low byte (= 0x05) > + * provides more accurate fractional part of the SOC and its LSB is (1/2= 56) %. > + * For now, we are only interested in the high byte. > + */ > +static int cw2217_get_capacity(struct cw2217_battery *cw_bat) > +{ > + int val =3D cw_bat->soc; > + u32 reg; > + int ret; > + > + ret =3D regmap_read(cw_bat->regmap, CW2217_SOC_H, ®); > + if (ret) > + return ret; > + > + cw_bat->soc =3D clamp(reg, 0, 100); > + if (val !=3D cw_bat->soc) > + cw_bat->battery_changed =3D true; > + > + return 0; > +} > + > +/* > + * The TEMP register is an UNSIGNED 8bit read only register. > + * It reports the real-time battery temperature > + * measured at TS pin. The scope is from -40 to 87.5 degrees Celsius, > + * LSB is 0.5 degree Celsius. TEMP(C) =3D - 40 + Value(0x06 Reg) / 2 > + */ > +static int cw2217_get_temp(struct cw2217_battery *cw_bat) > +{ > + int val =3D cw_bat->temp; > + u32 reg; > + int ret; > + > + ret =3D regmap_read(cw_bat->regmap, CW2217_TEMP, ®); > + if (ret) > + return ret; > + > + cw_bat->temp =3D (int)reg * 5 - 400; > + > + if (val !=3D cw_bat->temp) > + cw_bat->battery_changed =3D true; > + > + return 0; > +} > + > +/* > + * Current is a signed 16bit register (0x0E 0x0F) that reports current A= /D > + * converter result of the voltage across the current sense resistor, > + * 10mohm typical. The register value should be divided by the sense > + * resistance to convert to amperes. The value of the sense resistor > + * determines the resolution and the full-scale range of the current rea= dings. > + * The LSB of 0x0F is (52.4/32768)uV for CW2215 and CW2217. The LSB of 0= x0F is > + * (125/32768)uV for CW2218. The calculation in the datasheet sets the u= nits > + * of the sense resistor at milliohms (not microohms) so multiply by 1000 > + * to return the current in the correct units. > + */ > +static int cw2217_get_current(struct cw2217_battery *cw_bat) > +{ > + int val =3D cw_bat->cw_current; > + s32 cw_current; > + __be16 reg; > + u8 fw_ver; > + int ret; > + > + if (cw_bat->fw_version =3D=3D 0) { > + cw_bat->cw_current =3D 0; > + dev_err(cw_bat->dev, "Error! CW2217 firmware read error!\n"); > + return -EIO; > + } > + > + ret =3D regmap_bulk_read(cw_bat->regmap, CW2217_CURRENT_H, > + ®, sizeof(reg)); > + if (ret) > + return ret; > + > + cw_current =3D (s16)be16_to_cpu(reg); > + > + fw_ver =3D FIELD_GET(CW2217_ID_MASK, cw_bat->fw_version); > + switch (fw_ver) { > + case CW2215_ID: > + case CW2217_ID: > + cw_bat->cw_current =3D div_s64((s64)cw_current * 1600 * 1000, cw_bat->= user_rsense); > + break; > + case CW2218_ID: > + cw_bat->cw_current =3D div_s64((s64)cw_current * 3815 * 1000, cw_bat->= user_rsense); > + break; > + default: > + cw_bat->cw_current =3D 0; > + dev_err(cw_bat->dev, "Error! CW2217 firmware read error!\n"); > + return -EIO; > + } > + > + if (val !=3D cw_bat->cw_current) > + cw_bat->battery_changed =3D true; > + > + return 0; > +} > + > +/* > + * CYCLECNT is an UNSIGNED 16bit register (0xA4 0xA5) that counts cycle = life > + * of the battery. The LSB of 0xA5 stands for 1/16 cycle. This register = will > + * be cleared after entering shutdown mode. > + */ > +static int cw2217_get_cycle_count(struct cw2217_battery *cw_bat) > +{ > + int val =3D cw_bat->cycle; > + __be16 reg; > + int ret; > + > + ret =3D regmap_bulk_read(cw_bat->regmap, CW2217_CYCLECNT_H, > + ®, sizeof(reg)); > + if (ret) > + return ret; > + > + cw_bat->cycle =3D be16_to_cpu(reg) / 16; > + > + if (val !=3D cw_bat->cycle) > + cw_bat->battery_changed =3D true; > + > + return 0; > +} > + > +/* > + * SOH (State of Health) is an UNSIGNED 8bit register(0xA6) that represe= nts the level of > + * battery aging by tracking battery internal impedance increment. When = the device enters > + * active mode, this register refresh to 0x64 by default. Its range is 0= x00 to 0x64, > + * indicating 0 to 100%. This register will be clear after enters shutdo= wn mode. > + */ > +static int cw2217_get_soh(struct cw2217_battery *cw_bat) > +{ > + int val =3D cw_bat->soh; > + u32 reg; > + int ret; > + > + ret =3D regmap_read(cw_bat->regmap, CW2217_SOH, ®); > + if (ret) > + return ret; > + > + cw_bat->soh =3D clamp(reg, 0, 100); > + > + if (val !=3D cw_bat->soh) > + cw_bat->battery_changed =3D true; > + > + return 0; > +} > + > +/* > + * FW_VERSION register reports the firmware (FW) running in the chip. It= is > + * fixed to 0x00 when the chip is in shutdown mode. When in active mode, > + * Bit [7:6] =3D '01' stand for the CW2217, Bit [7:6] =3D '00' stand for= the > + * CW2218 and Bit [7:6] =3D '10' stand for CW2215. Bit[5:0] stand for th= e FW > + * version running in the chip. Note that the FW version is subject to u= pdate. > + */ > +static int cw2217_get_fw_version(struct cw2217_battery *cw_bat) > +{ > + u32 reg; > + int ret; > + > + ret =3D regmap_read(cw_bat->regmap, CW2217_FW_VERSION, ®); > + if (ret) > + return ret; > + > + cw_bat->fw_version =3D reg; > + > + return 0; > +} > + > +static int cw2217_update_data(struct cw2217_battery *cw_bat) > +{ > + int ret; > + > + ret =3D cw2217_get_voltage(cw_bat); > + if (ret) > + return ret; > + > + ret =3D cw2217_get_capacity(cw_bat); > + if (ret) > + return ret; > + > + ret =3D cw2217_get_temp(cw_bat); > + if (ret) > + return ret; > + > + ret =3D cw2217_get_current(cw_bat); > + if (ret) > + return ret; > + > + ret =3D cw2217_get_cycle_count(cw_bat); > + if (ret) > + return ret; > + > + return cw2217_get_soh(cw_bat); > +} > + > +static int cw2217_parse_properties(struct cw2217_battery *cw_bat) > +{ > + struct device *dev =3D cw_bat->dev; > + int length; > + int ret; > + > + ret =3D device_property_read_u32(dev, "cellwise,monitor-interval-ms", > + &cw_bat->poll_interval_ms); > + if (ret) { > + dev_dbg(cw_bat->dev, "Using default poll interval\n"); > + cw_bat->poll_interval_ms =3D CW2217_DEFAULT_POLL_INTERVAL_MS; > + } > + > + /* > + * The hardware reports a minimum resolution of 10ms for certain values, > + * but the previously used minimum value of 250ms seems like a sane > + * minimum value to allow. > + */ > + if (cw_bat->poll_interval_ms < 250) { > + dev_warn(cw_bat->dev, "Monitor interval too low, setting to 250ms\n"); > + cw_bat->poll_interval_ms =3D 250; > + } > + > + /* > + * Datasheet only lists options of between 1M and 50M for resistor M =3D Mega. A shunt resistor is in the milliohm range (so small m). > + * values, so assuming that is all and clamping if out of range. > + */ > + ret =3D device_property_read_u32(dev, > + "shunt-resistor-micro-ohms", > + &cw_bat->user_rsense); > + if (ret) { > + cw_bat->user_rsense =3D CW2217_USER_RSENSE; > + dev_warn(cw_bat->dev, > + "Using default resistor value, reported mA may be wrong\n"); > + } > + > + /* Clamp, but also warn we're clamping due to out of range. */ > + if ((cw_bat->user_rsense < 1000) || (cw_bat->user_rsense > 50000)) { > + cw_bat->user_rsense =3D clamp(cw_bat->user_rsense, 1000, 50000); > + dev_warn(cw_bat->dev, "Shunt resistor value out of range, setting to %= d", > + cw_bat->user_rsense); > + } > + > + length =3D device_property_count_u8(dev, "cellwise,battery-profile"); > + if (length < 0) { > + dev_warn(cw_bat->dev, > + "No battery-profile found, using current flash contents\n"); > + } else if (length !=3D CW2217_PROFILE_SZ) { > + dev_err(cw_bat->dev, "battery-profile must be %d bytes\n", > + CW2217_PROFILE_SZ); > + return -EINVAL; > + } else { > + cw_bat->bat_profile =3D devm_kzalloc(dev, length, GFP_KERNEL); > + if (!cw_bat->bat_profile) > + return -ENOMEM; > + > + ret =3D device_property_read_u8_array(dev, > + "cellwise,battery-profile", > + cw_bat->bat_profile, > + length); > + if (ret) > + return ret; > + } > + > + return 0; > +} > + > +/* Load a new battery profile provided from the device tree. */ > +static int cw2217_update_config(struct cw2217_battery *cw_bat) > +{ > + u32 reg; > + int ret; > + > + /* Set the battery monitor state to shutdown. */ > + ret =3D cw2217_active(cw_bat, false); > + if (ret) > + return ret; > + > + /* > + * Update new battery info if we have a profile, otherwise continue > + * like we updated the profile (without actually updating it). > + */ > + if (cw_bat->bat_profile) { > + ret =3D regmap_raw_write(cw_bat->regmap, CW2217_BAT_PROFILE, > + cw_bat->bat_profile, CW2217_PROFILE_SZ); > + if (ret) > + return ret; > + } > + > + /* > + * Set update flag for new profile (even if we didn't update it) and > + * disable SOC alert. > + */ > + ret =3D regmap_write(cw_bat->regmap, CW2217_SOC_ALERT, > + CW2217_SOC_UPDATE_FLG | CW2217_SOC_ALERT_OFF); > + if (ret) > + return ret; > + > + /* mask all interrupts */ > + ret =3D regmap_clear_bits(cw_bat->regmap, CW2217_INT_CONF, > + CW2217_EN_SOC_INT | CW2217_EN_TMX_INT | CW2217_EN_TMN_INT); > + if (ret) > + return ret; > + > + ret =3D cw2217_active(cw_bat, true); > + if (ret) > + return ret; > + > + /* > + * Check the undocumented register for bits 2 and 3 to be 1. Note that > + * the BSP driver would sleep 100ms after every iteration and retry up > + * to 50 times, this felt excessive so reducing to 10. > + */ > + ret =3D regmap_read_poll_timeout(cw_bat->regmap, CW2217_IC_STATE, reg, > + FIELD_GET(CW2217_IC_READY_MASK, reg) =3D=3D CW2217_IC_READY, > + 100 * USEC_PER_MSEC, 10 * USEC_PER_SEC); > + if (ret) { > + dev_err(cw_bat->dev, > + "Gauge did not become ready after profile upload\n"); > + cw2217_active(cw_bat, false); > + return ret; > + } > + > + return 0; > +} > + > +/* Determine whether the battery profile needs to be updated. */ > +static int cw2217_check_profile(struct cw2217_battery *cw_bat) > +{ > + u8 bat_info[CW2217_PROFILE_SZ]; > + int ret; > + u32 reg; > + > + cw_bat->update_profile =3D false; > + > + ret =3D regmap_read(cw_bat->regmap, CW2217_CONFIG, ®); > + if (ret) > + return ret; > + > + /* Active is 0x00, anything else is inactive */ > + if (FIELD_GET(CW2217_CONFIG_ACT_MSK, reg)) { > + cw_bat->update_profile =3D true; > + return 0; > + } > + > + ret =3D regmap_read(cw_bat->regmap, CW2217_SOC_ALERT, ®); > + if (ret) > + return ret; > + > + if (FIELD_GET(CW2217_SOC_UPDATE_FLG, reg) =3D=3D 0) { > + cw_bat->update_profile =3D true; > + return 0; > + } > + > + /* We're using the existing profile, so checking here isn't needed. */ > + if (!cw_bat->bat_profile) > + return 0; > + > + ret =3D regmap_raw_read(cw_bat->regmap, CW2217_BAT_PROFILE, bat_info, C= W2217_PROFILE_SZ); > + if (ret) { > + dev_err(cw_bat->dev, "Failed to read existing battery profile\n"); > + return ret; > + } > + > + if (memcmp(bat_info, cw_bat->bat_profile, CW2217_PROFILE_SZ)) { > + dev_warn(cw_bat->dev, "Replacing stored battery profile\n"); > + cw_bat->update_profile =3D true; > + } > + > + return 0; > +} > + > +static int cw2217_init(struct cw2217_battery *cw_bat) > +{ > + int ret; > + > + ret =3D cw2217_get_chip_id(cw_bat); ret =3D regmap_read(cw_bat->regmap, CW2217_VERSION, &cw_bat->chip_id); > + if (ret) > + return dev_err_probe(cw_bat->dev, ret, "I2C read write error"); > + > + /* > + * All of the known Cellwise 221x chips set the ID bits to 0xa0. If it > + * doesn't match, we're trying to probe the wrong hardware. > + */ > + if (cw_bat->chip_id !=3D CW2217_CHIP_ID) > + return dev_err_probe(cw_bat->dev, -ENODEV, "Not a cw221x\n"); > + > + ret =3D cw2217_check_profile(cw_bat); > + if (ret) { > + dev_err(cw_bat->dev, "i2c read write error"); > + return ret; > + } > + > + if (cw_bat->update_profile) { > + ret =3D cw2217_update_config(cw_bat); > + if (ret) > + return ret; > + } > + > + return 0; > +} > + > +static void cw2217_bat_work(struct work_struct *work) > +{ > + struct delayed_work *delay_work; > + struct cw2217_battery *cw_bat; > + int ret; > + > + delay_work =3D container_of(work, > + struct delayed_work, > + work); > + > + cw_bat =3D container_of(delay_work, > + struct cw2217_battery, > + battery_delay_work); > + > + ret =3D cw2217_update_data(cw_bat); > + if (ret) > + dev_err(cw_bat->dev, "i2c read error when update data"); > + > + /* > + * Note that the very first run of this will always have a > + * battery_changed =3D true because the initial values will be compared > + * against zero, that's not a big deal for us. > + */ > + if (cw_bat->battery_changed) { > + power_supply_changed(cw_bat->cw_bat); > + cw_bat->battery_changed =3D false; > + } > + > + queue_delayed_work(cw_bat->battery_workqueue, > + &cw_bat->battery_delay_work, > + msecs_to_jiffies(cw_bat->poll_interval_ms)); > +} > + > +static int cw2217_battery_get_property(struct power_supply *psy, > + enum power_supply_property psp, > + union power_supply_propval *val) > +{ > + struct cw2217_battery *cw_bat =3D power_supply_get_drvdata(psy); > + int ret =3D 0; > + > + switch (psp) { > + case POWER_SUPPLY_PROP_CYCLE_COUNT: > + val->intval =3D cw_bat->cycle; > + break; > + case POWER_SUPPLY_PROP_CAPACITY: > + val->intval =3D cw_bat->soc; > + break; > + case POWER_SUPPLY_PROP_STATUS: > + /* > + * Observed when battery was full that it would report > + * approximately -12mv at between 97-99 SoC. Assume that if the > + * battery current is above 0 it's charging, it's below > + * 0 but with an SoC 95% or above it's not charging because > + * it's full, otherwise it's simply not charging. If it's not > + * supplied it is discharging. > + */ > + if (power_supply_is_system_supplied()) { > + if (cw_bat->cw_current > 0) > + val->intval =3D POWER_SUPPLY_STATUS_CHARGING; > + else if (cw_bat->soc >=3D 95) > + val->intval =3D POWER_SUPPLY_STATUS_FULL; > + else > + val->intval =3D POWER_SUPPLY_STATUS_NOT_CHARGING; > + } else > + val->intval =3D POWER_SUPPLY_STATUS_DISCHARGING; > + break; > + case POWER_SUPPLY_PROP_PRESENT: > + val->intval =3D (cw_bat->voltage <=3D 0) ? 0 : 1; > + break; > + case POWER_SUPPLY_PROP_VOLTAGE_NOW: > + val->intval =3D cw_bat->voltage; > + break; > + case POWER_SUPPLY_PROP_CURRENT_NOW: > + val->intval =3D cw_bat->cw_current; > + break; > + case POWER_SUPPLY_PROP_STATE_OF_HEALTH: > + val->intval =3D cw_bat->soh; > + break; > + case POWER_SUPPLY_PROP_TEMP: > + val->intval =3D cw_bat->temp; > + break; > + default: > + ret =3D -EINVAL; > + break; > + } > + > + return ret; > +} > + > +static enum power_supply_property cw2217_battery_properties[] =3D { > + POWER_SUPPLY_PROP_CYCLE_COUNT, > + POWER_SUPPLY_PROP_CAPACITY, > + POWER_SUPPLY_PROP_PRESENT, > + POWER_SUPPLY_PROP_VOLTAGE_NOW, > + POWER_SUPPLY_PROP_CURRENT_NOW, > + POWER_SUPPLY_PROP_STATUS, > + POWER_SUPPLY_PROP_STATE_OF_HEALTH, > + POWER_SUPPLY_PROP_TEMP, > +}; > + > +static const struct power_supply_desc cw2217_bat_desc =3D { > + .name =3D "cw2217-battery", > + .type =3D POWER_SUPPLY_TYPE_BATTERY, > + .properties =3D cw2217_battery_properties, > + .num_properties =3D ARRAY_SIZE(cw2217_battery_properties), > + .get_property =3D cw2217_battery_get_property, > +}; > + > +static const struct regmap_range cw2217_ranges_rd_yes[] =3D { > + regmap_reg_range(CW2217_VERSION, CW2217_VERSION), > + regmap_reg_range(CW2217_VCELL_H, CW2217_TEMP), > + regmap_reg_range(CW2217_CONFIG, CW2217_CONFIG), > + regmap_reg_range(CW2217_INT_CONF, CW2217_CURRENT_L), > + regmap_reg_range(CW2217_BAT_PROFILE, > + CW2217_BAT_PROFILE + CW2217_PROFILE_SZ - 1), > + regmap_reg_range(CW2217_T_HOST_H, CW2217_USER_CONF), > + regmap_reg_range(CW2217_CYCLECNT_H, CW2217_STB_CUR_L), > + regmap_reg_range(CW2217_FW_VERSION, CW2217_FW_VERSION), > +}; > + > +static const struct regmap_access_table cw2217_rd_table =3D { > + .yes_ranges =3D cw2217_ranges_rd_yes, > + .n_yes_ranges =3D ARRAY_SIZE(cw2217_ranges_rd_yes), > +}; > + > +static const struct regmap_range cw2217_ranges_wr_yes[] =3D { > + regmap_reg_range(CW2217_CONFIG, CW2217_CONFIG), > + regmap_reg_range(CW2217_INT_CONF, CW2217_TEMP_MIN), > + regmap_reg_range(CW2217_BAT_PROFILE, > + CW2217_BAT_PROFILE + CW2217_PROFILE_SZ - 1), > + regmap_reg_range(CW2217_T_HOST_H, CW2217_USER_CONF), > +}; > + > +static const struct regmap_access_table cw2217_wr_table =3D { > + .yes_ranges =3D cw2217_ranges_wr_yes, > + .n_yes_ranges =3D ARRAY_SIZE(cw2217_ranges_wr_yes), > +}; > + > +static const struct regmap_range cw2217_ranges_vol_yes[] =3D { > + regmap_reg_range(CW2217_VCELL_H, CW2217_TEMP), > + regmap_reg_range(CW2217_INT_CONF, CW2217_SOC_ALERT), > + regmap_reg_range(CW2217_CURRENT_H, CW2217_CURRENT_L), > + regmap_reg_range(CW2217_CYCLECNT_H, CW2217_STB_CUR_L), > + regmap_reg_range(CW2217_FW_VERSION, CW2217_FW_VERSION), > +}; > + > +static const struct regmap_access_table cw2217_vol_table =3D { > + .yes_ranges =3D cw2217_ranges_vol_yes, > + .n_yes_ranges =3D ARRAY_SIZE(cw2217_ranges_vol_yes), > +}; > + > +static const struct regmap_config cw2217_regmap_config =3D { > + .reg_bits =3D 8, > + .val_bits =3D 8, > + .rd_table =3D &cw2217_rd_table, > + .wr_table =3D &cw2217_wr_table, > + .volatile_table =3D &cw2217_vol_table, > + .cache_type =3D REGCACHE_MAPLE, > + .max_register =3D CW2217_FW_VERSION, > +}; > + > +static int cw2217_probe(struct i2c_client *client) > +{ > + struct power_supply_config psy_cfg =3D { 0 }; > + struct cw2217_battery *cw_bat; > + int ret; This function could use a helper variable: struct device *dev =3D &client->dev; > + cw_bat =3D devm_kzalloc(&client->dev, sizeof(*cw_bat), GFP_KERNEL); > + if (!cw_bat) > + return -ENOMEM; > + > + i2c_set_clientdata(client, cw_bat); This is fine, but since there will be a v6 anyways and there is the new dev helper variable - make this: dev_set_drvdata(dev, cw_bat); > + cw_bat->dev =3D &client->dev; > + > + cw_bat->regmap =3D devm_regmap_init_i2c(client, &cw2217_regmap_config); > + if (IS_ERR(cw_bat->regmap)) > + return dev_err_probe(cw_bat->dev, PTR_ERR(cw_bat->regmap), > + "Failed to allocate regmap: %ld\n", > + PTR_ERR(cw_bat->regmap)); > + > + ret =3D cw2217_parse_properties(cw_bat); > + if (ret) > + return ret; > + > + ret =3D cw2217_init(cw_bat); > + if (ret) > + return dev_err_probe(cw_bat->dev, ret, "cw2217 init fail\n"); > + > + ret =3D cw2217_get_fw_version(cw_bat); > + if (ret) > + return dev_err_probe(cw_bat->dev, ret, > + "Unable to read fw version\n"); > + > + ret =3D cw2217_update_data(cw_bat); > + if (ret) > + return dev_err_probe(cw_bat->dev, ret, > + "Unable to read initial data\n"); > + > + psy_cfg.fwnode =3D dev_fwnode(&client->dev); > + psy_cfg.drv_data =3D cw_bat; > + cw_bat->cw_bat =3D devm_power_supply_register(&client->dev, > + &cw2217_bat_desc, &psy_cfg); > + if (IS_ERR(cw_bat->cw_bat)) > + return dev_err_probe(cw_bat->dev, PTR_ERR(cw_bat->cw_bat), > + "Failed to register battery: %ld\n", > + PTR_ERR(cw_bat->cw_bat)); > + > + cw_bat->battery_workqueue =3D devm_alloc_ordered_workqueue(&client->dev, > + "cw2217_gauge", 0); > + if (!cw_bat->battery_workqueue) > + return -ENOMEM; > + > + ret =3D devm_delayed_work_autocancel(&client->dev, > + &cw_bat->battery_delay_work, > + cw2217_bat_work); > + if (ret) > + return dev_err_probe(&client->dev, ret, > + "Failed to register delayed work\n"); > + > + queue_delayed_work(cw_bat->battery_workqueue, > + &cw_bat->battery_delay_work, > + msecs_to_jiffies(50)); > + > + return 0; > +} > + > +static int __maybe_unused cw2217_bat_suspend(struct device *dev) > +{ > + struct i2c_client *client =3D to_i2c_client(dev); > + struct cw2217_battery *cw_bat =3D i2c_get_clientdata(client); struct cw2217_battery *cw_bat =3D dev_get_drvdata(dev); > + cancel_delayed_work_sync(&cw_bat->battery_delay_work); > + return 0; > +} > + > +static int __maybe_unused cw2217_bat_resume(struct device *dev) > +{ > + struct i2c_client *client =3D to_i2c_client(dev); > + struct cw2217_battery *cw_bat =3D i2c_get_clientdata(client); struct cw2217_battery *cw_bat =3D dev_get_drvdata(dev); > + > + queue_delayed_work(cw_bat->battery_workqueue, > + &cw_bat->battery_delay_work, > + msecs_to_jiffies(20)); > + return 0; > +} > + > +static SIMPLE_DEV_PM_OPS(cw2217_bat_pm_ops, cw2217_bat_suspend, > + cw2217_bat_resume); > + > +static const struct i2c_device_id cw2217_id_table[] =3D { > + { .name =3D "cw2215" }, > + { .name =3D "cw2217" }, > + { .name =3D "cw2218" }, > + { } > +}; > +MODULE_DEVICE_TABLE(i2c, cw2217_id_table); > + > +static const struct of_device_id cw2217_match_table[] =3D { > + { .compatible =3D "cellwise,cw2215", }, > + { .compatible =3D "cellwise,cw2217", }, > + { .compatible =3D "cellwise,cw2218", }, > + { }, > +}; > +MODULE_DEVICE_TABLE(of, cw2217_match_table); > + > +static struct i2c_driver cw2217_driver =3D { > + .driver =3D { > + .name =3D "cw2217", > + .pm =3D &cw2217_bat_pm_ops, > + .of_match_table =3D cw2217_match_table, > + }, > + .probe =3D cw2217_probe, > + .id_table =3D cw2217_id_table, > +}; > + > +module_i2c_driver(cw2217_driver); > + > +MODULE_AUTHOR("Xu Shengfei "); > +MODULE_AUTHOR("Chris Morgan "); > +MODULE_DESCRIPTION("cw2217 battery driver"); > +MODULE_LICENSE("GPL"); Greetings, -- Sebastian --z2x7shjdeklfd6pg Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEE72YNB0Y/i3JqeVQT2O7X88g7+poFAmsUiLYACgkQ2O7X88g7 +pqmOg//TG9jIqisZeWRAPGvzsM4W1JVXs9MvWNwlSzD4EAwmjF2T6DAs3wX+0LZ 0xYyQybNphI904FOZ1QDVemYxi1r/BVf5GmE8VE5mN+1YHJCfBBGBOZKeRtsTOrp wXD/mEzszg6uyyehnE/vXJ/LYRCKmVgFxzaMpsK33wjXq1jUb12vKakhkQlVBrBF RjcDlnC95pJDh5D9DfGl01EMs+lZd526ARSAE8D37cfMIwNBAcRkkkQU8PFGpiGf Q0zR27xqNxLCEvxiT8ziv8XUXEyHUm7JxZc4318blw3dtADd6ED8RpF9254osozY WpDl9RUXNGj3Yo9JwW0aQ7nSnPgzi5sxfXRpphoQ1oAus2cwULB8X6U5mp1UYDc3 70cURjnJZfeMvT1e1hVDwLbzMczS4iVsboMlgRBKSA6d0AZnMX3cDzYbCR7HcDWe hWwXVw6jv6Dl055jcBuewTdBNoh9mPc7BwFo46oqoUyUcigK2d8QZESHfmAlKiBB ai0qa8w54FPVXnPMKXgfU9Hol6dsJbOvamzdbk015gf0Qa/mZUQ97kJ4U3x8CD3s dm0tsCHW0P2puWl9zd0ngZo2iCJ1DP9lRDGgaF799qeztAPnq8p8qzbpBTJxVOZn On+nOx5114q4fsHRkOnRBEIWhNSafC8q6wPX+dg5bRJvUdyyGAM= =JcpZ -----END PGP SIGNATURE----- --z2x7shjdeklfd6pg--