Skip to contentSkip to conversation

iio: temperature: add support for tmp117

PATCH In review Puranjay Mohan 5 participants 9 messages Apr 7, 2021 – Apr 18, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v4 cover iio: temperature: add support for tmp117

Add the dt-bindings and the driver for tmp117 sensor.

Changes since v3:
1. Remove bug related to caching the calibbias.
2. Expand NV to Non-Volatile.
3. Include limits.h and use its macros in clamp(). Changes since v2:
1. Made Calibbias read and write in same units.
2. Add missing includes.
3. Clamp the values before writing to calibbias.
4. Add i2c description to dt-binding example.
5. Remove explicit casting at different places. Changes since v1:
1. Remove unused headers
2. Add error checking in i2c read/write.
3. Correct DT bindings.
4. Correct implementation to return tmp in milli celcius.
5. Remove unused mutex lock.
6. Modify MAINTAINERS.
Changes since v0:
1. Correct Yaml syntax.
2. Change IIO_CHAN_INFO_OFFSET to IIO_CHAN_INFO_CALIBBIAS.
3. Implement IIO_CHAN_INFO_SCALE.
4. Use devm_iio_device_register().
5. Remove unused headers like delay.h

Puranjay Mohan (2):
dt-bindings: iio: temperature: Add DT bindings for TMP117
iio: temperature: add driver support for ti tmp117

 .../bindings/iio/temperature/ti,tmp117.yaml   |  41 ++++
 MAINTAINERS                                   |   7 +
 drivers/iio/temperature/Kconfig               |  10 +
 drivers/iio/temperature/Makefile              |   1 +
 drivers/iio/temperature/tmp117.c              | 184 ++++++++++++++++++
 5 files changed, 243 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/temperature/ti,tmp117.yaml
 create mode 100644 drivers/iio/temperature/tmp117.c
Signature · 1 line
2.30.1
#1 lore raw

PATCH v4 1/2 dt-bindings: iio: temperature: Add DT bindings for TMP117

Add devicetree binding document for TMP117, a digital temperature sensor.

---
 .../bindings/iio/temperature/ti,tmp117.yaml   | 41 +++++++++++++++++++
 1 file changed, 41 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/temperature/ti,tmp117.yaml
Documentation/devicetree/bindings/iio/temperature/ti,tmp117.yaml new file +41 -0
new file mode 100644
index 000000000..347bc16a4
@@ -0,0 +1,41 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/iio/temperature/ti,tmp117.yaml#"
+$schema: "http://devicetree.org/meta-schemas/core.yaml#"
+
+title: "TI TMP117 - Digital temperature sensor with integrated NV memory"
+
+description: |
+    TI TMP117 - Digital temperature sensor with integrated NV memory that supports
+    I2C interface.
+      https://www.ti.com/lit/gpn/tmp1
+
+maintainers:
+  - Puranjay Mohan <[email protected]>
+
+properties:
+  compatible:
+    enum:
+      - ti,tmp117
+
+  reg:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        tmp117@48 {
+             compatible = "ti,tmp117";
+             reg = <0x48>;
+        };
+    };
Signature · 1 line
2.30.1
maintainer Reviewed-by on v4 1/2
#2 lore raw

Re: [PATCH v4 1/2] dt-bindings: iio: temperature: Add DT bindings for TMP117

Quoted 8 lines · Puranjay Mohan

Add devicetree binding document for TMP117, a digital temperature sensor.

---
 .../bindings/iio/temperature/ti,tmp117.yaml   | 41 +++++++++++++++++++
 1 file changed, 41 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/temperature/ti,tmp117.yaml
#3 lore raw

PATCH v4 2/2 iio: temperature: add driver support for ti tmp117

In reply to Puranjay Mohan

TMP117 is a Digital temperature sensor with integrated Non-Volatile memory. Add support for tmp117 driver in iio subsystem.

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

---
 MAINTAINERS                      |   7 ++
 drivers/iio/temperature/Kconfig  |  10 ++
 drivers/iio/temperature/Makefile |   1 +
 drivers/iio/temperature/tmp117.c | 184 +++++++++++++++++++++++++++++++
 4 files changed, 202 insertions(+)
 create mode 100644 drivers/iio/temperature/tmp117.c
MAINTAINERS +7 -0
index 60ed2963e..c9b806b63 100644
@@ -16666,6 +16666,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 <[email protected]>
+L:	[email protected]
+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 <[email protected]>
 L:	[email protected]
drivers/iio/temperature/Kconfig +10 -0
index f1f2a1499..c5482983f 100644
@@ -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
drivers/iio/temperature/Makefile +1 -0
index 90c113115..e3392c4b2 100644
@@ -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
drivers/iio/temperature/tmp117.c new file +184 -0

184 lines not on this page · raw message

Signature · 1 line
2.30.1
Reviewed-by on v4 2/2
#4 lore raw

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

Quoted 6 lines · Puranjay Mohan

TMP117 is a Digital temperature sensor with integrated Non-Volatile memory. Add support for tmp117 driver in iio subsystem.

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

Looks good to me, thanks.

maintainer
#5 lore raw

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

In reply to Puranjay Mohan
Quoted 6 lines · On Wed, 7 Apr 2021 23:51:47 +0530 Puranjay Mohan <[email protected]> wrote:

TMP117 is a Digital temperature sensor with integrated Non-Volatile memory. Add support for tmp117 driver in iio subsystem.

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

A few really small things in here that I tidied up whilst applying and running local build tests.

Note that as IIO is effectively closed for the coming merge window, I'm queuing this up for the next cycle and it will be in linux-next after the merge window closes in about 3 weeks time.

Thanks,

Jonathan

Quoted 16 lines
drivers/iio/temperature/tmp117.c new file +16 -0
+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;
+

drivers/iio/temperature/tmp117.c line 67

Quoted 10 lines
drivers/iio/temperature/tmp117.c new file +10 -0
+	case IIO_CHAN_INFO_CALIBBIAS:
+		ret = i2c_smbus_read_word_swapped(data->client,
+					TMP117_REG_TEMP_OFFSET);
+		if (ret < 0)
+			return ret;
+		*val = sign_extend32(ret, 15);
+		return IIO_VAL_INT;
+
+	case IIO_CHAN_INFO_SCALE:
+		/* Conversion from 10s of uC to mC

Totally trivial so I'll fix it whilst applying, but comment convention used in IIO is

/*
* Conversion

Quoted 13 lines
drivers/iio/temperature/tmp117.c new file +13 -0
+		 * as IIO reports temperature in mC
+		 */
+		*val = TMP117_RESOLUTION_10UC / MICRODEGREE_PER_10MILLIDEGREE;
+		*val2 = (TMP117_RESOLUTION_10UC %
+					MICRODEGREE_PER_10MILLIDEGREE) * 100;
+
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	default:
+		return -EINVAL;
+	}
+}
+

drivers/iio/temperature/tmp117.c line 90

Quoted 10 lines
drivers/iio/temperature/tmp117.c new file +10 -0
+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 = clamp(val, S16_MIN, S16_MAX);

With a C=1 W=1 build (sparse an lots of warnings) this causes problems because the S16_MIN and S16_MAX are as you might imagine s16 values whereas val is an int. I've added casts to force S16_MIN and S16_MAX to ints as well.

Quoted 10 lines
drivers/iio/temperature/tmp117.c new file +10 -0
+		if (off == data->calibbias)
+			return 0;
+		data->calibbias = off;
+		return i2c_smbus_write_word_swapped(data->client,
+						TMP117_REG_TEMP_OFFSET, off);
+
+	default:
+		return -EINVAL;
+	}
+}

...

#6 lore raw

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

Quoted 6 lines · Jonathan Cameron
Quoted 2 lines · On Wed, 7 Apr 2021 23:51:47 +0530 Puranjay Mohan <[email protected]> wrote:

TMP117 is a Digital temperature sensor with integrated Non-Volatile memory. Add support for tmp117 driver in iio subsystem.

...

Quoted 5 lines
Quoted 1 line

+ off = clamp(val, S16_MIN, S16_MAX);

With a C=1 W=1 build (sparse an lots of warnings) this causes problems because the S16_MIN and S16_MAX are as you might imagine s16 values whereas val is an int. I've added casts to force S16_MIN and S16_MAX to ints as well.

Good point, but better is to use clamp_t(s16, ...) rather than explicit casting. I always consider explicit casting in C (and esp. in Linux kernel) is a red flag. Should be really rarely needed.

Quoted 2 lines
Quoted 2 lines
(quoted diff) +2 -0
+             if (off == data->calibbias)
+                     return 0;
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

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

Quoted 5 lines · Andy Shevchenko
Quoted 2 lines · Jonathan Cameron

On Wed, 7 Apr 2021 23:51:47 +0530
Puranjay Mohan <[email protected]> wrote:

Good point, but better is to use clamp_t(s16, ...) rather than explicit casting.

Sorry, I meant clamp_t(int, ...) of course, otherwise it will give wrong values.

Quoted 2 lines

I always consider explicit casting in C (and esp. in Linux kernel) is a red flag. Should be really rarely needed.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#8 lore raw

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

Quoted 9 lines · On Sun, 11 Apr 2021 21:08:29 +0300 Andy Shevchenko <[email protected]>…
Quoted 5 lines · Andy Shevchenko
Quoted 2 lines · Jonathan Cameron

On Wed, 7 Apr 2021 23:51:47 +0530
Puranjay Mohan <[email protected]> wrote:

Good point, but better is to use clamp_t(s16, ...) rather than explicit casting.

Sorry, I meant clamp_t(int, ...) of course, otherwise it will give wrong values.

I've switched it over to this which is indeed nicer.

Jonathan

Quoted 6 lines
Quoted 3 lines

I always consider explicit casting in C (and esp. in Linux kernel) is a red flag. Should be really rarely needed.