2023-04-17 09:22:43

by Matti Vaittinen

[permalink] [raw]
Subject: [PATCH 0/2] iio: Fix integration time unit

The recently introduced BU27034 used micro-seconds as units for
integration time. So did the GTS-helpers.

The IIO ABI mandates using seconds. This series fixes integration times
for the BU27034 ALS driver and the GTS-helpers.

---

Matti Vaittinen (2):
iio: bu27034: Fix integration time
iio: gts-helpers: fix integration time units

drivers/iio/industrialio-gts-helper.c | 43 ++++++++++++++++++++-------
drivers/iio/light/rohm-bu27034.c | 14 +++++----
2 files changed, 42 insertions(+), 15 deletions(-)


base-commit: c86b0e73f0bebbb0245ef2bac4cf269d61ff828c
--
2.39.2


--
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =]


Attachments:
(No filename) (946.00 B)
signature.asc (499.00 B)
Download all attachments

2023-04-17 09:23:11

by Matti Vaittinen

[permalink] [raw]
Subject: [PATCH 1/2] iio: bu27034: Fix integration time

The bu27034 uses micro seconds for integration time configuration. This
is incorrect as the ABI mandates use of seconds.

Change BU27034 driver to use seconds for integration time.

Signed-off-by: Matti Vaittinen <[email protected]>
---
drivers/iio/light/rohm-bu27034.c | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/drivers/iio/light/rohm-bu27034.c b/drivers/iio/light/rohm-bu27034.c
index e486dcf35eba..25c9b79574a5 100644
--- a/drivers/iio/light/rohm-bu27034.c
+++ b/drivers/iio/light/rohm-bu27034.c
@@ -1167,11 +1167,12 @@ static int bu27034_read_raw(struct iio_dev *idev,

switch (mask) {
case IIO_CHAN_INFO_INT_TIME:
- *val = bu27034_get_int_time(data);
- if (*val < 0)
- return *val;
+ *val = 0;
+ *val2 = bu27034_get_int_time(data);
+ if (*val2 < 0)
+ return *val2;

- return IIO_VAL_INT;
+ return IIO_VAL_INT_PLUS_MICRO;

case IIO_CHAN_INFO_SCALE:
return bu27034_get_scale(data, chan->channel, val, val2);
@@ -1229,7 +1230,10 @@ static int bu27034_write_raw(struct iio_dev *idev,
ret = bu27034_set_scale(data, chan->channel, val, val2);
break;
case IIO_CHAN_INFO_INT_TIME:
- ret = bu27034_try_set_int_time(data, val);
+ if (!val)
+ ret = bu27034_try_set_int_time(data, val2);
+ else
+ ret = -EINVAL;
break;
default:
ret = -EINVAL;
--
2.39.2


--
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =]


Attachments:
(No filename) (1.69 kB)
signature.asc (499.00 B)
Download all attachments

2023-04-17 09:23:33

by Matti Vaittinen

[permalink] [raw]
Subject: [PATCH 2/2] 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.

Signed-off-by: Matti Vaittinen <[email protected]>
---
drivers/iio/industrialio-gts-helper.c | 43 ++++++++++++++++++++-------
1 file changed, 33 insertions(+), 10 deletions(-)

diff --git a/drivers/iio/industrialio-gts-helper.c b/drivers/iio/industrialio-gts-helper.c
index 8bb68975b259..2ab8d2dce019 100644
--- a/drivers/iio/industrialio-gts-helper.c
+++ b/drivers/iio/industrialio-gts-helper.c
@@ -337,6 +337,17 @@ static int iio_gts_build_avail_scale_table(struct iio_gts *gts)
return ret;
}

+static void iio_gts_us_to_int_micro(int *time_us, int *int_micro_times,
+ int num_times)
+{
+ int i;
+
+ for (i = 0; i < num_times; i++) {
+ int_micro_times[i * 2] = time_us[i] / 1000000;
+ int_micro_times[i * 2 + 1] = time_us[i] % 1000000;
+ }
+}
+
/**
* iio_gts_build_avail_time_table - build table of available integration times
* @gts: Gain time scale descriptor
@@ -351,7 +362,7 @@ static int iio_gts_build_avail_scale_table(struct iio_gts *gts)
*/
static int iio_gts_build_avail_time_table(struct iio_gts *gts)
{
- int *times, i, j, idx = 0;
+ int *times, i, j, idx = 0, *int_micro_times;

if (!gts->num_itime)
return 0;
@@ -360,6 +371,7 @@ static int iio_gts_build_avail_time_table(struct iio_gts *gts)
if (!times)
return -ENOMEM;

+
/* Sort times from all tables to one and remove duplicates */
for (i = gts->num_itime - 1; i >= 0; i--) {
int new = gts->itime_table[i].time_us;
@@ -378,13 +390,24 @@ static int iio_gts_build_avail_time_table(struct iio_gts *gts)
}
}
}
- gts->avail_time_tables = times;
- /*
- * This is just to survive a unlikely corner-case where times in the
- * given time table were not unique. Else we could just trust the
- * gts->num_itime.
- */
- gts->num_avail_time_tables = idx;
+
+ /* create a list of times formatted as list of IIO_VAL_INT_PLUS_MICRO */
+ int_micro_times = kcalloc(idx, sizeof(int) * 2, GFP_KERNEL);
+ if (int_micro_times) {
+ /*
+ * This is just to survive a unlikely corner-case where times in
+ * the given time table were not unique. Else we could just
+ * trust the gts->num_itime.
+ */
+ gts->num_avail_time_tables = idx;
+ iio_gts_us_to_int_micro(times, int_micro_times, idx);
+ }
+
+ gts->avail_time_tables = int_micro_times;
+ kfree(times);
+
+ if (!int_micro_times)
+ return -ENOMEM;

return 0;
}
@@ -683,8 +706,8 @@ int iio_gts_avail_times(struct iio_gts *gts, const int **vals, int *type,
return -EINVAL;

*vals = gts->avail_time_tables;
- *type = IIO_VAL_INT;
- *length = gts->num_avail_time_tables;
+ *type = IIO_VAL_INT_PLUS_MICRO;
+ *length = gts->num_avail_time_tables * 2;

return IIO_AVAIL_LIST;
}
--
2.39.2


--
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =]


Attachments:
(No filename) (3.23 kB)
signature.asc (499.00 B)
Download all attachments

2023-04-23 11:06:34

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH 2/2] iio: gts-helpers: fix integration time units

On Mon, 17 Apr 2023 12:20:18 +0300
Matti Vaittinen <[email protected]> wrote:

> 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.
>
> Signed-off-by: Matti Vaittinen <[email protected]>
> ---
> drivers/iio/industrialio-gts-helper.c | 43 ++++++++++++++++++++-------
> 1 file changed, 33 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/iio/industrialio-gts-helper.c b/drivers/iio/industrialio-gts-helper.c
> index 8bb68975b259..2ab8d2dce019 100644
> --- a/drivers/iio/industrialio-gts-helper.c
> +++ b/drivers/iio/industrialio-gts-helper.c
> @@ -337,6 +337,17 @@ static int iio_gts_build_avail_scale_table(struct iio_gts *gts)
> return ret;
> }
>
> +static void iio_gts_us_to_int_micro(int *time_us, int *int_micro_times,
> + int num_times)
> +{
> + int i;
> +
> + for (i = 0; i < num_times; i++) {
> + int_micro_times[i * 2] = time_us[i] / 1000000;
> + int_micro_times[i * 2 + 1] = time_us[i] % 1000000;
> + }
> +}
> +
> /**
> * iio_gts_build_avail_time_table - build table of available integration times
> * @gts: Gain time scale descriptor
> @@ -351,7 +362,7 @@ static int iio_gts_build_avail_scale_table(struct iio_gts *gts)
> */
> static int iio_gts_build_avail_time_table(struct iio_gts *gts)
> {
> - int *times, i, j, idx = 0;
> + int *times, i, j, idx = 0, *int_micro_times;
>
> if (!gts->num_itime)
> return 0;
> @@ -360,6 +371,7 @@ static int iio_gts_build_avail_time_table(struct iio_gts *gts)
> if (!times)
> return -ENOMEM;
>
> +

Grumble.

If nothing else comes up I'll tidy that stray line up when applying.

Note that these will need to wait for after rc1 now so my fixes branch
has moved on to include the code being fixed.

> /* Sort times from all tables to one and remove duplicates */
> for (i = gts->num_itime - 1; i >= 0; i--) {
> int new = gts->itime_table[i].time_us;
> @@ -378,13 +390,24 @@ static int iio_gts_build_avail_time_table(struct iio_gts *gts)
> }
> }
> }
> - gts->avail_time_tables = times;
> - /*
> - * This is just to survive a unlikely corner-case where times in the
> - * given time table were not unique. Else we could just trust the
> - * gts->num_itime.
> - */
> - gts->num_avail_time_tables = idx;
> +
> + /* create a list of times formatted as list of IIO_VAL_INT_PLUS_MICRO */
> + int_micro_times = kcalloc(idx, sizeof(int) * 2, GFP_KERNEL);
> + if (int_micro_times) {
> + /*
> + * This is just to survive a unlikely corner-case where times in
> + * the given time table were not unique. Else we could just
> + * trust the gts->num_itime.
> + */
> + gts->num_avail_time_tables = idx;
> + iio_gts_us_to_int_micro(times, int_micro_times, idx);
> + }
> +
> + gts->avail_time_tables = int_micro_times;
> + kfree(times);
> +
> + if (!int_micro_times)
> + return -ENOMEM;
>
> return 0;
> }
> @@ -683,8 +706,8 @@ int iio_gts_avail_times(struct iio_gts *gts, const int **vals, int *type,
> return -EINVAL;
>
> *vals = gts->avail_time_tables;
> - *type = IIO_VAL_INT;
> - *length = gts->num_avail_time_tables;
> + *type = IIO_VAL_INT_PLUS_MICRO;
> + *length = gts->num_avail_time_tables * 2;
>
> return IIO_AVAIL_LIST;
> }

2023-04-24 05:26:12

by Matti Vaittinen

[permalink] [raw]
Subject: Re: [PATCH 2/2] iio: gts-helpers: fix integration time units

On 4/23/23 14:18, Jonathan Cameron wrote:
> On Mon, 17 Apr 2023 12:20:18 +0300
> Matti Vaittinen <[email protected]> wrote:
>
>> 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.
>>
>> Signed-off-by: Matti Vaittinen <[email protected]>
>> ---
>> drivers/iio/industrialio-gts-helper.c | 43 ++++++++++++++++++++-------
>> 1 file changed, 33 insertions(+), 10 deletions(-)
>>
>> diff --git a/drivers/iio/industrialio-gts-helper.c b/drivers/iio/industrialio-gts-helper.c
>> index 8bb68975b259..2ab8d2dce019 100644
>> --- a/drivers/iio/industrialio-gts-helper.c
>> +++ b/drivers/iio/industrialio-gts-helper.c
>> @@ -337,6 +337,17 @@ static int iio_gts_build_avail_scale_table(struct iio_gts *gts)
>> return ret;
>> }
>>
>> +static void iio_gts_us_to_int_micro(int *time_us, int *int_micro_times,
>> + int num_times)
>> +{
>> + int i;
>> +
>> + for (i = 0; i < num_times; i++) {
>> + int_micro_times[i * 2] = time_us[i] / 1000000;
>> + int_micro_times[i * 2 + 1] = time_us[i] % 1000000;
>> + }
>> +}
>> +
>> /**
>> * iio_gts_build_avail_time_table - build table of available integration times
>> * @gts: Gain time scale descriptor
>> @@ -351,7 +362,7 @@ static int iio_gts_build_avail_scale_table(struct iio_gts *gts)
>> */
>> static int iio_gts_build_avail_time_table(struct iio_gts *gts)
>> {
>> - int *times, i, j, idx = 0;
>> + int *times, i, j, idx = 0, *int_micro_times;
>>
>> if (!gts->num_itime)
>> return 0;
>> @@ -360,6 +371,7 @@ static int iio_gts_build_avail_time_table(struct iio_gts *gts)
>> if (!times)
>> return -ENOMEM;
>>
>> +
>
> Grumble.

Oh. I wonder how things like this tend to slip-in. Maybe I should change
my password, it must be someone else has cracked my it and is typing
these in at night while I am sleeping ^_^;

> If nothing else comes up I'll tidy that stray line up when applying.

Thanks!

> Note that these will need to wait for after rc1 now so my fixes branch
> has moved on to include the code being fixed.

Well, that's Ok. Please, let me know if you want me to rebase to rc1 and
respin the series.

--Matti

--
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~

2023-05-01 15:37:35

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH 2/2] iio: gts-helpers: fix integration time units

On Mon, 24 Apr 2023 05:09:36 +0000
"Vaittinen, Matti" <[email protected]> wrote:

> On 4/23/23 14:18, Jonathan Cameron wrote:
> > On Mon, 17 Apr 2023 12:20:18 +0300
> > Matti Vaittinen <[email protected]> wrote:
> >
> >> 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.
> >>
> >> Signed-off-by: Matti Vaittinen <[email protected]>
> >> ---
> >> drivers/iio/industrialio-gts-helper.c | 43 ++++++++++++++++++++-------
> >> 1 file changed, 33 insertions(+), 10 deletions(-)
> >>
> >> diff --git a/drivers/iio/industrialio-gts-helper.c b/drivers/iio/industrialio-gts-helper.c
> >> index 8bb68975b259..2ab8d2dce019 100644
> >> --- a/drivers/iio/industrialio-gts-helper.c
> >> +++ b/drivers/iio/industrialio-gts-helper.c
> >> @@ -337,6 +337,17 @@ static int iio_gts_build_avail_scale_table(struct iio_gts *gts)
> >> return ret;
> >> }
> >>
> >> +static void iio_gts_us_to_int_micro(int *time_us, int *int_micro_times,
> >> + int num_times)
> >> +{
> >> + int i;
> >> +
> >> + for (i = 0; i < num_times; i++) {
> >> + int_micro_times[i * 2] = time_us[i] / 1000000;
> >> + int_micro_times[i * 2 + 1] = time_us[i] % 1000000;
> >> + }
> >> +}
> >> +
> >> /**
> >> * iio_gts_build_avail_time_table - build table of available integration times
> >> * @gts: Gain time scale descriptor
> >> @@ -351,7 +362,7 @@ static int iio_gts_build_avail_scale_table(struct iio_gts *gts)
> >> */
> >> static int iio_gts_build_avail_time_table(struct iio_gts *gts)
> >> {
> >> - int *times, i, j, idx = 0;
> >> + int *times, i, j, idx = 0, *int_micro_times;
> >>
> >> if (!gts->num_itime)
> >> return 0;
> >> @@ -360,6 +371,7 @@ static int iio_gts_build_avail_time_table(struct iio_gts *gts)
> >> if (!times)
> >> return -ENOMEM;
> >>
> >> +
> >
> > Grumble.
>
> Oh. I wonder how things like this tend to slip-in. Maybe I should change
> my password, it must be someone else has cracked my it and is typing
> these in at night while I am sleeping ^_^;
>
> > If nothing else comes up I'll tidy that stray line up when applying.
>
> Thanks!
>
> > Note that these will need to wait for after rc1 now so my fixes branch
> > has moved on to include the code being fixed.
>
> Well, that's Ok. Please, let me know if you want me to rebase to rc1 and
> respin the series.
No need as not much changed around these (not I'm not at rc1 yet, but
char-misc-linus now includes these so I rebased on that)

Applied to the fixes-togreg branch of iio.git with fixes tags added now
the commits are upstream.

Thanks,

Jonathan

>
> --Matti
>