Skip to contentSkip to conversation

Add Cellwise CW2217

PATCH Superseded Chris Morgan 2 participants 5 messages Nov 23

Subsystems (4)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Add Cellwise CW2217

From: Chris Morgan <[email protected]>

Add the Cellwise CW2217 battery gauge driver. This driver is based on the sources used for Rockchip's Board Support package [1] but has been updated to use the regmap API and devm helpers.

[1] https://github.com/rockchip-linux/kernel/blob/develop-6.12/drivers/power/supply/cw221x_battery.c

Changes Since V2:
- Fixed more bugs pointed out by Sashiko.
- Corrected an integer overflow condition when measuring current.
- Corrected logic for when we don't have a user specified battery profile.
- Added power supply changed logic in the worker.
- Updated the Kconfig text to note support for the cw2215 and cw2218.
- Added a check to ensure polling interval cannot be below 250ms.

Changes Since V1:
- Added a default value for the shunt resistor in the binding documentation, and updated the commit description to note that the min and max for the battery profile is now per-compatible string.
- Fixed bugs pointed out by Sashiko bot, and corrected units for temp and shunt resistor values.
- Corrected include pointed out by kernel test robot.
- Noted in the commit message the patch is based on original work by Xu Shengfei at Rockchip. I did not list him as co-developed or signed-off since I did not have his permission (yet) to do so, but I have his authorship included in the file itself.

Chris Morgan (3):
dt-bindings: power: cw2015: add cw2215, cw2217, and cw2218
power: supply: cw2217: Add support for Cellwise cw2217
arm64: dts: rockchip: Add Battery Gauge to Anbernic Vita-Pro

 .../bindings/power/supply/cw2015_battery.yaml |  41 +-
 .../rockchip/rk3576-anbernic-rg-vita-pro.dts  |  21 +-
 drivers/power/supply/Kconfig                  |  11 +
 drivers/power/supply/Makefile                 |   1 +
 drivers/power/supply/cw2217_battery.c         | 878 ++++++++++++++++++
 5 files changed, 945 insertions(+), 7 deletions(-)
 create mode 100644 drivers/power/supply/cw2217_battery.c
Signature · 1 line
2.53.0
#1 lore raw

PATCH v3 1/3 dt-bindings: power: cw2015: add cw2215, cw2217, and cw2218

From: Chris Morgan <[email protected]>

Add bindings for the Cellwise cw2215/cw2217/cw2218 i2C interface battery gauge. It is similar in function to the existing cw2015 but with a different register layout. Each of the cw2215, cw2217 and cw2218 only really differ in the minor specs and the hardware ID they display when queried. shunt-resistor-micro-ohms is a property in use for multiple devices so not appending a vendor prefix to it.

Move the minItems and maxItems for the battery profile to a conditional based on the compatible string, as the new monitors require 80 bytes of firmware instead of 64 bytes.

---
 .../bindings/power/supply/cw2015_battery.yaml | 41 ++++++++++++++++---
 1 file changed, 35 insertions(+), 6 deletions(-)
Documentation/devicetree/bindings/power/supply/cw2015_battery.yaml +35 -6
index f7bde324153d..992cd5f6a216 100644
@@ -14,12 +14,13 @@ description: |
   phandle in monitored-battery. If specified the driver uses the
   charge-full-design-microamp-hours property of the battery.
 
-allOf:
-  - $ref: power-supply.yaml#
-
 properties:
   compatible:
-    const: cellwise,cw2015
+    enum:
+      - cellwise,cw2015
+      - cellwise,cw2215
+      - cellwise,cw2217
+      - cellwise,cw2218
 
   reg:
     maxItems: 1
@@ -31,8 +32,6 @@ properties:
       it is to mail two batteries to a test facility of CellWise and receive
       back a test report with the binary blob.
     $ref: /schemas/types.yaml#/definitions/uint8-array
-    minItems: 64
-    maxItems: 64
 
   cellwise,monitor-interval-ms:
     description:
@@ -43,12 +42,42 @@ properties:
     minItems: 1
     maxItems: 8 # Should be enough
 
+  shunt-resistor-micro-ohms:
+    description:
+      The value, in micro-ohms, of the shunt resistor used for sampling
+      the battery current.
+    minimum: 1000
+    maximum: 50000
+    default: 1500
+
   monitored-battery: true
 
 required:
   - compatible
   - reg
 
+allOf:
+  - $ref: power-supply.yaml#
+  - if:
+      properties:
+        compatible:
+          contains:
+            enum:
+              - cellwise,cw2015
+    then:
+      properties:
+        shunt-resistor-micro-ohms: false
+
+        cellwise,battery-profile:
+          minItems: 64
+          maxItems: 64
+
+    else:
+      properties:
+        cellwise,battery-profile:
+          minItems: 80
+          maxItems: 80
+
 additionalProperties: false
 
 examples:
Signature · 1 line
2.53.0
#2 lore raw

PATCH v3 2/3 power: supply: cw2217: Add support for Cellwise cw2217

In reply to Chris Morgan

From: Chris Morgan <[email protected]>

Add support for the CW2217 i2c battery gauge. The gauge driver was adapted from the Rockchip BSP driver [1].

This patch was adapted from work originally done by Xu Shengfei at Rockchip <[email protected]>.

[1] https://github.com/rockchip-linux/kernel/blob/develop-6.12/drivers/power/supply/cw221x_battery.c

---
 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
drivers/power/supply/Kconfig +11 -0
index ee7dc5f2617a..9a3592dc9767 100644
@@ -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.
 
+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
drivers/power/supply/Makefile +1 -0
index f3c0d1006c1a..66e6303888c7 100644
@@ -27,6 +27,7 @@ obj-$(CONFIG_CHARGER_AXP20X)	+= axp20x_ac_power.o
 obj-$(CONFIG_BATTERY_CHAGALL)	+= chagall-battery.o
 obj-$(CONFIG_BATTERY_CPCAP)	+= cpcap-battery.o
 obj-$(CONFIG_BATTERY_CW2015)	+= cw2015_battery.o
+obj-$(CONFIG_BATTERY_CW2217)	+= cw2217_battery.o
 obj-$(CONFIG_BATTERY_DS2760)	+= ds2760_battery.o
 obj-$(CONFIG_BATTERY_DS2780)	+= ds2780_battery.o
 obj-$(CONFIG_BATTERY_DS2781)	+= ds2781_battery.o
drivers/power/supply/cw2217_battery.c new file +878 -0

878 lines not on this page · raw message

Signature · 1 line
2.53.0
#3 lore raw

Re: [PATCH V3 2/3] power: supply: cw2217: Add support for Cellwise cw2217

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] power: supply: cw2217: cw2217_active() wakes chip before entering shutdown - [Low] power: supply…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] power: supply: cw2217: cw2217_active() wakes chip before entering shutdown
- [Low] power: supply: cw2217: inaccurate writable and volatile regmap ranges
--

commit 9be66fa8e5df716905efd0c61236d0fe980e4cc9
Author: Chris Morgan <[email protected]>

power: supply: cw2217: Add support for Cellwise cw2217

This commit adds a power supply driver for the Cellwise CW2215, CW2217, and CW2218 I2C battery fuel gauges.

Quoted 6 lines
drivers/power/supply/cw2217_battery.c new file +0 -0
new file mode 100644
index 0000000000000..a25803865943e
@@ -0,0 +1,878 @@

[ ... ]

Quoted 30 lines
drivers/power/supply/cw2217_battery.c new file +30 -0
+/*
+ * The CONFIG register is used for the host MCU to configure the fuel gauge IC.
+ * The default value is 0xF0, SLEEP and RESTART bits are set. To power up 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 parameters
+ * and settings and restart SOC calculation. Note that the SOC may be a
+ * different value after reset operation since it is a brand-new calculation
+ * based on the latest battery status. The BSP driver slept 20ms after restart
+ * 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;
+
+	ret = regmap_write(cw_bat->regmap, CW2217_CONFIG,
+			   CW2217_CONFIG_RESTART);
+	if (ret)
+		return ret;
+
+	fsleep(20000);
+
+	if (active) {
+		ret = regmap_write(cw_bat->regmap, CW2217_CONFIG,
+				   CW2217_CONFIG_ACTIVE);
+		if (ret)
+			return ret;
+	} else {

drivers/power/supply/cw2217_battery.c line 149

Quoted 10 lines
drivers/power/supply/cw2217_battery.c new file +10 -0
+		ret = regmap_write(cw_bat->regmap, CW2217_CONFIG,
+				   CW2217_CONFIG_SLEEP | CW2217_CONFIG_RESTART);
+		if (ret)
+			return ret;
+	}
+
+	fsleep(10000);
+
+	return 0;
+}

[Severity: Low]
Should writing CW2217_CONFIG_RESTART (0x30) only happen when active is true?

Writing 0x30 unconditionally means cw2217_active(cw_bat, false) in cw2217_update_config() first wakes the gauge out of shutdown mode for 20ms before writing 0xF0 (CW2217_CONFIG_SLEEP | CW2217_CONFIG_RESTART), producing a 0x30 -> 0xF0 -> 0x30 -> 0x00 sequence instead of the documented 0xF0 -> 0x30 -> 0x00 reset sequence.

[ ... ]

Quoted 10 lines
drivers/power/supply/cw2217_battery.c new file +10 -0
+static const struct regmap_range cw2217_ranges_wr_yes[] = {
+	regmap_reg_range(CW2217_TEMP, 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),
+};
+
+static const struct regmap_access_table cw2217_wr_table = {

drivers/power/supply/cw2217_battery.c line 742

Quoted 10 lines
drivers/power/supply/cw2217_battery.c new file +10 -0
+	.yes_ranges = cw2217_ranges_wr_yes,
+	.n_yes_ranges = ARRAY_SIZE(cw2217_ranges_wr_yes),
+};
+
+static const struct regmap_range cw2217_ranges_vol_yes[] = {
+	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),
+};

[Severity: Low]
Are the writable and volatile regmap ranges for CW2217_TEMP, CW2217_CURRENT_H..CW2217_CURRENT_L, and CW2217_FW_VERSION intended?

CW2217_TEMP (0x06) and CW2217_CURRENT_H..CW2217_CURRENT_L (0x0E..0x0F) are documented as read-only measurement registers, yet cw2217_ranges_wr_yes marks CW2217_TEMP as writable and extends the CW2217_INT_CONF range through CW2217_CURRENT_L instead of stopping at CW2217_TEMP_MIN (0x0D).

In addition, CW2217_FW_VERSION (0xAB) changes in hardware between 0x00 in shutdown mode and the chip ID / firmware version in active mode, but it is missing from cw2217_ranges_vol_yes while REGCACHE_MAPLE is enabled. If CW2217_FW_VERSION is read while the chip is in shutdown mode (for example via regmap debugfs), 0x00 is cached and subsequent reads in active mode will return 0x00, causing cw2217_get_current() to fail with -EIO.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw

PATCH v3 3/3 arm64: dts: rockchip: Add Battery Gauge to Anbernic Vita-Pro

In reply to Chris Morgan

From: Chris Morgan <[email protected]>

Add support for the Cellwise cw2217 battery gauge to the Anbernic Vita-Pro.

---
 .../rockchip/rk3576-anbernic-rg-vita-pro.dts  | 21 ++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
arch/arm64/boot/dts/rockchip/rk3576-anbernic-rg-vita-pro.dts +20 -1
index e66803b5399c..2203e092e8c8 100644
@@ -954,7 +954,26 @@ hym8563: rtc@51 {
 		wakeup-source;
 	};
 
-	/* cellwise,cw221X battery manager at 0x64 */
+	cw2217: fuel-gauge@64 {
+		compatible = "cellwise,cw2217";
+		reg = <0x64>;
+		cellwise,battery-profile = /bits/ 8 <
+			0x5a 0x00 0x00 0x00 0x00 0x00 0x00 0x00
+			0xab 0xb5 0xae 0xc0 0xa9 0xa4 0xcf 0xaf
+			0x96 0xff 0xff 0xd7 0x9a 0x83 0x70 0x69
+			0x65 0x65 0x65 0xb8 0x81 0xd9 0xaf 0xd9
+			0xd2 0xce 0xcd 0xcb 0xca 0xc7 0xc8 0xc5
+			0xc2 0xba 0xa8 0x96 0x8a 0x85 0x7d 0x7a
+			0x7f 0x88 0x92 0x93 0x94 0xd3 0xff 0x5f
+			0x20 0x00 0xab 0x10 0x00 0x91 0x68 0x00
+			0x00 0x00 0x64 0x3c 0xa0 0x89 0x00 0x00
+			0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xb2
+		>;
+		monitored-battery = <&battery>;
+		power-supplies = <&sgm41542>;
+		shunt-resistor-micro-ohms = <2000>;
+		status = "okay";
+	};
 };
 
 &mipidcphy {
Signature · 1 line
2.53.0