[PATCH v2 5/6] hwmon: sparx5: register LAN969x fan as a cooling device

From: Robert Marko

Date: Mon Nov 09 2026 - 08:44:54 EST


Allow the LAN969x hardware fan controller to be used by thermal-zone
cooling maps when cooling-levels are provided on the fan child node.

Translate cooling states to PWM duty-cycle values and keep the cooling
state synchronized with direct pwm1 writes. Register the cooling device
against the fan child so thermal references resolve to the correct node.

Signed-off-by: Robert Marko <robert.marko@xxxxxxxxxx>
---
Changes in v2:
- Use hwmon_lock() for cooling callbacks instead of a private mutex.

Documentation/hwmon/sparx5-temp.rst | 14 +++
drivers/hwmon/sparx5-temp.c | 145 ++++++++++++++++++++++++++--
2 files changed, 150 insertions(+), 9 deletions(-)

diff --git a/Documentation/hwmon/sparx5-temp.rst b/Documentation/hwmon/sparx5-temp.rst
index edb12f890172c..522707ed5a5f5 100644
--- a/Documentation/hwmon/sparx5-temp.rst
+++ b/Documentation/hwmon/sparx5-temp.rst
@@ -49,3 +49,17 @@ PWM consumers. The pwm1 value describes the logical duty cycle independently
of the output polarity. Frequency requests are rounded to the nearest
hardware divider value; requests outside the divider range are rejected.
pwm1_freq reports the resulting frequency.
+
+Thermal cooling
+---------------
+
+With device-tree thermal support enabled, the LAN969x fan can also act as a
+thermal cooling device. The fan child's cooling-levels property maps cooling
+states to PWM values from 0 to 255, in ascending order. When cooling levels
+are provided, the driver initially selects the highest level.
+
+Manual pwm1 writes remain available and update the reported cooling state
+to the highest state whose PWM value does not exceed the requested value,
+or state zero if the value is below the first level. Subsequent thermal
+cooling requests can override manual PWM settings. Each cooling request
+programs the exact PWM value associated with the requested state.
diff --git a/drivers/hwmon/sparx5-temp.c b/drivers/hwmon/sparx5-temp.c
index bdf08931f5b93..caa3fad367684 100644
--- a/drivers/hwmon/sparx5-temp.c
+++ b/drivers/hwmon/sparx5-temp.c
@@ -14,6 +14,7 @@
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/property.h>
+#include <linux/thermal.h>

#include <dt-bindings/pwm/pwm.h>

@@ -47,10 +48,14 @@ struct s5_hwmon {
void __iomem *base;
void __iomem *fan;
struct clk *clk;
+ struct device *hwmon_dev;
unsigned long clk_rate;
u32 pulses_per_revolution;
u32 pwm_period;
bool pwm_inverted;
+ u32 *cooling_levels;
+ unsigned long cooling_state;
+ unsigned long max_cooling_state;
};

static void s5_temp_enable(struct s5_hwmon *hwmon)
@@ -124,6 +129,20 @@ static int s5_read_fan(struct s5_hwmon *hwmon, long *val)
return 0;
}

+static void s5_update_cooling_state(struct s5_hwmon *hwmon, u32 pwm)
+{
+ unsigned long state;
+
+ if (!hwmon->cooling_levels)
+ return;
+
+ for (state = 0; state < hwmon->max_cooling_state; state++)
+ if (pwm < hwmon->cooling_levels[state + 1])
+ break;
+
+ hwmon->cooling_state = state;
+}
+
static int s5_read_pwm(struct s5_hwmon *hwmon, long *val)
{
u32 data;
@@ -183,17 +202,23 @@ static int s5_read(struct device *dev, enum hwmon_sensor_types type,
return -EOPNOTSUPP;
}

-static int s5_write_pwm(struct s5_hwmon *hwmon, long val)
+static void s5_set_pwm(struct s5_hwmon *hwmon, u32 pwm)
{
u32 data;

- if (val < 0 || val > 255)
- return -EINVAL;
-
data = readl_relaxed(hwmon->fan + FAN_CFG);
data &= ~FAN_CFG_DUTY_CYCLE;
- data |= FIELD_PREP(FAN_CFG_DUTY_CYCLE, val);
+ data |= FIELD_PREP(FAN_CFG_DUTY_CYCLE, pwm);
writel_relaxed(data, hwmon->fan + FAN_CFG);
+}
+
+static int s5_write_pwm(struct s5_hwmon *hwmon, long val)
+{
+ if (val < 0 || val > 255)
+ return -EINVAL;
+
+ s5_set_pwm(hwmon, val);
+ s5_update_cooling_state(hwmon, val);

return 0;
}
@@ -313,6 +338,89 @@ static const struct hwmon_chip_info s5_fan_chip_info = {
.info = s5_fan_info,
};

+static int s5_cooling_get_max_state(struct thermal_cooling_device *cdev,
+ unsigned long *state)
+{
+ struct s5_hwmon *hwmon = cdev->devdata;
+
+ *state = hwmon->max_cooling_state;
+
+ return 0;
+}
+
+static int s5_cooling_get_cur_state(struct thermal_cooling_device *cdev,
+ unsigned long *state)
+{
+ struct s5_hwmon *hwmon = cdev->devdata;
+
+ hwmon_lock(hwmon->hwmon_dev);
+ *state = hwmon->cooling_state;
+ hwmon_unlock(hwmon->hwmon_dev);
+
+ return 0;
+}
+
+static int s5_cooling_set_cur_state(struct thermal_cooling_device *cdev,
+ unsigned long state)
+{
+ struct s5_hwmon *hwmon = cdev->devdata;
+
+ if (state > hwmon->max_cooling_state)
+ return -EINVAL;
+
+ hwmon_lock(hwmon->hwmon_dev);
+ s5_set_pwm(hwmon, hwmon->cooling_levels[state]);
+ hwmon->cooling_state = state;
+ hwmon_unlock(hwmon->hwmon_dev);
+
+ return 0;
+}
+
+static const struct thermal_cooling_device_ops s5_cooling_ops = {
+ .get_max_state = s5_cooling_get_max_state,
+ .get_cur_state = s5_cooling_get_cur_state,
+ .set_cur_state = s5_cooling_set_cur_state,
+};
+
+static int s5_get_cooling_data(struct device *dev, struct device_node *fan_np,
+ struct s5_hwmon *hwmon)
+{
+ u32 *levels;
+ int count, i, ret;
+
+ if (!of_property_present(fan_np, "cooling-levels"))
+ return 0;
+
+ count = of_property_count_u32_elems(fan_np, "cooling-levels");
+ if (count <= 0)
+ return dev_err_probe(dev, count ? : -EINVAL,
+ "invalid cooling-levels property\n");
+
+ levels = devm_kcalloc(dev, count, sizeof(*levels), GFP_KERNEL);
+ if (!levels)
+ return -ENOMEM;
+ hwmon->cooling_levels = levels;
+
+ ret = of_property_read_u32_array(fan_np, "cooling-levels",
+ hwmon->cooling_levels, count);
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "failed to read cooling-levels\n");
+
+ for (i = 0; i < count; i++) {
+ if (hwmon->cooling_levels[i] > 255)
+ return dev_err_probe(dev, -EINVAL,
+ "cooling level %d exceeds 255\n", i);
+ if (i && hwmon->cooling_levels[i] < hwmon->cooling_levels[i - 1])
+ return dev_err_probe(dev, -EINVAL,
+ "cooling levels must be ascending\n");
+ }
+
+ hwmon->max_cooling_state = count - 1;
+
+ return 0;
+}
+
static int s5_get_fan_data(struct device *dev, struct device_node *fan_np,
struct s5_hwmon *hwmon)
{
@@ -346,18 +454,19 @@ static int s5_get_fan_data(struct device *dev, struct device_node *fan_np,
return dev_err_probe(dev, -EINVAL,
"invalid pulses-per-revolution\n");

- return 0;
+ return s5_get_cooling_data(dev, fan_np, hwmon);
}

static int s5_temp_probe(struct platform_device *pdev)
{
const struct s5_match_data *match_data;
+ struct thermal_cooling_device *cdev;
struct device *dev = &pdev->dev;
- struct device *hwmon_dev;
struct s5_hwmon *hwmon;
struct device_node *fan_np __free(device_node) = NULL;
void __iomem *base;
bool is_fan;
+ u32 initial_pwm;
int ret;

match_data = device_get_match_data(dev);
@@ -397,16 +506,34 @@ static int s5_temp_probe(struct platform_device *pdev)
"invalid fan PWM period\n");
}
s5_fan_enable(hwmon);
+
+ if (hwmon->cooling_levels) {
+ initial_pwm = hwmon->cooling_levels[hwmon->max_cooling_state];
+ ret = s5_write_pwm(hwmon, initial_pwm);
+ if (ret)
+ return ret;
+ }
} else {
s5_temp_enable(hwmon);
}

- hwmon_dev = devm_hwmon_device_register_with_info(dev,
+ hwmon->hwmon_dev = devm_hwmon_device_register_with_info(dev,
is_fan ? "lan969x_fan" : "s5_temp", hwmon,
is_fan ? &s5_fan_chip_info :
&s5_temp_chip_info, NULL);
+ if (IS_ERR(hwmon->hwmon_dev))
+ return PTR_ERR(hwmon->hwmon_dev);
+
+ if (IS_ENABLED(CONFIG_THERMAL_OF) && hwmon->cooling_levels) {
+ cdev = devm_thermal_of_child_cooling_device_register(dev, fan_np,
+ "lan969x-fan", hwmon,
+ &s5_cooling_ops);
+ if (IS_ERR(cdev))
+ return dev_err_probe(dev, PTR_ERR(cdev),
+ "failed to register cooling device\n");
+ }

- return PTR_ERR_OR_ZERO(hwmon_dev);
+ return 0;
}

static const struct s5_match_data lan969x_fan_match_data = {
--
2.56.0