All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] hwmon: sht3x: read out sensor serial number
@ 2023-11-24 14:55 Stefan Gloor
  2023-11-24 17:34 ` Guenter Roeck
  0 siblings, 1 reply; 4+ messages in thread
From: Stefan Gloor @ 2023-11-24 14:55 UTC (permalink / raw)
  To: jdelvare, linux, corbet, linux-hwmon, linux-doc, linux-kernel
  Cc: Stefan Gloor

Some of the temperature/humidity sensors of the STS3x/SHT3x family are
calibrated and factory-programmed with a unique serial number. This serial
number can be used to obtain a calibration certificate via an API provided
by the manufacturer (Sensirion). The serial number is exposed as a
non-standard sysfs attribute.

This feature is only available for STS32, STS33 and SHT33. The capability
to read out the serial number is not documented for other sensors such as
the STS31, but it is implemented in the ones I tested. To be safe, the
driver will silently set the serial number to zero if it can not be read.

Tested with:
1x STS32: serial number present
2x STS31: serial number present (feature not documented)
1x SHT31: serial number present (feature not documented)

Signed-off-by: Stefan Gloor <code@stefan-gloor.ch>
---
 Documentation/hwmon/sht3x.rst |  2 ++
 drivers/hwmon/sht3x.c         | 39 +++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/Documentation/hwmon/sht3x.rst b/Documentation/hwmon/sht3x.rst
index 87864ffd1777..1727994f4a4a 100644
--- a/Documentation/hwmon/sht3x.rst
+++ b/Documentation/hwmon/sht3x.rst
@@ -85,4 +85,6 @@ repeatability:      write or read repeatability, higher repeatability means
                         - 0: low repeatability
                         - 1: medium repeatability
                         - 2: high repeatability
+serial_number:      unique serial number of the sensor in decimal, 0 if
+                    unavailable
 =================== ============================================================
diff --git a/drivers/hwmon/sht3x.c b/drivers/hwmon/sht3x.c
index 79657910b79e..e4d436af1097 100644
--- a/drivers/hwmon/sht3x.c
+++ b/drivers/hwmon/sht3x.c
@@ -41,6 +41,7 @@ static const unsigned char sht3x_cmd_heater_off[]              = { 0x30, 0x66 };
 /* other commands */
 static const unsigned char sht3x_cmd_read_status_reg[]         = { 0xf3, 0x2d };
 static const unsigned char sht3x_cmd_clear_status_reg[]        = { 0x30, 0x41 };
+static const unsigned char sht3x_cmd_read_serial_number[]      = { 0x37, 0x80 };
 
 /* delays for single-shot mode i2c commands, both in us */
 #define SHT3X_SINGLE_WAIT_TIME_HPM  15000
@@ -169,6 +170,7 @@ struct sht3x_data {
 	u32 wait_time;			/* in us*/
 	unsigned long last_update;	/* last update in periodic mode*/
 	enum sht3x_repeatability repeatability;
+	u32 serial_number;
 
 	/*
 	 * cached values for temperature and humidity and limits
@@ -606,6 +608,33 @@ static int update_interval_write(struct device *dev, int val)
 	return 0;
 }
 
+static int serial_number_read(struct sht3x_data *data)
+{
+	int ret;
+	char buffer[SHT3X_RESPONSE_LENGTH];
+	struct i2c_client *client = data->client;
+
+	ret = sht3x_read_from_command(client, data,
+				      sht3x_cmd_read_serial_number,
+				      buffer,
+				      SHT3X_RESPONSE_LENGTH, 0);
+	if (ret)
+		return ret;
+
+	data->serial_number = (buffer[0] << 24) | (buffer[1] << 16) |
+			      (buffer[3] << 8) | buffer[4];
+	return ret;
+}
+
+static ssize_t serial_number_show(struct device *dev,
+				  struct device_attribute *attr,
+				  char *buf)
+{
+	struct sht3x_data *data = dev_get_drvdata(dev);
+
+	return sysfs_emit(buf, "%d\n", data->serial_number);
+}
+
 static ssize_t repeatability_show(struct device *dev,
 				  struct device_attribute *attr,
 				  char *buf)
@@ -639,10 +668,12 @@ static ssize_t repeatability_store(struct device *dev,
 
 static SENSOR_DEVICE_ATTR_RW(heater_enable, heater_enable, 0);
 static SENSOR_DEVICE_ATTR_RW(repeatability, repeatability, 0);
+static SENSOR_DEVICE_ATTR_RO(serial_number, serial_number, 0);
 
 static struct attribute *sht3x_attrs[] = {
 	&sensor_dev_attr_heater_enable.dev_attr.attr,
 	&sensor_dev_attr_repeatability.dev_attr.attr,
+	&sensor_dev_attr_serial_number.dev_attr.attr,
 	NULL
 };
 
@@ -899,6 +930,14 @@ static int sht3x_probe(struct i2c_client *client)
 	if (ret)
 		return ret;
 
+	/*
+	 * Serial number readout is not documented for the whole
+	 * STS3x/SHT3x series, so we don't return on error here.
+	 */
+	ret = serial_number_read(data);
+	if (ret)
+		data->serial_number = 0;
+
 	hwmon_dev = devm_hwmon_device_register_with_info(dev,
 							 client->name,
 							 data,
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] hwmon: sht3x: read out sensor serial number
  2023-11-24 14:55 [PATCH] hwmon: sht3x: read out sensor serial number Stefan Gloor
@ 2023-11-24 17:34 ` Guenter Roeck
  2023-11-24 22:09   ` Stefan Gloor
  0 siblings, 1 reply; 4+ messages in thread
From: Guenter Roeck @ 2023-11-24 17:34 UTC (permalink / raw)
  To: Stefan Gloor, jdelvare, corbet, linux-hwmon, linux-doc, linux-kernel

On 11/24/23 06:55, Stefan Gloor wrote:
> Some of the temperature/humidity sensors of the STS3x/SHT3x family are
> calibrated and factory-programmed with a unique serial number. This serial
> number can be used to obtain a calibration certificate via an API provided
> by the manufacturer (Sensirion). The serial number is exposed as a
> non-standard sysfs attribute.
> 
> This feature is only available for STS32, STS33 and SHT33. The capability
> to read out the serial number is not documented for other sensors such as
> the STS31, but it is implemented in the ones I tested. To be safe, the
> driver will silently set the serial number to zero if it can not be read.
> 
> Tested with:
> 1x STS32: serial number present
> 2x STS31: serial number present (feature not documented)
> 1x SHT31: serial number present (feature not documented)
> 
> Signed-off-by: Stefan Gloor <code@stefan-gloor.ch>

I am not going to accept this as sysfs attribute. Please implement
using debugfs.


Also, the attribute (sysfs or debugfs) should not exist if not supported.
Please only provide if supported.

Thanks,
Guenter


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] hwmon: sht3x: read out sensor serial number
  2023-11-24 17:34 ` Guenter Roeck
@ 2023-11-24 22:09   ` Stefan Gloor
  2023-11-25 23:59     ` Guenter Roeck
  0 siblings, 1 reply; 4+ messages in thread
From: Stefan Gloor @ 2023-11-24 22:09 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: jdelvare, corbet, linux-hwmon, linux-doc, linux-kernel

Hi Guenter,

thank you for your comments.

On Fri, Nov 24, 2023 at 09:34:48AM -0800, Guenter Roeck wrote:
> I am not going to accept this as sysfs attribute. Please implement
> using debugfs.
> 
I will do this in V2.

> 
> Also, the attribute (sysfs or debugfs) should not exist if not supported.
> Please only provide if supported.
The driver is currently only compatible with "sht3x" and "sts3x". 
As only a subset of these support this feature, do you recommend I
create new additional device IDs, i.e., sts32, sts33 and sht33?

Best,
Stefan
-- 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] hwmon: sht3x: read out sensor serial number
  2023-11-24 22:09   ` Stefan Gloor
@ 2023-11-25 23:59     ` Guenter Roeck
  0 siblings, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2023-11-25 23:59 UTC (permalink / raw)
  To: Stefan Gloor; +Cc: jdelvare, corbet, linux-hwmon, linux-doc, linux-kernel

On Fri, Nov 24, 2023 at 11:09:31PM +0100, Stefan Gloor wrote:
> Hi Guenter,
> 
> thank you for your comments.
> 
> On Fri, Nov 24, 2023 at 09:34:48AM -0800, Guenter Roeck wrote:
> > I am not going to accept this as sysfs attribute. Please implement
> > using debugfs.
> > 
> I will do this in V2.
> 
> > 
> > Also, the attribute (sysfs or debugfs) should not exist if not supported.
> > Please only provide if supported.
> The driver is currently only compatible with "sht3x" and "sts3x". 
> As only a subset of these support this feature, do you recommend I
> create new additional device IDs, i.e., sts32, sts33 and sht33?
> 

If they have different functionality, they need different IDs.

Guenter

> Best,
> Stefan
> -- 
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-11-25 23:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-24 14:55 [PATCH] hwmon: sht3x: read out sensor serial number Stefan Gloor
2023-11-24 17:34 ` Guenter Roeck
2023-11-24 22:09   ` Stefan Gloor
2023-11-25 23:59     ` Guenter Roeck

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.