gpio_led.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 == STATUS_LED_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. }