blob: 96c486de49e0c8291af7363aa17ed9b281d7ae77 [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>
Dmitry Torokhov77a8f0a2016-01-17 10:18:12 -080023#include <linux/gpio/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Sachin Kamat2e45e532013-10-06 00:56:24 -070025#include <linux/of.h>
Sylvain Rochet47ec6e52015-10-13 23:24:36 -070026#include <linux/pm.h>
Dmitry Torokhova9e340d2016-01-17 10:57:09 -080027#include <linux/property.h>
Daniel Mack73969ff2009-03-04 23:27:14 -080028
29#define DRV_NAME "rotary-encoder"
30
31struct rotary_encoder {
Daniel Mack73969ff2009-03-04 23:27:14 -080032 struct input_dev *input;
Dmitry Torokhova9e340d2016-01-17 10:57:09 -080033
Timo Teräsdee520e2016-01-16 20:46:24 -080034 struct mutex access_mutex;
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -070035
Dmitry Torokhova9e340d2016-01-17 10:57:09 -080036 u32 steps;
37 u32 axis;
38 bool relative_axis;
39 bool rollover;
40
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -070041 unsigned int pos;
42
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -080043 struct gpio_descs *gpios;
Dmitry Torokhov77a8f0a2016-01-17 10:18:12 -080044
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -080045 unsigned int *irq;
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -070046
47 bool armed;
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -080048 signed char dir; /* 1 - clockwise, -1 - CCW */
Johan Hovolde70bdd42011-05-11 16:35:30 -070049
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -080050 unsigned last_stable;
Daniel Mack73969ff2009-03-04 23:27:14 -080051};
52
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -080053static unsigned rotary_encoder_get_state(struct rotary_encoder *encoder)
Daniel Mack73969ff2009-03-04 23:27:14 -080054{
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -080055 int i;
56 unsigned ret = 0;
Johan Hovold521a8f52011-05-11 16:33:50 -070057
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -080058 for (i = 0; i < encoder->gpios->ndescs; ++i) {
59 int val = gpiod_get_value_cansleep(encoder->gpios->desc[i]);
60 /* convert from gray encoding to normal */
61 if (ret & 1)
62 val = !val;
63
64 ret = ret << 1 | val;
65 }
66
67 return ret & 3;
Johan Hovold521a8f52011-05-11 16:33:50 -070068}
69
70static void rotary_encoder_report_event(struct rotary_encoder *encoder)
71{
Dmitry Torokhova9e340d2016-01-17 10:57:09 -080072 if (encoder->relative_axis) {
Johan Hovold521a8f52011-05-11 16:33:50 -070073 input_report_rel(encoder->input,
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -080074 encoder->axis, encoder->dir);
Johan Hovold521a8f52011-05-11 16:33:50 -070075 } else {
76 unsigned int pos = encoder->pos;
77
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -080078 if (encoder->dir < 0) {
Johan Hovold521a8f52011-05-11 16:33:50 -070079 /* turning counter-clockwise */
Dmitry Torokhova9e340d2016-01-17 10:57:09 -080080 if (encoder->rollover)
81 pos += encoder->steps;
Johan Hovold521a8f52011-05-11 16:33:50 -070082 if (pos)
83 pos--;
84 } else {
85 /* turning clockwise */
Dmitry Torokhova9e340d2016-01-17 10:57:09 -080086 if (encoder->rollover || pos < encoder->steps)
Johan Hovold521a8f52011-05-11 16:33:50 -070087 pos++;
88 }
89
Dmitry Torokhova9e340d2016-01-17 10:57:09 -080090 if (encoder->rollover)
91 pos %= encoder->steps;
Johan Hovold521a8f52011-05-11 16:33:50 -070092
93 encoder->pos = pos;
Dmitry Torokhova9e340d2016-01-17 10:57:09 -080094 input_report_abs(encoder->input, encoder->axis, encoder->pos);
Johan Hovold521a8f52011-05-11 16:33:50 -070095 }
96
97 input_sync(encoder->input);
98}
99
100static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
101{
102 struct rotary_encoder *encoder = dev_id;
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -0800103 unsigned state;
Johan Hovold521a8f52011-05-11 16:33:50 -0700104
Timo Teräsdee520e2016-01-16 20:46:24 -0800105 mutex_lock(&encoder->access_mutex);
106
Dmitry Torokhov77a8f0a2016-01-17 10:18:12 -0800107 state = rotary_encoder_get_state(encoder);
Daniel Mack73969ff2009-03-04 23:27:14 -0800108
109 switch (state) {
Daniel Mack73969ff2009-03-04 23:27:14 -0800110 case 0x0:
Johan Hovold521a8f52011-05-11 16:33:50 -0700111 if (encoder->armed) {
112 rotary_encoder_report_event(encoder);
113 encoder->armed = false;
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -0700114 }
Daniel Mack73969ff2009-03-04 23:27:14 -0800115 break;
116
117 case 0x1:
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -0800118 case 0x3:
Daniel Mack73969ff2009-03-04 23:27:14 -0800119 if (encoder->armed)
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -0800120 encoder->dir = 2 - state;
Daniel Mack73969ff2009-03-04 23:27:14 -0800121 break;
122
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -0800123 case 0x2:
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -0700124 encoder->armed = true;
Daniel Mack73969ff2009-03-04 23:27:14 -0800125 break;
126 }
127
Timo Teräsdee520e2016-01-16 20:46:24 -0800128 mutex_unlock(&encoder->access_mutex);
129
Daniel Mack73969ff2009-03-04 23:27:14 -0800130 return IRQ_HANDLED;
131}
132
Johan Hovolde70bdd42011-05-11 16:35:30 -0700133static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id)
134{
135 struct rotary_encoder *encoder = dev_id;
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -0800136 unsigned int state;
Johan Hovolde70bdd42011-05-11 16:35:30 -0700137
Timo Teräsdee520e2016-01-16 20:46:24 -0800138 mutex_lock(&encoder->access_mutex);
139
Dmitry Torokhov77a8f0a2016-01-17 10:18:12 -0800140 state = rotary_encoder_get_state(encoder);
Johan Hovolde70bdd42011-05-11 16:35:30 -0700141
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -0800142 if (state & 1) {
143 encoder->dir = ((encoder->last_stable - state + 1) % 4) - 1;
144 } else {
Johan Hovolde70bdd42011-05-11 16:35:30 -0700145 if (state != encoder->last_stable) {
146 rotary_encoder_report_event(encoder);
147 encoder->last_stable = state;
148 }
Johan Hovolde70bdd42011-05-11 16:35:30 -0700149 }
150
Timo Teräsdee520e2016-01-16 20:46:24 -0800151 mutex_unlock(&encoder->access_mutex);
152
Johan Hovolde70bdd42011-05-11 16:35:30 -0700153 return IRQ_HANDLED;
154}
155
Ezequiel Garcia3a341a42015-10-13 23:39:50 -0700156static irqreturn_t rotary_encoder_quarter_period_irq(int irq, void *dev_id)
157{
158 struct rotary_encoder *encoder = dev_id;
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -0800159 unsigned int state;
Ezequiel Garcia3a341a42015-10-13 23:39:50 -0700160
Timo Teräsdee520e2016-01-16 20:46:24 -0800161 mutex_lock(&encoder->access_mutex);
162
Dmitry Torokhov77a8f0a2016-01-17 10:18:12 -0800163 state = rotary_encoder_get_state(encoder);
Ezequiel Garcia3a341a42015-10-13 23:39:50 -0700164
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -0800165 if ((encoder->last_stable + 1) % 4 == state)
166 encoder->dir = 1;
167 else if (encoder->last_stable == (state + 1) % 4)
168 encoder->dir = -1;
169 else
Ezequiel Garcia3a341a42015-10-13 23:39:50 -0700170 goto out;
Ezequiel Garcia3a341a42015-10-13 23:39:50 -0700171
172 rotary_encoder_report_event(encoder);
173
174out:
175 encoder->last_stable = state;
Timo Teräsdee520e2016-01-16 20:46:24 -0800176 mutex_unlock(&encoder->access_mutex);
177
Ezequiel Garcia3a341a42015-10-13 23:39:50 -0700178 return IRQ_HANDLED;
179}
180
Bill Pemberton5298cc42012-11-23 21:38:25 -0800181static int rotary_encoder_probe(struct platform_device *pdev)
Daniel Mack73969ff2009-03-04 23:27:14 -0800182{
Dmitry Torokhovce919532012-07-31 22:08:49 -0700183 struct device *dev = &pdev->dev;
Daniel Mack73969ff2009-03-04 23:27:14 -0800184 struct rotary_encoder *encoder;
185 struct input_dev *input;
Johan Hovolde70bdd42011-05-11 16:35:30 -0700186 irq_handler_t handler;
Dmitry Torokhova9e340d2016-01-17 10:57:09 -0800187 u32 steps_per_period;
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -0800188 unsigned int i;
Daniel Mack73969ff2009-03-04 23:27:14 -0800189 int err;
190
Timo Teräsd9202af2016-01-16 20:22:07 -0800191 encoder = devm_kzalloc(dev, sizeof(struct rotary_encoder), GFP_KERNEL);
192 if (!encoder)
193 return -ENOMEM;
194
Dmitry Torokhov77a8f0a2016-01-17 10:18:12 -0800195 mutex_init(&encoder->access_mutex);
Dmitry Torokhova9e340d2016-01-17 10:57:09 -0800196
197 device_property_read_u32(dev, "rotary-encoder,steps", &encoder->steps);
198
199 err = device_property_read_u32(dev, "rotary-encoder,steps-per-period",
200 &steps_per_period);
201 if (err) {
202 /*
203 * The 'half-period' property has been deprecated, you must
204 * use 'steps-per-period' and set an appropriate value, but
205 * we still need to parse it to maintain compatibility. If
206 * neither property is present we fall back to the one step
207 * per period behavior.
208 */
209 steps_per_period = device_property_read_bool(dev,
210 "rotary-encoder,half-period") ? 2 : 1;
211 }
212
213 encoder->rollover =
214 device_property_read_bool(dev, "rotary-encoder,rollover");
215
216 device_property_read_u32(dev, "linux,axis", &encoder->axis);
217 encoder->relative_axis =
218 device_property_read_bool(dev, "rotary-encoder,relative-axis");
Dmitry Torokhov77a8f0a2016-01-17 10:18:12 -0800219
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -0800220 encoder->gpios = devm_gpiod_get_array(dev, NULL, GPIOD_IN);
221 if (IS_ERR(encoder->gpios)) {
222 dev_err(dev, "unable to get gpios\n");
223 return PTR_ERR(encoder->gpios);
Dmitry Torokhov77a8f0a2016-01-17 10:18:12 -0800224 }
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -0800225 if (encoder->gpios->ndescs < 2) {
226 dev_err(dev, "not enough gpios found\n");
227 return -EINVAL;
Dmitry Torokhov77a8f0a2016-01-17 10:18:12 -0800228 }
229
Timo Teräsd9202af2016-01-16 20:22:07 -0800230 input = devm_input_allocate_device(dev);
231 if (!input)
232 return -ENOMEM;
Daniel Mack73969ff2009-03-04 23:27:14 -0800233
234 encoder->input = input;
Daniel Mack73969ff2009-03-04 23:27:14 -0800235
Daniel Mack73969ff2009-03-04 23:27:14 -0800236 input->name = pdev->name;
237 input->id.bustype = BUS_HOST;
Daniel Mack80c99bc2012-07-31 22:08:50 -0700238 input->dev.parent = dev;
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -0700239
Dmitry Torokhova9e340d2016-01-17 10:57:09 -0800240 if (encoder->relative_axis)
241 input_set_capability(input, EV_REL, encoder->axis);
Dmitry Torokhov86315802016-01-17 10:52:54 -0800242 else
Dmitry Torokhova9e340d2016-01-17 10:57:09 -0800243 input_set_abs_params(input,
244 encoder->axis, 0, encoder->steps, 0, 1);
Daniel Mack73969ff2009-03-04 23:27:14 -0800245
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -0800246 switch (steps_per_period >> (encoder->gpios->ndescs - 2)) {
Ezequiel Garcia3a341a42015-10-13 23:39:50 -0700247 case 4:
248 handler = &rotary_encoder_quarter_period_irq;
Dmitry Torokhov77a8f0a2016-01-17 10:18:12 -0800249 encoder->last_stable = rotary_encoder_get_state(encoder);
Ezequiel Garcia3a341a42015-10-13 23:39:50 -0700250 break;
251 case 2:
Johan Hovolde70bdd42011-05-11 16:35:30 -0700252 handler = &rotary_encoder_half_period_irq;
Dmitry Torokhov77a8f0a2016-01-17 10:18:12 -0800253 encoder->last_stable = rotary_encoder_get_state(encoder);
Ezequiel Garcia3a341a42015-10-13 23:39:50 -0700254 break;
255 case 1:
Johan Hovolde70bdd42011-05-11 16:35:30 -0700256 handler = &rotary_encoder_irq;
Ezequiel Garcia3a341a42015-10-13 23:39:50 -0700257 break;
258 default:
259 dev_err(dev, "'%d' is not a valid steps-per-period value\n",
Dmitry Torokhova9e340d2016-01-17 10:57:09 -0800260 steps_per_period);
Timo Teräsd9202af2016-01-16 20:22:07 -0800261 return -EINVAL;
Johan Hovolde70bdd42011-05-11 16:35:30 -0700262 }
263
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -0800264 encoder->irq =
265 devm_kzalloc(dev,
266 sizeof(*encoder->irq) * encoder->gpios->ndescs,
267 GFP_KERNEL);
268 if (!encoder->irq)
269 return -ENOMEM;
Daniel Mack73969ff2009-03-04 23:27:14 -0800270
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -0800271 for (i = 0; i < encoder->gpios->ndescs; ++i) {
272 encoder->irq[i] = gpiod_to_irq(encoder->gpios->desc[i]);
273
274 err = devm_request_threaded_irq(dev, encoder->irq[i],
275 NULL, handler,
Timo Teräsdee520e2016-01-16 20:46:24 -0800276 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
277 IRQF_ONESHOT,
278 DRV_NAME, encoder);
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -0800279 if (err) {
280 dev_err(dev, "unable to request IRQ %d (gpio#%d)\n",
281 encoder->irq[i], i);
282 return err;
283 }
Daniel Mack73969ff2009-03-04 23:27:14 -0800284 }
285
Daniel Mack80c99bc2012-07-31 22:08:50 -0700286 err = input_register_device(input);
287 if (err) {
288 dev_err(dev, "failed to register input device\n");
Timo Teräsd9202af2016-01-16 20:22:07 -0800289 return err;
Daniel Mack80c99bc2012-07-31 22:08:50 -0700290 }
291
Dmitry Torokhova9e340d2016-01-17 10:57:09 -0800292 device_init_wakeup(dev,
293 device_property_read_bool(dev, "wakeup-source"));
Sylvain Rochet47ec6e52015-10-13 23:24:36 -0700294
Daniel Mack73969ff2009-03-04 23:27:14 -0800295 platform_set_drvdata(pdev, encoder);
296
297 return 0;
Daniel Mack73969ff2009-03-04 23:27:14 -0800298}
299
Dmitry Torokhov6a6f70b2016-01-16 20:50:06 -0800300static int __maybe_unused rotary_encoder_suspend(struct device *dev)
Sylvain Rochet47ec6e52015-10-13 23:24:36 -0700301{
302 struct rotary_encoder *encoder = dev_get_drvdata(dev);
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -0800303 unsigned int i;
Sylvain Rochet47ec6e52015-10-13 23:24:36 -0700304
305 if (device_may_wakeup(dev)) {
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -0800306 for (i = 0; i < encoder->gpios->ndescs; ++i)
307 enable_irq_wake(encoder->irq[i]);
Sylvain Rochet47ec6e52015-10-13 23:24:36 -0700308 }
309
310 return 0;
311}
312
Dmitry Torokhov6a6f70b2016-01-16 20:50:06 -0800313static int __maybe_unused rotary_encoder_resume(struct device *dev)
Sylvain Rochet47ec6e52015-10-13 23:24:36 -0700314{
315 struct rotary_encoder *encoder = dev_get_drvdata(dev);
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -0800316 unsigned int i;
Sylvain Rochet47ec6e52015-10-13 23:24:36 -0700317
318 if (device_may_wakeup(dev)) {
Uwe Kleine-König7dde4e72016-03-01 18:25:26 -0800319 for (i = 0; i < encoder->gpios->ndescs; ++i)
320 disable_irq_wake(encoder->irq[i]);
Sylvain Rochet47ec6e52015-10-13 23:24:36 -0700321 }
322
323 return 0;
324}
Sylvain Rochet47ec6e52015-10-13 23:24:36 -0700325
326static SIMPLE_DEV_PM_OPS(rotary_encoder_pm_ops,
Dmitry Torokhov6a6f70b2016-01-16 20:50:06 -0800327 rotary_encoder_suspend, rotary_encoder_resume);
Sylvain Rochet47ec6e52015-10-13 23:24:36 -0700328
Dmitry Torokhova9e340d2016-01-17 10:57:09 -0800329#ifdef CONFIG_OF
330static const struct of_device_id rotary_encoder_of_match[] = {
331 { .compatible = "rotary-encoder", },
332 { },
333};
334MODULE_DEVICE_TABLE(of, rotary_encoder_of_match);
335#endif
336
Daniel Mack73969ff2009-03-04 23:27:14 -0800337static struct platform_driver rotary_encoder_driver = {
338 .probe = rotary_encoder_probe,
Daniel Mack73969ff2009-03-04 23:27:14 -0800339 .driver = {
340 .name = DRV_NAME,
Sylvain Rochet47ec6e52015-10-13 23:24:36 -0700341 .pm = &rotary_encoder_pm_ops,
Daniel Mack80c99bc2012-07-31 22:08:50 -0700342 .of_match_table = of_match_ptr(rotary_encoder_of_match),
Daniel Mack73969ff2009-03-04 23:27:14 -0800343 }
344};
JJ Ding840a7462011-11-29 11:08:40 -0800345module_platform_driver(rotary_encoder_driver);
Daniel Mack73969ff2009-03-04 23:27:14 -0800346
347MODULE_ALIAS("platform:" DRV_NAME);
348MODULE_DESCRIPTION("GPIO rotary encoder driver");
Johan Hovolde70bdd42011-05-11 16:35:30 -0700349MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>, Johan Hovold");
Daniel Mack73969ff2009-03-04 23:27:14 -0800350MODULE_LICENSE("GPL v2");