led-class.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. static int led_suspend(struct device *dev)
  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. static const struct dev_pm_ops leds_class_dev_pm_ops = {
  163. .suspend = led_suspend,
  164. .resume = led_resume,
  165. };
  166. /**
  167. * led_classdev_register - register a new object of led_classdev class.
  168. * @parent: The device to register.
  169. * @led_cdev: the led_classdev structure for this device.
  170. */
  171. int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
  172. {
  173. led_cdev->dev = device_create_with_groups(leds_class, parent, 0,
  174. led_cdev, led_cdev->groups,
  175. "%s", led_cdev->name);
  176. if (IS_ERR(led_cdev->dev))
  177. return PTR_ERR(led_cdev->dev);
  178. #ifdef CONFIG_LEDS_TRIGGERS
  179. init_rwsem(&led_cdev->trigger_lock);
  180. #endif
  181. /* add to the list of leds */
  182. down_write(&leds_list_lock);
  183. list_add_tail(&led_cdev->node, &leds_list);
  184. up_write(&leds_list_lock);
  185. if (!led_cdev->max_brightness)
  186. led_cdev->max_brightness = LED_FULL;
  187. led_update_brightness(led_cdev);
  188. INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
  189. init_timer(&led_cdev->blink_timer);
  190. led_cdev->blink_timer.function = led_timer_function;
  191. led_cdev->blink_timer.data = (unsigned long)led_cdev;
  192. #ifdef CONFIG_LEDS_TRIGGERS
  193. led_trigger_set_default(led_cdev);
  194. #endif
  195. dev_dbg(parent, "Registered led device: %s\n",
  196. led_cdev->name);
  197. return 0;
  198. }
  199. EXPORT_SYMBOL_GPL(led_classdev_register);
  200. /**
  201. * led_classdev_unregister - unregisters a object of led_properties class.
  202. * @led_cdev: the led device to unregister
  203. *
  204. * Unregisters a previously registered via led_classdev_register object.
  205. */
  206. void led_classdev_unregister(struct led_classdev *led_cdev)
  207. {
  208. #ifdef CONFIG_LEDS_TRIGGERS
  209. down_write(&led_cdev->trigger_lock);
  210. if (led_cdev->trigger)
  211. led_trigger_set(led_cdev, NULL);
  212. up_write(&led_cdev->trigger_lock);
  213. #endif
  214. cancel_work_sync(&led_cdev->set_brightness_work);
  215. /* Stop blinking */
  216. led_stop_software_blink(led_cdev);
  217. led_set_brightness(led_cdev, LED_OFF);
  218. device_unregister(led_cdev->dev);
  219. down_write(&leds_list_lock);
  220. list_del(&led_cdev->node);
  221. up_write(&leds_list_lock);
  222. }
  223. EXPORT_SYMBOL_GPL(led_classdev_unregister);
  224. static int __init leds_init(void)
  225. {
  226. leds_class = class_create(THIS_MODULE, "leds");
  227. if (IS_ERR(leds_class))
  228. return PTR_ERR(leds_class);
  229. leds_class->pm = &leds_class_dev_pm_ops;
  230. leds_class->dev_groups = led_groups;
  231. return 0;
  232. }
  233. static void __exit leds_exit(void)
  234. {
  235. class_destroy(leds_class);
  236. }
  237. subsys_initcall(leds_init);
  238. module_exit(leds_exit);
  239. MODULE_AUTHOR("John Lenz, Richard Purdie");
  240. MODULE_LICENSE("GPL");
  241. MODULE_DESCRIPTION("LED Class Interface");