led-class.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * LED Class Core
  3. *
  4. * Copyright (C) 2005 John Lenz <lenz@cs.wisc.edu>
  5. * Copyright (C) 2005-2007 Richard Purdie <rpurdie@openedhand.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/list.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/device.h>
  17. #include <linux/sysdev.h>
  18. #include <linux/timer.h>
  19. #include <linux/err.h>
  20. #include <linux/ctype.h>
  21. #include <linux/leds.h>
  22. #include "leds.h"
  23. static struct class *leds_class;
  24. static void led_update_brightness(struct led_classdev *led_cdev)
  25. {
  26. if (led_cdev->brightness_get)
  27. led_cdev->brightness = led_cdev->brightness_get(led_cdev);
  28. }
  29. static ssize_t led_brightness_show(struct device *dev,
  30. struct device_attribute *attr, char *buf)
  31. {
  32. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  33. /* no lock needed for this */
  34. led_update_brightness(led_cdev);
  35. return sprintf(buf, "%u\n", led_cdev->brightness);
  36. }
  37. static ssize_t led_brightness_store(struct device *dev,
  38. struct device_attribute *attr, const char *buf, size_t size)
  39. {
  40. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  41. ssize_t ret = -EINVAL;
  42. char *after;
  43. unsigned long state = simple_strtoul(buf, &after, 10);
  44. size_t count = after - buf;
  45. if (isspace(*after))
  46. count++;
  47. if (count == size) {
  48. ret = count;
  49. if (state == LED_OFF)
  50. led_trigger_remove(led_cdev);
  51. led_set_brightness(led_cdev, state);
  52. }
  53. return ret;
  54. }
  55. static ssize_t led_max_brightness_show(struct device *dev,
  56. struct device_attribute *attr, char *buf)
  57. {
  58. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  59. return sprintf(buf, "%u\n", led_cdev->max_brightness);
  60. }
  61. static struct device_attribute led_class_attrs[] = {
  62. __ATTR(brightness, 0644, led_brightness_show, led_brightness_store),
  63. __ATTR(max_brightness, 0444, led_max_brightness_show, NULL),
  64. #ifdef CONFIG_LEDS_TRIGGERS
  65. __ATTR(trigger, 0644, led_trigger_show, led_trigger_store),
  66. #endif
  67. __ATTR_NULL,
  68. };
  69. static void led_timer_function(unsigned long data)
  70. {
  71. struct led_classdev *led_cdev = (void *)data;
  72. unsigned long brightness;
  73. unsigned long delay;
  74. if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
  75. led_set_brightness(led_cdev, LED_OFF);
  76. return;
  77. }
  78. brightness = led_get_brightness(led_cdev);
  79. if (!brightness) {
  80. /* Time to switch the LED on. */
  81. brightness = led_cdev->blink_brightness;
  82. delay = led_cdev->blink_delay_on;
  83. } else {
  84. /* Store the current brightness value to be able
  85. * to restore it when the delay_off period is over.
  86. */
  87. led_cdev->blink_brightness = brightness;
  88. brightness = LED_OFF;
  89. delay = led_cdev->blink_delay_off;
  90. }
  91. led_set_brightness(led_cdev, brightness);
  92. mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
  93. }
  94. static void led_stop_software_blink(struct led_classdev *led_cdev)
  95. {
  96. /* deactivate previous settings */
  97. del_timer_sync(&led_cdev->blink_timer);
  98. led_cdev->blink_delay_on = 0;
  99. led_cdev->blink_delay_off = 0;
  100. }
  101. static void led_set_software_blink(struct led_classdev *led_cdev,
  102. unsigned long delay_on,
  103. unsigned long delay_off)
  104. {
  105. int current_brightness;
  106. current_brightness = led_get_brightness(led_cdev);
  107. if (current_brightness)
  108. led_cdev->blink_brightness = current_brightness;
  109. if (!led_cdev->blink_brightness)
  110. led_cdev->blink_brightness = led_cdev->max_brightness;
  111. if (led_get_trigger_data(led_cdev) &&
  112. delay_on == led_cdev->blink_delay_on &&
  113. delay_off == led_cdev->blink_delay_off)
  114. return;
  115. led_stop_software_blink(led_cdev);
  116. led_cdev->blink_delay_on = delay_on;
  117. led_cdev->blink_delay_off = delay_off;
  118. /* never on - don't blink */
  119. if (!delay_on)
  120. return;
  121. /* never off - just set to brightness */
  122. if (!delay_off) {
  123. led_set_brightness(led_cdev, led_cdev->blink_brightness);
  124. return;
  125. }
  126. mod_timer(&led_cdev->blink_timer, jiffies + 1);
  127. }
  128. /**
  129. * led_classdev_suspend - suspend an led_classdev.
  130. * @led_cdev: the led_classdev to suspend.
  131. */
  132. void led_classdev_suspend(struct led_classdev *led_cdev)
  133. {
  134. led_cdev->flags |= LED_SUSPENDED;
  135. led_cdev->brightness_set(led_cdev, 0);
  136. }
  137. EXPORT_SYMBOL_GPL(led_classdev_suspend);
  138. /**
  139. * led_classdev_resume - resume an led_classdev.
  140. * @led_cdev: the led_classdev to resume.
  141. */
  142. void led_classdev_resume(struct led_classdev *led_cdev)
  143. {
  144. led_cdev->brightness_set(led_cdev, led_cdev->brightness);
  145. led_cdev->flags &= ~LED_SUSPENDED;
  146. }
  147. EXPORT_SYMBOL_GPL(led_classdev_resume);
  148. static int led_suspend(struct device *dev, pm_message_t state)
  149. {
  150. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  151. if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
  152. led_classdev_suspend(led_cdev);
  153. return 0;
  154. }
  155. static int led_resume(struct device *dev)
  156. {
  157. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  158. if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
  159. led_classdev_resume(led_cdev);
  160. return 0;
  161. }
  162. /**
  163. * led_classdev_register - register a new object of led_classdev class.
  164. * @parent: The device to register.
  165. * @led_cdev: the led_classdev structure for this device.
  166. */
  167. int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
  168. {
  169. led_cdev->dev = device_create(leds_class, parent, 0, led_cdev,
  170. "%s", led_cdev->name);
  171. if (IS_ERR(led_cdev->dev))
  172. return PTR_ERR(led_cdev->dev);
  173. #ifdef CONFIG_LEDS_TRIGGERS
  174. init_rwsem(&led_cdev->trigger_lock);
  175. #endif
  176. /* add to the list of leds */
  177. down_write(&leds_list_lock);
  178. list_add_tail(&led_cdev->node, &leds_list);
  179. up_write(&leds_list_lock);
  180. if (!led_cdev->max_brightness)
  181. led_cdev->max_brightness = LED_FULL;
  182. led_update_brightness(led_cdev);
  183. init_timer(&led_cdev->blink_timer);
  184. led_cdev->blink_timer.function = led_timer_function;
  185. led_cdev->blink_timer.data = (unsigned long)led_cdev;
  186. #ifdef CONFIG_LEDS_TRIGGERS
  187. led_trigger_set_default(led_cdev);
  188. #endif
  189. printk(KERN_DEBUG "Registered led device: %s\n",
  190. led_cdev->name);
  191. return 0;
  192. }
  193. EXPORT_SYMBOL_GPL(led_classdev_register);
  194. /**
  195. * led_classdev_unregister - unregisters a object of led_properties class.
  196. * @led_cdev: the led device to unregister
  197. *
  198. * Unregisters a previously registered via led_classdev_register object.
  199. */
  200. void led_classdev_unregister(struct led_classdev *led_cdev)
  201. {
  202. #ifdef CONFIG_LEDS_TRIGGERS
  203. down_write(&led_cdev->trigger_lock);
  204. if (led_cdev->trigger)
  205. led_trigger_set(led_cdev, NULL);
  206. up_write(&led_cdev->trigger_lock);
  207. #endif
  208. /* Stop blinking */
  209. led_brightness_set(led_cdev, LED_OFF);
  210. device_unregister(led_cdev->dev);
  211. down_write(&leds_list_lock);
  212. list_del(&led_cdev->node);
  213. up_write(&leds_list_lock);
  214. }
  215. EXPORT_SYMBOL_GPL(led_classdev_unregister);
  216. void led_blink_set(struct led_classdev *led_cdev,
  217. unsigned long *delay_on,
  218. unsigned long *delay_off)
  219. {
  220. del_timer_sync(&led_cdev->blink_timer);
  221. if (led_cdev->blink_set &&
  222. !led_cdev->blink_set(led_cdev, delay_on, delay_off))
  223. return;
  224. /* blink with 1 Hz as default if nothing specified */
  225. if (!*delay_on && !*delay_off)
  226. *delay_on = *delay_off = 500;
  227. led_set_software_blink(led_cdev, *delay_on, *delay_off);
  228. }
  229. EXPORT_SYMBOL(led_blink_set);
  230. void led_brightness_set(struct led_classdev *led_cdev,
  231. enum led_brightness brightness)
  232. {
  233. led_stop_software_blink(led_cdev);
  234. led_cdev->brightness_set(led_cdev, brightness);
  235. }
  236. EXPORT_SYMBOL(led_brightness_set);
  237. static int __init leds_init(void)
  238. {
  239. leds_class = class_create(THIS_MODULE, "leds");
  240. if (IS_ERR(leds_class))
  241. return PTR_ERR(leds_class);
  242. leds_class->suspend = led_suspend;
  243. leds_class->resume = led_resume;
  244. leds_class->dev_attrs = led_class_attrs;
  245. return 0;
  246. }
  247. static void __exit leds_exit(void)
  248. {
  249. class_destroy(leds_class);
  250. }
  251. subsys_initcall(leds_init);
  252. module_exit(leds_exit);
  253. MODULE_AUTHOR("John Lenz, Richard Purdie");
  254. MODULE_LICENSE("GPL");
  255. MODULE_DESCRIPTION("LED Class Interface");