led-class.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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/ctype.h>
  12. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/leds.h>
  17. #include <linux/list.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/timer.h>
  22. #include "leds.h"
  23. static struct class *leds_class;
  24. static ssize_t brightness_show(struct device *dev,
  25. struct device_attribute *attr, char *buf)
  26. {
  27. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  28. /* no lock needed for this */
  29. led_update_brightness(led_cdev);
  30. return sprintf(buf, "%u\n", led_cdev->brightness);
  31. }
  32. static ssize_t brightness_store(struct device *dev,
  33. struct device_attribute *attr, const char *buf, size_t size)
  34. {
  35. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  36. unsigned long state;
  37. ssize_t ret = -EINVAL;
  38. ret = kstrtoul(buf, 10, &state);
  39. if (ret)
  40. return ret;
  41. if (state == LED_OFF)
  42. led_trigger_remove(led_cdev);
  43. __led_set_brightness(led_cdev, state);
  44. return size;
  45. }
  46. static DEVICE_ATTR_RW(brightness);
  47. static ssize_t max_brightness_show(struct device *dev,
  48. struct device_attribute *attr, char *buf)
  49. {
  50. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  51. return sprintf(buf, "%u\n", led_cdev->max_brightness);
  52. }
  53. static DEVICE_ATTR_RO(max_brightness);
  54. #ifdef CONFIG_LEDS_TRIGGERS
  55. static DEVICE_ATTR(trigger, 0644, led_trigger_show, led_trigger_store);
  56. static struct attribute *led_trigger_attrs[] = {
  57. &dev_attr_trigger.attr,
  58. NULL,
  59. };
  60. static const struct attribute_group led_trigger_group = {
  61. .attrs = led_trigger_attrs,
  62. };
  63. #endif
  64. static struct attribute *led_class_attrs[] = {
  65. &dev_attr_brightness.attr,
  66. &dev_attr_max_brightness.attr,
  67. NULL,
  68. };
  69. static const struct attribute_group led_group = {
  70. .attrs = led_class_attrs,
  71. };
  72. static const struct attribute_group *led_groups[] = {
  73. &led_group,
  74. #ifdef CONFIG_LEDS_TRIGGERS
  75. &led_trigger_group,
  76. #endif
  77. NULL,
  78. };
  79. static void led_timer_function(unsigned long data)
  80. {
  81. struct led_classdev *led_cdev = (void *)data;
  82. unsigned long brightness;
  83. unsigned long delay;
  84. if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
  85. __led_set_brightness(led_cdev, LED_OFF);
  86. return;
  87. }
  88. if (led_cdev->flags & LED_BLINK_ONESHOT_STOP) {
  89. led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
  90. return;
  91. }
  92. brightness = led_get_brightness(led_cdev);
  93. if (!brightness) {
  94. /* Time to switch the LED on. */
  95. brightness = led_cdev->blink_brightness;
  96. delay = led_cdev->blink_delay_on;
  97. } else {
  98. /* Store the current brightness value to be able
  99. * to restore it when the delay_off period is over.
  100. */
  101. led_cdev->blink_brightness = brightness;
  102. brightness = LED_OFF;
  103. delay = led_cdev->blink_delay_off;
  104. }
  105. __led_set_brightness(led_cdev, brightness);
  106. /* Return in next iteration if led is in one-shot mode and we are in
  107. * the final blink state so that the led is toggled each delay_on +
  108. * delay_off milliseconds in worst case.
  109. */
  110. if (led_cdev->flags & LED_BLINK_ONESHOT) {
  111. if (led_cdev->flags & LED_BLINK_INVERT) {
  112. if (brightness)
  113. led_cdev->flags |= LED_BLINK_ONESHOT_STOP;
  114. } else {
  115. if (!brightness)
  116. led_cdev->flags |= LED_BLINK_ONESHOT_STOP;
  117. }
  118. }
  119. mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
  120. }
  121. static void set_brightness_delayed(struct work_struct *ws)
  122. {
  123. struct led_classdev *led_cdev =
  124. container_of(ws, struct led_classdev, set_brightness_work);
  125. led_stop_software_blink(led_cdev);
  126. __led_set_brightness(led_cdev, led_cdev->delayed_set_value);
  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. #ifdef CONFIG_PM_SLEEP
  149. static int led_suspend(struct device *dev)
  150. {
  151. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  152. if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
  153. led_classdev_suspend(led_cdev);
  154. return 0;
  155. }
  156. static int led_resume(struct device *dev)
  157. {
  158. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  159. if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
  160. led_classdev_resume(led_cdev);
  161. return 0;
  162. }
  163. #endif
  164. static SIMPLE_DEV_PM_OPS(leds_class_dev_pm_ops, led_suspend, led_resume);
  165. /**
  166. * led_classdev_register - register a new object of led_classdev class.
  167. * @parent: The device to register.
  168. * @led_cdev: the led_classdev structure for this device.
  169. */
  170. int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
  171. {
  172. led_cdev->dev = device_create_with_groups(leds_class, parent, 0,
  173. led_cdev, led_cdev->groups,
  174. "%s", led_cdev->name);
  175. if (IS_ERR(led_cdev->dev))
  176. return PTR_ERR(led_cdev->dev);
  177. #ifdef CONFIG_LEDS_TRIGGERS
  178. init_rwsem(&led_cdev->trigger_lock);
  179. #endif
  180. /* add to the list of leds */
  181. down_write(&leds_list_lock);
  182. list_add_tail(&led_cdev->node, &leds_list);
  183. up_write(&leds_list_lock);
  184. if (!led_cdev->max_brightness)
  185. led_cdev->max_brightness = LED_FULL;
  186. led_update_brightness(led_cdev);
  187. INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
  188. init_timer(&led_cdev->blink_timer);
  189. led_cdev->blink_timer.function = led_timer_function;
  190. led_cdev->blink_timer.data = (unsigned long)led_cdev;
  191. #ifdef CONFIG_LEDS_TRIGGERS
  192. led_trigger_set_default(led_cdev);
  193. #endif
  194. dev_dbg(parent, "Registered led device: %s\n",
  195. led_cdev->name);
  196. return 0;
  197. }
  198. EXPORT_SYMBOL_GPL(led_classdev_register);
  199. /**
  200. * led_classdev_unregister - unregisters a object of led_properties class.
  201. * @led_cdev: the led device to unregister
  202. *
  203. * Unregisters a previously registered via led_classdev_register object.
  204. */
  205. void led_classdev_unregister(struct led_classdev *led_cdev)
  206. {
  207. #ifdef CONFIG_LEDS_TRIGGERS
  208. down_write(&led_cdev->trigger_lock);
  209. if (led_cdev->trigger)
  210. led_trigger_set(led_cdev, NULL);
  211. up_write(&led_cdev->trigger_lock);
  212. #endif
  213. cancel_work_sync(&led_cdev->set_brightness_work);
  214. /* Stop blinking */
  215. led_stop_software_blink(led_cdev);
  216. led_set_brightness(led_cdev, LED_OFF);
  217. device_unregister(led_cdev->dev);
  218. down_write(&leds_list_lock);
  219. list_del(&led_cdev->node);
  220. up_write(&leds_list_lock);
  221. }
  222. EXPORT_SYMBOL_GPL(led_classdev_unregister);
  223. static int __init leds_init(void)
  224. {
  225. leds_class = class_create(THIS_MODULE, "leds");
  226. if (IS_ERR(leds_class))
  227. return PTR_ERR(leds_class);
  228. leds_class->pm = &leds_class_dev_pm_ops;
  229. leds_class->dev_groups = led_groups;
  230. return 0;
  231. }
  232. static void __exit leds_exit(void)
  233. {
  234. class_destroy(leds_class);
  235. }
  236. subsys_initcall(leds_init);
  237. module_exit(leds_exit);
  238. MODULE_AUTHOR("John Lenz, Richard Purdie");
  239. MODULE_LICENSE("GPL");
  240. MODULE_DESCRIPTION("LED Class Interface");