led-core.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 int __led_set_brightness(struct led_classdev *led_cdev,
  25. enum led_brightness value)
  26. {
  27. if (!led_cdev->brightness_set)
  28. return -ENOTSUPP;
  29. led_cdev->brightness_set(led_cdev, value);
  30. return 0;
  31. }
  32. static int __led_set_brightness_blocking(struct led_classdev *led_cdev,
  33. enum led_brightness value)
  34. {
  35. if (!led_cdev->brightness_set_blocking)
  36. return -ENOTSUPP;
  37. return led_cdev->brightness_set_blocking(led_cdev, value);
  38. }
  39. static void led_timer_function(unsigned long data)
  40. {
  41. struct led_classdev *led_cdev = (void *)data;
  42. unsigned long brightness;
  43. unsigned long delay;
  44. if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
  45. led_set_brightness_nosleep(led_cdev, LED_OFF);
  46. led_cdev->flags &= ~LED_BLINK_SW;
  47. return;
  48. }
  49. if (led_cdev->flags & LED_BLINK_ONESHOT_STOP) {
  50. led_cdev->flags &= ~(LED_BLINK_ONESHOT_STOP | LED_BLINK_SW);
  51. return;
  52. }
  53. brightness = led_get_brightness(led_cdev);
  54. if (!brightness) {
  55. /* Time to switch the LED on. */
  56. brightness = led_cdev->blink_brightness;
  57. delay = led_cdev->blink_delay_on;
  58. } else {
  59. /* Store the current brightness value to be able
  60. * to restore it when the delay_off period is over.
  61. * Do it only if there is no pending blink brightness
  62. * change, to avoid overwriting the new value.
  63. */
  64. if (!(led_cdev->flags & LED_BLINK_BRIGHTNESS_CHANGE))
  65. led_cdev->blink_brightness = brightness;
  66. else
  67. led_cdev->flags &= ~LED_BLINK_BRIGHTNESS_CHANGE;
  68. brightness = LED_OFF;
  69. delay = led_cdev->blink_delay_off;
  70. }
  71. led_set_brightness_nosleep(led_cdev, brightness);
  72. /* Return in next iteration if led is in one-shot mode and we are in
  73. * the final blink state so that the led is toggled each delay_on +
  74. * delay_off milliseconds in worst case.
  75. */
  76. if (led_cdev->flags & LED_BLINK_ONESHOT) {
  77. if (led_cdev->flags & LED_BLINK_INVERT) {
  78. if (brightness)
  79. led_cdev->flags |= LED_BLINK_ONESHOT_STOP;
  80. } else {
  81. if (!brightness)
  82. led_cdev->flags |= LED_BLINK_ONESHOT_STOP;
  83. }
  84. }
  85. mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
  86. }
  87. static void set_brightness_delayed(struct work_struct *ws)
  88. {
  89. struct led_classdev *led_cdev =
  90. container_of(ws, struct led_classdev, set_brightness_work);
  91. int ret = 0;
  92. if (led_cdev->flags & LED_BLINK_DISABLE) {
  93. led_cdev->delayed_set_value = LED_OFF;
  94. led_stop_software_blink(led_cdev);
  95. led_cdev->flags &= ~LED_BLINK_DISABLE;
  96. }
  97. ret = __led_set_brightness(led_cdev, led_cdev->delayed_set_value);
  98. if (ret == -ENOTSUPP)
  99. ret = __led_set_brightness_blocking(led_cdev,
  100. led_cdev->delayed_set_value);
  101. if (ret < 0 &&
  102. /* LED HW might have been unplugged, therefore don't warn */
  103. !(ret == -ENODEV && (led_cdev->flags & LED_UNREGISTERING) &&
  104. (led_cdev->flags & LED_HW_PLUGGABLE)))
  105. dev_err(led_cdev->dev,
  106. "Setting an LED's brightness failed (%d)\n", ret);
  107. }
  108. static void led_set_software_blink(struct led_classdev *led_cdev,
  109. unsigned long delay_on,
  110. unsigned long delay_off)
  111. {
  112. int current_brightness;
  113. current_brightness = led_get_brightness(led_cdev);
  114. if (current_brightness)
  115. led_cdev->blink_brightness = current_brightness;
  116. if (!led_cdev->blink_brightness)
  117. led_cdev->blink_brightness = led_cdev->max_brightness;
  118. led_cdev->blink_delay_on = delay_on;
  119. led_cdev->blink_delay_off = delay_off;
  120. /* never on - just set to off */
  121. if (!delay_on) {
  122. led_set_brightness_nosleep(led_cdev, LED_OFF);
  123. return;
  124. }
  125. /* never off - just set to brightness */
  126. if (!delay_off) {
  127. led_set_brightness_nosleep(led_cdev,
  128. led_cdev->blink_brightness);
  129. return;
  130. }
  131. led_cdev->flags |= LED_BLINK_SW;
  132. mod_timer(&led_cdev->blink_timer, jiffies + 1);
  133. }
  134. static void led_blink_setup(struct led_classdev *led_cdev,
  135. unsigned long *delay_on,
  136. unsigned long *delay_off)
  137. {
  138. if (!(led_cdev->flags & LED_BLINK_ONESHOT) &&
  139. led_cdev->blink_set &&
  140. !led_cdev->blink_set(led_cdev, delay_on, delay_off))
  141. return;
  142. /* blink with 1 Hz as default if nothing specified */
  143. if (!*delay_on && !*delay_off)
  144. *delay_on = *delay_off = 500;
  145. led_set_software_blink(led_cdev, *delay_on, *delay_off);
  146. }
  147. void led_init_core(struct led_classdev *led_cdev)
  148. {
  149. INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
  150. setup_timer(&led_cdev->blink_timer, led_timer_function,
  151. (unsigned long)led_cdev);
  152. }
  153. EXPORT_SYMBOL_GPL(led_init_core);
  154. void led_blink_set(struct led_classdev *led_cdev,
  155. unsigned long *delay_on,
  156. unsigned long *delay_off)
  157. {
  158. del_timer_sync(&led_cdev->blink_timer);
  159. led_cdev->flags &= ~LED_BLINK_SW;
  160. led_cdev->flags &= ~LED_BLINK_ONESHOT;
  161. led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
  162. led_blink_setup(led_cdev, delay_on, delay_off);
  163. }
  164. EXPORT_SYMBOL_GPL(led_blink_set);
  165. void led_blink_set_oneshot(struct led_classdev *led_cdev,
  166. unsigned long *delay_on,
  167. unsigned long *delay_off,
  168. int invert)
  169. {
  170. if ((led_cdev->flags & LED_BLINK_ONESHOT) &&
  171. timer_pending(&led_cdev->blink_timer))
  172. return;
  173. led_cdev->flags |= LED_BLINK_ONESHOT;
  174. led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
  175. if (invert)
  176. led_cdev->flags |= LED_BLINK_INVERT;
  177. else
  178. led_cdev->flags &= ~LED_BLINK_INVERT;
  179. led_blink_setup(led_cdev, delay_on, delay_off);
  180. }
  181. EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
  182. void led_stop_software_blink(struct led_classdev *led_cdev)
  183. {
  184. del_timer_sync(&led_cdev->blink_timer);
  185. led_cdev->blink_delay_on = 0;
  186. led_cdev->blink_delay_off = 0;
  187. led_cdev->flags &= ~LED_BLINK_SW;
  188. }
  189. EXPORT_SYMBOL_GPL(led_stop_software_blink);
  190. void led_set_brightness(struct led_classdev *led_cdev,
  191. enum led_brightness brightness)
  192. {
  193. /*
  194. * If software blink is active, delay brightness setting
  195. * until the next timer tick.
  196. */
  197. if (led_cdev->flags & LED_BLINK_SW) {
  198. /*
  199. * If we need to disable soft blinking delegate this to the
  200. * work queue task to avoid problems in case we are called
  201. * from hard irq context.
  202. */
  203. if (brightness == LED_OFF) {
  204. led_cdev->flags |= LED_BLINK_DISABLE;
  205. schedule_work(&led_cdev->set_brightness_work);
  206. } else {
  207. led_cdev->flags |= LED_BLINK_BRIGHTNESS_CHANGE;
  208. led_cdev->blink_brightness = brightness;
  209. }
  210. return;
  211. }
  212. led_set_brightness_nosleep(led_cdev, brightness);
  213. }
  214. EXPORT_SYMBOL_GPL(led_set_brightness);
  215. void led_set_brightness_nopm(struct led_classdev *led_cdev,
  216. enum led_brightness value)
  217. {
  218. /* Use brightness_set op if available, it is guaranteed not to sleep */
  219. if (!__led_set_brightness(led_cdev, value))
  220. return;
  221. /* If brightness setting can sleep, delegate it to a work queue task */
  222. led_cdev->delayed_set_value = value;
  223. schedule_work(&led_cdev->set_brightness_work);
  224. }
  225. EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
  226. void led_set_brightness_nosleep(struct led_classdev *led_cdev,
  227. enum led_brightness value)
  228. {
  229. led_cdev->brightness = min(value, led_cdev->max_brightness);
  230. if (led_cdev->flags & LED_SUSPENDED)
  231. return;
  232. led_set_brightness_nopm(led_cdev, led_cdev->brightness);
  233. }
  234. EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
  235. int led_set_brightness_sync(struct led_classdev *led_cdev,
  236. enum led_brightness value)
  237. {
  238. if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
  239. return -EBUSY;
  240. led_cdev->brightness = min(value, led_cdev->max_brightness);
  241. if (led_cdev->flags & LED_SUSPENDED)
  242. return 0;
  243. return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
  244. }
  245. EXPORT_SYMBOL_GPL(led_set_brightness_sync);
  246. int led_update_brightness(struct led_classdev *led_cdev)
  247. {
  248. int ret = 0;
  249. if (led_cdev->brightness_get) {
  250. ret = led_cdev->brightness_get(led_cdev);
  251. if (ret >= 0) {
  252. led_cdev->brightness = ret;
  253. return 0;
  254. }
  255. }
  256. return ret;
  257. }
  258. EXPORT_SYMBOL_GPL(led_update_brightness);
  259. /* Caller must ensure led_cdev->led_access held */
  260. void led_sysfs_disable(struct led_classdev *led_cdev)
  261. {
  262. lockdep_assert_held(&led_cdev->led_access);
  263. led_cdev->flags |= LED_SYSFS_DISABLE;
  264. }
  265. EXPORT_SYMBOL_GPL(led_sysfs_disable);
  266. /* Caller must ensure led_cdev->led_access held */
  267. void led_sysfs_enable(struct led_classdev *led_cdev)
  268. {
  269. lockdep_assert_held(&led_cdev->led_access);
  270. led_cdev->flags &= ~LED_SYSFS_DISABLE;
  271. }
  272. EXPORT_SYMBOL_GPL(led_sysfs_enable);