led-class.c 7.3 KB

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