button.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Philippe Reynes <philippe.reynes@softathome.com>
  4. *
  5. * Based on led.c
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <dm.h>
  10. #include <button.h>
  11. #include <dm/uclass-internal.h>
  12. static const char *const state_label[] = {
  13. [BUTTON_OFF] = "off",
  14. [BUTTON_ON] = "on",
  15. };
  16. static int show_button_state(struct udevice *dev)
  17. {
  18. int ret;
  19. ret = button_get_state(dev);
  20. if (ret >= BUTTON_COUNT)
  21. ret = -EINVAL;
  22. if (ret >= 0)
  23. printf("%s\n", state_label[ret]);
  24. return ret;
  25. }
  26. static int list_buttons(void)
  27. {
  28. struct udevice *dev;
  29. int ret;
  30. for (uclass_find_first_device(UCLASS_BUTTON, &dev);
  31. dev;
  32. uclass_find_next_device(&dev)) {
  33. struct button_uc_plat *plat = dev_get_uclass_plat(dev);
  34. if (!plat->label)
  35. continue;
  36. printf("%-15s ", plat->label);
  37. if (device_active(dev)) {
  38. ret = show_button_state(dev);
  39. if (ret < 0)
  40. printf("Error %d\n", ret);
  41. } else {
  42. printf("<inactive>\n");
  43. }
  44. }
  45. return 0;
  46. }
  47. int do_button(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  48. {
  49. const char *button_label;
  50. struct udevice *dev;
  51. int ret;
  52. /* Validate arguments */
  53. if (argc < 2)
  54. return CMD_RET_USAGE;
  55. button_label = argv[1];
  56. if (strncmp(button_label, "list", 4) == 0)
  57. return list_buttons();
  58. ret = button_get_by_label(button_label, &dev);
  59. if (ret) {
  60. printf("Button '%s' not found (err=%d)\n", button_label, ret);
  61. return CMD_RET_FAILURE;
  62. }
  63. ret = show_button_state(dev);
  64. return !ret;
  65. }
  66. U_BOOT_CMD(
  67. button, 2, 1, do_button,
  68. "manage buttons",
  69. "<button_label> \tGet button state\n"
  70. "button list\t\tShow a list of buttons"
  71. );