All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Neil Armstrong" <neil.armstrong@linaro.org>
Cc: devicetree@vger.kernel.org, linux-hwmon@vger.kernel.org,
	robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v7 3/3] iio: adc: add driver for the MAX34417 Four-Channel High Dynamic Range Power Accumulator
Date: Mon, 23 Nov 2026 18:02:52 +0000	[thread overview]
Message-ID: <sashiko-outbox-229791@kernel.org> (raw)
In-Reply-To: <20261123-topic-sm8x50-iio-max34417-adc-v7-3-d8f87edc5086@linaro.org>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] iio: adc: max34417: voltage read fails on power overflow in
  CAM mode
- [Medium] iio: adc: max34417: stale OVF and state desync in
  max34417_setup_cam_mode()
--

commit bc8a43ebc9b20a80f36676c2f0db12f5485b7c1d
Author: Neil Armstrong <neil.armstrong@linaro.org>

iio: adc: add driver for the MAX34417 Four-Channel High Dynamic Range Power Accumulator

This commit adds an IIO ADC driver for the MAX34417 four-channel voltage and
power accumulator over I2C/SMBus, supporting single-shot voltage and power
measurements as well as continuous energy and average power accumulation.

> diff --git a/drivers/iio/adc/max34417.c b/drivers/iio/adc/max34417.c
> new file mode 100644
> index 0000000000000..241cc9c3c2546
> --- /dev/null
> +++ b/drivers/iio/adc/max34417.c
[ ... ]
> +static int max34417_check_ovf(struct max34417_data *max34417)
> +{
> +	struct regmap *map = max34417->regmap;
> +	u32 control;
> +	int rc;
> +
> +	rc = regmap_read(map, MAX34417_CONTROL_REG, &control);
> +	if (rc)
> +		return rc;
> +
> +	if (!(control & MAX34417_CONTROL_OVF))
> +		return 0;
> +
> +	rc = regmap_clear_bits(map, MAX34417_CONTROL_REG, MAX34417_CONTROL_OVF);
> +	if (rc)
> +		return rc;
> +
> +	rc = regmap_write(map, MAX34417_UPDATE_REG, 1);
> +	if (rc)
> +		return rc;
> +
> +	/* Save the timestamp for the next read */
> +	max34417->last_update = ktime_get();
> +
> +	return 0;
> +}
[ ... ]
> +static int max34417_save_accumulators(struct max34417_data *max34417)
> +{
> +	s64 acc_power;
> +	s32 acc_count;
> +	int rc;
> +
> +	rc = max34417_read_acc_count(max34417, &acc_count);
> +	if (rc)
> +		return rc;
> +
> +	if (check_add_overflow(max34417->acc_count, acc_count, &max34417->acc_count)) {
> +		max34417->overflow = true;
> +		goto check_ovf;
> +	}
> +
> +	for (int i = 0; i < MAX34417_CHANNEL_COUNT; i++) {
> +		rc = max34417_read_acc_power(max34417, MAX34417_PWR_ACC_1_REG + i,
> +					     &acc_power);
> +		if (rc)
> +			return rc;
> +
> +		/* Check for overflow on local accumulators */
> +		if (check_add_overflow(max34417->acc_power[i], acc_power,
> +				       &max34417->acc_power[i])) {
> +			max34417->overflow = true;
> +			goto check_ovf;
> +		}
> +	}
> +
> +check_ovf:
> +	rc = max34417_check_ovf(max34417);
> +	if (rc)
> +		return rc;
> +
> +	return max34417->overflow ? -EOVERFLOW : 0;
> +}
> +
> +static int max34417_read_voltage(struct max34417_data *max34417,
> +				 const struct iio_chan_spec *chan, int *val)
> +{
[ ... ]
> +	rc = regmap_write(map, MAX34417_UPDATE_REG, 1);
> +	if (rc)
> +		return rc;
> +
> +	/* Save the timestamp for the next read */
> +	max34417->last_update = ktime_get();
> +
> +	if (max34417->cam_mode) {
> +		/* Retrieve all channels to avoid missing data for other channels */
> +		rc = max34417_save_accumulators(max34417);
> +		if (rc)
> +			return rc;
> +	}
> +
> +	rc = regmap_noinc_read(map, chan->address, &buf, sizeof(buf));
> +	if (rc)
> +		return rc;
> +
> +	/* Ignore first byte which contains the Byte Count & data starts at Bit 2 */
> +	*val = get_unaligned_be16(&buf[MAX34417_VOLTAGE_READ_OFFSET]) >> 2;
> +
> +	return IIO_VAL_INT;
> +}

[Severity: Medium]
Should max34417_read_voltage() fail with -EOVERFLOW when a software power
accumulator overflows in CAM mode?

Voltage readings from MAX34417_V_CHx_REG are instantaneous 14-bit samples
that do not depend on max34417->acc_power[] or max34417->acc_count, yet once
max34417->overflow is set, max34417_save_accumulators() returns -EOVERFLOW
and aborts every voltage read.

In addition, because neither max34417_read_voltage() nor
max34417_save_accumulators() checks max34417->overflow before running
check_add_overflow(), subsequent voltage reads continue adding into the
wrapped max34417->acc_count and max34417->acc_power[] values.  And if
MAX34417_CONTROL_OVF is set in hardware, max34417_check_ovf() clears it and
writes MAX34417_UPDATE_REG before max34417_read_voltage() has read
chan->address.

[ ... ]
> +static int max34417_setup_cam_mode(struct max34417_data *max34417, int val)
> +{
> +	struct regmap *map = max34417->regmap;
> +	int rc;
> +
> +	if (max34417->cam_mode && !val) {
> +		/* Reset counters and accumulators */
> +		for (int i = 0; i < MAX34417_CHANNEL_COUNT; i++)
> +			max34417->acc_power[i] = 0;
> +		max34417->acc_count = 0;
> +		max34417->overflow = false;
> +
> +		rc = regmap_clear_bits(map, MAX34417_CONTROL_REG,
> +				       MAX34417_CONTROL_CAM);
> +		if (rc)
> +			return rc;
> +
> +		rc = regmap_write(map, MAX34417_UPDATE_REG, 1);
> +		if (rc)
> +			return rc;
> +	} else if (!max34417->cam_mode && val) {
> +		/*
> +		 * If CAM mode must be set, the Control byte must be set to desired
> +		 * values with CAM = 0, then followed by an UPDATE.
> +		 */
> +		rc = regmap_clear_bits(map, MAX34417_CONTROL_REG,
> +				       MAX34417_CONTROL_CAM);
> +		if (rc)
> +			return rc;
> +
> +		rc = regmap_write(map, MAX34417_UPDATE_REG, 1);
> +		if (rc)
> +			return rc;
> +
> +		rc = regmap_set_bits(map, MAX34417_CONTROL_REG, MAX34417_CONTROL_CAM);
> +		if (rc)
> +			return rc;
> +
> +		rc = regmap_write(map, MAX34417_UPDATE_REG, 1);
> +		if (rc)
> +			return rc;
> +	}
> +
> +	/* Save the timestamp for the next read */
> +	max34417->last_update = ktime_get();
> +
> +	max34417->cam_mode = val;
> +
> +	return 0;
> +}

[Severity: Medium]
Does max34417_setup_cam_mode() leave MAX34417_CONTROL_OVF set in
MAX34417_CONTROL_REG when toggling CAM mode after an accumulator saturation?

When a hardware accumulator saturates, the chip sets MAX34417_CONTROL_OVF
and stops measuring until MAX34417_CONTROL_OVF is cleared followed by an
UPDATE command.  And once max34417->overflow is set, max34417_read_power()
and max34417_read_energy() return -EOVERFLOW before reaching
max34417_check_ovf(), so disabling and re-enabling CAM mode is the way to
reset accumulation.  Because both branches here only modify
MAX34417_CONTROL_CAM via regmap_clear_bits() and regmap_set_bits(),
MAX34417_CONTROL_OVF stays set in hardware across the reset and keeps
accumulation halted when CAM mode is re-enabled.

Also, in the disable branch, max34417->acc_power[], max34417->acc_count and
max34417->overflow are cleared before the I2C writes, so a failed write
discards the accumulated data while leaving max34417->cam_mode true; in the
enable branch, if the final regmap_write(map, MAX34417_UPDATE_REG, 1) fails,
MAX34417_CONTROL_CAM remains set in hardware while max34417->cam_mode stays
false.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123-topic-sm8x50-iio-max34417-adc-v7-0-d8f87edc5086@linaro.org?part=3

  reply	other threads:[~2026-11-23 18:02 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 17:47 [PATCH v7 0/3] iio: adc: add support for the MAX34417 Four-Channel High Dynamic Range Power Accumulator Neil Armstrong
2026-11-23 17:47 ` [PATCH v7 1/3] dt-bindings: iio: add: document " Neil Armstrong
2026-11-23 17:47 ` [PATCH v7 2/3] unaligned: introduce be56 unaligned accessors Neil Armstrong
2026-11-23 17:47 ` [PATCH v7 3/3] iio: adc: add driver for the MAX34417 Four-Channel High Dynamic Range Power Accumulator Neil Armstrong
2026-11-23 18:02   ` sashiko-bot [this message]
2026-11-24 14:03     ` Neil Armstrong
2026-11-23 19:39   ` Andy Shevchenko
2026-11-24  9:30     ` Neil Armstrong
2026-11-24  9:47       ` Andy Shevchenko
2026-11-28 20:38   ` Jonathan Cameron
2026-11-30 15:04     ` Neil Armstrong
2026-11-23 18:02 ` [PATCH v7 0/3] iio: adc: add support " sashiko-bot

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-229791@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=neil.armstrong@linaro.org \
    --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.