Linux 4.19.133
[linux/fpc-iii.git] / drivers / input / misc / rotary_encoder.c
blob30ec77ad32c6efa6a56637246e342bad5b7e93b1
1 /*
2 * rotary_encoder.c
4 * (c) 2009 Daniel Mack <daniel@caiaq.de>
5 * Copyright (C) 2011 Johan Hovold <jhovold@gmail.com>
7 * state machine code inspired by code from Tim Ruetz
9 * A generic driver for rotary encoders connected to GPIO lines.
10 * See file:Documentation/input/devices/rotary-encoder.rst for more information
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.
17 #include <linux/kernel.h>
18 #include <linux/module.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/consumer.h>
24 #include <linux/slab.h>
25 #include <linux/of.h>
26 #include <linux/pm.h>
27 #include <linux/property.h>
29 #define DRV_NAME "rotary-encoder"
31 enum rotary_encoder_encoding {
32 ROTENC_GRAY,
33 ROTENC_BINARY,
36 struct rotary_encoder {
37 struct input_dev *input;
39 struct mutex access_mutex;
41 u32 steps;
42 u32 axis;
43 bool relative_axis;
44 bool rollover;
45 enum rotary_encoder_encoding encoding;
47 unsigned int pos;
49 struct gpio_descs *gpios;
51 unsigned int *irq;
53 bool armed;
54 signed char dir; /* 1 - clockwise, -1 - CCW */
56 unsigned int last_stable;
59 static unsigned int rotary_encoder_get_state(struct rotary_encoder *encoder)
61 int i;
62 unsigned int ret = 0;
64 for (i = 0; i < encoder->gpios->ndescs; ++i) {
65 int val = gpiod_get_value_cansleep(encoder->gpios->desc[i]);
67 /* convert from gray encoding to normal */
68 if (encoder->encoding == ROTENC_GRAY && ret & 1)
69 val = !val;
71 ret = ret << 1 | val;
74 return ret & 3;
77 static void rotary_encoder_report_event(struct rotary_encoder *encoder)
79 if (encoder->relative_axis) {
80 input_report_rel(encoder->input,
81 encoder->axis, encoder->dir);
82 } else {
83 unsigned int pos = encoder->pos;
85 if (encoder->dir < 0) {
86 /* turning counter-clockwise */
87 if (encoder->rollover)
88 pos += encoder->steps;
89 if (pos)
90 pos--;
91 } else {
92 /* turning clockwise */
93 if (encoder->rollover || pos < encoder->steps)
94 pos++;
97 if (encoder->rollover)
98 pos %= encoder->steps;
100 encoder->pos = pos;
101 input_report_abs(encoder->input, encoder->axis, encoder->pos);
104 input_sync(encoder->input);
107 static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
109 struct rotary_encoder *encoder = dev_id;
110 unsigned int state;
112 mutex_lock(&encoder->access_mutex);
114 state = rotary_encoder_get_state(encoder);
116 switch (state) {
117 case 0x0:
118 if (encoder->armed) {
119 rotary_encoder_report_event(encoder);
120 encoder->armed = false;
122 break;
124 case 0x1:
125 case 0x3:
126 if (encoder->armed)
127 encoder->dir = 2 - state;
128 break;
130 case 0x2:
131 encoder->armed = true;
132 break;
135 mutex_unlock(&encoder->access_mutex);
137 return IRQ_HANDLED;
140 static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id)
142 struct rotary_encoder *encoder = dev_id;
143 unsigned int state;
145 mutex_lock(&encoder->access_mutex);
147 state = rotary_encoder_get_state(encoder);
149 if (state & 1) {
150 encoder->dir = ((encoder->last_stable - state + 1) % 4) - 1;
151 } else {
152 if (state != encoder->last_stable) {
153 rotary_encoder_report_event(encoder);
154 encoder->last_stable = state;
158 mutex_unlock(&encoder->access_mutex);
160 return IRQ_HANDLED;
163 static irqreturn_t rotary_encoder_quarter_period_irq(int irq, void *dev_id)
165 struct rotary_encoder *encoder = dev_id;
166 unsigned int state;
168 mutex_lock(&encoder->access_mutex);
170 state = rotary_encoder_get_state(encoder);
172 if ((encoder->last_stable + 1) % 4 == state)
173 encoder->dir = 1;
174 else if (encoder->last_stable == (state + 1) % 4)
175 encoder->dir = -1;
176 else
177 goto out;
179 rotary_encoder_report_event(encoder);
181 out:
182 encoder->last_stable = state;
183 mutex_unlock(&encoder->access_mutex);
185 return IRQ_HANDLED;
188 static int rotary_encoder_probe(struct platform_device *pdev)
190 struct device *dev = &pdev->dev;
191 struct rotary_encoder *encoder;
192 struct input_dev *input;
193 irq_handler_t handler;
194 u32 steps_per_period;
195 unsigned int i;
196 int err;
198 encoder = devm_kzalloc(dev, sizeof(struct rotary_encoder), GFP_KERNEL);
199 if (!encoder)
200 return -ENOMEM;
202 mutex_init(&encoder->access_mutex);
204 device_property_read_u32(dev, "rotary-encoder,steps", &encoder->steps);
206 err = device_property_read_u32(dev, "rotary-encoder,steps-per-period",
207 &steps_per_period);
208 if (err) {
210 * The 'half-period' property has been deprecated, you must
211 * use 'steps-per-period' and set an appropriate value, but
212 * we still need to parse it to maintain compatibility. If
213 * neither property is present we fall back to the one step
214 * per period behavior.
216 steps_per_period = device_property_read_bool(dev,
217 "rotary-encoder,half-period") ? 2 : 1;
220 encoder->rollover =
221 device_property_read_bool(dev, "rotary-encoder,rollover");
223 if (!device_property_present(dev, "rotary-encoder,encoding") ||
224 !device_property_match_string(dev, "rotary-encoder,encoding",
225 "gray")) {
226 dev_info(dev, "gray");
227 encoder->encoding = ROTENC_GRAY;
228 } else if (!device_property_match_string(dev, "rotary-encoder,encoding",
229 "binary")) {
230 dev_info(dev, "binary");
231 encoder->encoding = ROTENC_BINARY;
232 } else {
233 dev_err(dev, "unknown encoding setting\n");
234 return -EINVAL;
237 device_property_read_u32(dev, "linux,axis", &encoder->axis);
238 encoder->relative_axis =
239 device_property_read_bool(dev, "rotary-encoder,relative-axis");
241 encoder->gpios = devm_gpiod_get_array(dev, NULL, GPIOD_IN);
242 if (IS_ERR(encoder->gpios)) {
243 dev_err(dev, "unable to get gpios\n");
244 return PTR_ERR(encoder->gpios);
246 if (encoder->gpios->ndescs < 2) {
247 dev_err(dev, "not enough gpios found\n");
248 return -EINVAL;
251 input = devm_input_allocate_device(dev);
252 if (!input)
253 return -ENOMEM;
255 encoder->input = input;
257 input->name = pdev->name;
258 input->id.bustype = BUS_HOST;
259 input->dev.parent = dev;
261 if (encoder->relative_axis)
262 input_set_capability(input, EV_REL, encoder->axis);
263 else
264 input_set_abs_params(input,
265 encoder->axis, 0, encoder->steps, 0, 1);
267 switch (steps_per_period >> (encoder->gpios->ndescs - 2)) {
268 case 4:
269 handler = &rotary_encoder_quarter_period_irq;
270 encoder->last_stable = rotary_encoder_get_state(encoder);
271 break;
272 case 2:
273 handler = &rotary_encoder_half_period_irq;
274 encoder->last_stable = rotary_encoder_get_state(encoder);
275 break;
276 case 1:
277 handler = &rotary_encoder_irq;
278 break;
279 default:
280 dev_err(dev, "'%d' is not a valid steps-per-period value\n",
281 steps_per_period);
282 return -EINVAL;
285 encoder->irq =
286 devm_kcalloc(dev,
287 encoder->gpios->ndescs, sizeof(*encoder->irq),
288 GFP_KERNEL);
289 if (!encoder->irq)
290 return -ENOMEM;
292 for (i = 0; i < encoder->gpios->ndescs; ++i) {
293 encoder->irq[i] = gpiod_to_irq(encoder->gpios->desc[i]);
295 err = devm_request_threaded_irq(dev, encoder->irq[i],
296 NULL, handler,
297 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
298 IRQF_ONESHOT,
299 DRV_NAME, encoder);
300 if (err) {
301 dev_err(dev, "unable to request IRQ %d (gpio#%d)\n",
302 encoder->irq[i], i);
303 return err;
307 err = input_register_device(input);
308 if (err) {
309 dev_err(dev, "failed to register input device\n");
310 return err;
313 device_init_wakeup(dev,
314 device_property_read_bool(dev, "wakeup-source"));
316 platform_set_drvdata(pdev, encoder);
318 return 0;
321 static int __maybe_unused rotary_encoder_suspend(struct device *dev)
323 struct rotary_encoder *encoder = dev_get_drvdata(dev);
324 unsigned int i;
326 if (device_may_wakeup(dev)) {
327 for (i = 0; i < encoder->gpios->ndescs; ++i)
328 enable_irq_wake(encoder->irq[i]);
331 return 0;
334 static int __maybe_unused rotary_encoder_resume(struct device *dev)
336 struct rotary_encoder *encoder = dev_get_drvdata(dev);
337 unsigned int i;
339 if (device_may_wakeup(dev)) {
340 for (i = 0; i < encoder->gpios->ndescs; ++i)
341 disable_irq_wake(encoder->irq[i]);
344 return 0;
347 static SIMPLE_DEV_PM_OPS(rotary_encoder_pm_ops,
348 rotary_encoder_suspend, rotary_encoder_resume);
350 #ifdef CONFIG_OF
351 static const struct of_device_id rotary_encoder_of_match[] = {
352 { .compatible = "rotary-encoder", },
353 { },
355 MODULE_DEVICE_TABLE(of, rotary_encoder_of_match);
356 #endif
358 static struct platform_driver rotary_encoder_driver = {
359 .probe = rotary_encoder_probe,
360 .driver = {
361 .name = DRV_NAME,
362 .pm = &rotary_encoder_pm_ops,
363 .of_match_table = of_match_ptr(rotary_encoder_of_match),
366 module_platform_driver(rotary_encoder_driver);
368 MODULE_ALIAS("platform:" DRV_NAME);
369 MODULE_DESCRIPTION("GPIO rotary encoder driver");
370 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>, Johan Hovold");
371 MODULE_LICENSE("GPL v2");