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/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/input.h>
22 #include <linux/device.h>
23 #include <linux/platform_device.h>
24 #include <linux/gpio.h>
25 #include <linux/rotary_encoder.h>
26 #include <linux/slab.h>
28 #define DRV_NAME "rotary-encoder"
30 struct rotary_encoder
{
31 struct input_dev
*input
;
32 struct rotary_encoder_platform_data
*pdata
;
41 unsigned char dir
; /* 0 - clockwise, 1 - CCW */
46 static int rotary_encoder_get_state(struct rotary_encoder_platform_data
*pdata
)
48 int a
= !!gpio_get_value(pdata
->gpio_a
);
49 int b
= !!gpio_get_value(pdata
->gpio_b
);
51 a
^= pdata
->inverted_a
;
52 b
^= pdata
->inverted_b
;
54 return ((a
<< 1) | b
);
57 static void rotary_encoder_report_event(struct rotary_encoder
*encoder
)
59 struct rotary_encoder_platform_data
*pdata
= encoder
->pdata
;
61 if (pdata
->relative_axis
) {
62 input_report_rel(encoder
->input
,
63 pdata
->axis
, encoder
->dir
? -1 : 1);
65 unsigned int pos
= encoder
->pos
;
68 /* turning counter-clockwise */
74 /* turning clockwise */
75 if (pdata
->rollover
|| pos
< pdata
->steps
)
83 input_report_abs(encoder
->input
, pdata
->axis
, encoder
->pos
);
86 input_sync(encoder
->input
);
89 static irqreturn_t
rotary_encoder_irq(int irq
, void *dev_id
)
91 struct rotary_encoder
*encoder
= dev_id
;
94 state
= rotary_encoder_get_state(encoder
->pdata
);
99 rotary_encoder_report_event(encoder
);
100 encoder
->armed
= false;
107 encoder
->dir
= state
- 1;
111 encoder
->armed
= true;
118 static irqreturn_t
rotary_encoder_half_period_irq(int irq
, void *dev_id
)
120 struct rotary_encoder
*encoder
= dev_id
;
123 state
= rotary_encoder_get_state(encoder
->pdata
);
128 if (state
!= encoder
->last_stable
) {
129 rotary_encoder_report_event(encoder
);
130 encoder
->last_stable
= state
;
136 encoder
->dir
= (encoder
->last_stable
+ state
) & 0x01;
143 static int __devinit
rotary_encoder_probe(struct platform_device
*pdev
)
145 struct rotary_encoder_platform_data
*pdata
= pdev
->dev
.platform_data
;
146 struct rotary_encoder
*encoder
;
147 struct input_dev
*input
;
148 irq_handler_t handler
;
152 dev_err(&pdev
->dev
, "missing platform data\n");
156 encoder
= kzalloc(sizeof(struct rotary_encoder
), GFP_KERNEL
);
157 input
= input_allocate_device();
158 if (!encoder
|| !input
) {
159 dev_err(&pdev
->dev
, "failed to allocate memory for device\n");
164 encoder
->input
= input
;
165 encoder
->pdata
= pdata
;
166 encoder
->irq_a
= gpio_to_irq(pdata
->gpio_a
);
167 encoder
->irq_b
= gpio_to_irq(pdata
->gpio_b
);
169 /* create and register the input driver */
170 input
->name
= pdev
->name
;
171 input
->id
.bustype
= BUS_HOST
;
172 input
->dev
.parent
= &pdev
->dev
;
174 if (pdata
->relative_axis
) {
175 input
->evbit
[0] = BIT_MASK(EV_REL
);
176 input
->relbit
[0] = BIT_MASK(pdata
->axis
);
178 input
->evbit
[0] = BIT_MASK(EV_ABS
);
179 input_set_abs_params(encoder
->input
,
180 pdata
->axis
, 0, pdata
->steps
, 0, 1);
183 err
= input_register_device(input
);
185 dev_err(&pdev
->dev
, "failed to register input device\n");
189 /* request the GPIOs */
190 err
= gpio_request(pdata
->gpio_a
, DRV_NAME
);
192 dev_err(&pdev
->dev
, "unable to request GPIO %d\n",
194 goto exit_unregister_input
;
197 err
= gpio_direction_input(pdata
->gpio_a
);
199 dev_err(&pdev
->dev
, "unable to set GPIO %d for input\n",
201 goto exit_unregister_input
;
204 err
= gpio_request(pdata
->gpio_b
, DRV_NAME
);
206 dev_err(&pdev
->dev
, "unable to request GPIO %d\n",
208 goto exit_free_gpio_a
;
211 err
= gpio_direction_input(pdata
->gpio_b
);
213 dev_err(&pdev
->dev
, "unable to set GPIO %d for input\n",
215 goto exit_free_gpio_a
;
218 /* request the IRQs */
219 if (pdata
->half_period
) {
220 handler
= &rotary_encoder_half_period_irq
;
221 encoder
->last_stable
= rotary_encoder_get_state(pdata
);
223 handler
= &rotary_encoder_irq
;
226 err
= request_irq(encoder
->irq_a
, handler
,
227 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
230 dev_err(&pdev
->dev
, "unable to request IRQ %d\n",
232 goto exit_free_gpio_b
;
235 err
= request_irq(encoder
->irq_b
, handler
,
236 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
239 dev_err(&pdev
->dev
, "unable to request IRQ %d\n",
241 goto exit_free_irq_a
;
244 platform_set_drvdata(pdev
, encoder
);
249 free_irq(encoder
->irq_a
, encoder
);
251 gpio_free(pdata
->gpio_b
);
253 gpio_free(pdata
->gpio_a
);
254 exit_unregister_input
:
255 input_unregister_device(input
);
256 input
= NULL
; /* so we don't try to free it */
258 input_free_device(input
);
263 static int __devexit
rotary_encoder_remove(struct platform_device
*pdev
)
265 struct rotary_encoder
*encoder
= platform_get_drvdata(pdev
);
266 struct rotary_encoder_platform_data
*pdata
= pdev
->dev
.platform_data
;
268 free_irq(encoder
->irq_a
, encoder
);
269 free_irq(encoder
->irq_b
, encoder
);
270 gpio_free(pdata
->gpio_a
);
271 gpio_free(pdata
->gpio_b
);
272 input_unregister_device(encoder
->input
);
273 platform_set_drvdata(pdev
, NULL
);
279 static struct platform_driver rotary_encoder_driver
= {
280 .probe
= rotary_encoder_probe
,
281 .remove
= __devexit_p(rotary_encoder_remove
),
284 .owner
= THIS_MODULE
,
288 static int __init
rotary_encoder_init(void)
290 return platform_driver_register(&rotary_encoder_driver
);
293 static void __exit
rotary_encoder_exit(void)
295 platform_driver_unregister(&rotary_encoder_driver
);
298 module_init(rotary_encoder_init
);
299 module_exit(rotary_encoder_exit
);
301 MODULE_ALIAS("platform:" DRV_NAME
);
302 MODULE_DESCRIPTION("GPIO rotary encoder driver");
303 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>, Johan Hovold");
304 MODULE_LICENSE("GPL v2");