Skip to contentSkip to conversation

thermal: loongson2: Add Loongson-2K0500 and Loongson-2K2000 thermal support

PATCH Superseded Binbin Zhou 4 participants 8 messages Apr 10, 2024

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover thermal: loongson2: Add Loongson-2K0500 and Loongson-2K2000 thermal support

Hi all:

This patchset introduce the Loongson-2K0500 and Loongson-2K2000 temperature sensors.

The temperature sensors of Loongson-2K series CPUs are similar, except that the temperature reading method of the Loongson-2K2000 is different.

Specifically, the temperature output register of the Loongson-2K2000 is defined in the chip configuration domain. We need to define it in dts and calculate it using different calculation methods.

Thanks.

Binbin Zhou (4):
thermal: loongson2: Trivial code style adjustment
dt-bindings: thermal: loongson,ls2k-thermal: Add Loongson-2K0500
compaible
dt-bindings: thermal: loongson,ls2k-thermal: Fix incorrect compatible
definition
thermal: loongson2: Add Loongson-2K2000 support

 .../thermal/loongson,ls2k-thermal.yaml        |  23 +++-
 drivers/thermal/loongson2_thermal.c           | 111 +++++++++++-------
 2 files changed, 92 insertions(+), 42 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/4 thermal: loongson2: Trivial code style adjustment

Here are some minor code style adjustment. Such as fix whitespace code style; align function call arguments to opening parenthesis, and add devm_thermal_add_hwmon_sysfs() return value checking.

---
 drivers/thermal/loongson2_thermal.c | 69 +++++++++++++++--------------
 1 file changed, 35 insertions(+), 34 deletions(-)
drivers/thermal/loongson2_thermal.c +35 -34
index 0f475fe46bc9..d77d829c8b55 100644
@@ -16,45 +16,49 @@
 #include <linux/units.h>
 #include "thermal_hwmon.h"
 
-#define LOONGSON2_MAX_SENSOR_SEL_NUM			3
+#define LOONGSON2_MAX_SENSOR_SEL_NUM	3
 
-#define LOONGSON2_THSENS_CTRL_HI_REG			0x0
-#define LOONGSON2_THSENS_CTRL_LOW_REG			0x8
-#define LOONGSON2_THSENS_STATUS_REG			0x10
-#define LOONGSON2_THSENS_OUT_REG			0x14
+#define LOONGSON2_THSENS_CTRL_HI_REG	0x0
+#define LOONGSON2_THSENS_CTRL_LOW_REG	0x8
+#define LOONGSON2_THSENS_STATUS_REG	0x10
+#define LOONGSON2_THSENS_OUT_REG	0x14
 
-#define LOONGSON2_THSENS_INT_LO				BIT(0)
-#define LOONGSON2_THSENS_INT_HIGH			BIT(1)
-#define LOONGSON2_THSENS_OUT_MASK			0xFF
+#define LOONGSON2_THSENS_INT_LO		BIT(0)
+#define LOONGSON2_THSENS_INT_HIGH	BIT(1)
+#define LOONGSON2_THSENS_INT_EN		(LOONGSON2_THSENS_INT_LO | \
+					 LOONGSON2_THSENS_INT_HIGH)
+#define LOONGSON2_THSENS_OUT_MASK	0xFF
 
 struct loongson2_thermal_chip_data {
-	unsigned int	thermal_sensor_sel;
+	unsigned int thermal_sensor_sel;
 };
 
 struct loongson2_thermal_data {
-	void __iomem	*regs;
+	void __iomem *regs;
 	const struct loongson2_thermal_chip_data *chip_data;
 };
 
-static int loongson2_thermal_set(struct loongson2_thermal_data *data,
-					int low, int high, bool enable)
+static void loongson2_set_ctrl_regs(struct loongson2_thermal_data *data,
+				    int ctrl_data, bool low, bool enable)
 {
-	u64 reg_ctrl = 0;
-	int reg_off = data->chip_data->thermal_sensor_sel * 2;
+	int reg_ctrl = 0;
+	int reg_off  = data->chip_data->thermal_sensor_sel * 2;
+	int ctrl_reg = low ? LOONGSON2_THSENS_CTRL_LOW_REG :
+		       LOONGSON2_THSENS_CTRL_HI_REG;
+
+	reg_ctrl = ctrl_data + HECTO;
+	reg_ctrl |= enable ? 0x100 : 0;
+	writew(reg_ctrl, data->regs + ctrl_reg + reg_off);
+}
 
+static int loongson2_thermal_set(struct loongson2_thermal_data *data,
+				 int low, int high, bool enable)
+{
 	low = clamp(-40, low, high);
 	high = clamp(125, low, high);
 
-	low += HECTO;
-	high += HECTO;
-
-	reg_ctrl = low;
-	reg_ctrl |= enable ? 0x100 : 0;
-	writew(reg_ctrl, data->regs + LOONGSON2_THSENS_CTRL_LOW_REG + reg_off);
-
-	reg_ctrl = high;
-	reg_ctrl |= enable ? 0x100 : 0;
-	writew(reg_ctrl, data->regs + LOONGSON2_THSENS_CTRL_HI_REG + reg_off);
+	loongson2_set_ctrl_regs(data, low, true, enable);
+	loongson2_set_ctrl_regs(data, high, false, enable);
 
 	return 0;
 }
@@ -75,15 +79,15 @@ static irqreturn_t loongson2_thermal_irq_thread(int irq, void *dev)
 	struct thermal_zone_device *tzd = dev;
 	struct loongson2_thermal_data *data = thermal_zone_device_priv(tzd);
 
-	writeb(LOONGSON2_THSENS_INT_LO | LOONGSON2_THSENS_INT_HIGH, data->regs +
-		LOONGSON2_THSENS_STATUS_REG);
+	writeb(LOONGSON2_THSENS_INT_EN, data->regs + LOONGSON2_THSENS_STATUS_REG);
 
 	thermal_zone_device_update(tzd, THERMAL_EVENT_UNSPECIFIED);
 
 	return IRQ_HANDLED;
 }
 
-static int loongson2_thermal_set_trips(struct thermal_zone_device *tz, int low, int high)
+static int loongson2_thermal_set_trips(struct thermal_zone_device *tz,
+				       int low, int high)
 {
 	struct loongson2_thermal_data *data = thermal_zone_device_priv(tz);
 
@@ -116,14 +120,13 @@ static int loongson2_thermal_probe(struct platform_device *pdev)
 	if (irq < 0)
 		return irq;
 
-	writeb(LOONGSON2_THSENS_INT_LO | LOONGSON2_THSENS_INT_HIGH, data->regs +
-		LOONGSON2_THSENS_STATUS_REG);
+	writeb(LOONGSON2_THSENS_INT_EN, data->regs + LOONGSON2_THSENS_STATUS_REG);
 
 	loongson2_thermal_set(data, 0, 0, false);
 
 	for (i = 0; i <= LOONGSON2_MAX_SENSOR_SEL_NUM; i++) {
 		tzd = devm_thermal_of_zone_register(dev, i, data,
-			&loongson2_of_thermal_ops);
+						    &loongson2_of_thermal_ops);
 
 		if (!IS_ERR(tzd))
 			break;
@@ -135,13 +138,11 @@ static int loongson2_thermal_probe(struct platform_device *pdev)
 	}
 
 	ret = devm_request_threaded_irq(dev, irq, NULL, loongson2_thermal_irq_thread,
-			IRQF_ONESHOT, "loongson2_thermal", tzd);
+					IRQF_ONESHOT, "loongson2_thermal", tzd);
 	if (ret < 0)
 		return dev_err_probe(dev, ret, "failed to request alarm irq\n");
 
-	devm_thermal_add_hwmon_sysfs(dev, tzd);
-
-	return 0;
+	return devm_thermal_add_hwmon_sysfs(dev, tzd);
 }
 
 static const struct loongson2_thermal_chip_data loongson2_thermal_ls2k1000_data = {
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/4 dt-bindings: thermal: loongson,ls2k-thermal: Add Loongson-2K0500 compaible

In reply to Binbin Zhou

The thermal on the Loongson-2K0500 shares the design with the Loongson-2K1000. Define corresponding compatible string, having the loongson,ls2k1000-thermal as a fallback.

---
 .../devicetree/bindings/thermal/loongson,ls2k-thermal.yaml       | 1 +
 1 file changed, 1 insertion(+)
Documentation/devicetree/bindings/thermal/loongson,ls2k-thermal.yaml +1 -0
index b634f57cd011..9748a479dcd4 100644
@@ -20,6 +20,7 @@ properties:
           - loongson,ls2k1000-thermal
       - items:
           - enum:
+              - loongson,ls2k0500-thermal
               - loongson,ls2k2000-thermal
           - const: loongson,ls2k1000-thermal
 
Signature · 1 line
2.43.0
maintainer Acked-by on v1 2/4
#3 lore raw

Re: [PATCH 2/4] dt-bindings: thermal: loongson,ls2k-thermal: Add Loongson-2K0500 compaible

Quoted 9 lines · Binbin Zhou

The thermal on the Loongson-2K0500 shares the design with the Loongson-2K1000. Define corresponding compatible string, having the loongson,ls2k1000-thermal as a fallback.

---
 .../devicetree/bindings/thermal/loongson,ls2k-thermal.yaml       | 1 +
 1 file changed, 1 insertion(+)
#4 lore raw

PATCH 3/4 dt-bindings: thermal: loongson,ls2k-thermal: Fix incorrect compatible definition

In reply to Binbin Zhou

The temperature output register of the Loongson-2K2000 is defined in the chip configuration domain, which is different from the Loongson-2K1000, so it can't be fallbacked.

We need to use two groups of registers to describe it: the first group is the high and low temperature threshold setting register; the second group is the temperature output register.

It is true that this fix will cause ABI corruption, but it is necessary otherwise the Loongson-2K2000 temperature sensor will not work properly.

Fixes 72684d99a854 ("thermal: dt-bindings: add loongson-2 thermal")
Cc Yinbo Zhu <[email protected]>
---
 .../thermal/loongson,ls2k-thermal.yaml        | 22 +++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
Documentation/devicetree/bindings/thermal/loongson,ls2k-thermal.yaml +20 -2
index 9748a479dcd4..a25b42702788 100644
@@ -18,14 +18,15 @@ properties:
     oneOf:
       - enum:
           - loongson,ls2k1000-thermal
+          - loongson,ls2k2000-thermal
       - items:
           - enum:
               - loongson,ls2k0500-thermal
-              - loongson,ls2k2000-thermal
           - const: loongson,ls2k1000-thermal
 
   reg:
-    maxItems: 1
+    minItems: 1
+    maxItems: 2
 
   interrupts:
     maxItems: 1
@@ -41,6 +42,23 @@ required:
 
 unevaluatedProperties: false
 
+if:
+  properties:
+    compatible:
+      contains:
+        enum:
+          - loongson,ls2k2000-thermal
+
+then:
+  properties:
+    reg:
+      maxItems: 2
+
+else:
+  properties:
+    reg:
+      maxItems: 1
+
 examples:
   - |
     #include <dt-bindings/interrupt-controller/irq.h>
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH 3/4] dt-bindings: thermal: loongson,ls2k-thermal: Fix incorrect compatible definition

Quoted 45 lines · Binbin Zhou

The temperature output register of the Loongson-2K2000 is defined in the chip configuration domain, which is different from the Loongson-2K1000, so it can't be fallbacked.

We need to use two groups of registers to describe it: the first group is the high and low temperature threshold setting register; the second group is the temperature output register.

It is true that this fix will cause ABI corruption, but it is necessary otherwise the Loongson-2K2000 temperature sensor will not work properly.

Fixes 72684d99a854 ("thermal: dt-bindings: add loongson-2 thermal")
Cc Yinbo Zhu <[email protected]>
---
 .../thermal/loongson,ls2k-thermal.yaml        | 22 +++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
Documentation/devicetree/bindings/thermal/loongson,ls2k-thermal.yaml +4 -2
index 9748a479dcd4..a25b42702788 100644
@@ -18,14 +18,15 @@ properties:
     oneOf:
       - enum:
           - loongson,ls2k1000-thermal
+          - loongson,ls2k2000-thermal
       - items:
           - enum:
               - loongson,ls2k0500-thermal
-              - loongson,ls2k2000-thermal
           - const: loongson,ls2k1000-thermal
 
   reg:
-    maxItems: 1
+    minItems: 1
+    maxItems: 2
 
   interrupts:
     maxItems: 1
@@ -41,6 +42,23 @@ required:
 
 unevaluatedProperties: false
 
+if:

Documentation/devicetree/bindings/thermal/loongson,ls2k-thermal.yaml required: · line 55

Quoted 10 lines
Documentation/devicetree/bindings/thermal/loongson,ls2k-thermal.yaml +10 -0
+  properties:
+    compatible:
+      contains:
+        enum:
+          - loongson,ls2k2000-thermal
+
+then:
+  properties:
+    reg:
+      maxItems: 2

You need a minItems: 2 here also, so that providing only the original reg is not allowed anymore.

Cheers,
Conor.

Quoted 12 lines
Documentation/devicetree/bindings/thermal/loongson,ls2k-thermal.yaml +6 -0
+
+else:
+  properties:
+    reg:
+      maxItems: 1
+
 examples:
   - |
     #include <dt-bindings/interrupt-controller/irq.h>
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH 3/4] dt-bindings: thermal: loongson,ls2k-thermal: Fix incorrect compatible definition

Quoted 46 lines · Conor Dooley
Quoted 44 lines · Binbin Zhou

The temperature output register of the Loongson-2K2000 is defined in the chip configuration domain, which is different from the Loongson-2K1000, so it can't be fallbacked.

We need to use two groups of registers to describe it: the first group is the high and low temperature threshold setting register; the second group is the temperature output register.

It is true that this fix will cause ABI corruption, but it is necessary otherwise the Loongson-2K2000 temperature sensor will not work properly.

Fixes 72684d99a854 ("thermal: dt-bindings: add loongson-2 thermal")
Cc Yinbo Zhu <[email protected]>
---
 .../thermal/loongson,ls2k-thermal.yaml        | 22 +++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
Documentation/devicetree/bindings/thermal/loongson,ls2k-thermal.yaml +3 -2
index 9748a479dcd4..a25b42702788 100644
@@ -18,14 +18,15 @@ properties:
     oneOf:
       - enum:
           - loongson,ls2k1000-thermal
+          - loongson,ls2k2000-thermal
       - items:
           - enum:
               - loongson,ls2k0500-thermal
-              - loongson,ls2k2000-thermal
           - const: loongson,ls2k1000-thermal
 
   reg:
-    maxItems: 1
+    minItems: 1
+    maxItems: 2
 
   interrupts:
     maxItems: 1
@@ -41,6 +42,23 @@ required:
 
 unevaluatedProperties: false
 
Quoted 14 lines

Documentation/devicetree/bindings/thermal/loongson,ls2k-thermal.yaml required: · line 55

Quoted 11 lines
Documentation/devicetree/bindings/thermal/loongson,ls2k-thermal.yaml +11 -0
+if:
+  properties:
+    compatible:
+      contains:
+        enum:
+          - loongson,ls2k2000-thermal
+
+then:
+  properties:
+    reg:
+      maxItems: 2

You need a minItems: 2 here also, so that providing only the original reg is not allowed anymore.

Hi Conor:

Indeed, it is necessary that the whole description is more rigorous. I'll fix it in the next version.

Thanks.
Binbin

Quoted 16 lines

Cheers,
Conor.

Quoted 12 lines
Documentation/devicetree/bindings/thermal/loongson,ls2k-thermal.yaml +6 -1
+
+else:
+  properties:
+    reg:
+      maxItems: 1
+
 examples:
   - |
     #include <dt-bindings/interrupt-controller/irq.h>
--

2.43.0

#7 lore raw

PATCH 4/4 thermal: loongson2: Add Loongson-2K2000 support

In reply to Binbin Zhou

The Loongson-2K2000 and Loongson-2K1000 have similar thermal sensors, except that the temperature is read differently.

In particular, the temperature output registers of the Loongson-2K2000 are defined in the chip configuration domain and are read in a different way.

---
 drivers/thermal/loongson2_thermal.c | 50 +++++++++++++++++++++++------
 1 file changed, 40 insertions(+), 10 deletions(-)
drivers/thermal/loongson2_thermal.c +40 -10
index d77d829c8b55..d803b6bc35b7 100644
@@ -29,12 +29,20 @@
 					 LOONGSON2_THSENS_INT_HIGH)
 #define LOONGSON2_THSENS_OUT_MASK	0xFF
 
+/*
+ * This flag is used to indicate the temperature reading
+ * method of the Loongson-2K2000
+ */
+#define LS2K2000_THSENS_OUT_FLAG	BIT(0)
+
 struct loongson2_thermal_chip_data {
 	unsigned int thermal_sensor_sel;
+	unsigned int flags;
 };
 
 struct loongson2_thermal_data {
-	void __iomem *regs;
+	void __iomem *ctrl_reg;
+	void __iomem *temp_reg;
 	const struct loongson2_thermal_chip_data *chip_data;
 };
 
@@ -48,7 +56,7 @@ static void loongson2_set_ctrl_regs(struct loongson2_thermal_data *data,
 
 	reg_ctrl = ctrl_data + HECTO;
 	reg_ctrl |= enable ? 0x100 : 0;
-	writew(reg_ctrl, data->regs + ctrl_reg + reg_off);
+	writew(reg_ctrl, data->ctrl_reg + ctrl_reg + reg_off);
 }
 
 static int loongson2_thermal_set(struct loongson2_thermal_data *data,
@@ -65,11 +73,16 @@ static int loongson2_thermal_set(struct loongson2_thermal_data *data,
 
 static int loongson2_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
 {
-	u32 reg_val;
+	int val;
 	struct loongson2_thermal_data *data = thermal_zone_device_priv(tz);
 
-	reg_val = readl(data->regs + LOONGSON2_THSENS_OUT_REG);
-	*temp = ((reg_val & LOONGSON2_THSENS_OUT_MASK) - HECTO) * KILO;
+	if (data->chip_data->flags) {
+		val = readl(data->temp_reg);
+		*temp = ((val & 0xffff) * 820 / 0x4000 - 311) * KILO;
+	} else {
+		val = readl(data->ctrl_reg + LOONGSON2_THSENS_OUT_REG);
+		*temp = ((val & LOONGSON2_THSENS_OUT_MASK) - HECTO) * KILO;
+	}
 
 	return 0;
 }
@@ -79,7 +92,7 @@ static irqreturn_t loongson2_thermal_irq_thread(int irq, void *dev)
 	struct thermal_zone_device *tzd = dev;
 	struct loongson2_thermal_data *data = thermal_zone_device_priv(tzd);
 
-	writeb(LOONGSON2_THSENS_INT_EN, data->regs + LOONGSON2_THSENS_STATUS_REG);
+	writeb(LOONGSON2_THSENS_INT_EN, data->ctrl_reg + LOONGSON2_THSENS_STATUS_REG);
 
 	thermal_zone_device_update(tzd, THERMAL_EVENT_UNSPECIFIED);
 
@@ -112,15 +125,22 @@ static int loongson2_thermal_probe(struct platform_device *pdev)
 
 	data->chip_data = device_get_match_data(dev);
 
-	data->regs = devm_platform_ioremap_resource(pdev, 0);
-	if (IS_ERR(data->regs))
-		return PTR_ERR(data->regs);
+	data->ctrl_reg = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(data->ctrl_reg))
+		return PTR_ERR(data->ctrl_reg);
+
+	/* The temperature output register is separate for Loongson-2K2000 */
+	if (data->chip_data->flags) {
+		data->temp_reg = devm_platform_ioremap_resource(pdev, 1);
+		if (IS_ERR(data->temp_reg))
+			return PTR_ERR(data->temp_reg);
+	}
 
 	irq = platform_get_irq(pdev, 0);
 	if (irq < 0)
 		return irq;
 
-	writeb(LOONGSON2_THSENS_INT_EN, data->regs + LOONGSON2_THSENS_STATUS_REG);
+	writeb(LOONGSON2_THSENS_INT_EN, data->ctrl_reg + LOONGSON2_THSENS_STATUS_REG);
 
 	loongson2_thermal_set(data, 0, 0, false);
 
@@ -147,6 +167,12 @@ static int loongson2_thermal_probe(struct platform_device *pdev)
 
 static const struct loongson2_thermal_chip_data loongson2_thermal_ls2k1000_data = {
 	.thermal_sensor_sel = 0,
+	.flags = 0,
+};
+
+static const struct loongson2_thermal_chip_data loongson2_thermal_ls2k2000_data = {
+	.thermal_sensor_sel = 0,
+	.flags = LS2K2000_THSENS_OUT_FLAG,
 };
 
 static const struct of_device_id of_loongson2_thermal_match[] = {
@@ -154,6 +180,10 @@ static const struct of_device_id of_loongson2_thermal_match[] = {
 		.compatible = "loongson,ls2k1000-thermal",
 		.data = &loongson2_thermal_ls2k1000_data,
 	},
+	{
+		.compatible = "loongson,ls2k2000-thermal",
+		.data = &loongson2_thermal_ls2k2000_data,
+	},
 	{ /* end */ }
 };
 MODULE_DEVICE_TABLE(of, of_loongson2_thermal_match);
Signature · 1 line
2.43.0