Re: [PATCH v3 2/2] iio: humditiy: hdc3020: fix units for thresholds and hysteresis
From: Andy Shevchenko
Date: Wed Oct 15 2025 - 05:46:09 EST
On Mon, Oct 13, 2025 at 11:12 AM Dimitri Fedrau via B4 Relay
<devnull+dimitri.fedrau.liebherr.com@xxxxxxxxxx> wrote:
>
> 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.
...
> - return -2949075 + (175 * temp);
> + return -589815 + (35 * temp);
I was under the impression that we agreed on the explicit division by 5.
return -2949075 / 5 + (175 / 5 * temp);
...
> - return hum * 100;
> + return hum * 20;
And in a similar way here.
> s_clr = max(s_clr, HDC3020_MIN_TEMP_MICRO);
> s_clr = min(s_clr, HDC3020_MAX_TEMP_MICRO);
> @@ -565,7 +574,8 @@ static int hdc3020_write_thresh(struct iio_dev *indio_dev,
> /* Calculate old hysteresis */
> s_thresh = (s64)hdc3020_thresh_get_hum(thresh) * 1000000;
> s_clr = (s64)hdc3020_thresh_get_hum(clr) * 1000000;
> - s_hyst = div_s64(abs(s_thresh - s_clr), 65535);
> + s_hyst = div_s64(abs(s_thresh - s_clr),
> + HDC3020_THRESH_FRACTION);
> /* Set new threshold */
> thresh = reg_val;
> /* Try to set old hysteresis */
> @@ -574,15 +584,16 @@ static int hdc3020_write_thresh(struct iio_dev *indio_dev,
> case IIO_EV_INFO_HYSTERESIS:
> /*
> * Function hdc3020_thresh_get_hum returns relative
> - * humidity in percent scaled by 65535. Scale by 1000000
> - * to be able to subtract scaled hysteresis value.
> + * humidity in percent scaled by HDC3020_THRESH_FRACTION.
> + * Scale by 1000000 to be able to subtract scaled
> + * hysteresis value.
> */
> s_thresh = (s64)hdc3020_thresh_get_hum(thresh) * 1000000;
> /*
> - * Units of s_val are in micro percent, scale by 65535
> - * to get same units as s_thresh.
> + * Units of s_val are in micro percent, scale by
> + * HDC3020_THRESH_FRACTION to get same units as s_thresh.
> */
> - s_hyst = (s64)s_val * 65535;
> + s_hyst = (s64)s_val * HDC3020_THRESH_FRACTION;
> s_clr = hdc3020_thresh_clr(s_thresh, s_hyst, dir);
> s_clr = max(s_clr, 0);
> s_clr = min(s_clr, HDC3020_MAX_HUM_MICRO);
> @@ -630,7 +641,7 @@ static int hdc3020_read_thresh(struct iio_dev *indio_dev,
> thresh = hdc3020_thresh_get_temp(ret);
> switch (info) {
> case IIO_EV_INFO_VALUE:
> - *val = thresh;
> + *val = thresh * MILLI;
> break;
> case IIO_EV_INFO_HYSTERESIS:
> ret = hdc3020_read_be16(data, reg_clr);
> @@ -638,18 +649,18 @@ static int hdc3020_read_thresh(struct iio_dev *indio_dev,
> return ret;
>
> clr = hdc3020_thresh_get_temp(ret);
> - *val = abs(thresh - clr);
> + *val = abs(thresh - clr) * MILLI;
> break;
> default:
> return -EOPNOTSUPP;
> }
> - *val2 = 65535;
> + *val2 = HDC3020_THRESH_FRACTION;
> return IIO_VAL_FRACTIONAL;
> case IIO_HUMIDITYRELATIVE:
> thresh = hdc3020_thresh_get_hum(ret);
> switch (info) {
> case IIO_EV_INFO_VALUE:
> - *val = thresh;
> + *val = thresh * MILLI;
> break;
> case IIO_EV_INFO_HYSTERESIS:
> ret = hdc3020_read_be16(data, reg_clr);
> @@ -657,12 +668,12 @@ static int hdc3020_read_thresh(struct iio_dev *indio_dev,
> return ret;
>
> clr = hdc3020_thresh_get_hum(ret);
> - *val = abs(thresh - clr);
> + *val = abs(thresh - clr) * MILLI;
> break;
> default:
> return -EOPNOTSUPP;
> }
> - *val2 = 65535;
> + *val2 = HDC3020_THRESH_FRACTION;
> return IIO_VAL_FRACTIONAL;
> default:
> return -EOPNOTSUPP;
>
> --
> 2.39.5
>
>
--
With Best Regards,
Andy Shevchenko