led-core.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_timer_function(unsigned long data)
  25. {
  26. struct led_classdev *led_cdev = (void *)data;
  27. unsigned long brightness;
  28. unsigned long delay;
  29. if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
  30. led_set_brightness_async(led_cdev, LED_OFF);
  31. return;
  32. }
  33. if (led_cdev->flags & LED_BLINK_ONESHOT_STOP) {
  34. led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
  35. return;
  36. }
  37. brightness = led_get_brightness(led_cdev);
  38. if (!brightness) {
  39. /* Time to switch the LED on. */
  40. if (led_cdev->delayed_set_value) {
  41. led_cdev->blink_brightness =
  42. led_cdev->delayed_set_value;
  43. led_cdev->delayed_set_value = 0;
  44. }
  45. brightness = led_cdev->blink_brightness;
  46. delay = led_cdev->blink_delay_on;
  47. } else {
  48. /* Store the current brightness value to be able
  49. * to restore it when the delay_off period is over.
  50. */
  51. led_cdev->blink_brightness = brightness;
  52. brightness = LED_OFF;
  53. delay = led_cdev->blink_delay_off;
  54. }
  55. led_set_brightness_async(led_cdev, brightness);
  56. /* Return in next iteration if led is in one-shot mode and we are in
  57. * the final blink state so that the led is toggled each delay_on +
  58. * delay_off milliseconds in worst case.
  59. */
  60. if (led_cdev->flags & LED_BLINK_ONESHOT) {
  61. if (led_cdev->flags & LED_BLINK_INVERT) {
  62. if (brightness)
  63. led_cdev->flags |= LED_BLINK_ONESHOT_STOP;
  64. } else {
  65. if (!brightness)
  66. led_cdev->flags |= LED_BLINK_ONESHOT_STOP;
  67. }
  68. }
  69. mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
  70. }
  71. static void set_brightness_delayed(struct work_struct *ws)
  72. {
  73. struct led_classdev *led_cdev =
  74. container_of(ws, struct led_classdev, set_brightness_work);
  75. led_stop_software_blink(led_cdev);
  76. led_set_brightness_async(led_cdev, led_cdev->delayed_set_value);
  77. }
  78. static void led_set_software_blink(struct led_classdev *led_cdev,
  79. unsigned long delay_on,
  80. unsigned long delay_off)
  81. {
  82. int current_brightness;
  83. current_brightness = led_get_brightness(led_cdev);
  84. if (current_brightness)
  85. led_cdev->blink_brightness = current_brightness;
  86. if (!led_cdev->blink_brightness)
  87. led_cdev->blink_brightness = led_cdev->max_brightness;
  88. led_cdev->blink_delay_on = delay_on;
  89. led_cdev->blink_delay_off = delay_off;
  90. /* never on - just set to off */
  91. if (!delay_on) {
  92. led_set_brightness_async(led_cdev, LED_OFF);
  93. return;
  94. }
  95. /* never off - just set to brightness */
  96. if (!delay_off) {
  97. led_set_brightness_async(led_cdev, led_cdev->blink_brightness);
  98. return;
  99. }
  100. mod_timer(&led_cdev->blink_timer, jiffies + 1);
  101. }
  102. static void led_blink_setup(struct led_classdev *led_cdev,
  103. unsigned long *delay_on,
  104. unsigned long *delay_off)
  105. {
  106. if (!(led_cdev->flags & LED_BLINK_ONESHOT) &&
  107. led_cdev->blink_set &&
  108. !led_cdev->blink_set(led_cdev, delay_on, delay_off))
  109. return;
  110. /* blink with 1 Hz as default if nothing specified */
  111. if (!*delay_on && !*delay_off)
  112. *delay_on = *delay_off = 500;
  113. led_set_software_blink(led_cdev, *delay_on, *delay_off);
  114. }
  115. void led_init_core(struct led_classdev *led_cdev)
  116. {
  117. INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
  118. setup_timer(&led_cdev->blink_timer, led_timer_function,
  119. (unsigned long)led_cdev);
  120. }
  121. EXPORT_SYMBOL_GPL(led_init_core);
  122. void led_blink_set(struct led_classdev *led_cdev,
  123. unsigned long *delay_on,
  124. unsigned long *delay_off)
  125. {
  126. del_timer_sync(&led_cdev->blink_timer);
  127. led_cdev->flags &= ~LED_BLINK_ONESHOT;
  128. led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
  129. led_blink_setup(led_cdev, delay_on, delay_off);
  130. }
  131. EXPORT_SYMBOL(led_blink_set);
  132. void led_blink_set_oneshot(struct led_classdev *led_cdev,
  133. unsigned long *delay_on,
  134. unsigned long *delay_off,
  135. int invert)
  136. {
  137. if ((led_cdev->flags & LED_BLINK_ONESHOT) &&
  138. timer_pending(&led_cdev->blink_timer))
  139. return;
  140. led_cdev->flags |= LED_BLINK_ONESHOT;
  141. led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
  142. if (invert)
  143. led_cdev->flags |= LED_BLINK_INVERT;
  144. else
  145. led_cdev->flags &= ~LED_BLINK_INVERT;
  146. led_blink_setup(led_cdev, delay_on, delay_off);
  147. }
  148. EXPORT_SYMBOL(led_blink_set_oneshot);
  149. void led_stop_software_blink(struct led_classdev *led_cdev)
  150. {
  151. del_timer_sync(&led_cdev->blink_timer);
  152. led_cdev->blink_delay_on = 0;
  153. led_cdev->blink_delay_off = 0;
  154. }
  155. EXPORT_SYMBOL_GPL(led_stop_software_blink);
  156. void led_set_brightness(struct led_classdev *led_cdev,
  157. enum led_brightness brightness)
  158. {
  159. int ret = 0;
  160. /* delay brightness if soft-blink is active */
  161. if (led_cdev->blink_delay_on || led_cdev->blink_delay_off) {
  162. led_cdev->delayed_set_value = brightness;
  163. if (brightness == LED_OFF)
  164. schedule_work(&led_cdev->set_brightness_work);
  165. return;
  166. }
  167. if (led_cdev->flags & SET_BRIGHTNESS_ASYNC) {
  168. led_set_brightness_async(led_cdev, brightness);
  169. return;
  170. } else if (led_cdev->flags & SET_BRIGHTNESS_SYNC)
  171. ret = led_set_brightness_sync(led_cdev, brightness);
  172. else
  173. ret = -EINVAL;
  174. if (ret < 0)
  175. dev_dbg(led_cdev->dev, "Setting LED brightness failed (%d)\n",
  176. ret);
  177. }
  178. EXPORT_SYMBOL(led_set_brightness);
  179. int led_update_brightness(struct led_classdev *led_cdev)
  180. {
  181. int ret = 0;
  182. if (led_cdev->brightness_get) {
  183. ret = led_cdev->brightness_get(led_cdev);
  184. if (ret >= 0) {
  185. led_cdev->brightness = ret;
  186. return 0;
  187. }
  188. }
  189. return ret;
  190. }
  191. EXPORT_SYMBOL(led_update_brightness);
  192. /* Caller must ensure led_cdev->led_access held */
  193. void led_sysfs_disable(struct led_classdev *led_cdev)
  194. {
  195. lockdep_assert_held(&led_cdev->led_access);
  196. led_cdev->flags |= LED_SYSFS_DISABLE;
  197. }
  198. EXPORT_SYMBOL_GPL(led_sysfs_disable);
  199. /* Caller must ensure led_cdev->led_access held */
  200. void led_sysfs_enable(struct led_classdev *led_cdev)
  201. {
  202. lockdep_assert_held(&led_cdev->led_access);
  203. led_cdev->flags &= ~LED_SYSFS_DISABLE;
  204. }
  205. EXPORT_SYMBOL_GPL(led_sysfs_enable);