2021-04-07 21:51:24

by Puranjay Mohan

[permalink] [raw]
Subject: [PATCH v4 0/2] 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

--
2.30.1


2021-04-07 21:52:10

by Puranjay Mohan

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

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

Signed-off-by: Puranjay Mohan <[email protected]>
---
.../bindings/iio/temperature/ti,tmp117.yaml | 41 +++++++++++++++++++
1 file changed, 41 insertions(+)
create mode 100644 Documentation/devicetree/bindings/iio/temperature/ti,tmp117.yaml

diff --git a/Documentation/devicetree/bindings/iio/temperature/ti,tmp117.yaml b/Documentation/devicetree/bindings/iio/temperature/ti,tmp117.yaml
new file mode 100644
index 000000000..347bc16a4
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/temperature/ti,tmp117.yaml
@@ -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>;
+ };
+ };
--
2.30.1

2021-04-07 21:52:43

by Puranjay Mohan

[permalink] [raw]
Subject: [PATCH v4 2/2] iio: temperature: add driver support for ti tmp117

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
Signed-off-by: Puranjay Mohan <[email protected]>
Reviewed-by: Andy Shevchenko <[email protected]>
---
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

diff --git a/MAINTAINERS b/MAINTAINERS
index 60ed2963e..c9b806b63 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -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]
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 000000000..e2536e53f
--- /dev/null
+++ b/drivers/iio/temperature/tmp117.c
@@ -0,0 +1,184 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Digital temperature sensor with integrated Non-volatile memory
+ * Copyright (c) 2021 Puranjay Mohan <[email protected]>
+ *
+ * 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 <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/bitops.h>
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <linux/limits.h>
+
+#include <linux/iio/iio.h>
+
+#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_RESOLUTION_10UC 78125
+#define TMP117_DEVICE_ID 0x0117
+#define MICRODEGREE_PER_10MILLIDEGREE 10000
+
+struct tmp117_data {
+ struct i2c_client *client;
+ s16 calibbias;
+};
+
+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 = sign_extend32(ret, 15);
+ return IIO_VAL_INT;
+
+ case IIO_CHAN_INFO_SCALE:
+ /* Conversion from 10s of uC to mC
+ * 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;
+ }
+}
+
+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);
+ 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;
+ }
+}
+
+static const struct iio_chan_spec tmp117_channels[] = {
+ {
+ .type = IIO_TEMP,
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+ BIT(IIO_CHAN_INFO_CALIBBIAS) | BIT(IIO_CHAN_INFO_SCALE),
+ },
+};
+
+static const struct iio_info tmp117_info = {
+ .read_raw = tmp117_read_raw,
+ .write_raw = tmp117_write_raw,
+};
+
+static int tmp117_identify(struct i2c_client *client)
+{
+ int dev_id;
+
+ dev_id = i2c_smbus_read_word_swapped(client, TMP117_REG_DEVICE_ID);
+ if (dev_id < 0)
+ return dev_id;
+ if (dev_id != TMP117_DEVICE_ID) {
+ dev_err(&client->dev, "TMP117 not found\n");
+ return -ENODEV;
+ }
+ return 0;
+}
+
+static int tmp117_probe(struct i2c_client *client)
+{
+ struct tmp117_data *data;
+ struct iio_dev *indio_dev;
+ int ret;
+
+ if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
+ return -EOPNOTSUPP;
+
+ ret = tmp117_identify(client);
+ if (ret < 0)
+ return ret;
+
+ indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+ if (!indio_dev)
+ return -ENOMEM;
+
+ data = iio_priv(indio_dev);
+ data->client = client;
+ data->calibbias = 0;
+
+ indio_dev->name = "tmp117";
+ indio_dev->modes = INDIO_DIRECT_MODE;
+ indio_dev->info = &tmp117_info;
+
+ indio_dev->channels = tmp117_channels;
+ indio_dev->num_channels = ARRAY_SIZE(tmp117_channels);
+
+ return devm_iio_device_register(&client->dev, indio_dev);
+}
+
+static const struct of_device_id tmp117_of_match[] = {
+ { .compatible = "ti,tmp117", },
+ { }
+};
+MODULE_DEVICE_TABLE(of, tmp117_of_match);
+
+static const struct i2c_device_id tmp117_id[] = {
+ { "tmp117", 0 },
+ { }
+};
+MODULE_DEVICE_TABLE(i2c, tmp117_id);
+
+static struct i2c_driver tmp117_driver = {
+ .driver = {
+ .name = "tmp117",
+ .of_match_table = tmp117_of_match,
+ },
+ .probe_new = tmp117_probe,
+ .id_table = tmp117_id,
+};
+module_i2c_driver(tmp117_driver);
+
+MODULE_AUTHOR("Puranjay Mohan <[email protected]>");
+MODULE_DESCRIPTION("TI TMP117 Temperature sensor driver");
+MODULE_LICENSE("GPL");
--
2.30.1

2021-04-08 20:24:06

by Rob Herring

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

On Wed, 07 Apr 2021 23:51:46 +0530, Puranjay Mohan wrote:
> Add devicetree binding document for TMP117, a digital temperature sensor.
>
> Signed-off-by: Puranjay Mohan <[email protected]>
> ---
> .../bindings/iio/temperature/ti,tmp117.yaml | 41 +++++++++++++++++++
> 1 file changed, 41 insertions(+)
> create mode 100644 Documentation/devicetree/bindings/iio/temperature/ti,tmp117.yaml
>

Reviewed-by: Rob Herring <[email protected]>

2021-04-09 06:55:44

by Lars-Peter Clausen

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

On 4/7/21 8:21 PM, Puranjay Mohan 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
> Signed-off-by: Puranjay Mohan <[email protected]>
> Reviewed-by: Andy Shevchenko <[email protected]>

Looks good to me, thanks.

Reviewed-by: Lars-Peter Clausen <[email protected]>

2021-04-11 17:12:43

by Jonathan Cameron

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

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
> Signed-off-by: Puranjay Mohan <[email protected]>
> Reviewed-by: Andy Shevchenko <[email protected]>

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


> +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 = 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

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

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

2021-04-11 18:11:25

by Andy Shevchenko

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

On Sun, Apr 11, 2021 at 9:07 PM Andy Shevchenko
<[email protected]> wrote:
> On Sun, Apr 11, 2021 at 5:53 PM Jonathan Cameron <[email protected]> wrote:
> > 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 always consider explicit casting in C (and esp. in Linux kernel) is
> a red flag. Should be really rarely needed.



--
With Best Regards,
Andy Shevchenko

2021-04-11 18:12:08

by Andy Shevchenko

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

On Sun, Apr 11, 2021 at 5:53 PM Jonathan Cameron <[email protected]> wrote:
>
> 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.

...

> > + 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.

> > + if (off == data->calibbias)
> > + return 0;

--
With Best Regards,
Andy Shevchenko

2021-04-18 09:49:42

by Jonathan Cameron

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

On Sun, 11 Apr 2021 21:08:29 +0300
Andy Shevchenko <[email protected]> wrote:

> On Sun, Apr 11, 2021 at 9:07 PM Andy Shevchenko
> <[email protected]> wrote:
> > On Sun, Apr 11, 2021 at 5:53 PM Jonathan Cameron <[email protected]> wrote:
> > > 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

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