2 * Driver for keys on GPIO lines capable of generating interrupts.
4 * Copyright 2005 Phil Blundell
5 * Copyright 2010, 2011 David Jander <david@protonic.nl>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/module.h>
14 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/sysctl.h>
22 #include <linux/proc_fs.h>
23 #include <linux/delay.h>
24 #include <linux/platform_device.h>
25 #include <linux/input.h>
26 #include <linux/gpio_keys.h>
27 #include <linux/workqueue.h>
28 #include <linux/gpio.h>
29 #include <linux/gpio/consumer.h>
31 #include <linux/of_platform.h>
32 #include <linux/of_gpio.h>
33 #include <linux/of_irq.h>
34 #include <linux/spinlock.h>
36 struct gpio_button_data {
37 const struct gpio_keys_button *button;
38 struct input_dev *input;
39 struct gpio_desc *gpiod;
41 struct timer_list release_timer;
42 unsigned int release_delay; /* in msecs, for IRQ-only buttons */
44 struct delayed_work work;
45 unsigned int software_debounce; /* in msecs, for GPIO-driven buttons */
53 struct gpio_keys_drvdata {
54 const struct gpio_keys_platform_data *pdata;
55 struct input_dev *input;
56 struct mutex disable_lock;
57 struct gpio_button_data data[0];
61 * SYSFS interface for enabling/disabling keys and switches:
63 * There are 4 attributes under /sys/devices/platform/gpio-keys/
64 * keys [ro] - bitmap of keys (EV_KEY) which can be
66 * switches [ro] - bitmap of switches (EV_SW) which can be
68 * disabled_keys [rw] - bitmap of keys currently disabled
69 * disabled_switches [rw] - bitmap of switches currently disabled
71 * Userland can change these values and hence disable event generation
72 * for each key (or switch). Disabling a key means its interrupt line
75 * For example, if we have following switches set up as gpio-keys:
77 * SW_CAMERA_LENS_COVER = 9
78 * SW_KEYPAD_SLIDE = 10
79 * SW_FRONT_PROXIMITY = 11
80 * This is read from switches:
82 * Next we want to disable proximity (11) and dock (5), we write:
84 * to file disabled_switches. Now proximity and dock IRQs are disabled.
85 * This can be verified by reading the file disabled_switches:
87 * If we now want to enable proximity (11) switch we write:
89 * to disabled_switches.
91 * We can disable only those keys which don't allow sharing the irq.
95 * get_n_events_by_type() - returns maximum number of events per @type
96 * @type: type of button (%EV_KEY, %EV_SW)
98 * Return value of this function can be used to allocate bitmap
99 * large enough to hold all bits for given type.
101 static int get_n_events_by_type(int type)
103 BUG_ON(type != EV_SW && type != EV_KEY);
105 return (type == EV_KEY) ? KEY_CNT : SW_CNT;
109 * get_bm_events_by_type() - returns bitmap of supported events per @type
110 * @input: input device from which bitmap is retrieved
111 * @type: type of button (%EV_KEY, %EV_SW)
113 * Return value of this function can be used to allocate bitmap
114 * large enough to hold all bits for given type.
116 static const unsigned long *get_bm_events_by_type(struct input_dev *dev,
119 BUG_ON(type != EV_SW && type != EV_KEY);
121 return (type == EV_KEY) ? dev->keybit : dev->swbit;
125 * gpio_keys_disable_button() - disables given GPIO button
126 * @bdata: button data for button to be disabled
128 * Disables button pointed by @bdata. This is done by masking
129 * IRQ line. After this function is called, button won't generate
130 * input events anymore. Note that one can only disable buttons
131 * that don't share IRQs.
133 * Make sure that @bdata->disable_lock is locked when entering
134 * this function to avoid races when concurrent threads are
135 * disabling buttons at the same time.
137 static void gpio_keys_disable_button(struct gpio_button_data *bdata)
139 if (!bdata->disabled) {
141 * Disable IRQ and associated timer/work structure.
143 disable_irq(bdata->irq);
146 cancel_delayed_work_sync(&bdata->work);
148 del_timer_sync(&bdata->release_timer);
150 bdata->disabled = true;
155 * gpio_keys_enable_button() - enables given GPIO button
156 * @bdata: button data for button to be disabled
158 * Enables given button pointed by @bdata.
160 * Make sure that @bdata->disable_lock is locked when entering
161 * this function to avoid races with concurrent threads trying
162 * to enable the same button at the same time.
164 static void gpio_keys_enable_button(struct gpio_button_data *bdata)
166 if (bdata->disabled) {
167 enable_irq(bdata->irq);
168 bdata->disabled = false;
173 * gpio_keys_attr_show_helper() - fill in stringified bitmap of buttons
174 * @ddata: pointer to drvdata
175 * @buf: buffer where stringified bitmap is written
176 * @type: button type (%EV_KEY, %EV_SW)
177 * @only_disabled: does caller want only those buttons that are
178 * currently disabled or all buttons that can be
181 * This function writes buttons that can be disabled to @buf. If
182 * @only_disabled is true, then @buf contains only those buttons
183 * that are currently disabled. Returns 0 on success or negative
186 static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
187 char *buf, unsigned int type,
190 int n_events = get_n_events_by_type(type);
195 bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
199 for (i = 0; i < ddata->pdata->nbuttons; i++) {
200 struct gpio_button_data *bdata = &ddata->data[i];
202 if (bdata->button->type != type)
205 if (only_disabled && !bdata->disabled)
208 __set_bit(bdata->button->code, bits);
211 ret = scnprintf(buf, PAGE_SIZE - 1, "%*pbl", n_events, bits);
221 * gpio_keys_attr_store_helper() - enable/disable buttons based on given bitmap
222 * @ddata: pointer to drvdata
223 * @buf: buffer from userspace that contains stringified bitmap
224 * @type: button type (%EV_KEY, %EV_SW)
226 * This function parses stringified bitmap from @buf and disables/enables
227 * GPIO buttons accordingly. Returns 0 on success and negative error
230 static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
231 const char *buf, unsigned int type)
233 int n_events = get_n_events_by_type(type);
234 const unsigned long *bitmap = get_bm_events_by_type(ddata->input, type);
239 bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
243 error = bitmap_parselist(buf, bits, n_events);
248 if (!bitmap_subset(bits, bitmap, n_events)) {
253 for (i = 0; i < ddata->pdata->nbuttons; i++) {
254 struct gpio_button_data *bdata = &ddata->data[i];
256 if (bdata->button->type != type)
259 if (test_bit(bdata->button->code, bits) &&
260 !bdata->button->can_disable) {
266 mutex_lock(&ddata->disable_lock);
268 for (i = 0; i < ddata->pdata->nbuttons; i++) {
269 struct gpio_button_data *bdata = &ddata->data[i];
271 if (bdata->button->type != type)
274 if (test_bit(bdata->button->code, bits))
275 gpio_keys_disable_button(bdata);
277 gpio_keys_enable_button(bdata);
280 mutex_unlock(&ddata->disable_lock);
287 #define ATTR_SHOW_FN(name, type, only_disabled) \
288 static ssize_t gpio_keys_show_##name(struct device *dev, \
289 struct device_attribute *attr, \
292 struct platform_device *pdev = to_platform_device(dev); \
293 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
295 return gpio_keys_attr_show_helper(ddata, buf, \
296 type, only_disabled); \
299 ATTR_SHOW_FN(keys, EV_KEY, false);
300 ATTR_SHOW_FN(switches, EV_SW, false);
301 ATTR_SHOW_FN(disabled_keys, EV_KEY, true);
302 ATTR_SHOW_FN(disabled_switches, EV_SW, true);
307 * /sys/devices/platform/gpio-keys/keys [ro]
308 * /sys/devices/platform/gpio-keys/switches [ro]
310 static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL);
311 static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL);
313 #define ATTR_STORE_FN(name, type) \
314 static ssize_t gpio_keys_store_##name(struct device *dev, \
315 struct device_attribute *attr, \
319 struct platform_device *pdev = to_platform_device(dev); \
320 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
323 error = gpio_keys_attr_store_helper(ddata, buf, type); \
330 ATTR_STORE_FN(disabled_keys, EV_KEY);
331 ATTR_STORE_FN(disabled_switches, EV_SW);
336 * /sys/devices/platform/gpio-keys/disabled_keys [rw]
337 * /sys/devices/platform/gpio-keys/disables_switches [rw]
339 static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO,
340 gpio_keys_show_disabled_keys,
341 gpio_keys_store_disabled_keys);
342 static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO,
343 gpio_keys_show_disabled_switches,
344 gpio_keys_store_disabled_switches);
346 static struct attribute *gpio_keys_attrs[] = {
348 &dev_attr_switches.attr,
349 &dev_attr_disabled_keys.attr,
350 &dev_attr_disabled_switches.attr,
354 static struct attribute_group gpio_keys_attr_group = {
355 .attrs = gpio_keys_attrs,
358 static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
360 const struct gpio_keys_button *button = bdata->button;
361 struct input_dev *input = bdata->input;
362 unsigned int type = button->type ?: EV_KEY;
365 state = gpiod_get_value_cansleep(bdata->gpiod);
367 dev_err(input->dev.parent,
368 "failed to get gpio state: %d\n", state);
372 if (type == EV_ABS) {
374 input_event(input, type, button->code, button->value);
376 input_event(input, type, button->code, state);
381 static void gpio_keys_gpio_work_func(struct work_struct *work)
383 struct gpio_button_data *bdata =
384 container_of(work, struct gpio_button_data, work.work);
386 gpio_keys_gpio_report_event(bdata);
388 if (bdata->button->wakeup)
389 pm_relax(bdata->input->dev.parent);
392 static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
394 struct gpio_button_data *bdata = dev_id;
396 BUG_ON(irq != bdata->irq);
398 if (bdata->button->wakeup)
399 pm_stay_awake(bdata->input->dev.parent);
401 mod_delayed_work(system_wq,
403 msecs_to_jiffies(bdata->software_debounce));
408 static void gpio_keys_irq_timer(unsigned long _data)
410 struct gpio_button_data *bdata = (struct gpio_button_data *)_data;
411 struct input_dev *input = bdata->input;
414 spin_lock_irqsave(&bdata->lock, flags);
415 if (bdata->key_pressed) {
416 input_event(input, EV_KEY, bdata->button->code, 0);
418 bdata->key_pressed = false;
420 spin_unlock_irqrestore(&bdata->lock, flags);
423 static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
425 struct gpio_button_data *bdata = dev_id;
426 const struct gpio_keys_button *button = bdata->button;
427 struct input_dev *input = bdata->input;
430 BUG_ON(irq != bdata->irq);
432 spin_lock_irqsave(&bdata->lock, flags);
434 if (!bdata->key_pressed) {
435 if (bdata->button->wakeup)
436 pm_wakeup_event(bdata->input->dev.parent, 0);
438 input_event(input, EV_KEY, button->code, 1);
441 if (!bdata->release_delay) {
442 input_event(input, EV_KEY, button->code, 0);
447 bdata->key_pressed = true;
450 if (bdata->release_delay)
451 mod_timer(&bdata->release_timer,
452 jiffies + msecs_to_jiffies(bdata->release_delay));
454 spin_unlock_irqrestore(&bdata->lock, flags);
458 static void gpio_keys_quiesce_key(void *data)
460 struct gpio_button_data *bdata = data;
463 cancel_delayed_work_sync(&bdata->work);
465 del_timer_sync(&bdata->release_timer);
468 static int gpio_keys_setup_key(struct platform_device *pdev,
469 struct input_dev *input,
470 struct gpio_button_data *bdata,
471 const struct gpio_keys_button *button)
473 const char *desc = button->desc ? button->desc : "gpio_keys";
474 struct device *dev = &pdev->dev;
476 unsigned long irqflags;
480 bdata->input = input;
481 bdata->button = button;
482 spin_lock_init(&bdata->lock);
485 * Legacy GPIO number, so request the GPIO here and
486 * convert it to descriptor.
488 if (gpio_is_valid(button->gpio)) {
489 unsigned flags = GPIOF_IN;
491 if (button->active_low)
492 flags |= GPIOF_ACTIVE_LOW;
494 error = devm_gpio_request_one(&pdev->dev, button->gpio, flags,
497 dev_err(dev, "Failed to request GPIO %d, error %d\n",
498 button->gpio, error);
502 bdata->gpiod = gpio_to_desc(button->gpio);
506 if (button->debounce_interval) {
507 error = gpiod_set_debounce(bdata->gpiod,
508 button->debounce_interval * 1000);
509 /* use timer if gpiolib doesn't provide debounce */
511 bdata->software_debounce =
512 button->debounce_interval;
516 bdata->irq = button->irq;
518 irq = gpiod_to_irq(bdata->gpiod);
522 "Unable to get irq number for GPIO %d, error %d\n",
523 button->gpio, error);
529 INIT_DELAYED_WORK(&bdata->work, gpio_keys_gpio_work_func);
531 isr = gpio_keys_gpio_isr;
532 irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
536 dev_err(dev, "No IRQ specified\n");
539 bdata->irq = button->irq;
541 if (button->type && button->type != EV_KEY) {
542 dev_err(dev, "Only EV_KEY allowed for IRQ buttons.\n");
546 bdata->release_delay = button->debounce_interval;
547 setup_timer(&bdata->release_timer,
548 gpio_keys_irq_timer, (unsigned long)bdata);
550 isr = gpio_keys_irq_isr;
554 input_set_capability(input, button->type ?: EV_KEY, button->code);
557 * Install custom action to cancel release timer and
560 error = devm_add_action(&pdev->dev, gpio_keys_quiesce_key, bdata);
563 "failed to register quiesce action, error: %d\n",
569 * If platform has specified that the button can be disabled,
570 * we don't want it to share the interrupt line.
572 if (!button->can_disable)
573 irqflags |= IRQF_SHARED;
575 error = devm_request_any_context_irq(&pdev->dev, bdata->irq,
576 isr, irqflags, desc, bdata);
578 dev_err(dev, "Unable to claim irq %d; error %d\n",
586 static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata)
588 struct input_dev *input = ddata->input;
591 for (i = 0; i < ddata->pdata->nbuttons; i++) {
592 struct gpio_button_data *bdata = &ddata->data[i];
594 gpio_keys_gpio_report_event(bdata);
599 static int gpio_keys_open(struct input_dev *input)
601 struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
602 const struct gpio_keys_platform_data *pdata = ddata->pdata;
606 error = pdata->enable(input->dev.parent);
611 /* Report current state of buttons that are connected to GPIOs */
612 gpio_keys_report_state(ddata);
617 static void gpio_keys_close(struct input_dev *input)
619 struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
620 const struct gpio_keys_platform_data *pdata = ddata->pdata;
623 pdata->disable(input->dev.parent);
627 * Handlers for alternative sources of platform_data
632 * Translate OpenFirmware node properties into platform_data
634 static struct gpio_keys_platform_data *
635 gpio_keys_get_devtree_pdata(struct device *dev)
637 struct device_node *node, *pp;
638 struct gpio_keys_platform_data *pdata;
639 struct gpio_keys_button *button;
646 return ERR_PTR(-ENODEV);
648 nbuttons = of_get_available_child_count(node);
650 return ERR_PTR(-ENODEV);
652 pdata = devm_kzalloc(dev,
653 sizeof(*pdata) + nbuttons * sizeof(*button),
656 return ERR_PTR(-ENOMEM);
658 pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
659 pdata->nbuttons = nbuttons;
661 pdata->rep = !!of_get_property(node, "autorepeat", NULL);
663 of_property_read_string(node, "label", &pdata->name);
666 for_each_available_child_of_node(node, pp) {
667 enum of_gpio_flags flags;
669 button = &pdata->buttons[i++];
671 button->gpio = of_get_gpio_flags(pp, 0, &flags);
672 if (button->gpio < 0) {
673 error = button->gpio;
674 if (error != -ENOENT) {
675 if (error != -EPROBE_DEFER)
677 "Failed to get gpio flags, error: %d\n",
679 return ERR_PTR(error);
682 button->active_low = flags & OF_GPIO_ACTIVE_LOW;
685 button->irq = irq_of_parse_and_map(pp, 0);
687 if (!gpio_is_valid(button->gpio) && !button->irq) {
688 dev_err(dev, "Found button without gpios or irqs\n");
689 return ERR_PTR(-EINVAL);
692 if (of_property_read_u32(pp, "linux,code", &button->code)) {
693 dev_err(dev, "Button without keycode: 0x%x\n",
695 return ERR_PTR(-EINVAL);
698 button->desc = of_get_property(pp, "label", NULL);
700 if (of_property_read_u32(pp, "linux,input-type", &button->type))
701 button->type = EV_KEY;
703 button->wakeup = of_property_read_bool(pp, "wakeup-source") ||
705 of_property_read_bool(pp, "gpio-key,wakeup");
707 button->can_disable = !!of_get_property(pp, "linux,can-disable", NULL);
709 if (of_property_read_u32(pp, "debounce-interval",
710 &button->debounce_interval))
711 button->debounce_interval = 5;
714 if (pdata->nbuttons == 0)
715 return ERR_PTR(-EINVAL);
720 static const struct of_device_id gpio_keys_of_match[] = {
721 { .compatible = "gpio-keys", },
724 MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
728 static inline struct gpio_keys_platform_data *
729 gpio_keys_get_devtree_pdata(struct device *dev)
731 return ERR_PTR(-ENODEV);
736 static int gpio_keys_probe(struct platform_device *pdev)
738 struct device *dev = &pdev->dev;
739 const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
740 struct gpio_keys_drvdata *ddata;
741 struct input_dev *input;
747 pdata = gpio_keys_get_devtree_pdata(dev);
749 return PTR_ERR(pdata);
752 size = sizeof(struct gpio_keys_drvdata) +
753 pdata->nbuttons * sizeof(struct gpio_button_data);
754 ddata = devm_kzalloc(dev, size, GFP_KERNEL);
756 dev_err(dev, "failed to allocate state\n");
760 input = devm_input_allocate_device(dev);
762 dev_err(dev, "failed to allocate input device\n");
766 ddata->pdata = pdata;
767 ddata->input = input;
768 mutex_init(&ddata->disable_lock);
770 platform_set_drvdata(pdev, ddata);
771 input_set_drvdata(input, ddata);
773 input->name = pdata->name ? : pdev->name;
774 input->phys = "gpio-keys/input0";
775 input->dev.parent = &pdev->dev;
776 input->open = gpio_keys_open;
777 input->close = gpio_keys_close;
779 input->id.bustype = BUS_HOST;
780 input->id.vendor = 0x0001;
781 input->id.product = 0x0001;
782 input->id.version = 0x0100;
784 /* Enable auto repeat feature of Linux input subsystem */
786 __set_bit(EV_REP, input->evbit);
788 for (i = 0; i < pdata->nbuttons; i++) {
789 const struct gpio_keys_button *button = &pdata->buttons[i];
790 struct gpio_button_data *bdata = &ddata->data[i];
792 error = gpio_keys_setup_key(pdev, input, bdata, button);
800 error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group);
802 dev_err(dev, "Unable to export keys/switches, error: %d\n",
807 error = input_register_device(input);
809 dev_err(dev, "Unable to register input device, error: %d\n",
811 goto err_remove_group;
814 device_init_wakeup(&pdev->dev, wakeup);
819 sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
823 static int gpio_keys_remove(struct platform_device *pdev)
825 sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
827 device_init_wakeup(&pdev->dev, 0);
832 #ifdef CONFIG_PM_SLEEP
833 static int gpio_keys_suspend(struct device *dev)
835 struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
836 struct input_dev *input = ddata->input;
839 if (device_may_wakeup(dev)) {
840 for (i = 0; i < ddata->pdata->nbuttons; i++) {
841 struct gpio_button_data *bdata = &ddata->data[i];
842 if (bdata->button->wakeup)
843 enable_irq_wake(bdata->irq);
846 mutex_lock(&input->mutex);
848 gpio_keys_close(input);
849 mutex_unlock(&input->mutex);
855 static int gpio_keys_resume(struct device *dev)
857 struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
858 struct input_dev *input = ddata->input;
862 if (device_may_wakeup(dev)) {
863 for (i = 0; i < ddata->pdata->nbuttons; i++) {
864 struct gpio_button_data *bdata = &ddata->data[i];
865 if (bdata->button->wakeup)
866 disable_irq_wake(bdata->irq);
869 mutex_lock(&input->mutex);
871 error = gpio_keys_open(input);
872 mutex_unlock(&input->mutex);
878 gpio_keys_report_state(ddata);
883 static SIMPLE_DEV_PM_OPS(gpio_keys_pm_ops, gpio_keys_suspend, gpio_keys_resume);
885 static struct platform_driver gpio_keys_device_driver = {
886 .probe = gpio_keys_probe,
887 .remove = gpio_keys_remove,
890 .pm = &gpio_keys_pm_ops,
891 .of_match_table = of_match_ptr(gpio_keys_of_match),
895 static int __init gpio_keys_init(void)
897 return platform_driver_register(&gpio_keys_device_driver);
900 static void __exit gpio_keys_exit(void)
902 platform_driver_unregister(&gpio_keys_device_driver);
905 late_initcall(gpio_keys_init);
906 module_exit(gpio_keys_exit);
908 MODULE_LICENSE("GPL");
909 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
910 MODULE_DESCRIPTION("Keyboard driver for GPIOs");
911 MODULE_ALIAS("platform:gpio-keys");