4 * (c) 2009 Daniel Mack <daniel@caiaq.de>
6 * state machine code inspired by code from Tim Ruetz
8 * A generic driver for rotary encoders connected to GPIO lines.
9 * See file:Documentation/input/rotary_encoder.txt for more information
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.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 #define DRV_NAME "rotary-encoder"
29 struct rotary_encoder
{
30 struct input_dev
*input
;
31 struct rotary_encoder_platform_data
*pdata
;
40 unsigned char dir
; /* 0 - clockwise, 1 - CCW */
43 static int rotary_encoder_get_state(struct rotary_encoder_platform_data
*pdata
)
45 int a
= !!gpio_get_value(pdata
->gpio_a
);
46 int b
= !!gpio_get_value(pdata
->gpio_b
);
48 a
^= pdata
->inverted_a
;
49 b
^= pdata
->inverted_b
;
51 return ((a
<< 1) | b
);
54 static void rotary_encoder_report_event(struct rotary_encoder
*encoder
)
56 struct rotary_encoder_platform_data
*pdata
= encoder
->pdata
;
58 if (pdata
->relative_axis
) {
59 input_report_rel(encoder
->input
,
60 pdata
->axis
, encoder
->dir
? -1 : 1);
62 unsigned int pos
= encoder
->pos
;
65 /* turning counter-clockwise */
71 /* turning clockwise */
72 if (pdata
->rollover
|| pos
< pdata
->steps
)
80 input_report_abs(encoder
->input
, pdata
->axis
, encoder
->pos
);
83 input_sync(encoder
->input
);
86 static irqreturn_t
rotary_encoder_irq(int irq
, void *dev_id
)
88 struct rotary_encoder
*encoder
= dev_id
;
91 state
= rotary_encoder_get_state(encoder
->pdata
);
96 rotary_encoder_report_event(encoder
);
97 encoder
->armed
= false;
104 encoder
->dir
= state
- 1;
108 encoder
->armed
= true;
115 static int __devinit
rotary_encoder_probe(struct platform_device
*pdev
)
117 struct rotary_encoder_platform_data
*pdata
= pdev
->dev
.platform_data
;
118 struct rotary_encoder
*encoder
;
119 struct input_dev
*input
;
123 dev_err(&pdev
->dev
, "missing platform data\n");
127 encoder
= kzalloc(sizeof(struct rotary_encoder
), GFP_KERNEL
);
128 input
= input_allocate_device();
129 if (!encoder
|| !input
) {
130 dev_err(&pdev
->dev
, "failed to allocate memory for device\n");
135 encoder
->input
= input
;
136 encoder
->pdata
= pdata
;
137 encoder
->irq_a
= gpio_to_irq(pdata
->gpio_a
);
138 encoder
->irq_b
= gpio_to_irq(pdata
->gpio_b
);
140 /* create and register the input driver */
141 input
->name
= pdev
->name
;
142 input
->id
.bustype
= BUS_HOST
;
143 input
->dev
.parent
= &pdev
->dev
;
145 if (pdata
->relative_axis
) {
146 input
->evbit
[0] = BIT_MASK(EV_REL
);
147 input
->relbit
[0] = BIT_MASK(pdata
->axis
);
149 input
->evbit
[0] = BIT_MASK(EV_ABS
);
150 input_set_abs_params(encoder
->input
,
151 pdata
->axis
, 0, pdata
->steps
, 0, 1);
154 err
= input_register_device(input
);
156 dev_err(&pdev
->dev
, "failed to register input device\n");
160 /* request the GPIOs */
161 err
= gpio_request(pdata
->gpio_a
, DRV_NAME
);
163 dev_err(&pdev
->dev
, "unable to request GPIO %d\n",
165 goto exit_unregister_input
;
168 err
= gpio_direction_input(pdata
->gpio_a
);
170 dev_err(&pdev
->dev
, "unable to set GPIO %d for input\n",
172 goto exit_unregister_input
;
175 err
= gpio_request(pdata
->gpio_b
, DRV_NAME
);
177 dev_err(&pdev
->dev
, "unable to request GPIO %d\n",
179 goto exit_free_gpio_a
;
182 err
= gpio_direction_input(pdata
->gpio_b
);
184 dev_err(&pdev
->dev
, "unable to set GPIO %d for input\n",
186 goto exit_free_gpio_a
;
189 /* request the IRQs */
190 err
= request_irq(encoder
->irq_a
, &rotary_encoder_irq
,
191 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
194 dev_err(&pdev
->dev
, "unable to request IRQ %d\n",
196 goto exit_free_gpio_b
;
199 err
= request_irq(encoder
->irq_b
, &rotary_encoder_irq
,
200 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
203 dev_err(&pdev
->dev
, "unable to request IRQ %d\n",
205 goto exit_free_irq_a
;
208 platform_set_drvdata(pdev
, encoder
);
213 free_irq(encoder
->irq_a
, encoder
);
215 gpio_free(pdata
->gpio_b
);
217 gpio_free(pdata
->gpio_a
);
218 exit_unregister_input
:
219 input_unregister_device(input
);
220 input
= NULL
; /* so we don't try to free it */
222 input_free_device(input
);
227 static int __devexit
rotary_encoder_remove(struct platform_device
*pdev
)
229 struct rotary_encoder
*encoder
= platform_get_drvdata(pdev
);
230 struct rotary_encoder_platform_data
*pdata
= pdev
->dev
.platform_data
;
232 free_irq(encoder
->irq_a
, encoder
);
233 free_irq(encoder
->irq_b
, encoder
);
234 gpio_free(pdata
->gpio_a
);
235 gpio_free(pdata
->gpio_b
);
236 input_unregister_device(encoder
->input
);
237 platform_set_drvdata(pdev
, NULL
);
243 static struct platform_driver rotary_encoder_driver
= {
244 .probe
= rotary_encoder_probe
,
245 .remove
= __devexit_p(rotary_encoder_remove
),
248 .owner
= THIS_MODULE
,
252 static int __init
rotary_encoder_init(void)
254 return platform_driver_register(&rotary_encoder_driver
);
257 static void __exit
rotary_encoder_exit(void)
259 platform_driver_unregister(&rotary_encoder_driver
);
262 module_init(rotary_encoder_init
);
263 module_exit(rotary_encoder_exit
);
265 MODULE_ALIAS("platform:" DRV_NAME
);
266 MODULE_DESCRIPTION("GPIO rotary encoder driver");
267 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
268 MODULE_LICENSE("GPL v2");