[PATCH] iio: light: tsl2591: reduce code duplication in tsl2591_persist_* functions
LucasRabaquim
lucas.rabaquim em usp.br
Sex Abr 17 18:10:21 -03 2026
Add tsl2591_persist_table as a bidirectional lookup table for
tsl2591_persist_cycle_to_lit and tsl2591_persist_lit_to_cycle functions
These functions were prone to errors in the case of adding or updating
TSL2591_PRST_ALS_INT_CYCLE_* values. The functions utilize a for-loop
like in the function tsl2591_als_time_to_fval, allowing it to compare
the cycle value with tsl2591_persist_table[i].cycle and return lit
value with tsl2591_persist_table[i].lit as defined in the new type
tsl2591_persist_entry, reducing the risk of logic mismatches.
Signed-off-by: LucasRabaquim <lucas.rabaquim em usp.br>
Co-developed-by: Matheus Feitosa <matheus.feitosa em usp.br>
Signed-off-by: Matheus Feitosa <matheus.feitosa em usp.br>
---
drivers/iio/light/tsl2591.c | 120 ++++++++++--------------------------
1 file changed, 34 insertions(+), 86 deletions(-)
diff --git a/drivers/iio/light/tsl2591.c b/drivers/iio/light/tsl2591.c
index c5557867ea43..3214a8fccafd 100644
--- a/drivers/iio/light/tsl2591.c
+++ b/drivers/iio/light/tsl2591.c
@@ -170,6 +170,12 @@ struct tsl2591_chip {
bool events_enabled;
};
+/* Persist cycle conversion table mapping register cycles to lit values */
+struct tsl2591_persist_entry {
+ u8 cycle;
+ int lit;
+};
+
/*
* Period table is ALS persist cycle x integration time setting
* Integration times: 100ms, 200ms, 300ms, 400ms, 500ms, 600ms
@@ -229,80 +235,40 @@ static int tsl2591_multiplier_to_gain(const u32 multiplier)
}
}
+static const struct tsl2591_persist_entry 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;
+ for (int i = 0; i < ARRAY_SIZE(tsl2591_persist_table); i++) {
+ if (als_persist == tsl2591_persist_table[i].cycle)
+ 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;
+ for (int i = 0; i < ARRAY_SIZE(tsl2591_persist_table); i++) {
+ if (als_persist == tsl2591_persist_table[i].lit)
+ return tsl2591_persist_table[i].cycle;
}
+ return -EINVAL;
}
static int tsl2591_compatible_int_time(struct tsl2591_chip *chip,
@@ -349,26 +315,7 @@ static int tsl2591_compatible_gain(struct tsl2591_chip *chip, const u8 als_gain)
static int tsl2591_compatible_als_persist_cycle(struct tsl2591_chip *chip,
const u32 als_persist)
{
- switch (als_persist) {
- case TSL2591_PRST_ALS_INT_CYCLE_ANY:
- case TSL2591_PRST_ALS_INT_CYCLE_2:
- case TSL2591_PRST_ALS_INT_CYCLE_3:
- case TSL2591_PRST_ALS_INT_CYCLE_5:
- case TSL2591_PRST_ALS_INT_CYCLE_10:
- case TSL2591_PRST_ALS_INT_CYCLE_15:
- case TSL2591_PRST_ALS_INT_CYCLE_20:
- case TSL2591_PRST_ALS_INT_CYCLE_25:
- case TSL2591_PRST_ALS_INT_CYCLE_30:
- case TSL2591_PRST_ALS_INT_CYCLE_35:
- case TSL2591_PRST_ALS_INT_CYCLE_40:
- case TSL2591_PRST_ALS_INT_CYCLE_45:
- case TSL2591_PRST_ALS_INT_CYCLE_50:
- case TSL2591_PRST_ALS_INT_CYCLE_55:
- case TSL2591_PRST_ALS_INT_CYCLE_60:
- return 0;
- default:
- return -EINVAL;
- }
+ return tsl2591_persist_cycle_to_lit((u8)als_persist) != -EINVAL ? 0 : -EINVAL;
}
static int tsl2591_check_als_valid(struct i2c_client *client)
@@ -421,6 +368,7 @@ static int tsl2591_wait_adc_complete(struct tsl2591_chip *chip)
* Link: https://github.com/adafruit/Adafruit_TSL2591_Library
* Counts Per Lux (CPL) = (ATIME_ms * AGAIN) / LUX DF
* lux = ((C0DATA - C1DATA) * (1 - (C1DATA / C0DATA))) / CPL
+ { TSL2591_PRST_ALS_INT_CYCLE_0, 0 },
*
* Scale values to get more representative value of lux i.e.
* lux = ((C0DATA - C1DATA) * (1000 - ((C1DATA * 1000) / C0DATA))) / CPL
--
2.47.3
Mais detalhes sobre a lista de discussão kernel