cmd_gpio.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <dm.h>
  11. #include <asm/gpio.h>
  12. int __weak name_to_gpio(const char *name)
  13. {
  14. return simple_strtoul(name, NULL, 10);
  15. }
  16. enum gpio_cmd {
  17. GPIO_INPUT,
  18. GPIO_SET,
  19. GPIO_CLEAR,
  20. GPIO_TOGGLE,
  21. };
  22. #if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
  23. static const char * const gpio_function[] = {
  24. "input",
  25. "output",
  26. "unknown",
  27. };
  28. static void show_gpio(struct device *dev, const char *bank_name, int offset)
  29. {
  30. struct dm_gpio_ops *ops = gpio_get_ops(dev);
  31. char buf[80];
  32. int ret;
  33. *buf = '\0';
  34. if (ops->get_state) {
  35. ret = ops->get_state(dev, offset, buf, sizeof(buf));
  36. if (ret) {
  37. puts("<unknown>");
  38. return;
  39. }
  40. } else {
  41. int func = GPIOF_UNKNOWN;
  42. int ret;
  43. if (ops->get_function) {
  44. ret = ops->get_function(dev, offset);
  45. if (ret >= 0 && ret < ARRAY_SIZE(gpio_function))
  46. func = ret;
  47. }
  48. sprintf(buf, "%s%u: %8s %d", bank_name, offset,
  49. gpio_function[func], ops->get_value(dev, offset));
  50. }
  51. puts(buf);
  52. puts("\n");
  53. }
  54. static int do_gpio_status(const char *gpio_name)
  55. {
  56. struct device *dev;
  57. int newline = 0;
  58. int ret;
  59. if (gpio_name && !*gpio_name)
  60. gpio_name = NULL;
  61. for (ret = uclass_first_device(UCLASS_GPIO, &dev);
  62. dev;
  63. ret = uclass_next_device(&dev)) {
  64. const char *bank_name;
  65. int num_bits;
  66. bank_name = gpio_get_bank_info(dev, &num_bits);
  67. if (!gpio_name || !bank_name ||
  68. !strncmp(gpio_name, bank_name, strlen(bank_name))) {
  69. const char *p = NULL;
  70. int offset;
  71. if (bank_name) {
  72. if (newline)
  73. putc('\n');
  74. printf("Bank %s:\n", bank_name);
  75. }
  76. newline = 1;
  77. if (gpio_name && bank_name) {
  78. p = gpio_name + strlen(bank_name);
  79. offset = simple_strtoul(p, NULL, 10);
  80. show_gpio(dev, bank_name, offset);
  81. } else {
  82. for (offset = 0; offset < num_bits; offset++)
  83. show_gpio(dev, bank_name, offset);
  84. }
  85. }
  86. }
  87. return ret;
  88. }
  89. #endif
  90. static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  91. {
  92. unsigned int gpio;
  93. enum gpio_cmd sub_cmd;
  94. ulong value;
  95. const char *str_cmd, *str_gpio = NULL;
  96. #ifdef CONFIG_DM_GPIO
  97. int ret;
  98. #endif
  99. if (argc < 2)
  100. show_usage:
  101. return CMD_RET_USAGE;
  102. str_cmd = argv[1];
  103. if (argc > 2)
  104. str_gpio = argv[2];
  105. if (!strcmp(str_cmd, "status")) {
  106. /* Support deprecated gpio_status() */
  107. #ifdef gpio_status
  108. gpio_status();
  109. return 0;
  110. #elif defined(CONFIG_DM_GPIO)
  111. return cmd_process_error(cmdtp, do_gpio_status(str_gpio));
  112. #else
  113. goto show_usage;
  114. #endif
  115. }
  116. if (!str_gpio)
  117. goto show_usage;
  118. /* parse the behavior */
  119. switch (*str_cmd) {
  120. case 'i': sub_cmd = GPIO_INPUT; break;
  121. case 's': sub_cmd = GPIO_SET; break;
  122. case 'c': sub_cmd = GPIO_CLEAR; break;
  123. case 't': sub_cmd = GPIO_TOGGLE; break;
  124. default: goto show_usage;
  125. }
  126. #if defined(CONFIG_DM_GPIO)
  127. /*
  128. * TODO(sjg@chromium.org): For now we must fit into the existing GPIO
  129. * framework, so we look up the name here and convert it to a GPIO number.
  130. * Once all GPIO drivers are converted to driver model, we can change the
  131. * code here to use the GPIO uclass interface instead of the numbered
  132. * GPIO compatibility layer.
  133. */
  134. ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
  135. if (ret)
  136. return cmd_process_error(cmdtp, ret);
  137. #else
  138. /* turn the gpio name into a gpio number */
  139. gpio = name_to_gpio(str_gpio);
  140. if (gpio < 0)
  141. goto show_usage;
  142. #endif
  143. /* grab the pin before we tweak it */
  144. if (gpio_request(gpio, "cmd_gpio")) {
  145. printf("gpio: requesting pin %u failed\n", gpio);
  146. return -1;
  147. }
  148. /* finally, let's do it: set direction and exec command */
  149. if (sub_cmd == GPIO_INPUT) {
  150. gpio_direction_input(gpio);
  151. value = gpio_get_value(gpio);
  152. } else {
  153. switch (sub_cmd) {
  154. case GPIO_SET: value = 1; break;
  155. case GPIO_CLEAR: value = 0; break;
  156. case GPIO_TOGGLE: value = !gpio_get_value(gpio); break;
  157. default: goto show_usage;
  158. }
  159. gpio_direction_output(gpio, value);
  160. }
  161. printf("gpio: pin %s (gpio %i) value is %lu\n",
  162. str_gpio, gpio, value);
  163. gpio_free(gpio);
  164. return value;
  165. }
  166. U_BOOT_CMD(gpio, 3, 0, do_gpio,
  167. "query and control gpio pins",
  168. "<input|set|clear|toggle> <pin>\n"
  169. " - input/set/clear/toggle the specified pin\n"
  170. "gpio status [<bank> | <pin>]");