summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNattan Ferreira <nattanferreira58@gmail.com>2025-06-11 14:42:53 -0300
committerJonathan Cameron <Jonathan.Cameron@huawei.com>2025-06-26 19:32:54 +0100
commit6f6bf97823451dceda121e7341b41ce8d0560c14 (patch)
tree9397a74b2795050556f6d31aac77e48b0926578f
parentfb1d3b24ebf5c85f4179cb045ee28715e249e1ca (diff)
iio: light: apds9306: Refactor threshold get/set functions to use helper
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>
-rw-r--r--drivers/iio/light/apds9306.c36
1 files changed, 20 insertions, 16 deletions
diff --git a/drivers/iio/light/apds9306.c b/drivers/iio/light/apds9306.c
index e9b237de180a..f676da245aa7 100644
--- a/drivers/iio/light/apds9306.c
+++ b/drivers/iio/light/apds9306.c
@@ -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)