led-class.c 7.4 KB

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