diff options
author | Daniel Mack <daniel@caiaq.de> | 2009-03-05 02:27:14 -0500 |
---|---|---|
committer | Dmitry Torokhov <dmitry.torokhov@gmail.com> | 2009-03-08 19:35:53 -0400 |
commit | 73969ff0eda233f140bcbed1251431387b43f383 (patch) | |
tree | b7dd2b670d9e03916b04469e9d38b71d69c6cd60 | |
parent | b0ecc7309443dbcf1a0ce2d93f39f5d92c124d42 (diff) |
Input: generic driver for rotary encoders on GPIOs
This patch adds a generic driver for rotary encoders connected to GPIO
pins of a system. It relies on gpiolib and generic hardware irqs. The
documentation that also comes with this patch explains the concept and
how to use the driver.
Signed-off-by: Daniel Mack <daniel@caiaq.de>
Tested-by: H Hartley Sweeten <hsweeten@visionengravers.com>
Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
-rw-r--r-- | Documentation/input/rotary-encoder.txt | 101 | ||||
-rw-r--r-- | drivers/input/misc/Kconfig | 11 | ||||
-rw-r--r-- | drivers/input/misc/Makefile | 2 | ||||
-rw-r--r-- | drivers/input/misc/rotary_encoder.c | 221 | ||||
-rw-r--r-- | include/linux/rotary_encoder.h | 13 |
5 files changed, 348 insertions, 0 deletions
diff --git a/Documentation/input/rotary-encoder.txt b/Documentation/input/rotary-encoder.txt new file mode 100644 index 000000000000..435102a26d96 --- /dev/null +++ b/Documentation/input/rotary-encoder.txt | |||
@@ -0,0 +1,101 @@ | |||
1 | rotary-encoder - a generic driver for GPIO connected devices | ||
2 | Daniel Mack <daniel@caiaq.de>, Feb 2009 | ||
3 | |||
4 | 0. Function | ||
5 | ----------- | ||
6 | |||
7 | Rotary encoders are devices which are connected to the CPU or other | ||
8 | peripherals with two wires. The outputs are phase-shifted by 90 degrees | ||
9 | and by triggering on falling and rising edges, the turn direction can | ||
10 | be determined. | ||
11 | |||
12 | The phase diagram of these two outputs look like this: | ||
13 | |||
14 | _____ _____ _____ | ||
15 | | | | | | | | ||
16 | Channel A ____| |_____| |_____| |____ | ||
17 | |||
18 | : : : : : : : : : : : : | ||
19 | __ _____ _____ _____ | ||
20 | | | | | | | | | ||
21 | Channel B |_____| |_____| |_____| |__ | ||
22 | |||
23 | : : : : : : : : : : : : | ||
24 | Event a b c d a b c d a b c d | ||
25 | |||
26 | |<-------->| | ||
27 | one step | ||
28 | |||
29 | |||
30 | For more information, please see | ||
31 | http://en.wikipedia.org/wiki/Rotary_encoder | ||
32 | |||
33 | |||
34 | 1. Events / state machine | ||
35 | ------------------------- | ||
36 | |||
37 | a) Rising edge on channel A, channel B in low state | ||
38 | This state is used to recognize a clockwise turn | ||
39 | |||
40 | b) Rising edge on channel B, channel A in high state | ||
41 | When entering this state, the encoder is put into 'armed' state, | ||
42 | meaning that there it has seen half the way of a one-step transition. | ||
43 | |||
44 | c) Falling edge on channel A, channel B in high state | ||
45 | This state is used to recognize a counter-clockwise turn | ||
46 | |||
47 | d) Falling edge on channel B, channel A in low state | ||
48 | Parking position. If the encoder enters this state, a full transition | ||
49 | should have happend, unless it flipped back on half the way. The | ||
50 | 'armed' state tells us about that. | ||
51 | |||
52 | 2. Platform requirements | ||
53 | ------------------------ | ||
54 | |||
55 | As there is no hardware dependent call in this driver, the platform it is | ||
56 | used with must support gpiolib. Another requirement is that IRQs must be | ||
57 | able to fire on both edges. | ||
58 | |||
59 | |||
60 | 3. Board integration | ||
61 | -------------------- | ||
62 | |||
63 | To use this driver in your system, register a platform_device with the | ||
64 | name 'rotary-encoder' and associate the IRQs and some specific platform | ||
65 | data with it. | ||
66 | |||
67 | struct rotary_encoder_platform_data is declared in | ||
68 | include/linux/rotary-encoder.h and needs to be filled with the number of | ||
69 | steps the encoder has and can carry information about externally inverted | ||
70 | signals (because of used invertig buffer or other reasons). | ||
71 | |||
72 | Because GPIO to IRQ mapping is platform specific, this information must | ||
73 | be given in seperately to the driver. See the example below. | ||
74 | |||
75 | ---------<snip>--------- | ||
76 | |||
77 | /* board support file example */ | ||
78 | |||
79 | #include <linux/input.h> | ||
80 | #include <linux/rotary_encoder.h> | ||
81 | |||
82 | #define GPIO_ROTARY_A 1 | ||
83 | #define GPIO_ROTARY_B 2 | ||
84 | |||
85 | static struct rotary_encoder_platform_data my_rotary_encoder_info = { | ||
86 | .steps = 24, | ||
87 | .axis = ABS_X, | ||
88 | .gpio_a = GPIO_ROTARY_A, | ||
89 | .gpio_b = GPIO_ROTARY_B, | ||
90 | .inverted_a = 0, | ||
91 | .inverted_b = 0, | ||
92 | }; | ||
93 | |||
94 | static struct platform_device rotary_encoder_device = { | ||
95 | .name = "rotary-encoder", | ||
96 | .id = 0, | ||
97 | .dev = { | ||
98 | .platform_data = &my_rotary_encoder_info, | ||
99 | } | ||
100 | }; | ||
101 | |||
diff --git a/drivers/input/misc/Kconfig b/drivers/input/misc/Kconfig index 67e5553f699a..806d2e66d249 100644 --- a/drivers/input/misc/Kconfig +++ b/drivers/input/misc/Kconfig | |||
@@ -227,4 +227,15 @@ config INPUT_PCF50633_PMU | |||
227 | Say Y to include support for delivering PMU events via input | 227 | Say Y to include support for delivering PMU events via input |
228 | layer on NXP PCF50633. | 228 | layer on NXP PCF50633. |
229 | 229 | ||
230 | config INPUT_GPIO_ROTARY_ENCODER | ||
231 | tristate "Rotary encoders connected to GPIO pins" | ||
232 | depends on GPIOLIB && GENERIC_GPIO | ||
233 | help | ||
234 | Say Y here to add support for rotary encoders connected to GPIO lines. | ||
235 | Check file:Documentation/incput/rotary_encoder.txt for more | ||
236 | information. | ||
237 | |||
238 | To compile this driver as a module, choose M here: the | ||
239 | module will be called rotary_encoder. | ||
240 | |||
230 | endif | 241 | endif |
diff --git a/drivers/input/misc/Makefile b/drivers/input/misc/Makefile index bb62e6efacf3..e86cee66c914 100644 --- a/drivers/input/misc/Makefile +++ b/drivers/input/misc/Makefile | |||
@@ -22,3 +22,5 @@ obj-$(CONFIG_INPUT_UINPUT) += uinput.o | |||
22 | obj-$(CONFIG_INPUT_APANEL) += apanel.o | 22 | obj-$(CONFIG_INPUT_APANEL) += apanel.o |
23 | obj-$(CONFIG_INPUT_SGI_BTNS) += sgi_btns.o | 23 | obj-$(CONFIG_INPUT_SGI_BTNS) += sgi_btns.o |
24 | obj-$(CONFIG_INPUT_PCF50633_PMU) += pcf50633-input.o | 24 | obj-$(CONFIG_INPUT_PCF50633_PMU) += pcf50633-input.o |
25 | obj-$(CONFIG_INPUT_GPIO_ROTARY_ENCODER) += rotary_encoder.o | ||
26 | |||
diff --git a/drivers/input/misc/rotary_encoder.c b/drivers/input/misc/rotary_encoder.c new file mode 100644 index 000000000000..5bb3ab51b8c6 --- /dev/null +++ b/drivers/input/misc/rotary_encoder.c | |||
@@ -0,0 +1,221 @@ | |||
1 | /* | ||
2 | * rotary_encoder.c | ||
3 | * | ||
4 | * (c) 2009 Daniel Mack <daniel@caiaq.de> | ||
5 | * | ||
6 | * state machine code inspired by code from Tim Ruetz | ||
7 | * | ||
8 | * A generic driver for rotary encoders connected to GPIO lines. | ||
9 | * See file:Documentation/input/rotary_encoder.txt for more information | ||
10 | * | ||
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. | ||
14 | */ | ||
15 | |||
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 | |||
26 | #define DRV_NAME "rotary-encoder" | ||
27 | |||
28 | struct rotary_encoder { | ||
29 | unsigned int irq_a; | ||
30 | unsigned int irq_b; | ||
31 | unsigned int pos; | ||
32 | unsigned int armed; | ||
33 | unsigned int dir; | ||
34 | struct input_dev *input; | ||
35 | struct rotary_encoder_platform_data *pdata; | ||
36 | }; | ||
37 | |||
38 | static irqreturn_t rotary_encoder_irq(int irq, void *dev_id) | ||
39 | { | ||
40 | struct rotary_encoder *encoder = dev_id; | ||
41 | struct rotary_encoder_platform_data *pdata = encoder->pdata; | ||
42 | int a = !!gpio_get_value(pdata->gpio_a); | ||
43 | int b = !!gpio_get_value(pdata->gpio_b); | ||
44 | int state; | ||
45 | |||
46 | a ^= pdata->inverted_a; | ||
47 | b ^= pdata->inverted_b; | ||
48 | state = (a << 1) | b; | ||
49 | |||
50 | switch (state) { | ||
51 | |||
52 | case 0x0: | ||
53 | if (!encoder->armed) | ||
54 | break; | ||
55 | |||
56 | if (encoder->dir) { | ||
57 | /* turning counter-clockwise */ | ||
58 | encoder->pos += pdata->steps; | ||
59 | encoder->pos--; | ||
60 | encoder->pos %= pdata->steps; | ||
61 | } else { | ||
62 | /* turning clockwise */ | ||
63 | encoder->pos++; | ||
64 | encoder->pos %= pdata->steps; | ||
65 | } | ||
66 | |||
67 | input_report_abs(encoder->input, pdata->axis, encoder->pos); | ||
68 | input_sync(encoder->input); | ||
69 | |||
70 | encoder->armed = 0; | ||
71 | break; | ||
72 | |||
73 | case 0x1: | ||
74 | case 0x2: | ||
75 | if (encoder->armed) | ||
76 | encoder->dir = state - 1; | ||
77 | break; | ||
78 | |||
79 | case 0x3: | ||
80 | encoder->armed = 1; | ||
81 | break; | ||
82 | } | ||
83 | |||
84 | return IRQ_HANDLED; | ||
85 | } | ||
86 | |||
87 | static int __devinit rotary_encoder_probe(struct platform_device *pdev) | ||
88 | { | ||
89 | struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data; | ||
90 | struct rotary_encoder *encoder; | ||
91 | struct input_dev *input; | ||
92 | int err; | ||
93 | |||
94 | if (!pdata || !pdata->steps) { | ||
95 | dev_err(&pdev->dev, "invalid platform data\n"); | ||
96 | return -ENOENT; | ||
97 | } | ||
98 | |||
99 | encoder = kzalloc(sizeof(struct rotary_encoder), GFP_KERNEL); | ||
100 | input = input_allocate_device(); | ||
101 | if (!encoder || !input) { | ||
102 | dev_err(&pdev->dev, "failed to allocate memory for device\n"); | ||
103 | err = -ENOMEM; | ||
104 | goto exit_free_mem; | ||
105 | } | ||
106 | |||
107 | encoder->input = input; | ||
108 | encoder->pdata = pdata; | ||
109 | encoder->irq_a = gpio_to_irq(pdata->gpio_a); | ||
110 | encoder->irq_b = gpio_to_irq(pdata->gpio_b); | ||
111 | |||
112 | /* create and register the input driver */ | ||
113 | input->name = pdev->name; | ||
114 | input->id.bustype = BUS_HOST; | ||
115 | input->dev.parent = &pdev->dev; | ||
116 | input->evbit[0] = BIT_MASK(EV_ABS); | ||
117 | input_set_abs_params(encoder->input, | ||
118 | pdata->axis, 0, pdata->steps, 0, 1); | ||
119 | |||
120 | err = input_register_device(input); | ||
121 | if (err) { | ||
122 | dev_err(&pdev->dev, "failed to register input device\n"); | ||
123 | goto exit_free_mem; | ||
124 | } | ||
125 | |||
126 | /* request the GPIOs */ | ||
127 | err = gpio_request(pdata->gpio_a, DRV_NAME); | ||
128 | if (err) { | ||
129 | dev_err(&pdev->dev, "unable to request GPIO %d\n", | ||
130 | pdata->gpio_a); | ||
131 | goto exit_unregister_input; | ||
132 | } | ||
133 | |||
134 | err = gpio_request(pdata->gpio_b, DRV_NAME); | ||
135 | if (err) { | ||
136 | dev_err(&pdev->dev, "unable to request GPIO %d\n", | ||
137 | pdata->gpio_b); | ||
138 | goto exit_free_gpio_a; | ||
139 | } | ||
140 | |||
141 | /* request the IRQs */ | ||
142 | err = request_irq(encoder->irq_a, &rotary_encoder_irq, | ||
143 | IORESOURCE_IRQ_HIGHEDGE | IORESOURCE_IRQ_LOWEDGE, | ||
144 | DRV_NAME, encoder); | ||
145 | if (err) { | ||
146 | dev_err(&pdev->dev, "unable to request IRQ %d\n", | ||
147 | encoder->irq_a); | ||
148 | goto exit_free_gpio_b; | ||
149 | } | ||
150 | |||
151 | err = request_irq(encoder->irq_b, &rotary_encoder_irq, | ||
152 | IORESOURCE_IRQ_HIGHEDGE | IORESOURCE_IRQ_LOWEDGE, | ||
153 | DRV_NAME, encoder); | ||
154 | if (err) { | ||
155 | dev_err(&pdev->dev, "unable to request IRQ %d\n", | ||
156 | encoder->irq_b); | ||
157 | goto exit_free_irq_a; | ||
158 | } | ||
159 | |||
160 | platform_set_drvdata(pdev, encoder); | ||
161 | |||
162 | return 0; | ||
163 | |||
164 | exit_free_irq_a: | ||
165 | free_irq(encoder->irq_a, encoder); | ||
166 | exit_free_gpio_b: | ||
167 | gpio_free(pdata->gpio_b); | ||
168 | exit_free_gpio_a: | ||
169 | gpio_free(pdata->gpio_a); | ||
170 | exit_unregister_input: | ||
171 | input_unregister_device(input); | ||
172 | input = NULL; /* so we don't try to free it */ | ||
173 | exit_free_mem: | ||
174 | input_free_device(input); | ||
175 | kfree(encoder); | ||
176 | return err; | ||
177 | } | ||
178 | |||
179 | static int __devexit rotary_encoder_remove(struct platform_device *pdev) | ||
180 | { | ||
181 | struct rotary_encoder *encoder = platform_get_drvdata(pdev); | ||
182 | struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data; | ||
183 | |||
184 | free_irq(encoder->irq_a, encoder); | ||
185 | free_irq(encoder->irq_b, encoder); | ||
186 | gpio_free(pdata->gpio_a); | ||
187 | gpio_free(pdata->gpio_b); | ||
188 | input_unregister_device(encoder->input); | ||
189 | platform_set_drvdata(pdev, NULL); | ||
190 | kfree(encoder); | ||
191 | |||
192 | return 0; | ||
193 | } | ||
194 | |||
195 | static struct platform_driver rotary_encoder_driver = { | ||
196 | .probe = rotary_encoder_probe, | ||
197 | .remove = __devexit_p(rotary_encoder_remove), | ||
198 | .driver = { | ||
199 | .name = DRV_NAME, | ||
200 | .owner = THIS_MODULE, | ||
201 | } | ||
202 | }; | ||
203 | |||
204 | static int __init rotary_encoder_init(void) | ||
205 | { | ||
206 | return platform_driver_register(&rotary_encoder_driver); | ||
207 | } | ||
208 | |||
209 | static void __exit rotary_encoder_exit(void) | ||
210 | { | ||
211 | platform_driver_unregister(&rotary_encoder_driver); | ||
212 | } | ||
213 | |||
214 | module_init(rotary_encoder_init); | ||
215 | module_exit(rotary_encoder_exit); | ||
216 | |||
217 | MODULE_ALIAS("platform:" DRV_NAME); | ||
218 | MODULE_DESCRIPTION("GPIO rotary encoder driver"); | ||
219 | MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); | ||
220 | MODULE_LICENSE("GPL v2"); | ||
221 | |||
diff --git a/include/linux/rotary_encoder.h b/include/linux/rotary_encoder.h new file mode 100644 index 000000000000..12d63a30c347 --- /dev/null +++ b/include/linux/rotary_encoder.h | |||
@@ -0,0 +1,13 @@ | |||
1 | #ifndef __ROTARY_ENCODER_H__ | ||
2 | #define __ROTARY_ENCODER_H__ | ||
3 | |||
4 | struct rotary_encoder_platform_data { | ||
5 | unsigned int steps; | ||
6 | unsigned int axis; | ||
7 | unsigned int gpio_a; | ||
8 | unsigned int gpio_b; | ||
9 | unsigned int inverted_a; | ||
10 | unsigned int inverted_b; | ||
11 | }; | ||
12 | |||
13 | #endif /* __ROTARY_ENCODER_H__ */ | ||