Skip to contentSkip to conversation

hwmon: (adt7470) Add thermal zone and PWM provider support

PATCH Superseded Luiz Angelo Daros de Luca 5 participants 23 messages Jul 17 – Jul 23

Subsystems (3)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover hwmon: (adt7470) Add thermal zone and PWM provider support

The ADT7470 is a temperature monitor and PWM fan controller capable of monitoring up to 10 external temperature sensors and controlling up to 4 PWM outputs.

Currently, the driver exposes these sensors and PWM controls solely via the standard hwmon sysfs interface. This patch series integrates the ADT7470 driver with the kernel's thermal and PWM subsystems. This allows its temperature sensors to be referenced by Device Tree thermal zones, and its PWM channels to be consumed by generic Device Tree nodes (such as "pwm-fan").

Patch 1 adds the YAML device tree binding documentation. Patch 2 and 3 fix two pre-existing bugs found while testing this series (fans stuck in manual mode after an I2C error, and a busy-loop/I2C-flooding condition when auto_update_interval or num_temp_sensors is 0).
Patch 4 performs a minor refactoring to introduce the ADT7470_PWM_MAX macro.
Patch 5 registers the 4 PWM channels as a generic PWM provider. Patch 6 registers the external temperature sensors as optional thermal zones.

---
Changes in v2:
- Fixed 2 previous bugs uncovered by Sashiko-bot during review.
- Dropped the thermal cooling device registration for the PWM channels.
- Implemented generic PWM provider support using the PWM subsystem.
- Updated Kconfig to safely handle the PWM subsystem dependency (depends on PWM || PWM=n).
- Updated Device Tree bindings to require #pwm-cells instead of #cooling-cells.
- Link to v1: https://patch.msgid.link/[email protected]

To: Guenter Roeck <[email protected]>
To: Rob Herring <[email protected]>
To: Krzysztof Kozlowski <[email protected]>
To: Conor Dooley <[email protected]>
To: de Luca <[email protected]>
To: Chris Packham <[email protected]>
To: Andrew Morton <[email protected]>
To: "Darrick J. Wong" <[email protected]>
To: Uwe Kleine-König <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
Luiz Angelo Daros de Luca (6):
dt-bindings: hwmon: add binding for adi,adt7470
hwmon: (adt7470) Fix fans stuck in manual mode on I2C errors
hwmon: (adt7470) Fix busy-loop and I2C flooding in update thread
hwmon: (adt7470) Add ADT7470_PWM_MAX macro
hwmon: (adt7470) Register as a PWM provider
hwmon: (adt7470) Add thermal zone sensor support

 .../devicetree/bindings/hwmon/adi,adt7470.yaml     |  62 +++++++++++
 drivers/hwmon/Kconfig                              |   1 +
 drivers/hwmon/adt7470.c                            | 118 ++++++++++++++++++---
 3 files changed, 166 insertions(+), 15 deletions(-)

---
base-commit: ca078d004cf58137bcf8cb24a8b271397431ba58
change-id: 20260716-adt7470_thermalzone-59a102278d72

Best regards,

Signature · 1 line
Luiz Angelo Daros de Luca <[email protected]>
#1 lore raw

PATCH v2 1/6 dt-bindings: hwmon: add binding for adi,adt7470

The Analog Devices ADT7470 is a temperature monitor and PWM fan controller. It supports up to 10 external temperature sensors and up to 4 PWM fan outputs.

Add the YAML device tree binding documentation for it. This includes support for the thermal framework by defining the "#thermal-sensor-cells" property, and support for the PWM subsystem by defining the "#pwm-cells" property.

---
 .../devicetree/bindings/hwmon/adi,adt7470.yaml     | 62 ++++++++++++++++++++++
 1 file changed, 62 insertions(+)
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +62 -0
new file mode 100644
index 000000000000..14bb6359c91f
@@ -0,0 +1,62 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/hwmon/adi,adt7470.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Analog Devices ADT7470 hwmon sensor
+
+maintainers:
+  - Luiz Angelo Daros de Luca <[email protected]>
+
+description: |
+  The ADT7470 is a temperature monitor and multiple PWM outputs.
+  It supports monitoring up to 10 external temperature sensors and controlling
+  up to four fans.
+
+properties:
+  compatible:
+    const: adi,adt7470
+
+  reg:
+    maxItems: 1
+
+  "#pwm-cells":
+    const: 2
+    description: |
+      Number of cells in a PWM specifier.
+      - The first cell is the PWM channel (0 to 3).
+      - The second cell is the PWM period in nanoseconds.
+
+  "#thermal-sensor-cells":
+    const: 1
+    description: |
+      Number of cells required to uniquely identify the temperature sensors.
+      Valid index values are 0 to 9, corresponding to temp1 through temp10.
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        adt7470: hwmon@2f {
+            compatible = "adi,adt7470";
+            reg = <0x2f>;
+            #pwm-cells = <2>;
+            #thermal-sensor-cells = <1>;
+        };
+    };
+
+    fan0 {
+        compatible = "pwm-fan";
+        /* Connects to PWM channel 0 with a 50000 ns (20 kHz) period */
+        pwms = <&adt7470 0 50000>;
+        cooling-levels = <0 100 150 255>;
+    };
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH v2 1/6] dt-bindings: hwmon: add binding for adi,adt7470

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The binding defines PWM output and thermal sensor provider capabilities but fails to include…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The binding defines PWM output and thermal sensor provider capabilities but fails to include #pwm-cells and #thermal-sensor-cells in the required list. --

--- Patch [1]: [PATCH v2 1/6] dt-bindings: hwmon: add binding for adi,adt7470 --- commit 6372aff696282f1ac0ec9a8f1372c186734704a2
Author: Luiz Angelo Daros de Luca <[email protected]>

dt-bindings: hwmon: add binding for adi,adt7470

The ADT7470 hardware monitoring chip supports PWM fan control and temperature monitoring. This commit introduces the YAML device tree binding schema for it, defining PWM and thermal sensor provider capabilities.

Quoted 5 lines
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +0 -0
new file mode 100644
index 0000000000000..14bb6359c91f2

[ ... ]

Quoted 13 lines
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +13 -0
+properties:
+  compatible:
+    const: adi,adt7470
+
+  reg:
+    maxItems: 1
+
+  "#pwm-cells":
+    const: 2
+    description: |
+      Number of cells in a PWM specifier.
+      - The first cell is the PWM channel (0 to 3).
+      - The second cell is the PWM period in nanoseconds.

Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml line 39

Quoted 10 lines
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +10 -0
+
+  "#thermal-sensor-cells":
+    const: 1
+    description: |
+      Number of cells required to uniquely identify the temperature sensors.
+      Valid index values are 0 to 9, corresponding to temp1 through temp10.
+
+required:
+  - compatible
+  - reg

[Severity: Medium]
Should #pwm-cells and #thermal-sensor-cells be included in this required list?

Because this binding defines the device as a PWM output and thermal sensor provider, omitting these cell properties from the required list allows incomplete device tree nodes to silently pass schema validation.

If an adt7470 node is created without them, dependent drivers attempting to reference it as a provider may fail at runtime when parsing the provider specifier.

Could both #pwm-cells and #thermal-sensor-cells be added to the required array to enforce complete node validation?

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

Re: [PATCH v2 1/6] dt-bindings: hwmon: add binding for adi,adt7470

Quoted 55 lines

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The binding defines PWM output and thermal sensor provider capabilities but fails to include #pwm-cells and #thermal-sensor-cells in the required list. --

--- Patch [1]: [PATCH v2 1/6] dt-bindings: hwmon: add binding for adi,adt7470 --- commit 6372aff696282f1ac0ec9a8f1372c186734704a2
Author: Luiz Angelo Daros de Luca <[email protected]>

dt-bindings: hwmon: add binding for adi,adt7470

The ADT7470 hardware monitoring chip supports PWM fan control and temperature monitoring. This commit introduces the YAML device tree binding schema for it, defining PWM and thermal sensor provider capabilities.

Quoted 5 lines
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +0 -0
new file mode 100644
index 0000000000000..14bb6359c91f2

[ ... ]

Quoted 13 lines
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +13 -0
+properties:
+  compatible:
+    const: adi,adt7470
+
+  reg:
+    maxItems: 1
+
+  "#pwm-cells":
+    const: 2
+    description: |
+      Number of cells in a PWM specifier.
+      - The first cell is the PWM channel (0 to 3).
+      - The second cell is the PWM period in nanoseconds.

Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml line 39

Quoted 10 lines
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +10 -0
+
+  "#thermal-sensor-cells":
+    const: 1
+    description: |
+      Number of cells required to uniquely identify the temperature sensors.
+      Valid index values are 0 to 9, corresponding to temp1 through temp10.
+
+required:
+  - compatible
+  - reg

[Severity: Medium]
Should #pwm-cells and #thermal-sensor-cells be included in this required list?

Because this binding defines the device as a PWM output and thermal sensor provider, omitting these cell properties from the required list allows incomplete device tree nodes to silently pass schema validation.

If an adt7470 node is created without them, dependent drivers attempting to reference it as a provider may fail at runtime when parsing the provider specifier.

Quoted 5 lines

Could both #pwm-cells and #thermal-sensor-cells be added to the required array to enforce complete node validation?

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

It would be nice if Sashiko-bot could have previous submission threads as input context. It would be a great way to avoid false-positives like this.

The ADT7470 can be (and is, in real-world devices ) used as a PWM-only device. Likewise, nothing prevents its use solely as an ADC for temperature sensors. Because its use cases are modular, neither of these provider properties should be strictly required in the binding.

Regards,

Luiz

#4 lore raw

Re: [PATCH v2 1/6] dt-bindings: hwmon: add binding for adi,adt7470

In reply to Luiz Angelo Daros de Luca
Quoted 3 lines · Luiz Angelo Daros de Luca

The Analog Devices ADT7470 is a temperature monitor and PWM fan controller. It supports up to 10 external temperature sensors and up to 4 PWM fan outputs.

A nit, subject: drop second/last, redundant "binding for". The "dt-bindings" prefix is already stating that these are bindings. See also:
https://elixir.bootlin.com/linux/v7.1-rc7/source/Documentation/devicetree/bindings/submitting-patches.rst#L23

Quoted 2 lines

Add the YAML device tree binding documentation for it. This includes

s/YAML//
There is no YAML in Devicetree.

Quoted 17 lines

support for the thermal framework by defining the "#thermal-sensor-cells" property, and support for the PWM subsystem by defining the "#pwm-cells" property.

---
 .../devicetree/bindings/hwmon/adi,adt7470.yaml     | 62 ++++++++++++++++++++++
 1 file changed, 62 insertions(+)
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +2 -0
new file mode 100644
index 000000000000..14bb6359c91f
@@ -0,0 +1,62 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2

Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml line 12

Quoted 10 lines
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +10 -0
+---
+$id: http://devicetree.org/schemas/hwmon/adi,adt7470.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Analog Devices ADT7470 hwmon sensor
+
+maintainers:
+  - Luiz Angelo Daros de Luca <[email protected]>
+
+description: |

Do not need '|' unless you need to preserve formatting.

Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml line 15

Quoted 3 lines
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +3 -0
+  The ADT7470 is a temperature monitor and multiple PWM outputs.
+  It supports monitoring up to 10 external temperature sensors and controlling
+  up to four fans.

"controling fans" is confusing and suggests this is a fan controller, so you would need to reference proper schema for that. It is just PWM output, no? PWM output could also go to LEDs or motors (well, fan is a motor).

Quoted 8 lines
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +8 -0
+
+properties:
+  compatible:
+    const: adi,adt7470
+
+  reg:
+    maxItems: 1
+

Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml line 33

Quoted 10 lines
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +10 -0
+  "#pwm-cells":
+    const: 2
+    description: |
+      Number of cells in a PWM specifier.
+      - The first cell is the PWM channel (0 to 3).
+      - The second cell is the PWM period in nanoseconds.
+
+  "#thermal-sensor-cells":
+    const: 1
+    description: |

Do not need '|' unless you need to preserve formatting.

Quoted 14 lines
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +14 -0
+      Number of cells required to uniquely identify the temperature sensors.
+      Valid index values are 0 to 9, corresponding to temp1 through temp10.
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;

Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml line 57

Quoted 10 lines
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +10 -0
+
+        adt7470: hwmon@2f {
+            compatible = "adi,adt7470";
+            reg = <0x2f>;
+            #pwm-cells = <2>;
+            #thermal-sensor-cells = <1>;
+        };
+    };
+
+    fan0 {

Drio the node, not relevant. And then drop adt7470 label from hwmon node.

Quoted 9 lines
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +5 -0
+        compatible = "pwm-fan";
+        /* Connects to PWM channel 0 with a 50000 ns (20 kHz) period */
+        pwms = <&adt7470 0 50000>;
+        cooling-levels = <0 100 150 255>;
+    };
Signature · 2 lines
-- 
2.55.0
maintainer
#5 lore raw

Re: [PATCH v2 1/6] dt-bindings: hwmon: add binding for adi,adt7470

Quoted 46 lines · Krzysztof Kozlowski
Quoted 3 lines · Luiz Angelo Daros de Luca

The Analog Devices ADT7470 is a temperature monitor and PWM fan controller. It supports up to 10 external temperature sensors and up to 4 PWM fan outputs.

A nit, subject: drop second/last, redundant "binding for". The "dt-bindings" prefix is already stating that these are bindings. See also:
https://elixir.bootlin.com/linux/v7.1-rc7/source/Documentation/devicetree/bindings/submitting-patches.rst#L23

Quoted 2 lines

Add the YAML device tree binding documentation for it. This includes

s/YAML//
There is no YAML in Devicetree.

Quoted 17 lines

support for the thermal framework by defining the "#thermal-sensor-cells" property, and support for the PWM subsystem by defining the "#pwm-cells" property.

---
  .../devicetree/bindings/hwmon/adi,adt7470.yaml     | 62 ++++++++++++++++++++++
  1 file changed, 62 insertions(+)
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +2 -0
new file mode 100644
index 000000000000..14bb6359c91f
@@ -0,0 +1,62 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2

Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml line 12

Quoted 10 lines
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +10 -0
+---
+$id: http://devicetree.org/schemas/hwmon/adi,adt7470.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Analog Devices ADT7470 hwmon sensor
+
+maintainers:
+  - Luiz Angelo Daros de Luca <[email protected]>
+
+description: |

Do not need '|' unless you need to preserve formatting.

Quoted 9 lines

Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml line 15

Quoted 3 lines
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +3 -0
+  The ADT7470 is a temperature monitor and multiple PWM outputs.
+  It supports monitoring up to 10 external temperature sensors and controlling
+  up to four fans.

"controling fans" is confusing and suggests this is a fan controller, so you would need to reference proper schema for that. It is just PWM output, no? PWM output could also go to LEDs or motors (well, fan is a motor).

From the datasheet: "The ADT74701 controller is a multichannel temperature sensor and PWM fan controller and fan speed monitor".

It is a fan controller which happens to use pwm signals to control fan speeds. It is not a PWM controller, and its pwm outputs are not intended to be used for anything but controlling fan speeds.

Technically it is _possible_ to use the pwm outputs to control something else. But that does not mean that chip is a pwm controller. It is still a fan controller. I don't mind modeling outputs as non-fan-control pwm channels if that is what they are used for, but that must be clearly distinguished in both the driver code and the bindings.

Guenter

#6 lore raw

Re: [PATCH v2 1/6] dt-bindings: hwmon: add binding for adi,adt7470

Quoted 7 lines

From the datasheet: "The ADT74701 controller is a multichannel
temperature sensor and PWM fan controller and fan speed monitor".

It is a fan controller which happens to use pwm signals to control fan speeds. It is not a PWM controller, and its pwm outputs are not intended to be used for anything but controlling fan speeds.

Quoted 6 lines

Technically it is _possible_ to use the pwm outputs to control something else. But that does not mean that chip is a pwm controller. It is still a fan controller. I don't mind modeling outputs as non-fan-control pwm channels if that is what they are used for, but that must be clearly distinguished in both the driver code and the bindings.

Hi Guenter,

You are absolutely right. The datasheet explicitly defines the ADT7470 as a "PWM fan controller and fan speed monitor," and its internal features (like tachometer inputs and thermal acoustic enhancement) are heavily tailored for fan control. I agree that calling it a generic PWM controller is technically inaccurate. I will revert the change and describe it as a PWM Fan Controller, provided everyone is on board with that.

Initially, I modeled the driver purely as a cooling device. However, following Sashiko-dev's suggestion in a previous review, I converted it to expose PWM channels so it could be used with the generic pwm-fan driver (which indeed makes the code cleaner). My primary experience is with DSA switches, so hwmon is a new area for me.

Whatever you and the DT maintainers agree on, I will gladly adjust for v3.

Best regards,

Luiz

#7 lore raw

Re: [PATCH v2 1/6] dt-bindings: hwmon: add binding for adi,adt7470

Hi Luiz,

Quoted 5 lines · Luiz Angelo Daros de Luca

Initially, I modeled the driver purely as a cooling device. However, following Sashiko-dev's suggestion in a previous review, I converted it to expose PWM channels so it could be used with the generic pwm-fan driver (which indeed makes the code cleaner). My primary experience is with DSA switches, so hwmon is a new area for me.

I like the driver exposing a generic PWM. For me it's quite in line with the "Linux spirit" to do things with devices that are possible even if that doesn't figure in the vendor's scheme.

Quoted 1 line

Whatever you and the DT maintainers agree on, I will gladly adjust for v3.

But my opinion doesn't count ... :-)

Best regards
Uwe

#8 lore raw

Re: [PATCH v2 1/6] dt-bindings: hwmon: add binding for adi,adt7470

In reply to Luiz Angelo Daros de Luca

Hello,

Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml line 29

Quoted 6 lines · Luiz Angelo Daros de Luca
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +6 -0
+  "#pwm-cells":
+    const: 2
+    description: |
+      Number of cells in a PWM specifier.
+      - The first cell is the PWM channel (0 to 3).
+      - The second cell is the PWM period in nanoseconds.

Please use #pwm-cells = <3> and $ref: pwm.yaml#

(I'm not 100% the latter works for such a device that is a PWM and a hwmon, but I think it does.)

Best regards
Uwe

#9 lore raw

Re: [PATCH v2 1/6] dt-bindings: hwmon: add binding for adi,adt7470

Hi Uwe,

Quoted 9 lines · Luiz Angelo Daros de Luca

Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml line 29

Quoted 6 lines
Documentation/devicetree/bindings/hwmon/adi,adt7470.yaml new file +6 -0
+  "#pwm-cells":
+    const: 2
+    description: |
+      Number of cells in a PWM specifier.
+      - The first cell is the PWM channel (0 to 3).
+      - The second cell is the PWM period in nanoseconds.

Please use #pwm-cells = <3> and $ref: pwm.yaml#

Thanks for the review. I already switched to #pwm-cells = <3> for the next version, so that part is settled.

Regarding $ref: /schemas/pwm/pwm.yaml#, it doesn't quite work here. When I added it and ran
dt_binding_check, it started rejecting the example node:

hwmon@2f (adi,adt7470): $nodename:0: 'hwmon@2f' does not match '^pwm(@.*|-([0-9]|[1-9][0-9]+))?$'

It turns out pwm.yaml also constrains $nodename as part of the allOf, so referencing it forces the node name into the pwm@... pattern. That's not right for this device: the ADT7470 is primarily a hwmon chip (10 temperature inputs, 4 fan/PWM outputs), and the PWM channels are just one of the interfaces it exposes, so hwmon@2f is the correct node name here, not pwm@2f.

So, for the next version, I'm declaring #pwm-cells and #thermal-sensor-cells directly in the binding instead of pulling in pwm.yaml via $ref, and pointing to pwm.yaml only in the description for context:

"#pwm-cells":
const: 3
description:
See Documentation/devicetree/bindings/pwm/pwm.yaml for the generic PWM binding description.

Let me know if you'd rather see this handled differently (e.g. keeping the $ref and accepting the pwm@ node name), but I think keeping the node name aligned with the device's primary function is the more correct approach.

Regards,

Luiz

#10 lore raw

Re: [PATCH v2 1/6] dt-bindings: hwmon: add binding for adi,adt7470

Hello,

Quoted 11 lines · Luiz Angelo Daros de Luca

So, for the next version, I'm declaring #pwm-cells and #thermal-sensor-cells directly in the binding instead of pulling in pwm.yaml via $ref, and pointing to pwm.yaml only in the description for context:

"#pwm-cells":
const: 3
description:
See Documentation/devicetree/bindings/pwm/pwm.yaml for the generic PWM binding description.

Quoted 4 lines

Let me know if you'd rather see this handled differently (e.g. keeping the $ref and accepting the pwm@ node name), but I think keeping the node name aligned with the device's primary function is the more correct approach.

Sounds sensible. Thanks for looking into that.

Best regards
Uwe

#11 lore raw

PATCH v2 2/6 hwmon: (adt7470) Fix fans stuck in manual mode on I2C errors

In reply to Luiz Angelo Daros de Luca

During adt7470_read_temperatures(), the driver temporarily switches the PWM channels to manual mode, performs the temperature collection, and then restores the original configuration registers.

However, if an I2C transaction fails at any point after entering manual mode, the function aborts and returns immediately. This leaves the configuration registers un-restored, permanently trapping the fans in manual mode.

Introduce a recovery path to ensure that the original PWM configuration registers are always restored, even when intermediate I2C operations fail.

Reported-by [email protected]
Fixes ef67959c4253 ("hwmon: (adt7470) Convert to use regmap")
---
 drivers/hwmon/adt7470.c | 40 +++++++++++++++++++++++++++++-----------
 1 file changed, 29 insertions(+), 11 deletions(-)
drivers/hwmon/adt7470.c +29 -11
index 664349756dc2..481d51617f4b 100644
@@ -205,11 +205,12 @@ static inline int adt7470_write_word_data(struct adt7470_data *data, unsigned in
 /* Probe for temperature sensors.  Assumes lock is held */
 static int adt7470_read_temperatures(struct adt7470_data *data)
 {
-	unsigned long res;
+	struct device *dev = regmap_get_device(data->regmap);
+	u8 pwm[ADT7470_FAN_COUNT];
 	unsigned int pwm_cfg[2];
-	int err;
+	unsigned long res;
+	int err, err2;
 	int i;
-	u8 pwm[ADT7470_FAN_COUNT];
 
 	/* save pwm[1-4] config register */
 	err = regmap_read(data->regmap, ADT7470_REG_PWM_CFG(0), &pwm_cfg[0]);
@@ -233,19 +234,19 @@ static int adt7470_read_temperatures(struct adt7470_data *data)
 	err = regmap_update_bits(data->regmap, ADT7470_REG_PWM_CFG(2),
 				 ADT7470_PWM_AUTO_MASK, 0);
 	if (err < 0)
-		return err;
+		goto out_restore;
 
 	/* write pwm control to whatever it was */
 	err = regmap_bulk_write(data->regmap, ADT7470_REG_PWM(0), &pwm[0],
 				ADT7470_PWM_COUNT);
 	if (err < 0)
-		return err;
+		goto out_restore;
 
 	/* start reading temperature sensors */
 	err = regmap_update_bits(data->regmap, ADT7470_REG_CFG,
 				 ADT7470_T05_STB_MASK, ADT7470_T05_STB_MASK);
 	if (err < 0)
-		return err;
+		goto out_restore;
 
 	/* Delay is 200ms * number of temp sensors. */
 	res = msleep_interruptible((data->num_temp_sensors >= 0 ?
@@ -256,13 +257,30 @@ static int adt7470_read_temperatures(struct adt7470_data *data)
 	err = regmap_update_bits(data->regmap, ADT7470_REG_CFG,
 				 ADT7470_T05_STB_MASK, 0);
 	if (err < 0)
-		return err;
+		goto out_restore;
 
+out_restore:
 	/* restore pwm[1-4] config registers */
-	err = regmap_write(data->regmap, ADT7470_REG_PWM_CFG(0), pwm_cfg[0]);
-	if (err < 0)
-		return err;
-	err = regmap_write(data->regmap, ADT7470_REG_PWM_CFG(2), pwm_cfg[1]);
+	err2 = regmap_write(data->regmap, ADT7470_REG_PWM_CFG(0), pwm_cfg[0]);
+	if (err2 < 0) {
+		dev_warn_ratelimited(dev,
+				     "failed to restore PWM{1,2} config (%d)\n",
+				     err2);
+
+		if (!err)
+			err = err2;
+	}
+
+	err2 = regmap_write(data->regmap, ADT7470_REG_PWM_CFG(2), pwm_cfg[1]);
+	if (err2 < 0) {
+		dev_warn_ratelimited(dev,
+				     "failed to restore PWM{3,4} config (%d)\n",
+				     err2);
+
+		if (!err)
+			err = err2;
+	}
+
 	if (err < 0)
 		return err;
 
Signature · 1 line
2.55.0
#12 lore raw

Re: [PATCH v2 2/6] hwmon: (adt7470) Fix fans stuck in manual mode on I2C errors

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the driver violates hwmon subsystem guidelines by using a private mutex data->lock for sysfs attributes instead of the standard hwmon_lock() API. --

--- Patch [2]: [PATCH v2 2/6] hwmon: (adt7470) Fix fans stuck in manual mode on I2C errors --- commit f5bf8b0ee6f2d048580f6d00a71e61df362fa4c6
Author: Luiz Angelo Daros de Luca <[email protected]>

hwmon: (adt7470) Fix fans stuck in manual mode on I2C errors

This commit introduces a recovery path in adt7470_read_temperatures() to ensure that original PWM configuration registers are restored if an I2C operation fails after temporarily switching the fans to manual mode.

drivers/hwmon/adt7470.c static inline int adt7470_write_word_data(struct adt7470_data *data, unsigned in · line 207

Quoted 8 lines
drivers/hwmon/adt7470.c +0 -0
index 664349756dc2b..481d51617f4be 100644
@@ -205,11 +205,12 @@ static inline int adt7470_write_word_data(struct adt7470_data *data, unsigned in
 /* Probe for temperature sensors.  Assumes lock is held */
 static int adt7470_read_temperatures(struct adt7470_data *data)
 {

[Severity: High]
This isn't a bug introduced by this patch, but this driver appears to use a private mutex (data->lock) for internal synchronization instead of the standard hwmon_lock() API.

For example, it uses mutex_init(&data->lock) in adt7470_probe() and locks it with mutex_lock(&data->lock) in sysfs attribute operations like pwm_auto_temp_store().

Does this bypass the centralized hwmon core serialization mechanism and risk logical race conditions with the thermal subsystem? Should this be updated to follow the hwmon subsystem guidelines which mandate using hwmon_lock() and hwmon_unlock() for attributes not registered via the info parameter?

[ ... ]

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

PATCH v2 3/6 hwmon: (adt7470) Fix busy-loop and I2C flooding in update thread

In reply to Luiz Angelo Daros de Luca

When userspace configures 'auto_update_interval' to 0 via sysfs, the background kthread executes schedule_timeout_interruptible(0), which returns immediately.

If 'num_temp_sensors' is concurrently or previously set to 0, the msleep_interruptible() delay inside adt7470_read_temperatures() also becomes 0. This combination forces the background thread into a tight, unbounded busy-loop, hogging the CPU and flooding the I2C bus with a continuous stream of transactions.

Fix this vulnerability by raising the lower limit of the clamp_val in auto_update_interval_store() from 0 to 500 milliseconds. This guarantees a reasonable minimum sleep window between sensor updates, protecting the system from intentional or accidental I2C bus denial of service.

Reported-by [email protected]
Fixes 89fac11cb3e7 ("adt7470: make automatic fan control really work")
---
 drivers/hwmon/adt7470.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/adt7470.c +1 -1
index 481d51617f4b..77e7be692790 100644
@@ -509,7 +509,7 @@ static ssize_t auto_update_interval_store(struct device *dev,
 	if (kstrtol(buf, 10, &temp))
 		return -EINVAL;
 
-	temp = clamp_val(temp, 0, 60000);
+	temp = clamp_val(temp, 500, 60000);
 
 	mutex_lock(&data->lock);
 	data->auto_update_interval = temp;
Signature · 1 line
2.55.0
#14 lore raw

Re: [PATCH v2 3/6] hwmon: (adt7470) Fix busy-loop and I2C flooding in update thread

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Pre-existing issues:
- [High] Module unload hang due to missed wakeup in kthread loop
- [High] Temperature alarm bitmask logic error causes false negatives and incorrect states
- [High] Unfreezable polling kthread can crash or hang the I2C bus during system suspend
- [High] Subsystem guideline violation: Driver uses private lock instead of hwmon_lock() --

--- Patch [3]: [PATCH v2 3/6] hwmon: (adt7470) Fix busy-loop and I2C flooding in update thread --- commit e6d87eed20a630165f01d0513e17b82247a7f775
Author: Luiz Angelo Daros de Luca <[email protected]>

hwmon: (adt7470) Fix busy-loop and I2C flooding in update thread

This commit prevents a busy-loop and I2C flooding in the background kthread when userspace configures auto_update_interval and num_temp_sensors to 0. It resolves the issue by raising the lower limit of the clamp_val in auto_update_interval_store() from 0 to 500 milliseconds.

drivers/hwmon/adt7470.c static ssize_t auto_update_interval_store(struct device *dev, · line 514

Quoted 12 lines
drivers/hwmon/adt7470.c +1 -1
index 481d51617f4be..77e7be6927906 100644
@@ -509,7 +509,7 @@ static ssize_t auto_update_interval_store(struct device *dev,
 	if (kstrtol(buf, 10, &temp))
 		return -EINVAL;
 
-	temp = clamp_val(temp, 0, 60000);
+	temp = clamp_val(temp, 500, 60000);
 
 	mutex_lock(&data->lock);

[Severity: High]
This isn't a bug introduced by this patch, but does the use of a private mutex for an extra sysfs attribute violate the HWMON subsystem directive to use the central hwmon_lock() and hwmon_unlock() for this purpose?

drivers/hwmon/adt7470.c static ssize_t auto_update_interval_store(struct device *dev, · line 515

Quoted 1 line
drivers/hwmon/adt7470.c +0 -0
 	data->auto_update_interval = temp;

I would also like to ask about some other pre-existing issues in this file:

drivers/hwmon/adt7470.c:adt7470_update_thread() {
    [ ... ]
	while (!kthread_should_stop()) {
		mutex_lock(&data->lock);
		adt7470_read_temperatures(data);
		mutex_unlock(&data->lock);

		if (kthread_should_stop())
			break;

		schedule_timeout_interruptible(msecs_to_jiffies(data->auto_update_interval));
	}
    [ ... ]
}

[Severity: High]
This is a pre-existing issue, but does checking the stop condition while the thread is in TASK_RUNNING without preparing to sleep cause missed wakeups?

If the unbind process calls kthread_stop(), it sets the stop flag and wakes the thread. Because the thread is still in TASK_RUNNING, the wake is a no-op, and the thread will then call schedule_timeout_interruptible() and go to sleep, hanging module unload for up to 60 seconds.

[Severity: High]
This is also a pre-existing issue, but does this kthread lack set_freezable() and try_to_freeze()? It appears to unconditionally attempt I2C access while the bus might be suspended. Can this crash or hang the I2C bus during system suspend and resume transitions?

drivers/hwmon/adt7470.c:adt7470_temp_read() {
    [ ... ]
	case hwmon_temp_alarm:
		*val = !!(data->alarm & channel);
		break;
    [ ... ]
}

[Severity: High]
This is a pre-existing issue, but does this logic incorrectly use the integer index channel as a bitwise AND mask instead of a bit shift? Could this yield completely incorrect hardware alarm states, preventing userspace from properly monitoring temperature emergencies?

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

PATCH v2 4/6 hwmon: (adt7470) Add ADT7470_PWM_MAX macro

In reply to Luiz Angelo Daros de Luca

Instead of a magic 255, use a macro to refer to it.

---
 drivers/hwmon/adt7470.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/hwmon/adt7470.c +4 -3
index 77e7be692790..d881609c99aa 100644
@@ -100,6 +100,7 @@ static const unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END };
 #define ADT7470_REG_FAN_MAX(x)	(ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2))
 
 #define ADT7470_PWM_COUNT	4
+#define ADT7470_PWM_MAX		255
 #define ADT7470_REG_PWM(x)	(ADT7470_REG_PWM_BASE_ADDR + (x))
 #define ADT7470_REG_PWM_MAX(x)	(ADT7470_REG_PWM_MAX_BASE_ADDR + (x))
 #define ADT7470_REG_PWM_MIN(x)	(ADT7470_REG_PWM_MIN_BASE_ADDR + (x))
@@ -829,7 +830,7 @@ static int adt7470_pwm_write(struct device *dev, u32 attr, int channel, long val
 
 	switch (attr) {
 	case hwmon_pwm_input:
-		val = clamp_val(val, 0, 255);
+		val = clamp_val(val, 0, ADT7470_PWM_MAX);
 		mutex_lock(&data->lock);
 		data->pwm[channel] = val;
 		err = regmap_write(data->regmap, ADT7470_REG_PWM(channel),
@@ -887,7 +888,7 @@ static ssize_t pwm_max_store(struct device *dev,
 	if (kstrtol(buf, 10, &temp))
 		return -EINVAL;
 
-	temp = clamp_val(temp, 0, 255);
+	temp = clamp_val(temp, 0, ADT7470_PWM_MAX);
 
 	mutex_lock(&data->lock);
 	data->pwm_max[attr->index] = temp;
@@ -922,7 +923,7 @@ static ssize_t pwm_min_store(struct device *dev,
 	if (kstrtol(buf, 10, &temp))
 		return -EINVAL;
 
-	temp = clamp_val(temp, 0, 255);
+	temp = clamp_val(temp, 0, ADT7470_PWM_MAX);
 
 	mutex_lock(&data->lock);
 	data->pwm_min[attr->index] = temp;
Signature · 1 line
2.55.0
#16 lore raw

Re: [PATCH v2 4/6] hwmon: (adt7470) Add ADT7470_PWM_MAX macro

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

Instead of a magic 255, use a macro to refer to it.

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=4
#17 lore raw

PATCH v2 5/6 hwmon: (adt7470) Register as a PWM provider

In reply to Luiz Angelo Daros de Luca

hwmon: (adt7470) Register as a PWM provider

The ADT7470 features four PWM outputs that can be used to control fans. Previously, these were only accessible through the legacy hwmon sysfs interface.

Register the ADT7470 as a generic PWM provider. This enables standard Device Tree PWM consumers, such as "pwm-fan", to use the device through the "#pwm-cells" property.

When a PWM consumer applies a new PWM state, the driver automatically switches the corresponding PWM channel to manual mode so that the requested duty cycle takes effect. The duty cycle specified by the PWM framework is internally converted to the 0-255 scale expected by the hardware registers.

The legacy sysfs interface remains unaffected and operates in parallel.

---
 drivers/hwmon/Kconfig   |  1 +
 drivers/hwmon/adt7470.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)
drivers/hwmon/Kconfig +1 -0
index 08c29685126a..8838b6714e60 100644
@@ -224,6 +224,7 @@ config SENSORS_ADT7462
 config SENSORS_ADT7470
 	tristate "Analog Devices ADT7470"
 	depends on I2C
+	depends on PWM || PWM=n
 	select REGMAP_I2C
 	help
 	  If you say yes here you get support for the Analog Devices
drivers/hwmon/adt7470.c +67 -0

85 lines not on this page · raw message

Signature · 1 line
2.55.0
#18 lore raw

Re: [PATCH v2 5/6] hwmon: (adt7470) Register as a PWM provider

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

New issues:
- [High] Calculating the PWM duty cycle using DIV_ROUND_CLOSEST_ULL with a 64-bit divisor causes 32-bit truncation and a divide-by-zero kernel panic on 32-bit architectures.
- [High] Early return in PWM apply relies on unsynchronized, zero-initialized cache values, potentially ignoring the first 0% duty cycle request.
- [Medium] Registering a generic PWM provider directly from a hardware monitoring driver violates subsystem layering guidelines.
- [Medium] The driver silently ignores the requested PWM signal polarity, applying normal polarity even when inverted is requested.

Pre-existing issues:
- [High] The PWM automatic mode mask calculation swaps the masks for PWM channels 3 and 4, causing writes to one channel to affect the configuration of the other. --

--- Patch [5]: [PATCH v2 5/6] hwmon: (adt7470) Register as a PWM provider --- commit e11bee28efc81066c4940f5c07591cd6181c6453
Author: Luiz Angelo Daros de Luca <[email protected]>

hwmon: (adt7470) Register as a PWM provider

The patch registers the ADT7470 hardware monitoring driver as a generic PWM provider, allowing standard Device Tree consumers like "pwm-fan" to use it. It converts the PWM framework's requested duty cycle to the 0-255 scale used by the hardware registers. The legacy sysfs interface operates in parallel and remains unaffected.

Quoted 4 lines
drivers/hwmon/adt7470.c +0 -0
index d881609c99aa3..aaae14a421cd8 100644

[ ... ]

Quoted 6 lines
drivers/hwmon/adt7470.c +2 -0
@@ -864,6 +865,57 @@ static int adt7470_pwm_write(struct device *dev, u32 attr, int channel, long val
 	return err;
 }
 
+static int adt7470_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
+			     const struct pwm_state *state)

drivers/hwmon/adt7470.c static int adt7470_pwm_write(struct device *dev, u32 attr, int channel, long val · line 879

Quoted 10 lines
drivers/hwmon/adt7470.c +10 -0
+{
+	struct adt7470_data *data = pwmchip_get_drvdata(chip);
+	unsigned int pwm_auto_reg_mask;
+	int err;
+	u8 val;
+
+	if (pwm->hwpwm % 2)
+		pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
+	else
+		pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;

[Severity: High]
This is a pre-existing issue, but does this logic incorrectly swap the masks for PWM channels 3 and 4?

The logic uses hwpwm % 2 to select between ADT7470_PWM1_AUTO_MASK and ADT7470_PWM2_AUTO_MASK. For channel 3 (hwpwm = 2) and channel 4 (hwpwm = 3), does this inadvertently select 0x80 for PWM3 and 0x40 for PWM4?

If the datasheet and macro definitions dictate 0x40 for PWM3 and 0x80 for PWM4, could writing to one of these channels corrupt the configuration of the other?

This issue seems to be copied from the preexisting adt7470_pwm_write() sysfs handler.

drivers/hwmon/adt7470.c static int adt7470_pwm_write(struct device *dev, u32 attr, int channel, long val · line 881

Quoted 2 lines
drivers/hwmon/adt7470.c +2 -0
+
+	if (state->enabled && state->period > 0)

[Severity: Medium]
Does this apply callback silently ignore requested PWM polarities?

If a consumer specifies PWM_POLARITY_INVERSED in the PWM state, the hardware only supports normal polarity.

Since there is no check to see if state->polarity != PWM_POLARITY_NORMAL, will this cause the driver to quietly apply normal polarity instead of rejecting the unsupported request?

drivers/hwmon/adt7470.c static int adt7470_pwm_write(struct device *dev, u32 attr, int channel, long val · line 882

Quoted 1 line
drivers/hwmon/adt7470.c +1 -0
+		val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * 255, state->period);

[Severity: High]
Does this calculation cause a divide-by-zero on 32-bit architectures?

When a consumer requests a PWM period that is a multiple of 2^32 ns, do_div() implicitly casts the divisor to a 32-bit integer on 32-bit systems, meaning a value like 1ULL << 32 becomes 0.

Since the truncation happens during the division after the state->period > 0 check, can this lead to a kernel panic?

drivers/hwmon/adt7470.c static int adt7470_pwm_write(struct device *dev, u32 attr, int channel, long val · line 892

Quoted 10 lines
drivers/hwmon/adt7470.c +10 -0
+	else
+		val = 0;
+
+	mutex_lock(&data->lock);
+
+	if (data->pwm[pwm->hwpwm] == val &&
+	    data->pwm_automatic[pwm->hwpwm] == 0) {
+		mutex_unlock(&data->lock);
+		return 0;
+	}

[Severity: High]
Can this early return falsely ignore the first 0% duty cycle request?

Because the driver data is allocated with devm_kzalloc(), data->pwm is zero-initialized.

If a consumer requests a 0% duty cycle (val = 0) before the adt7470_update_thread has completed its first hardware state synchronization, will this condition pass and return early without sending the 0% duty cycle to the hardware?

[ ... ]

Quoted 8 lines
drivers/hwmon/adt7470.c +4 -0
@@ -1299,6 +1351,21 @@ static int adt7470_probe(struct i2c_client *client)
 	if (IS_ERR(hwmon_dev))
 		return PTR_ERR(hwmon_dev);
 
+	if (IS_REACHABLE(CONFIG_PWM)) {
+		struct pwm_chip *chip;
+
+		chip = devm_pwmchip_alloc(dev, ADT7470_PWM_COUNT, 0);

drivers/hwmon/adt7470.c static int adt7470_probe(struct i2c_client *client) · line 1367

Quoted 10 lines
drivers/hwmon/adt7470.c +10 -0
+		if (IS_ERR(chip))
+			return PTR_ERR(chip);
+
+		chip->ops = &adt7470_pwm_ops;
+		pwmchip_set_drvdata(chip, data);
+
+		err = devm_pwmchip_add(dev, chip);
+		if (err)
+			return dev_warn_probe(dev, err, "failed to register PWM chip\n");
+	}

[Severity: Medium]
Does this violate the hardware monitoring subsystem guidelines?

The guidelines indicate that secondary functionalities, such as PWM or GPIO, should be implemented as auxiliary devices rather than registering directly from drivers/hwmon/.

By using devm_pwmchip_alloc() and devm_pwmchip_add() directly in the hwmon probe function, does this bypass the separation into an auxiliary device and cross-subsystem review?

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

Re: [PATCH v2 5/6] hwmon: (adt7470) Register as a PWM provider

In reply to Luiz Angelo Daros de Luca
Quoted 9 lines · Luiz Angelo Daros de Luca

hwmon: (adt7470) Register as a PWM provider

The ADT7470 features four PWM outputs that can be used to control fans. Previously, these were only accessible through the legacy hwmon sysfs interface.

Register the ADT7470 as a generic PWM provider. This enables standard Device Tree PWM consumers, such as "pwm-fan", to use the device through the "#pwm-cells" property.

Use case, please.

Thanks,
Guenter

#20 lore raw

Re: [PATCH v2 5/6] hwmon: (adt7470) Register as a PWM provider

In reply to Luiz Angelo Daros de Luca

Hello,

Quoted 9 lines · Luiz Angelo Daros de Luca
drivers/hwmon/adt7470.c +5 -0
@@ -864,6 +865,57 @@ static int adt7470_pwm_write(struct device *dev, u32 attr, int channel, long val
 	return err;
 }
 
+static int adt7470_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
+			     const struct pwm_state *state)
+{
+	struct adt7470_data *data = pwmchip_get_drvdata(chip);
+	unsigned int pwm_auto_reg_mask;

drivers/hwmon/adt7470.c static int adt7470_pwm_write(struct device *dev, u32 attr, int channel, long val · line 882

Quoted 10 lines
drivers/hwmon/adt7470.c +10 -0
+	int err;
+	u8 val;
+
+	if (pwm->hwpwm % 2)
+		pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
+	else
+		pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
+
+	if (state->enabled && state->period > 0)
+		val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * 255, state->period);

rounding a division using closest is wrong in the context of PWM.

Also state->duty_cycle * 255 might overflow and you have to use the actual period in the calculation of val instead of the requested value.

drivers/hwmon/adt7470.c static int adt7470_pwm_write(struct device *dev, u32 attr, int channel, long val · line 886

Quoted 4 lines
drivers/hwmon/adt7470.c +4 -0
+	else
+		val = 0;
+
+	mutex_lock(&data->lock);

If you use guard(), the control flow simplifies and using goto becomes unnecessary.

Quoted 19 lines
drivers/hwmon/adt7470.c +19 -0
+	if (data->pwm[pwm->hwpwm] == val &&
+	    data->pwm_automatic[pwm->hwpwm] == 0) {
+		mutex_unlock(&data->lock);
+		return 0;
+	}
+
+	/* Put the PWM channel in manual mode before updating it. */
+	err = regmap_update_bits(data->regmap,
+				 ADT7470_REG_PWM_CFG(pwm->hwpwm),
+				 pwm_auto_reg_mask, 0);
+	if (err < 0)
+		goto out;
+
+	data->pwm_automatic[pwm->hwpwm] = 0;
+
+	err = regmap_write(data->regmap,
+			   ADT7470_REG_PWM(pwm->hwpwm), val);
+	if (err < 0)
+		goto out;

drivers/hwmon/adt7470.c static int adt7470_pwm_write(struct device *dev, u32 attr, int channel, long val · line 916

Quoted 10 lines
drivers/hwmon/adt7470.c +10 -0
+
+	data->pwm[pwm->hwpwm] = val;
+out:
+	mutex_unlock(&data->lock);
+
+	return err;
+}
+
+static const struct pwm_ops adt7470_pwm_ops = {
+	.apply = adt7470_pwm_apply,

For new drivers please implement .round_waveform_tohw(), .round_waveform_fromhw(), .write_waveform() and ideally so .read_waveform().

drivers/hwmon/adt7470.c static int adt7470_pwm_write(struct device *dev, u32 attr, int channel, long val · line 917

Quoted 1 line
drivers/hwmon/adt7470.c +1 -0
+};

Best regards
Uwe

#21 lore raw

PATCH v2 6/6 hwmon: (adt7470) Add thermal zone sensor support

In reply to Luiz Angelo Daros de Luca

Expose the ADT7470 external temperature sensors to the thermal framework using HWMON_C_REGISTER_TZ.

---
 drivers/hwmon/adt7470.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/hwmon/adt7470.c +2 -0
index aaae14a421cd..595a8f934d60 100644
@@ -1251,6 +1251,8 @@ static const struct hwmon_ops adt7470_hwmon_ops = {
 };
 
 static const struct hwmon_channel_info * const adt7470_info[] = {
+	HWMON_CHANNEL_INFO(chip,
+			   HWMON_C_REGISTER_TZ),
 	HWMON_CHANNEL_INFO(temp,
 			   HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM,
 			   HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM,
Signature · 1 line
2.55.0
#22 lore raw

Re: [PATCH v2 6/6] hwmon: (adt7470) Add thermal zone sensor support

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The background update thread holds a private mutex during a multi-second sleep, which will…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The background update thread holds a private mutex during a multi-second sleep, which will severely stall the thermal subsystem polling. --

--- Patch [6]: [PATCH v2 6/6] hwmon: (adt7470) Add thermal zone sensor support --- commit c1655de2721e60975758432b0053b21f699a5803
Author: Luiz Angelo Daros de Luca <[email protected]>

hwmon: (adt7470) Add thermal zone sensor support

This commit exposes the ADT7470 external temperature sensors to the thermal framework using the HWMON_C_REGISTER_TZ flag.

drivers/hwmon/adt7470.c static const struct hwmon_ops adt7470_hwmon_ops = { · line 1255

Quoted 10 lines
drivers/hwmon/adt7470.c +2 -0
index aaae14a421cd8..595a8f934d60a 100644
@@ -1251,6 +1251,8 @@ static const struct hwmon_ops adt7470_hwmon_ops = {
 };
 
 static const struct hwmon_channel_info * const adt7470_info[] = {
+	HWMON_CHANNEL_INFO(chip,
+			   HWMON_C_REGISTER_TZ),

[Severity: High]
Could exposing this sensor to the thermal framework cause a regression by stalling the thermal workqueue?

The thermal subsystem will automatically poll this sensor via adt7470_read(), which acquires data->lock.

Concurrently, the driver's background kthread adt7470_update_thread() holds this exact lock while executing msleep_interruptible() for up to 2 seconds in adt7470_read_temperatures():

drivers/hwmon/adt7470.c:adt7470_update_thread() {
    ...
    mutex_lock(&data->lock);
    adt7470_read_temperatures(data);
    mutex_unlock(&data->lock);
    ...
}
drivers/hwmon/adt7470.c:adt7470_read_temperatures() {
    ...
    res = msleep_interruptible((data->num_temp_sensors >= 0 ?
                                data->num_temp_sensors * 200 :
                                TEMP_COLLECTION_TIME));
    ...
}

Will the thermal workqueue block for up to 2 seconds when trying to read the temperature, risking a stall in thermal management for other zones sharing the same workqueue?

Quoted 2 lines
drivers/hwmon/adt7470.c +0 -0
 	HWMON_CHANNEL_INFO(temp,
 			   HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM,

[ ... ]

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