Linux IIO development
 help / color / mirror / Atom feed
From: Donggeun Yoo <donggeunyoo.kernel@gmail.com>
To: Jonathan Cameron <jic23@kernel.org>,
	Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>
Cc: "David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	"Donggeun Yoo" <donggeunyoo.kernel@gmail.com>
Subject: [PATCH 2/2] iio: proximity: Add driver for ST STHS34PF80
Date: Thu,  3 Dec 2026 09:50:55 +0900	[thread overview]
Message-ID: <20261203005055.26666-3-donggeunyoo.kernel@gmail.com> (raw)
In-Reply-To: <20261203005055.26666-1-donggeunyoo.kernel@gmail.com>

Add a driver for the STMicroelectronics STHS34PF80, an infrared sensor
for presence and motion detection, connected over I2C.

Each read triggers a one-shot measurement. The driver exposes the
ambient temperature and the object temperature. The object temperature
is not linear in the raw readings. The driver computes it from the raw
object and ambient readings and the sensitivity stored in the device,
as described in AN5867, and reports it as a processed value.

Presence and motion detection, the interrupt, buffered capture and SPI
are not supported yet.

Datasheet: https://www.st.com/resource/en/datasheet/sths34pf80.pdf
Assisted-by: LLM
Signed-off-by: Donggeun Yoo <donggeunyoo.kernel@gmail.com>
---
 MAINTAINERS                        |   1 +
 drivers/iio/proximity/Kconfig      |  11 +
 drivers/iio/proximity/Makefile     |   1 +
 drivers/iio/proximity/sths34pf80.c | 369 +++++++++++++++++++++++++++++
 4 files changed, 382 insertions(+)
 create mode 100644 drivers/iio/proximity/sths34pf80.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 09318d3f9670e..a40638a609975 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -26599,6 +26599,7 @@ M:	Donggeun Yoo <donggeunyoo.kernel@gmail.com>
 L:	linux-iio@vger.kernel.org
 S:	Maintained
 F:	Documentation/devicetree/bindings/iio/proximity/st,sths34pf80.yaml
+F:	drivers/iio/proximity/sths34pf80.c
 
 ST STM32 FIREWALL
 M:	Gatien Chevallier <gatien.chevallier@foss.st.com>
diff --git a/drivers/iio/proximity/Kconfig b/drivers/iio/proximity/Kconfig
index d16458ea4b0fd..5fb7db5d20d52 100644
--- a/drivers/iio/proximity/Kconfig
+++ b/drivers/iio/proximity/Kconfig
@@ -149,6 +149,17 @@ config SRF04
 	  To compile this driver as a module, choose M here: the
 	  module will be called srf04.
 
+config STHS34PF80
+	tristate "STMicroelectronics STHS34PF80 infrared sensor"
+	depends on I2C
+	select REGMAP_I2C
+	help
+	  Say Y here to build a driver for the STMicroelectronics STHS34PF80
+	  infrared presence and motion sensor.
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called sths34pf80.
+
 config SX_COMMON
 	tristate
 	help
diff --git a/drivers/iio/proximity/Makefile b/drivers/iio/proximity/Makefile
index 43741115a9218..a1a5e89f52f50 100644
--- a/drivers/iio/proximity/Makefile
+++ b/drivers/iio/proximity/Makefile
@@ -16,6 +16,7 @@ obj-$(CONFIG_PING)		+= ping.o
 obj-$(CONFIG_RFD77402)		+= rfd77402.o
 obj-$(CONFIG_SRF04)		+= srf04.o
 obj-$(CONFIG_SRF08)		+= srf08.o
+obj-$(CONFIG_STHS34PF80)	+= sths34pf80.o
 obj-$(CONFIG_SX9310)		+= sx9310.o
 obj-$(CONFIG_SX9324)		+= sx9324.o
 obj-$(CONFIG_SX9360)		+= sx9360.o
diff --git a/drivers/iio/proximity/sths34pf80.c b/drivers/iio/proximity/sths34pf80.c
new file mode 100644
index 0000000000000..a32e0c920a0c9
--- /dev/null
+++ b/drivers/iio/proximity/sths34pf80.c
@@ -0,0 +1,369 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * STMicroelectronics STHS34PF80 presence and motion sensor driver
+ *
+ * Copyright (c) 2026 Donggeun Yoo <donggeunyoo.kernel@gmail.com>
+ *
+ * Datasheet: https://www.st.com/resource/en/datasheet/sths34pf80.pdf
+ * Application note: https://www.st.com/resource/en/application_note/an5867-sths34pf80-lowpower-highsensitivity-infrared-ir-sensor-for-presence-and-motion-detection-stmicroelectronics.pdf
+ */
+
+#include <linux/array_size.h>
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/cleanup.h>
+#include <linux/delay.h>
+#include <linux/dev_printk.h>
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/math.h>
+#include <linux/math64.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/overflow.h>
+#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
+#include <linux/time.h>
+#include <linux/types.h>
+#include <linux/units.h>
+
+#include <asm/byteorder.h>
+
+#include <linux/iio/iio.h>
+
+#define STHS34PF80_REG_WHO_AM_I		0x0f
+#define STHS34PF80_WHO_AM_I_VAL		0xd3
+
+#define STHS34PF80_REG_SENS_DATA	0x1d
+
+#define STHS34PF80_REG_CTRL1		0x20
+#define STHS34PF80_CTRL1_BDU		BIT(4)
+#define STHS34PF80_CTRL1_ODR		GENMASK(3, 0)
+#define STHS34PF80_CTRL1_ODR_POWER_DOWN	0
+
+#define STHS34PF80_REG_CTRL2		0x21
+#define STHS34PF80_CTRL2_BOOT		BIT(7)
+#define STHS34PF80_CTRL2_ONE_SHOT	BIT(0)
+
+#define STHS34PF80_REG_STATUS		0x23
+#define STHS34PF80_REG_TOBJECT_L	0x26
+#define STHS34PF80_REG_TAMBIENT_L	0x28
+#define STHS34PF80_REG_TAMB_SHOCK_H	0x3f
+
+/* The boot procedure takes 2.5ms (AN5867 section 5) */
+#define STHS34PF80_BOOT_TIME_US		2500
+
+/* One-shot conversion takes 5.5ms to 252ms (AN5867 Table 6) */
+#define STHS34PF80_ONE_SHOT_POLL_US	(2 * USEC_PER_MSEC)
+#define STHS34PF80_ONE_SHOT_TIMEOUT_US	(300 * USEC_PER_MSEC)
+
+/* SENS_DATA to sensitivity in LSB/degC (AN5867 4.5.1) */
+#define STHS34PF80_SENS_STEP		16
+#define STHS34PF80_SENS_BASE		2048
+
+#define STHS34PF80_TAMBIENT_LSB_PER_DEG	100
+
+/* 0 degC and the sensitivity calibration point, 24 degC (AN5867 4.5.1) */
+#define STHS34PF80_ZERO_CELSIUS_CK	(-ABSOLUTE_ZERO_MILLICELSIUS * \
+					 STHS34PF80_TAMBIENT_LSB_PER_DEG / \
+					 MILLIDEGREE_PER_DEGREE)
+#define STHS34PF80_SENS_CAL_TEMP_CK	(STHS34PF80_ZERO_CELSIUS_CK + \
+					 24 * STHS34PF80_TAMBIENT_LSB_PER_DEG)
+
+struct sths34pf80_data {
+	struct regmap *regmap;
+	/* Serializes access to the device */
+	struct mutex lock;
+	unsigned int sensitivity;
+};
+
+static bool sths34pf80_volatile_reg(struct device *dev, unsigned int reg)
+{
+	switch (reg) {
+	case STHS34PF80_REG_CTRL2:
+	case STHS34PF80_REG_STATUS ... STHS34PF80_REG_TAMB_SHOCK_H:
+		return true;
+	default:
+		return false;
+	}
+}
+
+static bool sths34pf80_writeable_reg(struct device *dev, unsigned int reg)
+{
+	switch (reg) {
+	case STHS34PF80_REG_CTRL1:
+	case STHS34PF80_REG_CTRL2:
+		return true;
+	default:
+		return false;
+	}
+}
+
+static const struct regmap_config sths34pf80_regmap_config = {
+	.reg_bits = 8,
+	.val_bits = 8,
+	.max_register = STHS34PF80_REG_TAMB_SHOCK_H,
+	.writeable_reg = sths34pf80_writeable_reg,
+	.volatile_reg = sths34pf80_volatile_reg,
+	.cache_type = REGCACHE_MAPLE,
+};
+
+static const struct iio_chan_spec sths34pf80_channels[] = {
+	{
+		.type = IIO_TEMP,
+		.modified = 1,
+		.channel2 = IIO_MOD_TEMP_AMBIENT,
+		.address = STHS34PF80_REG_TAMBIENT_L,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				      BIT(IIO_CHAN_INFO_SCALE),
+	},
+	{
+		.type = IIO_TEMP,
+		.modified = 1,
+		.channel2 = IIO_MOD_TEMP_OBJECT,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+	},
+};
+
+static int sths34pf80_one_shot(struct sths34pf80_data *data)
+{
+	struct regmap *map = data->regmap;
+	unsigned int ctrl2;
+	int ret;
+
+	ret = regmap_set_bits(map, STHS34PF80_REG_CTRL2,
+			      STHS34PF80_CTRL2_ONE_SHOT);
+	if (ret)
+		return ret;
+
+	return regmap_read_poll_timeout(map, STHS34PF80_REG_CTRL2, ctrl2,
+					!(ctrl2 & STHS34PF80_CTRL2_ONE_SHOT),
+					STHS34PF80_ONE_SHOT_POLL_US,
+					STHS34PF80_ONE_SHOT_TIMEOUT_US);
+}
+
+static int sths34pf80_read_s16(struct sths34pf80_data *data, unsigned int reg,
+			       int *val)
+{
+	struct regmap *map = data->regmap;
+	__le16 raw;
+	int ret;
+
+	ret = regmap_bulk_read(map, reg, &raw, sizeof(raw));
+	if (ret)
+		return ret;
+
+	*val = (s16)le16_to_cpu(raw);
+
+	return 0;
+}
+
+static int sths34pf80_read_channel(struct sths34pf80_data *data,
+				   unsigned int reg, int *val)
+{
+	int ret;
+
+	guard(mutex)(&data->lock);
+
+	ret = sths34pf80_one_shot(data);
+	if (ret)
+		return ret;
+
+	return sths34pf80_read_s16(data, reg, val);
+}
+
+/*
+ * TOBJECT = sigma * (T_obj^4 - T_amb^4) and SENS = 4 * sigma * T_cal^3
+ * (AN5867 4.5.1), solved for T_obj in centikelvin.
+ */
+static int sths34pf80_calc_object_temp(struct sths34pf80_data *data,
+				       int tobject, int tambient, int *val)
+{
+	s64 tamb = tambient + STHS34PF80_ZERO_CELSIUS_CK;
+	u64 cal = STHS34PF80_SENS_CAL_TEMP_CK;
+	u64 tamb4, tobj4, scale;
+	s64 delta;
+	int tobj;
+
+	tamb4 = tamb * tamb;
+	tamb4 *= tamb4;
+
+	scale = div_u64(4 * cal * cal * cal * STHS34PF80_TAMBIENT_LSB_PER_DEG,
+			data->sensitivity);
+
+	if (check_mul_overflow(tobject, scale, &delta) ||
+	    check_add_overflow(tamb4, delta, &tobj4))
+		return -ERANGE;
+
+	tobj = int_sqrt64(int_sqrt64(tobj4));
+	*val = (tobj - STHS34PF80_ZERO_CELSIUS_CK) *
+	       (MILLIDEGREE_PER_DEGREE / STHS34PF80_TAMBIENT_LSB_PER_DEG);
+
+	return 0;
+}
+
+static int sths34pf80_read_object(struct sths34pf80_data *data, int *val)
+{
+	struct regmap *map = data->regmap;
+	int tobject, tambient;
+	/* TOBJECT and TAMBIENT are adjacent */
+	__le16 raw[2];
+	int ret;
+
+	guard(mutex)(&data->lock);
+
+	ret = sths34pf80_one_shot(data);
+	if (ret)
+		return ret;
+
+	ret = regmap_bulk_read(map, STHS34PF80_REG_TOBJECT_L, raw, sizeof(raw));
+	if (ret)
+		return ret;
+
+	tobject = (s16)le16_to_cpu(raw[0]);
+	tambient = (s16)le16_to_cpu(raw[1]);
+
+	return sths34pf80_calc_object_temp(data, tobject, tambient, val);
+}
+
+static int sths34pf80_read_raw(struct iio_dev *indio_dev,
+			       struct iio_chan_spec const *chan,
+			       int *val, int *val2, long mask)
+{
+	struct sths34pf80_data *data = iio_priv(indio_dev);
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW: {
+		IIO_DEV_ACQUIRE_DIRECT_MODE(indio_dev, claim);
+		if (IIO_DEV_ACQUIRE_FAILED(claim))
+			return -EBUSY;
+
+		ret = sths34pf80_read_channel(data, chan->address, val);
+		if (ret)
+			return ret;
+
+		return IIO_VAL_INT;
+	}
+	case IIO_CHAN_INFO_PROCESSED: {
+		IIO_DEV_ACQUIRE_DIRECT_MODE(indio_dev, claim);
+		if (IIO_DEV_ACQUIRE_FAILED(claim))
+			return -EBUSY;
+
+		ret = sths34pf80_read_object(data, val);
+		if (ret)
+			return ret;
+
+		return IIO_VAL_INT;
+	}
+	case IIO_CHAN_INFO_SCALE:
+		*val = MILLIDEGREE_PER_DEGREE / STHS34PF80_TAMBIENT_LSB_PER_DEG;
+		return IIO_VAL_INT;
+	default:
+		return -EINVAL;
+	}
+}
+
+static const struct iio_info sths34pf80_info = {
+	.read_raw = sths34pf80_read_raw,
+};
+
+static int sths34pf80_probe(struct i2c_client *client)
+{
+	struct device *dev = &client->dev;
+	struct sths34pf80_data *data;
+	struct iio_dev *indio_dev;
+	unsigned int sens_data;
+	struct regmap *regmap;
+	unsigned int whoami;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	data = iio_priv(indio_dev);
+
+	ret = devm_mutex_init(dev, &data->lock);
+	if (ret)
+		return ret;
+
+	ret = devm_regulator_get_enable(dev, "vdd");
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to enable vdd\n");
+
+	fsleep(STHS34PF80_BOOT_TIME_US);
+
+	regmap = devm_regmap_init_i2c(client, &sths34pf80_regmap_config);
+	if (IS_ERR(regmap))
+		return dev_err_probe(dev, PTR_ERR(regmap),
+				     "Failed to init regmap\n");
+
+	data->regmap = regmap;
+
+	ret = regmap_read(regmap, STHS34PF80_REG_WHO_AM_I, &whoami);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to read WHO_AM_I\n");
+
+	if (whoami != STHS34PF80_WHO_AM_I_VAL)
+		dev_info(dev, "Unknown WHO_AM_I: 0x%02x\n", whoami);
+
+	ret = regmap_write(regmap, STHS34PF80_REG_CTRL2, STHS34PF80_CTRL2_BOOT);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to reboot\n");
+
+	fsleep(STHS34PF80_BOOT_TIME_US);
+
+	ret = regmap_update_bits(regmap, STHS34PF80_REG_CTRL1,
+				 STHS34PF80_CTRL1_BDU | STHS34PF80_CTRL1_ODR,
+				 STHS34PF80_CTRL1_BDU |
+				 FIELD_PREP(STHS34PF80_CTRL1_ODR,
+					    STHS34PF80_CTRL1_ODR_POWER_DOWN));
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to set CTRL1\n");
+
+	ret = regmap_read(regmap, STHS34PF80_REG_SENS_DATA, &sens_data);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to read SENS_DATA\n");
+
+	data->sensitivity = (s8)sens_data * STHS34PF80_SENS_STEP +
+			    STHS34PF80_SENS_BASE;
+	if (!data->sensitivity)
+		return dev_err_probe(dev, -EINVAL, "Invalid sensitivity\n");
+
+	dev_dbg(dev, "Sensitivity: %u LSB/degC\n", data->sensitivity);
+
+	indio_dev->name = "sths34pf80";
+	indio_dev->info = &sths34pf80_info;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = sths34pf80_channels;
+	indio_dev->num_channels = ARRAY_SIZE(sths34pf80_channels);
+
+	return devm_iio_device_register(dev, indio_dev);
+}
+
+static const struct of_device_id sths34pf80_of_match[] = {
+	{ .compatible = "st,sths34pf80" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, sths34pf80_of_match);
+
+static const struct i2c_device_id sths34pf80_id[] = {
+	{ .name = "sths34pf80" },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, sths34pf80_id);
+
+static struct i2c_driver sths34pf80_driver = {
+	.driver = {
+		.name = "sths34pf80",
+		.of_match_table = sths34pf80_of_match,
+	},
+	.probe = sths34pf80_probe,
+	.id_table = sths34pf80_id,
+};
+module_i2c_driver(sths34pf80_driver);
+
+MODULE_AUTHOR("Donggeun Yoo <donggeunyoo.kernel@gmail.com>");
+MODULE_DESCRIPTION("STMicroelectronics STHS34PF80 presence and motion sensor driver");
+MODULE_LICENSE("GPL");
-- 
2.53.0


      parent reply	other threads:[~2026-12-03  0:51 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  0:50 [PATCH 0/2] iio: proximity: Add ST STHS34PF80 driver Donggeun Yoo
2026-12-03  0:50 ` [PATCH 1/2] dt-bindings: iio: proximity: Add ST STHS34PF80 infrared sensor Donggeun Yoo
2026-12-03  0:50 ` Donggeun Yoo [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203005055.26666-3-donggeunyoo.kernel@gmail.com \
    --to=donggeunyoo.kernel@gmail.com \
    --cc=andy@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=robh@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox