Skip to contentSkip to conversation

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

PATCH Superseded nattan 3 participants 4 messages May 26, 2025 – Jun 5, 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 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
#1 lore raw
Quoted 8 lines · On Mon, 26 May 2025 20:59:06 -0300 nattan <[email protected]> wrote:

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 and maintains consistency
in accessing threshold registers.

Quoted 4 lines

---

Change log?

Also this seems to be the second v3.

Quoted 71 lines
 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)
#2 lore raw

Hi Jonathan,

I accidentally sent the v4 patch with the v3 description. Please kindly disregard this patch.

I will send the corrected v4 shortly with the proper description.

Thank you for your understanding.

Best regards,
Nattan Ferreira

Quoted 92 lines · Jonathan Cameron
Quoted 8 lines · On Mon, 26 May 2025 20:59:06 -0300 nattan <[email protected]> wrote:

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 and maintains consistency
in accessing threshold registers.

Quoted 4 lines

---

Change log?

Also this seems to be the second v3.

Quoted 71 lines
 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)
#3 lore raw
In reply to nattan

Hello Nattan, Lucas,

This version looks good to me.
One minor thing below.

Quoted 4 lines · nattan

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

nitpicking: commit message and description are wrapped to 75 columns.

Quoted 2 lines

correct register based on the direction of the event. This improves code readability and maintains consistency

I think part of the description from the line below could also fit into the line above.

Quoted 6 lines

in accessing threshold registers.

---

Friendly reminder to add a change log below the --- if you send a v4 (or v5?).

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

Regards,
Marcelo