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;
67 key_entry = kp->keys_info->keymap;
68 key_state = kp->key_state;
69 for (i = 0; i < nkeys; i++, key_entry++, key_state++)
70 pr_info("gpio_read_detect_status %d %d\n", key_entry->gpio,
71 gpio_read_detect_status(key_entry->gpio));
73 key_entry = ds->info->keymap;
74 key_state = ds->key_state;
75 spin_lock_irqsave(&ds->irq_lock, irqflags);
76 for (i = 0; i < nkeys; i++, key_entry++, key_state++) {
77 debounce = key_state->debounce;
78 if (debounce & DEBOUNCE_WAIT_IRQ)
80 if (key_state->debounce & DEBOUNCE_UNSTABLE) {
81 debounce = key_state->debounce = DEBOUNCE_UNKNOWN;
82 enable_irq(gpio_to_irq(key_entry->gpio));
83 pr_info("gpio_keys_scan_keys: key %x-%x, %d "
84 "(%d) continue debounce\n",
85 ds->info->type, key_entry->code,
88 npolarity = !(gpio_flags & GPIOEDF_ACTIVE_HIGH);
89 pressed = gpio_get_value(key_entry->gpio) ^ npolarity;
90 if (debounce & DEBOUNCE_POLL) {
91 if (pressed == !(debounce & DEBOUNCE_PRESSED)) {
93 key_state->debounce = DEBOUNCE_UNKNOWN;
94 if (gpio_flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
95 pr_info("gpio_keys_scan_keys: key %x-"
96 "%x, %d (%d) start debounce\n",
97 ds->info->type, key_entry->code,
102 if (pressed && (debounce & DEBOUNCE_NOTPRESSED)) {
103 if (gpio_flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
104 pr_info("gpio_keys_scan_keys: key %x-%x, %d "
105 "(%d) debounce pressed 1\n",
106 ds->info->type, key_entry->code,
108 key_state->debounce = DEBOUNCE_PRESSED;
111 if (!pressed && (debounce & DEBOUNCE_PRESSED)) {
112 if (gpio_flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
113 pr_info("gpio_keys_scan_keys: key %x-%x, %d "
114 "(%d) debounce pressed 0\n",
115 ds->info->type, key_entry->code,
117 key_state->debounce = DEBOUNCE_NOTPRESSED;
121 ds->debounce_count--;
123 key_state->debounce |= DEBOUNCE_WAIT_IRQ;
125 key_state->debounce |= DEBOUNCE_POLL;
126 if (gpio_flags & GPIOEDF_PRINT_KEYS)
127 pr_info("gpio_keys_scan_keys: key %x-%x, %d (%d) "
128 "changed to %d\n", ds->info->type,
129 key_entry->code, i, key_entry->gpio, pressed);
130 input_event(ds->input_devs->dev[key_entry->dev], ds->info->type,
131 key_entry->code, pressed);
135 key_entry = kp->keys_info->keymap;
136 key_state = kp->key_state;
137 for (i = 0; i < nkeys; i++, key_entry++, key_state++) {
138 pr_info("gpio_read_detect_status %d %d\n", key_entry->gpio,
139 gpio_read_detect_status(key_entry->gpio));
143 if (ds->debounce_count)
144 hrtimer_start(timer, ds->info->debounce_time, HRTIMER_MODE_REL);
145 else if (!ds->use_irq)
146 hrtimer_start(timer, ds->info->poll_time, HRTIMER_MODE_REL);
148 wake_unlock(&ds->wake_lock);
150 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
152 return HRTIMER_NORESTART;
155 static irqreturn_t gpio_event_input_irq_handler(int irq, void *dev_id)
157 struct gpio_key_state *ks = dev_id;
158 struct gpio_input_state *ds = ks->ds;
159 int keymap_index = ks - ds->key_state;
160 const struct gpio_event_direct_entry *key_entry;
161 unsigned long irqflags;
167 key_entry = &ds->info->keymap[keymap_index];
169 if (ds->info->debounce_time.tv64) {
170 spin_lock_irqsave(&ds->irq_lock, irqflags);
171 if (ks->debounce & DEBOUNCE_WAIT_IRQ) {
172 ks->debounce = DEBOUNCE_UNKNOWN;
173 if (ds->debounce_count++ == 0) {
174 wake_lock(&ds->wake_lock);
176 &ds->timer, ds->info->debounce_time,
179 if (ds->info->flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
180 pr_info("gpio_event_input_irq_handler: "
181 "key %x-%x, %d (%d) start debounce\n",
182 ds->info->type, key_entry->code,
183 keymap_index, key_entry->gpio);
185 disable_irq_nosync(irq);
186 ks->debounce = DEBOUNCE_UNSTABLE;
188 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
190 pressed = gpio_get_value(key_entry->gpio) ^
191 !(ds->info->flags & GPIOEDF_ACTIVE_HIGH);
192 if (ds->info->flags & GPIOEDF_PRINT_KEYS)
193 pr_info("gpio_event_input_irq_handler: key %x-%x, %d "
194 "(%d) changed to %d\n",
195 ds->info->type, key_entry->code, keymap_index,
196 key_entry->gpio, pressed);
197 input_event(ds->input_devs->dev[key_entry->dev], ds->info->type,
198 key_entry->code, pressed);
203 static int gpio_event_input_request_irqs(struct gpio_input_state *ds)
208 unsigned long req_flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
210 for (i = 0; i < ds->info->keymap_size; i++) {
211 err = irq = gpio_to_irq(ds->info->keymap[i].gpio);
213 goto err_gpio_get_irq_num_failed;
214 err = request_irq(irq, gpio_event_input_irq_handler,
215 req_flags, "gpio_keys", &ds->key_state[i]);
217 pr_err("gpio_event_input_request_irqs: request_irq "
218 "failed for input %d, irq %d\n",
219 ds->info->keymap[i].gpio, irq);
220 goto err_request_irq_failed;
222 enable_irq_wake(irq);
226 for (i = ds->info->keymap_size - 1; i >= 0; i--) {
227 free_irq(gpio_to_irq(ds->info->keymap[i].gpio),
229 err_request_irq_failed:
230 err_gpio_get_irq_num_failed:
236 int gpio_event_input_func(struct gpio_event_input_devs *input_devs,
237 struct gpio_event_info *info, void **data, int func)
241 unsigned long irqflags;
242 struct gpio_event_input_info *di;
243 struct gpio_input_state *ds = *data;
245 di = container_of(info, struct gpio_event_input_info, info);
247 if (func == GPIO_EVENT_FUNC_SUSPEND) {
249 for (i = 0; i < di->keymap_size; i++)
250 disable_irq(gpio_to_irq(di->keymap[i].gpio));
251 hrtimer_cancel(&ds->timer);
254 if (func == GPIO_EVENT_FUNC_RESUME) {
255 spin_lock_irqsave(&ds->irq_lock, irqflags);
257 for (i = 0; i < di->keymap_size; i++)
258 enable_irq(gpio_to_irq(di->keymap[i].gpio));
259 hrtimer_start(&ds->timer, ktime_set(0, 0), HRTIMER_MODE_REL);
260 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
264 if (func == GPIO_EVENT_FUNC_INIT) {
265 if (ktime_to_ns(di->poll_time) <= 0)
266 di->poll_time = ktime_set(0, 20 * NSEC_PER_MSEC);
268 *data = ds = kzalloc(sizeof(*ds) + sizeof(ds->key_state[0]) *
269 di->keymap_size, GFP_KERNEL);
272 pr_err("gpio_event_input_func: "
273 "Failed to allocate private data\n");
274 goto err_ds_alloc_failed;
276 ds->debounce_count = di->keymap_size;
277 ds->input_devs = input_devs;
279 wake_lock_init(&ds->wake_lock, WAKE_LOCK_SUSPEND, "gpio_input");
280 spin_lock_init(&ds->irq_lock);
282 for (i = 0; i < di->keymap_size; i++) {
283 int dev = di->keymap[i].dev;
284 if (dev >= input_devs->count) {
285 pr_err("gpio_event_input_func: bad device "
286 "index %d >= %d for key code %d\n",
287 dev, input_devs->count,
292 input_set_capability(input_devs->dev[dev], di->type,
294 ds->key_state[i].ds = ds;
295 ds->key_state[i].debounce = DEBOUNCE_UNKNOWN;
298 for (i = 0; i < di->keymap_size; i++) {
299 ret = gpio_request(di->keymap[i].gpio, "gpio_kp_in");
301 pr_err("gpio_event_input_func: gpio_request "
302 "failed for %d\n", di->keymap[i].gpio);
303 goto err_gpio_request_failed;
305 ret = gpio_direction_input(di->keymap[i].gpio);
307 pr_err("gpio_event_input_func: "
308 "gpio_direction_input failed for %d\n",
310 goto err_gpio_configure_failed;
314 ret = gpio_event_input_request_irqs(ds);
316 spin_lock_irqsave(&ds->irq_lock, irqflags);
317 ds->use_irq = ret == 0;
319 pr_info("GPIO Input Driver: Start gpio inputs for %s%s in %s "
320 "mode\n", input_devs->dev[0]->name,
321 (input_devs->count > 1) ? "..." : "",
322 ret == 0 ? "interrupt" : "polling");
324 hrtimer_init(&ds->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
325 ds->timer.function = gpio_event_input_timer_func;
326 hrtimer_start(&ds->timer, ktime_set(0, 0), HRTIMER_MODE_REL);
327 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
332 spin_lock_irqsave(&ds->irq_lock, irqflags);
333 hrtimer_cancel(&ds->timer);
335 for (i = di->keymap_size - 1; i >= 0; i--) {
336 free_irq(gpio_to_irq(di->keymap[i].gpio),
340 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
342 for (i = di->keymap_size - 1; i >= 0; i--) {
343 err_gpio_configure_failed:
344 gpio_free(di->keymap[i].gpio);
345 err_gpio_request_failed:
349 wake_lock_destroy(&ds->wake_lock);