Re: [PATCH v3 3/4] iio: adc: Add support for MAX8907 ADC cell
From: Svyatoslav Ryhel
Date: Mon Nov 23 2026 - 14:24:48 EST
пн, 23 лист. 2026 р. о 21:01 Andy Shevchenko <andriy.shevchenko@xxxxxxxxx> пише:
>
> On Mon, Nov 23, 2026 at 05:07:24PM +0200, Svyatoslav Ryhel wrote:
> > The MAX8907 PMIC has a dedicated ADC cell, which provides 8 ADC channels
> > with information about different parts of the power system, including the
> > battery, charger/plug, jack and thermal readings.
>
> ...
>
> > +#define MAX8907_CHAN(_type, _idname, _datasheet_name) { \
> > + .type = (_type), \
> > + .address = MAX8907_REG_##_idname##_MSB | \
> > + MAX8907_REG_##_idname##_MEASURE << 8, \
> > + .indexed = 1, \
> > + .channel = (_idname), \
> > + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
> > + BIT(IIO_CHAN_INFO_SCALE), \
> > + .scan_index = (_idname), \
>
> Using () means that you expect an expression as a possible input. In such
> a case the macro is buggy as it will evaluate it twice for _idname. The
> simple solution is to drop these parentheses and expect that this macro
> won't ever be used with the expressions in the parameters.
>
You have a point but current state should not cause issues as well.
> > + .scan_type = { \
> > + .format = IIO_SCAN_FORMAT_UNSIGNED_INT, \
> > + .realbits = 12, \
> > + .storagebits = 16, \
> > + .endianness = IIO_CPU, \
> > + }, \
> > + .datasheet_name = (_datasheet_name), \
> > +}
>
> ...
>
> > +static int max8907_adc_read(struct iio_dev *indio_dev,
> > + struct iio_chan_spec const *chan,
> > + int *val, int *val2, long mask)
> > +{
> > + struct regmap **regmap = iio_priv(indio_dev);
>
> Then logically better to go further and have
>
> struct regmap *map = *regmap;
>
This is matter of taste, I was inspired by MAX77541 ADC which uses
exactly this approach.
> > + __be16 data;
> > + int ret;
> > +
> > + switch (mask) {
> > + case IIO_CHAN_INFO_RAW:
> > + ret = regmap_write(*regmap, MAX8907_TO_MEASURE(chan->address), 0);
> > + if (ret)
> > + return ret;
> > +
> > + ret = regmap_bulk_read(*regmap, MAX8907_TO_REG(chan->address), &data, sizeof(data));
>
> This is a bit too long.
>
101 char, yeah 1 char too long, though maybe it is not wort resending
for a single char too long?
> ret = regmap_bulk_read(map, MAX8907_TO_REG(chan->address),
> &data, sizeof(data));
>
> > + if (ret)
> > + return ret;
> > +
> > + *val = be16_to_cpu(data) >> 4;
> > +
> > + return IIO_VAL_INT;
> > +
> > + case IIO_CHAN_INFO_SCALE:
> > + *val = max8907_default_ref[chan->channel];
> > + *val2 = chan->scan_type.realbits;
> > +
> > + return IIO_VAL_FRACTIONAL_LOG2;
> > +
> > + default:
> > + return -EINVAL;
> > + }
> > +}
>
> ...
>
> > +static int max8907_adc_init(struct regmap *regmap)
>
> Why not name it 'map'?
>
Because regmap is more explicit and is used consistently across the driver.
> > +{
> > + u32 value;
> > + int ret;
> > +
> > + /* Enable internal voltage reference. Exact purpose of bits is not known. */
> > + ret = regmap_update_bits(regmap, MAX8907_REG_TSC_CNFG1, GENMASK(1, 0), BIT(1));
>
> ret = regmap_update_bits(map, MAX8907_REG_TSC_CNFG1, GENMASK(1, 0), BIT(1));
>
which map? bitmap? statemap? or maybe geographical map?
> > + if (ret)
> > + return ret;
> > +
> > + /* Check for internal reference IRQ */
>
> Please, make style of one-line comments consistent ("all or none" are with or
> without period, same for the first letter — capital or small).
>
First comment has 2 sentences, that is why it has periods. Second only
one, so it does not need period. "same for the first letter — capital
or small" - have you even checked the patch?
> > + ret = regmap_read_poll_timeout(regmap, MAX8907_REG_TSC_STA_INT, value,
> > + !(value & MAX8907_MASK_NREF_OK), 10, 1000);
>
> ret = regmap_read_poll_timeout(map, MAX8907_REG_TSC_STA_INT, value,
> !(value & MAX8907_MASK_NREF_OK),
> 10, 1 * USEC_PER_MSEC);
>
hmm, replace 1000 (4 chars, pretty readable) with USEC_PER_MSEC (12
chars and twice wrapped line and needs tinkering or looking into
units.h), I don't think so.
> > + if (ret)
> > + return ret;
> > +
> > + return regmap_update_bits(regmap, MAX8907_REG_ADC_SCHED,
> > + MAX8907_ADC_SCHED_AUTOSCH_ENABLE |
> > + MAX8907_ADC_SCHED_AUTOSCH_TIME,
> > + MAX8907_ADC_SCHED_AUTOSCH_ENABLE);
> > +}
>
> --
> With Best Regards,
> Andy Shevchenko
>
I would like to hear what Jonathan has to say.