Lines Matching +full:button +full:-

1 // SPDX-License-Identifier: GPL-2.0-only
31 #include <dt-bindings/input/gpio-keys.h>
34 const struct gpio_keys_button *button; member
41 unsigned int release_delay; /* in msecs, for IRQ-only buttons */
45 unsigned int software_debounce; /* in msecs, for GPIO-driven buttons */
69 * There are 4 attributes under /sys/devices/platform/gpio-keys/
70 * keys [ro] - bitmap of keys (EV_KEY) which can be
72 * switches [ro] - bitmap of switches (EV_SW) which can be
74 * disabled_keys [rw] - bitmap of keys currently disabled
75 * disabled_switches [rw] - bitmap of switches currently disabled
81 * For example, if we have following switches set up as gpio-keys:
87 * 11-9,5
101 * get_n_events_by_type() - returns maximum number of events per @type
102 * @type: type of button (%EV_KEY, %EV_SW)
115 * get_bm_events_by_type() - returns bitmap of supported events per @type
117 * @type: type of button (%EV_KEY, %EV_SW)
127 return (type == EV_KEY) ? dev->keybit : dev->swbit; in get_bm_events_by_type()
134 if (!bdata->gpiod) in gpio_keys_quiesce_key()
135 hrtimer_cancel(&bdata->release_timer); in gpio_keys_quiesce_key()
136 else if (bdata->debounce_use_hrtimer) in gpio_keys_quiesce_key()
137 hrtimer_cancel(&bdata->debounce_timer); in gpio_keys_quiesce_key()
139 cancel_delayed_work_sync(&bdata->work); in gpio_keys_quiesce_key()
143 * gpio_keys_disable_button() - disables given GPIO button
144 * @bdata: button data for button to be disabled
146 * Disables button pointed by @bdata. This is done by masking
147 * IRQ line. After this function is called, button won't generate
151 * Make sure that @bdata->disable_lock is locked when entering
157 if (!bdata->disabled) { in gpio_keys_disable_button()
161 disable_irq(bdata->irq); in gpio_keys_disable_button()
163 bdata->disabled = true; in gpio_keys_disable_button()
168 * gpio_keys_enable_button() - enables given GPIO button
169 * @bdata: button data for button to be disabled
171 * Enables given button pointed by @bdata.
173 * Make sure that @bdata->disable_lock is locked when entering
175 * to enable the same button at the same time.
179 if (bdata->disabled) { in gpio_keys_enable_button()
180 enable_irq(bdata->irq); in gpio_keys_enable_button()
181 bdata->disabled = false; in gpio_keys_enable_button()
186 * gpio_keys_attr_show_helper() - fill in stringified bitmap of buttons
189 * @type: button type (%EV_KEY, %EV_SW)
210 return -ENOMEM; in gpio_keys_attr_show_helper()
212 for (i = 0; i < ddata->pdata->nbuttons; i++) { in gpio_keys_attr_show_helper()
213 struct gpio_button_data *bdata = &ddata->data[i]; in gpio_keys_attr_show_helper()
215 if (bdata->button->type != type) in gpio_keys_attr_show_helper()
218 if (only_disabled && !bdata->disabled) in gpio_keys_attr_show_helper()
221 __set_bit(*bdata->code, bits); in gpio_keys_attr_show_helper()
224 ret = scnprintf(buf, PAGE_SIZE - 1, "%*pbl", n_events, bits); in gpio_keys_attr_show_helper()
234 * gpio_keys_attr_store_helper() - enable/disable buttons based on given bitmap
237 * @type: button type (%EV_KEY, %EV_SW)
247 const unsigned long *bitmap = get_bm_events_by_type(ddata->input, type); in gpio_keys_attr_store_helper()
253 return -ENOMEM; in gpio_keys_attr_store_helper()
261 return -EINVAL; in gpio_keys_attr_store_helper()
263 for (i = 0; i < ddata->pdata->nbuttons; i++) { in gpio_keys_attr_store_helper()
264 struct gpio_button_data *bdata = &ddata->data[i]; in gpio_keys_attr_store_helper()
266 if (bdata->button->type != type) in gpio_keys_attr_store_helper()
269 if (test_bit(*bdata->code, bits) && in gpio_keys_attr_store_helper()
270 !bdata->button->can_disable) { in gpio_keys_attr_store_helper()
271 return -EINVAL; in gpio_keys_attr_store_helper()
275 guard(mutex)(&ddata->disable_lock); in gpio_keys_attr_store_helper()
277 for (i = 0; i < ddata->pdata->nbuttons; i++) { in gpio_keys_attr_store_helper()
278 struct gpio_button_data *bdata = &ddata->data[i]; in gpio_keys_attr_store_helper()
280 if (bdata->button->type != type) in gpio_keys_attr_store_helper()
283 if (test_bit(*bdata->code, bits)) in gpio_keys_attr_store_helper()
312 * /sys/devices/platform/gpio-keys/keys [ro]
313 * /sys/devices/platform/gpio-keys/switches [ro]
341 * /sys/devices/platform/gpio-keys/disabled_keys [rw]
342 * /sys/devices/platform/gpio-keys/disables_switches [rw]
362 const struct gpio_keys_button *button = bdata->button; in gpio_keys_gpio_report_event() local
363 struct input_dev *input = bdata->input; in gpio_keys_gpio_report_event()
364 unsigned int type = button->type ?: EV_KEY; in gpio_keys_gpio_report_event()
367 state = bdata->debounce_use_hrtimer ? in gpio_keys_gpio_report_event()
368 gpiod_get_value(bdata->gpiod) : in gpio_keys_gpio_report_event()
369 gpiod_get_value_cansleep(bdata->gpiod); in gpio_keys_gpio_report_event()
371 dev_err(input->dev.parent, in gpio_keys_gpio_report_event()
378 input_event(input, type, button->code, button->value); in gpio_keys_gpio_report_event()
380 input_event(input, type, *bdata->code, state); in gpio_keys_gpio_report_event()
387 input_sync(bdata->input); in gpio_keys_debounce_event()
389 if (bdata->button->wakeup) in gpio_keys_debounce_event()
390 pm_relax(bdata->input->dev.parent); in gpio_keys_debounce_event()
415 BUG_ON(irq != bdata->irq); in gpio_keys_gpio_isr()
417 if (bdata->button->wakeup) { in gpio_keys_gpio_isr()
418 const struct gpio_keys_button *button = bdata->button; in gpio_keys_gpio_isr() local
420 pm_stay_awake(bdata->input->dev.parent); in gpio_keys_gpio_isr()
421 if (bdata->suspended && in gpio_keys_gpio_isr()
422 (button->type == 0 || button->type == EV_KEY)) { in gpio_keys_gpio_isr()
428 input_report_key(bdata->input, button->code, 1); in gpio_keys_gpio_isr()
432 if (bdata->debounce_use_hrtimer) { in gpio_keys_gpio_isr()
433 hrtimer_start(&bdata->debounce_timer, in gpio_keys_gpio_isr()
434 ms_to_ktime(bdata->software_debounce), in gpio_keys_gpio_isr()
438 &bdata->work, in gpio_keys_gpio_isr()
439 msecs_to_jiffies(bdata->software_debounce)); in gpio_keys_gpio_isr()
450 struct input_dev *input = bdata->input; in gpio_keys_irq_timer()
452 if (bdata->key_pressed) { in gpio_keys_irq_timer()
453 input_report_key(input, *bdata->code, 0); in gpio_keys_irq_timer()
455 bdata->key_pressed = false; in gpio_keys_irq_timer()
464 struct input_dev *input = bdata->input; in gpio_keys_irq_isr()
466 BUG_ON(irq != bdata->irq); in gpio_keys_irq_isr()
468 guard(spinlock_irqsave)(&bdata->lock); in gpio_keys_irq_isr()
470 if (!bdata->key_pressed) { in gpio_keys_irq_isr()
471 if (bdata->button->wakeup) in gpio_keys_irq_isr()
472 pm_wakeup_event(bdata->input->dev.parent, 0); in gpio_keys_irq_isr()
474 input_report_key(input, *bdata->code, 1); in gpio_keys_irq_isr()
477 if (!bdata->release_delay) { in gpio_keys_irq_isr()
478 input_report_key(input, *bdata->code, 0); in gpio_keys_irq_isr()
483 bdata->key_pressed = true; in gpio_keys_irq_isr()
486 if (bdata->release_delay) in gpio_keys_irq_isr()
487 hrtimer_start(&bdata->release_timer, in gpio_keys_irq_isr()
488 ms_to_ktime(bdata->release_delay), in gpio_keys_irq_isr()
497 const struct gpio_keys_button *button, in gpio_keys_setup_key() argument
501 const char *desc = button->desc ? button->desc : "gpio_keys"; in gpio_keys_setup_key()
502 struct device *dev = &pdev->dev; in gpio_keys_setup_key()
503 struct gpio_button_data *bdata = &ddata->data[idx]; in gpio_keys_setup_key()
510 bdata->input = input; in gpio_keys_setup_key()
511 bdata->button = button; in gpio_keys_setup_key()
512 spin_lock_init(&bdata->lock); in gpio_keys_setup_key()
515 bdata->gpiod = devm_fwnode_gpiod_get(dev, child, in gpio_keys_setup_key()
517 if (IS_ERR(bdata->gpiod)) { in gpio_keys_setup_key()
518 error = PTR_ERR(bdata->gpiod); in gpio_keys_setup_key()
519 if (error != -ENOENT) in gpio_keys_setup_key()
525 * purely interrupt-driven setup. in gpio_keys_setup_key()
527 bdata->gpiod = NULL; in gpio_keys_setup_key()
529 } else if (gpio_is_valid(button->gpio)) { in gpio_keys_setup_key()
536 if (button->active_low) in gpio_keys_setup_key()
539 error = devm_gpio_request_one(dev, button->gpio, flags, desc); in gpio_keys_setup_key()
542 button->gpio, error); in gpio_keys_setup_key()
546 bdata->gpiod = gpio_to_desc(button->gpio); in gpio_keys_setup_key()
547 if (!bdata->gpiod) in gpio_keys_setup_key()
548 return -EINVAL; in gpio_keys_setup_key()
551 if (bdata->gpiod) { in gpio_keys_setup_key()
552 bool active_low = gpiod_is_active_low(bdata->gpiod); in gpio_keys_setup_key()
554 if (button->debounce_interval) { in gpio_keys_setup_key()
555 error = gpiod_set_debounce(bdata->gpiod, in gpio_keys_setup_key()
556 button->debounce_interval * 1000); in gpio_keys_setup_key()
559 bdata->software_debounce = in gpio_keys_setup_key()
560 button->debounce_interval; in gpio_keys_setup_key()
567 bdata->debounce_use_hrtimer = in gpio_keys_setup_key()
568 !gpiod_cansleep(bdata->gpiod); in gpio_keys_setup_key()
574 * interrupt may be used as the main button interrupt for in gpio_keys_setup_key()
577 * for bdata->wakeirq setup. in gpio_keys_setup_key()
579 if (button->irq) { in gpio_keys_setup_key()
580 bdata->irq = button->irq; in gpio_keys_setup_key()
582 irq = gpiod_to_irq(bdata->gpiod); in gpio_keys_setup_key()
587 button->gpio); in gpio_keys_setup_key()
590 bdata->irq = irq; in gpio_keys_setup_key()
593 INIT_DELAYED_WORK(&bdata->work, gpio_keys_gpio_work_func); in gpio_keys_setup_key()
595 hrtimer_init(&bdata->debounce_timer, in gpio_keys_setup_key()
597 bdata->debounce_timer.function = gpio_keys_debounce_timer; in gpio_keys_setup_key()
602 switch (button->wakeup_event_action) { in gpio_keys_setup_key()
604 bdata->wakeup_trigger_type = active_low ? in gpio_keys_setup_key()
608 bdata->wakeup_trigger_type = active_low ? in gpio_keys_setup_key()
620 if (!button->irq) { in gpio_keys_setup_key()
621 dev_err(dev, "Found button without gpio or irq\n"); in gpio_keys_setup_key()
622 return -EINVAL; in gpio_keys_setup_key()
625 bdata->irq = button->irq; in gpio_keys_setup_key()
627 if (button->type && button->type != EV_KEY) { in gpio_keys_setup_key()
629 return -EINVAL; in gpio_keys_setup_key()
632 bdata->release_delay = button->debounce_interval; in gpio_keys_setup_key()
633 hrtimer_init(&bdata->release_timer, in gpio_keys_setup_key()
635 bdata->release_timer.function = gpio_keys_irq_timer; in gpio_keys_setup_key()
646 bdata->code = &ddata->keymap[idx]; in gpio_keys_setup_key()
647 *bdata->code = button->code; in gpio_keys_setup_key()
648 input_set_capability(input, button->type ?: EV_KEY, *bdata->code); in gpio_keys_setup_key()
662 * If platform has specified that the button can be disabled, in gpio_keys_setup_key()
665 if (!button->can_disable) in gpio_keys_setup_key()
668 error = devm_request_any_context_irq(dev, bdata->irq, isr, irqflags, in gpio_keys_setup_key()
672 bdata->irq, error); in gpio_keys_setup_key()
676 if (!button->wakeirq) in gpio_keys_setup_key()
682 return -ENOMEM; in gpio_keys_setup_key()
684 bdata->wakeirq = button->wakeirq; in gpio_keys_setup_key()
692 error = devm_request_any_context_irq(dev, bdata->wakeirq, isr, in gpio_keys_setup_key()
696 bdata->irq, error); in gpio_keys_setup_key()
702 * IRQF_SHARED was set based on !button->can_disable. in gpio_keys_setup_key()
704 disable_irq(bdata->wakeirq); in gpio_keys_setup_key()
711 struct input_dev *input = ddata->input; in gpio_keys_report_state()
714 for (i = 0; i < ddata->pdata->nbuttons; i++) { in gpio_keys_report_state()
715 struct gpio_button_data *bdata = &ddata->data[i]; in gpio_keys_report_state()
716 if (bdata->gpiod) in gpio_keys_report_state()
725 const struct gpio_keys_platform_data *pdata = ddata->pdata; in gpio_keys_open()
728 if (pdata->enable) { in gpio_keys_open()
729 error = pdata->enable(input->dev.parent); in gpio_keys_open()
743 const struct gpio_keys_platform_data *pdata = ddata->pdata; in gpio_keys_close()
745 if (pdata->disable) in gpio_keys_close()
746 pdata->disable(input->dev.parent); in gpio_keys_close()
760 struct gpio_keys_button *button; in gpio_keys_get_devtree_pdata() local
765 return ERR_PTR(-ENODEV); in gpio_keys_get_devtree_pdata()
768 sizeof(*pdata) + nbuttons * sizeof(*button), in gpio_keys_get_devtree_pdata()
771 return ERR_PTR(-ENOMEM); in gpio_keys_get_devtree_pdata()
773 button = (struct gpio_keys_button *)(pdata + 1); in gpio_keys_get_devtree_pdata()
775 pdata->buttons = button; in gpio_keys_get_devtree_pdata()
776 pdata->nbuttons = nbuttons; in gpio_keys_get_devtree_pdata()
778 pdata->rep = device_property_read_bool(dev, "autorepeat"); in gpio_keys_get_devtree_pdata()
780 device_property_read_string(dev, "label", &pdata->name); in gpio_keys_get_devtree_pdata()
786 button->irq = irq; in gpio_keys_get_devtree_pdata()
790 button->wakeirq = irq; in gpio_keys_get_devtree_pdata()
792 if (!button->irq && !button->wakeirq) in gpio_keys_get_devtree_pdata()
793 button->irq = in gpio_keys_get_devtree_pdata()
798 &button->code)) { in gpio_keys_get_devtree_pdata()
799 dev_err(dev, "Button without keycode\n"); in gpio_keys_get_devtree_pdata()
800 return ERR_PTR(-EINVAL); in gpio_keys_get_devtree_pdata()
803 fwnode_property_read_string(child, "label", &button->desc); in gpio_keys_get_devtree_pdata()
805 if (fwnode_property_read_u32(child, "linux,input-type", in gpio_keys_get_devtree_pdata()
806 &button->type)) in gpio_keys_get_devtree_pdata()
807 button->type = EV_KEY; in gpio_keys_get_devtree_pdata()
809 fwnode_property_read_u32(child, "linux,input-value", in gpio_keys_get_devtree_pdata()
810 (u32 *)&button->value); in gpio_keys_get_devtree_pdata()
812 button->wakeup = in gpio_keys_get_devtree_pdata()
813 fwnode_property_read_bool(child, "wakeup-source") || in gpio_keys_get_devtree_pdata()
815 fwnode_property_read_bool(child, "gpio-key,wakeup"); in gpio_keys_get_devtree_pdata()
817 fwnode_property_read_u32(child, "wakeup-event-action", in gpio_keys_get_devtree_pdata()
818 &button->wakeup_event_action); in gpio_keys_get_devtree_pdata()
820 button->can_disable = in gpio_keys_get_devtree_pdata()
821 fwnode_property_read_bool(child, "linux,can-disable"); in gpio_keys_get_devtree_pdata()
823 if (fwnode_property_read_u32(child, "debounce-interval", in gpio_keys_get_devtree_pdata()
824 &button->debounce_interval)) in gpio_keys_get_devtree_pdata()
825 button->debounce_interval = 5; in gpio_keys_get_devtree_pdata()
827 button++; in gpio_keys_get_devtree_pdata()
834 { .compatible = "gpio-keys", },
841 struct device *dev = &pdev->dev; in gpio_keys_probe()
855 ddata = devm_kzalloc(dev, struct_size(ddata, data, pdata->nbuttons), in gpio_keys_probe()
859 return -ENOMEM; in gpio_keys_probe()
862 ddata->keymap = devm_kcalloc(dev, in gpio_keys_probe()
863 pdata->nbuttons, sizeof(ddata->keymap[0]), in gpio_keys_probe()
865 if (!ddata->keymap) in gpio_keys_probe()
866 return -ENOMEM; in gpio_keys_probe()
871 return -ENOMEM; in gpio_keys_probe()
874 ddata->pdata = pdata; in gpio_keys_probe()
875 ddata->input = input; in gpio_keys_probe()
876 mutex_init(&ddata->disable_lock); in gpio_keys_probe()
881 input->name = pdata->name ? : pdev->name; in gpio_keys_probe()
882 input->phys = "gpio-keys/input0"; in gpio_keys_probe()
883 input->dev.parent = dev; in gpio_keys_probe()
884 input->open = gpio_keys_open; in gpio_keys_probe()
885 input->close = gpio_keys_close; in gpio_keys_probe()
887 input->id.bustype = BUS_HOST; in gpio_keys_probe()
888 input->id.vendor = 0x0001; in gpio_keys_probe()
889 input->id.product = 0x0001; in gpio_keys_probe()
890 input->id.version = 0x0100; in gpio_keys_probe()
892 input->keycode = ddata->keymap; in gpio_keys_probe()
893 input->keycodesize = sizeof(ddata->keymap[0]); in gpio_keys_probe()
894 input->keycodemax = pdata->nbuttons; in gpio_keys_probe()
897 if (pdata->rep) in gpio_keys_probe()
898 __set_bit(EV_REP, input->evbit); in gpio_keys_probe()
900 for (i = 0; i < pdata->nbuttons; i++) { in gpio_keys_probe()
901 const struct gpio_keys_button *button = &pdata->buttons[i]; in gpio_keys_probe() local
909 return -EINVAL; in gpio_keys_probe()
914 button, i, child); in gpio_keys_probe()
920 if (button->wakeup) in gpio_keys_probe()
943 error = enable_irq_wake(bdata->irq); in gpio_keys_button_enable_wakeup()
945 dev_err(bdata->input->dev.parent, in gpio_keys_button_enable_wakeup()
947 bdata->irq, error); in gpio_keys_button_enable_wakeup()
951 if (bdata->wakeup_trigger_type) { in gpio_keys_button_enable_wakeup()
952 error = irq_set_irq_type(bdata->irq, in gpio_keys_button_enable_wakeup()
953 bdata->wakeup_trigger_type); in gpio_keys_button_enable_wakeup()
955 dev_err(bdata->input->dev.parent, in gpio_keys_button_enable_wakeup()
957 bdata->wakeup_trigger_type, bdata->irq, error); in gpio_keys_button_enable_wakeup()
958 disable_irq_wake(bdata->irq); in gpio_keys_button_enable_wakeup()
963 if (bdata->wakeirq) { in gpio_keys_button_enable_wakeup()
964 enable_irq(bdata->wakeirq); in gpio_keys_button_enable_wakeup()
965 disable_irq(bdata->irq); in gpio_keys_button_enable_wakeup()
976 if (bdata->wakeirq) { in gpio_keys_button_disable_wakeup()
977 enable_irq(bdata->irq); in gpio_keys_button_disable_wakeup()
978 disable_irq(bdata->wakeirq); in gpio_keys_button_disable_wakeup()
982 * The trigger type is always both edges for gpio-based keys and we do in gpio_keys_button_disable_wakeup()
983 * not support changing wakeup trigger for interrupt-based keys. in gpio_keys_button_disable_wakeup()
985 if (bdata->wakeup_trigger_type) { in gpio_keys_button_disable_wakeup()
986 error = irq_set_irq_type(bdata->irq, IRQ_TYPE_EDGE_BOTH); in gpio_keys_button_disable_wakeup()
988 dev_warn(bdata->input->dev.parent, in gpio_keys_button_disable_wakeup()
990 bdata->irq, error); in gpio_keys_button_disable_wakeup()
993 error = disable_irq_wake(bdata->irq); in gpio_keys_button_disable_wakeup()
995 dev_warn(bdata->input->dev.parent, in gpio_keys_button_disable_wakeup()
997 bdata->irq, error); in gpio_keys_button_disable_wakeup()
1007 for (i = 0; i < ddata->pdata->nbuttons; i++) { in gpio_keys_enable_wakeup()
1008 bdata = &ddata->data[i]; in gpio_keys_enable_wakeup()
1009 if (bdata->button->wakeup) { in gpio_keys_enable_wakeup()
1014 bdata->suspended = true; in gpio_keys_enable_wakeup()
1020 while (i--) { in gpio_keys_enable_wakeup()
1021 bdata = &ddata->data[i]; in gpio_keys_enable_wakeup()
1022 if (bdata->button->wakeup) in gpio_keys_enable_wakeup()
1024 bdata->suspended = false; in gpio_keys_enable_wakeup()
1036 for (i = 0; i < ddata->pdata->nbuttons; i++) { in gpio_keys_disable_wakeup()
1037 bdata = &ddata->data[i]; in gpio_keys_disable_wakeup()
1038 bdata->suspended = false; in gpio_keys_disable_wakeup()
1039 if (irqd_is_wakeup_set(irq_get_irq_data(bdata->irq))) in gpio_keys_disable_wakeup()
1047 struct input_dev *input = ddata->input; in gpio_keys_suspend()
1055 guard(mutex)(&input->mutex); in gpio_keys_suspend()
1067 struct input_dev *input = ddata->input; in gpio_keys_resume()
1073 guard(mutex)(&input->mutex); in gpio_keys_resume()
1092 ret = gpio_keys_suspend(&pdev->dev); in gpio_keys_shutdown()
1094 dev_err(&pdev->dev, "failed to shutdown\n"); in gpio_keys_shutdown()
1101 .name = "gpio-keys",
1124 MODULE_ALIAS("platform:gpio-keys");