led-class-flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 <[email protected]>
  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 = -EBUSY;
  80. mutex_lock(&led_cdev->led_access);
  81. if (led_sysfs_is_disabled(led_cdev))
  82. goto unlock;
  83. ret = kstrtoul(buf, 10, &state);
  84. if (ret)
  85. goto unlock;
  86. if (state > 1) {
  87. ret = -EINVAL;
  88. goto unlock;
  89. }
  90. ret = led_set_flash_strobe(fled_cdev, state);
  91. if (ret < 0)
  92. goto unlock;
  93. ret = size;
  94. unlock:
  95. mutex_unlock(&led_cdev->led_access);
  96. return ret;
  97. }
  98. static ssize_t flash_strobe_show(struct device *dev,
  99. struct device_attribute *attr, char *buf)
  100. {
  101. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  102. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  103. bool state;
  104. int ret;
  105. /* no lock needed for this */
  106. ret = led_get_flash_strobe(fled_cdev, &state);
  107. if (ret < 0)
  108. return ret;
  109. return sprintf(buf, "%u\n", state);
  110. }
  111. static DEVICE_ATTR_RW(flash_strobe);
  112. static ssize_t flash_timeout_store(struct device *dev,
  113. struct device_attribute *attr, const char *buf, size_t size)
  114. {
  115. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  116. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  117. unsigned long flash_timeout;
  118. ssize_t ret;
  119. mutex_lock(&led_cdev->led_access);
  120. if (led_sysfs_is_disabled(led_cdev)) {
  121. ret = -EBUSY;
  122. goto unlock;
  123. }
  124. ret = kstrtoul(buf, 10, &flash_timeout);
  125. if (ret)
  126. goto unlock;
  127. ret = led_set_flash_timeout(fled_cdev, flash_timeout);
  128. if (ret < 0)
  129. goto unlock;
  130. ret = size;
  131. unlock:
  132. mutex_unlock(&led_cdev->led_access);
  133. return ret;
  134. }
  135. static ssize_t flash_timeout_show(struct device *dev,
  136. struct device_attribute *attr, char *buf)
  137. {
  138. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  139. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  140. return sprintf(buf, "%u\n", fled_cdev->timeout.val);
  141. }
  142. static DEVICE_ATTR_RW(flash_timeout);
  143. static ssize_t max_flash_timeout_show(struct device *dev,
  144. struct device_attribute *attr, char *buf)
  145. {
  146. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  147. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  148. return sprintf(buf, "%u\n", fled_cdev->timeout.max);
  149. }
  150. static DEVICE_ATTR_RO(max_flash_timeout);
  151. static ssize_t flash_fault_show(struct device *dev,
  152. struct device_attribute *attr, char *buf)
  153. {
  154. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  155. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  156. u32 fault, mask = 0x1;
  157. char *pbuf = buf;
  158. int i, ret, buf_len;
  159. ret = led_get_flash_fault(fled_cdev, &fault);
  160. if (ret < 0)
  161. return -EINVAL;
  162. *buf = '\0';
  163. for (i = 0; i < LED_NUM_FLASH_FAULTS; ++i) {
  164. if (fault & mask) {
  165. buf_len = sprintf(pbuf, "%s ",
  166. led_flash_fault_names[i]);
  167. pbuf += buf_len;
  168. }
  169. mask <<= 1;
  170. }
  171. return strlen(strcat(buf, "\n"));
  172. }
  173. static DEVICE_ATTR_RO(flash_fault);
  174. static struct attribute *led_flash_strobe_attrs[] = {
  175. &dev_attr_flash_strobe.attr,
  176. NULL,
  177. };
  178. static struct attribute *led_flash_timeout_attrs[] = {
  179. &dev_attr_flash_timeout.attr,
  180. &dev_attr_max_flash_timeout.attr,
  181. NULL,
  182. };
  183. static struct attribute *led_flash_brightness_attrs[] = {
  184. &dev_attr_flash_brightness.attr,
  185. &dev_attr_max_flash_brightness.attr,
  186. NULL,
  187. };
  188. static struct attribute *led_flash_fault_attrs[] = {
  189. &dev_attr_flash_fault.attr,
  190. NULL,
  191. };
  192. static const struct attribute_group led_flash_strobe_group = {
  193. .attrs = led_flash_strobe_attrs,
  194. };
  195. static const struct attribute_group led_flash_timeout_group = {
  196. .attrs = led_flash_timeout_attrs,
  197. };
  198. static const struct attribute_group led_flash_brightness_group = {
  199. .attrs = led_flash_brightness_attrs,
  200. };
  201. static const struct attribute_group led_flash_fault_group = {
  202. .attrs = led_flash_fault_attrs,
  203. };
  204. static void led_flash_resume(struct led_classdev *led_cdev)
  205. {
  206. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  207. call_flash_op(fled_cdev, flash_brightness_set,
  208. fled_cdev->brightness.val);
  209. call_flash_op(fled_cdev, timeout_set, fled_cdev->timeout.val);
  210. }
  211. static void led_flash_init_sysfs_groups(struct led_classdev_flash *fled_cdev)
  212. {
  213. struct led_classdev *led_cdev = &fled_cdev->led_cdev;
  214. const struct led_flash_ops *ops = fled_cdev->ops;
  215. const struct attribute_group **flash_groups = fled_cdev->sysfs_groups;
  216. int num_sysfs_groups = 0;
  217. flash_groups[num_sysfs_groups++] = &led_flash_strobe_group;
  218. if (ops->flash_brightness_set)
  219. flash_groups[num_sysfs_groups++] = &led_flash_brightness_group;
  220. if (ops->timeout_set)
  221. flash_groups[num_sysfs_groups++] = &led_flash_timeout_group;
  222. if (ops->fault_get)
  223. flash_groups[num_sysfs_groups++] = &led_flash_fault_group;
  224. led_cdev->groups = flash_groups;
  225. }
  226. int led_classdev_flash_register_ext(struct device *parent,
  227. struct led_classdev_flash *fled_cdev,
  228. struct led_init_data *init_data)
  229. {
  230. struct led_classdev *led_cdev;
  231. const struct led_flash_ops *ops;
  232. int ret;
  233. if (!fled_cdev)
  234. return -EINVAL;
  235. led_cdev = &fled_cdev->led_cdev;
  236. if (led_cdev->flags & LED_DEV_CAP_FLASH) {
  237. if (!led_cdev->brightness_set_blocking)
  238. return -EINVAL;
  239. ops = fled_cdev->ops;
  240. if (!ops || !ops->strobe_set)
  241. return -EINVAL;
  242. led_cdev->flash_resume = led_flash_resume;
  243. /* Select the sysfs attributes to be created for the device */
  244. led_flash_init_sysfs_groups(fled_cdev);
  245. }
  246. /* Register led class device */
  247. ret = led_classdev_register_ext(parent, led_cdev, init_data);
  248. if (ret < 0)
  249. return ret;
  250. return 0;
  251. }
  252. EXPORT_SYMBOL_GPL(led_classdev_flash_register_ext);
  253. void led_classdev_flash_unregister(struct led_classdev_flash *fled_cdev)
  254. {
  255. if (!fled_cdev)
  256. return;
  257. led_classdev_unregister(&fled_cdev->led_cdev);
  258. }
  259. EXPORT_SYMBOL_GPL(led_classdev_flash_unregister);
  260. static void devm_led_classdev_flash_release(struct device *dev, void *res)
  261. {
  262. led_classdev_flash_unregister(*(struct led_classdev_flash **)res);
  263. }
  264. int devm_led_classdev_flash_register_ext(struct device *parent,
  265. struct led_classdev_flash *fled_cdev,
  266. struct led_init_data *init_data)
  267. {
  268. struct led_classdev_flash **dr;
  269. int ret;
  270. dr = devres_alloc(devm_led_classdev_flash_release, sizeof(*dr),
  271. GFP_KERNEL);
  272. if (!dr)
  273. return -ENOMEM;
  274. ret = led_classdev_flash_register_ext(parent, fled_cdev, init_data);
  275. if (ret) {
  276. devres_free(dr);
  277. return ret;
  278. }
  279. *dr = fled_cdev;
  280. devres_add(parent, dr);
  281. return 0;
  282. }
  283. EXPORT_SYMBOL_GPL(devm_led_classdev_flash_register_ext);
  284. static int devm_led_classdev_flash_match(struct device *dev,
  285. void *res, void *data)
  286. {
  287. struct led_classdev_flash **p = res;
  288. if (WARN_ON(!p || !*p))
  289. return 0;
  290. return *p == data;
  291. }
  292. void devm_led_classdev_flash_unregister(struct device *dev,
  293. struct led_classdev_flash *fled_cdev)
  294. {
  295. WARN_ON(devres_release(dev,
  296. devm_led_classdev_flash_release,
  297. devm_led_classdev_flash_match, fled_cdev));
  298. }
  299. EXPORT_SYMBOL_GPL(devm_led_classdev_flash_unregister);
  300. static void led_clamp_align(struct led_flash_setting *s)
  301. {
  302. u32 v, offset;
  303. v = s->val + s->step / 2;
  304. v = clamp(v, s->min, s->max);
  305. offset = v - s->min;
  306. offset = s->step * (offset / s->step);
  307. s->val = s->min + offset;
  308. }
  309. int led_set_flash_timeout(struct led_classdev_flash *fled_cdev, u32 timeout)
  310. {
  311. struct led_classdev *led_cdev = &fled_cdev->led_cdev;
  312. struct led_flash_setting *s = &fled_cdev->timeout;
  313. s->val = timeout;
  314. led_clamp_align(s);
  315. if (!(led_cdev->flags & LED_SUSPENDED))
  316. return call_flash_op(fled_cdev, timeout_set, s->val);
  317. return 0;
  318. }
  319. EXPORT_SYMBOL_GPL(led_set_flash_timeout);
  320. int led_get_flash_fault(struct led_classdev_flash *fled_cdev, u32 *fault)
  321. {
  322. return call_flash_op(fled_cdev, fault_get, fault);
  323. }
  324. EXPORT_SYMBOL_GPL(led_get_flash_fault);
  325. int led_set_flash_brightness(struct led_classdev_flash *fled_cdev,
  326. u32 brightness)
  327. {
  328. struct led_classdev *led_cdev = &fled_cdev->led_cdev;
  329. struct led_flash_setting *s = &fled_cdev->brightness;
  330. s->val = brightness;
  331. led_clamp_align(s);
  332. if (!(led_cdev->flags & LED_SUSPENDED))
  333. return call_flash_op(fled_cdev, flash_brightness_set, s->val);
  334. return 0;
  335. }
  336. EXPORT_SYMBOL_GPL(led_set_flash_brightness);
  337. int led_update_flash_brightness(struct led_classdev_flash *fled_cdev)
  338. {
  339. struct led_flash_setting *s = &fled_cdev->brightness;
  340. u32 brightness;
  341. if (has_flash_op(fled_cdev, flash_brightness_get)) {
  342. int ret = call_flash_op(fled_cdev, flash_brightness_get,
  343. &brightness);
  344. if (ret < 0)
  345. return ret;
  346. s->val = brightness;
  347. }
  348. return 0;
  349. }
  350. EXPORT_SYMBOL_GPL(led_update_flash_brightness);
  351. MODULE_AUTHOR("Jacek Anaszewski <[email protected]>");
  352. MODULE_DESCRIPTION("LED Flash class interface");
  353. MODULE_LICENSE("GPL v2");