Linux IIO development
 help / color / mirror / Atom feed
* [PATCH v3] iio: light: apds9306: Refactor threshold get/set functions to use helper
@ 2025-04-22 19:11 nattan
  2025-04-24 13:04 ` Subhajit Ghosh
  0 siblings, 1 reply; 7+ messages in thread
From: nattan @ 2025-04-22 19:11 UTC (permalink / raw)
  To: subhajit.ghosh, jic23; +Cc: lucasantonio.santos, Nattan Ferreira, linux-iio

From: Nattan Ferreira <nattanferreira58@gmail.com>

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.

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>
---
 drivers/iio/light/apds9306.c | 36 ++++++++++++++++++++----------------
 1 file changed, 20 insertions(+), 16 deletions(-)

diff --git a/drivers/iio/light/apds9306.c b/drivers/iio/light/apds9306.c
index 69a0d609c..9216d4974 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)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v3] iio: light: apds9306: Refactor threshold get/set functions to use helper
  2025-04-22 19:11 nattan
@ 2025-04-24 13:04 ` Subhajit Ghosh
  2025-05-26 23:33   ` Nattan Ferreira
  0 siblings, 1 reply; 7+ messages in thread
From: Subhajit Ghosh @ 2025-04-24 13:04 UTC (permalink / raw)
  To: nattan, jic23; +Cc: lucasantonio.santos, linux-iio

Hi Nattan, Lucas,

> 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
It actually adds four more lines to the driver file. Rephrase maybe.

>   drivers/iio/light/apds9306.c | 36 ++++++++++++++++++++----------------
>   1 file changed, 20 insertions(+), 16 deletions(-)
20 additions and 16 deletions.
   
> @@ -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;
I like the name changed from 'var' to reg', makes much more sense.
>   	u8 buff[3];
>
There is always a balance/trade-off between modularity and execution speed.
I agree with Marcelo's reply in the first patch and I also think that separate function for this does not add much value.

Overall the patch looks good to me.

With the above:
Acked-by: Subhajit Ghosh <subhajit.ghosh@tweaklogic.com>

Regards,
Subhajit Ghosh

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3] iio: light: apds9306: Refactor threshold get/set functions to use helper
  2025-04-24 13:04 ` Subhajit Ghosh
@ 2025-05-26 23:33   ` Nattan Ferreira
  0 siblings, 0 replies; 7+ messages in thread
From: Nattan Ferreira @ 2025-05-26 23:33 UTC (permalink / raw)
  To: Subhajit Ghosh; +Cc: jic23, lucasantonio.santos, linux-iio

Hi Subhajit,

Thanks for the feedback.

I’ll send a new version with an updated commit message to better
reflect the intent of the change, focusing on clarity and code
consistency rather than line count reduction.

Best regards,
Nattan

On Thu, Apr 24, 2025 at 10:03 AM Subhajit Ghosh
<subhajit.ghosh@tweaklogic.com> wrote:
>
> Hi Nattan, Lucas,
>
> > 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
> It actually adds four more lines to the driver file. Rephrase maybe.
>
> >   drivers/iio/light/apds9306.c | 36 ++++++++++++++++++++----------------
> >   1 file changed, 20 insertions(+), 16 deletions(-)
> 20 additions and 16 deletions.
>
> > @@ -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;
> I like the name changed from 'var' to reg', makes much more sense.
> >       u8 buff[3];
> >
> There is always a balance/trade-off between modularity and execution speed.
> I agree with Marcelo's reply in the first patch and I also think that separate function for this does not add much value.
>
> Overall the patch looks good to me.
>
> With the above:
> Acked-by: Subhajit Ghosh <subhajit.ghosh@tweaklogic.com>
>
> Regards,
> Subhajit Ghosh

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v3] iio: light: apds9306: Refactor threshold get/set functions to use helper
@ 2025-05-26 23:59 nattan
  2025-05-31 17:23 ` Jonathan Cameron
  2025-06-04  1:33 ` Marcelo Schmitt
  0 siblings, 2 replies; 7+ messages in thread
From: nattan @ 2025-05-26 23:59 UTC (permalink / raw)
  To: subhajit.ghosh, jic23; +Cc: Nattan Ferreira, Lucas Antonio, linux-iio

From: Nattan Ferreira <nattanferreira58@gmail.com>

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>
---
 drivers/iio/light/apds9306.c | 36 ++++++++++++++++++++----------------
 1 file changed, 20 insertions(+), 16 deletions(-)

diff --git a/drivers/iio/light/apds9306.c b/drivers/iio/light/apds9306.c
index 69a0d609c..9216d4974 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)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v3] iio: light: apds9306: Refactor threshold get/set functions to use helper
  2025-05-26 23:59 [PATCH v3] iio: light: apds9306: Refactor threshold get/set functions to use helper nattan
@ 2025-05-31 17:23 ` Jonathan Cameron
  2025-06-05 18:55   ` Nattan Ferreira
  2025-06-04  1:33 ` Marcelo Schmitt
  1 sibling, 1 reply; 7+ messages in thread
From: Jonathan Cameron @ 2025-05-31 17:23 UTC (permalink / raw)
  To: nattan; +Cc: subhajit.ghosh, Lucas Antonio, linux-iio

On Mon, 26 May 2025 20:59:06 -0300
nattan <nattanferreira58@gmail.com> wrote:

> From: Nattan Ferreira <nattanferreira58@gmail.com>
> 
> 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>
> ---
Change log?

Also this seems to be the second v3.

>  drivers/iio/light/apds9306.c | 36 ++++++++++++++++++++----------------
>  1 file changed, 20 insertions(+), 16 deletions(-)
> 
> diff --git a/drivers/iio/light/apds9306.c b/drivers/iio/light/apds9306.c
> index 69a0d609c..9216d4974 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)


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3] iio: light: apds9306: Refactor threshold get/set functions to use helper
  2025-05-26 23:59 [PATCH v3] iio: light: apds9306: Refactor threshold get/set functions to use helper nattan
  2025-05-31 17:23 ` Jonathan Cameron
@ 2025-06-04  1:33 ` Marcelo Schmitt
  1 sibling, 0 replies; 7+ messages in thread
From: Marcelo Schmitt @ 2025-06-04  1:33 UTC (permalink / raw)
  To: nattan; +Cc: subhajit.ghosh, jic23, Lucas Antonio, linux-iio

Hello Nattan, Lucas,

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

On 05/26, nattan wrote:
> From: Nattan Ferreira <nattanferreira58@gmail.com>
> 
> 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.

> 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.
> 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>
> ---
Friendly reminder to add a change log below the --- if you send a v4 (or v5?).

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

Regards,
Marcelo

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3] iio: light: apds9306: Refactor threshold get/set functions to use helper
  2025-05-31 17:23 ` Jonathan Cameron
@ 2025-06-05 18:55   ` Nattan Ferreira
  0 siblings, 0 replies; 7+ messages in thread
From: Nattan Ferreira @ 2025-06-05 18:55 UTC (permalink / raw)
  To: Jonathan Cameron; +Cc: subhajit.ghosh, Lucas Antonio, linux-iio

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

On Sat, May 31, 2025 at 2:23 PM Jonathan Cameron <jic23@kernel.org> wrote:
>
> On Mon, 26 May 2025 20:59:06 -0300
> nattan <nattanferreira58@gmail.com> wrote:
>
> > From: Nattan Ferreira <nattanferreira58@gmail.com>
> >
> > 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>
> > ---
> Change log?
>
> Also this seems to be the second v3.
>
> >  drivers/iio/light/apds9306.c | 36 ++++++++++++++++++++----------------
> >  1 file changed, 20 insertions(+), 16 deletions(-)
> >
> > diff --git a/drivers/iio/light/apds9306.c b/drivers/iio/light/apds9306.c
> > index 69a0d609c..9216d4974 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)
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-06-05 18:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-26 23:59 [PATCH v3] iio: light: apds9306: Refactor threshold get/set functions to use helper nattan
2025-05-31 17:23 ` Jonathan Cameron
2025-06-05 18:55   ` Nattan Ferreira
2025-06-04  1:33 ` Marcelo Schmitt
  -- strict thread matches above, loose matches on Subject: below --
2025-04-22 19:11 nattan
2025-04-24 13:04 ` Subhajit Ghosh
2025-05-26 23:33   ` Nattan Ferreira

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox