[openwrt/openwrt] realtek: extend simple thermal driver to support rtl839x

LEDE Commits lede-commits at lists.infradead.org
Tue Jun 3 02:14:56 PDT 2025


robimarko pushed a commit to openwrt/openwrt.git, branch main:
https://git.openwrt.org/cc5421128e44effd5df05227cec4d4c5d05be8dc

commit cc5421128e44effd5df05227cec4d4c5d05be8dc
Author: Stephen Howell <howels at allthatwemight.be>
AuthorDate: Sat May 17 12:06:10 2025 +0100

    realtek: extend simple thermal driver to support rtl839x
    
    * Extends SoC thermal sensor on rtl839x
    * Tested on HP JG928A
    
    Signed-off-by: Stephen Howell <howels at allthatwemight.be>
    Link: https://github.com/openwrt/openwrt/pull/18825
    Signed-off-by: Robert Marko <robimarko at gmail.com>
---
 target/linux/realtek/dts/rtl839x.dtsi              | 21 +++++++++++
 .../files-6.6/drivers/thermal/realtek-thermal.c    | 44 ++++++++++++++++++++++
 .../330-add-realtek-thernal-driver.patch           |  4 +-
 target/linux/realtek/rtl839x/config-6.6            |  9 +++++
 4 files changed, 76 insertions(+), 2 deletions(-)

diff --git a/target/linux/realtek/dts/rtl839x.dtsi b/target/linux/realtek/dts/rtl839x.dtsi
index 7fc5aea70c..84473fa2c3 100644
--- a/target/linux/realtek/dts/rtl839x.dtsi
+++ b/target/linux/realtek/dts/rtl839x.dtsi
@@ -272,6 +272,11 @@
 			pinctrl-names = "default";
 			pinctrl-0 = <&mdio_aux_mdx>;
 		};
+
+		soc_thermal: thermal {
+			compatible = "realtek,rtl8390-thermal";
+			#thermal-sensor-cells = <0>;
+		};
 	};
 
 	pinmux at 1b000004 {
@@ -343,4 +348,20 @@
 		interrupt-parent = <&intc>;
 		interrupts = <20 2>;
 	};
+
+	thermal_zones: thermal-zones {
+		cpu-thermal {
+			polling-delay-passive = <1000>;
+			polling-delay = <1000>;
+			coefficients = <1000 0>;
+			thermal-sensors = <&soc_thermal>;
+			trips {
+				cpu-crit {
+					temperature = <105000>;
+					hysteresis = <2000>;
+					type = "critical";
+				};
+			};
+		};
+	};
 };
diff --git a/target/linux/realtek/files-6.6/drivers/thermal/realtek-thermal.c b/target/linux/realtek/files-6.6/drivers/thermal/realtek-thermal.c
index 242f11bd89..f91ddbccc0 100644
--- a/target/linux/realtek/files-6.6/drivers/thermal/realtek-thermal.c
+++ b/target/linux/realtek/files-6.6/drivers/thermal/realtek-thermal.c
@@ -20,6 +20,18 @@
 #define RTL8380_TEMP_VALID		BIT(8)
 #define RTL8380_TEMP_OUT_MASK		GENMASK(6, 0)
 
+#define RTL8390_THERMAL_METER0_CTRL0	0x274
+#define RTL8390_THERMAL_METER0_CTRL1	0x278
+#define RTL8390_THERMAL_METER0_CTRL2	0x27c
+#define RTL8390_THERMAL_METER0_RESULT	0x280
+#define RTL8390_THERMAL_METER1_CTRL0	0x284
+#define RTL8390_THERMAL_METER1_CTRL1	0x288
+#define RTL8390_THERMAL_METER1_CTRL2	0x28c
+#define RTL8390_THERMAL_METER1_RESULT	0x290
+#define RTL8390_TM_ENABLE		BIT(0)
+#define RTL8390_TEMP_VALID		BIT(8)
+#define RTL8390_TEMP_OUT_MASK		GENMASK(6, 0)
+
 #define RTL9300_THERMAL_METER_CTRL0	0x60
 #define RTL9300_THERMAL_METER_CTRL1	0x64
 #define RTL9300_THERMAL_METER_CTRL2	0x68
@@ -69,6 +81,37 @@ static const struct thermal_zone_device_ops rtl8380_ops = {
 	.get_temp = rtl8380_get_temp,
 };
 
+static void rtl8390_thermal_init(struct realtek_thermal_priv *priv)
+{
+	priv->enabled = !regmap_update_bits(priv->regmap, RTL8390_THERMAL_METER0_CTRL0, RTL8390_TM_ENABLE, RTL8390_TM_ENABLE);
+}
+
+static int rtl8390_get_temp(struct thermal_zone_device *tz, int *res)
+{
+	struct realtek_thermal_priv *priv = thermal_zone_device_priv(tz);
+	int offset = thermal_zone_get_offset(tz);
+	int slope = thermal_zone_get_slope(tz);
+	u32 val;
+	int ret;
+
+	if (!priv->enabled)
+		rtl8390_thermal_init(priv);
+	/* assume sensor0 is the CPU, both sensor0 & sensor1 report same values +/- 1 degree C */
+	ret = regmap_read(priv->regmap, RTL8390_THERMAL_METER0_RESULT, &val);
+	if (ret)
+		return ret;
+
+	if (!(val & RTL8390_TEMP_VALID))
+		return -EAGAIN;
+
+	*res = FIELD_GET(RTL8390_TEMP_OUT_MASK, val) * slope + offset;
+	return 0;
+}
+
+static const struct thermal_zone_device_ops rtl8390_ops = {
+	.get_temp = rtl8390_get_temp,
+};
+
 static void rtl9300_thermal_init(struct realtek_thermal_priv *priv)
 {
 	/* increasing sample delay makes get_temp() succeed more often */
@@ -126,6 +169,7 @@ static int realtek_thermal_probe(struct platform_device *pdev)
 
 static const struct of_device_id realtek_sensor_ids[] = {
 	{ .compatible = "realtek,rtl8380-thermal", .data = &rtl8380_ops, },
+	{ .compatible = "realtek,rtl8390-thermal", .data = &rtl8390_ops, },
 	{ .compatible = "realtek,rtl9300-thermal", .data = &rtl9300_ops, },
 	{ /* sentinel */ }
 };
diff --git a/target/linux/realtek/patches-6.6/330-add-realtek-thernal-driver.patch b/target/linux/realtek/patches-6.6/330-add-realtek-thernal-driver.patch
index 0c8d4ab3ab..af550a8c20 100644
--- a/target/linux/realtek/patches-6.6/330-add-realtek-thernal-driver.patch
+++ b/target/linux/realtek/patches-6.6/330-add-realtek-thernal-driver.patch
@@ -6,10 +6,10 @@
  
 +config REALTEK_THERMAL
 +	tristate "Realtek RTL838x and RTL930x thermal sensor support"
-+	depends on RTL838X || RTL930X || COMPILE_TEST
++	depends on RTL838X || RTL839X || RTL930X || COMPILE_TEST
 +	depends on THERMAL_OF
 +	help
-+	  Support thermal sensor in Realtek RTL838x and RTL930x SoCs
++	  Support thermal sensor in Realtek RTL838x, RTL839x and RTL930x SoCs
 +
  endif
 --- a/drivers/thermal/Makefile
diff --git a/target/linux/realtek/rtl839x/config-6.6 b/target/linux/realtek/rtl839x/config-6.6
index 7ea9f707cb..f71be02906 100644
--- a/target/linux/realtek/rtl839x/config-6.6
+++ b/target/linux/realtek/rtl839x/config-6.6
@@ -218,6 +218,7 @@ CONFIG_REALTEK_OTTO_WDT=y
 CONFIG_REALTEK_PHY=y
 CONFIG_REALTEK_PHY_HWMON=y
 CONFIG_REALTEK_SOC_PHY=y
+CONFIG_REALTEK_THERMAL=y
 CONFIG_REGMAP=y
 CONFIG_REGMAP_I2C=y
 CONFIG_REGMAP_MDIO=y
@@ -254,6 +255,14 @@ CONFIG_SYS_SUPPORTS_MULTITHREADING=y
 CONFIG_SYS_SUPPORTS_SCHED_SMT=y
 CONFIG_SYS_SUPPORTS_SMP=y
 CONFIG_TARGET_ISA_REV=2
+CONFIG_THERMAL=y
+CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE=y
+# CONFIG_THERMAL_DEFAULT_GOV_FAIR_SHARE is not set
+# CONFIG_THERMAL_DEFAULT_GOV_USER_SPACE is not set
+CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS=0
+CONFIG_THERMAL_GOV_STEP_WISE=y
+CONFIG_THERMAL_HWMON=y
+CONFIG_THERMAL_OF=y
 CONFIG_TICK_CPU_ACCOUNTING=y
 CONFIG_TIMER_OF=y
 CONFIG_TIMER_PROBE=y




More information about the lede-commits mailing list