lkml.org 
[lkml]   [2021]   [Mar]   [21]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: [PATCH v1 2/2] iio: temperature: add driver support for ti tmp117
From
Date
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@gmail.com>

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

\
 
 \ /
  Last update: 2021-03-21 06:10    [W:0.079 / U:1.664 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site