led-core.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * LED Class Core
  3. *
  4. * Copyright 2005-2006 Openedhand Ltd.
  5. *
  6. * Author: Richard Purdie <rpurdie@openedhand.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/leds.h>
  15. #include <linux/list.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/rwsem.h>
  19. #include "leds.h"
  20. DECLARE_RWSEM(leds_list_lock);
  21. EXPORT_SYMBOL_GPL(leds_list_lock);
  22. LIST_HEAD(leds_list);
  23. EXPORT_SYMBOL_GPL(leds_list);
  24. static void led_set_software_blink(struct led_classdev *led_cdev,
  25. unsigned long delay_on,
  26. unsigned long delay_off)
  27. {
  28. int current_brightness;
  29. current_brightness = led_get_brightness(led_cdev);
  30. if (current_brightness)
  31. led_cdev->blink_brightness = current_brightness;
  32. if (!led_cdev->blink_brightness)
  33. led_cdev->blink_brightness = led_cdev->max_brightness;
  34. led_cdev->blink_delay_on = delay_on;
  35. led_cdev->blink_delay_off = delay_off;
  36. /* never on - just set to off */
  37. if (!delay_on) {
  38. led_set_brightness_async(led_cdev, LED_OFF);
  39. return;
  40. }
  41. /* never off - just set to brightness */
  42. if (!delay_off) {
  43. led_set_brightness_async(led_cdev, led_cdev->blink_brightness);
  44. return;
  45. }
  46. mod_timer(&led_cdev->blink_timer, jiffies + 1);
  47. }
  48. static void led_blink_setup(struct led_classdev *led_cdev,
  49. unsigned long *delay_on,
  50. unsigned long *delay_off)
  51. {
  52. if (!(led_cdev->flags & LED_BLINK_ONESHOT) &&
  53. led_cdev->blink_set &&
  54. !led_cdev->blink_set(led_cdev, delay_on, delay_off))
  55. return;
  56. /* blink with 1 Hz as default if nothing specified */
  57. if (!*delay_on && !*delay_off)
  58. *delay_on = *delay_off = 500;
  59. led_set_software_blink(led_cdev, *delay_on, *delay_off);
  60. }
  61. void led_blink_set(struct led_classdev *led_cdev,
  62. unsigned long *delay_on,
  63. unsigned long *delay_off)
  64. {
  65. del_timer_sync(&led_cdev->blink_timer);
  66. led_cdev->flags &= ~LED_BLINK_ONESHOT;
  67. led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
  68. led_blink_setup(led_cdev, delay_on, delay_off);
  69. }
  70. EXPORT_SYMBOL(led_blink_set);
  71. void led_blink_set_oneshot(struct led_classdev *led_cdev,
  72. unsigned long *delay_on,
  73. unsigned long *delay_off,
  74. int invert)
  75. {
  76. if ((led_cdev->flags & LED_BLINK_ONESHOT) &&
  77. timer_pending(&led_cdev->blink_timer))
  78. return;
  79. led_cdev->flags |= LED_BLINK_ONESHOT;
  80. led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
  81. if (invert)
  82. led_cdev->flags |= LED_BLINK_INVERT;
  83. else
  84. led_cdev->flags &= ~LED_BLINK_INVERT;
  85. led_blink_setup(led_cdev, delay_on, delay_off);
  86. }
  87. EXPORT_SYMBOL(led_blink_set_oneshot);
  88. void led_stop_software_blink(struct led_classdev *led_cdev)
  89. {
  90. del_timer_sync(&led_cdev->blink_timer);
  91. led_cdev->blink_delay_on = 0;
  92. led_cdev->blink_delay_off = 0;
  93. }
  94. EXPORT_SYMBOL_GPL(led_stop_software_blink);
  95. void led_set_brightness(struct led_classdev *led_cdev,
  96. enum led_brightness brightness)
  97. {
  98. int ret = 0;
  99. /* delay brightness if soft-blink is active */
  100. if (led_cdev->blink_delay_on || led_cdev->blink_delay_off) {
  101. led_cdev->delayed_set_value = brightness;
  102. if (brightness == LED_OFF)
  103. schedule_work(&led_cdev->set_brightness_work);
  104. return;
  105. }
  106. if (led_cdev->flags & SET_BRIGHTNESS_ASYNC) {
  107. led_set_brightness_async(led_cdev, brightness);
  108. return;
  109. } else if (led_cdev->flags & SET_BRIGHTNESS_SYNC)
  110. ret = led_set_brightness_sync(led_cdev, brightness);
  111. else
  112. ret = -EINVAL;
  113. if (ret < 0)
  114. dev_dbg(led_cdev->dev, "Setting LED brightness failed (%d)\n",
  115. ret);
  116. }
  117. EXPORT_SYMBOL(led_set_brightness);
  118. int led_update_brightness(struct led_classdev *led_cdev)
  119. {
  120. int ret = 0;
  121. if (led_cdev->brightness_get) {
  122. ret = led_cdev->brightness_get(led_cdev);
  123. if (ret >= 0) {
  124. led_cdev->brightness = ret;
  125. return 0;
  126. }
  127. }
  128. return ret;
  129. }
  130. EXPORT_SYMBOL(led_update_brightness);
  131. /* Caller must ensure led_cdev->led_access held */
  132. void led_sysfs_disable(struct led_classdev *led_cdev)
  133. {
  134. lockdep_assert_held(&led_cdev->led_access);
  135. led_cdev->flags |= LED_SYSFS_DISABLE;
  136. }
  137. EXPORT_SYMBOL_GPL(led_sysfs_disable);
  138. /* Caller must ensure led_cdev->led_access held */
  139. void led_sysfs_enable(struct led_classdev *led_cdev)
  140. {
  141. lockdep_assert_held(&led_cdev->led_access);
  142. led_cdev->flags &= ~LED_SYSFS_DISABLE;
  143. }
  144. EXPORT_SYMBOL_GPL(led_sysfs_enable);