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