Skip to content

Commit 40d7dc9

Browse files
MatheusSilverjic23
authored andcommitted
light: tsl2591: simplify tsl2591_persist functions via lookup table
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. Signed-off-by: Matheus Silveira <matheus.feitosa@usp.br> Co-developed-by: Lucas Rabaquim <lucas.rabaquim@usp.br> Signed-off-by: Lucas Rabaquim <lucas.rabaquim@usp.br> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent 3686272 commit 40d7dc9

1 file changed

Lines changed: 28 additions & 66 deletions

File tree

drivers/iio/light/tsl2591.c

Lines changed: 28 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,24 @@ static const int tsl2591_calibscale_available[] = {
192192
1, 25, 428, 9876,
193193
};
194194

195+
static const u8 tsl2591_persist_table[] = {
196+
[TSL2591_PRST_ALS_INT_CYCLE_ANY] = 1,
197+
[TSL2591_PRST_ALS_INT_CYCLE_2] = 2,
198+
[TSL2591_PRST_ALS_INT_CYCLE_3] = 3,
199+
[TSL2591_PRST_ALS_INT_CYCLE_5] = 5,
200+
[TSL2591_PRST_ALS_INT_CYCLE_10] = 10,
201+
[TSL2591_PRST_ALS_INT_CYCLE_15] = 15,
202+
[TSL2591_PRST_ALS_INT_CYCLE_20] = 20,
203+
[TSL2591_PRST_ALS_INT_CYCLE_25] = 25,
204+
[TSL2591_PRST_ALS_INT_CYCLE_30] = 30,
205+
[TSL2591_PRST_ALS_INT_CYCLE_35] = 35,
206+
[TSL2591_PRST_ALS_INT_CYCLE_40] = 40,
207+
[TSL2591_PRST_ALS_INT_CYCLE_45] = 45,
208+
[TSL2591_PRST_ALS_INT_CYCLE_50] = 50,
209+
[TSL2591_PRST_ALS_INT_CYCLE_55] = 55,
210+
[TSL2591_PRST_ALS_INT_CYCLE_60] = 60,
211+
};
212+
195213
static int tsl2591_set_als_lower_threshold(struct tsl2591_chip *chip,
196214
u16 als_lower_threshold);
197215
static int tsl2591_set_als_upper_threshold(struct tsl2591_chip *chip,
@@ -231,78 +249,22 @@ static int tsl2591_multiplier_to_gain(const u32 multiplier)
231249

232250
static int tsl2591_persist_cycle_to_lit(const u8 als_persist)
233251
{
234-
switch (als_persist) {
235-
case TSL2591_PRST_ALS_INT_CYCLE_ANY:
236-
return 1;
237-
case TSL2591_PRST_ALS_INT_CYCLE_2:
238-
return 2;
239-
case TSL2591_PRST_ALS_INT_CYCLE_3:
240-
return 3;
241-
case TSL2591_PRST_ALS_INT_CYCLE_5:
242-
return 5;
243-
case TSL2591_PRST_ALS_INT_CYCLE_10:
244-
return 10;
245-
case TSL2591_PRST_ALS_INT_CYCLE_15:
246-
return 15;
247-
case TSL2591_PRST_ALS_INT_CYCLE_20:
248-
return 20;
249-
case TSL2591_PRST_ALS_INT_CYCLE_25:
250-
return 25;
251-
case TSL2591_PRST_ALS_INT_CYCLE_30:
252-
return 30;
253-
case TSL2591_PRST_ALS_INT_CYCLE_35:
254-
return 35;
255-
case TSL2591_PRST_ALS_INT_CYCLE_40:
256-
return 40;
257-
case TSL2591_PRST_ALS_INT_CYCLE_45:
258-
return 45;
259-
case TSL2591_PRST_ALS_INT_CYCLE_50:
260-
return 50;
261-
case TSL2591_PRST_ALS_INT_CYCLE_55:
262-
return 55;
263-
case TSL2591_PRST_ALS_INT_CYCLE_60:
264-
return 60;
265-
default:
252+
if (als_persist >= ARRAY_SIZE(tsl2591_persist_table) ||
253+
!tsl2591_persist_table[als_persist])
266254
return -EINVAL;
267-
}
255+
256+
return tsl2591_persist_table[als_persist];
268257
}
269258

270259
static int tsl2591_persist_lit_to_cycle(const u8 als_persist)
271260
{
272-
switch (als_persist) {
273-
case 1:
274-
return TSL2591_PRST_ALS_INT_CYCLE_ANY;
275-
case 2:
276-
return TSL2591_PRST_ALS_INT_CYCLE_2;
277-
case 3:
278-
return TSL2591_PRST_ALS_INT_CYCLE_3;
279-
case 5:
280-
return TSL2591_PRST_ALS_INT_CYCLE_5;
281-
case 10:
282-
return TSL2591_PRST_ALS_INT_CYCLE_10;
283-
case 15:
284-
return TSL2591_PRST_ALS_INT_CYCLE_15;
285-
case 20:
286-
return TSL2591_PRST_ALS_INT_CYCLE_20;
287-
case 25:
288-
return TSL2591_PRST_ALS_INT_CYCLE_25;
289-
case 30:
290-
return TSL2591_PRST_ALS_INT_CYCLE_30;
291-
case 35:
292-
return TSL2591_PRST_ALS_INT_CYCLE_35;
293-
case 40:
294-
return TSL2591_PRST_ALS_INT_CYCLE_40;
295-
case 45:
296-
return TSL2591_PRST_ALS_INT_CYCLE_45;
297-
case 50:
298-
return TSL2591_PRST_ALS_INT_CYCLE_50;
299-
case 55:
300-
return TSL2591_PRST_ALS_INT_CYCLE_55;
301-
case 60:
302-
return TSL2591_PRST_ALS_INT_CYCLE_60;
303-
default:
304-
return -EINVAL;
261+
for (unsigned int i = TSL2591_PRST_ALS_INT_CYCLE_ANY;
262+
i < ARRAY_SIZE(tsl2591_persist_table); i++) {
263+
if (als_persist == tsl2591_persist_table[i])
264+
return i;
305265
}
266+
267+
return -EINVAL;
306268
}
307269

308270
static int tsl2591_compatible_int_time(struct tsl2591_chip *chip,

0 commit comments

Comments
 (0)