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/devices/rotary-encoder.rst 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 		err = PTR_ERR(encoder->gpios);
244 		if (err != -EPROBE_DEFER)
245 			dev_err(dev, "unable to get gpios: %d\n", err);
246 		return err;
247 	}
248 	if (encoder->gpios->ndescs < 2) {
249 		dev_err(dev, "not enough gpios found\n");
250 		return -EINVAL;
251 	}
252 
253 	input = devm_input_allocate_device(dev);
254 	if (!input)
255 		return -ENOMEM;
256 
257 	encoder->input = input;
258 
259 	input->name = pdev->name;
260 	input->id.bustype = BUS_HOST;
261 	input->dev.parent = dev;
262 
263 	if (encoder->relative_axis)
264 		input_set_capability(input, EV_REL, encoder->axis);
265 	else
266 		input_set_abs_params(input,
267 				     encoder->axis, 0, encoder->steps, 0, 1);
268 
269 	switch (steps_per_period >> (encoder->gpios->ndescs - 2)) {
270 	case 4:
271 		handler = &rotary_encoder_quarter_period_irq;
272 		encoder->last_stable = rotary_encoder_get_state(encoder);
273 		break;
274 	case 2:
275 		handler = &rotary_encoder_half_period_irq;
276 		encoder->last_stable = rotary_encoder_get_state(encoder);
277 		break;
278 	case 1:
279 		handler = &rotary_encoder_irq;
280 		break;
281 	default:
282 		dev_err(dev, "'%d' is not a valid steps-per-period value\n",
283 			steps_per_period);
284 		return -EINVAL;
285 	}
286 
287 	encoder->irq =
288 		devm_kcalloc(dev,
289 			     encoder->gpios->ndescs, sizeof(*encoder->irq),
290 			     GFP_KERNEL);
291 	if (!encoder->irq)
292 		return -ENOMEM;
293 
294 	for (i = 0; i < encoder->gpios->ndescs; ++i) {
295 		encoder->irq[i] = gpiod_to_irq(encoder->gpios->desc[i]);
296 
297 		err = devm_request_threaded_irq(dev, encoder->irq[i],
298 				NULL, handler,
299 				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
300 				IRQF_ONESHOT,
301 				DRV_NAME, encoder);
302 		if (err) {
303 			dev_err(dev, "unable to request IRQ %d (gpio#%d)\n",
304 				encoder->irq[i], i);
305 			return err;
306 		}
307 	}
308 
309 	err = input_register_device(input);
310 	if (err) {
311 		dev_err(dev, "failed to register input device\n");
312 		return err;
313 	}
314 
315 	device_init_wakeup(dev,
316 			   device_property_read_bool(dev, "wakeup-source"));
317 
318 	platform_set_drvdata(pdev, encoder);
319 
320 	return 0;
321 }
322 
323 static int __maybe_unused rotary_encoder_suspend(struct device *dev)
324 {
325 	struct rotary_encoder *encoder = dev_get_drvdata(dev);
326 	unsigned int i;
327 
328 	if (device_may_wakeup(dev)) {
329 		for (i = 0; i < encoder->gpios->ndescs; ++i)
330 			enable_irq_wake(encoder->irq[i]);
331 	}
332 
333 	return 0;
334 }
335 
336 static int __maybe_unused rotary_encoder_resume(struct device *dev)
337 {
338 	struct rotary_encoder *encoder = dev_get_drvdata(dev);
339 	unsigned int i;
340 
341 	if (device_may_wakeup(dev)) {
342 		for (i = 0; i < encoder->gpios->ndescs; ++i)
343 			disable_irq_wake(encoder->irq[i]);
344 	}
345 
346 	return 0;
347 }
348 
349 static SIMPLE_DEV_PM_OPS(rotary_encoder_pm_ops,
350 			 rotary_encoder_suspend, rotary_encoder_resume);
351 
352 #ifdef CONFIG_OF
353 static const struct of_device_id rotary_encoder_of_match[] = {
354 	{ .compatible = "rotary-encoder", },
355 	{ },
356 };
357 MODULE_DEVICE_TABLE(of, rotary_encoder_of_match);
358 #endif
359 
360 static struct platform_driver rotary_encoder_driver = {
361 	.probe		= rotary_encoder_probe,
362 	.driver		= {
363 		.name	= DRV_NAME,
364 		.pm	= &rotary_encoder_pm_ops,
365 		.of_match_table = of_match_ptr(rotary_encoder_of_match),
366 	}
367 };
368 module_platform_driver(rotary_encoder_driver);
369 
370 MODULE_ALIAS("platform:" DRV_NAME);
371 MODULE_DESCRIPTION("GPIO rotary encoder driver");
372 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>, Johan Hovold");
373 MODULE_LICENSE("GPL v2");
374