button-gpio.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Philippe Reynes <philippe.reynes@softathome.com>
  4. */
  5. #include <common.h>
  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. struct button_gpio_priv {
  13. struct gpio_desc gpio;
  14. };
  15. static enum button_state_t button_gpio_get_state(struct udevice *dev)
  16. {
  17. struct button_gpio_priv *priv = dev_get_priv(dev);
  18. int ret;
  19. if (!dm_gpio_is_valid(&priv->gpio))
  20. return -EREMOTEIO;
  21. ret = dm_gpio_get_value(&priv->gpio);
  22. if (ret < 0)
  23. return ret;
  24. return ret ? BUTTON_ON : BUTTON_OFF;
  25. }
  26. static int button_gpio_probe(struct udevice *dev)
  27. {
  28. struct button_uc_plat *uc_plat = dev_get_uclass_plat(dev);
  29. struct button_gpio_priv *priv = dev_get_priv(dev);
  30. int ret;
  31. /* Ignore the top-level button node */
  32. if (!uc_plat->label)
  33. return 0;
  34. ret = gpio_request_by_name(dev, "gpios", 0, &priv->gpio, GPIOD_IS_IN);
  35. if (ret)
  36. return ret;
  37. return 0;
  38. }
  39. static int button_gpio_remove(struct udevice *dev)
  40. {
  41. /*
  42. * The GPIO driver may have already been removed. We will need to
  43. * address this more generally.
  44. */
  45. if (!IS_ENABLED(CONFIG_SANDBOX)) {
  46. struct button_gpio_priv *priv = dev_get_priv(dev);
  47. if (dm_gpio_is_valid(&priv->gpio))
  48. dm_gpio_free(dev, &priv->gpio);
  49. }
  50. return 0;
  51. }
  52. static int button_gpio_bind(struct udevice *parent)
  53. {
  54. struct udevice *dev;
  55. ofnode node;
  56. int ret;
  57. dev_for_each_subnode(node, parent) {
  58. struct button_uc_plat *uc_plat;
  59. const char *label;
  60. label = ofnode_read_string(node, "label");
  61. if (!label) {
  62. debug("%s: node %s has no label\n", __func__,
  63. ofnode_get_name(node));
  64. return -EINVAL;
  65. }
  66. ret = device_bind_driver_to_node(parent, "button_gpio",
  67. ofnode_get_name(node),
  68. node, &dev);
  69. if (ret)
  70. return ret;
  71. uc_plat = dev_get_uclass_plat(dev);
  72. uc_plat->label = label;
  73. }
  74. return 0;
  75. }
  76. static const struct button_ops button_gpio_ops = {
  77. .get_state = button_gpio_get_state,
  78. };
  79. static const struct udevice_id button_gpio_ids[] = {
  80. { .compatible = "gpio-keys" },
  81. { .compatible = "gpio-keys-polled" },
  82. { }
  83. };
  84. U_BOOT_DRIVER(button_gpio) = {
  85. .name = "button_gpio",
  86. .id = UCLASS_BUTTON,
  87. .of_match = button_gpio_ids,
  88. .ops = &button_gpio_ops,
  89. .priv_auto = sizeof(struct button_gpio_priv),
  90. .bind = button_gpio_bind,
  91. .probe = button_gpio_probe,
  92. .remove = button_gpio_remove,
  93. };