led-class-flash.c 11 KB

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