* [PATCH] iio: light: tsl2591: replace persist switch statements with lookup table
@ 2026-06-17 16:42 Caio
2026-06-17 17:01 ` David Lechner
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Caio @ 2026-06-17 16:42 UTC (permalink / raw)
To: jic23, dlechner, nuno.sa, andy; +Cc: Caio, linux-iio
tsl2591_persist_cycle_to_lit() and tsl2591_persist_lit_to_cycle() are
inverse mappings of the same 15-entry table, duplicating the same
pairs across two switch statements. Replace both with a shared
tsl2591_persist_table[] array and simple linear searches, eliminating
the duplication.
Signed-off-by: Caio <caio.groff@usp.br>
---
drivers/iio/light/tsl2591.c | 104 ++++++++++++------------------------
1 file changed, 35 insertions(+), 69 deletions(-)
diff --git a/drivers/iio/light/tsl2591.c b/drivers/iio/light/tsl2591.c
index c5557867e..6626e6d0b 100644
--- a/drivers/iio/light/tsl2591.c
+++ b/drivers/iio/light/tsl2591.c
@@ -229,80 +229,46 @@ static int tsl2591_multiplier_to_gain(const u32 multiplier)
}
}
+struct tsl2591_persist_map {
+ u8 cycle;
+ int lit;
+};
+
+static const struct tsl2591_persist_map 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_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:
- return -EINVAL;
- }
-}
+ int i;
+ for (i = 0; i < ARRAY_SIZE(tsl2591_persist_table); i++)
+ if (tsl2591_persist_table[i].cycle == als_persist)
+ return tsl2591_persist_table[i].lit;
+ return -EINVAL;
+}
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 (tsl2591_persist_table[i].lit == als_persist)
+ return tsl2591_persist_table[i].cycle;
+ return -EINVAL;
}
static int tsl2591_compatible_int_time(struct tsl2591_chip *chip,
--
2.34.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] iio: light: tsl2591: replace persist switch statements with lookup table
2026-06-17 16:42 [PATCH] iio: light: tsl2591: replace persist switch statements with lookup table Caio
@ 2026-06-17 17:01 ` David Lechner
2026-06-19 7:31 ` Joshua Crofts
2026-06-19 9:12 ` Andy Shevchenko
2 siblings, 0 replies; 4+ messages in thread
From: David Lechner @ 2026-06-17 17:01 UTC (permalink / raw)
To: Caio, jic23, nuno.sa, andy; +Cc: linux-iio
On 6/17/26 11:42 AM, Caio wrote:
> tsl2591_persist_cycle_to_lit() and tsl2591_persist_lit_to_cycle() are
> inverse mappings of the same 15-entry table, duplicating the same
> pairs across two switch statements. Replace both with a shared
> tsl2591_persist_table[] array and simple linear searches, eliminating
> the duplication.
>
Isn't this the same thing as [1]?
https://lore.kernel.org/linux-iio/20260522182058.199762-1-matheus.feitosa@usp.br/
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] iio: light: tsl2591: replace persist switch statements with lookup table
2026-06-17 16:42 [PATCH] iio: light: tsl2591: replace persist switch statements with lookup table Caio
2026-06-17 17:01 ` David Lechner
@ 2026-06-19 7:31 ` Joshua Crofts
2026-06-19 9:12 ` Andy Shevchenko
2 siblings, 0 replies; 4+ messages in thread
From: Joshua Crofts @ 2026-06-19 7:31 UTC (permalink / raw)
To: Caio; +Cc: jic23, dlechner, nuno.sa, andy, linux-iio
On Wed, 17 Jun 2026 13:42:58 -0300
Caio <caio.groff@usp.br> wrote:
> tsl2591_persist_cycle_to_lit() and tsl2591_persist_lit_to_cycle() are
> inverse mappings of the same 15-entry table, duplicating the same
> pairs across two switch statements. Replace both with a shared
> tsl2591_persist_table[] array and simple linear searches, eliminating
> the duplication.
>
> Signed-off-by: Caio <caio.groff@usp.br>
> ---
Alongside David's comment, you're missing your full name in the
Signed-off-by tag.
--
Kind regards
CJD
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] iio: light: tsl2591: replace persist switch statements with lookup table
2026-06-17 16:42 [PATCH] iio: light: tsl2591: replace persist switch statements with lookup table Caio
2026-06-17 17:01 ` David Lechner
2026-06-19 7:31 ` Joshua Crofts
@ 2026-06-19 9:12 ` Andy Shevchenko
2 siblings, 0 replies; 4+ messages in thread
From: Andy Shevchenko @ 2026-06-19 9:12 UTC (permalink / raw)
To: Caio; +Cc: jic23, dlechner, nuno.sa, andy, linux-iio
On Wed, Jun 17, 2026 at 01:42:58PM -0300, Caio wrote:
> tsl2591_persist_cycle_to_lit() and tsl2591_persist_lit_to_cycle() are
> inverse mappings of the same 15-entry table, duplicating the same
> pairs across two switch statements. Replace both with a shared
> tsl2591_persist_table[] array and simple linear searches, eliminating
> the duplication.
I have a déjà vu about this...
https://lore.kernel.org/linux-iio/20260528185912.24774-1-matheus.feitosa@usp.br/
Ah, and this what was applied already. please, base your code on the respective
tree and branch.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-06-19 9:12 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-17 16:42 [PATCH] iio: light: tsl2591: replace persist switch statements with lookup table Caio
2026-06-17 17:01 ` David Lechner
2026-06-19 7:31 ` Joshua Crofts
2026-06-19 9:12 ` Andy Shevchenko
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.