led-class.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. /**
  88. * led_classdev_suspend - suspend an led_classdev.
  89. * @led_cdev: the led_classdev to suspend.
  90. */
  91. void led_classdev_suspend(struct led_classdev *led_cdev)
  92. {
  93. led_cdev->flags |= LED_SUSPENDED;
  94. led_cdev->brightness_set(led_cdev, 0);
  95. }
  96. EXPORT_SYMBOL_GPL(led_classdev_suspend);
  97. /**
  98. * led_classdev_resume - resume an led_classdev.
  99. * @led_cdev: the led_classdev to resume.
  100. */
  101. void led_classdev_resume(struct led_classdev *led_cdev)
  102. {
  103. led_cdev->brightness_set(led_cdev, led_cdev->brightness);
  104. if (led_cdev->flash_resume)
  105. led_cdev->flash_resume(led_cdev);
  106. led_cdev->flags &= ~LED_SUSPENDED;
  107. }
  108. EXPORT_SYMBOL_GPL(led_classdev_resume);
  109. #ifdef CONFIG_PM_SLEEP
  110. static int led_suspend(struct device *dev)
  111. {
  112. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  113. if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
  114. led_classdev_suspend(led_cdev);
  115. return 0;
  116. }
  117. static int led_resume(struct device *dev)
  118. {
  119. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  120. if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
  121. led_classdev_resume(led_cdev);
  122. return 0;
  123. }
  124. #endif
  125. static SIMPLE_DEV_PM_OPS(leds_class_dev_pm_ops, led_suspend, led_resume);
  126. static int match_name(struct device *dev, const void *data)
  127. {
  128. if (!dev_name(dev))
  129. return 0;
  130. return !strcmp(dev_name(dev), (char *)data);
  131. }
  132. static int led_classdev_next_name(const char *init_name, char *name,
  133. size_t len)
  134. {
  135. unsigned int i = 0;
  136. int ret = 0;
  137. struct device *dev;
  138. strlcpy(name, init_name, len);
  139. while ((ret < len) &&
  140. (dev = class_find_device(leds_class, NULL, name, match_name))) {
  141. put_device(dev);
  142. ret = snprintf(name, len, "%s_%u", init_name, ++i);
  143. }
  144. if (ret >= len)
  145. return -ENOMEM;
  146. return i;
  147. }
  148. /**
  149. * led_classdev_register - register a new object of led_classdev class.
  150. * @parent: The device to register.
  151. * @led_cdev: the led_classdev structure for this device.
  152. */
  153. int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
  154. {
  155. char name[64];
  156. int ret;
  157. ret = led_classdev_next_name(led_cdev->name, name, sizeof(name));
  158. if (ret < 0)
  159. return ret;
  160. led_cdev->dev = device_create_with_groups(leds_class, parent, 0,
  161. led_cdev, led_cdev->groups, "%s", name);
  162. if (IS_ERR(led_cdev->dev))
  163. return PTR_ERR(led_cdev->dev);
  164. if (ret)
  165. dev_warn(parent, "Led %s renamed to %s due to name collision",
  166. led_cdev->name, dev_name(led_cdev->dev));
  167. #ifdef CONFIG_LEDS_TRIGGERS
  168. init_rwsem(&led_cdev->trigger_lock);
  169. #endif
  170. mutex_init(&led_cdev->led_access);
  171. /* add to the list of leds */
  172. down_write(&leds_list_lock);
  173. list_add_tail(&led_cdev->node, &leds_list);
  174. up_write(&leds_list_lock);
  175. if (!led_cdev->max_brightness)
  176. led_cdev->max_brightness = LED_FULL;
  177. led_cdev->flags |= SET_BRIGHTNESS_ASYNC;
  178. led_update_brightness(led_cdev);
  179. led_init_core(led_cdev);
  180. #ifdef CONFIG_LEDS_TRIGGERS
  181. led_trigger_set_default(led_cdev);
  182. #endif
  183. dev_dbg(parent, "Registered led device: %s\n",
  184. led_cdev->name);
  185. return 0;
  186. }
  187. EXPORT_SYMBOL_GPL(led_classdev_register);
  188. /**
  189. * led_classdev_unregister - unregisters a object of led_properties class.
  190. * @led_cdev: the led device to unregister
  191. *
  192. * Unregisters a previously registered via led_classdev_register object.
  193. */
  194. void led_classdev_unregister(struct led_classdev *led_cdev)
  195. {
  196. #ifdef CONFIG_LEDS_TRIGGERS
  197. down_write(&led_cdev->trigger_lock);
  198. if (led_cdev->trigger)
  199. led_trigger_set(led_cdev, NULL);
  200. up_write(&led_cdev->trigger_lock);
  201. #endif
  202. /* Stop blinking */
  203. led_stop_software_blink(led_cdev);
  204. led_set_brightness(led_cdev, LED_OFF);
  205. flush_work(&led_cdev->set_brightness_work);
  206. device_unregister(led_cdev->dev);
  207. down_write(&leds_list_lock);
  208. list_del(&led_cdev->node);
  209. up_write(&leds_list_lock);
  210. mutex_destroy(&led_cdev->led_access);
  211. }
  212. EXPORT_SYMBOL_GPL(led_classdev_unregister);
  213. static void devm_led_classdev_release(struct device *dev, void *res)
  214. {
  215. led_classdev_unregister(*(struct led_classdev **)res);
  216. }
  217. /**
  218. * devm_led_classdev_register - resource managed led_classdev_register()
  219. * @parent: The device to register.
  220. * @led_cdev: the led_classdev structure for this device.
  221. */
  222. int devm_led_classdev_register(struct device *parent,
  223. struct led_classdev *led_cdev)
  224. {
  225. struct led_classdev **dr;
  226. int rc;
  227. dr = devres_alloc(devm_led_classdev_release, sizeof(*dr), GFP_KERNEL);
  228. if (!dr)
  229. return -ENOMEM;
  230. rc = led_classdev_register(parent, led_cdev);
  231. if (rc) {
  232. devres_free(dr);
  233. return rc;
  234. }
  235. *dr = led_cdev;
  236. devres_add(parent, dr);
  237. return 0;
  238. }
  239. EXPORT_SYMBOL_GPL(devm_led_classdev_register);
  240. static int devm_led_classdev_match(struct device *dev, void *res, void *data)
  241. {
  242. struct led_cdev **p = res;
  243. if (WARN_ON(!p || !*p))
  244. return 0;
  245. return *p == data;
  246. }
  247. /**
  248. * devm_led_classdev_unregister() - resource managed led_classdev_unregister()
  249. * @parent: The device to unregister.
  250. * @led_cdev: the led_classdev structure for this device.
  251. */
  252. void devm_led_classdev_unregister(struct device *dev,
  253. struct led_classdev *led_cdev)
  254. {
  255. WARN_ON(devres_release(dev,
  256. devm_led_classdev_release,
  257. devm_led_classdev_match, led_cdev));
  258. }
  259. EXPORT_SYMBOL_GPL(devm_led_classdev_unregister);
  260. static int __init leds_init(void)
  261. {
  262. leds_class = class_create(THIS_MODULE, "leds");
  263. if (IS_ERR(leds_class))
  264. return PTR_ERR(leds_class);
  265. leds_class->pm = &leds_class_dev_pm_ops;
  266. leds_class->dev_groups = led_groups;
  267. return 0;
  268. }
  269. static void __exit leds_exit(void)
  270. {
  271. class_destroy(leds_class);
  272. }
  273. subsys_initcall(leds_init);
  274. module_exit(leds_exit);
  275. MODULE_AUTHOR("John Lenz, Richard Purdie");
  276. MODULE_LICENSE("GPL");
  277. MODULE_DESCRIPTION("LED Class Interface");