mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Zi-Yu Chen <zychennvt@gmail.com>
To: rafael@kernel.org, daniel.lezcano@kernel.org
Cc: ychuang3@nuvoton.com, schung@nuvoton.com, p.zabel@pengutronix.de,
	robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org,
	rui.zhang@intel.com, lukasz.luba@arm.com,
	linux-arm-kernel@lists.infradead.org, devicetree@vger.kernel.org,
	linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
	Zi-Yu Chen <zychennvt@gmail.com>
Subject: [PATCH 3/4] thermal/drivers/ma35d1: Add Nuvoton MA35D1 thermal sensor driver
Date: Tue, 17 Nov 2026 14:05:00 +0800	[thread overview]
Message-ID: <20261117060501.2611071-4-zychennvt@gmail.com> (raw)
In-Reply-To: <20261117060501.2611071-1-zychennvt@gmail.com>

Add a driver for the on-chip temperature sensor of the Nuvoton MA35D1
SoC. The sensor is controlled through a register in the system
management (SYS) block and is instantiated as an auxiliary device by
the MA35D1 reset driver.

The sensor converts continuously while powered on. The 12-bit result
is converted to millidegrees Celsius using a linear fit of the
temperature table from the sensor datasheet.

Signed-off-by: Zi-Yu Chen <zychennvt@gmail.com>
---
 drivers/thermal/Kconfig          |  11 +++
 drivers/thermal/Makefile         |   1 +
 drivers/thermal/ma35d1_thermal.c | 149 +++++++++++++++++++++++++++++++
 3 files changed, 161 insertions(+)
 create mode 100644 drivers/thermal/ma35d1_thermal.c

diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
index 810eeccedfba..571b2c026311 100644
--- a/drivers/thermal/Kconfig
+++ b/drivers/thermal/Kconfig
@@ -530,4 +530,15 @@ config LOONGSON2_THERMAL
 	  is higher than the high temperature threshold or lower than the low
 	  temperature threshold, the interrupt will occur.
 
+config MA35D1_THERMAL
+	tristate "Nuvoton MA35D1 thermal sensor"
+	depends on RESET_NUVOTON_MA35D1
+	depends on THERMAL_OF
+	select MFD_SYSCON
+	help
+	  Support for the on-chip temperature sensor found on Nuvoton
+	  MA35D1 SoCs. The driver provides temperature readings through the
+	  Linux thermal framework. The sensor registers are accessed through
+	  the system register block.
+
 endif
diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
index 3b249195c088..bf2796e3dd3f 100644
--- a/drivers/thermal/Makefile
+++ b/drivers/thermal/Makefile
@@ -70,3 +70,4 @@ obj-$(CONFIG_SPRD_THERMAL)	+= sprd_thermal.o
 obj-$(CONFIG_KHADAS_MCU_FAN_THERMAL)	+= khadas_mcu_fan.o
 obj-$(CONFIG_LOONGSON2_THERMAL)	+= loongson2_thermal.o
 obj-$(CONFIG_THERMAL_CORE_TESTING)	+= testing/
+obj-$(CONFIG_MA35D1_THERMAL)	+= ma35d1_thermal.o
diff --git a/drivers/thermal/ma35d1_thermal.c b/drivers/thermal/ma35d1_thermal.c
new file mode 100644
index 000000000000..453f57d236ec
--- /dev/null
+++ b/drivers/thermal/ma35d1_thermal.c
@@ -0,0 +1,149 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2026 Nuvoton Technology Corp.
+ *
+ * Author: Zi-Yu Chen <zychennvt@gmail.com>
+ */
+
+#include <linux/auxiliary_bus.h>
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/mfd/syscon.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/regmap.h>
+#include <linux/thermal.h>
+
+#include "thermal_hwmon.h"
+
+#define MA35_SYS_TSENSRFCR		0x104
+#define MA35_TSEN_DATA_VALID	BIT(31)
+#define MA35_TSEN_POWER_DOWN	BIT(28)
+#define MA35_TSEN_DATA			GENMASK(27, 16)
+
+/*
+ * Conversion from the 12-bit sensor data to millidegrees Celsius:
+ *   temp (mC) = data * MA35_TS_CAL_A / 4096 - MA35_TS_CAL_B
+ * A and B come from a linear fit of the S40LLV33_TEMPSENSOR temperature table.
+ */
+#define MA35_TS_CAL_A			274350
+#define MA35_TS_CAL_B			93330
+
+struct ma35d1_thermal {
+	struct device *dev;
+	struct regmap *regmap;
+};
+
+static int ma35d1_thermal_get_temp(struct thermal_zone_device *tzd, int *temp)
+{
+	struct ma35d1_thermal *sensor = thermal_zone_device_priv(tzd);
+	unsigned int reg;
+	int data, ret;
+
+	ret = regmap_read(sensor->regmap, MA35_SYS_TSENSRFCR, &reg);
+	if (ret)
+		return ret;
+
+	if (!(reg & MA35_TSEN_DATA_VALID))
+		return -EAGAIN;
+
+	data = FIELD_GET(MA35_TSEN_DATA, reg);
+	*temp = data * MA35_TS_CAL_A / 4096 - MA35_TS_CAL_B;
+
+	return 0;
+}
+
+static int ma35d1_thermal_power_on(struct ma35d1_thermal *sensor)
+{
+	return regmap_clear_bits(sensor->regmap, MA35_SYS_TSENSRFCR,
+				 MA35_TSEN_POWER_DOWN);
+}
+
+static int ma35d1_thermal_power_off(struct ma35d1_thermal *sensor)
+{
+	return regmap_set_bits(sensor->regmap, MA35_SYS_TSENSRFCR,
+			       MA35_TSEN_POWER_DOWN);
+}
+
+static int ma35d1_thermal_change_mode(struct thermal_zone_device *tzd,
+				      enum thermal_device_mode mode)
+{
+	struct ma35d1_thermal *sensor = thermal_zone_device_priv(tzd);
+
+	if (mode == THERMAL_DEVICE_ENABLED)
+		return ma35d1_thermal_power_on(sensor);
+
+	return ma35d1_thermal_power_off(sensor);
+}
+
+static void ma35d1_thermal_power_off_action(void *data)
+{
+	struct ma35d1_thermal *sensor = data;
+
+	if (ma35d1_thermal_power_off(sensor))
+		dev_warn(sensor->dev, "failed to power off sensor\n");
+}
+
+static const struct thermal_zone_device_ops ma35d1_tz_ops = {
+	.get_temp = ma35d1_thermal_get_temp,
+	.change_mode = ma35d1_thermal_change_mode,
+};
+
+static int ma35d1_thermal_probe(struct auxiliary_device *adev,
+				const struct auxiliary_device_id *id)
+{
+	struct device *dev = &adev->dev;
+	struct thermal_zone_device *tzd;
+	struct ma35d1_thermal *sensor;
+	int ret;
+
+	sensor = devm_kzalloc_obj(dev, *sensor);
+	if (!sensor)
+		return -ENOMEM;
+
+	sensor->dev = dev;
+
+	sensor->regmap = syscon_node_to_regmap(dev->parent->of_node);
+	if (IS_ERR(sensor->regmap))
+		return dev_err_probe(dev, PTR_ERR(sensor->regmap),
+				     "failed to get SYS regmap\n");
+
+	/*
+	 * Registered before the thermal zone so that the sensor is powered
+	 * off only after the zone has been unregistered.
+	 */
+	ret = devm_add_action_or_reset(dev, ma35d1_thermal_power_off_action,
+				       sensor);
+	if (ret)
+		return ret;
+
+	tzd = devm_thermal_of_zone_register(dev, 0, sensor, &ma35d1_tz_ops);
+	if (IS_ERR(tzd))
+		return dev_err_probe(dev, PTR_ERR(tzd),
+				     "failed to register thermal zone\n");
+
+	/* thermal_of disables hwmon by default */
+	ret = devm_thermal_add_hwmon_sysfs(dev, tzd);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to add hwmon sysfs\n");
+
+	return 0;
+}
+
+static const struct auxiliary_device_id ma35d1_thermal_id_table[] = {
+	{ .name = "reset_ma35d1.thermal" },
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(auxiliary, ma35d1_thermal_id_table);
+
+static struct auxiliary_driver ma35d1_thermal_driver = {
+	.probe		= ma35d1_thermal_probe,
+	.id_table	= ma35d1_thermal_id_table,
+};
+module_auxiliary_driver(ma35d1_thermal_driver);
+
+MODULE_AUTHOR("Zi-Yu Chen <zychennvt@gmail.com>");
+MODULE_DESCRIPTION("Nuvoton MA35D1 Temperature Sensor Driver");
+MODULE_LICENSE("GPL");
-- 
2.34.1


  parent reply	other threads:[~2026-11-17  6:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  6:04 [PATCH 0/4] Add Nuvoton MA35D1 thermal sensor support Zi-Yu Chen
2026-11-17  6:04 ` [PATCH 1/4] dt-bindings: reset: nuvoton,ma35d1-reset: Add #thermal-sensor-cells Zi-Yu Chen
2026-11-17  6:04 ` [PATCH 2/4] reset: ma35d1: Register auxiliary device for the temperature sensor Zi-Yu Chen
2026-11-17  6:05 ` Zi-Yu Chen [this message]
2026-11-17  6:05 ` [PATCH 4/4] arm64: dts: nuvoton: ma35d1: Add thermal zone Zi-Yu Chen

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=20261117060501.2611071-4-zychennvt@gmail.com \
    --to=zychennvt@gmail.com \
    --cc=conor+dt@kernel.org \
    --cc=daniel.lezcano@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=lukasz.luba@arm.com \
    --cc=p.zabel@pengutronix.de \
    --cc=rafael@kernel.org \
    --cc=robh@kernel.org \
    --cc=rui.zhang@intel.com \
    --cc=schung@nuvoton.com \
    --cc=ychuang3@nuvoton.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

all inboxes | Powered by JetHome®