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/wakelock.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 wake_lock wake_lock;
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);
156 wake_unlock(&ds->wake_lock);
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 wake_lock(&ds->wake_lock);
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;
266 di = container_of(info, struct gpio_event_input_info, info);
268 if (func == GPIO_EVENT_FUNC_SUSPEND) {
270 for (i = 0; i < di->keymap_size; i++)
271 disable_irq(gpio_to_irq(di->keymap[i].gpio));
272 hrtimer_cancel(&ds->timer);
275 if (func == GPIO_EVENT_FUNC_RESUME) {
276 spin_lock_irqsave(&ds->irq_lock, irqflags);
278 for (i = 0; i < di->keymap_size; i++)
279 enable_irq(gpio_to_irq(di->keymap[i].gpio));
280 hrtimer_start(&ds->timer, ktime_set(0, 0), HRTIMER_MODE_REL);
281 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
285 if (func == GPIO_EVENT_FUNC_INIT) {
286 if (ktime_to_ns(di->poll_time) <= 0)
287 di->poll_time = ktime_set(0, 20 * NSEC_PER_MSEC);
289 *data = ds = kzalloc(sizeof(*ds) + sizeof(ds->key_state[0]) *
290 di->keymap_size, GFP_KERNEL);
293 pr_err("gpio_event_input_func: "
294 "Failed to allocate private data\n");
295 goto err_ds_alloc_failed;
297 ds->debounce_count = di->keymap_size;
298 ds->input_devs = input_devs;
300 wake_lock_init(&ds->wake_lock, WAKE_LOCK_SUSPEND, "gpio_input");
301 spin_lock_init(&ds->irq_lock);
303 for (i = 0; i < di->keymap_size; i++) {
304 int dev = di->keymap[i].dev;
305 if (dev >= input_devs->count) {
306 pr_err("gpio_event_input_func: bad device "
307 "index %d >= %d for key code %d\n",
308 dev, input_devs->count,
313 input_set_capability(input_devs->dev[dev], di->type,
315 ds->key_state[i].ds = ds;
316 ds->key_state[i].debounce = DEBOUNCE_UNKNOWN;
319 for (i = 0; i < di->keymap_size; i++) {
320 ret = gpio_request(di->keymap[i].gpio, "gpio_kp_in");
322 pr_err("gpio_event_input_func: gpio_request "
323 "failed for %d\n", di->keymap[i].gpio);
324 goto err_gpio_request_failed;
326 ret = gpio_direction_input(di->keymap[i].gpio);
328 pr_err("gpio_event_input_func: "
329 "gpio_direction_input failed for %d\n",
331 goto err_gpio_configure_failed;
335 ret = gpio_event_input_request_irqs(ds);
337 spin_lock_irqsave(&ds->irq_lock, irqflags);
338 ds->use_irq = ret == 0;
340 pr_info("GPIO Input Driver: Start gpio inputs for %s%s in %s "
341 "mode\n", input_devs->dev[0]->name,
342 (input_devs->count > 1) ? "..." : "",
343 ret == 0 ? "interrupt" : "polling");
345 hrtimer_init(&ds->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
346 ds->timer.function = gpio_event_input_timer_func;
347 hrtimer_start(&ds->timer, ktime_set(0, 0), HRTIMER_MODE_REL);
348 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
353 spin_lock_irqsave(&ds->irq_lock, irqflags);
354 hrtimer_cancel(&ds->timer);
356 for (i = di->keymap_size - 1; i >= 0; i--) {
357 int irq = gpio_to_irq(di->keymap[i].gpio);
358 if (ds->info->info.no_suspend)
359 disable_irq_wake(irq);
360 free_irq(irq, &ds->key_state[i]);
363 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
365 for (i = di->keymap_size - 1; i >= 0; i--) {
366 err_gpio_configure_failed:
367 gpio_free(di->keymap[i].gpio);
368 err_gpio_request_failed:
372 wake_lock_destroy(&ds->wake_lock);