led-class.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/timer.h>
  18. #include <linux/err.h>
  19. #include <linux/ctype.h>
  20. #include <linux/leds.h>
  21. #include "leds.h"
  22. static struct class *leds_class;
  23. static void led_update_brightness(struct led_classdev *led_cdev)
  24. {
  25. if (led_cdev->brightness_get)
  26. led_cdev->brightness = led_cdev->brightness_get(led_cdev);
  27. }
  28. static ssize_t led_brightness_show(struct device *dev,
  29. struct device_attribute *attr, char *buf)
  30. {
  31. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  32. /* no lock needed for this */
  33. led_update_brightness(led_cdev);
  34. return sprintf(buf, "%u\n", led_cdev->brightness);
  35. }
  36. static ssize_t led_brightness_store(struct device *dev,
  37. struct device_attribute *attr, const char *buf, size_t size)
  38. {
  39. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  40. unsigned long state;
  41. ssize_t ret = -EINVAL;
  42. ret = kstrtoul(buf, 10, &state);
  43. if (ret)
  44. return ret;
  45. if (state == LED_OFF)
  46. led_trigger_remove(led_cdev);
  47. __led_set_brightness(led_cdev, state);
  48. return size;
  49. }
  50. static ssize_t led_max_brightness_show(struct device *dev,
  51. struct device_attribute *attr, char *buf)
  52. {
  53. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  54. return sprintf(buf, "%u\n", led_cdev->max_brightness);
  55. }
  56. static struct device_attribute led_class_attrs[] = {
  57. __ATTR(brightness, 0644, led_brightness_show, led_brightness_store),
  58. __ATTR(max_brightness, 0444, led_max_brightness_show, NULL),
  59. #ifdef CONFIG_LEDS_TRIGGERS
  60. __ATTR(trigger, 0644, led_trigger_show, led_trigger_store),
  61. #endif
  62. __ATTR_NULL,
  63. };
  64. static void led_timer_function(unsigned long data)
  65. {
  66. struct led_classdev *led_cdev = (void *)data;
  67. unsigned long brightness;
  68. unsigned long delay;
  69. if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
  70. __led_set_brightness(led_cdev, LED_OFF);
  71. return;
  72. }
  73. if (led_cdev->flags & LED_BLINK_ONESHOT_STOP) {
  74. led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
  75. return;
  76. }
  77. brightness = led_get_brightness(led_cdev);
  78. if (!brightness) {
  79. /* Time to switch the LED on. */
  80. brightness = led_cdev->blink_brightness;
  81. delay = led_cdev->blink_delay_on;
  82. } else {
  83. /* Store the current brightness value to be able
  84. * to restore it when the delay_off period is over.
  85. */
  86. led_cdev->blink_brightness = brightness;
  87. brightness = LED_OFF;
  88. delay = led_cdev->blink_delay_off;
  89. }
  90. __led_set_brightness(led_cdev, brightness);
  91. /* Return in next iteration if led is in one-shot mode and we are in
  92. * the final blink state so that the led is toggled each delay_on +
  93. * delay_off milliseconds in worst case.
  94. */
  95. if (led_cdev->flags & LED_BLINK_ONESHOT) {
  96. if (led_cdev->flags & LED_BLINK_INVERT) {
  97. if (brightness)
  98. led_cdev->flags |= LED_BLINK_ONESHOT_STOP;
  99. } else {
  100. if (!brightness)
  101. led_cdev->flags |= LED_BLINK_ONESHOT_STOP;
  102. }
  103. }
  104. mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
  105. }
  106. /**
  107. * led_classdev_suspend - suspend an led_classdev.
  108. * @led_cdev: the led_classdev to suspend.
  109. */
  110. void led_classdev_suspend(struct led_classdev *led_cdev)
  111. {
  112. led_cdev->flags |= LED_SUSPENDED;
  113. led_cdev->brightness_set(led_cdev, 0);
  114. }
  115. EXPORT_SYMBOL_GPL(led_classdev_suspend);
  116. /**
  117. * led_classdev_resume - resume an led_classdev.
  118. * @led_cdev: the led_classdev to resume.
  119. */
  120. void led_classdev_resume(struct led_classdev *led_cdev)
  121. {
  122. led_cdev->brightness_set(led_cdev, led_cdev->brightness);
  123. led_cdev->flags &= ~LED_SUSPENDED;
  124. }
  125. EXPORT_SYMBOL_GPL(led_classdev_resume);
  126. static int led_suspend(struct device *dev, pm_message_t state)
  127. {
  128. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  129. if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
  130. led_classdev_suspend(led_cdev);
  131. return 0;
  132. }
  133. static int led_resume(struct device *dev)
  134. {
  135. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  136. if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
  137. led_classdev_resume(led_cdev);
  138. return 0;
  139. }
  140. /**
  141. * led_classdev_register - register a new object of led_classdev class.
  142. * @parent: The device to register.
  143. * @led_cdev: the led_classdev structure for this device.
  144. */
  145. int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
  146. {
  147. led_cdev->dev = device_create(leds_class, parent, 0, led_cdev,
  148. "%s", led_cdev->name);
  149. if (IS_ERR(led_cdev->dev))
  150. return PTR_ERR(led_cdev->dev);
  151. #ifdef CONFIG_LEDS_TRIGGERS
  152. init_rwsem(&led_cdev->trigger_lock);
  153. #endif
  154. /* add to the list of leds */
  155. down_write(&leds_list_lock);
  156. list_add_tail(&led_cdev->node, &leds_list);
  157. up_write(&leds_list_lock);
  158. if (!led_cdev->max_brightness)
  159. led_cdev->max_brightness = LED_FULL;
  160. led_update_brightness(led_cdev);
  161. init_timer(&led_cdev->blink_timer);
  162. led_cdev->blink_timer.function = led_timer_function;
  163. led_cdev->blink_timer.data = (unsigned long)led_cdev;
  164. #ifdef CONFIG_LEDS_TRIGGERS
  165. led_trigger_set_default(led_cdev);
  166. #endif
  167. printk(KERN_DEBUG "Registered led device: %s\n",
  168. led_cdev->name);
  169. return 0;
  170. }
  171. EXPORT_SYMBOL_GPL(led_classdev_register);
  172. /**
  173. * led_classdev_unregister - unregisters a object of led_properties class.
  174. * @led_cdev: the led device to unregister
  175. *
  176. * Unregisters a previously registered via led_classdev_register object.
  177. */
  178. void led_classdev_unregister(struct led_classdev *led_cdev)
  179. {
  180. #ifdef CONFIG_LEDS_TRIGGERS
  181. down_write(&led_cdev->trigger_lock);
  182. if (led_cdev->trigger)
  183. led_trigger_set(led_cdev, NULL);
  184. up_write(&led_cdev->trigger_lock);
  185. #endif
  186. /* Stop blinking */
  187. led_set_brightness(led_cdev, LED_OFF);
  188. device_unregister(led_cdev->dev);
  189. down_write(&leds_list_lock);
  190. list_del(&led_cdev->node);
  191. up_write(&leds_list_lock);
  192. }
  193. EXPORT_SYMBOL_GPL(led_classdev_unregister);
  194. static int __init leds_init(void)
  195. {
  196. leds_class = class_create(THIS_MODULE, "leds");
  197. if (IS_ERR(leds_class))
  198. return PTR_ERR(leds_class);
  199. leds_class->suspend = led_suspend;
  200. leds_class->resume = led_resume;
  201. leds_class->dev_attrs = led_class_attrs;
  202. return 0;
  203. }
  204. static void __exit leds_exit(void)
  205. {
  206. class_destroy(leds_class);
  207. }
  208. subsys_initcall(leds_init);
  209. module_exit(leds_exit);
  210. MODULE_AUTHOR("John Lenz, Richard Purdie");
  211. MODULE_LICENSE("GPL");
  212. MODULE_DESCRIPTION("LED Class Interface");