Loading...
1/*
2 * rotary_encoder.c
3 *
4 * (c) 2009 Daniel Mack <daniel@caiaq.de>
5 * Copyright (C) 2011 Johan Hovold <jhovold@gmail.com>
6 *
7 * state machine code inspired by code from Tim Ruetz
8 *
9 * A generic driver for rotary encoders connected to GPIO lines.
10 * See file:Documentation/input/rotary_encoder.txt for more information
11 *
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>
26#include <linux/slab.h>
27
28#define DRV_NAME "rotary-encoder"
29
30struct rotary_encoder {
31 struct input_dev *input;
32 struct rotary_encoder_platform_data *pdata;
33
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 */
42
43 char last_stable;
44};
45
46static int rotary_encoder_get_state(struct rotary_encoder_platform_data *pdata)
47{
48 int a = !!gpio_get_value(pdata->gpio_a);
49 int b = !!gpio_get_value(pdata->gpio_b);
50
51 a ^= pdata->inverted_a;
52 b ^= pdata->inverted_b;
53
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);
95
96 switch (state) {
97 case 0x0:
98 if (encoder->armed) {
99 rotary_encoder_report_event(encoder);
100 encoder->armed = false;
101 }
102 break;
103
104 case 0x1:
105 case 0x2:
106 if (encoder->armed)
107 encoder->dir = state - 1;
108 break;
109
110 case 0x3:
111 encoder->armed = true;
112 break;
113 }
114
115 return IRQ_HANDLED;
116}
117
118static 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
143static 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;
148 irq_handler_t handler;
149 int err;
150
151 if (!pdata) {
152 dev_err(&pdev->dev, "missing platform data\n");
153 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;
166 encoder->irq_a = gpio_to_irq(pdata->gpio_a);
167 encoder->irq_b = gpio_to_irq(pdata->gpio_b);
168
169 /* create and register the input driver */
170 input->name = pdev->name;
171 input->id.bustype = BUS_HOST;
172 input->dev.parent = &pdev->dev;
173
174 if (pdata->relative_axis) {
175 input->evbit[0] = BIT_MASK(EV_REL);
176 input->relbit[0] = BIT_MASK(pdata->axis);
177 } else {
178 input->evbit[0] = BIT_MASK(EV_ABS);
179 input_set_abs_params(encoder->input,
180 pdata->axis, 0, pdata->steps, 0, 1);
181 }
182
183 err = input_register_device(input);
184 if (err) {
185 dev_err(&pdev->dev, "failed to register input device\n");
186 goto exit_free_mem;
187 }
188
189 /* request the GPIOs */
190 err = gpio_request(pdata->gpio_a, DRV_NAME);
191 if (err) {
192 dev_err(&pdev->dev, "unable to request GPIO %d\n",
193 pdata->gpio_a);
194 goto exit_unregister_input;
195 }
196
197 err = gpio_direction_input(pdata->gpio_a);
198 if (err) {
199 dev_err(&pdev->dev, "unable to set GPIO %d for input\n",
200 pdata->gpio_a);
201 goto exit_unregister_input;
202 }
203
204 err = gpio_request(pdata->gpio_b, DRV_NAME);
205 if (err) {
206 dev_err(&pdev->dev, "unable to request GPIO %d\n",
207 pdata->gpio_b);
208 goto exit_free_gpio_a;
209 }
210
211 err = gpio_direction_input(pdata->gpio_b);
212 if (err) {
213 dev_err(&pdev->dev, "unable to set GPIO %d for input\n",
214 pdata->gpio_b);
215 goto exit_free_gpio_a;
216 }
217
218 /* request the IRQs */
219 if (pdata->half_period) {
220 handler = &rotary_encoder_half_period_irq;
221 encoder->last_stable = rotary_encoder_get_state(pdata);
222 } else {
223 handler = &rotary_encoder_irq;
224 }
225
226 err = request_irq(encoder->irq_a, handler,
227 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
228 DRV_NAME, encoder);
229 if (err) {
230 dev_err(&pdev->dev, "unable to request IRQ %d\n",
231 encoder->irq_a);
232 goto exit_free_gpio_b;
233 }
234
235 err = request_irq(encoder->irq_b, handler,
236 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
237 DRV_NAME, encoder);
238 if (err) {
239 dev_err(&pdev->dev, "unable to request IRQ %d\n",
240 encoder->irq_b);
241 goto exit_free_irq_a;
242 }
243
244 platform_set_drvdata(pdev, encoder);
245
246 return 0;
247
248exit_free_irq_a:
249 free_irq(encoder->irq_a, encoder);
250exit_free_gpio_b:
251 gpio_free(pdata->gpio_b);
252exit_free_gpio_a:
253 gpio_free(pdata->gpio_a);
254exit_unregister_input:
255 input_unregister_device(input);
256 input = NULL; /* so we don't try to free it */
257exit_free_mem:
258 input_free_device(input);
259 kfree(encoder);
260 return err;
261}
262
263static int __devexit rotary_encoder_remove(struct platform_device *pdev)
264{
265 struct rotary_encoder *encoder = platform_get_drvdata(pdev);
266 struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
267
268 free_irq(encoder->irq_a, encoder);
269 free_irq(encoder->irq_b, encoder);
270 gpio_free(pdata->gpio_a);
271 gpio_free(pdata->gpio_b);
272 input_unregister_device(encoder->input);
273 platform_set_drvdata(pdev, NULL);
274 kfree(encoder);
275
276 return 0;
277}
278
279static struct platform_driver rotary_encoder_driver = {
280 .probe = rotary_encoder_probe,
281 .remove = __devexit_p(rotary_encoder_remove),
282 .driver = {
283 .name = DRV_NAME,
284 .owner = THIS_MODULE,
285 }
286};
287
288static int __init rotary_encoder_init(void)
289{
290 return platform_driver_register(&rotary_encoder_driver);
291}
292
293static void __exit rotary_encoder_exit(void)
294{
295 platform_driver_unregister(&rotary_encoder_driver);
296}
297
298module_init(rotary_encoder_init);
299module_exit(rotary_encoder_exit);
300
301MODULE_ALIAS("platform:" DRV_NAME);
302MODULE_DESCRIPTION("GPIO rotary encoder driver");
303MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>, Johan Hovold");
304MODULE_LICENSE("GPL v2");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * rotary_encoder.c
4 *
5 * (c) 2009 Daniel Mack <daniel@caiaq.de>
6 * Copyright (C) 2011 Johan Hovold <jhovold@gmail.com>
7 *
8 * state machine code inspired by code from Tim Ruetz
9 *
10 * A generic driver for rotary encoders connected to GPIO lines.
11 * See file:Documentation/input/devices/rotary-encoder.rst for more information
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/interrupt.h>
17#include <linux/input.h>
18#include <linux/device.h>
19#include <linux/platform_device.h>
20#include <linux/gpio/consumer.h>
21#include <linux/slab.h>
22#include <linux/of.h>
23#include <linux/pm.h>
24#include <linux/property.h>
25
26#define DRV_NAME "rotary-encoder"
27
28enum rotary_encoder_encoding {
29 ROTENC_GRAY,
30 ROTENC_BINARY,
31};
32
33struct rotary_encoder {
34 struct input_dev *input;
35
36 struct mutex access_mutex;
37
38 u32 steps;
39 u32 axis;
40 bool relative_axis;
41 bool rollover;
42 enum rotary_encoder_encoding encoding;
43
44 unsigned int pos;
45
46 struct gpio_descs *gpios;
47
48 unsigned int *irq;
49
50 bool armed;
51 signed char dir; /* 1 - clockwise, -1 - CCW */
52
53 unsigned int last_stable;
54};
55
56static unsigned int rotary_encoder_get_state(struct rotary_encoder *encoder)
57{
58 int i;
59 unsigned int ret = 0;
60
61 for (i = 0; i < encoder->gpios->ndescs; ++i) {
62 int val = gpiod_get_value_cansleep(encoder->gpios->desc[i]);
63
64 /* convert from gray encoding to normal */
65 if (encoder->encoding == ROTENC_GRAY && ret & 1)
66 val = !val;
67
68 ret = ret << 1 | val;
69 }
70
71 return ret & 3;
72}
73
74static void rotary_encoder_report_event(struct rotary_encoder *encoder)
75{
76 if (encoder->relative_axis) {
77 input_report_rel(encoder->input,
78 encoder->axis, encoder->dir);
79 } else {
80 unsigned int pos = encoder->pos;
81
82 if (encoder->dir < 0) {
83 /* turning counter-clockwise */
84 if (encoder->rollover)
85 pos += encoder->steps;
86 if (pos)
87 pos--;
88 } else {
89 /* turning clockwise */
90 if (encoder->rollover || pos < encoder->steps)
91 pos++;
92 }
93
94 if (encoder->rollover)
95 pos %= encoder->steps;
96
97 encoder->pos = pos;
98 input_report_abs(encoder->input, encoder->axis, encoder->pos);
99 }
100
101 input_sync(encoder->input);
102}
103
104static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
105{
106 struct rotary_encoder *encoder = dev_id;
107 unsigned int state;
108
109 guard(mutex)(&encoder->access_mutex);
110
111 state = rotary_encoder_get_state(encoder);
112
113 switch (state) {
114 case 0x0:
115 if (encoder->armed) {
116 rotary_encoder_report_event(encoder);
117 encoder->armed = false;
118 }
119 break;
120
121 case 0x1:
122 case 0x3:
123 if (encoder->armed)
124 encoder->dir = 2 - state;
125 break;
126
127 case 0x2:
128 encoder->armed = true;
129 break;
130 }
131
132 return IRQ_HANDLED;
133}
134
135static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id)
136{
137 struct rotary_encoder *encoder = dev_id;
138 unsigned int state;
139
140 guard(mutex)(&encoder->access_mutex);
141
142 state = rotary_encoder_get_state(encoder);
143
144 if (state & 1) {
145 encoder->dir = ((encoder->last_stable - state + 1) % 4) - 1;
146 } else {
147 if (state != encoder->last_stable) {
148 rotary_encoder_report_event(encoder);
149 encoder->last_stable = state;
150 }
151 }
152
153 return IRQ_HANDLED;
154}
155
156static irqreturn_t rotary_encoder_quarter_period_irq(int irq, void *dev_id)
157{
158 struct rotary_encoder *encoder = dev_id;
159 unsigned int state;
160
161 guard(mutex)(&encoder->access_mutex);
162
163 state = rotary_encoder_get_state(encoder);
164
165 if ((encoder->last_stable + 1) % 4 == state) {
166 encoder->dir = 1;
167 rotary_encoder_report_event(encoder);
168 } else if (encoder->last_stable == (state + 1) % 4) {
169 encoder->dir = -1;
170 rotary_encoder_report_event(encoder);
171 }
172
173 encoder->last_stable = state;
174
175 return IRQ_HANDLED;
176}
177
178static int rotary_encoder_probe(struct platform_device *pdev)
179{
180 struct device *dev = &pdev->dev;
181 struct rotary_encoder *encoder;
182 struct input_dev *input;
183 irq_handler_t handler;
184 u32 steps_per_period;
185 unsigned int i;
186 int err;
187
188 encoder = devm_kzalloc(dev, sizeof(struct rotary_encoder), GFP_KERNEL);
189 if (!encoder)
190 return -ENOMEM;
191
192 mutex_init(&encoder->access_mutex);
193
194 device_property_read_u32(dev, "rotary-encoder,steps", &encoder->steps);
195
196 err = device_property_read_u32(dev, "rotary-encoder,steps-per-period",
197 &steps_per_period);
198 if (err) {
199 /*
200 * The 'half-period' property has been deprecated, you must
201 * use 'steps-per-period' and set an appropriate value, but
202 * we still need to parse it to maintain compatibility. If
203 * neither property is present we fall back to the one step
204 * per period behavior.
205 */
206 steps_per_period = device_property_read_bool(dev,
207 "rotary-encoder,half-period") ? 2 : 1;
208 }
209
210 encoder->rollover =
211 device_property_read_bool(dev, "rotary-encoder,rollover");
212
213 if (!device_property_present(dev, "rotary-encoder,encoding") ||
214 !device_property_match_string(dev, "rotary-encoder,encoding",
215 "gray")) {
216 dev_info(dev, "gray");
217 encoder->encoding = ROTENC_GRAY;
218 } else if (!device_property_match_string(dev, "rotary-encoder,encoding",
219 "binary")) {
220 dev_info(dev, "binary");
221 encoder->encoding = ROTENC_BINARY;
222 } else {
223 dev_err(dev, "unknown encoding setting\n");
224 return -EINVAL;
225 }
226
227 device_property_read_u32(dev, "linux,axis", &encoder->axis);
228 encoder->relative_axis =
229 device_property_read_bool(dev, "rotary-encoder,relative-axis");
230
231 encoder->gpios = devm_gpiod_get_array(dev, NULL, GPIOD_IN);
232 if (IS_ERR(encoder->gpios))
233 return dev_err_probe(dev, PTR_ERR(encoder->gpios), "unable to get gpios\n");
234 if (encoder->gpios->ndescs < 2) {
235 dev_err(dev, "not enough gpios found\n");
236 return -EINVAL;
237 }
238
239 input = devm_input_allocate_device(dev);
240 if (!input)
241 return -ENOMEM;
242
243 encoder->input = input;
244
245 input->name = pdev->name;
246 input->id.bustype = BUS_HOST;
247
248 if (encoder->relative_axis)
249 input_set_capability(input, EV_REL, encoder->axis);
250 else
251 input_set_abs_params(input,
252 encoder->axis, 0, encoder->steps, 0, 1);
253
254 switch (steps_per_period >> (encoder->gpios->ndescs - 2)) {
255 case 4:
256 handler = &rotary_encoder_quarter_period_irq;
257 encoder->last_stable = rotary_encoder_get_state(encoder);
258 break;
259 case 2:
260 handler = &rotary_encoder_half_period_irq;
261 encoder->last_stable = rotary_encoder_get_state(encoder);
262 break;
263 case 1:
264 handler = &rotary_encoder_irq;
265 break;
266 default:
267 dev_err(dev, "'%d' is not a valid steps-per-period value\n",
268 steps_per_period);
269 return -EINVAL;
270 }
271
272 encoder->irq =
273 devm_kcalloc(dev,
274 encoder->gpios->ndescs, sizeof(*encoder->irq),
275 GFP_KERNEL);
276 if (!encoder->irq)
277 return -ENOMEM;
278
279 for (i = 0; i < encoder->gpios->ndescs; ++i) {
280 encoder->irq[i] = gpiod_to_irq(encoder->gpios->desc[i]);
281
282 err = devm_request_threaded_irq(dev, encoder->irq[i],
283 NULL, handler,
284 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
285 IRQF_ONESHOT,
286 DRV_NAME, encoder);
287 if (err) {
288 dev_err(dev, "unable to request IRQ %d (gpio#%d)\n",
289 encoder->irq[i], i);
290 return err;
291 }
292 }
293
294 err = input_register_device(input);
295 if (err) {
296 dev_err(dev, "failed to register input device\n");
297 return err;
298 }
299
300 device_init_wakeup(dev,
301 device_property_read_bool(dev, "wakeup-source"));
302
303 platform_set_drvdata(pdev, encoder);
304
305 return 0;
306}
307
308static int rotary_encoder_suspend(struct device *dev)
309{
310 struct rotary_encoder *encoder = dev_get_drvdata(dev);
311 unsigned int i;
312
313 if (device_may_wakeup(dev)) {
314 for (i = 0; i < encoder->gpios->ndescs; ++i)
315 enable_irq_wake(encoder->irq[i]);
316 }
317
318 return 0;
319}
320
321static int rotary_encoder_resume(struct device *dev)
322{
323 struct rotary_encoder *encoder = dev_get_drvdata(dev);
324 unsigned int i;
325
326 if (device_may_wakeup(dev)) {
327 for (i = 0; i < encoder->gpios->ndescs; ++i)
328 disable_irq_wake(encoder->irq[i]);
329 }
330
331 return 0;
332}
333
334static DEFINE_SIMPLE_DEV_PM_OPS(rotary_encoder_pm_ops,
335 rotary_encoder_suspend, rotary_encoder_resume);
336
337#ifdef CONFIG_OF
338static const struct of_device_id rotary_encoder_of_match[] = {
339 { .compatible = "rotary-encoder", },
340 { },
341};
342MODULE_DEVICE_TABLE(of, rotary_encoder_of_match);
343#endif
344
345static struct platform_driver rotary_encoder_driver = {
346 .probe = rotary_encoder_probe,
347 .driver = {
348 .name = DRV_NAME,
349 .pm = pm_sleep_ptr(&rotary_encoder_pm_ops),
350 .of_match_table = of_match_ptr(rotary_encoder_of_match),
351 }
352};
353module_platform_driver(rotary_encoder_driver);
354
355MODULE_ALIAS("platform:" DRV_NAME);
356MODULE_DESCRIPTION("GPIO rotary encoder driver");
357MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>, Johan Hovold");
358MODULE_LICENSE("GPL v2");