Skip to contentSkip to conversation

iio: light: apds9306: Refactor threshold get/set functions to use helper

PATCH Superseded nattan 2 participants 3 messages Apr 22, 2025 – May 26, 2025

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

nattan

author
#0 lore raw

PATCH v3 1/1 iio: light: apds9306: Refactor threshold get/set functions to use helper

From: Nattan Ferreira <[email protected]>

Refactor the apds9306_event_thresh_get and apds9306_event_thresh_set functions to use a helper function (apds9306_get_thresh_reg) for obtaining the correct register based on the direction of the event. This improves code readability,minimize the number of lines and maintains consistency in accessing threshold registers.

---
 drivers/iio/light/apds9306.c | 36 ++++++++++++++++++++----------------
 1 file changed, 20 insertions(+), 16 deletions(-)
drivers/iio/light/apds9306.c +20 -16
index 69a0d609c..9216d4974 100644
@@ -744,20 +744,27 @@ static int apds9306_event_period_set(struct apds9306_data *data, int val)
 	return regmap_field_write(rf->int_persist_val, val);
 }
 
-static int apds9306_event_thresh_get(struct apds9306_data *data, int dir,
-				     int *val)
+static int apds9306_get_thresh_reg(int dir)
 {
-	int var, ret;
-	u8 buff[3];
-
 	if (dir == IIO_EV_DIR_RISING)
-		var = APDS9306_ALS_THRES_UP_0_REG;
+		return APDS9306_ALS_THRES_UP_0_REG;
 	else if (dir == IIO_EV_DIR_FALLING)
-		var = APDS9306_ALS_THRES_LOW_0_REG;
+		return APDS9306_ALS_THRES_LOW_0_REG;
 	else
 		return -EINVAL;
+}
+
+static int apds9306_event_thresh_get(struct apds9306_data *data, int dir,
+				     int *val)
+{
+	int reg, ret;
+	u8 buff[3];
 
-	ret = regmap_bulk_read(data->regmap, var, buff, sizeof(buff));
+	reg = apds9306_get_thresh_reg(dir);
+	if (reg < 0)
+		return reg;
+
+	ret = regmap_bulk_read(data->regmap, reg, buff, sizeof(buff));
 	if (ret)
 		return ret;
 
@@ -769,22 +776,19 @@ static int apds9306_event_thresh_get(struct apds9306_data *data, int dir,
 static int apds9306_event_thresh_set(struct apds9306_data *data, int dir,
 				     int val)
 {
-	int var;
+	int reg;
 	u8 buff[3];
 
-	if (dir == IIO_EV_DIR_RISING)
-		var = APDS9306_ALS_THRES_UP_0_REG;
-	else if (dir == IIO_EV_DIR_FALLING)
-		var = APDS9306_ALS_THRES_LOW_0_REG;
-	else
-		return -EINVAL;
+	reg = apds9306_get_thresh_reg(dir);
+	if (reg < 0)
+		return reg;
 
 	if (!in_range(val, 0, APDS9306_ALS_THRES_VAL_MAX))
 		return -EINVAL;
 
 	put_unaligned_le24(val, buff);
 
-	return regmap_bulk_write(data->regmap, var, buff, sizeof(buff));
+	return regmap_bulk_write(data->regmap, reg, buff, sizeof(buff));
 }
 
 static int apds9306_event_thresh_adaptive_get(struct apds9306_data *data, int *val)
Signature · 1 line
2.34.1
maintainer Acked-by on v3 1/1
#1 lore raw

Hi Nattan, Lucas,

Quoted 4 lines

Refactor the apds9306_event_thresh_get and apds9306_event_thresh_set functions to use a helper function (apds9306_get_thresh_reg) for obtaining the correct register based on the direction of the event. This improves code readability,minimize the number of lines and maintains consistency

It actually adds four more lines to the driver file. Rephrase maybe.

Quoted 2 lines
  drivers/iio/light/apds9306.c | 36 ++++++++++++++++++++----------------
  1 file changed, 20 insertions(+), 16 deletions(-)

20 additions and 16 deletions.

drivers/iio/light/apds9306.c static int apds9306_event_thresh_get(struct apds9306_data *data, int dir, · line 779

Quoted 6 lines
drivers/iio/light/apds9306.c +1 -1
@@ -769,22 +776,19 @@ static int apds9306_event_thresh_get(struct apds9306_data *data, int dir,
  static int apds9306_event_thresh_set(struct apds9306_data *data, int dir,
  				     int val)
  {
-	int var;
+	int reg;

I like the name changed from 'var' to reg', makes much more sense.

drivers/iio/light/apds9306.c static int apds9306_event_thresh_get(struct apds9306_data *data, int dir, · line 781

Quoted 2 lines
drivers/iio/light/apds9306.c +0 -0
  	u8 buff[3];
 

There is always a balance/trade-off between modularity and execution speed.
I agree with Marcelo's reply in the first patch and I also think that separate function for this does not add much value.

Overall the patch looks good to me.

With the above:

Regards,
Subhajit Ghosh

#2 lore raw

Hi Subhajit,

Thanks for the feedback.

I’ll send a new version with an updated commit message to better reflect the intent of the change, focusing on clarity and code consistency rather than line count reduction.

Best regards,
Nattan

Quoted 32 lines · Subhajit Ghosh

Hi Nattan, Lucas,

Quoted 4 lines

Refactor the apds9306_event_thresh_get and apds9306_event_thresh_set functions to use a helper function (apds9306_get_thresh_reg) for obtaining the correct register based on the direction of the event. This improves code readability,minimize the number of lines and maintains consistency

It actually adds four more lines to the driver file. Rephrase maybe.

Quoted 2 lines
  drivers/iio/light/apds9306.c | 36 ++++++++++++++++++++----------------
  1 file changed, 20 insertions(+), 16 deletions(-)

20 additions and 16 deletions.

drivers/iio/light/apds9306.c static int apds9306_event_thresh_get(struct apds9306_data *data, int dir, · line 779

Quoted 6 lines
drivers/iio/light/apds9306.c +1 -1
@@ -769,22 +776,19 @@ static int apds9306_event_thresh_get(struct apds9306_data *data, int dir,
  static int apds9306_event_thresh_set(struct apds9306_data *data, int dir,
                                   int val)
  {
-     int var;
+     int reg;

I like the name changed from 'var' to reg', makes much more sense.

drivers/iio/light/apds9306.c static int apds9306_event_thresh_get(struct apds9306_data *data, int dir, · line 781

Quoted 2 lines
drivers/iio/light/apds9306.c +0 -0
      u8 buff[3];
 

There is always a balance/trade-off between modularity and execution speed.
I agree with Marcelo's reply in the first patch and I also think that separate function for this does not add much value.

Overall the patch looks good to me.

With the above:

Regards,
Subhajit Ghosh