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>
28 #include <linux/irqdesc.h>
30 extern struct class *sec_class;
32 struct gpio_button_data {
33 struct gpio_keys_button *button;
34 struct input_dev *input;
35 struct timer_list timer;
36 struct work_struct work;
37 int timer_debounce; /* in msecs */
43 struct gpio_keys_drvdata {
44 struct input_dev *input;
45 struct device *sec_key;
46 struct mutex disable_lock;
47 unsigned int n_buttons;
48 int (*enable)(struct device *dev);
49 void (*disable)(struct device *dev);
50 struct gpio_button_data data[0];
51 /* WARNING: this area can be expanded. Do NOT add any member! */
54 #if defined(CONFIG_MACH_GRANDE) || defined(CONFIG_MACH_IRON)
55 extern int Is_folder_state(void);
58 * SYSFS interface for enabling/disabling keys and switches:
60 * There are 4 attributes under /sys/devices/platform/gpio-keys/
61 * keys [ro] - bitmap of keys (EV_KEY) which can be
63 * switches [ro] - bitmap of switches (EV_SW) which can be
65 * disabled_keys [rw] - bitmap of keys currently disabled
66 * disabled_switches [rw] - bitmap of switches currently disabled
68 * Userland can change these values and hence disable event generation
69 * for each key (or switch). Disabling a key means its interrupt line
72 * For example, if we have following switches set up as gpio-keys:
74 * SW_CAMERA_LENS_COVER = 9
75 * SW_KEYPAD_SLIDE = 10
76 * SW_FRONT_PROXIMITY = 11
77 * This is read from switches:
79 * Next we want to disable proximity (11) and dock (5), we write:
81 * to file disabled_switches. Now proximity and dock IRQs are disabled.
82 * This can be verified by reading the file disabled_switches:
84 * If we now want to enable proximity (11) switch we write:
86 * to disabled_switches.
88 * We can disable only those keys which don't allow sharing the irq.
92 * get_n_events_by_type() - returns maximum number of events per @type
93 * @type: type of button (%EV_KEY, %EV_SW)
95 * Return value of this function can be used to allocate bitmap
96 * large enough to hold all bits for given type.
98 static inline int get_n_events_by_type(int type)
100 BUG_ON(type != EV_SW && type != EV_KEY);
102 return (type == EV_KEY) ? KEY_CNT : SW_CNT;
106 * gpio_keys_disable_button() - disables given GPIO button
107 * @bdata: button data for button to be disabled
109 * Disables button pointed by @bdata. This is done by masking
110 * IRQ line. After this function is called, button won't generate
111 * input events anymore. Note that one can only disable buttons
112 * that don't share IRQs.
114 * Make sure that @bdata->disable_lock is locked when entering
115 * this function to avoid races when concurrent threads are
116 * disabling buttons at the same time.
118 static void gpio_keys_disable_button(struct gpio_button_data *bdata)
120 if (!bdata->disabled) {
122 * Disable IRQ and possible debouncing timer.
124 disable_irq(gpio_to_irq(bdata->button->gpio));
125 if (bdata->timer_debounce)
126 del_timer_sync(&bdata->timer);
128 bdata->disabled = true;
133 * gpio_keys_enable_button() - enables given GPIO button
134 * @bdata: button data for button to be disabled
136 * Enables given button pointed by @bdata.
138 * Make sure that @bdata->disable_lock is locked when entering
139 * this function to avoid races with concurrent threads trying
140 * to enable the same button at the same time.
142 static void gpio_keys_enable_button(struct gpio_button_data *bdata)
144 if (bdata->disabled) {
145 enable_irq(gpio_to_irq(bdata->button->gpio));
146 bdata->disabled = false;
151 * gpio_keys_attr_show_helper() - fill in stringified bitmap of buttons
152 * @ddata: pointer to drvdata
153 * @buf: buffer where stringified bitmap is written
154 * @type: button type (%EV_KEY, %EV_SW)
155 * @only_disabled: does caller want only those buttons that are
156 * currently disabled or all buttons that can be
159 * This function writes buttons that can be disabled to @buf. If
160 * @only_disabled is true, then @buf contains only those buttons
161 * that are currently disabled. Returns 0 on success or negative
164 static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
165 char *buf, unsigned int type,
168 int n_events = get_n_events_by_type(type);
173 bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
177 for (i = 0; i < ddata->n_buttons; i++) {
178 struct gpio_button_data *bdata = &ddata->data[i];
180 if (bdata->button->type != type)
183 if (only_disabled && !bdata->disabled)
186 __set_bit(bdata->button->code, bits);
189 ret = bitmap_scnlistprintf(buf, PAGE_SIZE - 2, bits, n_events);
199 * gpio_keys_attr_store_helper() - enable/disable buttons based on given bitmap
200 * @ddata: pointer to drvdata
201 * @buf: buffer from userspace that contains stringified bitmap
202 * @type: button type (%EV_KEY, %EV_SW)
204 * This function parses stringified bitmap from @buf and disables/enables
205 * GPIO buttons accordinly. Returns 0 on success and negative error
208 static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
209 const char *buf, unsigned int type)
211 int n_events = get_n_events_by_type(type);
216 bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
220 error = bitmap_parselist(buf, bits, n_events);
225 for (i = 0; i < ddata->n_buttons; i++) {
226 struct gpio_button_data *bdata = &ddata->data[i];
228 if (bdata->button->type != type)
231 if (test_bit(bdata->button->code, bits) &&
232 !bdata->button->can_disable) {
238 mutex_lock(&ddata->disable_lock);
240 for (i = 0; i < ddata->n_buttons; i++) {
241 struct gpio_button_data *bdata = &ddata->data[i];
243 if (bdata->button->type != type)
246 if (test_bit(bdata->button->code, bits))
247 gpio_keys_disable_button(bdata);
249 gpio_keys_enable_button(bdata);
252 mutex_unlock(&ddata->disable_lock);
259 #define ATTR_SHOW_FN(name, type, only_disabled) \
260 static ssize_t gpio_keys_show_##name(struct device *dev, \
261 struct device_attribute *attr, \
264 struct platform_device *pdev = to_platform_device(dev); \
265 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
267 return gpio_keys_attr_show_helper(ddata, buf, \
268 type, only_disabled); \
271 ATTR_SHOW_FN(keys, EV_KEY, false);
272 ATTR_SHOW_FN(switches, EV_SW, false);
273 ATTR_SHOW_FN(disabled_keys, EV_KEY, true);
274 ATTR_SHOW_FN(disabled_switches, EV_SW, true);
276 #ifdef CONFIG_SLP_WAKEUP_COUNT
277 static int gpio_key_wakeup_irq;
282 * /sys/devices/platform/gpio-keys/keys [ro]
283 * /sys/devices/platform/gpio-keys/switches [ro]
285 static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL);
286 static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL);
288 #define ATTR_STORE_FN(name, type) \
289 static ssize_t gpio_keys_store_##name(struct device *dev, \
290 struct device_attribute *attr, \
294 struct platform_device *pdev = to_platform_device(dev); \
295 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
298 error = gpio_keys_attr_store_helper(ddata, buf, type); \
305 ATTR_STORE_FN(disabled_keys, EV_KEY);
306 ATTR_STORE_FN(disabled_switches, EV_SW);
311 * /sys/devices/platform/gpio-keys/disabled_keys [rw]
312 * /sys/devices/platform/gpio-keys/disables_switches [rw]
314 static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO,
315 gpio_keys_show_disabled_keys,
316 gpio_keys_store_disabled_keys);
317 static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO,
318 gpio_keys_show_disabled_switches,
319 gpio_keys_store_disabled_switches);
321 static struct attribute *gpio_keys_attrs[] = {
323 &dev_attr_switches.attr,
324 &dev_attr_disabled_keys.attr,
325 &dev_attr_disabled_switches.attr,
329 static struct attribute_group gpio_keys_attr_group = {
330 .attrs = gpio_keys_attrs,
333 static ssize_t key_pressed_show(struct device *dev,
334 struct device_attribute *attr, char *buf)
336 struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
340 for (i = 0; i < ddata->n_buttons; i++) {
341 struct gpio_button_data *bdata = &ddata->data[i];
342 keystate |= bdata->key_state;
346 sprintf(buf, "PRESS");
348 sprintf(buf, "RELEASE");
353 /* the volume keys can be the wakeup keys in special case */
354 static ssize_t wakeup_enable(struct device *dev,
355 struct device_attribute *attr, const char *buf, size_t count)
357 struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
358 int n_events = get_n_events_by_type(EV_KEY);
363 bits = kcalloc(BITS_TO_LONGS(n_events),
364 sizeof(*bits), GFP_KERNEL);
368 error = bitmap_parselist(buf, bits, n_events);
372 for (i = 0; i < ddata->n_buttons; i++) {
373 struct gpio_button_data *button = &ddata->data[i];
374 if (test_bit(button->button->code, bits))
375 button->button->wakeup = 1;
377 button->button->wakeup = 0;
378 #if defined(CONFIG_MACH_GRANDE) || defined(CONFIG_MACH_IRON)
379 printk(KERN_DEBUG "Code %d , wakeup bit %d\n", \
380 button->button->code, button->button->wakeup);
389 static DEVICE_ATTR(sec_key_pressed, 0664, key_pressed_show, NULL);
390 static DEVICE_ATTR(wakeup_keys, 0664, NULL, wakeup_enable);
392 static struct attribute *sec_key_attrs[] = {
393 &dev_attr_sec_key_pressed.attr,
394 &dev_attr_wakeup_keys.attr,
398 static struct attribute_group sec_key_attr_group = {
399 .attrs = sec_key_attrs,
402 #ifdef CONFIG_MACH_GC1
403 void gpio_keys_check_zoom_exception(unsigned int code,
404 bool *zoomkey, unsigned int *hotkey, unsigned int *index)
407 case KEY_CAMERA_ZOOMIN:
411 case KEY_CAMERA_ZOOMOUT:
416 *hotkey = KEY_CAMERA_ZOOMIN;
420 *hotkey = KEY_CAMERA_ZOOMOUT;
431 static void gpio_keys_report_event(struct gpio_button_data *bdata)
433 struct gpio_keys_button *button = bdata->button;
434 struct input_dev *input = bdata->input;
435 unsigned int type = button->type ?: EV_KEY;
436 int state = (gpio_get_value_cansleep(button->gpio) ? 1 : 0)
437 ^ button->active_low;
438 #ifdef CONFIG_MACH_GC1
439 struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
440 struct gpio_button_data *tmp_bdata;
441 static bool overlapped;
442 static unsigned int hotkey;
443 unsigned int index_hotkey = 0;
444 bool zoomkey = false;
446 if (system_rev < 6 && system_rev >= 2) {
448 if (hotkey == button->code && !state) {
449 bdata->key_state = !!state;
450 bdata->wakeup = false;
452 #ifdef CONFIG_SAMSUNG_PRODUCT_SHIP
453 printk(KERN_DEBUG"[KEYS] Ignored\n");
455 printk(KERN_DEBUG"[KEYS] Ignore %d %d\n",
462 gpio_keys_check_zoom_exception(button->code, &zoomkey,
463 &hotkey, &index_hotkey);
467 if (type == EV_ABS) {
469 input_event(input, type, button->code, button->value);
472 #if defined(CONFIG_MACH_GRANDE) || defined(CONFIG_MACH_IRON)
473 if(Is_folder_state() && (button->code == KEY_END))
475 printk(KERN_DEBUG"[keys] not report PWR %d : Folder is closed\n", !state);
480 if (bdata->wakeup && !state) {
481 input_event(input, type, button->code, !state);
482 if (button->code == KEY_POWER)
483 printk(KERN_DEBUG"[keys] f PWR %d\n", !state);
486 bdata->key_state = !!state;
487 bdata->wakeup = false;
490 #ifdef CONFIG_MACH_GC1
491 if (system_rev < 6 && system_rev >= 2
492 && zoomkey && state) {
493 tmp_bdata = &ddata->data[index_hotkey];
495 if (tmp_bdata->key_state) {
496 #ifdef CONFIG_SAMSUNG_PRODUCT_SHIP
497 printk(KERN_DEBUG"[KEYS] overlapped\n");
499 printk(KERN_DEBUG"[KEYS] overlapped. Forced release c %d h %d\n",
500 tmp_bdata->button->code, hotkey);
502 input_event(input, type, hotkey, 0);
507 input_event(input, type, button->code, !!state);
508 if (button->code == KEY_POWER)
509 printk(KERN_DEBUG"[keys]PWR %d\n", !!state);
515 static void gpio_keys_work_func(struct work_struct *work)
517 struct gpio_button_data *bdata =
518 container_of(work, struct gpio_button_data, work);
520 gpio_keys_report_event(bdata);
523 static void gpio_keys_timer(unsigned long _data)
525 struct gpio_button_data *data = (struct gpio_button_data *)_data;
527 schedule_work(&data->work);
530 static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
532 struct gpio_button_data *bdata = dev_id;
533 struct gpio_keys_button *button = bdata->button;
534 struct irq_desc *desc = irq_to_desc(irq);
535 int state = (gpio_get_value_cansleep(button->gpio) ? 1 : 0)
536 ^ button->active_low;
538 BUG_ON(irq != gpio_to_irq(button->gpio));
541 if (0 < desc->wake_depth) {
542 bdata->wakeup = true;
543 printk(KERN_DEBUG"[keys] in the sleep\n");
547 if (bdata->timer_debounce)
548 mod_timer(&bdata->timer,
549 jiffies + msecs_to_jiffies(bdata->timer_debounce));
551 schedule_work(&bdata->work);
553 if (button->isr_hook)
554 button->isr_hook(button->code, state);
559 static int __devinit gpio_keys_setup_key(struct platform_device *pdev,
560 struct gpio_button_data *bdata,
561 struct gpio_keys_button *button)
563 const char *desc = button->desc ? button->desc : "gpio_keys";
564 struct device *dev = &pdev->dev;
565 unsigned long irqflags;
568 setup_timer(&bdata->timer, gpio_keys_timer, (unsigned long)bdata);
569 INIT_WORK(&bdata->work, gpio_keys_work_func);
571 error = gpio_request(button->gpio, desc);
573 dev_err(dev, "failed to request GPIO %d, error %d\n",
574 button->gpio, error);
578 error = gpio_direction_input(button->gpio);
580 dev_err(dev, "failed to configure"
581 " direction for GPIO %d, error %d\n",
582 button->gpio, error);
586 if (button->debounce_interval) {
587 error = gpio_set_debounce(button->gpio,
588 button->debounce_interval * 1000);
589 /* use timer if gpiolib doesn't provide debounce */
591 bdata->timer_debounce = button->debounce_interval;
594 irq = gpio_to_irq(button->gpio);
595 #ifdef CONFIG_SLP_WAKEUP_COUNT
596 gpio_key_wakeup_irq = irq;
600 dev_err(dev, "Unable to get irq number for GPIO %d, error %d\n",
601 button->gpio, error);
605 irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
607 * If platform has specified that the button can be disabled,
608 * we don't want it to share the interrupt line.
610 if (!button->can_disable)
611 irqflags |= IRQF_SHARED;
614 irqflags |= IRQF_NO_SUSPEND;
616 error = request_any_context_irq(irq, gpio_keys_isr, irqflags, desc, bdata);
618 dev_err(dev, "Unable to claim irq %d; error %d\n",
626 gpio_free(button->gpio);
631 static int gpio_keys_open(struct input_dev *input)
633 struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
635 return ddata->enable ? ddata->enable(input->dev.parent) : 0;
638 static void gpio_keys_close(struct input_dev *input)
640 struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
643 ddata->disable(input->dev.parent);
646 static int __devinit gpio_keys_probe(struct platform_device *pdev)
648 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
649 struct gpio_keys_drvdata *ddata;
650 struct device *dev = &pdev->dev;
651 struct input_dev *input;
655 ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
656 pdata->nbuttons * sizeof(struct gpio_button_data),
658 input = input_allocate_device();
659 if (!ddata || !input) {
660 dev_err(dev, "failed to allocate state\n");
665 ddata->input = input;
666 ddata->n_buttons = pdata->nbuttons;
667 ddata->enable = pdata->enable;
668 ddata->disable = pdata->disable;
669 mutex_init(&ddata->disable_lock);
671 platform_set_drvdata(pdev, ddata);
672 input_set_drvdata(input, ddata);
674 input->name = pdata->name ? : pdev->name;
675 input->phys = "gpio-keys/input0";
676 input->dev.parent = &pdev->dev;
677 input->open = gpio_keys_open;
678 input->close = gpio_keys_close;
680 input->id.bustype = BUS_HOST;
681 input->id.vendor = 0x0001;
682 input->id.product = 0x0001;
683 input->id.version = 0x0100;
685 /* Enable auto repeat feature of Linux input subsystem */
687 __set_bit(EV_REP, input->evbit);
689 for (i = 0; i < pdata->nbuttons; i++) {
690 struct gpio_keys_button *button = &pdata->buttons[i];
691 struct gpio_button_data *bdata = &ddata->data[i];
692 unsigned int type = button->type ?: EV_KEY;
694 bdata->input = input;
695 bdata->button = button;
697 error = gpio_keys_setup_key(pdev, bdata, button);
704 input_set_capability(input, type, button->code);
707 error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group);
709 dev_err(dev, "Unable to export keys/switches, error: %d\n",
715 device_create(sec_class, NULL, 0, ddata, "sec_key");
716 if (IS_ERR(ddata->sec_key))
717 dev_err(dev, "Failed to create sec_key device\n");
719 error = sysfs_create_group(&ddata->sec_key->kobj, &sec_key_attr_group);
721 dev_err(dev, "Failed to create the test sysfs: %d\n",
726 error = input_register_device(input);
728 dev_err(dev, "Unable to register input device, error: %d\n",
733 /* get current state of buttons */
734 for (i = 0; i < pdata->nbuttons; i++)
735 gpio_keys_report_event(&ddata->data[i]);
737 #ifdef CONFIG_SLP_WAKEUP_COUNT
738 device_init_wakeup_setirq(&pdev->dev, gpio_key_wakeup_irq);
740 device_init_wakeup(&pdev->dev, wakeup);
746 sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
747 sysfs_remove_group(&ddata->sec_key->kobj, &sec_key_attr_group);
750 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
751 if (ddata->data[i].timer_debounce)
752 del_timer_sync(&ddata->data[i].timer);
753 cancel_work_sync(&ddata->data[i].work);
754 gpio_free(pdata->buttons[i].gpio);
757 platform_set_drvdata(pdev, NULL);
759 input_free_device(input);
765 static int __devexit gpio_keys_remove(struct platform_device *pdev)
767 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
768 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
769 struct input_dev *input = ddata->input;
772 sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
774 device_init_wakeup(&pdev->dev, 0);
776 for (i = 0; i < pdata->nbuttons; i++) {
777 int irq = gpio_to_irq(pdata->buttons[i].gpio);
778 free_irq(irq, &ddata->data[i]);
779 if (ddata->data[i].timer_debounce)
780 del_timer_sync(&ddata->data[i].timer);
781 cancel_work_sync(&ddata->data[i].work);
782 gpio_free(pdata->buttons[i].gpio);
785 input_unregister_device(input);
792 static int gpio_keys_suspend(struct device *dev)
794 struct platform_device *pdev = to_platform_device(dev);
795 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
798 if (device_may_wakeup(&pdev->dev)) {
799 for (i = 0; i < pdata->nbuttons; i++) {
800 struct gpio_keys_button *button = &pdata->buttons[i];
801 if (button->wakeup) {
802 int irq = gpio_to_irq(button->gpio);
803 enable_irq_wake(irq);
811 static int gpio_keys_resume(struct device *dev)
813 struct platform_device *pdev = to_platform_device(dev);
814 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
815 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
818 for (i = 0; i < pdata->nbuttons; i++) {
820 struct gpio_keys_button *button = &pdata->buttons[i];
821 if (button->wakeup && device_may_wakeup(&pdev->dev)) {
822 int irq = gpio_to_irq(button->gpio);
823 disable_irq_wake(irq);
826 gpio_keys_report_event(&ddata->data[i]);
828 input_sync(ddata->input);
833 static const struct dev_pm_ops gpio_keys_pm_ops = {
834 .suspend = gpio_keys_suspend,
835 .resume = gpio_keys_resume,
839 static struct platform_driver gpio_keys_device_driver = {
840 .probe = gpio_keys_probe,
841 .remove = __devexit_p(gpio_keys_remove),
844 .owner = THIS_MODULE,
846 .pm = &gpio_keys_pm_ops,
851 static int __init gpio_keys_init(void)
853 return platform_driver_register(&gpio_keys_device_driver);
856 static void __exit gpio_keys_exit(void)
858 platform_driver_unregister(&gpio_keys_device_driver);
861 module_init(gpio_keys_init);
862 module_exit(gpio_keys_exit);
864 MODULE_LICENSE("GPL");
865 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
866 MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
867 MODULE_ALIAS("platform:gpio-keys");