From: Matti Vaittinen <mazziesaccount@gmail.com>
To: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
Cc: Jonathan Cameron <jic23@kernel.org>,
Lars-Peter Clausen <lars@metafoo.de>,
linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org
Subject: Re: [PATCH v6 2/7] iio: light: Add gain-time-scale helpers
Date: Fri, 31 Mar 2023 10:39:32 +0300 [thread overview]
Message-ID: <cbbb45d3-b2e0-d57c-870f-553b7a6cbf99@gmail.com> (raw)
In-Reply-To: <b8904407-87d1-c8fa-d70c-67259211445e@gmail.com>
On 3/30/23 19:48, Matti Vaittinen wrote:
> On 3/27/23 14:28, Matti Vaittinen wrote:
>> Some light sensors can adjust both the HW-gain and integration time.
>> There are cases where adjusting the integration time has similar impact
>> to the scale of the reported values as gain setting has.
>>
>> IIO users do typically expect to handle scale by a single writable
>> 'scale'
>> entry. Driver should then adjust the gain/time accordingly.
>>
>> It however is difficult for a driver to know whether it should change
>> gain or integration time to meet the requested scale. Usually it is
>> preferred to have longer integration time which usually improves
>> accuracy, but there may be use-cases where long measurement times can be
>> an issue. Thus it can be preferable to allow also changing the
>> integration time - but mitigate the scale impact by also changing the
>> gain
>> underneath. Eg, if integration time change doubles the measured values,
>> the driver can reduce the HW-gain to half.
>>
>> The theory of the computations of gain-time-scale is simple. However,
>> some people (undersigned) got that implemented wrong for more than once.
>>
>> Add some gain-time-scale helpers in order to not dublicate errors in all
>> drivers needing these computations.
>>
>> Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
>>
>> ---
>> Currently it is only BU27034 using these in this series. I am however
>> working
>> with drivers for RGB sensors BU27008 and BU27010 which have similar
>> [gain - integration time - scale] - relation. I hope sending those
>> follows soon after the BU27034 is done.
>>
>
>> +/**
>> + * iio_gts_find_new_gain_sel_by_old_gain_time - compensate for time
>> change
>> + * @gts: Gain time scale descriptor
>> + * @old_gain: Previously set gain
>> + * @old_time_sel: Selector corresponding previously set time
>> + * @new_time_sel: Selector corresponding new time to be set
>> + * @new_gain: Pointer to value where new gain is to be written
>> + *
>> + * We may want to mitigate the scale change caused by setting a new
>> integration
>> + * time (for a light sensor) by also updating the (HW)gain. This
>> helper computes
>> + * new gain value to maintain the scale with new integration time.
>> + *
>> + * Return: 0 on success. -EINVAL if gain matching the new time is not
>> found.
>
> Here we need to document another return value denote whether the
> @new_gain was updated.
>
>> + */
>> +int iio_gts_find_new_gain_sel_by_old_gain_time(struct iio_gts *gts,
>> + int old_gain, int old_time_sel,
>> + int new_time_sel, int *new_gain)
>> +{
>> + const struct iio_itime_sel_mul *itime_old, *itime_new;
>> + u64 scale;
>> + int ret;
>> +
>> + itime_old = iio_gts_find_itime_by_sel(gts, old_time_sel);
>> + if (!itime_old)
>> + return -EINVAL;
>> +
>> + itime_new = iio_gts_find_itime_by_sel(gts, new_time_sel);
>> + if (!itime_new)
>> + return -EINVAL;
>> +
>> + ret = iio_gts_get_scale_linear(gts, old_gain, itime_old->time_us,
>> + &scale);
>> + if (ret)
>> + return ret;
>> +
>> + ret = gain_get_scale_fraction(gts->max_scale, scale, itime_new->mul,
>> + new_gain);
>> + if (ret)
>> + return ret;
>> +
>> + if (!iio_gts_valid_gain(gts, *new_gain))
>> + return -EINVAL;
>
> I would change this to -ERANGE to differentiate the case where the new
> gain was computed but was not valid. The bu27034 (and
> not-yet-fully-finished bu27008) driver uses the computed gain to find
> closest matching gain the hardware supports. I am not super happy with
> the -ERANGE, as it is also possible the gain is in the "range" of
> supported gains but not _exactly_ supported one. In a sense -EINVAL
> would be more correct. The invalid time could in a sense be interpreted
> as an "time selector not found" - so maybe the -ENOENT could be somehow
> tolerated. Still, in my opinion the invalid integration time is very
> much more an -EINVAL than anything else...
Looks like I keep discussing with myself. This however was not a good
solution as we might detect non integer gain to be required in the
gain_get_scale_fraction(). And deciding if that function should return
-ERANGE or -EINVAL got things even worse.
So, the take N (where N is a positive integer, much greater than 1) is
that I'll do:
int iio_gts_find_new_gain_sel_by_old_gain_time(struct iio_gts *gts,
int old_gain, int old_time_sel,
int new_time_sel, int *new_gain)
{
const struct iio_itime_sel_mul *itime_old, *itime_new;
u64 scale;
int ret;
*new_gain = -1;
and add return value doc like:
* Return: 0 if an exactly matching supported new gain was found. When
a
* non-zero value is returned, the @new_gain will be set to a negative or
* positive value. The negative value means that no gain could be computed.
* Positive value will be the "best possible new gain there could be".
There
* can be two reasons why finding the "best possible" new gain is not
deemed
* successful. 1) This new value cannot be supported by the hardware.
2) The new
* gain required to maintain the scale would not be an integer. In this
case,
* the "best possible" new gain will be a floored optimal gain, which
may or
* may not be supported by the hardware.
>
> I will fix this for v7.
>
--
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland
~~ When things go utterly wrong vim users can always type :help! ~~
next prev parent reply other threads:[~2023-03-31 7:39 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-27 11:27 [PATCH v6 0/7] Support ROHM BU27034 ALS sensor Matti Vaittinen
2023-03-27 11:27 ` [PATCH v6 1/7] dt-bindings: iio: light: Support ROHM BU27034 Matti Vaittinen
2023-03-27 11:28 ` [PATCH v6 2/7] iio: light: Add gain-time-scale helpers Matti Vaittinen
2023-03-30 16:48 ` Matti Vaittinen
2023-03-31 7:39 ` Matti Vaittinen [this message]
2023-03-27 11:34 ` [PATCH v6 3/7] kunit: Add kunit wrappers for (root) device creation Matti Vaittinen
2023-03-27 12:01 ` Greg Kroah-Hartman
2023-03-27 12:20 ` Matti Vaittinen
2023-03-27 12:38 ` Greg Kroah-Hartman
2023-03-28 12:45 ` David Gow
2023-03-28 13:22 ` Matti Vaittinen
2023-03-28 13:38 ` David Gow
2023-03-30 16:53 ` Maxime Ripard
2023-03-27 11:34 ` [PATCH v6 4/7] iio: test: test gain-time-scale helpers Matti Vaittinen
2023-03-27 11:34 ` [PATCH v6 5/7] MAINTAINERS: Add IIO " Matti Vaittinen
2023-03-27 11:34 ` [PATCH v6 6/7] iio: light: ROHM BU27034 Ambient Light Sensor Matti Vaittinen
2023-03-28 5:59 ` kernel test robot
2023-03-30 16:59 ` Matti Vaittinen
2023-03-27 11:35 ` [PATCH v6 7/7] MAINTAINERS: Add ROHM BU27034 Matti Vaittinen
2023-03-27 11:39 ` [PATCH v6 0/7] Support ROHM BU27034 ALS sensor Matti Vaittinen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cbbb45d3-b2e0-d57c-870f-553b7a6cbf99@gmail.com \
--to=mazziesaccount@gmail.com \
--cc=jic23@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=matti.vaittinen@fi.rohmeurope.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox