Skip to contentSkip to conversation

iio: light: tsl2591: simplify tsl2591_persist functions via lookup table

PATCH Superseded [email protected] 3 participants 3 messages May 22 – May 26 IIO

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 iio: light: tsl2591: simplify tsl2591_persist functions via lookup table

From: Matheus Silveira <[email protected]>

Replace switch statements with an indexed lookup table for persist cycle conversions.

Both functions contain redundant switch statements. This reduces code duplication and makes future updates to TSL2591_PRST_ALS_INT_CYCLE_* definitions easier to maintain by keeping the mapping in a single place.

---
Changes in V3:
- Remove struct persist_entry and replace the struct list with an array of u8 values
- Remove the loop from tsl2591_persist_cycle_to_lit() as suggested by Andy Shevchenko
- Align tsl2591_persist_lit_to_cycle() with tsl2591_als_time_to_fval()
- Keep the loop in tsl2591_persist_lit_to_cycle() as suggested during discussion by David Lechner

---
 drivers/iio/light/tsl2591.c | 95 +++++++++++--------------------------
 1 file changed, 29 insertions(+), 66 deletions(-)
drivers/iio/light/tsl2591.c +29 -66
index 08476f193a44..0ba2e86a11fc 100644
@@ -192,6 +192,24 @@ static const int tsl2591_calibscale_available[] = {
 	1, 25, 428, 9876,
 };
 
+static const u8 tsl2591_persist_table[] = {
+	[TSL2591_PRST_ALS_INT_CYCLE_ANY] = 1,
+	[TSL2591_PRST_ALS_INT_CYCLE_2]   = 2,
+	[TSL2591_PRST_ALS_INT_CYCLE_3]   = 3,
+	[TSL2591_PRST_ALS_INT_CYCLE_5]   = 5,
+	[TSL2591_PRST_ALS_INT_CYCLE_10]  = 10,
+	[TSL2591_PRST_ALS_INT_CYCLE_15]  = 15,
+	[TSL2591_PRST_ALS_INT_CYCLE_20]  = 20,
+	[TSL2591_PRST_ALS_INT_CYCLE_25]  = 25,
+	[TSL2591_PRST_ALS_INT_CYCLE_30]  = 30,
+	[TSL2591_PRST_ALS_INT_CYCLE_35]  = 35,
+	[TSL2591_PRST_ALS_INT_CYCLE_40]  = 40,
+	[TSL2591_PRST_ALS_INT_CYCLE_45]  = 45,
+	[TSL2591_PRST_ALS_INT_CYCLE_50]  = 50,
+	[TSL2591_PRST_ALS_INT_CYCLE_55]  = 55,
+	[TSL2591_PRST_ALS_INT_CYCLE_60]  = 60,
+};
+
 static int tsl2591_set_als_lower_threshold(struct tsl2591_chip *chip,
 					   u16 als_lower_threshold);
 static int tsl2591_set_als_upper_threshold(struct tsl2591_chip *chip,
@@ -231,78 +249,23 @@ static int tsl2591_multiplier_to_gain(const u32 multiplier)
 
 static int tsl2591_persist_cycle_to_lit(const u8 als_persist)
 {
-	switch (als_persist) {
-	case TSL2591_PRST_ALS_INT_CYCLE_ANY:
-		return 1;
-	case TSL2591_PRST_ALS_INT_CYCLE_2:
-		return 2;
-	case TSL2591_PRST_ALS_INT_CYCLE_3:
-		return 3;
-	case TSL2591_PRST_ALS_INT_CYCLE_5:
-		return 5;
-	case TSL2591_PRST_ALS_INT_CYCLE_10:
-		return 10;
-	case TSL2591_PRST_ALS_INT_CYCLE_15:
-		return 15;
-	case TSL2591_PRST_ALS_INT_CYCLE_20:
-		return 20;
-	case TSL2591_PRST_ALS_INT_CYCLE_25:
-		return 25;
-	case TSL2591_PRST_ALS_INT_CYCLE_30:
-		return 30;
-	case TSL2591_PRST_ALS_INT_CYCLE_35:
-		return 35;
-	case TSL2591_PRST_ALS_INT_CYCLE_40:
-		return 40;
-	case TSL2591_PRST_ALS_INT_CYCLE_45:
-		return 45;
-	case TSL2591_PRST_ALS_INT_CYCLE_50:
-		return 50;
-	case TSL2591_PRST_ALS_INT_CYCLE_55:
-		return 55;
-	case TSL2591_PRST_ALS_INT_CYCLE_60:
-		return 60;
-	default:
+	if (als_persist >= ARRAY_SIZE(tsl2591_persist_table) ||
+	    !tsl2591_persist_table[als_persist])
 		return -EINVAL;
-	}
+
+	return tsl2591_persist_table[als_persist];
 }
 
 static int tsl2591_persist_lit_to_cycle(const u8 als_persist)
 {
-	switch (als_persist) {
-	case 1:
-		return TSL2591_PRST_ALS_INT_CYCLE_ANY;
-	case 2:
-		return TSL2591_PRST_ALS_INT_CYCLE_2;
-	case 3:
-		return TSL2591_PRST_ALS_INT_CYCLE_3;
-	case 5:
-		return TSL2591_PRST_ALS_INT_CYCLE_5;
-	case 10:
-		return TSL2591_PRST_ALS_INT_CYCLE_10;
-	case 15:
-		return TSL2591_PRST_ALS_INT_CYCLE_15;
-	case 20:
-		return TSL2591_PRST_ALS_INT_CYCLE_20;
-	case 25:
-		return TSL2591_PRST_ALS_INT_CYCLE_25;
-	case 30:
-		return TSL2591_PRST_ALS_INT_CYCLE_30;
-	case 35:
-		return TSL2591_PRST_ALS_INT_CYCLE_35;
-	case 40:
-		return TSL2591_PRST_ALS_INT_CYCLE_40;
-	case 45:
-		return TSL2591_PRST_ALS_INT_CYCLE_45;
-	case 50:
-		return TSL2591_PRST_ALS_INT_CYCLE_50;
-	case 55:
-		return TSL2591_PRST_ALS_INT_CYCLE_55;
-	case 60:
-		return TSL2591_PRST_ALS_INT_CYCLE_60;
-	default:
-		return -EINVAL;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(tsl2591_persist_table); i++) {
+		if (als_persist == tsl2591_persist_table[i])
+			return i;
 	}
+
+	return -EINVAL;
 }
 
 static int tsl2591_compatible_int_time(struct tsl2591_chip *chip,
Signature · 1 line
2.43.0
Reviewed-by on v3 1/1
#1 lore raw

Hello Matheus, Lucas.

Quoted 5 lines · On 05/22, [email protected] wrote:

From: Matheus Silveira <[email protected]>

Replace switch statements with an indexed lookup table for persist cycle conversions.

Quoted 8 lines

Both functions contain redundant switch statements. This reduces code duplication and makes future updates to TSL2591_PRST_ALS_INT_CYCLE_* definitions easier to maintain by keeping the mapping in a single place.

Do the above tags got the expected name strings? Maybe you wanted 'Lucas Rabaquim' instead of 'LucasRabaquim' ?

Quoted 15 lines

---
Changes in V3:
- Remove struct persist_entry and replace the struct list with an array of u8 values
- Remove the loop from tsl2591_persist_cycle_to_lit() as suggested by Andy Shevchenko
- Align tsl2591_persist_lit_to_cycle() with tsl2591_als_time_to_fval()
- Keep the loop in tsl2591_persist_lit_to_cycle() as suggested during discussion by David Lechner

---
 drivers/iio/light/tsl2591.c | 95 +++++++++++--------------------------
 1 file changed, 29 insertions(+), 66 deletions(-)
drivers/iio/light/tsl2591.c +0 -0

...

Quoted 2 lines

static int tsl2591_persist_lit_to_cycle(const u8 als_persist) {

...

drivers/iio/light/tsl2591.c static int tsl2591_multiplier_to_gain(const u32 multiplier) · line 261

Quoted 1 line
drivers/iio/light/tsl2591.c +1 -0
+	int i;

Can now use in-loop declarations.

drivers/iio/light/tsl2591.c

Quoted 2 lines
drivers/iio/light/tsl2591.c +2 -0
+
+	for (i = 0; i < ARRAY_SIZE(tsl2591_persist_table); i++) {

Hmm, TSL2591_PRST_ALS_INT_CYCLE_0 is not an index of tsl2591_persist_table. Maybe start looking from index 1?
+ for (unsigned int i = 1; i < ARRAY_SIZE(tsl2591_persist_table); i++) {

drivers/iio/light/tsl2591.c static int tsl2591_multiplier_to_gain(const u32 multiplier) · line 269

Quoted 6 lines
drivers/iio/light/tsl2591.c +4 -0
+		if (als_persist == tsl2591_persist_table[i])
+			return i;
 	}
+
+	return -EINVAL;
 }

With the above nit and double check of tag names,

maintainer
#2 lore raw
Quoted 37 lines · On Sat, 23 May 2026 15:11:28 -0300 Marcelo Schmitt…

Hello Matheus, Lucas.

Quoted 5 lines · On 05/22, [email protected] wrote:

From: Matheus Silveira <[email protected]>

Replace switch statements with an indexed lookup table for persist cycle conversions.

Quoted 8 lines

Both functions contain redundant switch statements. This reduces code duplication and makes future updates to TSL2591_PRST_ALS_INT_CYCLE_* definitions easier to maintain by keeping the mapping in a single place.

Do the above tags got the expected name strings? Maybe you wanted 'Lucas Rabaquim' instead of 'LucasRabaquim' ?

Quoted 15 lines

---
Changes in V3:
- Remove struct persist_entry and replace the struct list with an array of u8 values
- Remove the loop from tsl2591_persist_cycle_to_lit() as suggested by Andy Shevchenko
- Align tsl2591_persist_lit_to_cycle() with tsl2591_als_time_to_fval()
- Keep the loop in tsl2591_persist_lit_to_cycle() as suggested during discussion by David Lechner

---
 drivers/iio/light/tsl2591.c | 95 +++++++++++--------------------------
 1 file changed, 29 insertions(+), 66 deletions(-)
drivers/iio/light/tsl2591.c +0 -0

...

Quoted 2 lines

static int tsl2591_persist_lit_to_cycle(const u8 als_persist) {

Quoted 7 lines

...

drivers/iio/light/tsl2591.c static int tsl2591_multiplier_to_gain(const u32 multiplier) · line 261

Quoted 1 line
drivers/iio/light/tsl2591.c +1 -0
+	int i;  

Can now use in-loop declarations.

drivers/iio/light/tsl2591.c

Quoted 2 lines
drivers/iio/light/tsl2591.c +2 -0
+
+	for (i = 0; i < ARRAY_SIZE(tsl2591_persist_table); i++) {  

Hmm, TSL2591_PRST_ALS_INT_CYCLE_0 is not an index of tsl2591_persist_table. Maybe start looking from index 1?

Good spot. Given the array elements are assigned by defined index values perhaps use TSL2591_PRST_ALS_INT_CYCLE_ANY rather than the numeric value 1?

Quoted 11 lines
(quoted diff) +1 -0
+	for (unsigned int i = 1; i < ARRAY_SIZE(tsl2591_persist_table); i++) {

drivers/iio/light/tsl2591.c static int tsl2591_multiplier_to_gain(const u32 multiplier) · line 269

Quoted 7 lines
drivers/iio/light/tsl2591.c +4 -0
+		if (als_persist == tsl2591_persist_table[i])
+			return i;
 	}
+
+	return -EINVAL;
 }  

With the above nit and double check of tag names,