thermal/drivers/loongson2: Fix trip programming and add Loongson-2K0300 support
Subsystems (5)
v9 cover thermal/drivers/loongson2: Fix trip programming and add Loongson-2K0300 support
Hi all:
This series fixes trip-point handling in the Loongson-2 thermal driver, refactors the driver around per-SoC chip data, and adds support for the Loongson-2K0300 thermal sensor.
The first three patches are fixes and are marked for stable:
- Patch 1 fixes signed division when converting negative milli-degree trip points to degrees Celsius.
- Patch 2 fixes the arguments to clamp() so trip points are limited to the supported sensor range.
- Patch 3 fixes Loongson-2K2000 trip-point programming by adding a per-SoC register callback and handling the split HI_GATE register.
Patches 4-5 are cleanups and preparation work:
- Patch 4 removes the unnecessary multi-sensor registration loop.
- Patch 5 moves the per-SoC ops and hardware initialization into chip data.
Patch 6 adds the Loongson-2K0300 DT binding, and patch 7 adds driver support for the Loongson-2K0300 thermal sensor.
The patchset have been validated on the Loongson-2K series development boards.
Thanks.
Binbin
=========
V9:
- Add Reviewed-by from Huacai, thanks;
- Fix issues reported by Sashiko AI:
Patch (3/7):
- extract gate_lo with FIELD_GET(GENMASK(7, 0), gate) before FIELD_PREP()
to avoid the __BF_FIELD_CHECK_MASK() BUILD_BUG_ON();
- disable INTEN while updating the 16-bit gate to prevent a torn {old_hi, new_lo} threshold from latching a spurious interrupt.
Patch (7/7):
- add depends on MFD_SYSCON;
- delay after writing LS2K0300_THSENS_CFG_REG to avoid misdetecting old fuse on a premature OUT_REG read.
Link to V8:
https://lore.kernel.org/all/[email protected]/
V8:
Patch (1/7):
- Use MILLIDEGREE_PER_DEGREE macro instead of explicitly casting MILLI;
- Split patch-4 into two parts: the bitfields section will be merged into patch-3, and the remaining changes will be merged into patch-6 as preparation for introducing the new SoC.
Link to V7:
https://lore.kernel.org/all/[email protected]/
V7:
Patch (1/8):
- New patch;
- Fix negative temperature conversion in set_trips;
Patch (3/8):
- Use writew()/writeb() in loongson2_2k2000_set_trips_regs;
Patch (4/8):
- Rewite the thermal sensor registration loop to clearer;
Patch (5/8):
- Fix loognson2_thermal_set_trips typo;
Patch (8/8):
- Read CHIP_ID1 only when EXTERN_ID is set;
- Simplify the Internal ID calculation;
- Add the necessary comments.
Link to V6:
https://lore.kernel.org/all/[email protected]/
V6:
- Rebase on linux-pm/thermal;
Patch (2/7):
- New patch;
- Fix incorrect clamp() argument order for thermal trip points;
Patch (3/7):
- New patch;
- Fix Loongson-2K2000 trip point programming by adding a per-SoC set_trips_regs callback and handling the split HI_GATE registers;
Patch (4/7):
- New patch;
- cleanup: use GENMASK/FIELD_* helpers, add named constants, and factor common trip point setup;
Patch (5/7):
- New patch;
- refactor: move thermal_zone_device_ops into chip_data and replace the Loongson-2K2000 flag special case with an optional init_extend_resource callback;
Patch (7/7):
- Updated the 10-bit output to 11-bit in commit message;
- Temporarily remove the implementation of set_trips;
- Remove unnecessary header files;
- Rewrite get_temp, separating read_calib and detect_old_fuse into new functions to be called during the probe;
- Use macros to define the relevant numbers.
Link to V5:
https://lore.kernel.org/all/[email protected]/#t
V5:
- Rebase on linux-pm/thermal;
- Drop patch-3 for it was applied.
Link to V4:
https://lore.kernel.org/all/[email protected]/
V4:
- Rebase on linux-mfd/for-mfd-next;
Patch (3/5):
- Remove the operation that adds compatible to the select node.
Link to V3:
https://lore.kernel.org/all/[email protected]/
V3:
Patch (2/5):
- New patch for moving thermal_ops to chip_data;
Patch (3/5):
- Correct spelling errors;
Patch (4/5):
- Add Acked-by from Conor, thanks;
Patch (5/5):
- Keep loongson2_thermal_data->chip_data as const, and use data->flags as the private data.
- Renmae LOONGSON2_THSENS_OUT_10B_MASK -> LOONGSON2_THSENS_OUT_11B_MASK;
Link to V2:
https://lore.kernel.org/all/[email protected]/
V2:
Patch (1/4):
- New patch form Sashiko;
Patch (2/4):
- New patch for adding chipid syscon compatible;
Patch (3/4):
- Use loongson,chipid phandle replace reg property;
Patch (3/4):
- Refact calib_offset calculate routing;
- Add LS2K0300_OLD_FUSE_FLAG flag.
Link to V1:
https://lore.kernel.org/all/[email protected]/
Binbin Zhou (7):
thermal/drivers/loongson2: Fix negative trip conversion in set_trips thermal/drivers/loongson2: Fix clamp() usage for thermal trip points thermal/drivers/loongson2: Fix Loongson-2K2000 trip point programming thermal/drivers/loongson2: Simplify thermal sensor registration thermal/drivers/loongson2: Move per-SoC ops and init into chip data
dt-bindings: thermal: loongson,ls2k-thermal: Add compatible for
Loongson-2K0300
thermal/drivers/loongson2: Add Loongson-2K0300 support
.../thermal/loongson,ls2k-thermal.yaml | 73 +++- drivers/thermal/Kconfig | 1 + drivers/thermal/loongson2_thermal.c | 383 ++++++++++++++---- 3 files changed, 355 insertions(+), 102 deletions(-)
base-commit: 4358090906b01806ee483229e8d7163b806bd116
Signature · 1 line
2.52.0
PATCH v9 1/7 thermal/drivers/loongson2: Fix negative trip conversion in set_trips
The MILLI is defined as an unsigned long constant. When the temperatures passed to loongson2_thermal_set_trips() are negative, the expression low / MILLI triggers implicit conversion of the signed operand to unsigned long, and the division is then carried out in unsigned arithmetic.
For example, with a low trip of -40000 (milli-degree Celsius):
-40000 / 1000UL = 18446744073709511 /* unsigned long */
After truncation to the 32-bit register field this becomes 1271310379, instead of the expected 60 (i.e. -40 + HECTO). Writing such bogus value into the trip register leaves most of the upper bits set, which may hit status/trigger bits and cause spurious thermal interrupts.
Use MILLIDEGREE_PER_DEGREE instead of MILLI so that the division is performed in signed arithmetic.
--- drivers/thermal/loongson2_thermal.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/thermal/loongson2_thermal.c +2 -1
@@ -108,7 +108,8 @@ static int loongson2_thermal_set_trips(struct thermal_zone_device *tz, int low, {struct loongson2_thermal_data *data =thermal_zone_device_priv (tz); -return loongson2_thermal_set (data, low/MILLI , high/MILLI , true); +return loongson2_thermal_set (data, low /MILLIDEGREE_PER_DEGREE , + high /MILLIDEGREE_PER_DEGREE , true); }static const struct thermal_zone_device_ops loongson2_2k1000_of_thermal_ops = {
Signature · 1 line
2.52.0
PATCH v9 2/7 thermal/drivers/loongson2: Fix clamp() usage for thermal trip points
In reply to Binbin Zhouclamp() takes the value to clamp as its first argument, followed by the minimum and maximum bounds. The current code passes the -40 and 125 limits as the value instead, so the trip thresholds are not clamped to the supported sensor range.
Correct the arguments so that low and high are clamped to [-40, 125].
--- drivers/thermal/loongson2_thermal.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/thermal/loongson2_thermal.c +2 -2
@@ -62,10 +62,10 @@ static int loongson2_thermal_set(struct loongson2_thermal_data *data, int low, int high, bool enable) {/* Set low temperature threshold */ - loongson2_set_ctrl_regs(data, clamp(-40, low, high), true, enable); + loongson2_set_ctrl_regs(data, clamp(low, -40, 125), true, enable);/* Set high temperature threshold */ -loongson2_set_ctrl_regs (data,clamp (125 , low, high), false, enable); +loongson2_set_ctrl_regs (data,clamp (high,-40 ,125 ), false, enable);return 0 ; }
Signature · 1 line
2.52.0
PATCH v9 3/7 thermal/drivers/loongson2: Fix Loongson-2K2000 trip point programming
In reply to Binbin ZhouThe Loongson-2K2000 splits its 16-bit gate value across the control register (low byte) and a dedicated HI_GATE register (high byte), and derives the gate value from the target temperature with a different formula than the Loongson-2K1000.
Add a per-SoC set_trips_regs callback and implement the Loongson-2K2000 variant alongside the existing Loongson-2K1000 one.
--- drivers/thermal/loongson2_thermal.c | 122 +++++++++++++++++++++------- 1 file changed, 93 insertions(+), 29 deletions(-)
drivers/thermal/loongson2_thermal.c +93 -29
@@ -5,6 +5,7 @@* Copyright (C) 2022-2023 Loongson Technology Corporation Limited */ +#include <linux/bitfield.h> #include <linux/interrupt.h> #include <linux/io.h> #include <linux/minmax.h>
@@ -25,9 +26,30 @@#define LOONGSON2_THSENS_INT_LO BIT(0)#define LOONGSON2_THSENS_INT_HIGH BIT(1) -#define LOONGSON2_THSENS_INT_EN (LOONGSON2_THSENS_INT_LO | \ +#define LOONGSON2_THSENS_INT_CLR (LOONGSON2_THSENS_INT_LO | \ LOONGSON2_THSENS_INT_HIGH) -#define LOONGSON2_THSENS_OUT_MASK 0xFF + +#define LOONGSON2_THSENS_CTRL_GATE_MASK GENMASK(7, 0) +#define LOONGSON2_THSENS_CTRL_INTEN BIT(8) +#define LOONGSON2_THSENS_CTRL_SEL_MASK GENMASK(11, 10) + +#define LOONGSON2_THSENS_OUT_8B_MASK GENMASK(7, 0) +#define LOONGSON2_THSENS_OUT_16B_MASK GENMASK(15, 0) + +#define LOONGSON2_LOWEST_VALID_TEMP (-40000) +#define LOONGSON2_HIGHEST_VALID_TEMP (125000) + +/* Loongson-2K2000-Specific Registers */ +#define LS2K2000_THSENS_HI_GATE_HI_REG 0x18 +#define LS2K2000_THSENS_HI_GATE_LOW_REG 0x1c + +#define LS2K2000_TEMP_MUL (820) +#define LS2K2000_TEMP_DIV (0x4000) +#define LS2K2000_TEMP_OFFSET (311) + +/* 16-bit gate value is split into low and high bytes */ +#define LS2K2000_GATE_LO_MASK GENMASK(7, 0) +#define LS2K2000_GATE_HI_MASK GENMASK(15, 8)/* * This flag is used to indicate the temperature reading
@@ -35,9 +57,13 @@*/ #define LS2K2000_THSENS_OUT_FLAG BIT(0) +struct loongson2_thermal_data ; +struct loongson2_thermal_chip_data {unsigned int thermal_sensor_sel;unsigned int flags; +void (*set_trips_regs)(struct loongson2_thermal_data *data, +int temp,bool low,bool enable); };struct loongson2_thermal_data {
@@ -46,58 +72,93 @@ struct loongson2_thermal_data {const struct loongson2_thermal_chip_data *chip_data; }; -static void loongson2_set_ctrl_regs (struct loongson2_thermal_data *data, -int ctrl_data,bool low,bool enable) +static void loongson2_thermal_set (struct loongson2_thermal_data *data, +int low,int high,bool enable) { -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->ctrl_reg + ctrl_reg + reg_off); -} + low =clamp (low,LOONGSON2_LOWEST_VALID_TEMP ,LOONGSON2_HIGHEST_VALID_TEMP ); + high =clamp (high,LOONGSON2_LOWEST_VALID_TEMP ,LOONGSON2_HIGHEST_VALID_TEMP ); -static int loongson2_thermal_set (struct loongson2_thermal_data *data, -int low,int high,bool enable) -{/* Set low temperature threshold */ -loongson2_set_ctrl_regs (data,clamp (low,-40 ,125 ), true, enable); + data->chip_data->set_trips_regs (data, low /MILLIDEGREE_PER_DEGREE , true, enable);/* Set high temperature threshold */ -loongson2_set_ctrl_regs (data,clamp (high,-40 ,125 ), false, enable); - -return 0 ; + data->chip_data->set_trips_regs (data, high /MILLIDEGREE_PER_DEGREE , false, enable); }static int loongson2_2k1000_get_temp (struct thermal_zone_device *tz,int *temp) { -int val;struct loongson2_thermal_data *data =thermal_zone_device_priv (tz); +int val; - val =readl (data->ctrl_reg +LOONGSON2_THSENS_OUT_REG ); - *temp = ((val &LOONGSON2_THSENS_OUT_MASK ) -HECTO ) *KILO ; + val =FIELD_GET (LOONGSON2_THSENS_OUT_8B_MASK , +readl (data->ctrl_reg +LOONGSON2_THSENS_OUT_REG )); + *temp = (val -HECTO ) *KILO ;return 0 ; } +static void loongson2_2k1000_set_trips_regs (struct loongson2_thermal_data *data, +int temp,bool low,bool enable) +{ +u32 sel = data->chip_data->thermal_sensor_sel; +int ctrl_reg; +u16 val; + + ctrl_reg = low ?LOONGSON2_THSENS_CTRL_LOW_REG :LOONGSON2_THSENS_CTRL_HI_REG ; + val =FIELD_PREP (LOONGSON2_THSENS_CTRL_GATE_MASK , temp +HECTO ) | +FIELD_PREP (LOONGSON2_THSENS_CTRL_INTEN , enable) | +FIELD_PREP (LOONGSON2_THSENS_CTRL_SEL_MASK , sel); +writew (val, data->ctrl_reg + ctrl_reg + sel *2 ); +} +static int loongson2_2k2000_get_temp (struct thermal_zone_device *tz,int *temp) { -int val;struct loongson2_thermal_data *data =thermal_zone_device_priv (tz); +int val; - val =readl (data->temp_reg); - *temp = ((val &0xffff ) *820 /0x4000 -311 ) *KILO ; + val =FIELD_GET (LOONGSON2_THSENS_OUT_16B_MASK ,readl (data->temp_reg)); + *temp = (val *LS2K2000_TEMP_MUL /LS2K2000_TEMP_DIV -LS2K2000_TEMP_OFFSET ) *KILO ;return 0 ; } +static void loongson2_2k2000_set_trips_regs (struct loongson2_thermal_data *data, +int temp,bool low,bool enable) +{ +u32 sel = data->chip_data->thermal_sensor_sel; +u32 hi_reg, ctrl_reg; +u8 gate_lo, gate_hi; +int val, gate; + + gate = (temp +LS2K2000_TEMP_OFFSET ) *LS2K2000_TEMP_DIV /LS2K2000_TEMP_MUL ; + gate_lo =FIELD_GET (LS2K2000_GATE_LO_MASK , gate); + gate_hi =FIELD_GET (LS2K2000_GATE_HI_MASK , gate); + + ctrl_reg = low ?LOONGSON2_THSENS_CTRL_LOW_REG :LOONGSON2_THSENS_CTRL_HI_REG ; + hi_reg = low ?LS2K2000_THSENS_HI_GATE_LOW_REG :LS2K2000_THSENS_HI_GATE_HI_REG ; + +/* Disable INTEN first so a torn 16-bit gate is never compared */ + val =FIELD_PREP (LOONGSON2_THSENS_CTRL_GATE_MASK , gate_lo) | +FIELD_PREP (LOONGSON2_THSENS_CTRL_INTEN ,0 ) | +FIELD_PREP (LOONGSON2_THSENS_CTRL_SEL_MASK , sel); +writew (val, data->ctrl_reg + ctrl_reg + sel *2 ); + +/* High byte of the gate value goes to the low byte of the HI_GATE register */ +writeb (gate_hi, data->ctrl_reg + hi_reg + sel); + +/* Re-enable with the complete {gate_hi, gate_lo} in place */ + val =FIELD_PREP (LOONGSON2_THSENS_CTRL_GATE_MASK , gate_lo) | +FIELD_PREP (LOONGSON2_THSENS_CTRL_INTEN , enable) | +FIELD_PREP (LOONGSON2_THSENS_CTRL_SEL_MASK , sel); +writew (val, data->ctrl_reg + ctrl_reg + sel *2 ); +} +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->ctrl_reg +LOONGSON2_THSENS_STATUS_REG ); +writeb (LOONGSON2_THSENS_INT_CLR , data->ctrl_reg +LOONGSON2_THSENS_STATUS_REG );thermal_zone_device_update (tzd,THERMAL_EVENT_UNSPECIFIED );
@@ -108,8 +169,9 @@ static int loongson2_thermal_set_trips(struct thermal_zone_device *tz, int low, {struct loongson2_thermal_data *data =thermal_zone_device_priv (tz); -return loongson2_thermal_set (data, low /MILLIDEGREE_PER_DEGREE , - high /MILLIDEGREE_PER_DEGREE , true); +loongson2_thermal_set (data, low, high, true); + +return 0 ; }static const struct thermal_zone_device_ops loongson2_2k1000_of_thermal_ops = {
@@ -155,7 +217,7 @@ static int loongson2_thermal_probe(struct platform_device *pdev)if (irq <0 )return irq; -writeb (LOONGSON2_THSENS_INT_EN , data->ctrl_reg +LOONGSON2_THSENS_STATUS_REG ); +writeb (LOONGSON2_THSENS_INT_CLR , data->ctrl_reg +LOONGSON2_THSENS_STATUS_REG );loongson2_thermal_set (data,0 ,0 , false);
@@ -184,11 +246,13 @@ 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 , + .set_trips_regs = loongson2_2k1000_set_trips_regs, };static const struct loongson2_thermal_chip_data loongson2_thermal_ls2k2000_data = { .thermal_sensor_sel =0 , .flags =LS2K2000_THSENS_OUT_FLAG , + .set_trips_regs = loongson2_2k2000_set_trips_regs, };static const struct of_device_id of_loongson2_thermal_match[] = {
Signature · 1 line
2.52.0
PATCH v9 4/7 thermal/drivers/loongson2: Simplify thermal sensor registration
In reply to Binbin ZhouThe Loongson-2K SoCs expose a single on-chip thermal sensor, so the loop over LOONGSON2_MAX_SENSOR_SEL_NUM only ever registers one thermal zone. The additional iterations never succeed and the surrounding error handling ends up relying on the value of tzd after the loop, which is fragile and hard to follow.
Register sensor index 0 directly and fail the probe if that single registration fails.
--- drivers/thermal/loongson2_thermal.c | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-)
drivers/thermal/loongson2_thermal.c +4 -14
@@ -17,8 +17,6 @@#include "thermal_hwmon.h" -#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
@@ -190,7 +188,7 @@ static int loongson2_thermal_probe(struct platform_device *pdev)struct device *dev = &pdev->dev;struct loongson2_thermal_data *data;struct thermal_zone_device *tzd; -int ret, irq, i; +int ret, irq; data =devm_kzalloc (dev,sizeof (*data),GFP_KERNEL );if (!data)
@@ -221,17 +219,9 @@ static int loongson2_thermal_probe(struct platform_device *pdev)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, thermal_ops); - -if (!IS_ERR (tzd)) -break ; - -if (PTR_ERR (tzd) != -ENODEV ) -continue ; - -return dev_err_probe (dev,PTR_ERR (tzd),"failed to register" ); - } + tzd =devm_thermal_of_zone_register (dev,0 , data, thermal_ops); +if (IS_ERR (tzd)) +return dev_err_probe (dev,PTR_ERR (tzd),"failed to register sensor" ); ret =devm_request_threaded_irq (dev, irq,NULL , loongson2_thermal_irq_thread,IRQF_ONESHOT ,"loongson2_thermal" , tzd);
Signature · 1 line
2.52.0
PATCH v9 5/7 thermal/drivers/loongson2: Move per-SoC ops and init into chip data
In reply to Binbin ZhouInstead of using a flags field to select the thermal_zone_device_ops in probe and handling the Loongson-2K2000 extended register mapping inline, store the ops pointer and an optional hw_init() callback in struct loongson2_thermal_chip_data. Each SoC now provides its own hw_init() that performs the common initialization and, for Loongson-2K2000, maps the extended temperature register.
Rename the second mapped resource from temp_reg to extend_reg to better reflect its purpose. Also rename the per-SoC functions with consistent ls2k1000/ls2k2000 prefixes and introduce a common set_trips callback.
This makes adding support for new SoCs with different register layouts easier.
No functional change intended.
--- drivers/thermal/loongson2_thermal.c | 138 +++++++++++++++------------- 1 file changed, 74 insertions(+), 64 deletions(-)
drivers/thermal/loongson2_thermal.c +74 -64
@@ -49,29 +49,24 @@#define LS2K2000_GATE_LO_MASK GENMASK(7, 0)#define LS2K2000_GATE_HI_MASK GENMASK(15, 8) -/* -* This flag is used to indicate the temperature reading -* method of the Loongson-2K2000 -*/ -#define LS2K2000_THSENS_OUT_FLAG BIT(0) -struct loongson2_thermal_data ;struct loongson2_thermal_chip_data {unsigned int thermal_sensor_sel; -unsigned int flags; +const struct thermal_zone_device_ops *thermal_ops;void (*set_trips_regs)(struct loongson2_thermal_data *data,int temp,bool low,bool enable); +int (*hw_init)(struct platform_device *pdev,struct loongson2_thermal_data *data); };struct loongson2_thermal_data {void __iomem *ctrl_reg; -void __iomem *temp_reg; +void __iomem *extend_reg;const struct loongson2_thermal_chip_data *chip_data; }; -static void loongson2_thermal_set (struct loongson2_thermal_data *data, -int low,int high,bool enable) +static void loongson2_thermal_set_trip_temp (struct loongson2_thermal_data *data, +int low,int high,bool enable) { low =clamp (low,LOONGSON2_LOWEST_VALID_TEMP ,LOONGSON2_HIGHEST_VALID_TEMP ); high =clamp (high,LOONGSON2_LOWEST_VALID_TEMP ,LOONGSON2_HIGHEST_VALID_TEMP );
@@ -83,7 +78,16 @@ static void loongson2_thermal_set(struct loongson2_thermal_data *data, data->chip_data->set_trips_regs (data, high /MILLIDEGREE_PER_DEGREE , false, enable); } -static int loongson2_2k1000_get_temp (struct thermal_zone_device *tz,int *temp) +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); + +loongson2_thermal_set_trip_temp (data, low, high, true); + +return 0 ; +} + +static int ls2k1000_thermal_get_temp (struct thermal_zone_device *tz,int *temp) {struct loongson2_thermal_data *data =thermal_zone_device_priv (tz);int val;
@@ -95,7 +99,7 @@ static int loongson2_2k1000_get_temp(struct thermal_zone_device *tz, int *temp)return 0 ; } -static void loongson2_2k1000_set_trips_regs (struct loongson2_thermal_data *data, +static void ls2k1000_thermal_set_trips_regs (struct loongson2_thermal_data *data,int temp,bool low,bool enable) {u32 sel = data->chip_data->thermal_sensor_sel;
@@ -109,18 +113,27 @@ static void loongson2_2k1000_set_trips_regs(struct loongson2_thermal_data *data,writew (val, data->ctrl_reg + ctrl_reg + sel *2 ); } -static int loongson2_2k2000_get_temp (struct thermal_zone_device *tz,int *temp) +static int ls2k1000_thermal_hw_init (struct platform_device *pdev, +struct loongson2_thermal_data *data) +{ +writeb (LOONGSON2_THSENS_INT_CLR , data->ctrl_reg +LOONGSON2_THSENS_STATUS_REG ); +loongson2_thermal_set_trip_temp (data,0 ,0 , false); + +return 0 ; +} + +static int ls2k2000_thermal_get_temp (struct thermal_zone_device *tz,int *temp) {struct loongson2_thermal_data *data =thermal_zone_device_priv (tz);int val; - val =FIELD_GET (LOONGSON2_THSENS_OUT_16B_MASK ,readl (data->temp_reg)); + val =FIELD_GET (LOONGSON2_THSENS_OUT_16B_MASK ,readl (data->extend_reg)); *temp = (val *LS2K2000_TEMP_MUL /LS2K2000_TEMP_DIV -LS2K2000_TEMP_OFFSET ) *KILO ;return 0 ; } -static void loongson2_2k2000_set_trips_regs (struct loongson2_thermal_data *data, +static void ls2k2000_thermal_set_trips_regs (struct loongson2_thermal_data *data,int temp,bool low,bool enable) {u32 sel = data->chip_data->thermal_sensor_sel;
@@ -151,42 +164,60 @@ static void loongson2_2k2000_set_trips_regs(struct loongson2_thermal_data *data,writew (val, data->ctrl_reg + ctrl_reg + sel *2 ); } -static irqreturn_t loongson2_thermal_irq_thread (int irq,void *dev) +static int ls2k2000_thermal_hw_init (struct platform_device *pdev, +struct loongson2_thermal_data *data) { -struct thermal_zone_device *tzd = dev; -struct loongson2_thermal_data *data =thermal_zone_device_priv (tzd); +/* The temperature output register is separate for Loongson-2K2000 */ + data->extend_reg =devm_platform_ioremap_resource (pdev,1 ); +if (IS_ERR (data->extend_reg)) +return PTR_ERR (data->extend_reg);writeb (LOONGSON2_THSENS_INT_CLR , data->ctrl_reg +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) -{ -struct loongson2_thermal_data *data =thermal_zone_device_priv (tz); - -loongson2_thermal_set (data, low, high, true); +loongson2_thermal_set_trip_temp (data,0 ,0 , false);return 0 ; } -static const struct thermal_zone_device_ops loongson2_2k1000_of_thermal_ops = { - .get_temp = loongson2_2k1000_get_temp, +static const struct thermal_zone_device_ops ls2k1000_thermal_zone_ops = { + .get_temp = ls2k1000_thermal_get_temp, .set_trips = loongson2_thermal_set_trips, }; -static const struct thermal_zone_device_ops loongson2_2k2000_of_thermal_ops = { - .get_temp = loongson2_2k2000_get_temp, +static const struct thermal_zone_device_ops ls2k2000_thermal_zone_ops = { + .get_temp = ls2k2000_thermal_get_temp, .set_trips = loongson2_thermal_set_trips, }; +static const struct loongson2_thermal_chip_data ls2k1000_thermal_chip_data = { + .thermal_sensor_sel =0 , + .thermal_ops = &ls2k1000_thermal_zone_ops, + .set_trips_regs = ls2k1000_thermal_set_trips_regs, + .hw_init = ls2k1000_thermal_hw_init, +}; + +static const struct loongson2_thermal_chip_data ls2k2000_thermal_chip_data = { + .thermal_sensor_sel =0 , + .thermal_ops = &ls2k2000_thermal_zone_ops, + .set_trips_regs = ls2k2000_thermal_set_trips_regs, + .hw_init = ls2k2000_thermal_hw_init, +}; + +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_CLR , data->ctrl_reg +LOONGSON2_THSENS_STATUS_REG ); + +thermal_zone_device_update (tzd,THERMAL_EVENT_UNSPECIFIED ); + +return IRQ_HANDLED ; +} +static int loongson2_thermal_probe (struct platform_device *pdev) { -const struct thermal_zone_device_ops *thermal_ops; -struct device *dev = &pdev->dev;struct loongson2_thermal_data *data; +struct device *dev = &pdev->dev;struct thermal_zone_device *tzd;int ret, irq;
@@ -195,31 +226,22 @@ static int loongson2_thermal_probe(struct platform_device *pdev)return -ENOMEM ; data->chip_data =device_get_match_data (dev); +if (!data->chip_data) +return -EINVAL ; 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 &LS2K2000_THSENS_OUT_FLAG ) { - data->temp_reg =devm_platform_ioremap_resource (pdev,1 ); -if (IS_ERR (data->temp_reg)) -return PTR_ERR (data->temp_reg); - - thermal_ops = &loongson2_2k2000_of_thermal_ops; - }else { - thermal_ops = &loongson2_2k1000_of_thermal_ops; - } - irq =platform_get_irq (pdev,0 );if (irq <0 )return irq; -writeb (LOONGSON2_THSENS_INT_CLR , data->ctrl_reg +LOONGSON2_THSENS_STATUS_REG ); - -loongson2_thermal_set (data,0 ,0 , false); + ret = data->chip_data->hw_init (pdev, data); +if (ret) +return ret; - tzd =devm_thermal_of_zone_register (dev,0 , data, thermal_ops); + tzd =devm_thermal_of_zone_register (dev,0 , data, data->chip_data->thermal_ops);if (IS_ERR (tzd))return dev_err_probe (dev,PTR_ERR (tzd),"failed to register sensor" );
@@ -233,26 +255,14 @@ static int loongson2_thermal_probe(struct platform_device *pdev)return 0 ; } -static const struct loongson2_thermal_chip_data loongson2_thermal_ls2k1000_data = { - .thermal_sensor_sel =0 , - .flags =0 , - .set_trips_regs = loongson2_2k1000_set_trips_regs, -}; - -static const struct loongson2_thermal_chip_data loongson2_thermal_ls2k2000_data = { - .thermal_sensor_sel =0 , - .flags =LS2K2000_THSENS_OUT_FLAG , - .set_trips_regs = loongson2_2k2000_set_trips_regs, -}; -static const struct of_device_id of_loongson2_thermal_match[] = { { .compatible ="loongson,ls2k1000-thermal" , - .data = &loongson2_thermal_ls2k1000_data, + .data = &ls2k1000_thermal_chip_data, }, { .compatible ="loongson,ls2k2000-thermal" , - .data = &loongson2_thermal_ls2k2000_data, + .data = &ls2k2000_thermal_chip_data, }, {/* end */ } };
Signature · 1 line
2.52.0
PATCH v9 6/7 dt-bindings: thermal: loongson,ls2k-thermal: Add compatible for Loongson-2K0300
In reply to Binbin ZhouAdd a new compatible string loongson,ls2k0300-thermal for the thermal sensor found on the Loongson-2K0300 SoC.
The hardware differs from existing SoCs in that it requires a phandle to a syscon node that provides the CHIP ID register, used as a compensation source in the temperature calculation.
Update the binding to make loongson,chipid required for this new compatible, and clarify the register region requirements for each variant.
--- .../thermal/loongson,ls2k-thermal.yaml | 73 ++++++++++++++----- 1 file changed, 55 insertions(+), 18 deletions(-)
Documentation/devicetree/bindings/thermal/loongson,ls2k-thermal.yaml +55 -18
@@ -10,13 +10,11 @@ maintainers: - zhanghongchen <[email protected]> - Yinbo Zhu <[email protected]> -allOf: - - $ref: /schemas/thermal/thermal-sensor.yaml# - properties: compatible: oneOf: - enum: + - loongson,ls2k0300-thermal - loongson,ls2k1000-thermal - loongson,ls2k2000-thermal - items:
@@ -34,28 +32,56 @@ properties: '#thermal-sensor-cells': const: 1 + loongson,chipid: + $ref: /schemas/types.yaml#/definitions/phandle + description: + Phandle to the syscon with the Loongson-2K0300 CHIP ID. + required: - compatible - reg - interrupts -if: - properties: - compatible: - contains: - enum: - - loongson,ls2k2000-thermal +allOf: + - $ref: /schemas/thermal/thermal-sensor.yaml# + + - if: + properties: + compatible: + contains: + const: loongson,ls2k0300-thermal + then: + properties: + reg: + items: + - description: Thermal base register region + required: + - loongson,chipid -then: - properties: - reg: - minItems: 2 - maxItems: 2 + - if: + properties: + compatible: + contains: + const: loongson,ls2k1000-thermal + then: + properties: + loongson,chipid: false + reg: + items: + - description: Thermal base register region -else: - properties: - reg: - maxItems: 1 + - if: + properties: + compatible: + contains: + const: loongson,ls2k2000-thermal + then: + properties: + loongson,chipid: false + reg: + items: + - description: Thermal base register region + - description: Thermal data output register region unevaluatedProperties: false
@@ -69,3 +95,14 @@ examples: interrupts = <7 IRQ_TYPE_LEVEL_LOW>; #thermal-sensor-cells = <1>; }; + + - | + #include <dt-bindings/interrupt-controller/irq.h> + thermal-sensor@16001500 { + compatible = "loongson,ls2k0300-thermal"; + reg = <0x16001500 0x30>; + interrupt-parent = <&liointc1>; + interrupts = <20 IRQ_TYPE_LEVEL_HIGH>; + loongson,chipid = <&chipid>; + #thermal-sensor-cells = <1>; + };
Signature · 1 line
2.52.0
PATCH v9 7/7 thermal/drivers/loongson2: Add Loongson-2K0300 support
In reply to Binbin ZhouThe Loongson-2K0300 sensor provides an 11-bit output and requires calibration data from the CHIP ID registers. Read the calibration offset through the loongson,chipid syscon phandle, initialize the sensor configuration register, and calculate the temperature with the calibrated slope and offset.
Early Loongson-2K0300 fuse revisions may provide unusable calibration data. If the calibrated formula reports a temperature outside the valid sensor range, fall back to the old-fuse slope with a zero calibration offset.
Trip-point programming for Loongson-2K0300 will be added later.
--- drivers/thermal/Kconfig | 1 + drivers/thermal/loongson2_thermal.c | 150 ++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+)
drivers/thermal/Kconfig +1 -0
@@ -522,6 +522,7 @@ config LOONGSON2_THERMAL tristate "Loongson-2 SoC series thermal driver" depends on LOONGARCH || COMPILE_TEST depends on OF + depends on MFD_SYSCON help Support for Thermal driver found on Loongson-2 SoC series platforms. The thermal driver realizes get_temp and set_trips function, which
drivers/thermal/loongson2_thermal.c +150 -0
201 lines not on this page · raw message
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH v9 2/7] thermal/drivers/loongson2: Fix clamp() usage for thermal trip points
In reply to Binbin ZhouThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] thermal/drivers/loongson2: incorrect Fixes tag in loongson2_thermal_set() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] thermal/drivers/loongson2: incorrect Fixes tag in loongson2_thermal_set()
--
commit 7c662ff635bdb14cac997976404a99f9d5a1e734
Author: Binbin Zhou <[email protected]>
Quoted 7 lines
thermal/drivers/loongson2: Fix
clamp()usage for thermal trip points
clamp()takes the value to clamp as its first argument, followed by the minimum and maximum bounds. The current code passes the -40 and 125 limits as the value instead, so the trip thresholds are not clamped to the supported sensor range.
Quoted 6 lines
Correct the arguments so that low and high are clamped to [-40, 125].
Fixes 734b5def91b5 ("thermal/drivers/loongson2: Add Loongson-2K2000 support")Reviewed-by Huacai Chen <[email protected]>Signed-off-by Binbin Zhou <[email protected]>
[Severity: Low]
Should the Fixes tag reference e7e3a7c35791 ("thermal/drivers/loongson-2: Add thermal management support") instead? Commit 734b5def91b5 ("thermal/drivers/loongson2:
Add Loongson-2K2000 support") did not modify loongson2_thermal_set(), whereas the inverted clamp() arguments were introduced in e7e3a7c35791 ("thermal/drivers/loongson-2:
Add thermal management support") and later moved into loongson2_set_ctrl_regs() calls in 6ef6d5ff5edd ("thermal/drivers/loongson2: Trivial code style adjustment").
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH v9 6/7] dt-bindings: thermal: loongson,ls2k-thermal: Add compatible for Loongson-2K0300
In reply to Binbin ZhouThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] dt-bindings: thermal: loongson,ls2k-thermal: unwieldy allOf conditionals --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] dt-bindings: thermal: loongson,ls2k-thermal: unwieldy allOf conditionals
--
commit f12fbd90282902d144235a2ecc3e0dfbcabab2a2
Author: Binbin Zhou <[email protected]>
dt-bindings: thermal: loongson,ls2k-thermal: Add compatible for Loongson-2K0300
This commit adds the loongson,ls2k0300-thermal compatible string along with the loongson,chipid phandle property and per-variant allOf conditionals for reg and loongson,chipid.
Quoted 4 lines
Documentation/devicetree/bindings/thermal/loongson,ls2k-thermal.yaml +0
-0
[ ... ]
Quoted 59 lines
Documentation/devicetree/bindings/thermal/loongson,ls2k-thermal.yaml +33
-15@@ -34,28 +32,56 @@ properties: '#thermal-sensor-cells': const: 1 + loongson,chipid: + $ref: /schemas/types.yaml#/definitions/phandle + description: + Phandle to the syscon with the Loongson-2K0300 CHIP ID. + required: - compatible - reg - interrupts -if: - properties: - compatible: - contains: - enum: - - loongson,ls2k2000-thermal +allOf: + - $ref: /schemas/thermal/thermal-sensor.yaml# + + - if: + properties: + compatible: + contains: + const: loongson,ls2k0300-thermal + then: + properties: + reg: + items: + - description: Thermal base register region + required: + - loongson,chipid -then: - properties: - reg: - minItems: 2 - maxItems: 2 + - if: + properties: + compatible: + contains: + const: loongson,ls2k1000-thermal + then: + properties: + loongson,chipid: false + reg: + items: + - description: Thermal base register region -else: - properties: - reg: - maxItems: 1 + - if: + properties:
Documentation/devicetree/bindings/thermal/loongson,ls2k-thermal.yaml properties: · line 84
Quoted 10 lines
Documentation/devicetree/bindings/thermal/loongson,ls2k-thermal.yaml +10
-0+ compatible: + contains: + const: loongson,ls2k2000-thermal + then: + properties: + loongson,chipid: false + reg: + items: + - description: Thermal base register region + - description: Thermal data output register region
[Severity: Low]
This isn't a bug, but with loongson,ls2k0300-thermal added, every standalone variant in this binding now has its own if-then block in allOf duplicating the reg item descriptions and differing on register regions or loongson,chipid requirements. Would it be cleaner to split the diverging variants into separate schema documents, or at least define the reg items descriptions once under top-level properties and only narrow the constraints in the conditionals?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
[email protected]
AI reviewRe: [PATCH v9 7/7] thermal/drivers/loongson2: Add Loongson-2K0300 support
In reply to Binbin ZhouThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] thermal/drivers/loongson2: unmasked trip IRQ in ls2k0300_thermal_hw_init() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] thermal/drivers/loongson2: unmasked trip IRQ in ls2k0300_thermal_hw_init()
--
commit 024ebeb46fbe6ba2893fe68297ad030cb9ce5fe2
Author: Binbin Zhou <[email protected]>
thermal/drivers/loongson2: Add Loongson-2K0300 support
This commit adds thermal sensor support for the Loongson-2K0300 SoC, reading calibration data from the CHIP ID syscon registers and falling back to an uncalibrated slope if the initial reading is out of range.
Quoted 4 lines
drivers/thermal/loongson2_thermal.c +0
-0
[ ... ]
Quoted 1 line
drivers/thermal/loongson2_thermal.c +0
-0@@ -87,6 +122,107 @@ static int loongson2_thermal_set_trips(struct thermal_zone_device *tz, int low,
[ ... ]
Quoted 26 lines
drivers/thermal/loongson2_thermal.c +26
-0+static int ls2k0300_thermal_hw_init (struct platform_device *pdev, +struct loongson2_thermal_data *data) +{ +struct device *dev = &pdev->dev; +int ret; + + data->regmap_cfg =syscon_regmap_lookup_by_phandle (dev->of_node,"loongson,chipid" ); +if (IS_ERR (data->regmap_cfg)) +return PTR_ERR (data->regmap_cfg); + + ret =ls2k0300_thermal_read_calib (data); +if (ret) +return dev_err_probe (dev, ret,"failed to read calibration data" ); + +writel (LS2K0300_THSENS_INIT_MASK , data->ctrl_reg +LS2K0300_THSENS_CFG_REG ); + +/* +* Wait for the ADC to produce the first valid conversion before +* using OUT_REG for the old-fuse sanity check. +*/ +usleep_range (1000 ,2000 ); + +ls2k0300_thermal_detect_old_fuse (dev, data); + +return 0 ; +}
[ ... ]
drivers/thermal/loongson2_thermal.c static int ls2k2000_thermal_hw_init(struct platform_device *pdev, · line 319
Quoted 7 lines
drivers/thermal/loongson2_thermal.c +3
-0@@ -178,6 +314,10 @@ static int ls2k2000_thermal_hw_init(struct platform_device *pdev,return 0 ; } +static const struct thermal_zone_device_ops ls2k0300_thermal_zone_ops = { + .get_temp = ls2k0300_thermal_get_temp, +};
[Severity: Medium]
Can this cause a thermal interrupt storm on Loongson-2K0300 if trip interrupts were left enabled by firmware or a previous kernel across kexec?
Unlike ls2k1000_thermal_hw_init() and ls2k2000_thermal_hw_init(), ls2k0300_thermal_hw_init() does not clear LOONGSON2_THSENS_STATUS_REG or disable trip-point interrupts, while loongson2_thermal_probe() still unconditionally registers loongson2_thermal_irq_thread(). Because ls2k0300_thermal_zone_ops omits .set_trips, thermal_zone_device_update() in the IRQ handler cannot update the hardware trip window or mask the interrupt enable bit, so the comparator will keep re-asserting the level-triggered interrupt on subsequent ADC conversions.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Re: [PATCH v9 7/7] thermal/drivers/loongson2: Add Loongson-2K0300 support
Quoted 62 lines · On 2026/11/9 16:32, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] thermal/drivers/loongson2: unmasked trip IRQ inls2k0300_thermal_hw_init()
--commit
024ebeb46fbe6ba2893fe68297ad030cb9ce5fe2
Author: Binbin Zhou <[email protected]>thermal/drivers/loongson2: Add Loongson-2K0300 support
This commit adds thermal sensor support for the Loongson-2K0300 SoC, reading calibration data from the CHIP ID syscon registers and falling back to an uncalibrated slope if the initial reading is out of range.
Quoted 4 lines
drivers/thermal/loongson2_thermal.c +0
-0[ ... ]
Quoted 1 line
drivers/thermal/loongson2_thermal.c +0
-0@@ -87,6 +122,107 @@ static int loongson2_thermal_set_trips(struct thermal_zone_device *tz, int low,[ ... ]
Quoted 26 lines
drivers/thermal/loongson2_thermal.c +26
-0+static int ls2k0300_thermal_hw_init (struct platform_device *pdev, +struct loongson2_thermal_data *data) +{ +struct device *dev = &pdev->dev; +int ret; + + data->regmap_cfg =syscon_regmap_lookup_by_phandle (dev->of_node,"loongson,chipid" ); +if (IS_ERR (data->regmap_cfg)) +return PTR_ERR (data->regmap_cfg); + + ret =ls2k0300_thermal_read_calib (data); +if (ret) +return dev_err_probe (dev, ret,"failed to read calibration data" ); + +writel (LS2K0300_THSENS_INIT_MASK , data->ctrl_reg +LS2K0300_THSENS_CFG_REG ); + +/* +* Wait for the ADC to produce the first valid conversion before +* using OUT_REG for the old-fuse sanity check. +*/ +usleep_range (1000 ,2000 ); + +ls2k0300_thermal_detect_old_fuse (dev, data); + +return 0 ; +}[ ... ]
drivers/thermal/loongson2_thermal.c static int ls2k2000_thermal_hw_init(struct platform_device *pdev, · line 319
Quoted 7 lines
drivers/thermal/loongson2_thermal.c +3
-0@@ -178,6 +314,10 @@ static int ls2k2000_thermal_hw_init(struct platform_device *pdev,return 0 ; } +static const struct thermal_zone_device_ops ls2k0300_thermal_zone_ops = { + .get_temp = ls2k0300_thermal_get_temp, +};[Severity: Medium]
Can this cause a thermal interrupt storm on Loongson-2K0300 if trip interrupts were left enabled by firmware or a previous kernel across kexec?
Quoted 8 lines
Unlike
ls2k1000_thermal_hw_init()andls2k2000_thermal_hw_init(),ls2k0300_thermal_hw_init()does not clearLOONGSON2_THSENS_STATUS_REGor disable trip-point interrupts, whileloongson2_thermal_probe()still unconditionally registersloongson2_thermal_irq_thread(). Becausels2k0300_thermal_zone_opsomits .set_trips,thermal_zone_device_update()in the IRQ handler cannot update the hardware trip window or mask the interrupt enable bit, so the comparator will keep re-asserting the level-triggered interrupt on subsequent ADC conversions.
The Loongson-2K0300 thermal sensor hardware does not have a trip-point comparator circuit — so .set_trips is intentionally absent, so I think the interrupt-storm scenario you describe cannot occur on this SoC variant.
Quoted 1 line
Signature · 2 lines
Thanks. Binbin