[PATCH 5/6] hwmon: sparx5: register LAN969x fan as a cooling device
Robert Marko
robert.marko at sartura.hr
Tue Oct 13 05:56:29 PDT 2026
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 at sartura.hr>
---
Documentation/hwmon/sparx5-temp.rst | 14 +++
drivers/hwmon/sparx5-temp.c | 144 ++++++++++++++++++++++++++--
2 files changed, 150 insertions(+), 8 deletions(-)
diff --git a/Documentation/hwmon/sparx5-temp.rst b/Documentation/hwmon/sparx5-temp.rst
index a1f8218e68794..28f91537fc376 100644
--- a/Documentation/hwmon/sparx5-temp.rst
+++ b/Documentation/hwmon/sparx5-temp.rst
@@ -48,3 +48,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 5130140e1b579..3eabcc7946098 100644
--- a/drivers/hwmon/sparx5-temp.c
+++ b/drivers/hwmon/sparx5-temp.c
@@ -15,6 +15,7 @@
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/property.h>
+#include <linux/thermal.h>
#include <dt-bindings/pwm/pwm.h>
@@ -53,6 +54,9 @@ struct s5_hwmon {
u32 pulses_per_revolution;
u32 pwm_frequency;
bool pwm_inverted;
+ u32 *cooling_levels;
+ unsigned long cooling_state;
+ unsigned long max_cooling_state;
};
static void s5_temp_enable(struct s5_hwmon *hwmon)
@@ -123,6 +127,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;
@@ -184,19 +202,25 @@ 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)
+/* Caller must hold hwmon->lock. */
+static void s5_set_pwm(struct s5_hwmon *hwmon, u32 pwm)
{
u32 data;
+ data = readl_relaxed(hwmon->fan + FAN_CFG);
+ data &= ~FAN_CFG_DUTY_CYCLE;
+ 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;
mutex_lock(&hwmon->lock);
-
- data = readl_relaxed(hwmon->fan + FAN_CFG);
- data &= ~FAN_CFG_DUTY_CYCLE;
- data |= FIELD_PREP(FAN_CFG_DUTY_CYCLE, val);
- writel_relaxed(data, hwmon->fan + FAN_CFG);
+ s5_set_pwm(hwmon, val);
+ s5_update_cooling_state(hwmon, val);
mutex_unlock(&hwmon->lock);
@@ -289,6 +313,90 @@ static const struct hwmon_chip_info s5_chip_info = {
.info = s5_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;
+
+ mutex_lock(&hwmon->lock);
+ *state = hwmon->cooling_state;
+ mutex_unlock(&hwmon->lock);
+
+ 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;
+
+ mutex_lock(&hwmon->lock);
+ s5_set_pwm(hwmon, hwmon->cooling_levels[state]);
+ hwmon->cooling_state = state;
+
+ mutex_unlock(&hwmon->lock);
+
+ 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)
{
@@ -320,16 +428,18 @@ 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;
+ u32 initial_pwm;
int ret;
match_data = device_get_match_data(dev);
@@ -372,14 +482,32 @@ static int s5_temp_probe(struct platform_device *pdev)
return dev_err_probe(dev, ret,
"invalid fan PWM period\n");
}
+
+ if (hwmon->cooling_levels) {
+ initial_pwm = hwmon->cooling_levels[hwmon->max_cooling_state];
+ ret = s5_write_pwm(hwmon, initial_pwm);
+ if (ret)
+ return ret;
+ }
}
hwmon_dev = devm_hwmon_device_register_with_info(dev, "s5_temp",
hwmon,
&s5_chip_info,
NULL);
+ if (IS_ERR(hwmon_dev))
+ return PTR_ERR(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_match_data = {
--
2.56.0
More information about the linux-arm-kernel
mailing list