iio: light: apds9306: Refactor threshold get/set functions to
Subsystems (2)
PATCH 1/1 iio: light: apds9306: Refactor threshold get/set functions to
From: Nattan Ferreira <[email protected]>
Refactor the apds9306_event_thresh_get and apds9306_event_thresh_set functions to use a helper function (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 | 38 ++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 17 deletions(-)
drivers/iio/light/apds9306.c +21 -17
@@ -6,7 +6,6 @@* * Copyright (C) 2024 Subhajit Ghosh <[email protected]> */ -#include <linux/bits.h> #include <linux/cleanup.h> #include <linux/delay.h>
@@ -744,20 +743,28 @@ 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 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 ]; + + reg =get_thresh_reg (dir); - ret =regmap_bulk_read (data->regmap, var, buff,sizeof (buff)); +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 ]; + reg =get_thresh_reg (dir); -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 ; +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
Hello Nattan, Lucas,
Not sure it's worth extrancting the event direction comparison into that separate function. Even though there is less code repetition, we now have an extra comparison to handle get_thresh_reg() return. Despite of that, see comments below.
Quoted 4 lines · nattan
From: Nattan Ferreira <[email protected]>
Refactor the
apds9306_event_thresh_getandapds9306_event_thresh_setfunctions to use a helper function (get_thresh_reg) for obtaining the correct register
Wrap commit description to 75 columns.
Quoted 16 lines
based on the direction of the event. This improves code readability, minimize the number of lines and maintains consistency in accessing threshold registers.
Signed-off-by Nattan Ferreira <[email protected]>Co-developed-by Lucas Antonio <[email protected]>Signed-off-by Lucas Antonio <[email protected]>--- drivers/iio/light/apds9306.c | 38 ++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 17 deletions(-)drivers/iio/light/apds9306.c +0
-0
drivers/iio/light/apds9306.c line 9
Quoted 4 lines
drivers/iio/light/apds9306.c +0
-1@@ -6,7 +6,6 @@* * Copyright (C) 2024 Subhajit Ghosh <[email protected]> */ -
This doesn't seem to have any relation with the intent of the patch. Drop this unrelated change.
drivers/iio/light/apds9306.c static int apds9306_event_period_set(struct apds9306_data *data, int val) · line 746
Quoted 10 lines
drivers/iio/light/apds9306.c +1
-2#include <linux/bits.h> #include <linux/cleanup.h> #include <linux/delay.h> @@ -744,20 +743,28 @@ 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 get_thresh_reg (int dir)
Even though these functions are declared static, it's common patter in IIO that driver functions start with the name of the driver. So, it would be more conventional to call it apds9306_get_thresh_reg().
Quoted 13 lines
drivers/iio/light/apds9306.c +3
-5{ -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 ; +}
drivers/iio/light/apds9306.c static int apds9306_event_period_set(struct apds9306_data *data, int val) · line 760
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 ]; + + reg =get_thresh_reg (dir); - ret =regmap_bulk_read (data->regmap, var, buff,sizeof (buff));
There is now a blank line between function call (apds9306_get_thresh_reg()) and the return check, which is unusual in IIO. Drop the blank line between those
reg = get_thresh_reg(dir); if (reg == -EINVAL)
...
Quoted 8 lines
drivers/iio/light/apds9306.c +4
-0+if (reg == -EINVAL ) +return reg; + + ret =regmap_bulk_read (data->regmap, reg, buff,sizeof (buff));if (ret)return ret;
drivers/iio/light/apds9306.c static int apds9306_event_thresh_get(struct apds9306_data *data, int dir, · line 780
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;u8 buff[3 ];
Add a blank line between local variable declarations and rest of function code.
drivers/iio/light/apds9306.c static int apds9306_event_thresh_get(struct apds9306_data *data, int dir, · line 782
Quoted 2 lines
drivers/iio/light/apds9306.c +1
-0+ reg =get_thresh_reg (dir);
Another blank line between function call and return check to be dropped here.
drivers/iio/light/apds9306.c static int apds9306_event_thresh_get(struct apds9306_data *data, int dir, · line 785
Quoted 9 lines
drivers/iio/light/apds9306.c +2
-6-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 ; +if (reg == -EINVAL ) +return reg;
Regards,
Marcelo
Jonathan Cameron
maintainerQuoted 51 lines · On Sun, 20 Apr 2025 15:26:45 -0300 Marcelo Schmitt…
Hello Nattan, Lucas,
Not sure it's worth extrancting the event direction comparison into that separate function. Even though there is less code repetition, we now have an extra comparison to handle
get_thresh_reg()return. Despite of that, see comments below.Quoted 4 lines · nattan
From: Nattan Ferreira <[email protected]>
Refactor the
apds9306_event_thresh_getandapds9306_event_thresh_setfunctions to use a helper function (get_thresh_reg) for obtaining the correct registerWrap commit description to 75 columns.
Quoted 16 lines
based on the direction of the event. This improves code readability, minimize the number of lines and maintains consistency in accessing threshold registers.
Signed-off-by Nattan Ferreira <[email protected]>Co-developed-by Lucas Antonio <[email protected]>Signed-off-by Lucas Antonio <[email protected]>--- drivers/iio/light/apds9306.c | 38 ++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 17 deletions(-)drivers/iio/light/apds9306.c +0
-0drivers/iio/light/apds9306.c line 9
Quoted 4 lines
drivers/iio/light/apds9306.c +0
-1@@ -6,7 +6,6 @@* * Copyright (C) 2024 Subhajit Ghosh <[email protected]> */ -This doesn't seem to have any relation with the intent of the patch. Drop this unrelated change.
drivers/iio/light/apds9306.c static int apds9306_event_period_set(struct apds9306_data *data, int val) · line 746
Quoted 10 lines
drivers/iio/light/apds9306.c +1
-2#include <linux/bits.h> #include <linux/cleanup.h> #include <linux/delay.h> @@ -744,20 +743,28 @@ 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 get_thresh_reg (int dir)Even though these functions are declared static, it's common patter in IIO that driver functions start with the name of the driver. So, it would be more conventional to call it
apds9306_get_thresh_reg().
Quoted 7 lines
drivers/iio/light/apds9306.c static int apds9306_event_period_set(struct apds9306_data *data, int val) · line 749
Quoted 7 lines
drivers/iio/light/apds9306.c +1
-4{ -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 ;
Just one space after return
Quoted 53 lines
Quoted 6 lines
drivers/iio/light/apds9306.c +2
-1else 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 760
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 ]; + + reg =get_thresh_reg (dir); - ret =regmap_bulk_read (data->regmap, var, buff,sizeof (buff));There is now a blank line between function call (
apds9306_get_thresh_reg()) and the return check, which is unusual in IIO. Drop the blank line between thosereg = get_thresh_reg(dir); if (reg == -EINVAL)...
Quoted 8 lines
drivers/iio/light/apds9306.c +4
-0+if (reg == -EINVAL ) +return reg; + + ret =regmap_bulk_read (data->regmap, reg, buff,sizeof (buff));if (ret)return ret;drivers/iio/light/apds9306.c static int apds9306_event_thresh_get(struct apds9306_data *data, int dir, · line 780
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;u8 buff[3 ];Add a blank line between local variable declarations and rest of function code.
drivers/iio/light/apds9306.c static int apds9306_event_thresh_get(struct apds9306_data *data, int dir, · line 782
Quoted 2 lines
drivers/iio/light/apds9306.c +1
-0+ reg =get_thresh_reg (dir);Another blank line between function call and return check to be dropped here.
drivers/iio/light/apds9306.c static int apds9306_event_thresh_get(struct apds9306_data *data, int dir, · line 785
Quoted 9 lines
drivers/iio/light/apds9306.c +2
-6-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 ; +if (reg == -EINVAL ) +return reg;Regards,
Marcelo
Hello Marcelo,
thank you for the feedback.
Quoted 1 line
Not sure it's worth extracting the event direction comparison into that separate function. Even though there is less code repetition, we now have an extra comparison to handle
get_thresh_reg()return.
I understand your point. The main goal was to reduce code duplication and perhaps improve readability and maintainability, but I agree that it introduces a small trade-off with the additional return check.
Quoted 1 line
Even though these functions are declared static, it's common pattern in IIO that driver functions start with the name of the driver. So, it would be more conventional to call it
apds9306_get_thresh_reg().
Got it. I’ll rename the helper function to follow the IIO driver naming convention.
I understand the blank line problems and I'll correct them.
I’ll send a v2 soon with all these adjustments.
Thanks again for your feedback!
Best regards,
Nattan
Quoted 112 lines · Marcelo Schmitt
Hello Nattan, Lucas,
Not sure it's worth extrancting the event direction comparison into that separate function. Even though there is less code repetition, we now have an extra comparison to handle
get_thresh_reg()return. Despite of that, see comments below.Quoted 4 lines · nattan
From: Nattan Ferreira <[email protected]>
Refactor the
apds9306_event_thresh_getandapds9306_event_thresh_setfunctions to use a helper function (get_thresh_reg) for obtaining the correct registerWrap commit description to 75 columns.
Quoted 16 lines
based on the direction of the event. This improves code readability, minimize the number of lines and maintains consistency in accessing threshold registers.
Signed-off-by Nattan Ferreira <[email protected]>Co-developed-by Lucas Antonio <[email protected]>Signed-off-by Lucas Antonio <[email protected]>--- drivers/iio/light/apds9306.c | 38 ++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 17 deletions(-)drivers/iio/light/apds9306.c +0
-0drivers/iio/light/apds9306.c line 9
Quoted 4 lines
drivers/iio/light/apds9306.c +0
-1@@ -6,7 +6,6 @@* * Copyright (C) 2024 Subhajit Ghosh <[email protected]> */ -This doesn't seem to have any relation with the intent of the patch. Drop this unrelated change.
drivers/iio/light/apds9306.c static int apds9306_event_period_set(struct apds9306_data *data, int val) · line 746
Quoted 10 lines
drivers/iio/light/apds9306.c +1
-2#include <linux/bits.h> #include <linux/cleanup.h> #include <linux/delay.h> @@ -744,20 +743,28 @@ 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 get_thresh_reg (int dir)Even though these functions are declared static, it's common patter in IIO that driver functions start with the name of the driver. So, it would be more conventional to call it
apds9306_get_thresh_reg().Quoted 13 lines
drivers/iio/light/apds9306.c +3
-5{ -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 ; +}drivers/iio/light/apds9306.c static int apds9306_event_period_set(struct apds9306_data *data, int val) · line 760
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 ]; + + reg =get_thresh_reg (dir); - ret =regmap_bulk_read (data->regmap, var, buff,sizeof (buff));There is now a blank line between function call (
apds9306_get_thresh_reg()) and the return check, which is unusual in IIO. Drop the blank line between thosereg = get_thresh_reg(dir); if (reg == -EINVAL)...
Quoted 8 lines
drivers/iio/light/apds9306.c +4
-0+if (reg == -EINVAL ) +return reg; + + ret =regmap_bulk_read (data->regmap, reg, buff,sizeof (buff));if (ret)return ret;drivers/iio/light/apds9306.c static int apds9306_event_thresh_get(struct apds9306_data *data, int dir, · line 780
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;u8 buff[3 ];Add a blank line between local variable declarations and rest of function code.
drivers/iio/light/apds9306.c static int apds9306_event_thresh_get(struct apds9306_data *data, int dir, · line 782
Quoted 2 lines
drivers/iio/light/apds9306.c +1
-0+ reg =get_thresh_reg (dir);Another blank line between function call and return check to be dropped here.
drivers/iio/light/apds9306.c static int apds9306_event_thresh_get(struct apds9306_data *data, int dir, · line 785
Quoted 9 lines
drivers/iio/light/apds9306.c +2
-6-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 ; +if (reg == -EINVAL ) +return reg;Regards,
Marcelo