mux.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * List, select, and deselect mux controllers on the fly.
  4. *
  5. * Copyright (c) 2020 Texas Instruments Inc.
  6. * Author: Pratyush Yadav <p.yadav@ti.com>
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <errno.h>
  11. #include <dm.h>
  12. #include <dm/device_compat.h>
  13. #include <mux.h>
  14. #include <mux-internal.h>
  15. #include <linux/err.h>
  16. #include <dt-bindings/mux/mux.h>
  17. #define COLUMN_SIZE 16
  18. /*
  19. * Print a member of a column. The total size of the text printed, including
  20. * trailing whitespace, will always be COLUMN_SIZE.
  21. */
  22. #define PRINT_COLUMN(fmt, args...) do { \
  23. char buf[COLUMN_SIZE + 1]; \
  24. snprintf(buf, COLUMN_SIZE + 1, fmt, ##args); \
  25. printf("%-*s", COLUMN_SIZE, buf); \
  26. } while (0)
  27. /*
  28. * Find a mux based on its device name in argv[1] and index in the chip in
  29. * argv[2].
  30. */
  31. static struct mux_control *cmd_mux_find(char *const argv[])
  32. {
  33. struct udevice *dev;
  34. struct mux_chip *chip;
  35. int ret;
  36. unsigned long id;
  37. ret = strict_strtoul(argv[2], 10, &id);
  38. if (ret)
  39. return ERR_PTR(ret);
  40. ret = uclass_get_device_by_name(UCLASS_MUX, argv[1], &dev);
  41. if (ret)
  42. return ERR_PTR(ret);
  43. chip = dev_get_uclass_priv(dev);
  44. if (!chip)
  45. return ERR_PTR(ret);
  46. if (id >= chip->controllers)
  47. return ERR_PTR(-EINVAL);
  48. return &chip->mux[id];
  49. }
  50. /*
  51. * Print the details of a mux. The columns printed correspond to: "Selected",
  52. * "Current State", "Idle State", and "Num States".
  53. */
  54. static void print_mux(struct mux_control *mux)
  55. {
  56. PRINT_COLUMN("%s", mux->in_use ? "yes" : "no");
  57. if (mux->cached_state == MUX_IDLE_AS_IS)
  58. PRINT_COLUMN("%s", "unknown");
  59. else
  60. PRINT_COLUMN("0x%x", mux->cached_state);
  61. if (mux->idle_state == MUX_IDLE_AS_IS)
  62. PRINT_COLUMN("%s", "as-is");
  63. else if (mux->idle_state == MUX_IDLE_DISCONNECT)
  64. PRINT_COLUMN("%s", "disconnect");
  65. else
  66. PRINT_COLUMN("0x%x", mux->idle_state);
  67. PRINT_COLUMN("0x%x", mux->states);
  68. printf("\n");
  69. }
  70. static int do_mux_list(struct cmd_tbl *cmdtp, int flag, int argc,
  71. char *const argv[])
  72. {
  73. struct udevice *dev;
  74. struct mux_chip *chip;
  75. int j;
  76. for (uclass_first_device(UCLASS_MUX, &dev);
  77. dev;
  78. uclass_next_device(&dev)) {
  79. chip = dev_get_uclass_priv(dev);
  80. if (!chip) {
  81. dev_err(dev, "can't find mux chip\n");
  82. continue;
  83. }
  84. printf("%s:\n", dev->name);
  85. printf(" ");
  86. PRINT_COLUMN("ID");
  87. PRINT_COLUMN("Selected");
  88. PRINT_COLUMN("Current State");
  89. PRINT_COLUMN("Idle State");
  90. PRINT_COLUMN("Num States");
  91. printf("\n");
  92. for (j = 0; j < chip->controllers; j++) {
  93. printf(" ");
  94. PRINT_COLUMN("%d", j);
  95. print_mux(&chip->mux[j]);
  96. }
  97. printf("\n");
  98. }
  99. return 0;
  100. }
  101. static int do_mux_select(struct cmd_tbl *cmdtp, int flag, int argc,
  102. char *const argv[])
  103. {
  104. struct mux_control *mux;
  105. int ret;
  106. unsigned long state;
  107. if (argc != 4)
  108. return CMD_RET_USAGE;
  109. mux = cmd_mux_find(argv);
  110. if (IS_ERR_OR_NULL(mux)) {
  111. printf("Failed to find the specified mux\n");
  112. return CMD_RET_FAILURE;
  113. }
  114. ret = strict_strtoul(argv[3], 16, &state);
  115. if (ret) {
  116. printf("Invalid state\n");
  117. return CMD_RET_FAILURE;
  118. }
  119. ret = mux_control_select(mux, state);
  120. if (ret) {
  121. printf("Failed to select requested state\n");
  122. return CMD_RET_FAILURE;
  123. }
  124. return CMD_RET_SUCCESS;
  125. }
  126. static int do_mux_deselect(struct cmd_tbl *cmdtp, int flag, int argc,
  127. char *const argv[])
  128. {
  129. struct mux_control *mux;
  130. int ret;
  131. if (argc != 3)
  132. return CMD_RET_USAGE;
  133. mux = cmd_mux_find(argv);
  134. if (IS_ERR_OR_NULL(mux)) {
  135. printf("Failed to find the specified mux\n");
  136. return CMD_RET_FAILURE;
  137. }
  138. ret = mux_control_deselect(mux);
  139. if (ret) {
  140. printf("Failed to deselect mux\n");
  141. return CMD_RET_FAILURE;
  142. }
  143. return CMD_RET_SUCCESS;
  144. }
  145. static char mux_help_text[] =
  146. "list - List all Muxes and their states\n"
  147. "select <chip> <id> <state> - Select the given mux state\n"
  148. "deselect <chip> <id> - Deselect the given mux and reset it to its idle state";
  149. U_BOOT_CMD_WITH_SUBCMDS(mux, "List, select, and deselect muxes", mux_help_text,
  150. U_BOOT_SUBCMD_MKENT(list, 1, 1, do_mux_list),
  151. U_BOOT_SUBCMD_MKENT(select, 4, 0, do_mux_select),
  152. U_BOOT_SUBCMD_MKENT(deselect, 3, 0, do_mux_deselect));