cmd_gpio.c 2.6 KB

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