2017-04-05 08:47:55 +08:00
|
|
|
============================================================
|
2009-03-05 15:27:14 +08:00
|
|
|
rotary-encoder - a generic driver for GPIO connected devices
|
2017-04-05 08:47:55 +08:00
|
|
|
============================================================
|
2009-03-05 15:27:14 +08:00
|
|
|
|
2017-04-05 08:47:55 +08:00
|
|
|
:Author: Daniel Mack <daniel@caiaq.de>, Feb 2009
|
|
|
|
|
|
|
|
Function
|
|
|
|
--------
|
2009-03-05 15:27:14 +08:00
|
|
|
|
|
|
|
Rotary encoders are devices which are connected to the CPU or other
|
|
|
|
peripherals with two wires. The outputs are phase-shifted by 90 degrees
|
|
|
|
and by triggering on falling and rising edges, the turn direction can
|
|
|
|
be determined.
|
|
|
|
|
2015-10-14 14:39:50 +08:00
|
|
|
Some encoders have both outputs low in stable states, others also have
|
|
|
|
a stable state with both outputs high (half-period mode) and some have
|
|
|
|
a stable state in all steps (quarter-period mode).
|
2011-05-12 07:35:30 +08:00
|
|
|
|
2017-04-05 08:47:55 +08:00
|
|
|
The phase diagram of these two outputs look like this::
|
2009-03-05 15:27:14 +08:00
|
|
|
|
|
|
|
_____ _____ _____
|
|
|
|
| | | | | |
|
|
|
|
Channel A ____| |_____| |_____| |____
|
|
|
|
|
|
|
|
: : : : : : : : : : : :
|
|
|
|
__ _____ _____ _____
|
|
|
|
| | | | | | |
|
|
|
|
Channel B |_____| |_____| |_____| |__
|
|
|
|
|
|
|
|
: : : : : : : : : : : :
|
|
|
|
Event a b c d a b c d a b c d
|
|
|
|
|
|
|
|
|<-------->|
|
|
|
|
one step
|
|
|
|
|
2011-05-12 07:35:30 +08:00
|
|
|
|<-->|
|
|
|
|
one step (half-period mode)
|
2009-03-05 15:27:14 +08:00
|
|
|
|
2015-10-14 14:39:50 +08:00
|
|
|
|<>|
|
|
|
|
one step (quarter-period mode)
|
|
|
|
|
2009-03-05 15:27:14 +08:00
|
|
|
For more information, please see
|
2015-06-17 23:12:02 +08:00
|
|
|
https://en.wikipedia.org/wiki/Rotary_encoder
|
2009-03-05 15:27:14 +08:00
|
|
|
|
|
|
|
|
2017-04-05 08:47:55 +08:00
|
|
|
Events / state machine
|
|
|
|
----------------------
|
2009-03-05 15:27:14 +08:00
|
|
|
|
2011-05-12 07:35:30 +08:00
|
|
|
In half-period mode, state a) and c) above are used to determine the
|
|
|
|
rotational direction based on the last stable state. Events are reported in
|
|
|
|
states b) and d) given that the new stable state is different from the last
|
|
|
|
(i.e. the rotation was not reversed half-way).
|
|
|
|
|
|
|
|
Otherwise, the following apply:
|
|
|
|
|
2009-03-05 15:27:14 +08:00
|
|
|
a) Rising edge on channel A, channel B in low state
|
|
|
|
This state is used to recognize a clockwise turn
|
|
|
|
|
|
|
|
b) Rising edge on channel B, channel A in high state
|
|
|
|
When entering this state, the encoder is put into 'armed' state,
|
|
|
|
meaning that there it has seen half the way of a one-step transition.
|
|
|
|
|
|
|
|
c) Falling edge on channel A, channel B in high state
|
|
|
|
This state is used to recognize a counter-clockwise turn
|
|
|
|
|
|
|
|
d) Falling edge on channel B, channel A in low state
|
|
|
|
Parking position. If the encoder enters this state, a full transition
|
2011-03-31 09:57:33 +08:00
|
|
|
should have happened, unless it flipped back on half the way. The
|
2009-03-05 15:27:14 +08:00
|
|
|
'armed' state tells us about that.
|
|
|
|
|
2017-04-05 08:47:55 +08:00
|
|
|
Platform requirements
|
|
|
|
---------------------
|
2009-03-05 15:27:14 +08:00
|
|
|
|
|
|
|
As there is no hardware dependent call in this driver, the platform it is
|
|
|
|
used with must support gpiolib. Another requirement is that IRQs must be
|
|
|
|
able to fire on both edges.
|
|
|
|
|
|
|
|
|
2017-04-05 08:47:55 +08:00
|
|
|
Board integration
|
|
|
|
-----------------
|
2009-03-05 15:27:14 +08:00
|
|
|
|
|
|
|
To use this driver in your system, register a platform_device with the
|
|
|
|
name 'rotary-encoder' and associate the IRQs and some specific platform
|
2017-04-06 08:13:22 +08:00
|
|
|
data with it. Because the driver uses generic device properties, this can
|
|
|
|
be done either via device tree, ACPI, or using static board files, like in
|
|
|
|
example below:
|
2009-03-05 15:27:14 +08:00
|
|
|
|
2017-04-06 08:13:22 +08:00
|
|
|
::
|
2009-03-05 15:27:14 +08:00
|
|
|
|
2017-04-06 08:13:22 +08:00
|
|
|
/* board support file example */
|
2009-03-05 15:27:14 +08:00
|
|
|
|
2017-04-06 08:13:22 +08:00
|
|
|
#include <linux/input.h>
|
|
|
|
#include <linux/gpio/machine.h>
|
|
|
|
#include <linux/property.h>
|
|
|
|
|
|
|
|
#define GPIO_ROTARY_A 1
|
|
|
|
#define GPIO_ROTARY_B 2
|
|
|
|
|
|
|
|
static struct gpiod_lookup_table rotary_encoder_gpios = {
|
|
|
|
.dev_id = "rotary-encoder.0",
|
|
|
|
.table = {
|
|
|
|
GPIO_LOOKUP_IDX("gpio-0",
|
|
|
|
GPIO_ROTARY_A, NULL, 0, GPIO_ACTIVE_LOW),
|
|
|
|
GPIO_LOOKUP_IDX("gpio-0",
|
|
|
|
GPIO_ROTARY_B, NULL, 1, GPIO_ACTIVE_HIGH),
|
|
|
|
{ },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct property_entry rotary_encoder_properties[] __initconst = {
|
2018-01-23 01:27:22 +08:00
|
|
|
PROPERTY_ENTRY_U32("rotary-encoder,steps-per-period", 24),
|
|
|
|
PROPERTY_ENTRY_U32("linux,axis", ABS_X),
|
|
|
|
PROPERTY_ENTRY_U32("rotary-encoder,relative_axis", 0),
|
2017-04-06 08:13:22 +08:00
|
|
|
{ },
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct platform_device rotary_encoder_device = {
|
|
|
|
.name = "rotary-encoder",
|
|
|
|
.id = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
gpiod_add_lookup_table(&rotary_encoder_gpios);
|
|
|
|
device_add_properties(&rotary_encoder_device, rotary_encoder_properties);
|
|
|
|
platform_device_register(&rotary_encoder_device);
|
|
|
|
|
|
|
|
...
|
2017-04-05 08:47:55 +08:00
|
|
|
|
2017-04-06 08:13:22 +08:00
|
|
|
Please consult device tree binding documentation to see all properties
|
|
|
|
supported by the driver.
|