Re: [PATCH v2 2/2] iio: adc: hx711: add support for HX710B
From: Piyush Patle
Date: Wed Apr 22 2026 - 01:23:26 EST
On Tue, Apr 21, 2026 at 8:47 PM Jonathan Cameron <jic23@xxxxxxxxxx> wrote:
>
> On Sun, 19 Apr 2026 23:16:40 +0530
> Piyush Patle <piyushpatle228@xxxxxxxxx> wrote:
>
> > The HX711 uses trailing SCK pulses after each 24-bit conversion to
> > select the channel and gain for the next measurement: 1 pulse gives
> > channel A at gain 128, 2 pulses give channel B at gain 32, and 3 pulses
> > give channel A at gain 64.
> >
> > The HX710B works differently: gain is fixed at 128 and the trailing
> > pulses select only the channel. One trailing pulse selects the
> > differential input (channel 0, 10 SPS) and two trailing pulses select
> > the DVDD-AVDD supply monitor (channel 1, 40 SPS).
> >
> > Refactor the driver around a per-chip hx711_chip_info structure so both
> > variants can share the same core. Each chip provides its own
> > iio_chan_spec array and iio_info pointer. The HX710B stores per-channel
> > trailing pulse counts in chan->address (1 for channel 0, 2 for
> > channel 1) instead of a separate array. A bool fixed_gain flag and
> > fixed_gain_val field in hx711_chip_info distinguish the fixed-gain path
> > from the HX711's user-selectable gain path without conflating unrelated
> > properties. The HX710B differential input channel is described with
> > .differential=1 and .channel2=1 as required by the IIO ABI.
> >
> > Signed-off-by: Piyush Patle <piyushpatle228@xxxxxxxxx>
>
> I'm slowly incorporating looking at the feedback that the sashiko LLM based
> code review is providing. In amongst some stuff I'm fairly sure was
> garbage it did raise some stuff that needs a closer look. See inline...
>
> > @@ -246,27 +275,50 @@ static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)
> > return 0;
> > }
> >
> > -static int hx711_reset_read(struct hx711_data *hx711_data, int chan)
> > +/* Select HX710B channel for the next conversion. */
> > +static int hx710b_set_channel(struct hx711_data *hx711_data,
> > + const struct iio_chan_spec *chan)
> > {
> > int ret;
> > - int val;
> >
> > - /*
> > - * hx711_reset() must be called from here
> > - * because it could be calling hx711_read() by itself
> > - */
> > + if (hx711_data->channel_set == chan->channel)
> > + return 0;
> > +
> > + hx711_data->channel_set = chan->channel;
> Sashiko raises what sound like a plausible issue here. If the read
> that follows fails is the current channel updated? I.e. should we
> set this or not?
> https://sashiko.dev/#/patchset/20260419174654.683692-1-piyushpatle228%40gmail.com
> (be careful with these AI reviews. Some of the other things it says are - I think
> not true!)
Some of the stuff by sashiko really seems to be garbage here ,still i think
right now channel_set is updated before the read completes, so in case
of a failure the cached state could become inconsistent with the hardware
I'll rework this so the channel state is only updated after a successful
read + wait_for_ready() sequence.
>
> > +
> > + ret = hx711_read(hx711_data, chan->address);
> > + if (ret < 0)
> > + return ret;
> > +
> > + return hx711_wait_for_ready(hx711_data);
> > +}
>
> > static const struct iio_chan_spec hx711_chan_spec[] = {
> > {
> > .type = IIO_VOLTAGE,
> > @@ -455,10 +516,69 @@ static const struct iio_chan_spec hx711_chan_spec[] = {
> > IIO_CHAN_SOFT_TIMESTAMP(2),
> > };
> >
> > +/*
> > + * HX710B channels.
> > + * Channel 0: differential input (IN+ vs IN-), 10 SPS, 1 trailing pulse.
> > + * Channel 1: DVDD-AVDD supply monitor, 40 SPS, 2 trailing pulses.
> This triggered a sashiko question...
> https://sashiko.dev/#/patchset/20260419174654.683692-1-piyushpatle228%40gmail.com
>
> Specifically if the fixed scale is correct for the supply monitor or not.
> It seems unlikely we'd want a large gain on a supply monitor.
>
okay, Agreed, this needs a closer look.
Currently I am using a single fixed gain derived scale for both channels.
I'll verify this against the datasheet and measurements for the supply
monitor channel and adjust the scale handling if required.
I'll address both issues in v3.
>
> > + * .address holds the trailing pulse count used by hx710b_set_channel().
> > + */
> > +static const struct iio_chan_spec hx710b_chan_spec[] = {
> > + {
> > + .type = IIO_VOLTAGE,
> > + .differential = 1,
> > + .channel = 0,
> > + .channel2 = 1,
> > + .indexed = 1,
> > + .address = 1,
> > + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> > + BIT(IIO_CHAN_INFO_SCALE),
> > + .scan_index = 0,
> > + .scan_type = {
> > + .sign = 'u',
> > + .realbits = 24,
> > + .storagebits = 32,
> > + .endianness = IIO_CPU,
> > + },
> > + },
> > + {
> > + .type = IIO_VOLTAGE,
> > + .channel = 1,
> > + .indexed = 1,
> > + .address = 2,
> > + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> > + BIT(IIO_CHAN_INFO_SCALE),
> > + .scan_index = 1,
> > + .scan_type = {
> > + .sign = 'u',
> > + .realbits = 24,
> > + .storagebits = 32,
> > + .endianness = IIO_CPU,
> > + },
> > + },
> > + IIO_CHAN_SOFT_TIMESTAMP(2),
> > +};