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

2021-04-03 Thread Lars-Peter Clausen

On 4/3/21 4:58 PM, Puranjay Mohan wrote:

On Fri, Apr 2, 2021 at 1:43 PM Lars-Peter Clausen  wrote:

On 4/1/21 11:16 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 

Nice and clean driver. Just some comments about the CALIBBIAS.


[...]
+#define TMP117_RESOLUTION_10UC   78125

Isn't the unit here 100 uC?

it is 7.8125 milli degrees_C so 78125 x 10^-4 milli degrees_C
which is 78125 x 10^-4 x 10^3 micro degrees_C
so it becomes 78125 x 10^-1 micro degrees_C = 78125 10_microdegrees_C.
Did it in detail so I remember it in the future. I guess you thought
it as 0.78125 millidegrees_C?
Ah, I get it, it is a tenth micro degree, not tens of micro degrees, 
sorry got confused.

[...]


I think that would be quite unexpected behavior. The unit should be the
same. Here in the read function you can just return the register value.

Okay, if you feel that would be right then I will do it.
Yea, I think reading and writing in different units would be a bit 
confusing.

Just make sure to properly sign extend like for the RAW property.


+ return IIO_VAL_INT_PLUS_MICRO;
[...]
+}
+
+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);
+ s16 off;
+
+ switch (mask) {
+ case IIO_CHAN_INFO_CALIBBIAS:
+ off = (s16)val;

This should have some input validation to avoid writing bogus values to
the register when the value is out of range. You can either reject out
of range values or clamp them into the valid range (using the clamp()
macro).

the maximum value which this register takes is 0x, so it should
get clamped automatically when casting to s16?
I might be wrong here.
Casting will truncate the upper bits. So something like 0x12345 gets 
turned into 0x2345.



+ return i2c_smbus_write_word_swapped(data->client,
+ TMP117_REG_TEMP_OFFSET, off);
+
+ default:
+ return -EINVAL;
+ }
+}
+

[...]







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

2021-04-03 Thread Puranjay Mohan
On Fri, Apr 2, 2021 at 1:43 PM Lars-Peter Clausen  wrote:
>
> On 4/1/21 11:16 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 
>
> Nice and clean driver. Just some comments about the CALIBBIAS.
>
> > [...]
> > +#define TMP117_RESOLUTION_10UC   78125
> Isn't the unit here 100 uC?

it is 7.8125 milli degrees_C so 78125 x 10^-4 milli degrees_C
which is 78125 x 10^-4 x 10^3 micro degrees_C
so it becomes 78125 x 10^-1 micro degrees_C = 78125 10_microdegrees_C.
Did it in detail so I remember it in the future. I guess you thought
it as 0.78125 millidegrees_C?

> > +#define TMP117_DEVICE_ID 0x0117
> > +
> > +struct tmp117_data {
> > + struct i2c_client *client;
> > +};
> > +
> > +static int tmp117_read_raw(struct iio_dev *indio_dev,
> > + struct iio_chan_spec const *channel, int *val,
> > + int *val2, long mask)
> > +{
> > [...]
> > + case IIO_CHAN_INFO_CALIBBIAS:
> > + ret = i2c_smbus_read_word_swapped(data->client,
> > + TMP117_REG_TEMP_OFFSET);
> > + if (ret < 0)
> > + return ret;
> > + *val = ((int16_t)ret * (int32_t)TMP117_RESOLUTION_10UC)
> > + / 1;
> > + *val2 = ((int16_t)ret * (int32_t)TMP117_RESOLUTION_10UC)
> > + % 1;
>
> If I understand this right CALBBIAS is written in one unit, but read in
> another unit. E.g. if you do `echo 100 > ..._calibbias` and then `cat
> ..._calibbias` you'd read a different value back.
>
yes, reading it will return the value in milli degrees_C. but to write
it should be in the register format according to the datasheet.

> I think that would be quite unexpected behavior. The unit should be the
> same. Here in the read function you can just return the register value.

Okay, if you feel that would be right then I will do it.
> Just make sure to properly sign extend like for the RAW property.
>
> > + return IIO_VAL_INT_PLUS_MICRO;
> > [...]
> > +}
> > +
> > +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);
> > + s16 off;
> > +
> > + switch (mask) {
> > + case IIO_CHAN_INFO_CALIBBIAS:
> > + off = (s16)val;
> This should have some input validation to avoid writing bogus values to
> the register when the value is out of range. You can either reject out
> of range values or clamp them into the valid range (using the clamp()
> macro).
the maximum value which this register takes is 0x, so it should
get clamped automatically when casting to s16?
I might be wrong here.

> > + return i2c_smbus_write_word_swapped(data->client,
> > + TMP117_REG_TEMP_OFFSET, off);
> > +
> > + default:
> > + return -EINVAL;
> > + }
> > +}
> > +
> [...]



-- 
Thanks and Regards

Yours Truly,

Puranjay Mohan


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

2021-04-03 Thread Andy Shevchenko
On Fri, Apr 2, 2021 at 11:14 AM Lars-Peter Clausen  wrote:
>
> On 4/1/21 11:36 AM, Andy Shevchenko wrote:
> > [...]
> >> +   case IIO_CHAN_INFO_SCALE:
> >> +   /* Conversion from 10s of uC to mC
> >> +* as IIO reports temperature in mC
> >> +*/
> >> +   *val = TMP117_RESOLUTION_10UC / 1;
> >> +   *val2 = (TMP117_RESOLUTION_10UC % 1) * 100;
> >> +   return IIO_VAL_INT_PLUS_MICRO;
> > You use 1 many times, can you give it an appropriate name (via #define)?
> #define TENTHOUSAND 1 ;)

Actually, besides April fool's day, this can have a value if 1000 is
defined as mC _PER_uC or so.
Oh, wait. We have it already.

#define MILLIDEGREE_PER_DEGREE 1000





--
With Best Regards,
Andy Shevchenko


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

2021-04-02 Thread Lars-Peter Clausen

On 4/1/21 11:36 AM, Andy Shevchenko wrote:

[...]

+   case IIO_CHAN_INFO_SCALE:
+   /* Conversion from 10s of uC to mC
+* as IIO reports temperature in mC
+*/
+   *val = TMP117_RESOLUTION_10UC / 1;
+   *val2 = (TMP117_RESOLUTION_10UC % 1) * 100;
+   return IIO_VAL_INT_PLUS_MICRO;

You use 1 many times, can you give it an appropriate name (via #define)?

#define TENTHOUSAND 1 ;)




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

2021-04-02 Thread Lars-Peter Clausen

On 4/1/21 11:16 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 


Nice and clean driver. Just some comments about the CALIBBIAS.


[...]
+#define TMP117_RESOLUTION_10UC 78125

Isn't the unit here 100 uC?

+#define TMP117_DEVICE_ID   0x0117
+
+struct tmp117_data {
+   struct i2c_client *client;
+};
+
+static int tmp117_read_raw(struct iio_dev *indio_dev,
+   struct iio_chan_spec const *channel, int *val,
+   int *val2, long mask)
+{
[...]
+   case IIO_CHAN_INFO_CALIBBIAS:
+   ret = i2c_smbus_read_word_swapped(data->client,
+   TMP117_REG_TEMP_OFFSET);
+   if (ret < 0)
+   return ret;
+   *val = ((int16_t)ret * (int32_t)TMP117_RESOLUTION_10UC)
+   / 1;
+   *val2 = ((int16_t)ret * (int32_t)TMP117_RESOLUTION_10UC)
+   % 1;


If I understand this right CALBBIAS is written in one unit, but read in 
another unit. E.g. if you do `echo 100 > ..._calibbias` and then `cat 
..._calibbias` you'd read a different value back.


I think that would be quite unexpected behavior. The unit should be the 
same. Here in the read function you can just return the register value. 
Just make sure to properly sign extend like for the RAW property.



+   return IIO_VAL_INT_PLUS_MICRO;
[...]
+}
+
+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);
+   s16 off;
+
+   switch (mask) {
+   case IIO_CHAN_INFO_CALIBBIAS:
+   off = (s16)val;
This should have some input validation to avoid writing bogus values to 
the register when the value is out of range. You can either reject out 
of range values or clamp them into the valid range (using the clamp() 
macro).

+   return i2c_smbus_write_word_swapped(data->client,
+   TMP117_REG_TEMP_OFFSET, off);
+
+   default:
+   return -EINVAL;
+   }
+}
+

[...]


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

2021-04-01 Thread Andy Shevchenko
On Thu, Apr 1, 2021 at 12:19 PM Puranjay Mohan  wrote:
>
> TMP117 is a Digital temperature sensor with integrated NV memory.
>
> Add support for tmp117 driver in iio subsystem.

+ blank line

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

Make it a tag, i.e. remove the following blank line and use a space after colon.

>
> Signed-off-by: Puranjay Mohan 

...

> +/*
> + * tmp117.c - Digital temperature sensor with integrated NV memory

It's useless and provokes an unneeded churn when having a file name
inside the file.
Please, drop it for good.

> + *
> + * Copyright (c) 2021 Puranjay Mohan 
> + *
> + * Driver for the Texas Instruments TMP117 Temperature Sensor

> + *

Redundant blank line.

> + * (7-bit I2C slave address (0x48 - 0x4B), changeable via ADD pins)
> + *
> + * Note: This driver assumes that the sensor has been calibrated beforehand.
> + */

...

> +#include 
> +#include 
> +#include 

Missed:
  bitops.h //sign_extend32()
  types.h // s32


> +
> +#include 

...

> +struct tmp117_data {
> +   struct i2c_client *client;
> +};

Doesn't make any sense to have a separate structure for just one
pointer member. Use that pointer directly.

...

> +   case IIO_CHAN_INFO_CALIBBIAS:
> +   ret = i2c_smbus_read_word_swapped(data->client,
> +   TMP117_REG_TEMP_OFFSET);
> +   if (ret < 0)
> +   return ret;
> +   *val = ((int16_t)ret * (int32_t)TMP117_RESOLUTION_10UC)
> +   / 1;

One line

> +   *val2 = ((int16_t)ret * (int32_t)TMP117_RESOLUTION_10UC)
> +   % 1;

One line.

I'll be honest, I do not like these explicit castings at all. Can you
revisit and try to refactor that you won't need them?
For example, I can't understand how ret can be higher than 16 bit
since we checked on negative values beforehand.

> +   return IIO_VAL_INT_PLUS_MICRO;
> +
> +   case IIO_CHAN_INFO_SCALE:
> +   /* Conversion from 10s of uC to mC
> +* as IIO reports temperature in mC
> +*/
> +   *val = TMP117_RESOLUTION_10UC / 1;
> +   *val2 = (TMP117_RESOLUTION_10UC % 1) * 100;
> +   return IIO_VAL_INT_PLUS_MICRO;

You use 1 many times, can you give it an appropriate name (via #define)?

...

> +   s16 off;

> +   case IIO_CHAN_INFO_CALIBBIAS:

> +   off = (s16)val;

Redundant explicit casting.

> +   return i2c_smbus_write_word_swapped(data->client,
> +   TMP117_REG_TEMP_OFFSET, off);

...

> +static const struct of_device_id tmp117_of_match[] = {
> +   { .compatible = "ti,tmp117", },

> +   { },

No need to comma in terminator line(s).

> +};

-- 
With Best Regards,
Andy Shevchenko


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

2021-04-01 Thread Puranjay Mohan
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 
---
 MAINTAINERS  |   7 ++
 drivers/iio/temperature/Kconfig  |  10 ++
 drivers/iio/temperature/Makefile |   1 +
 drivers/iio/temperature/tmp117.c | 179 +++
 4 files changed, 197 insertions(+)
 create mode 100644 drivers/iio/temperature/tmp117.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 60ed2963e..c9b806b63 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1,6 +1,13 @@ F: include/dt-bindings/soc/ti,sci_pm_domain.h
 F: include/linux/soc/ti/ti_sci_inta_msi.h
 F: include/linux/soc/ti/ti_sci_protocol.h
 
+TEXAS INSTRUMENTS' TMP117 TEMPERATURE SENSOR DRIVER
+M: Puranjay Mohan 
+L: linux-...@vger.kernel.org
+S: Supported
+F: Documentation/devicetree/bindings/iio/temperature/ti,tmp117.yaml
+F: drivers/iio/temperature/tmp117.c
+
 THANKO'S RAREMONO AM/FM/SW RADIO RECEIVER USB DRIVER
 M: Hans Verkuil 
 L: linux-me...@vger.kernel.org
diff --git a/drivers/iio/temperature/Kconfig b/drivers/iio/temperature/Kconfig
index f1f2a1499..c5482983f 100644
--- a/drivers/iio/temperature/Kconfig
+++ b/drivers/iio/temperature/Kconfig
@@ -97,6 +97,16 @@ config TMP007
  This driver can also be built as a module. If so, the module will
  be called tmp007.
 
+config TMP117
+   tristate "TMP117 Digital temperature sensor with integrated NV memory"
+   depends on I2C
+   help
+ If you say yes here you get support for the Texas Instruments
+ TMP117 Digital temperature sensor with integrated NV memory.
+
+ This driver can also be built as a module. If so, the module will
+ be called tmp117.
+
 config TSYS01
tristate "Measurement Specialties TSYS01 temperature sensor using I2C 
bus connection"
depends on I2C
diff --git a/drivers/iio/temperature/Makefile b/drivers/iio/temperature/Makefile
index 90c113115..e3392c4b2 100644
--- a/drivers/iio/temperature/Makefile
+++ b/drivers/iio/temperature/Makefile
@@ -12,5 +12,6 @@ obj-$(CONFIG_MLX90614) += mlx90614.o
 obj-$(CONFIG_MLX90632) += mlx90632.o
 obj-$(CONFIG_TMP006) += tmp006.o
 obj-$(CONFIG_TMP007) += tmp007.o
+obj-$(CONFIG_TMP117) += tmp117.o
 obj-$(CONFIG_TSYS01) += tsys01.o
 obj-$(CONFIG_TSYS02D) += tsys02d.o
diff --git a/drivers/iio/temperature/tmp117.c b/drivers/iio/temperature/tmp117.c
new file mode 100644
index 0..83c0b856f
--- /dev/null
+++ b/drivers/iio/temperature/tmp117.c
@@ -0,0 +1,179 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * tmp117.c - Digital temperature sensor with integrated NV memory
+ *
+ * Copyright (c) 2021 Puranjay Mohan 
+ *
+ * Driver for the Texas Instruments TMP117 Temperature Sensor
+ *
+ * (7-bit I2C slave address (0x48 - 0x4B), changeable via ADD pins)
+ *
+ * Note: This driver assumes that the sensor has been calibrated beforehand.
+ */
+
+#include 
+#include 
+#include 
+
+#include 
+
+#define TMP117_REG_TEMP0x0
+#define TMP117_REG_CFGR0x1
+#define TMP117_REG_HIGH_LIM0x2
+#define TMP117_REG_LOW_LIM 0x3
+#define TMP117_REG_EEPROM_UL   0x4
+#define TMP117_REG_EEPROM1 0x5
+#define TMP117_REG_EEPROM2 0x6
+#define TMP117_REG_TEMP_OFFSET 0x7
+#define TMP117_REG_EEPROM3 0x8
+#define TMP117_REG_DEVICE_ID   0xF
+
+#define TMP117_RESOLUTION_10UC 78125
+#define TMP117_DEVICE_ID   0x0117
+
+struct tmp117_data {
+   struct i2c_client *client;
+};
+
+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);
+   s32 ret;
+
+   switch (mask) {
+   case IIO_CHAN_INFO_RAW:
+   ret = i2c_smbus_read_word_swapped(data->client,
+   TMP117_REG_TEMP);
+   if (ret < 0)
+   return ret;
+   *val = sign_extend32(ret, 15);
+   return IIO_VAL_INT;
+
+   case IIO_CHAN_INFO_CALIBBIAS:
+   ret = i2c_smbus_read_word_swapped(data->client,
+   TMP117_REG_TEMP_OFFSET);
+   if (ret < 0)
+   return ret;
+   *val = ((int16_t)ret * (int32_t)TMP117_RESOLUTION_10UC)
+   / 1;
+   *val2 = ((int16_t)ret * (int32_t)TMP117_RESOLUTION_10UC)
+   % 1;
+   return IIO_VAL_INT_PLUS_MICRO;
+
+   case IIO_CHAN_INFO_SCALE:
+   /* Conversion from 10s of uC to mC
+* as IIO reports temperature in mC
+*/
+   *val = TMP1