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>
23 #include <linux/pm_wakeup.h>
26 DEBOUNCE_UNSTABLE = BIT(0), /* Got irq, while debouncing */
27 DEBOUNCE_PRESSED = BIT(1),
28 DEBOUNCE_NOTPRESSED = BIT(2),
29 DEBOUNCE_WAIT_IRQ = BIT(3), /* Stable irq state */
30 DEBOUNCE_POLL = BIT(4), /* Stable polling state */
33 DEBOUNCE_PRESSED | DEBOUNCE_NOTPRESSED,
36 struct gpio_key_state {
37 struct gpio_input_state *ds;
41 struct gpio_input_state {
42 struct gpio_event_input_devs *input_devs;
43 const struct gpio_event_input_info *info;
48 struct wakeup_source *ws;
49 struct gpio_key_state key_state[0];
52 static enum hrtimer_restart gpio_event_input_timer_func(struct hrtimer *timer)
56 struct gpio_input_state *ds =
57 container_of(timer, struct gpio_input_state, timer);
58 unsigned gpio_flags = ds->info->flags;
60 int nkeys = ds->info->keymap_size;
61 const struct gpio_event_direct_entry *key_entry;
62 struct gpio_key_state *key_state;
63 unsigned long irqflags;
68 key_entry = kp->keys_info->keymap;
69 key_state = kp->key_state;
70 for (i = 0; i < nkeys; i++, key_entry++, key_state++)
71 pr_info("gpio_read_detect_status %d %d\n", key_entry->gpio,
72 gpio_read_detect_status(key_entry->gpio));
74 key_entry = ds->info->keymap;
75 key_state = ds->key_state;
77 spin_lock_irqsave(&ds->irq_lock, irqflags);
78 for (i = 0; i < nkeys; i++, key_entry++, key_state++) {
79 debounce = key_state->debounce;
80 if (debounce & DEBOUNCE_WAIT_IRQ)
82 if (key_state->debounce & DEBOUNCE_UNSTABLE) {
83 debounce = key_state->debounce = DEBOUNCE_UNKNOWN;
84 enable_irq(gpio_to_irq(key_entry->gpio));
85 if (gpio_flags & GPIOEDF_PRINT_KEY_UNSTABLE)
86 pr_info("gpio_keys_scan_keys: key %x-%x, %d "
87 "(%d) continue debounce\n",
88 ds->info->type, key_entry->code,
91 npolarity = !(gpio_flags & GPIOEDF_ACTIVE_HIGH);
92 pressed = gpio_get_value(key_entry->gpio) ^ npolarity;
93 if (debounce & DEBOUNCE_POLL) {
94 if (pressed == !(debounce & DEBOUNCE_PRESSED)) {
96 key_state->debounce = DEBOUNCE_UNKNOWN;
97 if (gpio_flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
98 pr_info("gpio_keys_scan_keys: key %x-"
99 "%x, %d (%d) start debounce\n",
100 ds->info->type, key_entry->code,
105 if (pressed && (debounce & DEBOUNCE_NOTPRESSED)) {
106 if (gpio_flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
107 pr_info("gpio_keys_scan_keys: key %x-%x, %d "
108 "(%d) debounce pressed 1\n",
109 ds->info->type, key_entry->code,
111 key_state->debounce = DEBOUNCE_PRESSED;
114 if (!pressed && (debounce & DEBOUNCE_PRESSED)) {
115 if (gpio_flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
116 pr_info("gpio_keys_scan_keys: key %x-%x, %d "
117 "(%d) debounce pressed 0\n",
118 ds->info->type, key_entry->code,
120 key_state->debounce = DEBOUNCE_NOTPRESSED;
124 ds->debounce_count--;
126 key_state->debounce |= DEBOUNCE_WAIT_IRQ;
128 key_state->debounce |= DEBOUNCE_POLL;
129 if (gpio_flags & GPIOEDF_PRINT_KEYS)
130 pr_info("gpio_keys_scan_keys: key %x-%x, %d (%d) "
131 "changed to %d\n", ds->info->type,
132 key_entry->code, i, key_entry->gpio, pressed);
133 input_event(ds->input_devs->dev[key_entry->dev], ds->info->type,
134 key_entry->code, pressed);
138 for (i = 0; i < ds->input_devs->count; i++)
139 input_sync(ds->input_devs->dev[i]);
143 key_entry = kp->keys_info->keymap;
144 key_state = kp->key_state;
145 for (i = 0; i < nkeys; i++, key_entry++, key_state++) {
146 pr_info("gpio_read_detect_status %d %d\n", key_entry->gpio,
147 gpio_read_detect_status(key_entry->gpio));
151 if (ds->debounce_count)
152 hrtimer_start(timer, ds->info->debounce_time, HRTIMER_MODE_REL);
153 else if (!ds->use_irq)
154 hrtimer_start(timer, ds->info->poll_time, HRTIMER_MODE_REL);
158 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
160 return HRTIMER_NORESTART;
163 static irqreturn_t gpio_event_input_irq_handler(int irq, void *dev_id)
165 struct gpio_key_state *ks = dev_id;
166 struct gpio_input_state *ds = ks->ds;
167 int keymap_index = ks - ds->key_state;
168 const struct gpio_event_direct_entry *key_entry;
169 unsigned long irqflags;
175 key_entry = &ds->info->keymap[keymap_index];
177 if (ds->info->debounce_time.tv64) {
178 spin_lock_irqsave(&ds->irq_lock, irqflags);
179 if (ks->debounce & DEBOUNCE_WAIT_IRQ) {
180 ks->debounce = DEBOUNCE_UNKNOWN;
181 if (ds->debounce_count++ == 0) {
182 __pm_stay_awake(ds->ws);
184 &ds->timer, ds->info->debounce_time,
187 if (ds->info->flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
188 pr_info("gpio_event_input_irq_handler: "
189 "key %x-%x, %d (%d) start debounce\n",
190 ds->info->type, key_entry->code,
191 keymap_index, key_entry->gpio);
193 disable_irq_nosync(irq);
194 ks->debounce = DEBOUNCE_UNSTABLE;
196 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
198 pressed = gpio_get_value(key_entry->gpio) ^
199 !(ds->info->flags & GPIOEDF_ACTIVE_HIGH);
200 if (ds->info->flags & GPIOEDF_PRINT_KEYS)
201 pr_info("gpio_event_input_irq_handler: key %x-%x, %d "
202 "(%d) changed to %d\n",
203 ds->info->type, key_entry->code, keymap_index,
204 key_entry->gpio, pressed);
205 input_event(ds->input_devs->dev[key_entry->dev], ds->info->type,
206 key_entry->code, pressed);
207 input_sync(ds->input_devs->dev[key_entry->dev]);
212 static int gpio_event_input_request_irqs(struct gpio_input_state *ds)
217 unsigned long req_flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
219 for (i = 0; i < ds->info->keymap_size; i++) {
220 err = irq = gpio_to_irq(ds->info->keymap[i].gpio);
222 goto err_gpio_get_irq_num_failed;
223 err = request_irq(irq, gpio_event_input_irq_handler,
224 req_flags, "gpio_keys", &ds->key_state[i]);
226 pr_err("gpio_event_input_request_irqs: request_irq "
227 "failed for input %d, irq %d\n",
228 ds->info->keymap[i].gpio, irq);
229 goto err_request_irq_failed;
231 if (ds->info->info.no_suspend) {
232 err = enable_irq_wake(irq);
234 pr_err("gpio_event_input_request_irqs: "
235 "enable_irq_wake failed for input %d, "
237 ds->info->keymap[i].gpio, irq);
238 goto err_enable_irq_wake_failed;
244 for (i = ds->info->keymap_size - 1; i >= 0; i--) {
245 irq = gpio_to_irq(ds->info->keymap[i].gpio);
246 if (ds->info->info.no_suspend)
247 disable_irq_wake(irq);
248 err_enable_irq_wake_failed:
249 free_irq(irq, &ds->key_state[i]);
250 err_request_irq_failed:
251 err_gpio_get_irq_num_failed:
257 int gpio_event_input_func(struct gpio_event_input_devs *input_devs,
258 struct gpio_event_info *info, void **data, int func)
262 unsigned long irqflags;
263 struct gpio_event_input_info *di;
264 struct gpio_input_state *ds = *data;
267 di = container_of(info, struct gpio_event_input_info, info);
269 if (func == GPIO_EVENT_FUNC_SUSPEND) {
271 for (i = 0; i < di->keymap_size; i++)
272 disable_irq(gpio_to_irq(di->keymap[i].gpio));
273 hrtimer_cancel(&ds->timer);
276 if (func == GPIO_EVENT_FUNC_RESUME) {
277 spin_lock_irqsave(&ds->irq_lock, irqflags);
279 for (i = 0; i < di->keymap_size; i++)
280 enable_irq(gpio_to_irq(di->keymap[i].gpio));
281 hrtimer_start(&ds->timer, ktime_set(0, 0), HRTIMER_MODE_REL);
282 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
286 if (func == GPIO_EVENT_FUNC_INIT) {
287 if (ktime_to_ns(di->poll_time) <= 0)
288 di->poll_time = ktime_set(0, 20 * NSEC_PER_MSEC);
290 *data = ds = kzalloc(sizeof(*ds) + sizeof(ds->key_state[0]) *
291 di->keymap_size, GFP_KERNEL);
294 pr_err("gpio_event_input_func: "
295 "Failed to allocate private data\n");
296 goto err_ds_alloc_failed;
298 ds->debounce_count = di->keymap_size;
299 ds->input_devs = input_devs;
301 wlname = kasprintf(GFP_KERNEL, "gpio_input:%s%s",
302 input_devs->dev[0]->name,
303 (input_devs->count > 1) ? "..." : "");
305 ds->ws = wakeup_source_register(wlname);
309 pr_err("gpio_event_input_func: "
310 "Failed to allocate wakeup source\n");
314 spin_lock_init(&ds->irq_lock);
316 for (i = 0; i < di->keymap_size; i++) {
317 int dev = di->keymap[i].dev;
318 if (dev >= input_devs->count) {
319 pr_err("gpio_event_input_func: bad device "
320 "index %d >= %d for key code %d\n",
321 dev, input_devs->count,
326 input_set_capability(input_devs->dev[dev], di->type,
328 ds->key_state[i].ds = ds;
329 ds->key_state[i].debounce = DEBOUNCE_UNKNOWN;
332 for (i = 0; i < di->keymap_size; i++) {
333 ret = gpio_request(di->keymap[i].gpio, "gpio_kp_in");
335 pr_err("gpio_event_input_func: gpio_request "
336 "failed for %d\n", di->keymap[i].gpio);
337 goto err_gpio_request_failed;
339 ret = gpio_direction_input(di->keymap[i].gpio);
341 pr_err("gpio_event_input_func: "
342 "gpio_direction_input failed for %d\n",
344 goto err_gpio_configure_failed;
348 ret = gpio_event_input_request_irqs(ds);
350 spin_lock_irqsave(&ds->irq_lock, irqflags);
351 ds->use_irq = ret == 0;
353 pr_info("GPIO Input Driver: Start gpio inputs for %s%s in %s "
354 "mode\n", input_devs->dev[0]->name,
355 (input_devs->count > 1) ? "..." : "",
356 ret == 0 ? "interrupt" : "polling");
358 hrtimer_init(&ds->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
359 ds->timer.function = gpio_event_input_timer_func;
360 hrtimer_start(&ds->timer, ktime_set(0, 0), HRTIMER_MODE_REL);
361 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
366 spin_lock_irqsave(&ds->irq_lock, irqflags);
367 hrtimer_cancel(&ds->timer);
369 for (i = di->keymap_size - 1; i >= 0; i--) {
370 int irq = gpio_to_irq(di->keymap[i].gpio);
371 if (ds->info->info.no_suspend)
372 disable_irq_wake(irq);
373 free_irq(irq, &ds->key_state[i]);
376 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
378 for (i = di->keymap_size - 1; i >= 0; i--) {
379 err_gpio_configure_failed:
380 gpio_free(di->keymap[i].gpio);
381 err_gpio_request_failed:
385 wakeup_source_unregister(ds->ws);