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/rotary-encoder.txt 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.h>
24 #include <linux/rotary_encoder.h>
25 #include <linux/slab.h>
27 #include <linux/of_platform.h>
28 #include <linux/of_gpio.h>
31 #define DRV_NAME "rotary-encoder"
33 struct rotary_encoder
{
34 struct input_dev
*input
;
35 const struct rotary_encoder_platform_data
*pdata
;
44 unsigned char dir
; /* 0 - clockwise, 1 - CCW */
49 static int rotary_encoder_get_state(const struct rotary_encoder_platform_data
*pdata
)
51 int a
= !!gpio_get_value(pdata
->gpio_a
);
52 int b
= !!gpio_get_value(pdata
->gpio_b
);
54 a
^= pdata
->inverted_a
;
55 b
^= pdata
->inverted_b
;
57 return ((a
<< 1) | b
);
60 static void rotary_encoder_report_event(struct rotary_encoder
*encoder
)
62 const struct rotary_encoder_platform_data
*pdata
= encoder
->pdata
;
64 if (pdata
->relative_axis
) {
65 input_report_rel(encoder
->input
,
66 pdata
->axis
, encoder
->dir
? -1 : 1);
68 unsigned int pos
= encoder
->pos
;
71 /* turning counter-clockwise */
77 /* turning clockwise */
78 if (pdata
->rollover
|| pos
< pdata
->steps
)
86 input_report_abs(encoder
->input
, pdata
->axis
, encoder
->pos
);
89 input_sync(encoder
->input
);
92 static irqreturn_t
rotary_encoder_irq(int irq
, void *dev_id
)
94 struct rotary_encoder
*encoder
= dev_id
;
97 state
= rotary_encoder_get_state(encoder
->pdata
);
101 if (encoder
->armed
) {
102 rotary_encoder_report_event(encoder
);
103 encoder
->armed
= false;
110 encoder
->dir
= state
- 1;
114 encoder
->armed
= true;
121 static irqreturn_t
rotary_encoder_half_period_irq(int irq
, void *dev_id
)
123 struct rotary_encoder
*encoder
= dev_id
;
126 state
= rotary_encoder_get_state(encoder
->pdata
);
131 if (state
!= encoder
->last_stable
) {
132 rotary_encoder_report_event(encoder
);
133 encoder
->last_stable
= state
;
139 encoder
->dir
= (encoder
->last_stable
+ state
) & 0x01;
146 static irqreturn_t
rotary_encoder_quarter_period_irq(int irq
, void *dev_id
)
148 struct rotary_encoder
*encoder
= dev_id
;
152 state
= rotary_encoder_get_state(encoder
->pdata
);
155 * We encode the previous and the current state using a byte.
156 * The previous state in the MSB nibble, the current state in the LSB
157 * nibble. Then use a table to decide the direction of the turn.
159 sum
= (encoder
->last_stable
<< 4) + state
;
165 encoder
->dir
= 0; /* clockwise */
172 encoder
->dir
= 1; /* counter-clockwise */
177 * Ignore all other values. This covers the case when the
178 * state didn't change (a spurious interrupt) and the
179 * cases where the state changed by two steps, making it
180 * impossible to tell the direction.
182 * In either case, don't report any event and save the
188 rotary_encoder_report_event(encoder
);
191 encoder
->last_stable
= state
;
196 static const struct of_device_id rotary_encoder_of_match
[] = {
197 { .compatible
= "rotary-encoder", },
200 MODULE_DEVICE_TABLE(of
, rotary_encoder_of_match
);
202 static struct rotary_encoder_platform_data
*rotary_encoder_parse_dt(struct device
*dev
)
204 const struct of_device_id
*of_id
=
205 of_match_device(rotary_encoder_of_match
, dev
);
206 struct device_node
*np
= dev
->of_node
;
207 struct rotary_encoder_platform_data
*pdata
;
208 enum of_gpio_flags flags
;
214 pdata
= kzalloc(sizeof(struct rotary_encoder_platform_data
),
217 return ERR_PTR(-ENOMEM
);
219 of_property_read_u32(np
, "rotary-encoder,steps", &pdata
->steps
);
220 of_property_read_u32(np
, "linux,axis", &pdata
->axis
);
222 pdata
->gpio_a
= of_get_gpio_flags(np
, 0, &flags
);
223 pdata
->inverted_a
= flags
& OF_GPIO_ACTIVE_LOW
;
225 pdata
->gpio_b
= of_get_gpio_flags(np
, 1, &flags
);
226 pdata
->inverted_b
= flags
& OF_GPIO_ACTIVE_LOW
;
228 pdata
->relative_axis
=
229 of_property_read_bool(np
, "rotary-encoder,relative-axis");
230 pdata
->rollover
= of_property_read_bool(np
, "rotary-encoder,rollover");
232 error
= of_property_read_u32(np
, "rotary-encoder,steps-per-period",
233 &pdata
->steps_per_period
);
236 * The 'half-period' property has been deprecated, you must use
237 * 'steps-per-period' and set an appropriate value, but we still
238 * need to parse it to maintain compatibility.
240 if (of_property_read_bool(np
, "rotary-encoder,half-period")) {
241 pdata
->steps_per_period
= 2;
243 /* Fallback to one step per period behavior */
244 pdata
->steps_per_period
= 1;
248 pdata
->wakeup_source
= of_property_read_bool(np
, "wakeup-source");
253 static inline struct rotary_encoder_platform_data
*
254 rotary_encoder_parse_dt(struct device
*dev
)
260 static int rotary_encoder_probe(struct platform_device
*pdev
)
262 struct device
*dev
= &pdev
->dev
;
263 const struct rotary_encoder_platform_data
*pdata
= dev_get_platdata(dev
);
264 struct rotary_encoder
*encoder
;
265 struct input_dev
*input
;
266 irq_handler_t handler
;
270 pdata
= rotary_encoder_parse_dt(dev
);
272 return PTR_ERR(pdata
);
275 dev_err(dev
, "missing platform data\n");
280 encoder
= kzalloc(sizeof(struct rotary_encoder
), GFP_KERNEL
);
281 input
= input_allocate_device();
282 if (!encoder
|| !input
) {
287 encoder
->input
= input
;
288 encoder
->pdata
= pdata
;
290 input
->name
= pdev
->name
;
291 input
->id
.bustype
= BUS_HOST
;
292 input
->dev
.parent
= dev
;
294 if (pdata
->relative_axis
) {
295 input
->evbit
[0] = BIT_MASK(EV_REL
);
296 input
->relbit
[0] = BIT_MASK(pdata
->axis
);
298 input
->evbit
[0] = BIT_MASK(EV_ABS
);
299 input_set_abs_params(encoder
->input
,
300 pdata
->axis
, 0, pdata
->steps
, 0, 1);
303 /* request the GPIOs */
304 err
= gpio_request_one(pdata
->gpio_a
, GPIOF_IN
, dev_name(dev
));
306 dev_err(dev
, "unable to request GPIO %d\n", pdata
->gpio_a
);
310 err
= gpio_request_one(pdata
->gpio_b
, GPIOF_IN
, dev_name(dev
));
312 dev_err(dev
, "unable to request GPIO %d\n", pdata
->gpio_b
);
313 goto exit_free_gpio_a
;
316 encoder
->irq_a
= gpio_to_irq(pdata
->gpio_a
);
317 encoder
->irq_b
= gpio_to_irq(pdata
->gpio_b
);
319 switch (pdata
->steps_per_period
) {
321 handler
= &rotary_encoder_quarter_period_irq
;
322 encoder
->last_stable
= rotary_encoder_get_state(pdata
);
325 handler
= &rotary_encoder_half_period_irq
;
326 encoder
->last_stable
= rotary_encoder_get_state(pdata
);
329 handler
= &rotary_encoder_irq
;
332 dev_err(dev
, "'%d' is not a valid steps-per-period value\n",
333 pdata
->steps_per_period
);
335 goto exit_free_gpio_b
;
338 err
= request_irq(encoder
->irq_a
, handler
,
339 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
342 dev_err(dev
, "unable to request IRQ %d\n", encoder
->irq_a
);
343 goto exit_free_gpio_b
;
346 err
= request_irq(encoder
->irq_b
, handler
,
347 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
350 dev_err(dev
, "unable to request IRQ %d\n", encoder
->irq_b
);
351 goto exit_free_irq_a
;
354 err
= input_register_device(input
);
356 dev_err(dev
, "failed to register input device\n");
357 goto exit_free_irq_b
;
360 device_init_wakeup(&pdev
->dev
, pdata
->wakeup_source
);
362 platform_set_drvdata(pdev
, encoder
);
367 free_irq(encoder
->irq_b
, encoder
);
369 free_irq(encoder
->irq_a
, encoder
);
371 gpio_free(pdata
->gpio_b
);
373 gpio_free(pdata
->gpio_a
);
375 input_free_device(input
);
377 if (!dev_get_platdata(&pdev
->dev
))
383 static int rotary_encoder_remove(struct platform_device
*pdev
)
385 struct rotary_encoder
*encoder
= platform_get_drvdata(pdev
);
386 const struct rotary_encoder_platform_data
*pdata
= encoder
->pdata
;
388 device_init_wakeup(&pdev
->dev
, false);
390 free_irq(encoder
->irq_a
, encoder
);
391 free_irq(encoder
->irq_b
, encoder
);
392 gpio_free(pdata
->gpio_a
);
393 gpio_free(pdata
->gpio_b
);
395 input_unregister_device(encoder
->input
);
398 if (!dev_get_platdata(&pdev
->dev
))
404 #ifdef CONFIG_PM_SLEEP
405 static int rotary_encoder_suspend(struct device
*dev
)
407 struct rotary_encoder
*encoder
= dev_get_drvdata(dev
);
409 if (device_may_wakeup(dev
)) {
410 enable_irq_wake(encoder
->irq_a
);
411 enable_irq_wake(encoder
->irq_b
);
417 static int rotary_encoder_resume(struct device
*dev
)
419 struct rotary_encoder
*encoder
= dev_get_drvdata(dev
);
421 if (device_may_wakeup(dev
)) {
422 disable_irq_wake(encoder
->irq_a
);
423 disable_irq_wake(encoder
->irq_b
);
430 static SIMPLE_DEV_PM_OPS(rotary_encoder_pm_ops
,
431 rotary_encoder_suspend
, rotary_encoder_resume
);
433 static struct platform_driver rotary_encoder_driver
= {
434 .probe
= rotary_encoder_probe
,
435 .remove
= rotary_encoder_remove
,
438 .pm
= &rotary_encoder_pm_ops
,
439 .of_match_table
= of_match_ptr(rotary_encoder_of_match
),
442 module_platform_driver(rotary_encoder_driver
);
444 MODULE_ALIAS("platform:" DRV_NAME
);
445 MODULE_DESCRIPTION("GPIO rotary encoder driver");
446 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>, Johan Hovold");
447 MODULE_LICENSE("GPL v2");