Skip to content

Commit e650651

Browse files
M-Vaittinenjic23
authored andcommitted
iio: gts-helpers: fix integration time units
The IIO ABI mandates expressing integration times in seconds. The GTS helper errorneously uses micro seconds in integration_times_available. Fix this by converting the lists to IIO_VAL_INT_PLUS_MICRO. Fixes: 38416c2 ("iio: light: Add gain-time-scale helpers") Signed-off-by: Matti Vaittinen <[email protected]> Link: https://lore.kernel.org/r/eeacd192c259e885850b5a2dd8b776bccfc44fa8.1681722914.git.mazziesaccount@gmail.com Signed-off-by: Jonathan Cameron <[email protected]>
1 parent b7b04f3 commit e650651

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

drivers/iio/industrialio-gts-helper.c

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,17 @@ static int iio_gts_build_avail_scale_table(struct iio_gts *gts)
337337
return ret;
338338
}
339339

340+
static void iio_gts_us_to_int_micro(int *time_us, int *int_micro_times,
341+
int num_times)
342+
{
343+
int i;
344+
345+
for (i = 0; i < num_times; i++) {
346+
int_micro_times[i * 2] = time_us[i] / 1000000;
347+
int_micro_times[i * 2 + 1] = time_us[i] % 1000000;
348+
}
349+
}
350+
340351
/**
341352
* iio_gts_build_avail_time_table - build table of available integration times
342353
* @gts: Gain time scale descriptor
@@ -351,7 +362,7 @@ static int iio_gts_build_avail_scale_table(struct iio_gts *gts)
351362
*/
352363
static int iio_gts_build_avail_time_table(struct iio_gts *gts)
353364
{
354-
int *times, i, j, idx = 0;
365+
int *times, i, j, idx = 0, *int_micro_times;
355366

356367
if (!gts->num_itime)
357368
return 0;
@@ -378,13 +389,24 @@ static int iio_gts_build_avail_time_table(struct iio_gts *gts)
378389
}
379390
}
380391
}
381-
gts->avail_time_tables = times;
382-
/*
383-
* This is just to survive a unlikely corner-case where times in the
384-
* given time table were not unique. Else we could just trust the
385-
* gts->num_itime.
386-
*/
387-
gts->num_avail_time_tables = idx;
392+
393+
/* create a list of times formatted as list of IIO_VAL_INT_PLUS_MICRO */
394+
int_micro_times = kcalloc(idx, sizeof(int) * 2, GFP_KERNEL);
395+
if (int_micro_times) {
396+
/*
397+
* This is just to survive a unlikely corner-case where times in
398+
* the given time table were not unique. Else we could just
399+
* trust the gts->num_itime.
400+
*/
401+
gts->num_avail_time_tables = idx;
402+
iio_gts_us_to_int_micro(times, int_micro_times, idx);
403+
}
404+
405+
gts->avail_time_tables = int_micro_times;
406+
kfree(times);
407+
408+
if (!int_micro_times)
409+
return -ENOMEM;
388410

389411
return 0;
390412
}
@@ -683,8 +705,8 @@ int iio_gts_avail_times(struct iio_gts *gts, const int **vals, int *type,
683705
return -EINVAL;
684706

685707
*vals = gts->avail_time_tables;
686-
*type = IIO_VAL_INT;
687-
*length = gts->num_avail_time_tables;
708+
*type = IIO_VAL_INT_PLUS_MICRO;
709+
*length = gts->num_avail_time_tables * 2;
688710

689711
return IIO_AVAIL_LIST;
690712
}

0 commit comments

Comments
 (0)