Re: [PATCH v7 3/3] iio: adc: add driver for the MAX34417 Four-Channel High Dynamic Range Power Accumulator

From: Neil Armstrong

Date: Tue Nov 24 2026 - 04:31:03 EST


On 11/23/26 20:39, Andy Shevchenko wrote:
On Mon, Nov 23, 2026 at 06:47:46PM +0100, Neil Armstrong wrote:
The MAX34417 is a specialized current and voltage monitor used to
determine power consumption of portable systems. The driver support
getting the channels voltage, average power, accumulated average
power and energy over an I2C/SMBUS serial interface.

The MAX34417 datasheet only specified on how to calculate the
accumulated average power, but with the Sample Per Second rate
we can easily get accumulated Energy in Joules.

By default the IIO driver only supports single-shot Voltage
and Power sensing, but the accumulation can be enabled by
enabling the Energy IIO channel which then enables the
Continuous Accumulation Mode.

The counter is common to all channels, so when reading a single
channel we will copy all accumulators to registers, so in order
to permit reading individual channels we must accumulate the
other channels in the driver when Continuous Accumulation Mode
is enabled.

...

+#define MAX34417_MIN_RSENSE_UOHM (1 * MILLI)
+#define MAX34417_MAX_RSENSE_UOHM (100 * MILLI)
+#define MAX34417_DEFAULT_RSENSE_UOHM (1 * MILLI)

Oh, it's micro, but you use MILLI. Also even in macros we nowadays want to see
the unit as defined in SI.

#define MAX34417_MIN_RSENSE_uOhm (1 * (MICRO / MILLI))
#define MAX34417_MAX_RSENSE_uOhm (100 * (MICRO / MILLI))
#define MAX34417_DEFAULT_RSENSE_uOhm (1 * (MICRO / MILLI))

Alternatively add another patch to start Ohm multipliers in units.h

#define MICROOHM_PER_MILLIOHM 1000

I will Ack it.


Hmm, I want to express the min/max/default rsense in uOhm, minimum is 1000 uOhm,
max is 100000 uOhm, so initially they were:
#define MAX34417_MIN_RSENSE (1000)
#define MAX34417_MAX_RSENSE (100000)

and the unit is uOhm, so how should I define them ?

with #define MICROOHM_PER_MILLIOHM 1000

#define MAX34417_MIN_RSENSE_uOhm (1 * MICROOHM_PER_MILLIOHM)
#define MAX34417_MAX_RSENSE_uOhm (100 * MICROOHM_PER_MILLIOHM)

?

...

+struct max34417_data {
+ struct iio_dev *indio_dev;

Useless. You have it already available when calling the function that uses it.

+ struct regmap *regmap;
+ struct mutex lock;
+ u32 input_correction[MAX34417_CHANNEL_COUNT];
+ const char *input_label[MAX34417_CHANNEL_COUNT];
+ s64 acc_power[MAX34417_CHANNEL_COUNT];
+ s64 acc_count;
+ bool overflow;
+ ktime_t last_update;
+ bool cam_mode;

Are the booleans protected by the same lock? If not, just group them, it closes
the gap, if yes, group and possibly make a bitfield, then they will occupy only
a single byte.

+};

...

+static int max34417_read_energy(struct max34417_data *max34417,
+ const struct iio_chan_spec *chan,
+ int *val, int *val2)
+{
+ struct regmap *map = max34417->regmap;
+ s64 acc_power;
+ s64 delta_us;
+ int rc;
+
+ if (max34417->cam_mode && max34417->overflow)
+ return -EOVERFLOW;
+
+ /* Sleep up to 1msec to make sure we have a measurement cycle done */

ms or millisecond

+ delta_us = ktime_us_delta(ktime_get(), max34417->last_update);
+ if (delta_us >= 0 && delta_us < (1 * USEC_PER_MSEC))
+ fsleep((1 * USEC_PER_MSEC) - delta_us);
+
+ /* Move the data to the SMBus registers */
+ rc = regmap_write(map, MAX34417_UPDATE_REG, 1);
+ if (rc)
+ return rc;
+
+ /* Save the timestamp for the next read */
+ max34417->last_update = ktime_get();
+
+ /* Retrieve all channels to avoid missing data for other channels */
+ rc = max34417_save_accumulators(max34417);
+ if (rc)
+ return rc;
+
+ /* Get current channel power */
+ acc_power = max34417->acc_power[chan->channel];
+
+ iio_val_s64_decompose(acc_power, val, val2);
+
+ return IIO_VAL_INT_64;
+}