Skip to contentSkip to conversation

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

PATCH Superseded nattan 2 participants 2 messages Apr 22, 2025

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

nattan

author
#0 lore raw

PATCH v2 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..d46fb45ae 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 == -EINVAL)
+		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 == -EINVAL)
+		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 23 lines · On Tue, 22 Apr 2025 14:15:02 -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,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 +0 -0
index 69a0d609c..d46fb45ae 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);
 }
 

drivers/iio/light/apds9306.c static int apds9306_event_period_set(struct apds9306_data *data, int val) · line 750

Quoted 10 lines
drivers/iio/light/apds9306.c +2 -6
-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;

As per v1 review. There are 2 spaces after that return. Should only be one.

Quoted 6 lines
drivers/iio/light/apds9306.c +2 -1
 	else if (dir == IIO_EV_DIR_FALLING)
-		var = APDS9306_ALS_THRES_LOW_0_REG;
+		return APDS9306_ALS_THRES_LOW_0_REG;
 	else
 		return -EINVAL;
+}

drivers/iio/light/apds9306.c static int apds9306_event_period_set(struct apds9306_data *data, int val) · line 764

Quoted 10 lines
drivers/iio/light/apds9306.c +8 -1
+
+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 == -EINVAL)

Just check if < 0

Quoted 12 lines
drivers/iio/light/apds9306.c +4 -1
+		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;

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

Quoted 10 lines
drivers/iio/light/apds9306.c +2 -6
 	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 == -EINVAL)

As above.

Quoted 12 lines
drivers/iio/light/apds9306.c +2 -1
+		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)