[PATCH v4 2/7] iio: adc: hx711: move scale computation to per-device storage

Piyush Patle
2026-04-27T10:10:22Z

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[] 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.

Signed-off-by: Piyush Patle <piyushpatle228@gmail.com>
---
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(-)

diff --git a/drivers/iio/adc/hx711.c b/drivers/iio/adc/hx711.c
index 1db8b68a8f64..0776a23bf59b 100644
--- a/drivers/iio/adc/hx711.c
+++ b/drivers/iio/adc/hx711.c
@@ -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;
--
2.43.0