iio: adc: hx711: add HX710B support
v4 cover iio: adc: hx711: add HX710B support
Add support for the HX710B ADC, a variant of the HX711 with the same GPIO interface but a different channel and gain model.
The first patch updates the devicetree binding to add the avia,hx710b compatible, document the variant-specific hardware behaviour, and add chip-specific supply properties (avdd-supply, dvdd-supply, vsup-supply, vref-supply), the RATE pin for the HX711, and allOf constraints forbidding HX711-only properties on HX710B nodes and vice versa.
The second patch fixes a pre-existing bug: hx711_gain_to_scale[].scale is a global array overwritten on each probe call, which breaks multiple sensor instances that have different AVDD supplies. This is fixed by moving the scale into a per-device array.
Patches 3-6 split the v3 refactor into smaller independent steps as requested by Andy Shevchenko: text cleanup (Kconfig, header, MODULE_DESCRIPTION), chip_info introduction, trailing-pulse parametrisation of hx711_read(), and iio_chan_spec * signature for hx711_reset_read().
The final patch adds HX710B support using that infrastructure. Trailing pulse counts from Table 3 of the HX710B datasheet are stored in chan->address. Three channels are exposed: two differential inputs (10 SPS and 40 SPS) and a DVDD-AVDD supply monitor (40 SPS). The driver probes vref-supply first for the ADC reference and falls back to avdd-supply when the VREF pin is tied to AVDD on the board.
Tested on PocketBeagle2 with an HX710B breakout module. The device probed successfully, all three channels returned stable raw readings, and alternating reads confirmed channel switching between the differential input and the DVDD-AVDD supply monitor path.
Build-tested with:
- make -s W=1 drivers/iio/adc/hx711.o
- pahole -C hx711_chip_info drivers/iio/adc/hx711.o
pahole reports hx711_chip_info as 40 bytes with no internal holes. The remaining 7 bytes are tail padding from 8-byte pointer alignment.
Changes in v4:
- Fix pre-existing global scale mutation bug (new patch 2).
- Split the v3 refactor patch into four independent patches (patches 3-6) as requested by Andy Shevchenko.
- Keep hx711_chip_info fields in their final order from introduction; the HX710B patch only appends new fields without reordering.
- Add vref-supply binding and probe path for HX710B VREF pin.
- Add a third HX710B channel (27 pulses, differential 40 SPS) from Table 3 of the datasheet.
- Use .channel = 2 for the supply monitor to avoid indexing clash with .channel2 = 1 of the differential pair.
- Remove dvdd-supply from the HX710B forbidden list in the binding; the HX710B has a DVDD supply and the DVDD-AVDD channel relies on it.
- Add NULL guard on device_get_match_data() return value.
- Fix checkpatch alignment in multi-line hx711_read() calls.
Changes in v3:
- See individual patch changelogs for full details.
- dt-bindings: remove vref-supply wording; drop repeated clock-frequency default text; restore example node name; remove the extra HX710B example
- driver: split the hx711_chip_info refactor from the HX710B support patch; fix signedness; update channel_set only after successful channel switching; keep HX710B scale based on the documented fixed gain of 128; add direct ARRAY_SIZE/types includes; use pahole to verify and improve hx711_chip_info field ordering
- Reworked Kconfig text to use "HX711 and compatible ADCs" and list supported AVIA ADCs one per line.
- Restored/updated driver header and module description wording while keeping the bitbanging context.
Changes in v2:
- See individual patch changelogs for full details.
- dt-bindings: add dvdd-supply, vsup-supply, rate-gpios; allOf if/then for HX710B; fix clock-frequency description; remove dead vendor URL; clarify AVDD as voltage reference on both chips
- driver: fix pulse count bug ({25,26}->{1,2}); move counts to chan->address; add fixed_gain flag; add .differential/.channel2; remove NULL check; drop reset_channel; pass iio_chan_spec * directly
Piyush Patle (7):
dt-bindings: iio: adc: avia-hx711: add avia,hx710b compatible
iio: adc: hx711: move scale computation to per-device storage
iio: adc: hx711: update Kconfig, module description and file header
iio: adc: hx711: introduce hx711_chip_info per-variant structure
iio: adc: hx711: pass trailing pulse count into hx711_read()
iio: adc: hx711: pass iio_chan_spec to hx711_reset_read()
iio: adc: hx711: add support for HX710B
.../bindings/iio/adc/avia-hx711.yaml | 82 +++++- drivers/iio/adc/Kconfig | 8 +- drivers/iio/adc/hx711.c | 278 ++++++++++++++---- 3 files changed, 297 insertions(+), 71 deletions(-)
Signature · 1 line
2.43.0
PATCH v4 1/7 dt-bindings: iio: adc: avia-hx711: add avia,hx710b compatible
The HX710B shares the same two-wire interface as the HX711 but differs in its channel and gain model: gain is fixed at 128 and the number of trailing PD_SCK pulses selects the input channel rather than the gain.
Add avia,hx710b to the compatible enum. Document the chip differences in the description and add chip-specific supply properties (dvdd-supply, vsup-supply, vref-supply) and a rate-gpios property for the HX711 RATE pin. Add allOf constraints that forbid HX711-only properties on HX710B nodes and vice versa. Clarify the clock-frequency description to reflect its actual purpose: controlling the SCK bit-bang timing.
---
Changes in v4:
- Add vref-supply for the HX710B VREF reference voltage pin.
- Remove dvdd-supply from the HX710B forbidden properties list; the HX710B has a DVDD supply and the DVDD-AVDD channel relies on it.
- Add allOf block forbidding vref-supply on HX711 nodes.
- Add an HX710B example showing vref-supply.
- Update description: avoid specific channel-number references in hardware text.
Changes in v3:
- Drop the vref-supply mention from avdd-supply; no such binding property exists.
- Drop the clock-frequency sentence that repeated the schema default.
- Restore the example node name to weight.
- Remove the separate HX710B example.
Changes in v2:
- Remove driver implementation details from the description and describe hardware behaviour only.
- Drop unrelated punctuation cleanup.
- Add dvdd-supply and vsup-supply optional properties for HX711.
- Add rate-gpios optional property for the HX711 RATE pin and forbid it on HX710B.
- Add the allOf if/then block forbidding HX711-only properties on HX710B nodes.
- Clarify clock-frequency as SCK bit-bang timing, not a crystal or external clock input.
- Sort compatible enum alphabetically.
- Remove redundant example comments.
- Update the HX711 example to exercise rate-gpios.
--- .../bindings/iio/adc/avia-hx711.yaml | 82 +++++++++++++++---- 1 file changed, 67 insertions(+), 15 deletions(-)
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +67 -15
@@ -4,49 +4,91 @@ $id: http://devicetree.org/schemas/iio/adc/avia-hx711.yaml# $schema: http://devicetree.org/meta-schemas/core.yaml# -title: AVIA HX711 ADC chip for weight cells +title: AVIA HX711 and HX710B ADCs maintainers: - Andreas Klinger <[email protected]> description: | - Bit-banging driver using two GPIOs: - - sck-gpio gives a clock to the sensor with 24 cycles for data retrieval - and up to 3 cycles for selection of the input channel and gain for the - next measurement - - dout-gpio is the sensor data the sensor responds to the clock + The HX711 is a 24-bit ADC with selectable gain (32/64/128) and two + differential input channels. Channel A supports gain 64 and 128; + channel B supports gain 32. - Specifications about the driver can be found at: - http://www.aviaic.com/ENProducts.aspx + The HX710B is a 24-bit ADC with fixed gain of 128. One input measures + the differential voltage between the two input pins; a second measures + the DVDD-AVDD supply voltage difference for battery level detection. properties: compatible: enum: + - avia,hx710b - avia,hx711 sck-gpios: description: - Definition of the GPIO for the clock (output). In the datasheet it is - named PD_SCK + GPIO for the clock output (PD_SCK in the datasheet). maxItems: 1 dout-gpios: description: - Definition of the GPIO for the data-out sent by the sensor in - response to the clock (input). - See Documentation/devicetree/bindings/gpio/gpio.txt for information - on how to specify a consumer gpio. + GPIO for the data output from the sensor (DOUT in the datasheet). maxItems: 1 avdd-supply: description: - Definition of the regulator used as analog supply + Analog supply voltage (AVDD). Also serves as the voltage reference on + both chips. + + dvdd-supply: + description: + Digital supply voltage (DVDD). For the HX710B, DVDD must be greater + than or equal to AVDD. When DVDD is a battery rail and AVDD is a + regulated supply, one channel monitors the DVDD-AVDD difference for + battery level detection. + + vsup-supply: + description: + Supply voltage for the on-chip regulator (VSUP). HX711 only. + + vref-supply: + description: + Reference voltage input (VREF). HX710B only. When omitted, the driver + assumes VREF is tied to AVDD on the board. + + rate-gpios: + description: + GPIO connected to the RATE pin (HX711 only). When driven low the + output data rate is 10 SPS; when driven high it is 80 SPS. If + omitted the RATE pin state is determined by the board wiring. + maxItems: 1 clock-frequency: + description: + Controls the SCK bit-bang timing. The value is used to derive the + delay between SCK edges; keep the SCK high time below 60 us to + avoid triggering chip power-down mode. minimum: 20000 maximum: 2500000 default: 400000 +allOf: + - if: + properties: + compatible: + const: avia,hx710b + then: + properties: + vsup-supply: false + rate-gpios: false + + - if: + properties: + compatible: + const: avia,hx711 + then: + properties: + vref-supply: false + required: - compatible - sck-gpios
@@ -62,6 +104,16 @@ examples: compatible = "avia,hx711"; sck-gpios = <&gpio3 10 GPIO_ACTIVE_HIGH>; dout-gpios = <&gpio0 7 GPIO_ACTIVE_HIGH>; + rate-gpios = <&gpio0 9 GPIO_ACTIVE_HIGH>; avdd-supply = <&avdd>; clock-frequency = <100000>; }; + - | + #include <dt-bindings/gpio/gpio.h> + weight { + compatible = "avia,hx710b"; + sck-gpios = <&gpio3 10 GPIO_ACTIVE_HIGH>; + dout-gpios = <&gpio0 7 GPIO_ACTIVE_HIGH>; + avdd-supply = <&avdd>; + vref-supply = <&vref>; + };
Signature · 1 line
2.43.0
PATCH v4 2/7 iio: adc: hx711: move scale computation to per-device storage
In reply to Piyush PatleThe gain-to-scale table hx711_gain_to_scale[] is a global array whose .scale fields are overwritten in hx711_probe() using the AVDD voltage read at probe time. When two HX711 sensors are connected to supplies at different voltages, the second probe call overwrites the scale values computed for the first sensor, silently corrupting its readings.
Fix this by removing the .scale field from the global table, making the table const, and adding a per-instance gain_scale[] array to hx711_data. Populate gain_scale[] in hx711_probe() using the device's own AVDD regulator voltage. Update hx711_get_gain_to_scale() and hx711_get_scale_to_gain() to take the per-instance array as a parameter, and update hx711_scale_available_show() to retrieve it via iio_priv().
No functional change for single-sensor configurations.
---
Changes in v4:
- New patch. Fixes a pre-existing bug reported during v3 review: the global hx711_gain_to_scale[].scale is overwritten on each probe call, breaking multiple sensor instances with different AVDD supplies.
--- drivers/iio/adc/hx711.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-)
drivers/iio/adc/hx711.c +17 -15
@@ -28,22 +28,20 @@struct hx711_gain_to_scale {int gain;int gain_pulse; -int scale;int channel; };/* * .scale depends on AVDD which in turn is known as soon as the regulator -* is available -* therefore we set .scale in hx711_probe() +* is available; it is stored per device in hx711_data.gain_scale[] * * channel A in documentation is channel 0 in source code * channel B in documentation is channel 1 in source code */ -static struct hx711_gain_to_scale hx711_gain_to_scale[HX711_GAIN_MAX ] = { - {128 ,1 ,0 ,0 }, - {32 ,2 ,0 ,1 }, - {64 ,3 ,0 ,0 } +static const struct hx711_gain_to_scale hx711_gain_to_scale[HX711_GAIN_MAX ] = { + {128 ,1 ,0 }, + {32 ,2 ,1 }, + {64 ,3 ,0 } };static int hx711_get_gain_to_pulse (int gain)
@@ -56,22 +54,22 @@ static int hx711_get_gain_to_pulse(int gain)return 1 ; } -static int hx711_get_gain_to_scale (int gain) +static int hx711_get_gain_to_scale (const int *gain_scale,int gain) {int i;for (i =0 ; i <HX711_GAIN_MAX ; i++)if (hx711_gain_to_scale[i].gain == gain) -return hx711_gain_to_scale[i].scale; +return gain_scale[i];return 0 ; } -static int hx711_get_scale_to_gain (int scale) +static int hx711_get_scale_to_gain (const int *gain_scale,int scale) {int i;for (i =0 ; i <HX711_GAIN_MAX ; i++) -if (hx711_gain_to_scale[i].scale == scale) +if (gain_scale[i] == scale)return hx711_gain_to_scale[i].gain;return -EINVAL ; }
@@ -82,6 +80,7 @@ struct hx711_data {struct gpio_desc *gpiod_dout;int gain_set;/* gain set on device */ int gain_chan_a;/* gain for channel A */ +int gain_scale[HX711_GAIN_MAX ];struct mutex lock;/* * triggered buffer
@@ -290,7 +289,8 @@ static int hx711_read_raw(struct iio_dev *indio_dev, *val =0 ;mutex_lock (&hx711_data->lock); - *val2 =hx711_get_gain_to_scale (hx711_data->gain_set); + *val2 =hx711_get_gain_to_scale (hx711_data->gain_scale, + hx711_data->gain_set);mutex_unlock (&hx711_data->lock);
@@ -321,7 +321,7 @@ static int hx711_write_raw(struct iio_dev *indio_dev,mutex_lock (&hx711_data->lock); - gain =hx711_get_scale_to_gain (val2); + gain =hx711_get_scale_to_gain (hx711_data->gain_scale, val2);if (gain <0 ) {mutex_unlock (&hx711_data->lock);return gain;
@@ -388,12 +388,14 @@ static ssize_t hx711_scale_available_show(struct device *dev, {struct iio_dev_attr *iio_attr =to_iio_dev_attr (attr);int channel = iio_attr->address; +struct hx711_data *hx711_data = +iio_priv (dev_to_iio_dev (dev));int i, len =0 ;for (i =0 ; i <HX711_GAIN_MAX ; i++)if (hx711_gain_to_scale[i].channel == channel) len +=sprintf (buf + len,"0.%09d " , - hx711_gain_to_scale[i].scale); + hx711_data->gain_scale[i]); len +=sprintf (buf + len,"\n" );
@@ -511,7 +513,7 @@ static int hx711_probe(struct platform_device *pdev) ret *=100 ;for (i =0 ; i <HX711_GAIN_MAX ; i++) - hx711_gain_to_scale[i].scale = + hx711_data->gain_scale[i] = ret / hx711_gain_to_scale[i].gain /1678 ; hx711_data->gain_set =128 ;
Signature · 1 line
2.43.0
PATCH v4 3/7 iio: adc: hx711: update Kconfig, module description and file header
In reply to Piyush PatleUpdate the Kconfig entry to "AVIA HX711 and compatible ADCs" and list supported chips one per line to make it easy to extend when new variants are added. Change the module description and file header to match.
Add direct includes for linux/array_size.h and linux/types.h rather than relying on transitive includes pulled in by other headers.
---
Changes in v4:
- New patch. Split out from the v3 refactor patch as requested by Andy Shevchenko so that text/description changes are separate from code refactoring.
- Add direct linux/array_size.h and linux/types.h includes.
--- drivers/iio/adc/Kconfig | 7 ++++--- drivers/iio/adc/hx711.c | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-)
drivers/iio/adc/Kconfig +4 -3
@@ -784,13 +784,14 @@ 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: + - 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
drivers/iio/adc/hx711.c +4 -2
@@ -1,12 +1,14 @@// SPDX-License-Identifier: GPL-2.0-or-later /* -* HX711: analog to digital converter for weight sensor module +* HX711 and compatible ADCs driver for weight sensor modules * * Copyright (c) 2016 Andreas Klinger <[email protected]> */ +#include <linux/array_size.h> #include <linux/err.h> #include <linux/kernel.h> #include <linux/module.h> +#include <linux/types.h> #include <linux/mod_devicetable.h> #include <linux/platform_device.h> #include <linux/property.h>
@@ -573,7 +575,7 @@ static struct platform_driver hx711_driver = {module_platform_driver (hx711_driver);MODULE_AUTHOR ("Andreas Klinger <[email protected]>" ); -MODULE_DESCRIPTION ("HX711 bitbanging driver - ADC for weight cells" ); +MODULE_DESCRIPTION ("HX711 and compatible bitbanging ADC driver" );MODULE_LICENSE ("GPL" );MODULE_ALIAS ("platform:hx711-gpio" );
Signature · 1 line
2.43.0
PATCH v4 4/7 iio: adc: hx711: introduce hx711_chip_info per-variant structure
In reply to Piyush PatleAdd struct hx711_chip_info to hold per-variant static configuration:
device name, IIO channel spec array, IIO info ops pointer, and channel count. Store a chip_info pointer in hx711_data and populate indio_dev fields from it at probe time instead of hardcoding them.
Use device_get_match_data() to look up the chip_info pointer from the of_device_id table. Guard against a NULL return so that the driver fails cleanly if probed via the platform name match path without associated match data.
No functional change for existing HX711 users.
---
Changes in v4:
- New patch. Split out from the v3 refactor patch as requested by Andy Shevchenko.
- Place hx711_chip_info fields in their final order from the start so that the HX710B support patch (which adds fixed_gain/fixed_gain_val) appends new fields without reordering existing ones.
- Add NULL guard on device_get_match_data() return value to handle the platform alias match path that can bind without match data.
--- drivers/iio/adc/hx711.c | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/iio/adc/hx711.c +33 -5
@@ -76,6 +76,20 @@ static int hx711_get_scale_to_gain(const int *gain_scale, int scale)return -EINVAL ; } +/** +* struct hx711_chip_info - per-variant static configuration +* @name: IIO device name +* @channels: channel specification array +* @iio_info: IIO info ops for this variant +* @num_channels: number of entries in @channels +*/ +struct hx711_chip_info { +const char *name; +const struct iio_chan_spec *channels; +const struct iio_info *iio_info; +unsigned int num_channels; +}; +struct hx711_data {struct device *dev;struct gpio_desc *gpiod_pd_sck;
@@ -83,6 +97,7 @@ struct hx711_data {int gain_set;/* gain set on device */ int gain_chan_a;/* gain for channel A */ int gain_scale[HX711_GAIN_MAX ]; +const struct hx711_chip_info *chip_info;struct mutex lock;/* * triggered buffer
@@ -459,10 +474,18 @@ static const struct iio_chan_spec hx711_chan_spec[] = { IIO_CHAN_SOFT_TIMESTAMP(2), }; +static const struct hx711_chip_info hx711_chip = { + .name ="hx711" , + .channels = hx711_chan_spec, + .iio_info = &hx711_iio_info, + .num_channels =ARRAY_SIZE (hx711_chan_spec), +}; +static int hx711_probe (struct platform_device *pdev) {struct device *dev = &pdev->dev;struct hx711_data *hx711_data; +const struct hx711_chip_info *chip_info;struct iio_dev *indio_dev;int ret;int i;
@@ -476,6 +499,11 @@ static int hx711_probe(struct platform_device *pdev)mutex_init (&hx711_data->lock); + chip_info =device_get_match_data (dev); +if (!chip_info) +return dev_err_probe (dev, -ENODEV ,"missing match data\n" ); + hx711_data->chip_info = chip_info; +/* * PD_SCK stands for power down and serial clock input of HX711 * in the driver it is an output
@@ -537,11 +565,11 @@ static int hx711_probe(struct platform_device *pdev) hx711_data->data_ready_delay_ns =1000000000 / hx711_data->clock_frequency; - indio_dev->name ="hx711" ; - indio_dev->info = &hx711_iio_info; + indio_dev->name = chip_info->name; + indio_dev->info = chip_info->iio_info; indio_dev->modes =INDIO_DIRECT_MODE ; - indio_dev->channels = hx711_chan_spec; - indio_dev->num_channels =ARRAY_SIZE (hx711_chan_spec); + indio_dev->channels = chip_info->channels; + indio_dev->num_channels = chip_info->num_channels; ret = devm_iio_triggered_buffer_setup(dev, indio_dev, iio_pollfunc_store_time,
@@ -558,7 +586,7 @@ static int hx711_probe(struct platform_device *pdev) }static const struct of_device_id of_hx711_match[] = { - { .compatible ="avia,hx711" , }, + { .compatible ="avia,hx711" , .data = &hx711_chip }, { } };
Signature · 1 line
2.43.0
PATCH v4 5/7 iio: adc: hx711: pass trailing pulse count into hx711_read()
In reply to Piyush PatleMove the trailing-pulse computation out of hx711_read() and into the callers. hx711_read() now takes an explicit 'trailing_pulses' parameter so the HX710B support patch can pass the per-channel count stored in chan->address without adding a separate code path in hx711_read().
Use scoped loop variables (for (int i = ...) / for (unsigned int i = ...)) to tighten the scope of the loop counters.
No functional change.
---
Changes in v4:
- New patch. Split out from the v3 refactor patch as requested by Andy Shevchenko.
--- drivers/iio/adc/hx711.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-)
drivers/iio/adc/hx711.c +13 -11
@@ -155,9 +155,9 @@ static int hx711_cycle(struct hx711_data *hx711_data)return gpiod_get_value (hx711_data->gpiod_dout); } -static int hx711_read (struct hx711_data *hx711_data) +static int hx711_read (struct hx711_data *hx711_data,unsigned int trailing_pulses) { -int i, ret; +int ret;int value =0 ;int val =gpiod_get_value (hx711_data->gpiod_dout);
@@ -165,7 +165,7 @@ static int hx711_read(struct hx711_data *hx711_data)if (val)return -EIO ; -for (i =0 ; i <24 ; i++) { +for (int i =0 ; i <24 ; i++) { value <<=1 ; ret =hx711_cycle (hx711_data);if (ret)
@@ -174,7 +174,7 @@ static int hx711_read(struct hx711_data *hx711_data) value ^=0x800000 ; -for (i =0 ; i <hx711_get_gain_to_pulse (hx711_data->gain_set); i++) +for (unsigned int i =0 ; i < trailing_pulses; i++)hx711_cycle (hx711_data);return value;
@@ -237,7 +237,8 @@ static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)if (hx711_data->gain_set ==32 ) { hx711_data->gain_set = hx711_data->gain_chan_a; - ret =hx711_read (hx711_data); + ret =hx711_read (hx711_data, +hx711_get_gain_to_pulse (hx711_data->gain_set));if (ret <0 )return ret;
@@ -249,7 +250,8 @@ static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)if (hx711_data->gain_set !=32 ) { hx711_data->gain_set =32 ; - ret =hx711_read (hx711_data); + ret =hx711_read (hx711_data, +hx711_get_gain_to_pulse (hx711_data->gain_set));if (ret <0 )return ret;
@@ -264,8 +266,8 @@ static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)static int hx711_reset_read (struct hx711_data *hx711_data,int chan) { +unsigned int trailing_pulses;int ret; -int val;/* * hx711_reset() must be called from here
@@ -280,9 +282,8 @@ static int hx711_reset_read(struct hx711_data *hx711_data, int chan)if (ret <0 )return ret; - val =hx711_read (hx711_data); - -return val; + trailing_pulses =hx711_get_gain_to_pulse (hx711_data->gain_set); +return hx711_read (hx711_data, trailing_pulses); } static int hx711_read_raw(struct iio_dev *indio_dev,
@@ -349,7 +350,8 @@ static int hx711_write_raw(struct iio_dev *indio_dev,if (gain !=32 ) hx711_data->gain_chan_a = gain; - ret =hx711_read (hx711_data); + ret =hx711_read (hx711_data, +hx711_get_gain_to_pulse (hx711_data->gain_set));if (ret <0 ) {mutex_unlock (&hx711_data->lock);return ret;
Signature · 1 line
2.43.0
PATCH v4 6/7 iio: adc: hx711: pass iio_chan_spec to hx711_reset_read()
In reply to Piyush PatleChange hx711_reset_read() to accept a const struct iio_chan_spec * instead of a plain integer channel index. This lets the HX710B support patch pass the full channel descriptor (including chan->address for the trailing pulse count) without adding a wrapper or a separate code path for each call site.
Update the two callers: hx711_read_raw() already has the iio_chan_spec * from its own parameter, and hx711_trigger() passes &indio_dev->channels[i].
No functional change.
---
Changes in v4:
- New patch. Split out from the v3 refactor patch as requested by Andy Shevchenko.
--- drivers/iio/adc/hx711.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/iio/adc/hx711.c +5 -4
@@ -264,7 +264,8 @@ 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) +static int hx711_reset_read (struct hx711_data *hx711_data, +const struct iio_chan_spec *chan) {unsigned int trailing_pulses;int ret;
@@ -278,7 +279,7 @@ static int hx711_reset_read(struct hx711_data *hx711_data, int chan)return -EIO ; } - ret =hx711_set_gain_for_channel (hx711_data, chan); + ret =hx711_set_gain_for_channel (hx711_data, chan->channel);if (ret <0 )return ret;
@@ -296,7 +297,7 @@ static int hx711_read_raw(struct iio_dev *indio_dev,case IIO_CHAN_INFO_RAW :mutex_lock (&hx711_data->lock); - *val =hx711_reset_read (hx711_data, chan->channel); + *val =hx711_reset_read (hx711_data, chan);mutex_unlock (&hx711_data->lock);
@@ -387,7 +388,7 @@ static irqreturn_t hx711_trigger(int irq, void *p)iio_for_each_active_channel (indio_dev, i) { hx711_data->buffer.channel[j] =hx711_reset_read (hx711_data, - indio_dev->channels[i].channel); + &indio_dev->channels[i]); j++; }
Signature · 1 line
2.43.0
PATCH v4 7/7 iio: adc: hx711: add support for HX710B
In reply to Piyush PatleAdd support for the AVIA HX710B ADC, which shares the HX711 GPIO interface but has a fixed gain of 128 and uses trailing PD_SCK pulses to select the active channel rather than the gain.
The HX710B has three operating modes controlled by the trailing pulse count after the 24 data bits (Table 3 in the HX710B datasheet):
25 pulses (1 trailing): differential input at 10 SPS
26 pulses (2 trailing): DVDD-AVDD supply monitor at 40 SPS 27 pulses (3 trailing): differential input at 40 SPS
Model the HX710B with its own hx710b_chan_spec[] and hx710b_iio_info. Store the trailing pulse count in chan->address so hx710b_set_channel() can switch channels without a separate lookup table. The supply monitor uses .channel = 2 to avoid aliasing the channel2 terminal (index 1) of the differential pair.
The HX710B has a dedicated VREF pin for the ADC reference voltage. The driver tries vref-supply first; if absent it falls back to avdd-supply (for boards where VREF is tied to AVDD). The HX711 uses AVDD as its reference and is unaffected.
Add fixed_gain and fixed_gain_val fields to hx711_chip_info to carry the gain into the scale calculation. Store a per-instance scale in hx711_data for HX710B and use it in hx711_read_raw() when fixed_gain is set. Update hx711_reset() to reset channel_set on HX710B after a power-down cycle.
Enlarge the trigger buffer from 2 to 3 channels plus a pad word to keep the timestamp naturally aligned; HX711 continues to use only the first two slots.
---
Changes in v4:
- Add a third HX710B channel (27 pulses, differential 40 SPS) based on Table 3 of the HX710B datasheet.
- Use .channel = 2 for the supply monitor channel to avoid aliasing the .channel2 = 1 terminal of the first differential pair.
- Add vref-supply probe path for the HX710B VREF reference pin; fall back to avdd-supply when vref-supply is absent.
- Add NULL guard on device_get_match_data() in the chip_info introduction patch; kept here as documentation that the guard exists.
- Keep hx711_chip_info fields in the final order introduced by the chip_info patch; this patch appends fixed_gain_val and fixed_gain without reordering existing fields.
- Move channel_set and scale fields to hx711_data (per-instance).
- Update channel_set only after both hx711_read() and hx711_wait_for_ready() succeed.
- Keep a single HX710B scale derived from the fixed gain of 128.
Changes in v3:
- Add HX710B support on top of the separate hx711_chip_info refactor.
- Update channel_set only after hx711_read() and hx711_wait_for_ready() both succeed.
- Keep a single HX710B fixed-gain scale based on the datasheet's fixed PGA gain of 128; do not apply the HX711 channel-B gain of 32 to the HX710B supply monitor path.
- Use unsigned int for channel state and fixed-gain scale storage.
- Keep HX710B trailing pulse counts in chan->address.
- Describe HX710B channel 0 as a differential IIO channel.
- Reorder hx711_chip_info fields based on pahole output so the structure has no internal holes.
Changes in v2:
- Fix pulse count bug: HX710B values were {25, 26} total SCK cycles; corrected to {1, 2} trailing pulses because hx711_read() already clocks the 24 data bits.
- Add .differential = 1 and .channel2 = 1 to HX710B channel 0.
- Move trailing pulse counts from a separate array to chan->address.
- Replace chan_pulse_count tests with a dedicated fixed_gain flag.
- Add fixed_gain_val to hx711_chip_info.
- Add the iio_info pointer to hx711_chip_info and assign indio_dev->info from chip_info.
- Remove the NULL check after device_get_match_data().
- Remove reset_channel from hx711_chip_info.
- Change hx711_reset_read() and hx710b_set_channel() to take const struct iio_chan_spec *.
- Revert unrelated hx711_data struct member alignment noise.
- Sort of_device_id entries alphabetically.
- Expand the commit message to explain HX711 versus HX710B trailing pulse behaviour.
- Restore the file header to mention weight sensor modules.
--- drivers/iio/adc/Kconfig | 1 + drivers/iio/adc/hx711.c | 189 ++++++++++++++++++++++++++++++++++------ 2 files changed, 164 insertions(+), 26 deletions(-)
drivers/iio/adc/Kconfig +1 -0
@@ -790,6 +790,7 @@ config HX711 select IIO_TRIGGERED_BUFFER help 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.
drivers/iio/adc/hx711.c +163 -26
298 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v4 3/7] iio: adc: hx711: update Kconfig, module description and file header
In reply to Piyush PatleQuoted 1 line · Piyush Patle
+ which are used for bridge sensors such as weigh cells.
Typo here.
Signature · 3 lines
Kind regards CJD
Re: [PATCH v4 3/7] iio: adc: hx711: update Kconfig, module description and file header
Quoted 1 line · Joshua Crofts
On Mon, 27 Apr 2026 at 12:23, Piyush Patle <[email protected]> wrote:
...
Quoted 3 lines
Quoted 1 line
+ which are used for bridge sensors such as weigh cells.
Typo here.
It's better if you point exactly what the typo is. I think you meant "weigh" that should be "weight"?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 3/7] iio: adc: hx711: update Kconfig, module description and file header
Quoted 4 lines · Andy Shevchenko
Quoted 1 line · Joshua Crofts
On Mon, 27 Apr 2026 at 12:23, Piyush Patle <[email protected]> wrote:
Quoted 8 lines
...
Quoted 3 lines
Quoted 1 line
+ which are used for bridge sensors such as weigh cells.
Typo here.
It's better if you point exactly what the typo is. I think you meant "weigh" that should be "weight"?
Yep, that's it, my mistake.
Signature · 3 lines
Kind regards CJD
Re: [PATCH v4 3/7] iio: adc: hx711: update Kconfig, module description and file header
In reply to Piyush PatleQuoted 6 lines · Piyush Patle
Update the
Kconfigentry to "AVIA HX711 and compatible ADCs" and list supported chips one per line to make it easy to extend when new variants are added. Change the module description and file header to match.Add direct includes for
linux/array_size.handlinux/types.hrather than relying on transitive includes pulled in by other headers.
Obviously something went wrong. First of all, I have told that MODULE_*() changes doesn't belong to the refactoring patch. Second, I meant that the Kconfig help text to go to a separate patch.
Header inclusions should be taken care when it's required. It doesn't belong here.
Looking at this again, most of it probably should be folded into the new HW support patch where you add a new chip_info.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 2/7] iio: adc: hx711: move scale computation to per-device storage
In reply to Piyush PatleQuoted 14 lines · Piyush Patle
The gain-to-scale table hx711_gain_to_scale[] is a global array whose .scale fields are overwritten in
hx711_probe()using the AVDD voltage read at probe time. When two HX711 sensors are connected to supplies at different voltages, the second probe call overwrites the scale values computed for the first sensor, silently corrupting its readings.Fix this by removing the .scale field from the global table, making the table const, and adding a per-instance gain_scale[] array to
hx711_data. Populate gain_scale[] inhx711_probe()using the device's own AVDD regulator voltage. Updatehx711_get_gain_to_scale()andhx711_get_scale_to_gain()to take the per-instance array as a parameter, and updatehx711_scale_available_show()to retrieve it viaiio_priv().No functional change for single-sensor configurations.
...
Quoted 9 lines
/*
* .scale depends on AVDD which in turn is known as soon as the regulator
- * is available
- * therefore we set .scale inhx711_probe()
+ * is available; it is stored per device in hx711_data.gain_scale[] *
* channel A in documentation is channel 0 in source code
* channel B in documentation is channel 1 in source code
*/
drivers/iio/adc/hx711.c line 44
Quoted 8 lines
drivers/iio/adc/hx711.c +4
-4-static struct hx711_gain_to_scale hx711_gain_to_scale[HX711_GAIN_MAX ] = { - {128 ,1 ,0 ,0 }, - {32 ,2 ,0 ,1 }, - {64 ,3 ,0 ,0 } +static const struct hx711_gain_to_scale hx711_gain_to_scale[HX711_GAIN_MAX ] = { + {128 ,1 ,0 }, + {32 ,2 ,1 }, + {64 ,3 ,0 }
I would leave trailing comma, as it is semantically is not the terminator entry.
Quoted 1 line
};
...
drivers/iio/adc/hx711.c static ssize_t hx711_scale_available_show(struct device *dev, · line 392
Quoted 5 lines
{
drivers/iio/adc/hx711.c +2
-0struct iio_dev_attr *iio_attr =to_iio_dev_attr (attr);int channel = iio_attr->address; +struct hx711_data *hx711_data = +iio_priv (dev_to_iio_dev (dev));
First of all, please preserve the reversed xmas tree order. Second, it's more than enough room to have it a single line.
struct hx711_data *hx711_data = iio_priv(dev_to_iio_dev(dev));
Quoted 1 line
int i, len = 0;
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 4/7] iio: adc: hx711: introduce hx711_chip_info per-variant structure
In reply to Piyush PatleQuoted 11 lines · Piyush Patle
Add struct
hx711_chip_infoto hold per-variant static configuration:
device name, IIO channel spec array, IIO info ops pointer, and channel count. Store achip_infopointer inhx711_dataand populateindio_devfields from it at probe time instead of hardcoding them.Use
device_get_match_data()to look up thechip_infopointer from theof_device_idtable. Guard against a NULL return so that the driver fails cleanly if probed via the platform name match path without associated match data.No functional change for existing HX711 users.
All the above is fine, but I missed the answer to the question "why?" You need to add that this is preparatory change to add more HW support or something alike along these words.
...
drivers/iio/adc/hx711.c static const struct iio_chan_spec hx711_chan_spec[] = { · line 481
Quoted 5 lines
drivers/iio/adc/hx711.c +5
-0+static const struct hx711_chip_info hx711_chip = { + .name ="hx711" , + .channels = hx711_chan_spec, + .iio_info = &hx711_iio_info, + .num_channels =ARRAY_SIZE (hx711_chan_spec),
The array_size.h inclusion should go with this patch.
Quoted 1 line
+};
...
drivers/iio/adc/hx711.c static const struct iio_chan_spec hx711_chan_spec[] = { · line 488
Quoted 3 lines
drivers/iio/adc/hx711.c +1
-0struct device *dev = &pdev->dev;struct hx711_data *hx711_data; +const struct hx711_chip_info *chip_info;
Preserve reversed xmas tree order.
Quoted 3 lines
drivers/iio/adc/hx711.c +0
-0struct iio_dev *indio_dev;int ret;int i;
...
drivers/iio/adc/hx711.c static int hx711_probe(struct platform_device *pdev) · line 504
Quoted 3 lines
drivers/iio/adc/hx711.c +3
-0+ chip_info =device_get_match_data (dev); +if (!chip_info) +return dev_err_probe (dev, -ENODEV ,"missing match data\n" );
Since we are here, it's not match data, it's driver data. "missing driver data".
drivers/iio/adc/hx711.c static int hx711_probe(struct platform_device *pdev) · line 505
Quoted 1 line
drivers/iio/adc/hx711.c +1
-0+ hx711_data->chip_info = chip_info;
+ Blank line.
Quoted 1 line
/*
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 5/7] iio: adc: hx711: pass trailing pulse count into hx711_read()
In reply to Piyush PatleQuoted 9 lines · Piyush Patle
Move the trailing-pulse computation out of
hx711_read()and into the callers.hx711_read()now takes an explicit 'trailing_pulses' parameter so the HX710B support patch can pass the per-channel count stored inchan->addresswithout adding a separate code path inhx711_read().Use scoped loop variables (for (int i = ...) / for (unsigned int i = ...)) to tighten the scope of the loop counters.
No functional change.
...
drivers/iio/adc/hx711.c static int hx711_cycle(struct hx711_data *hx711_data) · line 162
Quoted 7 lines
drivers/iio/adc/hx711.c +2
-2-static int hx711_read (struct hx711_data *hx711_data) +static int hx711_read (struct hx711_data *hx711_data,unsigned int trailing_pulses) { -int i, ret; +int ret;int value =0 ;int val =gpiod_get_value (hx711_data->gpiod_dout);
Try to follow the reversed xmas tree order.
drivers/iio/adc/hx711.c static int hx711_read(struct hx711_data *hx711_data) · line 168
Quoted 2 lines
drivers/iio/adc/hx711.c +1
-1-for (i =0 ; i <24 ; i++) { +for (int i =0 ; i <24 ; i++) {
Why is 'i' signed?
drivers/iio/adc/hx711.c static int hx711_read(struct hx711_data *hx711_data) · line 178
Quoted 3 lines
drivers/iio/adc/hx711.c +1
-1-for (i =0 ; i <hx711_get_gain_to_pulse (hx711_data->gain_set); i++) +for (unsigned int i =0 ; i < trailing_pulses; i++)hx711_cycle (hx711_data);
With the above the both seems like a candidate to a separate change (after this trailing_pulses patch).
Quoted 1 line
return value;
...
So, split this to two:
- trailing_pulses (without touching 'i')
- localising loop iterators
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 4/7] iio: adc: hx711: introduce hx711_chip_info per-variant structure
In reply to Andy ShevchenkoQuoted 1 line · Andy Shevchenko
On Mon, Apr 27, 2026 at 03:39:35PM +0530, Piyush Patle wrote:
...
Quoted 5 lines
drivers/iio/adc/hx711.c static int hx711_probe(struct platform_device *pdev) · line 504
Quoted 3 lines
drivers/iio/adc/hx711.c +3
-0+ chip_info =device_get_match_data (dev); +if (!chip_info) +return dev_err_probe (dev, -ENODEV ,"missing match data\n" );Since we are here, it's not match data, it's driver data. "missing driver data".
And does it need property.h to be included?
Also check if dev_printk.h and errno.h are there (it may be that driver already uses device.h and err.h that cover these two).
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 6/7] iio: adc: hx711: pass iio_chan_spec to hx711_reset_read()
In reply to Piyush PatleQuoted 3 lines · Piyush Patle
Change
hx711_reset_read()to accept a const structiio_chan_spec* instead of a plain integer channel index. This lets the HX710B support patch pass the full channel descriptor (includingchan->addressfor the
s/patch/change in the future/
Quoted 7 lines
trailing pulse count) without adding a wrapper or a separate code path for each call site.
Update the two callers:
hx711_read_raw()already has theiio_chan_spec* from its own parameter, andhx711_trigger()passes &indio_dev->channels[i].No functional change.
...
drivers/iio/adc/hx711.c static irqreturn_t hx711_trigger(int irq, void *p) · line 391
Quoted 4 lines
drivers/iio/adc/hx711.c +1
-1iio_for_each_active_channel (indio_dev, i) { hx711_data->buffer.channel[j] =hx711_reset_read (hx711_data, - indio_dev->channels[i].channel); + &indio_dev->channels[i]);
Re-indent this to be
hx711_data->buffer.channel[j] = hx711_reset_read(hx711_data, &indio_dev->channels[i]);
Quoted 2 lines
(quoted diff) +0
-0j++; }
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 3/7] iio: adc: hx711: update Kconfig, module description and file header
In reply to Andy ShevchenkoQuoted 12 lines · Andy Shevchenko
Quoted 6 lines · Piyush Patle
Update the
Kconfigentry to "AVIA HX711 and compatible ADCs" and list supported chips one per line to make it easy to extend when new variants are added. Change the module description and file header to match.Add direct includes for
linux/array_size.handlinux/types.hrather than relying on transitive includes pulled in by other headers.Obviously something went wrong. First of all, I have told that MODULE_*() changes doesn't belong to the refactoring patch. Second, I meant that the
Kconfighelp text to go to a separate patch.
Quoted 5 lines
Header inclusions should be taken care when it's required. It doesn't belong here.
Looking at this again, most of it probably should be folded into the new HW support patch where you add a new
chip_info.
I checked again after browsing through the whole series. This patch basically should be either folded to the patch 7, or immediately prepend that (without being any between them).
- Kconfig + MODULE_*() + respective comments update // no patches in between!
- new HW support
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 7/7] iio: adc: hx711: add support for HX710B
In reply to Piyush PatleQuoted 27 lines · Piyush Patle
Add support for the AVIA HX710B ADC, which shares the HX711 GPIO interface but has a fixed gain of 128 and uses trailing
PD_SCKpulses to select the active channel rather than the gain.The HX710B has three operating modes controlled by the trailing pulse count after the 24 data bits (Table 3 in the HX710B datasheet):
25 pulses (1 trailing): differential input at 10 SPS
26 pulses (2 trailing): DVDD-AVDD supply monitor at 40 SPS 27 pulses (3 trailing): differential input at 40 SPSModel the HX710B with its own hx710b_chan_spec[] and
hx710b_iio_info. Store the trailing pulse count inchan->addresssohx710b_set_channel()can switch channels without a separate lookup table. The supply monitor uses .channel = 2 to avoid aliasing the channel2 terminal (index 1) of the differential pair.The HX710B has a dedicated VREF pin for the ADC reference voltage. The driver tries vref-supply first; if absent it falls back to avdd-supply (for boards where VREF is tied to AVDD). The HX711 uses AVDD as its reference and is unaffected.
Add
fixed_gainandfixed_gain_valfields tohx711_chip_infoto carry the gain into the scale calculation. Store a per-instance scale inhx711_datafor HX710B and use it inhx711_read_raw()whenfixed_gainis set. Updatehx711_reset()to resetchannel_seton HX710B after a power-down cycle.
Quoted 3 lines
Enlarge the trigger buffer from 2 to 3 channels plus a pad word to keep the timestamp naturally aligned; HX711 continues to use only the first two slots.
So, missed types.h should be in this patch.
...
Quoted 10 lines
/**
* structhx711_chip_info- per-variant static configuration- * @name: IIO device name - * @channels: channel specification array - * @iio_info: IIO info ops for this variant - * @num_channels: number of entries in @channels + * @name: IIO device name + * @channels: channel specification array + * @iio_info: IIO info ops for this variant + * @num_channels: number of entries in @channels
Quoted 2 lines
+ * @fixed_gain_val: fixed PGA gain (used when @fixed_gain is true)
+ * @fixed_gain: true if the variant has a fixed ADC gain (e.g. HX710B)
No, make sure there will be no '-' lines here.
Quoted 1 line
*/
...
drivers/iio/adc/hx711.c struct hx711_data { · line 106
Quoted 9 lines
struct
hx711_data{drivers/iio/adc/hx711.c +2
-0int gain_set;/* gain set on device */ int gain_chan_a;/* gain for channel A */ int gain_scale[HX711_GAIN_MAX ]; +unsigned int channel_set;/* HX710B active channel */ const struct hx711_chip_info *chip_info; +unsigned int scale;/* HX710B fixed-gain scale */
Semantically the scale is closer to the above gain_* ones AFAICS.
Quoted 13 lines
drivers/iio/adc/hx711.c +3
-2struct mutex lock;/* * triggered buffer -* 2x32-bit channel + 64-bit naturally aligned timestamp +* up to 3x32-bit channels + pad + 64-bit naturally aligned timestamp */ struct { -u32 channel[2 ]; +u32 channel[3 ]; +u32 pad;aligned_s64 timestamp; } buffer;/*
...
drivers/iio/adc/hx711.c static int hx711_wait_for_ready(struct hx711_data *hx711_data) · line 217
Quoted 6 lines
static int
hx711_reset(struct hx711_data *hx711_data){drivers/iio/adc/hx711.c +1
-0+const struct hx711_chip_info *info = hx711_data->chip_info;int val =hx711_wait_for_ready (hx711_data);if (val) {
The rule of thumb is to split definition and assignment for the variables that are going to be validated.
Here I expect to see
int val;
val = hx711_wait_for_ready(hx711_data);
if (val) {But since it's not related directly to this change, make a preparatory patch. Because now with two variables this looks worse and adds more potential to be mistaken in the future.
Quoted 20 lines
drivers/iio/adc/hx711.c +20
-0+static int hx710b_set_channel (struct hx711_data *hx711_data, +const struct iio_chan_spec *chan) +{ +int ret; + +if (hx711_data->channel_set == (unsigned int )chan->channel) +return 0 ; + + ret =hx711_read (hx711_data, chan->address); +if (ret <0 ) +return ret; + + ret =hx711_wait_for_ready (hx711_data); +if (ret) +return ret; + + hx711_data->channel_set = chan->channel; + +return 0 ; +}
...
drivers/iio/adc/hx711.c static int hx711_reset_read(struct hx711_data *hx711_data, · line 332
Quoted 12 lines
drivers/iio/adc/hx711.c +5
-4- ret =hx711_set_gain_for_channel (hx711_data, chan->channel); -if (ret <0 ) -return ret; + ret =hx711_set_gain_for_channel (hx711_data, chan->channel); +if (ret <0 ) +return ret; + trailing_pulses =hx711_get_gain_to_pulse (hx711_data->gain_set); + } - trailing_pulses =hx711_get_gain_to_pulse (hx711_data->gain_set);return hx711_read (hx711_data, trailing_pulses);
Instead of the above, first split these lines to a helper
static int hx711_set_hx711_channel(..., *tp)
{
int ret;
ret = hx711_set_gain_for_channel(hx711_data, chan->channel);
if (ret < 0)
return ret;
*tp = hx711_get_gain_to_pulse(hx711_data->gain_set);
return 0;
}And name your new function something like
hx711_set_hx710b_channel()
...
drivers/iio/adc/hx711.c static int hx711_probe(struct platform_device *pdev) · line 655
Quoted 4 lines
drivers/iio/adc/hx711.c +4
-0+/* +* The HX710B uses the VREF pin as the ADC reference; try vref-supply +* first and fall back to avdd-supply when VREF is tied to AVDD on the +* board. The HX711 uses AVDD as its reference.
I think we use a single space in the comment, but double check that. The rule of thumb is to keep the original driver style and change it separately if required.
Quoted 1 line
(quoted diff) +1
-0+ */
...
Review
Quoted 2 lines
(quoted diff) +0
-0/* * with
While at it, with --> With.
Quoted 15 lines
drivers/iio/adc/hx711.c +5
-5-* full scale differential input range: AVDD / GAIN +* full scale differential input range: VREF / GAIN * full scale output data: 2^24 * we can say: -* AVDD / GAIN = 2^24 +* VREF / GAIN = 2^24 * therefore: -* 1 LSB = AVDD / GAIN / 2^24 -* AVDD is in uV, but we need 10^-9 mV +* 1 LSB = VREF / GAIN / 2^24 +* VREF 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 = (VREF * 100) / GAIN / 1678 [10^-9 mV] */
...
Quoted 5 lines
drivers/iio/adc/hx711.c +2
-3-for (i =0 ; i <HX711_GAIN_MAX ; i++) - hx711_data->gain_scale[i] = - ret / hx711_gain_to_scale[i].gain /1678 ; +if (chip_info->fixed_gain) { + hx711_data->scale = ret / chip_info->fixed_gain_val /1678 ;
drivers/iio/adc/hx711.c static int hx711_probe(struct platform_device *pdev) · line 689
Quoted 10 lines
drivers/iio/adc/hx711.c +7
-2+ }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 ; + }
Looking at this I'm wondering wouldn't be easier just to have the array, but feel it with the same data.
...
drivers/iio/adc/hx711.c static int hx711_probe(struct platform_device *pdev)
Quoted 3 lines
drivers/iio/adc/hx711.c +1
-1- { .compatible = "avia,hx711", .data = &hx711_chip }, + { .compatible = "avia,hx711", .data = &hx711_chip },
Why?! This is stray change.
Signature · 2 lines
With Best Regards, Andy Shevchenko
David Lechner
reviewerRe: [PATCH v4 1/7] dt-bindings: iio: adc: avia-hx711: add avia,hx710b compatible
In reply to Piyush PatleQuoted 10 lines · Piyush Patle
The HX710B shares the same two-wire interface as the HX711 but differs in its channel and gain model: gain is fixed at 128 and the number of trailing
PD_SCKpulses selects the input channel rather than the gain.Add avia,hx710b to the compatible enum. Document the chip differences in the description and add chip-specific supply properties (dvdd-supply, vsup-supply, vref-supply) and a rate-gpios property for the HX711 RATE pin. Add allOf constraints that forbid HX711-only properties on HX710B nodes and vice versa. Clarify the clock-frequency description to reflect its actual purpose: controlling the SCK bit-bang timing.
Krzysztof gave a Reviewed-by on the last revision [1], so you need to include that here or explain why you didn't pick it up. If you just make a trivial change, you can still pick it up. In this case though, I think that enough changes were made that it is worth having a DT maintainer have another look.
https://lore.kernel.org/linux-iio/20260423-famous-tody-of-opposition-73c8af@quoll/
Quoted 128 lines
Signed-off-by Piyush Patle <[email protected]>---
Changes in v4:
- Add vref-supply for the HX710B VREF reference voltage pin.
- Remove dvdd-supply from the HX710B forbidden properties list; the HX710B has a DVDD supply and the DVDD-AVDD channel relies on it.
- Add allOf block forbidding vref-supply on HX711 nodes.
- Add an HX710B example showing vref-supply.
- Update description: avoid specific channel-number references in hardware text.Changes in v3:
- Drop the vref-supply mention from avdd-supply; no such binding property exists.
- Drop the clock-frequency sentence that repeated the schema default.
- Restore the example node name to weight.
- Remove the separate HX710B example.Changes in v2:
- Remove driver implementation details from the description and describe hardware behaviour only.
- Drop unrelated punctuation cleanup.
- Add dvdd-supply and vsup-supply optional properties for HX711.
- Add rate-gpios optional property for the HX711 RATE pin and forbid it on HX710B.
- Add the allOf if/then block forbidding HX711-only properties on HX710B nodes.
- Clarify clock-frequency as SCK bit-bang timing, not a crystal or external clock input.
- Sort compatible enum alphabetically.
- Remove redundant example comments.
- Update the HX711 example to exercise rate-gpios.--- .../bindings/iio/adc/avia-hx711.yaml | 82 +++++++++++++++---- 1 file changed, 67 insertions(+), 15 deletions(-)Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +40
-15@@ -4,49 +4,91 @@ $id: http://devicetree.org/schemas/iio/adc/avia-hx711.yaml# $schema: http://devicetree.org/meta-schemas/core.yaml# -title: AVIA HX711 ADC chip for weight cells +title: AVIA HX711 and HX710B ADCs maintainers: - Andreas Klinger <[email protected]> description: | - Bit-banging driver using two GPIOs: - - sck-gpio gives a clock to the sensor with 24 cycles for data retrieval - and up to 3 cycles for selection of the input channel and gain for the - next measurement - - dout-gpio is the sensor data the sensor responds to the clock + The HX711 is a 24-bit ADC with selectable gain (32/64/128) and two + differential input channels. Channel A supports gain 64 and 128; + channel B supports gain 32. - Specifications about the driver can be found at: - http://www.aviaic.com/ENProducts.aspx + The HX710B is a 24-bit ADC with fixed gain of 128. One input measures + the differential voltage between the two input pins; a second measures + the DVDD-AVDD supply voltage difference for battery level detection. properties: compatible: enum: + - avia,hx710b - avia,hx711 sck-gpios: description: - Definition of the GPIO for the clock (output). In the datasheet it is - named PD_SCK + GPIO for the clock output (PD_SCK in the datasheet). maxItems: 1 dout-gpios: description: - Definition of the GPIO for the data-out sent by the sensor in - response to the clock (input). - See Documentation/devicetree/bindings/gpio/gpio.txt for information - on how to specify a consumer gpio. + GPIO for the data output from the sensor (DOUT in the datasheet). maxItems: 1 avdd-supply: description: - Definition of the regulator used as analog supply + Analog supply voltage (AVDD). Also serves as the voltage reference on + both chips. + + dvdd-supply: + description: + Digital supply voltage (DVDD). For the HX710B, DVDD must be greater + than or equal to AVDD. When DVDD is a battery rail and AVDD is a + regulated supply, one channel monitors the DVDD-AVDD difference for + battery level detection. + + vsup-supply: + description: + Supply voltage for the on-chip regulator (VSUP). HX711 only. + + vref-supply: + description: + Reference voltage input (VREF). HX710B only. When omitted, the driver + assumes VREF is tied to AVDD on the board. + + rate-gpios: + description: + GPIO connected to the RATE pin (HX711 only). When driven low the + output data rate is 10 SPS; when driven high it is 80 SPS. If + omitted the RATE pin state is determined by the board wiring. + maxItems: 1 clock-frequency: + description: + Controls the SCK bit-bang timing. The value is used to derive the + delay between SCK edges; keep the SCK high time below 60 us to + avoid triggering chip power-down mode. minimum: 20000 maximum: 2500000 default: 400000 +allOf:
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml line 84
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +10
-0+ - if: + properties: + compatible: + const: avia,hx710b + then: + properties: + vsup-supply: false + rate-gpios: false + + - if:
Could just be else:
Probably OK either way though.
Quoted 27 lines
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +17
-0+ properties: + compatible: + const: avia,hx711 + then: + properties: + vref-supply: false + required: - compatible - sck-gpios@@ -62,6 +104,16 @@ examples: compatible = "avia,hx711"; sck-gpios = <&gpio3 10 GPIO_ACTIVE_HIGH>; dout-gpios = <&gpio0 7 GPIO_ACTIVE_HIGH>; + rate-gpios = <&gpio0 9 GPIO_ACTIVE_HIGH>; avdd-supply = <&avdd>; clock-frequency = <100000>; }; + - | + #include <dt-bindings/gpio/gpio.h> + weight { + compatible = "avia,hx710b"; + sck-gpios = <&gpio3 10 GPIO_ACTIVE_HIGH>; + dout-gpios = <&gpio0 7 GPIO_ACTIVE_HIGH>; + avdd-supply = <&avdd>; + vref-supply = <&vref>; + };
Re: [PATCH v4 1/7] dt-bindings: iio: adc: avia-hx711: add avia,hx710b compatible
Quoted 13 lines · David Lechner
Quoted 10 lines · Piyush Patle
The HX710B shares the same two-wire interface as the HX711 but differs in its channel and gain model: gain is fixed at 128 and the number of trailing
PD_SCKpulses selects the input channel rather than the gain.Add avia,hx710b to the compatible enum. Document the chip differences in the description and add chip-specific supply properties (dvdd-supply, vsup-supply, vref-supply) and a rate-gpios property for the HX711 RATE pin. Add allOf constraints that forbid HX711-only properties on HX710B nodes and vice versa. Clarify the clock-frequency description to reflect its actual purpose: controlling the SCK bit-bang timing.
Quoted 8 lines
Krzysztof gave a Reviewed-by on the last revision [1], so you need to include that here or explain why you didn't pick it up. If you just make a trivial change, you can still pick it up. In this case though, I think that enough changes were made that it is worth having a DT maintainer have another look.
https://lore.kernel.org/linux-iio/20260423-famous-tody-of-opposition-73c8af@quoll/
I did not carry the Reviewed-by because the binding changed majorly in v4 (vref-supply added, dvdd-supply removed from the HX710B forbidden list, new allOf block forbidding vref-supply on HX711).
I should have mentioned that in the cover letter; I’ll do that in v5.
Quoted 141 lines
Quoted 128 lines
Signed-off-by Piyush Patle <[email protected]>---
Changes in v4:
- Add vref-supply for the HX710B VREF reference voltage pin.
- Remove dvdd-supply from the HX710B forbidden properties list; the HX710B has a DVDD supply and the DVDD-AVDD channel relies on it.
- Add allOf block forbidding vref-supply on HX711 nodes.
- Add an HX710B example showing vref-supply.
- Update description: avoid specific channel-number references in hardware text.Changes in v3:
- Drop the vref-supply mention from avdd-supply; no such binding property exists.
- Drop the clock-frequency sentence that repeated the schema default.
- Restore the example node name to weight.
- Remove the separate HX710B example.Changes in v2:
- Remove driver implementation details from the description and describe hardware behaviour only.
- Drop unrelated punctuation cleanup.
- Add dvdd-supply and vsup-supply optional properties for HX711.
- Add rate-gpios optional property for the HX711 RATE pin and forbid it on HX710B.
- Add the allOf if/then block forbidding HX711-only properties on HX710B nodes.
- Clarify clock-frequency as SCK bit-bang timing, not a crystal or external clock input.
- Sort compatible enum alphabetically.
- Remove redundant example comments.
- Update the HX711 example to exercise rate-gpios.--- .../bindings/iio/adc/avia-hx711.yaml | 82 +++++++++++++++---- 1 file changed, 67 insertions(+), 15 deletions(-)Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +40
-15@@ -4,49 +4,91 @@ $id: http://devicetree.org/schemas/iio/adc/avia-hx711.yaml# $schema: http://devicetree.org/meta-schemas/core.yaml# -title: AVIA HX711 ADC chip for weight cells +title: AVIA HX711 and HX710B ADCs maintainers: - Andreas Klinger <[email protected]> description: | - Bit-banging driver using two GPIOs: - - sck-gpio gives a clock to the sensor with 24 cycles for data retrieval - and up to 3 cycles for selection of the input channel and gain for the - next measurement - - dout-gpio is the sensor data the sensor responds to the clock + The HX711 is a 24-bit ADC with selectable gain (32/64/128) and two + differential input channels. Channel A supports gain 64 and 128; + channel B supports gain 32. - Specifications about the driver can be found at: - http://www.aviaic.com/ENProducts.aspx + The HX710B is a 24-bit ADC with fixed gain of 128. One input measures + the differential voltage between the two input pins; a second measures + the DVDD-AVDD supply voltage difference for battery level detection. properties: compatible: enum: + - avia,hx710b - avia,hx711 sck-gpios: description: - Definition of the GPIO for the clock (output). In the datasheet it is - named PD_SCK + GPIO for the clock output (PD_SCK in the datasheet). maxItems: 1 dout-gpios: description: - Definition of the GPIO for the data-out sent by the sensor in - response to the clock (input). - See Documentation/devicetree/bindings/gpio/gpio.txt for information - on how to specify a consumer gpio. + GPIO for the data output from the sensor (DOUT in the datasheet). maxItems: 1 avdd-supply: description: - Definition of the regulator used as analog supply + Analog supply voltage (AVDD). Also serves as the voltage reference on + both chips. + + dvdd-supply: + description: + Digital supply voltage (DVDD). For the HX710B, DVDD must be greater + than or equal to AVDD. When DVDD is a battery rail and AVDD is a + regulated supply, one channel monitors the DVDD-AVDD difference for + battery level detection. + + vsup-supply: + description: + Supply voltage for the on-chip regulator (VSUP). HX711 only. + + vref-supply: + description: + Reference voltage input (VREF). HX710B only. When omitted, the driver + assumes VREF is tied to AVDD on the board. + + rate-gpios: + description: + GPIO connected to the RATE pin (HX711 only). When driven low the + output data rate is 10 SPS; when driven high it is 80 SPS. If + omitted the RATE pin state is determined by the board wiring. + maxItems: 1 clock-frequency: + description: + Controls the SCK bit-bang timing. The value is used to derive the + delay between SCK edges; keep the SCK high time below 60 us to + avoid triggering chip power-down mode. minimum: 20000 maximum: 2500000 default: 400000 +allOf:Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml line 84
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +10
-0+ - if: + properties: + compatible: + const: avia,hx710b + then: + properties: + vsup-supply: false + rate-gpios: false + + - if:Could just be else:
Quoted 2 lines
Probably OK either way though.
Will switch the second allOf block to else: in v5.
Quoted 28 lines
Quoted 27 lines
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +17
-0+ properties: + compatible: + const: avia,hx711 + then: + properties: + vref-supply: false + required: - compatible - sck-gpios@@ -62,6 +104,16 @@ examples: compatible = "avia,hx711"; sck-gpios = <&gpio3 10 GPIO_ACTIVE_HIGH>; dout-gpios = <&gpio0 7 GPIO_ACTIVE_HIGH>; + rate-gpios = <&gpio0 9 GPIO_ACTIVE_HIGH>; avdd-supply = <&avdd>; clock-frequency = <100000>; }; + - | + #include <dt-bindings/gpio/gpio.h> + weight { + compatible = "avia,hx710b"; + sck-gpios = <&gpio3 10 GPIO_ACTIVE_HIGH>; + dout-gpios = <&gpio0 7 GPIO_ACTIVE_HIGH>; + avdd-supply = <&avdd>; + vref-supply = <&vref>; + };
Re: [PATCH v4 2/7] iio: adc: hx711: move scale computation to per-device storage
In reply to Andy ShevchenkoQuoted 30 lines · Andy Shevchenko
Quoted 14 lines · Piyush Patle
The gain-to-scale table hx711_gain_to_scale[] is a global array whose .scale fields are overwritten in
hx711_probe()using the AVDD voltage read at probe time. When two HX711 sensors are connected to supplies at different voltages, the second probe call overwrites the scale values computed for the first sensor, silently corrupting its readings.Fix this by removing the .scale field from the global table, making the table const, and adding a per-instance gain_scale[] array to
hx711_data. Populate gain_scale[] inhx711_probe()using the device's own AVDD regulator voltage. Updatehx711_get_gain_to_scale()andhx711_get_scale_to_gain()to take the per-instance array as a parameter, and updatehx711_scale_available_show()to retrieve it viaiio_priv().No functional change for single-sensor configurations.
...
Quoted 11 lines
/*
* .scale depends on AVDD which in turn is known as soon as the regulator
- * is available
- * therefore we set .scale inhx711_probe()
+ * is available; it is stored per device in hx711_data.gain_scale[] *
* channel A in documentation is channel 0 in source code
* channel B in documentation is channel 1 in source code
*/drivers/iio/adc/hx711.c +0
-2-static struct hx711_gain_to_scale hx711_gain_to_scale[HX711_GAIN_MAX ] = { - {128 ,1 ,0 ,0 },
Quoted 9 lines
drivers/iio/adc/hx711.c line 44
Quoted 6 lines
drivers/iio/adc/hx711.c +4
-2- { 32, 2, 0, 1 }, - { 64, 3, 0, 0 } +static const struct hx711_gain_to_scale hx711_gain_to_scale[HX711_GAIN_MAX ] = { + {128 ,1 ,0 }, + {32 ,2 ,1 }, + {64 ,3 ,0 }I would leave trailing comma, as it is semantically is not the terminator entry.
Will add trailing commas to all three initializer lines.
Quoted 4 lines
Quoted 1 line
};
...
Quoted 8 lines
drivers/iio/adc/hx711.c static ssize_t hx711_scale_available_show(struct device *dev, · line 392
Quoted 5 lines
{ struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr); int channel = iio_attr->address;drivers/iio/adc/hx711.c +2
-0+struct hx711_data *hx711_data = +iio_priv (dev_to_iio_dev (dev));First of all, please preserve the reversed xmas tree order. Second, it's more than enough room to have it a single line.
Works , will do in v5
Quoted 10 lines
struct hx711_data *hx711_data = iio_priv(dev_to_iio_dev(dev));Quoted 1 line
int i, len = 0;--
With Best Regards,
Andy Shevchenko
Re: [PATCH v4 4/7] iio: adc: hx711: introduce hx711_chip_info per-variant structure
In reply to Andy ShevchenkoQuoted 12 lines · Andy Shevchenko
Quoted 10 lines · Piyush Patle
Add struct
hx711_chip_infoto hold per-variant static configuration:
device name, IIO channel spec array, IIO info ops pointer, and channel count. Store achip_infopointer inhx711_dataand populateindio_devfields from it at probe time instead of hardcoding them.Use
device_get_match_data()to look up thechip_infopointer from theof_device_idtable. Guard against a NULL return so that the driver fails cleanly if probed via the platform name match path without associated match data.
Quoted 6 lines
Quoted 1 line
No functional change for existing HX711 users.
All the above is fine, but I missed the answer to the question "why?" You need to add that this is preparatory change to add more HW support or something alike along these words.
Will add to the commit message: this introduces hx711_chip_info as a preparatory step so that the HX710B support patch can register a second
variant without duplicating probe-time setup.
Quoted 12 lines
...
drivers/iio/adc/hx711.c static const struct iio_chan_spec hx711_chan_spec[] = { · line 481
Quoted 5 lines
drivers/iio/adc/hx711.c +5
-0+static const struct hx711_chip_info hx711_chip = { + .name ="hx711" , + .channels = hx711_chan_spec, + .iio_info = &hx711_iio_info, + .num_channels =ARRAY_SIZE (hx711_chan_spec),The
array_size.hinclusion should go with this patch.Quoted 1 line
+};
Quoted 7 lines
...
Quoted 3 lines
struct device *dev = &pdev->dev; struct hx711_data *hx711_data;+ const struct
hx711_chip_info*chip_info;Preserve reversed xmas tree order.
Will reorder the probe locals so the widest declarations come first.
Quoted 15 lines
Quoted 4 lines
struct iio_dev *indio_dev; int ret; int i;...
drivers/iio/adc/hx711.c static int hx711_probe(struct platform_device *pdev) · line 504
Quoted 3 lines
drivers/iio/adc/hx711.c +3
-0+ chip_info =device_get_match_data (dev); +if (!chip_info) +return dev_err_probe (dev, -ENODEV ,"missing match data\n" );Since we are here, it's not match data, it's driver data. "missing driver data".
Quoted 1 line
+
hx711_data->chip_info=chip_info;
Quoted 9 lines
+ Blank line.
Quoted 1 line
/*--
With Best Regards,
Andy Shevchenko
Will make changes as suggested and give v5
Re: [PATCH v4 4/7] iio: adc: hx711: introduce hx711_chip_info per-variant structure
In reply to Andy ShevchenkoQuoted 6 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko
On Mon, Apr 27, 2026 at 03:39:35PM +0530, Piyush Patle wrote:
...
Quoted 8 lines
Quoted 5 lines
drivers/iio/adc/hx711.c static int hx711_probe(struct platform_device *pdev) · line 504
Quoted 3 lines
drivers/iio/adc/hx711.c +3
-0+ chip_info =device_get_match_data (dev); +if (!chip_info) +return dev_err_probe (dev, -ENODEV ,"missing match data\n" );Since we are here, it's not match data, it's driver data. "missing driver data".
And does it need
property.hto be included?
property.h is already present in the driver
Quoted 2 lines
Also check if
dev_printk.handerrno.hare there (it may be that driver already usesdevice.handerr.hthat cover these two).
platform_device.h pulls in device.h which covers dev_err_probe(). err.h covers the errno values. No new includes are needed for this patch.
Quoted 6 lines
--
With Best Regards,
Andy Shevchenko
Re: [PATCH v4 5/7] iio: adc: hx711: pass trailing pulse count into hx711_read()
In reply to Andy ShevchenkoQuoted 14 lines · Andy Shevchenko
Quoted 9 lines · Piyush Patle
Move the trailing-pulse computation out of
hx711_read()and into the callers.hx711_read()now takes an explicit 'trailing_pulses' parameter so the HX710B support patch can pass the per-channel count stored inchan->addresswithout adding a separate code path inhx711_read().Use scoped loop variables (for (int i = ...) / for (unsigned int i = ...)) to tighten the scope of the loop counters.
No functional change.
...
Quoted 9 lines
drivers/iio/adc/hx711.c static int hx711_cycle(struct hx711_data *hx711_data) · line 162
Quoted 7 lines
drivers/iio/adc/hx711.c +2
-2-static int hx711_read (struct hx711_data *hx711_data) +static int hx711_read (struct hx711_data *hx711_data,unsigned int trailing_pulses) { -int i, ret; +int ret;int value =0 ;int val =gpiod_get_value (hx711_data->gpiod_dout);Try to follow the reversed xmas tree order.
Will reorder hx711_read() locals
Quoted 5 lines
drivers/iio/adc/hx711.c static int hx711_read(struct hx711_data *hx711_data) · line 168
Quoted 3 lines
drivers/iio/adc/hx711.c +1
-1-for (i =0 ; i <24 ; i++) { +for (int i =0 ; i <24 ; i++) {Why is 'i' signed?
Will change to unsigned int i in the scoped-iterator patch.
Quoted 8 lines
drivers/iio/adc/hx711.c static int hx711_read(struct hx711_data *hx711_data) · line 178
Quoted 4 lines
drivers/iio/adc/hx711.c +1
-1-for (i =0 ; i <hx711_get_gain_to_pulse (hx711_data->gain_set); i++) +for (unsigned int i =0 ; i < trailing_pulses; i++)hx711_cycle (hx711_data);With the above the both seems like a candidate to a separate change (after this
trailing_pulsespatch).
Quoted 8 lines
Quoted 1 line
return value;...
So, split this to two:
-trailing_pulses(without touching 'i')
- localising loop iterators
Will split in v5.
Quoted 5 lines
--
With Best Regards,
Andy Shevchenko
Re: [PATCH v4 7/7] iio: adc: hx711: add support for HX710B
In reply to Andy ShevchenkoQuoted 96 lines · Andy Shevchenko
Quoted 27 lines · Piyush Patle
Add support for the AVIA HX710B ADC, which shares the HX711 GPIO interface but has a fixed gain of 128 and uses trailing
PD_SCKpulses to select the active channel rather than the gain.The HX710B has three operating modes controlled by the trailing pulse count after the 24 data bits (Table 3 in the HX710B datasheet):
25 pulses (1 trailing): differential input at 10 SPS
26 pulses (2 trailing): DVDD-AVDD supply monitor at 40 SPS 27 pulses (3 trailing): differential input at 40 SPSModel the HX710B with its own hx710b_chan_spec[] and
hx710b_iio_info. Store the trailing pulse count inchan->addresssohx710b_set_channel()can switch channels without a separate lookup table. The supply monitor uses .channel = 2 to avoid aliasing the channel2 terminal (index 1) of the differential pair.The HX710B has a dedicated VREF pin for the ADC reference voltage. The driver tries vref-supply first; if absent it falls back to avdd-supply (for boards where VREF is tied to AVDD). The HX711 uses AVDD as its reference and is unaffected.
Add
fixed_gainandfixed_gain_valfields tohx711_chip_infoto carry the gain into the scale calculation. Store a per-instance scale inhx711_datafor HX710B and use it inhx711_read_raw()whenfixed_gainis set. Updatehx711_reset()to resetchannel_seton HX710B after a power-down cycle.Quoted 3 lines
Enlarge the trigger buffer from 2 to 3 channels plus a pad word to keep the timestamp naturally aligned; HX711 continues to use only the first two slots.
So, missed
types.hshould be in this patch....
Quoted 10 lines
/**
* structhx711_chip_info- per-variant static configuration- * @name: IIO device name - * @channels: channel specification array - * @iio_info: IIO info ops for this variant - * @num_channels: number of entries in @channels + * @name: IIO device name + * @channels: channel specification array + * @iio_info: IIO info ops for this variant + * @num_channels: number of entries in @channelsQuoted 2 lines
+ * @fixed_gain_val: fixed PGA gain (used when @fixed_gain is true)
+ * @fixed_gain: true if the variant has a fixed ADC gain (e.g. HX710B)No, make sure there will be no '-' lines here.
Quoted 1 line
*/
...
drivers/iio/adc/hx711.c struct hx711_data { · line 106
Quoted 9 lines
struct
hx711_data{int gain_set; /* gain set on device */ int gain_chan_a; /* gain for channel A */ int gain_scale[HX711_GAIN_MAX];drivers/iio/adc/hx711.c +2
-0+unsigned int channel_set;/* HX710B active channel */ const struct hx711_chip_info *chip_info; +unsigned int scale;/* HX710B fixed-gain scale */ Semantically the scale is closer to the above gain_* ones AFAICS.
Quoted 13 lines
struct mutex lock; /* * triggered bufferdrivers/iio/adc/hx711.c +3
-2-* 2x32-bit channel + 64-bit naturally aligned timestamp +* up to 3x32-bit channels + pad + 64-bit naturally aligned timestamp */ struct { -u32 channel[2 ]; +u32 channel[3 ]; +u32 pad;aligned_s64 timestamp; } buffer;/* ...
Quoted 6 lines
static int
hx711_reset(struct hx711_data *hx711_data){+ const struct hx711_chip_info *info = hx711_data->chip_info; int val = hx711_wait_for_ready(hx711_data); if (val) {The rule of thumb is to split definition and assignment for the variables that are going to be validated.
Here I expect to see
Quoted 9 lines
int val; val = hx711_wait_for_ready(hx711_data); if (val) {But since it's not related directly to this change, make a preparatory patch. Because now with two variables this looks worse and adds more potential to be mistaken in the future.
Will add a preparatory patch before the HX710B patch that splits int val = hx711_wait_for_ready(hx711_data) into a plain declaration followed by a separate assignment.
Quoted 51 lines
Quoted 21 lines
drivers/iio/adc/hx711.c +20
-0+static int hx710b_set_channel (struct hx711_data *hx711_data, +const struct iio_chan_spec *chan) +{ +int ret; + +if (hx711_data->channel_set == (unsigned int )chan->channel) +return 0 ; + + ret =hx711_read (hx711_data, chan->address); +if (ret <0 ) +return ret; + + ret =hx711_wait_for_ready (hx711_data); +if (ret) +return ret; + + hx711_data->channel_set = chan->channel; + +return 0 ; +}...
drivers/iio/adc/hx711.c static int hx711_reset_read(struct hx711_data *hx711_data, · line 332
Quoted 12 lines
drivers/iio/adc/hx711.c +5
-4- ret =hx711_set_gain_for_channel (hx711_data, chan->channel); -if (ret <0 ) -return ret; + ret =hx711_set_gain_for_channel (hx711_data, chan->channel); +if (ret <0 ) +return ret; + trailing_pulses =hx711_get_gain_to_pulse (hx711_data->gain_set); + } - trailing_pulses =hx711_get_gain_to_pulse (hx711_data->gain_set);return hx711_read (hx711_data, trailing_pulses);Instead of the above, first split these lines to a helper
static int
hx711_set_hx711_channel(..., *tp){ int ret; ret = hx711_set_gain_for_channel(hx711_data, chan->channel); if (ret < 0) return ret; *tp = hx711_get_gain_to_pulse(hx711_data->gain_set); return 0; }
Quoted 4 lines
And name your new function something like
hx711_set_hx710b_channel()
Good idea. Will extract the existing gain-switching logic from hx711_reset_read()
into a hx711_set_hx711_channel() helper in the iio_chan_spec refactor patch (patch 6), then add hx711_set_hx710b_channel() symmetrically in patch 7. The current hx710b_set_channel() name will change to hx711_set_hx710b_channel() to match the naming convention.
Quoted 10 lines
...
drivers/iio/adc/hx711.c static int hx711_probe(struct platform_device *pdev) · line 655
Quoted 4 lines
drivers/iio/adc/hx711.c +4
-0+/* +* The HX710B uses the VREF pin as the ADC reference; try vref-supply +* first and fall back to avdd-supply when VREF is tied to AVDD on the +* board. The HX711 uses AVDD as its reference. I think we use a single space in the comment, but double check that. The rule of thumb is to keep the original driver style and change it separately if required.
The rest of the driver uses single spaces. Will fix
Quoted 9 lines
Quoted 2 lines
+ */
...
Quoted 2 lines
/*
* withWhile at it, with --> With.
Will fix.
Quoted 29 lines
Quoted 16 lines
drivers/iio/adc/hx711.c +5
-5-* full scale differential input range: AVDD / GAIN +* full scale differential input range: VREF / GAIN * full scale output data: 2^24 * we can say: -* AVDD / GAIN = 2^24 +* VREF / GAIN = 2^24 * therefore: -* 1 LSB = AVDD / GAIN / 2^24 -* AVDD is in uV, but we need 10^-9 mV +* 1 LSB = VREF / GAIN / 2^24 +* VREF 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 = (VREF * 100) / GAIN / 1678 [10^-9 mV] */ ...
Quoted 10 lines
drivers/iio/adc/hx711.c +6
-3-for (i =0 ; i <HX711_GAIN_MAX ; i++) - hx711_data->gain_scale[i] = - ret / hx711_gain_to_scale[i].gain /1678 ; +if (chip_info->fixed_gain) { + hx711_data->scale = ret / chip_info->fixed_gain_val /1678 ; + }else { +for (i =0 ; i <HX711_GAIN_MAX ; i++) + hx711_data->gain_scale[i] = + ret / hx711_gain_to_scale[i].gain /1678 ;
Quoted 9 lines
drivers/iio/adc/hx711.c static int hx711_probe(struct platform_device *pdev) · line 689
Quoted 5 lines
drivers/iio/adc/hx711.c +3
-2- hx711_data->gain_set =128 ; - hx711_data->gain_chan_a =128 ; + hx711_data->gain_set =128 ; + hx711_data->gain_chan_a =128 ; + }Looking at this I'm wondering wouldn't be easier just to have the array, but feel it with the same data.
Yes, agreed.
In v5 I will drop the separate scale field and the fixed_gain special-casing in hx711_read_raw()
For HX710B, probe will populate gain_scale[] with the same scale value for all entries based on the fixed gain, and set gain_set to fixed_gain_val. reusing the existing hx711_get_gain_to_scale() path without any variant-specific handling.
Quoted 8 lines
...
drivers/iio/adc/hx711.c static int hx711_probe(struct platform_device *pdev)
Quoted 3 lines
drivers/iio/adc/hx711.c +1
-1- { .compatible = "avia,hx711", .data = &hx711_chip }, + { .compatible = "avia,hx711", .data = &hx711_chip },Why?! This is stray change.
Will drop stray alignment spaces.
Quoted 6 lines
--
With Best Regards,
Andy Shevchenko
Jonathan Cameron
maintainerRe: [PATCH v4 1/7] dt-bindings: iio: adc: avia-hx711: add avia,hx710b compatible
In reply to Piyush PatleQuoted 4 lines · On Mon, 27 Apr 2026 15:39:32 +0530 Piyush Patle <[email protected]>…
The HX710B shares the same two-wire interface as the HX711 but differs in its channel and gain model: gain is fixed at 128 and the number of trailing
PD_SCKpulses selects the input channel rather than the gain.
Quoted 8 lines
Add avia,hx710b to the compatible enum. Document the chip differences in the description and add chip-specific supply properties (dvdd-supply, vsup-supply, vref-supply) and a rate-gpios property for the HX711 RATE pin. Add allOf constraints that forbid HX711-only properties on HX710B nodes and vice versa. Clarify the clock-frequency description to reflect its actual purpose: controlling the SCK bit-bang timing.
Signed-off-by Piyush Patle <[email protected]>
Hi Piyush,
This is doing at least 3 different things. See below for why I think it needs to be 3 patches.
Quoted 48 lines
---
Changes in v4:
- Add vref-supply for the HX710B VREF reference voltage pin.
- Remove dvdd-supply from the HX710B forbidden properties list; the HX710B has a DVDD supply and the DVDD-AVDD channel relies on it.
- Add allOf block forbidding vref-supply on HX711 nodes.
- Add an HX710B example showing vref-supply.
- Update description: avoid specific channel-number references in hardware text.Changes in v3:
- Drop the vref-supply mention from avdd-supply; no such binding property exists.
- Drop the clock-frequency sentence that repeated the schema default.
- Restore the example node name to weight.
- Remove the separate HX710B example.Changes in v2:
- Remove driver implementation details from the description and describe hardware behaviour only.
- Drop unrelated punctuation cleanup.
- Add dvdd-supply and vsup-supply optional properties for HX711.
- Add rate-gpios optional property for the HX711 RATE pin and forbid it on HX710B.
- Add the allOf if/then block forbidding HX711-only properties on HX710B nodes.
- Clarify clock-frequency as SCK bit-bang timing, not a crystal or external clock input.
- Sort compatible enum alphabetically.
- Remove redundant example comments.
- Update the HX711 example to exercise rate-gpios.--- .../bindings/iio/adc/avia-hx711.yaml | 82 +++++++++++++++---- 1 file changed, 67 insertions(+), 15 deletions(-)Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +1
-1@@ -4,49 +4,91 @@ $id: http://devicetree.org/schemas/iio/adc/avia-hx711.yaml# $schema: http://devicetree.org/meta-schemas/core.yaml# -title: AVIA HX711 ADC chip for weight cells +title: AVIA HX711 and HX710B ADCs maintainers: - Andreas Klinger <[email protected]>
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml line 15
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +3
-5description: | - Bit-banging driver using two GPIOs: - - sck-gpio gives a clock to the sensor with 24 cycles for data retrieval - and up to 3 cycles for selection of the input channel and gain for the - next measurement - - dout-gpio is the sensor data the sensor responds to the clock + The HX711 is a 24-bit ADC with selectable gain (32/64/128) and two + differential input channels. Channel A supports gain 64 and 128; + channel B supports gain 32.
Do this in two patches. First one changes like this that apply to existing HX711 binding and second one that adds the HX710B elements (and any text about stuff being specific to the HX711 as that only makes sense when adding the new stuff.
Quoted 8 lines
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +3
-2- Specifications about the driver can be found at: - http://www.aviaic.com/ENProducts.aspx + The HX710B is a 24-bit ADC with fixed gain of 128. One input measures + the differential voltage between the two input pins; a second measures + the DVDD-AVDD supply voltage difference for battery level detection. properties:
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml line 29
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +2
-2compatible: enum: + - avia,hx710b - avia,hx711 sck-gpios: description: - Definition of the GPIO for the clock (output). In the datasheet it is - named PD_SCK + GPIO for the clock output (PD_SCK in the datasheet).
This cleanup goes in that first patch.
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml line 34
Quoted 9 lines
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +1
-4maxItems: 1 dout-gpios: description: - Definition of the GPIO for the data-out sent by the sensor in - response to the clock (input). - See Documentation/devicetree/bindings/gpio/gpio.txt for information - on how to specify a consumer gpio. + GPIO for the data output from the sensor (DOUT in the datasheet).
As does this.
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml line 40
Quoted 7 lines
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +2
-1maxItems: 1 avdd-supply: description: - Definition of the regulator used as analog supply + Analog supply voltage (AVDD). Also serves as the voltage reference on + both chips.
Don't talk about 'both' chips. Also it doesn't if vref is supplied. So I'd drop the sentence entirely.
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml line 51
Quoted 11 lines
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +11
-0+ + dvdd-supply: + description: + Digital supply voltage (DVDD). For the HX710B, DVDD must be greater + than or equal to AVDD. When DVDD is a battery rail and AVDD is a + regulated supply, one channel monitors the DVDD-AVDD difference for + battery level detection. + + vsup-supply: + description: + Supply voltage for the on-chip regulator (VSUP). HX711 only.
This applies to the existing hx711 binding rather than being related to the new stuff. Separate patch with an explanation. This isn't just tidy up so needs to be the 2nd patch after tidy up and before hx710b addition.
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml line 56
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +5
-0+ + vref-supply: + description: + Reference voltage input (VREF). HX710B only. When omitted, the driver + assumes VREF is tied to AVDD on the board.
Is it an external pin? If so no such assumption. It should be specified. If both end up pointing at same regulator that's fine. This is for a new device that wasn't supported before so we don't need to be careful with backwards compatibility - can do it right!
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml line 63
Quoted 7 lines
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +7
-0+ + rate-gpios: + description: + GPIO connected to the RATE pin (HX711 only). When driven low the + output data rate is 10 SPS; when driven high it is 80 SPS. If + omitted the RATE pin state is determined by the board wiring. + maxItems: 1
This is new binding applying to hx711. Put it in a separate patch with explanation of just this bit int he patch description.
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml line 69
Quoted 6 lines
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +4
-0clock-frequency: + description: + Controls the SCK bit-bang timing. The value is used to derive the + delay between SCK edges; keep the SCK high time below 60 us to + avoid triggering chip power-down mode.
Should be in that first hx711 dt binding clean up patch.
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml line 74
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +1
-0minimum: 20000 maximum: 2500000 default: 400000 +allOf:
Bring all this in as part of the hx710b addition patch as first time we need constraints.
Quoted 37 lines
Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml +27
-0+ - if: + properties: + compatible: + const: avia,hx710b + then: + properties: + vsup-supply: false + rate-gpios: false + + - if: + properties: + compatible: + const: avia,hx711 + then: + properties: + vref-supply: false + required: - compatible - sck-gpios@@ -62,6 +104,16 @@ examples: compatible = "avia,hx711"; sck-gpios = <&gpio3 10 GPIO_ACTIVE_HIGH>; dout-gpios = <&gpio0 7 GPIO_ACTIVE_HIGH>; + rate-gpios = <&gpio0 9 GPIO_ACTIVE_HIGH>; avdd-supply = <&avdd>; clock-frequency = <100000>; }; + - | + #include <dt-bindings/gpio/gpio.h> + weight { + compatible = "avia,hx710b"; + sck-gpios = <&gpio3 10 GPIO_ACTIVE_HIGH>; + dout-gpios = <&gpio0 7 GPIO_ACTIVE_HIGH>; + avdd-supply = <&avdd>; + vref-supply = <&vref>; + };
Jonathan Cameron
maintainerRe: [PATCH v4 2/7] iio: adc: hx711: move scale computation to per-device storage
In reply to Piyush PatleQuoted 17 lines · On Tue, 28 Apr 2026 04:08:26 +0530 Piyush Patle <[email protected]>…
Quoted 15 lines · Andy Shevchenko
Quoted 13 lines · Piyush Patle
The gain-to-scale table hx711_gain_to_scale[] is a global array whose .scale fields are overwritten in
hx711_probe()using the AVDD voltage read at probe time. When two HX711 sensors are connected to supplies at different voltages, the second probe call overwrites the scale values computed for the first sensor, silently corrupting its readings.Fix this by removing the .scale field from the global table, making the table const, and adding a per-instance gain_scale[] array to
hx711_data. Populate gain_scale[] inhx711_probe()using the device's own AVDD regulator voltage. Updatehx711_get_gain_to_scale()andhx711_get_scale_to_gain()to take the per-instance array as a parameter, and updatehx711_scale_available_show()to retrieve it viaiio_priv().
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
No functional change for single-sensor configurations.
...
Hi Pisyush
A small process thing. Whilst it may seem less polite than you'd like to be, we are all drowning in email! So don't send a reply if you agree with all the feedback. Feel free to acknowledge the reviewer when you list the changes in the changelog for v5.
Not almost everyone falls into this overreplying trap when they start out so I send this email a lot!
Jonathan
Jonathan Cameron
maintainerRe: [PATCH v4 3/7] iio: adc: hx711: update Kconfig, module description and file header
In reply to Joshua CroftsQuoted 8 lines · On Mon, 27 Apr 2026 15:49:06 +0200 Joshua Crofts <[email protected]>…
Quoted 6 lines · Andy Shevchenko
Quoted 1 line · Joshua Crofts
On Mon, 27 Apr 2026 at 12:23, Piyush Patle <[email protected]> wrote:
...
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
+ which are used for bridge sensors such as weigh cells.
Typo here.
It's better if you point exactly what the typo is. I think you meant "weigh" that should be "weight"?
Yep, that's it, my mistake.
Nope. They are called weigh cells not weight cells.
Quoted 1 line
Jonathan Cameron
maintainerRe: [PATCH v4 4/7] iio: adc: hx711: introduce hx711_chip_info per-variant structure
In reply to Piyush PatleQuoted 14 lines · On Tue, 28 Apr 2026 04:17:20 +0530 Piyush Patle <[email protected]>…
Quoted 12 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko
On Mon, Apr 27, 2026 at 03:39:35PM +0530, Piyush Patle wrote:
...
Quoted 5 lines
drivers/iio/adc/hx711.c static int hx711_probe(struct platform_device *pdev) · line 504
Quoted 3 lines
drivers/iio/adc/hx711.c +3
-0+ chip_info =device_get_match_data (dev); +if (!chip_info) +return dev_err_probe (dev, -ENODEV ,"missing match data\n" );Since we are here, it's not match data, it's driver data. "missing driver data".
Quoted 7 lines
Quoted 2 lines
And does it need
property.hto be included?
property.his already present in the driverQuoted 2 lines
Also check if
dev_printk.handerrno.hare there (it may be that driver already usesdevice.handerr.hthat cover these two).
platform_device.hpulls indevice.hwhich coversdev_err_probe().
That's generally not something we'd count as guaranteed for all time so I'd expect to see dev_printk.h in here. Never include device.h directly in new code unless you really need to. It's an historical artefact - if it was already included though because we actually need it then it would have been fine to assume dev_printk.h was included via device.h
Quoted 8 lines
err.hcovers the errno values. No new includes are needed for this patch.Quoted 6 lines
--
With Best Regards,
Andy Shevchenko
Re: [PATCH v4 3/7] iio: adc: hx711: update Kconfig, module description and file header
In reply to Jonathan CameronQuoted 14 lines · Jonathan Cameron
Quoted 10 lines · On Mon, 27 Apr 2026 15:49:06 +0200 Joshua Crofts <[email protected]>…
Quoted 8 lines · Andy Shevchenko
Quoted 1 line · Joshua Crofts
On Mon, 27 Apr 2026 at 12:23, Piyush Patle <[email protected]> wrote:
...
Quoted 2 lines
Quoted 1 line
+ which are used for bridge sensors such as weigh cells.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Typo here.
It's better if you point exactly what the typo is. I think you meant "weigh" that should be "weight"?
Yep, that's it, my mistake.
Nope. They are called weigh cells not weight cells.
I stand corrected.
Signature · 3 lines
Kind regards CJD
Re: [PATCH v4 3/7] iio: adc: hx711: update Kconfig, module description and file header
In reply to Jonathan CameronQuoted 6 lines · Jonathan Cameron
Quoted 4 lines · On Mon, 27 Apr 2026 15:49:06 +0200 Joshua Crofts <[email protected]>…
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Joshua Crofts
On Mon, 27 Apr 2026 at 12:23, Piyush Patle <[email protected]> wrote:
...
Quoted 10 lines
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
+ which are used for bridge sensors such as weigh cells.
Typo here.
It's better if you point exactly what the typo is. I think you meant "weigh" that should be "weight"?
Yep, that's it, my mistake.
Nope. They are called weigh cells not weight cells.
MODULE_DESCRIPTION() is not aligned. Somewhere a mistake is lurking :-)
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerRe: [PATCH v4 3/7] iio: adc: hx711: update Kconfig, module description and file header
Quoted 14 lines · On Wed, 29 Apr 2026 12:57:51 +0300 Andy Shevchenko…
Quoted 6 lines · Jonathan Cameron
Quoted 4 lines · On Mon, 27 Apr 2026 15:49:06 +0200 Joshua Crofts <[email protected]>…
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Joshua Crofts
On Mon, 27 Apr 2026 at 12:23, Piyush Patle <[email protected]> wrote:
...
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line
+ which are used for bridge sensors such as weigh cells.
Typo here.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
It's better if you point exactly what the typo is. I think you meant "weigh" that should be "weight"?
Yep, that's it, my mistake.
Nope. They are called weigh cells not weight cells.
MODULE_DESCRIPTION()is not aligned. Somewhere a mistake is lurking :-)
True enough but that happens to get cleaned up in a rewording in this patch as it no longer mentions weight or weigh cells.