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