Skip to contentSkip to conversation

Add Cellwise CW2217

PATCH New Chris Morgan 1 participant 4 messages Nov 27

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v5 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 V4:
- Updated bindings to set 64 as the minItems and 80 as the maxItems for all battery profiles. 64 remains the min and max for the cw2015 series and 80 remains the min and max for the others.
- Renived the status okay line from the vita-pro device tree for the battery monitor since it is implied.

Changes Since V3:
- Two more small fixes requested by Sashiko, one trivial fix for the to not always write CW2217_CONFIG_RESTART for the cw2217_active method and a small fix for the regmap writeable/volatile ranges.

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  |  20 +-
 drivers/power/supply/Kconfig                  |  11 +
 drivers/power/supply/Makefile                 |   1 +
 drivers/power/supply/cw2217_battery.c         | 878 ++++++++++++++++++
 5 files changed, 945 insertions(+), 6 deletions(-)
 create mode 100644 drivers/power/supply/cw2217_battery.c
Signature · 1 line
2.53.0
#1 lore raw

PATCH v5 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.

Update the minItems and maxItems for the battery profile so that the cw2015 continues to have a min and max of 64, but the new hardware has a max of 80.

---
 .../bindings/power/supply/cw2015_battery.yaml | 41 ++++++++++++++++---
 1 file changed, 36 insertions(+), 5 deletions(-)
Documentation/devicetree/bindings/power/supply/cw2015_battery.yaml +36 -5
index f7bde324153d..2db80287def4 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
@@ -32,7 +33,7 @@ properties:
       back a test report with the binary blob.
     $ref: /schemas/types.yaml#/definitions/uint8-array
     minItems: 64
-    maxItems: 64
+    maxItems: 80
 
   cellwise,monitor-interval-ms:
     description:
@@ -43,12 +44,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 v5 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

PATCH v5 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  | 20 ++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
arch/arm64/boot/dts/rockchip/rk3576-anbernic-rg-vita-pro.dts +19 -1
index e66803b5399c..d4abd497f355 100644
@@ -954,7 +954,25 @@ 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>;
+	};
 };
 
 &mipidcphy {
Signature · 1 line
2.53.0