1 /* drivers/input/misc/gpio_input.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/gpio.h>
18 #include <linux/gpio_event.h>
19 #include <linux/hrtimer.h>
20 #include <linux/input.h>
21 #include <linux/interrupt.h>
22 #include <linux/slab.h>
25 DEBOUNCE_UNSTABLE
= BIT(0), /* Got irq, while debouncing */
26 DEBOUNCE_PRESSED
= BIT(1),
27 DEBOUNCE_NOTPRESSED
= BIT(2),
28 DEBOUNCE_WAIT_IRQ
= BIT(3), /* Stable irq state */
29 DEBOUNCE_POLL
= BIT(4), /* Stable polling state */
32 DEBOUNCE_PRESSED
| DEBOUNCE_NOTPRESSED
,
35 struct gpio_key_state
{
36 struct gpio_input_state
*ds
;
40 struct gpio_input_state
{
41 struct input_dev
*input_dev
;
42 const struct gpio_event_input_info
*info
;
47 struct gpio_key_state key_state
[0];
50 static enum hrtimer_restart
gpio_event_input_timer_func(struct hrtimer
*timer
)
54 struct gpio_input_state
*ds
=
55 container_of(timer
, struct gpio_input_state
, timer
);
56 unsigned gpio_flags
= ds
->info
->flags
;
58 int nkeys
= ds
->info
->keymap_size
;
59 const struct gpio_event_direct_entry
*key_entry
;
60 struct gpio_key_state
*key_state
;
61 unsigned long irqflags
;
65 key_entry
= kp
->keys_info
->keymap
;
66 key_state
= kp
->key_state
;
67 for (i
= 0; i
< nkeys
; i
++, key_entry
++, key_state
++)
68 pr_info("gpio_read_detect_status %d %d\n", key_entry
->gpio
,
69 gpio_read_detect_status(key_entry
->gpio
));
71 key_entry
= ds
->info
->keymap
;
72 key_state
= ds
->key_state
;
73 spin_lock_irqsave(&ds
->irq_lock
, irqflags
);
74 for (i
= 0; i
< nkeys
; i
++, key_entry
++, key_state
++) {
75 debounce
= key_state
->debounce
;
76 if (debounce
& DEBOUNCE_WAIT_IRQ
)
78 if (key_state
->debounce
& DEBOUNCE_UNSTABLE
) {
79 debounce
= key_state
->debounce
= DEBOUNCE_UNKNOWN
;
80 enable_irq(gpio_to_irq(key_entry
->gpio
));
81 pr_info("gpio_keys_scan_keys: key %x-%x, %d "
82 "(%d) continue debounce\n",
83 ds
->info
->type
, key_entry
->code
,
86 npolarity
= !(gpio_flags
& GPIOEDF_ACTIVE_HIGH
);
87 pressed
= gpio_get_value(key_entry
->gpio
) ^ npolarity
;
88 if (debounce
& DEBOUNCE_POLL
) {
89 if (pressed
== !(debounce
& DEBOUNCE_PRESSED
)) {
91 key_state
->debounce
= DEBOUNCE_UNKNOWN
;
92 if (gpio_flags
& GPIOEDF_PRINT_KEY_DEBOUNCE
)
93 pr_info("gpio_keys_scan_keys: key %x-"
94 "%x, %d (%d) start debounce\n",
95 ds
->info
->type
, key_entry
->code
,
100 if (pressed
&& (debounce
& DEBOUNCE_NOTPRESSED
)) {
101 if (gpio_flags
& GPIOEDF_PRINT_KEY_DEBOUNCE
)
102 pr_info("gpio_keys_scan_keys: key %x-%x, %d "
103 "(%d) debounce pressed 1\n",
104 ds
->info
->type
, key_entry
->code
,
106 key_state
->debounce
= DEBOUNCE_PRESSED
;
109 if (!pressed
&& (debounce
& DEBOUNCE_PRESSED
)) {
110 if (gpio_flags
& GPIOEDF_PRINT_KEY_DEBOUNCE
)
111 pr_info("gpio_keys_scan_keys: key %x-%x, %d "
112 "(%d) debounce pressed 0\n",
113 ds
->info
->type
, key_entry
->code
,
115 key_state
->debounce
= DEBOUNCE_NOTPRESSED
;
119 ds
->debounce_count
--;
121 key_state
->debounce
|= DEBOUNCE_WAIT_IRQ
;
123 key_state
->debounce
|= DEBOUNCE_POLL
;
124 if (gpio_flags
& GPIOEDF_PRINT_KEYS
)
125 pr_info("gpio_keys_scan_keys: key %x-%x, %d (%d) "
126 "changed to %d\n", ds
->info
->type
,
127 key_entry
->code
, i
, key_entry
->gpio
, pressed
);
128 input_event(ds
->input_dev
, ds
->info
->type
,
129 key_entry
->code
, pressed
);
133 key_entry
= kp
->keys_info
->keymap
;
134 key_state
= kp
->key_state
;
135 for (i
= 0; i
< nkeys
; i
++, key_entry
++, key_state
++) {
136 pr_info("gpio_read_detect_status %d %d\n", key_entry
->gpio
,
137 gpio_read_detect_status(key_entry
->gpio
));
141 if (ds
->debounce_count
)
142 hrtimer_start(timer
, ds
->info
->debounce_time
, HRTIMER_MODE_REL
);
143 else if (!ds
->use_irq
)
144 hrtimer_start(timer
, ds
->info
->poll_time
, HRTIMER_MODE_REL
);
146 spin_unlock_irqrestore(&ds
->irq_lock
, irqflags
);
148 return HRTIMER_NORESTART
;
151 static irqreturn_t
gpio_event_input_irq_handler(int irq
, void *dev_id
)
153 struct gpio_key_state
*ks
= dev_id
;
154 struct gpio_input_state
*ds
= ks
->ds
;
155 int keymap_index
= ks
- ds
->key_state
;
156 const struct gpio_event_direct_entry
*key_entry
;
157 unsigned long irqflags
;
163 key_entry
= &ds
->info
->keymap
[keymap_index
];
165 if (ds
->info
->debounce_time
.tv64
) {
166 spin_lock_irqsave(&ds
->irq_lock
, irqflags
);
167 if (ks
->debounce
& DEBOUNCE_WAIT_IRQ
) {
168 ks
->debounce
= DEBOUNCE_UNKNOWN
;
169 if (ds
->debounce_count
++ == 0) {
171 &ds
->timer
, ds
->info
->debounce_time
,
174 if (ds
->info
->flags
& GPIOEDF_PRINT_KEY_DEBOUNCE
)
175 pr_info("gpio_event_input_irq_handler: "
176 "key %x-%x, %d (%d) start debounce\n",
177 ds
->info
->type
, key_entry
->code
,
178 keymap_index
, key_entry
->gpio
);
181 ks
->debounce
= DEBOUNCE_UNSTABLE
;
183 spin_unlock_irqrestore(&ds
->irq_lock
, irqflags
);
185 pressed
= gpio_get_value(key_entry
->gpio
) ^
186 !(ds
->info
->flags
& GPIOEDF_ACTIVE_HIGH
);
187 if (ds
->info
->flags
& GPIOEDF_PRINT_KEYS
)
188 pr_info("gpio_event_input_irq_handler: key %x-%x, %d "
189 "(%d) changed to %d\n",
190 ds
->info
->type
, key_entry
->code
, keymap_index
,
191 key_entry
->gpio
, pressed
);
192 input_event(ds
->input_dev
, ds
->info
->type
,
193 key_entry
->code
, pressed
);
198 static int gpio_event_input_request_irqs(struct gpio_input_state
*ds
)
203 unsigned long req_flags
= IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
;
205 for (i
= 0; i
< ds
->info
->keymap_size
; i
++) {
206 err
= irq
= gpio_to_irq(ds
->info
->keymap
[i
].gpio
);
208 goto err_gpio_get_irq_num_failed
;
209 err
= request_irq(irq
, gpio_event_input_irq_handler
,
210 req_flags
, "gpio_keys", &ds
->key_state
[i
]);
212 pr_err("gpio_event_input_request_irqs: request_irq "
213 "failed for input %d, irq %d\n",
214 ds
->info
->keymap
[i
].gpio
, irq
);
215 goto err_request_irq_failed
;
217 enable_irq_wake(irq
);
221 for (i
= ds
->info
->keymap_size
- 1; i
>= 0; i
--) {
222 free_irq(gpio_to_irq(ds
->info
->keymap
[i
].gpio
),
224 err_request_irq_failed
:
225 err_gpio_get_irq_num_failed
:
231 int gpio_event_input_func(struct input_dev
*input_dev
,
232 struct gpio_event_info
*info
, void **data
, int func
)
236 unsigned long irqflags
;
237 struct gpio_event_input_info
*di
;
238 struct gpio_input_state
*ds
= *data
;
240 di
= container_of(info
, struct gpio_event_input_info
, info
);
242 if (func
== GPIO_EVENT_FUNC_SUSPEND
) {
243 spin_lock_irqsave(&ds
->irq_lock
, irqflags
);
245 for (i
= 0; i
< di
->keymap_size
; i
++)
246 disable_irq(gpio_to_irq(di
->keymap
[i
].gpio
));
247 spin_unlock_irqrestore(&ds
->irq_lock
, irqflags
);
248 hrtimer_cancel(&ds
->timer
);
251 if (func
== GPIO_EVENT_FUNC_RESUME
) {
252 spin_lock_irqsave(&ds
->irq_lock
, irqflags
);
254 for (i
= 0; i
< di
->keymap_size
; i
++)
255 enable_irq(gpio_to_irq(di
->keymap
[i
].gpio
));
256 hrtimer_start(&ds
->timer
, ktime_set(0, 0), HRTIMER_MODE_REL
);
257 spin_unlock_irqrestore(&ds
->irq_lock
, irqflags
);
261 if (func
== GPIO_EVENT_FUNC_INIT
) {
262 if (ktime_to_ns(di
->poll_time
) <= 0)
263 di
->poll_time
= ktime_set(0, 20 * NSEC_PER_MSEC
);
265 *data
= ds
= kzalloc(sizeof(*ds
) + sizeof(ds
->key_state
[0]) *
266 di
->keymap_size
, GFP_KERNEL
);
269 pr_err("gpio_event_input_func: "
270 "Failed to allocate private data\n");
271 goto err_ds_alloc_failed
;
273 ds
->debounce_count
= di
->keymap_size
;
274 ds
->input_dev
= input_dev
;
276 spin_lock_init(&ds
->irq_lock
);
278 for (i
= 0; i
< di
->keymap_size
; i
++) {
279 input_set_capability(input_dev
, di
->type
,
281 ds
->key_state
[i
].ds
= ds
;
282 ds
->key_state
[i
].debounce
= DEBOUNCE_UNKNOWN
;
285 for (i
= 0; i
< di
->keymap_size
; i
++) {
286 ret
= gpio_request(di
->keymap
[i
].gpio
, "gpio_kp_in");
288 pr_err("gpio_event_input_func: gpio_request "
289 "failed for %d\n", di
->keymap
[i
].gpio
);
290 goto err_gpio_request_failed
;
292 ret
= gpio_direction_input(di
->keymap
[i
].gpio
);
294 pr_err("gpio_event_input_func: "
295 "gpio_direction_input failed for %d\n",
297 goto err_gpio_configure_failed
;
301 ret
= gpio_event_input_request_irqs(ds
);
303 spin_lock_irqsave(&ds
->irq_lock
, irqflags
);
304 ds
->use_irq
= ret
== 0;
306 pr_info("GPIO Input Driver: Start gpio inputs for %s in %s "
308 input_dev
->name
, ret
== 0 ? "interrupt" : "polling");
310 hrtimer_init(&ds
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
311 ds
->timer
.function
= gpio_event_input_timer_func
;
312 hrtimer_start(&ds
->timer
, ktime_set(0, 0), HRTIMER_MODE_REL
);
313 spin_unlock_irqrestore(&ds
->irq_lock
, irqflags
);
318 spin_lock_irqsave(&ds
->irq_lock
, irqflags
);
319 hrtimer_cancel(&ds
->timer
);
321 for (i
= di
->keymap_size
- 1; i
>= 0; i
--) {
322 free_irq(gpio_to_irq(di
->keymap
[i
].gpio
),
326 spin_unlock_irqrestore(&ds
->irq_lock
, irqflags
);
328 for (i
= di
->keymap_size
- 1; i
>= 0; i
--) {
329 err_gpio_configure_failed
:
330 gpio_free(di
->keymap
[i
].gpio
);
331 err_gpio_request_failed
: