From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from 4.mo3.mail-out.ovh.net ([178.33.46.10]:56644 "EHLO 4.mo3.mail-out.ovh.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753973AbbHTOsj (ORCPT ); Thu, 20 Aug 2015 10:48:39 -0400 Received: from mail638.ha.ovh.net (gw6.ovh.net [213.251.189.206]) by mo3.mail-out.ovh.net (Postfix) with SMTP id 1E3AEFF94D0 for ; Thu, 20 Aug 2015 16:13:16 +0200 (CEST) From: Nicola Corna To: Jonathan Cameron , Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald Cc: linux-iio@vger.kernel.org, Nicola Corna Subject: [PATCH 3/3] iio:humidity:si7020: added processed data Date: Thu, 20 Aug 2015 16:11:49 +0200 Message-Id: <1440079909-1337-3-git-send-email-nicola@corna.info> In-Reply-To: <1440079909-1337-1-git-send-email-nicola@corna.info> References: <1440079909-1337-1-git-send-email-nicola@corna.info> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-iio-owner@vger.kernel.org List-Id: linux-iio@vger.kernel.org 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 --- 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