1 From 6730377482c05a1742abb92f1463701f5d084a27 Mon Sep 17 00:00:00 2001
2 From: =?utf-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?= <arve@android.com>
3 Date: Wed, 15 Oct 2008 18:23:47 -0700
4 Subject: [PATCH 086/134] Input: Generic GPIO Input device.
6 Content-Type: text/plain; charset=utf-8
7 Content-Transfer-Encoding: 8bit
9 Supports keyboard matrixces, direct inputs, direct outputs and axes connected to gpios.
11 Signed-off-by: Arve Hjønnevåg <arve@android.com>
12 Signed-off-by: Nick Pelly <npelly@google.com>
14 drivers/input/misc/Kconfig | 5 +
15 drivers/input/misc/Makefile | 1 +
16 drivers/input/misc/gpio_axis.c | 180 +++++++++++++++++
17 drivers/input/misc/gpio_event.c | 224 +++++++++++++++++++++
18 drivers/input/misc/gpio_input.c | 343 ++++++++++++++++++++++++++++++++
19 drivers/input/misc/gpio_matrix.c | 406 ++++++++++++++++++++++++++++++++++++++
20 drivers/input/misc/gpio_output.c | 84 ++++++++
21 include/linux/gpio_event.h | 154 ++++++++++++++
22 8 files changed, 1397 insertions(+), 0 deletions(-)
23 create mode 100644 drivers/input/misc/gpio_axis.c
24 create mode 100644 drivers/input/misc/gpio_event.c
25 create mode 100644 drivers/input/misc/gpio_input.c
26 create mode 100644 drivers/input/misc/gpio_matrix.c
27 create mode 100644 drivers/input/misc/gpio_output.c
28 create mode 100644 include/linux/gpio_event.h
30 --- a/drivers/input/misc/Kconfig
31 +++ b/drivers/input/misc/Kconfig
32 @@ -212,6 +212,11 @@ config INPUT_SGI_BTNS
33 To compile this driver as a module, choose M here: the
34 module will be called sgi_btns.
37 + tristate "GPIO driver support"
39 + Say Y here if you want to support gpio based keys, wheels etc...
42 tristate "HP SDC Real Time Clock"
43 depends on (GSC || HP300) && SERIO
44 --- a/drivers/input/misc/Makefile
45 +++ b/drivers/input/misc/Makefile
46 @@ -25,3 +25,5 @@ obj-$(CONFIG_INPUT_UINPUT) += uinput.o
47 obj-$(CONFIG_INPUT_WISTRON_BTNS) += wistron_btns.o
48 obj-$(CONFIG_INPUT_YEALINK) += yealink.o
49 obj-$(CONFIG_INPUT_GPIO_BUTTONS) += gpio_buttons.o
50 +obj-$(CONFIG_INPUT_GPIO) += gpio_event.o gpio_matrix.o gpio_input.o gpio_output.o gpio_axis.o
53 +++ b/drivers/input/misc/gpio_axis.c
55 +/* drivers/input/misc/gpio_axis.c
57 + * Copyright (C) 2007 Google, Inc.
59 + * This software is licensed under the terms of the GNU General Public
60 + * License version 2, as published by the Free Software Foundation, and
61 + * may be copied, distributed, and modified under those terms.
63 + * This program is distributed in the hope that it will be useful,
64 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
65 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
66 + * GNU General Public License for more details.
70 +#include <linux/kernel.h>
71 +#include <linux/gpio.h>
72 +#include <linux/gpio_event.h>
73 +#include <linux/interrupt.h>
75 +struct gpio_axis_state {
76 + struct input_dev *input_dev;
77 + struct gpio_event_axis_info *info;
81 +uint16_t gpio_axis_4bit_gray_map_table[] = {
82 + [0x0] = 0x0, [0x1] = 0x1, /* 0000 0001 */
83 + [0x3] = 0x2, [0x2] = 0x3, /* 0011 0010 */
84 + [0x6] = 0x4, [0x7] = 0x5, /* 0110 0111 */
85 + [0x5] = 0x6, [0x4] = 0x7, /* 0101 0100 */
86 + [0xc] = 0x8, [0xd] = 0x9, /* 1100 1101 */
87 + [0xf] = 0xa, [0xe] = 0xb, /* 1111 1110 */
88 + [0xa] = 0xc, [0xb] = 0xd, /* 1010 1011 */
89 + [0x9] = 0xe, [0x8] = 0xf, /* 1001 1000 */
91 +uint16_t gpio_axis_4bit_gray_map(struct gpio_event_axis_info *info, uint16_t in)
93 + return gpio_axis_4bit_gray_map_table[in];
96 +uint16_t gpio_axis_5bit_singletrack_map_table[] = {
97 + [0x10] = 0x00, [0x14] = 0x01, [0x1c] = 0x02, /* 10000 10100 11100 */
98 + [0x1e] = 0x03, [0x1a] = 0x04, [0x18] = 0x05, /* 11110 11010 11000 */
99 + [0x08] = 0x06, [0x0a] = 0x07, [0x0e] = 0x08, /* 01000 01010 01110 */
100 + [0x0f] = 0x09, [0x0d] = 0x0a, [0x0c] = 0x0b, /* 01111 01101 01100 */
101 + [0x04] = 0x0c, [0x05] = 0x0d, [0x07] = 0x0e, /* 00100 00101 00111 */
102 + [0x17] = 0x0f, [0x16] = 0x10, [0x06] = 0x11, /* 10111 10110 00110 */
103 + [0x02] = 0x12, [0x12] = 0x13, [0x13] = 0x14, /* 00010 10010 10011 */
104 + [0x1b] = 0x15, [0x0b] = 0x16, [0x03] = 0x17, /* 11011 01011 00011 */
105 + [0x01] = 0x18, [0x09] = 0x19, [0x19] = 0x1a, /* 00001 01001 11001 */
106 + [0x1d] = 0x1b, [0x15] = 0x1c, [0x11] = 0x1d, /* 11101 10101 10001 */
108 +uint16_t gpio_axis_5bit_singletrack_map(
109 + struct gpio_event_axis_info *info, uint16_t in)
111 + return gpio_axis_5bit_singletrack_map_table[in];
114 +static void gpio_event_update_axis(struct gpio_axis_state *as, int report)
116 + struct gpio_event_axis_info *ai = as->info;
119 + uint16_t state = 0;
121 + uint16_t old_pos = as->pos;
122 + for (i = ai->count - 1; i >= 0; i--)
123 + state = (state << 1) | gpio_get_value(ai->gpio[i]);
124 + pos = ai->map(ai, state);
125 + if (ai->flags & GPIOEAF_PRINT_RAW)
126 + pr_info("axis %d-%d raw %x, pos %d -> %d\n",
127 + ai->type, ai->code, state, old_pos, pos);
128 + if (report && pos != old_pos) {
129 + if (ai->type == EV_REL) {
130 + change = (ai->decoded_size + pos - old_pos) %
132 + if (change > ai->decoded_size / 2)
133 + change -= ai->decoded_size;
134 + if (change == ai->decoded_size / 2) {
135 + if (ai->flags & GPIOEAF_PRINT_EVENT)
136 + pr_info("axis %d-%d unknown direction, "
137 + "pos %d -> %d\n", ai->type,
138 + ai->code, old_pos, pos);
139 + change = 0; /* no closest direction */
141 + if (ai->flags & GPIOEAF_PRINT_EVENT)
142 + pr_info("axis %d-%d change %d\n",
143 + ai->type, ai->code, change);
144 + input_report_rel(as->input_dev, ai->code, change);
146 + if (ai->flags & GPIOEAF_PRINT_EVENT)
147 + pr_info("axis %d-%d now %d\n",
148 + ai->type, ai->code, pos);
149 + input_event(as->input_dev, ai->type, ai->code, pos);
151 + input_sync(as->input_dev);
156 +static irqreturn_t gpio_axis_irq_handler(int irq, void *dev_id)
158 + struct gpio_axis_state *as = dev_id;
159 + gpio_event_update_axis(as, 1);
160 + return IRQ_HANDLED;
163 +int gpio_event_axis_func(struct input_dev *input_dev,
164 + struct gpio_event_info *info, void **data, int func)
169 + struct gpio_event_axis_info *ai;
170 + struct gpio_axis_state *as;
172 + ai = container_of(info, struct gpio_event_axis_info, info);
173 + if (func == GPIO_EVENT_FUNC_SUSPEND) {
174 + for (i = 0; i < ai->count; i++)
175 + disable_irq(gpio_to_irq(ai->gpio[i]));
178 + if (func == GPIO_EVENT_FUNC_RESUME) {
179 + for (i = 0; i < ai->count; i++)
180 + enable_irq(gpio_to_irq(ai->gpio[i]));
184 + if (func == GPIO_EVENT_FUNC_INIT) {
185 + *data = as = kmalloc(sizeof(*as), GFP_KERNEL);
188 + goto err_alloc_axis_state_failed;
190 + as->input_dev = input_dev;
193 + input_set_capability(input_dev, ai->type, ai->code);
194 + if (ai->type == EV_ABS) {
195 + input_set_abs_params(input_dev, ai->code, 0,
196 + ai->decoded_size - 1, 0, 0);
198 + for (i = 0; i < ai->count; i++) {
199 + ret = gpio_request(ai->gpio[i], "gpio_event_axis");
201 + goto err_request_gpio_failed;
202 + ret = gpio_direction_input(ai->gpio[i]);
204 + goto err_gpio_direction_input_failed;
205 + ret = irq = gpio_to_irq(ai->gpio[i]);
207 + goto err_get_irq_num_failed;
208 + ret = request_irq(irq, gpio_axis_irq_handler,
209 + IRQF_TRIGGER_RISING |
210 + IRQF_TRIGGER_FALLING,
211 + "gpio_event_axis", as);
213 + goto err_request_irq_failed;
215 + gpio_event_update_axis(as, 0);
221 + for (i = ai->count - 1; i >= 0; i--) {
222 + free_irq(gpio_to_irq(ai->gpio[i]), as);
223 +err_request_irq_failed:
224 +err_get_irq_num_failed:
225 +err_gpio_direction_input_failed:
226 + gpio_free(ai->gpio[i]);
227 +err_request_gpio_failed:
232 +err_alloc_axis_state_failed:
236 +++ b/drivers/input/misc/gpio_event.c
238 +/* drivers/input/misc/gpio_event.c
240 + * Copyright (C) 2007 Google, Inc.
242 + * This software is licensed under the terms of the GNU General Public
243 + * License version 2, as published by the Free Software Foundation, and
244 + * may be copied, distributed, and modified under those terms.
246 + * This program is distributed in the hope that it will be useful,
247 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
248 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
249 + * GNU General Public License for more details.
253 +#include <linux/earlysuspend.h>
254 +#include <linux/module.h>
255 +#include <linux/input.h>
256 +#include <linux/gpio_event.h>
257 +#include <linux/hrtimer.h>
258 +#include <linux/platform_device.h>
261 + struct input_dev *input_dev;
262 + const struct gpio_event_platform_data *info;
263 + struct early_suspend early_suspend;
267 +static int gpio_input_event(
268 + struct input_dev *dev, unsigned int type, unsigned int code, int value)
273 + struct gpio_event_info **ii;
274 + struct gpio_event *ip = input_get_drvdata(dev);
276 + for (i = 0, ii = ip->info->info; i < ip->info->info_count; i++, ii++) {
277 + if ((*ii)->event) {
278 + tmp_ret = (*ii)->event(ip->input_dev, *ii,
279 + &ip->state[i], type, code, value);
287 +static int gpio_event_call_all_func(struct gpio_event *ip, int func)
291 + struct gpio_event_info **ii;
293 + if (func == GPIO_EVENT_FUNC_INIT || func == GPIO_EVENT_FUNC_RESUME) {
294 + ii = ip->info->info;
295 + for (i = 0; i < ip->info->info_count; i++, ii++) {
296 + if ((*ii)->func == NULL) {
298 + pr_err("gpio_event_probe: Incomplete pdata, "
302 + ret = (*ii)->func(ip->input_dev, *ii, &ip->state[i],
305 + pr_err("gpio_event_probe: function failed\n");
306 + goto err_func_failed;
313 + i = ip->info->info_count;
314 + ii = ip->info->info + i;
318 + (*ii)->func(ip->input_dev, *ii, &ip->state[i], func & ~1);
326 +#ifdef CONFIG_HAS_EARLYSUSPEND
327 +void gpio_event_suspend(struct early_suspend *h)
329 + struct gpio_event *ip;
330 + ip = container_of(h, struct gpio_event, early_suspend);
331 + gpio_event_call_all_func(ip, GPIO_EVENT_FUNC_SUSPEND);
332 + ip->info->power(ip->info, 0);
335 +void gpio_event_resume(struct early_suspend *h)
337 + struct gpio_event *ip;
338 + ip = container_of(h, struct gpio_event, early_suspend);
339 + ip->info->power(ip->info, 1);
340 + gpio_event_call_all_func(ip, GPIO_EVENT_FUNC_RESUME);
344 +static int __init gpio_event_probe(struct platform_device *pdev)
347 + struct gpio_event *ip;
348 + struct input_dev *input_dev;
349 + struct gpio_event_platform_data *event_info;
351 + event_info = pdev->dev.platform_data;
352 + if (event_info == NULL) {
353 + pr_err("gpio_event_probe: No pdata\n");
356 + if (event_info->name == NULL ||
357 + event_info->info == NULL ||
358 + event_info->info_count == 0) {
359 + pr_err("gpio_event_probe: Incomplete pdata\n");
362 + ip = kzalloc(sizeof(*ip) +
363 + sizeof(ip->state[0]) * event_info->info_count, GFP_KERNEL);
366 + pr_err("gpio_event_probe: Failed to allocate private data\n");
367 + goto err_kp_alloc_failed;
369 + platform_set_drvdata(pdev, ip);
371 + input_dev = input_allocate_device();
372 + if (input_dev == NULL) {
374 + pr_err("gpio_event_probe: Failed to allocate input device\n");
375 + goto err_input_dev_alloc_failed;
377 + input_set_drvdata(input_dev, ip);
378 + ip->input_dev = input_dev;
379 + ip->info = event_info;
380 + if (event_info->power) {
381 +#ifdef CONFIG_HAS_EARLYSUSPEND
382 + ip->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
383 + ip->early_suspend.suspend = gpio_event_suspend;
384 + ip->early_suspend.resume = gpio_event_resume;
385 + register_early_suspend(&ip->early_suspend);
387 + ip->info->power(ip->info, 1);
390 + input_dev->name = ip->info->name;
391 + input_dev->event = gpio_input_event;
393 + err = gpio_event_call_all_func(ip, GPIO_EVENT_FUNC_INIT);
395 + goto err_call_all_func_failed;
397 + err = input_register_device(input_dev);
399 + pr_err("gpio_event_probe: Unable to register %s input device\n",
401 + goto err_input_register_device_failed;
406 +err_input_register_device_failed:
407 + gpio_event_call_all_func(ip, GPIO_EVENT_FUNC_UNINIT);
408 +err_call_all_func_failed:
409 + if (event_info->power) {
410 +#ifdef CONFIG_HAS_EARLYSUSPEND
411 + unregister_early_suspend(&ip->early_suspend);
413 + ip->info->power(ip->info, 0);
415 + input_free_device(input_dev);
416 +err_input_dev_alloc_failed:
418 +err_kp_alloc_failed:
422 +static int gpio_event_remove(struct platform_device *pdev)
424 + struct gpio_event *ip = platform_get_drvdata(pdev);
426 + gpio_event_call_all_func(ip, GPIO_EVENT_FUNC_UNINIT);
427 + if (ip->info->power) {
428 +#ifdef CONFIG_HAS_EARLYSUSPEND
429 + unregister_early_suspend(&ip->early_suspend);
431 + ip->info->power(ip->info, 0);
433 + input_unregister_device(ip->input_dev);
438 +static struct platform_driver gpio_event_driver = {
439 + .probe = gpio_event_probe,
440 + .remove = gpio_event_remove,
442 + .name = GPIO_EVENT_DEV_NAME,
446 +static int __devinit gpio_event_init(void)
448 + return platform_driver_register(&gpio_event_driver);
451 +static void __exit gpio_event_exit(void)
453 + platform_driver_unregister(&gpio_event_driver);
456 +module_init(gpio_event_init);
457 +module_exit(gpio_event_exit);
459 +MODULE_DESCRIPTION("GPIO Event Driver");
460 +MODULE_LICENSE("GPL");
463 +++ b/drivers/input/misc/gpio_input.c
465 +/* drivers/input/misc/gpio_input.c
467 + * Copyright (C) 2007 Google, Inc.
469 + * This software is licensed under the terms of the GNU General Public
470 + * License version 2, as published by the Free Software Foundation, and
471 + * may be copied, distributed, and modified under those terms.
473 + * This program is distributed in the hope that it will be useful,
474 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
475 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
476 + * GNU General Public License for more details.
480 +#include <linux/kernel.h>
481 +#include <linux/gpio.h>
482 +#include <linux/gpio_event.h>
483 +#include <linux/hrtimer.h>
484 +#include <linux/input.h>
485 +#include <linux/interrupt.h>
486 +#include <linux/wakelock.h>
489 + DEBOUNCE_UNSTABLE = BIT(0), /* Got irq, while debouncing */
490 + DEBOUNCE_PRESSED = BIT(1),
491 + DEBOUNCE_NOTPRESSED = BIT(2),
492 + DEBOUNCE_WAIT_IRQ = BIT(3), /* Stable irq state */
493 + DEBOUNCE_POLL = BIT(4), /* Stable polling state */
496 + DEBOUNCE_PRESSED | DEBOUNCE_NOTPRESSED,
499 +struct gpio_key_state {
500 + struct gpio_input_state *ds;
504 +struct gpio_input_state {
505 + struct input_dev *input_dev;
506 + const struct gpio_event_input_info *info;
507 + struct hrtimer timer;
509 + int debounce_count;
510 + spinlock_t irq_lock;
511 + struct wake_lock wake_lock;
512 + struct gpio_key_state key_state[0];
515 +static enum hrtimer_restart gpio_event_input_timer_func(struct hrtimer *timer)
519 + struct gpio_input_state *ds =
520 + container_of(timer, struct gpio_input_state, timer);
521 + unsigned gpio_flags = ds->info->flags;
522 + unsigned npolarity;
523 + int nkeys = ds->info->keymap_size;
524 + const struct gpio_event_direct_entry *key_entry;
525 + struct gpio_key_state *key_state;
526 + unsigned long irqflags;
530 + key_entry = kp->keys_info->keymap;
531 + key_state = kp->key_state;
532 + for (i = 0; i < nkeys; i++, key_entry++, key_state++)
533 + pr_info("gpio_read_detect_status %d %d\n", key_entry->gpio,
534 + gpio_read_detect_status(key_entry->gpio));
536 + key_entry = ds->info->keymap;
537 + key_state = ds->key_state;
538 + spin_lock_irqsave(&ds->irq_lock, irqflags);
539 + for (i = 0; i < nkeys; i++, key_entry++, key_state++) {
540 + debounce = key_state->debounce;
541 + if (debounce & DEBOUNCE_WAIT_IRQ)
543 + if (key_state->debounce & DEBOUNCE_UNSTABLE) {
544 + debounce = key_state->debounce = DEBOUNCE_UNKNOWN;
545 + enable_irq(gpio_to_irq(key_entry->gpio));
546 + pr_info("gpio_keys_scan_keys: key %x-%x, %d "
547 + "(%d) continue debounce\n",
548 + ds->info->type, key_entry->code,
549 + i, key_entry->gpio);
551 + npolarity = !(gpio_flags & GPIOEDF_ACTIVE_HIGH);
552 + pressed = gpio_get_value(key_entry->gpio) ^ npolarity;
553 + if (debounce & DEBOUNCE_POLL) {
554 + if (pressed == !(debounce & DEBOUNCE_PRESSED)) {
555 + ds->debounce_count++;
556 + key_state->debounce = DEBOUNCE_UNKNOWN;
557 + if (gpio_flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
558 + pr_info("gpio_keys_scan_keys: key %x-"
559 + "%x, %d (%d) start debounce\n",
560 + ds->info->type, key_entry->code,
561 + i, key_entry->gpio);
565 + if (pressed && (debounce & DEBOUNCE_NOTPRESSED)) {
566 + if (gpio_flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
567 + pr_info("gpio_keys_scan_keys: key %x-%x, %d "
568 + "(%d) debounce pressed 1\n",
569 + ds->info->type, key_entry->code,
570 + i, key_entry->gpio);
571 + key_state->debounce = DEBOUNCE_PRESSED;
574 + if (!pressed && (debounce & DEBOUNCE_PRESSED)) {
575 + if (gpio_flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
576 + pr_info("gpio_keys_scan_keys: key %x-%x, %d "
577 + "(%d) debounce pressed 0\n",
578 + ds->info->type, key_entry->code,
579 + i, key_entry->gpio);
580 + key_state->debounce = DEBOUNCE_NOTPRESSED;
583 + /* key is stable */
584 + ds->debounce_count--;
586 + key_state->debounce |= DEBOUNCE_WAIT_IRQ;
588 + key_state->debounce |= DEBOUNCE_POLL;
589 + if (gpio_flags & GPIOEDF_PRINT_KEYS)
590 + pr_info("gpio_keys_scan_keys: key %x-%x, %d (%d) "
591 + "changed to %d\n", ds->info->type,
592 + key_entry->code, i, key_entry->gpio, pressed);
593 + input_event(ds->input_dev, ds->info->type,
594 + key_entry->code, pressed);
598 + key_entry = kp->keys_info->keymap;
599 + key_state = kp->key_state;
600 + for (i = 0; i < nkeys; i++, key_entry++, key_state++) {
601 + pr_info("gpio_read_detect_status %d %d\n", key_entry->gpio,
602 + gpio_read_detect_status(key_entry->gpio));
606 + if (ds->debounce_count)
607 + hrtimer_start(timer, ds->info->debounce_time, HRTIMER_MODE_REL);
608 + else if (!ds->use_irq)
609 + hrtimer_start(timer, ds->info->poll_time, HRTIMER_MODE_REL);
611 + wake_unlock(&ds->wake_lock);
613 + spin_unlock_irqrestore(&ds->irq_lock, irqflags);
615 + return HRTIMER_NORESTART;
618 +static irqreturn_t gpio_event_input_irq_handler(int irq, void *dev_id)
620 + struct gpio_key_state *ks = dev_id;
621 + struct gpio_input_state *ds = ks->ds;
622 + int keymap_index = ks - ds->key_state;
623 + const struct gpio_event_direct_entry *key_entry;
624 + unsigned long irqflags;
628 + return IRQ_HANDLED;
630 + key_entry = &ds->info->keymap[keymap_index];
632 + if (ds->info->debounce_time.tv64) {
633 + spin_lock_irqsave(&ds->irq_lock, irqflags);
634 + if (ks->debounce & DEBOUNCE_WAIT_IRQ) {
635 + ks->debounce = DEBOUNCE_UNKNOWN;
636 + if (ds->debounce_count++ == 0) {
637 + wake_lock(&ds->wake_lock);
639 + &ds->timer, ds->info->debounce_time,
642 + if (ds->info->flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
643 + pr_info("gpio_event_input_irq_handler: "
644 + "key %x-%x, %d (%d) start debounce\n",
645 + ds->info->type, key_entry->code,
646 + keymap_index, key_entry->gpio);
649 + ks->debounce = DEBOUNCE_UNSTABLE;
651 + spin_unlock_irqrestore(&ds->irq_lock, irqflags);
653 + pressed = gpio_get_value(key_entry->gpio) ^
654 + !(ds->info->flags & GPIOEDF_ACTIVE_HIGH);
655 + if (ds->info->flags & GPIOEDF_PRINT_KEYS)
656 + pr_info("gpio_event_input_irq_handler: key %x-%x, %d "
657 + "(%d) changed to %d\n",
658 + ds->info->type, key_entry->code, keymap_index,
659 + key_entry->gpio, pressed);
660 + input_event(ds->input_dev, ds->info->type,
661 + key_entry->code, pressed);
663 + return IRQ_HANDLED;
666 +static int gpio_event_input_request_irqs(struct gpio_input_state *ds)
671 + unsigned long req_flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
673 + for (i = 0; i < ds->info->keymap_size; i++) {
674 + err = irq = gpio_to_irq(ds->info->keymap[i].gpio);
676 + goto err_gpio_get_irq_num_failed;
677 + err = request_irq(irq, gpio_event_input_irq_handler,
678 + req_flags, "gpio_keys", &ds->key_state[i]);
680 + pr_err("gpio_event_input_request_irqs: request_irq "
681 + "failed for input %d, irq %d\n",
682 + ds->info->keymap[i].gpio, irq);
683 + goto err_request_irq_failed;
685 + enable_irq_wake(irq);
689 + for (i = ds->info->keymap_size - 1; i >= 0; i--) {
690 + free_irq(gpio_to_irq(ds->info->keymap[i].gpio),
691 + &ds->key_state[i]);
692 +err_request_irq_failed:
693 +err_gpio_get_irq_num_failed:
699 +int gpio_event_input_func(struct input_dev *input_dev,
700 + struct gpio_event_info *info, void **data, int func)
704 + unsigned long irqflags;
705 + struct gpio_event_input_info *di;
706 + struct gpio_input_state *ds = *data;
708 + di = container_of(info, struct gpio_event_input_info, info);
710 + if (func == GPIO_EVENT_FUNC_SUSPEND) {
711 + spin_lock_irqsave(&ds->irq_lock, irqflags);
713 + for (i = 0; i < di->keymap_size; i++)
714 + disable_irq(gpio_to_irq(di->keymap[i].gpio));
715 + spin_unlock_irqrestore(&ds->irq_lock, irqflags);
716 + hrtimer_cancel(&ds->timer);
719 + if (func == GPIO_EVENT_FUNC_RESUME) {
720 + spin_lock_irqsave(&ds->irq_lock, irqflags);
722 + for (i = 0; i < di->keymap_size; i++)
723 + enable_irq(gpio_to_irq(di->keymap[i].gpio));
724 + hrtimer_start(&ds->timer, ktime_set(0, 0), HRTIMER_MODE_REL);
725 + spin_unlock_irqrestore(&ds->irq_lock, irqflags);
729 + if (func == GPIO_EVENT_FUNC_INIT) {
730 + if (ktime_to_ns(di->poll_time) <= 0)
731 + di->poll_time = ktime_set(0, 20 * NSEC_PER_MSEC);
733 + *data = ds = kzalloc(sizeof(*ds) + sizeof(ds->key_state[0]) *
734 + di->keymap_size, GFP_KERNEL);
737 + pr_err("gpio_event_input_func: "
738 + "Failed to allocate private data\n");
739 + goto err_ds_alloc_failed;
741 + ds->debounce_count = di->keymap_size;
742 + ds->input_dev = input_dev;
744 + wake_lock_init(&ds->wake_lock, WAKE_LOCK_SUSPEND, "gpio_input");
745 + spin_lock_init(&ds->irq_lock);
747 + for (i = 0; i < di->keymap_size; i++) {
748 + input_set_capability(input_dev, di->type,
749 + di->keymap[i].code);
750 + ds->key_state[i].ds = ds;
751 + ds->key_state[i].debounce = DEBOUNCE_UNKNOWN;
754 + for (i = 0; i < di->keymap_size; i++) {
755 + ret = gpio_request(di->keymap[i].gpio, "gpio_kp_in");
757 + pr_err("gpio_event_input_func: gpio_request "
758 + "failed for %d\n", di->keymap[i].gpio);
759 + goto err_gpio_request_failed;
761 + ret = gpio_direction_input(di->keymap[i].gpio);
763 + pr_err("gpio_event_input_func: "
764 + "gpio_direction_input failed for %d\n",
765 + di->keymap[i].gpio);
766 + goto err_gpio_configure_failed;
770 + ret = gpio_event_input_request_irqs(ds);
772 + spin_lock_irqsave(&ds->irq_lock, irqflags);
773 + ds->use_irq = ret == 0;
775 + pr_info("GPIO Input Driver: Start gpio inputs for %s in %s "
777 + input_dev->name, ret == 0 ? "interrupt" : "polling");
779 + hrtimer_init(&ds->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
780 + ds->timer.function = gpio_event_input_timer_func;
781 + hrtimer_start(&ds->timer, ktime_set(0, 0), HRTIMER_MODE_REL);
782 + spin_unlock_irqrestore(&ds->irq_lock, irqflags);
787 + spin_lock_irqsave(&ds->irq_lock, irqflags);
788 + hrtimer_cancel(&ds->timer);
790 + for (i = di->keymap_size - 1; i >= 0; i--) {
791 + free_irq(gpio_to_irq(di->keymap[i].gpio),
792 + &ds->key_state[i]);
795 + spin_unlock_irqrestore(&ds->irq_lock, irqflags);
797 + for (i = di->keymap_size - 1; i >= 0; i--) {
798 +err_gpio_configure_failed:
799 + gpio_free(di->keymap[i].gpio);
800 +err_gpio_request_failed:
803 + wake_lock_destroy(&ds->wake_lock);
805 +err_ds_alloc_failed:
809 +++ b/drivers/input/misc/gpio_matrix.c
811 +/* drivers/input/misc/gpio_matrix.c
813 + * Copyright (C) 2007 Google, Inc.
815 + * This software is licensed under the terms of the GNU General Public
816 + * License version 2, as published by the Free Software Foundation, and
817 + * may be copied, distributed, and modified under those terms.
819 + * This program is distributed in the hope that it will be useful,
820 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
821 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
822 + * GNU General Public License for more details.
826 +#include <linux/kernel.h>
827 +#include <linux/gpio.h>
828 +#include <linux/gpio_event.h>
829 +#include <linux/hrtimer.h>
830 +#include <linux/interrupt.h>
831 +#include <linux/wakelock.h>
834 + struct input_dev *input_dev;
835 + struct gpio_event_matrix_info *keypad_info;
836 + struct hrtimer timer;
837 + struct wake_lock wake_lock;
838 + int current_output;
839 + unsigned int use_irq:1;
840 + unsigned int key_state_changed:1;
841 + unsigned int last_key_state_changed:1;
842 + unsigned int some_keys_pressed:2;
843 + unsigned long keys_pressed[0];
846 +static void clear_phantom_key(struct gpio_kp *kp, int out, int in)
848 + struct gpio_event_matrix_info *mi = kp->keypad_info;
849 + int key_index = out * mi->ninputs + in;
850 + unsigned short keycode = mi->keymap[key_index];;
852 + if (!test_bit(keycode, kp->input_dev->key)) {
853 + if (mi->flags & GPIOKPF_PRINT_PHANTOM_KEYS)
854 + pr_info("gpiomatrix: phantom key %x, %d-%d (%d-%d) "
855 + "cleared\n", keycode, out, in,
856 + mi->output_gpios[out], mi->input_gpios[in]);
857 + __clear_bit(key_index, kp->keys_pressed);
859 + if (mi->flags & GPIOKPF_PRINT_PHANTOM_KEYS)
860 + pr_info("gpiomatrix: phantom key %x, %d-%d (%d-%d) "
861 + "not cleared\n", keycode, out, in,
862 + mi->output_gpios[out], mi->input_gpios[in]);
866 +static int restore_keys_for_input(struct gpio_kp *kp, int out, int in)
871 + key_index = out * kp->keypad_info->ninputs + in;
872 + while (out < kp->keypad_info->noutputs) {
873 + if (test_bit(key_index, kp->keys_pressed)) {
875 + clear_phantom_key(kp, out, in);
877 + key_index += kp->keypad_info->ninputs;
883 +static void remove_phantom_keys(struct gpio_kp *kp)
888 + if (kp->some_keys_pressed < 3)
891 + for (out = 0; out < kp->keypad_info->noutputs; out++) {
893 + key_index = out * kp->keypad_info->ninputs;
894 + for (in = 0; in < kp->keypad_info->ninputs; in++, key_index++) {
895 + if (test_bit(key_index, kp->keys_pressed)) {
901 + if (!restore_keys_for_input(kp, out + 1,
904 + clear_phantom_key(kp, out, inp);
907 + restore_keys_for_input(kp, out, in);
913 +static void report_key(struct gpio_kp *kp, int key_index, int out, int in)
915 + struct gpio_event_matrix_info *mi = kp->keypad_info;
916 + int pressed = test_bit(key_index, kp->keys_pressed);
917 + unsigned short keycode = mi->keymap[key_index];
918 + if (pressed != test_bit(keycode, kp->input_dev->key)) {
919 + if (keycode == KEY_RESERVED) {
920 + if (mi->flags & GPIOKPF_PRINT_UNMAPPED_KEYS)
921 + pr_info("gpiomatrix: unmapped key, %d-%d "
922 + "(%d-%d) changed to %d\n",
923 + out, in, mi->output_gpios[out],
924 + mi->input_gpios[in], pressed);
926 + if (mi->flags & GPIOKPF_PRINT_MAPPED_KEYS)
927 + pr_info("gpiomatrix: key %x, %d-%d (%d-%d) "
928 + "changed to %d\n", keycode,
929 + out, in, mi->output_gpios[out],
930 + mi->input_gpios[in], pressed);
931 + input_report_key(kp->input_dev, keycode, pressed);
936 +static enum hrtimer_restart gpio_keypad_timer_func(struct hrtimer *timer)
941 + struct gpio_kp *kp = container_of(timer, struct gpio_kp, timer);
942 + struct gpio_event_matrix_info *mi = kp->keypad_info;
943 + unsigned gpio_keypad_flags = mi->flags;
944 + unsigned polarity = !!(gpio_keypad_flags & GPIOKPF_ACTIVE_HIGH);
946 + out = kp->current_output;
947 + if (out == mi->noutputs) {
949 + kp->last_key_state_changed = kp->key_state_changed;
950 + kp->key_state_changed = 0;
951 + kp->some_keys_pressed = 0;
953 + key_index = out * mi->ninputs;
954 + for (in = 0; in < mi->ninputs; in++, key_index++) {
955 + gpio = mi->input_gpios[in];
956 + if (gpio_get_value(gpio) ^ !polarity) {
957 + if (kp->some_keys_pressed < 3)
958 + kp->some_keys_pressed++;
959 + kp->key_state_changed |= !__test_and_set_bit(
960 + key_index, kp->keys_pressed);
962 + kp->key_state_changed |= __test_and_clear_bit(
963 + key_index, kp->keys_pressed);
965 + gpio = mi->output_gpios[out];
966 + if (gpio_keypad_flags & GPIOKPF_DRIVE_INACTIVE)
967 + gpio_set_value(gpio, !polarity);
969 + gpio_direction_input(gpio);
972 + kp->current_output = out;
973 + if (out < mi->noutputs) {
974 + gpio = mi->output_gpios[out];
975 + if (gpio_keypad_flags & GPIOKPF_DRIVE_INACTIVE)
976 + gpio_set_value(gpio, polarity);
978 + gpio_direction_output(gpio, polarity);
979 + hrtimer_start(timer, mi->settle_time, HRTIMER_MODE_REL);
980 + return HRTIMER_NORESTART;
982 + if (gpio_keypad_flags & GPIOKPF_DEBOUNCE) {
983 + if (kp->key_state_changed) {
984 + hrtimer_start(&kp->timer, mi->debounce_delay,
986 + return HRTIMER_NORESTART;
988 + kp->key_state_changed = kp->last_key_state_changed;
990 + if (kp->key_state_changed) {
991 + if (gpio_keypad_flags & GPIOKPF_REMOVE_SOME_PHANTOM_KEYS)
992 + remove_phantom_keys(kp);
994 + for (out = 0; out < mi->noutputs; out++)
995 + for (in = 0; in < mi->ninputs; in++, key_index++)
996 + report_key(kp, key_index, out, in);
998 + if (!kp->use_irq || kp->some_keys_pressed) {
999 + hrtimer_start(timer, mi->poll_time, HRTIMER_MODE_REL);
1000 + return HRTIMER_NORESTART;
1003 + /* No keys are pressed, reenable interrupt */
1004 + for (out = 0; out < mi->noutputs; out++) {
1005 + if (gpio_keypad_flags & GPIOKPF_DRIVE_INACTIVE)
1006 + gpio_set_value(mi->output_gpios[out], polarity);
1008 + gpio_direction_output(mi->output_gpios[out], polarity);
1010 + for (in = 0; in < mi->ninputs; in++)
1011 + enable_irq(gpio_to_irq(mi->input_gpios[in]));
1012 + wake_unlock(&kp->wake_lock);
1013 + return HRTIMER_NORESTART;
1016 +static irqreturn_t gpio_keypad_irq_handler(int irq_in, void *dev_id)
1019 + struct gpio_kp *kp = dev_id;
1020 + struct gpio_event_matrix_info *mi = kp->keypad_info;
1021 + unsigned gpio_keypad_flags = mi->flags;
1023 + if (!kp->use_irq) /* ignore interrupt while registering the handler */
1024 + return IRQ_HANDLED;
1026 + for (i = 0; i < mi->ninputs; i++)
1027 + disable_irq(gpio_to_irq(mi->input_gpios[i]));
1028 + for (i = 0; i < mi->noutputs; i++) {
1029 + if (gpio_keypad_flags & GPIOKPF_DRIVE_INACTIVE)
1030 + gpio_set_value(mi->output_gpios[i],
1031 + !(gpio_keypad_flags & GPIOKPF_ACTIVE_HIGH));
1033 + gpio_direction_input(mi->output_gpios[i]);
1035 + wake_lock(&kp->wake_lock);
1036 + hrtimer_start(&kp->timer, ktime_set(0, 0), HRTIMER_MODE_REL);
1037 + return IRQ_HANDLED;
1040 +static int gpio_keypad_request_irqs(struct gpio_kp *kp)
1045 + unsigned long request_flags;
1046 + struct gpio_event_matrix_info *mi = kp->keypad_info;
1048 + switch (mi->flags & (GPIOKPF_ACTIVE_HIGH|GPIOKPF_LEVEL_TRIGGERED_IRQ)) {
1050 + request_flags = IRQF_TRIGGER_FALLING;
1052 + case GPIOKPF_ACTIVE_HIGH:
1053 + request_flags = IRQF_TRIGGER_RISING;
1055 + case GPIOKPF_LEVEL_TRIGGERED_IRQ:
1056 + request_flags = IRQF_TRIGGER_LOW;
1058 + case GPIOKPF_LEVEL_TRIGGERED_IRQ | GPIOKPF_ACTIVE_HIGH:
1059 + request_flags = IRQF_TRIGGER_HIGH;
1063 + for (i = 0; i < mi->ninputs; i++) {
1064 + err = irq = gpio_to_irq(mi->input_gpios[i]);
1066 + goto err_gpio_get_irq_num_failed;
1067 + err = request_irq(irq, gpio_keypad_irq_handler, request_flags,
1070 + pr_err("gpiomatrix: request_irq failed for input %d, "
1071 + "irq %d\n", mi->input_gpios[i], irq);
1072 + goto err_request_irq_failed;
1074 + err = set_irq_wake(irq, 1);
1076 + pr_err("gpiomatrix: set_irq_wake failed for input %d, "
1077 + "irq %d\n", mi->input_gpios[i], irq);
1083 + for (i = mi->noutputs - 1; i >= 0; i--) {
1084 + free_irq(gpio_to_irq(mi->input_gpios[i]), kp);
1085 +err_request_irq_failed:
1086 +err_gpio_get_irq_num_failed:
1092 +int gpio_event_matrix_func(struct input_dev *input_dev,
1093 + struct gpio_event_info *info, void **data, int func)
1098 + struct gpio_kp *kp;
1099 + struct gpio_event_matrix_info *mi;
1101 + mi = container_of(info, struct gpio_event_matrix_info, info);
1102 + if (func == GPIO_EVENT_FUNC_SUSPEND || func == GPIO_EVENT_FUNC_RESUME) {
1103 + /* TODO: disable scanning */
1107 + if (func == GPIO_EVENT_FUNC_INIT) {
1108 + if (mi->keymap == NULL ||
1109 + mi->input_gpios == NULL ||
1110 + mi->output_gpios == NULL) {
1112 + pr_err("gpiomatrix: Incomplete pdata\n");
1113 + goto err_invalid_platform_data;
1115 + key_count = mi->ninputs * mi->noutputs;
1117 + *data = kp = kzalloc(sizeof(*kp) + sizeof(kp->keys_pressed[0]) *
1118 + BITS_TO_LONGS(key_count), GFP_KERNEL);
1121 + pr_err("gpiomatrix: Failed to allocate private data\n");
1122 + goto err_kp_alloc_failed;
1124 + kp->input_dev = input_dev;
1125 + kp->keypad_info = mi;
1126 + set_bit(EV_KEY, input_dev->evbit);
1127 + for (i = 0; i < key_count; i++) {
1128 + if (mi->keymap[i])
1129 + set_bit(mi->keymap[i] & KEY_MAX,
1130 + input_dev->keybit);
1133 + for (i = 0; i < mi->noutputs; i++) {
1134 + if (gpio_cansleep(mi->output_gpios[i])) {
1135 + pr_err("gpiomatrix: unsupported output gpio %d,"
1136 + " can sleep\n", mi->output_gpios[i]);
1138 + goto err_request_output_gpio_failed;
1140 + err = gpio_request(mi->output_gpios[i], "gpio_kp_out");
1142 + pr_err("gpiomatrix: gpio_request failed for "
1143 + "output %d\n", mi->output_gpios[i]);
1144 + goto err_request_output_gpio_failed;
1146 + if (mi->flags & GPIOKPF_DRIVE_INACTIVE)
1147 + err = gpio_direction_output(mi->output_gpios[i],
1148 + !(mi->flags & GPIOKPF_ACTIVE_HIGH));
1150 + err = gpio_direction_input(mi->output_gpios[i]);
1152 + pr_err("gpiomatrix: gpio_configure failed for "
1153 + "output %d\n", mi->output_gpios[i]);
1154 + goto err_output_gpio_configure_failed;
1157 + for (i = 0; i < mi->ninputs; i++) {
1158 + err = gpio_request(mi->input_gpios[i], "gpio_kp_in");
1160 + pr_err("gpiomatrix: gpio_request failed for "
1161 + "input %d\n", mi->input_gpios[i]);
1162 + goto err_request_input_gpio_failed;
1164 + err = gpio_direction_input(mi->input_gpios[i]);
1166 + pr_err("gpiomatrix: gpio_direction_input failed"
1167 + " for input %d\n", mi->input_gpios[i]);
1168 + goto err_gpio_direction_input_failed;
1171 + kp->current_output = mi->noutputs;
1172 + kp->key_state_changed = 1;
1174 + hrtimer_init(&kp->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1175 + kp->timer.function = gpio_keypad_timer_func;
1176 + wake_lock_init(&kp->wake_lock, WAKE_LOCK_SUSPEND, "gpio_kp");
1177 + err = gpio_keypad_request_irqs(kp);
1178 + kp->use_irq = err == 0;
1180 + pr_info("GPIO Matrix Keypad Driver: Start keypad matrix for %s "
1181 + "in %s mode\n", input_dev->name,
1182 + kp->use_irq ? "interrupt" : "polling");
1185 + wake_lock(&kp->wake_lock);
1186 + hrtimer_start(&kp->timer, ktime_set(0, 0), HRTIMER_MODE_REL);
1195 + for (i = mi->noutputs - 1; i >= 0; i--)
1196 + free_irq(gpio_to_irq(mi->input_gpios[i]), kp);
1198 + hrtimer_cancel(&kp->timer);
1199 + wake_lock_destroy(&kp->wake_lock);
1200 + for (i = mi->noutputs - 1; i >= 0; i--) {
1201 +err_gpio_direction_input_failed:
1202 + gpio_free(mi->input_gpios[i]);
1203 +err_request_input_gpio_failed:
1206 + for (i = mi->noutputs - 1; i >= 0; i--) {
1207 +err_output_gpio_configure_failed:
1208 + gpio_free(mi->output_gpios[i]);
1209 +err_request_output_gpio_failed:
1213 +err_kp_alloc_failed:
1214 +err_invalid_platform_data:
1218 +++ b/drivers/input/misc/gpio_output.c
1220 +/* drivers/input/misc/gpio_output.c
1222 + * Copyright (C) 2007 Google, Inc.
1224 + * This software is licensed under the terms of the GNU General Public
1225 + * License version 2, as published by the Free Software Foundation, and
1226 + * may be copied, distributed, and modified under those terms.
1228 + * This program is distributed in the hope that it will be useful,
1229 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1230 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1231 + * GNU General Public License for more details.
1235 +#include <linux/kernel.h>
1236 +#include <linux/gpio.h>
1237 +#include <linux/gpio_event.h>
1239 +int gpio_event_output_event(
1240 + struct input_dev *input_dev, struct gpio_event_info *info, void **data,
1241 + unsigned int type, unsigned int code, int value)
1244 + struct gpio_event_output_info *oi;
1245 + oi = container_of(info, struct gpio_event_output_info, info);
1246 + if (type != oi->type)
1248 + if (!(oi->flags & GPIOEDF_ACTIVE_HIGH))
1250 + for (i = 0; i < oi->keymap_size; i++)
1251 + if (code == oi->keymap[i].code)
1252 + gpio_set_value(oi->keymap[i].gpio, value);
1256 +int gpio_event_output_func(
1257 + struct input_dev *input_dev, struct gpio_event_info *info, void **data,
1262 + struct gpio_event_output_info *oi;
1263 + oi = container_of(info, struct gpio_event_output_info, info);
1265 + if (func == GPIO_EVENT_FUNC_SUSPEND || func == GPIO_EVENT_FUNC_RESUME)
1268 + if (func == GPIO_EVENT_FUNC_INIT) {
1269 + int output_level = !(oi->flags & GPIOEDF_ACTIVE_HIGH);
1270 + for (i = 0; i < oi->keymap_size; i++)
1271 + input_set_capability(input_dev, oi->type,
1272 + oi->keymap[i].code);
1274 + for (i = 0; i < oi->keymap_size; i++) {
1275 + ret = gpio_request(oi->keymap[i].gpio,
1276 + "gpio_event_output");
1278 + pr_err("gpio_event_output_func: gpio_request "
1279 + "failed for %d\n", oi->keymap[i].gpio);
1280 + goto err_gpio_request_failed;
1282 + ret = gpio_direction_output(oi->keymap[i].gpio,
1285 + pr_err("gpio_event_output_func: "
1286 + "gpio_direction_output failed for %d\n",
1287 + oi->keymap[i].gpio);
1288 + goto err_gpio_direction_output_failed;
1295 + for (i = oi->keymap_size - 1; i >= 0; i--) {
1296 +err_gpio_direction_output_failed:
1297 + gpio_free(oi->keymap[i].gpio);
1298 +err_gpio_request_failed:
1305 +++ b/include/linux/gpio_event.h
1307 +/* include/linux/gpio_event.h
1309 + * Copyright (C) 2007 Google, Inc.
1311 + * This software is licensed under the terms of the GNU General Public
1312 + * License version 2, as published by the Free Software Foundation, and
1313 + * may be copied, distributed, and modified under those terms.
1315 + * This program is distributed in the hope that it will be useful,
1316 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1317 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1318 + * GNU General Public License for more details.
1322 +#ifndef _LINUX_GPIO_EVENT_H
1323 +#define _LINUX_GPIO_EVENT_H
1325 +#include <linux/input.h>
1328 + GPIO_EVENT_FUNC_UNINIT = 0x0,
1329 + GPIO_EVENT_FUNC_INIT = 0x1,
1330 + GPIO_EVENT_FUNC_SUSPEND = 0x2,
1331 + GPIO_EVENT_FUNC_RESUME = 0x3,
1333 +struct gpio_event_info {
1334 + int (*func)(struct input_dev *input_dev,
1335 + struct gpio_event_info *info,
1336 + void **data, int func);
1337 + int (*event)(struct input_dev *input_dev,
1338 + struct gpio_event_info *info,
1339 + void **data, unsigned int type,
1340 + unsigned int code, int value); /* out events */
1343 +struct gpio_event_platform_data {
1345 + struct gpio_event_info **info;
1346 + size_t info_count;
1347 + int (*power)(const struct gpio_event_platform_data *pdata, bool on);
1350 +#define GPIO_EVENT_DEV_NAME "gpio-event"
1354 +enum gpio_event_matrix_flags {
1355 + /* unset: drive active output low, set: drive active output high */
1356 + GPIOKPF_ACTIVE_HIGH = 1U << 0,
1357 + GPIOKPF_DEBOUNCE = 1U << 1,
1358 + GPIOKPF_REMOVE_SOME_PHANTOM_KEYS = 1U << 2,
1359 + GPIOKPF_REMOVE_PHANTOM_KEYS = GPIOKPF_REMOVE_SOME_PHANTOM_KEYS |
1361 + GPIOKPF_DRIVE_INACTIVE = 1U << 3,
1362 + GPIOKPF_LEVEL_TRIGGERED_IRQ = 1U << 4,
1363 + GPIOKPF_PRINT_UNMAPPED_KEYS = 1U << 16,
1364 + GPIOKPF_PRINT_MAPPED_KEYS = 1U << 17,
1365 + GPIOKPF_PRINT_PHANTOM_KEYS = 1U << 18,
1368 +extern int gpio_event_matrix_func(struct input_dev *input_dev,
1369 + struct gpio_event_info *info, void **data, int func);
1370 +struct gpio_event_matrix_info {
1371 + /* initialize to gpio_event_matrix_func */
1372 + struct gpio_event_info info;
1373 + /* size must be ninputs * noutputs */
1374 + const unsigned short *keymap;
1375 + unsigned int *input_gpios;
1376 + unsigned int *output_gpios;
1377 + unsigned int ninputs;
1378 + unsigned int noutputs;
1379 + /* time to wait before reading inputs after driving each output */
1380 + ktime_t settle_time;
1381 + /* time to wait before scanning the keypad a second time */
1382 + ktime_t debounce_delay;
1383 + ktime_t poll_time;
1387 +/* Directly connected inputs and outputs */
1389 +enum gpio_event_direct_flags {
1390 + GPIOEDF_ACTIVE_HIGH = 1U << 0,
1391 +/* GPIOEDF_USE_DOWN_IRQ = 1U << 1, */
1392 +/* GPIOEDF_USE_IRQ = (1U << 2) | GPIOIDF_USE_DOWN_IRQ, */
1393 + GPIOEDF_PRINT_KEYS = 1U << 8,
1394 + GPIOEDF_PRINT_KEY_DEBOUNCE = 1U << 9,
1397 +struct gpio_event_direct_entry {
1403 +extern int gpio_event_input_func(struct input_dev *input_dev,
1404 + struct gpio_event_info *info, void **data, int func);
1405 +struct gpio_event_input_info {
1406 + /* initialize to gpio_event_input_func */
1407 + struct gpio_event_info info;
1408 + ktime_t debounce_time;
1409 + ktime_t poll_time;
1412 + const struct gpio_event_direct_entry *keymap;
1413 + size_t keymap_size;
1417 +extern int gpio_event_output_func(struct input_dev *input_dev,
1418 + struct gpio_event_info *info, void **data, int func);
1419 +extern int gpio_event_output_event(struct input_dev *input_dev,
1420 + struct gpio_event_info *info, void **data,
1421 + unsigned int type, unsigned int code, int value);
1422 +struct gpio_event_output_info {
1423 + /* initialize to gpio_event_output_func and gpio_event_output_event */
1424 + struct gpio_event_info info;
1427 + const struct gpio_event_direct_entry *keymap;
1428 + size_t keymap_size;
1434 +enum gpio_event_axis_flags {
1435 + GPIOEAF_PRINT_UNKNOWN_DIRECTION = 1U << 16,
1436 + GPIOEAF_PRINT_RAW = 1U << 17,
1437 + GPIOEAF_PRINT_EVENT = 1U << 18,
1440 +extern int gpio_event_axis_func(struct input_dev *input_dev,
1441 + struct gpio_event_info *info, void **data, int func);
1442 +struct gpio_event_axis_info {
1443 + /* initialize to gpio_event_axis_func */
1444 + struct gpio_event_info info;
1446 + uint8_t type; /* EV_REL or EV_ABS */
1448 + uint16_t decoded_size;
1449 + uint16_t (*map)(struct gpio_event_axis_info *info, uint16_t in);
1453 +#define gpio_axis_2bit_gray_map gpio_axis_4bit_gray_map
1454 +#define gpio_axis_3bit_gray_map gpio_axis_4bit_gray_map
1455 +uint16_t gpio_axis_4bit_gray_map(
1456 + struct gpio_event_axis_info *info, uint16_t in);
1457 +uint16_t gpio_axis_5bit_singletrack_map(
1458 + struct gpio_event_axis_info *info, uint16_t in);