Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 1/4] dt-bindings: hwmon: fan-common: reject zero pulses per revolution
@ 2026-11-11 16:12 Robert Marko
  2026-11-11 16:12 ` [PATCH v3 2/4] dt-bindings: hwmon: add Microchip LAN969x fan controller Robert Marko
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Robert Marko @ 2026-11-11 16:12 UTC (permalink / raw)
  To: linux, robh, krzk+dt, conor+dt, corbet, skhan, rdunlap,
	nicolas.ferre, claudiu.beznea, linux-hwmon, devicetree,
	linux-kernel, linux-doc, linux-arm-kernel
  Cc: luka.perkov, Robert Marko

A fan sensor cannot produce zero tachometer pulses per revolution.
Add a minimum of one to the shared fan schema so every user rejects
this invalid value.

Signed-off-by: Robert Marko <robert.marko@sartura.hr>
---
Changes in v3:
- New patch: require at least one pulse per revolution in fan-common.

 Documentation/devicetree/bindings/hwmon/fan-common.yaml | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Documentation/devicetree/bindings/hwmon/fan-common.yaml b/Documentation/devicetree/bindings/hwmon/fan-common.yaml
index 0fb7380816994..63a369628a3a1 100644
--- a/Documentation/devicetree/bindings/hwmon/fan-common.yaml
+++ b/Documentation/devicetree/bindings/hwmon/fan-common.yaml
@@ -27,6 +27,7 @@ properties:
     description:
       The number of pulse from fan sensor per revolution.
     $ref: /schemas/types.yaml#/definitions/uint32
+    minimum: 1
     maximum: 4
 
   tach-div:
-- 
2.56.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3 2/4] dt-bindings: hwmon: add Microchip LAN969x fan controller
  2026-11-11 16:12 [PATCH v3 1/4] dt-bindings: hwmon: fan-common: reject zero pulses per revolution Robert Marko
@ 2026-11-11 16:12 ` Robert Marko
  2026-11-12  1:06   ` Rob Herring (Arm)
  2026-11-11 16:12 ` [PATCH v3 3/4] hwmon: Add Microchip LAN969x fan controller driver Robert Marko
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: Robert Marko @ 2026-11-11 16:12 UTC (permalink / raw)
  To: linux, robh, krzk+dt, conor+dt, corbet, skhan, rdunlap,
	nicolas.ferre, claudiu.beznea, linux-hwmon, devicetree,
	linux-kernel, linux-doc, linux-arm-kernel
  Cc: luka.perkov, Robert Marko, Conor Dooley

The LAN969x fan controller has a PWM output and tachometer input in a
register block separate from the Sparx5-compatible temperature sensor.
Describe it with its own compatible and binding.

The optional fan child specifies PWM period and polarity, tachometer
pulses per revolution, and thermal cooling levels.

Acked-by: Conor Dooley <conor.dooley@microchip.com>
Signed-off-by: Robert Marko <robert.marko@sartura.hr>
---
Changes in v3:
- Drop the unnecessary literal-style description marker.
- Inherit pulse-count bounds from fan-common.yaml.
- Require cooling-levels only when #cooling-cells is present.
- Add Conor Dooley's Acked-by tag.

Changes in v2:
- Describe the fan controller in a separate binding and compatible.
- Keep the existing temperature-sensor binding unchanged.

 .../bindings/hwmon/microchip,lan9691-fan.yaml | 79 +++++++++++++++++++
 1 file changed, 79 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/microchip,lan9691-fan.yaml

diff --git a/Documentation/devicetree/bindings/hwmon/microchip,lan9691-fan.yaml b/Documentation/devicetree/bindings/hwmon/microchip,lan9691-fan.yaml
new file mode 100644
index 0000000000000..6a8d05a62f7c3
--- /dev/null
+++ b/Documentation/devicetree/bindings/hwmon/microchip,lan9691-fan.yaml
@@ -0,0 +1,79 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/hwmon/microchip,lan9691-fan.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Microchip LAN969x Fan Controller
+
+maintainers:
+  - Robert Marko <robert.marko@sartura.hr>
+
+description:
+  LAN969x fan controller with a PWM output and tachometer input. The fan
+  child's pwms property references this node to specify the PWM period and
+  polarity.
+
+properties:
+  compatible:
+    const: microchip,lan9691-fan
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    items:
+      - description: System reference clock
+
+  '#pwm-cells':
+    const: 2
+    description: |
+      Number of cells in a PWM specifier.
+      - cell 0: PWM period in nanoseconds
+      - cell 1: PWM polarity: 0 or PWM_POLARITY_INVERTED
+
+  fan:
+    $ref: fan-common.yaml#
+    unevaluatedProperties: false
+
+    properties:
+      cooling-levels:
+        description:
+          PWM duty-cycle values corresponding to thermal cooling states.
+        items:
+          maximum: 255
+
+      pulses-per-revolution:
+        default: 2
+
+    required:
+      - pwms
+
+    dependencies:
+      '#cooling-cells':
+        - cooling-levels
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - '#pwm-cells'
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/pwm/pwm.h>
+
+    fanctrl: fan-controller@e2010348 {
+        compatible = "microchip,lan9691-fan";
+        reg = <0xe2010348 0xc>;
+        clocks = <&sys_clk>;
+        #pwm-cells = <2>;
+
+        fan {
+            pwms = <&fanctrl 40000 PWM_POLARITY_INVERTED>;
+            cooling-levels = <0 128 192 255>;
+            #cooling-cells = <2>;
+        };
+    };
-- 
2.56.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3 3/4] hwmon: Add Microchip LAN969x fan controller driver
  2026-11-11 16:12 [PATCH v3 1/4] dt-bindings: hwmon: fan-common: reject zero pulses per revolution Robert Marko
  2026-11-11 16:12 ` [PATCH v3 2/4] dt-bindings: hwmon: add Microchip LAN969x fan controller Robert Marko
@ 2026-11-11 16:12 ` Robert Marko
  2026-11-11 16:12 ` [PATCH v3 4/4] arm64: dts: microchip: lan969x: add fan controller Robert Marko
  2026-11-12  1:05 ` [PATCH v3 1/4] dt-bindings: hwmon: fan-common: reject zero pulses per revolution Rob Herring
  3 siblings, 0 replies; 6+ messages in thread
From: Robert Marko @ 2026-11-11 16:12 UTC (permalink / raw)
  To: linux, robh, krzk+dt, conor+dt, corbet, skhan, rdunlap,
	nicolas.ferre, claudiu.beznea, linux-hwmon, devicetree,
	linux-kernel, linux-doc, linux-arm-kernel
  Cc: luka.perkov, Robert Marko

The LAN969x fan controller has its own register block, separate
from the Sparx5-compatible temperature sensor.

Add a dedicated hwmon driver for tachometer, PWM, and thermal cooling.

Signed-off-by: Robert Marko <robert.marko@sartura.hr>
---
Changes in v3:
- Use a standalone fan driver instead of extending sparx5-temp.
- Combine tachometer, PWM and thermal cooling support in one patch.
- Reject a zero-rate fan clock during probe.
- Add a dedicated MAINTAINERS entry.

Changes in v2:
- Register fan and temperature monitoring as separate hwmon devices.
- Reject a present but malformed pulses-per-revolution property.
- Drop the private mutex; hwmon serializes sysfs accesses.
- Preserve firmware PWM polarity when no fan child is present.
- Convert the DT period directly to a divider and reject invalid values
  before changing fan registers.
- Use hwmon_lock() for cooling callbacks instead of a private mutex.

 Documentation/hwmon/index.rst       |   1 +
 Documentation/hwmon/lan969x-fan.rst |  53 ++++
 MAINTAINERS                         |   8 +
 drivers/hwmon/Kconfig               |  10 +
 drivers/hwmon/Makefile              |   1 +
 drivers/hwmon/lan969x-fan.c         | 444 ++++++++++++++++++++++++++++
 6 files changed, 517 insertions(+)
 create mode 100644 Documentation/hwmon/lan969x-fan.rst
 create mode 100644 drivers/hwmon/lan969x-fan.c

diff --git a/Documentation/hwmon/index.rst b/Documentation/hwmon/index.rst
index a8ea421128b50..aa84893729fac 100644
--- a/Documentation/hwmon/index.rst
+++ b/Documentation/hwmon/index.rst
@@ -122,6 +122,7 @@ Hardware Monitoring Kernel Drivers
    kbatt
    kfan
    lan966x
+   lan969x-fan
    lattepanda-sigma-ec
    lineage-pem
    lm25066
diff --git a/Documentation/hwmon/lan969x-fan.rst b/Documentation/hwmon/lan969x-fan.rst
new file mode 100644
index 0000000000000..c79d8eadbc158
--- /dev/null
+++ b/Documentation/hwmon/lan969x-fan.rst
@@ -0,0 +1,53 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+Microchip LAN969x fan controller
+================================
+
+Supported chips:
+
+  * Microchip LAN969x
+
+    Prefix: 'lan969x_fan'
+
+    Addresses scanned: -
+
+    Datasheet: https://microchip-ung.github.io/lan969x-industrial_reginfo/
+
+Description
+-----------
+
+The LAN969x fan controller provides a tachometer input and a PWM output.
+
+The tachometer counts pulses over one-second intervals, independently of the
+PWM output. The fan child's pulses-per-revolution property specifies how many
+pulses represent one revolution; it defaults to two.
+
+The fan child's pwms property selects the initial PWM period and polarity.
+The output is dedicated to this fan and is not available to external 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.
+
+Sysfs entries
+-------------
+
+======================= ========================================================
+fan1_input              Fan speed in RPM.
+pwm1                    PWM duty cycle, from 0 to 255.
+pwm1_freq               PWM frequency in Hz.
+======================= ========================================================
+
+Thermal cooling
+---------------
+
+With device-tree thermal support enabled, the 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/MAINTAINERS b/MAINTAINERS
index aa647f5d37ddb..bc8af689ce9b2 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -18369,6 +18369,14 @@ L:	netdev@vger.kernel.org
 S:	Maintained
 F:	drivers/net/ethernet/microchip/sparx5/lan969x/*
 
+MICROCHIP LAN969X FAN CONTROLLER DRIVER
+M:	Robert Marko <robert.marko@sartura.hr>
+L:	linux-hwmon@vger.kernel.org
+S:	Maintained
+F:	Documentation/devicetree/bindings/hwmon/microchip,lan9691-fan.yaml
+F:	Documentation/hwmon/lan969x-fan.rst
+F:	drivers/hwmon/lan969x-fan.c
+
 MICROCHIP LCDFB DRIVER
 M:	Nicolas Ferre <nicolas.ferre@microchip.com>
 L:	linux-fbdev@vger.kernel.org
diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index 3ae676a56d95c..634e174eb4d8b 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -1060,6 +1060,16 @@ config SENSORS_LAN966X
 	  This driver can also be built as a module. If so, the module
 	  will be called lan966x-hwmon.
 
+config SENSORS_LAN969X_FAN
+	tristate "Microchip LAN969x fan controller"
+	depends on ARCH_LAN969X || COMPILE_TEST
+	help
+	  If you say yes here you get support for the fan controller on
+	  Microchip LAN969x SoCs.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called lan969x-fan.
+
 config SENSORS_LATTEPANDA_SIGMA_EC
 	tristate "LattePanda Sigma EC hardware monitoring"
 	depends on X86
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
index be3eb0992a0d0..3d54ead07f0b4 100644
--- a/drivers/hwmon/Makefile
+++ b/drivers/hwmon/Makefile
@@ -122,6 +122,7 @@ obj-$(CONFIG_SENSORS_KB9002)	+= kb9002.o
 obj-$(CONFIG_SENSORS_KBATT)	+= kbatt.o
 obj-$(CONFIG_SENSORS_KFAN)	+= kfan.o
 obj-$(CONFIG_SENSORS_LAN966X)	+= lan966x-hwmon.o
+obj-$(CONFIG_SENSORS_LAN969X_FAN)	+= lan969x-fan.o
 obj-$(CONFIG_SENSORS_LATTEPANDA_SIGMA_EC) += lattepanda-sigma-ec.o
 obj-$(CONFIG_SENSORS_LENOVO_EC)	+= lenovo-ec-sensors.o
 obj-$(CONFIG_SENSORS_LINEAGE)	+= lineage-pem.o
diff --git a/drivers/hwmon/lan969x-fan.c b/drivers/hwmon/lan969x-fan.c
new file mode 100644
index 0000000000000..fae0248658818
--- /dev/null
+++ b/drivers/hwmon/lan969x-fan.c
@@ -0,0 +1,444 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Microchip LAN969x fan controller driver */
+
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/hwmon.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
+#include <linux/thermal.h>
+
+#include <dt-bindings/pwm/pwm.h>
+
+#define FAN_CFG			0
+#define  FAN_CFG_DUTY_CYCLE	GENMASK(23, 16)
+#define  FAN_CFG_INV_POL		BIT(3)
+#define  FAN_CFG_GATE_ENA	BIT(2)
+#define  FAN_CFG_STAT_MODE	BIT(0)
+#define FAN_PWM_FREQ		4
+#define  FAN_PWM_FREQ_CYCLES	GENMASK(27, 16)
+#define  FAN_PWM_FREQ_DIV	GENMASK(15, 0)
+#define FAN_CNT			8
+#define  FAN_CNT_DATA		GENMASK(15, 0)
+
+#define FAN_TACH_INTERVAL_HZ	100000
+#define FAN_PWM_FREQ_SCALE	256
+
+struct lan969x_fan {
+	void __iomem *base;
+	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 lan969x_fan_enable(struct lan969x_fan *fan)
+{
+	u32 cycles, val;
+
+	cycles = DIV_ROUND_CLOSEST(fan->clk_rate, FAN_TACH_INTERVAL_HZ);
+	cycles = clamp_val(cycles, 1, FIELD_MAX(FAN_PWM_FREQ_CYCLES));
+
+	val = readl(fan->base + FAN_PWM_FREQ);
+	val &= ~FAN_PWM_FREQ_CYCLES;
+	val |= FIELD_PREP(FAN_PWM_FREQ_CYCLES, cycles);
+	writel(val, fan->base + FAN_PWM_FREQ);
+
+	/* Count all tachometer pulses over one-second intervals. */
+	val = readl(fan->base + FAN_CFG);
+	val &= ~(FAN_CFG_STAT_MODE | FAN_CFG_GATE_ENA);
+	if (fan->pwm_period) {
+		val &= ~FAN_CFG_INV_POL;
+		if (fan->pwm_inverted)
+			val |= FAN_CFG_INV_POL;
+	}
+	writel(val, fan->base + FAN_CFG);
+}
+
+static void lan969x_fan_update_cooling_state(struct lan969x_fan *fan, u32 pwm)
+{
+	unsigned long state;
+
+	if (!fan->cooling_levels)
+		return;
+
+	for (state = 0; state < fan->max_cooling_state; state++)
+		if (pwm < fan->cooling_levels[state + 1])
+			break;
+
+	fan->cooling_state = state;
+}
+
+static int lan969x_fan_read(struct device *dev, enum hwmon_sensor_types type,
+			    u32 attr, int channel, long *val)
+{
+	struct lan969x_fan *fan = dev_get_drvdata(dev);
+	unsigned long rate;
+	u32 data;
+
+	switch (type) {
+	case hwmon_fan:
+		if (attr != hwmon_fan_input)
+			break;
+		data = readl_relaxed(fan->base + FAN_CNT);
+		data = FIELD_GET(FAN_CNT_DATA, data);
+		*val = data * 60 / fan->pulses_per_revolution;
+		return 0;
+	case hwmon_pwm:
+		switch (attr) {
+		case hwmon_pwm_input:
+			data = readl_relaxed(fan->base + FAN_CFG);
+			*val = FIELD_GET(FAN_CFG_DUTY_CYCLE, data);
+			return 0;
+		case hwmon_pwm_freq:
+			data = readl_relaxed(fan->base + FAN_PWM_FREQ);
+			data = FIELD_GET(FAN_PWM_FREQ_DIV, data);
+			if (!data)
+				return -ENODATA;
+
+			rate = DIV_ROUND_CLOSEST(fan->clk_rate,
+						 FAN_PWM_FREQ_SCALE);
+			*val = DIV_ROUND_CLOSEST(rate, data);
+			return 0;
+		default:
+			break;
+		}
+		break;
+	default:
+		break;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static void lan969x_fan_set_pwm(struct lan969x_fan *fan, u32 pwm)
+{
+	u32 data;
+
+	data = readl_relaxed(fan->base + FAN_CFG);
+	data &= ~FAN_CFG_DUTY_CYCLE;
+	data |= FIELD_PREP(FAN_CFG_DUTY_CYCLE, pwm);
+	writel_relaxed(data, fan->base + FAN_CFG);
+}
+
+static int lan969x_fan_write_pwm(struct lan969x_fan *fan, long val)
+{
+	if (val < 0 || val > 255)
+		return -EINVAL;
+
+	lan969x_fan_set_pwm(fan, val);
+	lan969x_fan_update_cooling_state(fan, val);
+
+	return 0;
+}
+
+static void lan969x_fan_write_pwm_divider(struct lan969x_fan *fan, u32 divider)
+{
+	u32 data;
+
+	data = readl_relaxed(fan->base + FAN_PWM_FREQ);
+	data &= ~FAN_PWM_FREQ_DIV;
+	data |= FIELD_PREP(FAN_PWM_FREQ_DIV, divider);
+	writel_relaxed(data, fan->base + FAN_PWM_FREQ);
+}
+
+static int lan969x_fan_write_pwm_period(struct lan969x_fan *fan)
+{
+	unsigned long rate;
+	u64 divider;
+
+	rate = DIV_ROUND_CLOSEST(fan->clk_rate, FAN_PWM_FREQ_SCALE);
+	divider = DIV_ROUND_CLOSEST_ULL((u64)rate * fan->pwm_period,
+					NSEC_PER_SEC);
+	if (!divider || divider > FIELD_MAX(FAN_PWM_FREQ_DIV))
+		return -ERANGE;
+
+	lan969x_fan_write_pwm_divider(fan, divider);
+
+	return 0;
+}
+
+static int lan969x_fan_write_pwm_freq(struct lan969x_fan *fan, long val)
+{
+	unsigned long rate, divider;
+
+	if (val <= 0)
+		return -EINVAL;
+
+	rate = DIV_ROUND_CLOSEST(fan->clk_rate, FAN_PWM_FREQ_SCALE);
+	if (val < DIV_ROUND_CLOSEST(rate, FIELD_MAX(FAN_PWM_FREQ_DIV)) ||
+	    val > rate)
+		return -ERANGE;
+
+	divider = DIV_ROUND_CLOSEST(rate, val);
+	divider = min_t(unsigned long, divider, FIELD_MAX(FAN_PWM_FREQ_DIV));
+
+	lan969x_fan_write_pwm_divider(fan, divider);
+
+	return 0;
+}
+
+static int lan969x_fan_write(struct device *dev, enum hwmon_sensor_types type,
+			     u32 attr, int channel, long val)
+{
+	struct lan969x_fan *fan = dev_get_drvdata(dev);
+
+	if (type != hwmon_pwm)
+		return -EOPNOTSUPP;
+
+	switch (attr) {
+	case hwmon_pwm_input:
+		return lan969x_fan_write_pwm(fan, val);
+	case hwmon_pwm_freq:
+		return lan969x_fan_write_pwm_freq(fan, val);
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static umode_t lan969x_fan_is_visible(const void *_data,
+				      enum hwmon_sensor_types type,
+				      u32 attr, int channel)
+{
+	switch (type) {
+	case hwmon_fan:
+		return attr == hwmon_fan_input ? 0444 : 0;
+	case hwmon_pwm:
+		if (attr == hwmon_pwm_input || attr == hwmon_pwm_freq)
+			return 0644;
+		return 0;
+	default:
+		return 0;
+	}
+}
+
+static const struct hwmon_channel_info * const lan969x_fan_info[] = {
+	HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT),
+	HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT | HWMON_PWM_FREQ),
+	NULL
+};
+
+static const struct hwmon_ops lan969x_fan_hwmon_ops = {
+	.is_visible = lan969x_fan_is_visible,
+	.read = lan969x_fan_read,
+	.write = lan969x_fan_write,
+};
+
+static const struct hwmon_chip_info lan969x_fan_chip_info = {
+	.ops = &lan969x_fan_hwmon_ops,
+	.info = lan969x_fan_info,
+};
+
+static int lan969x_fan_cooling_get_max_state(struct thermal_cooling_device *cdev,
+					      unsigned long *state)
+{
+	struct lan969x_fan *fan = cdev->devdata;
+
+	*state = fan->max_cooling_state;
+
+	return 0;
+}
+
+static int lan969x_fan_cooling_get_cur_state(struct thermal_cooling_device *cdev,
+					      unsigned long *state)
+{
+	struct lan969x_fan *fan = cdev->devdata;
+
+	hwmon_lock(fan->hwmon_dev);
+	*state = fan->cooling_state;
+	hwmon_unlock(fan->hwmon_dev);
+
+	return 0;
+}
+
+static int lan969x_fan_cooling_set_cur_state(struct thermal_cooling_device *cdev,
+					      unsigned long state)
+{
+	struct lan969x_fan *fan = cdev->devdata;
+
+	if (state > fan->max_cooling_state)
+		return -EINVAL;
+
+	hwmon_lock(fan->hwmon_dev);
+	lan969x_fan_set_pwm(fan, fan->cooling_levels[state]);
+	fan->cooling_state = state;
+	hwmon_unlock(fan->hwmon_dev);
+
+	return 0;
+}
+
+static const struct thermal_cooling_device_ops lan969x_fan_cooling_ops = {
+	.get_max_state = lan969x_fan_cooling_get_max_state,
+	.get_cur_state = lan969x_fan_cooling_get_cur_state,
+	.set_cur_state = lan969x_fan_cooling_set_cur_state,
+};
+
+static int lan969x_fan_get_cooling_data(struct device *dev,
+					struct device_node *fan_np,
+					struct lan969x_fan *fan)
+{
+	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;
+	fan->cooling_levels = levels;
+
+	ret = of_property_read_u32_array(fan_np, "cooling-levels",
+					 fan->cooling_levels, count);
+	if (ret)
+		return dev_err_probe(dev, ret,
+				     "failed to read cooling-levels\n");
+
+	for (i = 0; i < count; i++) {
+		if (fan->cooling_levels[i] > 255)
+			return dev_err_probe(dev, -EINVAL,
+					     "cooling level %d exceeds 255\n", i);
+		if (i && fan->cooling_levels[i] < fan->cooling_levels[i - 1])
+			return dev_err_probe(dev, -EINVAL,
+					     "cooling levels must be ascending\n");
+	}
+
+	fan->max_cooling_state = count - 1;
+
+	return 0;
+}
+
+static int lan969x_fan_get_fan_data(struct device *dev,
+				    struct device_node *fan_np,
+				    struct lan969x_fan *fan)
+{
+	struct of_phandle_args args;
+	int ret;
+
+	ret = of_parse_phandle_with_args(fan_np, "pwms", "#pwm-cells", 0,
+					 &args);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to parse fan PWM\n");
+
+	if (args.np != dev->of_node || args.args_count != 2 || !args.args[0] ||
+	    args.args[1] & ~PWM_POLARITY_INVERTED) {
+		of_node_put(args.np);
+		return dev_err_probe(dev, -EINVAL, "invalid fan PWM specifier\n");
+	}
+
+	fan->pwm_period = args.args[0];
+	fan->pwm_inverted = args.args[1] & PWM_POLARITY_INVERTED;
+	of_node_put(args.np);
+
+	if (of_property_present(fan_np, "pulses-per-revolution")) {
+		ret = of_property_read_u32(fan_np, "pulses-per-revolution",
+					   &fan->pulses_per_revolution);
+		if (ret)
+			return dev_err_probe(dev, ret,
+					     "invalid pulses-per-revolution\n");
+	}
+	if (!fan->pulses_per_revolution || fan->pulses_per_revolution > 4)
+		return dev_err_probe(dev, -EINVAL,
+				     "invalid pulses-per-revolution\n");
+
+	return lan969x_fan_get_cooling_data(dev, fan_np, fan);
+}
+
+static int lan969x_fan_probe(struct platform_device *pdev)
+{
+	struct thermal_cooling_device *cdev;
+	struct device *dev = &pdev->dev;
+	struct lan969x_fan *fan;
+	struct device_node *fan_np __free(device_node) = NULL;
+	u32 initial_pwm;
+	int ret;
+
+	fan = devm_kzalloc(dev, sizeof(*fan), GFP_KERNEL);
+	if (!fan)
+		return -ENOMEM;
+
+	fan->base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(fan->base))
+		return PTR_ERR(fan->base);
+
+	fan->pulses_per_revolution = 2;
+	fan_np = of_get_available_child_by_name(dev->of_node, "fan");
+	if (fan_np) {
+		ret = lan969x_fan_get_fan_data(dev, fan_np, fan);
+		if (ret)
+			return ret;
+	}
+
+	fan->clk = devm_clk_get_enabled(dev, NULL);
+	if (IS_ERR(fan->clk))
+		return PTR_ERR(fan->clk);
+	fan->clk_rate = clk_get_rate(fan->clk);
+	if (!fan->clk_rate)
+		return dev_err_probe(dev, -EINVAL, "invalid fan clock rate\n");
+
+	if (fan->pwm_period) {
+		ret = lan969x_fan_write_pwm_period(fan);
+		if (ret)
+			return dev_err_probe(dev, ret,
+					     "invalid fan PWM period\n");
+	}
+	lan969x_fan_enable(fan);
+
+	if (fan->cooling_levels) {
+		initial_pwm = fan->cooling_levels[fan->max_cooling_state];
+		ret = lan969x_fan_write_pwm(fan, initial_pwm);
+		if (ret)
+			return ret;
+	}
+
+	fan->hwmon_dev = devm_hwmon_device_register_with_info(dev, "lan969x_fan",
+							 fan,
+							 &lan969x_fan_chip_info,
+							 NULL);
+	if (IS_ERR(fan->hwmon_dev))
+		return PTR_ERR(fan->hwmon_dev);
+
+	if (IS_ENABLED(CONFIG_THERMAL_OF) && fan->cooling_levels) {
+		cdev = devm_thermal_of_child_cooling_device_register(dev, fan_np,
+							     "lan969x-fan", fan,
+							     &lan969x_fan_cooling_ops);
+		if (IS_ERR(cdev))
+			return dev_err_probe(dev, PTR_ERR(cdev),
+					     "failed to register cooling device\n");
+	}
+
+	return 0;
+}
+
+static const struct of_device_id lan969x_fan_match[] = {
+	{ .compatible = "microchip,lan9691-fan" },
+	{},
+};
+MODULE_DEVICE_TABLE(of, lan969x_fan_match);
+
+static struct platform_driver lan969x_fan_driver = {
+	.probe = lan969x_fan_probe,
+	.driver = {
+		.name = "lan969x-fan",
+		.of_match_table = lan969x_fan_match,
+	},
+};
+
+module_platform_driver(lan969x_fan_driver);
+
+MODULE_DESCRIPTION("Microchip LAN969x fan controller driver");
+MODULE_AUTHOR("Robert Marko <robert.marko@sartura.hr>");
+MODULE_LICENSE("GPL");
-- 
2.56.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3 4/4] arm64: dts: microchip: lan969x: add fan controller
  2026-11-11 16:12 [PATCH v3 1/4] dt-bindings: hwmon: fan-common: reject zero pulses per revolution Robert Marko
  2026-11-11 16:12 ` [PATCH v3 2/4] dt-bindings: hwmon: add Microchip LAN969x fan controller Robert Marko
  2026-11-11 16:12 ` [PATCH v3 3/4] hwmon: Add Microchip LAN969x fan controller driver Robert Marko
@ 2026-11-11 16:12 ` Robert Marko
  2026-11-12  1:05 ` [PATCH v3 1/4] dt-bindings: hwmon: fan-common: reject zero pulses per revolution Rob Herring
  3 siblings, 0 replies; 6+ messages in thread
From: Robert Marko @ 2026-11-11 16:12 UTC (permalink / raw)
  To: linux, robh, krzk+dt, conor+dt, corbet, skhan, rdunlap,
	nicolas.ferre, claudiu.beznea, linux-hwmon, devicetree,
	linux-kernel, linux-doc, linux-arm-kernel
  Cc: luka.perkov, Robert Marko

LAN969x provides a PWM fan output and tachometer input in a register
block separate from the temperature sensor. Describe it as its own
fan-controller node and keep the temperature sensor compatible.

Connect the EV23X71A fan to the controller and route its pins. Use a
25 kHz normal-polarity PWM for the board low-side FET. Set cooling
levels so the fan starts at full speed.

Signed-off-by: Robert Marko <robert.marko@sartura.hr>
---
Changes in v3:
- No DTS changes; renumber from 6/6 to 4/4.

Changes in v2:
- Split the fan controller from the temperature node.
- Add the EV23X71A fan child with board-specific PWM and cooling levels.

 arch/arm64/boot/dts/microchip/lan9691.dtsi         | 7 +++++++
 arch/arm64/boot/dts/microchip/lan9696-ev23x71a.dts | 8 +++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/boot/dts/microchip/lan9691.dtsi b/arch/arm64/boot/dts/microchip/lan9691.dtsi
index 52c22dd1a15d5..039c2014aa341 100644
--- a/arch/arm64/boot/dts/microchip/lan9691.dtsi
+++ b/arch/arm64/boot/dts/microchip/lan9691.dtsi
@@ -522,6 +522,13 @@ sgpio_out: gpio@1 {
 			};
 		};
 
+		fanctrl: fan-controller@e2010348 {
+			compatible = "microchip,lan9691-fan";
+			reg = <0xe2010348 0xc>;
+			clocks = <&fx100_clk>;
+			#pwm-cells = <2>;
+		};
+
 		tmon: hwmon@e2020100 {
 			compatible = "microchip,lan9691-temp", "microchip,sparx5-temp";
 			reg = <0xe2020100 0xc>;
diff --git a/arch/arm64/boot/dts/microchip/lan9696-ev23x71a.dts b/arch/arm64/boot/dts/microchip/lan9696-ev23x71a.dts
index abb21a8bace9e..3cbe5beebce1d 100644
--- a/arch/arm64/boot/dts/microchip/lan9696-ev23x71a.dts
+++ b/arch/arm64/boot/dts/microchip/lan9696-ev23x71a.dts
@@ -775,9 +775,15 @@ port29: port@29 {
 	};
 };
 
-&tmon {
+&fanctrl {
 	pinctrl-0 = <&fan_pins>;
 	pinctrl-names = "default";
+
+	fan {
+		pwms = <&fanctrl 40000 0>;
+		cooling-levels = <0 128 192 255>;
+		#cooling-cells = <2>;
+	};
 };
 
 &usart0 {
-- 
2.56.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 1/4] dt-bindings: hwmon: fan-common: reject zero pulses per revolution
  2026-11-11 16:12 [PATCH v3 1/4] dt-bindings: hwmon: fan-common: reject zero pulses per revolution Robert Marko
                   ` (2 preceding siblings ...)
  2026-11-11 16:12 ` [PATCH v3 4/4] arm64: dts: microchip: lan969x: add fan controller Robert Marko
@ 2026-11-12  1:05 ` Rob Herring
  3 siblings, 0 replies; 6+ messages in thread
From: Rob Herring @ 2026-11-12  1:05 UTC (permalink / raw)
  To: Robert Marko
  Cc: linux, krzk+dt, conor+dt, corbet, skhan, rdunlap, nicolas.ferre,
	claudiu.beznea, linux-hwmon, devicetree, linux-kernel, linux-doc,
	linux-arm-kernel, luka.perkov

On Wed, Nov 11, 2026 at 05:12:45PM +0100, Robert Marko wrote:
> A fan sensor cannot produce zero tachometer pulses per revolution.
> Add a minimum of one to the shared fan schema so every user rejects
> this invalid value.
> 
> Signed-off-by: Robert Marko <robert.marko@sartura.hr>
> ---
> Changes in v3:
> - New patch: require at least one pulse per revolution in fan-common.
> 
>  Documentation/devicetree/bindings/hwmon/fan-common.yaml | 1 +
>  1 file changed, 1 insertion(+)

Reviewed-by: Rob Herring (Arm) <robh@kernel.org>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 2/4] dt-bindings: hwmon: add Microchip LAN969x fan controller
  2026-11-11 16:12 ` [PATCH v3 2/4] dt-bindings: hwmon: add Microchip LAN969x fan controller Robert Marko
@ 2026-11-12  1:06   ` Rob Herring (Arm)
  0 siblings, 0 replies; 6+ messages in thread
From: Rob Herring (Arm) @ 2026-11-12  1:06 UTC (permalink / raw)
  To: Robert Marko
  Cc: skhan, rdunlap, nicolas.ferre, claudiu.beznea, Conor Dooley,
	conor+dt, luka.perkov, linux, devicetree, linux-doc, linux-hwmon,
	corbet, krzk+dt, linux-arm-kernel, linux-kernel


On Wed, 11 Nov 2026 17:12:46 +0100, Robert Marko wrote:
> The LAN969x fan controller has a PWM output and tachometer input in a
> register block separate from the Sparx5-compatible temperature sensor.
> Describe it with its own compatible and binding.
> 
> The optional fan child specifies PWM period and polarity, tachometer
> pulses per revolution, and thermal cooling levels.
> 
> Acked-by: Conor Dooley <conor.dooley@microchip.com>
> Signed-off-by: Robert Marko <robert.marko@sartura.hr>
> ---
> Changes in v3:
> - Drop the unnecessary literal-style description marker.
> - Inherit pulse-count bounds from fan-common.yaml.
> - Require cooling-levels only when #cooling-cells is present.
> - Add Conor Dooley's Acked-by tag.
> 
> Changes in v2:
> - Describe the fan controller in a separate binding and compatible.
> - Keep the existing temperature-sensor binding unchanged.
> 
>  .../bindings/hwmon/microchip,lan9691-fan.yaml | 79 +++++++++++++++++++
>  1 file changed, 79 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/hwmon/microchip,lan9691-fan.yaml
> 

Reviewed-by: Rob Herring (Arm) <robh@kernel.org>



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-12  1:06 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-11 16:12 [PATCH v3 1/4] dt-bindings: hwmon: fan-common: reject zero pulses per revolution Robert Marko
2026-11-11 16:12 ` [PATCH v3 2/4] dt-bindings: hwmon: add Microchip LAN969x fan controller Robert Marko
2026-11-12  1:06   ` Rob Herring (Arm)
2026-11-11 16:12 ` [PATCH v3 3/4] hwmon: Add Microchip LAN969x fan controller driver Robert Marko
2026-11-11 16:12 ` [PATCH v3 4/4] arm64: dts: microchip: lan969x: add fan controller Robert Marko
2026-11-12  1:05 ` [PATCH v3 1/4] dt-bindings: hwmon: fan-common: reject zero pulses per revolution Rob Herring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox