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 irqreturn_t
rotary_encoder_irq(int irq
, void *dev_id
)
45 struct rotary_encoder
*encoder
= dev_id
;
46 struct rotary_encoder_platform_data
*pdata
= encoder
->pdata
;
47 int a
= !!gpio_get_value(pdata
->gpio_a
);
48 int b
= !!gpio_get_value(pdata
->gpio_b
);
51 a
^= pdata
->inverted_a
;
52 b
^= pdata
->inverted_b
;
61 if (pdata
->relative_axis
) {
62 input_report_rel(encoder
->input
, pdata
->axis
,
63 encoder
->dir
? -1 : 1);
65 unsigned int pos
= encoder
->pos
;
68 /* turning counter-clockwise */
74 /* turning clockwise */
75 if (pdata
->rollover
|| pos
< pdata
->steps
)
81 input_report_abs(encoder
->input
, pdata
->axis
,
84 input_sync(encoder
->input
);
86 encoder
->armed
= false;
92 encoder
->dir
= state
- 1;
96 encoder
->armed
= true;
103 static int __devinit
rotary_encoder_probe(struct platform_device
*pdev
)
105 struct rotary_encoder_platform_data
*pdata
= pdev
->dev
.platform_data
;
106 struct rotary_encoder
*encoder
;
107 struct input_dev
*input
;
111 dev_err(&pdev
->dev
, "missing platform data\n");
115 encoder
= kzalloc(sizeof(struct rotary_encoder
), GFP_KERNEL
);
116 input
= input_allocate_device();
117 if (!encoder
|| !input
) {
118 dev_err(&pdev
->dev
, "failed to allocate memory for device\n");
123 encoder
->input
= input
;
124 encoder
->pdata
= pdata
;
125 encoder
->irq_a
= gpio_to_irq(pdata
->gpio_a
);
126 encoder
->irq_b
= gpio_to_irq(pdata
->gpio_b
);
128 /* create and register the input driver */
129 input
->name
= pdev
->name
;
130 input
->id
.bustype
= BUS_HOST
;
131 input
->dev
.parent
= &pdev
->dev
;
133 if (pdata
->relative_axis
) {
134 input
->evbit
[0] = BIT_MASK(EV_REL
);
135 input
->relbit
[0] = BIT_MASK(pdata
->axis
);
137 input
->evbit
[0] = BIT_MASK(EV_ABS
);
138 input_set_abs_params(encoder
->input
,
139 pdata
->axis
, 0, pdata
->steps
, 0, 1);
142 err
= input_register_device(input
);
144 dev_err(&pdev
->dev
, "failed to register input device\n");
148 /* request the GPIOs */
149 err
= gpio_request(pdata
->gpio_a
, DRV_NAME
);
151 dev_err(&pdev
->dev
, "unable to request GPIO %d\n",
153 goto exit_unregister_input
;
156 err
= gpio_direction_input(pdata
->gpio_a
);
158 dev_err(&pdev
->dev
, "unable to set GPIO %d for input\n",
160 goto exit_unregister_input
;
163 err
= gpio_request(pdata
->gpio_b
, DRV_NAME
);
165 dev_err(&pdev
->dev
, "unable to request GPIO %d\n",
167 goto exit_free_gpio_a
;
170 err
= gpio_direction_input(pdata
->gpio_b
);
172 dev_err(&pdev
->dev
, "unable to set GPIO %d for input\n",
174 goto exit_free_gpio_a
;
177 /* request the IRQs */
178 err
= request_irq(encoder
->irq_a
, &rotary_encoder_irq
,
179 IORESOURCE_IRQ_HIGHEDGE
| IORESOURCE_IRQ_LOWEDGE
,
182 dev_err(&pdev
->dev
, "unable to request IRQ %d\n",
184 goto exit_free_gpio_b
;
187 err
= request_irq(encoder
->irq_b
, &rotary_encoder_irq
,
188 IORESOURCE_IRQ_HIGHEDGE
| IORESOURCE_IRQ_LOWEDGE
,
191 dev_err(&pdev
->dev
, "unable to request IRQ %d\n",
193 goto exit_free_irq_a
;
196 platform_set_drvdata(pdev
, encoder
);
201 free_irq(encoder
->irq_a
, encoder
);
203 gpio_free(pdata
->gpio_b
);
205 gpio_free(pdata
->gpio_a
);
206 exit_unregister_input
:
207 input_unregister_device(input
);
208 input
= NULL
; /* so we don't try to free it */
210 input_free_device(input
);
215 static int __devexit
rotary_encoder_remove(struct platform_device
*pdev
)
217 struct rotary_encoder
*encoder
= platform_get_drvdata(pdev
);
218 struct rotary_encoder_platform_data
*pdata
= pdev
->dev
.platform_data
;
220 free_irq(encoder
->irq_a
, encoder
);
221 free_irq(encoder
->irq_b
, encoder
);
222 gpio_free(pdata
->gpio_a
);
223 gpio_free(pdata
->gpio_b
);
224 input_unregister_device(encoder
->input
);
225 platform_set_drvdata(pdev
, NULL
);
231 static struct platform_driver rotary_encoder_driver
= {
232 .probe
= rotary_encoder_probe
,
233 .remove
= __devexit_p(rotary_encoder_remove
),
236 .owner
= THIS_MODULE
,
240 static int __init
rotary_encoder_init(void)
242 return platform_driver_register(&rotary_encoder_driver
);
245 static void __exit
rotary_encoder_exit(void)
247 platform_driver_unregister(&rotary_encoder_driver
);
250 module_init(rotary_encoder_init
);
251 module_exit(rotary_encoder_exit
);
253 MODULE_ALIAS("platform:" DRV_NAME
);
254 MODULE_DESCRIPTION("GPIO rotary encoder driver");
255 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
256 MODULE_LICENSE("GPL v2");