Skip to contentSkip to conversation

Add JHB100 Fan-Tach support

PATCH Superseded Changhuang Liang 3 participants 14 messages Aug 19 – Aug 21

Subsystems (3)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 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 v1:
PATCH 1:
- Add explicit minItems/maxItems and range constraints for tach-ch.
- Switch fan node regex to support hex addresses.
- Add pulses-per-revolution demo.
- Rename example node to fan-controller for consistency.
- Limit the reg property to a maximum value of 15
- Fix tach-ch example by changing /bits/ 8 <0x0>, <0x8> to /bits/ 8 <0x0 0x8> so both values are parsed as 8-bit cells.

PATCH 2:
- Added hwmon documentation (starfive-fan-tach.rst)
- Added HAS_IOMEM dependency in Kconfig
- Added per-channel pulses-per-revolution DT property (default 2, range 1-4)
- Added mutex and spinlock for proper concurrency protection
- Added armed_stall/armed_slow mechanism to prevent spurious interrupts
- Added retry logic for zero sampling value in rpm read
- Improved probe sequence: clock enable before reset deassert
- Added devm action for remove path cleanup
- Added input validation for write operations
- Added MODULE_DEVICE_TABLE
- Added linux-hwmon mailing list to MAINTAINERS

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

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

 .../hwmon/starfive,jhb100-fan-tach.yaml       |  93 +++
 Documentation/hwmon/index.rst                 |   1 +
 Documentation/hwmon/starfive-fan-tach.rst     |  34 ++
 MAINTAINERS                                   |   8 +
 drivers/hwmon/Kconfig                         |  14 +
 drivers/hwmon/Makefile                        |   1 +
 drivers/hwmon/starfive-fan-tach.c             | 561 ++++++++++++++++++
 7 files changed, 712 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/starfive,jhb100-fan-tach.yaml
 create mode 100644 Documentation/hwmon/starfive-fan-tach.rst
 create mode 100644 drivers/hwmon/starfive-fan-tach.c

--
2.25.1

#1 lore raw

PATCH v2 1/2 dt-bindings: hwmon: Add starfive,jhb100-fan-tach

Add compatible "starfive,jhb100-fan-tach" for StarFive JHB100 Fan-Tach controller.

---
 .../hwmon/starfive,jhb100-fan-tach.yaml       | 93 +++++++++++++++++++
 1 file changed, 93 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/starfive,jhb100-fan-tach.yaml
Documentation/devicetree/bindings/hwmon/starfive,jhb100-fan-tach.yaml new file +93 -0
new file mode 100644
index 000000000000..1dcd4932768f
@@ -0,0 +1,93 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/hwmon/starfive,jhb100-fan-tach.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: StarFive JHB100 Fan-Tach controller
+
+maintainers:
+  - Changhuang Liang <[email protected]>
+
+description:
+  The StarFive Fan-Tach controller can support up to 16 fan tach input.
+
+properties:
+  compatible:
+    enum:
+      - starfive,jhb100-fan-tach
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  resets:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  "#address-cells":
+    const: 1
+
+  "#size-cells":
+    const: 0
+
+patternProperties:
+  "^fan@[0-9a-f]+$":
+    $ref: fan-common.yaml#
+    unevaluatedProperties: false
+
+    properties:
+      reg:
+        description:
+          PWM channel index. The driver allows two fans to share the same
+          PWM channel, or each fan to use a dedicated channel.
+        maximum: 15
+
+      tach-ch:
+        description:
+          The tachometer channel(s) wired to the fan(s) driven by this PWM
+          channel. Integer values in the range 0-15.
+        minItems: 1
+        maxItems: 2
+        items:
+          maximum: 15
+
+    required:
+      - reg
+      - tach-ch
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - resets
+  - interrupts
+
+additionalProperties: false
+
+examples:
+  - |
+    fan-controller@11be0000 {
+        compatible = "starfive,jhb100-fan-tach";
+        reg = <0x11be0000 0x400>;
+        interrupts = <128>;
+        clocks = <&per2crg 33>;
+        resets = <&per2crg 5>;
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        fan@0 {
+            reg = <0x0>;
+            tach-ch = /bits/ 8 <0x0 0x8>;
+            pulses-per-revolution = <2>;
+        };
+        fan@1 {
+            reg = <0x1>;
+            tach-ch = /bits/ 8 <0x1>;
+            pulses-per-revolution = <2>;
+        };
+    };
Signature · 1 line
2.25.1
#2 lore raw

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

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

Add compatible "starfive,jhb100-fan-tach" for StarFive JHB100 Fan-Tach controller.

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
#3 lore raw

PATCH v2 2/2 hwmon: (starfive-fan-tach) Add fan tach driver for StarFive JHB100

In reply to Changhuang Liang

Add fan tach driver for StarFive JHB100 SoC.

The controller supports up to 16 independent fan tachometer 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() than reset_control_deassert()
remove: clk_disable_unprepare() than reset_control_assert()

---
 Documentation/hwmon/index.rst             |   1 +
 Documentation/hwmon/starfive-fan-tach.rst |  34 ++
 MAINTAINERS                               |   8 +
 drivers/hwmon/Kconfig                     |  14 +
 drivers/hwmon/Makefile                    |   1 +
 drivers/hwmon/starfive-fan-tach.c         | 561 ++++++++++++++++++++++
 6 files changed, 619 insertions(+)
 create mode 100644 Documentation/hwmon/starfive-fan-tach.rst
 create mode 100644 drivers/hwmon/starfive-fan-tach.c
Documentation/hwmon/index.rst +1 -0
index 29130df44d12..0d64e4cf1051 100644
@@ -253,6 +253,7 @@ Hardware Monitoring Kernel Drivers
    smsc47m1
    sparx5-temp
    spd5118
+   starfive-fan-tach
    stpddc60
    surface_fan
    sy7636a-hwmon
Documentation/hwmon/starfive-fan-tach.rst new file +34 -0
new file mode 100644
index 000000000000..5ca03b52ad58
@@ -0,0 +1,34 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+Kernel driver starfive-fan-tach
+===============================
+
+Supported chips:
+
+  * StarFive JHB100
+
+    Prefix: 'starfive_fan_tach'
+
+    Addresses scanned: -
+
+Authors:
+      - Changhuang Liang <[email protected]>
+
+Description
+-----------
+
+This driver implements support for the fan tachometer controller found on
+the StarFive JHB100 SoC. The controller supports up to 16 independent fan
+tachometer inputs. Each tachometer channel measures the number of pulses
+within a fixed 100 ms window.
+
+Sysfs entries
+-------------
+
+==================== =======================================================
+fan[1-16]_input      Fan speed in RPM (read-only)
+fan[1-16]_min        Lower fan speed limit in RPM (read/write)
+fan[1-16]_enable     Enable/disable the tachometer channel (read/write)
+fan[1-16]_fault      Fan stall indication (read-only)
+fan[1-16]_min_alarm  Fan speed below fan[1-16]_min (read-only)
+==================== =======================================================
MAINTAINERS +8 -0
index c545aa121d51..83318d2e3e2e 100644
@@ -25897,6 +25897,14 @@ S:	Supported
 F:	Documentation/devicetree/bindings/interrupt-controller/starfive,jhb100-intc.yaml
 F:	drivers/irqchip/irq-starfive-jhb100-intc.c
 
+STARFIVE JHB100 FAN-TACH DRIVER
+M:	Changhuang Liang <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	Documentation/devicetree/bindings/hwmon/starfive,jhb100-fan-tach.yaml
+F:	Documentation/hwmon/starfive-fan-tach.rst
+F:	drivers/hwmon/starfive-fan-tach.c
+
 STATIC BRANCH/CALL
 M:	Peter Zijlstra <[email protected]>
 M:	Josh Poimboeuf <[email protected]>
drivers/hwmon/Kconfig +14 -0
index 5c2d3ff5fce8..2c0000f8e777 100644
@@ -2383,6 +2383,20 @@ config SENSORS_SPD5118_DETECT
 
 	  If unsure, say Y.
 
+config SENSORS_STARFIVE_FAN_TACH
+	tristate "StarFive Fan-Tach driver"
+	depends on ARCH_STARFIVE || COMPILE_TEST
+	depends on HAS_IOMEM
+	help
+	  This driver provides support for the fan tachometer controller
+	  found on StarFive SoCs such as the JHB100. The controller supports
+	  up to 16 independent fan tachometer inputs and reports 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-fan-tach.
+
 config SENSORS_TC74
 	tristate "Microchip TC74"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index 63effc0ab8d1..80b2704b94e5 100644
@@ -223,6 +223,7 @@ obj-$(CONFIG_SENSORS_SMSC47M1)	+= smsc47m1.o
 obj-$(CONFIG_SENSORS_SMSC47M192)+= smsc47m192.o
 obj-$(CONFIG_SENSORS_SPARX5)	+= sparx5-temp.o
 obj-$(CONFIG_SENSORS_SPD5118)	+= spd5118.o
+obj-$(CONFIG_SENSORS_STARFIVE_FAN_TACH)	+= starfive-fan-tach.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-fan-tach.c new file +561 -0

561 lines not on this page · raw message

Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH v2 2/2] hwmon: (starfive-fan-tach) Add fan tach driver for StarFive JHB100

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Alarm attributes violate the hwmon ABI (sticky semantics) and hold a global mutex across a 200ms…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Alarm attributes violate the hwmon ABI (sticky semantics) and hold a global mutex across a 200ms sleep, forcing strict serialization and massive latency. --

--- Patch [2]: [PATCH v2 2/2] hwmon: (starfive-fan-tach) Add fan tach driver for StarFive JHB100 --- commit f832cfa14cd6c4c786efa0a34afc865eca6dcf71
Author: Changhuang Liang <[email protected]>
Subject: hwmon: (starfive-fan-tach) Add fan tach driver for StarFive JHB100

This commit adds a fan tachometer driver for the StarFive JHB100 SoC. It supports up to 16 independent fan tachometer inputs and reports fan speed, stall, and low-speed alarms through the hwmon sysfs interface.

Quoted 5 lines
drivers/hwmon/starfive-fan-tach.c new file +0 -0
new file mode 100644
index 0000000000000..1e6399db424dc

[ ... ]

Quoted 14 lines
drivers/hwmon/starfive-fan-tach.c new file +14 -0
+static int starfive_fan_tach_hwmon_read(struct device *dev,
+					enum hwmon_sensor_types type, u32 attr,
+					int channel, long *val)
+{
+	struct starfive_fan_tach_data *priv = dev_get_drvdata(dev);
+	int ret = 0;
+
+	switch (attr) {
+	case hwmon_fan_fault:
+		scoped_guard(mutex, &priv->lock) {
+			scoped_guard(spinlock_irqsave, &priv->irq_lock) {
+				writel(STARFIVE_FAN_TACH_STALL_INT(channel),
+				       priv->regs + STARFIVE_FAN_TACH_STATUS);
+				/* clear fan_stall first */

drivers/hwmon/starfive-fan-tach.c line 240

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+				priv->fan_stall[channel] = 0;
+				reinit_completion(&priv->comp_stall[channel]);
+				priv->armed_stall |= BIT(channel);
+			}
+
+			starfive_fan_tach_ch_stall_unmask(priv, channel, true);
+
+			/* Waiting for hardware to measure */
+			wait_for_completion_timeout(&priv->comp_stall[channel],
+						    2 * STARFIVE_FAN_TACH_TIMEOUT_JIFFIES);

[Severity: Medium]
Does reading the fault attribute violate the hwmon ABI by blocking to poll the hardware instead of returning instantaneous sticky status?

Also, because wait_for_completion_timeout() is called inside scoped_guard(mutex, &priv->lock), doesn't this hold the global mutex across a 200ms sleep?

If a user tool (like sensors) sequentially reads the alarms for all 16 channels, could this stall the reader for over 6 seconds and block all other concurrent reads/writes to this device?

[ ... ]

Quoted 6 lines
drivers/hwmon/starfive-fan-tach.c new file +6 -0
+	case hwmon_fan_min_alarm:
+		scoped_guard(mutex, &priv->lock) {
+			scoped_guard(spinlock_irqsave, &priv->irq_lock) {
+				writel(STARFIVE_FAN_TACH_SLOW_INT(channel),
+				       priv->regs + STARFIVE_FAN_TACH_STATUS);
+				/* clear fan_slow first */

drivers/hwmon/starfive-fan-tach.c line 280

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+				priv->fan_slow[channel] = 0;
+				reinit_completion(&priv->comp_slow[channel]);
+				priv->armed_slow |= BIT(channel);
+			}
+
+			starfive_fan_tach_ch_slow_unmask(priv, channel, true);
+
+			/* Waiting for hardware to measure */
+			wait_for_completion_timeout(&priv->comp_slow[channel],
+						    2 * STARFIVE_FAN_TACH_TIMEOUT_JIFFIES);

[Severity: Medium]
Similar to the fault attribute above, does reading the min_alarm attribute violate the hwmon ABI by blocking instead of returning a sticky status?

Does holding the global mutex during this sleep similarly risk blocking concurrent device accesses and causing long stalls when reading multiple channels?

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

Re: [PATCH v2 2/2] hwmon: (starfive-fan-tach) Add fan tach driver for StarFive JHB100

In reply to Changhuang Liang
Quoted 9 lines · Changhuang Liang

Add fan tach driver for StarFive JHB100 SoC.

The controller supports up to 16 independent fan tachometer 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() than reset_control_deassert()
remove: clk_disable_unprepare() than reset_control_assert()

s/than/then/

Quoted 188 lines
---
  Documentation/hwmon/index.rst             |   1 +
  Documentation/hwmon/starfive-fan-tach.rst |  34 ++
  MAINTAINERS                               |   8 +
  drivers/hwmon/Kconfig                     |  14 +
  drivers/hwmon/Makefile                    |   1 +
  drivers/hwmon/starfive-fan-tach.c         | 561 ++++++++++++++++++++++
  6 files changed, 619 insertions(+)

create mode 100644 Documentation/hwmon/starfive-fan-tach.rst create mode 100644 drivers/hwmon/starfive-fan-tach.c

Documentation/hwmon/index.rst +1 -0
index 29130df44d12..0d64e4cf1051 100644
@@ -253,6 +253,7 @@ Hardware Monitoring Kernel Drivers
     smsc47m1
     sparx5-temp
     spd5118
+   starfive-fan-tach
     stpddc60
     surface_fan
     sy7636a-hwmon
Documentation/hwmon/starfive-fan-tach.rst new file +34 -0
new file mode 100644
index 000000000000..5ca03b52ad58
@@ -0,0 +1,34 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+Kernel driver starfive-fan-tach
+===============================
+
+Supported chips:
+
+  * StarFive JHB100
+
+    Prefix: 'starfive_fan_tach'
+
+    Addresses scanned: -
+
+Authors:
+      - Changhuang Liang <[email protected]>
+
+Description
+-----------
+
+This driver implements support for the fan tachometer controller found on
+the StarFive JHB100 SoC. The controller supports up to 16 independent fan
+tachometer inputs. Each tachometer channel measures the number of pulses
+within a fixed 100 ms window.
+
+Sysfs entries
+-------------
+
+==================== =======================================================
+fan[1-16]_input      Fan speed in RPM (read-only)
+fan[1-16]_min        Lower fan speed limit in RPM (read/write)
+fan[1-16]_enable     Enable/disable the tachometer channel (read/write)
+fan[1-16]_fault      Fan stall indication (read-only)
+fan[1-16]_min_alarm  Fan speed below fan[1-16]_min (read-only)
+==================== =======================================================
MAINTAINERS +8 -0
index c545aa121d51..83318d2e3e2e 100644
@@ -25897,6 +25897,14 @@ S:	Supported
  F:	Documentation/devicetree/bindings/interrupt-controller/starfive,jhb100-intc.yaml
  F:	drivers/irqchip/irq-starfive-jhb100-intc.c
 
+STARFIVE JHB100 FAN-TACH DRIVER
+M:	Changhuang Liang <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	Documentation/devicetree/bindings/hwmon/starfive,jhb100-fan-tach.yaml
+F:	Documentation/hwmon/starfive-fan-tach.rst
+F:	drivers/hwmon/starfive-fan-tach.c
+
  STATIC BRANCH/CALL
  M:	Peter Zijlstra <[email protected]>
  M:	Josh Poimboeuf <[email protected]>
drivers/hwmon/Kconfig +14 -0
index 5c2d3ff5fce8..2c0000f8e777 100644
@@ -2383,6 +2383,20 @@ config SENSORS_SPD5118_DETECT
 
  	  If unsure, say Y.
 
+config SENSORS_STARFIVE_FAN_TACH
+	tristate "StarFive Fan-Tach driver"
+	depends on ARCH_STARFIVE || COMPILE_TEST
+	depends on HAS_IOMEM
+	help
+	  This driver provides support for the fan tachometer controller
+	  found on StarFive SoCs such as the JHB100. The controller supports
+	  up to 16 independent fan tachometer inputs and reports 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-fan-tach.
+
  config SENSORS_TC74
  	tristate "Microchip TC74"
  	depends on I2C
drivers/hwmon/Makefile +1 -0
index 63effc0ab8d1..80b2704b94e5 100644
@@ -223,6 +223,7 @@ obj-$(CONFIG_SENSORS_SMSC47M1)	+= smsc47m1.o
  obj-$(CONFIG_SENSORS_SMSC47M192)+= smsc47m192.o
  obj-$(CONFIG_SENSORS_SPARX5)	+= sparx5-temp.o
  obj-$(CONFIG_SENSORS_SPD5118)	+= spd5118.o
+obj-$(CONFIG_SENSORS_STARFIVE_FAN_TACH)	+= starfive-fan-tach.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-fan-tach.c new file +59 -0
new file mode 100644
index 000000000000..1e6399db424d
@@ -0,0 +1,561 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * FAN-TACH controller driver for StarFive JHB100
+ *
+ * Copyright (C) 2018-2026 StarFive Technology Co., Ltd.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/completion.h>
+#include <linux/delay.h>
+#include <linux/errno.h>
+#include <linux/hwmon.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>
+#include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/math64.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/reset.h>
+#include <linux/spinlock.h>
+
+#define STARFIVE_FAN_TACH_CH			16
+
+/* Fan-tach register offest */
+#define STARFIVE_FAN_TACH_STATUS		0x0c
+
+#define STARFIVE_FAN_TACH_SPEED(ch)		(((ch) * 0x04) + 0x10)
+#define STARFIVE_FAN_TACH_SPEED_VALID		BIT(31)
+#define STARFIVE_FAN_TACH_VALUE_MASK		GENMASK(30, 0)
+
+#define STARFIVE_FAN_TACH_THRESHOLD(ch)		(((ch) * 0x04) + 0x50)
+
+#define STARFIVE_FAN_TACH_INT_EN		0x90
+#define STARFIVE_FAN_TACH_STALL_INT_MASK	GENMASK(15, 0)
+#define STARFIVE_FAN_TACH_SLOW_INT_MASK		GENMASK(31, 16)
+#define STARFIVE_FAN_TACH_STALL_INT(ch)		BIT(ch)
+#define STARFIVE_FAN_TACH_SLOW_INT(ch)		(BIT(ch) << 16)
+
+#define STARFIVE_FAN_TACH_MEASURE_TIME		0x94
+
+#define STARFIVE_FAN_TACH_CH_EN			0x98
+#define STARFIVE_FAN_TACH_EN(ch)		BIT(ch)
+
+#define STARFIVE_FAN_DEFAULT_PULSE_PR		2
+#define STARFIVE_FAN_DEFAULT_MEASURE_RATIO	10
+#define STARFIVE_FAN_DEFAULT_RPM_PAUSE_TIME	(60 * STARFIVE_FAN_DEFAULT_MEASURE_RATIO)
+
+#define STARFIVE_FAN_TACH_TIMEOUT \
+	(USEC_PER_SEC / STARFIVE_FAN_DEFAULT_MEASURE_RATIO)
+
+#define STARFIVE_FAN_TACH_TIMEOUT_JIFFIES \
+	(msecs_to_jiffies(1000) / STARFIVE_FAN_DEFAULT_MEASURE_RATIO)
+
+#define FAN_ATTRIBUTE_SET \

drivers/hwmon/starfive-fan-tach.c line 69

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+	(HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_ENABLE | \
+	 HWMON_F_FAULT | HWMON_F_MIN_ALARM)
+
+struct starfive_fan_tach_data {
+	struct device *dev;
+	/*
+	 * Serialises the read-modify-write sequences on CH_EN and INT_EN,
+	 * and the arm/disarm window of the fault/min_alarm read paths.
+	 */
+	struct mutex lock;

I don't see why this would be needed on top of the hwmon subsystem lock.

Quoted 129 lines
drivers/hwmon/starfive-fan-tach.c new file +129 -0
+	void __iomem *regs;
+	struct reset_control *rst;
+	struct clk *clk;
+	bool tach_present[STARFIVE_FAN_TACH_CH];
+	unsigned long clk_rate; /* APB clock frequency */
+	u8 pulses_per_rev[STARFIVE_FAN_TACH_CH];
+	struct completion comp_stall[STARFIVE_FAN_TACH_CH];
+	struct completion comp_slow[STARFIVE_FAN_TACH_CH];
+	u8 fan_stall[STARFIVE_FAN_TACH_CH];
+	u8 fan_slow[STARFIVE_FAN_TACH_CH];
+	u32 armed_stall;
+	u32 armed_slow;
+	/* protects fan_stall[]/fan_slow[]/armed_* and the STATUS register */
+	spinlock_t irq_lock;
+};
+
+static void starfive_fan_tach_ch_enable(struct starfive_fan_tach_data *priv, u8 tach_ch,
+					bool enable)
+{
+	if (enable) {
+		writel(readl(priv->regs + STARFIVE_FAN_TACH_CH_EN) |
+		       STARFIVE_FAN_TACH_EN(tach_ch),
+		       priv->regs + STARFIVE_FAN_TACH_CH_EN);
+	} else {
+		writel(readl(priv->regs + STARFIVE_FAN_TACH_CH_EN) &
+		       ~(STARFIVE_FAN_TACH_EN(tach_ch)),
+		       priv->regs + STARFIVE_FAN_TACH_CH_EN);
+	}
+}
+
+static void starfive_fan_tach_ch_stall_unmask(struct starfive_fan_tach_data *priv, u8 tach_ch,
+					      bool unmask)
+{
+	if (unmask) {
+		writel(readl(priv->regs + STARFIVE_FAN_TACH_INT_EN) |
+		       STARFIVE_FAN_TACH_STALL_INT(tach_ch),
+		       priv->regs + STARFIVE_FAN_TACH_INT_EN);
+	} else {
+		writel(readl(priv->regs + STARFIVE_FAN_TACH_INT_EN) &
+		       ~STARFIVE_FAN_TACH_STALL_INT(tach_ch),
+		       priv->regs + STARFIVE_FAN_TACH_INT_EN);
+	}
+}
+
+static void starfive_fan_tach_ch_slow_unmask(struct starfive_fan_tach_data *priv, u8 tach_ch,
+					     bool unmask)
+{
+	if (unmask) {
+		writel(readl(priv->regs + STARFIVE_FAN_TACH_INT_EN) |
+		       STARFIVE_FAN_TACH_SLOW_INT(tach_ch),
+		       priv->regs + STARFIVE_FAN_TACH_INT_EN);
+	} else {
+		writel(readl(priv->regs + STARFIVE_FAN_TACH_INT_EN) &
+		       ~STARFIVE_FAN_TACH_SLOW_INT(tach_ch),
+		       priv->regs + STARFIVE_FAN_TACH_INT_EN);
+	}
+}
+
+static u32 starfive_fan_tach_rpm_to_val(struct starfive_fan_tach_data *priv, u8 tach_ch, u32 rpm)
+{
+	u64 tach_val;
+	u8 ppr;
+
+	ppr = priv->pulses_per_rev[tach_ch];
+
+	tach_val = (u64)rpm * ppr;
+	tach_val = div_u64(tach_val, STARFIVE_FAN_DEFAULT_RPM_PAUSE_TIME);
+
+	return min_t(u64, tach_val, STARFIVE_FAN_TACH_VALUE_MASK);
+}
+
+static long starfive_fan_tach_val_to_rpm(struct starfive_fan_tach_data *priv, u8 tach_ch,
+					 u32 tach_val)
+{
+	u64 rpm;
+	u8 ppr;
+
+	ppr = priv->pulses_per_rev[tach_ch];
+
+	rpm = (u64)tach_val * STARFIVE_FAN_DEFAULT_RPM_PAUSE_TIME;
+
+	return div_u64(rpm, ppr);
+}
+
+static int starfive_fan_tach_get_rpm(struct starfive_fan_tach_data *priv,
+				     u8 tach_ch, long *rpm)
+{
+	u32 val;
+	int ret;
+
+	ret = readl_poll_timeout(priv->regs + STARFIVE_FAN_TACH_SPEED(tach_ch),
+				 val, val & STARFIVE_FAN_TACH_SPEED_VALID,
+				 100, STARFIVE_FAN_TACH_TIMEOUT);
+	if (ret)
+		return -ENODATA;
+
+	val = FIELD_GET(STARFIVE_FAN_TACH_VALUE_MASK, val);
+	if (!val) {
+		/* Sampling = 0 may mean no valid capture; wait 1.5 more cycles for valid read. */
+		fsleep(STARFIVE_FAN_TACH_TIMEOUT + STARFIVE_FAN_TACH_TIMEOUT / 2);
+		ret = readl_poll_timeout(priv->regs + STARFIVE_FAN_TACH_SPEED(tach_ch),
+					 val, val & STARFIVE_FAN_TACH_SPEED_VALID,
+					 100, STARFIVE_FAN_TACH_TIMEOUT);
+		if (ret)
+			return -ENODATA;
+
+		val = FIELD_GET(STARFIVE_FAN_TACH_VALUE_MASK, val);
+	}
+
+	*rpm = starfive_fan_tach_val_to_rpm(priv, tach_ch, val);
+
+	return 0;
+}
+
+static long starfive_fan_tach_get_rpm_threshold(struct starfive_fan_tach_data *priv,
+						u8 tach_ch)
+{
+	u32 val;
+
+	val = readl(priv->regs + STARFIVE_FAN_TACH_THRESHOLD(tach_ch));
+
+	val = FIELD_GET(STARFIVE_FAN_TACH_VALUE_MASK, val);
+
+	return starfive_fan_tach_val_to_rpm(priv, tach_ch, val);
+}
+
+static void starfive_set_tach_rpm_threshold(struct starfive_fan_tach_data *priv,
+					    u8 tach_ch, u32 val)
+{

drivers/hwmon/starfive-fan-tach.c line 208

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+	u32 reg_val;
+
+	reg_val = starfive_fan_tach_rpm_to_val(priv, tach_ch, val);
+	reg_val = clamp_val(reg_val, 1, STARFIVE_FAN_TACH_VALUE_MASK);
+
+	writel(reg_val, priv->regs + STARFIVE_FAN_TACH_THRESHOLD(tach_ch));
+}
+
+static int starfive_fan_tach_get_ch_en(struct starfive_fan_tach_data *priv,
+				       u8 tach_ch)

Why not bool ?

drivers/hwmon/starfive-fan-tach.c line 214

Quoted 6 lines
drivers/hwmon/starfive-fan-tach.c new file +6 -0
+{
+	u32 enable;
+
+	enable = readl(priv->regs + STARFIVE_FAN_TACH_CH_EN);
+
+	return (enable & (1 << tach_ch)) ? 1 : 0;
	return !!(enable & STARFIVE_FAN_TACH_EN(tach_ch));
Quoted 25 lines
drivers/hwmon/starfive-fan-tach.c new file +25 -0
+}
+
+static int starfive_fan_tach_hwmon_read(struct device *dev,
+					enum hwmon_sensor_types type, u32 attr,
+					int channel, long *val)
+{
+	struct starfive_fan_tach_data *priv = dev_get_drvdata(dev);
+	int ret = 0;
+
+	switch (attr) {
+	case hwmon_fan_fault:
+		scoped_guard(mutex, &priv->lock) {
+			scoped_guard(spinlock_irqsave, &priv->irq_lock) {
+				writel(STARFIVE_FAN_TACH_STALL_INT(channel),
+				       priv->regs + STARFIVE_FAN_TACH_STATUS);
+				/* clear fan_stall first */
+				priv->fan_stall[channel] = 0;
+				reinit_completion(&priv->comp_stall[channel]);
+				priv->armed_stall |= BIT(channel);
+			}
+
+			starfive_fan_tach_ch_stall_unmask(priv, channel, true);
+
+			/* Waiting for hardware to measure */
+			wait_for_completion_timeout(&priv->comp_stall[channel],

drivers/hwmon/starfive-fan-tach.c line 249

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+						    2 * STARFIVE_FAN_TACH_TIMEOUT_JIFFIES);
+
+			starfive_fan_tach_ch_stall_unmask(priv, channel, false);
+
+			scoped_guard(spinlock_irqsave, &priv->irq_lock) {
+				priv->armed_stall &= ~BIT(channel);
+				*val = priv->fan_stall[channel];
+			}
+		}
+

I'd really be interested to see how long it takes to read the alarm and fault status of all fans. And, yes, Sashiko has a point: the attributes are supported to report a sticky status, not something that is calculated on the fly. Is this based on some heuristics ? I don't claim to understand how fault and min_alarm are calculated. A comment in the code describing how this works would be helpful.

drivers/hwmon/starfive-fan-tach.c line 254

Quoted 5 lines
drivers/hwmon/starfive-fan-tach.c new file +5 -0
+		break;
+	case hwmon_fan_input:
+		ret = starfive_fan_tach_get_ch_en(priv, channel);
+		if (!ret)
+			return -ENODATA;

I don't see why the assignment to ret would add value here.

		if (!starfive_fan_tach_get_ch_en(priv, channel))
			return -ENODATA;
Quoted 141 lines
drivers/hwmon/starfive-fan-tach.c new file +141 -0
+
+		ret = starfive_fan_tach_get_rpm(priv, channel, val);
+		if (ret < 0)
+			return ret;
+
+		break;
+	case hwmon_fan_min:
+		*val = starfive_fan_tach_get_rpm_threshold(priv, channel);
+
+		break;
+	case hwmon_fan_min_alarm:
+		scoped_guard(mutex, &priv->lock) {
+			scoped_guard(spinlock_irqsave, &priv->irq_lock) {
+				writel(STARFIVE_FAN_TACH_SLOW_INT(channel),
+				       priv->regs + STARFIVE_FAN_TACH_STATUS);
+				/* clear fan_slow first */
+				priv->fan_slow[channel] = 0;
+				reinit_completion(&priv->comp_slow[channel]);
+				priv->armed_slow |= BIT(channel);
+			}
+
+			starfive_fan_tach_ch_slow_unmask(priv, channel, true);
+
+			/* Waiting for hardware to measure */
+			wait_for_completion_timeout(&priv->comp_slow[channel],
+						    2 * STARFIVE_FAN_TACH_TIMEOUT_JIFFIES);
+
+			starfive_fan_tach_ch_slow_unmask(priv, channel, false);
+
+			scoped_guard(spinlock_irqsave, &priv->irq_lock) {
+				priv->armed_slow &= ~BIT(channel);
+				*val = priv->fan_slow[channel];
+			}
+		}
+
+		break;
+	case hwmon_fan_enable:
+		*val = starfive_fan_tach_get_ch_en(priv, channel);
+
+		break;
+	default:
+		return -EOPNOTSUPP;
+	}
+
+	return 0;
+}
+
+static int starfive_fan_tach_hwmon_write(struct device *dev,
+					 enum hwmon_sensor_types type, u32 attr,
+					 int channel, long val)
+{
+	struct starfive_fan_tach_data *priv = dev_get_drvdata(dev);
+
+	guard(mutex)(&priv->lock);
+
+	switch (attr) {
+	case hwmon_fan_min:
+		if (val < 0 || val > U32_MAX)
+			return -EINVAL;
+
+		starfive_set_tach_rpm_threshold(priv, channel, val);
+		break;
+	case hwmon_fan_enable:
+		if (val != 0 && val != 1)
+			return -EINVAL;
+
+		starfive_fan_tach_ch_enable(priv, channel, val);
+		break;
+	default:
+		return -EOPNOTSUPP;
+	}
+
+	return 0;
+}
+
+static umode_t starfive_fan_tach_dev_is_visible(const void *drvdata,
+						enum hwmon_sensor_types type,
+						u32 attr, int channel)
+{
+	const struct starfive_fan_tach_data *priv = drvdata;
+
+	if (!priv->tach_present[channel])
+		return 0;
+
+	switch (attr) {
+	case hwmon_fan_input:
+	case hwmon_fan_fault:
+	case hwmon_fan_min_alarm:
+		return 0444;
+	case hwmon_fan_min:
+	case hwmon_fan_enable:
+		return 0644;
+	}
+
+	return 0;
+}
+
+static const struct hwmon_ops starfive_fan_tach_ops = {
+	.is_visible = starfive_fan_tach_dev_is_visible,
+	.read = starfive_fan_tach_hwmon_read,
+	.write = starfive_fan_tach_hwmon_write
+};
+
+static const struct hwmon_channel_info *starfive_fan_tach_info[] = {
+	HWMON_CHANNEL_INFO(fan,
+			   FAN_ATTRIBUTE_SET, FAN_ATTRIBUTE_SET,
+			   FAN_ATTRIBUTE_SET, FAN_ATTRIBUTE_SET,
+			   FAN_ATTRIBUTE_SET, FAN_ATTRIBUTE_SET,
+			   FAN_ATTRIBUTE_SET, FAN_ATTRIBUTE_SET,
+			   FAN_ATTRIBUTE_SET, FAN_ATTRIBUTE_SET,
+			   FAN_ATTRIBUTE_SET, FAN_ATTRIBUTE_SET,
+			   FAN_ATTRIBUTE_SET, FAN_ATTRIBUTE_SET,
+			   FAN_ATTRIBUTE_SET, FAN_ATTRIBUTE_SET),
+	NULL
+};
+
+static const struct hwmon_chip_info starfive_fan_tach_chip_info = {
+	.ops = &starfive_fan_tach_ops,
+	.info = starfive_fan_tach_info,
+};
+
+static int starfive_fan_tach_create_fan(struct device *dev, struct device_node *child,
+					struct starfive_fan_tach_data *priv)
+{
+	u8 tach_ch[STARFIVE_FAN_TACH_CH];
+	int ret, count;
+	u32 ppr, index;
+
+	count = of_property_count_u8_elems(child, "tach-ch");
+	if (count < 1 || count > STARFIVE_FAN_TACH_CH)
+		return -EINVAL;
+
+	ret = of_property_read_u8_array(child, "tach-ch", tach_ch, count);
+	if (ret)
+		return ret;
+
+	/* Parse pulses-per-revolution, default to 2 if not specified */
+	ppr = STARFIVE_FAN_DEFAULT_PULSE_PR;
+	of_property_read_u32(child, "pulses-per-revolution", &ppr);
+
+	if (!ppr || ppr > 4) {

drivers/hwmon/starfive-fan-tach.c line 405

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+		dev_err(dev, "Invalid pulses-per-revolution %u, must be 1-4\n", ppr);
+		return -EINVAL;
+	}
+
+	for (index = 0; index < count; index++) {
+		u8 ch = tach_ch[index];
+
+		if (ch >= STARFIVE_FAN_TACH_CH) {
+			dev_warn(priv->dev, "Invalid tach-ch %d, skipping\n", ch);
+			continue;

This should also return an error. Also, this is the only use of priv->dev, and priv->dev == dev. priv->dev is therefore unnecessary.

drivers/hwmon/starfive-fan-tach.c line 408

Quoted 3 lines
drivers/hwmon/starfive-fan-tach.c new file +3 -0
+		}
+
+		priv->pulses_per_rev[ch] = ppr;

What is the point of storing the same ppr value separately for each channel ?

Quoted 153 lines
drivers/hwmon/starfive-fan-tach.c new file +153 -0
+		priv->tach_present[ch] = true;
+		starfive_fan_tach_ch_enable(priv, ch, true);
+	}
+
+	return 0;
+}
+
+static irqreturn_t starfive_fan_tach_irq(int irq, void *dev_id)
+{
+	struct starfive_fan_tach_data *priv = dev_id;
+	u32 fan_status;
+	int i;
+
+	guard(spinlock)(&priv->irq_lock);
+
+	fan_status = readl(priv->regs + STARFIVE_FAN_TACH_STATUS);
+	if (!fan_status)
+		return IRQ_NONE;
+
+	writel(fan_status, priv->regs + STARFIVE_FAN_TACH_STATUS);
+
+	for (i = 0; i < STARFIVE_FAN_TACH_CH; i++) {
+		if (!priv->tach_present[i])
+			continue;
+
+		if ((fan_status & STARFIVE_FAN_TACH_STALL_INT(i)) &&
+		    (priv->armed_stall & BIT(i))) {
+			priv->fan_stall[i] = 1;
+			complete(&priv->comp_stall[i]);
+		}
+
+		if ((fan_status & STARFIVE_FAN_TACH_SLOW_INT(i)) &&
+		    (priv->armed_slow & BIT(i))) {
+			priv->fan_slow[i] = 1;
+			complete(&priv->comp_slow[i]);
+		}
+	}
+
+	return IRQ_HANDLED;
+}
+
+static void starfive_fan_tach_disable(void *data)
+{
+	struct starfive_fan_tach_data *priv = data;
+
+	clk_disable_unprepare(priv->clk);
+	reset_control_assert(priv->rst);
+}
+
+static int starfive_fan_tach_probe(struct platform_device *pdev)
+{
+	struct device *hwmon_dev, *dev = &pdev->dev;
+	struct starfive_fan_tach_data *priv;
+	int irq;
+	int ret;
+	u32 i;
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	priv->dev = dev;
+	spin_lock_init(&priv->irq_lock);
+
+	for (i = 0; i < STARFIVE_FAN_TACH_CH; i++) {
+		init_completion(&priv->comp_stall[i]);
+		init_completion(&priv->comp_slow[i]);
+	}
+
+	priv->regs = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(priv->regs))
+		return dev_err_probe(dev, PTR_ERR(priv->regs),
+				     "Unable to map IO resources\n");
+
+	priv->clk = devm_clk_get(dev, NULL);
+	if (IS_ERR(priv->clk))
+		return dev_err_probe(dev, PTR_ERR(priv->clk),
+				     "Unable to get fan tach's clock\n");
+
+	priv->rst = devm_reset_control_get_exclusive(dev, NULL);
+	if (IS_ERR(priv->rst))
+		return dev_err_probe(dev, PTR_ERR(priv->rst),
+				     "Unable to get fan tach's reset\n");
+
+	ret = clk_prepare_enable(priv->clk);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to enable clock\n");
+
+	priv->clk_rate = clk_get_rate(priv->clk);
+	if (!priv->clk_rate) {
+		clk_disable_unprepare(priv->clk);
+		return dev_err_probe(dev, -EINVAL, "Unable to get clock's rate\n");
+	}
+
+	ret = reset_control_deassert(priv->rst);
+	if (ret) {
+		clk_disable_unprepare(priv->clk);
+		return dev_err_probe(dev, ret, "Failed to deassert reset\n");
+	}
+
+	ret = devm_add_action_or_reset(dev, starfive_fan_tach_disable, priv);
+	if (ret)
+		return ret;
+
+	ret = devm_mutex_init(dev, &priv->lock);
+	if (ret)
+		return ret;
+
+	writel(STARFIVE_FAN_TACH_STALL_INT_MASK | STARFIVE_FAN_TACH_SLOW_INT_MASK,
+	       priv->regs + STARFIVE_FAN_TACH_STATUS);
+	writel(priv->clk_rate / STARFIVE_FAN_DEFAULT_MEASURE_RATIO,
+	       priv->regs + STARFIVE_FAN_TACH_MEASURE_TIME);
+
+	for_each_child_of_node_scoped(dev->of_node, child) {
+		ret = starfive_fan_tach_create_fan(dev, child, priv);
+		if (ret)
+			return dev_err_probe(dev, ret, "Failed to create fan %pOF\n", child);
+	}
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0)
+		return dev_err_probe(dev, irq, "Failed to get IRQ\n");
+
+	ret = devm_request_irq(dev, irq, starfive_fan_tach_irq, 0, pdev->name, priv);
+	if (ret)
+		return ret;
+
+	hwmon_dev = devm_hwmon_device_register_with_info(dev, "starfive_fan_tach",
+							 priv, &starfive_fan_tach_chip_info,
+							 NULL);
+	return PTR_ERR_OR_ZERO(hwmon_dev);
+}
+
+static const struct of_device_id starfive_fan_tach_of_match[] = {
+	{ .compatible = "starfive,jhb100-fan-tach", },
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, starfive_fan_tach_of_match);
+
+static struct platform_driver starfive_fan_tach_driver = {
+	.probe = starfive_fan_tach_probe,
+	.driver	= {
+		.name = "starfive-fan-tach",
+		.of_match_table = starfive_fan_tach_of_match,
+	},
+};
+
+module_platform_driver(starfive_fan_tach_driver);
+
+MODULE_AUTHOR("William Qiu <[email protected]>");
+MODULE_AUTHOR("Changhuang Liang <[email protected]>");
+MODULE_DESCRIPTION("StarFive JHB100 Fan Tach device driver");
+MODULE_LICENSE("GPL");
maintainer
#6 lore raw

Re: [PATCH v2 2/2] hwmon: (starfive-fan-tach) Add fan tach driver for StarFive JHB100

Quoted 18 lines · Guenter Roeck
Quoted 9 lines · Changhuang Liang

Add fan tach driver for StarFive JHB100 SoC.

The controller supports up to 16 independent fan tachometer 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() than reset_control_deassert()
   remove: clk_disable_unprepare() than reset_control_assert()

s/than/then/

Quoted 5 lines

---

...

Quoted 34 lines
Quoted 20 lines
drivers/hwmon/starfive-fan-tach.c new file +20 -0
+static int starfive_fan_tach_create_fan(struct device *dev, struct device_node *child,
+                    struct starfive_fan_tach_data *priv)
+{
+    u8 tach_ch[STARFIVE_FAN_TACH_CH];
+    int ret, count;
+    u32 ppr, index;
+
+    count = of_property_count_u8_elems(child, "tach-ch");
+    if (count < 1 || count > STARFIVE_FAN_TACH_CH)
+        return -EINVAL;
+
+    ret = of_property_read_u8_array(child, "tach-ch", tach_ch, count);
+    if (ret)
+        return ret;
+
+    /* Parse pulses-per-revolution, default to 2 if not specified */
+    ppr = STARFIVE_FAN_DEFAULT_PULSE_PR;
+    of_property_read_u32(child, "pulses-per-revolution", &ppr);
+
+    if (!ppr || ppr > 4) {

drivers/hwmon/starfive-fan-tach.c line 405

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+        dev_err(dev, "Invalid pulses-per-revolution %u, must be 1-4\n", ppr);
+        return -EINVAL;
+    }
+
+    for (index = 0; index < count; index++) {
+        u8 ch = tach_ch[index];
+
+        if (ch >= STARFIVE_FAN_TACH_CH) {
+            dev_warn(priv->dev, "Invalid tach-ch %d, skipping\n", ch);
+            continue;

This should also return an error. Also, this is the only use of priv->dev, and priv->dev == dev. priv->dev is therefore unnecessary.

Quoted 6 lines
Quoted 3 lines

+        }
+
+        priv->pulses_per_rev[ch] = ppr;

What is the point of storing the same ppr value separately for each channel ?

Sorry, not reading the code correctly. Ignore the noise.

Guenter

#7 lore raw

Re: [PATCH v2 2/2] hwmon: (starfive-fan-tach) Add fan tach driver for StarFive JHB100

In reply to Guenter Roeck

Hi, Guenter

Thanks for the review.

Quoted 219 lines · Changhuang Liang
Quoted 9 lines

Add fan tach driver for StarFive JHB100 SoC.

The controller supports up to 16 independent fan tachometer 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() than reset_control_deassert()
remove: clk_disable_unprepare() than reset_control_assert()

s/than/then/

Quoted 11 lines
---
  Documentation/hwmon/index.rst             |   1 +
  Documentation/hwmon/starfive-fan-tach.rst |  34 ++
  MAINTAINERS                               |   8 +
  drivers/hwmon/Kconfig                     |  14 +
  drivers/hwmon/Makefile                    |   1 +
  drivers/hwmon/starfive-fan-tach.c         | 561

++++++++++++++++++++++

Quoted 42 lines
  6 files changed, 619 insertions(+)

create mode 100644 Documentation/hwmon/starfive-fan-tach.rst create mode 100644 drivers/hwmon/starfive-fan-tach.c

(quoted diff) +0 -0

b/Documentation/hwmon/index.rst index 29130df44d12..0d64e4cf1051 100644

Documentation/hwmon/index.rst +1 -0
@@ -253,6 +253,7 @@ Hardware Monitoring Kernel Drivers
     smsc47m1
     sparx5-temp
     spd5118
+   starfive-fan-tach
     stpddc60
     surface_fan
     sy7636a-hwmon
Documentation/hwmon/starfive-fan-tach.rst new file +18 -0
new file mode 100644
index 000000000000..5ca03b52ad58
@@ -0,0 +1,34 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+Kernel driver starfive-fan-tach
+===============================
+
+Supported chips:
+
+  * StarFive JHB100
+
+    Prefix: 'starfive_fan_tach'
+
+    Addresses scanned: -
+
+Authors:
+      - Changhuang Liang <[email protected]>
+
+Description
+-----------

Documentation/hwmon/starfive-fan-tach.rst line 28

Quoted 10 lines
Documentation/hwmon/starfive-fan-tach.rst new file +10 -0
+
+This driver implements support for the fan tachometer controller
+found on the StarFive JHB100 SoC. The controller supports up to 16
+independent fan tachometer inputs. Each tachometer channel measures
+the number of pulses within a fixed 100 ms window.
+
+Sysfs entries
+-------------
+
+====================

=======================================================

Documentation/hwmon/starfive-fan-tach.rst

Quoted 3 lines
Documentation/hwmon/starfive-fan-tach.rst new file +3 -0
+fan[1-16]_input      Fan speed in RPM (read-only)
+fan[1-16]_min        Lower fan speed limit in RPM (read/write)
+fan[1-16]_enable     Enable/disable the tachometer channel

(read/write)

MAINTAINERS S: Supported · line 25897

Quoted 10 lines
Documentation/hwmon/starfive-fan-tach.rst new file +4 -0
+fan[1-16]_fault      Fan stall indication (read-only)
+fan[1-16]_min_alarm  Fan speed below fan[1-16]_min (read-only)
+====================
+=======================================================
MAINTAINERS index renamed from MAINTAINERS +0 -0

c545aa121d51..83318d2e3e2e 100644

MAINTAINERS +0 -0
@@ -25897,6 +25897,14 @@ S:	Supported
  F:

Documentation/devicetree/bindings/interrupt-controller/starfive,jhb100
-intc.yaml

MAINTAINERS S: Supported

Quoted 7 lines
MAINTAINERS +5 -0
  F:	drivers/irqchip/irq-starfive-jhb100-intc.c
 
+STARFIVE JHB100 FAN-TACH DRIVER
+M:	Changhuang Liang <[email protected]>
+L:	[email protected]
+S:	Supported
+F:

Documentation/devicetree/bindings/hwmon/starfive,jhb100-fan-tach.ya
ml

drivers/hwmon/Makefile obj-$(CONFIG_SENSORS_SMSC47M1) +=

Quoted 36 lines
MAINTAINERS +3 -0
+F:	Documentation/hwmon/starfive-fan-tach.rst
+F:	drivers/hwmon/starfive-fan-tach.c
+
  STATIC BRANCH/CALL
  M:	Peter Zijlstra <[email protected]>
  M:	Josh Poimboeuf <[email protected]>
drivers/hwmon/Kconfig index renamed from drivers/hwmon/Kconfig +0 -0

5c2d3ff5fce8..2c0000f8e777 100644

drivers/hwmon/Kconfig +14 -0
@@ -2383,6 +2383,20 @@ config SENSORS_SPD5118_DETECT
 
  	  If unsure, say Y.
 
+config SENSORS_STARFIVE_FAN_TACH
+	tristate "StarFive Fan-Tach driver"
+	depends on ARCH_STARFIVE || COMPILE_TEST
+	depends on HAS_IOMEM
+	help
+	  This driver provides support for the fan tachometer controller
+	  found on StarFive SoCs such as the JHB100. The controller supports
+	  up to 16 independent fan tachometer inputs and reports 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-fan-tach.
+
  config SENSORS_TC74
  	tristate "Microchip TC74"
  	depends on I2C
drivers/hwmon/Makefile index renamed from drivers/hwmon/Makefile +0 -0

63effc0ab8d1..80b2704b94e5 100644

drivers/hwmon/Makefile +0 -0
@@ -223,6 +223,7 @@ obj-$(CONFIG_SENSORS_SMSC47M1)	+=

smsc47m1.o

Quoted 40 lines
drivers/hwmon/Makefile +1 -0
  obj-$(CONFIG_SENSORS_SMSC47M192)+= smsc47m192.o
  obj-$(CONFIG_SENSORS_SPARX5)	+= sparx5-temp.o
  obj-$(CONFIG_SENSORS_SPD5118)	+= spd5118.o
+obj-$(CONFIG_SENSORS_STARFIVE_FAN_TACH)	+= starfive-fan-tach.o
  obj-$(CONFIG_SENSORS_STTS751)	+= stts751.o
  obj-$(CONFIG_SENSORS_SURFACE_FAN)+= surface_fan.o
  obj-$(CONFIG_SENSORS_SURFACE_TEMP)+= surface_temp.o diff --git

a/drivers/hwmon/starfive-fan-tach.c
b/drivers/hwmon/starfive-fan-tach.c
new file mode 100644
index 000000000000..1e6399db424d

drivers/hwmon/starfive-fan-tach.c new file +26 -0
@@ -0,0 +1,561 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * FAN-TACH controller driver for StarFive JHB100
+ *
+ * Copyright (C) 2018-2026 StarFive Technology Co., Ltd.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/completion.h>
+#include <linux/delay.h>
+#include <linux/errno.h>
+#include <linux/hwmon.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>
+#include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/math64.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/reset.h>
+#include <linux/spinlock.h>
+

drivers/hwmon/starfive-fan-tach.c line 36

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+#define STARFIVE_FAN_TACH_CH			16
+
+/* Fan-tach register offest */
+#define STARFIVE_FAN_TACH_STATUS		0x0c
+
+#define STARFIVE_FAN_TACH_SPEED(ch)		(((ch) * 0x04) + 0x10)
+#define STARFIVE_FAN_TACH_SPEED_VALID		BIT(31)
+#define STARFIVE_FAN_TACH_VALUE_MASK		GENMASK(30, 0)
+
+#define STARFIVE_FAN_TACH_THRESHOLD(ch)		(((ch) * 0x04) +

0x50)

Quoted 5 lines
drivers/hwmon/starfive-fan-tach.c new file +5 -0
+
+#define STARFIVE_FAN_TACH_INT_EN		0x90
+#define STARFIVE_FAN_TACH_STALL_INT_MASK	GENMASK(15, 0)
+#define STARFIVE_FAN_TACH_SLOW_INT_MASK		GENMASK(31, 16)
+#define STARFIVE_FAN_TACH_STALL_INT(ch)		BIT(ch)

drivers/hwmon/starfive-fan-tach.c

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+#define STARFIVE_FAN_TACH_SLOW_INT(ch)		(BIT(ch) << 16)
+
+#define STARFIVE_FAN_TACH_MEASURE_TIME		0x94
+
+#define STARFIVE_FAN_TACH_CH_EN			0x98
+#define STARFIVE_FAN_TACH_EN(ch)		BIT(ch)
+
+#define STARFIVE_FAN_DEFAULT_PULSE_PR		2
+#define STARFIVE_FAN_DEFAULT_MEASURE_RATIO	10
+#define STARFIVE_FAN_DEFAULT_RPM_PAUSE_TIME	(60 *

STARFIVE_FAN_DEFAULT_MEASURE_RATIO)

Quoted 12 lines
drivers/hwmon/starfive-fan-tach.c new file +12 -0
+
+#define STARFIVE_FAN_TACH_TIMEOUT \
+	(USEC_PER_SEC / STARFIVE_FAN_DEFAULT_MEASURE_RATIO)
+
+#define STARFIVE_FAN_TACH_TIMEOUT_JIFFIES \
+	(msecs_to_jiffies(1000) / STARFIVE_FAN_DEFAULT_MEASURE_RATIO)
+
+#define FAN_ATTRIBUTE_SET \
+	(HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_ENABLE | \
+	 HWMON_F_FAULT | HWMON_F_MIN_ALARM)
+
+struct starfive_fan_tach_data {
Quoted 8 lines

drivers/hwmon/starfive-fan-tach.c line 69

Quoted 6 lines
drivers/hwmon/starfive-fan-tach.c new file +6 -0
+	struct device *dev;
+	/*
+	 * Serialises the read-modify-write sequences on CH_EN and INT_EN,
+	 * and the arm/disarm window of the fault/min_alarm read paths.
+	 */
+	struct mutex lock;

I don't see why this would be needed on top of the hwmon subsystem lock.

Will drop it.

Quoted 189 lines
Quoted 8 lines
drivers/hwmon/starfive-fan-tach.c new file +7 -0
+	void __iomem *regs;
+	struct reset_control *rst;
+	struct clk *clk;
+	bool tach_present[STARFIVE_FAN_TACH_CH];
+	unsigned long clk_rate; /* APB clock frequency */
+	u8 pulses_per_rev[STARFIVE_FAN_TACH_CH];
+	struct completion comp_stall[STARFIVE_FAN_TACH_CH];

drivers/hwmon/starfive-fan-tach.c

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+	struct completion comp_slow[STARFIVE_FAN_TACH_CH];
+	u8 fan_stall[STARFIVE_FAN_TACH_CH];
+	u8 fan_slow[STARFIVE_FAN_TACH_CH];
+	u32 armed_stall;
+	u32 armed_slow;
+	/* protects fan_stall[]/fan_slow[]/armed_* and the STATUS register */
+	spinlock_t irq_lock;
+};
+
+static void starfive_fan_tach_ch_enable(struct starfive_fan_tach_data

*priv, u8 tach_ch,

Quoted 4 lines
drivers/hwmon/starfive-fan-tach.c new file +4 -0
+					bool enable)
+{
+	if (enable) {
+		writel(readl(priv->regs + STARFIVE_FAN_TACH_CH_EN) |

drivers/hwmon/starfive-fan-tach.c

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+		       STARFIVE_FAN_TACH_EN(tach_ch),
+		       priv->regs + STARFIVE_FAN_TACH_CH_EN);
+	} else {
+		writel(readl(priv->regs + STARFIVE_FAN_TACH_CH_EN) &
+		       ~(STARFIVE_FAN_TACH_EN(tach_ch)),
+		       priv->regs + STARFIVE_FAN_TACH_CH_EN);
+	}
+}
+
+static void starfive_fan_tach_ch_stall_unmask(struct

starfive_fan_tach_data *priv, u8 tach_ch,

Quoted 4 lines
drivers/hwmon/starfive-fan-tach.c new file +4 -0
+					      bool unmask)
+{
+	if (unmask) {
+		writel(readl(priv->regs + STARFIVE_FAN_TACH_INT_EN) |

drivers/hwmon/starfive-fan-tach.c

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+		       STARFIVE_FAN_TACH_STALL_INT(tach_ch),
+		       priv->regs + STARFIVE_FAN_TACH_INT_EN);
+	} else {
+		writel(readl(priv->regs + STARFIVE_FAN_TACH_INT_EN) &
+		       ~STARFIVE_FAN_TACH_STALL_INT(tach_ch),
+		       priv->regs + STARFIVE_FAN_TACH_INT_EN);
+	}
+}
+
+static void starfive_fan_tach_ch_slow_unmask(struct

starfive_fan_tach_data *priv, u8 tach_ch,

Quoted 12 lines
drivers/hwmon/starfive-fan-tach.c new file +12 -0
+					     bool unmask)
+{
+	if (unmask) {
+		writel(readl(priv->regs + STARFIVE_FAN_TACH_INT_EN) |
+		       STARFIVE_FAN_TACH_SLOW_INT(tach_ch),
+		       priv->regs + STARFIVE_FAN_TACH_INT_EN);
+	} else {
+		writel(readl(priv->regs + STARFIVE_FAN_TACH_INT_EN) &
+		       ~STARFIVE_FAN_TACH_SLOW_INT(tach_ch),
+		       priv->regs + STARFIVE_FAN_TACH_INT_EN);
+	}
+}

drivers/hwmon/starfive-fan-tach.c

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+
+static u32 starfive_fan_tach_rpm_to_val(struct starfive_fan_tach_data
+*priv, u8 tach_ch, u32 rpm) {
+	u64 tach_val;
+	u8 ppr;
+
+	ppr = priv->pulses_per_rev[tach_ch];
+
+	tach_val = (u64)rpm * ppr;
+	tach_val = div_u64(tach_val,

STARFIVE_FAN_DEFAULT_RPM_PAUSE_TIME);

drivers/hwmon/starfive-fan-tach.c line 140

Quoted 4 lines
drivers/hwmon/starfive-fan-tach.c new file +4 -0
+
+	return min_t(u64, tach_val, STARFIVE_FAN_TACH_VALUE_MASK); }
+
+static long starfive_fan_tach_val_to_rpm(struct starfive_fan_tach_data

*priv, u8 tach_ch,

Quoted 9 lines
drivers/hwmon/starfive-fan-tach.c new file +9 -0
+					 u32 tach_val)
+{
+	u64 rpm;
+	u8 ppr;
+
+	ppr = priv->pulses_per_rev[tach_ch];
+
+	rpm = (u64)tach_val * STARFIVE_FAN_DEFAULT_RPM_PAUSE_TIME;
+

drivers/hwmon/starfive-fan-tach.c

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+	return div_u64(rpm, ppr);
+}
+
+static int starfive_fan_tach_get_rpm(struct starfive_fan_tach_data *priv,
+				     u8 tach_ch, long *rpm)
+{
+	u32 val;
+	int ret;
+
+	ret = readl_poll_timeout(priv->regs +

STARFIVE_FAN_TACH_SPEED(tach_ch),

drivers/hwmon/starfive-fan-tach.c

Quoted 8 lines
drivers/hwmon/starfive-fan-tach.c new file +8 -0
+				 val, val & STARFIVE_FAN_TACH_SPEED_VALID,
+				 100, STARFIVE_FAN_TACH_TIMEOUT);
+	if (ret)
+		return -ENODATA;
+
+	val = FIELD_GET(STARFIVE_FAN_TACH_VALUE_MASK, val);
+	if (!val) {
+		/* Sampling = 0 may mean no valid capture; wait 1.5 more cycles for

valid read. */

drivers/hwmon/starfive-fan-tach.c line 168

Quoted 1 line
drivers/hwmon/starfive-fan-tach.c new file +1 -0
+		fsleep(STARFIVE_FAN_TACH_TIMEOUT +

STARFIVE_FAN_TACH_TIMEOUT / 2);

drivers/hwmon/starfive-fan-tach.c line 169

Quoted 1 line
drivers/hwmon/starfive-fan-tach.c new file +1 -0
+		ret = readl_poll_timeout(priv->regs +

STARFIVE_FAN_TACH_SPEED(tach_ch),

Quoted 4 lines
drivers/hwmon/starfive-fan-tach.c new file +4 -0
+					 val, val & STARFIVE_FAN_TACH_SPEED_VALID,
+					 100, STARFIVE_FAN_TACH_TIMEOUT);
+		if (ret)
+			return -ENODATA;

drivers/hwmon/starfive-fan-tach.c

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+
+		val = FIELD_GET(STARFIVE_FAN_TACH_VALUE_MASK, val);
+	}
+
+	*rpm = starfive_fan_tach_val_to_rpm(priv, tach_ch, val);
+
+	return 0;
+}
+
+static long starfive_fan_tach_get_rpm_threshold(struct

starfive_fan_tach_data *priv,

drivers/hwmon/starfive-fan-tach.c

Quoted 11 lines
drivers/hwmon/starfive-fan-tach.c new file +11 -0
+						u8 tach_ch)
+{
+	u32 val;
+
+	val = readl(priv->regs + STARFIVE_FAN_TACH_THRESHOLD(tach_ch));
+
+	val = FIELD_GET(STARFIVE_FAN_TACH_VALUE_MASK, val);
+
+	return starfive_fan_tach_val_to_rpm(priv, tach_ch, val); }
+
+static void starfive_set_tach_rpm_threshold(struct starfive_fan_tach_data

*priv,

drivers/hwmon/starfive-fan-tach.c

Quoted 11 lines
drivers/hwmon/starfive-fan-tach.c new file +11 -0
+					    u8 tach_ch, u32 val)
+{
+	u32 reg_val;
+
+	reg_val = starfive_fan_tach_rpm_to_val(priv, tach_ch, val);
+	reg_val = clamp_val(reg_val, 1, STARFIVE_FAN_TACH_VALUE_MASK);
+
+	writel(reg_val, priv->regs + STARFIVE_FAN_TACH_THRESHOLD(tach_ch));
+}
+
+static int starfive_fan_tach_get_ch_en(struct starfive_fan_tach_data

*priv,

drivers/hwmon/starfive-fan-tach.c line 206

Quoted 1 line
drivers/hwmon/starfive-fan-tach.c new file +1 -0
+				       u8 tach_ch)

Why not bool ?

drivers/hwmon/starfive-fan-tach.c line 214

Quoted 6 lines
drivers/hwmon/starfive-fan-tach.c new file +6 -0
+{
+	u32 enable;
+
+	enable = readl(priv->regs + STARFIVE_FAN_TACH_CH_EN);
+
+	return (enable & (1 << tach_ch)) ? 1 : 0;
	return !!(enable & STARFIVE_FAN_TACH_EN(tach_ch));
Quoted 16 lines
drivers/hwmon/starfive-fan-tach.c new file +16 -0
+}
+
+static int starfive_fan_tach_hwmon_read(struct device *dev,
+					enum hwmon_sensor_types type, u32 attr,
+					int channel, long *val)
+{
+	struct starfive_fan_tach_data *priv = dev_get_drvdata(dev);
+	int ret = 0;
+
+	switch (attr) {
+	case hwmon_fan_fault:
+		scoped_guard(mutex, &priv->lock) {
+			scoped_guard(spinlock_irqsave, &priv->irq_lock) {
+				writel(STARFIVE_FAN_TACH_STALL_INT(channel),
+				       priv->regs + STARFIVE_FAN_TACH_STATUS);
+				/* clear fan_stall first */

drivers/hwmon/starfive-fan-tach.c

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+				priv->fan_stall[channel] = 0;
+				reinit_completion(&priv->comp_stall[channel]);
+				priv->armed_stall |= BIT(channel);
+			}
+
+			starfive_fan_tach_ch_stall_unmask(priv, channel, true);
+
+			/* Waiting for hardware to measure */
+			wait_for_completion_timeout(&priv->comp_stall[channel],
+						    2 *

STARFIVE_FAN_TACH_TIMEOUT_JIFFIES);

Quoted 16 lines

drivers/hwmon/starfive-fan-tach.c line 249

Quoted 9 lines
drivers/hwmon/starfive-fan-tach.c new file +9 -0
+
+			starfive_fan_tach_ch_stall_unmask(priv, channel, false);
+
+			scoped_guard(spinlock_irqsave, &priv->irq_lock) {
+				priv->armed_stall &= ~BIT(channel);
+				*val = priv->fan_stall[channel];
+			}
+		}
+

I'd really be interested to see how long it takes to read the alarm and fault status of all fans. And, yes, Sashiko has a point: the attributes are supported to report a sticky status, not something that is calculated on the fly. Is this based on some heuristics ? I don't claim to understand how fault and min_alarm are calculated. A comment in the code describing how this works would be helpful.

There is no real calculation for fault and min_alarm, the hardware simply counts the corresponding pulses within a specified time period and reports the counts via interrupt when the counting period expires. I will try to modify it so that the previous statistical result is recorded, and when reading, the previous result is returned, thus eliminating the need to wait for the measurement.

Quoted 155 lines

drivers/hwmon/starfive-fan-tach.c line 254

Quoted 6 lines
drivers/hwmon/starfive-fan-tach.c new file +5 -0
+		break;
+	case hwmon_fan_input:
+		ret = starfive_fan_tach_get_ch_en(priv, channel);
+		if (!ret)
+			return -ENODATA;

I don't see why the assignment to ret would add value here.

		if (!starfive_fan_tach_get_ch_en(priv, channel))
			return -ENODATA;
Quoted 16 lines
drivers/hwmon/starfive-fan-tach.c new file +16 -0
+
+		ret = starfive_fan_tach_get_rpm(priv, channel, val);
+		if (ret < 0)
+			return ret;
+
+		break;
+	case hwmon_fan_min:
+		*val = starfive_fan_tach_get_rpm_threshold(priv, channel);
+
+		break;
+	case hwmon_fan_min_alarm:
+		scoped_guard(mutex, &priv->lock) {
+			scoped_guard(spinlock_irqsave, &priv->irq_lock) {
+				writel(STARFIVE_FAN_TACH_SLOW_INT(channel),
+				       priv->regs + STARFIVE_FAN_TACH_STATUS);
+				/* clear fan_slow first */

drivers/hwmon/starfive-fan-tach.c

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+				priv->fan_slow[channel] = 0;
+				reinit_completion(&priv->comp_slow[channel]);
+				priv->armed_slow |= BIT(channel);
+			}
+
+			starfive_fan_tach_ch_slow_unmask(priv, channel, true);
+
+			/* Waiting for hardware to measure */
+			wait_for_completion_timeout(&priv->comp_slow[channel],
+						    2 *

STARFIVE_FAN_TACH_TIMEOUT_JIFFIES);

Quoted 85 lines
drivers/hwmon/starfive-fan-tach.c new file +85 -0
+
+			starfive_fan_tach_ch_slow_unmask(priv, channel, false);
+
+			scoped_guard(spinlock_irqsave, &priv->irq_lock) {
+				priv->armed_slow &= ~BIT(channel);
+				*val = priv->fan_slow[channel];
+			}
+		}
+
+		break;
+	case hwmon_fan_enable:
+		*val = starfive_fan_tach_get_ch_en(priv, channel);
+
+		break;
+	default:
+		return -EOPNOTSUPP;
+	}
+
+	return 0;
+}
+
+static int starfive_fan_tach_hwmon_write(struct device *dev,
+					 enum hwmon_sensor_types type, u32 attr,
+					 int channel, long val)
+{
+	struct starfive_fan_tach_data *priv = dev_get_drvdata(dev);
+
+	guard(mutex)(&priv->lock);
+
+	switch (attr) {
+	case hwmon_fan_min:
+		if (val < 0 || val > U32_MAX)
+			return -EINVAL;
+
+		starfive_set_tach_rpm_threshold(priv, channel, val);
+		break;
+	case hwmon_fan_enable:
+		if (val != 0 && val != 1)
+			return -EINVAL;
+
+		starfive_fan_tach_ch_enable(priv, channel, val);
+		break;
+	default:
+		return -EOPNOTSUPP;
+	}
+
+	return 0;
+}
+
+static umode_t starfive_fan_tach_dev_is_visible(const void *drvdata,
+						enum hwmon_sensor_types type,
+						u32 attr, int channel)
+{
+	const struct starfive_fan_tach_data *priv = drvdata;
+
+	if (!priv->tach_present[channel])
+		return 0;
+
+	switch (attr) {
+	case hwmon_fan_input:
+	case hwmon_fan_fault:
+	case hwmon_fan_min_alarm:
+		return 0444;
+	case hwmon_fan_min:
+	case hwmon_fan_enable:
+		return 0644;
+	}
+
+	return 0;
+}
+
+static const struct hwmon_ops starfive_fan_tach_ops = {
+	.is_visible = starfive_fan_tach_dev_is_visible,
+	.read = starfive_fan_tach_hwmon_read,
+	.write = starfive_fan_tach_hwmon_write };
+
+static const struct hwmon_channel_info *starfive_fan_tach_info[] = {
+	HWMON_CHANNEL_INFO(fan,
+			   FAN_ATTRIBUTE_SET, FAN_ATTRIBUTE_SET,
+			   FAN_ATTRIBUTE_SET, FAN_ATTRIBUTE_SET,
+			   FAN_ATTRIBUTE_SET, FAN_ATTRIBUTE_SET,
+			   FAN_ATTRIBUTE_SET, FAN_ATTRIBUTE_SET,
+			   FAN_ATTRIBUTE_SET, FAN_ATTRIBUTE_SET,
+			   FAN_ATTRIBUTE_SET, FAN_ATTRIBUTE_SET,
+			   FAN_ATTRIBUTE_SET, FAN_ATTRIBUTE_SET,

drivers/hwmon/starfive-fan-tach.c

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+			   FAN_ATTRIBUTE_SET, FAN_ATTRIBUTE_SET),
+	NULL
+};
+
+static const struct hwmon_chip_info starfive_fan_tach_chip_info = {
+	.ops = &starfive_fan_tach_ops,
+	.info = starfive_fan_tach_info,
+};
+
+static int starfive_fan_tach_create_fan(struct device *dev, struct

device_node *child,

Quoted 9 lines
drivers/hwmon/starfive-fan-tach.c new file +9 -0
+					struct starfive_fan_tach_data *priv) {
+	u8 tach_ch[STARFIVE_FAN_TACH_CH];
+	int ret, count;
+	u32 ppr, index;
+
+	count = of_property_count_u8_elems(child, "tach-ch");
+	if (count < 1 || count > STARFIVE_FAN_TACH_CH)
+		return -EINVAL;
+

drivers/hwmon/starfive-fan-tach.c

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+	ret = of_property_read_u8_array(child, "tach-ch", tach_ch, count);
+	if (ret)
+		return ret;
+
+	/* Parse pulses-per-revolution, default to 2 if not specified */
+	ppr = STARFIVE_FAN_DEFAULT_PULSE_PR;
+	of_property_read_u32(child, "pulses-per-revolution", &ppr);
+
+	if (!ppr || ppr > 4) {
+		dev_err(dev, "Invalid pulses-per-revolution %u, must be 1-4\n",

ppr);

Quoted 13 lines

drivers/hwmon/starfive-fan-tach.c line 405

Quoted 9 lines
drivers/hwmon/starfive-fan-tach.c new file +9 -0
+		return -EINVAL;
+	}
+
+	for (index = 0; index < count; index++) {
+		u8 ch = tach_ch[index];
+
+		if (ch >= STARFIVE_FAN_TACH_CH) {
+			dev_warn(priv->dev, "Invalid tach-ch %d, skipping\n", ch);
+			continue;

This should also return an error. Also, this is the only use of priv->dev, and priv->dev == dev. priv->dev is therefore unnecessary.

Thank you for pointing that out. I noticed that the struct device *dev passed in the function starfive_fan_tach_create_fan() is somewhat redundant, so I plan to remove it directly and keep priv->dev instead.

Quoted 153 lines

drivers/hwmon/starfive-fan-tach.c line 408

Quoted 3 lines
drivers/hwmon/starfive-fan-tach.c new file +3 -0
+		}
+
+		priv->pulses_per_rev[ch] = ppr;

What is the point of storing the same ppr value separately for each channel ?

Quoted 96 lines
drivers/hwmon/starfive-fan-tach.c new file +96 -0
+		priv->tach_present[ch] = true;
+		starfive_fan_tach_ch_enable(priv, ch, true);
+	}
+
+	return 0;
+}
+
+static irqreturn_t starfive_fan_tach_irq(int irq, void *dev_id) {
+	struct starfive_fan_tach_data *priv = dev_id;
+	u32 fan_status;
+	int i;
+
+	guard(spinlock)(&priv->irq_lock);
+
+	fan_status = readl(priv->regs + STARFIVE_FAN_TACH_STATUS);
+	if (!fan_status)
+		return IRQ_NONE;
+
+	writel(fan_status, priv->regs + STARFIVE_FAN_TACH_STATUS);
+
+	for (i = 0; i < STARFIVE_FAN_TACH_CH; i++) {
+		if (!priv->tach_present[i])
+			continue;
+
+		if ((fan_status & STARFIVE_FAN_TACH_STALL_INT(i)) &&
+		    (priv->armed_stall & BIT(i))) {
+			priv->fan_stall[i] = 1;
+			complete(&priv->comp_stall[i]);
+		}
+
+		if ((fan_status & STARFIVE_FAN_TACH_SLOW_INT(i)) &&
+		    (priv->armed_slow & BIT(i))) {
+			priv->fan_slow[i] = 1;
+			complete(&priv->comp_slow[i]);
+		}
+	}
+
+	return IRQ_HANDLED;
+}
+
+static void starfive_fan_tach_disable(void *data) {
+	struct starfive_fan_tach_data *priv = data;
+
+	clk_disable_unprepare(priv->clk);
+	reset_control_assert(priv->rst);
+}
+
+static int starfive_fan_tach_probe(struct platform_device *pdev) {
+	struct device *hwmon_dev, *dev = &pdev->dev;
+	struct starfive_fan_tach_data *priv;
+	int irq;
+	int ret;
+	u32 i;
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	priv->dev = dev;
+	spin_lock_init(&priv->irq_lock);
+
+	for (i = 0; i < STARFIVE_FAN_TACH_CH; i++) {
+		init_completion(&priv->comp_stall[i]);
+		init_completion(&priv->comp_slow[i]);
+	}
+
+	priv->regs = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(priv->regs))
+		return dev_err_probe(dev, PTR_ERR(priv->regs),
+				     "Unable to map IO resources\n");
+
+	priv->clk = devm_clk_get(dev, NULL);
+	if (IS_ERR(priv->clk))
+		return dev_err_probe(dev, PTR_ERR(priv->clk),
+				     "Unable to get fan tach's clock\n");
+
+	priv->rst = devm_reset_control_get_exclusive(dev, NULL);
+	if (IS_ERR(priv->rst))
+		return dev_err_probe(dev, PTR_ERR(priv->rst),
+				     "Unable to get fan tach's reset\n");
+
+	ret = clk_prepare_enable(priv->clk);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to enable clock\n");
+
+	priv->clk_rate = clk_get_rate(priv->clk);
+	if (!priv->clk_rate) {
+		clk_disable_unprepare(priv->clk);
+		return dev_err_probe(dev, -EINVAL, "Unable to get clock's rate\n");
+	}
+
+	ret = reset_control_deassert(priv->rst);
+	if (ret) {
+		clk_disable_unprepare(priv->clk);
+		return dev_err_probe(dev, ret, "Failed to deassert reset\n");
+	}

drivers/hwmon/starfive-fan-tach.c

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+
+	ret = devm_add_action_or_reset(dev, starfive_fan_tach_disable, priv);
+	if (ret)
+		return ret;
+
+	ret = devm_mutex_init(dev, &priv->lock);
+	if (ret)
+		return ret;
+
+	writel(STARFIVE_FAN_TACH_STALL_INT_MASK |

STARFIVE_FAN_TACH_SLOW_INT_MASK,

drivers/hwmon/starfive-fan-tach.c

Quoted 8 lines
drivers/hwmon/starfive-fan-tach.c new file +8 -0
+	       priv->regs + STARFIVE_FAN_TACH_STATUS);
+	writel(priv->clk_rate / STARFIVE_FAN_DEFAULT_MEASURE_RATIO,
+	       priv->regs + STARFIVE_FAN_TACH_MEASURE_TIME);
+
+	for_each_child_of_node_scoped(dev->of_node, child) {
+		ret = starfive_fan_tach_create_fan(dev, child, priv);
+		if (ret)
+			return dev_err_probe(dev, ret, "Failed to create fan %pOF\n",

child);

drivers/hwmon/starfive-fan-tach.c

Quoted 7 lines
drivers/hwmon/starfive-fan-tach.c new file +7 -0
+	}
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0)
+		return dev_err_probe(dev, irq, "Failed to get IRQ\n");
+
+	ret = devm_request_irq(dev, irq, starfive_fan_tach_irq, 0, pdev->name,

priv);

drivers/hwmon/starfive-fan-tach.c

Quoted 4 lines
drivers/hwmon/starfive-fan-tach.c new file +4 -0
+	if (ret)
+		return ret;
+
+	hwmon_dev = devm_hwmon_device_register_with_info(dev,

"starfive_fan_tach",

Quoted 18 lines
drivers/hwmon/starfive-fan-tach.c new file +18 -0
+							 priv, &starfive_fan_tach_chip_info,
+							 NULL);
+	return PTR_ERR_OR_ZERO(hwmon_dev);
+}
+
+static const struct of_device_id starfive_fan_tach_of_match[] = {
+	{ .compatible = "starfive,jhb100-fan-tach", },
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, starfive_fan_tach_of_match);
+
+static struct platform_driver starfive_fan_tach_driver = {
+	.probe = starfive_fan_tach_probe,
+	.driver	= {
+		.name = "starfive-fan-tach",
+		.of_match_table = starfive_fan_tach_of_match,
+	},
+};
Quoted 8 lines

drivers/hwmon/starfive-fan-tach.c line 561

Quoted 8 lines
drivers/hwmon/starfive-fan-tach.c new file +8 -0
+
+module_platform_driver(starfive_fan_tach_driver);
+
+MODULE_AUTHOR("William Qiu <[email protected]>");
+MODULE_AUTHOR("Changhuang Liang
+<[email protected]>");
+MODULE_DESCRIPTION("StarFive JHB100 Fan Tach device driver");
+MODULE_LICENSE("GPL");

Best Regards,
Changhuang

maintainer
#8 lore raw

Re: [PATCH v2 2/2] hwmon: (starfive-fan-tach) Add fan tach driver for StarFive JHB100

Quoted 22 lines · Changhuang Liang

Hi, Guenter

Thanks for the review.

Quoted 18 lines · Changhuang Liang
Quoted 9 lines

Add fan tach driver for StarFive JHB100 SoC.

The controller supports up to 16 independent fan tachometer 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() than reset_control_deassert()
remove: clk_disable_unprepare() than reset_control_assert()

s/than/then/

Quoted 5 lines

---

...

Quoted 43 lines
Quoted 26 lines
Quoted 15 lines
drivers/hwmon/starfive-fan-tach.c new file +15 -0
+
+static int starfive_fan_tach_hwmon_read(struct device *dev,
+					enum hwmon_sensor_types type, u32 attr,
+					int channel, long *val)
+{
+	struct starfive_fan_tach_data *priv = dev_get_drvdata(dev);
+	int ret = 0;
+
+	switch (attr) {
+	case hwmon_fan_fault:
+		scoped_guard(mutex, &priv->lock) {
+			scoped_guard(spinlock_irqsave, &priv->irq_lock) {
+				writel(STARFIVE_FAN_TACH_STALL_INT(channel),
+				       priv->regs + STARFIVE_FAN_TACH_STATUS);
+				/* clear fan_stall first */

drivers/hwmon/starfive-fan-tach.c

Quoted 10 lines
drivers/hwmon/starfive-fan-tach.c new file +10 -0
+				priv->fan_stall[channel] = 0;
+				reinit_completion(&priv->comp_stall[channel]);
+				priv->armed_stall |= BIT(channel);
+			}
+
+			starfive_fan_tach_ch_stall_unmask(priv, channel, true);
+
+			/* Waiting for hardware to measure */
+			wait_for_completion_timeout(&priv->comp_stall[channel],
+						    2 *

STARFIVE_FAN_TACH_TIMEOUT_JIFFIES);

Quoted 16 lines

drivers/hwmon/starfive-fan-tach.c line 249

Quoted 9 lines
drivers/hwmon/starfive-fan-tach.c new file +9 -0
+
+			starfive_fan_tach_ch_stall_unmask(priv, channel, false);
+
+			scoped_guard(spinlock_irqsave, &priv->irq_lock) {
+				priv->armed_stall &= ~BIT(channel);
+				*val = priv->fan_stall[channel];
+			}
+		}
+

I'd really be interested to see how long it takes to read the alarm and fault status of all fans. And, yes, Sashiko has a point: the attributes are supported to report a sticky status, not something that is calculated on the fly. Is this based on some heuristics ? I don't claim to understand how fault and min_alarm are calculated. A comment in the code describing how this works would be helpful.

Quoted 6 lines

There is no real calculation for fault and min_alarm, the hardware simply counts the corresponding pulses within a specified time period and reports the counts via interrupt when the counting period expires. I will try to modify it so that the previous statistical result is recorded, and when reading, the previous result is returned, thus eliminating the need to wait for the measurement.

So how long does it take to execute the "sensors" command with 16 active fans ?

...

Quoted 12 lines
Quoted 11 lines

drivers/hwmon/starfive-fan-tach.c line 405

Quoted 7 lines
drivers/hwmon/starfive-fan-tach.c new file +7 -0
+
+	for (index = 0; index < count; index++) {
+		u8 ch = tach_ch[index];
+
+		if (ch >= STARFIVE_FAN_TACH_CH) {
+			dev_warn(priv->dev, "Invalid tach-ch %d, skipping\n", ch);
+			continue;

This should also return an error. Also, this is the only use of priv->dev, and priv->dev == dev. priv->dev is therefore unnecessary.

Quoted 3 lines

Thank you for pointing that out. I noticed that the struct device *dev passed in the function starfive_fan_tach_create_fan() is somewhat redundant, so I plan to remove it directly and keep priv->dev instead.

Just for use in this function ? Why ? It is not used after probe.

Guenter

#9 lore raw

Re: [PATCH v2 2/2] hwmon: (starfive-fan-tach) Add fan tach driver for StarFive JHB100

Hi, Guenter

Thanks for the review.

Quoted 69 lines
Quoted 19 lines
Quoted 19 lines · Changhuang Liang
Quoted 9 lines

Add fan tach driver for StarFive JHB100 SoC.

The controller supports up to 16 independent fan tachometer 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() than reset_control_deassert()
remove: clk_disable_unprepare() than reset_control_assert()

s/than/then/

Quoted 6 lines
Signed-off-by Changhuang Liang

<[email protected]>
---

...

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
drivers/hwmon/starfive-fan-tach.c new file +16 -0
+
+static int starfive_fan_tach_hwmon_read(struct device *dev,
+					enum hwmon_sensor_types type, u32 attr,
+					int channel, long *val)
+{
+	struct starfive_fan_tach_data *priv = dev_get_drvdata(dev);
+	int ret = 0;
+
+	switch (attr) {
+	case hwmon_fan_fault:
+		scoped_guard(mutex, &priv->lock) {
+			scoped_guard(spinlock_irqsave, &priv->irq_lock) {
+				writel(STARFIVE_FAN_TACH_STALL_INT(channel),
+				       priv->regs + STARFIVE_FAN_TACH_STATUS);
+				/* clear fan_stall first */
+				priv->fan_stall[channel] = 0;
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/starfive-fan-tach.c line 241

Quoted 8 lines
drivers/hwmon/starfive-fan-tach.c new file +8 -0
+				reinit_completion(&priv->comp_stall[channel]);
+				priv->armed_stall |= BIT(channel);
+			}
+
+			starfive_fan_tach_ch_stall_unmask(priv, channel, true);
+
+			/* Waiting for hardware to measure */
+
	wait_for_completion_timeout(&priv->comp_stall[channel],
Quoted 19 lines
Quoted 18 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+						    2 *

STARFIVE_FAN_TACH_TIMEOUT_JIFFIES);

drivers/hwmon/starfive-fan-tach.c line 249

Quoted 9 lines
drivers/hwmon/starfive-fan-tach.c new file +9 -0
+
+			starfive_fan_tach_ch_stall_unmask(priv, channel, false);
+
+			scoped_guard(spinlock_irqsave, &priv->irq_lock) {
+				priv->armed_stall &= ~BIT(channel);
+				*val = priv->fan_stall[channel];
+			}
+		}
+

I'd really be interested to see how long it takes to read the alarm and fault status of all fans. And, yes, Sashiko has a point: the attributes are supported to report a sticky status, not something that is calculated on the fly. Is this based on some heuristics ? I don't claim to understand how fault and min_alarm are calculated. A comment in the code describing how this works would be helpful.

Quoted 5 lines

There is no real calculation for fault and min_alarm, the hardware simply counts the corresponding pulses within a specified time period and reports the counts via interrupt when the counting period expires. I will try to modify it so that the previous statistical result is recorded, and when reading, the previous result is returned, thus

Quoted 5 lines

eliminating the need to wait for the measurement.

Quoted 1 line

So how long does it take to execute the "sensors" command with 16 active fans ?

# time sensors
starfive_fan_tach-isa-0000
Adapter: ISA adapter
fan1:           FAULT  (min =  300 RPM)
fan2:           FAULT  (min =  300 RPM)
fan3:           FAULT  (min =  300 RPM)
fan4:           FAULT  (min =  300 RPM)
fan5:           FAULT  (min =  300 RPM)
fan6:           FAULT  (min =  300 RPM)
fan7:           FAULT  (min =  300 RPM)
fan8:           FAULT  (min =  300 RPM)
fan9:           FAULT  (min =  300 RPM)
fan10:          FAULT  (min =  300 RPM)
fan11:          FAULT  (min =  300 RPM)
fan12:          FAULT  (min =  300 RPM)
fan13:          FAULT  (min =  300 RPM)
fan14:          FAULT  (min =  300 RPM)
fan15:          FAULT  (min =  300 RPM)
fan16:       1800 RPM  (min =  300 RPM)
real    0m4.865s
user    0m0.023s
sys     0m0.001s
Quoted 14 lines

...

Quoted 13 lines
Quoted 12 lines

drivers/hwmon/starfive-fan-tach.c line 405

Quoted 7 lines
drivers/hwmon/starfive-fan-tach.c new file +7 -0
+
+	for (index = 0; index < count; index++) {
+		u8 ch = tach_ch[index];
+
+		if (ch >= STARFIVE_FAN_TACH_CH) {
+			dev_warn(priv->dev, "Invalid tach-ch %d, skipping\n", ch);
+			continue;

This should also return an error. Also, this is the only use of priv->dev, and
priv->dev == dev. priv->dev is therefore unnecessary.

Quoted 6 lines
Quoted 4 lines

Thank you for pointing that out. I noticed that the struct device *dev passed in the function starfive_fan_tach_create_fan() is somewhat redundant, so I plan to remove it directly and keep priv->dev instead.

Just for use in this function ? Why ? It is not used after probe.

After probe, priv->dev is also used. In the starfive_fan_tach_create_fan() function, priv->dev can be used directly as well, so I plan to simply remove struct device *dev.

static int starfive_fan_tach_create_fan(struct device *dev, struct device_node *child,
struct starfive_fan_tach_data *priv)

===》

static int starfive_fan_tach_create_fan(struct device_node *child,
struct starfive_fan_tach_data *priv)

Best Regards,
Changhuang

maintainer
#10 lore raw

Re: [PATCH v2 2/2] hwmon: (starfive-fan-tach) Add fan tach driver for StarFive JHB100

Quoted 124 lines · Changhuang Liang

Hi, Guenter

Thanks for the review.

Quoted 69 lines
Quoted 19 lines
Quoted 19 lines · Changhuang Liang
Quoted 9 lines

Add fan tach driver for StarFive JHB100 SoC.

The controller supports up to 16 independent fan tachometer 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() than reset_control_deassert()
remove: clk_disable_unprepare() than reset_control_assert()

s/than/then/

Quoted 6 lines
Signed-off-by Changhuang Liang

<[email protected]>
---

...

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
drivers/hwmon/starfive-fan-tach.c new file +16 -0
+
+static int starfive_fan_tach_hwmon_read(struct device *dev,
+					enum hwmon_sensor_types type, u32 attr,
+					int channel, long *val)
+{
+	struct starfive_fan_tach_data *priv = dev_get_drvdata(dev);
+	int ret = 0;
+
+	switch (attr) {
+	case hwmon_fan_fault:
+		scoped_guard(mutex, &priv->lock) {
+			scoped_guard(spinlock_irqsave, &priv->irq_lock) {
+				writel(STARFIVE_FAN_TACH_STALL_INT(channel),
+				       priv->regs + STARFIVE_FAN_TACH_STATUS);
+				/* clear fan_stall first */
+				priv->fan_stall[channel] = 0;
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/starfive-fan-tach.c line 241

Quoted 8 lines
drivers/hwmon/starfive-fan-tach.c new file +8 -0
+				reinit_completion(&priv->comp_stall[channel]);
+				priv->armed_stall |= BIT(channel);
+			}
+
+			starfive_fan_tach_ch_stall_unmask(priv, channel, true);
+
+			/* Waiting for hardware to measure */
+
	wait_for_completion_timeout(&priv->comp_stall[channel],
Quoted 19 lines
Quoted 18 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+						    2 *

STARFIVE_FAN_TACH_TIMEOUT_JIFFIES);

drivers/hwmon/starfive-fan-tach.c line 249

Quoted 9 lines
drivers/hwmon/starfive-fan-tach.c new file +9 -0
+
+			starfive_fan_tach_ch_stall_unmask(priv, channel, false);
+
+			scoped_guard(spinlock_irqsave, &priv->irq_lock) {
+				priv->armed_stall &= ~BIT(channel);
+				*val = priv->fan_stall[channel];
+			}
+		}
+

I'd really be interested to see how long it takes to read the alarm and fault status of all fans. And, yes, Sashiko has a point: the attributes are supported to report a sticky status, not something that is calculated on the fly. Is this based on some heuristics ? I don't claim to understand how fault and min_alarm are calculated. A comment in the code describing how this works would be helpful.

Quoted 5 lines

There is no real calculation for fault and min_alarm, the hardware simply counts the corresponding pulses within a specified time period and reports the counts via interrupt when the counting period expires. I will try to modify it so that the previous statistical result is recorded, and when reading, the previous result is returned, thus

Quoted 5 lines

eliminating the need to wait for the measurement.

Quoted 1 line

So how long does it take to execute the "sensors" command with 16 active fans ?

# time sensors
starfive_fan_tach-isa-0000
Adapter: ISA adapter
fan1:           FAULT  (min =  300 RPM)
fan2:           FAULT  (min =  300 RPM)
fan3:           FAULT  (min =  300 RPM)
fan4:           FAULT  (min =  300 RPM)
fan5:           FAULT  (min =  300 RPM)
fan6:           FAULT  (min =  300 RPM)
fan7:           FAULT  (min =  300 RPM)
fan8:           FAULT  (min =  300 RPM)
fan9:           FAULT  (min =  300 RPM)
fan10:          FAULT  (min =  300 RPM)
fan11:          FAULT  (min =  300 RPM)
fan12:          FAULT  (min =  300 RPM)
fan13:          FAULT  (min =  300 RPM)
fan14:          FAULT  (min =  300 RPM)
fan15:          FAULT  (min =  300 RPM)
fan16:       1800 RPM  (min =  300 RPM)
real    0m4.865s
user    0m0.023s
sys     0m0.001s
Quoted 19 lines

...

Quoted 17 lines
Quoted 12 lines

drivers/hwmon/starfive-fan-tach.c line 405

Quoted 7 lines
drivers/hwmon/starfive-fan-tach.c new file +7 -0
+
+	for (index = 0; index < count; index++) {
+		u8 ch = tach_ch[index];
+
+		if (ch >= STARFIVE_FAN_TACH_CH) {
+			dev_warn(priv->dev, "Invalid tach-ch %d, skipping\n", ch);
+			continue;

This should also return an error. Also, this is the only use of priv->dev, and
priv->dev == dev. priv->dev is therefore unnecessary.

Thank you for pointing that out. I noticed that the struct device *dev passed in the function starfive_fan_tach_create_fan() is somewhat redundant, so I plan to remove it directly and keep priv->dev instead.

Quoted 5 lines
Quoted 1 line

Just for use in this function ? Why ? It is not used after probe.

After probe, priv->dev is also used. In the starfive_fan_tach_create_fan() function, priv->dev can be used directly as well, so I plan to simply remove struct device *dev.

Are you checking if I pay attention ? starfive_fan_tach_create_fan() is called from the probe function, not "after" probe.

Guenter

#11 lore raw

Re: [PATCH v2 2/2] hwmon: (starfive-fan-tach) Add fan tach driver for StarFive JHB100

Hi, Guenter

Thanks for the review.

Quoted 128 lines · Changhuang Liang
Quoted 71 lines

Hi, Guenter

Thanks for the review.

Quoted 67 lines
Quoted 19 lines
Quoted 19 lines · Changhuang Liang
Quoted 9 lines

Add fan tach driver for StarFive JHB100 SoC.

The controller supports up to 16 independent fan tachometer 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() than reset_control_deassert()
remove: clk_disable_unprepare() than reset_control_assert()

s/than/then/

Quoted 6 lines
Signed-off-by Changhuang Liang

<[email protected]>
---

...

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
drivers/hwmon/starfive-fan-tach.c new file +16 -0
+
+static int starfive_fan_tach_hwmon_read(struct device *dev,
+					enum hwmon_sensor_types type, u32 attr,
+					int channel, long *val)
+{
+	struct starfive_fan_tach_data *priv = dev_get_drvdata(dev);
+	int ret = 0;
+
+	switch (attr) {
+	case hwmon_fan_fault:
+		scoped_guard(mutex, &priv->lock) {
+			scoped_guard(spinlock_irqsave, &priv->irq_lock) {
+				writel(STARFIVE_FAN_TACH_STALL_INT(channel),
+				       priv->regs + STARFIVE_FAN_TACH_STATUS);
+				/* clear fan_stall first */
+				priv->fan_stall[channel] = 0;
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/starfive-fan-tach.c line 241

Quoted 8 lines
drivers/hwmon/starfive-fan-tach.c new file +8 -0
+				reinit_completion(&priv->comp_stall[channel]);
+				priv->armed_stall |= BIT(channel);
+			}
+
+			starfive_fan_tach_ch_stall_unmask(priv, channel, true);
+
+			/* Waiting for hardware to measure */
+
	wait_for_completion_timeout(&priv->comp_stall[channel],
Quoted 22 lines
Quoted 18 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+						    2 *

STARFIVE_FAN_TACH_TIMEOUT_JIFFIES);

drivers/hwmon/starfive-fan-tach.c line 249

Quoted 9 lines
drivers/hwmon/starfive-fan-tach.c new file +9 -0
+
+			starfive_fan_tach_ch_stall_unmask(priv, channel, false);
+
+			scoped_guard(spinlock_irqsave, &priv->irq_lock) {
+				priv->armed_stall &= ~BIT(channel);
+				*val = priv->fan_stall[channel];
+			}
+		}
+

I'd really be interested to see how long it takes to read the alarm and fault status of all fans. And, yes, Sashiko has a point: the attributes are supported to report a sticky status, not something that is calculated on the fly. Is this based on some heuristics ? I don't claim to understand how fault and min_alarm are calculated. A comment in the code describing how this works would be helpful.

There is no real calculation for fault and min_alarm, the hardware simply counts the corresponding pulses within a specified time period and reports the counts via interrupt when the counting period

expires.

Quoted 55 lines
Quoted 7 lines
Quoted 2 lines

I will try to modify it so that the previous statistical result is recorded, and when reading, the previous result is returned, thus

eliminating the need to wait for the measurement.

Quoted 1 line

So how long does it take to execute the "sensors" command with 16 active fans ?

# time sensors
starfive_fan_tach-isa-0000
Adapter: ISA adapter
fan1:           FAULT  (min =  300 RPM)
fan2:           FAULT  (min =  300 RPM)
fan3:           FAULT  (min =  300 RPM)
fan4:           FAULT  (min =  300 RPM)
fan5:           FAULT  (min =  300 RPM)
fan6:           FAULT  (min =  300 RPM)
fan7:           FAULT  (min =  300 RPM)
fan8:           FAULT  (min =  300 RPM)
fan9:           FAULT  (min =  300 RPM)
fan10:          FAULT  (min =  300 RPM)
fan11:          FAULT  (min =  300 RPM)
fan12:          FAULT  (min =  300 RPM)
fan13:          FAULT  (min =  300 RPM)
fan14:          FAULT  (min =  300 RPM)
fan15:          FAULT  (min =  300 RPM)
fan16:       1800 RPM  (min =  300 RPM)
real    0m4.865s
user    0m0.023s
sys     0m0.001s
Quoted 20 lines

...

Quoted 13 lines
Quoted 12 lines

drivers/hwmon/starfive-fan-tach.c line 405

Quoted 7 lines
drivers/hwmon/starfive-fan-tach.c new file +7 -0
+
+	for (index = 0; index < count; index++) {
+		u8 ch = tach_ch[index];
+
+		if (ch >= STARFIVE_FAN_TACH_CH) {
+			dev_warn(priv->dev, "Invalid tach-ch %d, skipping\n", ch);
+			continue;

This should also return an error. Also, this is the only use of priv->dev, and
priv->dev == dev. priv->dev is therefore unnecessary.

Quoted 4 lines

Thank you for pointing that out. I noticed that the struct device *dev passed in the function starfive_fan_tach_create_fan() is somewhat redundant, so I plan to remove it directly and keep priv->dev instead.

Just for use in this function ? Why ? It is not used after probe.

Quoted 9 lines
Quoted 3 lines

After probe, priv->dev is also used. In the starfive_fan_tach_create_fan() function,
priv->dev can be used directly as well, so I plan to simply remove struct

device *dev.

Quoted 1 line

Are you checking if I pay attention ? starfive_fan_tach_create_fan() is called from the probe function, not "after" probe.

Sorry, I initially misunderstood your point. You're right, I re-examined the code and realized that priv->dev is only used in starfive_fan_tach_create_fan() during probe, and not used after probe at all. So it's indeed unnecessary.

I'll fix it as you suggested:

Remove struct device *dev from struct starfive_fan_tach_data.

Remove priv->dev = dev; from probe.

Keep the struct device *dev parameter in starfive_fan_tach_create_fan() (passed from probe) and use it directly instead of priv->dev.

Thanks for your patience.

Best Regards,
Changhuang

maintainer
#12 lore raw

Re: [PATCH v2 2/2] hwmon: (starfive-fan-tach) Add fan tach driver for StarFive JHB100

In reply to Changhuang Liang

On 8/20/26 18:06, Changhuang Liang wrote:
...

Quoted 24 lines
Quoted 2 lines

So how long does it take to execute the "sensors" command with 16 active fans ?

# time sensors
starfive_fan_tach-isa-0000
Adapter: ISA adapter
fan1:           FAULT  (min =  300 RPM)
fan2:           FAULT  (min =  300 RPM)
fan3:           FAULT  (min =  300 RPM)
fan4:           FAULT  (min =  300 RPM)
fan5:           FAULT  (min =  300 RPM)
fan6:           FAULT  (min =  300 RPM)
fan7:           FAULT  (min =  300 RPM)
fan8:           FAULT  (min =  300 RPM)
fan9:           FAULT  (min =  300 RPM)
fan10:          FAULT  (min =  300 RPM)
fan11:          FAULT  (min =  300 RPM)
fan12:          FAULT  (min =  300 RPM)
fan13:          FAULT  (min =  300 RPM)
fan14:          FAULT  (min =  300 RPM)
fan15:          FAULT  (min =  300 RPM)
fan16:       1800 RPM  (min =  300 RPM)
Quoted 4 lines
real    0m4.865s
user    0m0.023s
sys     0m0.001s

Normally I would say this is a no-go, but this is your chip (and, as we say, your funeral). Just please document that reading the fan status will take forever for disconnected fans.

Thanks,
Guenter

#13 lore raw

Re: [PATCH v2 2/2] hwmon: (starfive-fan-tach) Add fan tach driver for StarFive JHB100

Hi, Guenter

Thanks for the review.

Quoted 27 lines · Changhuang Liang

...

Quoted 24 lines
Quoted 2 lines

So how long does it take to execute the "sensors" command with 16 active fans ?

# time sensors
starfive_fan_tach-isa-0000
Adapter: ISA adapter
fan1:           FAULT  (min =  300 RPM)
fan2:           FAULT  (min =  300 RPM)
fan3:           FAULT  (min =  300 RPM)
fan4:           FAULT  (min =  300 RPM)
fan5:           FAULT  (min =  300 RPM)
fan6:           FAULT  (min =  300 RPM)
fan7:           FAULT  (min =  300 RPM)
fan8:           FAULT  (min =  300 RPM)
fan9:           FAULT  (min =  300 RPM)
fan10:          FAULT  (min =  300 RPM)
fan11:          FAULT  (min =  300 RPM)
fan12:          FAULT  (min =  300 RPM)
fan13:          FAULT  (min =  300 RPM)
fan14:          FAULT  (min =  300 RPM)
fan15:          FAULT  (min =  300 RPM)
fan16:       1800 RPM  (min =  300 RPM)
Quoted 8 lines
Quoted 4 lines
real    0m4.865s
user    0m0.023s
sys     0m0.001s

Normally I would say this is a no-go, but this is your chip (and, as we say, your funeral). Just please document that reading the fan status will take forever for disconnected fans.

Okay, I'll proceed with this solution for the time being, and I can revisit it later to explore possible improvements. I will also record the current status in the documentation

Best Regards,
Changhuang