Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Kupreenko Danila" <keckerz@yandex.ru>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [RFC PATCH 2/3] power: supply: add read-only CW2017 battery monitor
Date: Thu, 29 Oct 2026 18:06:18 +0000	[thread overview]
Message-ID: <sashiko-outbox-189760@kernel.org> (raw)
In-Reply-To: <179329639684.489944.17646031923456877930.cw2017-first-public-rfc-2@yandex.ru>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] power: supply: cw2017: broken sample.error tracking in cw2017_suspend()
--

commit 2974e2dfc1b0336eb1b26f72d45d626971f0a216
Author: Kupreenko Danila <keckerz@yandex.ru>

power: supply: add read-only CW2017 battery monitor

This patch adds a read-only power_supply driver for firmware-initialized
CellWise CW2017 battery fuel gauges over I2C, polling voltage, state of
charge, and temperature with exponential backoff on read errors.

> diff --git a/drivers/power/supply/cw2017_battery.c b/drivers/power/supply/cw2017_battery.c
> new file mode 100644
> index 0000000000000..86323849a813a
> --- /dev/null
> +++ b/drivers/power/supply/cw2017_battery.c
> @@ -0,0 +1,370 @@

[ ... ]

> +static void cw2017_poll(struct work_struct *work)
> +{
> +	struct cw2017 *cw = container_of(to_delayed_work(work), struct cw2017, work);
> +	struct cw2017_sample sample = { };
> +	bool changed;
> +
> +	sample.error = cw2017_read_sample(cw, &sample);
> +	if (sample.error)
> +		dev_dbg_ratelimited(cw->dev, "Measurements unavailable (%d)\n",
> +				    sample.error);
> +
> +	mutex_lock(&cw->lock);
> +	changed = !cw->sample.error != !sample.error ||
> +		  (!sample.error &&
> +		   (cw->sample.capacity != sample.capacity ||
> +		    cw->sample.voltage_uv != sample.voltage_uv ||
> +		    cw->sample.temp != sample.temp));
> +	cw->sample = sample;
> +	mutex_unlock(&cw->lock);
> +
> +	if (changed)
> +		power_supply_changed(cw->psy);
> +
> +	if (sample.error)
> +		cw->poll_interval_ms = min(cw->poll_interval_ms * 2,
> +					   CW2017_MAX_POLL_INTERVAL_MS);
> +	else
> +		cw->poll_interval_ms = CW2017_POLL_INTERVAL_MS;
> +
> +	queue_delayed_work(system_freezable_wq, &cw->work,
> +			   round_jiffies_relative(msecs_to_jiffies(cw->poll_interval_ms)));
> +}

[ ... ]

> +static int cw2017_suspend(struct device *dev)
> +{
> +	struct cw2017 *cw = dev_get_drvdata(dev);
> +
> +	cancel_delayed_work_sync(&cw->work);
> +	mutex_lock(&cw->lock);
> +	cw->sample.error = -ENODATA;
> +	mutex_unlock(&cw->lock);
> +
> +	return 0;
> +}
> +
> +static int cw2017_resume(struct device *dev)
> +{
> +	struct cw2017 *cw = dev_get_drvdata(dev);
> +
> +	cw->poll_interval_ms = CW2017_POLL_INTERVAL_MS;
> +	queue_delayed_work(system_freezable_wq, &cw->work, 0);
> +
> +	return 0;
> +}

[Severity: Medium]
Does setting cw->sample.error to -ENODATA in cw2017_suspend() without
emitting a notification break the validity-transition tracking in
cw2017_poll() across suspend and resume?

In cw2017_poll(), validity transitions are detected by comparing the cached
error state against the new sample error state:

	changed = !cw->sample.error != !sample.error ||
		  (!sample.error &&
		   (cw->sample.capacity != sample.capacity ||
		    cw->sample.voltage_uv != sample.voltage_uv ||
		    cw->sample.temp != sample.temp));

Because cw2017_suspend() overwrites cw->sample.error with -ENODATA without
calling power_supply_changed(cw->psy), two notification issues arise when
cw2017_poll() runs after resume:

1. If measurements were valid before suspend (cw->sample.error == 0) and
   cw2017_read_sample() fails after resume (for example, if the gauge lost
   its profile or readiness across suspend or an I2C error occurs), both
   cw->sample.error and sample.error are non-zero. As a result, changed
   evaluates to false and power_supply_changed(cw->psy) is never called,
   leaving consumers with stale pre-suspend values.

2. If measurements were valid before suspend and remain valid and unchanged
   after resume (sample.error == 0), !cw->sample.error != !sample.error
   evaluates to true solely because cw2017_suspend() forced cw->sample.error
   to -ENODATA, triggering an unconditional power_supply_changed(cw->psy)
   uevent on every resume.

In addition, cw2017_resume() queues cw->work on system_freezable_wq, which
remains frozen until suspend_thaw_processes() runs after dpm_complete().
When dpm_complete() -> thermal_pm_complete() schedules
thermal_zone_device_resume() on the non-freezable thermal_wq (registered
for POWER_SUPPLY_PROP_TEMP via __power_supply_register() ->
psy_register_thermal()), cw2017_get_property() will see cw->sample.error ==
-ENODATA and fail thermal zone resume before the post-resume poll work has a
chance to run.

Would it be better to preserve cw->sample across suspend so cw2017_poll()
can compare the post-resume reading against the last reported state?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/179329639684.489944.6322623410181934358.cw2017-first-public-rfc@yandex.ru?part=2

  reply	other threads:[~2026-10-29 18:06 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-29 17:53 [RFC PATCH 0/3] power: supply: read-only CellWise CW2017 monitor for Redmi Go Kupreenko Danila
2026-10-29 17:53 ` [RFC PATCH 1/3] dt-bindings: power: supply: add CellWise CW2017 fuel gauge Kupreenko Danila
2026-10-29 17:53 ` [RFC PATCH 3/3] arm64: dts: qcom: tiare: describe " Kupreenko Danila
2026-10-29 18:11   ` sashiko-bot
2026-10-29 17:53 ` [RFC PATCH 2/3] power: supply: add read-only CW2017 battery monitor Kupreenko Danila
2026-10-29 18:06   ` sashiko-bot [this message]
2026-10-29 19:10     ` Kupreenko Danila

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-189760@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=keckerz@yandex.ru \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox