[PATCH v10 11/11] iio: adc: hx711: add support for HX710B

From: Piyush Patle

Date: Mon May 25 2026 - 06:54:31 EST


Add support for the AVIA HX710B ADC, which shares the HX711 GPIO
interface but uses trailing PD_SCK pulses to select the active mode.

Model the HX710B with variant-specific channel tables and IIO info,
track the active channel across conversions, and use the fixed gain
value when computing scale.

Also update the adjacent Kconfig text, file header, and module
description so the driver text matches the newly supported variant.

Signed-off-by: Piyush Patle <piyushpatle228@xxxxxxxxx>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxx>
---
drivers/iio/adc/Kconfig | 8 +-
drivers/iio/adc/hx711.c | 230 ++++++++++++++++++++++++++++++++++++----
2 files changed, 212 insertions(+), 26 deletions(-)

diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index a9dedbb8eb46..38ba8cae13f1 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -791,13 +791,15 @@ config HI8435
called hi8435.

config HX711
- tristate "AVIA HX711 ADC for weight cells"
+ tristate "AVIA HX711 and compatible ADCs"
depends on GPIOLIB
select IIO_BUFFER
select IIO_TRIGGERED_BUFFER
help
- If you say yes here you get support for AVIA HX711 ADC which is used
- for weigh cells
+ If you say Y here you get support for the following AVIA ADCs:
+ - HX710B
+ - HX711
+ which are used for bridge sensors such as weigh cells.

This driver uses two GPIOs, one acts as the clock and controls the
channel selection and gain, the other one is used for the measurement
diff --git a/drivers/iio/adc/hx711.c b/drivers/iio/adc/hx711.c
index fde95758b9a0..32a35f644834 100644
--- a/drivers/iio/adc/hx711.c
+++ b/drivers/iio/adc/hx711.c
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
- * HX711: analog to digital converter for weight sensor module
+ * HX711 and compatible ADCs driver for weigh sensor modules
*
* Copyright (c) 2016 Andreas Klinger <ak@xxxxxxxxxxxxx>
*/
@@ -15,6 +15,8 @@
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/delay.h>
+#include <linux/types.h>
+
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/iio/buffer.h>
@@ -84,12 +86,16 @@ static int hx711_get_scale_to_gain(const int *gain_scale, int scale)
* @channels: channel specification array
* @num_channels: number of entries in @channels
* @iio_info: IIO info ops for this variant
+ * @fixed_gain_val: fixed PGA gain (used when @has_fixed_gain is true)
+ * @has_fixed_gain: true if the variant has a fixed ADC gain
*/
struct hx711_chip_info {
const char *name;
const struct iio_chan_spec *channels __counted_by_ptr(num_channels);
unsigned int num_channels;
const struct iio_info *iio_info;
+ unsigned int fixed_gain_val;
+ bool has_fixed_gain;
};

struct hx711_data {
@@ -99,6 +105,8 @@ struct hx711_data {
int gain_set; /* gain set on device */
int gain_chan_a; /* gain for channel A */
int gain_scale[HX711_GAIN_MAX];
+ int channel_set; /* HX710B active channel */
+ unsigned int samp_freq; /* HX710B differential channel sample rate */
const struct hx711_chip_info *chip_info;
struct mutex lock;
/*
@@ -207,6 +215,7 @@ static int hx711_wait_for_ready(struct hx711_data *hx711_data)

static int hx711_reset(struct hx711_data *hx711_data)
{
+ const struct hx711_chip_info *info = hx711_data->chip_info;
int val;

val = hx711_wait_for_ready(hx711_data);
@@ -226,8 +235,11 @@ static int hx711_reset(struct hx711_data *hx711_data)

val = hx711_wait_for_ready(hx711_data);

- /* after a reset the gain is 128 */
- hx711_data->gain_set = HX711_RESET_GAIN;
+ if (info->has_fixed_gain)
+ hx711_data->channel_set = 0;
+ else
+ /* after a reset the gain is 128 */
+ hx711_data->gain_set = HX711_RESET_GAIN;
}

return val;
@@ -283,9 +295,51 @@ static int hx711_set_hx711_channel(struct hx711_data *hx711_data,
return 0;
}

+/*
+ * Return the trailing pulse count for an HX710B channel (Table 3 in
+ * the datasheet). The supply-monitor channel always uses 2 trailing
+ * pulses; the differential channel uses 1 (10 SPS) or 3 (40 SPS).
+ */
+static int hx710b_trailing_pulses(const struct hx711_data *hx711_data,
+ const struct iio_chan_spec *chan)
+{
+ if (!chan->differential)
+ return 2;
+ return hx711_data->samp_freq == 10 ? 1 : 3;
+}
+
+/*
+ * Switch the HX710B to the requested channel for the next conversion.
+ * channel_set tracks the trailing pulse count that was last sent to the
+ * chip; it is updated immediately after hx711_read() commits the new
+ * mode in hardware, before waiting for the chip to be ready again.
+ */
+static int hx711_set_hx710b_channel(struct hx711_data *hx711_data,
+ const struct iio_chan_spec *chan)
+{
+ int trailing_pulses = hx710b_trailing_pulses(hx711_data, chan);
+ int ret;
+
+ if (hx711_data->channel_set == trailing_pulses)
+ return 0;
+
+ ret = hx711_read(hx711_data, trailing_pulses);
+ if (ret < 0)
+ return ret;
+
+ hx711_data->channel_set = trailing_pulses;
+
+ ret = hx711_wait_for_ready(hx711_data);
+ if (ret)
+ return ret;
+
+ return 0;
+}
+
static int hx711_reset_read(struct hx711_data *hx711_data,
const struct iio_chan_spec *chan)
{
+ const struct hx711_chip_info *info = hx711_data->chip_info;
int trailing_pulses;
int ret;

@@ -298,9 +352,16 @@ static int hx711_reset_read(struct hx711_data *hx711_data,
return -EIO;
}

- ret = hx711_set_hx711_channel(hx711_data, chan, &trailing_pulses);
- if (ret < 0)
- return ret;
+ if (info->has_fixed_gain) {
+ ret = hx711_set_hx710b_channel(hx711_data, chan);
+ if (ret < 0)
+ return ret;
+ trailing_pulses = hx710b_trailing_pulses(hx711_data, chan);
+ } else {
+ ret = hx711_set_hx711_channel(hx711_data, chan, &trailing_pulses);
+ if (ret < 0)
+ return ret;
+ }

return hx711_read(hx711_data, trailing_pulses);
}
@@ -322,6 +383,9 @@ static int hx711_read_raw(struct iio_dev *indio_dev,
if (*val < 0)
return *val;
return IIO_VAL_INT;
+ case IIO_CHAN_INFO_SAMP_FREQ:
+ *val = hx711_data->samp_freq;
+ return IIO_VAL_INT;
case IIO_CHAN_INFO_SCALE:
*val = 0;
mutex_lock(&hx711_data->lock);
@@ -399,20 +463,26 @@ static irqreturn_t hx711_trigger(int irq, void *p)
struct iio_dev *indio_dev = pf->indio_dev;
struct hx711_data *hx711_data = iio_priv(indio_dev);
int i, j = 0;
+ int ret;

mutex_lock(&hx711_data->lock);

memset(&hx711_data->buffer, 0, sizeof(hx711_data->buffer));

iio_for_each_active_channel(indio_dev, i) {
- hx711_data->buffer.channel[j] =
- hx711_reset_read(hx711_data, &indio_dev->channels[i]);
- j++;
+ ret = hx711_reset_read(hx711_data, &indio_dev->channels[i]);
+ if (ret < 0) {
+ dev_err_ratelimited(hx711_data->dev,
+ "read failed: %d\n", ret);
+ goto out_unlock;
+ }
+ hx711_data->buffer.channel[j++] = ret;
}

iio_push_to_buffers_with_timestamp(indio_dev, &hx711_data->buffer,
- pf->timestamp);
+ pf->timestamp);

+out_unlock:
mutex_unlock(&hx711_data->lock);

iio_trigger_notify_done(indio_dev->trig);
@@ -462,6 +532,54 @@ static const struct iio_info hx711_iio_info = {
.attrs = &hx711_attribute_group,
};

+static const int hx710b_samp_freq_avail[] = { 10, 40 };
+
+static int hx710b_read_avail(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ const int **vals, int *type, int *length,
+ long mask)
+{
+ switch (mask) {
+ case IIO_CHAN_INFO_SAMP_FREQ:
+ *vals = hx710b_samp_freq_avail;
+ *type = IIO_VAL_INT;
+ *length = ARRAY_SIZE(hx710b_samp_freq_avail);
+ return IIO_AVAIL_LIST;
+ default:
+ return -EINVAL;
+ }
+}
+
+static int hx710b_write_raw(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ int val, int val2, long mask)
+{
+ struct hx711_data *hx711_data = iio_priv(indio_dev);
+ int ret;
+
+ switch (mask) {
+ case IIO_CHAN_INFO_SAMP_FREQ:
+ if (val != 10 && val != 40)
+ return -EINVAL;
+ ret = iio_device_claim_direct_mode(indio_dev);
+ if (ret)
+ return ret;
+ mutex_lock(&hx711_data->lock);
+ hx711_data->samp_freq = val;
+ mutex_unlock(&hx711_data->lock);
+ iio_device_release_direct_mode(indio_dev);
+ return 0;
+ default:
+ return -EINVAL;
+ }
+}
+
+static const struct iio_info hx710b_iio_info = {
+ .read_raw = hx711_read_raw,
+ .write_raw = hx710b_write_raw,
+ .read_avail = hx710b_read_avail,
+};
+
static const struct iio_chan_spec hx711_chan_spec[] = {
{
.type = IIO_VOLTAGE,
@@ -494,6 +612,51 @@ static const struct iio_chan_spec hx711_chan_spec[] = {
IIO_CHAN_SOFT_TIMESTAMP(2),
};

+/*
+ * HX710B channels (Table 3 in datasheet).
+ * 25 pulses (1 trailing): differential input, 10 SPS
+ * 26 pulses (2 trailing): DVDD-AVDD supply monitor, 40 SPS
+ * 27 pulses (3 trailing): differential input, 40 SPS
+ * Channel 0 exposes both differential rates via IIO_CHAN_INFO_SAMP_FREQ.
+ * Channel 2 is used for the supply monitor to avoid aliasing the
+ * channel2 terminal of the differential pair.
+ */
+static const struct iio_chan_spec hx710b_chan_spec[] = {
+ {
+ .type = IIO_VOLTAGE,
+ .differential = 1,
+ .channel = 0,
+ .channel2 = 1,
+ .indexed = 1,
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+ BIT(IIO_CHAN_INFO_SCALE) |
+ BIT(IIO_CHAN_INFO_SAMP_FREQ),
+ .info_mask_separate_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),
+ .scan_index = 0,
+ .scan_type = {
+ .sign = 'u',
+ .realbits = 24,
+ .storagebits = 32,
+ .endianness = IIO_CPU,
+ },
+ },
+ {
+ .type = IIO_VOLTAGE,
+ .channel = 2,
+ .indexed = 1,
+ .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),
+};
+
static const struct hx711_chip_info hx711_chip = {
.name = "hx711",
.channels = hx711_chan_spec,
@@ -501,6 +664,15 @@ static const struct hx711_chip_info hx711_chip = {
.num_channels = ARRAY_SIZE(hx711_chan_spec),
};

+static const struct hx711_chip_info hx710b_chip = {
+ .name = "hx710b",
+ .channels = hx710b_chan_spec,
+ .iio_info = &hx710b_iio_info,
+ .num_channels = ARRAY_SIZE(hx710b_chan_spec),
+ .fixed_gain_val = 128,
+ .has_fixed_gain = true,
+};
+
static int hx711_probe(struct platform_device *pdev)
{
const struct hx711_chip_info *chip_info;
@@ -543,32 +715,43 @@ static int hx711_probe(struct platform_device *pdev)
return dev_err_probe(dev, PTR_ERR(hx711_data->gpiod_dout),
"failed to get dout-gpiod\n");

- ret = devm_regulator_get_enable_read_voltage(dev, "avdd");
+ if (chip_info->has_fixed_gain)
+ ret = devm_regulator_get_enable_read_voltage(dev, "vref");
+ else
+ ret = devm_regulator_get_enable_read_voltage(dev, "avdd");
if (ret < 0)
return ret;

/*
- * with
- * full scale differential input range: AVDD / GAIN
+ * With
+ * full scale differential input range: reference / GAIN
* full scale output data: 2^24
* we can say:
- * AVDD / GAIN = 2^24
+ * reference / GAIN = 2^24
* therefore:
- * 1 LSB = AVDD / GAIN / 2^24
- * AVDD is in uV, but we need 10^-9 mV
+ * 1 LSB = reference / GAIN / 2^24
+ * reference is in uV, but we need 10^-9 mV
* approximately to fit into a 32 bit number:
- * 1 LSB = (AVDD * 100) / GAIN / 1678 [10^-9 mV]
+ * 1 LSB = (reference * 100) / GAIN / 1678 [10^-9 mV]
*/

/* we need 10^-9 mV */
ret *= 100;

- for (i = 0; i < HX711_GAIN_MAX; i++)
- hx711_data->gain_scale[i] =
- ret / hx711_gain_to_scale[i].gain / 1678;
+ if (chip_info->has_fixed_gain) {
+ for (i = 0; i < HX711_GAIN_MAX; i++)
+ hx711_data->gain_scale[i] =
+ ret / chip_info->fixed_gain_val / 1678;
+ hx711_data->gain_set = chip_info->fixed_gain_val;
+ hx711_data->samp_freq = 10;
+ } else {
+ for (i = 0; i < HX711_GAIN_MAX; i++)
+ hx711_data->gain_scale[i] =
+ ret / hx711_gain_to_scale[i].gain / 1678;

- hx711_data->gain_set = 128;
- hx711_data->gain_chan_a = 128;
+ hx711_data->gain_set = 128;
+ hx711_data->gain_chan_a = 128;
+ }

hx711_data->clock_frequency = 400000;
ret = device_property_read_u32(&pdev->dev, "clock-frequency",
@@ -607,6 +790,7 @@ static int hx711_probe(struct platform_device *pdev)
}

static const struct of_device_id of_hx711_match[] = {
+ { .compatible = "avia,hx710b", .data = &hx710b_chip },
{ .compatible = "avia,hx711", .data = &hx711_chip },
{ }
};
@@ -624,7 +808,7 @@ static struct platform_driver hx711_driver = {
module_platform_driver(hx711_driver);

MODULE_AUTHOR("Andreas Klinger <ak@xxxxxxxxxxxxx>");
-MODULE_DESCRIPTION("HX711 bitbanging driver - ADC for weight cells");
+MODULE_DESCRIPTION("HX711 and similar bitbanging ADC driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:hx711-gpio");

--
2.43.0