gpio.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. GPIOC_INPUT,
  19. GPIOC_SET,
  20. GPIOC_CLEAR,
  21. GPIOC_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, bool show_all)
  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 (!show_all && !(*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. !strncasecmp(gpio_name, bank_name, banklen)) {
  81. const char *p;
  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, true);
  88. } else {
  89. for (offset = 0; offset < num_bits; offset++) {
  90. gpio_get_description(dev, bank_name,
  91. offset, &flags, false);
  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':
  143. sub_cmd = GPIOC_INPUT;
  144. break;
  145. case 's':
  146. sub_cmd = GPIOC_SET;
  147. break;
  148. case 'c':
  149. sub_cmd = GPIOC_CLEAR;
  150. break;
  151. case 't':
  152. sub_cmd = GPIOC_TOGGLE;
  153. break;
  154. default:
  155. goto show_usage;
  156. }
  157. #if defined(CONFIG_DM_GPIO)
  158. /*
  159. * TODO(sjg@chromium.org): For now we must fit into the existing GPIO
  160. * framework, so we look up the name here and convert it to a GPIO number.
  161. * Once all GPIO drivers are converted to driver model, we can change the
  162. * code here to use the GPIO uclass interface instead of the numbered
  163. * GPIO compatibility layer.
  164. */
  165. ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
  166. if (ret) {
  167. printf("GPIO: '%s' not found\n", str_gpio);
  168. return cmd_process_error(cmdtp, ret);
  169. }
  170. #else
  171. /* turn the gpio name into a gpio number */
  172. gpio = name_to_gpio(str_gpio);
  173. if (gpio < 0)
  174. goto show_usage;
  175. #endif
  176. /* grab the pin before we tweak it */
  177. ret = gpio_request(gpio, "cmd_gpio");
  178. if (ret && ret != -EBUSY) {
  179. printf("gpio: requesting pin %u failed\n", gpio);
  180. return -1;
  181. }
  182. /* finally, let's do it: set direction and exec command */
  183. if (sub_cmd == GPIOC_INPUT) {
  184. gpio_direction_input(gpio);
  185. value = gpio_get_value(gpio);
  186. } else {
  187. switch (sub_cmd) {
  188. case GPIOC_SET:
  189. value = 1;
  190. break;
  191. case GPIOC_CLEAR:
  192. value = 0;
  193. break;
  194. case GPIOC_TOGGLE:
  195. value = gpio_get_value(gpio);
  196. if (!IS_ERR_VALUE(value))
  197. value = !value;
  198. break;
  199. default:
  200. goto show_usage;
  201. }
  202. gpio_direction_output(gpio, value);
  203. }
  204. printf("gpio: pin %s (gpio %u) value is ", str_gpio, gpio);
  205. if (IS_ERR_VALUE(value))
  206. printf("unknown (ret=%d)\n", value);
  207. else
  208. printf("%d\n", value);
  209. if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)) {
  210. int nval = gpio_get_value(gpio);
  211. if (IS_ERR_VALUE(nval))
  212. printf(" Warning: no access to GPIO output value\n");
  213. else if (nval != value)
  214. printf(" Warning: value of pin is still %d\n", nval);
  215. }
  216. if (ret != -EBUSY)
  217. gpio_free(gpio);
  218. return value;
  219. }
  220. U_BOOT_CMD(gpio, 4, 0, do_gpio,
  221. "query and control gpio pins",
  222. "<input|set|clear|toggle> <pin>\n"
  223. " - input/set/clear/toggle the specified pin\n"
  224. "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");