blob: 1f8e0108962eff45fc03a1fc0257e39486f4aaed [file] [log] [blame]
Daniel Mack73969ff2009-03-04 23:27:14 -08001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Daniel Mack73969ff2009-03-04 23:27:14 -080026
27#define DRV_NAME "rotary-encoder"
28
29struct rotary_encoder {
Daniel Mack73969ff2009-03-04 23:27:14 -080030 struct input_dev *input;
31 struct rotary_encoder_platform_data *pdata;
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -070032
33 unsigned int axis;
34 unsigned int pos;
35
36 unsigned int irq_a;
37 unsigned int irq_b;
38
39 bool armed;
40 unsigned char dir; /* 0 - clockwise, 1 - CCW */
Daniel Mack73969ff2009-03-04 23:27:14 -080041};
42
43static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
44{
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);
49 int state;
50
51 a ^= pdata->inverted_a;
52 b ^= pdata->inverted_b;
53 state = (a << 1) | b;
54
55 switch (state) {
56
57 case 0x0:
58 if (!encoder->armed)
59 break;
60
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -070061 if (pdata->relative_axis) {
62 input_report_rel(encoder->input, pdata->axis,
63 encoder->dir ? -1 : 1);
Daniel Mack73969ff2009-03-04 23:27:14 -080064 } else {
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -070065 unsigned int pos = encoder->pos;
Daniel Mack73969ff2009-03-04 23:27:14 -080066
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -070067 if (encoder->dir) {
68 /* turning counter-clockwise */
69 if (pdata->rollover)
70 pos += pdata->steps;
71 if (pos)
72 pos--;
73 } else {
74 /* turning clockwise */
75 if (pdata->rollover || pos < pdata->steps)
76 pos++;
77 }
78 if (pdata->rollover)
79 pos %= pdata->steps;
80 encoder->pos = pos;
81 input_report_abs(encoder->input, pdata->axis,
82 encoder->pos);
83 }
Daniel Mack73969ff2009-03-04 23:27:14 -080084 input_sync(encoder->input);
85
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -070086 encoder->armed = false;
Daniel Mack73969ff2009-03-04 23:27:14 -080087 break;
88
89 case 0x1:
90 case 0x2:
91 if (encoder->armed)
92 encoder->dir = state - 1;
93 break;
94
95 case 0x3:
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -070096 encoder->armed = true;
Daniel Mack73969ff2009-03-04 23:27:14 -080097 break;
98 }
99
100 return IRQ_HANDLED;
101}
102
103static int __devinit rotary_encoder_probe(struct platform_device *pdev)
104{
105 struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
106 struct rotary_encoder *encoder;
107 struct input_dev *input;
108 int err;
109
H Hartley Sweeten06ee3d32009-10-05 21:43:42 -0700110 if (!pdata) {
111 dev_err(&pdev->dev, "missing platform data\n");
Daniel Mack73969ff2009-03-04 23:27:14 -0800112 return -ENOENT;
113 }
114
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");
119 err = -ENOMEM;
120 goto exit_free_mem;
121 }
122
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);
127
128 /* create and register the input driver */
129 input->name = pdev->name;
130 input->id.bustype = BUS_HOST;
131 input->dev.parent = &pdev->dev;
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -0700132
133 if (pdata->relative_axis) {
134 input->evbit[0] = BIT_MASK(EV_REL);
135 input->relbit[0] = BIT_MASK(pdata->axis);
136 } else {
137 input->evbit[0] = BIT_MASK(EV_ABS);
138 input_set_abs_params(encoder->input,
139 pdata->axis, 0, pdata->steps, 0, 1);
140 }
Daniel Mack73969ff2009-03-04 23:27:14 -0800141
142 err = input_register_device(input);
143 if (err) {
144 dev_err(&pdev->dev, "failed to register input device\n");
145 goto exit_free_mem;
146 }
147
148 /* request the GPIOs */
149 err = gpio_request(pdata->gpio_a, DRV_NAME);
150 if (err) {
151 dev_err(&pdev->dev, "unable to request GPIO %d\n",
152 pdata->gpio_a);
153 goto exit_unregister_input;
154 }
155
Andrew Clayton5deeac92010-02-10 23:18:05 -0800156 err = gpio_direction_input(pdata->gpio_a);
157 if (err) {
158 dev_err(&pdev->dev, "unable to set GPIO %d for input\n",
159 pdata->gpio_a);
160 goto exit_unregister_input;
161 }
162
Daniel Mack73969ff2009-03-04 23:27:14 -0800163 err = gpio_request(pdata->gpio_b, DRV_NAME);
164 if (err) {
165 dev_err(&pdev->dev, "unable to request GPIO %d\n",
166 pdata->gpio_b);
167 goto exit_free_gpio_a;
168 }
169
Andrew Clayton5deeac92010-02-10 23:18:05 -0800170 err = gpio_direction_input(pdata->gpio_b);
171 if (err) {
172 dev_err(&pdev->dev, "unable to set GPIO %d for input\n",
173 pdata->gpio_b);
174 goto exit_free_gpio_a;
175 }
176
Daniel Mack73969ff2009-03-04 23:27:14 -0800177 /* request the IRQs */
178 err = request_irq(encoder->irq_a, &rotary_encoder_irq,
179 IORESOURCE_IRQ_HIGHEDGE | IORESOURCE_IRQ_LOWEDGE,
180 DRV_NAME, encoder);
181 if (err) {
182 dev_err(&pdev->dev, "unable to request IRQ %d\n",
183 encoder->irq_a);
184 goto exit_free_gpio_b;
185 }
186
187 err = request_irq(encoder->irq_b, &rotary_encoder_irq,
188 IORESOURCE_IRQ_HIGHEDGE | IORESOURCE_IRQ_LOWEDGE,
189 DRV_NAME, encoder);
190 if (err) {
191 dev_err(&pdev->dev, "unable to request IRQ %d\n",
192 encoder->irq_b);
193 goto exit_free_irq_a;
194 }
195
196 platform_set_drvdata(pdev, encoder);
197
198 return 0;
199
200exit_free_irq_a:
201 free_irq(encoder->irq_a, encoder);
202exit_free_gpio_b:
203 gpio_free(pdata->gpio_b);
204exit_free_gpio_a:
205 gpio_free(pdata->gpio_a);
206exit_unregister_input:
207 input_unregister_device(input);
208 input = NULL; /* so we don't try to free it */
209exit_free_mem:
210 input_free_device(input);
211 kfree(encoder);
212 return err;
213}
214
215static int __devexit rotary_encoder_remove(struct platform_device *pdev)
216{
217 struct rotary_encoder *encoder = platform_get_drvdata(pdev);
218 struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
219
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);
226 kfree(encoder);
227
228 return 0;
229}
230
231static struct platform_driver rotary_encoder_driver = {
232 .probe = rotary_encoder_probe,
233 .remove = __devexit_p(rotary_encoder_remove),
234 .driver = {
235 .name = DRV_NAME,
236 .owner = THIS_MODULE,
237 }
238};
239
240static int __init rotary_encoder_init(void)
241{
242 return platform_driver_register(&rotary_encoder_driver);
243}
244
245static void __exit rotary_encoder_exit(void)
246{
247 platform_driver_unregister(&rotary_encoder_driver);
248}
249
250module_init(rotary_encoder_init);
251module_exit(rotary_encoder_exit);
252
253MODULE_ALIAS("platform:" DRV_NAME);
254MODULE_DESCRIPTION("GPIO rotary encoder driver");
255MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
256MODULE_LICENSE("GPL v2");
257