Devicetree
 help / color / mirror / Atom feed
From: Jiayu Du <duge1256@gmail.com>
To: daniel.lezcano@kernel.org, rafael@kernel.org
Cc: rui.zhang@intel.com, lukasz.luba@arm.com, robh@kernel.org,
	krzk+dt@kernel.org, conor+dt@kernel.org, dlan@kernel.org,
	troy.mitchell@linux.spacemit.com, pjw@kernel.org,
	palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr,
	p.zabel@pengutronix.de, me@ziyao.cc, linux.amoon@gmail.com,
	shuwei.wu@mailbox.org, linux-pm@vger.kernel.org,
	devicetree@vger.kernel.org, linux-riscv@lists.infradead.org,
	spacemit@lists.linux.dev, linux-kernel@vger.kernel.org,
	ipedrosa@redhat.com, aurelien@aurel32.net, srini@kernel.org,
	Jiayu Du <duge1256@gmail.com>
Subject: [RFC PATCH 2/3] thermal/drivers/spacemit/k3: Add temperature sensor support
Date: Fri,  4 Dec 2026 22:23:11 +0800	[thread overview]
Message-ID: <20261204142313.137015-3-duge1256@gmail.com> (raw)
In-Reply-To: <20261204142313.137015-1-duge1256@gmail.com>

Add temperature readings for the seven on-die sensors in the K3 SoC
through the thermal and hwmon interfaces.

Serialize channel selection and reads of the shared data register, waiting
for the sample to settle after each selection. Convert the 12-bit sample
to millidegrees Celsius and use polling for temperature updates.

Read voltage reference calibration through NVMEM, using the backup cell
if the primary value is below 8, or 0xd8 if both values are below 8.

Signed-off-by: Jiayu Du <duge1256@gmail.com>
---
 MAINTAINERS                           |   1 +
 drivers/thermal/spacemit/Kconfig      |  11 ++
 drivers/thermal/spacemit/Makefile     |   1 +
 drivers/thermal/spacemit/k3_tsensor.c | 224 ++++++++++++++++++++++++++
 4 files changed, 237 insertions(+)
 create mode 100644 drivers/thermal/spacemit/k3_tsensor.c

diff --git a/MAINTAINERS b/MAINTAINERS
index b852a90873a0..d21b0bd475b0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -26535,6 +26535,7 @@ SPACEMIT K3 THERMAL DRIVER
 M:	Jiayu Du <duge1256@gmail.com>
 S:	Maintained
 F:	Documentation/devicetree/bindings/thermal/spacemit,k3-tsensor.yaml
+F:	drivers/thermal/spacemit/k3_tsensor.c
 
 SPACEMIT RESET CONTROLLER DRIVER
 M:	Guodong Xu <docular.xu@gmail.com>
diff --git a/drivers/thermal/spacemit/Kconfig b/drivers/thermal/spacemit/Kconfig
index de7b5ece5af2..00b19029916c 100644
--- a/drivers/thermal/spacemit/Kconfig
+++ b/drivers/thermal/spacemit/Kconfig
@@ -16,4 +16,15 @@ config SPACEMIT_K1_TSENSOR
 	  Say Y here if you want to enable thermal monitoring on SpacemiT K1.
 	  If compiled as a module, it will be called k1_tsensor.
 
+config SPACEMIT_K3_TSENSOR
+	tristate "SpacemiT K3 thermal sensor driver"
+	depends on THERMAL_OF && NVMEM
+	help
+	  Support the multiplexed on-die temperature sensors on SpacemiT K3.
+	  This driver reads calibration data through NVMEM and provides
+	  temperature readings using polling thermal zones. Hardware threshold
+	  interrupts are not supported.
+
+	  If compiled as a module, it will be called k3_tsensor.
+
 endmenu
diff --git a/drivers/thermal/spacemit/Makefile b/drivers/thermal/spacemit/Makefile
index 82b30741e4ec..8ea3b3a6a96a 100644
--- a/drivers/thermal/spacemit/Makefile
+++ b/drivers/thermal/spacemit/Makefile
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
 obj-$(CONFIG_SPACEMIT_K1_TSENSOR)	+= k1_tsensor.o
+obj-$(CONFIG_SPACEMIT_K3_TSENSOR)	+= k3_tsensor.o
diff --git a/drivers/thermal/spacemit/k3_tsensor.c b/drivers/thermal/spacemit/k3_tsensor.c
new file mode 100644
index 000000000000..59bfb07a10a4
--- /dev/null
+++ b/drivers/thermal/spacemit/k3_tsensor.c
@@ -0,0 +1,224 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Thermal sensor driver for SpacemiT K3 SoC
+ *
+ * Copyright (C) 2026 Jiayu Du <duge1256@gmail.com>
+ */
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/err.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/nvmem-consumer.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/reset.h>
+#include <linux/slab.h>
+#include <linux/thermal.h>
+
+#include "../thermal_hwmon.h"
+
+#define K3_TSENSOR_CFG_REG		0x00
+#define K3_TSENSOR_CFG_ENABLE		BIT(0)
+#define K3_TSENSOR_CFG_DIV		GENMASK(3, 2)
+#define K3_TSENSOR_CFG_DIV_4		0
+#define K3_TSENSOR_CFG_VREF		GENMASK(14, 7)
+
+#define K3_TSENSOR_SELECT_REG		0x04
+#define K3_TSENSOR_INT_EN_REG		0x08
+#define K3_TSENSOR_INT_CLR_REG		0x10
+#define K3_TSENSOR_INT_MASK_ALL		GENMASK(31, 0)
+
+#define K3_TSENSOR_DATA_REG		0x18
+#define K3_TSENSOR_DATA_MASK		GENMASK(11, 0)
+
+#define MAX_SENSOR_NUMBER		8
+#define K3_TSENSOR_UNUSED_CHANNEL	1
+#define K3_TSENSOR_SETTLE_MIN_US	1000
+#define K3_TSENSOR_SETTLE_MAX_US	2000
+#define TEMPERATURE_OFFSET_C		274
+#define TEMP_RAW_PER_C			8
+#define CALIB_VREF_MIN			8
+#define CALIB_VREF_DEFAULT		0xd8
+
+struct k3_tsensor_channel {
+	struct k3_tsensor *ts;
+	int id;
+};
+
+struct k3_tsensor {
+	void __iomem *base;
+	/* Protects channel selection and sample reads. */
+	struct mutex lock;
+	struct k3_tsensor_channel ch[MAX_SENSOR_NUMBER];
+};
+
+static int k3_tsensor_read_calibration(struct device *dev, u8 *trim)
+{
+	u32 val;
+	int ret;
+
+	ret = nvmem_cell_read_variable_le_u32(dev, "soc_rtemp_trim", &val);
+	if (ret)
+		return ret;
+
+	if (val < CALIB_VREF_MIN) {
+		ret = nvmem_cell_read_variable_le_u32(dev, "soc_rtemp_trim1", &val);
+		if (ret)
+			return ret;
+	}
+
+	if (val < CALIB_VREF_MIN) {
+		val = CALIB_VREF_DEFAULT;
+		dev_warn(dev, "Using default temperature calibration\n");
+	}
+
+	*trim = val;
+
+	return 0;
+}
+
+static void k3_tsensor_init(struct k3_tsensor *ts, u8 trim)
+{
+	u32 val;
+
+	writel(K3_TSENSOR_INT_MASK_ALL, ts->base + K3_TSENSOR_INT_EN_REG);
+	writel(K3_TSENSOR_INT_MASK_ALL, ts->base + K3_TSENSOR_INT_CLR_REG);
+
+	val = readl(ts->base + K3_TSENSOR_CFG_REG);
+	val &= ~(K3_TSENSOR_CFG_ENABLE | K3_TSENSOR_CFG_DIV | K3_TSENSOR_CFG_VREF);
+	val |= FIELD_PREP(K3_TSENSOR_CFG_DIV, K3_TSENSOR_CFG_DIV_4) |
+	       FIELD_PREP(K3_TSENSOR_CFG_VREF, trim);
+	writel(val, ts->base + K3_TSENSOR_CFG_REG);
+
+	val |= K3_TSENSOR_CFG_ENABLE;
+	writel(val, ts->base + K3_TSENSOR_CFG_REG);
+}
+
+static int k3_tsensor_get_temp(struct thermal_zone_device *tz, int *temp)
+{
+	struct k3_tsensor_channel *ch = thermal_zone_device_priv(tz);
+	struct k3_tsensor *ts = ch->ts;
+	u32 val;
+
+	mutex_lock(&ts->lock);
+	writel(ch->id, ts->base + K3_TSENSOR_SELECT_REG);
+	usleep_range(K3_TSENSOR_SETTLE_MIN_US, K3_TSENSOR_SETTLE_MAX_US);
+	val = readl(ts->base + K3_TSENSOR_DATA_REG);
+	mutex_unlock(&ts->lock);
+
+	*temp = FIELD_GET(K3_TSENSOR_DATA_MASK, val);
+	*temp *= 1000;
+	*temp /= TEMP_RAW_PER_C;
+	*temp -= TEMPERATURE_OFFSET_C * 1000;
+
+	return 0;
+}
+
+static const struct thermal_zone_device_ops k3_tsensor_ops = {
+	.get_temp = k3_tsensor_get_temp,
+};
+
+static void k3_tsensor_shutdown(struct k3_tsensor *ts)
+{
+	u32 val;
+
+	writel(K3_TSENSOR_INT_MASK_ALL, ts->base + K3_TSENSOR_INT_EN_REG);
+
+	val = readl(ts->base + K3_TSENSOR_CFG_REG);
+	val &= ~K3_TSENSOR_CFG_ENABLE;
+	writel(val, ts->base + K3_TSENSOR_CFG_REG);
+}
+
+static void k3_tsensor_shutdown_action(void *data)
+{
+	k3_tsensor_shutdown(data);
+}
+
+static int k3_tsensor_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct k3_tsensor *ts;
+	struct reset_control *reset;
+	struct thermal_zone_device *tzd;
+	struct clk *clk;
+	u8 trim;
+	int i, ret;
+
+	ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
+	if (!ts)
+		return -ENOMEM;
+
+	ts->base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(ts->base))
+		return PTR_ERR(ts->base);
+
+	ret = k3_tsensor_read_calibration(dev, &trim);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to read calibration\n");
+
+	ret = devm_mutex_init(dev, &ts->lock);
+	if (ret)
+		return ret;
+
+	reset = devm_reset_control_get_exclusive_deasserted(dev, NULL);
+	if (IS_ERR(reset))
+		return dev_err_probe(dev, PTR_ERR(reset), "Failed to get/deassert reset control\n");
+
+	clk = devm_clk_get_enabled(dev, "core");
+	if (IS_ERR(clk))
+		return dev_err_probe(dev, PTR_ERR(clk), "Failed to get core clock\n");
+
+	clk = devm_clk_get_enabled(dev, "bus");
+	if (IS_ERR(clk))
+		return dev_err_probe(dev, PTR_ERR(clk), "Failed to get bus clock\n");
+
+	k3_tsensor_init(ts, trim);
+
+	/* Unregister polling zones before stopping sampling */
+	ret = devm_add_action_or_reset(dev, k3_tsensor_shutdown_action, ts);
+	if (ret)
+		return ret;
+
+	for (i = 0; i < MAX_SENSOR_NUMBER; ++i) {
+		if (i == K3_TSENSOR_UNUSED_CHANNEL)
+			continue;
+
+		ts->ch[i].id = i;
+		ts->ch[i].ts = ts;
+		tzd = devm_thermal_of_zone_register(dev, i, ts->ch + i, &k3_tsensor_ops);
+		if (IS_ERR(tzd)) {
+			ret = PTR_ERR(tzd);
+			if (ret == -ENODEV)
+				continue;
+
+			return ret;
+		}
+
+		devm_thermal_add_hwmon_sysfs(dev, tzd);
+	}
+
+	return 0;
+}
+
+static const struct of_device_id k3_tsensor_dt_ids[] = {
+	{ .compatible = "spacemit,k3-tsensor" },
+	{ }
+};
+
+MODULE_DEVICE_TABLE(of, k3_tsensor_dt_ids);
+
+static struct platform_driver k3_tsensor_driver = {
+	.driver = {
+		.name		= "k3_tsensor",
+		.of_match_table = k3_tsensor_dt_ids,
+	},
+	.probe	= k3_tsensor_probe,
+};
+module_platform_driver(k3_tsensor_driver);
+
+MODULE_DESCRIPTION("SpacemiT K3 Thermal Sensor Driver");
+MODULE_AUTHOR("Jiayu Du <duge1256@gmail.com>");
+MODULE_LICENSE("GPL");
-- 
2.43.0


  parent reply	other threads:[~2026-12-04 14:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 14:23 [RFC PATCH 0/3] Add polling temperature monitoring for SpacemiT K3 Jiayu Du
2026-12-04 14:23 ` [RFC PATCH 1/3] dt-bindings: thermal: Add SpacemiT K3 temperature sensor Jiayu Du
2026-12-04 14:33   ` sashiko-bot
2026-12-04 14:23 ` Jiayu Du [this message]
2026-12-04 14:23 ` [RFC PATCH 3/3] riscv: dts: spacemit: Describe K3 temperature monitoring Jiayu Du
2026-12-04 14:57   ` sashiko-bot

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=20261204142313.137015-3-duge1256@gmail.com \
    --to=duge1256@gmail.com \
    --cc=alex@ghiti.fr \
    --cc=aou@eecs.berkeley.edu \
    --cc=aurelien@aurel32.net \
    --cc=conor+dt@kernel.org \
    --cc=daniel.lezcano@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dlan@kernel.org \
    --cc=ipedrosa@redhat.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=linux.amoon@gmail.com \
    --cc=lukasz.luba@arm.com \
    --cc=me@ziyao.cc \
    --cc=p.zabel@pengutronix.de \
    --cc=palmer@dabbelt.com \
    --cc=pjw@kernel.org \
    --cc=rafael@kernel.org \
    --cc=robh@kernel.org \
    --cc=rui.zhang@intel.com \
    --cc=shuwei.wu@mailbox.org \
    --cc=spacemit@lists.linux.dev \
    --cc=srini@kernel.org \
    --cc=troy.mitchell@linux.spacemit.com \
    /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