The value given by the Si7013/20/21 modules needs a conversion using a small expression in order to obtain the measurement in °C or %RH. This patch adds the computation inside the si7020 module, allowing the user to obtain the measurement in m°C and 10E-3%RH without any additional calculation. Signed-off-by: Nicola Corna <nicola@xxxxxxxxxx> --- drivers/iio/humidity/si7020.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/drivers/iio/humidity/si7020.c b/drivers/iio/humidity/si7020.c index a8bad04..c534706 100644 --- a/drivers/iio/humidity/si7020.c +++ b/drivers/iio/humidity/si7020.c @@ -70,6 +70,7 @@ static int si7020_read_raw(struct iio_dev *indio_dev, switch (mask) { case IIO_CHAN_INFO_RAW: + case IIO_CHAN_INFO_PROCESSED: if (holdmode) { ret = i2c_smbus_read_word_data(*client, chan->type == IIO_TEMP ? @@ -108,6 +109,17 @@ static int si7020_read_raw(struct iio_dev *indio_dev, else if (*val > 55575) /* 100%RH */ *val = 55575; } + if (mask == IIO_CHAN_INFO_PROCESSED) { + /* + * To avoid overflows the fractions can be simplified: + * temperature --> 175720 / (65536 >> 2) = 21965 / 2048 + * humidity --> 125000 / (65536 >> 2) = 15625 / 2048 + */ + if (chan->type == IIO_TEMP) + *val = (*val - 4368) * 21965 / 2048; + else + *val = (*val - 768) * 15625 / 2048; + } return IIO_VAL_INT; case IIO_CHAN_INFO_SCALE: if (chan->type == IIO_TEMP) @@ -142,12 +154,14 @@ static const struct iio_chan_spec si7020_channels[] = { { .type = IIO_HUMIDITYRELATIVE, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | - BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET), + BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET) | + BIT(IIO_CHAN_INFO_PROCESSED), }, { .type = IIO_TEMP, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | - BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET), + BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET) | + BIT(IIO_CHAN_INFO_PROCESSED), } }; -- 2.5.0 -- To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html