[PATCH v4 5/7] iio: adc: hx711: pass trailing pulse count into hx711_read()

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

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

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

diff --git a/drivers/iio/adc/hx711.c b/drivers/iio/adc/hx711.c
index 2a6e9645f54d..8e77978f062b 100644
--- a/drivers/iio/adc/hx711.c
+++ b/drivers/iio/adc/hx711.c
@@ -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;
--
2.43.0