17 #include <linux/kernel.h>
18 #include <linux/module.h>
21 #include <linux/input.h>
22 #include <linux/device.h>
26 #include <linux/slab.h>
30 #define DRV_NAME "rotary-encoder"
56 return ((a << 1) | b);
59 static void rotary_encoder_report_event(
struct rotary_encoder *encoder)
64 input_report_rel(encoder->
input,
65 pdata->
axis, encoder->
dir ? -1 : 1);
67 unsigned int pos = encoder->
pos;
85 input_report_abs(encoder->
input, pdata->
axis, encoder->
pos);
88 input_sync(encoder->
input);
96 state = rotary_encoder_get_state(encoder->
pdata);
100 if (encoder->
armed) {
101 rotary_encoder_report_event(encoder);
102 encoder->
armed =
false;
109 encoder->
dir = state - 1;
113 encoder->
armed =
true;
120 static irqreturn_t rotary_encoder_half_period_irq(
int irq,
void *dev_id)
125 state = rotary_encoder_get_state(encoder->
pdata);
131 rotary_encoder_report_event(encoder);
146 static struct of_device_id rotary_encoder_of_match[] = {
153 rotary_encoder_parse_dt(
struct device *
dev)
169 of_property_read_u32(np,
"rotary-encoder,steps", &pdata->
steps);
170 of_property_read_u32(np,
"linux,axis", &pdata->
axis);
172 pdata->
gpio_a = of_get_gpio_flags(np, 0, &flags);
175 pdata->
gpio_b = of_get_gpio_flags(np, 1, &flags);
179 "rotary-encoder,relative-axis",
NULL);
181 "rotary-encoder,rollover",
NULL);
183 "rotary-encoder,half-period",
NULL);
189 rotary_encoder_parse_dt(
struct device *dev)
200 struct input_dev *
input;
205 pdata = rotary_encoder_parse_dt(dev);
207 return PTR_ERR(pdata);
210 dev_err(dev,
"missing platform data\n");
216 input = input_allocate_device();
217 if (!encoder || !input) {
225 input->name = pdev->
name;
227 input->dev.parent =
dev;
234 input_set_abs_params(encoder->
input,
248 goto exit_free_gpio_a;
256 handler = &rotary_encoder_half_period_irq;
257 encoder->
last_stable = rotary_encoder_get_state(pdata);
259 handler = &rotary_encoder_irq;
266 dev_err(dev,
"unable to request IRQ %d\n", encoder->
irq_a);
267 goto exit_free_gpio_b;
274 dev_err(dev,
"unable to request IRQ %d\n", encoder->
irq_b);
275 goto exit_free_irq_a;
278 err = input_register_device(input);
280 dev_err(dev,
"failed to register input device\n");
281 goto exit_free_irq_b;
284 platform_set_drvdata(pdev, encoder);
297 input_free_device(input);
299 if (!dev_get_platdata(&pdev->
dev))
315 input_unregister_device(encoder->
input);
318 if (!dev_get_platdata(&pdev->
dev))
321 platform_set_drvdata(pdev,
NULL);
327 .probe = rotary_encoder_probe,
332 .of_match_table =
of_match_ptr(rotary_encoder_of_match),