Dzmitry Sankouski | bdd50d1 | 2023-01-22 18:21:25 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2023 Dzmitry Sankouski <dsankouski@gmail.com> |
| 4 | */ |
| 5 | |
| 6 | #include <stdlib.h> |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <fdtdec.h> |
| 10 | #include <input.h> |
| 11 | #include <keyboard.h> |
| 12 | #include <button.h> |
| 13 | #include <dm/device-internal.h> |
| 14 | #include <log.h> |
| 15 | #include <asm/io.h> |
| 16 | #include <asm/gpio.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/input.h> |
| 19 | |
| 20 | /** |
| 21 | * struct button_kbd_priv - driver private data |
| 22 | * |
| 23 | * @input: input configuration |
| 24 | * @button_size: number of buttons found |
| 25 | * @old_state: a pointer to old button states array. Used to determine button state change. |
| 26 | */ |
| 27 | struct button_kbd_priv { |
| 28 | struct input_config *input; |
| 29 | u32 button_size; |
| 30 | u32 *old_state; |
| 31 | }; |
| 32 | |
| 33 | static int button_kbd_start(struct udevice *dev) |
| 34 | { |
| 35 | struct button_kbd_priv *priv = dev_get_priv(dev); |
| 36 | int i = 0; |
Caleb Connolly | 573b113 | 2024-04-11 19:52:37 +0200 | [diff] [blame^] | 37 | struct udevice *button_gpio_devp, *next_devp; |
| 38 | struct uclass *uc; |
Dzmitry Sankouski | bdd50d1 | 2023-01-22 18:21:25 +0300 | [diff] [blame] | 39 | |
| 40 | uclass_foreach_dev_probe(UCLASS_BUTTON, button_gpio_devp) { |
| 41 | struct button_uc_plat *uc_plat = dev_get_uclass_plat(button_gpio_devp); |
| 42 | /* Ignore the top-level button node */ |
| 43 | if (!uc_plat->label) |
| 44 | continue; |
| 45 | debug("Found button %s #%d - %s, probing...\n", |
| 46 | uc_plat->label, i, button_gpio_devp->name); |
| 47 | i++; |
| 48 | } |
| 49 | |
Caleb Connolly | 573b113 | 2024-04-11 19:52:37 +0200 | [diff] [blame^] | 50 | if (uclass_get(UCLASS_BUTTON, &uc)) |
| 51 | return -ENOENT; |
| 52 | |
| 53 | /* |
| 54 | * Unbind any buttons that failed to probe so we don't iterate over |
| 55 | * them when polling. |
| 56 | */ |
| 57 | uclass_foreach_dev_safe(button_gpio_devp, next_devp, uc) { |
| 58 | if (!(dev_get_flags(button_gpio_devp) & DM_FLAG_ACTIVATED)) { |
| 59 | log_warning("Button %s failed to probe\n", |
| 60 | button_gpio_devp->name); |
| 61 | device_unbind(button_gpio_devp); |
| 62 | } |
| 63 | } |
| 64 | |
Dzmitry Sankouski | bdd50d1 | 2023-01-22 18:21:25 +0300 | [diff] [blame] | 65 | priv->button_size = i; |
| 66 | priv->old_state = calloc(i, sizeof(int)); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int button_read_keys(struct input_config *input) |
| 72 | { |
| 73 | struct button_kbd_priv *priv = dev_get_priv(input->dev); |
| 74 | struct udevice *button_gpio_devp; |
| 75 | struct uclass *uc; |
| 76 | int i = 0; |
| 77 | u32 code, state, state_changed = 0; |
| 78 | |
| 79 | uclass_id_foreach_dev(UCLASS_BUTTON, button_gpio_devp, uc) { |
| 80 | struct button_uc_plat *uc_plat = dev_get_uclass_plat(button_gpio_devp); |
| 81 | /* Ignore the top-level button node */ |
| 82 | if (!uc_plat->label) |
| 83 | continue; |
| 84 | code = button_get_code(button_gpio_devp); |
| 85 | if (!code) |
| 86 | continue; |
| 87 | |
| 88 | state = button_get_state(button_gpio_devp); |
| 89 | state_changed = state != priv->old_state[i]; |
| 90 | |
| 91 | if (state_changed) { |
| 92 | debug("%s: %d\n", uc_plat->label, code); |
| 93 | priv->old_state[i] = state; |
| 94 | input_add_keycode(input, code, state); |
| 95 | } |
| 96 | i++; |
| 97 | } |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static const struct keyboard_ops button_kbd_ops = { |
| 102 | .start = button_kbd_start, |
| 103 | }; |
| 104 | |
| 105 | static int button_kbd_probe(struct udevice *dev) |
| 106 | { |
| 107 | struct button_kbd_priv *priv = dev_get_priv(dev); |
| 108 | struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev); |
| 109 | struct stdio_dev *sdev = &uc_priv->sdev; |
| 110 | struct input_config *input = &uc_priv->input; |
| 111 | int ret = 0; |
| 112 | |
| 113 | input_init(input, false); |
| 114 | input_add_tables(input, false); |
| 115 | |
| 116 | /* Register the device. */ |
| 117 | priv->input = input; |
| 118 | input->dev = dev; |
| 119 | input->read_keys = button_read_keys; |
| 120 | strcpy(sdev->name, "button-kbd"); |
| 121 | ret = input_stdio_register(sdev); |
| 122 | if (ret) { |
| 123 | debug("%s: input_stdio_register() failed\n", __func__); |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
Dzmitry Sankouski | bdd50d1 | 2023-01-22 18:21:25 +0300 | [diff] [blame] | 130 | U_BOOT_DRIVER(button_kbd) = { |
| 131 | .name = "button_kbd", |
| 132 | .id = UCLASS_KEYBOARD, |
Dzmitry Sankouski | bdd50d1 | 2023-01-22 18:21:25 +0300 | [diff] [blame] | 133 | .ops = &button_kbd_ops, |
| 134 | .priv_auto = sizeof(struct button_kbd_priv), |
| 135 | .probe = button_kbd_probe, |
| 136 | }; |
Svyatoslav Ryhel | 0b9f89d | 2023-03-20 21:06:30 +0200 | [diff] [blame] | 137 | |
| 138 | U_BOOT_DRVINFO(button_kbd) = { |
| 139 | .name = "button_kbd" |
| 140 | }; |