Re: [PATCH v1 2/2] iio: temperature: add driver support for ti tmp117

From: Lars-Peter Clausen
Date: Sun Mar 21 2021 - 01:09:12 EST


On 3/20/21 7:45 AM, Puranjay Mohan wrote:
TMP117 is a Digital temperature sensor with integrated NV memory.

Add support for tmp117 driver in iio subsystem.

Datasheet:-https://www.ti.com/lit/gpn/tmp117

Signed-off-by: Puranjay Mohan <puranjay12@xxxxxxxxx>

This looks good to me. Just two small bits I overlooked during the first review, sorry for that.

+};
+
[...]
+static int tmp117_read_raw(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *channel, int *val,
+ int *val2, long mask)
+{
+ struct tmp117_data *data = iio_priv(indio_dev);
+ u16 tmp, off;
+
+ switch (mask) {
+ case IIO_CHAN_INFO_RAW:
+ tmp = tmp117_read_reg(data, TMP117_REG_TEMP);
+ *val = tmp;
No need for tmp here. Just directly assign to val.
+ return IIO_VAL_INT;
+
+ case IIO_CHAN_INFO_CALIBBIAS:
+ off = tmp117_read_reg(data, TMP117_REG_TEMP_OFFSET);
+ *val = ((int16_t)off * (int32_t)TMP117_RESOLUTION) / 10000000;
+ *val2 = ((int16_t)off * (int32_t)TMP117_RESOLUTION) % 10000000;
+ return IIO_VAL_INT_PLUS_MICRO;
+
+ case IIO_CHAN_INFO_SCALE:
+ *val = 0;
+ *val2 = TMP117_SCALE;
+ return IIO_VAL_INT_PLUS_NANO;
+
+ default:
+ return -EINVAL;
+ }
+}
+
+static int tmp117_write_raw(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *channel, int val,
+ int val2, long mask)
+{
+ struct tmp117_data *data = iio_priv(indio_dev);
+ u16 off;
+
+ switch (mask) {
+ case IIO_CHAN_INFO_CALIBBIAS:
+ off = ((val * 10000000) + (val2 * 10))
+ / (int32_t)TMP117_RESOLUTION;

This needs some input validation. Writing a too large or too small value will cause an overflow/underflow and a bogus value will be written to the register.

You can either reject invalid values by returning -EINVAL or clamp them into the right range. Up to you how you want to handle this.

+ return tmp117_write_reg(data, TMP117_REG_TEMP_OFFSET, off);
+
+ default:
+ return -EINVAL;
+ }
+}