cmd_gpio.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Control GPIO pins on the fly
  3. *
  4. * Copyright (c) 2008-2011 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <errno.h>
  11. #include <dm.h>
  12. #include <asm/gpio.h>
  13. int __weak name_to_gpio(const char *name)
  14. {
  15. return simple_strtoul(name, NULL, 10);
  16. }
  17. enum gpio_cmd {
  18. GPIO_INPUT,
  19. GPIO_SET,
  20. GPIO_CLEAR,
  21. GPIO_TOGGLE,
  22. };
  23. #if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
  24. static const char * const gpio_function[GPIOF_COUNT] = {
  25. "input",
  26. "output",
  27. "unused",
  28. "unknown",
  29. "func",
  30. };
  31. /* A few flags used by show_gpio() */
  32. enum {
  33. FLAG_SHOW_ALL = 1 << 0,
  34. FLAG_SHOW_BANK = 1 << 1,
  35. FLAG_SHOW_NEWLINE = 1 << 2,
  36. };
  37. static void show_gpio(struct udevice *dev, const char *bank_name, int offset,
  38. int *flagsp)
  39. {
  40. struct dm_gpio_ops *ops = gpio_get_ops(dev);
  41. int func = GPIOF_UNKNOWN;
  42. char buf[80];
  43. int ret;
  44. BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
  45. if (ops->get_function) {
  46. ret = ops->get_function(dev, offset);
  47. if (ret >= 0 && ret < ARRAY_SIZE(gpio_function))
  48. func = ret;
  49. }
  50. if (!(*flagsp & FLAG_SHOW_ALL) && func == GPIOF_UNUSED)
  51. return;
  52. if ((*flagsp & FLAG_SHOW_BANK) && bank_name) {
  53. if (*flagsp & FLAG_SHOW_NEWLINE) {
  54. putc('\n');
  55. *flagsp &= ~FLAG_SHOW_NEWLINE;
  56. }
  57. printf("Bank %s:\n", bank_name);
  58. *flagsp &= ~FLAG_SHOW_BANK;
  59. }
  60. *buf = '\0';
  61. if (ops->get_state) {
  62. ret = ops->get_state(dev, offset, buf, sizeof(buf));
  63. if (ret) {
  64. puts("<unknown>");
  65. return;
  66. }
  67. } else {
  68. sprintf(buf, "%s%u: %8s %d", bank_name, offset,
  69. gpio_function[func], ops->get_value(dev, offset));
  70. }
  71. puts(buf);
  72. puts("\n");
  73. }
  74. static int do_gpio_status(bool all, const char *gpio_name)
  75. {
  76. struct udevice *dev;
  77. int banklen;
  78. int flags;
  79. int ret;
  80. flags = 0;
  81. if (gpio_name && !*gpio_name)
  82. gpio_name = NULL;
  83. for (ret = uclass_first_device(UCLASS_GPIO, &dev);
  84. dev;
  85. ret = uclass_next_device(&dev)) {
  86. const char *bank_name;
  87. int num_bits;
  88. flags |= FLAG_SHOW_BANK;
  89. if (all)
  90. flags |= FLAG_SHOW_ALL;
  91. bank_name = gpio_get_bank_info(dev, &num_bits);
  92. if (!num_bits)
  93. continue;
  94. banklen = bank_name ? strlen(bank_name) : 0;
  95. if (!gpio_name || !bank_name ||
  96. !strncmp(gpio_name, bank_name, banklen)) {
  97. const char *p = NULL;
  98. int offset;
  99. p = gpio_name + banklen;
  100. if (gpio_name && *p) {
  101. offset = simple_strtoul(p, NULL, 10);
  102. show_gpio(dev, bank_name, offset, &flags);
  103. } else {
  104. for (offset = 0; offset < num_bits; offset++) {
  105. show_gpio(dev, bank_name, offset,
  106. &flags);
  107. }
  108. }
  109. }
  110. /* Add a newline between bank names */
  111. if (!(flags & FLAG_SHOW_BANK))
  112. flags |= FLAG_SHOW_NEWLINE;
  113. }
  114. return ret;
  115. }
  116. #endif
  117. static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  118. {
  119. unsigned int gpio;
  120. enum gpio_cmd sub_cmd;
  121. ulong value;
  122. const char *str_cmd, *str_gpio = NULL;
  123. int ret;
  124. #ifdef CONFIG_DM_GPIO
  125. bool all = false;
  126. #endif
  127. if (argc < 2)
  128. show_usage:
  129. return CMD_RET_USAGE;
  130. str_cmd = argv[1];
  131. argc -= 2;
  132. argv += 2;
  133. #ifdef CONFIG_DM_GPIO
  134. if (argc > 0 && !strcmp(*argv, "-a")) {
  135. all = true;
  136. argc--;
  137. argv++;
  138. }
  139. #endif
  140. if (argc > 0)
  141. str_gpio = *argv;
  142. if (!strcmp(str_cmd, "status")) {
  143. /* Support deprecated gpio_status() */
  144. #ifdef gpio_status
  145. gpio_status();
  146. return 0;
  147. #elif defined(CONFIG_DM_GPIO)
  148. return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio));
  149. #else
  150. goto show_usage;
  151. #endif
  152. }
  153. if (!str_gpio)
  154. goto show_usage;
  155. /* parse the behavior */
  156. switch (*str_cmd) {
  157. case 'i': sub_cmd = GPIO_INPUT; break;
  158. case 's': sub_cmd = GPIO_SET; break;
  159. case 'c': sub_cmd = GPIO_CLEAR; break;
  160. case 't': sub_cmd = GPIO_TOGGLE; break;
  161. default: goto show_usage;
  162. }
  163. #if defined(CONFIG_DM_GPIO)
  164. /*
  165. * TODO(sjg@chromium.org): For now we must fit into the existing GPIO
  166. * framework, so we look up the name here and convert it to a GPIO number.
  167. * Once all GPIO drivers are converted to driver model, we can change the
  168. * code here to use the GPIO uclass interface instead of the numbered
  169. * GPIO compatibility layer.
  170. */
  171. ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
  172. if (ret)
  173. return cmd_process_error(cmdtp, ret);
  174. #else
  175. /* turn the gpio name into a gpio number */
  176. gpio = name_to_gpio(str_gpio);
  177. if (gpio < 0)
  178. goto show_usage;
  179. #endif
  180. /* grab the pin before we tweak it */
  181. ret = gpio_request(gpio, "cmd_gpio");
  182. if (ret && ret != -EBUSY) {
  183. printf("gpio: requesting pin %u failed\n", gpio);
  184. return -1;
  185. }
  186. /* finally, let's do it: set direction and exec command */
  187. if (sub_cmd == GPIO_INPUT) {
  188. gpio_direction_input(gpio);
  189. value = gpio_get_value(gpio);
  190. } else {
  191. switch (sub_cmd) {
  192. case GPIO_SET: value = 1; break;
  193. case GPIO_CLEAR: value = 0; break;
  194. case GPIO_TOGGLE: value = !gpio_get_value(gpio); break;
  195. default: goto show_usage;
  196. }
  197. gpio_direction_output(gpio, value);
  198. }
  199. printf("gpio: pin %s (gpio %i) value is %lu\n",
  200. str_gpio, gpio, value);
  201. if (ret != -EBUSY)
  202. gpio_free(gpio);
  203. return value;
  204. }
  205. U_BOOT_CMD(gpio, 4, 0, do_gpio,
  206. "query and control gpio pins",
  207. "<input|set|clear|toggle> <pin>\n"
  208. " - input/set/clear/toggle the specified pin\n"
  209. "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");