led-class.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 void led_update_brightness(struct led_classdev *led_cdev)
  25. {
  26. if (led_cdev->brightness_get)
  27. led_cdev->brightness = led_cdev->brightness_get(led_cdev);
  28. }
  29. static ssize_t brightness_show(struct device *dev,
  30. struct device_attribute *attr, char *buf)
  31. {
  32. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  33. /* no lock needed for this */
  34. led_update_brightness(led_cdev);
  35. return sprintf(buf, "%u\n", led_cdev->brightness);
  36. }
  37. static ssize_t brightness_store(struct device *dev,
  38. struct device_attribute *attr, const char *buf, size_t size)
  39. {
  40. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  41. unsigned long state;
  42. ssize_t ret = -EINVAL;
  43. ret = kstrtoul(buf, 10, &state);
  44. if (ret)
  45. return ret;
  46. if (state == LED_OFF)
  47. led_trigger_remove(led_cdev);
  48. __led_set_brightness(led_cdev, state);
  49. return size;
  50. }
  51. static DEVICE_ATTR_RW(brightness);
  52. static ssize_t led_max_brightness_show(struct device *dev,
  53. struct device_attribute *attr, char *buf)
  54. {
  55. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  56. return sprintf(buf, "%u\n", led_cdev->max_brightness);
  57. }
  58. static DEVICE_ATTR(max_brightness, 0444, led_max_brightness_show, NULL);
  59. #ifdef CONFIG_LEDS_TRIGGERS
  60. static DEVICE_ATTR(trigger, 0644, led_trigger_show, led_trigger_store);
  61. static struct attribute *led_trigger_attrs[] = {
  62. &dev_attr_trigger.attr,
  63. NULL,
  64. };
  65. static const struct attribute_group led_trigger_group = {
  66. .attrs = led_trigger_attrs,
  67. };
  68. #endif
  69. static struct attribute *led_class_attrs[] = {
  70. &dev_attr_brightness.attr,
  71. &dev_attr_max_brightness.attr,
  72. NULL,
  73. };
  74. static const struct attribute_group led_group = {
  75. .attrs = led_class_attrs,
  76. };
  77. static const struct attribute_group *led_groups[] = {
  78. &led_group,
  79. #ifdef CONFIG_LEDS_TRIGGERS
  80. &led_trigger_group,
  81. #endif
  82. NULL,
  83. };
  84. static void led_timer_function(unsigned long data)
  85. {
  86. struct led_classdev *led_cdev = (void *)data;
  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. mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
  125. }
  126. static void set_brightness_delayed(struct work_struct *ws)
  127. {
  128. struct led_classdev *led_cdev =
  129. container_of(ws, struct led_classdev, set_brightness_work);
  130. led_stop_software_blink(led_cdev);
  131. __led_set_brightness(led_cdev, led_cdev->delayed_set_value);
  132. }
  133. /**
  134. * led_classdev_suspend - suspend an led_classdev.
  135. * @led_cdev: the led_classdev to suspend.
  136. */
  137. void led_classdev_suspend(struct led_classdev *led_cdev)
  138. {
  139. led_cdev->flags |= LED_SUSPENDED;
  140. led_cdev->brightness_set(led_cdev, 0);
  141. }
  142. EXPORT_SYMBOL_GPL(led_classdev_suspend);
  143. /**
  144. * led_classdev_resume - resume an led_classdev.
  145. * @led_cdev: the led_classdev to resume.
  146. */
  147. void led_classdev_resume(struct led_classdev *led_cdev)
  148. {
  149. led_cdev->brightness_set(led_cdev, led_cdev->brightness);
  150. led_cdev->flags &= ~LED_SUSPENDED;
  151. }
  152. EXPORT_SYMBOL_GPL(led_classdev_resume);
  153. static int led_suspend(struct device *dev)
  154. {
  155. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  156. if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
  157. led_classdev_suspend(led_cdev);
  158. return 0;
  159. }
  160. static int led_resume(struct device *dev)
  161. {
  162. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  163. if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
  164. led_classdev_resume(led_cdev);
  165. return 0;
  166. }
  167. static const struct dev_pm_ops leds_class_dev_pm_ops = {
  168. .suspend = led_suspend,
  169. .resume = led_resume,
  170. };
  171. /**
  172. * led_classdev_register - register a new object of led_classdev class.
  173. * @parent: The device to register.
  174. * @led_cdev: the led_classdev structure for this device.
  175. */
  176. int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
  177. {
  178. led_cdev->dev = device_create_with_groups(leds_class, parent, 0,
  179. led_cdev, led_cdev->groups,
  180. "%s", led_cdev->name);
  181. if (IS_ERR(led_cdev->dev))
  182. return PTR_ERR(led_cdev->dev);
  183. #ifdef CONFIG_LEDS_TRIGGERS
  184. init_rwsem(&led_cdev->trigger_lock);
  185. #endif
  186. /* add to the list of leds */
  187. down_write(&leds_list_lock);
  188. list_add_tail(&led_cdev->node, &leds_list);
  189. up_write(&leds_list_lock);
  190. if (!led_cdev->max_brightness)
  191. led_cdev->max_brightness = LED_FULL;
  192. led_update_brightness(led_cdev);
  193. INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
  194. init_timer(&led_cdev->blink_timer);
  195. led_cdev->blink_timer.function = led_timer_function;
  196. led_cdev->blink_timer.data = (unsigned long)led_cdev;
  197. #ifdef CONFIG_LEDS_TRIGGERS
  198. led_trigger_set_default(led_cdev);
  199. #endif
  200. dev_dbg(parent, "Registered led device: %s\n",
  201. led_cdev->name);
  202. return 0;
  203. }
  204. EXPORT_SYMBOL_GPL(led_classdev_register);
  205. /**
  206. * led_classdev_unregister - unregisters a object of led_properties class.
  207. * @led_cdev: the led device to unregister
  208. *
  209. * Unregisters a previously registered via led_classdev_register object.
  210. */
  211. void led_classdev_unregister(struct led_classdev *led_cdev)
  212. {
  213. #ifdef CONFIG_LEDS_TRIGGERS
  214. down_write(&led_cdev->trigger_lock);
  215. if (led_cdev->trigger)
  216. led_trigger_set(led_cdev, NULL);
  217. up_write(&led_cdev->trigger_lock);
  218. #endif
  219. cancel_work_sync(&led_cdev->set_brightness_work);
  220. /* Stop blinking */
  221. led_stop_software_blink(led_cdev);
  222. led_set_brightness(led_cdev, LED_OFF);
  223. device_unregister(led_cdev->dev);
  224. down_write(&leds_list_lock);
  225. list_del(&led_cdev->node);
  226. up_write(&leds_list_lock);
  227. }
  228. EXPORT_SYMBOL_GPL(led_classdev_unregister);
  229. static int __init leds_init(void)
  230. {
  231. leds_class = class_create(THIS_MODULE, "leds");
  232. if (IS_ERR(leds_class))
  233. return PTR_ERR(leds_class);
  234. leds_class->pm = &leds_class_dev_pm_ops;
  235. leds_class->dev_groups = led_groups;
  236. return 0;
  237. }
  238. static void __exit leds_exit(void)
  239. {
  240. class_destroy(leds_class);
  241. }
  242. subsys_initcall(leds_init);
  243. module_exit(leds_exit);
  244. MODULE_AUTHOR("John Lenz, Richard Purdie");
  245. MODULE_LICENSE("GPL");
  246. MODULE_DESCRIPTION("LED Class Interface");