cmd_gpio.c 4.2 KB

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