Skip to contentSkip to conversation

iio: hudmidity: hdc100x: fix incorrect shifting and scaling

PATCH In review Matt Ranostay 3 participants 6 messages May 30, 2016 – Jun 3, 2016 IIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: hudmidity: hdc100x: fix incorrect shifting and scaling

Shifting sensor data to the right 2 bits was incorrect and caused the scaling values + offsets to be invalid.

---
 drivers/iio/humidity/hdc100x.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/iio/humidity/hdc100x.c +8 -8
index 3070983..a03832a 100644
@@ -164,14 +164,14 @@ static int hdc100x_get_measurement(struct hdc100x_data *data,
 		dev_err(&client->dev, "cannot read high byte measurement");
 		return ret;
 	}
-	val = ret << 6;
+	val = ret << 8;
 
 	ret = i2c_smbus_read_byte(client);
 	if (ret < 0) {
 		dev_err(&client->dev, "cannot read low byte measurement");
 		return ret;
 	}
-	val |= ret >> 2;
+	val |= ret;
 
 	return val;
 }
@@ -212,17 +212,17 @@ static int hdc100x_read_raw(struct iio_dev *indio_dev,
 	case IIO_CHAN_INFO_SCALE:
 		if (chan->type == IIO_TEMP) {
 			*val = 165000;
-			*val2 = 65536 >> 2;
+			*val2 = 65536;
 			return IIO_VAL_FRACTIONAL;
 		} else {
-			*val = 0;
-			*val2 = 10000;
-			return IIO_VAL_INT_PLUS_MICRO;
+			*val = 100;
+			*val2 = 65536;
+			return IIO_VAL_FRACTIONAL;
 		}
 		break;
 	case IIO_CHAN_INFO_OFFSET:
-		*val = -3971;
-		*val2 = 879096;
+		*val = -15887;
+		*val2 = 515151;
 		return IIO_VAL_INT_PLUS_MICRO;
 	default:
 		return -EINVAL;
Signature · 1 line
2.7.4
#1 lore raw

Alison,

Can you give a Tested-by: since I don't own this sensor anymore.

Thanks,

Matt

Quoted 57 lines · Matt Ranostay

Shifting sensor data to the right 2 bits was incorrect and caused the scaling values + offsets to be invalid.

---
 drivers/iio/humidity/hdc100x.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/iio/humidity/hdc100x.c +8 -9
index 3070983..a03832a 100644
@@ -164,14 +164,14 @@ static int hdc100x_get_measurement(struct hdc100x_data *data,
                dev_err(&client->dev, "cannot read high byte measurement");
                return ret;
        }
-       val = ret << 6;
+       val = ret << 8;
 
        ret = i2c_smbus_read_byte(client);
        if (ret < 0) {
                dev_err(&client->dev, "cannot read low byte measurement");
                return ret;
        }
-       val |= ret >> 2;
+       val |= ret;
 
        return val;
 }
@@ -212,17 +212,17 @@ static int hdc100x_read_raw(struct iio_dev *indio_dev,
        case IIO_CHAN_INFO_SCALE:
                if (chan->type == IIO_TEMP) {
                        *val = 165000;
-                       *val2 = 65536 >> 2;
+                       *val2 = 65536;
                        return IIO_VAL_FRACTIONAL;
                } else {
-                       *val = 0;
-                       *val2 = 10000;
-                       return IIO_VAL_INT_PLUS_MICRO;
+                       *val = 100;
+                       *val2 = 65536;
+                       return IIO_VAL_FRACTIONAL;
                }
                break;
        case IIO_CHAN_INFO_OFFSET:
-               *val = -3971;
-               *val2 = 879096;
+               *val = -15887;
+               *val2 = 515151;
                return IIO_VAL_INT_PLUS_MICRO;
        default:
                return -EINVAL;
--

2.7.4

#2 lore raw
Quoted 7 lines · Matt Ranostay

Alison,

Can you give a Tested-by: since I don't own this sensor anymore.

Thanks,

Matt

Tested it. Verified temp & humidty calcs. Looks great!

And, (seeing a pattern here ;)) this comes with another question, probably directed a Jonathan...

with regards to temperature calculations:

Datasheets give numbers for this method:
temp = ((raw * scale) + offset)
IIO drivers report numbers for this method:
temp = ((raw + offset) * scale)

I've figured out the math for translating between them and creating the offset for the driver to report in sysfs, but don't know why we are doing that. 'Our' way seems kind of ugly. For example, for HDC1008 the offset is -40. It's the minimum expected temp value. But, we munge it to be -15887.515151 so we can apply it before scaling.

Why do we do it this way in IIO?

alisons

Quoted 59 lines · Matt Ranostay
Quoted 57 lines

Shifting sensor data to the right 2 bits was incorrect and caused the scaling values + offsets to be invalid.

---
 drivers/iio/humidity/hdc100x.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/iio/humidity/hdc100x.c +8 -9
index 3070983..a03832a 100644
@@ -164,14 +164,14 @@ static int hdc100x_get_measurement(struct hdc100x_data *data,
                dev_err(&client->dev, "cannot read high byte measurement");
                return ret;
        }
-       val = ret << 6;
+       val = ret << 8;
 
        ret = i2c_smbus_read_byte(client);
        if (ret < 0) {
                dev_err(&client->dev, "cannot read low byte measurement");
                return ret;
        }
-       val |= ret >> 2;
+       val |= ret;
 
        return val;
 }
@@ -212,17 +212,17 @@ static int hdc100x_read_raw(struct iio_dev *indio_dev,
        case IIO_CHAN_INFO_SCALE:
                if (chan->type == IIO_TEMP) {
                        *val = 165000;
-                       *val2 = 65536 >> 2;
+                       *val2 = 65536;
                        return IIO_VAL_FRACTIONAL;
                } else {
-                       *val = 0;
-                       *val2 = 10000;
-                       return IIO_VAL_INT_PLUS_MICRO;
+                       *val = 100;
+                       *val2 = 65536;
+                       return IIO_VAL_FRACTIONAL;
                }
                break;
        case IIO_CHAN_INFO_OFFSET:
-               *val = -3971;
-               *val2 = 879096;
+               *val = -15887;
+               *val2 = 515151;
                return IIO_VAL_INT_PLUS_MICRO;
        default:
                return -EINVAL;
--

2.7.4

#3 lore raw
Quoted 14 lines · Alison Schofield
Quoted 7 lines · Matt Ranostay

Alison,

Can you give a Tested-by: since I don't own this sensor anymore.

Thanks,

Matt

Tested it. Verified temp & humidty calcs. Looks great!

And, (seeing a pattern here ;)) this comes with another question, probably directed a Jonathan...

Quoted 7 lines

with regards to temperature calculations:

Datasheets give numbers for this method:
temp = ((raw * scale) + offset)
IIO drivers report numbers for this method:
temp = ((raw + offset) * scale)

We do it this way in iio.. hence why the offset needs the scaling value applied to it.

Quoted 69 lines

I've figured out the math for translating between them and creating the offset for the driver to report in sysfs, but don't know why we are doing that. 'Our' way seems kind of ugly. For example, for HDC1008 the offset is -40. It's the minimum expected temp value. But, we munge it to be -15887.515151 so we can apply it before scaling.

Why do we do it this way in IIO?

alisons

Quoted 59 lines · Matt Ranostay
Quoted 57 lines

Shifting sensor data to the right 2 bits was incorrect and caused the scaling values + offsets to be invalid.

---
 drivers/iio/humidity/hdc100x.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/iio/humidity/hdc100x.c +8 -9
index 3070983..a03832a 100644
@@ -164,14 +164,14 @@ static int hdc100x_get_measurement(struct hdc100x_data *data,
                dev_err(&client->dev, "cannot read high byte measurement");
                return ret;
        }
-       val = ret << 6;
+       val = ret << 8;
 
        ret = i2c_smbus_read_byte(client);
        if (ret < 0) {
                dev_err(&client->dev, "cannot read low byte measurement");
                return ret;
        }
-       val |= ret >> 2;
+       val |= ret;
 
        return val;
 }
@@ -212,17 +212,17 @@ static int hdc100x_read_raw(struct iio_dev *indio_dev,
        case IIO_CHAN_INFO_SCALE:
                if (chan->type == IIO_TEMP) {
                        *val = 165000;
-                       *val2 = 65536 >> 2;
+                       *val2 = 65536;
                        return IIO_VAL_FRACTIONAL;
                } else {
-                       *val = 0;
-                       *val2 = 10000;
-                       return IIO_VAL_INT_PLUS_MICRO;
+                       *val = 100;
+                       *val2 = 65536;
+                       return IIO_VAL_FRACTIONAL;
                }
                break;
        case IIO_CHAN_INFO_OFFSET:
-               *val = -3971;
-               *val2 = 879096;
+               *val = -15887;
+               *val2 = 515151;
                return IIO_VAL_INT_PLUS_MICRO;
        default:
                return -EINVAL;
--

2.7.4

maintainer
#4 lore raw
Quoted 26 lines · Matt Ranostay
Quoted 14 lines · Alison Schofield
Quoted 7 lines · Matt Ranostay

Alison,

Can you give a Tested-by: since I don't own this sensor anymore.

Thanks,

Matt

Tested it. Verified temp & humidty calcs. Looks great!

And, (seeing a pattern here ;)) this comes with another question, probably directed a Jonathan...

Quoted 7 lines

with regards to temperature calculations:

Datasheets give numbers for this method:
temp = ((raw * scale) + offset)
IIO drivers report numbers for this method:
temp = ((raw + offset) * scale)

We do it this way in iio.. hence why the offset needs the scaling value applied to it.

Quoted 9 lines
Quoted 3 lines

I've figured out the math for translating between them and creating the offset for the driver to report in sysfs, but don't know why we are doing that. 'Our' way seems kind of ugly. For example, for

HDC1008

Quoted 1 line

the offset is -40. It's the minimum expected temp value. But, we

munge

Quoted 3 lines

it to be -15887.515151 so we can apply it before scaling.

Why do we do it this way in IIO?

It is very device dependant or more correctly very data sheet dependant. By coincidence a
lot of early IIO drivers were for parts that had it the other way around so we ended up with that. A classic example is a differential ADC that does an offset rather than 2's complement. Those are much neater offset first.

So six of one and half a dozen of the other...

Jonathan

Quoted 69 lines
Quoted 5 lines

alisons

Quoted 2 lines

On Sun, May 29, 2016 at 7:52 PM, Matt Ranostay <[email protected]>

Quoted 1 line · wrote:
Quoted 1 line
Quoted 1 line

Shifting sensor data to the right 2 bits was incorrect and caused

the

Quoted 9 lines
Quoted 9 lines

Review

Quoted 9 lines

scaling values + offsets to be invalid.

---
 drivers/iio/humidity/hdc100x.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
(quoted diff) +0 -0

b/drivers/iio/humidity/hdc100x.c

Quoted 4 lines
Quoted 4 lines

drivers/iio/humidity/hdc100x.c static int hdc100x_get_measurement(struct

Quoted 4 lines

index 3070983..a03832a 100644

drivers/iio/humidity/hdc100x.c +0 -0
@@ -164,14 +164,14 @@ static int hdc100x_get_measurement(struct

hdc100x_data *data,

Quoted 1 line
Quoted 1 line

drivers/iio/humidity/hdc100x.c static int hdc100x_get_measurement(struct · line 164

Quoted 1 line
drivers/iio/humidity/hdc100x.c +0 -0
                dev_err(&client->dev, "cannot read high byte

measurement");

Quoted 8 lines
Quoted 8 lines

drivers/iio/humidity/hdc100x.c static int hdc100x_get_measurement(struct hdc100x_data *data,

Quoted 8 lines
drivers/iio/humidity/hdc100x.c +1 -1
                return ret;
        }
-       val = ret << 6;
+       val = ret << 8;
 
        ret = i2c_smbus_read_byte(client);
        if (ret < 0) {
                dev_err(&client->dev, "cannot read low byte

measurement");

Quoted 8 lines
Quoted 8 lines

drivers/iio/humidity/hdc100x.c static int hdc100x_read_raw(struct iio_dev

Quoted 8 lines
drivers/iio/humidity/hdc100x.c +1 -1
                return ret;
        }
-       val |= ret >> 2;
+       val |= ret;
 
        return val;
 }
@@ -212,17 +212,17 @@ static int hdc100x_read_raw(struct iio_dev

*indio_dev,

Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
drivers/iio/humidity/hdc100x.c +6 -7
        case IIO_CHAN_INFO_SCALE:
                if (chan->type == IIO_TEMP) {
                        *val = 165000;
-                       *val2 = 65536 >> 2;
+                       *val2 = 65536;
                        return IIO_VAL_FRACTIONAL;
                } else {
-                       *val = 0;
-                       *val2 = 10000;
-                       return IIO_VAL_INT_PLUS_MICRO;
+                       *val = 100;
+                       *val2 = 65536;
+                       return IIO_VAL_FRACTIONAL;
                }
                break;
        case IIO_CHAN_INFO_OFFSET:
-               *val = -3971;
-               *val2 = 879096;
+               *val = -15887;
+               *val2 = 515151;
                return IIO_VAL_INT_PLUS_MICRO;
        default:
                return -EINVAL;
--

2.7.4

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
maintainer Tested-by Alison Schofield
#5 lore raw
In reply to Alison Schofield
Quoted 10 lines · Alison Schofield
Quoted 7 lines · Matt Ranostay

Alison,

Can you give a Tested-by: since I don't own this sensor anymore.

Thanks,

Matt

Tested it. Verified temp & humidty calcs. Looks great!

Ideally please give an explicit tag.

(I've add this time on the basis you meant that I think...) Applied to fixes-togreg and marked for stable. Thanks,

Jonathan

Quoted 80 lines

And, (seeing a pattern here ;)) this comes with another question, probably directed a Jonathan...

with regards to temperature calculations:

Datasheets give numbers for this method:
temp = ((raw * scale) + offset)
IIO drivers report numbers for this method:
temp = ((raw + offset) * scale)

I've figured out the math for translating between them and creating the offset for the driver to report in sysfs, but don't know why we are doing that. 'Our' way seems kind of ugly. For example, for HDC1008 the offset is -40. It's the minimum expected temp value. But, we munge it to be -15887.515151 so we can apply it before scaling.

Why do we do it this way in IIO?

alisons

Quoted 59 lines · Matt Ranostay
Quoted 57 lines

Shifting sensor data to the right 2 bits was incorrect and caused the scaling values + offsets to be invalid.

---
 drivers/iio/humidity/hdc100x.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/iio/humidity/hdc100x.c +8 -9
index 3070983..a03832a 100644
@@ -164,14 +164,14 @@ static int hdc100x_get_measurement(struct hdc100x_data *data,
                dev_err(&client->dev, "cannot read high byte measurement");
                return ret;
        }
-       val = ret << 6;
+       val = ret << 8;
 
        ret = i2c_smbus_read_byte(client);
        if (ret < 0) {
                dev_err(&client->dev, "cannot read low byte measurement");
                return ret;
        }
-       val |= ret >> 2;
+       val |= ret;
 
        return val;
 }
@@ -212,17 +212,17 @@ static int hdc100x_read_raw(struct iio_dev *indio_dev,
        case IIO_CHAN_INFO_SCALE:
                if (chan->type == IIO_TEMP) {
                        *val = 165000;
-                       *val2 = 65536 >> 2;
+                       *val2 = 65536;
                        return IIO_VAL_FRACTIONAL;
                } else {
-                       *val = 0;
-                       *val2 = 10000;
-                       return IIO_VAL_INT_PLUS_MICRO;
+                       *val = 100;
+                       *val2 = 65536;
+                       return IIO_VAL_FRACTIONAL;
                }
                break;
        case IIO_CHAN_INFO_OFFSET:
-               *val = -3971;
-               *val2 = 879096;
+               *val = -15887;
+               *val2 = 515151;
                return IIO_VAL_INT_PLUS_MICRO;
        default:
                return -EINVAL;
--

2.7.4