led-class.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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/err.h>
  18. #include <linux/ctype.h>
  19. #include <linux/leds.h>
  20. #include <linux/workqueue.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 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 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 DEVICE_ATTR_RW(brightness);
  51. static ssize_t led_max_brightness_show(struct device *dev,
  52. struct device_attribute *attr, char *buf)
  53. {
  54. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  55. return sprintf(buf, "%u\n", led_cdev->max_brightness);
  56. }
  57. static DEVICE_ATTR(max_brightness, 0444, led_max_brightness_show, NULL);
  58. #ifdef CONFIG_LEDS_TRIGGERS
  59. static DEVICE_ATTR(trigger, 0644, led_trigger_show, led_trigger_store);
  60. static struct attribute *led_trigger_attrs[] = {
  61. &dev_attr_trigger.attr,
  62. NULL,
  63. };
  64. static const struct attribute_group led_trigger_group = {
  65. .attrs = led_trigger_attrs,
  66. };
  67. #endif
  68. static struct attribute *led_class_attrs[] = {
  69. &dev_attr_brightness.attr,
  70. &dev_attr_max_brightness.attr,
  71. NULL,
  72. };
  73. static const struct attribute_group led_group = {
  74. .attrs = led_class_attrs,
  75. };
  76. static const struct attribute_group *led_groups[] = {
  77. &led_group,
  78. #ifdef CONFIG_LEDS_TRIGGERS
  79. &led_trigger_group,
  80. #endif
  81. NULL,
  82. };
  83. static void led_work_function(struct work_struct *ws)
  84. {
  85. struct led_classdev *led_cdev =
  86. container_of(ws, struct led_classdev, blink_work.work);
  87. unsigned long brightness;
  88. unsigned long delay;
  89. if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
  90. __led_set_brightness(led_cdev, LED_OFF);
  91. return;
  92. }
  93. if (led_cdev->flags & LED_BLINK_ONESHOT_STOP) {
  94. led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
  95. return;
  96. }
  97. brightness = led_get_brightness(led_cdev);
  98. if (!brightness) {
  99. /* Time to switch the LED on. */
  100. brightness = led_cdev->blink_brightness;
  101. delay = led_cdev->blink_delay_on;
  102. } else {
  103. /* Store the current brightness value to be able
  104. * to restore it when the delay_off period is over.
  105. */
  106. led_cdev->blink_brightness = brightness;
  107. brightness = LED_OFF;
  108. delay = led_cdev->blink_delay_off;
  109. }
  110. __led_set_brightness(led_cdev, brightness);
  111. /* Return in next iteration if led is in one-shot mode and we are in
  112. * the final blink state so that the led is toggled each delay_on +
  113. * delay_off milliseconds in worst case.
  114. */
  115. if (led_cdev->flags & LED_BLINK_ONESHOT) {
  116. if (led_cdev->flags & LED_BLINK_INVERT) {
  117. if (brightness)
  118. led_cdev->flags |= LED_BLINK_ONESHOT_STOP;
  119. } else {
  120. if (!brightness)
  121. led_cdev->flags |= LED_BLINK_ONESHOT_STOP;
  122. }
  123. }
  124. queue_delayed_work(system_wq, &led_cdev->blink_work,
  125. msecs_to_jiffies(delay));
  126. }
  127. static void set_brightness_delayed(struct work_struct *ws)
  128. {
  129. struct led_classdev *led_cdev =
  130. container_of(ws, struct led_classdev, set_brightness_work);
  131. led_stop_software_blink(led_cdev);
  132. __led_set_brightness(led_cdev, led_cdev->delayed_set_value);
  133. }
  134. /**
  135. * led_classdev_suspend - suspend an led_classdev.
  136. * @led_cdev: the led_classdev to suspend.
  137. */
  138. void led_classdev_suspend(struct led_classdev *led_cdev)
  139. {
  140. led_cdev->flags |= LED_SUSPENDED;
  141. led_cdev->brightness_set(led_cdev, 0);
  142. }
  143. EXPORT_SYMBOL_GPL(led_classdev_suspend);
  144. /**
  145. * led_classdev_resume - resume an led_classdev.
  146. * @led_cdev: the led_classdev to resume.
  147. */
  148. void led_classdev_resume(struct led_classdev *led_cdev)
  149. {
  150. led_cdev->brightness_set(led_cdev, led_cdev->brightness);
  151. led_cdev->flags &= ~LED_SUSPENDED;
  152. }
  153. EXPORT_SYMBOL_GPL(led_classdev_resume);
  154. static int led_suspend(struct device *dev)
  155. {
  156. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  157. if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
  158. led_classdev_suspend(led_cdev);
  159. return 0;
  160. }
  161. static int led_resume(struct device *dev)
  162. {
  163. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  164. if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
  165. led_classdev_resume(led_cdev);
  166. return 0;
  167. }
  168. static const struct dev_pm_ops leds_class_dev_pm_ops = {
  169. .suspend = led_suspend,
  170. .resume = led_resume,
  171. };
  172. /**
  173. * led_classdev_register - register a new object of led_classdev class.
  174. * @parent: The device to register.
  175. * @led_cdev: the led_classdev structure for this device.
  176. */
  177. int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
  178. {
  179. led_cdev->dev = device_create_with_groups(leds_class, parent, 0,
  180. led_cdev, led_cdev->groups,
  181. "%s", led_cdev->name);
  182. if (IS_ERR(led_cdev->dev))
  183. return PTR_ERR(led_cdev->dev);
  184. #ifdef CONFIG_LEDS_TRIGGERS
  185. init_rwsem(&led_cdev->trigger_lock);
  186. #endif
  187. /* add to the list of leds */
  188. down_write(&leds_list_lock);
  189. list_add_tail(&led_cdev->node, &leds_list);
  190. up_write(&leds_list_lock);
  191. if (!led_cdev->max_brightness)
  192. led_cdev->max_brightness = LED_FULL;
  193. led_update_brightness(led_cdev);
  194. INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
  195. INIT_DELAYED_WORK(&led_cdev->blink_work, led_work_function);
  196. #ifdef CONFIG_LEDS_TRIGGERS
  197. led_trigger_set_default(led_cdev);
  198. #endif
  199. dev_dbg(parent, "Registered led device: %s\n",
  200. led_cdev->name);
  201. return 0;
  202. }
  203. EXPORT_SYMBOL_GPL(led_classdev_register);
  204. /**
  205. * led_classdev_unregister - unregisters a object of led_properties class.
  206. * @led_cdev: the led device to unregister
  207. *
  208. * Unregisters a previously registered via led_classdev_register object.
  209. */
  210. void led_classdev_unregister(struct led_classdev *led_cdev)
  211. {
  212. #ifdef CONFIG_LEDS_TRIGGERS
  213. down_write(&led_cdev->trigger_lock);
  214. if (led_cdev->trigger)
  215. led_trigger_set(led_cdev, NULL);
  216. up_write(&led_cdev->trigger_lock);
  217. #endif
  218. cancel_work_sync(&led_cdev->set_brightness_work);
  219. /* Stop blinking */
  220. led_stop_software_blink(led_cdev);
  221. led_set_brightness(led_cdev, LED_OFF);
  222. device_unregister(led_cdev->dev);
  223. down_write(&leds_list_lock);
  224. list_del(&led_cdev->node);
  225. up_write(&leds_list_lock);
  226. }
  227. EXPORT_SYMBOL_GPL(led_classdev_unregister);
  228. static int __init leds_init(void)
  229. {
  230. leds_class = class_create(THIS_MODULE, "leds");
  231. if (IS_ERR(leds_class))
  232. return PTR_ERR(leds_class);
  233. leds_class->pm = &leds_class_dev_pm_ops;
  234. leds_class->dev_groups = led_groups;
  235. return 0;
  236. }
  237. static void __exit leds_exit(void)
  238. {
  239. class_destroy(leds_class);
  240. }
  241. subsys_initcall(leds_init);
  242. module_exit(leds_exit);
  243. MODULE_AUTHOR("John Lenz, Richard Purdie");
  244. MODULE_LICENSE("GPL");
  245. MODULE_DESCRIPTION("LED Class Interface");