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

Andy Shevchenko
2026-04-27T14:02:19Z

On Mon, Apr 27, 2026 at 03:39:33PM +0530, Piyush Patle wrote:
> 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.

...

> /*
> * .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 }

I would leave trailing comma, as it is semantically is not the terminator entry.

> };

...

> {
> 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));

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));

> int i, len = 0;

--
With Best Regards,
Andy Shevchenko