Skip to contentSkip to conversation

pinctrl: sunxi: Use minimal debouncing period as default

PATCH In review Paul Kocialkowski 3 participants 16 messages Nov 19, 2024 – Jan 7, 2025

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

PATCH 1/1 pinctrl: sunxi: Use minimal debouncing period as default

From: Paul Kocialkowski <[email protected]>

The sunxi external interrupts (available from GPIO pins) come with a built-in debouncing mechanism that cannot be disabled. It can be configured to use either the low-frequency oscillator (32 KHz) or the high-frequency oscillator (24 MHz), with a pre-scaler.

The pinctrl code supports an input-debounce device-tree property to set a specific debouncing period and choose which clock source is most relevant. However the property is specified in microseconds, which is longer than the minimal period achievable from the high-frequency oscillator without a pre-scaler.

When the property is missing, the reset configuration is kept, which selects the low-frequency oscillator without pre-scaling. This severely limits the possible interrupt periods that can be detected.

Instead of keeping this default, use the minimal debouncing period from the high-frequency oscillator without a pre-scaler to allow the largest possible range of interrupt periods.

This issue was encountered with a peripheral that generates active-low interrupts for 1 us. No interrupt was detected with the default setup, while it is now correctly detected with this change.

---
 drivers/pinctrl/sunxi/pinctrl-sunxi.c | 49 ++++++++++++++++-----------
 1 file changed, 29 insertions(+), 20 deletions(-)
drivers/pinctrl/sunxi/pinctrl-sunxi.c +29 -20
index 73bcf806af0e..06c650d97645 100644
@@ -1416,6 +1416,7 @@ static int sunxi_pinctrl_setup_debounce(struct sunxi_pinctrl *pctl,
 	unsigned int hosc_diff, losc_diff;
 	unsigned int hosc_div, losc_div;
 	struct clk *hosc, *losc;
+	bool debounce_minimal = false;
 	u8 div, src;
 	int i, ret;
 
@@ -1423,9 +1424,9 @@ static int sunxi_pinctrl_setup_debounce(struct sunxi_pinctrl *pctl,
 	if (of_clk_get_parent_count(node) != 3)
 		return 0;
 
-	/* If we don't have any setup, bail out */
+	/* If we don't have any setup, use minimal debouncing. */
 	if (!of_property_present(node, "input-debounce"))
-		return 0;
+		debounce_minimal = true;
 
 	losc = devm_clk_get(pctl->dev, "losc");
 	if (IS_ERR(losc))
@@ -1439,29 +1440,37 @@ static int sunxi_pinctrl_setup_debounce(struct sunxi_pinctrl *pctl,
 		unsigned long debounce_freq;
 		u32 debounce;
 
-		ret = of_property_read_u32_index(node, "input-debounce",
-						 i, &debounce);
-		if (ret)
-			return ret;
+		if (!debounce_minimal) {
+			ret = of_property_read_u32_index(node, "input-debounce",
+							 i, &debounce);
+			if (ret)
+				return ret;
 
-		if (!debounce)
-			continue;
+			if (!debounce)
+				continue;
 
-		debounce_freq = DIV_ROUND_CLOSEST(USEC_PER_SEC, debounce);
-		losc_div = sunxi_pinctrl_get_debounce_div(losc,
-							  debounce_freq,
-							  &losc_diff);
+			debounce_freq = DIV_ROUND_CLOSEST(USEC_PER_SEC,
+							  debounce);
 
-		hosc_div = sunxi_pinctrl_get_debounce_div(hosc,
-							  debounce_freq,
-							  &hosc_diff);
+			losc_div = sunxi_pinctrl_get_debounce_div(losc,
+								  debounce_freq,
+								  &losc_diff);
 
-		if (hosc_diff < losc_diff) {
-			div = hosc_div;
-			src = 1;
+			hosc_div = sunxi_pinctrl_get_debounce_div(hosc,
+								  debounce_freq,
+								  &hosc_diff);
+
+			if (hosc_diff < losc_diff) {
+				div = hosc_div;
+				src = 1;
+			} else {
+				div = losc_div;
+				src = 0;
+			}
 		} else {
-			div = losc_div;
-			src = 0;
+			/* Achieve minimal debouncing using undivided hosc. */
+			div = 0;
+			src = 1;
 		}
 
 		writel(src | div << 4,
Signature · 1 line
2.47.0
#1 lore raw
Quoted 7 lines · Paul Kocialkowski

From: Paul Kocialkowski <[email protected]>

The sunxi external interrupts (available from GPIO pins) come with a built-in debouncing mechanism that cannot be disabled. It can be configured to use either the low-frequency oscillator (32 KHz) or the high-frequency oscillator (24 MHz), with a pre-scaler.

Quoted 5 lines

The pinctrl code supports an input-debounce device-tree property to set a specific debouncing period and choose which clock source is most relevant. However the property is specified in microseconds, which is longer than the minimal period achievable from the high-frequency oscillator without a pre-scaler.

That can be fixed by introducing a new property with a ns resolution.

Quoted 11 lines

When the property is missing, the reset configuration is kept, which selects the low-frequency oscillator without pre-scaling. This severely limits the possible interrupt periods that can be detected.

Instead of keeping this default, use the minimal debouncing period from the high-frequency oscillator without a pre-scaler to allow the largest possible range of interrupt periods.

This issue was encountered with a peripheral that generates active-low interrupts for 1 us. No interrupt was detected with the default setup, while it is now correctly detected with this change.

I don't think it's wise. If the debouncing is kept as is, the worst case scenario is the one you had: a device doesn't work, you change it, everything works.

If we set it up as fast as it can however, then our risk becomes thousands of spurious interrupts, which is much more detrimental to the system.

And that's without accounting the fact that devices might have relied on that default for years

Maxime

#2 lore raw

Hi Maxime,

Quoted 8 lines · Maxime Ripard
Quoted 7 lines · Paul Kocialkowski

From: Paul Kocialkowski <[email protected]>

The sunxi external interrupts (available from GPIO pins) come with a built-in debouncing mechanism that cannot be disabled. It can be configured to use either the low-frequency oscillator (32 KHz) or the high-frequency oscillator (24 MHz), with a pre-scaler.

Quoted 7 lines
Quoted 5 lines

The pinctrl code supports an input-debounce device-tree property to set a specific debouncing period and choose which clock source is most relevant. However the property is specified in microseconds, which is longer than the minimal period achievable from the high-frequency oscillator without a pre-scaler.

That can be fixed by introducing a new property with a ns resolution.

Sure but my point here is rather about what should be default behavior.

The issue I had will remain unsolved by default even with a new property, since people will still need to patch their device-tree to apply it.

Quoted 8 lines
Quoted 8 lines

When the property is missing, the reset configuration is kept, which selects the low-frequency oscillator without pre-scaling. This severely limits the possible interrupt periods that can be detected.

Instead of keeping this default, use the minimal debouncing period from the high-frequency oscillator without a pre-scaler to allow the largest possible range of interrupt periods.

Quoted 7 lines
Quoted 3 lines

This issue was encountered with a peripheral that generates active-low interrupts for 1 us. No interrupt was detected with the default setup, while it is now correctly detected with this change.

I don't think it's wise. If the debouncing is kept as is, the worst case scenario is the one you had: a device doesn't work, you change it, everything works.

I think this worst-case scenario is very bad and not what people will expect. In addition it is difficult to debug the issue without specific knowledge about the SoC.

My use-case here was hooking up a sparkfun sensor board by the way, not some very advanced corner-case.

Quoted 3 lines

If we set it up as fast as it can however, then our risk becomes thousands of spurious interrupts, which is much more detrimental to the system.

Keep in mind that this only concerns external GPIO-based interrupts, which have to be explicitely hooked to a device. If a device or circuit does generate such spurious interrupts, I think it makes sense that it would be reflected by default.

Also the notion of spurious interrupt is pretty vague. Having lots of interrupts happening may be the desired behavior in many cases.

In any case I don't think it makes sense for the platform code to impose what a reasonable period for interrupts is (especially with such a large period as default). Some drivers also have mechanisms to detect spurious interrupts based on their specific use case.

Quoted 2 lines

And that's without accounting the fact that devices might have relied on that default for years

They definitely shouldn't have. This feels much closer to a bug, and relying on a bug not being fixed is not a reasonable expectation.

Cheers,

Paul

Signature · 6 lines
Paul Kocialkowski,

Independent contractor - sys-base - https://www.sys-base.io/
Free software developer - https://www.paulk.fr/

Specialist in multimedia, graphics and embedded hardware support with Linux.
#3 lore raw
Quoted 40 lines · Paul Kocialkowski

Hi Maxime,

Quoted 8 lines · Maxime Ripard
Quoted 7 lines · Paul Kocialkowski

From: Paul Kocialkowski <[email protected]>

The sunxi external interrupts (available from GPIO pins) come with a built-in debouncing mechanism that cannot be disabled. It can be configured to use either the low-frequency oscillator (32 KHz) or the high-frequency oscillator (24 MHz), with a pre-scaler.

Quoted 7 lines
Quoted 5 lines

The pinctrl code supports an input-debounce device-tree property to set a specific debouncing period and choose which clock source is most relevant. However the property is specified in microseconds, which is longer than the minimal period achievable from the high-frequency oscillator without a pre-scaler.

That can be fixed by introducing a new property with a ns resolution.

Sure but my point here is rather about what should be default behavior.

The issue I had will remain unsolved by default even with a new property, since people will still need to patch their device-tree to apply it.

Quoted 8 lines
Quoted 8 lines

When the property is missing, the reset configuration is kept, which selects the low-frequency oscillator without pre-scaling. This severely limits the possible interrupt periods that can be detected.

Instead of keeping this default, use the minimal debouncing period from the high-frequency oscillator without a pre-scaler to allow the largest possible range of interrupt periods.

Quoted 7 lines
Quoted 3 lines

This issue was encountered with a peripheral that generates active-low interrupts for 1 us. No interrupt was detected with the default setup, while it is now correctly detected with this change.

I don't think it's wise. If the debouncing is kept as is, the worst case scenario is the one you had: a device doesn't work, you change it, everything works.

Quoted 6 lines

I think this worst-case scenario is very bad and not what people will expect. In addition it is difficult to debug the issue without specific knowledge about the SoC.

My use-case here was hooking up a sparkfun sensor board by the way, not some very advanced corner-case.

Are you really arguing that a single sparkfun sensor not working is a worse outcome than the system not booting?

Quoted 8 lines
Quoted 3 lines

If we set it up as fast as it can however, then our risk becomes thousands of spurious interrupts, which is much more detrimental to the system.

Keep in mind that this only concerns external GPIO-based interrupts, which have to be explicitely hooked to a device. If a device or circuit does generate such spurious interrupts, I think it makes sense that it would be reflected by default.

I mean... debouncing is here for a reason. Any hardware button will generate plenty of interrupts when you press it precisely because it bounces.

Quoted 2 lines

Also the notion of spurious interrupt is pretty vague. Having lots of interrupts happening may be the desired behavior in many cases.

Which cases?

Quoted 3 lines

In any case I don't think it makes sense for the platform code to impose what a reasonable period for interrupts is (especially with such a large period as default).

So you don't think it makes sense for the platform code to impose a reasonable period, so you want to impose a (more, obviously) reasonable period?

If anything, the status quo doesn't impose anything, it just rolls with the hardware default. Yours would impose one though.

Quoted 8 lines

Some drivers also have mechanisms to detect spurious interrupts based on their specific use case.

Quoted 2 lines

And that's without accounting the fact that devices might have relied on that default for years

They definitely shouldn't have. This feels much closer to a bug, and relying on a bug not being fixed is not a reasonable expectation.

No, it's not a bug, really. It might be inconvenient to you, and that's fine, but it's definitely not a bug.

Maxime

#4 lore raw
Quoted 45 lines · Maxime Ripard
Quoted 44 lines · Paul Kocialkowski

Hi Maxime,

Quoted 8 lines · Maxime Ripard
Quoted 7 lines · Paul Kocialkowski

From: Paul Kocialkowski <[email protected]>

The sunxi external interrupts (available from GPIO pins) come with a built-in debouncing mechanism that cannot be disabled. It can be configured to use either the low-frequency oscillator (32 KHz) or the high-frequency oscillator (24 MHz), with a pre-scaler.

Quoted 7 lines
Quoted 5 lines

The pinctrl code supports an input-debounce device-tree property to set a specific debouncing period and choose which clock source is most relevant. However the property is specified in microseconds, which is longer than the minimal period achievable from the high-frequency oscillator without a pre-scaler.

That can be fixed by introducing a new property with a ns resolution.

Sure but my point here is rather about what should be default behavior.

The issue I had will remain unsolved by default even with a new property, since people will still need to patch their device-tree to apply it.

Quoted 8 lines
Quoted 8 lines

When the property is missing, the reset configuration is kept, which selects the low-frequency oscillator without pre-scaling. This severely limits the possible interrupt periods that can be detected.

Instead of keeping this default, use the minimal debouncing period from the high-frequency oscillator without a pre-scaler to allow the largest possible range of interrupt periods.

Quoted 7 lines
Quoted 3 lines

This issue was encountered with a peripheral that generates active-low interrupts for 1 us. No interrupt was detected with the default setup, while it is now correctly detected with this change.

I don't think it's wise. If the debouncing is kept as is, the worst case scenario is the one you had: a device doesn't work, you change it, everything works.

I think this worst-case scenario is very bad and not what people will expect. In addition it is difficult to debug the issue without specific knowledge about the SoC.

Quoted 5 lines
Quoted 2 lines

My use-case here was hooking up a sparkfun sensor board by the way, not some very advanced corner-case.

Are you really arguing that a single sparkfun sensor not working is a worse outcome than the system not booting?

No, what I'm saying is that this is a very common and basic use-case that most users will expect to work out-of-the-box.

Also the possibility of interrupt storms happening is nothing new (and it can still happen with any non-external interrupt). It would typically result from a hardware-related issue and there's no reason why it would happen on correctly-designed boards. If anything, it would allow spotting these isues more easily.

I think it comes down to whether we expect an interrupt controller to "just report interrupts" or whether it's reasonable that it applies extra policy to cover for unlikely (yet very problematic) situations. I think it's good that it supports that, but also that it should not enforce such a restrictive policy by default.

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

If we set it up as fast as it can however, then our risk becomes thousands of spurious interrupts, which is much more detrimental to the system.

Quoted 8 lines
Quoted 4 lines

Keep in mind that this only concerns external GPIO-based interrupts, which have to be explicitely hooked to a device. If a device or circuit does generate such spurious interrupts, I think it makes sense that it would be reflected by default.

I mean... debouncing is here for a reason. Any hardware button will generate plenty of interrupts when you press it precisely because it bounces.

Well this is why we have both electronics to filter out these frequencies and code in related drivers to implement such debouncing.

I am not arguing that debouncing is not important, I am saying that it should not be that agressive on every interrupt line by default.

Quoted 4 lines
Quoted 2 lines

Also the notion of spurious interrupt is pretty vague. Having lots of interrupts happening may be the desired behavior in many cases.

Which cases?

Any kind of data sampling happening at high-speeds really.
And this situation also concerns interrupts that are short even if not very frequent. That's a very large scope of use cases.

Quoted 7 lines
Quoted 3 lines

In any case I don't think it makes sense for the platform code to impose what a reasonable period for interrupts is (especially with such a large period as default).

So you don't think it makes sense for the platform code to impose a reasonable period, so you want to impose a (more, obviously) reasonable period?

Yes absolutely. Anything that brings us closer to "you get what is really happening with the hardware". The sunxi controller doesn't allow disabling debouncing entirely, so the next best thing is to have it with the smallest period.

Quoted 2 lines

If anything, the status quo doesn't impose anything, it just rolls with the hardware default. Yours would impose one though.

The result is that it puts a strong limitation and breaks many use cases by default. I don't think we have to accept whatever register default was chosen by hardware engineers as the most sensible default choice and pretend that this is not a policy decision.

Quoted 11 lines
Quoted 8 lines

Some drivers also have mechanisms to detect spurious interrupts based on their specific use case.

Quoted 2 lines

And that's without accounting the fact that devices might have relied on that default for years

They definitely shouldn't have. This feels much closer to a bug, and relying on a bug not being fixed is not a reasonable expectation.

No, it's not a bug, really. It might be inconvenient to you, and that's fine, but it's definitely not a bug.

I agree it's not a bug, just a poor default choice that is neither documented nor explicitely announced. For all we know U-Boot could configure that to something completely different and that would break the assumption too.

Cheers,

Paul

Signature · 6 lines
Paul Kocialkowski,

Independent contractor - sys-base - https://www.sys-base.io/
Free software developer - https://www.paulk.fr/

Specialist in multimedia, graphics and embedded hardware support with Linux.
#5 lore raw
Quoted 4 lines · Paul Kocialkowski
Quoted 4 lines
Quoted 3 lines

In any case I don't think it makes sense for the platform code to impose what a reasonable period for interrupts is (especially with such a large period as default).

Quoted 8 lines
Quoted 3 lines

So you don't think it makes sense for the platform code to impose a reasonable period, so you want to impose a (more, obviously) reasonable period?

Yes absolutely. Anything that brings us closer to "you get what is really happening with the hardware". The sunxi controller doesn't allow disabling debouncing entirely, so the next best thing is to have it with the smallest period.

That's an opinion, not a fact.

Quoted 7 lines
Quoted 2 lines

If anything, the status quo doesn't impose anything, it just rolls with the hardware default. Yours would impose one though.

The result is that it puts a strong limitation and breaks many use cases by default. I don't think we have to accept whatever register default was chosen by hardware engineers as the most sensible default choice and pretend that this is not a policy decision.

You're making it much worse than it is. It doesn't "break many use cases" it broke one, by default, with a supported way to unbreak it, in 12 years.

Maxime

#6 lore raw
Quoted 9 lines · Maxime Ripard
Quoted 8 lines · Paul Kocialkowski
Quoted 7 lines
Quoted 3 lines

In any case I don't think it makes sense for the platform code to impose what a reasonable period for interrupts is (especially with such a large period as default).

So you don't think it makes sense for the platform code to impose a reasonable period, so you want to impose a (more, obviously) reasonable period?

Quoted 6 lines
Quoted 4 lines

Yes absolutely. Anything that brings us closer to "you get what is really happening with the hardware". The sunxi controller doesn't allow disabling debouncing entirely, so the next best thing is to have it with the smallest period.

That's an opinion, not a fact.

Yes and I'm trying to explain why I believe this is the most sensible opinion here. I'm not saying everyone *has to* agree with me.

Quoted 11 lines
Quoted 7 lines
Quoted 2 lines

If anything, the status quo doesn't impose anything, it just rolls with the hardware default. Yours would impose one though.

The result is that it puts a strong limitation and breaks many use cases by default. I don't think we have to accept whatever register default was chosen by hardware engineers as the most sensible default choice and pretend that this is not a policy decision.

You're making it much worse than it is. It doesn't "break many use cases" it broke one, by default, with a supported way to unbreak it, in 12 years.

I think this is exaggerated. Like I mentioned previously there are *many* situations that are not covered by the default. The fact that I'm the first person to bring it up in 12 years doesn't change that.

Sofar the downside you brought up boils down to: badly-designed hardware may have relied on this mechanism to avoid interrupt storms that could prevent the system from booting.

If that happens people can always increase the debouncing time with the property as they need.

My point here is that the property should be used to accommodate for broken hardware and that the default should work for most use cases, including things as simple as an off-the-shelf sensor board.

That's all.

Paul

Signature · 6 lines
Paul Kocialkowski,

Independent contractor - sys-base - https://www.sys-base.io/
Free software developer - https://www.paulk.fr/

Specialist in multimedia, graphics and embedded hardware support with Linux.
#7 lore raw
Quoted 9 lines · Paul Kocialkowski
Quoted 8 lines · Maxime Ripard
Quoted 7 lines
Quoted 2 lines

If anything, the status quo doesn't impose anything, it just rolls with the hardware default. Yours would impose one though.

The result is that it puts a strong limitation and breaks many use cases by default. I don't think we have to accept whatever register default was chosen by hardware engineers as the most sensible default choice and pretend that this is not a policy decision.

Quoted 6 lines
Quoted 3 lines

You're making it much worse than it is. It doesn't "break many use cases" it broke one, by default, with a supported way to unbreak it, in 12 years.

I think this is exaggerated. Like I mentioned previously there are *many* situations that are not covered by the default.

Note that this statement would be true for any default. The current, the one you suggest, or any other really. The fact that we have a way to override it is an acknowledgement that it's not a one size fits all situation.

Quoted 2 lines

The fact that I'm the first person to bring it up in 12 years doesn't change that.

Sure. It does however hint that it seems like it's a sane enough default.

Quoted 3 lines

Sofar the downside you brought up boils down to: badly-designed hardware may have relied on this mechanism to avoid interrupt storms that could prevent the system from booting.

It's not about good or bad design. Buttons bounce, HPD signals bounce, it's just the world we live in.

But let me rephrase if my main objection wasn't clear enough: you want to introduce an ABI breaking change. With the possibility of breaking devices that have worked fine so far. That's not ok.

Maxime

#8 lore raw

Hi Maxime,

Quoted 14 lines · Maxime Ripard
Quoted 13 lines · Paul Kocialkowski
Quoted 11 lines · Maxime Ripard
Quoted 7 lines
Quoted 2 lines

If anything, the status quo doesn't impose anything, it just rolls with the hardware default. Yours would impose one though.

The result is that it puts a strong limitation and breaks many use cases by default. I don't think we have to accept whatever register default was chosen by hardware engineers as the most sensible default choice and pretend that this is not a policy decision.

You're making it much worse than it is. It doesn't "break many use cases" it broke one, by default, with a supported way to unbreak it, in 12 years.

Quoted 7 lines
Quoted 2 lines

I think this is exaggerated. Like I mentioned previously there are *many* situations that are not covered by the default.

Note that this statement would be true for any default. The current, the one you suggest, or any other really. The fact that we have a way to override it is an acknowledgement that it's not a one size fits all situation.

Again the debate is about which option has the best advantages over disadvantages. I'm not saying the default I suggest has no issues, but rather that the benefits very clearly outweight the issues. Hence the many situations that would be supported with the shortest debouncing period (both short and frequent interrupts) versus the few broken-hardware use-cases (related to interrupt storms) support with the largest period.

Quoted 5 lines
Quoted 2 lines

The fact that I'm the first person to bring it up in 12 years doesn't change that.

Sure. It does however hint that it seems like it's a sane enough default.

Or maybe people didn't realize this mechanism existed, failed to understand why their device didn't work with Allwinner platforms and just moved on to use something else. Indeed it's all very subjective interpretation.

Quoted 6 lines
Quoted 3 lines

Sofar the downside you brought up boils down to: badly-designed hardware may have relied on this mechanism to avoid interrupt storms that could prevent the system from booting.

It's not about good or bad design. Buttons bounce, HPD signals bounce, it's just the world we live in.

Well I'm an electrical engineer and the first thing we were told about buttons and connectors is to include hardware debouncing. The second thing is that it can be done in software (which again is done in a number of drivers) by just disabling the interrupt for a while if it happens too often.

So I'm quite affirmative that taking none of these into account is constitutive of a broken hardware design. No electrical engineer is told that they shouldn't care about this because the SoC will filter interrupts for them.

Of course it's fine to use this mechanism when it exists, but it's not a reasonable expectation to just assume it will always be there. This is why I think it's not a legitimate reason to make it a default.

Quoted 3 lines

But let me rephrase if my main objection wasn't clear enough: you want to introduce an ABI breaking change. With the possibility of breaking devices that have worked fine so far. That's not ok.

I believe it is highly questionable that this constitutes ABI breakage. To me there was no defined behavior in the first place, since the debouncing configuration is inherited either from the reset value or the boot stage. There is also no formal indication of what the default is, anywhere.

Changing the default configuration of hardware is commonplace. One might for example introduce a reset to a driver to get a clean state because it happened that some boot software would mess it up and it went unnoticed for a while. Would you also call that ABI breakage?

I think there's a number of situations where it's much more sensible to change a default state to avoid very visible and practical issues. And it does happen.

Also my understanding of the "ABI breakage" rule in the kernel is that no userspace program should stop working when it was implemented to (correctly) follow some kernel-related ABI. It doesn't mean that we cannot change any default state, or that everything the kernel does should remain exactly the same ad vitam.

Sometimes it just makes a lot more sense to have these fine adjustments.

And honestly I would be extremely surprised that this specific change resulted in any actual system breakage, as we can be hopeful that electrical engineers did their jobs correctly.

Cheers,

Paul

Signature · 6 lines
Paul Kocialkowski,

Independent contractor - sys-base - https://www.sys-base.io/
Free software developer - https://www.paulk.fr/

Expert in multimedia, graphics and embedded hardware support with Linux.
#9 lore raw
Quoted 49 lines · Paul Kocialkowski

Hi Maxime,

Quoted 14 lines · Maxime Ripard
Quoted 13 lines · Paul Kocialkowski
Quoted 11 lines · Maxime Ripard
Quoted 7 lines
Quoted 2 lines

If anything, the status quo doesn't impose anything, it just rolls with the hardware default. Yours would impose one though.

The result is that it puts a strong limitation and breaks many use cases by default. I don't think we have to accept whatever register default was chosen by hardware engineers as the most sensible default choice and pretend that this is not a policy decision.

You're making it much worse than it is. It doesn't "break many use cases" it broke one, by default, with a supported way to unbreak it, in 12 years.

Quoted 7 lines
Quoted 2 lines

I think this is exaggerated. Like I mentioned previously there are *many* situations that are not covered by the default.

Note that this statement would be true for any default. The current, the one you suggest, or any other really. The fact that we have a way to override it is an acknowledgement that it's not a one size fits all situation.

Again the debate is about which option has the best advantages over disadvantages. I'm not saying the default I suggest has no issues, but rather that the benefits very clearly outweight the issues. Hence the many situations that would be supported with the shortest debouncing period (both short and frequent interrupts) versus the few broken-hardware use-cases (related to interrupt storms) support with the largest period.

Quoted 5 lines
Quoted 2 lines

The fact that I'm the first person to bring it up in 12 years doesn't change that.

Sure. It does however hint that it seems like it's a sane enough default.

Or maybe people didn't realize this mechanism existed, failed to understand why their device didn't work with Allwinner platforms and just moved on to use something else. Indeed it's all very subjective interpretation.

Quoted 6 lines
Quoted 3 lines

Sofar the downside you brought up boils down to: badly-designed hardware may have relied on this mechanism to avoid interrupt storms that could prevent the system from booting.

It's not about good or bad design. Buttons bounce, HPD signals bounce, it's just the world we live in.

Quoted 8 lines

Well I'm an electrical engineer and the first thing we were told about buttons and connectors is to include hardware debouncing. The second thing is that it can be done in software (which again is done in a number of drivers) by just disabling the interrupt for a while if it happens too often.

So I'm quite affirmative that taking none of these into account is constitutive of a broken hardware design. No electrical engineer is told that they shouldn't care about this because the SoC will filter interrupts for them.

The SoC provides the hardware debouncing. There's no reason not to use it, or to choose something redundant. Some might, but it's also perfectly valid to just rely on the SoC there.

Quoted 3 lines

Of course it's fine to use this mechanism when it exists, but it's not a reasonable expectation to just assume it will always be there. This is why I think it's not a legitimate reason to make it a default.

Nobody ever designed a board without considering the SoC features but rather by adhering to a dogma. The SoC features, components chosen and their price, etc. all play a role.

Quoted 8 lines
Quoted 3 lines

But let me rephrase if my main objection wasn't clear enough: you want to introduce an ABI breaking change. With the possibility of breaking devices that have worked fine so far. That's not ok.

I believe it is highly questionable that this constitutes ABI breakage. To me there was no defined behavior in the first place, since the debouncing configuration is inherited either from the reset value or the boot stage. There is also no formal indication of what the default is, anywhere.

Depending on the interpretation, it either means that you change the default, or add a default, to a device-tree property. That constitutes an ABI breakage on its own right. And then we can introduce regressions for boards, which is another breakage.

Quoted 4 lines

Changing the default configuration of hardware is commonplace. One might for example introduce a reset to a driver to get a clean state because it happened that some boot software would mess it up and it went unnoticed for a while. Would you also call that ABI breakage?

No, because it doesn't require a change in the default state Linux expects when it boots, or changing anything in the device tree. It's a self-contained change, and thus there's no interface to break.

Quoted 7 lines

I think there's a number of situations where it's much more sensible to change a default state to avoid very visible and practical issues. And it does happen.

Also my understanding of the "ABI breakage" rule in the kernel is that no userspace program should stop working when it was implemented to (correctly) follow some kernel-related ABI. It doesn't mean that we cannot change any default state

If applications rely on that default one way or another, it's absolutely what it means. The only criteria is whether this will create a regression for any application.

Maxime

#10 lore raw

Hi,

Quoted 14 lines · Maxime Ripard
Quoted 8 lines · Paul Kocialkowski

Well I'm an electrical engineer and the first thing we were told about buttons and connectors is to include hardware debouncing. The second thing is that it can be done in software (which again is done in a number of drivers) by just disabling the interrupt for a while if it happens too often.

So I'm quite affirmative that taking none of these into account is constitutive of a broken hardware design. No electrical engineer is told that they shouldn't care about this because the SoC will filter interrupts for them.

The SoC provides the hardware debouncing. There's no reason not to use it, or to choose something redundant. Some might, but it's also perfectly valid to just rely on the SoC there.

Quoted 7 lines
Quoted 3 lines

Of course it's fine to use this mechanism when it exists, but it's not a reasonable expectation to just assume it will always be there. This is why I think it's not a legitimate reason to make it a default.

Nobody ever designed a board without considering the SoC features but rather by adhering to a dogma. The SoC features, components chosen and their price, etc. all play a role.

Okay that's fair enough. I guess what I had in mind was that it would be unusual and unlikely. I don't think it disqualifies the fact that it would be sensible to enable this behavior with the property and not the other way round though.

Quoted 9 lines
Quoted 8 lines
Quoted 3 lines

But let me rephrase if my main objection wasn't clear enough: you want to introduce an ABI breaking change. With the possibility of breaking devices that have worked fine so far. That's not ok.

I believe it is highly questionable that this constitutes ABI breakage. To me there was no defined behavior in the first place, since the debouncing configuration is inherited either from the reset value or the boot stage. There is also no formal indication of what the default is, anywhere.

Quoted 4 lines

Depending on the interpretation, it either means that you change the default, or add a default, to a device-tree property. That constitutes an ABI breakage on its own right. And then we can introduce regressions for boards, which is another breakage.

It feels very questionable that adding a default over undefined behavior constitutes an ABI breakage.

Quoted 8 lines
Quoted 4 lines

Changing the default configuration of hardware is commonplace. One might for example introduce a reset to a driver to get a clean state because it happened that some boot software would mess it up and it went unnoticed for a while. Would you also call that ABI breakage?

No, because it doesn't require a change in the default state Linux expects when it boots, or changing anything in the device tree. It's a self-contained change, and thus there's no interface to break.

It could definitely result in changes in behavior though. One could imagine the case of a sensor that was configured with 4x gain by the boot software, which wasn't reset by the driver. Now when the driver is updated to include a reset the sensor will fall back to 1x gain, with a direct result on the values that are read with default configuration.

This would however be perfectly fine since it wasn't specified anywhere that the sensor should report values with 4x gain in its default state. It's a behavior change that has direct effect, but doesn't break any ABI at all.

Quoted 11 lines
Quoted 7 lines

I think there's a number of situations where it's much more sensible to change a default state to avoid very visible and practical issues. And it does happen.

Also my understanding of the "ABI breakage" rule in the kernel is that no userspace program should stop working when it was implemented to (correctly) follow some kernel-related ABI. It doesn't mean that we cannot change any default state

If applications rely on that default one way or another, it's absolutely what it means. The only criteria is whether this will create a regression for any application.

For any application *that uses the ABI correctly*. Any example of defaults that are inherited from the boot phase clearly shows that it is not a reasonable expectation to rely on them and doing so is not correct use of the ABI. It is rather very clearly relying on undefined behavior.

Cheers,

Paul

Signature · 6 lines
Paul Kocialkowski,

Independent contractor - sys-base - https://www.sys-base.io/
Free software developer - https://www.paulk.fr/

Expert in multimedia, graphics and embedded hardware support with Linux.
#11 lore raw
In reply to Paul Kocialkowski

Some discussion here, and some emotions involved.

I can't seem to follow the technical matter because of all the social matters :/

Quoted 2 lines · Paul Kocialkowski

My use-case here was hooking up a sparkfun sensor board by the way, not some very advanced corner-case.

Are you adding this as a DT overlay or by modifying an existing device tree? Does this sensor have an established device tree binding?

Are you using that sparkfun sensor by a kernel driver or from userspace using the GPIO character device?

I noticed that the sunxi GPIO driver is implementing the .set_config() callback calling gpiochip_generic_config, which makes it call down to the pin control driver to set up the pin config.

which would in turn make it eligible to use
the gpiod_set_debounce() callback to push down the debounce period.

But pinctrl-sunxi.c's sunxi_pconf_set() does *NOT* implement support for setting up the debounce, because it is (as I understand it) not part of the pin config hardware, but part of the interrupt generator hardware, correct?

In that case I think we the gpiochip .set_config() callback should be modified something like this (pseudo code):

sunxi_pinctrl_gpio_set_config()

{
    if (irq_is_in_use && param_is_debounce) {
        modify_irq_debounce_according_to_param()
        return 0;
    }
    gpiochip_generic_config()
}

pctl->chip->set_config = sunxi_pinctrl_gpio_set_config()

Maybe the debounce can also be set even if the line is not used for IRQ? I'm not sure.

In either case the latter would give the GPIO driver a handle on the debounce, which is good because the irqchip generally does not.

There is a way it is possible to use the interrupt with desired debounce setting by first getting the GPIO descriptor and modify the debounce setting and then getting the interrupt from the GPIO descriptor:

struct gpio_desc *g;
int irq;

g = gpiod_get(dev, "dataready", GPIOD_IN);
gpiod_set_debounce(g, 1);
irq = gpiod_to_irq(g);
...request irq...

Here I assume the line out from the sensor is named "dataready" the actual component likely has a name like that for the line.

This requires changes in the device tree as well, so that a GPIO line is assigned to the sensor instead of "just" an interrupt:

sensor {
dataready-gpios = <&gpio 14>;
...
};

instead of:

sensor {
interrupt-parent = <&gpio>;
interrupts = <14 IRQ_TYPE_EDGE_RISING>;
};

Unfortunately this is one of the areas where DT bindings are a bit ambiguous. Some just assign the GPIO line as an interrupt as both APIs are available, sometimes a proper GPIO line is preferred for the above reasons.

Yours,
Linus Walleij

#12 lore raw

Hi Linus,

Quoted 4 lines · Linus Walleij

Some discussion here, and some emotions involved.

I can't seem to follow the technical matter because of all the social matters :/

Thanks for looking into this. It's a bit of a heated discussion but nothing personal, really. The context is that Maxime and I often disagree on what constitutes ABI breakage or not. To be fair he has already proven me wrong in the past and I'm still learning.

The debate here is about what the default behavior should be.

Quoted 6 lines · Paul Kocialkowski
Quoted 2 lines

My use-case here was hooking up a sparkfun sensor board by the way, not some very advanced corner-case.

Are you adding this as a DT overlay or by modifying an existing device tree?

The answer would be an overlay. There's no device that I know of that has this sensor built-in.

Quoted 1 line

Does this sensor have an established device tree binding?

It is underway. You can find it at:
https://patchwork.kernel.org/project/linux-iio/patch/[email protected]/

Nothing really stands out and the short interrupt time is mentionned there.

Quoted 2 lines

Are you using that sparkfun sensor by a kernel driver or from userspace using the GPIO character device?

With a dedicated IIO driver currently under review.

Quoted 5 lines

I noticed that the sunxi GPIO driver is implementing the .set_config() callback calling gpiochip_generic_config, which makes it call down to the pin control driver to set up the pin config.

Quoted 8 lines

which would in turn make it eligible to use
the gpiod_set_debounce() callback to push down the debounce period.

But pinctrl-sunxi.c's sunxi_pconf_set() does *NOT* implement support for setting up the debounce, because it is (as I understand it) not part of the pin config hardware, but part of the interrupt generator hardware, correct?

Yes I believe this is correct. Debouncing is said (according to scarce documentation) to be tied to the interrupt part, not GPIO in general.

Quoted 12 lines

In that case I think we the gpiochip .set_config() callback should be modified something like this (pseudo code):

sunxi_pinctrl_gpio_set_config()

{
    if (irq_is_in_use && param_is_debounce) {
        modify_irq_debounce_according_to_param()
        return 0;
    }
    gpiochip_generic_config()
}
Quoted 1 line

pctl->chip->set_config = sunxi_pinctrl_gpio_set_config()

I didn't even know the gpio API supported this, thanks! Then it would make sense for the driver to specify this.

Quoted 2 lines

Maybe the debounce can also be set even if the line is not used for IRQ? I'm not sure.

My guess would be that it can't.

Quoted 7 lines

In either case the latter would give the GPIO driver a handle on the debounce, which is good because the irqchip generally does not.

There is a way it is possible to use the interrupt with desired debounce setting by first getting the GPIO descriptor and modify the debounce setting and then getting the interrupt from the GPIO descriptor:

However I see the debounce time is specified in microseconds, which is longer that the minimum achievable and probably too close to the limit for the sensor board, that generates the interrupt for 1 us only.

Is it specified that a value of 0 to gpiod_set_debounce would select the lowest debouncing time?

Quoted 26 lines

struct gpio_desc *g;
int irq;

g = gpiod_get(dev, "dataready", GPIOD_IN);
gpiod_set_debounce(g, 1);
irq = gpiod_to_irq(g);
...request irq...

Here I assume the line out from the sensor is named "dataready" the actual component likely has a name like that for the line.

This requires changes in the device tree as well, so that a GPIO line is assigned to the sensor instead of "just" an interrupt:

sensor {
dataready-gpios = <&gpio 14>;
...
};

instead of:

sensor {
interrupt-parent = <&gpio>;
interrupts = <14 IRQ_TYPE_EDGE_RISING>;
};

Quoted 4 lines

Unfortunately this is one of the areas where DT bindings are a bit ambiguous. Some just assign the GPIO line as an interrupt as both APIs are available, sometimes a proper GPIO line is preferred for the above reasons.

I can see how that would be confusing indeed. And the same change would be required for pretty much any driver that could have the same problem. I'm not sure this is desirable.

Would there be a way to do the opposite and grab the gpio handle from the irq line, as to leave the dt binding unchanged?

Cheers,

Paul

Signature · 6 lines
Paul Kocialkowski,

Independent contractor - sys-base - https://www.sys-base.io/
Free software developer - https://www.paulk.fr/

Expert in multimedia, graphics and embedded hardware support with Linux.
#13 lore raw
In reply to Maxime Ripard
Quoted 8 lines · Maxime Ripard

[Paul]

Quoted 5 lines

The pinctrl code supports an input-debounce device-tree property to set a specific debouncing period and choose which clock source is most relevant. However the property is specified in microseconds, which is longer than the minimal period achievable from the high-frequency oscillator without a pre-scaler.

That can be fixed by introducing a new property with a ns resolution.

Perhaps, but I think a setting of 0 should just yield the minimum resolution in this case, as apparently the debounce cannot be turned off? This would also make sense to expose for gpiod_set_debounce() as explained in my other mail.

Yours,
Linus Walleij

#14 lore raw
Quoted 9 lines · Linus Walleij
Quoted 7 lines · Maxime Ripard

[Paul]

Quoted 5 lines

The pinctrl code supports an input-debounce device-tree property to set a specific debouncing period and choose which clock source is most relevant. However the property is specified in microseconds, which is longer than the minimal period achievable from the high-frequency oscillator without a pre-scaler.

Quoted 6 lines
Quoted 1 line

That can be fixed by introducing a new property with a ns resolution.

Perhaps, but I think a setting of 0 should just yield the minimum resolution in this case, as apparently the debounce cannot be turned off? This would also make sense to expose for gpiod_set_debounce() as explained in my other mail.

Yeah, if we want to have 0 == lowest resolution, I'm all for it.

Maxime

#15 lore raw

Hi,

Quoted 10 lines · Maxime Ripard
Quoted 9 lines · Linus Walleij
Quoted 7 lines · Maxime Ripard

[Paul]

Quoted 5 lines

The pinctrl code supports an input-debounce device-tree property to set a specific debouncing period and choose which clock source is most relevant. However the property is specified in microseconds, which is longer than the minimal period achievable from the high-frequency oscillator without a pre-scaler.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

That can be fixed by introducing a new property with a ns resolution.

Perhaps, but I think a setting of 0 should just yield the minimum resolution in this case, as apparently the debounce cannot be turned off? This would also make sense to expose for gpiod_set_debounce() as explained in my other mail.

Yeah, if we want to have 0 == lowest resolution, I'm all for it.

I agree that it would be easier and less confusing than introducing a new property just to have a finer resolution.

I could craft a patch in that direction then, with a mention about it in the bindings.

Cheers,

Paul

Signature · 6 lines
Paul Kocialkowski,

Independent contractor - sys-base - https://www.sys-base.io/
Free software developer - https://www.paulk.fr/

Expert in multimedia, graphics and embedded hardware support with Linux.