blob: 43b82d98aeb7f9c26aa113b38b2351dab1a55766 [file] [log] [blame]
Philippe Reynes7686d9f2020-07-24 18:19:46 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2020 Philippe Reynes <philippe.reynes@softathome.com>
4 */
5
Philippe Reynes7686d9f2020-07-24 18:19:46 +02006#include <button.h>
7#include <dm.h>
8#include <dm/lists.h>
9#include <dm/uclass-internal.h>
10#include <log.h>
11#include <asm/gpio.h>
12
13struct button_gpio_priv {
14 struct gpio_desc gpio;
Dzmitry Sankouski157f2c52023-01-22 18:21:24 +030015 int linux_code;
Philippe Reynes7686d9f2020-07-24 18:19:46 +020016};
17
18static enum button_state_t button_gpio_get_state(struct udevice *dev)
19{
20 struct button_gpio_priv *priv = dev_get_priv(dev);
21 int ret;
22
23 if (!dm_gpio_is_valid(&priv->gpio))
24 return -EREMOTEIO;
25 ret = dm_gpio_get_value(&priv->gpio);
26 if (ret < 0)
27 return ret;
28
29 return ret ? BUTTON_ON : BUTTON_OFF;
30}
31
Dzmitry Sankouski157f2c52023-01-22 18:21:24 +030032static int button_gpio_get_code(struct udevice *dev)
33{
34 struct button_gpio_priv *priv = dev_get_priv(dev);
35 int code = priv->linux_code;
36
37 if (!code)
38 return -ENODATA;
39
40 return code;
41}
42
Philippe Reynes7686d9f2020-07-24 18:19:46 +020043static int button_gpio_probe(struct udevice *dev)
44{
Simon Glass71fa5b42020-12-03 16:55:18 -070045 struct button_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Philippe Reynes7686d9f2020-07-24 18:19:46 +020046 struct button_gpio_priv *priv = dev_get_priv(dev);
47 int ret;
48
49 /* Ignore the top-level button node */
50 if (!uc_plat->label)
51 return 0;
52
53 ret = gpio_request_by_name(dev, "gpios", 0, &priv->gpio, GPIOD_IS_IN);
54 if (ret)
55 return ret;
56
Dzmitry Sankouski157f2c52023-01-22 18:21:24 +030057 ret = dev_read_u32(dev, "linux,code", &priv->linux_code);
58
59 return ret;
Philippe Reynes7686d9f2020-07-24 18:19:46 +020060}
61
62static int button_gpio_remove(struct udevice *dev)
63{
64 /*
65 * The GPIO driver may have already been removed. We will need to
66 * address this more generally.
67 */
68 if (!IS_ENABLED(CONFIG_SANDBOX)) {
69 struct button_gpio_priv *priv = dev_get_priv(dev);
70
71 if (dm_gpio_is_valid(&priv->gpio))
72 dm_gpio_free(dev, &priv->gpio);
73 }
74
75 return 0;
76}
77
78static int button_gpio_bind(struct udevice *parent)
79{
80 struct udevice *dev;
81 ofnode node;
82 int ret;
83
84 dev_for_each_subnode(node, parent) {
85 struct button_uc_plat *uc_plat;
86 const char *label;
87
88 label = ofnode_read_string(node, "label");
89 if (!label) {
90 debug("%s: node %s has no label\n", __func__,
91 ofnode_get_name(node));
92 return -EINVAL;
93 }
94 ret = device_bind_driver_to_node(parent, "button_gpio",
95 ofnode_get_name(node),
96 node, &dev);
97 if (ret)
98 return ret;
Simon Glass71fa5b42020-12-03 16:55:18 -070099 uc_plat = dev_get_uclass_plat(dev);
Philippe Reynes7686d9f2020-07-24 18:19:46 +0200100 uc_plat->label = label;
101 }
102
103 return 0;
104}
105
106static const struct button_ops button_gpio_ops = {
107 .get_state = button_gpio_get_state,
Dzmitry Sankouski157f2c52023-01-22 18:21:24 +0300108 .get_code = button_gpio_get_code,
Philippe Reynes7686d9f2020-07-24 18:19:46 +0200109};
110
111static const struct udevice_id button_gpio_ids[] = {
112 { .compatible = "gpio-keys" },
113 { .compatible = "gpio-keys-polled" },
114 { }
115};
116
117U_BOOT_DRIVER(button_gpio) = {
118 .name = "button_gpio",
119 .id = UCLASS_BUTTON,
120 .of_match = button_gpio_ids,
121 .ops = &button_gpio_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700122 .priv_auto = sizeof(struct button_gpio_priv),
Philippe Reynes7686d9f2020-07-24 18:19:46 +0200123 .bind = button_gpio_bind,
124 .probe = button_gpio_probe,
125 .remove = button_gpio_remove,
126};