Add StarFive SAR-ADC driver
Subsystems (2)
cover Add StarFive SAR-ADC driver
Hi all,
This series adds an IIO ADC driver for the controller of StarFive Successive Approximation Register A/D converter (SAR-ADC).
The StarFive SAR-ADC is a 12-bit converter with up to 8 input channels and a fixed 1.8V reference domain. The driver provides raw and processed voltage readouts via IIO, runtime PM support, and threshold-based voltage monitoring.
Tested on StarFive JHB100 EVB with all ADC channels and monitor interrupt path.
Xingyu Wu (2):
bindings: iio: adc: Add StarFive JHB100 SARADC
iio: adc: Add StarFive SAR-ADC driver
.../iio/adc/starfive,jhb100-saradc.yaml | 62 ++ MAINTAINERS | 6 + drivers/iio/adc/Kconfig | 11 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/starfive-saradc.c | 978 ++++++++++++++++++ 5 files changed, 1058 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml create mode 100644 drivers/iio/adc/starfive-saradc.c
Signature · 1 line
2.34.1
PATCH 1/2 bindings: iio: adc: Add StarFive JHB100 SARADC
Add the new documentation of SAR-ADC for the StarFive JHB100 SoC.
--- .../iio/adc/starfive,jhb100-saradc.yaml | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml new file +62 -0
@@ -0,0 +1,62 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/adc/starfive,jhb100-saradc.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Successive Approximation Register (SAR) A/D converter for the StarFive JHB100 SoC + +maintainers: + - Xingyu Wu <[email protected]> + +properties: + compatible: + const: starfive,jhb100-saradc + + reg: + maxItem: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + + resets: + maxItems: 2 + + "#io-channel-cells": + const: 1 + + upper-bound-mv: + description: The upper bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + lower-bound-mv: + description: The lower bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + scan-freq: + description: Number of the scan cycle interval. + $ref: /schemas/types.yaml#/definitions/uint16 + +required: + - compatible + - reg + - interrupts + - clocks + - resets + - "#io-channel-cells" + +additionalProperties: false + +examples: + - | + adc@11be1400 { + compatible = "starfive,jhb100-saradc"; + reg = <0x11be1400 0x400>; + interrupts = <172>; + clocks = <&per0crg 18>; + resets = <&per0crg 11>, <&per0crg 46>; + #io-channel-cells = <1>; + };
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v1 1/2] bindings: iio: adc: Add StarFive JHB100 SARADC
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Low] Typo in the `reg` property schema keyword where `maxItem` is used instead of `maxItems`. - [Low]…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Low] Typo in the reg property schema keyword where maxItem is used instead of maxItems.
- [Low] Hardware-specific properties upper-bound-mv, lower-bound-mv, and scan-freq lack a vendor prefix.
- [Low] The resets property allows 2 items but does not document their order or meaning. --
commit f51adfa18df9b732393baa87f11dbef506bb4b5b
Author: Xingyu Wu <[email protected]>
bindings: iio: adc: Add StarFive JHB100 SARADC
This commit adds a new device tree binding document for the Successive Approximation Register A/D converter found on the StarFive JHB100 SoC.
Quoted 6 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml new file +0
-0@@ -0,0 +1,62 @@
[ ... ]
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml line 17
Quoted 6 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml new file +6
-0+properties: + compatible: + const: starfive,jhb100-saradc + + reg: + maxItem: 1
This isn't a bug, but should this be maxItems instead of maxItem? The dt-schema validator might silently ignore the unrecognized keyword, allowing nodes with multiple reg entries to pass validation.
[ ... ]
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml line 26
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml new file +5
-0+ clocks: + maxItems: 1 + + resets: + maxItems: 2
When allowing multiple reset lines, should the binding define their expected order and meaning? Usually, this is done by providing an items list with descriptions for each entry, or by defining a corresponding reset-names property.
[ ... ]
Quoted 4 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml new file +4
-0+ "#io-channel-cells": + const: 1 + + upper-bound-mv:
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml line 41
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml new file +10
-0+ description: The upper bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + lower-bound-mv: + description: The lower bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + scan-freq: + description: Number of the scan cycle interval. + $ref: /schemas/types.yaml#/definitions/uint16
Since these appear to be custom, hardware-specific properties, should they be prefixed with the vendor string (e.g., starfive,upper-bound-mv) to prevent future namespace collisions with generic properties?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
RE: [PATCH v1 1/2] bindings: iio: adc: Add StarFive JHB100 SARADC
Quoted 31 lines · Sashiko
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Low] Typo in theregproperty schema keyword wheremaxItemis used instead ofmaxItems.
- [Low] Hardware-specific propertiesupper-bound-mv,lower-bound-mv, andscan-freqlack a vendor prefix.
- [Low] Theresetsproperty allows 2 items but does not document their order or meaning.
--commit
f51adfa18df9b732393baa87f11dbef506bb4b5b
Author: Xingyu Wu <[email protected]>bindings: iio: adc: Add StarFive JHB100 SARADC
This commit adds a new device tree binding document for the Successive Approximation Register A/D converter found on the StarFive JHB100 SoC.
Quoted 11 lines
diff --git a/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yam l b/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yam l
new file mode 100644
index 0000000000000..ba8e19b72ad71Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc new file +1
-0+++ .yaml@@ -0,0 +1,62 @@[ ... ]
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc
Quoted 6 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc new file +6
-0+properties: + compatible: + const: starfive,jhb100-saradc + + reg: + maxItem: 1This isn't a bug, but should this be maxItems instead of maxItem? The dt-schema validator might silently ignore the unrecognized keyword, allowing nodes with multiple reg entries to pass validation.
Will fix.
Quoted 11 lines
[ ... ]
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc new file +5
-0+ clocks: + maxItems: 1 + + resets: + maxItems: 2When allowing multiple reset lines, should the binding define their expected order and meaning? Usually, this is done by providing an items list with descriptions for each entry, or by defining a corresponding reset-names property.
Noted.
Quoted 12 lines
[ ... ]
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc new file +10
-0+ "#io-channel-cells": + const: 1 + + upper-bound-mv: + description: The upper bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + lower-bound-mv: + description: The lower bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16
Quoted 8 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc
Quoted 4 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc new file +4
-0+ + scan-freq: + description: Number of the scan cycle interval. + $ref: /schemas/types.yaml#/definitions/uint16Since these appear to be custom, hardware-specific properties, should they be prefixed with the vendor string (e.g., starfive,upper-bound-mv) to prevent future namespace collisions with generic properties?
Noted.
Thanks,
Xingyu Wu
Rob Herring (Arm)
maintainerRe: [PATCH v1 1/2] bindings: iio: adc: Add StarFive JHB100 SARADC
In reply to Xingyu WuQuoted 8 lines · Xingyu Wu
Add the new documentation of SAR-ADC for the StarFive JHB100 SoC.
Signed-off-by Xingyu Wu <[email protected]>--- .../iio/adc/starfive,jhb100-saradc.yaml | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml
My bot found errors running 'make dt_binding_check' on your patch:
yamllint warnings/errors:
dtschema/dtc warnings/errors:
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml: properties:reg: 'anyOf' conditional failed, one must be fixed:
'maxItem' is not one of ['$ref', 'additionalItems', 'additionalProperties', 'allOf', 'anyOf', 'const', 'contains', 'default', 'dependencies', 'dependentRequired', 'dependentSchemas', 'deprecated', 'description', 'else', 'enum', 'exclusiveMaximum', 'exclusiveMinimum', 'items', 'if', 'minItems', 'minimum', 'maxItems', 'maximum', 'multipleOf', 'not', 'oneOf', 'pattern', 'patternProperties', 'properties', 'required', 'then', 'typeSize', 'unevaluatedProperties', 'uniqueItems'] 'type' was expected
from schema $id: http://devicetree.org/meta-schemas/keywords.yaml
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml: properties:reg: 'anyOf' conditional failed, one must be fixed:
'maxItems' is a required property
hint: Only "maxItems" is required for a single entry if there are no constraints defined for the values.
'maxItem' is not one of ['maxItems', 'description', 'deprecated']
hint: Only "maxItems" is required for a single entry if there are no constraints defined for the values.
Additional properties are not allowed ('maxItem' was unexpected)
hint: Arrays must be described with a combination of minItems/maxItems/items
'maxItem' is not one of ['description', 'deprecated', 'const', 'enum', 'minimum', 'maximum', 'multipleOf', 'default', '$ref', 'oneOf']
hint: cell array properties must define how many entries and what the entries are when there is more than one entry.
from schema $id: http://devicetree.org/meta-schemas/cell.yaml
doc reference errors (make refcheckdocs):
See https://patchwork.kernel.org/project/devicetree/patch/[email protected]
The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.
If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:
pip3 install dtschema --upgrade
Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.
Re: [PATCH v1 1/2] bindings: iio: adc: Add StarFive JHB100 SARADC
In reply to Xingyu WuQuoted 45 lines · Xingyu Wu
Add the new documentation of SAR-ADC for the StarFive JHB100 SoC.
Signed-off-by Xingyu Wu <[email protected]>--- .../iio/adc/starfive,jhb100-saradc.yaml | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yamlDocumentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml new file +31
-0@@ -0,0 +1,62 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/adc/starfive,jhb100-saradc.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Successive Approximation Register (SAR) A/D converter for the StarFive JHB100 SoC + +maintainers: + - Xingyu Wu <[email protected]> + +properties: + compatible: + const: starfive,jhb100-saradc + + reg: + maxItem: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + + resets: + maxItems: 2 + + "#io-channel-cells": + const: 1 + + upper-bound-mv:
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml line 41
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml new file +10
-0+ description: The upper bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + lower-bound-mv: + description: The lower bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + scan-freq: + description: Number of the scan cycle interval. + $ref: /schemas/types.yaml#/definitions/uint16
Can you explain why any of these three properties are something that should be in the devicetree rather than software controlled?
How are the bounds calculated?
Given the warnings etc,
pw-bot: changes-requested
Cheers,
Conor.
Quoted 25 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml new file +21
-0+ +required: + - compatible + - reg + - interrupts + - clocks + - resets + - "#io-channel-cells" + +additionalProperties: false + +examples: + - | + adc@11be1400 { + compatible = "starfive,jhb100-saradc"; + reg = <0x11be1400 0x400>; + interrupts = <172>; + clocks = <&per0crg 18>; + resets = <&per0crg 11>, <&per0crg 46>; + #io-channel-cells = <1>; + };Signature · 1 line
2.34.1
RE: [PATCH v1 1/2] bindings: iio: adc: Add StarFive JHB100 SARADC
Quoted 59 lines · Conor Dooley
Quoted 57 lines · Xingyu Wu
Add the new documentation of SAR-ADC for the StarFive JHB100 SoC.
Signed-off-by Xingyu Wu <[email protected]>--- .../iio/adc/starfive,jhb100-saradc.yaml | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yamldiff --git a/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yam l b/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yam l
new file mode 100644
index 000000000000..ba8e19b72ad7Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc new file +38
-0+++ .yaml@@ -0,0 +1,62 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) %YAML 1.2 +--- +$id: +http://devicetree.org/schemas/iio/adc/starfive,jhb100-saradc.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Successive Approximation Register (SAR) A/D converter for the +StarFive JHB100 SoC + +maintainers: + - Xingyu Wu <[email protected]> + +properties: + compatible: + const: starfive,jhb100-saradc + + reg: + maxItem: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + + resets: + maxItems: 2 + + "#io-channel-cells": + const: 1 + + upper-bound-mv: + description: The upper bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + lower-bound-mv: + description: The lower bound voltage value of the monitor.
Quoted 8 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc line 42
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc new file +5
-0+ $ref: /schemas/types.yaml#/definitions/uint16 + + scan-freq: + description: Number of the scan cycle interval. + $ref: /schemas/types.yaml#/definitions/uint16Can you explain why any of these three properties are something that should be in the devicetree rather than software controlled?
My intention is to be able to obtain the initial values from the devicetree during probe and preset them. Do I need to drop them and just set them through sysfs?
Quoted 2 lines
How are the bounds calculated?
The measurement range of this ADC hardware is from 0 to 1800 mV. This set value cannot exceed it. This explanation will be added later.
Best regards,
Xingyu Wu
Re: [PATCH v1 1/2] bindings: iio: adc: Add StarFive JHB100 SARADC
Quoted 69 lines · Xingyu Wu
Quoted 59 lines · Conor Dooley
Quoted 57 lines · Xingyu Wu
Add the new documentation of SAR-ADC for the StarFive JHB100 SoC.
Signed-off-by Xingyu Wu <[email protected]>--- .../iio/adc/starfive,jhb100-saradc.yaml | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yamldiff --git a/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yam l b/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yam l
new file mode 100644
index 000000000000..ba8e19b72ad7Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc new file +38
-0+++ .yaml@@ -0,0 +1,62 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) %YAML 1.2 +--- +$id: +http://devicetree.org/schemas/iio/adc/starfive,jhb100-saradc.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Successive Approximation Register (SAR) A/D converter for the +StarFive JHB100 SoC + +maintainers: + - Xingyu Wu <[email protected]> + +properties: + compatible: + const: starfive,jhb100-saradc + + reg: + maxItem: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + + resets: + maxItems: 2 + + "#io-channel-cells": + const: 1 + + upper-bound-mv: + description: The upper bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + lower-bound-mv: + description: The lower bound voltage value of the monitor.Quoted 8 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc line 42
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc new file +5
-0+ $ref: /schemas/types.yaml#/definitions/uint16 + + scan-freq: + description: Number of the scan cycle interval. + $ref: /schemas/types.yaml#/definitions/uint16Can you explain why any of these three properties are something that should be in the devicetree rather than software controlled?
Quoted 2 lines
My intention is to be able to obtain the initial values from the devicetree during probe and preset them. Do I need to drop them and just set them through sysfs?
Unless the hardware configuration determines the values (which I can't really see being the case for scan-freq at least) then yes, you need to drop and set them via sysfs.
Quoted 3 lines
Quoted 1 line
How are the bounds calculated?
The measurement range of this ADC hardware is from 0 to 1800 mV. This set value cannot exceed it. This explanation will be added later.
I'm asking how this is calculated so that I can tell if you the property is permitted or not.
Cheers,
Conor.
RE: [PATCH v1 1/2] bindings: iio: adc: Add StarFive JHB100 SARADC
Quoted 76 lines · Conor Dooley
Quoted 74 lines · Xingyu Wu
Quoted 72 lines · Conor Dooley
Quoted 57 lines · Xingyu Wu
Add the new documentation of SAR-ADC for the StarFive JHB100 SoC.
Signed-off-by Xingyu Wu <[email protected]>--- .../iio/adc/starfive,jhb100-saradc.yaml | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yamldiff --git a/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc .yam
l b/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc .yam
l
new file mode 100644
index 000000000000..ba8e19b72ad7Documentation/devicetree/bindings/iio/adc/starfive,jhb100-sa new file +35
-0+++ radc +++ .yaml@@ -0,0 +1,62 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) %YAML 1.2 +--- +$id: +http://devicetree.org/schemas/iio/adc/starfive,jhb100-saradc.yaml +# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Successive Approximation Register (SAR) A/D converter for +the StarFive JHB100 SoC + +maintainers: + - Xingyu Wu <[email protected]> + +properties: + compatible: + const: starfive,jhb100-saradc + + reg: + maxItem: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + + resets: + maxItems: 2 + + "#io-channel-cells": + const: 1 + + upper-bound-mv:Documentation/devicetree/bindings/iio/adc/starfive,jhb100-sa line 43
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-sa new file +10
-0+ description: The upper bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + lower-bound-mv: + description: The lower bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + scan-freq: + description: Number of the scan cycle interval. + $ref: /schemas/types.yaml#/definitions/uint16Can you explain why any of these three properties are something that should be in the devicetree rather than software controlled?
Quoted 7 lines
Quoted 1 line
My intention is to be able to obtain the initial values from the devicetree during
probe and preset them.
Quoted 1 line
Do I need to drop them and just set them through sysfs?
Unless the hardware configuration determines the values (which I can't really see being the case for scan-freq at least) then yes, you need to drop and set them via sysfs.
The ADC hardware can be set the scan-freq register to determine how frequent it should scan its inputs. The calculation is:
frequency = 100/((register value) + 5) MHz, The register value should >= 15.
The maximum allowable scan frequency is 5MHz.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
How are the bounds calculated?
The measurement range of this ADC hardware is from 0 to 1800 mV. This set
value cannot exceed it. This explanation will be added later.
I'm asking how this is calculated so that I can tell if you the property is permitted or not.
The calculation of bound is:
bound-mv = 1800mv * (register value) / 0xFFF
Best regards,
Xingyu Wu
Re: [PATCH v1 1/2] bindings: iio: adc: Add StarFive JHB100 SARADC
Quoted 93 lines · Xingyu Wu
Quoted 76 lines · Conor Dooley
Quoted 74 lines · Xingyu Wu
Quoted 72 lines · Conor Dooley
Quoted 57 lines · Xingyu Wu
Add the new documentation of SAR-ADC for the StarFive JHB100 SoC.
Signed-off-by Xingyu Wu <[email protected]>--- .../iio/adc/starfive,jhb100-saradc.yaml | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yamldiff --git a/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc .yam
l b/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc .yam
l
new file mode 100644
index 000000000000..ba8e19b72ad7Documentation/devicetree/bindings/iio/adc/starfive,jhb100-sa new file +35
-0+++ radc +++ .yaml@@ -0,0 +1,62 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) %YAML 1.2 +--- +$id: +http://devicetree.org/schemas/iio/adc/starfive,jhb100-saradc.yaml +# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Successive Approximation Register (SAR) A/D converter for +the StarFive JHB100 SoC + +maintainers: + - Xingyu Wu <[email protected]> + +properties: + compatible: + const: starfive,jhb100-saradc + + reg: + maxItem: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + + resets: + maxItems: 2 + + "#io-channel-cells": + const: 1 + + upper-bound-mv:Documentation/devicetree/bindings/iio/adc/starfive,jhb100-sa line 43
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-sa new file +10
-0+ description: The upper bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + lower-bound-mv: + description: The lower bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + scan-freq: + description: Number of the scan cycle interval. + $ref: /schemas/types.yaml#/definitions/uint16Can you explain why any of these three properties are something that should be in the devicetree rather than software controlled?
Quoted 7 lines
Quoted 1 line
My intention is to be able to obtain the initial values from the devicetree during
probe and preset them.
Quoted 1 line
Do I need to drop them and just set them through sysfs?
Unless the hardware configuration determines the values (which I can't really see being the case for scan-freq at least) then yes, you need to drop and set them via sysfs.
The ADC hardware can be set the scan-freq register to determine how frequent it should scan its inputs. The calculation is:
frequency = 100/((register value) + 5) MHz, The register value should >= 15.
The maximum allowable scan frequency is 5MHz.Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
How are the bounds calculated?
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
The measurement range of this ADC hardware is from 0 to 1800 mV. This set
value cannot exceed it. This explanation will be added later.
I'm asking how this is calculated so that I can tell if you the property is permitted or not.
The calculation of bound is:
bound-mv = 1800mv * (register value) /0xFFF
These are the formulas, but how does someone know what the value for bound-mv needs to be? Why would someone not just want to always use 1800mv?
Quoted 4 lines
Best regards,
Xingyu Wu
RE: [PATCH v1 1/2] bindings: iio: adc: Add StarFive JHB100 SARADC
Quoted 106 lines · Conor Dooley
Quoted 84 lines · Xingyu Wu
Quoted 83 lines · Conor Dooley
Quoted 81 lines · Xingyu Wu
Quoted 69 lines · Conor Dooley
Quoted 67 lines · Xingyu Wu
Add the new documentation of SAR-ADC for the StarFive JHB100 SoC.
Signed-off-by Xingyu Wu <[email protected]>--- .../iio/adc/starfive,jhb100-saradc.yaml | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.y
amldiff --git a/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-sa radc
.yam
l b/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-sa radc
.yam
l
new file mode 100644
index 000000000000..ba8e19b72ad7Documentation/devicetree/bindings/iio/adc/starfive,jhb10 new file +43
-0+++ 0-sa +++ radc +++ .yaml@@ -0,0 +1,62 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) %YAML +1.2 +--- +$id: +http://devicetree.org/schemas/iio/adc/starfive,jhb100-saradc. +yaml +# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Successive Approximation Register (SAR) A/D converter +for the StarFive JHB100 SoC + +maintainers: + - Xingyu Wu <[email protected]> + +properties: + compatible: + const: starfive,jhb100-saradc + + reg: + maxItem: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + + resets: + maxItems: 2 + + "#io-channel-cells": + const: 1 + + upper-bound-mv: + description: The upper bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + lower-bound-mv: + description: The lower bound voltage value of the monitor.Quoted 8 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb10 line 45
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb10 new file +5
-0+ $ref: /schemas/types.yaml#/definitions/uint16 + + scan-freq: + description: Number of the scan cycle interval. + $ref: /schemas/types.yaml#/definitions/uint16Can you explain why any of these three properties are something that should be in the devicetree rather than software controlled?
My intention is to be able to obtain the initial values from the devicetree during
Quoted 8 lines
Quoted 6 lines
probe and preset them.
Quoted 1 line
Do I need to drop them and just set them through sysfs?
Unless the hardware configuration determines the values (which I can't really see being the case for scan-freq at least) then yes, you need to drop and set them via sysfs.
The ADC hardware can be set the scan-freq register to determine how frequent it
should scan its inputs.
Quoted 11 lines
The calculation is:
frequency = 100/((register value) + 5) MHz, The register value should >= 15.
The maximum allowable scan frequency is 5MHz.Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
How are the bounds calculated?
The measurement range of this ADC hardware is from 0 to 1800 mV. This set
value cannot exceed it. This explanation will be added later.
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
I'm asking how this is calculated so that I can tell if you the property is permitted or not.
The calculation of bound is:
bound-mv = 1800mv * (register value) /0xFFFThese are the formulas, but how does someone know what the value for bound- mv needs to be? Why would someone not just want to always use 1800mv?
Can I add the 'maximum' and ' minimum' to provide clarification? And the driver will also check.
Best regards,
Xingyu Wu
Re: [PATCH v1 1/2] bindings: iio: adc: Add StarFive JHB100 SARADC
Quoted 110 lines · Xingyu Wu
Quoted 109 lines · Conor Dooley
Quoted 84 lines · Xingyu Wu
Quoted 83 lines · Conor Dooley
Quoted 81 lines · Xingyu Wu
Quoted 69 lines · Conor Dooley
Quoted 67 lines · Xingyu Wu
Add the new documentation of SAR-ADC for the StarFive JHB100 SoC.
Signed-off-by Xingyu Wu <[email protected]>--- .../iio/adc/starfive,jhb100-saradc.yaml | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.y
amldiff --git a/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-sa radc
.yam
l b/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-sa radc
.yam
l
new file mode 100644
index 000000000000..ba8e19b72ad7Documentation/devicetree/bindings/iio/adc/starfive,jhb10 new file +43
-0+++ 0-sa +++ radc +++ .yaml@@ -0,0 +1,62 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) %YAML +1.2 +--- +$id: +http://devicetree.org/schemas/iio/adc/starfive,jhb100-saradc. +yaml +# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Successive Approximation Register (SAR) A/D converter +for the StarFive JHB100 SoC + +maintainers: + - Xingyu Wu <[email protected]> + +properties: + compatible: + const: starfive,jhb100-saradc + + reg: + maxItem: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + + resets: + maxItems: 2 + + "#io-channel-cells": + const: 1 + + upper-bound-mv: + description: The upper bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + lower-bound-mv: + description: The lower bound voltage value of the monitor.Quoted 8 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb10 line 45
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb10 new file +5
-0+ $ref: /schemas/types.yaml#/definitions/uint16 + + scan-freq: + description: Number of the scan cycle interval. + $ref: /schemas/types.yaml#/definitions/uint16Can you explain why any of these three properties are something that should be in the devicetree rather than software controlled?
My intention is to be able to obtain the initial values from the devicetree during
Quoted 8 lines
Quoted 6 lines
probe and preset them.
Quoted 1 line
Do I need to drop them and just set them through sysfs?
Unless the hardware configuration determines the values (which I can't really see being the case for scan-freq at least) then yes, you need to drop and set them via sysfs.
The ADC hardware can be set the scan-freq register to determine how frequent it
should scan its inputs.
Quoted 14 lines
The calculation is:
frequency = 100/((register value) + 5) MHz, The register value should >= 15.
The maximum allowable scan frequency is 5MHz.Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
How are the bounds calculated?
The measurement range of this ADC hardware is from 0 to 1800 mV. This set
value cannot exceed it. This explanation will be added later.
I'm asking how this is calculated so that I can tell if you the property is permitted or not.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
The calculation of bound is:
bound-mv = 1800mv * (register value) /0xFFFThese are the formulas, but how does someone know what the value for bound- mv needs to be? Why would someone not just want to always use 1800mv?
Can I add the 'maximum' and ' minimum' to provide clarification? And the driver will also check.
All that does is repeat the 1800 mV though, what I am interested in is how someone determines if they should use 1600 mV or 200 mV etc. What aspect of the hardware do the bounds depend on?
RE: [PATCH v1 1/2] bindings: iio: adc: Add StarFive JHB100 SARADC
Quoted 140 lines
-----Original Message-----
From: Conor Dooley <[email protected]>
Sent: 2026年5月21日 18:21
To: Xingyu Wu <[email protected]>
Cc: Jonathan Cameron <[email protected]>; David Lechner <[email protected]>; Nuno Sá <[email protected]>; Andy Shevchenko <[email protected]>; Rob Herring <[email protected]>; Krzysztof Kozlowski <[email protected]>; Conor Dooley <[email protected]>; [email protected]; [email protected]; linux- [email protected]
Subject: Re: [PATCH v1 1/2] bindings: iio: adc: Add StarFive JHB100 SARADCQuoted 81 lines · Xingyu Wu
Quoted 80 lines · Conor Dooley
Quoted 78 lines · Xingyu Wu
Quoted 77 lines · Conor Dooley
Quoted 75 lines · Xingyu Wu
Quoted 74 lines · Conor Dooley
Quoted 72 lines · Xingyu Wu
Add the new documentation of SAR-ADC for the StarFive JHB100 SoC.
Signed-off-by Xingyu Wu <[email protected]>--- .../iio/adc/starfive,jhb100-saradc.yaml | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-sara
dc.y
amldiff --git a/Documentation/devicetree/bindings/iio/adc/starfive,jhb10 0-sa
radc
.yam
l b/Documentation/devicetree/bindings/iio/adc/starfive,jhb10 0-sa
radc
.yam
l
new file mode 100644
index 000000000000..ba8e19b72ad7Documentation/devicetree/bindings/iio/adc/starfive,j new file +45
-0+++ hb10 +++ 0-sa +++ radc +++ .yaml@@ -0,0 +1,62 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML +1.2 +--- +$id: +http://devicetree.org/schemas/iio/adc/starfive,jhb100-saradc. +yaml +# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Successive Approximation Register (SAR) A/D +converter for the StarFive JHB100 SoC + +maintainers: + - Xingyu Wu <[email protected]> + +properties: + compatible: + const: starfive,jhb100-saradc + + reg: + maxItem: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + + resets: + maxItems: 2 + + "#io-channel-cells": + const: 1 + + upper-bound-mv: + description: The upper bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + lower-bound-mv: + description: The lower bound voltage value of the monitor.Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Documentation/devicetree/bindings/iio/adc/starfive,j line 46
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/starfive,j new file +5
-0+ $ref: /schemas/types.yaml#/definitions/uint16 + + scan-freq: + description: Number of the scan cycle interval. + $ref: /schemas/types.yaml#/definitions/uint16Can you explain why any of these three properties are something that should be in the devicetree rather than software
controlled?
Quoted 24 lines
Quoted 24 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
My intention is to be able to obtain the initial values from the devicetree during
probe and preset them.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Do I need to drop them and just set them through sysfs?
Unless the hardware configuration determines the values (which I can't really see being the case for scan-freq at least) then yes, you need to drop and set them via sysfs.
The ADC hardware can be set the scan-freq register to determine how frequent it
should scan its inputs.
Quoted 11 lines
The calculation is:
frequency = 100/((register value) + 5) MHz, The register value should >= 15.
The maximum allowable scan frequency is 5MHz.Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
How are the bounds calculated?
The measurement range of this ADC hardware is from 0 to 1800 mV. This set
value cannot exceed it. This explanation will be added later.
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
I'm asking how this is calculated so that I can tell if you the property is permitted or not.
The calculation of bound is:
bound-mv = 1800mv * (register value) /0xFFFThese are the formulas, but how does someone know what the value for bound- mv needs to be? Why would someone not just want to always use
1800mv?
Quoted 2 lines
Quoted 1 line
Quoted 6 lines
Quoted 1 line
Can I add the 'maximum' and ' minimum' to provide clarification? And the driver
will also check.
All that does is repeat the 1800 mV though, what I am interested in is how someone determines if they should use 1600 mV or 200 mV etc. What aspect of the hardware do the bounds depend on?
These bounds are just for the monitor mode. If the input voltage is outside this range, interrupt will be triggered to report the user space. The bounds value (1600mv or 200mv) are determined by the users' applications. Users can freely set them according to the range they want to monitor.
Best regards,
Xingyu Wu
Jonathan Cameron
maintainerRe: [PATCH v1 1/2] bindings: iio: adc: Add StarFive JHB100 SARADC
In reply to Conor DooleyQuoted 118 lines · On Thu, 21 May 2026 11:20:52 +0100 Conor Dooley <[email protected]> wrote:
Quoted 117 lines · Xingyu Wu
Quoted 115 lines · Conor Dooley
Quoted 84 lines · Xingyu Wu
Quoted 83 lines · Conor Dooley
Quoted 81 lines · Xingyu Wu
Quoted 69 lines · Conor Dooley
Quoted 67 lines · Xingyu Wu
Add the new documentation of SAR-ADC for the StarFive JHB100 SoC.
Signed-off-by Xingyu Wu <[email protected]>--- .../iio/adc/starfive,jhb100-saradc.yaml | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.y
amldiff --git a/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-sa radc
.yam
l b/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-sa radc
.yam
l
new file mode 100644
index 000000000000..ba8e19b72ad7Documentation/devicetree/bindings/iio/adc/starfive,jhb10 new file +43
-0+++ 0-sa +++ radc +++ .yaml@@ -0,0 +1,62 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) %YAML +1.2 +--- +$id: +http://devicetree.org/schemas/iio/adc/starfive,jhb100-saradc. +yaml +# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Successive Approximation Register (SAR) A/D converter +for the StarFive JHB100 SoC + +maintainers: + - Xingyu Wu <[email protected]> + +properties: + compatible: + const: starfive,jhb100-saradc + + reg: + maxItem: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + + resets: + maxItems: 2 + + "#io-channel-cells": + const: 1 + + upper-bound-mv: + description: The upper bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + lower-bound-mv: + description: The lower bound voltage value of the monitor.Quoted 8 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb10 line 45
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb10 new file +5
-0+ $ref: /schemas/types.yaml#/definitions/uint16 + + scan-freq: + description: Number of the scan cycle interval. + $ref: /schemas/types.yaml#/definitions/uint16Can you explain why any of these three properties are something that should be in the devicetree rather than software controlled?
My intention is to be able to obtain the initial values from the devicetree during
Quoted 8 lines
Quoted 6 lines
probe and preset them.
Quoted 1 line
Do I need to drop them and just set them through sysfs?
Unless the hardware configuration determines the values (which I can't really see being the case for scan-freq at least) then yes, you need to drop and set them via sysfs.
The ADC hardware can be set the scan-freq register to determine how frequent it
should scan its inputs.
Quoted 9 lines
The calculation is:
frequency = 100/((register value) + 5) MHz, The register value should >= 15.
The maximum allowable scan frequency is 5MHz.Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
How are the bounds calculated?
The measurement range of this ADC hardware is from 0 to 1800 mV. This set
Quoted 7 lines
Quoted 4 lines
value cannot exceed it. This explanation will be added later.
I'm asking how this is calculated so that I can tell if you the property is permitted or not.
The calculation of bound is:
bound-mv = 1800mv * (register value) /0xFFFThese are the formulas, but how does someone know what the value for bound- mv needs to be? Why would someone not just want to always use 1800mv?
Quoted 5 lines
Quoted 1 line
Can I add the 'maximum' and ' minimum' to provide clarification? And the driver will also check.
All that does is repeat the 1800 mV though, what I am interested in is how someone determines if they should use 1600 mV or 200 mV etc. What aspect of the hardware do the bounds depend on?
There are two options here
1. This is critical stuff to avoid hardware damage. (If you are relying on Linux for that you built your system wrong but if we ignore that...) Then userspace control should not be possible - or at least should only be able to move boundaries in directions that make them tighter.
2. It is advisory only and not related to hardware damage - in that case generally doesn't belong in DT.
Jonathan
Re: [PATCH v1 1/2] bindings: iio: adc: Add StarFive JHB100 SARADC
Quoted 127 lines · Jonathan Cameron
Quoted 118 lines · On Thu, 21 May 2026 11:20:52 +0100 Conor Dooley <[email protected]> wrote:
Quoted 117 lines · Xingyu Wu
Quoted 115 lines · Conor Dooley
Quoted 84 lines · Xingyu Wu
Quoted 83 lines · Conor Dooley
Quoted 81 lines · Xingyu Wu
Quoted 69 lines · Conor Dooley
Quoted 67 lines · Xingyu Wu
Add the new documentation of SAR-ADC for the StarFive JHB100 SoC.
Signed-off-by Xingyu Wu <[email protected]>--- .../iio/adc/starfive,jhb100-saradc.yaml | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.y
amldiff --git a/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-sa radc
.yam
l b/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-sa radc
.yam
l
new file mode 100644
index 000000000000..ba8e19b72ad7Documentation/devicetree/bindings/iio/adc/starfive,jhb10 new file +43
-0+++ 0-sa +++ radc +++ .yaml@@ -0,0 +1,62 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) %YAML +1.2 +--- +$id: +http://devicetree.org/schemas/iio/adc/starfive,jhb100-saradc. +yaml +# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Successive Approximation Register (SAR) A/D converter +for the StarFive JHB100 SoC + +maintainers: + - Xingyu Wu <[email protected]> + +properties: + compatible: + const: starfive,jhb100-saradc + + reg: + maxItem: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + + resets: + maxItems: 2 + + "#io-channel-cells": + const: 1 + + upper-bound-mv: + description: The upper bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + lower-bound-mv: + description: The lower bound voltage value of the monitor.Quoted 8 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb10 line 45
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb10 new file +5
-0+ $ref: /schemas/types.yaml#/definitions/uint16 + + scan-freq: + description: Number of the scan cycle interval. + $ref: /schemas/types.yaml#/definitions/uint16Can you explain why any of these three properties are something that should be in the devicetree rather than software controlled?
My intention is to be able to obtain the initial values from the devicetree during
Quoted 8 lines
Quoted 6 lines
probe and preset them.
Quoted 1 line
Do I need to drop them and just set them through sysfs?
Unless the hardware configuration determines the values (which I can't really see being the case for scan-freq at least) then yes, you need to drop and set them via sysfs.
The ADC hardware can be set the scan-freq register to determine how frequent it
should scan its inputs.
Quoted 9 lines
The calculation is:
frequency = 100/((register value) + 5) MHz, The register value should >= 15.
The maximum allowable scan frequency is 5MHz.Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
How are the bounds calculated?
The measurement range of this ADC hardware is from 0 to 1800 mV. This set
Quoted 7 lines
Quoted 4 lines
value cannot exceed it. This explanation will be added later.
I'm asking how this is calculated so that I can tell if you the property is permitted or not.
The calculation of bound is:
bound-mv = 1800mv * (register value) /0xFFFThese are the formulas, but how does someone know what the value for bound- mv needs to be? Why would someone not just want to always use 1800mv?
Quoted 5 lines
Quoted 1 line
Can I add the 'maximum' and ' minimum' to provide clarification? And the driver will also check.
All that does is repeat the 1800 mV though, what I am interested in is how someone determines if they should use 1600 mV or 200 mV etc. What aspect of the hardware do the bounds depend on?
Quoted 7 lines
There are two options here
1. This is critical stuff to avoid hardware damage. (If you are relying on Linux for that you built your system wrong but if we ignore that...) Then userspace control should not be possible - or at least should only be able to move boundaries in directions that make them tighter.
2. It is advisory only and not related to hardware damage - in that case generally doesn't belong in DT.
Per Xingyu's latest response
| These bounds are just for the monitor mode. If the input voltage is outside this range, interrupt will be triggered to report the user space. | The bounds value (1600mv or 200mv) are determined by the users' applications. Users can freely set them according to the range they want to monitor. it controls the level at which interrupts occur, and is therefore not
suitable for DT.
Cheers,
Conor.
RE: [PATCH v1 1/2] bindings: iio: adc: Add StarFive JHB100 SARADC
Quoted 150 lines · Conor Dooley
Quoted 6 lines · Jonathan Cameron
Quoted 3 lines · On Thu, 21 May 2026 11:20:52 +0100 Conor Dooley <[email protected]> wrote:
Quoted 2 lines · Xingyu Wu
Quoted 1 line · Conor Dooley
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines · Xingyu Wu
Quoted 7 lines · Conor Dooley
Quoted 6 lines · Xingyu Wu
Quoted 4 lines · Conor Dooley
Quoted 3 lines · Xingyu Wu
Quoted 1 line
Add the new documentation of SAR-ADC for the StarFive JHB100
SoC.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Signed-off-by Xingyu Wu <[email protected]>--- .../iio/adc/starfive,jhb100-saradc.yaml | 62+++++++++++++++++++
Quoted 68 lines
Quoted 68 lines
Quoted 68 lines
Quoted 68 lines
Quoted 68 lines
Quoted 68 lines
Quoted 68 lines
Quoted 68 lines
Quoted 68 lines
1 file changed, 62 insertions(+) create mode 100644
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-sara
dc.y
amldiff --git a/Documentation/devicetree/bindings/iio/adc/starfive,j hb100-sa
radc
.yam
l b/Documentation/devicetree/bindings/iio/adc/starfive,j hb100-sa
radc
.yam
l
new file mode 100644
index 000000000000..ba8e19b72ad7Documentation/devicetree/bindings/iio/adc/starfi new file +46
-0+++ ve,jhb10 +++ 0-sa +++ radc +++ .yaml@@ -0,0 +1,62 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML +1.2 +--- +$id: +http://devicetree.org/schemas/iio/adc/starfive,jhb100-saradc. +yaml +# +$schema: +http://devicetree.org/meta-schemas/core.yaml# + +title: Successive Approximation Register (SAR) A/D +converter for the StarFive JHB100 SoC + +maintainers: + - Xingyu Wu <[email protected]> + +properties: + compatible: + const: starfive,jhb100-saradc + + reg: + maxItem: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + + resets: + maxItems: 2 + + "#io-channel-cells": + const: 1 + + upper-bound-mv: + description: The upper bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + lower-bound-mv: + description: The lower bound voltage value of the monitor.Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Documentation/devicetree/bindings/iio/adc/starfi line 47
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/starfi new file +5
-0+ $ref: /schemas/types.yaml#/definitions/uint16 + + scan-freq: + description: Number of the scan cycle interval. + $ref: /schemas/types.yaml#/definitions/uint16Can you explain why any of these three properties are something that should be in the devicetree rather than software
controlled?
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 3 lines
My intention is to be able to obtain the initial values from the devicetree during
probe and preset them.
Quoted 1 line
Do I need to drop them and just set them through sysfs?
Unless the hardware configuration determines the values (which I can't really see being the case for scan-freq at least) then yes, you need to drop and set them via sysfs.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
The ADC hardware can be set the scan-freq register to determine how frequent it
should scan its inputs.
Quoted 2 lines
The calculation is:
frequency = 100/((register value) + 5) MHz, The register valueshould >= 15.
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
The maximum allowable scan frequency is 5MHz.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
How are the bounds calculated?
The measurement range of this ADC hardware is from 0 to 1800 mV. This set
value cannot exceed it. This explanation will be added later.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
I'm asking how this is calculated so that I can tell if you the property is permitted or not.
The calculation of bound is:
bound-mv = 1800mv * (register value) /0xFFFThese are the formulas, but how does someone know what the value for bound- mv needs to be? Why would someone not just want to always
use 1800mv?
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Can I add the 'maximum' and ' minimum' to provide clarification? And the
driver will also check.
Quoted 5 lines
Quoted 4 lines
All that does is repeat the 1800 mV though, what I am interested in is how someone determines if they should use 1600 mV or 200 mV etc. What aspect of the hardware do the bounds depend on?
Quoted 7 lines
There are two options here
1. This is critical stuff to avoid hardware damage. (If you are relying on Linux for that you built your system wrong but if we ignore that...) Then userspace control should not be possible - or at least should only be able to move boundaries in directions that make them tighter.
2. It is advisory only and not related to hardware damage - in that case generally doesn't belong in DT.
Quoted 7 lines
Per Xingyu's latest response
| These bounds are just for the monitor mode. If the input voltage is outside this range, interrupt will be triggered to report the user space.
| The bounds value (1600mv or 200mv) are determined by the users' applications. Users can freely set them according to the range they want to monitor. it controls the level at which interrupts occur, and is therefore not suitable for DT.
OK, I will drop them in DT and use IIO event to set them in driver by user.
Best regards,
Xingyu Wu
Jonathan Cameron
maintainerRe: [PATCH v1 1/2] bindings: iio: adc: Add StarFive JHB100 SARADC
In reply to Conor DooleyQuoted 77 lines · On Tue, 19 May 2026 10:59:59 +0100 Conor Dooley <[email protected]> wrote:
Quoted 69 lines · Xingyu Wu
Quoted 59 lines · Conor Dooley
Quoted 57 lines · Xingyu Wu
Add the new documentation of SAR-ADC for the StarFive JHB100 SoC.
Signed-off-by Xingyu Wu <[email protected]>--- .../iio/adc/starfive,jhb100-saradc.yaml | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yamldiff --git a/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yam l b/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yam l
new file mode 100644
index 000000000000..ba8e19b72ad7Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc new file +38
-0+++ .yaml@@ -0,0 +1,62 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) %YAML 1.2 +--- +$id: +http://devicetree.org/schemas/iio/adc/starfive,jhb100-saradc.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Successive Approximation Register (SAR) A/D converter for the +StarFive JHB100 SoC + +maintainers: + - Xingyu Wu <[email protected]> + +properties: + compatible: + const: starfive,jhb100-saradc + + reg: + maxItem: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + + resets: + maxItems: 2 + + "#io-channel-cells": + const: 1 + + upper-bound-mv: + description: The upper bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + lower-bound-mv: + description: The lower bound voltage value of the monitor.Quoted 8 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc line 42
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc new file +5
-0+ $ref: /schemas/types.yaml#/definitions/uint16 + + scan-freq: + description: Number of the scan cycle interval. + $ref: /schemas/types.yaml#/definitions/uint16Can you explain why any of these three properties are something that should be in the devicetree rather than software controlled?
Quoted 2 lines
My intention is to be able to obtain the initial values from the devicetree during probe and preset them. Do I need to drop them and just set them through sysfs?
Unless the hardware configuration determines the values (which I can't really see being the case for scan-freq at least) then yes, you need to drop and set them via sysfs.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
How are the bounds calculated?
The measurement range of this ADC hardware is from 0 to 1800 mV. This set value cannot exceed it. This explanation will be added later.
I'm asking how this is calculated so that I can tell if you the property is permitted or not.
Far as I can tell this stuff is boot script / udev rule stuff - doesn't belong in DT.
Jonathan
Quoted 3 lines
Cheers,
Conor.
Re: [PATCH v1 1/2] bindings: iio: adc: Add StarFive JHB100 SARADC
On Wed, May 20, 2026 at 12:11:09PM +0100, Jonathan Cameron wrote:
rom 0 to 1800 mV. This set value cannot exceed it. This explanation will be added later.
Quoted 6 lines
Quoted 3 lines
I'm asking how this is calculated so that I can tell if you the property is permitted or not.
Far as I can tell this stuff is boot script / udev rule stuff - doesn't belong in DT.
I have suspected this to be the case, but I have been giving the submitter the benefit of the doubt ;)
Re: [PATCH v1 1/2] bindings: iio: adc: Add StarFive JHB100 SARADC
In reply to Xingyu WuQuoted 3 lines · Xingyu Wu
Add the new documentation of SAR-ADC for the StarFive JHB100 SoC.
Signed-off-by Xingyu Wu <[email protected]>
Please use subject prefixes matching the subsystem. You can get them for example with git log --oneline -- DIRECTORY_OR_FILE on the directory your patch is touching. For bindings, the preferred subjects are explained here:
https://www.kernel.org/doc/html/latest/devicetree/bindings/submitting-patches.html#i-for-patch-submitters
Quoted 27 lines
--- .../iio/adc/starfive,jhb100-saradc.yaml | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yamlDocumentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml new file +16
-0@@ -0,0 +1,62 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/adc/starfive,jhb100-saradc.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Successive Approximation Register (SAR) A/D converter for the StarFive JHB100 SoC + +maintainers: + - Xingyu Wu <[email protected]> + +properties: + compatible: + const: starfive,jhb100-saradc + + reg:
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml line 26
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml new file +10
-0+ maxItem: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + + resets: + maxItems: 2
Need to list items. See writing bindings.
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml line 32
Quoted 6 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml new file +6
-0+ + "#io-channel-cells": + const: 1 + + upper-bound-mv: + description: The upper bound voltage value of the monitor.
Please read writing bindings about proper naming.
Quoted 20 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml new file +20
-0+ $ref: /schemas/types.yaml#/definitions/uint16 + + lower-bound-mv: + description: The lower bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + scan-freq: + description: Number of the scan cycle interval. + $ref: /schemas/types.yaml#/definitions/uint16 + +required: + - compatible + - reg + - interrupts + - clocks + - resets + - "#io-channel-cells" + +additionalProperties: false +
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml line 62
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml new file +10
-0+examples: + - | + adc@11be1400 { + compatible = "starfive,jhb100-saradc"; + reg = <0x11be1400 0x400>; + interrupts = <172>; + clocks = <&per0crg 18>; + resets = <&per0crg 11>, <&per0crg 46>; + #io-channel-cells = <1>; + };
Messed indentation.
Best regards,
Krzysztof
RE: [PATCH v1 1/2] bindings: iio: adc: Add StarFive JHB100 SARADC
Quoted 11 lines · Krzysztof Kozlowski
Quoted 3 lines · Xingyu Wu
Add the new documentation of SAR-ADC for the StarFive JHB100 SoC.
Signed-off-by Xingyu Wu <[email protected]>Please use subject prefixes matching the subsystem. You can get them for example with
git log --oneline -- DIRECTORY_OR_FILEon the directory your patch is touching. For bindings, the preferred subjects are explained here:
https://www.kernel.org/doc/html/latest/devicetree/bindings/submitting-
patches.html#i-for-patch-submitters
Noted.
Quoted 39 lines
Quoted 39 lines
--- .../iio/adc/starfive,jhb100-saradc.yaml | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yamldiff --git a/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yam l b/Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yam l
new file mode 100644
index 000000000000..ba8e19b72ad7Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc new file +22
-0+++ .yaml@@ -0,0 +1,62 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) %YAML 1.2 +--- +$id: +http://devicetree.org/schemas/iio/adc/starfive,jhb100-saradc.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Successive Approximation Register (SAR) A/D converter for the +StarFive JHB100 SoC + +maintainers: + - Xingyu Wu <[email protected]> + +properties: + compatible: + const: starfive,jhb100-saradc + + reg: + maxItem: 1 + + interrupts: + maxItems: 1
Quoted 8 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc line 27
Quoted 6 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc new file +6
-0+ + clocks: + maxItems: 1 + + resets: + maxItems: 2Need to list items. See writing bindings.
Will fix.
Quoted 9 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc line 33
Quoted 7 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc new file +6
-0+ + "#io-channel-cells": + const: 1 + + upper-bound-mv: + description: The upper bound voltage value of the monitor.Please read writing bindings about proper naming.
Noted.
Quoted 26 lines
Quoted 26 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc new file +24
-0+ $ref: /schemas/types.yaml#/definitions/uint16 + + lower-bound-mv: + description: The lower bound voltage value of the monitor. + $ref: /schemas/types.yaml#/definitions/uint16 + + scan-freq: + description: Number of the scan cycle interval. + $ref: /schemas/types.yaml#/definitions/uint16 + +required: + - compatible + - reg + - interrupts + - clocks + - resets + - "#io-channel-cells" + +additionalProperties: false + +examples: + - | + adc@11be1400 { + compatible = "starfive,jhb100-saradc";
Quoted 8 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc
Quoted 6 lines
Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc new file +6
-0+ reg = <0x11be1400 0x400>; + interrupts = <172>; + clocks = <&per0crg 18>; + resets = <&per0crg 11>, <&per0crg 46>; + #io-channel-cells = <1>; + };Messed indentation.
Will fix.
Best regards,
Xingyu Wu
PATCH 2/2 iio: adc: Add StarFive SAR-ADC driver
In reply to Xingyu WuAdd a new IIO ADC driver for the StarFive JHB100 SAR ADC controller.
The hardware provides 12-bit conversion precision and up to 8 input channels. This driver supports single-shot channel reads and exposes standard IIO interfaces for raw ADC values, processed voltages, and scale reporting. The driver also supports channel monitor mode with out-of-bound detection and scan frequency configuration.
--- MAINTAINERS | 6 + drivers/iio/adc/Kconfig | 11 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/starfive-saradc.c | 978 ++++++++++++++++++++++++++++++ 4 files changed, 996 insertions(+) create mode 100644 drivers/iio/adc/starfive-saradc.c
MAINTAINERS +6 -0
@@ -25275,6 +25275,12 @@ F: Documentation/devicetree/bindings/power/starfive* F: drivers/pmdomain/starfive/ F: include/dt-bindings/power/starfive,jh7110-pmu.h +STARFIVE JHB100 SARADC DRIVER +M: Xingyu Wu <[email protected]> +S: Supported +F: Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml +F: drivers/iio/adc/starfive-saradc.c + STARFIVE SOC DRIVERS M: Conor Dooley <[email protected]> S: Maintained
drivers/iio/adc/Kconfig +11 -0
@@ -1514,6 +1514,17 @@ config SD_ADC_MODULATOR This driver can also be built as a module. If so, the module will be called sd_adc_modulator. +config STARFIVE_SARADC + tristate "StarFive SARADC driver" + depends on ARCH_STARFIVE || COMPILE_TEST + depends on COMMON_CLK && RESET_CONTROLLER + help + Say yes here to build support for the SARADC found in SoCs from + StarFive. + + To compile this driver as a module, choose M here: the + module will be called starfive_saradc. + config STM32_ADC_CORE tristate "STMicroelectronics STM32 adc core" depends on ARCH_STM32 || COMPILE_TEST
drivers/iio/adc/Makefile +1 -0
@@ -133,6 +133,7 @@ obj-$(CONFIG_SC27XX_ADC) += sc27xx_adc.o obj-$(CONFIG_SD_ADC_MODULATOR) += sd_adc_modulator.o obj-$(CONFIG_SOPHGO_CV1800B_ADC) += sophgo-cv1800b-adc.o obj-$(CONFIG_SPEAR_ADC) += spear_adc.o +obj-$(CONFIG_STARFIVE_SARADC) += starfive-saradc.o obj-$(CONFIG_STM32_ADC_CORE) += stm32-adc-core.o obj-$(CONFIG_STM32_ADC) += stm32-adc.o obj-$(CONFIG_STM32_DFSDM_ADC) += stm32-dfsdm-adc.o
drivers/iio/adc/starfive-saradc.c new file +978 -0
978 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v1 2/2] iio: adc: Add StarFive SAR-ADC driver
Quoted 7 lines · Xingyu Wu
Add a new IIO ADC driver for the StarFive JHB100 SAR ADC controller.
The hardware provides 12-bit conversion precision and up to 8 input channels. This driver supports single-shot channel reads and exposes standard IIO interfaces for raw ADC values, processed voltages, and scale reporting. The driver also supports channel monitor mode with out-of-bound detection and scan frequency configuration.
Seems like this is just dust off code from ca 2020, as it uses sometimes quite old APIs (no driver uses in the past few years in IIO!). This needs much more work, please take your time to go via existing IIO drivers that were submitted and accepted in 2025/2026 and take them as examples (may be not the best, but good enough) and update your code accordingly. Note, reviewing others' patches may help a lot to get your knowledge up-to-date.
...
drivers/iio/adc/Kconfig config SD_ADC_MODULATOR · line 1526
Quoted 10 lines
drivers/iio/adc/Kconfig +10
-0+config STARFIVE_SARADC + tristate "StarFive SARADC driver" + depends on ARCH_STARFIVE || COMPILE_TEST + depends on COMMON_CLK && RESET_CONTROLLER + help + Say yes here to build support for the SARADC found in SoCs from + StarFive. + + To compile this driver as a module, choose M here: the + module will be called starfive_saradc.
Indentation issues.
...
Follow IWYU.
drivers/iio/adc/starfive-saradc.c line 19
Quoted 13 lines
drivers/iio/adc/starfive-saradc.c new file +12
-0+#include <linux/bitfield.h> +#include <linux/clk.h> +#include <linux/completion.h> +#include <linux/delay.h> +#include <linux/iio/iio.h> +#include <linux/iio/sysfs.h> +#include <linux/interrupt.h> +#include <linux/io.h> +#include <linux/iopoll.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/of.h>
No OF in a new code for the drivers like this one.
Quoted 3 lines
drivers/iio/adc/starfive-saradc.c new file +3
-0+#include <linux/platform_device.h> +#include <linux/pm_runtime.h> +#include <linux/reset.h>
...
drivers/iio/adc/starfive-saradc.c line 93
Quoted 8 lines
drivers/iio/adc/starfive-saradc.c new file +8
-0+#define SARADC_VDD_MV_TO_RAW (x) ({ \ + ((x) == 0) ? 0U : ({ \ + u32 _raw = \ + (u32)(((u64)(x) * 64000000ULL + \ + 4514610639ULL) / 30318750ULL); \ + _raw > 4095 ? 4095 : _raw; \ + }); \ + })
This is so ugly! Use your common sense and see tons of the examples around (the more recent driver is the average better the code is).
...
Quoted 1 line
+struct
starfive_saradc{
Use pahole to check the layout, also shuffle members to see what bloat-o-meter says about the resulting binary (for example, moving 'dev' up might give less code).
Quoted 15 lines
drivers/iio/adc/starfive-saradc.c new file +15
-0+void __iomem *base; +struct device *dev; +struct clk *clk; +struct reset_control *rst; +/* lock to protect against multiple access to the device */ +struct mutex lock; +int using_ch; +/* flag of interrupts by error or data done */ +bool err; +bool mon_working; +u8 mon_ch; +bool mon_en; +u32 up_bounds[SARADC_MAX_CHANNELS ]; +u32 low_bounds[SARADC_MAX_CHANNELS ]; +};
...
drivers/iio/adc/starfive-saradc.c line 176
Quoted 8 lines
drivers/iio/adc/starfive-saradc.c new file +7
-0+static inline void starfive_saradc_irq_clr_all (struct starfive_saradc *priv) +{ +unsigned int reg =readl (priv->base +SARADC_IRQ_EN_ST ); +int i; + +starfive_saradc_irq_clr (priv, reg); +for (i =0 ; i <SARADC_MAX_CHANNELS ; i++)
for (unsigned int i = 0; i < SARADC_MAX_CHANNELS; i++)
Quoted 2 lines
(quoted diff) +2
-0+ starfive_saradc_data_clr_ch(priv, i); +}
...
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+static void starfive_saradc_ch_dis_save (struct starfive_saradc *priv) +{ +unsigned int reg; +
drivers/iio/adc/starfive-saradc.c line 193
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+if (priv->mon_en) { +writel (ADC_IRQ_ST_MSK , priv->base +SARADC_IRQ_EN_ST ); + + reg =readl (priv->base +SARADC_CTRL ) & ~ADC_CHAN_EN_MSK ; +writel (reg, priv->base +SARADC_CTRL ); + priv->mon_working = false; + } + +starfive_saradc_irq_clr_all (priv); +msleep (20 );
So long sleeps must be explained (in the code comments with the reference to datasheet or citing it in case of non-public document)!
drivers/iio/adc/starfive-saradc.c line 205
Quoted 12 lines
drivers/iio/adc/starfive-saradc.c new file +12
-0+} + +static void starfive_saradc_ch_start (struct starfive_saradc *priv) +{ +int ch = priv->using_ch; +unsigned int reg =readl (priv->base +SARADC_CTRL ); + +/* Enable channel */ + reg = (reg & ~ADC_CHAN_EN_MSK ) |SARADC_CHAN_EN (ch); +writel (reg, priv->base +SARADC_CTRL ); + +msleep (20 );
Ditto!
Quoted 3 lines
drivers/iio/adc/starfive-saradc.c new file +3
-0+/* Enable conversion */ +writel (reg |ADC_EN_MSK , priv->base +SARADC_CTRL ); +}
...
drivers/iio/adc/starfive-saradc.c line 426
Quoted 8 lines
drivers/iio/adc/starfive-saradc.c new file +7
-0+static ssize_t starfive_saradc_monitor_channel_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct iio_dev *indio_dev =dev_to_iio_dev (dev); +struct starfive_saradc *priv =iio_priv (indio_dev); +return sprintf (buf,"%d\n" , priv->mon_ch);
Huh?! Is this driver taken from the ancient times and got just dust off?
Quoted 1 line
+}
...
drivers/iio/adc/starfive-saradc.c line 879
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+ irq =platform_get_irq (pdev,0 ); +if (irq <0 ) +return dev_err_probe (&pdev->dev, irq, +"failed to get irq\n" );
No, we don't do dup messages.
drivers/iio/adc/starfive-saradc.c line 888
Quoted 8 lines
drivers/iio/adc/starfive-saradc.c new file +8
-0+ ret =devm_request_threaded_irq (&pdev->dev, irq, + starfive_saradc_irq_handler, + starfive_saradc_mon_stop_threadfn, +IRQF_TRIGGER_HIGH |IRQF_ONESHOT , +dev_name (&pdev->dev), priv); +if (ret) +return dev_err_probe (&pdev->dev, ret, +"failed to request irq handler\n" );
Ditto.
...
Quoted 1 line
+
Unneeded blank line.
Quoted 1 line
+module_platform_driver(starfive_saradc_driver);
Signature · 2 lines
With Best Regards, Andy Shevchenko
RE: [PATCH v1 2/2] iio: adc: Add StarFive SAR-ADC driver
Quoted 10 lines · Andy Shevchenko
Quoted 7 lines · Xingyu Wu
Add a new IIO ADC driver for the StarFive JHB100 SAR ADC controller.
The hardware provides 12-bit conversion precision and up to 8 input channels. This driver supports single-shot channel reads and exposes standard IIO interfaces for raw ADC values, processed voltages, and scale reporting. The driver also supports channel monitor mode with out-of-bound detection and scan frequency configuration.
Quoted 6 lines
Seems like this is just dust off code from ca 2020, as it uses sometimes quite old APIs (no driver uses in the past few years in IIO!).
This needs much more work, please take your time to go via existing IIO drivers that were submitted and accepted in 2025/2026 and take them as examples (may be not the best, but good enough) and update your code accordingly. Note, reviewing others' patches may help a lot to get your knowledge up-to-date.
Noted. I will update the driver in next patch.
Quoted 15 lines
...
drivers/iio/adc/Kconfig config SD_ADC_MODULATOR · line 1526
Quoted 10 lines
drivers/iio/adc/Kconfig +10
-0+config STARFIVE_SARADC + tristate "StarFive SARADC driver" + depends on ARCH_STARFIVE || COMPILE_TEST + depends on COMMON_CLK && RESET_CONTROLLER + help + Say yes here to build support for the SARADC found in SoCs from + StarFive. + + To compile this driver as a module, choose M here: the + module will be called starfive_saradc.Indentation issues.
Noted.
Quoted 18 lines
...
Follow IWYU.
Quoted 11 lines
drivers/iio/adc/starfive-saradc.c new file +11
-0+#include <linux/bitfield.h> +#include <linux/clk.h> +#include <linux/completion.h> +#include <linux/delay.h> +#include <linux/iio/iio.h> +#include <linux/iio/sysfs.h> +#include <linux/interrupt.h> +#include <linux/io.h> +#include <linux/iopoll.h> +#include <linux/module.h> +#include <linux/mutex.h>
Quoted 3 lines
Quoted 1 line
+#include <
linux/of.h>No OF in a new code for the drivers like this one.
Will drop.
Quoted 14 lines
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +3
-0+#include <linux/platform_device.h> +#include <linux/pm_runtime.h> +#include <linux/reset.h> ...
drivers/iio/adc/starfive-saradc.c
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+#define SARADC_VDD_MV_TO_RAW (x) ({ \ + ((x) == 0) ? 0U : ({ \ + u32 _raw = \ + (u32)(((u64)(x) *64000000ULL + \
Review
Quoted 1 line
(quoted diff) +1
-0+ 4514610639ULL) /30318750ULL); \
Quoted 7 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ _raw > 4095 ? 4095 : _raw;\
Review
Quoted 2 lines
(quoted diff) +2
-0+ }); \ + })This is so ugly! Use your common sense and see tons of the examples around (the more recent driver is the average better the code is).
Will fix.
Quoted 8 lines
...
Quoted 1 line
+struct
starfive_saradc{Use
paholeto check the layout, also shuffle members to see whatbloat-o- metersays about the resulting binary (for example, moving 'dev' up might give less code).
Noted.
Quoted 44 lines
Quoted 16 lines
drivers/iio/adc/starfive-saradc.c new file +15
-0+void __iomem *base; +struct device *dev; +struct clk *clk; +struct reset_control *rst; +/* lock to protect against multiple access to the device */ +struct mutex lock; +int using_ch; +/* flag of interrupts by error or data done */ +bool err; +bool mon_working; +u8 mon_ch; +bool mon_en; +u32 up_bounds[SARADC_MAX_CHANNELS ]; +u32 low_bounds[SARADC_MAX_CHANNELS ]; +};...
drivers/iio/adc/starfive-saradc.c line 176
Quoted 8 lines
drivers/iio/adc/starfive-saradc.c new file +7
-0+static inline void starfive_saradc_irq_clr_all (struct starfive_saradc +*priv) { +unsigned int reg =readl (priv->base +SARADC_IRQ_EN_ST ); +int i; + +starfive_saradc_irq_clr (priv, reg); +for (i =0 ; i <SARADC_MAX_CHANNELS ; i++)for (unsigned int i = 0; i <
SARADC_MAX_CHANNELS; i++)Quoted 1 line
(quoted diff) +1
-0+ starfive_saradc_data_clr_ch(priv, i); }...
Quoted 9 lines
drivers/iio/adc/starfive-saradc.c new file +9
-0+static void starfive_saradc_ch_dis_save (struct starfive_saradc *priv) +{ +unsigned int reg; + +if (priv->mon_en) { +writel (ADC_IRQ_ST_MSK , priv->base +SARADC_IRQ_EN_ST ); + + reg =readl (priv->base +SARADC_CTRL ) & ~ADC_CHAN_EN_MSK ; +writel (reg, priv->base +SARADC_CTRL );
Quoted 8 lines
drivers/iio/adc/starfive-saradc.c line 193
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c new file +5
-0+ priv->mon_working = false; + } + +starfive_saradc_irq_clr_all (priv); +msleep (20 );So long sleeps must be explained (in the code comments with the reference to datasheet or citing it in case of non-public document)!
Explanations will be added.
Quoted 33 lines
drivers/iio/adc/starfive-saradc.c line 205
Quoted 12 lines
drivers/iio/adc/starfive-saradc.c new file +11
-0+} + +static void starfive_saradc_ch_start (struct starfive_saradc *priv) { +int ch = priv->using_ch; +unsigned int reg =readl (priv->base +SARADC_CTRL ); + +/* Enable channel */ + reg = (reg & ~ADC_CHAN_EN_MSK ) |SARADC_CHAN_EN (ch); +writel (reg, priv->base +SARADC_CTRL ); + +msleep (20 );Ditto!
Quoted 2 lines
(quoted diff) +2
-0+ /* Enable conversion */ + writel(reg | ADC_EN_MSK, priv->base + SARADC_CTRL); }...
drivers/iio/adc/starfive-saradc.c line 426
Quoted 8 lines
drivers/iio/adc/starfive-saradc.c new file +7
-0+static ssize_t starfive_saradc_monitor_channel_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct iio_dev *indio_dev =dev_to_iio_dev (dev); +struct starfive_saradc *priv =iio_priv (indio_dev); +return sprintf (buf,"%d\n" , priv->mon_ch);Huh?! Is this driver taken from the ancient times and got just dust off?
Quoted 1 line
+}
Quoted 8 lines
...
drivers/iio/adc/starfive-saradc.c line 879
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+ irq =platform_get_irq (pdev,0 ); +if (irq <0 ) +return dev_err_probe (&pdev->dev, irq, +"failed to get irq\n" );No, we don't do dup messages.
Drop it.
Quoted 12 lines
drivers/iio/adc/starfive-saradc.c line 888
Quoted 9 lines
drivers/iio/adc/starfive-saradc.c new file +8
-0+ ret =devm_request_threaded_irq (&pdev->dev, irq, + starfive_saradc_irq_handler, + starfive_saradc_mon_stop_threadfn, +IRQF_TRIGGER_HIGH |IRQF_ONESHOT , +dev_name (&pdev->dev), priv); +if (ret) +return dev_err_probe (&pdev->dev, ret, +"failed to request irq handler\n" );Ditto.
Quoted 5 lines
...
Quoted 1 line
+
Unneeded blank line.
Drop it.
Quoted 7 lines
Quoted 2 lines
+module_platform_driver(starfive_saradc_driver);
--
With Best Regards,
Andy Shevchenko
Best regards,
Xingyu Wu
[email protected]
AI reviewRe: [PATCH v1 2/2] iio: adc: Add StarFive SAR-ADC driver
In reply to Xingyu WuThank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider: - [High] System crash due to requesting IRQ before enabling hardware clocks. - [High] IRQ handler…
Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [High] System crash due to requesting IRQ before enabling hardware clocks.
- [High] IRQ handler unconditionally returns IRQ_HANDLED without clearing interrupts, leading to infinite IRQ storms.
- [High] Clock reference count underflow due to incompatible mixing of devres and manual clock management.
- [High] Missing cleanup for runtime PM and shared resets in error and removal paths.
- [High] Data race on the SARADC_IRQ_EN_ST register causes lost interrupt enables.
- [High] Active hardware monitor is not stopped during device removal.
- [High] Lockless sysfs store functions cause data corruption and IRQ storms.
- [Medium] Multiple IIO ABI violations regarding standard events and data units.
- [Medium] Silent failure on ADC read timeouts.
--
commit 38cba5c631bad8026785f6fd6a6919692ab720de
Author: Xingyu Wu <[email protected]>
Subject: iio: adc: Add StarFive SAR-ADC driver
Add a new IIO ADC driver for the StarFive JHB100 SAR ADC controller.
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c new file +0
-0@@ -0,0 +1,978 @@
[ ... ]
Quoted 2 lines
drivers/iio/adc/starfive-saradc.c new file +2
-0+static void starfive_saradc_ch_monitor_start (struct starfive_saradc *priv,u8 ch) +{
[ ... ]
drivers/iio/adc/starfive-saradc.c line 476
Quoted 3 lines
drivers/iio/adc/starfive-saradc.c new file +3
-0+/* Enable IRQ */ + reg =readl (priv->base +SARADC_IRQ_EN_ST ); +writel (reg |SARADC_IRQ_CH_EN (ch), priv->base +SARADC_IRQ_EN_ST );
Could this race with the interrupt handler? The hardirq handler reads and writes back to SARADC_IRQ_EN_ST to clear status bits without holding locks. If this thread context reads, modifies, and writes the register, could the hardirq handler concurrently write back a stale value and overwrite the interrupt enable bits set here?
[ ... ]
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+static ssize_t starfive_saradc_upper_bound_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t len) +{
[ ... ]
drivers/iio/adc/starfive-saradc.c line 368
Quoted 6 lines
drivers/iio/adc/starfive-saradc.c new file +6
-0+starfive_saradc_ch_upper_bound_set (priv, ch,SARADC_VDD_MV_TO_RAW (upper)); + priv->up_bounds[ch] = upper; +pm_runtime_put (priv->dev); + +return len; +}
Does this need to hold priv->lock? The starfive_saradc_ch_upper_bound_set function performs a read-modify-write on shared hardware registers, which might allow concurrent sysfs writes to corrupt the bounds.
[ ... ]
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+static ssize_t starfive_saradc_monitor_channel_select (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t len) +{
[ ... ]
drivers/iio/adc/starfive-saradc.c line 446
Quoted 7 lines
drivers/iio/adc/starfive-saradc.c new file +7
-0+if (ch >=SARADC_MAX_CHANNELS || ch <0 ) +return -EINVAL ; + + priv->mon_ch = ch; + +return len; +}
Does this update priv->mon_ch without acquiring priv->lock? If the channel is changed while the monitor is active, could the IRQ handler check the wrong bit, fail to clear the interrupt, and trigger an infinite IRQ storm?
[ ... ]
Quoted 3 lines
drivers/iio/adc/starfive-saradc.c new file +3
-0+static IIO_DEVICE_ATTR(in_voltage0_upper, 0644, + starfive_saradc_upper_bound_show, + starfive_saradc_upper_bound_store, 0);
[ ... ]
drivers/iio/adc/starfive-saradc.c line 597
Quoted 3 lines
drivers/iio/adc/starfive-saradc.c new file +3
-0+static IIO_DEVICE_ATTR(voltage_monitor_en, 0644, + starfive_saradc_monitor_status, + starfive_saradc_monitor_enable, 0);
Should this driver use standard IIO events (IIO_EV_TYPE_THRESH) instead of introducing custom sysfs attributes for threshold monitoring?
[ ... ]
Quoted 2 lines
drivers/iio/adc/starfive-saradc.c new file +2
-0+static int starfive_saradc_read (struct starfive_saradc *priv) +{
[ ... ]
Quoted 9 lines
drivers/iio/adc/starfive-saradc.c new file +9
-0+ tmp =starfive_saradc_data_get (priv); +/* Check that the data is ready to be read. */ +if (!(tmp &ADC_DAT_RDY_MSK )) { + ret =readl_poll_timeout (priv->base +SARADC_DATAX_REG_GET (priv->using_ch), tmp, + (tmp &ADC_DAT_RDY_MSK ),10 ,SARADC_TIMEOUT ); +if (ret) { + priv->err = true; +dev_err (priv->dev,"channel%d is still not ready to be read! Timeout!\n" , + priv->using_ch);
drivers/iio/adc/starfive-saradc.c line 659
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+ } + } + +if (priv->err) + tmp =0 ; + +starfive_saradc_ch_stop (priv); + +return (int )(tmp &ADC_DAT_MSK ); +}
If the poll times out, this sets tmp to 0 and returns it. Will this result in starfive_saradc_read_raw passing 0 to userspace as a valid ADC reading instead of propagating an error code like -ETIMEDOUT?
[ ... ]
Quoted 11 lines
drivers/iio/adc/starfive-saradc.c new file +11
-0+case IIO_CHAN_INFO_PROCESSED : + ret =starfive_saradc_read (priv); +if (ret <0 ) +break ; + +/* VIN = AVDD * data[11:0] / 4096. (AVDD = 1.8v) */ + tmp =SARADC_RAW_TO_VDD_ADJ (ret); + *val = (int )(tmp /1000000 ); + *val2 = (int )(tmp %1000000 ); + ret =IIO_VAL_INT_PLUS_MICRO ; +break ;
drivers/iio/adc/starfive-saradc.c line 708
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+ +case IIO_CHAN_INFO_SCALE : +/* +* AVDD is fixed at 1.8v. +* 1.8 / (1 << 12) * 1000000 +*/ + *val =0 ; + *val2 =SARADC_AVDD_VOL / (1 <<SARADC_REALBITS ); + ret =IIO_VAL_INT_PLUS_MICRO ; +break ;
Does this calculate the scale to expose Volts instead of millivolts? The calculation yields 439 instead of 0.439453, meaning users multiplying raw data by the exposed scale will get Volts. This violates the IIO user-space ABI requirement of millivolts.
Similarly, IIO_CHAN_INFO_PROCESSED appears to be formatting the value as fractional Volts rather than millivolts.
[ ... ]
Quoted 8 lines
drivers/iio/adc/starfive-saradc.c new file +8
-0+static irqreturn_t starfive_saradc_irq_handler (int irq,void *data) +{ +struct starfive_saradc *priv = data; +u32 irq_err =readl (priv->base +SARADC_IRQ_EN_ST ); + +if (!priv->mon_working) +return IRQ_HANDLED ; +
drivers/iio/adc/starfive-saradc.c line 764
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+/* Error of out of bounds */ +if (irq_err &BIT (priv->mon_ch)) { +/* Clear the interrupt */ +writel (irq_err, priv->base +SARADC_IRQ_EN_ST ); + priv->err = true; +return IRQ_WAKE_THREAD ; + } + +return IRQ_HANDLED ; +}
If priv->mon_working is false, or if the interrupt bit is not set, this returns IRQ_HANDLED without clearing the hardware interrupt status bits. Since the interrupt is requested with IRQF_TRIGGER_HIGH, won't failing to clear the source while returning IRQ_HANDLED cause the interrupt controller to immediately re-trigger, resulting in an infinite IRQ storm?
[ ... ]
Quoted 2 lines
drivers/iio/adc/starfive-saradc.c new file +2
-0+static void starfive_saradc_init (struct starfive_saradc *priv) +{
[ ... ]
drivers/iio/adc/starfive-saradc.c line 773
Quoted 1 line
drivers/iio/adc/starfive-saradc.c new file +1
-0+if (of_property_read_u16 (priv->dev->of_node,"upper-bound-mv" , &tmp)) {
Are the default Device Tree bounds being parsed into microvolts here but exposed to sysfs where users expect millivolts?
[ ... ]
Quoted 2 lines
drivers/iio/adc/starfive-saradc.c new file +2
-0+static int starfive_saradc_probe (struct platform_device *pdev) +{
[ ... ]
drivers/iio/adc/starfive-saradc.c line 890
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+ ret =devm_request_threaded_irq (&pdev->dev, irq, + starfive_saradc_irq_handler, + starfive_saradc_mon_stop_threadfn, +IRQF_TRIGGER_HIGH |IRQF_ONESHOT , +dev_name (&pdev->dev), priv); +if (ret) +return dev_err_probe (&pdev->dev, ret, +"failed to request irq handler\n" ); + + priv->clk =devm_clk_get_enabled (&pdev->dev,NULL );
Since the IRQ is requested before the clock is enabled, could a pending or spurious interrupt fire immediately upon registration? If the handler executes and accesses the clock-gated MMIO register, it could cause a synchronous external abort or bus stall.
Quoted 13 lines
drivers/iio/adc/starfive-saradc.c new file +13
-0+if (IS_ERR (priv->clk)) +return dev_err_probe (&pdev->dev,PTR_ERR (priv->clk), +"failed to get clock\n" ); + + priv->rst =devm_reset_control_array_get_shared (&pdev->dev); +if (IS_ERR (priv->rst)) +return dev_err_probe (&pdev->dev,PTR_ERR (priv->rst), +"failed to get resets\n" ); + + ret =reset_control_deassert (priv->rst); +if (ret) +return dev_err_probe (&pdev->dev, ret, +"failed to deassert reset\n" );
[ ... ]
drivers/iio/adc/starfive-saradc.c line 920
Quoted 7 lines
drivers/iio/adc/starfive-saradc.c new file +7
-0+pm_runtime_set_active (&pdev->dev); +pm_runtime_set_autosuspend_delay (&pdev->dev,50 ); +pm_runtime_use_autosuspend (&pdev->dev); +pm_runtime_enable (&pdev->dev); + +return devm_iio_device_register (&pdev->dev, indio_dev); +}
If devm_iio_device_register fails, this returns the error without calling pm_runtime_disable. Does this leave the PM framework active on freed devres memory?
Additionally, the shared reset line is deasserted here, but reset_control_assert is never called in the error paths or in starfive_saradc_remove. Could this permanently leak the shared reset line's reference count and prevent the hardware block from ever being reset again?
drivers/iio/adc/starfive-saradc.c line 926
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c new file +5
-0+static void starfive_saradc_remove (struct platform_device *pdev) +{ +pm_runtime_disable (&pdev->dev); +pm_runtime_dont_use_autosuspend (&pdev->dev); +}
If continuous monitor mode is enabled (priv->mon_en = true), the hardware is actively sampling and generating interrupts. Does this leave the monitor running and the interrupt unmasked when the driver unbinds, potentially causing bus lockups when devres subsequently disables the clock?
drivers/iio/adc/starfive-saradc.c line 937
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+static int starfive_saradc_runtime_suspend (struct device *dev) +{ +struct iio_dev *indio_dev =dev_get_drvdata (dev); +struct starfive_saradc *priv =iio_priv (indio_dev); + +starfive_saradc_pwr_on (priv, false); +clk_disable_unprepare (priv->clk); + +return 0 ; +}
The clock is acquired via devm_clk_get_enabled in probe, which queues an automatic disable. Here, the driver manually manages the clock. If the driver is unbound while the device is runtime-suspended, devres will execute its queued clk_disable_unprepare. Will this drop the enable count below zero, causing a kernel warning and resource tracking corruption?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
RE: [PATCH v1 2/2] iio: adc: Add StarFive SAR-ADC driver
Quoted 36 lines · Sashiko
Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [High] System crash due to requesting IRQ before enabling hardware clocks.
- [High] IRQ handler unconditionally returnsIRQ_HANDLEDwithout clearing interrupts, leading to infinite IRQ storms.
- [High] Clock reference count underflow due to incompatible mixing of devres and manual clock management.
- [High] Missing cleanup for runtime PM and shared resets in error and removal paths.
- [High] Data race on theSARADC_IRQ_EN_STregister causes lost interrupt enables.
- [High] Active hardware monitor is not stopped during device removal.
- [High] Lockless sysfs store functions cause data corruption and IRQ storms.
- [Medium] Multiple IIO ABI violations regarding standard events and data units.
- [Medium] Silent failure on ADC read timeouts.
--commit
38cba5c631bad8026785f6fd6a6919692ab720de
Author: Xingyu Wu <[email protected]>
Subject: iio: adc: Add StarFive SAR-ADC driverAdd a new IIO ADC driver for the StarFive JHB100 SAR ADC controller.
Quoted 6 lines
drivers/iio/adc/starfive-saradc.c new file +0
-0@@ -0,0 +1,978 @@[ ... ]
Quoted 2 lines
drivers/iio/adc/starfive-saradc.c new file +2
-0+static void starfive_saradc_ch_monitor_start (struct starfive_saradc +*priv,u8 ch) {[ ... ]
Quoted 9 lines
drivers/iio/adc/starfive-saradc.c line 476
Quoted 3 lines
drivers/iio/adc/starfive-saradc.c new file +3
-0+/* Enable IRQ */ + reg =readl (priv->base +SARADC_IRQ_EN_ST ); +writel (reg |SARADC_IRQ_CH_EN (ch), priv->base +SARADC_IRQ_EN_ST );Could this race with the interrupt handler? The hardirq handler reads and writes back to
SARADC_IRQ_EN_STto clear status bits without holding locks. If this thread context reads, modifies, and writes the register, could the hardirq handler concurrently write back a stale value and overwrite the interrupt enable bits set here?
The function "starfive_saradc_monitor_enable()", which call this, already hold the lock.
Quoted 10 lines
[ ... ]
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+static ssize_t starfive_saradc_upper_bound_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t len) +{[ ... ]
drivers/iio/adc/starfive-saradc.c
Quoted 1 line
drivers/iio/adc/starfive-saradc.c new file +1
-0+ starfive_saradc_ch_upper_bound_set(priv, ch,
SARADC_VDD_MV_TO_RAW(upper));
Quoted 9 lines
drivers/iio/adc/starfive-saradc.c line 368
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c new file +5
-0+ priv->up_bounds[ch] = upper; +pm_runtime_put (priv->dev); + +return len; +}Does this need to hold
priv->lock? Thestarfive_saradc_ch_upper_bound_setfunction performs a read-modify-write on shared hardware registers, which might allow concurrent sysfs writes to corrupt the bounds.
Will add the lock.
Quoted 7 lines
[ ... ]
Quoted 3 lines
drivers/iio/adc/starfive-saradc.c new file +3
-0+static ssize_t starfive_saradc_monitor_channel_select (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t len) {[ ... ]
Quoted 11 lines
drivers/iio/adc/starfive-saradc.c line 446
Quoted 7 lines
drivers/iio/adc/starfive-saradc.c new file +7
-0+if (ch >=SARADC_MAX_CHANNELS || ch <0 ) +return -EINVAL ; + + priv->mon_ch = ch; + +return len; +}Does this update
priv->mon_chwithout acquiringpriv->lock? If the channel is changed while the monitor is active, could the IRQ handler check the wrong bit, fail to clear the interrupt, and trigger an infinite IRQ storm?
Thanks for your reminder. I will add the check about the status of monitor. If it is not working, we can change the priv->mon_ch.
Quoted 7 lines
[ ... ]
Quoted 3 lines
drivers/iio/adc/starfive-saradc.c new file +3
-0+static IIO_DEVICE_ATTR(in_voltage0_upper, 0644, + starfive_saradc_upper_bound_show, + starfive_saradc_upper_bound_store, 0);[ ... ]
Quoted 6 lines
drivers/iio/adc/starfive-saradc.c line 597
Quoted 3 lines
drivers/iio/adc/starfive-saradc.c new file +3
-0+static IIO_DEVICE_ATTR(voltage_monitor_en, 0644, + starfive_saradc_monitor_status, + starfive_saradc_monitor_enable, 0);Should this driver use standard IIO events (
IIO_EV_TYPE_THRESH) instead of introducing custom sysfs attributes for threshold monitoring?
Noted.
Quoted 16 lines
[ ... ]
Quoted 1 line
+static int
starfive_saradc_read(struct starfive_saradc *priv){[ ... ]
drivers/iio/adc/starfive-saradc.c
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+ tmp =starfive_saradc_data_get (priv); +/* Check that the data is ready to be read. */ +if (!(tmp &ADC_DAT_RDY_MSK )) { + ret = readl_poll_timeout(priv->base +
SARADC_DATAX_REG_GET(priv->using_ch), tmp,drivers/iio/adc/starfive-saradc.c
Quoted 1 line
drivers/iio/adc/starfive-saradc.c new file +1
-0+ (tmp & ADC_DAT_RDY_MSK), 10,
SARADC_TIMEOUT);drivers/iio/adc/starfive-saradc.c
Quoted 3 lines
drivers/iio/adc/starfive-saradc.c new file +3
-0+if (ret) { + priv->err = true; + dev_err(priv->dev, "channel%d is still not ready to be read!Timeout!\n",
Quoted 15 lines
drivers/iio/adc/starfive-saradc.c line 659
Quoted 11 lines
drivers/iio/adc/starfive-saradc.c new file +11
-0+ priv->using_ch); + } + } + +if (priv->err) + tmp =0 ; + +starfive_saradc_ch_stop (priv); + +return (int )(tmp &ADC_DAT_MSK ); +}If the poll times out, this sets tmp to 0 and returns it. Will this result in
starfive_saradc_read_rawpassing 0 to userspace as a valid ADC reading instead of propagating an error code like-ETIMEDOUT?
Noted.
Quoted 30 lines
[ ... ]
Quoted 11 lines
drivers/iio/adc/starfive-saradc.c new file +11
-0+case IIO_CHAN_INFO_PROCESSED : + ret =starfive_saradc_read (priv); +if (ret <0 ) +break ; + +/* VIN = AVDD * data[11:0] / 4096. (AVDD = 1.8v) */ + tmp =SARADC_RAW_TO_VDD_ADJ (ret); + *val = (int )(tmp /1000000 ); + *val2 = (int )(tmp %1000000 ); + ret =IIO_VAL_INT_PLUS_MICRO ; +break ;drivers/iio/adc/starfive-saradc.c line 708
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+ +case IIO_CHAN_INFO_SCALE : +/* +* AVDD is fixed at 1.8v. +* 1.8 / (1 << 12) * 1000000 +*/ + *val =0 ; + *val2 =SARADC_AVDD_VOL / (1 <<SARADC_REALBITS ); + ret =IIO_VAL_INT_PLUS_MICRO ; +break ;Does this calculate the scale to expose Volts instead of millivolts? The calculation yields 439 instead of 0.439453, meaning users multiplying raw data by the exposed scale will get Volts. This violates the IIO user-space ABI requirement of millivolts.
Quoted 2 lines
Similarly,
IIO_CHAN_INFO_PROCESSEDappears to be formatting the value as fractional Volts rather than millivolts.
Will fix.
Quoted 18 lines
[ ... ]
Quoted 15 lines
drivers/iio/adc/starfive-saradc.c new file +15
-0+static irqreturn_t starfive_saradc_irq_handler (int irq,void *data) { +struct starfive_saradc *priv = data; +u32 irq_err =readl (priv->base +SARADC_IRQ_EN_ST ); + +if (!priv->mon_working) +return IRQ_HANDLED ; + +/* Error of out of bounds */ +if (irq_err &BIT (priv->mon_ch)) { +/* Clear the interrupt */ +writel (irq_err, priv->base +SARADC_IRQ_EN_ST ); + priv->err = true; +return IRQ_WAKE_THREAD ; + } +
Quoted 8 lines
drivers/iio/adc/starfive-saradc.c
Quoted 2 lines
drivers/iio/adc/starfive-saradc.c new file +2
-0+return IRQ_HANDLED ; +}If
priv->mon_workingis false, or if the interrupt bit is not set, this returnsIRQ_HANDLEDwithout clearing the hardware interrupt status bits. Since the interrupt is requested withIRQF_TRIGGER_HIGH, won't failing to clear the source while returningIRQ_HANDLEDcause the interrupt controller to immediately re-trigger, resulting in an infinite IRQ storm?
Will fix.
Quoted 10 lines
[ ... ]
Quoted 1 line
+static void
starfive_saradc_init(struct starfive_saradc *priv){[ ... ]
drivers/iio/adc/starfive-saradc.c
Quoted 2 lines
drivers/iio/adc/starfive-saradc.c new file +2
-0+if (of_property_read_u16 (priv->dev->of_node,"upper-bound-mv" , +&tmp)) {Are the default Device Tree bounds being parsed into here but exposed to sysfs where users expect millivolts?
We hope to measure in millivolts to reduce the large errors caused by microvolts.
Quoted 5 lines
[ ... ]
Quoted 1 line
+static int
starfive_saradc_probe(struct platform_device *pdev){[ ... ]
Quoted 15 lines
drivers/iio/adc/starfive-saradc.c line 890
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+ ret =devm_request_threaded_irq (&pdev->dev, irq, + starfive_saradc_irq_handler, + starfive_saradc_mon_stop_threadfn, +IRQF_TRIGGER_HIGH |IRQF_ONESHOT , +dev_name (&pdev->dev), priv); +if (ret) +return dev_err_probe (&pdev->dev, ret, +"failed to request irq handler\n" ); + + priv->clk =devm_clk_get_enabled (&pdev->dev,NULL );Since the IRQ is requested before the clock is enabled, could a pending or spurious interrupt fire immediately upon registration? If the handler executes and accesses the clock-gated MMIO register, it could cause a synchronous external abort or bus stall.
Will fix.
Quoted 26 lines
Quoted 14 lines
drivers/iio/adc/starfive-saradc.c new file +13
-0+if (IS_ERR (priv->clk)) +return dev_err_probe (&pdev->dev,PTR_ERR (priv->clk), +"failed to get clock\n" ); + + priv->rst =devm_reset_control_array_get_shared (&pdev->dev); +if (IS_ERR (priv->rst)) +return dev_err_probe (&pdev->dev,PTR_ERR (priv->rst), +"failed to get resets\n" ); + + ret =reset_control_deassert (priv->rst); +if (ret) +return dev_err_probe (&pdev->dev, ret, +"failed to deassert reset\n" );[ ... ]
drivers/iio/adc/starfive-saradc.c
Quoted 6 lines
drivers/iio/adc/starfive-saradc.c new file +6
-0+pm_runtime_set_active (&pdev->dev); +pm_runtime_set_autosuspend_delay (&pdev->dev,50 ); +pm_runtime_use_autosuspend (&pdev->dev); +pm_runtime_enable (&pdev->dev); + +return devm_iio_device_register (&pdev->dev, indio_dev); }If
devm_iio_device_registerfails, this returns the error without callingpm_runtime_disable. Does this leave the PM framework active on freed devres memory?
Quoted 4 lines
Additionally, the shared reset line is deasserted here, but
reset_control_assertis never called in the error paths or instarfive_saradc_remove. Could this permanently leak the shared reset line's reference count and prevent the hardware block from ever being reset again?
Will fix.
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c line 926
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+static void starfive_saradc_remove (struct platform_device *pdev) { +pm_runtime_disable (&pdev->dev); +pm_runtime_dont_use_autosuspend (&pdev->dev); +}If continuous monitor mode is enabled (
priv->mon_en= true), the hardware is actively sampling and generating interrupts. Does this leave the monitor running and the interrupt unmasked when the driver unbinds, potentially causing bus lockups when devres subsequently disables the clock?
Your concern is correct. I will disable monitor and power down device when driver is removed.
Quoted 16 lines
drivers/iio/adc/starfive-saradc.c line 937
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +9
-0+static int starfive_saradc_runtime_suspend (struct device *dev) { +struct iio_dev *indio_dev =dev_get_drvdata (dev); +struct starfive_saradc *priv =iio_priv (indio_dev); + +starfive_saradc_pwr_on (priv, false); +clk_disable_unprepare (priv->clk); + +return 0 ; +}The clock is acquired via
devm_clk_get_enabledin probe, which queues an automatic disable. Here, the driver manually manages the clock. If the driver is unbound while the device is runtime-suspended, devres will execute its queuedclk_disable_unprepare. Will this drop the enable count below zero, causing a kernel warning and resource tracking corruption?
Will fix.
Quoted 4 lines
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260518081852.116909-1-
[email protected]?part=2
Best regards,
Xingyu Wu
Jonathan Cameron
maintainerRe: [PATCH v1 2/2] iio: adc: Add StarFive SAR-ADC driver
In reply to Xingyu WuQuoted 9 lines · On Mon, 18 May 2026 16:18:52 +0800 Xingyu Wu <[email protected]> wrote:
Add a new IIO ADC driver for the StarFive JHB100 SAR ADC controller.
The hardware provides 12-bit conversion precision and up to 8 input channels. This driver supports single-shot channel reads and exposes standard IIO interfaces for raw ADC values, processed voltages, and scale reporting. The driver also supports channel monitor mode with out-of-bound detection and scan frequency configuration.
Signed-off-by Xingyu Wu <[email protected]>
Hi
Welcome to IIO.
As Andy indicated there is quite a lot of work to do here. As such I did a quick review, but a lot of the code will change anyway as a result of ripping out the custom ABI and the dt-binding changes requested.
Jonathan
drivers/iio/adc/starfive-saradc.c line 100
Quoted 13 lines
drivers/iio/adc/starfive-saradc.c new file +7
-0+ +#define SARADC_CHAN (_index) { \ + .type = IIO_VOLTAGE, \ + .indexed = 1, \ + .channel = _index, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ + BIT(IIO_CHAN_INFO_PROCESSED), \
See below.
drivers/iio/adc/starfive-saradc.c line 103
Quoted 3 lines
drivers/iio/adc/starfive-saradc.c new file +3
-0+ .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SCALE), \ + .datasheet_name = "SARADC_"#_index, \ + .scan_index = _index, \
Not used.
drivers/iio/adc/starfive-saradc.c line 107
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+ .scan_type = { \ + .sign = 'u', \ + .realbits = SARADC_REALBITS, \ + .endianness = IIO_CPU, \
Not used - so don't specify it.
drivers/iio/adc/starfive-saradc.c line 136
Quoted 13 lines
drivers/iio/adc/starfive-saradc.c new file +12
-0+ }, \ +} + +static const struct iio_chan_spec starfive_saradc_iio_channels[] = { +SARADC_CHAN (0 ), +SARADC_CHAN (1 ), +SARADC_CHAN (2 ), +SARADC_CHAN (3 ), +SARADC_CHAN (4 ), +SARADC_CHAN (5 ), +SARADC_CHAN (6 ), +SARADC_CHAN (7 ),
SARADC is a common name, so prefix macros etc with something related to starfive.
drivers/iio/adc/starfive-saradc.c line 267
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +9
-0+}; + +static void starfive_saradc_ch_lower_bound_set (struct starfive_saradc *priv, +int ch,u32 data) +{ +void __iomem *base = priv->base +SARADC_ULB_CHX_REG_GET (ch); +u32 reg =readl (base) & ~ADC_LOWER_BOUND_MSK ; + +writel (FIELD_PREP (ADC_LOWER_BOUND_MSK , data) | reg, base);
These helpers don't really help much. I'd just put the readl/writel inline where it's needed.
Quoted 13 lines
drivers/iio/adc/starfive-saradc.c new file +12
-0+} + +static struct attribute *starfive_saradc_attributes[] = { + &iio_dev_attr_scan_frequency.dev_attr.attr, + &iio_dev_attr_in_voltage0_upper.dev_attr.attr, + &iio_dev_attr_in_voltage1_upper.dev_attr.attr, + &iio_dev_attr_in_voltage2_upper.dev_attr.attr, + &iio_dev_attr_in_voltage3_upper.dev_attr.attr, + &iio_dev_attr_in_voltage4_upper.dev_attr.attr, + &iio_dev_attr_in_voltage5_upper.dev_attr.attr, + &iio_dev_attr_in_voltage6_upper.dev_attr.attr, + &iio_dev_attr_in_voltage7_upper.dev_attr.attr,
drivers/iio/adc/starfive-saradc.c line 618
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+ &iio_dev_attr_in_voltage0_lower.dev_attr.attr, + &iio_dev_attr_in_voltage1_lower.dev_attr.attr, + &iio_dev_attr_in_voltage2_lower.dev_attr.attr, + &iio_dev_attr_in_voltage3_lower.dev_attr.attr, + &iio_dev_attr_in_voltage4_lower.dev_attr.attr, + &iio_dev_attr_in_voltage5_lower.dev_attr.attr, + &iio_dev_attr_in_voltage6_lower.dev_attr.attr, + &iio_dev_attr_in_voltage7_lower.dev_attr.attr, + &iio_dev_attr_voltage_monitor_channel.dev_attr.attr, + &iio_dev_attr_voltage_monitor_en.dev_attr.attr,
Look at IIO event support and use that. I'm not seeing anything in here that should be provided as custom attributes.
Basic rule is that if you add any custom attributes your driver will get a lot more push back. Key is that
custom attributes == no userspace support in standard code == not much used.
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c new file +5
-0+ NULL, +}; + +static const struct attribute_group starfive_saradc_attr_group = { + .attrs = starfive_saradc_attributes,
drivers/iio/adc/starfive-saradc.c line 633
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+}; + +static int starfive_saradc_read (struct starfive_saradc *priv) +{ +unsigned int tmp; +int ret; + +starfive_saradc_ch_dis_save (priv); +if (!starfive_saradc_is_ready (priv)) { +dev_err (priv->dev,"ADC do not ready, please try again later!\n" );
Generally just return -EBUSY for this - no print because otherwise you provide a userspace path to spam the kernel logs. If a print is really needed it should be rate limited.
Quoted 15 lines
drivers/iio/adc/starfive-saradc.c new file +15
-0+starfive_saradc_ch_stop (priv); +return -EBUSY ; + } + + priv->err = false; +starfive_saradc_ch_start (priv); + + tmp =starfive_saradc_data_get (priv); +/* Check that the data is ready to be read. */ +if (!(tmp &ADC_DAT_RDY_MSK )) { + ret =readl_poll_timeout (priv->base +SARADC_DATAX_REG_GET (priv->using_ch), tmp, + (tmp &ADC_DAT_RDY_MSK ),10 ,SARADC_TIMEOUT ); +if (ret) { + priv->err = true; + dev_err(priv->dev, "channel%d is still not ready to be read! Timeout!\n",
drivers/iio/adc/starfive-saradc.c line 658
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+ priv->using_ch); + } + } + +if (priv->err) + tmp =0 ; + +starfive_saradc_ch_stop (priv); + +return (int )(tmp &ADC_DAT_MSK );
Prefer FIELD_GET() for all value extraction.
drivers/iio/adc/starfive-saradc.c line 669
Quoted 11 lines
drivers/iio/adc/starfive-saradc.c new file +11
-0+} + +static int starfive_saradc_read_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +int *val,int *val2,long mask) +{ +struct starfive_saradc *priv =iio_priv (indio_dev); +int ret; +u64 tmp; + +mutex_lock (&priv->lock);
guard() and acquire for PM (see below).
But try to do these in the same order in all functions. Makes reasoning about what is protected simpler. Normally I'd turn the power on first as that is reference counted anyway so doesn't need to be under the lock.
drivers/iio/adc/starfive-saradc.c line 673
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+ priv->using_ch = chan->channel; + ret =pm_runtime_get_sync (priv->dev); +if (ret <0 ) { +pm_runtime_put_noidle (priv->dev);
As below. the call that failed doesn't have side effects on failure so this smells like an underflow.
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c new file +5
-0+mutex_unlock (&priv->lock); +return ret; + } + +switch (mask) {
drivers/iio/adc/starfive-saradc.c line 688
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+case IIO_CHAN_INFO_RAW : + ret =starfive_saradc_read (priv); +if (ret <0 ) +break ; + + *val = ret; + ret =IIO_VAL_INT ; +break ; + +case IIO_CHAN_INFO_PROCESSED :
Don't provide both processed and raw for a channel.
If the transform is linear then _raw + _scale if not then _processed.
Quoted 29 lines
drivers/iio/adc/starfive-saradc.c new file +28
-0+ ret =starfive_saradc_read (priv); +if (ret <0 ) +break ; + +/* VIN = AVDD * data[11:0] / 4096. (AVDD = 1.8v) */ + tmp =SARADC_RAW_TO_VDD_ADJ (ret); + *val = (int )(tmp /1000000 ); + *val2 = (int )(tmp %1000000 ); + ret =IIO_VAL_INT_PLUS_MICRO ; +break ; + +case IIO_CHAN_INFO_SCALE : +/* +* AVDD is fixed at 1.8v. +* 1.8 / (1 << 12) * 1000000 +*/ + *val =0 ; + *val2 =SARADC_AVDD_VOL / (1 <<SARADC_REALBITS ); + ret =IIO_VAL_INT_PLUS_MICRO ; +break ; + +default : + ret = -EINVAL ; + } + +pm_runtime_put_autosuspend (priv->dev); +mutex_unlock (&priv->lock); +
drivers/iio/adc/starfive-saradc.c line 726
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+return ret; +} + +static irqreturn_t starfive_saradc_mon_stop_threadfn (int irq,void *data) +{ +struct starfive_saradc *priv = data; +u8 ch = priv->mon_ch; +u32 up, low, raw; + +mutex_lock (&priv->lock);
guard()
drivers/iio/adc/starfive-saradc.c line 727
Quoted 1 line
drivers/iio/adc/starfive-saradc.c new file +1
-0+if (!priv->mon_en) {
How would we get here? If it's a race on shut down of events then normally we wouldn't worry as that's just precise timing of disable vs this thread running.
drivers/iio/adc/starfive-saradc.c line 738
Quoted 11 lines
drivers/iio/adc/starfive-saradc.c new file +11
-0+mutex_unlock (&priv->lock); +return IRQ_HANDLED ; + } + + raw =readl (priv->base +SARADC_DATAX_REG_GET (ch)) &ADC_DAT_MSK ; + up =starfive_saradc_ch_upper_bound_get (priv, ch); + low =starfive_saradc_ch_lower_bound_get (priv, ch); +dev_err (priv->dev, +"channel %d is out of bounds. sample data: %dmv (range: %dmv ~ %dmv)\n" , + ch, (SARADC_RAW_TO_VDD_ADJ (raw) /1000 ), + priv->low_bounds[ch], priv->up_bounds[ch]);
We have the IIO events infrastructure to report these to userspace. Use it or drop this. dev_err is not a valid way to report this sort of threshold events.
drivers/iio/adc/starfive-saradc.c
Quoted 2 lines
drivers/iio/adc/starfive-saradc.c new file +2
-0+ +starfive_saradc_ch_monitor_stop (priv, ch);
Why stop it? Add a comment. We aren't interested in future events?
drivers/iio/adc/starfive-saradc.c line 752
Quoted 12 lines
drivers/iio/adc/starfive-saradc.c new file +12
-0+pm_runtime_put_autosuspend (priv->dev); +mutex_unlock (&priv->lock); + +return IRQ_HANDLED ; +} + +static irqreturn_t starfive_saradc_irq_handler (int irq,void *data) +{ +struct starfive_saradc *priv = data; +u32 irq_err =readl (priv->base +SARADC_IRQ_EN_ST ); + +if (!priv->mon_working)
How do we hit this condition? If it's a race thing then add a comment on what that race is. If it's not our interrupt then return IRQ_NONE.
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c new file +5
-0+return IRQ_HANDLED ; + +/* Error of out of bounds */ +if (irq_err &BIT (priv->mon_ch)) { +/* Clear the interrupt */
drivers/iio/adc/starfive-saradc.c line 767
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+writel (irq_err, priv->base +SARADC_IRQ_EN_ST ); + priv->err = true; +return IRQ_WAKE_THREAD ; + } + +return IRQ_HANDLED ; +} + +static void starfive_saradc_init (struct starfive_saradc *priv) +{
This will probably go away anyway but comments inline for future reference.
drivers/iio/adc/starfive-saradc.c line 773
Quoted 6 lines
drivers/iio/adc/starfive-saradc.c new file +6
-0+bool use_def = false; +u16 up, low, scan, tmp; +u32 upmv, lowmv; +int i; + +if (of_property_read_u16 (priv->dev->of_node,"upper-bound-mv" , &tmp)) {
Use property.h accessors for new drivers, not the of_ones.
They are cleaner and maybe this driver will one day be used with other firmware types. Also I don't want of specific code in IIO drivers because it gets cut and paste into others where another firmware type is actually likely.
drivers/iio/adc/starfive-saradc.c line 777
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+ use_def = true; + }else { + up =SARADC_VDD_MV_TO_RAW (tmp); +if (up >ADC_UPPER_BOUND_DEF )
If DT provide an invalid input fail to probe. We need that fixed.
If it was a missing property then current convention is to use a check on the property existing as a separate call before reading it. That provides neater error handling.
Mind you as per he discussion in that dt-binding thread this is going away.
drivers/iio/adc/starfive-saradc.c line 789
Quoted 12 lines
drivers/iio/adc/starfive-saradc.c new file +12
-0+ use_def = true; + else + upmv = tmp; + } + +if (use_def) { + up =ADC_UPPER_BOUND_DEF ; + upmv =SARADC_RAW_TO_VDD_ADJ (up); + use_def = false; + } + +if (of_property_read_u16 (priv->dev->of_node,"lower-bound-mv" , &tmp)) {
Same for all of these as commnets above.
Quoted 32 lines
drivers/iio/adc/starfive-saradc.c new file +32
-0+ use_def = true; + }else { + low =SARADC_VDD_MV_TO_RAW (tmp); +if (low >ADC_UPPER_BOUND_DEF ) + use_def = true; +else + lowmv = tmp; + } + +if (use_def) { + low =ADC_LOWER_BOUND_DEF ; + lowmv =SARADC_RAW_TO_VDD_ADJ (low); + } + +if (of_property_read_u16 (priv->dev->of_node,"scan-freq" , &scan)) + scan =ADC_SCAN_FREQ_DEF ; + +if ((scan &ADC_SCAN_FREQ_MSK ) <ADC_SCAN_FREQ_MIN ) { +dev_warn (priv->dev,"The scan_freq is out of range and use the default value!\n" ); + scan =ADC_SCAN_FREQ_DEF ; + } + +starfive_saradc_scan_freq_set (priv, scan); + +for (i =0 ; i <SARADC_MAX_CHANNELS ; i++) { +starfive_saradc_ch_upper_bound_set (priv, i, up); +starfive_saradc_ch_lower_bound_set (priv, i, low); + priv->up_bounds[i] = upmv; + priv->low_bounds[i] = lowmv; + } +} +
drivers/iio/adc/starfive-saradc.c line 831
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+static int starfive_saradc_reg_access (struct iio_dev *indio_dev,unsigned int reg, +unsigned int writeval,unsigned int *readval) +{ +struct starfive_saradc *priv =iio_priv (indio_dev); +int ret =0 ; + +if (reg %4 || reg >SARADC_SCAN_FREQ ) +return -EINVAL ; + + ret =pm_runtime_get_sync (priv->dev);
Look at PM_RUNTIME_ACQUIRE_AUTOSUSPEND()
drivers/iio/adc/starfive-saradc.c
Quoted 2 lines
drivers/iio/adc/starfive-saradc.c new file +2
-0+if (ret <0 ) { +pm_runtime_put_noidle (priv->dev);
Why? It failed so we should not have a refcount to drop.
drivers/iio/adc/starfive-saradc.c line 726
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+return ret; + } + +mutex_lock (&priv->lock);
In combination with the acquire mentioned above, use a guard() for the mutex.
drivers/iio/adc/starfive-saradc.c line 843
Quoted 6 lines
drivers/iio/adc/starfive-saradc.c new file +6
-0+ +if (readval) + *readval =readl (priv->base + reg); +else if (reg <SARADC_ULB_CH0 ) +/* Allowed to write from SARADC_ULB_CH0 to SARADC_SCAN_FREQ */ + ret = -EINVAL ;
With the changes above you can just return here.
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+else +writel (writeval, priv->base + reg); + +mutex_unlock (&priv->lock); +pm_runtime_put_sync_autosuspend (priv->dev); + +return ret; +} + +static const struct iio_info starfive_saradc_iio_info = {
drivers/iio/adc/starfive-saradc.c line 863
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+ .read_raw = starfive_saradc_read_raw, + .debugfs_reg_access = starfive_saradc_reg_access, + .attrs = &starfive_saradc_attr_group, +}; + +static int starfive_saradc_probe (struct platform_device *pdev) +{ +struct starfive_saradc *priv; +struct iio_dev *indio_dev; +int irq, ret;
Worth using a local struct device.
e.g.
struct device *dev = &pdev->dev;
just to shorten the various lines where it's used.
drivers/iio/adc/starfive-saradc.c line 867
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+ + indio_dev =devm_iio_device_alloc (&pdev->dev,sizeof (*priv)); +if (!indio_dev) +return dev_err_probe (&pdev->dev, -ENOMEM ,"failed allocating iio device\n" );
This print is a noop for -ENOMEM so don't bother having it.
if (!indio_dev) return -ENOMEM;
drivers/iio/adc/starfive-saradc.c line 879
Quoted 12 lines
drivers/iio/adc/starfive-saradc.c new file +12
-0+ + priv =iio_priv (indio_dev); +platform_set_drvdata (pdev, indio_dev); + priv->dev = &pdev->dev; + priv->base =devm_platform_ioremap_resource (pdev,0 ); +if (IS_ERR (priv->base)) +return PTR_ERR (priv->base); + + irq =platform_get_irq (pdev,0 ); +if (irq <0 ) +return dev_err_probe (&pdev->dev, irq, +"failed to get irq\n" );
One line. Note that going slightly over 80 chars is fine if it helps readability. I think this one is just under 80 anyway.
drivers/iio/adc/starfive-saradc.c line 884
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c new file +5
-0+ + ret = devm_request_threaded_irq(&pdev->dev, irq, + starfive_saradc_irq_handler, + starfive_saradc_mon_stop_threadfn, + IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
Trigger direction should come from DT. So don't set it here. We do this historically in some drivers and can't fix it now because some board might rely on it. Don't introduce it in a new driver.
drivers/iio/adc/starfive-saradc.c line 893
Quoted 9 lines
drivers/iio/adc/starfive-saradc.c new file +9
-0+ dev_name(&pdev->dev), priv); +if (ret) +return dev_err_probe (&pdev->dev, ret, +"failed to request irq handler\n" ); + + priv->clk =devm_clk_get_enabled (&pdev->dev,NULL ); +if (IS_ERR (priv->clk)) +return dev_err_probe (&pdev->dev,PTR_ERR (priv->clk), +"failed to get clock\n" );
As sashiko pointed out this might result in a clock underflow. Fixing that the mess of devm + runtime pm and working out general patterns is still a work in progress. For now maybe just check if you get such a warning.
Quoted 22 lines
drivers/iio/adc/starfive-saradc.c new file +22
-0+ + priv->rst =devm_reset_control_array_get_shared (&pdev->dev); +if (IS_ERR (priv->rst)) +return dev_err_probe (&pdev->dev,PTR_ERR (priv->rst), +"failed to get resets\n" ); + + ret =reset_control_deassert (priv->rst); +if (ret) +return dev_err_probe (&pdev->dev, ret, +"failed to deassert reset\n" ); + + indio_dev->name =dev_name (&pdev->dev); + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->info = &starfive_saradc_iio_info; + indio_dev->channels = starfive_saradc_iio_channels; + indio_dev->num_channels =ARRAY_SIZE (starfive_saradc_iio_channels); + +starfive_saradc_init (priv); +mutex_init (&priv->lock); + +pm_runtime_set_active (&pdev->dev); +pm_runtime_set_autosuspend_delay (&pdev->dev,50 );
drivers/iio/adc/starfive-saradc.c line 925
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+pm_runtime_use_autosuspend (&pdev->dev); +pm_runtime_enable (&pdev->dev); + +return devm_iio_device_register (&pdev->dev, indio_dev); +} + +static void starfive_saradc_remove (struct platform_device *pdev) +{ +pm_runtime_disable (&pdev->dev); +pm_runtime_dont_use_autosuspend (&pdev->dev);
This is out of order wrt to the setup in probe. I suspect you can usedevm_pm_runtime_enable() instead and drop remove entirely.
drivers/iio/adc/starfive-saradc.c line 933
Quoted 8 lines
drivers/iio/adc/starfive-saradc.c new file +8
-0+} + +static int starfive_saradc_runtime_suspend (struct device *dev) +{ +struct iio_dev *indio_dev =dev_get_drvdata (dev); +struct starfive_saradc *priv =iio_priv (indio_dev); + +starfive_saradc_pwr_on (priv, false);
Having a function called pwr_on() is missleading given you use it to turn the power off. Rename it simply pwr() Or consider if it is worth having the helper at all as it's very small and maybe having the code inline is simpler.
drivers/iio/adc/starfive-saradc.c line 946
Quoted 13 lines
drivers/iio/adc/starfive-saradc.c new file +13
-0+clk_disable_unprepare (priv->clk); + +return 0 ; +} + +static int starfive_saradc_runtime_resume (struct device *dev) +{ +struct iio_dev *indio_dev =dev_get_drvdata (dev); +struct starfive_saradc *priv =iio_priv (indio_dev); +int ret =clk_prepare_enable (priv->clk); + +if (ret) +return ret;
slight preference for
int ret; ret = clk_prepare_enable(priv->clk); if (ret) return ret;
as it keeps the set of ret and it's check right next to each other.
drivers/iio/adc/starfive-saradc.c line 950
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+ +starfive_saradc_pwr_on (priv, true); +/* Need time to completely power on. */ +msleep (20 );
fsleep() preferred. Takes time in usecs but the point is it will use the generally right call for whatever the sleep is. That means readers don't have to think about it. See the docs for the implementation of fsleep() for more info on how it decides whether to call msleep or not.
If possible reference a datasheet section / table or similar for this sleep length. That is helpful if turns out to be too short on some device in the future.
Quoted 12 lines
drivers/iio/adc/starfive-saradc.c new file +12
-0+ +return 0 ; +} + +static DEFINE_RUNTIME_DEV_PM_OPS(starfive_saradc_pm_ops, + starfive_saradc_runtime_suspend, + starfive_saradc_runtime_resume, NULL); + +static const struct of_device_id starfive_saradc_of_match[] = { + { .compatible ="starfive,jhb100-saradc" , }, + { } +};
drivers/iio/adc/starfive-saradc.c line 972
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+ +static struct platform_driver starfive_saradc_driver = { + .probe = starfive_saradc_probe, + .remove = starfive_saradc_remove, + .driver = { + .name ="starfive_saradc" , + .of_match_table = starfive_saradc_of_match, + .pm =pm_ptr (&starfive_saradc_pm_ops) + }, +};
common practice to not have a blank line here. Dropping it makes a clear association between the struct platform_driver and the macro that uses it.
Quoted 6 lines
drivers/iio/adc/starfive-saradc.c new file +6
-0+ +module_platform_driver (starfive_saradc_driver); + +MODULE_AUTHOR ("Xingyu Wu <[email protected]>" ); +MODULE_DESCRIPTION ("StarFive Successive Approximation Register ADC driver" ); +MODULE_LICENSE ("GPL" );
RE: [PATCH v1 2/2] iio: adc: Add StarFive SAR-ADC driver
Quoted 38 lines · Jonathan Cameron
Quoted 9 lines · On Mon, 18 May 2026 16:18:52 +0800 Xingyu Wu <[email protected]> wrote:
Add a new IIO ADC driver for the StarFive JHB100 SAR ADC controller.
The hardware provides 12-bit conversion precision and up to 8 input channels. This driver supports single-shot channel reads and exposes standard IIO interfaces for raw ADC values, processed voltages, and scale reporting. The driver also supports channel monitor mode with out-of-bound detection and scan frequency configuration.
Signed-off-by Xingyu Wu <[email protected]>Hi
Welcome to IIO.
As Andy indicated there is quite a lot of work to do here.
As such I did a quick review, but a lot of the code will change anyway as a result of ripping out the custom ABI and the dt-binding changes requested.Jonathan
Quoted 6 lines
drivers/iio/adc/starfive-saradc.c new file +0
-0drivers/iio/adc/starfive-saradc.c line 100
Quoted 8 lines
drivers/iio/adc/starfive-saradc.c new file +7
-0+ +#define SARADC_CHAN (_index) { \ + .type = IIO_VOLTAGE, \ + .indexed = 1, \ + .channel = _index, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ + BIT(IIO_CHAN_INFO_PROCESSED), \See below.
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c line 103
Quoted 3 lines
drivers/iio/adc/starfive-saradc.c new file +3
-0+ .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SCALE), \ + .datasheet_name = "SARADC_"#_index, \ + .scan_index = _index, \Not used.
Noted.
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c line 107
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+ .scan_type = { \ + .sign = 'u', \ + .realbits = SARADC_REALBITS, \ + .endianness = IIO_CPU, \Not used - so don't specify it.
Will drop it.
Quoted 16 lines
drivers/iio/adc/starfive-saradc.c line 136
Quoted 13 lines
drivers/iio/adc/starfive-saradc.c new file +12
-0+ }, \ +} + +static const struct iio_chan_spec starfive_saradc_iio_channels[] = { +SARADC_CHAN (0 ), +SARADC_CHAN (1 ), +SARADC_CHAN (2 ), +SARADC_CHAN (3 ), +SARADC_CHAN (4 ), +SARADC_CHAN (5 ), +SARADC_CHAN (6 ), +SARADC_CHAN (7 ),SARADC is a common name, so prefix macros etc with something related to starfive.
Noted.
Quoted 13 lines
drivers/iio/adc/starfive-saradc.c line 267
Quoted 11 lines
drivers/iio/adc/starfive-saradc.c new file +9
-0+}; + +static void starfive_saradc_ch_lower_bound_set (struct starfive_saradc *priv, +int ch,u32 data) +{ +void __iomem *base = priv->base +SARADC_ULB_CHX_REG_GET (ch); +u32 reg =readl (base) & ~ADC_LOWER_BOUND_MSK ; + +writel (FIELD_PREP (ADC_LOWER_BOUND_MSK , data) | reg, base);These helpers don't really help much. I'd just put the readl/writel inline where it's needed.
Noted.
Quoted 28 lines
Quoted 14 lines
drivers/iio/adc/starfive-saradc.c new file +12
-0+} + +static struct attribute *starfive_saradc_attributes[] = { + &iio_dev_attr_scan_frequency.dev_attr.attr, + &iio_dev_attr_in_voltage0_upper.dev_attr.attr, + &iio_dev_attr_in_voltage1_upper.dev_attr.attr, + &iio_dev_attr_in_voltage2_upper.dev_attr.attr, + &iio_dev_attr_in_voltage3_upper.dev_attr.attr, + &iio_dev_attr_in_voltage4_upper.dev_attr.attr, + &iio_dev_attr_in_voltage5_upper.dev_attr.attr, + &iio_dev_attr_in_voltage6_upper.dev_attr.attr, + &iio_dev_attr_in_voltage7_upper.dev_attr.attr,drivers/iio/adc/starfive-saradc.c line 618
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+ &iio_dev_attr_in_voltage0_lower.dev_attr.attr, + &iio_dev_attr_in_voltage1_lower.dev_attr.attr, + &iio_dev_attr_in_voltage2_lower.dev_attr.attr, + &iio_dev_attr_in_voltage3_lower.dev_attr.attr, + &iio_dev_attr_in_voltage4_lower.dev_attr.attr, + &iio_dev_attr_in_voltage5_lower.dev_attr.attr, + &iio_dev_attr_in_voltage6_lower.dev_attr.attr, + &iio_dev_attr_in_voltage7_lower.dev_attr.attr, + &iio_dev_attr_voltage_monitor_channel.dev_attr.attr, + &iio_dev_attr_voltage_monitor_en.dev_attr.attr,Look at IIO event support and use that. I'm not seeing anything in here that should be provided as custom attributes.
Quoted 3 lines
Basic rule is that if you add any custom attributes your driver will get a lot more push back. Key is that custom attributes == no userspace support in standard code == not much used.
Well, I will use IIO event instead.
Quoted 11 lines
Quoted 11 lines
drivers/iio/adc/starfive-saradc.c new file +9
-0+ NULL, +}; + +static const struct attribute_group starfive_saradc_attr_group = { + .attrs = starfive_saradc_attributes, }; + +static int starfive_saradc_read (struct starfive_saradc *priv) { +unsigned int tmp; +int ret;
Quoted 8 lines
drivers/iio/adc/starfive-saradc.c line 633
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+ +starfive_saradc_ch_dis_save (priv); +if (!starfive_saradc_is_ready (priv)) { +dev_err (priv->dev,"ADC do not ready, please try again later!\n" );Generally just return
-EBUSYfor this - no print because otherwise you provide a userspace path to spam the kernel logs. If a print is really needed it should be rate limited.
I will drop the print.
Quoted 19 lines
drivers/iio/adc/starfive-saradc.c
Quoted 12 lines
drivers/iio/adc/starfive-saradc.c new file +11
-0+starfive_saradc_ch_stop (priv); +return -EBUSY ; + } + + priv->err = false; +starfive_saradc_ch_start (priv); + + tmp =starfive_saradc_data_get (priv); +/* Check that the data is ready to be read. */ +if (!(tmp &ADC_DAT_RDY_MSK )) { + ret = readl_poll_timeout(priv->base +
SARADC_DATAX_REG_GET(priv->using_ch), tmp,drivers/iio/adc/starfive-saradc.c
Quoted 1 line
drivers/iio/adc/starfive-saradc.c new file +1
-0+ (tmp & ADC_DAT_RDY_MSK), 10,
SARADC_TIMEOUT);drivers/iio/adc/starfive-saradc.c
Quoted 3 lines
drivers/iio/adc/starfive-saradc.c new file +3
-0+if (ret) { + priv->err = true; + dev_err(priv->dev, "channel%d is still not ready to be read!Timeout!\n",
Quoted 12 lines
drivers/iio/adc/starfive-saradc.c line 658
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+ priv->using_ch); + } + } + +if (priv->err) + tmp =0 ; + +starfive_saradc_ch_stop (priv); + +return (int )(tmp &ADC_DAT_MSK );Prefer
FIELD_GET()for all value extraction.
Noted.
Quoted 15 lines
drivers/iio/adc/starfive-saradc.c line 669
Quoted 11 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+} + +static int starfive_saradc_read_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +int *val,int *val2,long mask) { +struct starfive_saradc *priv =iio_priv (indio_dev); +int ret; +u64 tmp; + +mutex_lock (&priv->lock);
guard()and acquire for PM (see below).
But try to do these in the same order in all functions. Makes reasoning about what is protected simpler. Normally I'd turn the power on first as that is reference counted anyway so doesn't need to be under the lock.
Noted.
Quoted 26 lines
drivers/iio/adc/starfive-saradc.c line 673
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+ priv->using_ch = chan->channel; + ret =pm_runtime_get_sync (priv->dev); +if (ret <0 ) { +pm_runtime_put_noidle (priv->dev);As below. the call that failed doesn't have side effects on failure so this smells like an underflow.
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c new file +5
-0+mutex_unlock (&priv->lock); +return ret; + } + +switch (mask) {drivers/iio/adc/starfive-saradc.c line 688
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+case IIO_CHAN_INFO_RAW : + ret =starfive_saradc_read (priv); +if (ret <0 ) +break ; + + *val = ret; + ret =IIO_VAL_INT ; +break ; + +case IIO_CHAN_INFO_PROCESSED :Don't provide both processed and raw for a channel.
Quoted 1 line
If the transform is linear then
_raw+_scaleif not then_processed.
Drop it.
Quoted 42 lines
Quoted 30 lines
drivers/iio/adc/starfive-saradc.c new file +28
-0+ ret =starfive_saradc_read (priv); +if (ret <0 ) +break ; + +/* VIN = AVDD * data[11:0] / 4096. (AVDD = 1.8v) */ + tmp =SARADC_RAW_TO_VDD_ADJ (ret); + *val = (int )(tmp /1000000 ); + *val2 = (int )(tmp %1000000 ); + ret =IIO_VAL_INT_PLUS_MICRO ; +break ; + +case IIO_CHAN_INFO_SCALE : +/* +* AVDD is fixed at 1.8v. +* 1.8 / (1 << 12) * 1000000 +*/ + *val =0 ; + *val2 =SARADC_AVDD_VOL / (1 <<SARADC_REALBITS ); + ret =IIO_VAL_INT_PLUS_MICRO ; +break ; + +default : + ret = -EINVAL ; + } + +pm_runtime_put_autosuspend (priv->dev); +mutex_unlock (&priv->lock); +drivers/iio/adc/starfive-saradc.c line 726
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+return ret; +} + +static irqreturn_t starfive_saradc_mon_stop_threadfn (int irq,void +*data) { +struct starfive_saradc *priv = data; +u8 ch = priv->mon_ch; +u32 up, low, raw; + +mutex_lock (&priv->lock);
guard()
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c line 727
Quoted 1 line
drivers/iio/adc/starfive-saradc.c new file +1
-0+if (!priv->mon_en) {How would we get here? If it's a race on shut down of events then normally we wouldn't worry as that's just precise timing of disable vs this thread running.
Drop the check.
Quoted 11 lines
drivers/iio/adc/starfive-saradc.c
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +9
-0+mutex_unlock (&priv->lock); +return IRQ_HANDLED ; + } + + raw =readl (priv->base +SARADC_DATAX_REG_GET (ch)) &ADC_DAT_MSK ; + up =starfive_saradc_ch_upper_bound_get (priv, ch); + low =starfive_saradc_ch_lower_bound_get (priv, ch); + dev_err(priv->dev, + "channel %d is out of bounds. sample data: %dmv (range: %dmv~ %dmv)\n",
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c line 738
Quoted 2 lines
drivers/iio/adc/starfive-saradc.c new file +2
-0+ ch, (SARADC_RAW_TO_VDD_ADJ (raw) /1000 ), + priv->low_bounds[ch], priv->up_bounds[ch]);We have the IIO events infrastructure to report these to userspace. Use it or drop this.
dev_erris not a valid way to report this sort of threshold events.
Drop it.
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c
Quoted 3 lines
drivers/iio/adc/starfive-saradc.c new file +2
-0+ +starfive_saradc_ch_monitor_stop (priv, ch);Why stop it? Add a comment. We aren't interested in future events?
If we do not stop it and the input voltage remains constant, the ADC could continuously monitor the voltage and continuously trigger interrupts for the bound. As a result, the CPU utilization rate will be high.
Quoted 15 lines
drivers/iio/adc/starfive-saradc.c line 752
Quoted 12 lines
drivers/iio/adc/starfive-saradc.c new file +11
-0+pm_runtime_put_autosuspend (priv->dev); +mutex_unlock (&priv->lock); + +return IRQ_HANDLED ; +} + +static irqreturn_t starfive_saradc_irq_handler (int irq,void *data) { +struct starfive_saradc *priv = data; +u32 irq_err =readl (priv->base +SARADC_IRQ_EN_ST ); + +if (!priv->mon_working)How do we hit this condition? If it's a race thing then add a comment on what that race is. If it's not our interrupt then return
IRQ_NONE.
Drop it.
Quoted 16 lines
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+return IRQ_HANDLED ; + +/* Error of out of bounds */ +if (irq_err &BIT (priv->mon_ch)) {drivers/iio/adc/starfive-saradc.c
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+/* Clear the interrupt */ +writel (irq_err, priv->base +SARADC_IRQ_EN_ST ); + priv->err = true; +return IRQ_WAKE_THREAD ; + } + +return IRQ_HANDLED ; +} + +static void starfive_saradc_init (struct starfive_saradc *priv) {This will probably go away anyway but comments inline for future reference.
Noted.
Quoted 13 lines
drivers/iio/adc/starfive-saradc.c
Quoted 8 lines
drivers/iio/adc/starfive-saradc.c new file +7
-0+bool use_def = false; +u16 up, low, scan, tmp; +u32 upmv, lowmv; +int i; + +if (of_property_read_u16 (priv->dev->of_node,"upper-bound-mv" , +&tmp)) {Use
property.haccessors for new drivers, not theof_ones.
They are cleaner and maybe this driver will one day be used with other firmware types. Also I don't want of specific code in IIO drivers because it gets cut and paste into others where another firmware type is actually likely.
Noted.
Quoted 72 lines
drivers/iio/adc/starfive-saradc.c line 777
Quoted 6 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+ use_def = true; + }else { + up =SARADC_VDD_MV_TO_RAW (tmp); +if (up >ADC_UPPER_BOUND_DEF )If DT provide an invalid input fail to probe. We need that fixed.
If it was a missing property then current convention is to use a check on the property existing as a separate call before reading it. That provides neater error handling.Mind you as per he discussion in that dt-binding thread this is going away.
drivers/iio/adc/starfive-saradc.c
Quoted 13 lines
drivers/iio/adc/starfive-saradc.c new file +13
-0+ use_def = true; + else + upmv = tmp; + } + +if (use_def) { + up =ADC_UPPER_BOUND_DEF ; + upmv =SARADC_RAW_TO_VDD_ADJ (up); + use_def = false; + } + +if (of_property_read_u16 (priv->dev->of_node,"lower-bound-mv" , +&tmp)) {Same for all of these as commnets above.
Quoted 9 lines
drivers/iio/adc/starfive-saradc.c new file +9
-0+ use_def = true; + }else { + low =SARADC_VDD_MV_TO_RAW (tmp); +if (low >ADC_UPPER_BOUND_DEF ) + use_def = true; +else + lowmv = tmp; + } +drivers/iio/adc/starfive-saradc.c
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+if (use_def) { + low =ADC_LOWER_BOUND_DEF ; + lowmv =SARADC_RAW_TO_VDD_ADJ (low); + } + +if (of_property_read_u16 (priv->dev->of_node,"scan-freq" , &scan)) + scan =ADC_SCAN_FREQ_DEF ; + +if ((scan &ADC_SCAN_FREQ_MSK ) <ADC_SCAN_FREQ_MIN ) { + dev_warn(priv->dev, "The scan_freq is out of range and use thedefault value!\n");
Quoted 12 lines
drivers/iio/adc/starfive-saradc.c new file +12
-0+ scan =ADC_SCAN_FREQ_DEF ; + } + +starfive_saradc_scan_freq_set (priv, scan); + +for (i =0 ; i <SARADC_MAX_CHANNELS ; i++) { +starfive_saradc_ch_upper_bound_set (priv, i, up); +starfive_saradc_ch_lower_bound_set (priv, i, low); + priv->up_bounds[i] = upmv; + priv->low_bounds[i] = lowmv; + } +}drivers/iio/adc/starfive-saradc.c line 831
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+ +static int starfive_saradc_reg_access (struct iio_dev *indio_dev,unsigned int reg, +unsigned int writeval,unsigned int *readval) { +struct starfive_saradc *priv =iio_priv (indio_dev); +int ret =0 ; + +if (reg %4 || reg >SARADC_SCAN_FREQ ) +return -EINVAL ; + + ret =pm_runtime_get_sync (priv->dev);Look at
PM_RUNTIME_ACQUIRE_AUTOSUSPEND()
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c
Quoted 2 lines
drivers/iio/adc/starfive-saradc.c new file +2
-0+if (ret <0 ) { +pm_runtime_put_noidle (priv->dev);Why? It failed so we should not have a refcount to drop.
Will fix.
Quoted 6 lines
drivers/iio/adc/starfive-saradc.c line 726
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+return ret; + } + +mutex_lock (&priv->lock);In combination with the acquire mentioned above, use a
guard()for the mutex.
Noted
Quoted 29 lines
drivers/iio/adc/starfive-saradc.c
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c new file +5
-0+ +if (readval) + *readval =readl (priv->base + reg); +else if (reg <SARADC_ULB_CH0 ) +/* Allowed to write from SARADC_ULB_CH0 to
SARADC_SCAN_FREQ*/drivers/iio/adc/starfive-saradc.c
Quoted 1 line
drivers/iio/adc/starfive-saradc.c new file +1
-0+ ret = -EINVAL ;With the changes above you can just return here.
Quoted 8 lines
drivers/iio/adc/starfive-saradc.c new file +8
-0+else +writel (writeval, priv->base + reg); + +mutex_unlock (&priv->lock); +pm_runtime_put_sync_autosuspend (priv->dev); + +return ret; +}drivers/iio/adc/starfive-saradc.c line 863
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+ +static const struct iio_info starfive_saradc_iio_info = { + .read_raw = starfive_saradc_read_raw, + .debugfs_reg_access = starfive_saradc_reg_access, + .attrs = &starfive_saradc_attr_group, }; + +static int starfive_saradc_probe (struct platform_device *pdev) { +struct starfive_saradc *priv; +struct iio_dev *indio_dev; +int irq, ret;Worth using a local struct device.
e.g.
Quoted 2 lines
struct device *dev = &pdev->dev;just to shorten the various lines where it's used.
Noted.
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+ + indio_dev =devm_iio_device_alloc (&pdev->dev,sizeof (*priv)); +if (!indio_dev) + return dev_err_probe(&pdev->dev, -ENOMEM, "failed allocatingiio
Quoted 1 line
+device\n");
This print is a noop for
-ENOMEMso don't bother having it.if (!indio_dev) return -ENOMEM;
Noted.
Quoted 17 lines
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +2
-0+ + priv =iio_priv (indio_dev);drivers/iio/adc/starfive-saradc.c line 879
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+platform_set_drvdata (pdev, indio_dev); + priv->dev = &pdev->dev; + priv->base =devm_platform_ioremap_resource (pdev,0 ); +if (IS_ERR (priv->base)) +return PTR_ERR (priv->base); + + irq =platform_get_irq (pdev,0 ); +if (irq <0 ) +return dev_err_probe (&pdev->dev, irq, +"failed to get irq\n" );One line. Note that going slightly over 80 chars is fine if it helps readability. I think this one is just under 80 anyway.
Noted.
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c line 884
Quoted 6 lines
drivers/iio/adc/starfive-saradc.c new file +5
-0+ + ret = devm_request_threaded_irq(&pdev->dev, irq, + starfive_saradc_irq_handler, + starfive_saradc_mon_stop_threadfn, + IRQF_TRIGGER_HIGH | IRQF_ONESHOT,Trigger direction should come from DT. So don't set it here.
We do this historically in some drivers and can't fix it now because some board might rely on it. Don't introduce it in a new driver.
OK, I will remove it.
Quoted 14 lines
drivers/iio/adc/starfive-saradc.c line 893
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +9
-0+ dev_name(&pdev->dev), priv); +if (ret) +return dev_err_probe (&pdev->dev, ret, +"failed to request irq handler\n" ); + + priv->clk =devm_clk_get_enabled (&pdev->dev,NULL ); +if (IS_ERR (priv->clk)) +return dev_err_probe (&pdev->dev,PTR_ERR (priv->clk), +"failed to get clock\n" );As sashiko pointed out this might result in a clock underflow. Fixing that the mess of devm + runtime pm and working out general patterns is still a work in progress. For now maybe just check if you get such a warning.
Noted.
Quoted 27 lines
Quoted 27 lines
drivers/iio/adc/starfive-saradc.c new file +25
-0+ + priv->rst =devm_reset_control_array_get_shared (&pdev->dev); +if (IS_ERR (priv->rst)) +return dev_err_probe (&pdev->dev,PTR_ERR (priv->rst), +"failed to get resets\n" ); + + ret =reset_control_deassert (priv->rst); +if (ret) +return dev_err_probe (&pdev->dev, ret, +"failed to deassert reset\n" ); + + indio_dev->name =dev_name (&pdev->dev); + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->info = &starfive_saradc_iio_info; + indio_dev->channels = starfive_saradc_iio_channels; + indio_dev->num_channels =ARRAY_SIZE (starfive_saradc_iio_channels); + +starfive_saradc_init (priv); +mutex_init (&priv->lock); + +pm_runtime_set_active (&pdev->dev); +pm_runtime_set_autosuspend_delay (&pdev->dev,50 ); +pm_runtime_use_autosuspend (&pdev->dev); +pm_runtime_enable (&pdev->dev); +
Quoted 8 lines
drivers/iio/adc/starfive-saradc.c line 925
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c new file +5
-0+return devm_iio_device_register (&pdev->dev, indio_dev); } + +static void starfive_saradc_remove (struct platform_device *pdev) { +pm_runtime_disable (&pdev->dev); +pm_runtime_dont_use_autosuspend (&pdev->dev);This is out of order wrt to the setup in probe. I suspect you can use
devm_pm_runtime_enable()instead and drop remove entirely.
Noted.
Quoted 12 lines
drivers/iio/adc/starfive-saradc.c line 933
Quoted 8 lines
drivers/iio/adc/starfive-saradc.c new file +7
-0+} + +static int starfive_saradc_runtime_suspend (struct device *dev) { +struct iio_dev *indio_dev =dev_get_drvdata (dev); +struct starfive_saradc *priv =iio_priv (indio_dev); + +starfive_saradc_pwr_on (priv, false);Having a function called
pwr_on()is missleading given you use it to turn the power off. Rename it simplypwr()Or consider if it is worth having the helper at all as it's very small and maybe having the code inline is simpler.
Got it.
Quoted 16 lines
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +2
-0+clk_disable_unprepare (priv->clk); +drivers/iio/adc/starfive-saradc.c line 946
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c new file +10
-0+return 0 ; +} + +static int starfive_saradc_runtime_resume (struct device *dev) { +struct iio_dev *indio_dev =dev_get_drvdata (dev); +struct starfive_saradc *priv =iio_priv (indio_dev); +int ret =clk_prepare_enable (priv->clk); + +if (ret) +return ret;slight preference for
Quoted 7 lines
int ret; ret = clk_prepare_enable(priv->clk); if (ret) return ret;as it keeps the set of ret and it's check right next to each other.
Noted.
Quoted 10 lines
drivers/iio/adc/starfive-saradc.c line 950
Quoted 4 lines
drivers/iio/adc/starfive-saradc.c new file +4
-0+ +starfive_saradc_pwr_on (priv, true); +/* Need time to completely power on. */ +msleep (20 );
fsleep()preferred. Takes time in usecs but the point is it will use the generally right call for whatever the sleep is. That means readers don't have to think about it. See the docs for the implementation offsleep()for more info on how it decides whether to call msleep or not.
Quoted 2 lines
If possible reference a datasheet section / table or similar for this sleep length. That is helpful if turns out to be too short on some device in the future.
Got it.
Quoted 18 lines
Quoted 18 lines
drivers/iio/adc/starfive-saradc.c new file +17
-0+ +return 0 ; +} + +static DEFINE_RUNTIME_DEV_PM_OPS(starfive_saradc_pm_ops, + starfive_saradc_runtime_suspend, + starfive_saradc_runtime_resume, NULL); + +static const struct of_device_id starfive_saradc_of_match[] = { + { .compatible ="starfive,jhb100-saradc" , }, + { } +}; + +static struct platform_driver starfive_saradc_driver = { + .probe = starfive_saradc_probe, + .remove = starfive_saradc_remove, + .driver = {
Quoted 8 lines
drivers/iio/adc/starfive-saradc.c line 972
Quoted 5 lines
drivers/iio/adc/starfive-saradc.c new file +5
-0+ .name = "starfive_saradc", + .of_match_table = starfive_saradc_of_match, + .pm =pm_ptr (&starfive_saradc_pm_ops) + }, +};common practice to not have a blank line here. Dropping it makes a clear association between the struct
platform_driverand the macro that uses it.
Noted.
Quoted 7 lines
drivers/iio/adc/starfive-saradc.c
Quoted 7 lines
drivers/iio/adc/starfive-saradc.c new file +6
-0+ +module_platform_driver (starfive_saradc_driver); + +MODULE_AUTHOR ("Xingyu Wu <[email protected]>" ); +MODULE_DESCRIPTION ("StarFive Successive Approximation Register ADC +driver"); MODULE_LICENSE("GPL");
Thanks.
Best regards,
Xingyu Wu
Jonathan Cameron
maintainerRe: [PATCH v1 2/2] iio: adc: Add StarFive SAR-ADC driver
Quoted 9 lines
Quoted 6 lines
Review
Quoted 4 lines
(quoted diff) +2
-0+ + starfive_saradc_ch_monitor_stop(priv, ch);Why stop it? Add a comment. We aren't interested in future events?
If we do not stop it and the input voltage remains constant, the ADC could continuously monitor the voltage and continuously trigger interrupts for the bound. As a result, the CPU utilization rate will be high.
Can you use an edge interrupt instead of a level one?
If not, then a common solution is to disable for a period (maybe a second or so) then reenable. There are various more refined ways of doing this.
Basically we don't want to be in a situation where a momentary blip disables the event and we miss a later condition that must be handled.
Thanks,
Jonathan
RE: [PATCH v1 2/2] iio: adc: Add StarFive SAR-ADC driver
Quoted 8 lines · Jonathan Cameron
Quoted 8 lines
Quoted 7 lines
Review
Quoted 5 lines
(quoted diff) +2
-0+ + starfive_saradc_ch_monitor_stop(priv, ch);Why stop it? Add a comment. We aren't interested in future events?
Quoted 6 lines
Quoted 1 line
If we do not stop it and the input voltage remains constant, the ADC could
continuously monitor the voltage and continuously trigger interrupts for the bound.
Quoted 1 line
As a result, the CPU utilization rate will be high.
Can you use an edge interrupt instead of a level one?
No, it is a level interrupt in hardware.
Quoted 3 lines
If not, then a common solution is to disable for a period (maybe a second or so) then reenable. There are various more refined ways of doing this.
This is good idea. How about using timer to disable it?
Quoted 4 lines
Basically we don't want to be in a situation where a momentary blip disables the event and we miss a later condition that must be handled.
Noted.
Best regards,
Xingyu Wu
Jonathan Cameron
maintainerRe: [PATCH v1 2/2] iio: adc: Add StarFive SAR-ADC driver
Quoted 9 lines · On Fri, 22 May 2026 02:20:53 +0000 Xingyu Wu <[email protected]> wrote:
Quoted 8 lines · Jonathan Cameron
Quoted 8 lines
Quoted 7 lines
Review
Quoted 5 lines
(quoted diff) +2
-0+ + starfive_saradc_ch_monitor_stop(priv, ch);Why stop it? Add a comment. We aren't interested in future events?
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
If we do not stop it and the input voltage remains constant, the ADC could
continuously monitor the voltage and continuously trigger interrupts for the bound.
Quoted 1 line
As a result, the CPU utilization rate will be high.
Can you use an edge interrupt instead of a level one?
No, it is a level interrupt in hardware.
Just to check. That's a restriction of the interrupt controller IP in that it doesn't support edge interrupts on this particularly line? Seems unfortunate given the block on the other end is providing something that seems like an edge :( Ah well - hardly the first time we've hit that. Add some comments to the code when you add the timer approach to re enabling.
Quoted 6 lines
Quoted 4 lines
If not, then a common solution is to disable for a period (maybe a second or so) then reenable. There are various more refined ways of doing this.
This is good idea. How about using timer to disable it?
yes, there are examples in tree doing that.
J
Quoted 10 lines
Quoted 5 lines
Basically we don't want to be in a situation where a momentary blip disables the event and we miss a later condition that must be handled.
Noted.
Best regards,
Xingyu Wu
Jonathan Cameron
maintainerQuoted 5 lines · On Mon, 18 May 2026 16:18:50 +0800 Xingyu Wu <[email protected]> wrote:
Hi all,
This series adds an IIO ADC driver for the controller of StarFive Successive Approximation Register A/D converter (SAR-ADC).
Quoted 7 lines
The StarFive SAR-ADC is a 12-bit converter with up to 8 input channels and a fixed 1.8V reference domain. The driver provides raw and processed voltage readouts via IIO, runtime PM support, and threshold-based voltage monitoring.
Tested on StarFive JHB100 EVB with all ADC channels and monitor interrupt path.
Take a look at the AI bot feedback.
https://sashiko.dev/#/patchset/20260518081852.116909-1-xingyu.wu%40starfivetech.com
It won't necessarily all be correct but the hit rate tends to be good.
Jonathan
Quoted 14 lines
Xingyu Wu (2):
bindings: iio: adc: Add StarFive JHB100 SARADC
iio: adc: Add StarFive SAR-ADC driver.../iio/adc/starfive,jhb100-saradc.yaml | 62 ++ MAINTAINERS | 6 + drivers/iio/adc/Kconfig | 11 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/starfive-saradc.c | 978 ++++++++++++++++++ 5 files changed, 1058 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/starfive,jhb100-saradc.yaml create mode 100644 drivers/iio/adc/starfive-saradc.c