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, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v8 4/5] iio: adc: add driver for the MAX34417 Four-Channel High Dynamic Range Power Accumulator
Date: Mon, 30 Nov 2026 16:43:40 +0000	[thread overview]
Message-ID: <sashiko-outbox-241604@kernel.org> (raw)
In-Reply-To: <20261130-topic-sm8x50-iio-max34417-adc-v8-4-6efef2782816@linaro.org>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] iio: adc: max34417: missing hardware OVF recovery in
  max34417_read_voltage()
- [Low] iio: adc: max34417: integer truncation in
  max34417_calc_input_correction()
--

commit 9ace5564b012f4fd085ba7d50f6b5c2ae73e3e15
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 Analog Devices MAX34417
four-channel current and voltage monitor, supporting voltage, power, and
continuous energy accumulation over I2C/SMBus.

> diff --git a/drivers/iio/adc/max34417.c b/drivers/iio/adc/max34417.c
> new file mode 100644
> index 0000000000000..71a24d62c59c1
> --- /dev/null
> +++ b/drivers/iio/adc/max34417.c

[ ... ]

> +static int max34417_read_voltage(struct max34417_data *max34417,
> +				 const struct iio_chan_spec *chan, int *val)
> +{
> +	struct regmap *map = max34417->regmap;
> +	u8 buf[MAX34417_VOLTAGE_READ_LENGTH];
> +	s64 delta_us;
> +	int rc;

[ ... ]

> +	/*
> +	 * Move the new UPDATE data into the SMBus read registers.
> +	 * This will also reset the accumulators, returning the power accumulated
> +	 * since the last UPDATE.
> +	 */
> +	rc = regmap_write(map, MAX34417_UPDATE_REG, 1);
> +	if (rc)
> +		return rc;
> +
> +	/* Save the timestamp for the next read */
> +	max34417->last_update = ktime_get();
> +
> +	/* Continue reading voltage even if local accumulation overflows */
> +	if (max34417->cam_mode && !max34417->overflow) {
> +		/* Retrieve all channels to avoid missing data for other channels */
> +		rc = max34417_save_accumulators(max34417);
> +		if (rc && rc != -EOVERFLOW)
> +			return rc;
> +	}

[Severity: Low]
Does skipping max34417_save_accumulators() when max34417->overflow is set
also skip max34417_check_ovf()?

If a hardware accumulator later saturates while in Continuous Accumulation
Mode, MAX34417_CONTROL_OVF is never cleared and the device stops measuring,
so subsequent voltage reads return stale values.  Calling
max34417_check_ovf() when max34417->overflow is true would keep voltage
measurements running.

[ ... ]

> +	case IIO_CHAN_INFO_SCALE:
> +		switch (chan->type) {
> +		case IIO_VOLTAGE:
> +			/* Scale to mV */
> +			*val = MAX34417_VOLTAGE_CORRECTION_SCALE * MILLI;
> +			*val2 = MAX34417_VOLTAGE_FULL_SCALE_BITS;
> +
> +			return IIO_VAL_FRACTIONAL_LOG2;
> +		case IIO_POWER:
> +			/*
> +			 * Scale to mW
> +			 * (1000 * (Power Accumulation / Counter) * Correction) / 2^30
> +			 */
> +			*val = max34417->input_correction[chan->channel] * MILLIWATT_PER_WATT;
> +			*val2 = MAX34417_PWR_AVG_FULL_SCALE_BITS;
> +
> +			return IIO_VAL_FRACTIONAL_LOG2;
> +		case IIO_ENERGY:
> +			/*
> +			 * Scale to Joules
> +			 * P=((Power Accumulation / Counter) * Correction) / 2^30
> +			 * t=(Counter / Samples per Second)
> +			 * E=(P * t)
> +			 * Can be simplified to:
> +			 * (Power Accumulation * Correction) / (2^30 * SPS)
> +			 * And scale expressed in Femto (10^-15 precision):
> +			 * S=(Correction * 10^15) / (2^30 * SPS)
> +			 * With the maximum input_correction of 2400, it fits in 64bits
> +			 */
> +			num = (u64)max34417->input_correction[chan->channel] * FEMTO;
> +			den = (u64)MAX34417_FAST_SPS_CAM << MAX34417_PWR_AVG_FULL_SCALE_BITS;
> +			scale = div64_u64(num, den);
> +			iio_val_s64_decompose(scale, val, val2);
> +
> +			return IIO_VAL_DECIMAL64_FEMTO;

[ ... ]

> +static u32 max34417_calc_input_correction(u32 rsense_uOhm)
> +{
> +	/* Derived from Table 14. Correction Factors for Various Current-Sense Resistor Values */
> +	return (MAX34417_MAX_RSENSE_uOhm * MAX34417_PWR_CORRECTION_SCALE) / rsense_uOhm;
> +}

[Severity: Low]
Does integer division in max34417_calc_input_correction() truncate the
correction factor to whole Watts before max34417_read_raw() scales it to
mW or femtoJoules?

For a shunt-resistor-micro-ohms value that does not evenly divide 2400000,
such as 33000 uOhm, 2400000 / 33000 truncates 72.727 to 72, giving a power
scale numerator of 72000 mW instead of 72727 mW.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130-topic-sm8x50-iio-max34417-adc-v8-0-6efef2782816@linaro.org?part=4

  reply	other threads:[~2026-11-30 16:43 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 16:30 [PATCH v8 0/5] iio: adc: add support for the MAX34417 Four-Channel High Dynamic Range Power Accumulator Neil Armstrong
2026-11-30 16:30 ` [PATCH v8 1/5] dt-bindings: iio: add: document " Neil Armstrong
2026-11-30 16:30 ` [PATCH v8 2/5] unaligned: introduce be56 unaligned accessors Neil Armstrong
2026-11-30 16:30 ` [PATCH v8 3/5] units: Add MICROOHM_PER_MILLIOHM Neil Armstrong
2026-11-30 17:29   ` Andy Shevchenko
2026-11-30 16:30 ` [PATCH v8 4/5] iio: adc: add driver for the MAX34417 Four-Channel High Dynamic Range Power Accumulator Neil Armstrong
2026-11-30 16:43   ` sashiko-bot [this message]
2026-11-30 16:50     ` Neil Armstrong
2026-11-30 16:30 ` [PATCH v8 5/5] docs: iio: add documentation for MAX34417 driver Neil Armstrong

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-241604@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@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.