Skip to contentSkip to conversation

hwmon: new driver for ST stts751 thermal sensor

PATCH Superseded Andrea Merello 3 participants 9 messages Sep 7, 2016 – Sep 8, 2016 1 message not in the archive

Subsystems (2)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 hwmon: new driver for ST stts751 thermal sensor

This patch adds a HWMON driver for ST Microelectronics STTS751 temperature sensors.

It does support manual-triggered conversions as well as automatic conversions. The latter is used when the "event" or "therm" function is present (declaring the physical wire is attached in the DT).

Thanks-to: LABBE Corentin [for suggestions]

Cc LABBE Corentin <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Jean Delvare <[email protected]>
---
 drivers/hwmon/Kconfig   |  12 +-
 drivers/hwmon/Makefile  |   2 +-
 drivers/hwmon/stts751.c | 948 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 960 insertions(+), 2 deletions(-)
 create mode 100644 drivers/hwmon/stts751.c
drivers/hwmon/Kconfig +11 -1
index eaf2f91..8fdd241 100644
@@ -1448,6 +1448,16 @@ config SENSORS_SCH5636
 	  This driver can also be built as a module.  If so, the module
 	  will be called sch5636.
 
+config SENSORS_STTS751
+	tristate "ST Microelectronics STTS751"
+	depends on I2C
+	help
+	  If you say yes here you get support for STTS751
+	  temperature sensor chips.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called stts751.
+
 config SENSORS_SMM665
 	tristate "Summit Microelectronics SMM665"
 	depends on I2C
@@ -1506,7 +1516,7 @@ config SENSORS_ADS7871
 
 config SENSORS_AMC6821
 	tristate "Texas Instruments AMC6821"
-	depends on I2C 
+	depends on I2C
 	help
 	  If you say yes here you get support for the Texas Instruments
 	  AMC6821 hardware monitoring chips.
drivers/hwmon/Makefile +1 -1
index fe87d28..1114130 100644
@@ -147,6 +147,7 @@ obj-$(CONFIG_SENSORS_SMM665)	+= smm665.o
 obj-$(CONFIG_SENSORS_SMSC47B397)+= smsc47b397.o
 obj-$(CONFIG_SENSORS_SMSC47M1)	+= smsc47m1.o
 obj-$(CONFIG_SENSORS_SMSC47M192)+= smsc47m192.o
+obj-$(CONFIG_SENSORS_STTS751)	+= stts751.o
 obj-$(CONFIG_SENSORS_AMC6821)	+= amc6821.o
 obj-$(CONFIG_SENSORS_TC74)	+= tc74.o
 obj-$(CONFIG_SENSORS_THMC50)	+= thmc50.o
@@ -169,4 +170,3 @@ obj-$(CONFIG_SENSORS_WM8350)	+= wm8350-hwmon.o
 obj-$(CONFIG_PMBUS)		+= pmbus/
 
 ccflags-$(CONFIG_HWMON_DEBUG_CHIP) := -DDEBUG
-
drivers/hwmon/stts751.c new file +948 -0

948 lines not on this page · raw message

Signature · 1 line
2.7.4
#1 lore raw

PATCH 2/2 DT: add binding documentation for STTS751

Cc Rob Herring <[email protected]>
Cc Mark Rutland <[email protected]>
---
 Documentation/devicetree/bindings/hwmon/stts751.txt | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/stts751.txt
Documentation/devicetree/bindings/hwmon/stts751.txt new file +19 -0
new file mode 100644
index 0000000..277ad46
@@ -0,0 +1,19 @@
+* STTS751 thermometer.
+
+Required node properties:
+- compatible: "stts751"
+- reg: I2C bus address of the device
+
+Optional properties:
+- has-therm: specifies that the "therm" pin is wired
+- has-event: specifies that the "event" pin is wired
+- smbus-timeout-disable: when set, the smbus timeout function will be disabled
+
+Example stts751 node:
+
+temp-sensor {
+	compatible = "stts751";
+	reg = <0x48>;
+	has-therm;
+	has-event;
+}
Signature · 1 line
2.7.4
#2 lore raw
In reply to Andrea Merello

Hello

I have some coments below

Quoted 25 lines · Andrea Merello

This patch adds a HWMON driver for ST Microelectronics STTS751 temperature sensors.

It does support manual-triggered conversions as well as automatic conversions. The latter is used when the "event" or "therm" function is present (declaring the physical wire is attached in the DT).

Thanks-to: LABBE Corentin [for suggestions]

Cc LABBE Corentin <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Jean Delvare <[email protected]>
---
 drivers/hwmon/Kconfig   |  12 +-
 drivers/hwmon/Makefile  |   2 +-
 drivers/hwmon/stts751.c | 948 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 960 insertions(+), 2 deletions(-)
 create mode 100644 drivers/hwmon/stts751.c
drivers/hwmon/Kconfig +0 -0
index eaf2f91..8fdd241 100644
@@ -1448,6 +1448,16 @@ config SENSORS_SCH5636
 	  This driver can also be built as a module.  If so, the module

drivers/hwmon/Kconfig config SENSORS_SCH5636 · line 1458

Quoted 10 lines
drivers/hwmon/Kconfig +8 -0
 	  will be called sch5636.
 
+config SENSORS_STTS751
+	tristate "ST Microelectronics STTS751"
+	depends on I2C
+	help
+	  If you say yes here you get support for STTS751
+	  temperature sensor chips.
+
+	  This driver can also be built as a module.  If so, the module

Two space instead of one

drivers/hwmon/Kconfig config SENSORS_ADS7871 · line 1519

Quoted 11 lines
drivers/hwmon/Kconfig +3 -1
+	  will be called stts751.
+
 config SENSORS_SMM665
 	tristate "Summit Microelectronics SMM665"
 	depends on I2C
@@ -1506,7 +1516,7 @@ config SENSORS_ADS7871
 
 config SENSORS_AMC6821
 	tristate "Texas Instruments AMC6821"
-	depends on I2C 
+	depends on I2C

This change need to be in another set of patch

Quoted 30 lines
drivers/hwmon/Kconfig +0 -0
 	help
 	  If you say yes here you get support for the Texas Instruments
 	  AMC6821 hardware monitoring chips.
drivers/hwmon/Makefile +1 -1
index fe87d28..1114130 100644
@@ -147,6 +147,7 @@ obj-$(CONFIG_SENSORS_SMM665)	+= smm665.o
 obj-$(CONFIG_SENSORS_SMSC47B397)+= smsc47b397.o
 obj-$(CONFIG_SENSORS_SMSC47M1)	+= smsc47m1.o
 obj-$(CONFIG_SENSORS_SMSC47M192)+= smsc47m192.o
+obj-$(CONFIG_SENSORS_STTS751)	+= stts751.o
 obj-$(CONFIG_SENSORS_AMC6821)	+= amc6821.o
 obj-$(CONFIG_SENSORS_TC74)	+= tc74.o
 obj-$(CONFIG_SENSORS_THMC50)	+= thmc50.o
@@ -169,4 +170,3 @@ obj-$(CONFIG_SENSORS_WM8350)	+= wm8350-hwmon.o
 obj-$(CONFIG_PMBUS)		+= pmbus/
 
 ccflags-$(CONFIG_HWMON_DEBUG_CHIP) := -DDEBUG
-
drivers/hwmon/stts751.c new file +4 -0
new file mode 100644
index 0000000..94b7e2b
@@ -0,0 +1,948 @@
+/*
+ * STTS751 sensor driver
+ *
+ * Copyright (C) 2016 Istituto Italiano di Tecnologia - RBCS - EDL

drivers/hwmon/stts751.c line 14

Quoted 10 lines
drivers/hwmon/stts751.c new file +10 -0
+ * Robotics, Brain and Cognitive Sciences department
+ * Electronic Design Laboratory
+ *
+ * Written by Andrea Merello <[email protected]>
+ *
+ * Based on the following drivers:
+ * - LM95241 driver, which is:
+ *   Copyright (C) 2008, 2010 Davide Rizzo <[email protected]>
+ * - LM90 driver, which is:
+ *   Copyright (C) 2003-2010  Jean Delvare <[email protected]>

No need to recopy copyright, we can find them in their driver.

Quoted 4 lines
drivers/hwmon/stts751.c new file +4 -0
+
+/* HW index vs ASCII and int times in mS */
+static const struct stts751_intervals_t stts751_intervals[] = {
+	{.str = "16000", .val = 16000},

drivers/hwmon/stts751.c line 147

Quoted 10 lines
drivers/hwmon/stts751.c new file +10 -0
+	{.str = "8000", .val = 8000},
+	{.str = "4000", .val = 4000},
+	{.str = "2000", .val = 2000},
+	{.str = "1000", .val = 1000},
+	{.str = "500", .val = 500},
+	{.str = "250", .val = 250},
+	{.str = "125", .val = 125},
+	{.str = "62.5", .val = 62},
+	{.str = "31.25", .val = 31}
+};

So you are lying about 62.5 and 31.25 :) ?

Quoted 16 lines
drivers/hwmon/stts751.c new file +16 -0
+
+/* special value to indicate to the SW to use manual mode */
+#define STTS751_INTERVAL_MANUAL 0xFF
+
+struct stts751_priv {
+	struct device *dev;
+	struct i2c_client *client;
+	struct mutex access_lock;
+	unsigned long interval;
+	int res;
+	bool gen_therm, gen_event;
+	int event_max, event_min;
+	int therm;
+	int hyst;
+	bool smbus_timeout;
+	int temp;

drivers/hwmon/stts751.c line 173

Quoted 10 lines
drivers/hwmon/stts751.c new file +10 -0
+	unsigned long last_update;
+	u8 config;
+	bool min_alert, max_alert;
+	bool data_valid;
+
+	/* Temperature is always present
+	 * Depending by DT/platdata, therm, event, interval are
+	 * dynamically added.
+	 * There are max 4 entries plus the guard
+	 */

This type of comment is wrong, you need /* only at the first line

Quoted 19 lines
drivers/hwmon/stts751.c new file +19 -0
+	const struct attribute_group *groups[5];
+};
+
+static int stts751_manual_conversion(struct stts751_priv *priv)
+{
+	s32 ret;
+	unsigned long timeout;
+
+	/* Any value written to this reg will trigger manual conversion */
+	ret = i2c_smbus_write_byte_data(priv->client,
+				STTS751_REG_ONESHOT, 0xFF);
+	if (ret < 0)
+		return ret;
+
+	timeout = jiffies;
+
+	while (1) {
+		ret = i2c_smbus_read_byte_data(priv->client,
+					STTS751_REG_STATUS);

drivers/hwmon/stts751.c line 202

Quoted 10 lines
drivers/hwmon/stts751.c new file +10 -0
+		if (ret < 0)
+			return ret;
+		if (!(ret & STTS751_STATUS_BUSY))
+			return 0;
+		if (time_after(jiffies,
+				timeout + STTS751_CONV_TIMEOUT * HZ / 1000)) {
+			dev_warn(&priv->client->dev, "conversion timed out\n");
+			break;
+		}
+	}

Perhaps you could rewrite it as:
do {
...
} while (!time_after(xxx))
dev_warn()

drivers/hwmon/stts751.c line 208

Quoted 6 lines
drivers/hwmon/stts751.c new file +6 -0
+	return -ETIMEDOUT;
+}
+
+/* Converts temperature in C split in integer and fractional parts, as supplied
+ * by the HW, to an integer number in mC
+ */

Same problem for comment (and all comentw below)

Quoted 44 lines
drivers/hwmon/stts751.c new file +44 -0
+static int stts751_update_temp(struct stts751_priv *priv)
+{
+	s32 integer1, integer2, frac;
+	unsigned long sample1, sample2, timeout;
+	int i;
+	int ret = 0;
+
+	mutex_lock(&priv->access_lock);
+
+	if (priv->interval == STTS751_INTERVAL_MANUAL) {
+		/* perform a one-shot on-demand conversion */
+		ret = stts751_manual_conversion(priv);
+		if (ret) {
+			dev_warn(&priv->client->dev,
+				"failed to shot conversion %x\n", ret);
+			goto exit;
+		}
+	}
+
+	for (i = 0; i < STTS751_RACE_RETRY; i++) {
+		sample1 = jiffies;
+		integer1 = i2c_smbus_read_byte_data(priv->client,
+						STTS751_REG_TEMP_H);
+
+		if (integer1 < 0) {
+			ret = integer1;
+			dev_warn(&priv->client->dev,
+				"failed to read H reg %x\n", ret);
+			goto exit;
+		}
+
+		frac = i2c_smbus_read_byte_data(priv->client,
+						STTS751_REG_TEMP_L);
+
+		if (frac < 0) {
+			ret = frac;
+			dev_warn(&priv->client->dev,
+				"failed to read L reg %x\n", ret);
+			goto exit;
+		}
+
+		if (priv->interval == STTS751_INTERVAL_MANUAL) {
+			/* we'll look at integer2 later.. */
+			integer2 = integer1;

drivers/hwmon/stts751.c line 324

Quoted 10 lines
drivers/hwmon/stts751.c new file +10 -0
+			break;
+		}
+
+		integer2 = i2c_smbus_read_byte_data(priv->client,
+						STTS751_REG_TEMP_H);
+		sample2 = jiffies;
+
+		if (integer2 < 0) {
+			dev_warn(&priv->client->dev,
+				"failed to read H reg (2nd time) %x\n", ret);

Hard to find why you print ret and what exactly it is when you print it.

drivers/hwmon/stts751.c line 330

Quoted 6 lines
drivers/hwmon/stts751.c new file +6 -0
+			ret = integer2;
+			goto exit;
+		}
+
+		timeout = stts751_intervals[priv->interval].val * HZ / 1000;
+		timeout -= ((timeout < 10) && (timeout > 1)) ? 1 : timeout / 10;

What it is the purpose of this decrease ?

Quoted 22 lines
drivers/hwmon/stts751.c new file +22 -0
+		if ((integer1 == integer2) &&
+			time_after(sample1 + timeout, sample2))
+			break;
+
+		/* if we are going on with a racy read, don't pretend to be
+		 * super-precise, just use the MSBs ..
+		 */
+		frac = 0;
+	}
+
+exit:
+	mutex_unlock(&priv->access_lock);
+	if (ret)
+		return ret;
+
+	/* use integer2, because when we fallback to the "MSB-only" compromise
+	 * this is the more recent one
+	 */
+	priv->temp = stts751_to_deg(integer2, frac);
+	return ret;
+}
+

drivers/hwmon/stts751.c

Quoted 10 lines
drivers/hwmon/stts751.c new file +10 -0
+static int stts751_probe(struct i2c_client *client,
+			 const struct i2c_device_id *id)
+{
+	struct stts751_priv *priv;
+	int ret;
+	int groups_idx = 0;
+	struct device_node *of_node = client->dev.of_node;
+
+	priv = devm_kzalloc(&client->dev,
+			sizeof(struct stts751_priv), GFP_KERNEL);

Could be rewrite to sizeof(*priv)

Regards

Labbe Corentin

maintainer
#3 lore raw
Quoted 4 lines · LABBE Corentin

Hello

I have some coments below

Some more comments below.

Quoted 8 lines · Andrea Merello
Quoted 7 lines

This patch adds a HWMON driver for ST Microelectronics STTS751 temperature sensors.

It does support manual-triggered conversions as well as automatic conversions. The latter is used when the "event" or "therm" function is present (declaring the physical wire is attached in the DT).

All others are penaltized and have to wait for manual mode results when reading the temperature ? Why ?

Quoted 88 lines
Quoted 18 lines

Thanks-to: LABBE Corentin [for suggestions]

Cc LABBE Corentin <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Jean Delvare <[email protected]>
---
 drivers/hwmon/Kconfig   |  12 +-
 drivers/hwmon/Makefile  |   2 +-
 drivers/hwmon/stts751.c | 948 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 960 insertions(+), 2 deletions(-)
 create mode 100644 drivers/hwmon/stts751.c
drivers/hwmon/Kconfig +0 -0
index eaf2f91..8fdd241 100644
@@ -1448,6 +1448,16 @@ config SENSORS_SCH5636
 	  This driver can also be built as a module.  If so, the module

drivers/hwmon/Kconfig config SENSORS_SCH5636 · line 1458

Quoted 10 lines
drivers/hwmon/Kconfig +8 -0
 	  will be called sch5636.
 
+config SENSORS_STTS751
+	tristate "ST Microelectronics STTS751"
+	depends on I2C
+	help
+	  If you say yes here you get support for STTS751
+	  temperature sensor chips.
+
+	  This driver can also be built as a module.  If so, the module

Two space instead of one

drivers/hwmon/Kconfig config SENSORS_ADS7871 · line 1519

Quoted 11 lines
drivers/hwmon/Kconfig +3 -1
+	  will be called stts751.
+
 config SENSORS_SMM665
 	tristate "Summit Microelectronics SMM665"
 	depends on I2C
@@ -1506,7 +1516,7 @@ config SENSORS_ADS7871
 
 config SENSORS_AMC6821
 	tristate "Texas Instruments AMC6821"
-	depends on I2C
+	depends on I2C

This change need to be in another set of patch

Quoted 30 lines
drivers/hwmon/Kconfig +0 -0
 	help
 	  If you say yes here you get support for the Texas Instruments
 	  AMC6821 hardware monitoring chips.
drivers/hwmon/Makefile +1 -1
index fe87d28..1114130 100644
@@ -147,6 +147,7 @@ obj-$(CONFIG_SENSORS_SMM665)	+= smm665.o
 obj-$(CONFIG_SENSORS_SMSC47B397)+= smsc47b397.o
 obj-$(CONFIG_SENSORS_SMSC47M1)	+= smsc47m1.o
 obj-$(CONFIG_SENSORS_SMSC47M192)+= smsc47m192.o
+obj-$(CONFIG_SENSORS_STTS751)	+= stts751.o
 obj-$(CONFIG_SENSORS_AMC6821)	+= amc6821.o
 obj-$(CONFIG_SENSORS_TC74)	+= tc74.o
 obj-$(CONFIG_SENSORS_THMC50)	+= thmc50.o
@@ -169,4 +170,3 @@ obj-$(CONFIG_SENSORS_WM8350)	+= wm8350-hwmon.o
 obj-$(CONFIG_PMBUS)		+= pmbus/
 
 ccflags-$(CONFIG_HWMON_DEBUG_CHIP) := -DDEBUG
-
drivers/hwmon/stts751.c new file +4 -0
new file mode 100644
index 0000000..94b7e2b
@@ -0,0 +1,948 @@
+/*
+ * STTS751 sensor driver
+ *
+ * Copyright (C) 2016 Istituto Italiano di Tecnologia - RBCS - EDL

drivers/hwmon/stts751.c line 14

Quoted 10 lines
drivers/hwmon/stts751.c new file +10 -0
+ * Robotics, Brain and Cognitive Sciences department
+ * Electronic Design Laboratory
+ *
+ * Written by Andrea Merello <[email protected]>
+ *
+ * Based on the following drivers:
+ * - LM95241 driver, which is:
+ *   Copyright (C) 2008, 2010 Davide Rizzo <[email protected]>
+ * - LM90 driver, which is:
+ *   Copyright (C) 2003-2010  Jean Delvare <[email protected]>

No need to recopy copyright, we can find them in their driver.

Quoted 17 lines
Quoted 4 lines
drivers/hwmon/stts751.c new file +4 -0
+
+/* HW index vs ASCII and int times in mS */
+static const struct stts751_intervals_t stts751_intervals[] = {
+	{.str = "16000", .val = 16000},

drivers/hwmon/stts751.c line 147

Quoted 10 lines
drivers/hwmon/stts751.c new file +10 -0
+	{.str = "8000", .val = 8000},
+	{.str = "4000", .val = 4000},
+	{.str = "2000", .val = 2000},
+	{.str = "1000", .val = 1000},
+	{.str = "500", .val = 500},
+	{.str = "250", .val = 250},
+	{.str = "125", .val = 125},
+	{.str = "62.5", .val = 62},
+	{.str = "31.25", .val = 31}
+};

So you are lying about 62.5 and 31.25 :) ?

I don't see what the table (or rather, the string -> integer conversion) is used for anyway. kstrtol() and sprintf() (or similar) seem to be doing a good job there. Also, there is an API function to find the closest matching index in a value table.

Quoted 56 lines
Quoted 16 lines
drivers/hwmon/stts751.c new file +16 -0
+
+/* special value to indicate to the SW to use manual mode */
+#define STTS751_INTERVAL_MANUAL 0xFF
+
+struct stts751_priv {
+	struct device *dev;
+	struct i2c_client *client;
+	struct mutex access_lock;
+	unsigned long interval;
+	int res;
+	bool gen_therm, gen_event;
+	int event_max, event_min;
+	int therm;
+	int hyst;
+	bool smbus_timeout;
+	int temp;

drivers/hwmon/stts751.c line 173

Quoted 10 lines
drivers/hwmon/stts751.c new file +10 -0
+	unsigned long last_update;
+	u8 config;
+	bool min_alert, max_alert;
+	bool data_valid;
+
+	/* Temperature is always present
+	 * Depending by DT/platdata, therm, event, interval are
+	 * dynamically added.
+	 * There are max 4 entries plus the guard
+	 */

This type of comment is wrong, you need /* only at the first line

Quoted 27 lines
drivers/hwmon/stts751.c new file +27 -0
+	const struct attribute_group *groups[5];
+};
+
+static int stts751_manual_conversion(struct stts751_priv *priv)
+{
+	s32 ret;
+	unsigned long timeout;
+
+	/* Any value written to this reg will trigger manual conversion */
+	ret = i2c_smbus_write_byte_data(priv->client,
+				STTS751_REG_ONESHOT, 0xFF);
+	if (ret < 0)
+		return ret;
+
+	timeout = jiffies;
+
+	while (1) {
+		ret = i2c_smbus_read_byte_data(priv->client,
+					STTS751_REG_STATUS);
+		if (ret < 0)
+			return ret;
+		if (!(ret & STTS751_STATUS_BUSY))
+			return 0;
+		if (time_after(jiffies,
+				timeout + STTS751_CONV_TIMEOUT * HZ / 1000)) {
+			dev_warn(&priv->client->dev, "conversion timed out\n");
+			break;
Quoted 9 lines

drivers/hwmon/stts751.c line 83

Quoted 2 lines
drivers/hwmon/stts751.c new file +2 -0
+		}
+	}

Perhaps you could rewrite it as:
do {
...
} while (!time_after(xxx))
dev_warn()

It is unacceptable anyway, it being a hot loop, and I am not a friend of noisy warnings. I'd also like to see an explanation why manual mode has to be supported in the first place.

Quoted 9 lines

drivers/hwmon/stts751.c line 208

Quoted 6 lines
drivers/hwmon/stts751.c new file +6 -0
+	return -ETIMEDOUT;
+}
+
+/* Converts temperature in C split in integer and fractional parts, as supplied
+ * by the HW, to an integer number in mC
+ */

Same problem for comment (and all comentw below)

Quoted 15 lines
Quoted 5 lines
drivers/hwmon/stts751.c new file +5 -0
+static int stts751_update_temp(struct stts751_priv *priv)
+{
+	s32 integer1, integer2, frac;
+	unsigned long sample1, sample2, timeout;
+	int i;

drivers/hwmon/stts751.c line 285

Quoted 10 lines
drivers/hwmon/stts751.c new file +10 -0
+	int ret = 0;
+
+	mutex_lock(&priv->access_lock);
+
+	if (priv->interval == STTS751_INTERVAL_MANUAL) {
+		/* perform a one-shot on-demand conversion */
+		ret = stts751_manual_conversion(priv);
+		if (ret) {
+			dev_warn(&priv->client->dev,
+				"failed to shot conversion %x\n", ret);

If those are warnings, they are not errors and should not result in an abort. If they are errors, they should be displayed as errors. But, as mentioned before, I am not a friend of noisy drivers.

Quoted 33 lines
Quoted 33 lines
drivers/hwmon/stts751.c new file +33 -0
+			goto exit;
+		}
+	}
+
+	for (i = 0; i < STTS751_RACE_RETRY; i++) {
+		sample1 = jiffies;
+		integer1 = i2c_smbus_read_byte_data(priv->client,
+						STTS751_REG_TEMP_H);
+
+		if (integer1 < 0) {
+			ret = integer1;
+			dev_warn(&priv->client->dev,
+				"failed to read H reg %x\n", ret);
+			goto exit;
+		}
+
+		frac = i2c_smbus_read_byte_data(priv->client,
+						STTS751_REG_TEMP_L);
+
+		if (frac < 0) {
+			ret = frac;
+			dev_warn(&priv->client->dev,
+				"failed to read L reg %x\n", ret);
+			goto exit;
+		}
+
+		if (priv->interval == STTS751_INTERVAL_MANUAL) {
+			/* we'll look at integer2 later.. */
+			integer2 = integer1;
+			break;
+		}
+
+		integer2 = i2c_smbus_read_byte_data(priv->client,
Quoted 9 lines

drivers/hwmon/stts751.c line 324

Quoted 6 lines
drivers/hwmon/stts751.c new file +6 -0
+						STTS751_REG_TEMP_H);
+		sample2 = jiffies;
+
+		if (integer2 < 0) {
+			dev_warn(&priv->client->dev,
+				"failed to read H reg (2nd time) %x\n", ret);

Hard to find why you print ret and what exactly it is when you print it.

Plus the check should be right after the assignment.

Quoted 11 lines

drivers/hwmon/stts751.c line 330

Quoted 6 lines
drivers/hwmon/stts751.c new file +6 -0
+			ret = integer2;
+			goto exit;
+		}
+
+		timeout = stts751_intervals[priv->interval].val * HZ / 1000;
+		timeout -= ((timeout < 10) && (timeout > 1)) ? 1 : timeout / 10;

What it is the purpose of this decrease ?

drivers/hwmon/stts751.c line 332

Quoted 2 lines
drivers/hwmon/stts751.c new file +2 -0
+		if ((integer1 == integer2) &&
+			time_after(sample1 + timeout, sample2))

And what is the purpose of this time_after() ? Other drivers have the same problem and a much simpler solution that doesn't require a timeout like this. If it is in fact needed, I would want to see a comment explaining it.

Quoted 9 lines
Quoted 9 lines
drivers/hwmon/stts751.c new file +9 -0
+			break;
+
+		/* if we are going on with a racy read, don't pretend to be
+		 * super-precise, just use the MSBs ..
+		 */
+		frac = 0;
+	}
+
+exit:
Quoted 8 lines

drivers/hwmon/stts751.c line 349

Quoted 8 lines
drivers/hwmon/stts751.c new file +8 -0
+	mutex_unlock(&priv->access_lock);
+	if (ret)
+		return ret;
+
+	/* use integer2, because when we fallback to the "MSB-only" compromise
+	 * this is the more recent one
+	 */
+	priv->temp = stts751_to_deg(integer2, frac);

All other drivers I am aware of manage to handle conversions from 16 bit chip values to temperatures and from temperatures to chip values with a single parameter.

Quoted 11 lines

drivers/hwmon/stts751.c line 863

Quoted 11 lines
drivers/hwmon/stts751.c new file +11 -0
+	return ret;
+}
+
+static int stts751_probe(struct i2c_client *client,
+			 const struct i2c_device_id *id)
+{
+	struct stts751_priv *priv;
+	int ret;
+	int groups_idx = 0;
+	struct device_node *of_node = client->dev.of_node;
+

np is the commonly accepted variable name.

Quoted 10 lines

drivers/hwmon/stts751.c line 865

Quoted 2 lines
drivers/hwmon/stts751.c new file +2 -0
+	priv = devm_kzalloc(&client->dev,
+			sizeof(struct stts751_priv), GFP_KERNEL);

Could be rewrite to sizeof(*priv)

Regards

Labbe Corentin

A message that is not in the archive (sent off-list or not yet received): CAN8YU5OAR4RyyK-qvVVXV_bSjEkj9bLRSxUF98ZSLdOxkZr=Vw@mail.gmail.com

maintainer
#5 lore raw
Quoted 22 lines · Andrea Merello

On Wed, Sep 7, 2016 at 4:07 PM, Guenter Roeck <[email protected] <mailto:[email protected]>> wrote:

On 09/07/2016 06:16 AM, LABBE Corentin wrote:

Hello

I have some coments below

Some more comments below.

On Wed, Sep 07, 2016 at 02:33:36PM +0200, Andrea Merello wrote:

This patch adds a HWMON driver for ST Microelectronics STTS751 temperature sensors.

It does support manual-triggered conversions as well as automatic conversions. The latter is used when the "event" or "therm" function is present (declaring the physical wire is attached in the DT).

Quoted 6 lines

All others are penaltized and have to wait for manual mode results when reading the temperature ? Why ?

The reason was that with this HW reading a precise value when automatic conversion is running is a pain (as explained in the long comment at the file beginning).

As I said, other drivers / chips have the same problem of "non-atomic" reads across multiple registers. Maybe there is something else different with this chip, but from the explanation there is no indication what it might be.

Quoted 120 lines

BTW if we want to avoid waiting, I could try letting the converter running in auto mode, and disable it just before reading, perform the read, and enable again (but IMHO only when we have no therm/event, for the reasons explained in the comment)..

Thanks-to: LABBE Corentin [for suggestions]

Signed-off-by Andrea Merello <[email protected] <mailto:[email protected]>>
Cc LABBE Corentin <[email protected] <mailto:[email protected]>>
Cc Guenter Roeck <[email protected] <mailto:[email protected]>>
Cc Jean Delvare <[email protected] <mailto:[email protected]>>

---
drivers/hwmon/Kconfig | 12 +-
drivers/hwmon/Makefile | 2 +-
drivers/hwmon/stts751.c | 948 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 960 insertions(+), 2 deletions(-)
create mode 100644 drivers/hwmon/stts751.c

diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index eaf2f91..8fdd241 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -1448,6 +1448,16 @@ config SENSORS_SCH5636
This driver can also be built as a module. If so, the module will be called sch5636.

+config SENSORS_STTS751
+ tristate "ST Microelectronics STTS751"
+ depends on I2C
+ help
+ If you say yes here you get support for STTS751
+ temperature sensor chips.
+
+ This driver can also be built as a module. If so, the module

Two space instead of one

+ will be called stts751.
+
config SENSORS_SMM665
tristate "Summit Microelectronics SMM665" depends on I2C
@@ -1506,7 +1516,7 @@ config SENSORS_ADS7871

config SENSORS_AMC6821
tristate "Texas Instruments AMC6821"
- depends on I2C
+ depends on I2C

This change need to be in another set of patch

help
If you say yes here you get support for the Texas Instruments AMC6821 hardware monitoring chips.
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile index fe87d28..1114130 100644
--- a/drivers/hwmon/Makefile
+++ b/drivers/hwmon/Makefile
@@ -147,6 +147,7 @@ obj-$(CONFIG_SENSORS_SMM665) += smm665.o
obj-$(CONFIG_SENSORS_SMSC47B397)+= smsc47b397.o obj-$(CONFIG_SENSORS_SMSC47M1) += smsc47m1.o obj-$(CONFIG_SENSORS_SMSC47M192)+= smsc47m192.o
+obj-$(CONFIG_SENSORS_STTS751) += stts751.o
obj-$(CONFIG_SENSORS_AMC6821) += amc6821.o
obj-$(CONFIG_SENSORS_TC74) += tc74.o
obj-$(CONFIG_SENSORS_THMC50) += thmc50.o
@@ -169,4 +170,3 @@ obj-$(CONFIG_SENSORS_WM8350) += wm8350-hwmon.o
obj-$(CONFIG_PMBUS) += pmbus/

ccflags-$(CONFIG_HWMON_DEBUG_CHIP) := -DDEBUG
-
diff --git a/drivers/hwmon/stts751.c b/drivers/hwmon/stts751.c
new file mode 100644
index 0000000..94b7e2b
--- /dev/null
+++ b/drivers/hwmon/stts751.c
@@ -0,0 +1,948 @@
+/*
+ * STTS751 sensor driver
+ *
+ * Copyright (C) 2016 Istituto Italiano di Tecnologia - RBCS - EDL
+ * Robotics, Brain and Cognitive Sciences department
+ * Electronic Design Laboratory
+ *
+ * Written by Andrea Merello <[email protected] <mailto:[email protected]>>
+ *
+ * Based on the following drivers:
+ * - LM95241 driver, which is:
+ * Copyright (C) 2008, 2010 Davide Rizzo <[email protected] <mailto:[email protected]>>
+ * - LM90 driver, which is:
+ * Copyright (C) 2003-2010 Jean Delvare <[email protected] <mailto:[email protected]>>

No need to recopy copyright, we can find them in their driver.

+
+/* HW index vs ASCII and int times in mS */
+static const struct stts751_intervals_t stts751_intervals[] = {
+ {.str = "16000", .val = 16000},
+ {.str = "8000", .val = 8000},
+ {.str = "4000", .val = 4000},
+ {.str = "2000", .val = 2000},
+ {.str = "1000", .val = 1000},
+ {.str = "500", .val = 500},
+ {.str = "250", .val = 250},
+ {.str = "125", .val = 125},
+ {.str = "62.5", .val = 62},
+ {.str = "31.25", .val = 31}
+};

So you are lying about 62.5 and 31.25 :) ?

I don't see what the table (or rather, the string -> integer conversion) is used for anyway. kstrtol() and sprintf() (or similar) seem to be doing a good job there. Also, there is an API function to find the closest matching index in a value table.

Quoted 7 lines

Well, the table does not only relates strings to integer (that would be really natural doing with kstrtol() and friends), but it also relates the conversion rate with the HW index for that conversion rate (str vs table index). So, given that I would have an array anyway, keeping also the pre-converted values seemed reasonable to me

But I could store just the strings in the table, use kstrtol, and demote the table to be a single-dimensional array, if this is preferred.

I'll look at the API you are suggesting. I wasn't aware of it...

Try find_nearest().

Quoted 69 lines

+
+/* special value to indicate to the SW to use manual mode */ +#define STTS751_INTERVAL_MANUAL 0xFF
+
+struct stts751_priv {
+ struct device *dev;
+ struct i2c_client *client;
+ struct mutex access_lock;
+ unsigned long interval;
+ int res;
+ bool gen_therm, gen_event;
+ int event_max, event_min;
+ int therm;
+ int hyst;
+ bool smbus_timeout;
+ int temp;
+ unsigned long last_update;
+ u8 config;
+ bool min_alert, max_alert;
+ bool data_valid;
+
+ /* Temperature is always present
+ * Depending by DT/platdata, therm, event, interval are
+ * dynamically added.
+ * There are max 4 entries plus the guard
+ */

This type of comment is wrong, you need /* only at the first line

+ const struct attribute_group *groups[5];
+};
+
+static int stts751_manual_conversion(struct stts751_priv *priv) +{
+ s32 ret;
+ unsigned long timeout;
+
+ /* Any value written to this reg will trigger manual conversion */
+ ret = i2c_smbus_write_byte_data(priv->client,
+ STTS751_REG_ONESHOT, 0xFF);
+ if (ret < 0)
+ return ret;
+
+ timeout = jiffies;
+
+ while (1) {
+ ret = i2c_smbus_read_byte_data(priv->client,
+ STTS751_REG_STATUS);
+ if (ret < 0)
+ return ret;
+ if (!(ret & STTS751_STATUS_BUSY))
+ return 0;
+ if (time_after(jiffies,
+ timeout + STTS751_CONV_TIMEOUT * HZ / 1000)) {
+ dev_warn(&priv->client->dev, "conversion timed out\n");
+ break;
+ }
+ }

Perhaps you could rewrite it as:
do {
...
} while (!time_after(xxx))
dev_warn()

Quoted 7 lines

It is unacceptable anyway, it being a hot loop, and I am not a friend of noisy warnings. I'd also like to see an explanation why manual mode has to be supported in the first place.

I see your point, but It's supposed not to happen unless there is a problem. And if there is, then I thought you might prefer to have extra noise rather than silence.. Anyway, I can either pull the warn off the loop (or rip it out completely) or try avoiding manual mode (see above for explanation).

Just keep in mind that I won't accept a hot loop (ie one that keeps burning CPU without sleeping).

The only reason for possibly accepting manual mode might be to reduce power consumption. Since power consumption for continuous mode can be reduced to a max of 35uA, I don't really see the point. Unless there is more explanation how this chip is different to other chips in the same situation, I don't really accept the explanation for all the complexity either.

Quoted 102 lines

+ return -ETIMEDOUT;
+}
+
+/* Converts temperature in C split in integer and fractional parts, as supplied
+ * by the HW, to an integer number in mC
+ */

Same problem for comment (and all comentw below)

+static int stts751_update_temp(struct stts751_priv *priv) +{
+ s32 integer1, integer2, frac;
+ unsigned long sample1, sample2, timeout;
+ int i;
+ int ret = 0;
+
+ mutex_lock(&priv->access_lock);
+
+ if (priv->interval == STTS751_INTERVAL_MANUAL) {
+ /* perform a one-shot on-demand conversion */
+ ret = stts751_manual_conversion(priv);
+ if (ret) {
+ dev_warn(&priv->client->dev,
+ "failed to shot conversion %x\n", ret);

If those are warnings, they are not errors and should not result in an abort. If they are errors, they should be displayed as errors. But, as mentioned before, I am not a friend of noisy drivers.

You're right. It's an error. I might change this is either an error, or given that you don't like it, in a dev_dbg..

+ goto exit;
+ }
+ }
+
+ for (i = 0; i < STTS751_RACE_RETRY; i++) {
+ sample1 = jiffies;
+ integer1 = i2c_smbus_read_byte_data(priv->client,
+ STTS751_REG_TEMP_H);
+
+ if (integer1 < 0) {
+ ret = integer1;
+ dev_warn(&priv->client->dev,
+ "failed to read H reg %x\n", ret);
+ goto exit;
+ }
+
+ frac = i2c_smbus_read_byte_data(priv->client,
+ STTS751_REG_TEMP_L);
+
+ if (frac < 0) {
+ ret = frac;
+ dev_warn(&priv->client->dev,
+ "failed to read L reg %x\n", ret);
+ goto exit;
+ }
+
+ if (priv->interval == STTS751_INTERVAL_MANUAL) {
+ /* we'll look at integer2 later.. */
+ integer2 = integer1;
+ break;
+ }
+
+ integer2 = i2c_smbus_read_byte_data(priv->client,
+ STTS751_REG_TEMP_H);
+ sample2 = jiffies;
+
+ if (integer2 < 0) {
+ dev_warn(&priv->client->dev,
+ "failed to read H reg (2nd time) %x\n", ret);

Hard to find why you print ret and what exactly it is when you print it.

Plus the check should be right after the assignment.

Just sample the time ASAP, that is "critical", then check if we have to throw the error.

+ ret = integer2;
+ goto exit;
+ }
+
+ timeout = stts751_intervals[priv->interval].val * HZ / 1000;
+ timeout -= ((timeout < 10) && (timeout > 1)) ? 1 : timeout / 10;

What it is the purpose of this decrease ?

+ if ((integer1 == integer2) &&
+ time_after(sample1 + timeout, sample2))

Quoted 8 lines

And what is the purpose of this time_after() ? Other drivers have the same problem and a much simpler solution that doesn't require a timeout like this. If it is in fact needed, I would want to see a comment explaining it.

Will look if I can find another driver with similar issue to snoop at..

Try lm90.c:lm90_read16().

Quoted 21 lines

+ break;
+
+ /* if we are going on with a racy read, don't pretend to be
+ * super-precise, just use the MSBs ..
+ */
+ frac = 0;
+ }
+
+exit:
+ mutex_unlock(&priv->access_lock);
+ if (ret)
+ return ret;
+
+ /* use integer2, because when we fallback to the "MSB-only" compromise
+ * this is the more recent one
+ */
+ priv->temp = stts751_to_deg(integer2, frac);

Quoted 6 lines

All other drivers I am aware of manage to handle conversions from 16 bit chip values to temperatures and from temperatures to chip values with a single parameter.

I'll check this.. BTW This chip wants two separated reads from two registers, one for the integer and one for the fractional part. The purpose of this function is interpreting these two raw HW values to convert in a number. Is this so bad ?

Again, have a look at lm90.c:lm90_read16() and the conversion functions in drivers which have to read two registers to read the temperature.

Is it bad ? It makes the code more complicated than it has to be and increases code size, so, yes, I consider it bad.

To some degree I understand your concerns, but complex problems don't always mean that there has to be a complex solution. Unless you have a good reason for the complexity, please keep the driver as simple as possible. That not only reduces code size, it also reduces the probability to introduce bugs, and it reduces amount of time we have to spend reviewing the code.

Thanks,
Guenter

Quoted 36 lines

+ return ret;
+}
+
+static int stts751_probe(struct i2c_client *client,
+ const struct i2c_device_id *id)
+{
+ struct stts751_priv *priv;
+ int ret;
+ int groups_idx = 0;
+ struct device_node *of_node = client->dev.of_node;
+

np is the commonly accepted variable name.

Will follow the convention..

+ priv = devm_kzalloc(&client->dev,
+ sizeof(struct stts751_priv), GFP_KERNEL);

Could be rewrite to sizeof(*priv)

Regards

Labbe Corentin

#6 lore raw
Quoted 38 lines · Guenter Roeck
Quoted 26 lines · Andrea Merello

On Wed, Sep 7, 2016 at 4:07 PM, Guenter Roeck <[email protected] <mailto:[email protected]>> wrote:

On 09/07/2016 06:16 AM, LABBE Corentin wrote:

Hello

I have some coments below

Some more comments below.

On Wed, Sep 07, 2016 at 02:33:36PM +0200, Andrea Merello wrote:

This patch adds a HWMON driver for ST Microelectronics STTS751 temperature sensors.

It does support manual-triggered conversions as well as
automatic
conversions. The latter is used when the "event" or "therm"
function
is present (declaring the physical wire is attached in the
DT).

Quoted 10 lines

All others are penaltized and have to wait for manual mode results
when
reading the temperature ? Why ?

The reason was that with this HW reading a precise value when automatic conversion is running is a pain (as explained in the long comment at the file beginning).

Quoted 163 lines

As I said, other drivers / chips have the same problem of "non-atomic" reads across
multiple registers. Maybe there is something else different with this chip, but from
the explanation there is no indication what it might be.

BTW if we want to avoid waiting, I could try letting the converter running

Quoted 154 lines

in auto mode, and disable it just before reading, perform the read, and enable again (but IMHO only when we have no therm/event, for the reasons explained in the comment)..

Thanks-to: LABBE Corentin [for suggestions]

<mailto:[email protected]>>
Cc: LABBE Corentin <[email protected] <mailto:
[email protected]>>
Cc: Guenter Roeck <[email protected] <mailto:
[email protected]>>
Cc: Jean Delvare <[email protected] <mailto:[email protected]

Quoted 1 line
Quoted 1 line

---
drivers/hwmon/Kconfig | 12 +-
drivers/hwmon/Makefile | 2 +-
drivers/hwmon/stts751.c | 948 ++++++++++++++++++++++++++++++
++++++++++++++++++

             3 files changed, 960 insertions(+), 2 deletions(-)
             create mode 100644 drivers/hwmon/stts751.c

            diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
            index eaf2f91..8fdd241 100644
            --- a/drivers/hwmon/Kconfig
            +++ b/drivers/hwmon/Kconfig
            @@ -1448,6 +1448,16 @@ config SENSORS_SCH5636
                      This driver can also be built as a module.  If so,

the module
will be called sch5636.

+config SENSORS_STTS751
+ tristate "ST Microelectronics STTS751"
+ depends on I2C
+ help
+ If you say yes here you get support for STTS751
+ temperature sensor chips.
+
+ This driver can also be built as a module. If so,
the module

Two space instead of one

+ will be called stts751.
+
config SENSORS_SMM665
tristate "Summit Microelectronics SMM665" depends on I2C
@@ -1506,7 +1516,7 @@ config SENSORS_ADS7871

config SENSORS_AMC6821
tristate "Texas Instruments AMC6821"
- depends on I2C
+ depends on I2C

This change need to be in another set of patch

help
If you say yes here you get support for the Texas
Instruments

                      AMC6821 hardware monitoring chips.
            diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
            index fe87d28..1114130 100644
            --- a/drivers/hwmon/Makefile
            +++ b/drivers/hwmon/Makefile
            @@ -147,6 +147,7 @@ obj-$(CONFIG_SENSORS_SMM665)        +=
smm665.o
             obj-$(CONFIG_SENSORS_SMSC47B397)+= smsc47b397.o
             obj-$(CONFIG_SENSORS_SMSC47M1) += smsc47m1.o
             obj-$(CONFIG_SENSORS_SMSC47M192)+= smsc47m192.o
            +obj-$(CONFIG_SENSORS_STTS751)  += stts751.o
             obj-$(CONFIG_SENSORS_AMC6821)  += amc6821.o
             obj-$(CONFIG_SENSORS_TC74)     += tc74.o
             obj-$(CONFIG_SENSORS_THMC50)   += thmc50.o
            @@ -169,4 +170,3 @@ obj-$(CONFIG_SENSORS_WM8350)        +=
wm8350-hwmon.o
             obj-$(CONFIG_PMBUS)            += pmbus/

             ccflags-$(CONFIG_HWMON_DEBUG_CHIP) := -DDEBUG
            -
            diff --git a/drivers/hwmon/stts751.c b/drivers/hwmon/stts751.c
            new file mode 100644
            index 0000000..94b7e2b
            --- /dev/null
            +++ b/drivers/hwmon/stts751.c
            @@ -0,0 +1,948 @@
            +/*
            + * STTS751 sensor driver
            + *
            + * Copyright (C) 2016 Istituto Italiano di Tecnologia - RBCS

- EDL
+ * Robotics, Brain and Cognitive Sciences department
+ * Electronic Design Laboratory
+ *
+ * Written by Andrea Merello <[email protected]
<mailto:[email protected]>>
+ *
+ * Based on the following drivers:
+ * - LM95241 driver, which is:
+ * Copyright (C) 2008, 2010 Davide Rizzo <
[email protected] <mailto:[email protected]>>
+ * - LM90 driver, which is:
+ * Copyright (C) 2003-2010 Jean Delvare <[email protected]
<mailto:[email protected]>>

No need to recopy copyright, we can find them in their driver.

+
+/* HW index vs ASCII and int times in mS */ +static const struct stts751_intervals_t stts751_intervals[]
= {

            +       {.str = "16000", .val = 16000},
            +       {.str = "8000", .val = 8000},
            +       {.str = "4000", .val = 4000},
            +       {.str = "2000", .val = 2000},
            +       {.str = "1000", .val = 1000},
            +       {.str = "500", .val = 500},
            +       {.str = "250", .val = 250},
            +       {.str = "125", .val = 125},
            +       {.str = "62.5", .val = 62},
            +       {.str = "31.25", .val = 31}
            +};


        So you are lying about 62.5 and 31.25 :) ?


    I don't see what the table (or rather, the string -> integer

conversion) is used for anyway.
kstrtol() and sprintf() (or similar) seem to be doing a good job
there. Also, there
is an API function to find the closest matching index in a value
table.

Well, the table does not only relates strings to integer (that would be really natural doing with kstrtol() and friends), but it also relates the conversion rate with the HW index for that conversion rate (str vs table index). So, given that I would have an array anyway, keeping also the pre-converted values seemed reasonable to me

But I could store just the strings in the table, use kstrtol, and demote the table to be a single-dimensional array, if this is preferred.

I'll look at the API you are suggesting. I wasn't aware of it...

Try find_nearest().

Ok

Quoted 80 lines
Quoted 80 lines

+
+/* special value to indicate to the SW to use manual mode */ +#define STTS751_INTERVAL_MANUAL 0xFF
+
+struct stts751_priv {
+ struct device *dev;
+ struct i2c_client *client;
+ struct mutex access_lock;
+ unsigned long interval;
+ int res;
+ bool gen_therm, gen_event;
+ int event_max, event_min;
+ int therm;
+ int hyst;
+ bool smbus_timeout;
+ int temp;
+ unsigned long last_update;
+ u8 config;
+ bool min_alert, max_alert;
+ bool data_valid;
+
+ /* Temperature is always present
+ * Depending by DT/platdata, therm, event, interval
are

            +        * dynamically added.
            +        * There are max 4 entries plus the guard
            +        */

This type of comment is wrong, you need /* only at the first line

+ const struct attribute_group *groups[5];
+};
+
+static int stts751_manual_conversion(struct stts751_priv
*priv)
+{
+ s32 ret;
+ unsigned long timeout;
+
+ /* Any value written to this reg will trigger manual
conversion */
+ ret = i2c_smbus_write_byte_data(priv->client,
+ STTS751_REG_ONESHOT, 0xFF);
+ if (ret < 0)
+ return ret;
+
+ timeout = jiffies;
+
+ while (1) {
+ ret = i2c_smbus_read_byte_data(priv->client,
+ STTS751_REG_STATUS);
+ if (ret < 0)
+ return ret;
+ if (!(ret & STTS751_STATUS_BUSY))
+ return 0;
+ if (time_after(jiffies,
+ timeout +
STTS751_CONV_TIMEOUT * HZ / 1000)) {
+ dev_warn(&priv->client->dev,
"conversion timed out\n");
+ break;
+ }
+ }

Perhaps you could rewrite it as:
do {
...
} while (!time_after(xxx))
dev_warn()

It is unacceptable anyway, it being a hot loop, and I am not a friend of noisy warnings. I'd also like to see an explanation why manual mode has to be supported in the first place.

Quoted 10 lines
Quoted 7 lines

I see your point, but It's supposed not to happen unless there is a problem. And if there is, then I thought you might prefer to have extra noise rather than silence.. Anyway, I can either pull the warn off the loop (or rip it out completely) or try avoiding manual mode (see above for explanation).

Just keep in mind that I won't accept a hot loop (ie one that keeps

burning CPU without sleeping).

OK

Quoted 12 lines

The only reason for possibly accepting manual mode might be to reduce power consumption.
Since power consumption for continuous mode can be reduced to a max of 35uA, I don't
really see the point. Unless there is more explanation how this chip is different to
other chips in the same situation, I don't really accept the explanation for all the
complexity either.

OK, I'll try to make it always working in automatic mode.

Quoted 129 lines
Quoted 129 lines

+ return -ETIMEDOUT;
+}
+
+/* Converts temperature in C split in integer and fractional
parts, as supplied
+ * by the HW, to an integer number in mC
+ */

Same problem for comment (and all comentw below)

+static int stts751_update_temp(struct stts751_priv *priv) +{
+ s32 integer1, integer2, frac;
+ unsigned long sample1, sample2, timeout;
+ int i;
+ int ret = 0;
+
+ mutex_lock(&priv->access_lock);
+
+ if (priv->interval == STTS751_INTERVAL_MANUAL) {
+ /* perform a one-shot on-demand conversion */
+ ret = stts751_manual_conversion(priv);
+ if (ret) {
+ dev_warn(&priv->client->dev,
+ "failed to shot conversion
%x\n", ret);

If those are warnings, they are not errors and should not result in
an abort.
If they are errors, they should be displayed as errors. But, as
mentioned
before, I am not a friend of noisy drivers.

You're right. It's an error. I might change this is either an error, or given that you don't like it, in a dev_dbg..

+ goto exit;
+ }
+ }
+
+ for (i = 0; i < STTS751_RACE_RETRY; i++) {
+ sample1 = jiffies;
+ integer1 = i2c_smbus_read_byte_data(priv-

Quoted 1 line

client,

            +

STTS751_REG_TEMP_H);
+
+ if (integer1 < 0) {
+ ret = integer1;
+ dev_warn(&priv->client->dev,
+ "failed to read H reg %x\n",
ret);
+ goto exit;
+ }
+
+ frac = i2c_smbus_read_byte_data(priv->client,
+
STTS751_REG_TEMP_L);
+
+ if (frac < 0) {
+ ret = frac;
+ dev_warn(&priv->client->dev,
+ "failed to read L reg %x\n",
ret);
+ goto exit;
+ }
+
+ if (priv->interval ==
STTS751_INTERVAL_MANUAL) {

            +                       /* we'll look at integer2 later.. */
            +                       integer2 = integer1;
            +                       break;

+ }
+
+ integer2 = i2c_smbus_read_byte_data(priv-

Quoted 1 line

client,

            +

STTS751_REG_TEMP_H);
+ sample2 = jiffies;
+
+ if (integer2 < 0) {
+ dev_warn(&priv->client->dev,
+ "failed to read H reg (2nd
time) %x\n", ret);

Hard to find why you print ret and what exactly it is when you
print it.

Plus the check should be right after the assignment.

Just sample the time ASAP, that is "critical", then check if we have to throw the error.

+ ret = integer2;
+ goto exit;
+ }
+
+ timeout = stts751_intervals[priv->interval].val
* HZ / 1000;
+ timeout -= ((timeout < 10) && (timeout > 1))
? 1 : timeout / 10;

What it is the purpose of this decrease ?

+ if ((integer1 == integer2) &&
+ time_after(sample1 + timeout,
sample2))

And what is the purpose of this time_after() ? Other drivers have the same problem and a much simpler solution that doesn't require a
timeout
like this. If it is in fact needed, I would want to see a comment
explaining it.

Quoted 4 lines
Quoted 3 lines

Will look if I can find another driver with similar issue to snoop at..

Try lm90.c:lm90_read16().

I see.. This is reasonable, and I will implement this trick in stts751 driver.. This seems to have good chances to read correctly, but it can still go racy for example if the converter ends a conversion between the first two reads, and we get preempted in between the last two reads (so that the HW ends another conversion). IMHO looking _also_ at time, and eventually retry, makes things even more robust..

I'd say we could add time check and retry (with proper cpu_relax() to avoid those hot loops) also to lm90 rather than removing it from stts651 :)

Quoted 35 lines
Quoted 35 lines

+ break;
+
+ /* if we are going on with a racy read, don't
pretend to be
+ * super-precise, just use the MSBs ..
+ */
+ frac = 0;
+ }
+
+exit:
+ mutex_unlock(&priv->access_lock);
+ if (ret)
+ return ret;
+
+ /* use integer2, because when we fallback to the
"MSB-only" compromise
+ * this is the more recent one
+ */
+ priv->temp = stts751_to_deg(integer2, frac);

All other drivers I am aware of manage to handle conversions from 16
bit chip
values to temperatures and from temperatures to chip values with a
single parameter.

I'll check this.. BTW This chip wants two separated reads from two registers, one for the integer and one for the fractional part. The purpose of this function is interpreting these two raw HW values to convert in a number. Is this so bad ?

Quoted 6 lines
Quoted 1 line

Again, have a look at lm90.c:lm90_read16() and the conversion functions

in drivers which have to read two registers to read the temperature.

Is it bad ? It makes the code more complicated than it has to be and increases code size, so, yes, I consider it bad.

It does not look like that the simple register combination that lm90 does could also work for the integer/fractional format of the stts751.

I'll check again more carefully to see if for some math trick the two things eventually end up to produce the same result, but right now it seems to me that we need to do some math over the two read values in stts751. And the same for the reverse direction conversion.

If the point above turns out to be true, then I honestly can't see how dividing the two-registers read (with its trick to avoid races) and the math calculations in two functions, as it is now, could make things appearing so complicated. Honestly I would say the opposite :) .. However if it's better for you, I can merge the two things in one single function..

Quoted 8 lines

To some degree I understand your concerns, but complex problems don't always mean that there has to be a complex solution. Unless you have a good reason for the complexity, please keep the driver as simple as possible. That not only reduces code size, it also reduces the probability to introduce bugs, and it reduces amount of time we have to spend reviewing the code.

In general that's absolutely true :) But IMHO here we have neither complicated or simple solutions; we just have a certain degree of probability to do a (slightly) wrong read (in lm90 also), and we can put less or more effort (code) in trying to reduce this probability.

If you feel that what lm90 does is enough, and that is the "standard" way, I'll do the same.. Although I still would prefer to be more paranoid about races..

Quoted 3 lines

Thanks,
Guenter

BTW I will come back with a V2 as per your requirements, but unfortunately it will not happen very soon ;(

Thanks
Andrea

Quoted 41 lines
Quoted 40 lines

+ return ret;
+}
+
+static int stts751_probe(struct i2c_client *client,
+ const struct i2c_device_id *id)
+{
+ struct stts751_priv *priv;
+ int ret;
+ int groups_idx = 0;
+ struct device_node *of_node = client->dev.of_node;
+

np is the commonly accepted variable name.

Will follow the convention..

+ priv = devm_kzalloc(&client->dev,
+ sizeof(struct stts751_priv),
GFP_KERNEL);

Could be rewrite to sizeof(*priv)

Regards

Labbe Corentin

maintainer
#7 lore raw
Quoted 1 line · Andrea Merello

[ ... ]

Quoted 9 lines

Will look if I can find another driver with similar issue to snoop at..

Try lm90.c:lm90_read16().

I see.. This is reasonable, and I will implement this trick in stts751 driver.. This seems to have good chances to read correctly, but it can still go racy for example if the converter ends a conversion between the first two reads, and we get preempted in between the last two reads (so that the HW ends another conversion). IMHO looking _also_ at time, and eventually retry, makes things even more robust..

As a general comment, please use a mailer which splits lines at 80 columns or less.

While I understand your concern, the time check is complete overkill. What is the probability for a double bad reading, and on top of that one that includes a change in the high temperature register ? One in a Million ? With a downside of a wrong fraction in the "error" case ? And what does that really mean, on a chip with a specified accuracy of +/- 1 degrees C ?

Even your solution to drop the fraction in some cases doesn't really solve anything. It now reports, say, 30.0 degrees C instead of 30.875 degrees C. How is that better than a "wrong" 30.X degrees C, where X is any other value it might report ?

Quoted 2 lines

I'd say we could add time check and retry (with proper cpu_relax() to avoid those hot loops) also to lm90 rather than removing it from stts651 :)

Only if you can show that the problem is seen in the real world.

Quoted 29 lines

+ break;
+
+ /* if we are going on with a racy read, don't pretend to be
+ * super-precise, just use the MSBs ..
+ */
+ frac = 0;
+ }
+
+exit:
+ mutex_unlock(&priv->access_lock);
+ if (ret)
+ return ret;
+
+ /* use integer2, because when we fallback to the "MSB-only" compromise
+ * this is the more recent one
+ */
+ priv->temp = stts751_to_deg(integer2, frac);

All other drivers I am aware of manage to handle conversions from 16 bit chip values to temperatures and from temperatures to chip values with a single parameter.

I'll check this.. BTW This chip wants two separated reads from two registers, one for the integer and one for the fractional part. The purpose of this function is interpreting these two raw HW values to convert in a number. Is this so bad ?

Quoted 9 lines

Again, have a look at lm90.c:lm90_read16() and the conversion functions in drivers which have to read two registers to read the temperature.

Is it bad ? It makes the code more complicated than it has to be and increases code size, so, yes, I consider it bad.

It does not look like that the simple register combination that lm90 does could also work for the integer/fractional format of the stts751.

Why ? The register formats are exactly the same.

Quoted 4 lines

I'll check again more carefully to see if for some math trick the two things eventually end up to produce the same result, but right now it seems to me that we need to do some math over the two read values in stts751. And the same for the reverse direction conversion.

If the point above turns out to be true, then I honestly can't see how dividing the two-registers read (with its trick to avoid races) and the math calculations in two functions, as it is now, could make things appearing so complicated. Honestly I would say the opposite :) .. However if it's better for you, I can merge the two things in one single function..

Two vs. one function isn't the question here. The lm90 driver combines the two register values into a single variable. Using a single variable is what I asked for.

Quoted 10 lines

To some degree I understand your concerns, but complex problems don't always mean that there has to be a complex solution. Unless you have a good reason for the complexity, please keep the driver as simple as possible. That not only reduces code size, it also reduces the probability to introduce bugs, and it reduces amount of time we have to spend reviewing the code.

Quoted 4 lines

In general that's absolutely true :) But IMHO here we have neither complicated or simple solutions; we just have a certain degree of probability to do a (slightly) wrong read (in lm90 also), and we can put less or more effort (code) in trying to reduce this probability.

If you feel that what lm90 does is enough, and that is the "standard" way, I'll do the same.. Although I still would prefer to be more paranoid about races..

I would say it is sufficient. Many sensors have the same problem, and it would really be up to the chip vendors to define better (ie 16 bit) register sets. Until then, we have to find a trade-off between code complexity and avoiding races like this.

Quoted 3 lines

BTW I will come back with a V2 as per your requirements, but unfortunately it will not happen very soon ;(

No problem.

Guenter

#8 lore raw
Quoted 9 lines · Guenter Roeck
Quoted 2 lines · Andrea Merello

[ ... ]

Quoted 3 lines

Will look if I can find another driver with similar issue to snoop at..

Quoted 7 lines
Quoted 6 lines
    Try lm90.c:lm90_read16().

I see.. This is reasonable, and I will implement this trick in stts751 driver.. This seems to have good chances to read correctly, but it can still go racy for example if the converter ends a conversion between the first two reads, and we get preempted in between the last two reads (so that the HW ends another conversion). IMHO looking _also_ at time, and eventually retry, makes things even more robust..

As a general comment, please use a mailer which splits lines at 80 columns or less.

Thanks for pointing this out. (Probably I just need to remember to switch gmail in plain text mode..)

Quoted 7 lines

While I understand your concern, the time check is complete overkill. What is the probability for a double bad reading, and on top of that one that includes a change in the high temperature register ? One in a Million ? With a downside of a wrong fraction in the "error" case ? And what does that really mean, on a chip with a specified accuracy of +/- 1 degrees C ?

Yes, I see your point, and this is indeed true. Probably I have been too paranoid on this...

Quoted 3 lines

Even your solution to drop the fraction in some cases doesn't really solve anything. It now reports, say, 30.0 degrees C instead of 30.875 degrees C. How is that better than a "wrong" 30.X degrees C, where X is any other value it might report ?

You're right, I could let the "wrong" fraction data. (but the point of my solution was not this indeed).

Quoted 37 lines
Quoted 2 lines

I'd say we could add time check and retry (with proper cpu_relax() to avoid those hot loops) also to lm90 rather than removing it from stts651 :)

Only if you can show that the problem is seen in the real world.

Quoted 32 lines

+ break;
+
+ /* if we are going on with a racy read, don't pretend to be
+ * super-precise, just use the MSBs ..
+ */
+ frac = 0;
+ }
+
+exit:
+ mutex_unlock(&priv->access_lock);
+ if (ret)
+ return ret;
+
+ /* use integer2, because when we fallback to the "MSB-only" compromise
+ * this is the more recent one
+ */
+ priv->temp = stts751_to_deg(integer2, frac);

All other drivers I am aware of manage to handle conversions from 16 bit chip values to temperatures and from temperatures to chip values with a single parameter.

I'll check this.. BTW This chip wants two separated reads from two registers, one for the integer and one for the fractional part. The purpose of this function is interpreting these two raw HW values to convert in a number. Is this so bad ?

Again, have a look at lm90.c:lm90_read16() and the conversion functions in drivers which have to read two registers to read the temperature.

Quoted 7 lines
Quoted 6 lines

Is it bad ? It makes the code more complicated than it has to be and increases code size, so, yes, I consider it bad.

It does not look like that the simple register combination that lm90 does could also work for the integer/fractional format of the stts751.

Why ? The register formats are exactly the same.

Aah, I didn't saw temp_from_s16() in lm90.c that does the conversion afterwards, thus I assumed the data read from lm90 didn't need any conversion and that the reg format was different.. sorry.. Now I got what you meant :)

Quoted 6 lines
Quoted 4 lines

I'll check again more carefully to see if for some math trick the two things eventually end up to produce the same result, but right now it seems to me that we need to do some math over the two read values in stts751. And the same for the reverse direction conversion.

If the point above turns out to be true, then I honestly can't see how dividing the two-registers read (with its trick to avoid races) and the math calculations in two functions, as it is now, could make things appearing so complicated. Honestly I would say the opposite :) .. However if it's better for you, I can merge the two things in one single function..

Two vs. one function isn't the question here. The lm90 driver combines the two register values into a single variable. Using a single variable is what I asked for.

OK, I'll do the lm90 way :)

Quoted 10 lines
Quoted 10 lines

To some degree I understand your concerns, but complex problems don't always mean that there has to be a complex solution. Unless you have a good reason for the complexity, please keep the driver as simple as possible. That not only reduces code size, it also reduces the probability to introduce bugs, and it reduces amount of time we have to spend reviewing the code.

Quoted 8 lines
Quoted 4 lines

In general that's absolutely true :) But IMHO here we have neither complicated or simple solutions; we just have a certain degree of probability to do a (slightly) wrong read (in lm90 also), and we can put less or more effort (code) in trying to reduce this probability.

If you feel that what lm90 does is enough, and that is the "standard" way, I'll do the same.. Although I still would prefer to be more paranoid about races..

I would say it is sufficient. Many sensors have the same problem, and it would really be up to the chip vendors to define better (ie 16 bit) register sets. Until then, we have to find a trade-off between code complexity and avoiding races like this.

I see..

Thank you very much for discussion and explanations :)

Andrea

Quoted 7 lines
Quoted 3 lines

BTW I will come back with a V2 as per your requirements, but unfortunately it will not happen very soon ;(

No problem.

Guenter

#9 lore raw
In reply to LABBE Corentin
Quoted 33 lines · LABBE Corentin

Hello

I have some coments below

Quoted 8 lines · Andrea Merello

This patch adds a HWMON driver for ST Microelectronics STTS751 temperature sensors.

It does support manual-triggered conversions as well as automatic conversions. The latter is used when the "event" or "therm" function is present (declaring the physical wire is attached in the DT).

Thanks-to: LABBE Corentin [for suggestions]

Quoted 8 lines
Cc LABBE Corentin <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Jean Delvare <[email protected]>
---
 drivers/hwmon/Kconfig   |  12 +-
 drivers/hwmon/Makefile  |   2 +-
 drivers/hwmon/stts751.c | 948 ++++++++++++++++++++++++++++++

++++++++++++++++++

Quoted 11 lines
 3 files changed, 960 insertions(+), 2 deletions(-)
 create mode 100644 drivers/hwmon/stts751.c
drivers/hwmon/Kconfig +0 -0
index eaf2f91..8fdd241 100644
@@ -1448,6 +1448,16 @@ config SENSORS_SCH5636
        This driver can also be built as a module.  If so, the module
        will be called sch5636.
 
Quoted 11 lines

drivers/hwmon/Kconfig config SENSORS_SCH5636 · line 1458

Quoted 8 lines
drivers/hwmon/Kconfig +8 -0
+config SENSORS_STTS751
+     tristate "ST Microelectronics STTS751"
+     depends on I2C
+     help
+       If you say yes here you get support for STTS751
+       temperature sensor chips.
+
+       This driver can also be built as a module.  If so, the module

Two space instead of one

OK

Quoted 8 lines
Quoted 8 lines
drivers/hwmon/Kconfig +2 -0
+       will be called stts751.
+
 config SENSORS_SMM665
      tristate "Summit Microelectronics SMM665"
      depends on I2C
@@ -1506,7 +1516,7 @@ config SENSORS_ADS7871
 
Quoted 7 lines

drivers/hwmon/Kconfig config SENSORS_ADS7871 · line 1519

Quoted 4 lines
drivers/hwmon/Kconfig +1 -1
 config SENSORS_AMC6821
      tristate "Texas Instruments AMC6821"
-     depends on I2C
+     depends on I2C

This change need to be in another set of patch

OK

Quoted 35 lines
Quoted 35 lines
drivers/hwmon/Kconfig +0 -0
      help
        If you say yes here you get support for the Texas Instruments
        AMC6821 hardware monitoring chips.
drivers/hwmon/Makefile +1 -1
index fe87d28..1114130 100644
@@ -147,6 +147,7 @@ obj-$(CONFIG_SENSORS_SMM665)      += smm665.o
 obj-$(CONFIG_SENSORS_SMSC47B397)+= smsc47b397.o
 obj-$(CONFIG_SENSORS_SMSC47M1)       += smsc47m1.o
 obj-$(CONFIG_SENSORS_SMSC47M192)+= smsc47m192.o
+obj-$(CONFIG_SENSORS_STTS751)        += stts751.o
 obj-$(CONFIG_SENSORS_AMC6821)        += amc6821.o
 obj-$(CONFIG_SENSORS_TC74)   += tc74.o
 obj-$(CONFIG_SENSORS_THMC50) += thmc50.o
@@ -169,4 +170,3 @@ obj-$(CONFIG_SENSORS_WM8350)      += wm8350-hwmon.o
 obj-$(CONFIG_PMBUS)          += pmbus/
 
 ccflags-$(CONFIG_HWMON_DEBUG_CHIP) := -DDEBUG
-
drivers/hwmon/stts751.c new file +8 -0
new file mode 100644
index 0000000..94b7e2b
@@ -0,0 +1,948 @@
+/*
+ * STTS751 sensor driver
+ *
+ * Copyright (C) 2016 Istituto Italiano di Tecnologia - RBCS - EDL
+ * Robotics, Brain and Cognitive Sciences department
+ * Electronic Design Laboratory
+ *
+ * Written by Andrea Merello <[email protected]>
Quoted 9 lines

drivers/hwmon/stts751.c line 14

Quoted 6 lines
drivers/hwmon/stts751.c new file +6 -0
+ *
+ * Based on the following drivers:
+ * - LM95241 driver, which is:
+ *   Copyright (C) 2008, 2010 Davide Rizzo <[email protected]>
+ * - LM90 driver, which is:
+ *   Copyright (C) 2003-2010  Jean Delvare <[email protected]>

No need to recopy copyright, we can find them in their driver.

OK

Quoted 18 lines
Quoted 5 lines
drivers/hwmon/stts751.c new file +4 -0
+
+/* HW index vs ASCII and int times in mS */
+static const struct stts751_intervals_t stts751_intervals[] = {
+     {.str = "16000", .val = 16000},

drivers/hwmon/stts751.c line 147

Quoted 10 lines
drivers/hwmon/stts751.c new file +10 -0
+     {.str = "8000", .val = 8000},
+     {.str = "4000", .val = 4000},
+     {.str = "2000", .val = 2000},
+     {.str = "1000", .val = 1000},
+     {.str = "500", .val = 500},
+     {.str = "250", .val = 250},
+     {.str = "125", .val = 125},
+     {.str = "62.5", .val = 62},
+     {.str = "31.25", .val = 31}
+};

So you are lying about 62.5 and 31.25 :) ?

No, this is not my intention :)
The .val data is used only to find out reasonable (rounded) values for things like timeout and cache time, as well as finding the closest walue the HW can do. The user will see the (correct) string and the HW will use the right value..

Quoted 21 lines
Quoted 21 lines
drivers/hwmon/stts751.c new file +20 -0
+
+/* special value to indicate to the SW to use manual mode */
+#define STTS751_INTERVAL_MANUAL 0xFF
+
+struct stts751_priv {
+     struct device *dev;
+     struct i2c_client *client;
+     struct mutex access_lock;
+     unsigned long interval;
+     int res;
+     bool gen_therm, gen_event;
+     int event_max, event_min;
+     int therm;
+     int hyst;
+     bool smbus_timeout;
+     int temp;
+     unsigned long last_update;
+     u8 config;
+     bool min_alert, max_alert;
+     bool data_valid;
Quoted 9 lines

drivers/hwmon/stts751.c line 173

Quoted 6 lines
drivers/hwmon/stts751.c new file +6 -0
+
+     /* Temperature is always present
+      * Depending by DT/platdata, therm, event, interval are
+      * dynamically added.
+      * There are max 4 entries plus the guard
+      */

This type of comment is wrong, you need /* only at the first line

OK

Quoted 33 lines
Quoted 16 lines
drivers/hwmon/stts751.c new file +15 -0
+     const struct attribute_group *groups[5];
+};
+
+static int stts751_manual_conversion(struct stts751_priv *priv)
+{
+     s32 ret;
+     unsigned long timeout;
+
+     /* Any value written to this reg will trigger manual conversion */
+     ret = i2c_smbus_write_byte_data(priv->client,
+                             STTS751_REG_ONESHOT, 0xFF);
+     if (ret < 0)
+             return ret;
+
+     timeout = jiffies;

drivers/hwmon/stts751.c

Quoted 10 lines
drivers/hwmon/stts751.c new file +10 -0
+
+     while (1) {
+             ret = i2c_smbus_read_byte_data(priv->client,
+                                     STTS751_REG_STATUS);
+             if (ret < 0)
+                     return ret;
+             if (!(ret & STTS751_STATUS_BUSY))
+                     return 0;
+             if (time_after(jiffies,
+                             timeout + STTS751_CONV_TIMEOUT * HZ /

1000)) {

drivers/hwmon/stts751.c line 80

Quoted 1 line
drivers/hwmon/stts751.c new file +1 -0
+                     dev_warn(&priv->client->dev, "conversion timed

out\n");

drivers/hwmon/stts751.c line 202

Quoted 3 lines
drivers/hwmon/stts751.c new file +3 -0
+                     break;
+             }
+     }
Quoted 6 lines

Perhaps you could rewrite it as:
do {
...
} while (!time_after(xxx))
dev_warn()

OK

Quoted 11 lines

drivers/hwmon/stts751.c

Quoted 5 lines
drivers/hwmon/stts751.c new file +4 -0
+     return -ETIMEDOUT;
+}
+
+/* Converts temperature in C split in integer and fractional parts, as

supplied

drivers/hwmon/stts751.c line 89

Quoted 2 lines
drivers/hwmon/stts751.c new file +2 -0
+ * by the HW, to an integer number in mC
+ */

Same problem for comment (and all comentw below)

OK

Quoted 67 lines
Quoted 45 lines
drivers/hwmon/stts751.c new file +44 -0
+static int stts751_update_temp(struct stts751_priv *priv)
+{
+     s32 integer1, integer2, frac;
+     unsigned long sample1, sample2, timeout;
+     int i;
+     int ret = 0;
+
+     mutex_lock(&priv->access_lock);
+
+     if (priv->interval == STTS751_INTERVAL_MANUAL) {
+             /* perform a one-shot on-demand conversion */
+             ret = stts751_manual_conversion(priv);
+             if (ret) {
+                     dev_warn(&priv->client->dev,
+                             "failed to shot conversion %x\n", ret);
+                     goto exit;
+             }
+     }
+
+     for (i = 0; i < STTS751_RACE_RETRY; i++) {
+             sample1 = jiffies;
+             integer1 = i2c_smbus_read_byte_data(priv->client,
+                                             STTS751_REG_TEMP_H);
+
+             if (integer1 < 0) {
+                     ret = integer1;
+                     dev_warn(&priv->client->dev,
+                             "failed to read H reg %x\n", ret);
+                     goto exit;
+             }
+
+             frac = i2c_smbus_read_byte_data(priv->client,
+                                             STTS751_REG_TEMP_L);
+
+             if (frac < 0) {
+                     ret = frac;
+                     dev_warn(&priv->client->dev,
+                             "failed to read L reg %x\n", ret);
+                     goto exit;
+             }
+
+             if (priv->interval == STTS751_INTERVAL_MANUAL) {
+                     /* we'll look at integer2 later.. */
+                     integer2 = integer1;

drivers/hwmon/stts751.c

Quoted 10 lines
drivers/hwmon/stts751.c new file +10 -0
+                     break;
+             }
+
+             integer2 = i2c_smbus_read_byte_data(priv->client,
+                                             STTS751_REG_TEMP_H);
+             sample2 = jiffies;
+
+             if (integer2 < 0) {
+                     dev_warn(&priv->client->dev,
+                             "failed to read H reg (2nd time) %x\n",

ret);

Hard to find why you print ret and what exactly it is when you print it.

OK, I will change this someway

drivers/hwmon/stts751.c

Quoted 5 lines
drivers/hwmon/stts751.c new file +5 -0
+                     ret = integer2;
+                     goto exit;
+             }
+
+             timeout = stts751_intervals[priv->interval].val * HZ /
Quoted 6 lines

1000;

drivers/hwmon/stts751.c line 149

Quoted 1 line
drivers/hwmon/stts751.c new file +1 -0
+             timeout -= ((timeout < 10) && (timeout > 1)) ? 1 : timeout

/ 10;

What it is the purpose of this decrease ?

Now that I'm looking at it again, I admit I'm not completely convinced of it me too. BTW it was meant for being conservative, checking for a time interval slightly less than the real conversion rate.

Anyway I will add a comment and eventually rewrite/fix it.

Quoted 28 lines
Quoted 7 lines
drivers/hwmon/stts751.c new file +6 -0
+             if ((integer1 == integer2) &&
+                     time_after(sample1 + timeout, sample2))
+                     break;
+
+             /* if we are going on with a racy read, don't pretend to be
+              * super-precise, just use the MSBs ..

drivers/hwmon/stts751.c

Quoted 10 lines
drivers/hwmon/stts751.c new file +10 -0
+              */
+             frac = 0;
+     }
+
+exit:
+     mutex_unlock(&priv->access_lock);
+     if (ret)
+             return ret;
+
+     /* use integer2, because when we fallback to the "MSB-only"

compromise

Quoted 10 lines
drivers/hwmon/stts751.c new file +10 -0
+      * this is the more recent one
+      */
+     priv->temp = stts751_to_deg(integer2, frac);
+     return ret;
+}
+
+static int stts751_probe(struct i2c_client *client,
+                      const struct i2c_device_id *id)
+{
+     struct stts751_priv *priv;
Quoted 9 lines

drivers/hwmon/stts751.c line 865

Quoted 6 lines
drivers/hwmon/stts751.c new file +6 -0
+     int ret;
+     int groups_idx = 0;
+     struct device_node *of_node = client->dev.of_node;
+
+     priv = devm_kzalloc(&client->dev,
+                     sizeof(struct stts751_priv), GFP_KERNEL);

Could be rewrite to sizeof(*priv)

OK

Quoted 6 lines

Regards

Labbe Corentin