All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Chris Morgan" <macroalpha82@gmail.com>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH V2 2/3] power: supply: cw2217: Add support for Cellwise cw2217
Date: Fri, 20 Nov 2026 21:42:57 +0000	[thread overview]
Message-ID: <sashiko-outbox-226207@kernel.org> (raw)
In-Reply-To: <20261120212815.35663-3-macroalpha82@gmail.com>

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 <macromorgan@hotmail.com>

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.

> diff --git a/drivers/power/supply/cw2217_battery.c b/drivers/power/supply/cw2217_battery.c
> new file mode 100644
> index 0000000000000..a54e19889b46c
> --- /dev/null
> +++ b/drivers/power/supply/cw2217_battery.c
[ ... ]
> +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,
> +			       &reg, sizeof(reg));
> +	if (ret)
> +		return ret;
> +
> +	cw_current = (s16)be16_to_cpu(reg);
> +
> +	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.

[ ... ]

> +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");
> +
> +	/*
> +	 * 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.

[ ... ]

> +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,
> +			      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?

[ ... ]

> +static const struct of_device_id cw2217_match_table[] = {
> +	{ .compatible = "cellwise,cw2215", },
> +	{ .compatible = "cellwise,cw2217", },
> +	{ .compatible = "cellwise,cw2218", },
> +	{ },
> +};
> +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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120212815.35663-1-macroalpha82@gmail.com?part=2

  reply	other threads:[~2026-11-20 21:42 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 21:28 [PATCH V2 0/3] Add Cellwise CW2217 Chris Morgan
2026-11-20 21:28 ` Chris Morgan
2026-11-20 21:28 ` [PATCH V2 1/3] dt-bindings: power: cw2015: add cw2215, cw2217, and cw2218 Chris Morgan
2026-11-20 21:28   ` Chris Morgan
2026-11-20 21:28 ` [PATCH V2 2/3] power: supply: cw2217: Add support for Cellwise cw2217 Chris Morgan
2026-11-20 21:28   ` Chris Morgan
2026-11-20 21:42   ` sashiko-bot [this message]
2026-11-20 21:28 ` [PATCH V2 3/3] arm64: dts: rockchip: Add Battery Gauge to Anbernic Vita-Pro Chris Morgan
2026-11-20 21:28   ` Chris Morgan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-226207@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=macroalpha82@gmail.com \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.