led-class-flash.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * LED Flash class interface
  3. *
  4. * Copyright (C) 2015 Samsung Electronics Co., Ltd.
  5. * Author: Jacek Anaszewski <j.anaszewski@samsung.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/device.h>
  12. #include <linux/init.h>
  13. #include <linux/led-class-flash.h>
  14. #include <linux/leds.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include "leds.h"
  18. #define has_flash_op(fled_cdev, op) \
  19. (fled_cdev && fled_cdev->ops->op)
  20. #define call_flash_op(fled_cdev, op, args...) \
  21. ((has_flash_op(fled_cdev, op)) ? \
  22. (fled_cdev->ops->op(fled_cdev, args)) : \
  23. -EINVAL)
  24. static const char * const led_flash_fault_names[] = {
  25. "led-over-voltage",
  26. "flash-timeout-exceeded",
  27. "controller-over-temperature",
  28. "controller-short-circuit",
  29. "led-power-supply-over-current",
  30. "indicator-led-fault",
  31. "led-under-voltage",
  32. "controller-under-voltage",
  33. "led-over-temperature",
  34. };
  35. static ssize_t flash_brightness_store(struct device *dev,
  36. struct device_attribute *attr, const char *buf, size_t size)
  37. {
  38. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  39. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  40. unsigned long state;
  41. ssize_t ret;
  42. mutex_lock(&led_cdev->led_access);
  43. if (led_sysfs_is_disabled(led_cdev)) {
  44. ret = -EBUSY;
  45. goto unlock;
  46. }
  47. ret = kstrtoul(buf, 10, &state);
  48. if (ret)
  49. goto unlock;
  50. ret = led_set_flash_brightness(fled_cdev, state);
  51. if (ret < 0)
  52. goto unlock;
  53. ret = size;
  54. unlock:
  55. mutex_unlock(&led_cdev->led_access);
  56. return ret;
  57. }
  58. static ssize_t flash_brightness_show(struct device *dev,
  59. struct device_attribute *attr, char *buf)
  60. {
  61. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  62. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  63. /* no lock needed for this */
  64. led_update_flash_brightness(fled_cdev);
  65. return sprintf(buf, "%u\n", fled_cdev->brightness.val);
  66. }
  67. static DEVICE_ATTR_RW(flash_brightness);
  68. static ssize_t max_flash_brightness_show(struct device *dev,
  69. struct device_attribute *attr, char *buf)
  70. {
  71. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  72. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  73. return sprintf(buf, "%u\n", fled_cdev->brightness.max);
  74. }
  75. static DEVICE_ATTR_RO(max_flash_brightness);
  76. static ssize_t flash_strobe_store(struct device *dev,
  77. struct device_attribute *attr, const char *buf, size_t size)
  78. {
  79. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  80. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  81. unsigned long state;
  82. ssize_t ret = -EINVAL;
  83. mutex_lock(&led_cdev->led_access);
  84. if (led_sysfs_is_disabled(led_cdev)) {
  85. ret = -EBUSY;
  86. goto unlock;
  87. }
  88. ret = kstrtoul(buf, 10, &state);
  89. if (ret)
  90. goto unlock;
  91. if (state < 0 || state > 1) {
  92. ret = -EINVAL;
  93. goto unlock;
  94. }
  95. ret = led_set_flash_strobe(fled_cdev, state);
  96. if (ret < 0)
  97. goto unlock;
  98. ret = size;
  99. unlock:
  100. mutex_unlock(&led_cdev->led_access);
  101. return ret;
  102. }
  103. static ssize_t flash_strobe_show(struct device *dev,
  104. struct device_attribute *attr, char *buf)
  105. {
  106. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  107. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  108. bool state;
  109. int ret;
  110. /* no lock needed for this */
  111. ret = led_get_flash_strobe(fled_cdev, &state);
  112. if (ret < 0)
  113. return ret;
  114. return sprintf(buf, "%u\n", state);
  115. }
  116. static DEVICE_ATTR_RW(flash_strobe);
  117. static ssize_t flash_timeout_store(struct device *dev,
  118. struct device_attribute *attr, const char *buf, size_t size)
  119. {
  120. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  121. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  122. unsigned long flash_timeout;
  123. ssize_t ret;
  124. mutex_lock(&led_cdev->led_access);
  125. if (led_sysfs_is_disabled(led_cdev)) {
  126. ret = -EBUSY;
  127. goto unlock;
  128. }
  129. ret = kstrtoul(buf, 10, &flash_timeout);
  130. if (ret)
  131. goto unlock;
  132. ret = led_set_flash_timeout(fled_cdev, flash_timeout);
  133. if (ret < 0)
  134. goto unlock;
  135. ret = size;
  136. unlock:
  137. mutex_unlock(&led_cdev->led_access);
  138. return ret;
  139. }
  140. static ssize_t flash_timeout_show(struct device *dev,
  141. struct device_attribute *attr, char *buf)
  142. {
  143. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  144. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  145. return sprintf(buf, "%u\n", fled_cdev->timeout.val);
  146. }
  147. static DEVICE_ATTR_RW(flash_timeout);
  148. static ssize_t max_flash_timeout_show(struct device *dev,
  149. struct device_attribute *attr, char *buf)
  150. {
  151. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  152. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  153. return sprintf(buf, "%u\n", fled_cdev->timeout.max);
  154. }
  155. static DEVICE_ATTR_RO(max_flash_timeout);
  156. static ssize_t flash_fault_show(struct device *dev,
  157. struct device_attribute *attr, char *buf)
  158. {
  159. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  160. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  161. u32 fault, mask = 0x1;
  162. char *pbuf = buf;
  163. int i, ret, buf_len;
  164. ret = led_get_flash_fault(fled_cdev, &fault);
  165. if (ret < 0)
  166. return -EINVAL;
  167. *buf = '\0';
  168. for (i = 0; i < LED_NUM_FLASH_FAULTS; ++i) {
  169. if (fault & mask) {
  170. buf_len = sprintf(pbuf, "%s ",
  171. led_flash_fault_names[i]);
  172. pbuf += buf_len;
  173. }
  174. mask <<= 1;
  175. }
  176. return sprintf(buf, "%s\n", buf);
  177. }
  178. static DEVICE_ATTR_RO(flash_fault);
  179. static struct attribute *led_flash_strobe_attrs[] = {
  180. &dev_attr_flash_strobe.attr,
  181. NULL,
  182. };
  183. static struct attribute *led_flash_timeout_attrs[] = {
  184. &dev_attr_flash_timeout.attr,
  185. &dev_attr_max_flash_timeout.attr,
  186. NULL,
  187. };
  188. static struct attribute *led_flash_brightness_attrs[] = {
  189. &dev_attr_flash_brightness.attr,
  190. &dev_attr_max_flash_brightness.attr,
  191. NULL,
  192. };
  193. static struct attribute *led_flash_fault_attrs[] = {
  194. &dev_attr_flash_fault.attr,
  195. NULL,
  196. };
  197. static const struct attribute_group led_flash_strobe_group = {
  198. .attrs = led_flash_strobe_attrs,
  199. };
  200. static const struct attribute_group led_flash_timeout_group = {
  201. .attrs = led_flash_timeout_attrs,
  202. };
  203. static const struct attribute_group led_flash_brightness_group = {
  204. .attrs = led_flash_brightness_attrs,
  205. };
  206. static const struct attribute_group led_flash_fault_group = {
  207. .attrs = led_flash_fault_attrs,
  208. };
  209. static void led_flash_resume(struct led_classdev *led_cdev)
  210. {
  211. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  212. call_flash_op(fled_cdev, flash_brightness_set,
  213. fled_cdev->brightness.val);
  214. call_flash_op(fled_cdev, timeout_set, fled_cdev->timeout.val);
  215. }
  216. static void led_flash_init_sysfs_groups(struct led_classdev_flash *fled_cdev)
  217. {
  218. struct led_classdev *led_cdev = &fled_cdev->led_cdev;
  219. const struct led_flash_ops *ops = fled_cdev->ops;
  220. const struct attribute_group **flash_groups = fled_cdev->sysfs_groups;
  221. int num_sysfs_groups = 0;
  222. flash_groups[num_sysfs_groups++] = &led_flash_strobe_group;
  223. if (ops->flash_brightness_set)
  224. flash_groups[num_sysfs_groups++] = &led_flash_brightness_group;
  225. if (ops->timeout_set)
  226. flash_groups[num_sysfs_groups++] = &led_flash_timeout_group;
  227. if (ops->fault_get)
  228. flash_groups[num_sysfs_groups++] = &led_flash_fault_group;
  229. led_cdev->groups = flash_groups;
  230. }
  231. int led_classdev_flash_register(struct device *parent,
  232. struct led_classdev_flash *fled_cdev)
  233. {
  234. struct led_classdev *led_cdev;
  235. const struct led_flash_ops *ops;
  236. int ret;
  237. if (!fled_cdev)
  238. return -EINVAL;
  239. led_cdev = &fled_cdev->led_cdev;
  240. if (led_cdev->flags & LED_DEV_CAP_FLASH) {
  241. if (!led_cdev->brightness_set_sync)
  242. return -EINVAL;
  243. ops = fled_cdev->ops;
  244. if (!ops || !ops->strobe_set)
  245. return -EINVAL;
  246. led_cdev->flash_resume = led_flash_resume;
  247. /* Select the sysfs attributes to be created for the device */
  248. led_flash_init_sysfs_groups(fled_cdev);
  249. }
  250. /* Register led class device */
  251. ret = led_classdev_register(parent, led_cdev);
  252. if (ret < 0)
  253. return ret;
  254. /* Setting a torch brightness needs to have immediate effect */
  255. led_cdev->flags &= ~SET_BRIGHTNESS_ASYNC;
  256. led_cdev->flags |= SET_BRIGHTNESS_SYNC;
  257. return 0;
  258. }
  259. EXPORT_SYMBOL_GPL(led_classdev_flash_register);
  260. void led_classdev_flash_unregister(struct led_classdev_flash *fled_cdev)
  261. {
  262. if (!fled_cdev)
  263. return;
  264. led_classdev_unregister(&fled_cdev->led_cdev);
  265. }
  266. EXPORT_SYMBOL_GPL(led_classdev_flash_unregister);
  267. static void led_clamp_align(struct led_flash_setting *s)
  268. {
  269. u32 v, offset;
  270. v = s->val + s->step / 2;
  271. v = clamp(v, s->min, s->max);
  272. offset = v - s->min;
  273. offset = s->step * (offset / s->step);
  274. s->val = s->min + offset;
  275. }
  276. int led_set_flash_timeout(struct led_classdev_flash *fled_cdev, u32 timeout)
  277. {
  278. struct led_classdev *led_cdev = &fled_cdev->led_cdev;
  279. struct led_flash_setting *s = &fled_cdev->timeout;
  280. s->val = timeout;
  281. led_clamp_align(s);
  282. if (!(led_cdev->flags & LED_SUSPENDED))
  283. return call_flash_op(fled_cdev, timeout_set, s->val);
  284. return 0;
  285. }
  286. EXPORT_SYMBOL_GPL(led_set_flash_timeout);
  287. int led_get_flash_fault(struct led_classdev_flash *fled_cdev, u32 *fault)
  288. {
  289. return call_flash_op(fled_cdev, fault_get, fault);
  290. }
  291. EXPORT_SYMBOL_GPL(led_get_flash_fault);
  292. int led_set_flash_brightness(struct led_classdev_flash *fled_cdev,
  293. u32 brightness)
  294. {
  295. struct led_classdev *led_cdev = &fled_cdev->led_cdev;
  296. struct led_flash_setting *s = &fled_cdev->brightness;
  297. s->val = brightness;
  298. led_clamp_align(s);
  299. if (!(led_cdev->flags & LED_SUSPENDED))
  300. return call_flash_op(fled_cdev, flash_brightness_set, s->val);
  301. return 0;
  302. }
  303. EXPORT_SYMBOL_GPL(led_set_flash_brightness);
  304. int led_update_flash_brightness(struct led_classdev_flash *fled_cdev)
  305. {
  306. struct led_flash_setting *s = &fled_cdev->brightness;
  307. u32 brightness;
  308. if (has_flash_op(fled_cdev, flash_brightness_get)) {
  309. int ret = call_flash_op(fled_cdev, flash_brightness_get,
  310. &brightness);
  311. if (ret < 0)
  312. return ret;
  313. s->val = brightness;
  314. }
  315. return 0;
  316. }
  317. EXPORT_SYMBOL_GPL(led_update_flash_brightness);
  318. MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
  319. MODULE_DESCRIPTION("LED Flash class interface");
  320. MODULE_LICENSE("GPL v2");