1 // SPDX-License-Identifier: GPL-2.0-only
5 * (c) 2009 Daniel Mack <daniel@caiaq.de>
6 * Copyright (C) 2011 Johan Hovold <jhovold@gmail.com>
8 * state machine code inspired by code from Tim Ruetz
10 * A generic driver for rotary encoders connected to GPIO lines.
11 * See file:Documentation/input/devices/rotary-encoder.rst for more information
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/input.h>
18 #include <linux/device.h>
19 #include <linux/platform_device.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/slab.h>
24 #include <linux/property.h>
26 #define DRV_NAME "rotary-encoder"
28 enum rotary_encoder_encoding
{
33 struct rotary_encoder
{
34 struct input_dev
*input
;
36 struct mutex access_mutex
;
42 enum rotary_encoder_encoding encoding
;
46 struct gpio_descs
*gpios
;
51 signed char dir
; /* 1 - clockwise, -1 - CCW */
53 unsigned int last_stable
;
56 static unsigned int rotary_encoder_get_state(struct rotary_encoder
*encoder
)
61 for (i
= 0; i
< encoder
->gpios
->ndescs
; ++i
) {
62 int val
= gpiod_get_value_cansleep(encoder
->gpios
->desc
[i
]);
64 /* convert from gray encoding to normal */
65 if (encoder
->encoding
== ROTENC_GRAY
&& ret
& 1)
74 static void rotary_encoder_report_event(struct rotary_encoder
*encoder
)
76 if (encoder
->relative_axis
) {
77 input_report_rel(encoder
->input
,
78 encoder
->axis
, encoder
->dir
);
80 unsigned int pos
= encoder
->pos
;
82 if (encoder
->dir
< 0) {
83 /* turning counter-clockwise */
84 if (encoder
->rollover
)
85 pos
+= encoder
->steps
;
89 /* turning clockwise */
90 if (encoder
->rollover
|| pos
< encoder
->steps
)
94 if (encoder
->rollover
)
95 pos
%= encoder
->steps
;
98 input_report_abs(encoder
->input
, encoder
->axis
, encoder
->pos
);
101 input_sync(encoder
->input
);
104 static irqreturn_t
rotary_encoder_irq(int irq
, void *dev_id
)
106 struct rotary_encoder
*encoder
= dev_id
;
109 guard(mutex
)(&encoder
->access_mutex
);
111 state
= rotary_encoder_get_state(encoder
);
115 if (encoder
->armed
) {
116 rotary_encoder_report_event(encoder
);
117 encoder
->armed
= false;
124 encoder
->dir
= 2 - state
;
128 encoder
->armed
= true;
135 static irqreturn_t
rotary_encoder_half_period_irq(int irq
, void *dev_id
)
137 struct rotary_encoder
*encoder
= dev_id
;
140 guard(mutex
)(&encoder
->access_mutex
);
142 state
= rotary_encoder_get_state(encoder
);
145 encoder
->dir
= ((encoder
->last_stable
- state
+ 1) % 4) - 1;
147 if (state
!= encoder
->last_stable
) {
148 rotary_encoder_report_event(encoder
);
149 encoder
->last_stable
= state
;
156 static irqreturn_t
rotary_encoder_quarter_period_irq(int irq
, void *dev_id
)
158 struct rotary_encoder
*encoder
= dev_id
;
161 guard(mutex
)(&encoder
->access_mutex
);
163 state
= rotary_encoder_get_state(encoder
);
165 if ((encoder
->last_stable
+ 1) % 4 == state
) {
167 rotary_encoder_report_event(encoder
);
168 } else if (encoder
->last_stable
== (state
+ 1) % 4) {
170 rotary_encoder_report_event(encoder
);
173 encoder
->last_stable
= state
;
178 static int rotary_encoder_probe(struct platform_device
*pdev
)
180 struct device
*dev
= &pdev
->dev
;
181 struct rotary_encoder
*encoder
;
182 struct input_dev
*input
;
183 irq_handler_t handler
;
184 u32 steps_per_period
;
188 encoder
= devm_kzalloc(dev
, sizeof(struct rotary_encoder
), GFP_KERNEL
);
192 mutex_init(&encoder
->access_mutex
);
194 device_property_read_u32(dev
, "rotary-encoder,steps", &encoder
->steps
);
196 err
= device_property_read_u32(dev
, "rotary-encoder,steps-per-period",
200 * The 'half-period' property has been deprecated, you must
201 * use 'steps-per-period' and set an appropriate value, but
202 * we still need to parse it to maintain compatibility. If
203 * neither property is present we fall back to the one step
204 * per period behavior.
206 steps_per_period
= device_property_read_bool(dev
,
207 "rotary-encoder,half-period") ? 2 : 1;
211 device_property_read_bool(dev
, "rotary-encoder,rollover");
213 if (!device_property_present(dev
, "rotary-encoder,encoding") ||
214 !device_property_match_string(dev
, "rotary-encoder,encoding",
216 dev_info(dev
, "gray");
217 encoder
->encoding
= ROTENC_GRAY
;
218 } else if (!device_property_match_string(dev
, "rotary-encoder,encoding",
220 dev_info(dev
, "binary");
221 encoder
->encoding
= ROTENC_BINARY
;
223 dev_err(dev
, "unknown encoding setting\n");
227 device_property_read_u32(dev
, "linux,axis", &encoder
->axis
);
228 encoder
->relative_axis
=
229 device_property_read_bool(dev
, "rotary-encoder,relative-axis");
231 encoder
->gpios
= devm_gpiod_get_array(dev
, NULL
, GPIOD_IN
);
232 if (IS_ERR(encoder
->gpios
))
233 return dev_err_probe(dev
, PTR_ERR(encoder
->gpios
), "unable to get gpios\n");
234 if (encoder
->gpios
->ndescs
< 2) {
235 dev_err(dev
, "not enough gpios found\n");
239 input
= devm_input_allocate_device(dev
);
243 encoder
->input
= input
;
245 input
->name
= pdev
->name
;
246 input
->id
.bustype
= BUS_HOST
;
248 if (encoder
->relative_axis
)
249 input_set_capability(input
, EV_REL
, encoder
->axis
);
251 input_set_abs_params(input
,
252 encoder
->axis
, 0, encoder
->steps
, 0, 1);
254 switch (steps_per_period
>> (encoder
->gpios
->ndescs
- 2)) {
256 handler
= &rotary_encoder_quarter_period_irq
;
257 encoder
->last_stable
= rotary_encoder_get_state(encoder
);
260 handler
= &rotary_encoder_half_period_irq
;
261 encoder
->last_stable
= rotary_encoder_get_state(encoder
);
264 handler
= &rotary_encoder_irq
;
267 dev_err(dev
, "'%d' is not a valid steps-per-period value\n",
274 encoder
->gpios
->ndescs
, sizeof(*encoder
->irq
),
279 for (i
= 0; i
< encoder
->gpios
->ndescs
; ++i
) {
280 encoder
->irq
[i
] = gpiod_to_irq(encoder
->gpios
->desc
[i
]);
282 err
= devm_request_threaded_irq(dev
, encoder
->irq
[i
],
284 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
|
288 dev_err(dev
, "unable to request IRQ %d (gpio#%d)\n",
294 err
= input_register_device(input
);
296 dev_err(dev
, "failed to register input device\n");
300 device_init_wakeup(dev
,
301 device_property_read_bool(dev
, "wakeup-source"));
303 platform_set_drvdata(pdev
, encoder
);
308 static int rotary_encoder_suspend(struct device
*dev
)
310 struct rotary_encoder
*encoder
= dev_get_drvdata(dev
);
313 if (device_may_wakeup(dev
)) {
314 for (i
= 0; i
< encoder
->gpios
->ndescs
; ++i
)
315 enable_irq_wake(encoder
->irq
[i
]);
321 static int rotary_encoder_resume(struct device
*dev
)
323 struct rotary_encoder
*encoder
= dev_get_drvdata(dev
);
326 if (device_may_wakeup(dev
)) {
327 for (i
= 0; i
< encoder
->gpios
->ndescs
; ++i
)
328 disable_irq_wake(encoder
->irq
[i
]);
334 static DEFINE_SIMPLE_DEV_PM_OPS(rotary_encoder_pm_ops
,
335 rotary_encoder_suspend
, rotary_encoder_resume
);
338 static const struct of_device_id rotary_encoder_of_match
[] = {
339 { .compatible
= "rotary-encoder", },
342 MODULE_DEVICE_TABLE(of
, rotary_encoder_of_match
);
345 static struct platform_driver rotary_encoder_driver
= {
346 .probe
= rotary_encoder_probe
,
349 .pm
= pm_sleep_ptr(&rotary_encoder_pm_ops
),
350 .of_match_table
= of_match_ptr(rotary_encoder_of_match
),
353 module_platform_driver(rotary_encoder_driver
);
355 MODULE_ALIAS("platform:" DRV_NAME
);
356 MODULE_DESCRIPTION("GPIO rotary encoder driver");
357 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>, Johan Hovold");
358 MODULE_LICENSE("GPL v2");