button-gpio.c 2.6 KB

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