mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peter Rosin <peda@axentia.se>
To: Liam Beguin <liambeguin@gmail.com>,
	jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de
Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org,
	devicetree@vger.kernel.org, robh+dt@kernel.org
Subject: Re: [PATCH v14 02/11] iio: afe: rescale: add INT_PLUS_{MICRO,NANO} support
Date: Thu, 10 Feb 2022 17:42:25 +0100	[thread overview]
Message-ID: <fbb84b08-5d3d-3684-fdee-5ce367280857@axentia.se> (raw)
In-Reply-To: <20220208020441.3081162-3-liambeguin@gmail.com>

Hi!

As usual, sorry for my low bandwidth.

On 2022-02-08 03:04, Liam Beguin wrote:
> Some ADCs use IIO_VAL_INT_PLUS_{NANO,MICRO} scale types.
> Add support for these to allow using the iio-rescaler with them.
> 
> Signed-off-by: Liam Beguin <liambeguin@gmail.com>
> Reviewed-by: Peter Rosin <peda@axentia.se>
> ---
>  drivers/iio/afe/iio-rescale.c | 35 +++++++++++++++++++++++++++++++++++
>  1 file changed, 35 insertions(+)
> 
> diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c
> index 65832dd09249..f833eb38f8bb 100644
> --- a/drivers/iio/afe/iio-rescale.c
> +++ b/drivers/iio/afe/iio-rescale.c
> @@ -14,6 +14,7 @@
>  #include <linux/of_device.h>
>  #include <linux/platform_device.h>
>  #include <linux/property.h>
> +#include <linux/units.h>
>  
>  #include <linux/iio/afe/rescale.h>
>  #include <linux/iio/consumer.h>
> @@ -23,6 +24,9 @@ int rescale_process_scale(struct rescale *rescale, int scale_type,
>  			  int *val, int *val2)
>  {
>  	s64 tmp;
> +	s32 rem;
> +	u32 mult;
> +	u32 neg;
>  
>  	switch (scale_type) {
>  	case IIO_VAL_FRACTIONAL:
> @@ -41,6 +45,37 @@ int rescale_process_scale(struct rescale *rescale, int scale_type,
>  		tmp *= rescale->numerator;
>  		tmp = div_s64(tmp, 1000000000LL);
>  		*val = tmp;
> +		return scale_type;
> +	case IIO_VAL_INT_PLUS_NANO:
> +	case IIO_VAL_INT_PLUS_MICRO:
> +		mult = scale_type == IIO_VAL_INT_PLUS_NANO ? GIGA : MEGA;

By now, we all agree that the big numbers in this context have nothing
to do with unit prefixes of physical quantities, so the macros are not
really appropriate. However, in this case we have IIO_VAL_INT_PLUS_NANO
and IIO_VAL_INT_PLUS_MICRO. Not using "NANO : MICRO" here, and instead
go with "GIGA : MEGA" is just plain silly, if you ask me.

So, either "NANO : MICRO" or "1000000000 : 1000000".

I'm not sold on the macros. I frankly don't see all that much value
in them and am perfectly fine with raw numbers. To me, it just looks
like someone has read somewhere that constants should not appear in
the code, and from that concluded that #define TEN 10 is a good thing
without thinking very much about it. There is also the possibility
that someone who has never seen these defines thinks MEGA is 2^20
instead of 10^6, because that is a much more likely candidate for a
define in the frist place (not everybody knows all the digits of
1048576 by heart and 1 << 20 many times require extra brackets that
might make it look more complicated than it needs to be).

Back to this case; the connection to the naming of IIO_VAL_INT_PLUS_NANO
(and ..._MICRO) makes it ok to use the defines. So if you feel strongly
about not using "1000000000 : 1000000" I'm ok with that.

Cheers,
Peter

> +
> +		/*
> +		 * For IIO_VAL_INT_PLUS_{MICRO,NANO} scale types if either *val
> +		 * OR *val2 is negative the schan scale is negative, i.e.
> +		 * *val = 1 and *val2 = -0.5 yields -1.5 not -0.5.
> +		 */
> +		neg = *val < 0 || *val2 < 0;
> +
> +		tmp = (s64)abs(*val) * abs(rescale->numerator);
> +		*val = div_s64_rem(tmp, abs(rescale->denominator), &rem);
> +
> +		tmp = (s64)rem * mult + (s64)abs(*val2) * abs(rescale->numerator);
> +		tmp = div_s64(tmp, abs(rescale->denominator));
> +
> +		*val += div_s64_rem(tmp, mult, val2);
> +
> +		/*
> +		 * If only one of the rescaler elements or the schan scale is
> +		 * negative, the combined scale is negative.
> +		 */
> +		if (neg ^ ((rescale->numerator < 0) ^ (rescale->denominator < 0))) {
> +			if (*val)
> +				*val = -*val;
> +			else
> +				*val2 = -*val2;
> +		}
> +
>  		return scale_type;
>  	default:
>  		return -EOPNOTSUPP;

  reply	other threads:[~2022-02-10 16:42 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-08  2:04 [PATCH v14 00/11] iio: afe: add temperature rescaling support Liam Beguin
2022-02-08  2:04 ` [PATCH v14 01/11] iio: afe: rescale: expose scale processing function Liam Beguin
2022-02-08  2:04 ` [PATCH v14 02/11] iio: afe: rescale: add INT_PLUS_{MICRO,NANO} support Liam Beguin
2022-02-10 16:42   ` Peter Rosin [this message]
2022-02-10 17:36     ` Liam Beguin
2022-02-11 14:20       ` Peter Rosin
2022-02-08  2:04 ` [PATCH v14 03/11] iio: afe: rescale: add offset support Liam Beguin
2022-02-08 13:42   ` Andy Shevchenko
2022-02-09 16:35     ` Liam Beguin
2022-02-10 16:47   ` Peter Rosin
2022-02-08  2:04 ` [PATCH v14 04/11] iio: afe: rescale: fix accuracy for small fractional scales Liam Beguin
2022-02-08  2:04 ` [PATCH v14 05/11] iio: afe: rescale: reduce risk of integer overflow Liam Beguin
2022-02-08  2:04 ` [PATCH v14 06/11] iio: afe: rescale: make use of units.h Liam Beguin
2022-02-10 17:41   ` Peter Rosin
2022-02-08  2:04 ` [PATCH v14 07/11] iio: test: add basic tests for the iio-rescale driver Liam Beguin
2022-02-08  2:04 ` [PATCH v14 08/11] iio: afe: rescale: add RTD temperature sensor support Liam Beguin
2022-02-08  2:04 ` [PATCH v14 09/11] iio: afe: rescale: add temperature transducers Liam Beguin
2022-02-08 13:46   ` Andy Shevchenko
2022-02-09 16:38     ` Liam Beguin
2022-02-08  2:04 ` [PATCH v14 10/11] dt-bindings: iio: afe: add bindings for temperature-sense-rtd Liam Beguin
2022-02-08  2:04 ` [PATCH v14 11/11] dt-bindings: iio: afe: add bindings for temperature transducers Liam Beguin

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=fbb84b08-5d3d-3684-fdee-5ce367280857@axentia.se \
    --to=peda@axentia.se \
    --cc=andy.shevchenko@gmail.com \
    --cc=devicetree@vger.kernel.org \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=liambeguin@gmail.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robh+dt@kernel.org \
    /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

all inboxes | Powered by JetHome®