cmd_gpio.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Control GPIO pins on the fly
  3. *
  4. * Copyright (c) 2008-2010 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <linux/ctype.h>
  11. #include <asm/blackfin.h>
  12. #include <asm/gpio.h>
  13. enum {
  14. GPIO_INPUT,
  15. GPIO_SET,
  16. GPIO_CLEAR,
  17. GPIO_TOGGLE,
  18. };
  19. int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  20. {
  21. if (argc == 2 && !strcmp(argv[1], "status")) {
  22. bfin_gpio_labels();
  23. return 0;
  24. }
  25. if (argc != 3)
  26. show_usage:
  27. return cmd_usage(cmdtp);
  28. /* parse the behavior */
  29. ulong sub_cmd;
  30. switch (argv[1][0]) {
  31. case 'i': sub_cmd = GPIO_INPUT; break;
  32. case 's': sub_cmd = GPIO_SET; break;
  33. case 'c': sub_cmd = GPIO_CLEAR; break;
  34. case 't': sub_cmd = GPIO_TOGGLE; break;
  35. default: goto show_usage;
  36. }
  37. /* parse the pin with format: [p][port]<#> */
  38. const char *str_pin = argv[2];
  39. /* grab the [p]<port> portion */
  40. ulong port_base;
  41. if (tolower(*str_pin) == 'p') ++str_pin;
  42. switch (tolower(*str_pin)) {
  43. #ifdef GPIO_PA0
  44. case 'a': port_base = GPIO_PA0; break;
  45. #endif
  46. #ifdef GPIO_PB0
  47. case 'b': port_base = GPIO_PB0; break;
  48. #endif
  49. #ifdef GPIO_PC0
  50. case 'c': port_base = GPIO_PC0; break;
  51. #endif
  52. #ifdef GPIO_PD0
  53. case 'd': port_base = GPIO_PD0; break;
  54. #endif
  55. #ifdef GPIO_PE0
  56. case 'e': port_base = GPIO_PE0; break;
  57. #endif
  58. #ifdef GPIO_PF0
  59. case 'f': port_base = GPIO_PF0; break;
  60. #endif
  61. #ifdef GPIO_PG0
  62. case 'g': port_base = GPIO_PG0; break;
  63. #endif
  64. #ifdef GPIO_PH0
  65. case 'h': port_base = GPIO_PH0; break;
  66. #endif
  67. #ifdef GPIO_PI0
  68. case 'i': port_base = GPIO_PI0; break;
  69. #endif
  70. #ifdef GPIO_PJ
  71. case 'j': port_base = GPIO_PJ0; break;
  72. #endif
  73. default: goto show_usage;
  74. }
  75. /* grab the <#> portion */
  76. ulong pin = simple_strtoul(str_pin + 1, NULL, 10);
  77. if (pin > 15)
  78. goto show_usage;
  79. /* grab the pin before we tweak it */
  80. ulong gpio = port_base + pin;
  81. gpio_request(gpio, "cmd_gpio");
  82. /* finally, let's do it: set direction and exec command */
  83. ulong value;
  84. if (sub_cmd == GPIO_INPUT) {
  85. gpio_direction_input(gpio);
  86. value = gpio_get_value(gpio);
  87. } else {
  88. switch (sub_cmd) {
  89. case GPIO_SET: value = 1; break;
  90. case GPIO_CLEAR: value = 0; break;
  91. case GPIO_TOGGLE: value = !gpio_get_value(gpio); break;
  92. default: goto show_usage;
  93. }
  94. gpio_direction_output(gpio, value);
  95. }
  96. printf("gpio: pin %lu on port %c (gpio %lu) value is %lu\n",
  97. pin, *str_pin, gpio, value);
  98. gpio_free(gpio);
  99. return value;
  100. }
  101. U_BOOT_CMD(gpio, 3, 0, do_gpio,
  102. "input/set/clear/toggle gpio output pins",
  103. "<input|set|clear|toggle> <port><pin>\n"
  104. " - input/set/clear/toggle the specified pin (e.g. PF10)");