4 * (c) 2009 Daniel Mack <daniel@caiaq.de>
6 * state machine code inspired by code from Tim Ruetz
8 * A generic driver for rotary encoders connected to GPIO lines.
9 * See file:Documentation/input/rotary_encoder.txt for more information
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.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>
26 #define DRV_NAME "rotary-encoder"
28 struct rotary_encoder {
34 struct input_dev *input;
35 struct rotary_encoder_platform_data *pdata;
38 static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
40 struct rotary_encoder *encoder = dev_id;
41 struct rotary_encoder_platform_data *pdata = encoder->pdata;
42 int a = !!gpio_get_value(pdata->gpio_a);
43 int b = !!gpio_get_value(pdata->gpio_b);
46 a ^= pdata->inverted_a;
47 b ^= pdata->inverted_b;
57 /* turning counter-clockwise */
58 encoder->pos += pdata->steps;
60 encoder->pos %= pdata->steps;
62 /* turning clockwise */
64 encoder->pos %= pdata->steps;
67 input_report_abs(encoder->input, pdata->axis, encoder->pos);
68 input_sync(encoder->input);
76 encoder->dir = state - 1;
87 static int __devinit rotary_encoder_probe(struct platform_device *pdev)
89 struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
90 struct rotary_encoder *encoder;
91 struct input_dev *input;
94 if (!pdata || !pdata->steps) {
95 dev_err(&pdev->dev, "invalid platform data\n");
99 encoder = kzalloc(sizeof(struct rotary_encoder), GFP_KERNEL);
100 input = input_allocate_device();
101 if (!encoder || !input) {
102 dev_err(&pdev->dev, "failed to allocate memory for device\n");
107 encoder->input = input;
108 encoder->pdata = pdata;
109 encoder->irq_a = gpio_to_irq(pdata->gpio_a);
110 encoder->irq_b = gpio_to_irq(pdata->gpio_b);
112 /* create and register the input driver */
113 input->name = pdev->name;
114 input->id.bustype = BUS_HOST;
115 input->dev.parent = &pdev->dev;
116 input->evbit[0] = BIT_MASK(EV_ABS);
117 input_set_abs_params(encoder->input,
118 pdata->axis, 0, pdata->steps, 0, 1);
120 err = input_register_device(input);
122 dev_err(&pdev->dev, "failed to register input device\n");
126 /* request the GPIOs */
127 err = gpio_request(pdata->gpio_a, DRV_NAME);
129 dev_err(&pdev->dev, "unable to request GPIO %d\n",
131 goto exit_unregister_input;
134 err = gpio_request(pdata->gpio_b, DRV_NAME);
136 dev_err(&pdev->dev, "unable to request GPIO %d\n",
138 goto exit_free_gpio_a;
141 /* request the IRQs */
142 err = request_irq(encoder->irq_a, &rotary_encoder_irq,
143 IORESOURCE_IRQ_HIGHEDGE | IORESOURCE_IRQ_LOWEDGE,
146 dev_err(&pdev->dev, "unable to request IRQ %d\n",
148 goto exit_free_gpio_b;
151 err = request_irq(encoder->irq_b, &rotary_encoder_irq,
152 IORESOURCE_IRQ_HIGHEDGE | IORESOURCE_IRQ_LOWEDGE,
155 dev_err(&pdev->dev, "unable to request IRQ %d\n",
157 goto exit_free_irq_a;
160 platform_set_drvdata(pdev, encoder);
165 free_irq(encoder->irq_a, encoder);
167 gpio_free(pdata->gpio_b);
169 gpio_free(pdata->gpio_a);
170 exit_unregister_input:
171 input_unregister_device(input);
172 input = NULL; /* so we don't try to free it */
174 input_free_device(input);
179 static int __devexit rotary_encoder_remove(struct platform_device *pdev)
181 struct rotary_encoder *encoder = platform_get_drvdata(pdev);
182 struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
184 free_irq(encoder->irq_a, encoder);
185 free_irq(encoder->irq_b, encoder);
186 gpio_free(pdata->gpio_a);
187 gpio_free(pdata->gpio_b);
188 input_unregister_device(encoder->input);
189 platform_set_drvdata(pdev, NULL);
195 static struct platform_driver rotary_encoder_driver = {
196 .probe = rotary_encoder_probe,
197 .remove = __devexit_p(rotary_encoder_remove),
200 .owner = THIS_MODULE,
204 static int __init rotary_encoder_init(void)
206 return platform_driver_register(&rotary_encoder_driver);
209 static void __exit rotary_encoder_exit(void)
211 platform_driver_unregister(&rotary_encoder_driver);
214 module_init(rotary_encoder_init);
215 module_exit(rotary_encoder_exit);
217 MODULE_ALIAS("platform:" DRV_NAME);
218 MODULE_DESCRIPTION("GPIO rotary encoder driver");
219 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
220 MODULE_LICENSE("GPL v2");