blob: 548b5b5783d99581cdfec515a654124aca47ff0a [file] [log] [blame]
Arve Hjønnevågc3fffcb2008-10-15 18:23:47 -07001/* drivers/input/misc/gpio_input.c
2 *
3 * Copyright (C) 2007 Google, Inc.
4 *
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.
8 *
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.
13 *
14 */
15
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>
Flemmardc36b8672013-10-23 17:43:54 +020024#ifdef CONFIG_OPTICALJOYSTICK_CRUCIAL
25#include <linux/curcial_oj.h>
26#endif
Arve Hjønnevågc3fffcb2008-10-15 18:23:47 -070027
Flemmardce3c2c32013-05-23 16:53:44 -070028#ifdef CONFIG_TOUCHSCREEN_SYNAPTICS_3K
29static uint8_t power_key_state;
30static spinlock_t power_key_state_lock;
31
32#define PWRKEY_PRESS_DUE 1*HZ
33#include <linux/module.h>
34static void init_power_key_api(void)
35{
36 spin_lock_init(&power_key_state_lock);
37 power_key_state = 0;
38}
39
40static void setPowerKeyState(uint8_t flag)
41{
42 spin_lock(&power_key_state_lock);
43 power_key_state = flag;
44 spin_unlock(&power_key_state_lock);
45}
46
47uint8_t getPowerKeyState(void)
48{
49 uint8_t value;
50
51 spin_lock(&power_key_state_lock);
52 value = power_key_state;
53 spin_unlock(&power_key_state_lock);
54
55 return value;
56}
57EXPORT_SYMBOL(getPowerKeyState);
58
59static void power_key_state_disable_work_func(struct work_struct *dummy)
60{
61 setPowerKeyState(0);
62 printk(KERN_INFO "[KEY][PWR][STATE]power key pressed outdated\n");
63}
64static DECLARE_DELAYED_WORK(power_key_state_disable_work, power_key_state_disable_work_func);
65
66static void handle_power_key_state(unsigned int code, int value)
67{
68 int ret = 0;
69 if (code == KEY_POWER && value == 1) {
70 printk(KERN_INFO "[PWR][STATE]try to schedule power key pressed due\n");
71 ret = schedule_delayed_work(&power_key_state_disable_work, PWRKEY_PRESS_DUE);
72 if (!ret) {
73 printk(KERN_INFO "[PWR][STATE]Schedule power key pressed due failed, seems already have one, try to cancel...\n");
74 ret = cancel_delayed_work(&power_key_state_disable_work);
75 if (!ret) {
76 setPowerKeyState(1);
77 if (schedule_delayed_work(&power_key_state_disable_work, PWRKEY_PRESS_DUE)) {
78 printk(KERN_INFO "[PWR][STATE]Re-schedule power key pressed due SCCUESS.\n");
79 printk(KERN_INFO "[PWR][STATE] start count for power key pressed due\n");
80 setPowerKeyState(1);
81 } else
82 printk(KERN_INFO "[PWR][STATE]Re-schedule power key pressed due FAILED, reason unknown, give up.\n");
83 } else {
84 printk(KERN_INFO "[PWR][STATE]Cancel scheduled power key due success, now re-schedule.\n");
85 if (schedule_delayed_work(&power_key_state_disable_work, PWRKEY_PRESS_DUE)) {
86 printk(KERN_INFO "[PWR][STATE]Re-schedule power key pressed due SCCUESS.\n");
87 printk(KERN_INFO "[PWR][STATE] start count for power key pressed due\n");
88 setPowerKeyState(1);
89 } else
90 printk(KERN_INFO "[PWR][STATE]Re-schedule power key pressed due FAILED, reason unknown, give up.\n");
91 }
92 } else {
93 printk(KERN_INFO "[PWR][STATE] start count for power key pressed due\n");
94 setPowerKeyState(1);
95 }
96 }
97}
98#endif /* CONFIG_TOUCHSCREEN_SYNAPTICS_3K */
99
Matt Mowere727d6c2013-10-17 03:36:09 -0500100#ifdef CONFIG_HTC_WAKE_ON_VOL
101static DEFINE_MUTEX(wakeup_mutex);
102static unsigned char wakeup_bitmask;
103static unsigned char set_wakeup;
104static unsigned int vol_up_irq;
105static unsigned int vol_down_irq;
106static ssize_t vol_wakeup_store(struct device *dev,
107 struct device_attribute *attr,
108 const char *buf, size_t count)
109{
110 unsigned char bitmask = 0;
111 bitmask = simple_strtoull(buf, NULL, 10);
112 mutex_lock(&wakeup_mutex);
113 if (bitmask) {
114 if (bitmask == 127)
115 wakeup_bitmask &= bitmask;
116 else if (bitmask > 128)
117 wakeup_bitmask &= bitmask;
118 else
119 wakeup_bitmask |= bitmask;
120 }
121
122 if (wakeup_bitmask && (!set_wakeup)) {
123 enable_irq_wake(vol_up_irq);
124 enable_irq_wake(vol_down_irq);
125 set_wakeup = 1;
126 printk(KERN_INFO "%s:change to wake up function(%d, %d)\n", __func__, vol_up_irq, vol_down_irq);
127 } else if ((!wakeup_bitmask) && set_wakeup){
128 disable_irq_wake(vol_up_irq);
129 disable_irq_wake(vol_down_irq);
130 set_wakeup = 0;
131 printk(KERN_INFO "%s:change to non-wake up function(%d, %d)\n", __func__, vol_up_irq, vol_down_irq);
132 }
133 mutex_unlock(&wakeup_mutex);
134 return count;
135}
136static ssize_t vol_wakeup_show(struct device *dev,
137 struct device_attribute *attr, char *buf)
138{
139 return sprintf(buf, "%x\n", wakeup_bitmask);
140}
141
142static DEVICE_ATTR(vol_wakeup, 0664, vol_wakeup_show, vol_wakeup_store);
143#endif /* CONFIG_HTC_WAKE_ON_VOL */
144
Arve Hjønnevågc3fffcb2008-10-15 18:23:47 -0700145enum {
146 DEBOUNCE_UNSTABLE = BIT(0), /* Got irq, while debouncing */
147 DEBOUNCE_PRESSED = BIT(1),
148 DEBOUNCE_NOTPRESSED = BIT(2),
149 DEBOUNCE_WAIT_IRQ = BIT(3), /* Stable irq state */
150 DEBOUNCE_POLL = BIT(4), /* Stable polling state */
151
152 DEBOUNCE_UNKNOWN =
153 DEBOUNCE_PRESSED | DEBOUNCE_NOTPRESSED,
154};
155
156struct gpio_key_state {
157 struct gpio_input_state *ds;
158 uint8_t debounce;
159};
160
161struct gpio_input_state {
162 struct gpio_event_input_devs *input_devs;
163 const struct gpio_event_input_info *info;
164 struct hrtimer timer;
165 int use_irq;
166 int debounce_count;
167 spinlock_t irq_lock;
168 struct wake_lock wake_lock;
169 struct gpio_key_state key_state[0];
170};
171
172static enum hrtimer_restart gpio_event_input_timer_func(struct hrtimer *timer)
173{
174 int i;
175 int pressed;
176 struct gpio_input_state *ds =
177 container_of(timer, struct gpio_input_state, timer);
178 unsigned gpio_flags = ds->info->flags;
179 unsigned npolarity;
180 int nkeys = ds->info->keymap_size;
181 const struct gpio_event_direct_entry *key_entry;
182 struct gpio_key_state *key_state;
183 unsigned long irqflags;
184 uint8_t debounce;
185 bool sync_needed;
186
187#if 0
188 key_entry = kp->keys_info->keymap;
189 key_state = kp->key_state;
190 for (i = 0; i < nkeys; i++, key_entry++, key_state++)
191 pr_info("gpio_read_detect_status %d %d\n", key_entry->gpio,
192 gpio_read_detect_status(key_entry->gpio));
193#endif
194 key_entry = ds->info->keymap;
195 key_state = ds->key_state;
196 sync_needed = false;
197 spin_lock_irqsave(&ds->irq_lock, irqflags);
198 for (i = 0; i < nkeys; i++, key_entry++, key_state++) {
199 debounce = key_state->debounce;
200 if (debounce & DEBOUNCE_WAIT_IRQ)
201 continue;
202 if (key_state->debounce & DEBOUNCE_UNSTABLE) {
203 debounce = key_state->debounce = DEBOUNCE_UNKNOWN;
204 enable_irq(gpio_to_irq(key_entry->gpio));
205 if (gpio_flags & GPIOEDF_PRINT_KEY_UNSTABLE)
206 pr_info("gpio_keys_scan_keys: key %x-%x, %d "
207 "(%d) continue debounce\n",
208 ds->info->type, key_entry->code,
209 i, key_entry->gpio);
210 }
211 npolarity = !(gpio_flags & GPIOEDF_ACTIVE_HIGH);
212 pressed = gpio_get_value(key_entry->gpio) ^ npolarity;
213 if (debounce & DEBOUNCE_POLL) {
214 if (pressed == !(debounce & DEBOUNCE_PRESSED)) {
215 ds->debounce_count++;
216 key_state->debounce = DEBOUNCE_UNKNOWN;
217 if (gpio_flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
218 pr_info("gpio_keys_scan_keys: key %x-"
219 "%x, %d (%d) start debounce\n",
220 ds->info->type, key_entry->code,
221 i, key_entry->gpio);
222 }
223 continue;
224 }
225 if (pressed && (debounce & DEBOUNCE_NOTPRESSED)) {
226 if (gpio_flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
227 pr_info("gpio_keys_scan_keys: key %x-%x, %d "
228 "(%d) debounce pressed 1\n",
229 ds->info->type, key_entry->code,
230 i, key_entry->gpio);
231 key_state->debounce = DEBOUNCE_PRESSED;
232 continue;
233 }
234 if (!pressed && (debounce & DEBOUNCE_PRESSED)) {
235 if (gpio_flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
236 pr_info("gpio_keys_scan_keys: key %x-%x, %d "
237 "(%d) debounce pressed 0\n",
238 ds->info->type, key_entry->code,
239 i, key_entry->gpio);
240 key_state->debounce = DEBOUNCE_NOTPRESSED;
241 continue;
242 }
243 /* key is stable */
244 ds->debounce_count--;
245 if (ds->use_irq)
246 key_state->debounce |= DEBOUNCE_WAIT_IRQ;
247 else
248 key_state->debounce |= DEBOUNCE_POLL;
249 if (gpio_flags & GPIOEDF_PRINT_KEYS)
250 pr_info("gpio_keys_scan_keys: key %x-%x, %d (%d) "
251 "changed to %d\n", ds->info->type,
252 key_entry->code, i, key_entry->gpio, pressed);
Flemmardce3c2c32013-05-23 16:53:44 -0700253#ifdef CONFIG_TOUCHSCREEN_SYNAPTICS_3K
254 handle_power_key_state(key_entry->code, pressed);
255#endif
Flemmardc36b8672013-10-23 17:43:54 +0200256#ifdef CONFIG_OPTICALJOYSTICK_CRUCIAL
257 if (key_entry->code == BTN_MOUSE) {
258 pr_info("gpio_keys_scan_keys: OJ action key %d-%d, %d (%d) "
259 "changed to %d\n", ds->info->type,
260 key_entry->code, i, key_entry->gpio, pressed);
261 curcial_oj_send_key(BTN_MOUSE, pressed);
262 } else {
263#endif
Arve Hjønnevågc3fffcb2008-10-15 18:23:47 -0700264 input_event(ds->input_devs->dev[key_entry->dev], ds->info->type,
265 key_entry->code, pressed);
266 sync_needed = true;
Flemmardc36b8672013-10-23 17:43:54 +0200267#ifdef CONFIG_OPTICALJOYSTICK_CRUCIAL
268 }
269#endif
Arve Hjønnevågc3fffcb2008-10-15 18:23:47 -0700270 }
271 if (sync_needed) {
272 for (i = 0; i < ds->input_devs->count; i++)
273 input_sync(ds->input_devs->dev[i]);
274 }
275
276#if 0
277 key_entry = kp->keys_info->keymap;
278 key_state = kp->key_state;
279 for (i = 0; i < nkeys; i++, key_entry++, key_state++) {
280 pr_info("gpio_read_detect_status %d %d\n", key_entry->gpio,
281 gpio_read_detect_status(key_entry->gpio));
282 }
283#endif
284
285 if (ds->debounce_count)
286 hrtimer_start(timer, ds->info->debounce_time, HRTIMER_MODE_REL);
287 else if (!ds->use_irq)
288 hrtimer_start(timer, ds->info->poll_time, HRTIMER_MODE_REL);
289 else
290 wake_unlock(&ds->wake_lock);
291
292 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
293
294 return HRTIMER_NORESTART;
295}
296
297static irqreturn_t gpio_event_input_irq_handler(int irq, void *dev_id)
298{
299 struct gpio_key_state *ks = dev_id;
300 struct gpio_input_state *ds = ks->ds;
301 int keymap_index = ks - ds->key_state;
302 const struct gpio_event_direct_entry *key_entry;
303 unsigned long irqflags;
304 int pressed;
305
306 if (!ds->use_irq)
307 return IRQ_HANDLED;
308
309 key_entry = &ds->info->keymap[keymap_index];
310
311 if (ds->info->debounce_time.tv64) {
312 spin_lock_irqsave(&ds->irq_lock, irqflags);
313 if (ks->debounce & DEBOUNCE_WAIT_IRQ) {
314 ks->debounce = DEBOUNCE_UNKNOWN;
315 if (ds->debounce_count++ == 0) {
316 wake_lock(&ds->wake_lock);
317 hrtimer_start(
318 &ds->timer, ds->info->debounce_time,
319 HRTIMER_MODE_REL);
320 }
321 if (ds->info->flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
322 pr_info("gpio_event_input_irq_handler: "
323 "key %x-%x, %d (%d) start debounce\n",
324 ds->info->type, key_entry->code,
325 keymap_index, key_entry->gpio);
326 } else {
327 disable_irq_nosync(irq);
328 ks->debounce = DEBOUNCE_UNSTABLE;
329 }
330 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
331 } else {
332 pressed = gpio_get_value(key_entry->gpio) ^
333 !(ds->info->flags & GPIOEDF_ACTIVE_HIGH);
334 if (ds->info->flags & GPIOEDF_PRINT_KEYS)
335 pr_info("gpio_event_input_irq_handler: key %x-%x, %d "
336 "(%d) changed to %d\n",
337 ds->info->type, key_entry->code, keymap_index,
338 key_entry->gpio, pressed);
339 input_event(ds->input_devs->dev[key_entry->dev], ds->info->type,
340 key_entry->code, pressed);
341 input_sync(ds->input_devs->dev[key_entry->dev]);
342 }
343 return IRQ_HANDLED;
344}
345
346static int gpio_event_input_request_irqs(struct gpio_input_state *ds)
347{
348 int i;
349 int err;
350 unsigned int irq;
351 unsigned long req_flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
352
353 for (i = 0; i < ds->info->keymap_size; i++) {
354 err = irq = gpio_to_irq(ds->info->keymap[i].gpio);
355 if (err < 0)
356 goto err_gpio_get_irq_num_failed;
357 err = request_irq(irq, gpio_event_input_irq_handler,
358 req_flags, "gpio_keys", &ds->key_state[i]);
359 if (err) {
360 pr_err("gpio_event_input_request_irqs: request_irq "
361 "failed for input %d, irq %d\n",
362 ds->info->keymap[i].gpio, irq);
363 goto err_request_irq_failed;
364 }
Matt Mowere727d6c2013-10-17 03:36:09 -0500365#ifdef CONFIG_HTC_WAKE_ON_VOL
366 if (ds->info->keymap[i].code == KEY_VOLUMEUP ||
367 ds->info->keymap[i].code == KEY_VOLUMEDOWN) {
368 pr_info("keycode = %d, gpio = %d, irq = %d\n",
369 ds->info->keymap[i].code,
370 ds->info->keymap[i].gpio, irq);
371 if (ds->info->keymap[i].code == KEY_VOLUMEUP)
372 vol_up_irq = irq;
373 else
374 vol_down_irq = irq;
375 } else {
376 enable_irq_wake(irq);
377 }
378#endif
Arve Hjønnevågc3fffcb2008-10-15 18:23:47 -0700379 if (ds->info->info.no_suspend) {
380 err = enable_irq_wake(irq);
381 if (err) {
382 pr_err("gpio_event_input_request_irqs: "
383 "enable_irq_wake failed for input %d, "
384 "irq %d\n",
385 ds->info->keymap[i].gpio, irq);
386 goto err_enable_irq_wake_failed;
387 }
388 }
389 }
Flemmardce3c2c32013-05-23 16:53:44 -0700390#ifdef CONFIG_TOUCHSCREEN_SYNAPTICS_3K
391 init_power_key_api();
392#endif
Arve Hjønnevågc3fffcb2008-10-15 18:23:47 -0700393 return 0;
394
395 for (i = ds->info->keymap_size - 1; i >= 0; i--) {
396 irq = gpio_to_irq(ds->info->keymap[i].gpio);
397 if (ds->info->info.no_suspend)
398 disable_irq_wake(irq);
399err_enable_irq_wake_failed:
400 free_irq(irq, &ds->key_state[i]);
401err_request_irq_failed:
402err_gpio_get_irq_num_failed:
403 ;
404 }
405 return err;
406}
407
408int gpio_event_input_func(struct gpio_event_input_devs *input_devs,
409 struct gpio_event_info *info, void **data, int func)
410{
411 int ret;
412 int i;
413 unsigned long irqflags;
414 struct gpio_event_input_info *di;
415 struct gpio_input_state *ds = *data;
Matt Mowere727d6c2013-10-17 03:36:09 -0500416#ifdef CONFIG_HTC_WAKE_ON_VOL
417 struct kobject *keyboard_kobj;
418#endif
Arve Hjønnevågc3fffcb2008-10-15 18:23:47 -0700419
420 di = container_of(info, struct gpio_event_input_info, info);
421
422 if (func == GPIO_EVENT_FUNC_SUSPEND) {
423 if (ds->use_irq)
424 for (i = 0; i < di->keymap_size; i++)
425 disable_irq(gpio_to_irq(di->keymap[i].gpio));
426 hrtimer_cancel(&ds->timer);
427 return 0;
428 }
429 if (func == GPIO_EVENT_FUNC_RESUME) {
430 spin_lock_irqsave(&ds->irq_lock, irqflags);
431 if (ds->use_irq)
432 for (i = 0; i < di->keymap_size; i++)
433 enable_irq(gpio_to_irq(di->keymap[i].gpio));
434 hrtimer_start(&ds->timer, ktime_set(0, 0), HRTIMER_MODE_REL);
435 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
436 return 0;
437 }
438
439 if (func == GPIO_EVENT_FUNC_INIT) {
440 if (ktime_to_ns(di->poll_time) <= 0)
441 di->poll_time = ktime_set(0, 20 * NSEC_PER_MSEC);
442
443 *data = ds = kzalloc(sizeof(*ds) + sizeof(ds->key_state[0]) *
444 di->keymap_size, GFP_KERNEL);
445 if (ds == NULL) {
446 ret = -ENOMEM;
447 pr_err("gpio_event_input_func: "
448 "Failed to allocate private data\n");
449 goto err_ds_alloc_failed;
450 }
451 ds->debounce_count = di->keymap_size;
452 ds->input_devs = input_devs;
453 ds->info = di;
454 wake_lock_init(&ds->wake_lock, WAKE_LOCK_SUSPEND, "gpio_input");
455 spin_lock_init(&ds->irq_lock);
456
457 for (i = 0; i < di->keymap_size; i++) {
458 int dev = di->keymap[i].dev;
459 if (dev >= input_devs->count) {
460 pr_err("gpio_event_input_func: bad device "
461 "index %d >= %d for key code %d\n",
462 dev, input_devs->count,
463 di->keymap[i].code);
464 ret = -EINVAL;
465 goto err_bad_keymap;
466 }
467 input_set_capability(input_devs->dev[dev], di->type,
468 di->keymap[i].code);
469 ds->key_state[i].ds = ds;
470 ds->key_state[i].debounce = DEBOUNCE_UNKNOWN;
471 }
472
473 for (i = 0; i < di->keymap_size; i++) {
474 ret = gpio_request(di->keymap[i].gpio, "gpio_kp_in");
475 if (ret) {
476 pr_err("gpio_event_input_func: gpio_request "
477 "failed for %d\n", di->keymap[i].gpio);
478 goto err_gpio_request_failed;
479 }
480 ret = gpio_direction_input(di->keymap[i].gpio);
481 if (ret) {
482 pr_err("gpio_event_input_func: "
483 "gpio_direction_input failed for %d\n",
484 di->keymap[i].gpio);
485 goto err_gpio_configure_failed;
486 }
487 }
488
489 ret = gpio_event_input_request_irqs(ds);
490
Matt Mowere727d6c2013-10-17 03:36:09 -0500491#ifdef CONFIG_HTC_WAKE_ON_VOL
492 keyboard_kobj = kobject_create_and_add("keyboard", NULL);
493 if (keyboard_kobj == NULL) {
494 printk(KERN_ERR "KEY_ERR: %s: subsystem_register failed\n", __func__);
495 ret = -ENOMEM;
496 return ret;
497 }
498 if (sysfs_create_file(keyboard_kobj, &dev_attr_vol_wakeup.attr))
499 printk(KERN_ERR "KEY_ERR: %s: sysfs_create_file "
500 "return %d\n", __func__, ret);
501 wakeup_bitmask = 0;
502 set_wakeup = 0;
503#endif
504
Arve Hjønnevågc3fffcb2008-10-15 18:23:47 -0700505 spin_lock_irqsave(&ds->irq_lock, irqflags);
506 ds->use_irq = ret == 0;
507
508 pr_info("GPIO Input Driver: Start gpio inputs for %s%s in %s "
509 "mode\n", input_devs->dev[0]->name,
510 (input_devs->count > 1) ? "..." : "",
511 ret == 0 ? "interrupt" : "polling");
512
513 hrtimer_init(&ds->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
514 ds->timer.function = gpio_event_input_timer_func;
515 hrtimer_start(&ds->timer, ktime_set(0, 0), HRTIMER_MODE_REL);
516 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
517 return 0;
518 }
519
520 ret = 0;
521 spin_lock_irqsave(&ds->irq_lock, irqflags);
522 hrtimer_cancel(&ds->timer);
523 if (ds->use_irq) {
524 for (i = di->keymap_size - 1; i >= 0; i--) {
525 int irq = gpio_to_irq(di->keymap[i].gpio);
526 if (ds->info->info.no_suspend)
527 disable_irq_wake(irq);
528 free_irq(irq, &ds->key_state[i]);
529 }
530 }
531 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
532
533 for (i = di->keymap_size - 1; i >= 0; i--) {
534err_gpio_configure_failed:
535 gpio_free(di->keymap[i].gpio);
536err_gpio_request_failed:
537 ;
538 }
539err_bad_keymap:
540 wake_lock_destroy(&ds->wake_lock);
541 kfree(ds);
542err_ds_alloc_failed:
543 return ret;
544}