xref: /linux/drivers/input/misc/rotary_encoder.c (revision f3a8b6645dc2e60d11f20c1c23afd964ff4e55ae)
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/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>
28 
29 #define DRV_NAME "rotary-encoder"
30 
31 enum rotary_encoder_encoding {
32 	ROTENC_GRAY,
33 	ROTENC_BINARY,
34 };
35 
36 struct rotary_encoder {
37 	struct input_dev *input;
38 
39 	struct mutex access_mutex;
40 
41 	u32 steps;
42 	u32 axis;
43 	bool relative_axis;
44 	bool rollover;
45 	enum rotary_encoder_encoding encoding;
46 
47 	unsigned int pos;
48 
49 	struct gpio_descs *gpios;
50 
51 	unsigned int *irq;
52 
53 	bool armed;
54 	signed char dir;	/* 1 - clockwise, -1 - CCW */
55 
56 	unsigned int last_stable;
57 };
58 
59 static unsigned int rotary_encoder_get_state(struct rotary_encoder *encoder)
60 {
61 	int i;
62 	unsigned int ret = 0;
63 
64 	for (i = 0; i < encoder->gpios->ndescs; ++i) {
65 		int val = gpiod_get_value_cansleep(encoder->gpios->desc[i]);
66 
67 		/* convert from gray encoding to normal */
68 		if (encoder->encoding == ROTENC_GRAY && ret & 1)
69 			val = !val;
70 
71 		ret = ret << 1 | val;
72 	}
73 
74 	return ret & 3;
75 }
76 
77 static void rotary_encoder_report_event(struct rotary_encoder *encoder)
78 {
79 	if (encoder->relative_axis) {
80 		input_report_rel(encoder->input,
81 				 encoder->axis, encoder->dir);
82 	} else {
83 		unsigned int pos = encoder->pos;
84 
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++;
95 		}
96 
97 		if (encoder->rollover)
98 			pos %= encoder->steps;
99 
100 		encoder->pos = pos;
101 		input_report_abs(encoder->input, encoder->axis, encoder->pos);
102 	}
103 
104 	input_sync(encoder->input);
105 }
106 
107 static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
108 {
109 	struct rotary_encoder *encoder = dev_id;
110 	unsigned int state;
111 
112 	mutex_lock(&encoder->access_mutex);
113 
114 	state = rotary_encoder_get_state(encoder);
115 
116 	switch (state) {
117 	case 0x0:
118 		if (encoder->armed) {
119 			rotary_encoder_report_event(encoder);
120 			encoder->armed = false;
121 		}
122 		break;
123 
124 	case 0x1:
125 	case 0x3:
126 		if (encoder->armed)
127 			encoder->dir = 2 - state;
128 		break;
129 
130 	case 0x2:
131 		encoder->armed = true;
132 		break;
133 	}
134 
135 	mutex_unlock(&encoder->access_mutex);
136 
137 	return IRQ_HANDLED;
138 }
139 
140 static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id)
141 {
142 	struct rotary_encoder *encoder = dev_id;
143 	unsigned int state;
144 
145 	mutex_lock(&encoder->access_mutex);
146 
147 	state = rotary_encoder_get_state(encoder);
148 
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;
155 		}
156 	}
157 
158 	mutex_unlock(&encoder->access_mutex);
159 
160 	return IRQ_HANDLED;
161 }
162 
163 static irqreturn_t rotary_encoder_quarter_period_irq(int irq, void *dev_id)
164 {
165 	struct rotary_encoder *encoder = dev_id;
166 	unsigned int state;
167 
168 	mutex_lock(&encoder->access_mutex);
169 
170 	state = rotary_encoder_get_state(encoder);
171 
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;
178 
179 	rotary_encoder_report_event(encoder);
180 
181 out:
182 	encoder->last_stable = state;
183 	mutex_unlock(&encoder->access_mutex);
184 
185 	return IRQ_HANDLED;
186 }
187 
188 static int rotary_encoder_probe(struct platform_device *pdev)
189 {
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;
197 
198 	encoder = devm_kzalloc(dev, sizeof(struct rotary_encoder), GFP_KERNEL);
199 	if (!encoder)
200 		return -ENOMEM;
201 
202 	mutex_init(&encoder->access_mutex);
203 
204 	device_property_read_u32(dev, "rotary-encoder,steps", &encoder->steps);
205 
206 	err = device_property_read_u32(dev, "rotary-encoder,steps-per-period",
207 				       &steps_per_period);
208 	if (err) {
209 		/*
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.
215 		 */
216 		steps_per_period = device_property_read_bool(dev,
217 					"rotary-encoder,half-period") ? 2 : 1;
218 	}
219 
220 	encoder->rollover =
221 		device_property_read_bool(dev, "rotary-encoder,rollover");
222 
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;
235 	}
236 
237 	device_property_read_u32(dev, "linux,axis", &encoder->axis);
238 	encoder->relative_axis =
239 		device_property_read_bool(dev, "rotary-encoder,relative-axis");
240 
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);
245 	}
246 	if (encoder->gpios->ndescs < 2) {
247 		dev_err(dev, "not enough gpios found\n");
248 		return -EINVAL;
249 	}
250 
251 	input = devm_input_allocate_device(dev);
252 	if (!input)
253 		return -ENOMEM;
254 
255 	encoder->input = input;
256 
257 	input->name = pdev->name;
258 	input->id.bustype = BUS_HOST;
259 	input->dev.parent = dev;
260 
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);
266 
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;
283 	}
284 
285 	encoder->irq =
286 		devm_kzalloc(dev,
287 			     sizeof(*encoder->irq) * encoder->gpios->ndescs,
288 			     GFP_KERNEL);
289 	if (!encoder->irq)
290 		return -ENOMEM;
291 
292 	for (i = 0; i < encoder->gpios->ndescs; ++i) {
293 		encoder->irq[i] = gpiod_to_irq(encoder->gpios->desc[i]);
294 
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;
304 		}
305 	}
306 
307 	err = input_register_device(input);
308 	if (err) {
309 		dev_err(dev, "failed to register input device\n");
310 		return err;
311 	}
312 
313 	device_init_wakeup(dev,
314 			   device_property_read_bool(dev, "wakeup-source"));
315 
316 	platform_set_drvdata(pdev, encoder);
317 
318 	return 0;
319 }
320 
321 static int __maybe_unused rotary_encoder_suspend(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 			enable_irq_wake(encoder->irq[i]);
329 	}
330 
331 	return 0;
332 }
333 
334 static int __maybe_unused rotary_encoder_resume(struct device *dev)
335 {
336 	struct rotary_encoder *encoder = dev_get_drvdata(dev);
337 	unsigned int i;
338 
339 	if (device_may_wakeup(dev)) {
340 		for (i = 0; i < encoder->gpios->ndescs; ++i)
341 			disable_irq_wake(encoder->irq[i]);
342 	}
343 
344 	return 0;
345 }
346 
347 static SIMPLE_DEV_PM_OPS(rotary_encoder_pm_ops,
348 			 rotary_encoder_suspend, rotary_encoder_resume);
349 
350 #ifdef CONFIG_OF
351 static const struct of_device_id rotary_encoder_of_match[] = {
352 	{ .compatible = "rotary-encoder", },
353 	{ },
354 };
355 MODULE_DEVICE_TABLE(of, rotary_encoder_of_match);
356 #endif
357 
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),
364 	}
365 };
366 module_platform_driver(rotary_encoder_driver);
367 
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");
372