Re: [PATCH v3 2/2] iio: humditiy: hdc3020: fix units for thresholds and hysteresis

From: Javier Carrasco

Date: Wed Oct 15 2025 - 04:29:58 EST


On Mon Oct 13, 2025 at 4:12 PM CST, Dimitri Fedrau via B4 Relay wrote:
> From: Dimitri Fedrau <dimitri.fedrau@xxxxxxxxxxxx>
>
> 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@xxxxxxxxxxxx>
> Signed-off-by: Dimitri Fedrau <dimitri.fedrau@xxxxxxxxxxxx>
> ---
> drivers/iio/humidity/hdc3020.c | 67 ++++++++++++++++++++++++------------------
> 1 file changed, 39 insertions(+), 28 deletions(-)
>
> diff --git a/drivers/iio/humidity/hdc3020.c b/drivers/iio/humidity/hdc3020.c
> index 8aa567d9aded9cab461f1f905b6b5ada721ba2f0..5a978801fb09ca49c449028d046a91e0e67c38ea 100644
> --- a/drivers/iio/humidity/hdc3020.c
> +++ b/drivers/iio/humidity/hdc3020.c
> @@ -72,6 +72,9 @@
> #define HDC3020_MAX_TEMP_HYST_MICRO 164748607
> #define HDC3020_MAX_HUM_MICRO 99220264
>
> +/* Divide 65535 from the datasheet by 5 to avoid overflows */
> +#define HDC3020_THRESH_FRACTION (65535 / 5)
> +
> struct hdc3020_data {
> struct i2c_client *client;
> struct gpio_desc *reset_gpio;
> @@ -376,15 +379,17 @@ 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 explicit formula in the datasheet and
> + * additionally scale by HDC3020_THRESH_FRACTION to avoid precision loss
> + * when calculating threshold and hysteresis values. Result is degree
> + * celsius scaled by HDC3020_THRESH_FRACTION.

I see that Andy asked you to include the formula, and you accepted that
suggestion, but the formula is still missing. I also think it should be
added to the comment because that would clarify where this kind of
magic numbers in the return value come from.

> */
> temp = FIELD_GET(HDC3020_THRESH_TEMP_MASK, thresh) <<
> HDC3020_THRESH_TEMP_TRUNC_SHIFT;
>
> - return -2949075 + (175 * temp);
> + return -589815 + (35 * temp);
> }

With that modification,

Reviewed-by: Javier Carrasco <javier.carrasco.cruz@xxxxxxxxx>