1 /* drivers/input/misc/gpio_axis.c
3 * Copyright (C) 2007 Google, Inc.
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/gpio.h>
19 #include <linux/gpio_event.h>
20 #include <linux/interrupt.h>
22 struct gpio_axis_state
{
23 struct input_dev
*input_dev
;
24 struct gpio_event_axis_info
*info
;
28 uint16_t gpio_axis_4bit_gray_map_table
[] = {
29 [0x0] = 0x0, [0x1] = 0x1, /* 0000 0001 */
30 [0x3] = 0x2, [0x2] = 0x3, /* 0011 0010 */
31 [0x6] = 0x4, [0x7] = 0x5, /* 0110 0111 */
32 [0x5] = 0x6, [0x4] = 0x7, /* 0101 0100 */
33 [0xc] = 0x8, [0xd] = 0x9, /* 1100 1101 */
34 [0xf] = 0xa, [0xe] = 0xb, /* 1111 1110 */
35 [0xa] = 0xc, [0xb] = 0xd, /* 1010 1011 */
36 [0x9] = 0xe, [0x8] = 0xf, /* 1001 1000 */
38 uint16_t gpio_axis_4bit_gray_map(struct gpio_event_axis_info
*info
, uint16_t in
)
40 return gpio_axis_4bit_gray_map_table
[in
];
43 uint16_t gpio_axis_5bit_singletrack_map_table
[] = {
44 [0x10] = 0x00, [0x14] = 0x01, [0x1c] = 0x02, /* 10000 10100 11100 */
45 [0x1e] = 0x03, [0x1a] = 0x04, [0x18] = 0x05, /* 11110 11010 11000 */
46 [0x08] = 0x06, [0x0a] = 0x07, [0x0e] = 0x08, /* 01000 01010 01110 */
47 [0x0f] = 0x09, [0x0d] = 0x0a, [0x0c] = 0x0b, /* 01111 01101 01100 */
48 [0x04] = 0x0c, [0x05] = 0x0d, [0x07] = 0x0e, /* 00100 00101 00111 */
49 [0x17] = 0x0f, [0x16] = 0x10, [0x06] = 0x11, /* 10111 10110 00110 */
50 [0x02] = 0x12, [0x12] = 0x13, [0x13] = 0x14, /* 00010 10010 10011 */
51 [0x1b] = 0x15, [0x0b] = 0x16, [0x03] = 0x17, /* 11011 01011 00011 */
52 [0x01] = 0x18, [0x09] = 0x19, [0x19] = 0x1a, /* 00001 01001 11001 */
53 [0x1d] = 0x1b, [0x15] = 0x1c, [0x11] = 0x1d, /* 11101 10101 10001 */
55 uint16_t gpio_axis_5bit_singletrack_map(
56 struct gpio_event_axis_info
*info
, uint16_t in
)
58 return gpio_axis_5bit_singletrack_map_table
[in
];
61 static void gpio_event_update_axis(struct gpio_axis_state
*as
, int report
)
63 struct gpio_event_axis_info
*ai
= as
->info
;
68 uint16_t old_pos
= as
->pos
;
69 for (i
= ai
->count
- 1; i
>= 0; i
--)
70 state
= (state
<< 1) | gpio_get_value(ai
->gpio
[i
]);
71 pos
= ai
->map(ai
, state
);
72 if (ai
->flags
& GPIOEAF_PRINT_RAW
)
73 pr_info("axis %d-%d raw %x, pos %d -> %d\n",
74 ai
->type
, ai
->code
, state
, old_pos
, pos
);
75 if (report
&& pos
!= old_pos
) {
76 if (ai
->type
== EV_REL
) {
77 change
= (ai
->decoded_size
+ pos
- old_pos
) %
79 if (change
> ai
->decoded_size
/ 2)
80 change
-= ai
->decoded_size
;
81 if (change
== ai
->decoded_size
/ 2) {
82 if (ai
->flags
& GPIOEAF_PRINT_EVENT
)
83 pr_info("axis %d-%d unknown direction, "
84 "pos %d -> %d\n", ai
->type
,
85 ai
->code
, old_pos
, pos
);
86 change
= 0; /* no closest direction */
88 if (ai
->flags
& GPIOEAF_PRINT_EVENT
)
89 pr_info("axis %d-%d change %d\n",
90 ai
->type
, ai
->code
, change
);
91 input_report_rel(as
->input_dev
, ai
->code
, change
);
93 if (ai
->flags
& GPIOEAF_PRINT_EVENT
)
94 pr_info("axis %d-%d now %d\n",
95 ai
->type
, ai
->code
, pos
);
96 input_event(as
->input_dev
, ai
->type
, ai
->code
, pos
);
98 input_sync(as
->input_dev
);
103 static irqreturn_t
gpio_axis_irq_handler(int irq
, void *dev_id
)
105 struct gpio_axis_state
*as
= dev_id
;
106 gpio_event_update_axis(as
, 1);
110 int gpio_event_axis_func(struct input_dev
*input_dev
,
111 struct gpio_event_info
*info
, void **data
, int func
)
116 struct gpio_event_axis_info
*ai
;
117 struct gpio_axis_state
*as
;
119 ai
= container_of(info
, struct gpio_event_axis_info
, info
);
120 if (func
== GPIO_EVENT_FUNC_SUSPEND
) {
121 for (i
= 0; i
< ai
->count
; i
++)
122 disable_irq(gpio_to_irq(ai
->gpio
[i
]));
125 if (func
== GPIO_EVENT_FUNC_RESUME
) {
126 for (i
= 0; i
< ai
->count
; i
++)
127 enable_irq(gpio_to_irq(ai
->gpio
[i
]));
131 if (func
== GPIO_EVENT_FUNC_INIT
) {
132 *data
= as
= kmalloc(sizeof(*as
), GFP_KERNEL
);
135 goto err_alloc_axis_state_failed
;
137 as
->input_dev
= input_dev
;
140 input_set_capability(input_dev
, ai
->type
, ai
->code
);
141 if (ai
->type
== EV_ABS
) {
142 input_set_abs_params(input_dev
, ai
->code
, 0,
143 ai
->decoded_size
- 1, 0, 0);
145 for (i
= 0; i
< ai
->count
; i
++) {
146 ret
= gpio_request(ai
->gpio
[i
], "gpio_event_axis");
148 goto err_request_gpio_failed
;
149 ret
= gpio_direction_input(ai
->gpio
[i
]);
151 goto err_gpio_direction_input_failed
;
152 ret
= irq
= gpio_to_irq(ai
->gpio
[i
]);
154 goto err_get_irq_num_failed
;
155 ret
= request_irq(irq
, gpio_axis_irq_handler
,
156 IRQF_TRIGGER_RISING
|
157 IRQF_TRIGGER_FALLING
,
158 "gpio_event_axis", as
);
160 goto err_request_irq_failed
;
162 gpio_event_update_axis(as
, 0);
168 for (i
= ai
->count
- 1; i
>= 0; i
--) {
169 free_irq(gpio_to_irq(ai
->gpio
[i
]), as
);
170 err_request_irq_failed
:
171 err_get_irq_num_failed
:
172 err_gpio_direction_input_failed
:
173 gpio_free(ai
->gpio
[i
]);
174 err_request_gpio_failed
:
179 err_alloc_axis_state_failed
: