All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lars-Peter Clausen <lars@metafoo.de>
To: Puranjay Mohan <puranjay12@gmail.com>,
	alexandru.ardelean@analog.com, jic23@kernel.org,
	devicetree@vger.kernel.org, knaack.h@gmx.de,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v1 2/2] iio: temperature: add driver support for ti tmp117
Date: Sun, 21 Mar 2021 06:07:31 +0100	[thread overview]
Message-ID: <760dccc2-b098-71ce-037b-b667dbc05f57@metafoo.de> (raw)
In-Reply-To: <20210320064509.119878-3-puranjay12@gmail.com>

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


  parent reply	other threads:[~2021-03-21  5:08 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-20  6:45 [PATCH v1 0/2] iio: temperature: add support for tmp117 Puranjay Mohan
2021-03-20  6:45 ` [PATCH v1 1/2] dt-bindings: iio: temperature: Add DT bindings for TMP117 Puranjay Mohan
2021-03-20 17:51   ` Jonathan Cameron
2021-03-22  0:15   ` Rob Herring
2021-03-20  6:45 ` [PATCH v1 2/2] iio: temperature: add driver support for ti tmp117 Puranjay Mohan
2021-03-20 18:15   ` Jonathan Cameron
2021-03-21  5:07   ` Lars-Peter Clausen [this message]
2021-03-21  5:23     ` Lars-Peter Clausen
     [not found]   ` <CAHp75VdYU1VPGEHWii77wXnwp55aUd8FZ54yhO31fs_qzmTBSQ@mail.gmail.com>
2021-03-21 11:27     ` Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=760dccc2-b098-71ce-037b-b667dbc05f57@metafoo.de \
    --to=lars@metafoo.de \
    --cc=alexandru.ardelean@analog.com \
    --cc=devicetree@vger.kernel.org \
    --cc=jic23@kernel.org \
    --cc=knaack.h@gmx.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=puranjay12@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.