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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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;
+	}
+}




[Index of Archives]     [Device Tree Compilter]     [Device Tree Spec]     [Linux Driver Backports]     [Video for Linux]     [Linux USB Devel]     [Linux PCI Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [XFree86]     [Yosemite Backpacking]


  Powered by Linux