[rdc] remove empty subtargets directories
[openwrt.git] / target / linux / goldfish / patches-2.6.30 / 0086-Input-Generic-GPIO-Input-device.patch
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.
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=utf-8
7 Content-Transfer-Encoding: 8bit
8
9 Supports keyboard matrixces, direct inputs, direct outputs and axes connected to gpios.
10
11 Signed-off-by: Arve Hjønnevåg <arve@android.com>
12 Signed-off-by: Nick Pelly <npelly@google.com>
13 ---
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
29
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.
35
36 +config INPUT_GPIO
37 + tristate "GPIO driver support"
38 + help
39 + Say Y here if you want to support gpio based keys, wheels etc...
40 +
41 config HP_SDC_RTC
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
51 +
52 --- /dev/null
53 +++ b/drivers/input/misc/gpio_axis.c
54 @@ -0,0 +1,180 @@
55 +/* drivers/input/misc/gpio_axis.c
56 + *
57 + * Copyright (C) 2007 Google, Inc.
58 + *
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.
62 + *
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.
67 + *
68 + */
69 +
70 +#include <linux/kernel.h>
71 +#include <linux/gpio.h>
72 +#include <linux/gpio_event.h>
73 +#include <linux/interrupt.h>
74 +
75 +struct gpio_axis_state {
76 + struct input_dev *input_dev;
77 + struct gpio_event_axis_info *info;
78 + uint32_t pos;
79 +};
80 +
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 */
90 +};
91 +uint16_t gpio_axis_4bit_gray_map(struct gpio_event_axis_info *info, uint16_t in)
92 +{
93 + return gpio_axis_4bit_gray_map_table[in];
94 +}
95 +
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 */
107 +};
108 +uint16_t gpio_axis_5bit_singletrack_map(
109 + struct gpio_event_axis_info *info, uint16_t in)
110 +{
111 + return gpio_axis_5bit_singletrack_map_table[in];
112 +}
113 +
114 +static void gpio_event_update_axis(struct gpio_axis_state *as, int report)
115 +{
116 + struct gpio_event_axis_info *ai = as->info;
117 + int i;
118 + int change;
119 + uint16_t state = 0;
120 + uint16_t pos;
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) %
131 + ai->decoded_size;
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 */
140 + }
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);
145 + } else {
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);
150 + }
151 + input_sync(as->input_dev);
152 + }
153 + as->pos = pos;
154 +}
155 +
156 +static irqreturn_t gpio_axis_irq_handler(int irq, void *dev_id)
157 +{
158 + struct gpio_axis_state *as = dev_id;
159 + gpio_event_update_axis(as, 1);
160 + return IRQ_HANDLED;
161 +}
162 +
163 +int gpio_event_axis_func(struct input_dev *input_dev,
164 + struct gpio_event_info *info, void **data, int func)
165 +{
166 + int ret;
167 + int i;
168 + int irq;
169 + struct gpio_event_axis_info *ai;
170 + struct gpio_axis_state *as;
171 +
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]));
176 + return 0;
177 + }
178 + if (func == GPIO_EVENT_FUNC_RESUME) {
179 + for (i = 0; i < ai->count; i++)
180 + enable_irq(gpio_to_irq(ai->gpio[i]));
181 + return 0;
182 + }
183 +
184 + if (func == GPIO_EVENT_FUNC_INIT) {
185 + *data = as = kmalloc(sizeof(*as), GFP_KERNEL);
186 + if (as == NULL) {
187 + ret = -ENOMEM;
188 + goto err_alloc_axis_state_failed;
189 + }
190 + as->input_dev = input_dev;
191 + as->info = ai;
192 +
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);
197 + }
198 + for (i = 0; i < ai->count; i++) {
199 + ret = gpio_request(ai->gpio[i], "gpio_event_axis");
200 + if (ret < 0)
201 + goto err_request_gpio_failed;
202 + ret = gpio_direction_input(ai->gpio[i]);
203 + if (ret < 0)
204 + goto err_gpio_direction_input_failed;
205 + ret = irq = gpio_to_irq(ai->gpio[i]);
206 + if (ret < 0)
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);
212 + if (ret < 0)
213 + goto err_request_irq_failed;
214 + }
215 + gpio_event_update_axis(as, 0);
216 + return 0;
217 + }
218 +
219 + ret = 0;
220 + as = *data;
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:
228 + ;
229 + }
230 + kfree(as);
231 + *data = NULL;
232 +err_alloc_axis_state_failed:
233 + return ret;
234 +}
235 --- /dev/null
236 +++ b/drivers/input/misc/gpio_event.c
237 @@ -0,0 +1,224 @@
238 +/* drivers/input/misc/gpio_event.c
239 + *
240 + * Copyright (C) 2007 Google, Inc.
241 + *
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.
245 + *
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.
250 + *
251 + */
252 +
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>
259 +
260 +struct gpio_event {
261 + struct input_dev *input_dev;
262 + const struct gpio_event_platform_data *info;
263 + struct early_suspend early_suspend;
264 + void *state[0];
265 +};
266 +
267 +static int gpio_input_event(
268 + struct input_dev *dev, unsigned int type, unsigned int code, int value)
269 +{
270 + int i;
271 + int ret = 0;
272 + int tmp_ret;
273 + struct gpio_event_info **ii;
274 + struct gpio_event *ip = input_get_drvdata(dev);
275 +
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);
280 + if (tmp_ret)
281 + ret = tmp_ret;
282 + }
283 + }
284 + return ret;
285 +}
286 +
287 +static int gpio_event_call_all_func(struct gpio_event *ip, int func)
288 +{
289 + int i;
290 + int ret;
291 + struct gpio_event_info **ii;
292 +
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) {
297 + ret = -ENODEV;
298 + pr_err("gpio_event_probe: Incomplete pdata, "
299 + "no function\n");
300 + goto err_no_func;
301 + }
302 + ret = (*ii)->func(ip->input_dev, *ii, &ip->state[i],
303 + func);
304 + if (ret) {
305 + pr_err("gpio_event_probe: function failed\n");
306 + goto err_func_failed;
307 + }
308 + }
309 + return 0;
310 + }
311 +
312 + ret = 0;
313 + i = ip->info->info_count;
314 + ii = ip->info->info + i;
315 + while (i > 0) {
316 + i--;
317 + ii--;
318 + (*ii)->func(ip->input_dev, *ii, &ip->state[i], func & ~1);
319 +err_func_failed:
320 +err_no_func:
321 + ;
322 + }
323 + return ret;
324 +}
325 +
326 +#ifdef CONFIG_HAS_EARLYSUSPEND
327 +void gpio_event_suspend(struct early_suspend *h)
328 +{
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);
333 +}
334 +
335 +void gpio_event_resume(struct early_suspend *h)
336 +{
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);
341 +}
342 +#endif
343 +
344 +static int __init gpio_event_probe(struct platform_device *pdev)
345 +{
346 + int err;
347 + struct gpio_event *ip;
348 + struct input_dev *input_dev;
349 + struct gpio_event_platform_data *event_info;
350 +
351 + event_info = pdev->dev.platform_data;
352 + if (event_info == NULL) {
353 + pr_err("gpio_event_probe: No pdata\n");
354 + return -ENODEV;
355 + }
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");
360 + return -ENODEV;
361 + }
362 + ip = kzalloc(sizeof(*ip) +
363 + sizeof(ip->state[0]) * event_info->info_count, GFP_KERNEL);
364 + if (ip == NULL) {
365 + err = -ENOMEM;
366 + pr_err("gpio_event_probe: Failed to allocate private data\n");
367 + goto err_kp_alloc_failed;
368 + }
369 + platform_set_drvdata(pdev, ip);
370 +
371 + input_dev = input_allocate_device();
372 + if (input_dev == NULL) {
373 + err = -ENOMEM;
374 + pr_err("gpio_event_probe: Failed to allocate input device\n");
375 + goto err_input_dev_alloc_failed;
376 + }
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);
386 +#endif
387 + ip->info->power(ip->info, 1);
388 + }
389 +
390 + input_dev->name = ip->info->name;
391 + input_dev->event = gpio_input_event;
392 +
393 + err = gpio_event_call_all_func(ip, GPIO_EVENT_FUNC_INIT);
394 + if (err)
395 + goto err_call_all_func_failed;
396 +
397 + err = input_register_device(input_dev);
398 + if (err) {
399 + pr_err("gpio_event_probe: Unable to register %s input device\n",
400 + input_dev->name);
401 + goto err_input_register_device_failed;
402 + }
403 +
404 + return 0;
405 +
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);
412 +#endif
413 + ip->info->power(ip->info, 0);
414 + }
415 + input_free_device(input_dev);
416 +err_input_dev_alloc_failed:
417 + kfree(ip);
418 +err_kp_alloc_failed:
419 + return err;
420 +}
421 +
422 +static int gpio_event_remove(struct platform_device *pdev)
423 +{
424 + struct gpio_event *ip = platform_get_drvdata(pdev);
425 +
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);
430 +#endif
431 + ip->info->power(ip->info, 0);
432 + }
433 + input_unregister_device(ip->input_dev);
434 + kfree(ip);
435 + return 0;
436 +}
437 +
438 +static struct platform_driver gpio_event_driver = {
439 + .probe = gpio_event_probe,
440 + .remove = gpio_event_remove,
441 + .driver = {
442 + .name = GPIO_EVENT_DEV_NAME,
443 + },
444 +};
445 +
446 +static int __devinit gpio_event_init(void)
447 +{
448 + return platform_driver_register(&gpio_event_driver);
449 +}
450 +
451 +static void __exit gpio_event_exit(void)
452 +{
453 + platform_driver_unregister(&gpio_event_driver);
454 +}
455 +
456 +module_init(gpio_event_init);
457 +module_exit(gpio_event_exit);
458 +
459 +MODULE_DESCRIPTION("GPIO Event Driver");
460 +MODULE_LICENSE("GPL");
461 +
462 --- /dev/null
463 +++ b/drivers/input/misc/gpio_input.c
464 @@ -0,0 +1,343 @@
465 +/* drivers/input/misc/gpio_input.c
466 + *
467 + * Copyright (C) 2007 Google, Inc.
468 + *
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.
472 + *
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.
477 + *
478 + */
479 +
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>
487 +
488 +enum {
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 */
494 +
495 + DEBOUNCE_UNKNOWN =
496 + DEBOUNCE_PRESSED | DEBOUNCE_NOTPRESSED,
497 +};
498 +
499 +struct gpio_key_state {
500 + struct gpio_input_state *ds;
501 + uint8_t debounce;
502 +};
503 +
504 +struct gpio_input_state {
505 + struct input_dev *input_dev;
506 + const struct gpio_event_input_info *info;
507 + struct hrtimer timer;
508 + int use_irq;
509 + int debounce_count;
510 + spinlock_t irq_lock;
511 + struct wake_lock wake_lock;
512 + struct gpio_key_state key_state[0];
513 +};
514 +
515 +static enum hrtimer_restart gpio_event_input_timer_func(struct hrtimer *timer)
516 +{
517 + int i;
518 + int pressed;
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;
527 + uint8_t debounce;
528 +
529 +#if 0
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));
535 +#endif
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)
542 + continue;
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);
550 + }
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);
562 + }
563 + continue;
564 + }
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;
572 + continue;
573 + }
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;
581 + continue;
582 + }
583 + /* key is stable */
584 + ds->debounce_count--;
585 + if (ds->use_irq)
586 + key_state->debounce |= DEBOUNCE_WAIT_IRQ;
587 + else
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);
595 + }
596 +
597 +#if 0
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));
603 + }
604 +#endif
605 +
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);
610 + else
611 + wake_unlock(&ds->wake_lock);
612 +
613 + spin_unlock_irqrestore(&ds->irq_lock, irqflags);
614 +
615 + return HRTIMER_NORESTART;
616 +}
617 +
618 +static irqreturn_t gpio_event_input_irq_handler(int irq, void *dev_id)
619 +{
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;
625 + int pressed;
626 +
627 + if (!ds->use_irq)
628 + return IRQ_HANDLED;
629 +
630 + key_entry = &ds->info->keymap[keymap_index];
631 +
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);
638 + hrtimer_start(
639 + &ds->timer, ds->info->debounce_time,
640 + HRTIMER_MODE_REL);
641 + }
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);
647 + } else {
648 + disable_irq(irq);
649 + ks->debounce = DEBOUNCE_UNSTABLE;
650 + }
651 + spin_unlock_irqrestore(&ds->irq_lock, irqflags);
652 + } else {
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);
662 + }
663 + return IRQ_HANDLED;
664 +}
665 +
666 +static int gpio_event_input_request_irqs(struct gpio_input_state *ds)
667 +{
668 + int i;
669 + int err;
670 + unsigned int irq;
671 + unsigned long req_flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
672 +
673 + for (i = 0; i < ds->info->keymap_size; i++) {
674 + err = irq = gpio_to_irq(ds->info->keymap[i].gpio);
675 + if (err < 0)
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]);
679 + if (err) {
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;
684 + }
685 + enable_irq_wake(irq);
686 + }
687 + return 0;
688 +
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:
694 + ;
695 + }
696 + return err;
697 +}
698 +
699 +int gpio_event_input_func(struct input_dev *input_dev,
700 + struct gpio_event_info *info, void **data, int func)
701 +{
702 + int ret;
703 + int i;
704 + unsigned long irqflags;
705 + struct gpio_event_input_info *di;
706 + struct gpio_input_state *ds = *data;
707 +
708 + di = container_of(info, struct gpio_event_input_info, info);
709 +
710 + if (func == GPIO_EVENT_FUNC_SUSPEND) {
711 + spin_lock_irqsave(&ds->irq_lock, irqflags);
712 + if (ds->use_irq)
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);
717 + return 0;
718 + }
719 + if (func == GPIO_EVENT_FUNC_RESUME) {
720 + spin_lock_irqsave(&ds->irq_lock, irqflags);
721 + if (ds->use_irq)
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);
726 + return 0;
727 + }
728 +
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);
732 +
733 + *data = ds = kzalloc(sizeof(*ds) + sizeof(ds->key_state[0]) *
734 + di->keymap_size, GFP_KERNEL);
735 + if (ds == NULL) {
736 + ret = -ENOMEM;
737 + pr_err("gpio_event_input_func: "
738 + "Failed to allocate private data\n");
739 + goto err_ds_alloc_failed;
740 + }
741 + ds->debounce_count = di->keymap_size;
742 + ds->input_dev = input_dev;
743 + ds->info = di;
744 + wake_lock_init(&ds->wake_lock, WAKE_LOCK_SUSPEND, "gpio_input");
745 + spin_lock_init(&ds->irq_lock);
746 +
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;
752 + }
753 +
754 + for (i = 0; i < di->keymap_size; i++) {
755 + ret = gpio_request(di->keymap[i].gpio, "gpio_kp_in");
756 + if (ret) {
757 + pr_err("gpio_event_input_func: gpio_request "
758 + "failed for %d\n", di->keymap[i].gpio);
759 + goto err_gpio_request_failed;
760 + }
761 + ret = gpio_direction_input(di->keymap[i].gpio);
762 + if (ret) {
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;
767 + }
768 + }
769 +
770 + ret = gpio_event_input_request_irqs(ds);
771 +
772 + spin_lock_irqsave(&ds->irq_lock, irqflags);
773 + ds->use_irq = ret == 0;
774 +
775 + pr_info("GPIO Input Driver: Start gpio inputs for %s in %s "
776 + "mode\n",
777 + input_dev->name, ret == 0 ? "interrupt" : "polling");
778 +
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);
783 + return 0;
784 + }
785 +
786 + ret = 0;
787 + spin_lock_irqsave(&ds->irq_lock, irqflags);
788 + hrtimer_cancel(&ds->timer);
789 + if (ds->use_irq) {
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]);
793 + }
794 + }
795 + spin_unlock_irqrestore(&ds->irq_lock, irqflags);
796 +
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:
801 + ;
802 + }
803 + wake_lock_destroy(&ds->wake_lock);
804 + kfree(ds);
805 +err_ds_alloc_failed:
806 + return ret;
807 +}
808 --- /dev/null
809 +++ b/drivers/input/misc/gpio_matrix.c
810 @@ -0,0 +1,406 @@
811 +/* drivers/input/misc/gpio_matrix.c
812 + *
813 + * Copyright (C) 2007 Google, Inc.
814 + *
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.
818 + *
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.
823 + *
824 + */
825 +
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>
832 +
833 +struct gpio_kp {
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];
844 +};
845 +
846 +static void clear_phantom_key(struct gpio_kp *kp, int out, int in)
847 +{
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];;
851 +
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);
858 + } else {
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]);
863 + }
864 +}
865 +
866 +static int restore_keys_for_input(struct gpio_kp *kp, int out, int in)
867 +{
868 + int rv = 0;
869 + int key_index;
870 +
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)) {
874 + rv = 1;
875 + clear_phantom_key(kp, out, in);
876 + }
877 + key_index += kp->keypad_info->ninputs;
878 + out++;
879 + }
880 + return rv;
881 +}
882 +
883 +static void remove_phantom_keys(struct gpio_kp *kp)
884 +{
885 + int out, in, inp;
886 + int key_index;
887 +
888 + if (kp->some_keys_pressed < 3)
889 + return;
890 +
891 + for (out = 0; out < kp->keypad_info->noutputs; out++) {
892 + inp = -1;
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)) {
896 + if (inp == -1) {
897 + inp = in;
898 + continue;
899 + }
900 + if (inp >= 0) {
901 + if (!restore_keys_for_input(kp, out + 1,
902 + inp))
903 + break;
904 + clear_phantom_key(kp, out, inp);
905 + inp = -2;
906 + }
907 + restore_keys_for_input(kp, out, in);
908 + }
909 + }
910 + }
911 +}
912 +
913 +static void report_key(struct gpio_kp *kp, int key_index, int out, int in)
914 +{
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);
925 + } else {
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);
932 + }
933 + }
934 +}
935 +
936 +static enum hrtimer_restart gpio_keypad_timer_func(struct hrtimer *timer)
937 +{
938 + int out, in;
939 + int key_index;
940 + int gpio;
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);
945 +
946 + out = kp->current_output;
947 + if (out == mi->noutputs) {
948 + out = 0;
949 + kp->last_key_state_changed = kp->key_state_changed;
950 + kp->key_state_changed = 0;
951 + kp->some_keys_pressed = 0;
952 + } else {
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);
961 + } else
962 + kp->key_state_changed |= __test_and_clear_bit(
963 + key_index, kp->keys_pressed);
964 + }
965 + gpio = mi->output_gpios[out];
966 + if (gpio_keypad_flags & GPIOKPF_DRIVE_INACTIVE)
967 + gpio_set_value(gpio, !polarity);
968 + else
969 + gpio_direction_input(gpio);
970 + out++;
971 + }
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);
977 + else
978 + gpio_direction_output(gpio, polarity);
979 + hrtimer_start(timer, mi->settle_time, HRTIMER_MODE_REL);
980 + return HRTIMER_NORESTART;
981 + }
982 + if (gpio_keypad_flags & GPIOKPF_DEBOUNCE) {
983 + if (kp->key_state_changed) {
984 + hrtimer_start(&kp->timer, mi->debounce_delay,
985 + HRTIMER_MODE_REL);
986 + return HRTIMER_NORESTART;
987 + }
988 + kp->key_state_changed = kp->last_key_state_changed;
989 + }
990 + if (kp->key_state_changed) {
991 + if (gpio_keypad_flags & GPIOKPF_REMOVE_SOME_PHANTOM_KEYS)
992 + remove_phantom_keys(kp);
993 + key_index = 0;
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);
997 + }
998 + if (!kp->use_irq || kp->some_keys_pressed) {
999 + hrtimer_start(timer, mi->poll_time, HRTIMER_MODE_REL);
1000 + return HRTIMER_NORESTART;
1001 + }
1002 +
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);
1007 + else
1008 + gpio_direction_output(mi->output_gpios[out], polarity);
1009 + }
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;
1014 +}
1015 +
1016 +static irqreturn_t gpio_keypad_irq_handler(int irq_in, void *dev_id)
1017 +{
1018 + int i;
1019 + struct gpio_kp *kp = dev_id;
1020 + struct gpio_event_matrix_info *mi = kp->keypad_info;
1021 + unsigned gpio_keypad_flags = mi->flags;
1022 +
1023 + if (!kp->use_irq) /* ignore interrupt while registering the handler */
1024 + return IRQ_HANDLED;
1025 +
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));
1032 + else
1033 + gpio_direction_input(mi->output_gpios[i]);
1034 + }
1035 + wake_lock(&kp->wake_lock);
1036 + hrtimer_start(&kp->timer, ktime_set(0, 0), HRTIMER_MODE_REL);
1037 + return IRQ_HANDLED;
1038 +}
1039 +
1040 +static int gpio_keypad_request_irqs(struct gpio_kp *kp)
1041 +{
1042 + int i;
1043 + int err;
1044 + unsigned int irq;
1045 + unsigned long request_flags;
1046 + struct gpio_event_matrix_info *mi = kp->keypad_info;
1047 +
1048 + switch (mi->flags & (GPIOKPF_ACTIVE_HIGH|GPIOKPF_LEVEL_TRIGGERED_IRQ)) {
1049 + default:
1050 + request_flags = IRQF_TRIGGER_FALLING;
1051 + break;
1052 + case GPIOKPF_ACTIVE_HIGH:
1053 + request_flags = IRQF_TRIGGER_RISING;
1054 + break;
1055 + case GPIOKPF_LEVEL_TRIGGERED_IRQ:
1056 + request_flags = IRQF_TRIGGER_LOW;
1057 + break;
1058 + case GPIOKPF_LEVEL_TRIGGERED_IRQ | GPIOKPF_ACTIVE_HIGH:
1059 + request_flags = IRQF_TRIGGER_HIGH;
1060 + break;
1061 + }
1062 +
1063 + for (i = 0; i < mi->ninputs; i++) {
1064 + err = irq = gpio_to_irq(mi->input_gpios[i]);
1065 + if (err < 0)
1066 + goto err_gpio_get_irq_num_failed;
1067 + err = request_irq(irq, gpio_keypad_irq_handler, request_flags,
1068 + "gpio_kp", kp);
1069 + if (err) {
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;
1073 + }
1074 + err = set_irq_wake(irq, 1);
1075 + if (err) {
1076 + pr_err("gpiomatrix: set_irq_wake failed for input %d, "
1077 + "irq %d\n", mi->input_gpios[i], irq);
1078 + }
1079 + disable_irq(irq);
1080 + }
1081 + return 0;
1082 +
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:
1087 + ;
1088 + }
1089 + return err;
1090 +}
1091 +
1092 +int gpio_event_matrix_func(struct input_dev *input_dev,
1093 + struct gpio_event_info *info, void **data, int func)
1094 +{
1095 + int i;
1096 + int err;
1097 + int key_count;
1098 + struct gpio_kp *kp;
1099 + struct gpio_event_matrix_info *mi;
1100 +
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 */
1104 + return 0;
1105 + }
1106 +
1107 + if (func == GPIO_EVENT_FUNC_INIT) {
1108 + if (mi->keymap == NULL ||
1109 + mi->input_gpios == NULL ||
1110 + mi->output_gpios == NULL) {
1111 + err = -ENODEV;
1112 + pr_err("gpiomatrix: Incomplete pdata\n");
1113 + goto err_invalid_platform_data;
1114 + }
1115 + key_count = mi->ninputs * mi->noutputs;
1116 +
1117 + *data = kp = kzalloc(sizeof(*kp) + sizeof(kp->keys_pressed[0]) *
1118 + BITS_TO_LONGS(key_count), GFP_KERNEL);
1119 + if (kp == NULL) {
1120 + err = -ENOMEM;
1121 + pr_err("gpiomatrix: Failed to allocate private data\n");
1122 + goto err_kp_alloc_failed;
1123 + }
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);
1131 + }
1132 +
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]);
1137 + err = -EINVAL;
1138 + goto err_request_output_gpio_failed;
1139 + }
1140 + err = gpio_request(mi->output_gpios[i], "gpio_kp_out");
1141 + if (err) {
1142 + pr_err("gpiomatrix: gpio_request failed for "
1143 + "output %d\n", mi->output_gpios[i]);
1144 + goto err_request_output_gpio_failed;
1145 + }
1146 + if (mi->flags & GPIOKPF_DRIVE_INACTIVE)
1147 + err = gpio_direction_output(mi->output_gpios[i],
1148 + !(mi->flags & GPIOKPF_ACTIVE_HIGH));
1149 + else
1150 + err = gpio_direction_input(mi->output_gpios[i]);
1151 + if (err) {
1152 + pr_err("gpiomatrix: gpio_configure failed for "
1153 + "output %d\n", mi->output_gpios[i]);
1154 + goto err_output_gpio_configure_failed;
1155 + }
1156 + }
1157 + for (i = 0; i < mi->ninputs; i++) {
1158 + err = gpio_request(mi->input_gpios[i], "gpio_kp_in");
1159 + if (err) {
1160 + pr_err("gpiomatrix: gpio_request failed for "
1161 + "input %d\n", mi->input_gpios[i]);
1162 + goto err_request_input_gpio_failed;
1163 + }
1164 + err = gpio_direction_input(mi->input_gpios[i]);
1165 + if (err) {
1166 + pr_err("gpiomatrix: gpio_direction_input failed"
1167 + " for input %d\n", mi->input_gpios[i]);
1168 + goto err_gpio_direction_input_failed;
1169 + }
1170 + }
1171 + kp->current_output = mi->noutputs;
1172 + kp->key_state_changed = 1;
1173 +
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;
1179 +
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");
1183 +
1184 + if (kp->use_irq)
1185 + wake_lock(&kp->wake_lock);
1186 + hrtimer_start(&kp->timer, ktime_set(0, 0), HRTIMER_MODE_REL);
1187 +
1188 + return 0;
1189 + }
1190 +
1191 + err = 0;
1192 + kp = *data;
1193 +
1194 + if (kp->use_irq)
1195 + for (i = mi->noutputs - 1; i >= 0; i--)
1196 + free_irq(gpio_to_irq(mi->input_gpios[i]), kp);
1197 +
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:
1204 + ;
1205 + }
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:
1210 + ;
1211 + }
1212 + kfree(kp);
1213 +err_kp_alloc_failed:
1214 +err_invalid_platform_data:
1215 + return err;
1216 +}
1217 --- /dev/null
1218 +++ b/drivers/input/misc/gpio_output.c
1219 @@ -0,0 +1,84 @@
1220 +/* drivers/input/misc/gpio_output.c
1221 + *
1222 + * Copyright (C) 2007 Google, Inc.
1223 + *
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.
1227 + *
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.
1232 + *
1233 + */
1234 +
1235 +#include <linux/kernel.h>
1236 +#include <linux/gpio.h>
1237 +#include <linux/gpio_event.h>
1238 +
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)
1242 +{
1243 + int i;
1244 + struct gpio_event_output_info *oi;
1245 + oi = container_of(info, struct gpio_event_output_info, info);
1246 + if (type != oi->type)
1247 + return 0;
1248 + if (!(oi->flags & GPIOEDF_ACTIVE_HIGH))
1249 + value = !value;
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);
1253 + return 0;
1254 +}
1255 +
1256 +int gpio_event_output_func(
1257 + struct input_dev *input_dev, struct gpio_event_info *info, void **data,
1258 + int func)
1259 +{
1260 + int ret;
1261 + int i;
1262 + struct gpio_event_output_info *oi;
1263 + oi = container_of(info, struct gpio_event_output_info, info);
1264 +
1265 + if (func == GPIO_EVENT_FUNC_SUSPEND || func == GPIO_EVENT_FUNC_RESUME)
1266 + return 0;
1267 +
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);
1273 +
1274 + for (i = 0; i < oi->keymap_size; i++) {
1275 + ret = gpio_request(oi->keymap[i].gpio,
1276 + "gpio_event_output");
1277 + if (ret) {
1278 + pr_err("gpio_event_output_func: gpio_request "
1279 + "failed for %d\n", oi->keymap[i].gpio);
1280 + goto err_gpio_request_failed;
1281 + }
1282 + ret = gpio_direction_output(oi->keymap[i].gpio,
1283 + output_level);
1284 + if (ret) {
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;
1289 + }
1290 + }
1291 + return 0;
1292 + }
1293 +
1294 + ret = 0;
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:
1299 + ;
1300 + }
1301 + return ret;
1302 +}
1303 +
1304 --- /dev/null
1305 +++ b/include/linux/gpio_event.h
1306 @@ -0,0 +1,154 @@
1307 +/* include/linux/gpio_event.h
1308 + *
1309 + * Copyright (C) 2007 Google, Inc.
1310 + *
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.
1314 + *
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.
1319 + *
1320 + */
1321 +
1322 +#ifndef _LINUX_GPIO_EVENT_H
1323 +#define _LINUX_GPIO_EVENT_H
1324 +
1325 +#include <linux/input.h>
1326 +
1327 +enum {
1328 + GPIO_EVENT_FUNC_UNINIT = 0x0,
1329 + GPIO_EVENT_FUNC_INIT = 0x1,
1330 + GPIO_EVENT_FUNC_SUSPEND = 0x2,
1331 + GPIO_EVENT_FUNC_RESUME = 0x3,
1332 +};
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 */
1341 +};
1342 +
1343 +struct gpio_event_platform_data {
1344 + const char *name;
1345 + struct gpio_event_info **info;
1346 + size_t info_count;
1347 + int (*power)(const struct gpio_event_platform_data *pdata, bool on);
1348 +};
1349 +
1350 +#define GPIO_EVENT_DEV_NAME "gpio-event"
1351 +
1352 +/* Key matrix */
1353 +
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 |
1360 + GPIOKPF_DEBOUNCE,
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,
1366 +};
1367 +
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;
1384 + unsigned flags;
1385 +};
1386 +
1387 +/* Directly connected inputs and outputs */
1388 +
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,
1395 +};
1396 +
1397 +struct gpio_event_direct_entry {
1398 + uint32_t gpio:23;
1399 + uint32_t code:9;
1400 +};
1401 +
1402 +/* inputs */
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;
1410 + uint16_t flags;
1411 + uint16_t type;
1412 + const struct gpio_event_direct_entry *keymap;
1413 + size_t keymap_size;
1414 +};
1415 +
1416 +/* outputs */
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;
1425 + uint16_t flags;
1426 + uint16_t type;
1427 + const struct gpio_event_direct_entry *keymap;
1428 + size_t keymap_size;
1429 +};
1430 +
1431 +
1432 +/* axes */
1433 +
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,
1438 +};
1439 +
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;
1445 + uint8_t count;
1446 + uint8_t type; /* EV_REL or EV_ABS */
1447 + uint16_t code;
1448 + uint16_t decoded_size;
1449 + uint16_t (*map)(struct gpio_event_axis_info *info, uint16_t in);
1450 + uint32_t *gpio;
1451 + uint32_t flags;
1452 +};
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);
1459 +
1460 +#endif
This page took 0.136797 seconds and 5 git commands to generate.