public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Javier Carrasco" <javier.carrasco.cruz@gmail.com>
To: "Matti Vaittinen" <mazziesaccount@gmail.com>,
	"Jonathan Cameron" <jic23@kernel.org>,
	"Lars-Peter Clausen" <lars@metafoo.de>,
	"Rishi Gupta" <gupt21@gmail.com>
Cc: <linux-iio@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	"Jonathan Cameron" <Jonathan.Cameron@huawei.com>
Subject: Re: [PATCH 2/2] iio: light: veml6030: fix scale to conform to ABI
Date: Mon, 13 Jan 2025 16:05:36 +0100	[thread overview]
Message-ID: <D711DZJWGQX5.2D4PLVQN7NB8Z@gmail.com> (raw)
In-Reply-To: <129de6d6-50f4-44d7-9a06-db61a6669810@gmail.com>

On Mon Jan 13, 2025 at 12:56 PM CET, Matti Vaittinen wrote:
> On 07/01/2025 22:50, Javier Carrasco wrote:
> > The current scale is not ABI-compliant as it is just the sensor gain
> > instead of the value that acts as a multiplier to be applied to the raw
> > value (there is no offset).
> >
> > Use the iio-gts helpers to obtain the proper scale values according to
> > the gain and integration time to match the resolution tables from the
> > datasheet and drop dedicated variables to store the current values of
> > the integration time, gain and resolution. When at it, use 'scale'
> > instead of 'gain' consistently for the get/set functions to avoid
> > misunderstandings.
> >
> > Fixes: 7b779f573c48 ("iio: light: add driver for veml6030 ambient light sensor")
> > Signed-off-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
>
> Thanks for the patch Javier.
>
> And, sorry for a review which is more about questions than suggested
> improvements. I find it hard to focus on reading code today.
>
> > ---
> >   drivers/iio/light/Kconfig    |   1 +
> >   drivers/iio/light/veml6030.c | 499 ++++++++++++++++---------------------------
> >   2 files changed, 189 insertions(+), 311 deletions(-)
> >
>
> I like the diffstats of this Fix! :) It's nice you found gts-helpers
> helpful :)

I wonder how painful the int. time/gain/scale issue in ALS was before
iio-gts existed :D

>
> ...
>
> > +
> > +static int veml6030_process_als(struct veml6030_data *data, int raw,
> > +				int *val, int *val2)
> > +{
> > +	int ret, scale_int, scale_nano;
> > +	u64 tmp;
> > +
> > +	ret = veml6030_get_scale(data, &scale_int, &scale_nano);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	tmp = (u64)raw * scale_nano;
> > +	*val = raw * scale_int + div_u64(tmp, NANO);
> > +	*val2 = div_u64(do_div(tmp, NANO), MILLI);
>
> do_div() is horrible on some platforms. Or, at least it used to be. Is
> there really no way to go without it? We're dealing with 16bit data and
> maximum of 512x total gain only, so maybe there was a way(?)
>
> Maybe a stupid question (in which case I blame mucus in my head) - could
> you just divide the raw value by the total gain?
>

In its current form we need the 64-bit operations to handle the scale,
and it will probably have to stay like that for the reasons I give you
below.

> ...
>
> >
> >   static irqreturn_t veml6030_event_handler(int irq, void *private)
> > @@ -1084,6 +968,13 @@ static int veml6030_hw_init(struct iio_dev *indio_dev, struct device *dev)
> >   	int ret, val;
> >   	struct veml6030_data *data = iio_priv(indio_dev);
> >
> > +	ret = devm_iio_init_iio_gts(dev, 2, 150400000,
>
> Can you please explain the seemingly odd choice for the max scale?
>
> Just a quick look at the sensor spec and this driver allows me to assume
> following:
>
> Maximum 'total gain' from both HWGAIN and integration time is 16 * 32 = 512.
>
> If we chose the 'total gain' 1x to match scale 1, then the smallest
> scale would be 1/512 = 0.001 953 125
>
> This is 1953125 NANOs. This would mean the max-scale 1X => gain 1X would
> not cause precision loss. (Well, I'm not at my sharpest as I've caught
> cold - but I _think_ this is true, right?)
>
> If I read this correctly, the only channel where the scale gets applied
> is the INTENSITY channel, right? Hence it should be possible to chose
> the scale as we see best. (Unless the idea of this seemingly odd scale
> is to maintain the old intensity / scale values in order to not shake
> userland any more - in which case this could be mentioned).
>

The scale is applied to an IIO_LIGHT channel, not to the INTENSITY,
which forces us to provide the scale as a value that multiplied by the
raw measurement gives a result in lux. The maximum scale in that case,
as provided by the application note [1] (page 5, RESOLUTION AND MAXIMUM
DETECTION RANGE table) is 2.1504 to convert from cnt to lux.

The same applies for the rest of the device supported by this driver
(veml6035 and veml6035).

>
> Yours,
> 	-- Matti

Thank you for your feedback, I hope my reply could answer your
questions. If something is still not clear or simply wrong, please let
me know.

Best regards,
Javier Carrasco

Link to app note: https://www.vishay.com/docs/84367/designingveml6030.pdf [1]

  reply	other threads:[~2025-01-13 15:05 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-07 20:50 [PATCH 0/2] iio: light: fix scale in veml6030 Javier Carrasco
2025-01-07 20:50 ` [PATCH 1/2] iio: light: veml6030: extend regmap to support regfields and caching Javier Carrasco
2025-01-12 13:18   ` Jonathan Cameron
2025-01-12 14:10     ` Javier Carrasco
2025-01-12 16:40       ` Jonathan Cameron
2025-01-07 20:50 ` [PATCH 2/2] iio: light: veml6030: fix scale to conform to ABI Javier Carrasco
2025-01-09 17:46   ` Javier Carrasco
2025-01-12 13:22   ` Jonathan Cameron
2025-01-13 11:56   ` Matti Vaittinen
2025-01-13 15:05     ` Javier Carrasco [this message]
2025-01-13 19:52       ` Matti Vaittinen
2025-01-13 22:32         ` Javier Carrasco
2025-01-14  6:43           ` Matti Vaittinen
2025-01-14 13:02             ` Javier Carrasco
2025-01-14 14:26               ` Matti Vaittinen
2025-01-18 12:16                 ` Jonathan Cameron
2025-01-13 10:25 ` [PATCH 0/2] iio: light: fix scale in veml6030 Matti Vaittinen
2025-01-13 11:02   ` Javier Carrasco

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=D711DZJWGQX5.2D4PLVQN7NB8Z@gmail.com \
    --to=javier.carrasco.cruz@gmail.com \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=gupt21@gmail.com \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mazziesaccount@gmail.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