[PATCH v6 09/11] iio: adc: hx711: localize loop iterators in hx711_read

Piyush Patle
2026-05-03T12:10:57Z

Tighten the scope of the loop variables in hx711_read() now that
trailing-pulse selection is already handled by the callers.

Also replace the 24-bit loop bound with a named constant while touching
the same code.

Suggested-by: Andy Shevchenko <andy@kernel.org>
Signed-off-by: Piyush Patle <piyushpatle228@gmail.com>
---
drivers/iio/adc/hx711.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/adc/hx711.c b/drivers/iio/adc/hx711.c
index b2de8db285b2..98fb617ea32b 100644
--- a/drivers/iio/adc/hx711.c
+++ b/drivers/iio/adc/hx711.c
@@ -24,6 +24,7 @@
#include <linux/regulator/consumer.h>

/* gain to pulse and scale conversion */
+#define HX711_DATA_BITS 24
#define HX711_GAIN_MAX 3
#define HX711_RESET_GAIN 128

@@ -157,9 +158,9 @@ static int hx711_cycle(struct hx711_data *hx711_data)

static int hx711_read(struct hx711_data *hx711_data, int trailing_pulses)
{
- int i, ret;
int value = 0;
int val;
+ int ret;

val = gpiod_get_value(hx711_data->gpiod_dout);

@@ -167,7 +168,7 @@ static int hx711_read(struct hx711_data *hx711_data, int trailing_pulses)
if (val)
return -EIO;

- for (i = 0; i < 24; i++) {
+ for (int i = 0; i < HX711_DATA_BITS; i++) {
value <<= 1;
ret = hx711_cycle(hx711_data);
if (ret)
@@ -176,7 +177,7 @@ static int hx711_read(struct hx711_data *hx711_data, int trailing_pulses)

value ^= 0x800000;

- for (i = 0; i < trailing_pulses; i++)
+ for (int i = 0; i < trailing_pulses; i++)
hx711_cycle(hx711_data);

return value;
--
2.43.0