gpio.c 5.2 KB

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