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

2021-03-21 Thread Andy Shevchenko
(Seems it didn't make mailing list)

On Sat, Mar 20, 2021 at 10:55 PM Andy Shevchenko
 wrote:
>
>
>
> On Saturday, March 20, 2021, 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
>>
>
> No blank line is needed here
>
>
>>
>> Signed-off-by: Puranjay Mohan 
>> ---
>>  drivers/iio/temperature/Kconfig  |  10 ++
>>  drivers/iio/temperature/Makefile |   1 +
>>  drivers/iio/temperature/tmp117.c | 182 +++
>>  3 files changed, 193 insertions(+)
>>  create mode 100644 drivers/iio/temperature/tmp117.c
>>
>> 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..194820700
>> --- /dev/null
>> +++ b/drivers/iio/temperature/tmp117.c
>> @@ -0,0 +1,182 @@
>> +// SPDX-License-Identifier: GPL-2.0-only
>> +/*
>> + * tmp117.c - Digital temperature sensor with integrated NV memory
>
>
> File name inside the file is redundant, remove it
>
>>
>> + *
>> + * 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 
>
>
>>
>> +#include 
>
>
> You should use mod_devicetable.h rather than of.h.
>
>>
>> +#include 
>> +
>> +#include 
>> +#include 
>> +#include 
>
>
> Can you consider to use constants and /or formulas from units.h?
>
>>
>> +#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_SCALE   7812500   /* in uCelsius*/
>> +#define TMP117_RESOLUTION  78125
>> +#define TMP117_DEVICE_ID   0x0117
>> +
>> +struct tmp117_data {
>> +   struct i2c_client *client;
>> +   struct mutex lock;
>> +};
>> +
>> +static int tmp117_read_reg(struct tmp117_data *data, u8 reg)
>> +{
>> +   return i2c_smbus_read_word_swapped(data->client, reg);
>> +}
>> +
>> +static int tmp117_write_reg(struct tmp117_data *data, u8 reg, int val)
>> +{
>> +   return i2c_smbus_write_word_swapped(data->client, reg, val);
>> +}
>> +
>> +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;
>> +   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) / 
>> 1000;
>> +   *val2 = ((int16_t)off * (int32_t)TMP117_RESOLUTION) % 
>> 1000;
>
>
> Often when you do explicit casting it means something was not well thought 
> through. 

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

2021-03-20 Thread Lars-Peter Clausen

On 3/21/21 6:07 AM, Lars-Peter Clausen wrote:

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 


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.


Actually thinking about this, does the current implementation work 
correctly for negative temperatures?


I think there are two options to fix it. Either cast to s16 or use the 
sign_extend32() function.


Have a look at how the tmp006 driver handles this. It is a good example, 
including the error checking. In general you should check if your I2C 
read failed and return an error in that case rather than a bogus value 
for the measurement. Same for when reading the calibration offset.


Another thing. IIO reports temperature values in milli degrees Celsius. 
I believe in the current implementation the scale is so that it will 
report in degrees Celsius instead.




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

2021-03-20 Thread Lars-Peter Clausen

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 


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) / 1000;
+   *val2 = ((int16_t)off * (int32_t)TMP117_RESOLUTION) % 1000;
+   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 * 1000) + (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;
+   }
+}




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

2021-03-20 Thread Jonathan Cameron
On Sat, 20 Mar 2021 12:15:09 +0530
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 

Hi Puranjay,

Pretty clean driver.  A few comments inline.

>From a process point of view, I'd advise against sending out a new version
of a patch set until the previous one has sat on the list for a few days at 
least.
It will save you time as you can handle multiple reviews in one go.

I don't mind if you prefer to do a fast turn around though as I'm
very good at ignoring emails ;)

Jonathan

> ---
>  drivers/iio/temperature/Kconfig  |  10 ++
>  drivers/iio/temperature/Makefile |   1 +
>  drivers/iio/temperature/tmp117.c | 182 +++
>  3 files changed, 193 insertions(+)
>  create mode 100644 drivers/iio/temperature/tmp117.c
> 
> 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..194820700
> --- /dev/null
> +++ b/drivers/iio/temperature/tmp117.c
> @@ -0,0 +1,182 @@
> +// 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 

no power management yet.

> +#include 
> +#include 

No interrupts yet.

> +
> +#include 
> +#include 

I don't think you are using anything from that header.

> +#include 

You don't support any events yet.

> +
> +#define TMP117_REG_TEMP  0x0
> +#define TMP117_REG_CFGR  0x1
> +#define TMP117_REG_HIGH_LIM  0x2
> +#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_SCALE 7812500   /* in uCelsius*/
Always good to embed the units in the name so it's easy to see what it
is at usepoint. I also suspect this is actually in nano Celsius.

TMP117_SCALE_UC or MICRO_C

> +#define TMP117_RESOLUTION78125

Define one of scale or resolution in terms of the other though
with suggested change to calibbias scaling this may not be used anyway.

> +#define TMP117_DEVICE_ID 0x0117
> +
> +struct tmp117_data {
> + struct i2c_client *client;
> + struct mutex lock;

Locks always need documentation to explain what their scope is.
In this case the lock is never actually locked so writing that doc
would have made it fairly obvious it wasn't useful!

> +};
> +
> +static int tmp117_read_reg(struct tmp117_data *data, u8 reg)
> +{
> + return i2c_smbus_read_word_swapped(data->client, reg);

These two wrappers don't add anything significant.
Just put the i2c calls direct inline.

> +}
> +
> +static int tmp117_write_reg(struct tmp117_data *data, u8 reg, int val)
> +{
> + return i2c_smbus_write_word_swapped(data->client, reg, val);
> +}
> +
> +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);
> 

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

2021-03-20 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 
---
 drivers/iio/temperature/Kconfig  |  10 ++
 drivers/iio/temperature/Makefile |   1 +
 drivers/iio/temperature/tmp117.c | 182 +++
 3 files changed, 193 insertions(+)
 create mode 100644 drivers/iio/temperature/tmp117.c

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..194820700
--- /dev/null
+++ b/drivers/iio/temperature/tmp117.c
@@ -0,0 +1,182 @@
+// 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 
+#include 
+#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_SCALE   7812500   /* in uCelsius*/
+#define TMP117_RESOLUTION  78125
+#define TMP117_DEVICE_ID   0x0117
+
+struct tmp117_data {
+   struct i2c_client *client;
+   struct mutex lock;
+};
+
+static int tmp117_read_reg(struct tmp117_data *data, u8 reg)
+{
+   return i2c_smbus_read_word_swapped(data->client, reg);
+}
+
+static int tmp117_write_reg(struct tmp117_data *data, u8 reg, int val)
+{
+   return i2c_smbus_write_word_swapped(data->client, reg, val);
+}
+
+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;
+   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) / 1000;
+   *val2 = ((int16_t)off * (int32_t)TMP117_RESOLUTION) % 1000;
+   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 * 1000) + (val2 * 10))
+   / (int32_t)TMP117_RESOLUTION;
+   return tmp117_write_reg(data, TMP117_REG_TEMP_OFFSET, off);
+
+   default:
+   return -EINVAL;
+   }
+}
+
+static const struct iio_chan_spec