Add Cellwise CW2217
v2 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 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 | 826 ++++++++++++++++++ 5 files changed, 893 insertions(+), 7 deletions(-) create mode 100644 drivers/power/supply/cw2217_battery.c
Signature · 1 line
2.53.0
PATCH v2 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
@@ -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
PATCH v2 2/3 power: supply: cw2217: Add support for Cellwise cw2217
In reply to Chris MorganFrom: 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 | 826 ++++++++++++++++++++++++++ 3 files changed, 838 insertions(+) create mode 100644 drivers/power/supply/cw2217_battery.c
drivers/power/supply/Kconfig +11 -0
@@ -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 (used in the Anbernic RG Vita Pro & others) + + 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
@@ -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 +826 -0
826 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH V2 2/3] power: supply: cw2217: Add support for Cellwise cw2217
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] power: supply: cw2217: 32-bit signed overflow in cw2217_get_current() - [Medium] power: supply…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] power: supply: cw2217: 32-bit signed overflow in cw2217_get_current()
- [Medium] power: supply: cw2217: gauge left in shutdown mode without DT profile
- [Medium] power: supply: cw2217: missing power_supply_changed() in cw2217_bat_work()
- [Low] power: supply: cw2217: unused match table with !CONFIG_OF via of_match_ptr()
--
commit b7f80d5b2ad4376d864e98e5237d2f7d87f7e959
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 5 lines
drivers/power/supply/cw2217_battery.c new file +0
-0
[ ... ]
Quoted 19 lines
drivers/power/supply/cw2217_battery.c new file +19
-0+static int cw2217_get_current (struct cw2217_battery *cw_bat) +{ +s32 cw_current; +__be16 reg; +u8 fw_ver; +int ret; + +if (cw_bat->fw_version ==0 ) { + cw_bat->cw_current =0 ; +dev_err (cw_bat->dev,"Error! CW2217 firmware read error!\n" ); +return -EIO ; + } + + ret =regmap_bulk_read (cw_bat->regmap,CW2217_CURRENT_H , + ®,sizeof (reg)); +if (ret) +return ret; + + cw_current = (s16 )be16_to_cpu (reg);
drivers/power/supply/cw2217_battery.c line 273
Quoted 10 lines
drivers/power/supply/cw2217_battery.c new file +10
-0+ + fw_ver =FIELD_GET (CW2217_ID_MASK , cw_bat->fw_version); +switch (fw_ver) { +case CW2215_ID : +case CW2217_ID : + cw_bat->cw_current = (cw_current *1600 *1000 ) / cw_bat->user_rsense; +break ; +case CW2218_ID : + cw_bat->cw_current = (cw_current *3815 *1000 ) / cw_bat->user_rsense; +break ;
[Severity: High]
Can the numerator multiplication in cw2217_get_current() overflow s32?
Because cw_current and the multiplier literals are 32-bit signed integers, (cw_current * 1600 * 1000) overflows s32 once abs(cw_current) reaches 1343 (~1.43 A with the default 1500 uOhm shunt, or ~215 mA with a 10 mOhm shunt), and (cw_current * 3815 * 1000) overflows at 563. When charging current wraps negative, POWER_SUPPLY_PROP_CURRENT_NOW is corrupted and cw2217_battery_get_property() misreports POWER_SUPPLY_PROP_STATUS as NOT_CHARGING or FULL.
Using div_s64((s64)cw_current * 1600 * 1000, cw_bat->user_rsense) would avoid the overflow.
[ ... ]
Quoted 9 lines
drivers/power/supply/cw2217_battery.c new file +9
-0+static int cw2217_init (struct cw2217_battery *cw_bat) +{ +int ret; + + ret =cw2217_get_chip_id (cw_bat); +if (ret) +return dev_err_probe (cw_bat->dev, ret,"I2C read write error" ); + +/*
drivers/power/supply/cw2217_battery.c line 539
Quoted 10 lines
drivers/power/supply/cw2217_battery.c new file +10
-0+* 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 !=CW2217_CHIP_ID ) +return dev_err_probe (cw_bat->dev, -ENODEV ,"Not a cw221x\n" ); + +if (!cw_bat->bat_profile) { +dev_warn (cw_bat->dev,"continuing with default bat profile\n" ); +return 0 ; + }
[Severity: Medium]
Does returning early when cw_bat->bat_profile is NULL leave the gauge in shutdown mode?
If the optional cellwise,battery-profile property is omitted and the chip is still in its default shutdown state (CW2217_CONFIG = 0xF0), CW2217_FW_VERSION reads as 0x00, causing cw2217_get_current() to fail cw2217_probe() with -EIO.
[ ... ]
Quoted 12 lines
drivers/power/supply/cw2217_battery.c new file +12
-0+static void cw2217_bat_work (struct work_struct *work) +{ +struct delayed_work *delay_work; +struct cw2217_battery *cw_bat; +int ret; + + delay_work =container_of (work, + struct delayed_work, + work); + + cw_bat = container_of(delay_work, + struct cw2217_battery,
drivers/power/supply/cw2217_battery.c line 577
Quoted 10 lines
drivers/power/supply/cw2217_battery.c new file +10
-0+ battery_delay_work); + + ret =cw2217_update_data (cw_bat); +if (ret) +dev_err (cw_bat->dev,"i2c read error when update data" ); + +queue_delayed_work (cw_bat->battery_workqueue, + &cw_bat->battery_delay_work, +msecs_to_jiffies (cw_bat->poll_interval_ms)); +}
[Severity: Medium]
Should cw2217_bat_work() call power_supply_changed(cw_bat->cw_bat) when battery data changes so userspace and in-kernel subscribers receive update notifications?
[ ... ]
Quoted 4 lines
drivers/power/supply/cw2217_battery.c new file +4
-0+static const struct of_device_id cw2217_match_table[] = { + { .compatible ="cellwise,cw2215" , }, + { .compatible ="cellwise,cw2217" , }, + { .compatible ="cellwise,cw2218" , },
drivers/power/supply/cw2217_battery.c line 816
Quoted 10 lines
drivers/power/supply/cw2217_battery.c new file +10
-0+ { }, +}; +MODULE_DEVICE_TABLE (of, cw2217_match_table); + +static struct i2c_driver cw2217_driver = { + .driver = { + .name ="cw2217" , + .pm = &cw2217_bat_pm_ops, + .of_match_table =of_match_ptr (cw2217_match_table), + },
[Severity: Low]
Does using of_match_ptr() here trigger a -Wunused-const-variable warning on cw2217_match_table when CONFIG_OF is disabled? Since the driver uses generic device_property_* helpers, of_match_ptr() can be dropped.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v2 3/3 arm64: dts: rockchip: Add Battery Gauge to Anbernic Vita-Pro
In reply to Chris MorganFrom: 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
@@ -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