led-class-flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 ssize_t available_sync_leds_show(struct device *dev,
  180. struct device_attribute *attr, char *buf)
  181. {
  182. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  183. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  184. char *pbuf = buf;
  185. int i, buf_len;
  186. buf_len = sprintf(pbuf, "[0: none] ");
  187. pbuf += buf_len;
  188. for (i = 0; i < fled_cdev->num_sync_leds; ++i) {
  189. buf_len = sprintf(pbuf, "[%d: %s] ", i + 1,
  190. fled_cdev->sync_leds[i]->led_cdev.name);
  191. pbuf += buf_len;
  192. }
  193. return sprintf(buf, "%s\n", buf);
  194. }
  195. static DEVICE_ATTR_RO(available_sync_leds);
  196. static ssize_t flash_sync_strobe_store(struct device *dev,
  197. struct device_attribute *attr, const char *buf, size_t size)
  198. {
  199. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  200. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  201. unsigned long led_id;
  202. ssize_t ret;
  203. mutex_lock(&led_cdev->led_access);
  204. if (led_sysfs_is_disabled(led_cdev)) {
  205. ret = -EBUSY;
  206. goto unlock;
  207. }
  208. ret = kstrtoul(buf, 10, &led_id);
  209. if (ret)
  210. goto unlock;
  211. if (led_id > fled_cdev->num_sync_leds) {
  212. ret = -ERANGE;
  213. goto unlock;
  214. }
  215. fled_cdev->sync_led_id = led_id;
  216. ret = size;
  217. unlock:
  218. mutex_unlock(&led_cdev->led_access);
  219. return ret;
  220. }
  221. static ssize_t flash_sync_strobe_show(struct device *dev,
  222. struct device_attribute *attr, char *buf)
  223. {
  224. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  225. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  226. int sled_id = fled_cdev->sync_led_id;
  227. char *sync_led_name = "none";
  228. if (fled_cdev->sync_led_id > 0)
  229. sync_led_name = (char *)
  230. fled_cdev->sync_leds[sled_id - 1]->led_cdev.name;
  231. return sprintf(buf, "[%d: %s]\n", sled_id, sync_led_name);
  232. }
  233. static DEVICE_ATTR_RW(flash_sync_strobe);
  234. static struct attribute *led_flash_strobe_attrs[] = {
  235. &dev_attr_flash_strobe.attr,
  236. NULL,
  237. };
  238. static struct attribute *led_flash_timeout_attrs[] = {
  239. &dev_attr_flash_timeout.attr,
  240. &dev_attr_max_flash_timeout.attr,
  241. NULL,
  242. };
  243. static struct attribute *led_flash_brightness_attrs[] = {
  244. &dev_attr_flash_brightness.attr,
  245. &dev_attr_max_flash_brightness.attr,
  246. NULL,
  247. };
  248. static struct attribute *led_flash_fault_attrs[] = {
  249. &dev_attr_flash_fault.attr,
  250. NULL,
  251. };
  252. static struct attribute *led_flash_sync_strobe_attrs[] = {
  253. &dev_attr_available_sync_leds.attr,
  254. &dev_attr_flash_sync_strobe.attr,
  255. NULL,
  256. };
  257. static const struct attribute_group led_flash_strobe_group = {
  258. .attrs = led_flash_strobe_attrs,
  259. };
  260. static const struct attribute_group led_flash_timeout_group = {
  261. .attrs = led_flash_timeout_attrs,
  262. };
  263. static const struct attribute_group led_flash_brightness_group = {
  264. .attrs = led_flash_brightness_attrs,
  265. };
  266. static const struct attribute_group led_flash_fault_group = {
  267. .attrs = led_flash_fault_attrs,
  268. };
  269. static const struct attribute_group led_flash_sync_strobe_group = {
  270. .attrs = led_flash_sync_strobe_attrs,
  271. };
  272. static void led_flash_resume(struct led_classdev *led_cdev)
  273. {
  274. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  275. call_flash_op(fled_cdev, flash_brightness_set,
  276. fled_cdev->brightness.val);
  277. call_flash_op(fled_cdev, timeout_set, fled_cdev->timeout.val);
  278. }
  279. static void led_flash_init_sysfs_groups(struct led_classdev_flash *fled_cdev)
  280. {
  281. struct led_classdev *led_cdev = &fled_cdev->led_cdev;
  282. const struct led_flash_ops *ops = fled_cdev->ops;
  283. const struct attribute_group **flash_groups = fled_cdev->sysfs_groups;
  284. int num_sysfs_groups = 0;
  285. flash_groups[num_sysfs_groups++] = &led_flash_strobe_group;
  286. if (ops->flash_brightness_set)
  287. flash_groups[num_sysfs_groups++] = &led_flash_brightness_group;
  288. if (ops->timeout_set)
  289. flash_groups[num_sysfs_groups++] = &led_flash_timeout_group;
  290. if (ops->fault_get)
  291. flash_groups[num_sysfs_groups++] = &led_flash_fault_group;
  292. if (led_cdev->flags & LED_DEV_CAP_SYNC_STROBE)
  293. flash_groups[num_sysfs_groups++] = &led_flash_sync_strobe_group;
  294. led_cdev->groups = flash_groups;
  295. }
  296. int led_classdev_flash_register(struct device *parent,
  297. struct led_classdev_flash *fled_cdev)
  298. {
  299. struct led_classdev *led_cdev;
  300. const struct led_flash_ops *ops;
  301. int ret;
  302. if (!fled_cdev)
  303. return -EINVAL;
  304. led_cdev = &fled_cdev->led_cdev;
  305. if (led_cdev->flags & LED_DEV_CAP_FLASH) {
  306. if (!led_cdev->brightness_set_sync)
  307. return -EINVAL;
  308. ops = fled_cdev->ops;
  309. if (!ops || !ops->strobe_set)
  310. return -EINVAL;
  311. led_cdev->flash_resume = led_flash_resume;
  312. /* Select the sysfs attributes to be created for the device */
  313. led_flash_init_sysfs_groups(fled_cdev);
  314. }
  315. /* Register led class device */
  316. ret = led_classdev_register(parent, led_cdev);
  317. if (ret < 0)
  318. return ret;
  319. /* Setting a torch brightness needs to have immediate effect */
  320. led_cdev->flags &= ~SET_BRIGHTNESS_ASYNC;
  321. led_cdev->flags |= SET_BRIGHTNESS_SYNC;
  322. return 0;
  323. }
  324. EXPORT_SYMBOL_GPL(led_classdev_flash_register);
  325. void led_classdev_flash_unregister(struct led_classdev_flash *fled_cdev)
  326. {
  327. if (!fled_cdev)
  328. return;
  329. led_classdev_unregister(&fled_cdev->led_cdev);
  330. }
  331. EXPORT_SYMBOL_GPL(led_classdev_flash_unregister);
  332. static void led_clamp_align(struct led_flash_setting *s)
  333. {
  334. u32 v, offset;
  335. v = s->val + s->step / 2;
  336. v = clamp(v, s->min, s->max);
  337. offset = v - s->min;
  338. offset = s->step * (offset / s->step);
  339. s->val = s->min + offset;
  340. }
  341. int led_set_flash_timeout(struct led_classdev_flash *fled_cdev, u32 timeout)
  342. {
  343. struct led_classdev *led_cdev = &fled_cdev->led_cdev;
  344. struct led_flash_setting *s = &fled_cdev->timeout;
  345. s->val = timeout;
  346. led_clamp_align(s);
  347. if (!(led_cdev->flags & LED_SUSPENDED))
  348. return call_flash_op(fled_cdev, timeout_set, s->val);
  349. return 0;
  350. }
  351. EXPORT_SYMBOL_GPL(led_set_flash_timeout);
  352. int led_get_flash_fault(struct led_classdev_flash *fled_cdev, u32 *fault)
  353. {
  354. return call_flash_op(fled_cdev, fault_get, fault);
  355. }
  356. EXPORT_SYMBOL_GPL(led_get_flash_fault);
  357. int led_set_flash_brightness(struct led_classdev_flash *fled_cdev,
  358. u32 brightness)
  359. {
  360. struct led_classdev *led_cdev = &fled_cdev->led_cdev;
  361. struct led_flash_setting *s = &fled_cdev->brightness;
  362. s->val = brightness;
  363. led_clamp_align(s);
  364. if (!(led_cdev->flags & LED_SUSPENDED))
  365. return call_flash_op(fled_cdev, flash_brightness_set, s->val);
  366. return 0;
  367. }
  368. EXPORT_SYMBOL_GPL(led_set_flash_brightness);
  369. int led_update_flash_brightness(struct led_classdev_flash *fled_cdev)
  370. {
  371. struct led_flash_setting *s = &fled_cdev->brightness;
  372. u32 brightness;
  373. if (has_flash_op(fled_cdev, flash_brightness_get)) {
  374. int ret = call_flash_op(fled_cdev, flash_brightness_get,
  375. &brightness);
  376. if (ret < 0)
  377. return ret;
  378. s->val = brightness;
  379. }
  380. return 0;
  381. }
  382. EXPORT_SYMBOL_GPL(led_update_flash_brightness);
  383. MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
  384. MODULE_DESCRIPTION("LED Flash class interface");
  385. MODULE_LICENSE("GPL v2");