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.h>
24#include <linux/rotary_encoder.h>
25#include <linux/slab.h>
26#include <linux/of.h>
27#include <linux/of_platform.h>
28#include <linux/of_gpio.h>
29#include <linux/pm.h>
30
31#define DRV_NAME "rotary-encoder"
32
33struct rotary_encoder {
34	struct input_dev *input;
35	const struct rotary_encoder_platform_data *pdata;
36
37	unsigned int axis;
38	unsigned int pos;
39
40	unsigned int irq_a;
41	unsigned int irq_b;
42
43	bool armed;
44	unsigned char dir;	/* 0 - clockwise, 1 - CCW */
45
46	char last_stable;
47};
48
49static int rotary_encoder_get_state(const struct rotary_encoder_platform_data *pdata)
50{
51	int a = !!gpio_get_value(pdata->gpio_a);
52	int b = !!gpio_get_value(pdata->gpio_b);
53
54	a ^= pdata->inverted_a;
55	b ^= pdata->inverted_b;
56
57	return ((a << 1) | b);
58}
59
60static void rotary_encoder_report_event(struct rotary_encoder *encoder)
61{
62	const struct rotary_encoder_platform_data *pdata = encoder->pdata;
63
64	if (pdata->relative_axis) {
65		input_report_rel(encoder->input,
66				 pdata->axis, encoder->dir ? -1 : 1);
67	} else {
68		unsigned int pos = encoder->pos;
69
70		if (encoder->dir) {
71			/* turning counter-clockwise */
72			if (pdata->rollover)
73				pos += pdata->steps;
74			if (pos)
75				pos--;
76		} else {
77			/* turning clockwise */
78			if (pdata->rollover || pos < pdata->steps)
79				pos++;
80		}
81
82		if (pdata->rollover)
83			pos %= pdata->steps;
84
85		encoder->pos = pos;
86		input_report_abs(encoder->input, pdata->axis, encoder->pos);
87	}
88
89	input_sync(encoder->input);
90}
91
92static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
93{
94	struct rotary_encoder *encoder = dev_id;
95	int state;
96
97	state = rotary_encoder_get_state(encoder->pdata);
98
99	switch (state) {
100	case 0x0:
101		if (encoder->armed) {
102			rotary_encoder_report_event(encoder);
103			encoder->armed = false;
104		}
105		break;
106
107	case 0x1:
108	case 0x2:
109		if (encoder->armed)
110			encoder->dir = state - 1;
111		break;
112
113	case 0x3:
114		encoder->armed = true;
115		break;
116	}
117
118	return IRQ_HANDLED;
119}
120
121static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id)
122{
123	struct rotary_encoder *encoder = dev_id;
124	int state;
125
126	state = rotary_encoder_get_state(encoder->pdata);
127
128	switch (state) {
129	case 0x00:
130	case 0x03:
131		if (state != encoder->last_stable) {
132			rotary_encoder_report_event(encoder);
133			encoder->last_stable = state;
134		}
135		break;
136
137	case 0x01:
138	case 0x02:
139		encoder->dir = (encoder->last_stable + state) & 0x01;
140		break;
141	}
142
143	return IRQ_HANDLED;
144}
145
146static irqreturn_t rotary_encoder_quarter_period_irq(int irq, void *dev_id)
147{
148	struct rotary_encoder *encoder = dev_id;
149	unsigned char sum;
150	int state;
151
152	state = rotary_encoder_get_state(encoder->pdata);
153
154	/*
155	 * We encode the previous and the current state using a byte.
156	 * The previous state in the MSB nibble, the current state in the LSB
157	 * nibble. Then use a table to decide the direction of the turn.
158	 */
159	sum = (encoder->last_stable << 4) + state;
160	switch (sum) {
161	case 0x31:
162	case 0x10:
163	case 0x02:
164	case 0x23:
165		encoder->dir = 0; /* clockwise */
166		break;
167
168	case 0x13:
169	case 0x01:
170	case 0x20:
171	case 0x32:
172		encoder->dir = 1; /* counter-clockwise */
173		break;
174
175	default:
176		/*
177		 * Ignore all other values. This covers the case when the
178		 * state didn't change (a spurious interrupt) and the
179		 * cases where the state changed by two steps, making it
180		 * impossible to tell the direction.
181		 *
182		 * In either case, don't report any event and save the
183		 * state for later.
184		 */
185		goto out;
186	}
187
188	rotary_encoder_report_event(encoder);
189
190out:
191	encoder->last_stable = state;
192	return IRQ_HANDLED;
193}
194
195#ifdef CONFIG_OF
196static const struct of_device_id rotary_encoder_of_match[] = {
197	{ .compatible = "rotary-encoder", },
198	{ },
199};
200MODULE_DEVICE_TABLE(of, rotary_encoder_of_match);
201
202static struct rotary_encoder_platform_data *rotary_encoder_parse_dt(struct device *dev)
203{
204	const struct of_device_id *of_id =
205				of_match_device(rotary_encoder_of_match, dev);
206	struct device_node *np = dev->of_node;
207	struct rotary_encoder_platform_data *pdata;
208	enum of_gpio_flags flags;
209	int error;
210
211	if (!of_id || !np)
212		return NULL;
213
214	pdata = kzalloc(sizeof(struct rotary_encoder_platform_data),
215			GFP_KERNEL);
216	if (!pdata)
217		return ERR_PTR(-ENOMEM);
218
219	of_property_read_u32(np, "rotary-encoder,steps", &pdata->steps);
220	of_property_read_u32(np, "linux,axis", &pdata->axis);
221
222	pdata->gpio_a = of_get_gpio_flags(np, 0, &flags);
223	pdata->inverted_a = flags & OF_GPIO_ACTIVE_LOW;
224
225	pdata->gpio_b = of_get_gpio_flags(np, 1, &flags);
226	pdata->inverted_b = flags & OF_GPIO_ACTIVE_LOW;
227
228	pdata->relative_axis =
229		of_property_read_bool(np, "rotary-encoder,relative-axis");
230	pdata->rollover = of_property_read_bool(np, "rotary-encoder,rollover");
231
232	error = of_property_read_u32(np, "rotary-encoder,steps-per-period",
233				     &pdata->steps_per_period);
234	if (error) {
235		/*
236		 * The 'half-period' property has been deprecated, you must use
237		 * 'steps-per-period' and set an appropriate value, but we still
238		 * need to parse it to maintain compatibility.
239		 */
240		if (of_property_read_bool(np, "rotary-encoder,half-period")) {
241			pdata->steps_per_period = 2;
242		} else {
243			/* Fallback to one step per period behavior */
244			pdata->steps_per_period = 1;
245		}
246	}
247
248	pdata->wakeup_source = of_property_read_bool(np, "wakeup-source");
249
250	return pdata;
251}
252#else
253static inline struct rotary_encoder_platform_data *
254rotary_encoder_parse_dt(struct device *dev)
255{
256	return NULL;
257}
258#endif
259
260static int rotary_encoder_probe(struct platform_device *pdev)
261{
262	struct device *dev = &pdev->dev;
263	const struct rotary_encoder_platform_data *pdata = dev_get_platdata(dev);
264	struct rotary_encoder *encoder;
265	struct input_dev *input;
266	irq_handler_t handler;
267	int err;
268
269	if (!pdata) {
270		pdata = rotary_encoder_parse_dt(dev);
271		if (IS_ERR(pdata))
272			return PTR_ERR(pdata);
273
274		if (!pdata) {
275			dev_err(dev, "missing platform data\n");
276			return -EINVAL;
277		}
278	}
279
280	encoder = kzalloc(sizeof(struct rotary_encoder), GFP_KERNEL);
281	input = input_allocate_device();
282	if (!encoder || !input) {
283		err = -ENOMEM;
284		goto exit_free_mem;
285	}
286
287	encoder->input = input;
288	encoder->pdata = pdata;
289
290	input->name = pdev->name;
291	input->id.bustype = BUS_HOST;
292	input->dev.parent = dev;
293
294	if (pdata->relative_axis) {
295		input->evbit[0] = BIT_MASK(EV_REL);
296		input->relbit[0] = BIT_MASK(pdata->axis);
297	} else {
298		input->evbit[0] = BIT_MASK(EV_ABS);
299		input_set_abs_params(encoder->input,
300				     pdata->axis, 0, pdata->steps, 0, 1);
301	}
302
303	/* request the GPIOs */
304	err = gpio_request_one(pdata->gpio_a, GPIOF_IN, dev_name(dev));
305	if (err) {
306		dev_err(dev, "unable to request GPIO %d\n", pdata->gpio_a);
307		goto exit_free_mem;
308	}
309
310	err = gpio_request_one(pdata->gpio_b, GPIOF_IN, dev_name(dev));
311	if (err) {
312		dev_err(dev, "unable to request GPIO %d\n", pdata->gpio_b);
313		goto exit_free_gpio_a;
314	}
315
316	encoder->irq_a = gpio_to_irq(pdata->gpio_a);
317	encoder->irq_b = gpio_to_irq(pdata->gpio_b);
318
319	switch (pdata->steps_per_period) {
320	case 4:
321		handler = &rotary_encoder_quarter_period_irq;
322		encoder->last_stable = rotary_encoder_get_state(pdata);
323		break;
324	case 2:
325		handler = &rotary_encoder_half_period_irq;
326		encoder->last_stable = rotary_encoder_get_state(pdata);
327		break;
328	case 1:
329		handler = &rotary_encoder_irq;
330		break;
331	default:
332		dev_err(dev, "'%d' is not a valid steps-per-period value\n",
333			pdata->steps_per_period);
334		err = -EINVAL;
335		goto exit_free_gpio_b;
336	}
337
338	err = request_irq(encoder->irq_a, handler,
339			  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
340			  DRV_NAME, encoder);
341	if (err) {
342		dev_err(dev, "unable to request IRQ %d\n", encoder->irq_a);
343		goto exit_free_gpio_b;
344	}
345
346	err = request_irq(encoder->irq_b, handler,
347			  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
348			  DRV_NAME, encoder);
349	if (err) {
350		dev_err(dev, "unable to request IRQ %d\n", encoder->irq_b);
351		goto exit_free_irq_a;
352	}
353
354	err = input_register_device(input);
355	if (err) {
356		dev_err(dev, "failed to register input device\n");
357		goto exit_free_irq_b;
358	}
359
360	device_init_wakeup(&pdev->dev, pdata->wakeup_source);
361
362	platform_set_drvdata(pdev, encoder);
363
364	return 0;
365
366exit_free_irq_b:
367	free_irq(encoder->irq_b, encoder);
368exit_free_irq_a:
369	free_irq(encoder->irq_a, encoder);
370exit_free_gpio_b:
371	gpio_free(pdata->gpio_b);
372exit_free_gpio_a:
373	gpio_free(pdata->gpio_a);
374exit_free_mem:
375	input_free_device(input);
376	kfree(encoder);
377	if (!dev_get_platdata(&pdev->dev))
378		kfree(pdata);
379
380	return err;
381}
382
383static int rotary_encoder_remove(struct platform_device *pdev)
384{
385	struct rotary_encoder *encoder = platform_get_drvdata(pdev);
386	const struct rotary_encoder_platform_data *pdata = encoder->pdata;
387
388	device_init_wakeup(&pdev->dev, false);
389
390	free_irq(encoder->irq_a, encoder);
391	free_irq(encoder->irq_b, encoder);
392	gpio_free(pdata->gpio_a);
393	gpio_free(pdata->gpio_b);
394
395	input_unregister_device(encoder->input);
396	kfree(encoder);
397
398	if (!dev_get_platdata(&pdev->dev))
399		kfree(pdata);
400
401	return 0;
402}
403
404#ifdef CONFIG_PM_SLEEP
405static int rotary_encoder_suspend(struct device *dev)
406{
407	struct rotary_encoder *encoder = dev_get_drvdata(dev);
408
409	if (device_may_wakeup(dev)) {
410		enable_irq_wake(encoder->irq_a);
411		enable_irq_wake(encoder->irq_b);
412	}
413
414	return 0;
415}
416
417static int rotary_encoder_resume(struct device *dev)
418{
419	struct rotary_encoder *encoder = dev_get_drvdata(dev);
420
421	if (device_may_wakeup(dev)) {
422		disable_irq_wake(encoder->irq_a);
423		disable_irq_wake(encoder->irq_b);
424	}
425
426	return 0;
427}
428#endif
429
430static SIMPLE_DEV_PM_OPS(rotary_encoder_pm_ops,
431		 rotary_encoder_suspend, rotary_encoder_resume);
432
433static struct platform_driver rotary_encoder_driver = {
434	.probe		= rotary_encoder_probe,
435	.remove		= rotary_encoder_remove,
436	.driver		= {
437		.name	= DRV_NAME,
438		.pm	= &rotary_encoder_pm_ops,
439		.of_match_table = of_match_ptr(rotary_encoder_of_match),
440	}
441};
442module_platform_driver(rotary_encoder_driver);
443
444MODULE_ALIAS("platform:" DRV_NAME);
445MODULE_DESCRIPTION("GPIO rotary encoder driver");
446MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>, Johan Hovold");
447MODULE_LICENSE("GPL v2");
448