Skip to contentSkip to conversation

Add JHB100 Fan-Tach support

PATCH Superseded Changhuang Liang 3 participants 6 messages Sep 23

Subsystems (3)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover Add JHB100 Fan-Tach support

StarFive Fan-Tach controller can support up to 16 fan tach input. The series has been tested on the EVB1 board.

Change since v4:
PATCH 1:
- Convert the fan node from fan@0 to fan-0.
- Add the phandle of the PWM node.
- Remove the reg property under the fan node.
- Remove the properties "#address-cells" and "#size-cells".
- Convert the keyword "fan tach" to "pwm fan"

PATCH 2:
- Add PWM fan speed control through up to 8 PWM channels supplied by an external PWM controller, exposed as pwm[1-8].
- Add support for one PWM channel driving up to two fans.
- Add suspend/resume support to disable and restore PWM outputs across system sleep.
- Add pwm_lock to serialize PWM updates with PM operations.
- Convert the keyword "fan tach" to "pwm fan"

v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

Changhuang Liang (2):
dt-bindings: hwmon: Add starfive,jhb100-pwm-fan
hwmon: (starfive-pwm-fan) Add PWM fan driver for StarFive JHB100

 .../hwmon/starfive,jhb100-pwm-fan.yaml        |  87 ++
 Documentation/hwmon/index.rst                 |   1 +
 Documentation/hwmon/starfive-pwm-fan.rst      |  45 ++
 MAINTAINERS                                   |   8 +
 drivers/hwmon/Kconfig                         |  15 +
 drivers/hwmon/Makefile                        |   1 +
 drivers/hwmon/starfive-pwm-fan.c              | 762 ++++++++++++++++++
 7 files changed, 919 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/starfive,jhb100-pwm-fan.yaml
 create mode 100644 Documentation/hwmon/starfive-pwm-fan.rst
 create mode 100644 drivers/hwmon/starfive-pwm-fan.c

--
2.25.1

#1 lore raw

PATCH v5 1/2 dt-bindings: hwmon: Add starfive,jhb100-pwm-fan

Add a binding for the StarFive JHB100 PWM fan controller. The controller has a hardware tachometer block with 16 tachometer inputs, while fan speed is controlled through 8 PWM channels supplied by an external PWM controller.

One PWM channel can drive up to two fans, so each fan-N child node references one PWM channel and lists the tachometer inputs wired to the fans it drives.

---
 .../hwmon/starfive,jhb100-pwm-fan.yaml        | 87 +++++++++++++++++++
 1 file changed, 87 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/starfive,jhb100-pwm-fan.yaml
Documentation/devicetree/bindings/hwmon/starfive,jhb100-pwm-fan.yaml new file +87 -0
new file mode 100644
index 000000000000..b05789867c0e
@@ -0,0 +1,87 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/hwmon/starfive,jhb100-pwm-fan.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: StarFive JHB100 PWM Fan Controller
+
+maintainers:
+  - Changhuang Liang <[email protected]>
+
+description:
+  The StarFive JHB100 fan controller uses a hardware tachometer block with
+  16 tachometer inputs to measure fan speed. Fan speed is controlled through
+  8 PWM channels supplied by an external PWM controller, and one PWM channel
+  can drive up to two fans. Each fan child node references the PWM channel
+  driving the fan(s) and lists the tachometer inputs wired to them.
+
+properties:
+  compatible:
+    const: starfive,jhb100-pwm-fan
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  resets:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+patternProperties:
+  "^fan-[0-7]$":
+    $ref: fan-common.yaml#
+    unevaluatedProperties: false
+
+    properties:
+      pulses-per-revolution:
+        minimum: 1
+        default: 2
+
+      tach-ch:
+        description:
+          The tachometer input(s) wired to the fan(s) driven by the PWM
+          channel. One PWM channel can drive up to two fans.
+        minItems: 1
+        maxItems: 2
+        items:
+          maximum: 15
+
+    required:
+      - pwms
+      - tach-ch
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - resets
+  - interrupts
+
+additionalProperties: false
+
+examples:
+  - |
+    fan-controller@11be0000 {
+        compatible = "starfive,jhb100-pwm-fan";
+        reg = <0x11be0000 0x400>;
+        interrupts = <128>;
+        clocks = <&per2crg 33>;
+        resets = <&per2crg 5>;
+
+        fan-0 {
+            pwms = <&pwm0 0 40000 0>;
+            tach-ch = /bits/ 8 <0x0 0x8>;
+            pulses-per-revolution = <2>;
+        };
+
+        fan-1 {
+            pwms = <&pwm1 0 40000 0>;
+            tach-ch = /bits/ 8 <0x1 0x9>;
+        };
+    };
+...
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v5 1/2] dt-bindings: hwmon: Add starfive,jhb100-pwm-fan

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

Add a binding for the StarFive JHB100 PWM fan controller. The controller has a hardware tachometer block with 16 tachometer inputs, while fan speed is controlled through 8 PWM channels supplied by an external PWM controller.

One PWM channel can drive up to two fans, so each fan-N child node references one PWM channel and lists the tachometer inputs wired to the fans it drives.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v5 1/2
#3 lore raw

Re: [PATCH v5 1/2] dt-bindings: hwmon: Add starfive,jhb100-pwm-fan

In reply to Changhuang Liang
Quoted 10 lines · Changhuang Liang

Add a binding for the StarFive JHB100 PWM fan controller. The controller has a hardware tachometer block with 16 tachometer inputs, while fan speed is controlled through 8 PWM channels supplied by an external PWM controller.

One PWM channel can drive up to two fans, so each fan-N child node references one PWM channel and lists the tachometer inputs wired to the fans it drives.

pw-bot: not-applicable

Quoted 101 lines
---
 .../hwmon/starfive,jhb100-pwm-fan.yaml        | 87 +++++++++++++++++++
 1 file changed, 87 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/starfive,jhb100-pwm-fan.yaml
Documentation/devicetree/bindings/hwmon/starfive,jhb100-pwm-fan.yaml new file +87 -0
new file mode 100644
index 000000000000..b05789867c0e
@@ -0,0 +1,87 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/hwmon/starfive,jhb100-pwm-fan.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: StarFive JHB100 PWM Fan Controller
+
+maintainers:
+  - Changhuang Liang <[email protected]>
+
+description:
+  The StarFive JHB100 fan controller uses a hardware tachometer block with
+  16 tachometer inputs to measure fan speed. Fan speed is controlled through
+  8 PWM channels supplied by an external PWM controller, and one PWM channel
+  can drive up to two fans. Each fan child node references the PWM channel
+  driving the fan(s) and lists the tachometer inputs wired to them.
+
+properties:
+  compatible:
+    const: starfive,jhb100-pwm-fan
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  resets:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+patternProperties:
+  "^fan-[0-7]$":
+    $ref: fan-common.yaml#
+    unevaluatedProperties: false
+
+    properties:
+      pulses-per-revolution:
+        minimum: 1
+        default: 2
+
+      tach-ch:
+        description:
+          The tachometer input(s) wired to the fan(s) driven by the PWM
+          channel. One PWM channel can drive up to two fans.
+        minItems: 1
+        maxItems: 2
+        items:
+          maximum: 15
+
+    required:
+      - pwms
+      - tach-ch
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - resets
+  - interrupts
+
+additionalProperties: false
+
+examples:
+  - |
+    fan-controller@11be0000 {
+        compatible = "starfive,jhb100-pwm-fan";
+        reg = <0x11be0000 0x400>;
+        interrupts = <128>;
+        clocks = <&per2crg 33>;
+        resets = <&per2crg 5>;
+
+        fan-0 {
+            pwms = <&pwm0 0 40000 0>;
+            tach-ch = /bits/ 8 <0x0 0x8>;
+            pulses-per-revolution = <2>;
+        };
+
+        fan-1 {
+            pwms = <&pwm1 0 40000 0>;
+            tach-ch = /bits/ 8 <0x1 0x9>;
+        };
+    };
+...
Signature · 1 line
2.25.1
#4 lore raw

PATCH v5 2/2 hwmon: (starfive-pwm-fan) Add PWM fan driver for StarFive JHB100

In reply to Changhuang Liang

Add PWM fan controller driver for StarFive JHB100 SoC.

Fan speed is controlled through up to 8 PWM channels supplied by an external PWM controller and exposed as pwm[1-8], and one PWM channel can drive up to two fans. The tachometer block supports 16 independent inputs and reports fan speed in RPM through the hwmon sysfs interface, along with stall and low-speed alarms.

For this controller, the special clock and reset operation sequence is:
probe: clk_prepare_enable() then reset_control_deassert()
remove: clk_disable_unprepare() then reset_control_assert()

---
 Documentation/hwmon/index.rst            |   1 +
 Documentation/hwmon/starfive-pwm-fan.rst |  45 ++
 MAINTAINERS                              |   8 +
 drivers/hwmon/Kconfig                    |  15 +
 drivers/hwmon/Makefile                   |   1 +
 drivers/hwmon/starfive-pwm-fan.c         | 762 +++++++++++++++++++++++
 6 files changed, 832 insertions(+)
 create mode 100644 Documentation/hwmon/starfive-pwm-fan.rst
 create mode 100644 drivers/hwmon/starfive-pwm-fan.c
Documentation/hwmon/index.rst +1 -0
index 199ca79621e9..949b46dcc258 100644
@@ -263,6 +263,7 @@ Hardware Monitoring Kernel Drivers
    sparx5-temp
    spd5118
    sq24860
+   starfive-pwm-fan
    stpddc60
    surface_fan
    sy7636a-hwmon
Documentation/hwmon/starfive-pwm-fan.rst new file +45 -0

45 lines not on this page · raw message

MAINTAINERS +8 -0
index dccddc99ac9a..3735e87da3bf 100644
@@ -26483,6 +26483,14 @@ S:	Supported
 F:	Documentation/devicetree/bindings/interrupt-controller/starfive,jhb100-intc.yaml
 F:	drivers/irqchip/irq-starfive-jhb100-intc.c
 
+STARFIVE JHB100 PWM FAN DRIVER
+M:	Changhuang Liang <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	Documentation/devicetree/bindings/hwmon/starfive,jhb100-pwm-fan.yaml
+F:	Documentation/hwmon/starfive-pwm-fan.rst
+F:	drivers/hwmon/starfive-pwm-fan.c
+
 STATIC BRANCH/CALL
 M:	Peter Zijlstra <[email protected]>
 M:	Josh Poimboeuf <[email protected]>
drivers/hwmon/Kconfig +15 -0
index 18c682e1a475..f0d85da48956 100644
@@ -2467,6 +2467,21 @@ config SENSORS_SPD5118_DETECT
 
 	  If unsure, say Y.
 
+config SENSORS_STARFIVE_PWM_FAN
+	tristate "StarFive JHB100 PWM fan controller"
+	depends on ARCH_STARFIVE || COMPILE_TEST
+	depends on HAS_IOMEM
+	depends on PWM
+	help
+	  This driver supports the PWM fan controller found on StarFive SoCs
+	  such as the JHB100. It uses up to 8 PWM outputs supplied by an
+	  external PWM controller to control fan speed, and a tachometer block
+	  with 16 inputs to report fan speed in RPM through the hwmon sysfs
+	  interface, along with stall and low-speed alarms.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called starfive-pwm-fan.
+
 config SENSORS_TC74
 	tristate "Microchip TC74"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index d4c345ed7261..2b04216bfcf1 100644
@@ -231,6 +231,7 @@ obj-$(CONFIG_SENSORS_SMSC47M192)+= smsc47m192.o
 obj-$(CONFIG_SENSORS_ALTERA_SOCFPGA_HWMON)	+= socfpga-hwmon.o
 obj-$(CONFIG_SENSORS_SPARX5)	+= sparx5-temp.o
 obj-$(CONFIG_SENSORS_SPD5118)	+= spd5118.o
+obj-$(CONFIG_SENSORS_STARFIVE_PWM_FAN)	+= starfive-pwm-fan.o
 obj-$(CONFIG_SENSORS_STTS751)	+= stts751.o
 obj-$(CONFIG_SENSORS_SURFACE_FAN)+= surface_fan.o
 obj-$(CONFIG_SENSORS_SURFACE_TEMP)+= surface_temp.o
drivers/hwmon/starfive-pwm-fan.c new file +762 -0

762 lines not on this page · raw message

Signature · 1 line
2.25.1
#5 lore raw

Re: [PATCH v5 2/2] hwmon: (starfive-pwm-fan) Add PWM fan driver for StarFive JHB100

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] hwmon: starfive: fan tachometer register state is lost on system suspend - [Medium] hwmon…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] hwmon: starfive: fan tachometer register state is lost on system suspend
- [Medium] hwmon: starfive: aborted teardown loop in pwm_disable_all leaks hardware state --

commit ba72917646ababbe1ce560dd96b1e73eb3c6b2f3
Author: Changhuang Liang <[email protected]>
Subject: hwmon: (starfive-pwm-fan) Add PWM fan driver for StarFive JHB100

This commit adds a new PWM fan controller driver for the StarFive JHB100 SoC. It allows configuring fan speeds using up to 8 external PWM channels and provides RPM readings and alarms via a 16-channel hardware tachometer block.

Quoted 4 lines
drivers/hwmon/starfive-pwm-fan.c new file +0 -0
new file mode 100644

[ ... ]

Quoted 10 lines
drivers/hwmon/starfive-pwm-fan.c new file +10 -0
+static int starfive_pwm_fan_pwm_disable_all(struct starfive_pwm_fan_data *priv)
+{
+	struct pwm_state state;
+	int i, ret;
+
+	guard(mutex)(&priv->pwm_lock);
+
+	for (i = 0; i < STARFIVE_FAN_MAX; i++) {
+		struct starfive_pwm_fan *fan = &priv->fans[i];
+

drivers/hwmon/starfive-pwm-fan.c line 574

Quoted 10 lines
drivers/hwmon/starfive-pwm-fan.c new file +10 -0
+		if (!fan->pwm || !fan->pwm_state.enabled)
+			continue;
+
+		state = fan->pwm_state;
+		state.duty_cycle = 0;
+		state.enabled = false;
+
+		ret = pwm_apply_might_sleep(fan->pwm, &state);
+		if (ret)
+			return ret;

[Severity: Medium]
If pwm_apply_might_sleep() fails for a fan channel (for example, due to an underlying PWM controller communication error), this early return abruptly breaks the teardown loop.

Could this leave active PWM channels and fans running during driver removal or system suspend? Because a failure in one PWM channel shouldn't necessarily prevent the teardown of the remaining independent channels, would it be safer to log the error and continue disabling the others?

Quoted 4 lines
drivers/hwmon/starfive-pwm-fan.c new file +4 -0
+	}
+
+	return 0;
+}

[ ... ]

Quoted 10 lines
drivers/hwmon/starfive-pwm-fan.c new file +10 -0
+static int starfive_pwm_fan_resume(struct device *dev)
+{
+	struct starfive_pwm_fan_data *priv = dev_get_drvdata(dev);
+	int i, ret;
+
+	guard(mutex)(&priv->pwm_lock);
+
+	for (i = 0; i < STARFIVE_FAN_MAX; i++) {
+		struct starfive_pwm_fan *fan = &priv->fans[i];
+

drivers/hwmon/starfive-pwm-fan.c line 736

Quoted 10 lines
drivers/hwmon/starfive-pwm-fan.c new file +10 -0
+		if (!fan->pwm)
+			continue;
+
+		ret = pwm_apply_might_sleep(fan->pwm, &fan->pwm_state);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}

[Severity: High]
This resume handler restores the external PWM state, but does it miss restoring the internal tachometer registers (measure time, channel enables, and sysfs thresholds)?

If the peripheral's power domain is disabled during sleep, the hardware configuration will reset to power-on defaults. Could this cause fan monitoring and alarms to silently fail after a system resume? It appears the driver does not cache the user-configured threshold values written via sysfs, making it impossible to restore them upon resume.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2