]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
iio: light: apds9306: Refactor threshold get/set functions to use helper
authorNattan Ferreira <nattanferreira58@gmail.com>
Wed, 11 Jun 2025 17:42:53 +0000 (14:42 -0300)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Thu, 26 Jun 2025 18:32:54 +0000 (19:32 +0100)
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.

Signed-off-by: Nattan Ferreira <nattanferreira58@gmail.com>
Co-developed-by: Lucas Antonio <lucasantonio.santos@usp.br>
Signed-off-by: Lucas Antonio <lucasantonio.santos@usp.br>
Acked-by: Subhajit Ghosh <subhajit.ghosh@tweaklogic.com>
Link: https://patch.msgid.link/20250611174253.16578-1-nattanferreira58@gmail.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/light/apds9306.c

index e9b237de180a97d6541e2a491ed45381123d4945..f676da245aa758fe0aea3b837ef4a2438c166fc2 100644 (file)
@@ -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)