Philippe Reynes | 7686d9f | 2020-07-24 18:19:46 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2020 Philippe Reynes <philippe.reynes@softathome.com> |
| 4 | */ |
| 5 | |
Philippe Reynes | 7686d9f | 2020-07-24 18:19:46 +0200 | [diff] [blame] | 6 | #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 | |
| 13 | struct button_gpio_priv { |
| 14 | struct gpio_desc gpio; |
Dzmitry Sankouski | 157f2c5 | 2023-01-22 18:21:24 +0300 | [diff] [blame] | 15 | int linux_code; |
Philippe Reynes | 7686d9f | 2020-07-24 18:19:46 +0200 | [diff] [blame] | 16 | }; |
| 17 | |
| 18 | static 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 Sankouski | 157f2c5 | 2023-01-22 18:21:24 +0300 | [diff] [blame] | 32 | static 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 Reynes | 7686d9f | 2020-07-24 18:19:46 +0200 | [diff] [blame] | 43 | static int button_gpio_probe(struct udevice *dev) |
| 44 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 45 | struct button_uc_plat *uc_plat = dev_get_uclass_plat(dev); |
Philippe Reynes | 7686d9f | 2020-07-24 18:19:46 +0200 | [diff] [blame] | 46 | 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 Sankouski | 157f2c5 | 2023-01-22 18:21:24 +0300 | [diff] [blame] | 57 | ret = dev_read_u32(dev, "linux,code", &priv->linux_code); |
| 58 | |
| 59 | return ret; |
Philippe Reynes | 7686d9f | 2020-07-24 18:19:46 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static 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 | |
| 78 | static 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 Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 99 | uc_plat = dev_get_uclass_plat(dev); |
Philippe Reynes | 7686d9f | 2020-07-24 18:19:46 +0200 | [diff] [blame] | 100 | uc_plat->label = label; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static const struct button_ops button_gpio_ops = { |
| 107 | .get_state = button_gpio_get_state, |
Dzmitry Sankouski | 157f2c5 | 2023-01-22 18:21:24 +0300 | [diff] [blame] | 108 | .get_code = button_gpio_get_code, |
Philippe Reynes | 7686d9f | 2020-07-24 18:19:46 +0200 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | static const struct udevice_id button_gpio_ids[] = { |
| 112 | { .compatible = "gpio-keys" }, |
| 113 | { .compatible = "gpio-keys-polled" }, |
| 114 | { } |
| 115 | }; |
| 116 | |
| 117 | U_BOOT_DRIVER(button_gpio) = { |
| 118 | .name = "button_gpio", |
| 119 | .id = UCLASS_BUTTON, |
| 120 | .of_match = button_gpio_ids, |
| 121 | .ops = &button_gpio_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 122 | .priv_auto = sizeof(struct button_gpio_priv), |
Philippe Reynes | 7686d9f | 2020-07-24 18:19:46 +0200 | [diff] [blame] | 123 | .bind = button_gpio_bind, |
| 124 | .probe = button_gpio_probe, |
| 125 | .remove = button_gpio_remove, |
| 126 | }; |