blob: 8aee7198643001eb9252877b327569e4111744af [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>
Daniel Mack73969ff2009-03-04 23:27:14 -080019#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>
Sachin Kamat2e45e532013-10-06 00:56:24 -070026#include <linux/of.h>
Daniel Mack80c99bc2012-07-31 22:08:50 -070027#include <linux/of_platform.h>
28#include <linux/of_gpio.h>
Sylvain Rochet47ec6e52015-10-13 23:24:36 -070029#include <linux/pm.h>
Daniel Mack73969ff2009-03-04 23:27:14 -080030
31#define DRV_NAME "rotary-encoder"
32
33struct rotary_encoder {
Daniel Mack73969ff2009-03-04 23:27:14 -080034 struct input_dev *input;
Dmitry Torokhovce919532012-07-31 22:08:49 -070035 const struct rotary_encoder_platform_data *pdata;
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -070036
37 unsigned int axis;
38 unsigned int pos;
39
40 unsigned int irq_a;
41 unsigned int irq_b;
42
43 bool armed;
44 unsigned char dir; /* 0 - clockwise, 1 - CCW */
Johan Hovolde70bdd42011-05-11 16:35:30 -070045
46 char last_stable;
Daniel Mack73969ff2009-03-04 23:27:14 -080047};
48
Dmitry Torokhovce919532012-07-31 22:08:49 -070049static int rotary_encoder_get_state(const struct rotary_encoder_platform_data *pdata)
Daniel Mack73969ff2009-03-04 23:27:14 -080050{
Daniel Mack73969ff2009-03-04 23:27:14 -080051 int a = !!gpio_get_value(pdata->gpio_a);
52 int b = !!gpio_get_value(pdata->gpio_b);
Daniel Mack73969ff2009-03-04 23:27:14 -080053
54 a ^= pdata->inverted_a;
55 b ^= pdata->inverted_b;
Johan Hovold521a8f52011-05-11 16:33:50 -070056
57 return ((a << 1) | b);
58}
59
60static void rotary_encoder_report_event(struct rotary_encoder *encoder)
61{
Dmitry Torokhovce919532012-07-31 22:08:49 -070062 const struct rotary_encoder_platform_data *pdata = encoder->pdata;
Johan Hovold521a8f52011-05-11 16:33:50 -070063
64 if (pdata->relative_axis) {
65 input_report_rel(encoder->input,
66 pdata->axis, encoder->dir ? -1 : 1);
67 } else {
68 unsigned int pos = encoder->pos;
69
70 if (encoder->dir) {
71 /* turning counter-clockwise */
72 if (pdata->rollover)
73 pos += pdata->steps;
74 if (pos)
75 pos--;
76 } else {
77 /* turning clockwise */
78 if (pdata->rollover || pos < pdata->steps)
79 pos++;
80 }
81
82 if (pdata->rollover)
83 pos %= pdata->steps;
84
85 encoder->pos = pos;
86 input_report_abs(encoder->input, pdata->axis, encoder->pos);
87 }
88
89 input_sync(encoder->input);
90}
91
92static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
93{
94 struct rotary_encoder *encoder = dev_id;
95 int state;
96
97 state = rotary_encoder_get_state(encoder->pdata);
Daniel Mack73969ff2009-03-04 23:27:14 -080098
99 switch (state) {
Daniel Mack73969ff2009-03-04 23:27:14 -0800100 case 0x0:
Johan Hovold521a8f52011-05-11 16:33:50 -0700101 if (encoder->armed) {
102 rotary_encoder_report_event(encoder);
103 encoder->armed = false;
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -0700104 }
Daniel Mack73969ff2009-03-04 23:27:14 -0800105 break;
106
107 case 0x1:
108 case 0x2:
109 if (encoder->armed)
110 encoder->dir = state - 1;
111 break;
112
113 case 0x3:
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -0700114 encoder->armed = true;
Daniel Mack73969ff2009-03-04 23:27:14 -0800115 break;
116 }
117
118 return IRQ_HANDLED;
119}
120
Johan Hovolde70bdd42011-05-11 16:35:30 -0700121static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id)
122{
123 struct rotary_encoder *encoder = dev_id;
124 int state;
125
126 state = rotary_encoder_get_state(encoder->pdata);
127
128 switch (state) {
129 case 0x00:
130 case 0x03:
131 if (state != encoder->last_stable) {
132 rotary_encoder_report_event(encoder);
133 encoder->last_stable = state;
134 }
135 break;
136
137 case 0x01:
138 case 0x02:
139 encoder->dir = (encoder->last_stable + state) & 0x01;
140 break;
141 }
142
143 return IRQ_HANDLED;
144}
145
Ezequiel Garcia3a341a42015-10-13 23:39:50 -0700146static irqreturn_t rotary_encoder_quarter_period_irq(int irq, void *dev_id)
147{
148 struct rotary_encoder *encoder = dev_id;
149 unsigned char sum;
150 int state;
151
152 state = rotary_encoder_get_state(encoder->pdata);
153
154 /*
155 * We encode the previous and the current state using a byte.
156 * The previous state in the MSB nibble, the current state in the LSB
157 * nibble. Then use a table to decide the direction of the turn.
158 */
159 sum = (encoder->last_stable << 4) + state;
160 switch (sum) {
161 case 0x31:
162 case 0x10:
163 case 0x02:
164 case 0x23:
165 encoder->dir = 0; /* clockwise */
166 break;
167
168 case 0x13:
169 case 0x01:
170 case 0x20:
171 case 0x32:
172 encoder->dir = 1; /* counter-clockwise */
173 break;
174
175 default:
176 /*
177 * Ignore all other values. This covers the case when the
178 * state didn't change (a spurious interrupt) and the
179 * cases where the state changed by two steps, making it
180 * impossible to tell the direction.
181 *
182 * In either case, don't report any event and save the
183 * state for later.
184 */
185 goto out;
186 }
187
188 rotary_encoder_report_event(encoder);
189
190out:
191 encoder->last_stable = state;
192 return IRQ_HANDLED;
193}
194
Daniel Mack80c99bc2012-07-31 22:08:50 -0700195#ifdef CONFIG_OF
Jingoo Han5f155ee2014-05-07 13:03:00 -0700196static const struct of_device_id rotary_encoder_of_match[] = {
Daniel Mack80c99bc2012-07-31 22:08:50 -0700197 { .compatible = "rotary-encoder", },
198 { },
199};
200MODULE_DEVICE_TABLE(of, rotary_encoder_of_match);
201
Bill Pemberton5298cc42012-11-23 21:38:25 -0800202static struct rotary_encoder_platform_data *rotary_encoder_parse_dt(struct device *dev)
Daniel Mack80c99bc2012-07-31 22:08:50 -0700203{
204 const struct of_device_id *of_id =
205 of_match_device(rotary_encoder_of_match, dev);
206 struct device_node *np = dev->of_node;
207 struct rotary_encoder_platform_data *pdata;
208 enum of_gpio_flags flags;
Ezequiel Garcia3a341a42015-10-13 23:39:50 -0700209 int error;
Daniel Mack80c99bc2012-07-31 22:08:50 -0700210
211 if (!of_id || !np)
212 return NULL;
213
214 pdata = kzalloc(sizeof(struct rotary_encoder_platform_data),
215 GFP_KERNEL);
216 if (!pdata)
217 return ERR_PTR(-ENOMEM);
218
219 of_property_read_u32(np, "rotary-encoder,steps", &pdata->steps);
220 of_property_read_u32(np, "linux,axis", &pdata->axis);
221
222 pdata->gpio_a = of_get_gpio_flags(np, 0, &flags);
223 pdata->inverted_a = flags & OF_GPIO_ACTIVE_LOW;
224
225 pdata->gpio_b = of_get_gpio_flags(np, 1, &flags);
226 pdata->inverted_b = flags & OF_GPIO_ACTIVE_LOW;
227
Ben Gamari648b15c2015-10-13 23:37:28 -0700228 pdata->relative_axis =
229 of_property_read_bool(np, "rotary-encoder,relative-axis");
230 pdata->rollover = of_property_read_bool(np, "rotary-encoder,rollover");
Ezequiel Garcia3a341a42015-10-13 23:39:50 -0700231
232 error = of_property_read_u32(np, "rotary-encoder,steps-per-period",
233 &pdata->steps_per_period);
234 if (error) {
235 /*
236 * The 'half-period' property has been deprecated, you must use
237 * 'steps-per-period' and set an appropriate value, but we still
238 * need to parse it to maintain compatibility.
239 */
240 if (of_property_read_bool(np, "rotary-encoder,half-period")) {
241 pdata->steps_per_period = 2;
242 } else {
243 /* Fallback to one step per period behavior */
244 pdata->steps_per_period = 1;
245 }
246 }
247
Ben Gamari648b15c2015-10-13 23:37:28 -0700248 pdata->wakeup_source = of_property_read_bool(np, "wakeup-source");
Daniel Mack80c99bc2012-07-31 22:08:50 -0700249
250 return pdata;
251}
252#else
253static inline struct rotary_encoder_platform_data *
254rotary_encoder_parse_dt(struct device *dev)
255{
256 return NULL;
257}
258#endif
259
Bill Pemberton5298cc42012-11-23 21:38:25 -0800260static int rotary_encoder_probe(struct platform_device *pdev)
Daniel Mack73969ff2009-03-04 23:27:14 -0800261{
Dmitry Torokhovce919532012-07-31 22:08:49 -0700262 struct device *dev = &pdev->dev;
263 const struct rotary_encoder_platform_data *pdata = dev_get_platdata(dev);
Daniel Mack73969ff2009-03-04 23:27:14 -0800264 struct rotary_encoder *encoder;
265 struct input_dev *input;
Johan Hovolde70bdd42011-05-11 16:35:30 -0700266 irq_handler_t handler;
Daniel Mack73969ff2009-03-04 23:27:14 -0800267 int err;
268
H Hartley Sweeten06ee3d32009-10-05 21:43:42 -0700269 if (!pdata) {
Daniel Mack80c99bc2012-07-31 22:08:50 -0700270 pdata = rotary_encoder_parse_dt(dev);
271 if (IS_ERR(pdata))
272 return PTR_ERR(pdata);
273
274 if (!pdata) {
275 dev_err(dev, "missing platform data\n");
276 return -EINVAL;
277 }
Daniel Mack73969ff2009-03-04 23:27:14 -0800278 }
279
280 encoder = kzalloc(sizeof(struct rotary_encoder), GFP_KERNEL);
281 input = input_allocate_device();
282 if (!encoder || !input) {
Daniel Mack73969ff2009-03-04 23:27:14 -0800283 err = -ENOMEM;
284 goto exit_free_mem;
285 }
286
287 encoder->input = input;
288 encoder->pdata = pdata;
Daniel Mack73969ff2009-03-04 23:27:14 -0800289
Daniel Mack73969ff2009-03-04 23:27:14 -0800290 input->name = pdev->name;
291 input->id.bustype = BUS_HOST;
Daniel Mack80c99bc2012-07-31 22:08:50 -0700292 input->dev.parent = dev;
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -0700293
294 if (pdata->relative_axis) {
295 input->evbit[0] = BIT_MASK(EV_REL);
296 input->relbit[0] = BIT_MASK(pdata->axis);
297 } else {
298 input->evbit[0] = BIT_MASK(EV_ABS);
299 input_set_abs_params(encoder->input,
300 pdata->axis, 0, pdata->steps, 0, 1);
301 }
Daniel Mack73969ff2009-03-04 23:27:14 -0800302
Daniel Mack73969ff2009-03-04 23:27:14 -0800303 /* request the GPIOs */
Daniel Mack429a34d2012-07-31 22:08:48 -0700304 err = gpio_request_one(pdata->gpio_a, GPIOF_IN, dev_name(dev));
Daniel Mack73969ff2009-03-04 23:27:14 -0800305 if (err) {
Daniel Mack429a34d2012-07-31 22:08:48 -0700306 dev_err(dev, "unable to request GPIO %d\n", pdata->gpio_a);
Daniel Mack80c99bc2012-07-31 22:08:50 -0700307 goto exit_free_mem;
Daniel Mack73969ff2009-03-04 23:27:14 -0800308 }
309
Daniel Mack429a34d2012-07-31 22:08:48 -0700310 err = gpio_request_one(pdata->gpio_b, GPIOF_IN, dev_name(dev));
Andrew Clayton5deeac92010-02-10 23:18:05 -0800311 if (err) {
Daniel Mack429a34d2012-07-31 22:08:48 -0700312 dev_err(dev, "unable to request GPIO %d\n", pdata->gpio_b);
Andrew Clayton5deeac92010-02-10 23:18:05 -0800313 goto exit_free_gpio_a;
314 }
315
Daniel Macka78769b2012-07-31 22:08:47 -0700316 encoder->irq_a = gpio_to_irq(pdata->gpio_a);
317 encoder->irq_b = gpio_to_irq(pdata->gpio_b);
318
Ezequiel Garcia3a341a42015-10-13 23:39:50 -0700319 switch (pdata->steps_per_period) {
320 case 4:
321 handler = &rotary_encoder_quarter_period_irq;
322 encoder->last_stable = rotary_encoder_get_state(pdata);
323 break;
324 case 2:
Johan Hovolde70bdd42011-05-11 16:35:30 -0700325 handler = &rotary_encoder_half_period_irq;
326 encoder->last_stable = rotary_encoder_get_state(pdata);
Ezequiel Garcia3a341a42015-10-13 23:39:50 -0700327 break;
328 case 1:
Johan Hovolde70bdd42011-05-11 16:35:30 -0700329 handler = &rotary_encoder_irq;
Ezequiel Garcia3a341a42015-10-13 23:39:50 -0700330 break;
331 default:
332 dev_err(dev, "'%d' is not a valid steps-per-period value\n",
333 pdata->steps_per_period);
334 err = -EINVAL;
335 goto exit_free_gpio_b;
Johan Hovolde70bdd42011-05-11 16:35:30 -0700336 }
337
338 err = request_irq(encoder->irq_a, handler,
Alexander Steine0d5f4c2011-02-02 22:59:54 -0800339 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
Daniel Mack73969ff2009-03-04 23:27:14 -0800340 DRV_NAME, encoder);
341 if (err) {
Daniel Mack429a34d2012-07-31 22:08:48 -0700342 dev_err(dev, "unable to request IRQ %d\n", encoder->irq_a);
Daniel Mack73969ff2009-03-04 23:27:14 -0800343 goto exit_free_gpio_b;
344 }
345
Johan Hovolde70bdd42011-05-11 16:35:30 -0700346 err = request_irq(encoder->irq_b, handler,
Alexander Steine0d5f4c2011-02-02 22:59:54 -0800347 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
Daniel Mack73969ff2009-03-04 23:27:14 -0800348 DRV_NAME, encoder);
349 if (err) {
Daniel Mack429a34d2012-07-31 22:08:48 -0700350 dev_err(dev, "unable to request IRQ %d\n", encoder->irq_b);
Daniel Mack73969ff2009-03-04 23:27:14 -0800351 goto exit_free_irq_a;
352 }
353
Daniel Mack80c99bc2012-07-31 22:08:50 -0700354 err = input_register_device(input);
355 if (err) {
356 dev_err(dev, "failed to register input device\n");
357 goto exit_free_irq_b;
358 }
359
Sylvain Rochet47ec6e52015-10-13 23:24:36 -0700360 device_init_wakeup(&pdev->dev, pdata->wakeup_source);
361
Daniel Mack73969ff2009-03-04 23:27:14 -0800362 platform_set_drvdata(pdev, encoder);
363
364 return 0;
365
Daniel Mack80c99bc2012-07-31 22:08:50 -0700366exit_free_irq_b:
367 free_irq(encoder->irq_b, encoder);
Daniel Mack73969ff2009-03-04 23:27:14 -0800368exit_free_irq_a:
369 free_irq(encoder->irq_a, encoder);
370exit_free_gpio_b:
371 gpio_free(pdata->gpio_b);
372exit_free_gpio_a:
373 gpio_free(pdata->gpio_a);
Daniel Mack73969ff2009-03-04 23:27:14 -0800374exit_free_mem:
375 input_free_device(input);
376 kfree(encoder);
Daniel Mack80c99bc2012-07-31 22:08:50 -0700377 if (!dev_get_platdata(&pdev->dev))
378 kfree(pdata);
379
Daniel Mack73969ff2009-03-04 23:27:14 -0800380 return err;
381}
382
Bill Pembertone2619cf2012-11-23 21:50:47 -0800383static int rotary_encoder_remove(struct platform_device *pdev)
Daniel Mack73969ff2009-03-04 23:27:14 -0800384{
385 struct rotary_encoder *encoder = platform_get_drvdata(pdev);
Dmitry Torokhovce919532012-07-31 22:08:49 -0700386 const struct rotary_encoder_platform_data *pdata = encoder->pdata;
Daniel Mack73969ff2009-03-04 23:27:14 -0800387
Sylvain Rochet47ec6e52015-10-13 23:24:36 -0700388 device_init_wakeup(&pdev->dev, false);
389
Daniel Mack73969ff2009-03-04 23:27:14 -0800390 free_irq(encoder->irq_a, encoder);
391 free_irq(encoder->irq_b, encoder);
392 gpio_free(pdata->gpio_a);
393 gpio_free(pdata->gpio_b);
Daniel Mack80c99bc2012-07-31 22:08:50 -0700394
Daniel Mack73969ff2009-03-04 23:27:14 -0800395 input_unregister_device(encoder->input);
Daniel Mack73969ff2009-03-04 23:27:14 -0800396 kfree(encoder);
397
Daniel Mack80c99bc2012-07-31 22:08:50 -0700398 if (!dev_get_platdata(&pdev->dev))
399 kfree(pdata);
400
Daniel Mack73969ff2009-03-04 23:27:14 -0800401 return 0;
402}
403
Sylvain Rochet47ec6e52015-10-13 23:24:36 -0700404#ifdef CONFIG_PM_SLEEP
405static int rotary_encoder_suspend(struct device *dev)
406{
407 struct rotary_encoder *encoder = dev_get_drvdata(dev);
408
409 if (device_may_wakeup(dev)) {
410 enable_irq_wake(encoder->irq_a);
411 enable_irq_wake(encoder->irq_b);
412 }
413
414 return 0;
415}
416
417static int rotary_encoder_resume(struct device *dev)
418{
419 struct rotary_encoder *encoder = dev_get_drvdata(dev);
420
421 if (device_may_wakeup(dev)) {
422 disable_irq_wake(encoder->irq_a);
423 disable_irq_wake(encoder->irq_b);
424 }
425
426 return 0;
427}
428#endif
429
430static SIMPLE_DEV_PM_OPS(rotary_encoder_pm_ops,
431 rotary_encoder_suspend, rotary_encoder_resume);
432
Daniel Mack73969ff2009-03-04 23:27:14 -0800433static struct platform_driver rotary_encoder_driver = {
434 .probe = rotary_encoder_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800435 .remove = rotary_encoder_remove,
Daniel Mack73969ff2009-03-04 23:27:14 -0800436 .driver = {
437 .name = DRV_NAME,
Sylvain Rochet47ec6e52015-10-13 23:24:36 -0700438 .pm = &rotary_encoder_pm_ops,
Daniel Mack80c99bc2012-07-31 22:08:50 -0700439 .of_match_table = of_match_ptr(rotary_encoder_of_match),
Daniel Mack73969ff2009-03-04 23:27:14 -0800440 }
441};
JJ Ding840a7462011-11-29 11:08:40 -0800442module_platform_driver(rotary_encoder_driver);
Daniel Mack73969ff2009-03-04 23:27:14 -0800443
444MODULE_ALIAS("platform:" DRV_NAME);
445MODULE_DESCRIPTION("GPIO rotary encoder driver");
Johan Hovolde70bdd42011-05-11 16:35:30 -0700446MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>, Johan Hovold");
Daniel Mack73969ff2009-03-04 23:27:14 -0800447MODULE_LICENSE("GPL v2");