Re: [PATCH v2 5/5] iio: adc: ad7606: add gain calibration support

From: Andy Shevchenko
Date: Fri May 02 2025 - 09:38:00 EST


On Fri, May 02, 2025 at 03:27:02PM +0200, Angelo Dureghello wrote:
> From: Angelo Dureghello <adureghello@xxxxxxxxxxxx>
>
> Add gain calibration support, using resistor values set on devicetree,
> values to be set accordingly with ADC external RFilter, as explained in
> the ad7606c-16 datasheet, rev0, page 37.
>
> Usage example in the fdt yaml documentation.

...

> + device_for_each_child_node_scoped(dev, child) {
> + int reg, r_gain;

Both are defined in DT as unsigned. Are you able to use int as u32 in DT compiler?

> + ret = fwnode_property_read_u32(child, "reg", &reg);
> + if (ret)
> + return ret;
> +
> + /* channel number (here) is from 1 to num_channels */
> + if (reg < 1 || reg > num_channels) {
> + dev_warn(dev, "invalid ch number (ignoring): %d\n", reg);
> + continue;
> + }
> +
> + ret = fwnode_property_read_u32(child, "adi,rfilter-ohms",
> + &r_gain);
> + if (ret == -EINVAL)
> + /* Keep the default register value. */
> + continue;
> + if (ret)
> + return ret;

> + if (r_gain < AD7606_CALIB_GAIN_MIN ||
> + r_gain > AD7606_CALIB_GAIN_MAX)

Seems like minimum check is not needed. See above why.

> + return dev_err_probe(st->dev, -EINVAL,
> + "wrong gain calibration value.");
> +
> + /* Chan reg is 1-based index. */
> + ret = st->bops->reg_write(st, AD7606_CALIB_GAIN(reg - 1),
> + r_gain / AD7606_CALIB_GAIN_STEP);

Wonder if we need DIV_ROUND_CLOSEST() instead...

> + if (ret)
> + return ret;
> + }

--
With Best Regards,
Andy Shevchenko