blob: 00a7bdabb0b3dccd2d2c107ee692727dc72a7629 [file] [log] [blame]
Daniel Mack73969ff2009-03-04 23:27:14 -08001/*
2 * rotary_encoder.c
3 *
4 * (c) 2009 Daniel Mack <daniel@caiaq.de>
Johan Hovolde70bdd42011-05-11 16:35:30 -07005 * Copyright (C) 2011 Johan Hovold <jhovold@gmail.com>
Daniel Mack73969ff2009-03-04 23:27:14 -08006 *
7 * state machine code inspired by code from Tim Ruetz
8 *
9 * A generic driver for rotary encoders connected to GPIO lines.
Paul Bolle395cf962011-08-15 02:02:26 +020010 * See file:Documentation/input/rotary-encoder.txt for more information
Daniel Mack73969ff2009-03-04 23:27:14 -080011 *
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.
15 */
16
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Daniel Mack73969ff2009-03-04 23:27:14 -080027
28#define DRV_NAME "rotary-encoder"
29
30struct rotary_encoder {
Daniel Mack73969ff2009-03-04 23:27:14 -080031 struct input_dev *input;
32 struct rotary_encoder_platform_data *pdata;
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -070033
34 unsigned int axis;
35 unsigned int pos;
36
37 unsigned int irq_a;
38 unsigned int irq_b;
39
40 bool armed;
41 unsigned char dir; /* 0 - clockwise, 1 - CCW */
Johan Hovolde70bdd42011-05-11 16:35:30 -070042
43 char last_stable;
Daniel Mack73969ff2009-03-04 23:27:14 -080044};
45
Johan Hovold521a8f52011-05-11 16:33:50 -070046static int rotary_encoder_get_state(struct rotary_encoder_platform_data *pdata)
Daniel Mack73969ff2009-03-04 23:27:14 -080047{
Daniel Mack73969ff2009-03-04 23:27:14 -080048 int a = !!gpio_get_value(pdata->gpio_a);
49 int b = !!gpio_get_value(pdata->gpio_b);
Daniel Mack73969ff2009-03-04 23:27:14 -080050
51 a ^= pdata->inverted_a;
52 b ^= pdata->inverted_b;
Johan Hovold521a8f52011-05-11 16:33:50 -070053
54 return ((a << 1) | b);
55}
56
57static void rotary_encoder_report_event(struct rotary_encoder *encoder)
58{
59 struct rotary_encoder_platform_data *pdata = encoder->pdata;
60
61 if (pdata->relative_axis) {
62 input_report_rel(encoder->input,
63 pdata->axis, encoder->dir ? -1 : 1);
64 } else {
65 unsigned int pos = encoder->pos;
66
67 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
79 if (pdata->rollover)
80 pos %= pdata->steps;
81
82 encoder->pos = pos;
83 input_report_abs(encoder->input, pdata->axis, encoder->pos);
84 }
85
86 input_sync(encoder->input);
87}
88
89static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
90{
91 struct rotary_encoder *encoder = dev_id;
92 int state;
93
94 state = rotary_encoder_get_state(encoder->pdata);
Daniel Mack73969ff2009-03-04 23:27:14 -080095
96 switch (state) {
Daniel Mack73969ff2009-03-04 23:27:14 -080097 case 0x0:
Johan Hovold521a8f52011-05-11 16:33:50 -070098 if (encoder->armed) {
99 rotary_encoder_report_event(encoder);
100 encoder->armed = false;
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -0700101 }
Daniel Mack73969ff2009-03-04 23:27:14 -0800102 break;
103
104 case 0x1:
105 case 0x2:
106 if (encoder->armed)
107 encoder->dir = state - 1;
108 break;
109
110 case 0x3:
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -0700111 encoder->armed = true;
Daniel Mack73969ff2009-03-04 23:27:14 -0800112 break;
113 }
114
115 return IRQ_HANDLED;
116}
117
Johan Hovolde70bdd42011-05-11 16:35:30 -0700118static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id)
119{
120 struct rotary_encoder *encoder = dev_id;
121 int state;
122
123 state = rotary_encoder_get_state(encoder->pdata);
124
125 switch (state) {
126 case 0x00:
127 case 0x03:
128 if (state != encoder->last_stable) {
129 rotary_encoder_report_event(encoder);
130 encoder->last_stable = state;
131 }
132 break;
133
134 case 0x01:
135 case 0x02:
136 encoder->dir = (encoder->last_stable + state) & 0x01;
137 break;
138 }
139
140 return IRQ_HANDLED;
141}
142
Daniel Mack73969ff2009-03-04 23:27:14 -0800143static int __devinit rotary_encoder_probe(struct platform_device *pdev)
144{
145 struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
146 struct rotary_encoder *encoder;
147 struct input_dev *input;
Johan Hovolde70bdd42011-05-11 16:35:30 -0700148 irq_handler_t handler;
Daniel Mack73969ff2009-03-04 23:27:14 -0800149 int err;
150
H Hartley Sweeten06ee3d32009-10-05 21:43:42 -0700151 if (!pdata) {
152 dev_err(&pdev->dev, "missing platform data\n");
Daniel Mack73969ff2009-03-04 23:27:14 -0800153 return -ENOENT;
154 }
155
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");
160 err = -ENOMEM;
161 goto exit_free_mem;
162 }
163
164 encoder->input = input;
165 encoder->pdata = pdata;
Daniel Mack73969ff2009-03-04 23:27:14 -0800166
167 /* create and register the input driver */
168 input->name = pdev->name;
169 input->id.bustype = BUS_HOST;
170 input->dev.parent = &pdev->dev;
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -0700171
172 if (pdata->relative_axis) {
173 input->evbit[0] = BIT_MASK(EV_REL);
174 input->relbit[0] = BIT_MASK(pdata->axis);
175 } else {
176 input->evbit[0] = BIT_MASK(EV_ABS);
177 input_set_abs_params(encoder->input,
178 pdata->axis, 0, pdata->steps, 0, 1);
179 }
Daniel Mack73969ff2009-03-04 23:27:14 -0800180
181 err = input_register_device(input);
182 if (err) {
183 dev_err(&pdev->dev, "failed to register input device\n");
184 goto exit_free_mem;
185 }
186
187 /* request the GPIOs */
188 err = gpio_request(pdata->gpio_a, DRV_NAME);
189 if (err) {
190 dev_err(&pdev->dev, "unable to request GPIO %d\n",
191 pdata->gpio_a);
192 goto exit_unregister_input;
193 }
194
Andrew Clayton5deeac92010-02-10 23:18:05 -0800195 err = gpio_direction_input(pdata->gpio_a);
196 if (err) {
197 dev_err(&pdev->dev, "unable to set GPIO %d for input\n",
198 pdata->gpio_a);
199 goto exit_unregister_input;
200 }
201
Daniel Mack73969ff2009-03-04 23:27:14 -0800202 err = gpio_request(pdata->gpio_b, DRV_NAME);
203 if (err) {
204 dev_err(&pdev->dev, "unable to request GPIO %d\n",
205 pdata->gpio_b);
206 goto exit_free_gpio_a;
207 }
208
Andrew Clayton5deeac92010-02-10 23:18:05 -0800209 err = gpio_direction_input(pdata->gpio_b);
210 if (err) {
211 dev_err(&pdev->dev, "unable to set GPIO %d for input\n",
212 pdata->gpio_b);
213 goto exit_free_gpio_a;
214 }
215
Daniel Macka78769b2012-07-31 22:08:47 -0700216 encoder->irq_a = gpio_to_irq(pdata->gpio_a);
217 encoder->irq_b = gpio_to_irq(pdata->gpio_b);
218
Daniel Mack73969ff2009-03-04 23:27:14 -0800219 /* request the IRQs */
Johan Hovolde70bdd42011-05-11 16:35:30 -0700220 if (pdata->half_period) {
221 handler = &rotary_encoder_half_period_irq;
222 encoder->last_stable = rotary_encoder_get_state(pdata);
223 } else {
224 handler = &rotary_encoder_irq;
225 }
226
227 err = request_irq(encoder->irq_a, handler,
Alexander Steine0d5f4c2011-02-02 22:59:54 -0800228 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
Daniel Mack73969ff2009-03-04 23:27:14 -0800229 DRV_NAME, encoder);
230 if (err) {
231 dev_err(&pdev->dev, "unable to request IRQ %d\n",
232 encoder->irq_a);
233 goto exit_free_gpio_b;
234 }
235
Johan Hovolde70bdd42011-05-11 16:35:30 -0700236 err = request_irq(encoder->irq_b, handler,
Alexander Steine0d5f4c2011-02-02 22:59:54 -0800237 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
Daniel Mack73969ff2009-03-04 23:27:14 -0800238 DRV_NAME, encoder);
239 if (err) {
240 dev_err(&pdev->dev, "unable to request IRQ %d\n",
241 encoder->irq_b);
242 goto exit_free_irq_a;
243 }
244
245 platform_set_drvdata(pdev, encoder);
246
247 return 0;
248
249exit_free_irq_a:
250 free_irq(encoder->irq_a, encoder);
251exit_free_gpio_b:
252 gpio_free(pdata->gpio_b);
253exit_free_gpio_a:
254 gpio_free(pdata->gpio_a);
255exit_unregister_input:
256 input_unregister_device(input);
257 input = NULL; /* so we don't try to free it */
258exit_free_mem:
259 input_free_device(input);
260 kfree(encoder);
261 return err;
262}
263
264static int __devexit rotary_encoder_remove(struct platform_device *pdev)
265{
266 struct rotary_encoder *encoder = platform_get_drvdata(pdev);
267 struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
268
269 free_irq(encoder->irq_a, encoder);
270 free_irq(encoder->irq_b, encoder);
271 gpio_free(pdata->gpio_a);
272 gpio_free(pdata->gpio_b);
273 input_unregister_device(encoder->input);
274 platform_set_drvdata(pdev, NULL);
275 kfree(encoder);
276
277 return 0;
278}
279
280static struct platform_driver rotary_encoder_driver = {
281 .probe = rotary_encoder_probe,
282 .remove = __devexit_p(rotary_encoder_remove),
283 .driver = {
284 .name = DRV_NAME,
285 .owner = THIS_MODULE,
286 }
287};
JJ Ding840a7462011-11-29 11:08:40 -0800288module_platform_driver(rotary_encoder_driver);
Daniel Mack73969ff2009-03-04 23:27:14 -0800289
290MODULE_ALIAS("platform:" DRV_NAME);
291MODULE_DESCRIPTION("GPIO rotary encoder driver");
Johan Hovolde70bdd42011-05-11 16:35:30 -0700292MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>, Johan Hovold");
Daniel Mack73969ff2009-03-04 23:27:14 -0800293MODULE_LICENSE("GPL v2");