All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dimitri Fedrau <dima.fedrau@gmail.com>
To: Javier Carrasco <javier.carrasco.cruz@gmail.com>
Cc: dimitri.fedrau@liebherr.com, "Li peiyu" <579lpy@gmail.com>,
	"Jonathan Cameron" <jic23@kernel.org>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"Jonathan Cameron" <Jonathan.Cameron@huawei.com>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Chris Lesiak" <chris.lesiak@licorbio.com>
Subject: Re: [PATCH v2 2/2] iio: humditiy: hdc3020: fix units for thresholds and hysteresis
Date: Tue, 2 Sep 2025 14:51:23 +0200	[thread overview]
Message-ID: <20250902125123.GA49718@legfed1> (raw)
In-Reply-To: <DCIAU0EO8E0K.3JIXZ2XICP3YC@gmail.com>

Am Tue, Sep 02, 2025 at 08:04:53PM +0800 schrieb Javier Carrasco:
> Hi Dimitri, thank you for your patch. A few comments inline.
> 
> On Tue Sep 2, 2025 at 1:51 AM CST, Dimitri Fedrau via B4 Relay wrote:
> > From: Dimitri Fedrau <dimitri.fedrau@liebherr.com>
> >
> > According to the ABI the units after application of scale and offset are
> > milli degree celsius for temperature thresholds and milli percent for
> > relative humidity thresholds. Currently the resulting units are degree
> > celsius for temperature thresholds and hysteresis and percent for relative
> > humidity thresholds and hysteresis. Change scale factor to fix this issue.
> >
> > Fixes: 3ad0e7e5f0cb ("iio: humidity: hdc3020: add threshold events support")
> > Reported-by: Chris Lesiak <chris.lesiak@licorbio.com>
> > Signed-off-by: Dimitri Fedrau <dimitri.fedrau@liebherr.com>
> > ---
> >  drivers/iio/humidity/hdc3020.c | 69 +++++++++++++++++++++++++-----------------
> >  1 file changed, 41 insertions(+), 28 deletions(-)
> >
> > diff --git a/drivers/iio/humidity/hdc3020.c b/drivers/iio/humidity/hdc3020.c
> > index 8aa567d9aded9cab461f1f905b6b5ada721ba2f0..deb19e0291f7f820c5f8760fbf0682379ab5f34b 100644
> > --- a/drivers/iio/humidity/hdc3020.c
> > +++ b/drivers/iio/humidity/hdc3020.c
> > @@ -72,6 +72,13 @@
> >  #define HDC3020_MAX_TEMP_HYST_MICRO	164748607
> >  #define HDC3020_MAX_HUM_MICRO		99220264
> >  
> > +/*
> > + * In order to avoid overflows when returning thresholds and hysteresis values,
> > + * the fraction of these is set to 13107, also the datasheet refers to 65535
> > + * this is not enough to prevent overflows. Dividing this value by 5 fixes this.
> > + */
> > +#define HDC3020_THRESH_FRACTION		(65535 / 5)
> > +
> 
> I think the comment is a bit too verbose. If you explain why the value
> has to be divided by 5, you probably don't need to tell the result of
> the division. Something as simple as "divide 65535 from the datasheet by
> 5 to avoid overflows" could do.
>

Ok, will change the comment as you suggested.

> >  struct hdc3020_data {
> >  	struct i2c_client *client;
> >  	struct gpio_desc *reset_gpio;
> > @@ -376,15 +383,16 @@ static int hdc3020_thresh_get_temp(u16 thresh)
> >  	int temp;
> >  
> >  	/*
> > -	 * Get the temperature threshold from 9 LSBs, shift them to get
> > -	 * the truncated temperature threshold representation and
> > -	 * calculate the threshold according to the formula in the
> > -	 * datasheet. Result is degree celsius scaled by 65535.
> > +	 * Get the temperature threshold from 9 LSBs, shift them to get the
> > +	 * truncated temperature threshold representation and calculate the
> > +	 * threshold according to the formula in the datasheet and additionally
> > +	 * scale by HDC3020_THRESH_FRACTION to avoid precision loss when
> > +	 * calculating threshold and hysteresis values.
> 
> If I got it right, you are actually dividing by 5 and not by
> HDC3020_THRESH_FRACTION.

I do scale it by HDC3020_THRESH_FRACTION.

Formula from the datasheet is:
T( deggree celsius) = -45 + (175 * temp) / 65535

Before patching it was(scaled by 65535):

T(degree celsius) * 65535 = -2949075 + (175 * temp)

After patching(scaled by 13107):

T(degree celsius) * 13107 = -589815 + (35 * temp)

I need to scale the return value to avoid precision loss when
calculating threshold and hysteresis values.

> 
> >  	 */
> >  	temp = FIELD_GET(HDC3020_THRESH_TEMP_MASK, thresh) <<
> >  	       HDC3020_THRESH_TEMP_TRUNC_SHIFT;
> >  
> > -	return -2949075 + (175 * temp);
> > +	return -589815 + (35 * temp);
> >  }
> >  
> >  static int hdc3020_thresh_get_hum(u16 thresh)
> > @@ -394,13 +402,14 @@ static int hdc3020_thresh_get_hum(u16 thresh)
> >  	/*
> >  	 * Get the humidity threshold from 7 MSBs, shift them to get the
> >  	 * truncated humidity threshold representation and calculate the
> > -	 * threshold according to the formula in the datasheet. Result is
> > -	 * percent scaled by 65535.
> > +	 * threshold according to the formula in the datasheet and additionally
> > +	 * scale by HDC3020_THRESH_FRACTION to avoid precision loss when
> > +	 * calculating threshold and hysteresis values.
> 
> Same here?
> 

See above.

> >  	 */
> >  	hum = FIELD_GET(HDC3020_THRESH_HUM_MASK, thresh) <<
> >  	      HDC3020_THRESH_HUM_TRUNC_SHIFT;
> >  
> > -	return hum * 100;
> > +	return hum * 20;
> >  }
>

Best regards,
Dimitri Fedrau

  reply	other threads:[~2025-09-02 12:51 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-01 17:51 [PATCH v2 0/2] iio: humditiy: hdc3020: fix units Dimitri Fedrau
2025-09-01 17:51 ` Dimitri Fedrau via B4 Relay
2025-09-01 17:51 ` [PATCH v2 1/2] iio: humditiy: hdc3020: fix units for temperature and humidity measurement Dimitri Fedrau
2025-09-01 17:51   ` Dimitri Fedrau via B4 Relay
2025-09-02 12:05   ` Javier Carrasco
2025-09-01 17:51 ` [PATCH v2 2/2] iio: humditiy: hdc3020: fix units for thresholds and hysteresis Dimitri Fedrau
2025-09-01 17:51   ` Dimitri Fedrau via B4 Relay
2025-09-02 12:04   ` Javier Carrasco
2025-09-02 12:51     ` Dimitri Fedrau [this message]
2025-09-02 12:47   ` Andy Shevchenko
2025-09-02 13:54     ` Dimitri Fedrau
2025-09-02 14:22       ` Andy Shevchenko

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=20250902125123.GA49718@legfed1 \
    --to=dima.fedrau@gmail.com \
    --cc=579lpy@gmail.com \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=andy@kernel.org \
    --cc=chris.lesiak@licorbio.com \
    --cc=dimitri.fedrau@liebherr.com \
    --cc=dlechner@baylibre.com \
    --cc=javier.carrasco.cruz@gmail.com \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.