Skip to contentSkip to conversation

iio: ad74413r: allow configuring digital input threshold

PATCH In review Rasmus Villemoes 6 participants 10 messages Jun 23, 2023 – Jul 2, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover iio: ad74413r: allow configuring digital input threshold

The reset default value of the DIN_THRESH register is 0x0, meaning that the threshold for the digital input channels is 1/60 of AVDD. In most applications, that value is way too low and susceptible to noise.

These patches introduce a new DT property, digital-input-threshold-microvolt, which if present will be used as the threshold in "16V" mode, i.e. as an absolute threshold, not proportional to AVDD.

If someone needs the threshold to be proportional to AVDD, but being say 15/60, another DT property (mutually exclusive with this one) could be introduced. But since I don't need that and can't come up with a good name ('digital-input-threshold-60ths-avdd' ?) I punt that problem to whoever needs it.

Rasmus Villemoes (2):
dt-bindings: iio: ad74413r: add binding for digital input threshold
iio: addac: ad74413r: wire up digital-input-threshold-microvolt DT
property

 .../bindings/iio/addac/adi,ad74413r.yaml      | 10 ++++++++++
 drivers/iio/addac/ad74413r.c                  | 20 +++++++++++++++++++
 2 files changed, 30 insertions(+)
Signature · 1 line
2.37.2
#1 lore raw

PATCH 1/2 dt-bindings: iio: ad74413r: add binding for digital input threshold

Allow specifying the threshold for which the channels configured as digital input change state.

---

Running dt_binding_check on this with a too small or large value in the example does give me an error, but the multipleOf does not seem to be enforced; the value 1234567 is not flagged. I don't know if that's expected (maybe I have too old versions of something).

 .../devicetree/bindings/iio/addac/adi,ad74413r.yaml    | 10 ++++++++++
 1 file changed, 10 insertions(+)
Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml +10 -0
index 590ea7936ad7..1f90ce3c7932 100644
@@ -51,6 +51,14 @@ properties:
       Shunt (sense) resistor value in micro-Ohms.
     default: 100000000
 
+  digital-input-threshold-microvolt:
+    description:
+      Comparator threshold used by the channels configured to use the
+      digital input function.
+    minimum: 500000
+    maximum: 16000000
+    multipleOf: 500000
+
   reset-gpios:
     maxItems: 1
 
@@ -143,6 +151,8 @@ examples:
         refin-supply = <&ad74413r_refin>;
         reset-gpios = <&gpio2 6 GPIO_ACTIVE_LOW>;
 
+        digital-input-threshold-microvolt = <4000000>;
+
         channel@0 {
           reg = <0>;
 
Signature · 1 line
2.37.2
#2 lore raw

Re: [PATCH 1/2] dt-bindings: iio: ad74413r: add binding for digital input threshold

Quoted 10 lines · Rasmus Villemoes

Allow specifying the threshold for which the channels configured as digital input change state.

---

Running dt_binding_check on this with a too small or large value in the example does give me an error, but the multipleOf does not seem to be enforced; the value 1234567 is not flagged. I don't know if that's expected (maybe I have too old versions of something).

That's one for Rob. I checked a few others and behaviour was the same there.

Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml properties: · line 54

Quoted 12 lines
 .../devicetree/bindings/iio/addac/adi,ad74413r.yaml    | 10 ++++++++++
 1 file changed, 10 insertions(+)
Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml +1 -0
index 590ea7936ad7..1f90ce3c7932 100644
@@ -51,6 +51,14 @@ properties:
       Shunt (sense) resistor value in micro-Ohms.
     default: 100000000
 
+  digital-input-threshold-microvolt:

Should this not have an adi vendor prefix, similar to "adi,digital-input-threshold-mode-fixed"?

Cheers,
Conor.

Quoted 22 lines
Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml +9 -0
+    description:
+      Comparator threshold used by the channels configured to use the
+      digital input function.
+    minimum: 500000
+    maximum: 16000000
+    multipleOf: 500000
+
   reset-gpios:
     maxItems: 1
 
@@ -143,6 +151,8 @@ examples:
         refin-supply = <&ad74413r_refin>;
         reset-gpios = <&gpio2 6 GPIO_ACTIVE_LOW>;
 
+        digital-input-threshold-microvolt = <4000000>;
+
         channel@0 {
           reg = <0>;
 
Signature · 1 line
2.37.2
maintainer
#3 lore raw

Re: [PATCH 1/2] dt-bindings: iio: ad74413r: add binding for digital input threshold

Quoted 26 lines · Conor Dooley
Quoted 10 lines · Rasmus Villemoes

Allow specifying the threshold for which the channels configured as digital input change state.

---

Running dt_binding_check on this with a too small or large value in the example does give me an error, but the multipleOf does not seem to be enforced; the value 1234567 is not flagged. I don't know if that's expected (maybe I have too old versions of something).

That's one for Rob. I checked a few others and behaviour was the same there.

Quoted 11 lines
 .../devicetree/bindings/iio/addac/adi,ad74413r.yaml    | 10 ++++++++++
 1 file changed, 10 insertions(+)
Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml +0 -0
index 590ea7936ad7..1f90ce3c7932 100644
@@ -51,6 +51,14 @@ properties:
       Shunt (sense) resistor value in micro-Ohms.
     default: 100000000
 
Quoted 4 lines

Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml properties: · line 54

Quoted 1 line
Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml +1 -0
+  digital-input-threshold-microvolt:

Should this not have an adi vendor prefix, similar to "adi,digital-input-threshold-mode-fixed"?

Yes.

Rob

#4 lore raw

Re: [PATCH 1/2] dt-bindings: iio: ad74413r: add binding for digital input threshold

Quoted 10 lines · Rob Herring
Quoted 9 lines · Conor Dooley
Quoted 8 lines · Rasmus Villemoes
Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml +0 -0
index 590ea7936ad7..1f90ce3c7932 100644
@@ -51,6 +51,14 @@ properties:
       Shunt (sense) resistor value in micro-Ohms.
     default: 100000000
 
Quoted 6 lines
Quoted 4 lines

Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml properties: · line 54

Quoted 1 line
Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml +1 -0
+  digital-input-threshold-microvolt:

Should this not have an adi vendor prefix, similar to "adi,digital-input-threshold-mode-fixed"?

Yes.

OK. But I'm not really sure what the rules are for when such a prefix must be added, so some guidance would be appreciated. There's

- DO use a vendor prefix on device specific property names. Consider if properties could be common among devices of the same class.

And my thinking was that a threshold for when a digital input should count as high/low would be a rather generic thing, so not particularly device specific.

Also, this very binding has a shunt-resistor-micro-ohms, and the individual channels have a drive-strength-microamp (granted, that latter one is a recent one of mine and may have slipped through review?). I can certainly understand that when a property specifies a raw value to put into some register (or field), that's very specific to that chip (or small family of chips) - the adi,ch-func properties fall into that category.

Rasmus

#5 lore raw

Re: [PATCH 1/2] dt-bindings: iio: ad74413r: add binding for digital input threshold

Quoted 21 lines · Rasmus Villemoes
Quoted 10 lines · Rob Herring
Quoted 9 lines · Conor Dooley
Quoted 8 lines · Rasmus Villemoes
Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml +0 -0
index 590ea7936ad7..1f90ce3c7932 100644
@@ -51,6 +51,14 @@ properties:
       Shunt (sense) resistor value in micro-Ohms.
     default: 100000000
 
Quoted 6 lines
Quoted 4 lines

Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml properties: · line 54

Quoted 1 line
Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml +1 -0
+  digital-input-threshold-microvolt:

Should this not have an adi vendor prefix, similar to "adi,digital-input-threshold-mode-fixed"?

Yes.

OK. But I'm not really sure what the rules are for when such a prefix must be added, so some guidance would be appreciated. There's

Quoted 6 lines

- DO use a vendor prefix on device specific property names. Consider if properties could be common among devices of the same class.

And my thinking was that a threshold for when a digital input should count as high/low would be a rather generic thing, so not particularly device specific.

Then find some more users of it.

Quoted 7 lines

Also, this very binding has a shunt-resistor-micro-ohms, and the individual channels have a drive-strength-microamp (granted, that latter one is a recent one of mine and may have slipped through review?). I can certainly understand that when a property specifies a raw value to put into some register (or field), that's very specific to that chip (or small family of chips) - the adi,ch-func properties fall into that category.

Best regards,
Krzysztof

#6 lore raw

Re: [PATCH 1/2] dt-bindings: iio: ad74413r: add binding for digital input threshold

Quoted 22 lines · On Mon, 26 Jun 2023 10:29:22 +0200 Krzysztof Kozlowski…
Quoted 21 lines · Rasmus Villemoes
Quoted 10 lines · Rob Herring
Quoted 9 lines · Conor Dooley
Quoted 8 lines · Rasmus Villemoes
Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml +0 -0
index 590ea7936ad7..1f90ce3c7932 100644
@@ -51,6 +51,14 @@ properties:
       Shunt (sense) resistor value in micro-Ohms.
     default: 100000000
 
Quoted 6 lines
Quoted 4 lines

Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml properties: · line 54

Quoted 1 line
Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml +1 -0
+  digital-input-threshold-microvolt:  

Should this not have an adi vendor prefix, similar to "adi,digital-input-threshold-mode-fixed"?

Yes.

OK. But I'm not really sure what the rules are for when such a prefix must be added, so some guidance would be appreciated. There's

Quoted 8 lines
Quoted 6 lines

- DO use a vendor prefix on device specific property names. Consider if properties could be common among devices of the same class.

And my thinking was that a threshold for when a digital input should count as high/low would be a rather generic thing, so not particularly device specific.

Then find some more users of it.

The hi8435 could make use of this, but it currently doesn't get these thresholds from DT (despite there being a reasonable argument that these should be characteristics of the board wiring etc) but rather from userspace controls.

Might well be something in gpio drivers? Linus / Bartosz, any of the input gpio devices really threshold detectors? If so is there any precedence for a DT binding to set the threshold?

Quoted 12 lines
Quoted 8 lines

Also, this very binding has a shunt-resistor-micro-ohms, and the individual channels have a drive-strength-microamp (granted, that latter one is a recent one of mine and may have slipped through review?). I can certainly understand that when a property specifies a raw value to put into some register (or field), that's very specific to that chip (or small family of chips) - the adi,ch-func properties fall into that category.

Best regards,
Krzysztof

maintainer
#7 lore raw

Re: [PATCH 1/2] dt-bindings: iio: ad74413r: add binding for digital input threshold

In reply to Rasmus Villemoes
Quoted 10 lines · Rasmus Villemoes

Allow specifying the threshold for which the channels configured as digital input change state.

---

Running dt_binding_check on this with a too small or large value in the example does give me an error, but the multipleOf does not seem to be enforced; the value 1234567 is not flagged. I don't know if that's expected (maybe I have too old versions of something).

Thanks for the report. Indeed, 'multipleOf' was not handled correctly. I'll push a fix to dtschema shortly.

Rob

#8 lore raw

PATCH 2/2 iio: addac: ad74413r: wire up digital-input-threshold-microvolt DT property

In reply to Rasmus Villemoes

The threshold is common to all channels configured as digital input.

So far, we have not seen the settings in the DIN_THRESH affecting functions other than digital input, but with a4cba07e64e6 ("iio:
addac: ad74413: don't set DIN_SINK for functions other than digital input") in mind, do not read and apply the digital-input-threshold-microvolt setting unless at least one channel has been configured as one of the digital input variants.

---
 drivers/iio/addac/ad74413r.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/iio/addac/ad74413r.c +20 -0
index e3366cf5eb31..efdd7fdd7ab9 100644
@@ -129,6 +129,12 @@ struct ad74413r_state {
 #define AD74413R_GPO_CONFIG_COMPARATOR		0b011
 #define AD74413R_GPO_CONFIG_HIGH_IMPEDANCE	0b100
 
+#define AD74413R_REG_DIN_THRESH		0x22
+#define AD74413R_DIN_COMP_THRESH_MASK	GENMASK(5, 1)
+#define AD74413R_DIN_THRESH_MODE	BIT(0)
+#define AD74413R_DIN_THRESH_MODE_16V	BIT(0)
+#define AD74413R_DIN_THRESH_MODE_AVDD	0
+
 #define AD74413R_REG_ADC_CONV_CTRL	0x23
 #define AD74413R_CONV_SEQ_MASK		GENMASK(9, 8)
 #define AD74413R_CONV_SEQ_ON		0b00
@@ -1446,6 +1452,20 @@ static int ad74413r_probe(struct spi_device *spi)
 	}
 
 	if (st->num_comparator_gpios) {
+		u32 thresh, val, mask;
+
+		if (!device_property_read_u32(st->dev, "digital-input-threshold-microvolt",
+					      &thresh)) {
+			val = thresh/500000 - 1;
+			val = FIELD_PREP(AD74413R_DIN_COMP_THRESH_MASK, val);
+			val |= AD74413R_DIN_THRESH_MODE_16V;
+			mask = AD74413R_DIN_COMP_THRESH_MASK | AD74413R_DIN_THRESH_MODE;
+			ret = regmap_update_bits(st->regmap, AD74413R_REG_DIN_THRESH,
+						 mask, val);
+			if (ret)
+				return ret;
+		}
+
 		st->comp_gpiochip.owner = THIS_MODULE;
 		st->comp_gpiochip.label = st->chip_info->name;
 		st->comp_gpiochip.base = -1;
Signature · 1 line
2.37.2
maintainer
#9 lore raw

Re: [PATCH 2/2] iio: addac: ad74413r: wire up digital-input-threshold-microvolt DT property

Quoted 11 lines · On Fri, 23 Jun 2023 13:33:26 +0200 Rasmus Villemoes <[email protected]>…

The threshold is common to all channels configured as digital input.

So far, we have not seen the settings in the DIN_THRESH affecting functions other than digital input, but with a4cba07e64e6 ("iio:
addac: ad74413: don't set DIN_SINK for functions other than digital input") in mind, do not read and apply the digital-input-threshold-microvolt setting unless at least one channel has been configured as one of the digital input variants.

LGTM. Will pick up once the minor change in binding is done if no other feedback comes in.

Thanks,

Jonathan

Quoted 42 lines
---
 drivers/iio/addac/ad74413r.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/iio/addac/ad74413r.c +20 -0
index e3366cf5eb31..efdd7fdd7ab9 100644
@@ -129,6 +129,12 @@ struct ad74413r_state {
 #define AD74413R_GPO_CONFIG_COMPARATOR		0b011
 #define AD74413R_GPO_CONFIG_HIGH_IMPEDANCE	0b100
 
+#define AD74413R_REG_DIN_THRESH		0x22
+#define AD74413R_DIN_COMP_THRESH_MASK	GENMASK(5, 1)
+#define AD74413R_DIN_THRESH_MODE	BIT(0)
+#define AD74413R_DIN_THRESH_MODE_16V	BIT(0)
+#define AD74413R_DIN_THRESH_MODE_AVDD	0
+
 #define AD74413R_REG_ADC_CONV_CTRL	0x23
 #define AD74413R_CONV_SEQ_MASK		GENMASK(9, 8)
 #define AD74413R_CONV_SEQ_ON		0b00
@@ -1446,6 +1452,20 @@ static int ad74413r_probe(struct spi_device *spi)
 	}
 
 	if (st->num_comparator_gpios) {
+		u32 thresh, val, mask;
+
+		if (!device_property_read_u32(st->dev, "digital-input-threshold-microvolt",
+					      &thresh)) {
+			val = thresh/500000 - 1;
+			val = FIELD_PREP(AD74413R_DIN_COMP_THRESH_MASK, val);
+			val |= AD74413R_DIN_THRESH_MODE_16V;
+			mask = AD74413R_DIN_COMP_THRESH_MASK | AD74413R_DIN_THRESH_MODE;
+			ret = regmap_update_bits(st->regmap, AD74413R_REG_DIN_THRESH,
+						 mask, val);
+			if (ret)
+				return ret;
+		}
+
 		st->comp_gpiochip.owner = THIS_MODULE;
 		st->comp_gpiochip.label = st->chip_info->name;
 		st->comp_gpiochip.base = -1;