x86/topology: Fix function name in documentation
[cris-mirror.git] / drivers / input / keyboard / gpio_keys.c
blob87e613dc33b804bf60047133f773343550e1a044
1 /*
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>
15 #include <linux/fs.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/sched.h>
19 #include <linux/pm.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>
30 #include <linux/of.h>
31 #include <linux/of_irq.h>
32 #include <linux/spinlock.h>
34 struct gpio_button_data {
35 const struct gpio_keys_button *button;
36 struct input_dev *input;
37 struct gpio_desc *gpiod;
39 unsigned short *code;
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 */
47 unsigned int irq;
48 spinlock_t lock;
49 bool disabled;
50 bool key_pressed;
51 bool suspended;
54 struct gpio_keys_drvdata {
55 const struct gpio_keys_platform_data *pdata;
56 struct input_dev *input;
57 struct mutex disable_lock;
58 unsigned short *keymap;
59 struct gpio_button_data data[0];
63 * SYSFS interface for enabling/disabling keys and switches:
65 * There are 4 attributes under /sys/devices/platform/gpio-keys/
66 * keys [ro] - bitmap of keys (EV_KEY) which can be
67 * disabled
68 * switches [ro] - bitmap of switches (EV_SW) which can be
69 * disabled
70 * disabled_keys [rw] - bitmap of keys currently disabled
71 * disabled_switches [rw] - bitmap of switches currently disabled
73 * Userland can change these values and hence disable event generation
74 * for each key (or switch). Disabling a key means its interrupt line
75 * is disabled.
77 * For example, if we have following switches set up as gpio-keys:
78 * SW_DOCK = 5
79 * SW_CAMERA_LENS_COVER = 9
80 * SW_KEYPAD_SLIDE = 10
81 * SW_FRONT_PROXIMITY = 11
82 * This is read from switches:
83 * 11-9,5
84 * Next we want to disable proximity (11) and dock (5), we write:
85 * 11,5
86 * to file disabled_switches. Now proximity and dock IRQs are disabled.
87 * This can be verified by reading the file disabled_switches:
88 * 11,5
89 * If we now want to enable proximity (11) switch we write:
90 * 5
91 * to disabled_switches.
93 * We can disable only those keys which don't allow sharing the irq.
96 /**
97 * get_n_events_by_type() - returns maximum number of events per @type
98 * @type: type of button (%EV_KEY, %EV_SW)
100 * Return value of this function can be used to allocate bitmap
101 * large enough to hold all bits for given type.
103 static int get_n_events_by_type(int type)
105 BUG_ON(type != EV_SW && type != EV_KEY);
107 return (type == EV_KEY) ? KEY_CNT : SW_CNT;
111 * get_bm_events_by_type() - returns bitmap of supported events per @type
112 * @input: input device from which bitmap is retrieved
113 * @type: type of button (%EV_KEY, %EV_SW)
115 * Return value of this function can be used to allocate bitmap
116 * large enough to hold all bits for given type.
118 static const unsigned long *get_bm_events_by_type(struct input_dev *dev,
119 int type)
121 BUG_ON(type != EV_SW && type != EV_KEY);
123 return (type == EV_KEY) ? dev->keybit : dev->swbit;
127 * gpio_keys_disable_button() - disables given GPIO button
128 * @bdata: button data for button to be disabled
130 * Disables button pointed by @bdata. This is done by masking
131 * IRQ line. After this function is called, button won't generate
132 * input events anymore. Note that one can only disable buttons
133 * that don't share IRQs.
135 * Make sure that @bdata->disable_lock is locked when entering
136 * this function to avoid races when concurrent threads are
137 * disabling buttons at the same time.
139 static void gpio_keys_disable_button(struct gpio_button_data *bdata)
141 if (!bdata->disabled) {
143 * Disable IRQ and associated timer/work structure.
145 disable_irq(bdata->irq);
147 if (bdata->gpiod)
148 cancel_delayed_work_sync(&bdata->work);
149 else
150 del_timer_sync(&bdata->release_timer);
152 bdata->disabled = true;
157 * gpio_keys_enable_button() - enables given GPIO button
158 * @bdata: button data for button to be disabled
160 * Enables given button pointed by @bdata.
162 * Make sure that @bdata->disable_lock is locked when entering
163 * this function to avoid races with concurrent threads trying
164 * to enable the same button at the same time.
166 static void gpio_keys_enable_button(struct gpio_button_data *bdata)
168 if (bdata->disabled) {
169 enable_irq(bdata->irq);
170 bdata->disabled = false;
175 * gpio_keys_attr_show_helper() - fill in stringified bitmap of buttons
176 * @ddata: pointer to drvdata
177 * @buf: buffer where stringified bitmap is written
178 * @type: button type (%EV_KEY, %EV_SW)
179 * @only_disabled: does caller want only those buttons that are
180 * currently disabled or all buttons that can be
181 * disabled
183 * This function writes buttons that can be disabled to @buf. If
184 * @only_disabled is true, then @buf contains only those buttons
185 * that are currently disabled. Returns 0 on success or negative
186 * errno on failure.
188 static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
189 char *buf, unsigned int type,
190 bool only_disabled)
192 int n_events = get_n_events_by_type(type);
193 unsigned long *bits;
194 ssize_t ret;
195 int i;
197 bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
198 if (!bits)
199 return -ENOMEM;
201 for (i = 0; i < ddata->pdata->nbuttons; i++) {
202 struct gpio_button_data *bdata = &ddata->data[i];
204 if (bdata->button->type != type)
205 continue;
207 if (only_disabled && !bdata->disabled)
208 continue;
210 __set_bit(*bdata->code, bits);
213 ret = scnprintf(buf, PAGE_SIZE - 1, "%*pbl", n_events, bits);
214 buf[ret++] = '\n';
215 buf[ret] = '\0';
217 kfree(bits);
219 return ret;
223 * gpio_keys_attr_store_helper() - enable/disable buttons based on given bitmap
224 * @ddata: pointer to drvdata
225 * @buf: buffer from userspace that contains stringified bitmap
226 * @type: button type (%EV_KEY, %EV_SW)
228 * This function parses stringified bitmap from @buf and disables/enables
229 * GPIO buttons accordingly. Returns 0 on success and negative error
230 * on failure.
232 static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
233 const char *buf, unsigned int type)
235 int n_events = get_n_events_by_type(type);
236 const unsigned long *bitmap = get_bm_events_by_type(ddata->input, type);
237 unsigned long *bits;
238 ssize_t error;
239 int i;
241 bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
242 if (!bits)
243 return -ENOMEM;
245 error = bitmap_parselist(buf, bits, n_events);
246 if (error)
247 goto out;
249 /* First validate */
250 if (!bitmap_subset(bits, bitmap, n_events)) {
251 error = -EINVAL;
252 goto out;
255 for (i = 0; i < ddata->pdata->nbuttons; i++) {
256 struct gpio_button_data *bdata = &ddata->data[i];
258 if (bdata->button->type != type)
259 continue;
261 if (test_bit(*bdata->code, bits) &&
262 !bdata->button->can_disable) {
263 error = -EINVAL;
264 goto out;
268 mutex_lock(&ddata->disable_lock);
270 for (i = 0; i < ddata->pdata->nbuttons; i++) {
271 struct gpio_button_data *bdata = &ddata->data[i];
273 if (bdata->button->type != type)
274 continue;
276 if (test_bit(*bdata->code, bits))
277 gpio_keys_disable_button(bdata);
278 else
279 gpio_keys_enable_button(bdata);
282 mutex_unlock(&ddata->disable_lock);
284 out:
285 kfree(bits);
286 return error;
289 #define ATTR_SHOW_FN(name, type, only_disabled) \
290 static ssize_t gpio_keys_show_##name(struct device *dev, \
291 struct device_attribute *attr, \
292 char *buf) \
294 struct platform_device *pdev = to_platform_device(dev); \
295 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
297 return gpio_keys_attr_show_helper(ddata, buf, \
298 type, only_disabled); \
301 ATTR_SHOW_FN(keys, EV_KEY, false);
302 ATTR_SHOW_FN(switches, EV_SW, false);
303 ATTR_SHOW_FN(disabled_keys, EV_KEY, true);
304 ATTR_SHOW_FN(disabled_switches, EV_SW, true);
307 * ATTRIBUTES:
309 * /sys/devices/platform/gpio-keys/keys [ro]
310 * /sys/devices/platform/gpio-keys/switches [ro]
312 static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL);
313 static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL);
315 #define ATTR_STORE_FN(name, type) \
316 static ssize_t gpio_keys_store_##name(struct device *dev, \
317 struct device_attribute *attr, \
318 const char *buf, \
319 size_t count) \
321 struct platform_device *pdev = to_platform_device(dev); \
322 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
323 ssize_t error; \
325 error = gpio_keys_attr_store_helper(ddata, buf, type); \
326 if (error) \
327 return error; \
329 return count; \
332 ATTR_STORE_FN(disabled_keys, EV_KEY);
333 ATTR_STORE_FN(disabled_switches, EV_SW);
336 * ATTRIBUTES:
338 * /sys/devices/platform/gpio-keys/disabled_keys [rw]
339 * /sys/devices/platform/gpio-keys/disables_switches [rw]
341 static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO,
342 gpio_keys_show_disabled_keys,
343 gpio_keys_store_disabled_keys);
344 static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO,
345 gpio_keys_show_disabled_switches,
346 gpio_keys_store_disabled_switches);
348 static struct attribute *gpio_keys_attrs[] = {
349 &dev_attr_keys.attr,
350 &dev_attr_switches.attr,
351 &dev_attr_disabled_keys.attr,
352 &dev_attr_disabled_switches.attr,
353 NULL,
356 static const struct attribute_group gpio_keys_attr_group = {
357 .attrs = gpio_keys_attrs,
360 static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
362 const struct gpio_keys_button *button = bdata->button;
363 struct input_dev *input = bdata->input;
364 unsigned int type = button->type ?: EV_KEY;
365 int state;
367 state = gpiod_get_value_cansleep(bdata->gpiod);
368 if (state < 0) {
369 dev_err(input->dev.parent,
370 "failed to get gpio state: %d\n", state);
371 return;
374 if (type == EV_ABS) {
375 if (state)
376 input_event(input, type, button->code, button->value);
377 } else {
378 input_event(input, type, *bdata->code, state);
380 input_sync(input);
383 static void gpio_keys_gpio_work_func(struct work_struct *work)
385 struct gpio_button_data *bdata =
386 container_of(work, struct gpio_button_data, work.work);
388 gpio_keys_gpio_report_event(bdata);
390 if (bdata->button->wakeup)
391 pm_relax(bdata->input->dev.parent);
394 static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
396 struct gpio_button_data *bdata = dev_id;
398 BUG_ON(irq != bdata->irq);
400 if (bdata->button->wakeup) {
401 const struct gpio_keys_button *button = bdata->button;
403 pm_stay_awake(bdata->input->dev.parent);
404 if (bdata->suspended &&
405 (button->type == 0 || button->type == EV_KEY)) {
407 * Simulate wakeup key press in case the key has
408 * already released by the time we got interrupt
409 * handler to run.
411 input_report_key(bdata->input, button->code, 1);
415 mod_delayed_work(system_wq,
416 &bdata->work,
417 msecs_to_jiffies(bdata->software_debounce));
419 return IRQ_HANDLED;
422 static void gpio_keys_irq_timer(struct timer_list *t)
424 struct gpio_button_data *bdata = from_timer(bdata, t, release_timer);
425 struct input_dev *input = bdata->input;
426 unsigned long flags;
428 spin_lock_irqsave(&bdata->lock, flags);
429 if (bdata->key_pressed) {
430 input_event(input, EV_KEY, *bdata->code, 0);
431 input_sync(input);
432 bdata->key_pressed = false;
434 spin_unlock_irqrestore(&bdata->lock, flags);
437 static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
439 struct gpio_button_data *bdata = dev_id;
440 struct input_dev *input = bdata->input;
441 unsigned long flags;
443 BUG_ON(irq != bdata->irq);
445 spin_lock_irqsave(&bdata->lock, flags);
447 if (!bdata->key_pressed) {
448 if (bdata->button->wakeup)
449 pm_wakeup_event(bdata->input->dev.parent, 0);
451 input_event(input, EV_KEY, *bdata->code, 1);
452 input_sync(input);
454 if (!bdata->release_delay) {
455 input_event(input, EV_KEY, *bdata->code, 0);
456 input_sync(input);
457 goto out;
460 bdata->key_pressed = true;
463 if (bdata->release_delay)
464 mod_timer(&bdata->release_timer,
465 jiffies + msecs_to_jiffies(bdata->release_delay));
466 out:
467 spin_unlock_irqrestore(&bdata->lock, flags);
468 return IRQ_HANDLED;
471 static void gpio_keys_quiesce_key(void *data)
473 struct gpio_button_data *bdata = data;
475 if (bdata->gpiod)
476 cancel_delayed_work_sync(&bdata->work);
477 else
478 del_timer_sync(&bdata->release_timer);
481 static int gpio_keys_setup_key(struct platform_device *pdev,
482 struct input_dev *input,
483 struct gpio_keys_drvdata *ddata,
484 const struct gpio_keys_button *button,
485 int idx,
486 struct fwnode_handle *child)
488 const char *desc = button->desc ? button->desc : "gpio_keys";
489 struct device *dev = &pdev->dev;
490 struct gpio_button_data *bdata = &ddata->data[idx];
491 irq_handler_t isr;
492 unsigned long irqflags;
493 int irq;
494 int error;
496 bdata->input = input;
497 bdata->button = button;
498 spin_lock_init(&bdata->lock);
500 if (child) {
501 bdata->gpiod = devm_fwnode_get_gpiod_from_child(dev, NULL,
502 child,
503 GPIOD_IN,
504 desc);
505 if (IS_ERR(bdata->gpiod)) {
506 error = PTR_ERR(bdata->gpiod);
507 if (error == -ENOENT) {
509 * GPIO is optional, we may be dealing with
510 * purely interrupt-driven setup.
512 bdata->gpiod = NULL;
513 } else {
514 if (error != -EPROBE_DEFER)
515 dev_err(dev, "failed to get gpio: %d\n",
516 error);
517 return error;
520 } else if (gpio_is_valid(button->gpio)) {
522 * Legacy GPIO number, so request the GPIO here and
523 * convert it to descriptor.
525 unsigned flags = GPIOF_IN;
527 if (button->active_low)
528 flags |= GPIOF_ACTIVE_LOW;
530 error = devm_gpio_request_one(dev, button->gpio, flags, desc);
531 if (error < 0) {
532 dev_err(dev, "Failed to request GPIO %d, error %d\n",
533 button->gpio, error);
534 return error;
537 bdata->gpiod = gpio_to_desc(button->gpio);
538 if (!bdata->gpiod)
539 return -EINVAL;
542 if (bdata->gpiod) {
543 if (button->debounce_interval) {
544 error = gpiod_set_debounce(bdata->gpiod,
545 button->debounce_interval * 1000);
546 /* use timer if gpiolib doesn't provide debounce */
547 if (error < 0)
548 bdata->software_debounce =
549 button->debounce_interval;
552 if (button->irq) {
553 bdata->irq = button->irq;
554 } else {
555 irq = gpiod_to_irq(bdata->gpiod);
556 if (irq < 0) {
557 error = irq;
558 dev_err(dev,
559 "Unable to get irq number for GPIO %d, error %d\n",
560 button->gpio, error);
561 return error;
563 bdata->irq = irq;
566 INIT_DELAYED_WORK(&bdata->work, gpio_keys_gpio_work_func);
568 isr = gpio_keys_gpio_isr;
569 irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
571 } else {
572 if (!button->irq) {
573 dev_err(dev, "Found button without gpio or irq\n");
574 return -EINVAL;
577 bdata->irq = button->irq;
579 if (button->type && button->type != EV_KEY) {
580 dev_err(dev, "Only EV_KEY allowed for IRQ buttons.\n");
581 return -EINVAL;
584 bdata->release_delay = button->debounce_interval;
585 timer_setup(&bdata->release_timer, gpio_keys_irq_timer, 0);
587 isr = gpio_keys_irq_isr;
588 irqflags = 0;
591 bdata->code = &ddata->keymap[idx];
592 *bdata->code = button->code;
593 input_set_capability(input, button->type ?: EV_KEY, *bdata->code);
596 * Install custom action to cancel release timer and
597 * workqueue item.
599 error = devm_add_action(dev, gpio_keys_quiesce_key, bdata);
600 if (error) {
601 dev_err(dev, "failed to register quiesce action, error: %d\n",
602 error);
603 return error;
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;
613 error = devm_request_any_context_irq(dev, bdata->irq, isr, irqflags,
614 desc, bdata);
615 if (error < 0) {
616 dev_err(dev, "Unable to claim irq %d; error %d\n",
617 bdata->irq, error);
618 return error;
621 return 0;
624 static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata)
626 struct input_dev *input = ddata->input;
627 int i;
629 for (i = 0; i < ddata->pdata->nbuttons; i++) {
630 struct gpio_button_data *bdata = &ddata->data[i];
631 if (bdata->gpiod)
632 gpio_keys_gpio_report_event(bdata);
634 input_sync(input);
637 static int gpio_keys_open(struct input_dev *input)
639 struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
640 const struct gpio_keys_platform_data *pdata = ddata->pdata;
641 int error;
643 if (pdata->enable) {
644 error = pdata->enable(input->dev.parent);
645 if (error)
646 return error;
649 /* Report current state of buttons that are connected to GPIOs */
650 gpio_keys_report_state(ddata);
652 return 0;
655 static void gpio_keys_close(struct input_dev *input)
657 struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
658 const struct gpio_keys_platform_data *pdata = ddata->pdata;
660 if (pdata->disable)
661 pdata->disable(input->dev.parent);
665 * Handlers for alternative sources of platform_data
669 * Translate properties into platform_data
671 static struct gpio_keys_platform_data *
672 gpio_keys_get_devtree_pdata(struct device *dev)
674 struct gpio_keys_platform_data *pdata;
675 struct gpio_keys_button *button;
676 struct fwnode_handle *child;
677 int nbuttons;
679 nbuttons = device_get_child_node_count(dev);
680 if (nbuttons == 0)
681 return ERR_PTR(-ENODEV);
683 pdata = devm_kzalloc(dev,
684 sizeof(*pdata) + nbuttons * sizeof(*button),
685 GFP_KERNEL);
686 if (!pdata)
687 return ERR_PTR(-ENOMEM);
689 button = (struct gpio_keys_button *)(pdata + 1);
691 pdata->buttons = button;
692 pdata->nbuttons = nbuttons;
694 pdata->rep = device_property_read_bool(dev, "autorepeat");
696 device_property_read_string(dev, "label", &pdata->name);
698 device_for_each_child_node(dev, child) {
699 if (is_of_node(child))
700 button->irq =
701 irq_of_parse_and_map(to_of_node(child), 0);
703 if (fwnode_property_read_u32(child, "linux,code",
704 &button->code)) {
705 dev_err(dev, "Button without keycode\n");
706 fwnode_handle_put(child);
707 return ERR_PTR(-EINVAL);
710 fwnode_property_read_string(child, "label", &button->desc);
712 if (fwnode_property_read_u32(child, "linux,input-type",
713 &button->type))
714 button->type = EV_KEY;
716 button->wakeup =
717 fwnode_property_read_bool(child, "wakeup-source") ||
718 /* legacy name */
719 fwnode_property_read_bool(child, "gpio-key,wakeup");
721 button->can_disable =
722 fwnode_property_read_bool(child, "linux,can-disable");
724 if (fwnode_property_read_u32(child, "debounce-interval",
725 &button->debounce_interval))
726 button->debounce_interval = 5;
728 button++;
731 return pdata;
734 static const struct of_device_id gpio_keys_of_match[] = {
735 { .compatible = "gpio-keys", },
736 { },
738 MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
740 static int gpio_keys_probe(struct platform_device *pdev)
742 struct device *dev = &pdev->dev;
743 const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
744 struct fwnode_handle *child = NULL;
745 struct gpio_keys_drvdata *ddata;
746 struct input_dev *input;
747 size_t size;
748 int i, error;
749 int wakeup = 0;
751 if (!pdata) {
752 pdata = gpio_keys_get_devtree_pdata(dev);
753 if (IS_ERR(pdata))
754 return PTR_ERR(pdata);
757 size = sizeof(struct gpio_keys_drvdata) +
758 pdata->nbuttons * sizeof(struct gpio_button_data);
759 ddata = devm_kzalloc(dev, size, GFP_KERNEL);
760 if (!ddata) {
761 dev_err(dev, "failed to allocate state\n");
762 return -ENOMEM;
765 ddata->keymap = devm_kcalloc(dev,
766 pdata->nbuttons, sizeof(ddata->keymap[0]),
767 GFP_KERNEL);
768 if (!ddata->keymap)
769 return -ENOMEM;
771 input = devm_input_allocate_device(dev);
772 if (!input) {
773 dev_err(dev, "failed to allocate input device\n");
774 return -ENOMEM;
777 ddata->pdata = pdata;
778 ddata->input = input;
779 mutex_init(&ddata->disable_lock);
781 platform_set_drvdata(pdev, ddata);
782 input_set_drvdata(input, ddata);
784 input->name = pdata->name ? : pdev->name;
785 input->phys = "gpio-keys/input0";
786 input->dev.parent = dev;
787 input->open = gpio_keys_open;
788 input->close = gpio_keys_close;
790 input->id.bustype = BUS_HOST;
791 input->id.vendor = 0x0001;
792 input->id.product = 0x0001;
793 input->id.version = 0x0100;
795 input->keycode = ddata->keymap;
796 input->keycodesize = sizeof(ddata->keymap[0]);
797 input->keycodemax = pdata->nbuttons;
799 /* Enable auto repeat feature of Linux input subsystem */
800 if (pdata->rep)
801 __set_bit(EV_REP, input->evbit);
803 for (i = 0; i < pdata->nbuttons; i++) {
804 const struct gpio_keys_button *button = &pdata->buttons[i];
806 if (!dev_get_platdata(dev)) {
807 child = device_get_next_child_node(dev, child);
808 if (!child) {
809 dev_err(dev,
810 "missing child device node for entry %d\n",
812 return -EINVAL;
816 error = gpio_keys_setup_key(pdev, input, ddata,
817 button, i, child);
818 if (error) {
819 fwnode_handle_put(child);
820 return error;
823 if (button->wakeup)
824 wakeup = 1;
827 fwnode_handle_put(child);
829 error = devm_device_add_group(dev, &gpio_keys_attr_group);
830 if (error) {
831 dev_err(dev, "Unable to export keys/switches, error: %d\n",
832 error);
833 return error;
836 error = input_register_device(input);
837 if (error) {
838 dev_err(dev, "Unable to register input device, error: %d\n",
839 error);
840 return error;
843 device_init_wakeup(dev, wakeup);
845 return 0;
848 static int __maybe_unused gpio_keys_suspend(struct device *dev)
850 struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
851 struct input_dev *input = ddata->input;
852 int i;
854 if (device_may_wakeup(dev)) {
855 for (i = 0; i < ddata->pdata->nbuttons; i++) {
856 struct gpio_button_data *bdata = &ddata->data[i];
857 if (bdata->button->wakeup)
858 enable_irq_wake(bdata->irq);
859 bdata->suspended = true;
861 } else {
862 mutex_lock(&input->mutex);
863 if (input->users)
864 gpio_keys_close(input);
865 mutex_unlock(&input->mutex);
868 return 0;
871 static int __maybe_unused gpio_keys_resume(struct device *dev)
873 struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
874 struct input_dev *input = ddata->input;
875 int error = 0;
876 int i;
878 if (device_may_wakeup(dev)) {
879 for (i = 0; i < ddata->pdata->nbuttons; i++) {
880 struct gpio_button_data *bdata = &ddata->data[i];
881 if (bdata->button->wakeup)
882 disable_irq_wake(bdata->irq);
883 bdata->suspended = false;
885 } else {
886 mutex_lock(&input->mutex);
887 if (input->users)
888 error = gpio_keys_open(input);
889 mutex_unlock(&input->mutex);
892 if (error)
893 return error;
895 gpio_keys_report_state(ddata);
896 return 0;
899 static SIMPLE_DEV_PM_OPS(gpio_keys_pm_ops, gpio_keys_suspend, gpio_keys_resume);
901 static struct platform_driver gpio_keys_device_driver = {
902 .probe = gpio_keys_probe,
903 .driver = {
904 .name = "gpio-keys",
905 .pm = &gpio_keys_pm_ops,
906 .of_match_table = gpio_keys_of_match,
910 static int __init gpio_keys_init(void)
912 return platform_driver_register(&gpio_keys_device_driver);
915 static void __exit gpio_keys_exit(void)
917 platform_driver_unregister(&gpio_keys_device_driver);
920 late_initcall(gpio_keys_init);
921 module_exit(gpio_keys_exit);
923 MODULE_LICENSE("GPL");
924 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
925 MODULE_DESCRIPTION("Keyboard driver for GPIOs");
926 MODULE_ALIAS("platform:gpio-keys");