4 * (c) 2009 Daniel Mack <daniel@caiaq.de>
5 * Copyright (C) 2011 Johan Hovold <jhovold@gmail.com>
7 * state machine code inspired by code from Tim Ruetz
9 * A generic driver for rotary encoders connected to GPIO lines.
10 * See file:Documentation/input/devices/rotary-encoder.rst for more information
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/interrupt.h>
20 #include <linux/input.h>
21 #include <linux/device.h>
22 #include <linux/platform_device.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/slab.h>
27 #include <linux/property.h>
29 #define DRV_NAME "rotary-encoder"
31 enum rotary_encoder_encoding
{
36 struct rotary_encoder
{
37 struct input_dev
*input
;
39 struct mutex access_mutex
;
45 enum rotary_encoder_encoding encoding
;
49 struct gpio_descs
*gpios
;
54 signed char dir
; /* 1 - clockwise, -1 - CCW */
56 unsigned int last_stable
;
59 static unsigned int rotary_encoder_get_state(struct rotary_encoder
*encoder
)
64 for (i
= 0; i
< encoder
->gpios
->ndescs
; ++i
) {
65 int val
= gpiod_get_value_cansleep(encoder
->gpios
->desc
[i
]);
67 /* convert from gray encoding to normal */
68 if (encoder
->encoding
== ROTENC_GRAY
&& ret
& 1)
77 static void rotary_encoder_report_event(struct rotary_encoder
*encoder
)
79 if (encoder
->relative_axis
) {
80 input_report_rel(encoder
->input
,
81 encoder
->axis
, encoder
->dir
);
83 unsigned int pos
= encoder
->pos
;
85 if (encoder
->dir
< 0) {
86 /* turning counter-clockwise */
87 if (encoder
->rollover
)
88 pos
+= encoder
->steps
;
92 /* turning clockwise */
93 if (encoder
->rollover
|| pos
< encoder
->steps
)
97 if (encoder
->rollover
)
98 pos
%= encoder
->steps
;
101 input_report_abs(encoder
->input
, encoder
->axis
, encoder
->pos
);
104 input_sync(encoder
->input
);
107 static irqreturn_t
rotary_encoder_irq(int irq
, void *dev_id
)
109 struct rotary_encoder
*encoder
= dev_id
;
112 mutex_lock(&encoder
->access_mutex
);
114 state
= rotary_encoder_get_state(encoder
);
118 if (encoder
->armed
) {
119 rotary_encoder_report_event(encoder
);
120 encoder
->armed
= false;
127 encoder
->dir
= 2 - state
;
131 encoder
->armed
= true;
135 mutex_unlock(&encoder
->access_mutex
);
140 static irqreturn_t
rotary_encoder_half_period_irq(int irq
, void *dev_id
)
142 struct rotary_encoder
*encoder
= dev_id
;
145 mutex_lock(&encoder
->access_mutex
);
147 state
= rotary_encoder_get_state(encoder
);
150 encoder
->dir
= ((encoder
->last_stable
- state
+ 1) % 4) - 1;
152 if (state
!= encoder
->last_stable
) {
153 rotary_encoder_report_event(encoder
);
154 encoder
->last_stable
= state
;
158 mutex_unlock(&encoder
->access_mutex
);
163 static irqreturn_t
rotary_encoder_quarter_period_irq(int irq
, void *dev_id
)
165 struct rotary_encoder
*encoder
= dev_id
;
168 mutex_lock(&encoder
->access_mutex
);
170 state
= rotary_encoder_get_state(encoder
);
172 if ((encoder
->last_stable
+ 1) % 4 == state
)
174 else if (encoder
->last_stable
== (state
+ 1) % 4)
179 rotary_encoder_report_event(encoder
);
182 encoder
->last_stable
= state
;
183 mutex_unlock(&encoder
->access_mutex
);
188 static int rotary_encoder_probe(struct platform_device
*pdev
)
190 struct device
*dev
= &pdev
->dev
;
191 struct rotary_encoder
*encoder
;
192 struct input_dev
*input
;
193 irq_handler_t handler
;
194 u32 steps_per_period
;
198 encoder
= devm_kzalloc(dev
, sizeof(struct rotary_encoder
), GFP_KERNEL
);
202 mutex_init(&encoder
->access_mutex
);
204 device_property_read_u32(dev
, "rotary-encoder,steps", &encoder
->steps
);
206 err
= device_property_read_u32(dev
, "rotary-encoder,steps-per-period",
210 * The 'half-period' property has been deprecated, you must
211 * use 'steps-per-period' and set an appropriate value, but
212 * we still need to parse it to maintain compatibility. If
213 * neither property is present we fall back to the one step
214 * per period behavior.
216 steps_per_period
= device_property_read_bool(dev
,
217 "rotary-encoder,half-period") ? 2 : 1;
221 device_property_read_bool(dev
, "rotary-encoder,rollover");
223 if (!device_property_present(dev
, "rotary-encoder,encoding") ||
224 !device_property_match_string(dev
, "rotary-encoder,encoding",
226 dev_info(dev
, "gray");
227 encoder
->encoding
= ROTENC_GRAY
;
228 } else if (!device_property_match_string(dev
, "rotary-encoder,encoding",
230 dev_info(dev
, "binary");
231 encoder
->encoding
= ROTENC_BINARY
;
233 dev_err(dev
, "unknown encoding setting\n");
237 device_property_read_u32(dev
, "linux,axis", &encoder
->axis
);
238 encoder
->relative_axis
=
239 device_property_read_bool(dev
, "rotary-encoder,relative-axis");
241 encoder
->gpios
= devm_gpiod_get_array(dev
, NULL
, GPIOD_IN
);
242 if (IS_ERR(encoder
->gpios
)) {
243 dev_err(dev
, "unable to get gpios\n");
244 return PTR_ERR(encoder
->gpios
);
246 if (encoder
->gpios
->ndescs
< 2) {
247 dev_err(dev
, "not enough gpios found\n");
251 input
= devm_input_allocate_device(dev
);
255 encoder
->input
= input
;
257 input
->name
= pdev
->name
;
258 input
->id
.bustype
= BUS_HOST
;
259 input
->dev
.parent
= dev
;
261 if (encoder
->relative_axis
)
262 input_set_capability(input
, EV_REL
, encoder
->axis
);
264 input_set_abs_params(input
,
265 encoder
->axis
, 0, encoder
->steps
, 0, 1);
267 switch (steps_per_period
>> (encoder
->gpios
->ndescs
- 2)) {
269 handler
= &rotary_encoder_quarter_period_irq
;
270 encoder
->last_stable
= rotary_encoder_get_state(encoder
);
273 handler
= &rotary_encoder_half_period_irq
;
274 encoder
->last_stable
= rotary_encoder_get_state(encoder
);
277 handler
= &rotary_encoder_irq
;
280 dev_err(dev
, "'%d' is not a valid steps-per-period value\n",
287 encoder
->gpios
->ndescs
, sizeof(*encoder
->irq
),
292 for (i
= 0; i
< encoder
->gpios
->ndescs
; ++i
) {
293 encoder
->irq
[i
] = gpiod_to_irq(encoder
->gpios
->desc
[i
]);
295 err
= devm_request_threaded_irq(dev
, encoder
->irq
[i
],
297 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
|
301 dev_err(dev
, "unable to request IRQ %d (gpio#%d)\n",
307 err
= input_register_device(input
);
309 dev_err(dev
, "failed to register input device\n");
313 device_init_wakeup(dev
,
314 device_property_read_bool(dev
, "wakeup-source"));
316 platform_set_drvdata(pdev
, encoder
);
321 static int __maybe_unused
rotary_encoder_suspend(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 enable_irq_wake(encoder
->irq
[i
]);
334 static int __maybe_unused
rotary_encoder_resume(struct device
*dev
)
336 struct rotary_encoder
*encoder
= dev_get_drvdata(dev
);
339 if (device_may_wakeup(dev
)) {
340 for (i
= 0; i
< encoder
->gpios
->ndescs
; ++i
)
341 disable_irq_wake(encoder
->irq
[i
]);
347 static SIMPLE_DEV_PM_OPS(rotary_encoder_pm_ops
,
348 rotary_encoder_suspend
, rotary_encoder_resume
);
351 static const struct of_device_id rotary_encoder_of_match
[] = {
352 { .compatible
= "rotary-encoder", },
355 MODULE_DEVICE_TABLE(of
, rotary_encoder_of_match
);
358 static struct platform_driver rotary_encoder_driver
= {
359 .probe
= rotary_encoder_probe
,
362 .pm
= &rotary_encoder_pm_ops
,
363 .of_match_table
= of_match_ptr(rotary_encoder_of_match
),
366 module_platform_driver(rotary_encoder_driver
);
368 MODULE_ALIAS("platform:" DRV_NAME
);
369 MODULE_DESCRIPTION("GPIO rotary encoder driver");
370 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>, Johan Hovold");
371 MODULE_LICENSE("GPL v2");