From: William Breathitt Gray <wbg@kernel.org>
To: Krzysztof Kozlowski <krzk+dt@kernel.org>,
Rob Herring <robh@kernel.org>, Conor Dooley <conor+dt@kernel.org>,
linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org
Cc: Wadim Mueller <wafgo01@gmail.com>
Subject: Re: [PATCH v4 2/3] counter: add GPIO-based quadrature encoder driver
Date: Thu, 21 May 2026 09:26:25 +0900 [thread overview]
Message-ID: <20260521002627.172691-1-wbg@kernel.org> (raw)
In-Reply-To: <20260520044525.128529-1-wbg@kernel.org>
From: Wadim Mueller <wafgo01@gmail.com>
On Wed, May 20, 2026 at 01:45:20PM +0900, William Breathitt Gray wrote:
> On Fri, May 15, 2026 at 05:36:15PM +0200, Wadim Mueller wrote:
> > +static int gpio_qenc_a_delta(struct gpio_qenc_priv *priv, int a, int b,
> > + int prev_a, int prev_b)
> > +{
> > + int state = CREATE_QE_STATE(prev_a, prev_b, a, b);
> > +
> > + switch (priv->function) {
> > + case COUNTER_FUNCTION_QUADRATURE_X4:
> > + return gpio_qenc_quad_x4_table[state];
> > +
> > + case COUNTER_FUNCTION_QUADRATURE_X2_A:
> > + /* Both edges of A; sign comes from current A vs B. */
> > + return (a == b) ? -1 : 1;
> > +
> > + case COUNTER_FUNCTION_QUADRATURE_X1_A:
> > + /* Rising edge of A only. */
> > + if (!prev_a && a)
> > + return b ? -1 : 1;
> > + return 0;
>
> Quadrature X1 count modes trigger on the falling edge when the direction
> is backward. This isn't simply a requirement by definition, but
> necessary for the proper interpretation of the quadrature encoding.
>
> Let's evaluate an incremental encoder used in a positioning application
> as typical use case.[^1] These are commonly implemented using a rotating
> shaft with a quadrature-offset pattern; aligned sensors detect the
> physical A/B pattern as the shaft rotates.[^2] As the shaft rotates a
> quadrature encoding emerges whose A-B phase difference allows us to
> determine direction: forward when rising edge of signal A leads B, and
> backward when it trails.[^3]
>
> Now consider what happens to the signals when the rotation changes
> direction: there is a phase change between Signals A and B.[^4] The A/B
> pattern on the shaft is physically present so it has not changed; rather
> the pattern is now fed backwards to the sensors due to the direction
> reversal. The key point is the physical boundaries of the pattern are
> located in the same shaft positions they have always been, yet the
> signal edges representing those boundaries have flipped as a result of
> the direction change: positions marked by rising edges now appear as
> falling edges.
>
> In Quadrature X4 and X2, the pattern reversal doesn't affect positioning
> because we count on both edges, so swapping rising and falling edges
> nets the same position count. Quadrature X1 presents a problem because
> we count on a single edge type, so a phase-difference in the encoding
> results in a physical shift in real-life position. The way to account
> for that phase shift is to swap counting to the other edge type when the
> direction changes. That's how dedicated quadrature encoder devices solve
> this problem.
>
> I'm not sure of the best way to solve the Quadrature X1 problem in this
> driver. Right now we fire off interrupts on both edges, so perhaps
> there's a way for us to determine whether we're firing on a rising edge
> or falling edge and evaluate accordingly. Does the GPIO subsystem
> provide an indication for which edge triggered the interrupt? Or would
> it make sense to provide two interrupt service routines (one on rising
> edge and one on falling edge) and handle it that way?
The simplest method might be to evaluate the current GPIO level to
determine the edge polarity. Because we trigger on both edges, we can
assume a high level means a low-high transition (rising edge) and a low
level means a high-low transition (falling edge).
Using that assumption, we can implement the Quadrature X1 case by
checking the current state and direction, and adjusting the counting
accordingly when applicable: count up if rising edge and forward
direction, and count down if falling edge and backward direction.
Quadrature X1 A is handled by the signal A interrupt service routine:
/* COUNTER_FUNCTION_QUADRATURE_X1_A */
if (ca)
if (direction == COUNTER_COUNT_DIRECTION_FORWARD)
count++;
else
if (direction == COUNTER_COUNT_DIRECTION_BACKWARD)
count--;
Quadrature X1 B is handled by the signal B interrupt service routine:
/* COUNTER_FUNCTION_QUADRATURE_X1_B */
if (cb)
if (direction == COUNTER_COUNT_DIRECTION_FORWARD)
count++;
else
if (direction == COUNTER_COUNT_DIRECTION_BACKWARD)
count--;
There's an obvious caveat that this method only works if we're able to
check the GPIO level before the next edge arrives (we're limited to low
frequencies), but that's a caveat present regardless for our software
counter so I believe this is an acceptable solution.
William Breathitt Gray
next prev parent reply other threads:[~2026-05-21 0:26 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-01 20:07 [PATCH v3 0/3] counter: add GPIO-based quadrature encoder driver Wadim Mueller
2026-05-01 20:07 ` [PATCH v3 1/3] dt-bindings: counter: add gpio-quadrature-encoder binding Wadim Mueller
2026-05-01 20:07 ` [PATCH v3 2/3] counter: add GPIO-based quadrature encoder driver Wadim Mueller
2026-05-04 20:54 ` Krzysztof Kozlowski
2026-05-04 21:15 ` Wadim Mueller
2026-05-15 5:48 ` William Breathitt Gray
2026-05-15 15:28 ` Wadim Mueller
2026-05-01 20:07 ` [PATCH v3 3/3] MAINTAINERS: add entry for GPIO quadrature encoder counter driver Wadim Mueller
2026-05-04 9:36 ` [PATCH v3 0/3] counter: add GPIO-based quadrature encoder driver William Breathitt Gray
2026-05-04 19:37 ` Wadim Mueller
2026-05-06 6:50 ` Wadim Mueller
2026-05-14 13:17 ` William Breathitt Gray
2026-05-15 15:36 ` [PATCH v4 " Wadim Mueller
2026-05-15 15:36 ` [PATCH v4 1/3] dt-bindings: counter: add gpio-quadrature-encoder binding Wadim Mueller
2026-05-15 15:36 ` [PATCH v4 2/3] counter: add GPIO-based quadrature encoder driver Wadim Mueller
2026-05-15 16:14 ` sashiko-bot
2026-05-20 4:45 ` William Breathitt Gray
2026-05-21 0:26 ` William Breathitt Gray [this message]
2026-05-15 15:36 ` [PATCH v4 3/3] MAINTAINERS: add entry for GPIO quadrature encoder counter driver Wadim Mueller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260521002627.172691-1-wbg@kernel.org \
--to=wbg@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=krzk+dt@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=robh@kernel.org \
--cc=wafgo01@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox