of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / drivers / input / misc / rotary_encoder.c
blob8aee7198643001eb9252877b327569e4111744af
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/rotary-encoder.txt 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.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>
31 #define DRV_NAME "rotary-encoder"
33 struct rotary_encoder {
34 struct input_dev *input;
35 const struct rotary_encoder_platform_data *pdata;
37 unsigned int axis;
38 unsigned int pos;
40 unsigned int irq_a;
41 unsigned int irq_b;
43 bool armed;
44 unsigned char dir; /* 0 - clockwise, 1 - CCW */
46 char last_stable;
49 static int rotary_encoder_get_state(const struct rotary_encoder_platform_data *pdata)
51 int a = !!gpio_get_value(pdata->gpio_a);
52 int b = !!gpio_get_value(pdata->gpio_b);
54 a ^= pdata->inverted_a;
55 b ^= pdata->inverted_b;
57 return ((a << 1) | b);
60 static void rotary_encoder_report_event(struct rotary_encoder *encoder)
62 const struct rotary_encoder_platform_data *pdata = encoder->pdata;
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;
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++;
82 if (pdata->rollover)
83 pos %= pdata->steps;
85 encoder->pos = pos;
86 input_report_abs(encoder->input, pdata->axis, encoder->pos);
89 input_sync(encoder->input);
92 static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
94 struct rotary_encoder *encoder = dev_id;
95 int state;
97 state = rotary_encoder_get_state(encoder->pdata);
99 switch (state) {
100 case 0x0:
101 if (encoder->armed) {
102 rotary_encoder_report_event(encoder);
103 encoder->armed = false;
105 break;
107 case 0x1:
108 case 0x2:
109 if (encoder->armed)
110 encoder->dir = state - 1;
111 break;
113 case 0x3:
114 encoder->armed = true;
115 break;
118 return IRQ_HANDLED;
121 static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id)
123 struct rotary_encoder *encoder = dev_id;
124 int state;
126 state = rotary_encoder_get_state(encoder->pdata);
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;
135 break;
137 case 0x01:
138 case 0x02:
139 encoder->dir = (encoder->last_stable + state) & 0x01;
140 break;
143 return IRQ_HANDLED;
146 static irqreturn_t rotary_encoder_quarter_period_irq(int irq, void *dev_id)
148 struct rotary_encoder *encoder = dev_id;
149 unsigned char sum;
150 int state;
152 state = rotary_encoder_get_state(encoder->pdata);
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.
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;
168 case 0x13:
169 case 0x01:
170 case 0x20:
171 case 0x32:
172 encoder->dir = 1; /* counter-clockwise */
173 break;
175 default:
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.
182 * In either case, don't report any event and save the
183 * state for later.
185 goto out;
188 rotary_encoder_report_event(encoder);
190 out:
191 encoder->last_stable = state;
192 return IRQ_HANDLED;
195 #ifdef CONFIG_OF
196 static const struct of_device_id rotary_encoder_of_match[] = {
197 { .compatible = "rotary-encoder", },
198 { },
200 MODULE_DEVICE_TABLE(of, rotary_encoder_of_match);
202 static struct rotary_encoder_platform_data *rotary_encoder_parse_dt(struct device *dev)
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;
211 if (!of_id || !np)
212 return NULL;
214 pdata = kzalloc(sizeof(struct rotary_encoder_platform_data),
215 GFP_KERNEL);
216 if (!pdata)
217 return ERR_PTR(-ENOMEM);
219 of_property_read_u32(np, "rotary-encoder,steps", &pdata->steps);
220 of_property_read_u32(np, "linux,axis", &pdata->axis);
222 pdata->gpio_a = of_get_gpio_flags(np, 0, &flags);
223 pdata->inverted_a = flags & OF_GPIO_ACTIVE_LOW;
225 pdata->gpio_b = of_get_gpio_flags(np, 1, &flags);
226 pdata->inverted_b = flags & OF_GPIO_ACTIVE_LOW;
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");
232 error = of_property_read_u32(np, "rotary-encoder,steps-per-period",
233 &pdata->steps_per_period);
234 if (error) {
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.
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;
248 pdata->wakeup_source = of_property_read_bool(np, "wakeup-source");
250 return pdata;
252 #else
253 static inline struct rotary_encoder_platform_data *
254 rotary_encoder_parse_dt(struct device *dev)
256 return NULL;
258 #endif
260 static int rotary_encoder_probe(struct platform_device *pdev)
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;
269 if (!pdata) {
270 pdata = rotary_encoder_parse_dt(dev);
271 if (IS_ERR(pdata))
272 return PTR_ERR(pdata);
274 if (!pdata) {
275 dev_err(dev, "missing platform data\n");
276 return -EINVAL;
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;
287 encoder->input = input;
288 encoder->pdata = pdata;
290 input->name = pdev->name;
291 input->id.bustype = BUS_HOST;
292 input->dev.parent = dev;
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);
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;
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;
316 encoder->irq_a = gpio_to_irq(pdata->gpio_a);
317 encoder->irq_b = gpio_to_irq(pdata->gpio_b);
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;
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;
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;
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;
360 device_init_wakeup(&pdev->dev, pdata->wakeup_source);
362 platform_set_drvdata(pdev, encoder);
364 return 0;
366 exit_free_irq_b:
367 free_irq(encoder->irq_b, encoder);
368 exit_free_irq_a:
369 free_irq(encoder->irq_a, encoder);
370 exit_free_gpio_b:
371 gpio_free(pdata->gpio_b);
372 exit_free_gpio_a:
373 gpio_free(pdata->gpio_a);
374 exit_free_mem:
375 input_free_device(input);
376 kfree(encoder);
377 if (!dev_get_platdata(&pdev->dev))
378 kfree(pdata);
380 return err;
383 static int rotary_encoder_remove(struct platform_device *pdev)
385 struct rotary_encoder *encoder = platform_get_drvdata(pdev);
386 const struct rotary_encoder_platform_data *pdata = encoder->pdata;
388 device_init_wakeup(&pdev->dev, false);
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);
395 input_unregister_device(encoder->input);
396 kfree(encoder);
398 if (!dev_get_platdata(&pdev->dev))
399 kfree(pdata);
401 return 0;
404 #ifdef CONFIG_PM_SLEEP
405 static int rotary_encoder_suspend(struct device *dev)
407 struct rotary_encoder *encoder = dev_get_drvdata(dev);
409 if (device_may_wakeup(dev)) {
410 enable_irq_wake(encoder->irq_a);
411 enable_irq_wake(encoder->irq_b);
414 return 0;
417 static int rotary_encoder_resume(struct device *dev)
419 struct rotary_encoder *encoder = dev_get_drvdata(dev);
421 if (device_may_wakeup(dev)) {
422 disable_irq_wake(encoder->irq_a);
423 disable_irq_wake(encoder->irq_b);
426 return 0;
428 #endif
430 static SIMPLE_DEV_PM_OPS(rotary_encoder_pm_ops,
431 rotary_encoder_suspend, rotary_encoder_resume);
433 static 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),
442 module_platform_driver(rotary_encoder_driver);
444 MODULE_ALIAS("platform:" DRV_NAME);
445 MODULE_DESCRIPTION("GPIO rotary encoder driver");
446 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>, Johan Hovold");
447 MODULE_LICENSE("GPL v2");