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