blob: 31b85c8150e83134ec42523a238905295543041c [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
Caleb Connollycd8f9b92024-11-13 05:59:24 +010023 if (!priv)
24 return -ENODATA;
25
Philippe Reynes7686d9f2020-07-24 18:19:46 +020026 if (!dm_gpio_is_valid(&priv->gpio))
27 return -EREMOTEIO;
28 ret = dm_gpio_get_value(&priv->gpio);
29 if (ret < 0)
30 return ret;
31
32 return ret ? BUTTON_ON : BUTTON_OFF;
33}
34
Dzmitry Sankouski157f2c52023-01-22 18:21:24 +030035static int button_gpio_get_code(struct udevice *dev)
36{
37 struct button_gpio_priv *priv = dev_get_priv(dev);
Caleb Connollycd8f9b92024-11-13 05:59:24 +010038 if (!priv)
39 return -ENODATA;
Dzmitry Sankouski157f2c52023-01-22 18:21:24 +030040 int code = priv->linux_code;
41
42 if (!code)
43 return -ENODATA;
44
45 return code;
46}
47
Philippe Reynes7686d9f2020-07-24 18:19:46 +020048static int button_gpio_probe(struct udevice *dev)
49{
Simon Glass71fa5b42020-12-03 16:55:18 -070050 struct button_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Philippe Reynes7686d9f2020-07-24 18:19:46 +020051 struct button_gpio_priv *priv = dev_get_priv(dev);
52 int ret;
53
54 /* Ignore the top-level button node */
55 if (!uc_plat->label)
56 return 0;
57
58 ret = gpio_request_by_name(dev, "gpios", 0, &priv->gpio, GPIOD_IS_IN);
Caleb Connollycd8f9b92024-11-13 05:59:24 +010059 if (ret || !dm_gpio_is_valid(&priv->gpio))
Philippe Reynes7686d9f2020-07-24 18:19:46 +020060 return ret;
61
Dzmitry Sankouski157f2c52023-01-22 18:21:24 +030062 ret = dev_read_u32(dev, "linux,code", &priv->linux_code);
63
64 return ret;
Philippe Reynes7686d9f2020-07-24 18:19:46 +020065}
66
67static int button_gpio_remove(struct udevice *dev)
68{
69 /*
70 * The GPIO driver may have already been removed. We will need to
71 * address this more generally.
72 */
73 if (!IS_ENABLED(CONFIG_SANDBOX)) {
74 struct button_gpio_priv *priv = dev_get_priv(dev);
75
76 if (dm_gpio_is_valid(&priv->gpio))
77 dm_gpio_free(dev, &priv->gpio);
78 }
79
80 return 0;
81}
82
83static int button_gpio_bind(struct udevice *parent)
84{
85 struct udevice *dev;
86 ofnode node;
87 int ret;
88
89 dev_for_each_subnode(node, parent) {
90 struct button_uc_plat *uc_plat;
91 const char *label;
92
93 label = ofnode_read_string(node, "label");
94 if (!label) {
95 debug("%s: node %s has no label\n", __func__,
96 ofnode_get_name(node));
97 return -EINVAL;
98 }
99 ret = device_bind_driver_to_node(parent, "button_gpio",
100 ofnode_get_name(node),
101 node, &dev);
102 if (ret)
103 return ret;
Simon Glass71fa5b42020-12-03 16:55:18 -0700104 uc_plat = dev_get_uclass_plat(dev);
Philippe Reynes7686d9f2020-07-24 18:19:46 +0200105 uc_plat->label = label;
Caleb Connollycd8f9b92024-11-13 05:59:24 +0100106 debug("Button '%s' bound to driver '%s'\n", label,
107 dev->driver->name);
Philippe Reynes7686d9f2020-07-24 18:19:46 +0200108 }
109
110 return 0;
111}
112
113static const struct button_ops button_gpio_ops = {
114 .get_state = button_gpio_get_state,
Dzmitry Sankouski157f2c52023-01-22 18:21:24 +0300115 .get_code = button_gpio_get_code,
Philippe Reynes7686d9f2020-07-24 18:19:46 +0200116};
117
118static const struct udevice_id button_gpio_ids[] = {
119 { .compatible = "gpio-keys" },
120 { .compatible = "gpio-keys-polled" },
121 { }
122};
123
124U_BOOT_DRIVER(button_gpio) = {
125 .name = "button_gpio",
126 .id = UCLASS_BUTTON,
127 .of_match = button_gpio_ids,
128 .ops = &button_gpio_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700129 .priv_auto = sizeof(struct button_gpio_priv),
Philippe Reynes7686d9f2020-07-24 18:19:46 +0200130 .bind = button_gpio_bind,
131 .probe = button_gpio_probe,
132 .remove = button_gpio_remove,
133};