gpio_led.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Status LED driver based on GPIO access conventions of Linux
  3. *
  4. * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
  5. * Licensed under the GPL-2 or later.
  6. */
  7. #include <common.h>
  8. #include <status_led.h>
  9. #include <asm/gpio.h>
  10. #ifndef CONFIG_GPIO_LED_INVERTED_TABLE
  11. #define CONFIG_GPIO_LED_INVERTED_TABLE {}
  12. #endif
  13. static led_id_t gpio_led_inv[] = CONFIG_GPIO_LED_INVERTED_TABLE;
  14. static int gpio_led_gpio_value(led_id_t mask, int state)
  15. {
  16. int i, gpio_value = (state == CONFIG_LED_STATUS_ON);
  17. for (i = 0; i < ARRAY_SIZE(gpio_led_inv); i++) {
  18. if (gpio_led_inv[i] == mask)
  19. gpio_value = !gpio_value;
  20. }
  21. return gpio_value;
  22. }
  23. void __led_init(led_id_t mask, int state)
  24. {
  25. int gpio_value;
  26. if (gpio_request(mask, "gpio_led") != 0) {
  27. printf("%s: failed requesting GPIO%lu!\n", __func__, mask);
  28. return;
  29. }
  30. gpio_value = gpio_led_gpio_value(mask, state);
  31. gpio_direction_output(mask, gpio_value);
  32. }
  33. void __led_set(led_id_t mask, int state)
  34. {
  35. int gpio_value = gpio_led_gpio_value(mask, state);
  36. gpio_set_value(mask, gpio_value);
  37. }
  38. void __led_toggle(led_id_t mask)
  39. {
  40. gpio_set_value(mask, !gpio_get_value(mask));
  41. }
  42. #ifdef CONFIG_GPIO_LED_STUBS
  43. /* 'generic' override of colored LED stubs, to use GPIO functions instead */
  44. #ifdef CONFIG_LED_STATUS_RED
  45. void red_led_on(void)
  46. {
  47. __led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_ON);
  48. }
  49. void red_led_off(void)
  50. {
  51. __led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_OFF);
  52. }
  53. #endif
  54. #ifdef CONFIG_LED_STATUS_GREEN
  55. void green_led_on(void)
  56. {
  57. __led_set(CONFIG_LED_STATUS_GREEN, CONFIG_LED_STATUS_ON);
  58. }
  59. void green_led_off(void)
  60. {
  61. __led_set(CONFIG_LED_STATUS_GREEN, CONFIG_LED_STATUS_OFF);
  62. }
  63. #endif
  64. #ifdef CONFIG_LED_STATUS_YELLOW
  65. void yellow_led_on(void)
  66. {
  67. __led_set(CONFIG_LED_STATUS_YELLOW, CONFIG_LED_STATUS_ON);
  68. }
  69. void yellow_led_off(void)
  70. {
  71. __led_set(CONFIG_LED_STATUS_YELLOW, CONFIG_LED_STATUS_OFF);
  72. }
  73. #endif
  74. #ifdef CONFIG_LED_STATUS_BLUE
  75. void blue_led_on(void)
  76. {
  77. __led_set(CONFIG_LED_STATUS_BLUE, CONFIG_LED_STATUS_ON);
  78. }
  79. void blue_led_off(void)
  80. {
  81. __led_set(CONFIG_LED_STATUS_BLUE, CONFIG_LED_STATUS_OFF);
  82. }
  83. #endif
  84. #endif /* CONFIG_GPIO_LED_STUBS */