2 * Driver for keys on GPIO lines capable of generating interrupts.
4 * Copyright 2005 Phil Blundell
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/module.h>
13 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/sysctl.h>
21 #include <linux/proc_fs.h>
22 #include <linux/delay.h>
23 #include <linux/platform_device.h>
24 #include <linux/input.h>
25 #include <linux/gpio_keys.h>
26 #include <linux/workqueue.h>
27 #include <linux/gpio.h>
29 struct gpio_button_data {
30 struct gpio_keys_button *button;
31 struct input_dev *input;
32 struct timer_list timer;
33 struct work_struct work;
34 int timer_debounce; /* in msecs */
39 struct gpio_keys_drvdata {
40 struct input_dev *input;
41 struct mutex disable_lock;
42 unsigned int n_buttons;
43 int (*enable)(struct device *dev);
44 void (*disable)(struct device *dev);
45 struct gpio_button_data data[0];
48 static struct input_dev *input_temp;
49 static int gpiokey_debug_flag;
51 void earjack_key_report(unsigned int keycode, int value)
53 input_event(input_temp, EV_KEY, keycode, value);
54 input_sync(input_temp);
55 if (gpiokey_debug_flag)
56 printk(KERN_INFO"[EARKEY] keypad : 0x%x, press : %d\n",
59 EXPORT_SYMBOL(earjack_key_report);
62 * SYSFS interface for enabling/disabling keys and switches:
64 * There are 4 attributes under /sys/devices/platform/gpio-keys/
65 * keys [ro] - bitmap of keys (EV_KEY) which can be
67 * switches [ro] - bitmap of switches (EV_SW) which can be
69 * disabled_keys [rw] - bitmap of keys currently disabled
70 * disabled_switches [rw] - bitmap of switches currently disabled
72 * Userland can change these values and hence disable event generation
73 * for each key (or switch). Disabling a key means its interrupt line
76 * For example, if we have following switches set up as gpio-keys:
78 * SW_CAMERA_LENS_COVER = 9
79 * SW_KEYPAD_SLIDE = 10
80 * SW_FRONT_PROXIMITY = 11
81 * This is read from switches:
83 * Next we want to disable proximity (11) and dock (5), we write:
85 * to file disabled_switches. Now proximity and dock IRQs are disabled.
86 * This can be verified by reading the file disabled_switches:
88 * If we now want to enable proximity (11) switch we write:
90 * to disabled_switches.
92 * We can disable only those keys which don't allow sharing the irq.
96 * get_n_events_by_type() - returns maximum number of events per @type
97 * @type: type of button (%EV_KEY, %EV_SW)
99 * Return value of this function can be used to allocate bitmap
100 * large enough to hold all bits for given type.
102 static inline int get_n_events_by_type(int type)
104 BUG_ON(type != EV_SW && type != EV_KEY);
106 return (type == EV_KEY) ? KEY_CNT : SW_CNT;
110 * gpio_keys_disable_button() - disables given GPIO button
111 * @bdata: button data for button to be disabled
113 * Disables button pointed by @bdata. This is done by masking
114 * IRQ line. After this function is called, button won't generate
115 * input events anymore. Note that one can only disable buttons
116 * that don't share IRQs.
118 * Make sure that @bdata->disable_lock is locked when entering
119 * this function to avoid races when concurrent threads are
120 * disabling buttons at the same time.
122 static void gpio_keys_disable_button(struct gpio_button_data *bdata)
124 if (!bdata->disabled) {
126 * Disable IRQ and possible debouncing timer.
128 disable_irq(gpio_to_irq(bdata->button->gpio));
129 if (bdata->timer_debounce)
130 del_timer_sync(&bdata->timer);
132 bdata->disabled = true;
137 * gpio_keys_enable_button() - enables given GPIO button
138 * @bdata: button data for button to be disabled
140 * Enables given button pointed by @bdata.
142 * Make sure that @bdata->disable_lock is locked when entering
143 * this function to avoid races with concurrent threads trying
144 * to enable the same button at the same time.
146 static void gpio_keys_enable_button(struct gpio_button_data *bdata)
148 if (bdata->disabled) {
149 enable_irq(gpio_to_irq(bdata->button->gpio));
150 bdata->disabled = false;
155 * gpio_keys_attr_show_helper() - fill in stringified bitmap of buttons
156 * @ddata: pointer to drvdata
157 * @buf: buffer where stringified bitmap is written
158 * @type: button type (%EV_KEY, %EV_SW)
159 * @only_disabled: does caller want only those buttons that are
160 * currently disabled or all buttons that can be
163 * This function writes buttons that can be disabled to @buf. If
164 * @only_disabled is true, then @buf contains only those buttons
165 * that are currently disabled. Returns 0 on success or negative
168 static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
169 char *buf, unsigned int type,
172 int n_events = get_n_events_by_type(type);
177 bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
181 for (i = 0; i < ddata->n_buttons; i++) {
182 struct gpio_button_data *bdata = &ddata->data[i];
184 if (bdata->button->type != type)
187 if (only_disabled && !bdata->disabled)
190 __set_bit(bdata->button->code, bits);
193 ret = bitmap_scnlistprintf(buf, PAGE_SIZE - 2, bits, n_events);
203 * gpio_keys_attr_store_helper() - enable/disable buttons based on given bitmap
204 * @ddata: pointer to drvdata
205 * @buf: buffer from userspace that contains stringified bitmap
206 * @type: button type (%EV_KEY, %EV_SW)
208 * This function parses stringified bitmap from @buf and disables/enables
209 * GPIO buttons accordinly. Returns 0 on success and negative error
212 static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
213 const char *buf, unsigned int type)
215 int n_events = get_n_events_by_type(type);
220 bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
224 error = bitmap_parselist(buf, bits, n_events);
229 for (i = 0; i < ddata->n_buttons; i++) {
230 struct gpio_button_data *bdata = &ddata->data[i];
232 if (bdata->button->type != type)
235 if (test_bit(bdata->button->code, bits) &&
236 !bdata->button->can_disable) {
242 mutex_lock(&ddata->disable_lock);
244 for (i = 0; i < ddata->n_buttons; i++) {
245 struct gpio_button_data *bdata = &ddata->data[i];
247 if (bdata->button->type != type)
250 if (test_bit(bdata->button->code, bits))
251 gpio_keys_disable_button(bdata);
253 gpio_keys_enable_button(bdata);
256 mutex_unlock(&ddata->disable_lock);
263 #define ATTR_SHOW_FN(name, type, only_disabled) \
264 static ssize_t gpio_keys_show_##name(struct device *dev, \
265 struct device_attribute *attr, \
268 struct platform_device *pdev = to_platform_device(dev); \
269 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
271 return gpio_keys_attr_show_helper(ddata, buf, \
272 type, only_disabled); \
275 ATTR_SHOW_FN(keys, EV_KEY, false);
276 ATTR_SHOW_FN(switches, EV_SW, false);
277 ATTR_SHOW_FN(disabled_keys, EV_KEY, true);
278 ATTR_SHOW_FN(disabled_switches, EV_SW, true);
283 * /sys/devices/platform/gpio-keys.0/keys [ro]
284 * /sys/devices/platform/gpio-keys.0/switches [ro]
285 * /sys/devices/platform/gpio-keys.0/key_pressed [ro]
287 static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL);
288 static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL);
290 #define ATTR_STORE_FN(name, type) \
291 static ssize_t gpio_keys_store_##name(struct device *dev, \
292 struct device_attribute *attr, \
296 struct platform_device *pdev = to_platform_device(dev); \
297 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
300 error = gpio_keys_attr_store_helper(ddata, buf, type); \
307 ATTR_STORE_FN(disabled_keys, EV_KEY);
308 ATTR_STORE_FN(disabled_switches, EV_SW);
313 * /sys/devices/platform/gpio-keys/disabled_keys [rw]
314 * /sys/devices/platform/gpio-keys/disables_switches [rw]
316 static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO,
317 gpio_keys_show_disabled_keys,
318 gpio_keys_store_disabled_keys);
319 static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO,
320 gpio_keys_show_disabled_switches,
321 gpio_keys_store_disabled_switches);
323 static ssize_t key_pressed_show(struct device *dev,
324 struct device_attribute *attr, char *buf)
326 struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
330 for (i = 0; i < ddata->n_buttons; i++) {
331 struct gpio_button_data *bdata = &ddata->data[i];
332 keystate |= bdata->key_state;
336 sprintf(buf, "PRESS");
338 sprintf(buf, "RELEASE");
343 static DEVICE_ATTR(key_pressed, 0664, key_pressed_show, NULL);
345 static ssize_t sysfs_debug_store(struct device *dev,
346 struct device_attribute *attr, const char *buf, size_t count)
350 sscanf(buf, "%d", &type);
353 printk(KERN_INFO"gpio-keys : disable gpio key log\n");
354 gpiokey_debug_flag = 0;
355 } else if (type == 1) {
356 printk(KERN_INFO"gpio-keys : enable gpio key log\n");
357 gpiokey_debug_flag = 1;
362 static ssize_t sysfs_debug_show(struct device *dev,
363 struct device_attribute *attr, char *buf)
365 printk(KERN_INFO"gpio-keys : gpiokey_debug_flag = %d\n",
371 static DEVICE_ATTR(debug, 0664, sysfs_debug_show, sysfs_debug_store);
373 static struct attribute *gpio_keys_attrs[] = {
375 &dev_attr_switches.attr,
376 &dev_attr_key_pressed.attr,
377 &dev_attr_disabled_keys.attr,
378 &dev_attr_disabled_switches.attr,
379 &dev_attr_debug.attr,
383 static struct attribute_group gpio_keys_attr_group = {
384 .attrs = gpio_keys_attrs,
387 static void gpio_keys_report_event(struct gpio_button_data *bdata)
389 struct gpio_keys_button *button = bdata->button;
390 struct input_dev *input = bdata->input;
391 unsigned int type = button->type ?: EV_KEY;
392 struct irq_desc *desc = irq_to_desc(gpio_to_irq(button->gpio));
393 int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
395 bdata->key_state = !!state;
396 input_event(input, type, button->code,
397 (desc->status & IRQ_WAKEUP) ? 1 : !!state);
401 static void gpio_keys_work_func(struct work_struct *work)
403 struct gpio_button_data *bdata =
404 container_of(work, struct gpio_button_data, work);
406 gpio_keys_report_event(bdata);
409 static void gpio_keys_timer(unsigned long _data)
411 struct gpio_button_data *data = (struct gpio_button_data *)_data;
413 schedule_work(&data->work);
416 /* YUBAEK 20110905 RAMDUMP_DEBUG : Android forceable ramdump ++ */
417 #ifdef CONFIG_INPUT_FORCE_PANIC_KEY
418 int get_panic_reset_state(void);
419 void sec_debug_check_crash_key(unsigned int code, int value)
421 static enum { NONE, HOME_DOWN } state = NONE;
422 static unsigned long home_down_jiffies;
424 #ifdef CONFIG_SAMSUNG_WATCHDOG_RESET_BOOT
425 /* YUBAEK 20110906 RAMDUMP_DEBUG : forceable panic on/off ++ */
426 if (get_panic_reset_state() == 0)
430 /* YUBAEK 20110906 RAMDUMP_DEBUG : forceable panic on/off ++ */
432 pr_info("%s: %d %d\n", __func__, code, value);
434 if (code == KEY_MENU) {
437 home_down_jiffies = jiffies;
440 } else if (code == KEY_VOLUMEUP) {
442 if (state == HOME_DOWN) {
443 pr_err("%s: %u msec after home down\n",
445 jiffies_to_msecs(jiffies -
447 panic(FORCIBLE_CRASH_KEY_STING);
449 /* else do nothing */
455 #endif /* CONFIG_INPUT_FORCE_PANIC_KEY */
456 /* YUBAEK 20110905 RAMDUMP_DEBUG : Android forceable ramdump -- */
459 static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
461 struct gpio_button_data *bdata = dev_id;
462 struct gpio_keys_button *button = bdata->button;
464 BUG_ON(irq != gpio_to_irq(button->gpio));
466 if (bdata->timer_debounce)
467 mod_timer(&bdata->timer,
468 jiffies + msecs_to_jiffies(bdata->timer_debounce));
470 schedule_work(&bdata->work);
472 /* YUBAEK 20110905 RAMDUMP_DEBUG : Android forceable ramdump ++ */
473 #ifdef CONFIG_INPUT_FORCE_PANIC_KEY
474 if (button->isr_hook) {
476 (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
477 button->isr_hook(button->code, state);
479 #endif /* CONFIG_INPUT_FORCE_PANIC_KEY */
480 /* YUBAEK 20110905 RAMDUMP_DEBUG : Android forceable ramdump ++ */
486 static int __devinit gpio_keys_setup_key(struct platform_device *pdev,
487 struct gpio_button_data *bdata,
488 struct gpio_keys_button *button)
490 char *desc = button->desc ? button->desc : "gpio_keys";
491 struct device *dev = &pdev->dev;
492 unsigned long irqflags;
495 setup_timer(&bdata->timer, gpio_keys_timer, (unsigned long)bdata);
496 INIT_WORK(&bdata->work, gpio_keys_work_func);
498 error = gpio_request(button->gpio, desc);
500 dev_err(dev, "failed to request GPIO %d, error %d\n",
501 button->gpio, error);
505 error = gpio_direction_input(button->gpio);
507 dev_err(dev, "failed to configure"
508 " direction for GPIO %d, error %d\n",
509 button->gpio, error);
513 if (button->debounce_interval) {
514 error = gpio_set_debounce(button->gpio,
515 button->debounce_interval * 1000);
516 /* use timer if gpiolib doesn't provide debounce */
518 bdata->timer_debounce = button->debounce_interval;
521 irq = gpio_to_irq(button->gpio);
524 dev_err(dev, "Unable to get irq number for GPIO %d, error %d\n",
525 button->gpio, error);
529 irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
532 * If platform has specified that the button wakes up the system,
533 * we want the interrupt handler to be invoked in the
534 * suspend context as well.
537 irqflags |= IRQF_NO_SUSPEND;
540 * If platform has specified that the button can be disabled,
541 * we don't want it to share the interrupt line.
543 if (!button->can_disable)
544 irqflags |= IRQF_SHARED;
546 error = request_irq(irq, gpio_keys_isr, irqflags, desc, bdata);
548 dev_err(dev, "Unable to claim irq %d; error %d\n",
556 gpio_free(button->gpio);
561 static int gpio_keys_open(struct input_dev *input)
563 struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
565 return ddata->enable ? ddata->enable(input->dev.parent) : 0;
568 static void gpio_keys_close(struct input_dev *input)
570 struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
573 ddata->disable(input->dev.parent);
576 static int __devinit gpio_keys_probe(struct platform_device *pdev)
578 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
579 struct gpio_keys_drvdata *ddata;
580 struct device *dev = &pdev->dev;
581 struct input_dev *input;
585 ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
586 pdata->nbuttons * sizeof(struct gpio_button_data),
588 input = input_allocate_device();
589 if (!ddata || !input) {
590 dev_err(dev, "failed to allocate state\n");
595 ddata->input = input;
596 ddata->n_buttons = pdata->nbuttons;
597 ddata->enable = pdata->enable;
598 ddata->disable = pdata->disable;
599 mutex_init(&ddata->disable_lock);
601 platform_set_drvdata(pdev, ddata);
602 input_set_drvdata(input, ddata);
604 input->name = pdev->name;
605 input->phys = "gpio-keys/input0";
606 input->dev.parent = &pdev->dev;
607 input->open = gpio_keys_open;
608 input->close = gpio_keys_close;
610 input->id.bustype = BUS_HOST;
611 input->id.vendor = 0x0001;
612 input->id.product = 0x0001;
613 input->id.version = 0x0100;
615 /* Enable auto repeat feature of Linux input subsystem */
617 __set_bit(EV_REP, input->evbit);
619 for (i = 0; i < pdata->nbuttons; i++) {
620 struct gpio_keys_button *button = &pdata->buttons[i];
621 struct gpio_button_data *bdata = &ddata->data[i];
622 unsigned int type = button->type ?: EV_KEY;
624 bdata->input = input;
625 bdata->button = button;
627 error = gpio_keys_setup_key(pdev, bdata, button);
634 input_set_capability(input, type, button->code);
637 /* For earkey working */
638 input_set_capability(input, EV_KEY, KEY_SEND);
640 error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group);
642 dev_err(dev, "Unable to export keys/switches, error: %d\n",
647 error = input_register_device(input);
649 dev_err(dev, "Unable to register input device, error: %d\n",
654 /* Just for misc key such as earkey */
657 /* get current state of buttons */
658 for (i = 0; i < pdata->nbuttons; i++)
659 gpio_keys_report_event(&ddata->data[i]);
662 device_init_wakeup(&pdev->dev, wakeup);
667 sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
670 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
671 if (ddata->data[i].timer_debounce)
672 del_timer_sync(&ddata->data[i].timer);
673 cancel_work_sync(&ddata->data[i].work);
674 gpio_free(pdata->buttons[i].gpio);
677 platform_set_drvdata(pdev, NULL);
679 input_free_device(input);
685 static int __devexit gpio_keys_remove(struct platform_device *pdev)
687 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
688 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
689 struct input_dev *input = ddata->input;
692 sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
694 device_init_wakeup(&pdev->dev, 0);
696 for (i = 0; i < pdata->nbuttons; i++) {
697 int irq = gpio_to_irq(pdata->buttons[i].gpio);
698 free_irq(irq, &ddata->data[i]);
699 if (ddata->data[i].timer_debounce)
700 del_timer_sync(&ddata->data[i].timer);
701 cancel_work_sync(&ddata->data[i].work);
702 gpio_free(pdata->buttons[i].gpio);
705 input_unregister_device(input);
712 static int gpio_keys_suspend(struct device *dev)
714 struct platform_device *pdev = to_platform_device(dev);
715 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
718 if (device_may_wakeup(&pdev->dev)) {
719 for (i = 0; i < pdata->nbuttons; i++) {
720 struct gpio_keys_button *button = &pdata->buttons[i];
721 if (button->wakeup) {
722 int irq = gpio_to_irq(button->gpio);
723 enable_irq_wake(irq);
731 static int gpio_keys_resume(struct device *dev)
733 struct platform_device *pdev = to_platform_device(dev);
734 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
735 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
738 for (i = 0; i < pdata->nbuttons; i++) {
740 struct gpio_keys_button *button = &pdata->buttons[i];
741 if (button->wakeup && device_may_wakeup(&pdev->dev)) {
742 int irq = gpio_to_irq(button->gpio);
743 disable_irq_wake(irq);
746 gpio_keys_report_event(&ddata->data[i]);
748 input_sync(ddata->input);
753 static const struct dev_pm_ops gpio_keys_pm_ops = {
754 .suspend = gpio_keys_suspend,
755 .resume = gpio_keys_resume,
759 static struct platform_driver gpio_keys_device_driver = {
760 .probe = gpio_keys_probe,
761 .remove = __devexit_p(gpio_keys_remove),
764 .owner = THIS_MODULE,
766 .pm = &gpio_keys_pm_ops,
771 static int __init gpio_keys_init(void)
773 return platform_driver_register(&gpio_keys_device_driver);
776 static void __exit gpio_keys_exit(void)
778 platform_driver_unregister(&gpio_keys_device_driver);
781 module_init(gpio_keys_init);
782 module_exit(gpio_keys_exit);
784 MODULE_LICENSE("GPL");
785 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
786 MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
787 MODULE_ALIAS("platform:gpio-keys");