diff options
author | Johan Hovold <jhovold@gmail.com> | 2011-05-11 19:33:50 -0400 |
---|---|---|
committer | Dmitry Torokhov <dmitry.torokhov@gmail.com> | 2011-05-11 19:54:57 -0400 |
commit | 521a8f5cb79d1017d00d26143227159674e3b79d (patch) | |
tree | 557f868f6b926717687cf65480a857e0e4e0b747 | |
parent | baafb435372dd48e3854028aec669effae957c9e (diff) |
Input: rotary-encoder - refactor and clean up
Refactor state retrieval and event reporting in interrupt handler.
Remove a few empty lines.
Signed-off-by: Johan Hovold <jhovold@gmail.com>
Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com>
Acked-by: Daniel Mack <zonque@gmail.com>
Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
-rw-r--r-- | drivers/input/misc/rotary_encoder.c | 77 |
1 files changed, 44 insertions, 33 deletions
diff --git a/drivers/input/misc/rotary_encoder.c b/drivers/input/misc/rotary_encoder.c index 7e64d01da2be..077b80bdca69 100644 --- a/drivers/input/misc/rotary_encoder.c +++ b/drivers/input/misc/rotary_encoder.c | |||
@@ -40,50 +40,62 @@ struct rotary_encoder { | |||
40 | unsigned char dir; /* 0 - clockwise, 1 - CCW */ | 40 | unsigned char dir; /* 0 - clockwise, 1 - CCW */ |
41 | }; | 41 | }; |
42 | 42 | ||
43 | static irqreturn_t rotary_encoder_irq(int irq, void *dev_id) | 43 | static int rotary_encoder_get_state(struct rotary_encoder_platform_data *pdata) |
44 | { | 44 | { |
45 | struct rotary_encoder *encoder = dev_id; | ||
46 | struct rotary_encoder_platform_data *pdata = encoder->pdata; | ||
47 | int a = !!gpio_get_value(pdata->gpio_a); | 45 | int a = !!gpio_get_value(pdata->gpio_a); |
48 | int b = !!gpio_get_value(pdata->gpio_b); | 46 | int b = !!gpio_get_value(pdata->gpio_b); |
49 | int state; | ||
50 | 47 | ||
51 | a ^= pdata->inverted_a; | 48 | a ^= pdata->inverted_a; |
52 | b ^= pdata->inverted_b; | 49 | b ^= pdata->inverted_b; |
53 | state = (a << 1) | b; | ||
54 | 50 | ||
55 | switch (state) { | 51 | return ((a << 1) | b); |
52 | } | ||
56 | 53 | ||
57 | case 0x0: | 54 | static void rotary_encoder_report_event(struct rotary_encoder *encoder) |
58 | if (!encoder->armed) | 55 | { |
59 | break; | 56 | struct rotary_encoder_platform_data *pdata = encoder->pdata; |
60 | 57 | ||
61 | if (pdata->relative_axis) { | 58 | if (pdata->relative_axis) { |
62 | input_report_rel(encoder->input, pdata->axis, | 59 | input_report_rel(encoder->input, |
63 | encoder->dir ? -1 : 1); | 60 | pdata->axis, encoder->dir ? -1 : 1); |
64 | } else { | 61 | } else { |
65 | unsigned int pos = encoder->pos; | 62 | unsigned int pos = encoder->pos; |
66 | 63 | ||
67 | if (encoder->dir) { | 64 | if (encoder->dir) { |
68 | /* turning counter-clockwise */ | 65 | /* turning counter-clockwise */ |
69 | if (pdata->rollover) | ||
70 | pos += pdata->steps; | ||
71 | if (pos) | ||
72 | pos--; | ||
73 | } else { | ||
74 | /* turning clockwise */ | ||
75 | if (pdata->rollover || pos < pdata->steps) | ||
76 | pos++; | ||
77 | } | ||
78 | if (pdata->rollover) | 66 | if (pdata->rollover) |
79 | pos %= pdata->steps; | 67 | pos += pdata->steps; |
80 | encoder->pos = pos; | 68 | if (pos) |
81 | input_report_abs(encoder->input, pdata->axis, | 69 | pos--; |
82 | encoder->pos); | 70 | } else { |
71 | /* turning clockwise */ | ||
72 | if (pdata->rollover || pos < pdata->steps) | ||
73 | pos++; | ||
83 | } | 74 | } |
84 | input_sync(encoder->input); | ||
85 | 75 | ||
86 | encoder->armed = false; | 76 | if (pdata->rollover) |
77 | pos %= pdata->steps; | ||
78 | |||
79 | encoder->pos = pos; | ||
80 | input_report_abs(encoder->input, pdata->axis, encoder->pos); | ||
81 | } | ||
82 | |||
83 | input_sync(encoder->input); | ||
84 | } | ||
85 | |||
86 | static irqreturn_t rotary_encoder_irq(int irq, void *dev_id) | ||
87 | { | ||
88 | struct rotary_encoder *encoder = dev_id; | ||
89 | int state; | ||
90 | |||
91 | state = rotary_encoder_get_state(encoder->pdata); | ||
92 | |||
93 | switch (state) { | ||
94 | case 0x0: | ||
95 | if (encoder->armed) { | ||
96 | rotary_encoder_report_event(encoder); | ||
97 | encoder->armed = false; | ||
98 | } | ||
87 | break; | 99 | break; |
88 | 100 | ||
89 | case 0x1: | 101 | case 0x1: |
@@ -254,4 +266,3 @@ MODULE_ALIAS("platform:" DRV_NAME); | |||
254 | MODULE_DESCRIPTION("GPIO rotary encoder driver"); | 266 | MODULE_DESCRIPTION("GPIO rotary encoder driver"); |
255 | MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); | 267 | MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); |
256 | MODULE_LICENSE("GPL v2"); | 268 | MODULE_LICENSE("GPL v2"); |
257 | |||