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! */
55 * SYSFS interface for enabling/disabling keys and switches:
57 * There are 4 attributes under /sys/devices/platform/gpio-keys/
58 * keys [ro] - bitmap of keys (EV_KEY) which can be
60 * switches [ro] - bitmap of switches (EV_SW) which can be
62 * disabled_keys [rw] - bitmap of keys currently disabled
63 * disabled_switches [rw] - bitmap of switches currently disabled
65 * Userland can change these values and hence disable event generation
66 * for each key (or switch). Disabling a key means its interrupt line
69 * For example, if we have following switches set up as gpio-keys:
71 * SW_CAMERA_LENS_COVER = 9
72 * SW_KEYPAD_SLIDE = 10
73 * SW_FRONT_PROXIMITY = 11
74 * This is read from switches:
76 * Next we want to disable proximity (11) and dock (5), we write:
78 * to file disabled_switches. Now proximity and dock IRQs are disabled.
79 * This can be verified by reading the file disabled_switches:
81 * If we now want to enable proximity (11) switch we write:
83 * to disabled_switches.
85 * We can disable only those keys which don't allow sharing the irq.
89 * get_n_events_by_type() - returns maximum number of events per @type
90 * @type: type of button (%EV_KEY, %EV_SW)
92 * Return value of this function can be used to allocate bitmap
93 * large enough to hold all bits for given type.
95 static inline int get_n_events_by_type(int type)
97 BUG_ON(type != EV_SW && type != EV_KEY);
99 return (type == EV_KEY) ? KEY_CNT : SW_CNT;
103 * gpio_keys_disable_button() - disables given GPIO button
104 * @bdata: button data for button to be disabled
106 * Disables button pointed by @bdata. This is done by masking
107 * IRQ line. After this function is called, button won't generate
108 * input events anymore. Note that one can only disable buttons
109 * that don't share IRQs.
111 * Make sure that @bdata->disable_lock is locked when entering
112 * this function to avoid races when concurrent threads are
113 * disabling buttons at the same time.
115 static void gpio_keys_disable_button(struct gpio_button_data *bdata)
117 if (!bdata->disabled) {
119 * Disable IRQ and possible debouncing timer.
121 disable_irq(gpio_to_irq(bdata->button->gpio));
122 if (bdata->timer_debounce)
123 del_timer_sync(&bdata->timer);
125 bdata->disabled = true;
130 * gpio_keys_enable_button() - enables given GPIO button
131 * @bdata: button data for button to be disabled
133 * Enables given button pointed by @bdata.
135 * Make sure that @bdata->disable_lock is locked when entering
136 * this function to avoid races with concurrent threads trying
137 * to enable the same button at the same time.
139 static void gpio_keys_enable_button(struct gpio_button_data *bdata)
141 if (bdata->disabled) {
142 enable_irq(gpio_to_irq(bdata->button->gpio));
143 bdata->disabled = false;
148 * gpio_keys_attr_show_helper() - fill in stringified bitmap of buttons
149 * @ddata: pointer to drvdata
150 * @buf: buffer where stringified bitmap is written
151 * @type: button type (%EV_KEY, %EV_SW)
152 * @only_disabled: does caller want only those buttons that are
153 * currently disabled or all buttons that can be
156 * This function writes buttons that can be disabled to @buf. If
157 * @only_disabled is true, then @buf contains only those buttons
158 * that are currently disabled. Returns 0 on success or negative
161 static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
162 char *buf, unsigned int type,
165 int n_events = get_n_events_by_type(type);
170 bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
174 for (i = 0; i < ddata->n_buttons; i++) {
175 struct gpio_button_data *bdata = &ddata->data[i];
177 if (bdata->button->type != type)
180 if (only_disabled && !bdata->disabled)
183 __set_bit(bdata->button->code, bits);
186 ret = bitmap_scnlistprintf(buf, PAGE_SIZE - 2, bits, n_events);
196 * gpio_keys_attr_store_helper() - enable/disable buttons based on given bitmap
197 * @ddata: pointer to drvdata
198 * @buf: buffer from userspace that contains stringified bitmap
199 * @type: button type (%EV_KEY, %EV_SW)
201 * This function parses stringified bitmap from @buf and disables/enables
202 * GPIO buttons accordinly. Returns 0 on success and negative error
205 static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
206 const char *buf, unsigned int type)
208 int n_events = get_n_events_by_type(type);
213 bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
217 error = bitmap_parselist(buf, bits, n_events);
222 for (i = 0; i < ddata->n_buttons; i++) {
223 struct gpio_button_data *bdata = &ddata->data[i];
225 if (bdata->button->type != type)
228 if (test_bit(bdata->button->code, bits) &&
229 !bdata->button->can_disable) {
235 mutex_lock(&ddata->disable_lock);
237 for (i = 0; i < ddata->n_buttons; i++) {
238 struct gpio_button_data *bdata = &ddata->data[i];
240 if (bdata->button->type != type)
243 if (test_bit(bdata->button->code, bits))
244 gpio_keys_disable_button(bdata);
246 gpio_keys_enable_button(bdata);
249 mutex_unlock(&ddata->disable_lock);
256 #define ATTR_SHOW_FN(name, type, only_disabled) \
257 static ssize_t gpio_keys_show_##name(struct device *dev, \
258 struct device_attribute *attr, \
261 struct platform_device *pdev = to_platform_device(dev); \
262 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
264 return gpio_keys_attr_show_helper(ddata, buf, \
265 type, only_disabled); \
268 ATTR_SHOW_FN(keys, EV_KEY, false);
269 ATTR_SHOW_FN(switches, EV_SW, false);
270 ATTR_SHOW_FN(disabled_keys, EV_KEY, true);
271 ATTR_SHOW_FN(disabled_switches, EV_SW, true);
276 * /sys/devices/platform/gpio-keys/keys [ro]
277 * /sys/devices/platform/gpio-keys/switches [ro]
279 static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL);
280 static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL);
282 #define ATTR_STORE_FN(name, type) \
283 static ssize_t gpio_keys_store_##name(struct device *dev, \
284 struct device_attribute *attr, \
288 struct platform_device *pdev = to_platform_device(dev); \
289 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
292 error = gpio_keys_attr_store_helper(ddata, buf, type); \
299 ATTR_STORE_FN(disabled_keys, EV_KEY);
300 ATTR_STORE_FN(disabled_switches, EV_SW);
305 * /sys/devices/platform/gpio-keys/disabled_keys [rw]
306 * /sys/devices/platform/gpio-keys/disables_switches [rw]
308 static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO,
309 gpio_keys_show_disabled_keys,
310 gpio_keys_store_disabled_keys);
311 static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO,
312 gpio_keys_show_disabled_switches,
313 gpio_keys_store_disabled_switches);
315 static struct attribute *gpio_keys_attrs[] = {
317 &dev_attr_switches.attr,
318 &dev_attr_disabled_keys.attr,
319 &dev_attr_disabled_switches.attr,
323 static struct attribute_group gpio_keys_attr_group = {
324 .attrs = gpio_keys_attrs,
327 static ssize_t key_pressed_show(struct device *dev,
328 struct device_attribute *attr, char *buf)
330 struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
334 for (i = 0; i < ddata->n_buttons; i++) {
335 struct gpio_button_data *bdata = &ddata->data[i];
336 keystate |= bdata->key_state;
340 sprintf(buf, "PRESS");
342 sprintf(buf, "RELEASE");
347 /* the volume keys can be the wakeup keys in special case */
348 static ssize_t wakeup_enable(struct device *dev,
349 struct device_attribute *attr, const char *buf, size_t count)
351 struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
352 int n_events = get_n_events_by_type(EV_KEY);
357 bits = kcalloc(BITS_TO_LONGS(n_events),
358 sizeof(*bits), GFP_KERNEL);
362 error = bitmap_parselist(buf, bits, n_events);
366 for (i = 0; i < ddata->n_buttons; i++) {
367 struct gpio_button_data *button = &ddata->data[i];
368 if (test_bit(button->button->code, bits))
369 button->button->wakeup = 1;
371 button->button->wakeup = 0;
372 #if defined(CONFIG_MACH_GRANDE) || defined(CONFIG_MACH_IRON)
373 printk(KERN_DEBUG "Code %d , wakeup bit %d\n", \
374 button->button->code, button->button->wakeup);
383 static DEVICE_ATTR(sec_key_pressed, 0664, key_pressed_show, NULL);
384 static DEVICE_ATTR(wakeup_keys, 0664, NULL, wakeup_enable);
386 static struct attribute *sec_key_attrs[] = {
387 &dev_attr_sec_key_pressed.attr,
388 &dev_attr_wakeup_keys.attr,
392 static struct attribute_group sec_key_attr_group = {
393 .attrs = sec_key_attrs,
396 #ifdef CONFIG_MACH_GC1
397 void gpio_keys_check_zoom_exception(unsigned int code,
398 bool *zoomkey, unsigned int *hotkey, unsigned int *index)
401 case KEY_CAMERA_ZOOMIN:
405 case KEY_CAMERA_ZOOMOUT:
410 *hotkey = KEY_CAMERA_ZOOMIN;
414 *hotkey = KEY_CAMERA_ZOOMOUT;
425 static void gpio_keys_report_event(struct gpio_button_data *bdata)
427 struct gpio_keys_button *button = bdata->button;
428 struct input_dev *input = bdata->input;
429 unsigned int type = button->type ?: EV_KEY;
430 int state = (gpio_get_value_cansleep(button->gpio) ? 1 : 0)
431 ^ button->active_low;
432 #ifdef CONFIG_MACH_GC1
433 struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
434 struct gpio_button_data *tmp_bdata;
435 static bool overlapped;
436 static unsigned int hotkey;
437 unsigned int index_hotkey = 0;
438 bool zoomkey = false;
440 if (system_rev < 6 && system_rev >= 2) {
442 if (hotkey == button->code && !state) {
443 bdata->key_state = !!state;
444 bdata->wakeup = false;
446 #ifdef CONFIG_SAMSUNG_PRODUCT_SHIP
447 printk(KERN_DEBUG"[KEYS] Ignored\n");
449 printk(KERN_DEBUG"[KEYS] Ignore %d %d\n",
456 gpio_keys_check_zoom_exception(button->code, &zoomkey,
457 &hotkey, &index_hotkey);
461 if (type == EV_ABS) {
463 input_event(input, type, button->code, button->value);
465 if (bdata->wakeup && !state) {
466 input_event(input, type, button->code, !state);
467 if (button->code == KEY_POWER)
468 printk(KERN_DEBUG"[keys] f PWR %d\n", !state);
471 bdata->key_state = !!state;
472 bdata->wakeup = false;
475 #ifdef CONFIG_MACH_GC1
476 if (system_rev < 6 && system_rev >= 2
477 && zoomkey && state) {
478 tmp_bdata = &ddata->data[index_hotkey];
480 if (tmp_bdata->key_state) {
481 #ifdef CONFIG_SAMSUNG_PRODUCT_SHIP
482 printk(KERN_DEBUG"[KEYS] overlapped\n");
484 printk(KERN_DEBUG"[KEYS] overlapped. Forced release c %d h %d\n",
485 tmp_bdata->button->code, hotkey);
487 input_event(input, type, hotkey, 0);
492 input_event(input, type, button->code, !!state);
493 if (button->code == KEY_POWER)
494 printk(KERN_DEBUG"[keys]PWR %d\n", !!state);
500 static void gpio_keys_work_func(struct work_struct *work)
502 struct gpio_button_data *bdata =
503 container_of(work, struct gpio_button_data, work);
505 gpio_keys_report_event(bdata);
508 static void gpio_keys_timer(unsigned long _data)
510 struct gpio_button_data *data = (struct gpio_button_data *)_data;
512 schedule_work(&data->work);
515 static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
517 struct gpio_button_data *bdata = dev_id;
518 struct gpio_keys_button *button = bdata->button;
519 struct irq_desc *desc = irq_to_desc(irq);
520 int state = (gpio_get_value_cansleep(button->gpio) ? 1 : 0)
521 ^ button->active_low;
523 BUG_ON(irq != gpio_to_irq(button->gpio));
526 if (0 < desc->wake_depth) {
527 bdata->wakeup = true;
528 printk(KERN_DEBUG"[keys] in the sleep\n");
532 if (bdata->timer_debounce)
533 mod_timer(&bdata->timer,
534 jiffies + msecs_to_jiffies(bdata->timer_debounce));
536 schedule_work(&bdata->work);
538 if (button->isr_hook)
539 button->isr_hook(button->code, state);
544 static int __devinit gpio_keys_setup_key(struct platform_device *pdev,
545 struct gpio_button_data *bdata,
546 struct gpio_keys_button *button)
548 const char *desc = button->desc ? button->desc : "gpio_keys";
549 struct device *dev = &pdev->dev;
550 unsigned long irqflags;
553 setup_timer(&bdata->timer, gpio_keys_timer, (unsigned long)bdata);
554 INIT_WORK(&bdata->work, gpio_keys_work_func);
556 error = gpio_request(button->gpio, desc);
558 dev_err(dev, "failed to request GPIO %d, error %d\n",
559 button->gpio, error);
563 error = gpio_direction_input(button->gpio);
565 dev_err(dev, "failed to configure"
566 " direction for GPIO %d, error %d\n",
567 button->gpio, error);
571 if (button->debounce_interval) {
572 error = gpio_set_debounce(button->gpio,
573 button->debounce_interval * 1000);
574 /* use timer if gpiolib doesn't provide debounce */
576 bdata->timer_debounce = button->debounce_interval;
579 irq = gpio_to_irq(button->gpio);
582 dev_err(dev, "Unable to get irq number for GPIO %d, error %d\n",
583 button->gpio, error);
587 irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
589 * If platform has specified that the button can be disabled,
590 * we don't want it to share the interrupt line.
592 if (!button->can_disable)
593 irqflags |= IRQF_SHARED;
596 irqflags |= IRQF_NO_SUSPEND;
598 error = request_any_context_irq(irq, gpio_keys_isr, irqflags, desc, bdata);
600 dev_err(dev, "Unable to claim irq %d; error %d\n",
608 gpio_free(button->gpio);
613 static int gpio_keys_open(struct input_dev *input)
615 struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
617 return ddata->enable ? ddata->enable(input->dev.parent) : 0;
620 static void gpio_keys_close(struct input_dev *input)
622 struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
625 ddata->disable(input->dev.parent);
628 static int __devinit gpio_keys_probe(struct platform_device *pdev)
630 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
631 struct gpio_keys_drvdata *ddata;
632 struct device *dev = &pdev->dev;
633 struct input_dev *input;
637 ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
638 pdata->nbuttons * sizeof(struct gpio_button_data),
640 input = input_allocate_device();
641 if (!ddata || !input) {
642 dev_err(dev, "failed to allocate state\n");
647 ddata->input = input;
648 ddata->n_buttons = pdata->nbuttons;
649 ddata->enable = pdata->enable;
650 ddata->disable = pdata->disable;
651 mutex_init(&ddata->disable_lock);
653 platform_set_drvdata(pdev, ddata);
654 input_set_drvdata(input, ddata);
656 input->name = pdata->name ? : pdev->name;
657 input->phys = "gpio-keys/input0";
658 input->dev.parent = &pdev->dev;
659 input->open = gpio_keys_open;
660 input->close = gpio_keys_close;
662 input->id.bustype = BUS_HOST;
663 input->id.vendor = 0x0001;
664 input->id.product = 0x0001;
665 input->id.version = 0x0100;
667 /* Enable auto repeat feature of Linux input subsystem */
669 __set_bit(EV_REP, input->evbit);
671 for (i = 0; i < pdata->nbuttons; i++) {
672 struct gpio_keys_button *button = &pdata->buttons[i];
673 struct gpio_button_data *bdata = &ddata->data[i];
674 unsigned int type = button->type ?: EV_KEY;
676 bdata->input = input;
677 bdata->button = button;
679 error = gpio_keys_setup_key(pdev, bdata, button);
686 input_set_capability(input, type, button->code);
689 error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group);
691 dev_err(dev, "Unable to export keys/switches, error: %d\n",
697 device_create(sec_class, NULL, 0, ddata, "sec_key");
698 if (IS_ERR(ddata->sec_key))
699 dev_err(dev, "Failed to create sec_key device\n");
701 error = sysfs_create_group(&ddata->sec_key->kobj, &sec_key_attr_group);
703 dev_err(dev, "Failed to create the test sysfs: %d\n",
708 error = input_register_device(input);
710 dev_err(dev, "Unable to register input device, error: %d\n",
715 /* get current state of buttons */
716 for (i = 0; i < pdata->nbuttons; i++)
717 gpio_keys_report_event(&ddata->data[i]);
720 device_init_wakeup(&pdev->dev, wakeup);
725 sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
726 sysfs_remove_group(&ddata->sec_key->kobj, &sec_key_attr_group);
729 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
730 if (ddata->data[i].timer_debounce)
731 del_timer_sync(&ddata->data[i].timer);
732 cancel_work_sync(&ddata->data[i].work);
733 gpio_free(pdata->buttons[i].gpio);
736 platform_set_drvdata(pdev, NULL);
738 input_free_device(input);
744 static int __devexit gpio_keys_remove(struct platform_device *pdev)
746 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
747 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
748 struct input_dev *input = ddata->input;
751 sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
753 device_init_wakeup(&pdev->dev, 0);
755 for (i = 0; i < pdata->nbuttons; i++) {
756 int irq = gpio_to_irq(pdata->buttons[i].gpio);
757 free_irq(irq, &ddata->data[i]);
758 if (ddata->data[i].timer_debounce)
759 del_timer_sync(&ddata->data[i].timer);
760 cancel_work_sync(&ddata->data[i].work);
761 gpio_free(pdata->buttons[i].gpio);
764 input_unregister_device(input);
771 static int gpio_keys_suspend(struct device *dev)
773 struct platform_device *pdev = to_platform_device(dev);
774 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
777 if (device_may_wakeup(&pdev->dev)) {
778 for (i = 0; i < pdata->nbuttons; i++) {
779 struct gpio_keys_button *button = &pdata->buttons[i];
780 if (button->wakeup) {
781 int irq = gpio_to_irq(button->gpio);
782 enable_irq_wake(irq);
790 static int gpio_keys_resume(struct device *dev)
792 struct platform_device *pdev = to_platform_device(dev);
793 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
794 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
797 for (i = 0; i < pdata->nbuttons; i++) {
799 struct gpio_keys_button *button = &pdata->buttons[i];
800 if (button->wakeup && device_may_wakeup(&pdev->dev)) {
801 int irq = gpio_to_irq(button->gpio);
802 disable_irq_wake(irq);
805 gpio_keys_report_event(&ddata->data[i]);
807 input_sync(ddata->input);
812 static const struct dev_pm_ops gpio_keys_pm_ops = {
813 .suspend = gpio_keys_suspend,
814 .resume = gpio_keys_resume,
818 static struct platform_driver gpio_keys_device_driver = {
819 .probe = gpio_keys_probe,
820 .remove = __devexit_p(gpio_keys_remove),
823 .owner = THIS_MODULE,
825 .pm = &gpio_keys_pm_ops,
830 static int __init gpio_keys_init(void)
832 return platform_driver_register(&gpio_keys_device_driver);
835 static void __exit gpio_keys_exit(void)
837 platform_driver_unregister(&gpio_keys_device_driver);
840 module_init(gpio_keys_init);
841 module_exit(gpio_keys_exit);
843 MODULE_LICENSE("GPL");
844 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
845 MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
846 MODULE_ALIAS("platform:gpio-keys");