led-class-flash.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #ifndef __LINUX_FLASH_LEDS_H_INCLUDED
  9. #define __LINUX_FLASH_LEDS_H_INCLUDED
  10. #include <linux/leds.h>
  11. struct device_node;
  12. struct led_classdev_flash;
  13. /*
  14. * Supported led fault bits - must be kept in synch
  15. * with V4L2_FLASH_FAULT bits.
  16. */
  17. #define LED_FAULT_OVER_VOLTAGE (1 << 0)
  18. #define LED_FAULT_TIMEOUT (1 << 1)
  19. #define LED_FAULT_OVER_TEMPERATURE (1 << 2)
  20. #define LED_FAULT_SHORT_CIRCUIT (1 << 3)
  21. #define LED_FAULT_OVER_CURRENT (1 << 4)
  22. #define LED_FAULT_INDICATOR (1 << 5)
  23. #define LED_FAULT_UNDER_VOLTAGE (1 << 6)
  24. #define LED_FAULT_INPUT_VOLTAGE (1 << 7)
  25. #define LED_FAULT_LED_OVER_TEMPERATURE (1 << 8)
  26. #define LED_NUM_FLASH_FAULTS 9
  27. #define LED_FLASH_SYSFS_GROUPS_SIZE 5
  28. struct led_flash_ops {
  29. /* set flash brightness */
  30. int (*flash_brightness_set)(struct led_classdev_flash *fled_cdev,
  31. u32 brightness);
  32. /* get flash brightness */
  33. int (*flash_brightness_get)(struct led_classdev_flash *fled_cdev,
  34. u32 *brightness);
  35. /* set flash strobe state */
  36. int (*strobe_set)(struct led_classdev_flash *fled_cdev, bool state);
  37. /* get flash strobe state */
  38. int (*strobe_get)(struct led_classdev_flash *fled_cdev, bool *state);
  39. /* set flash timeout */
  40. int (*timeout_set)(struct led_classdev_flash *fled_cdev, u32 timeout);
  41. /* get the flash LED fault */
  42. int (*fault_get)(struct led_classdev_flash *fled_cdev, u32 *fault);
  43. };
  44. /*
  45. * Current value of a flash setting along
  46. * with its constraints.
  47. */
  48. struct led_flash_setting {
  49. /* maximum allowed value */
  50. u32 min;
  51. /* maximum allowed value */
  52. u32 max;
  53. /* step value */
  54. u32 step;
  55. /* current value */
  56. u32 val;
  57. };
  58. struct led_classdev_flash {
  59. /* led class device */
  60. struct led_classdev led_cdev;
  61. /* flash led specific ops */
  62. const struct led_flash_ops *ops;
  63. /* flash brightness value in microamperes along with its constraints */
  64. struct led_flash_setting brightness;
  65. /* flash timeout value in microseconds along with its constraints */
  66. struct led_flash_setting timeout;
  67. /* LED Flash class sysfs groups */
  68. const struct attribute_group *sysfs_groups[LED_FLASH_SYSFS_GROUPS_SIZE];
  69. };
  70. static inline struct led_classdev_flash *lcdev_to_flcdev(
  71. struct led_classdev *lcdev)
  72. {
  73. return container_of(lcdev, struct led_classdev_flash, led_cdev);
  74. }
  75. #if IS_ENABLED(CONFIG_LEDS_CLASS_FLASH)
  76. /**
  77. * led_classdev_flash_register_ext - register a new object of LED class with
  78. * init data and with support for flash LEDs
  79. * @parent: LED flash controller device this flash LED is driven by
  80. * @fled_cdev: the led_classdev_flash structure for this device
  81. * @init_data: the LED class flash device initialization data
  82. *
  83. * Returns: 0 on success or negative error value on failure
  84. */
  85. int led_classdev_flash_register_ext(struct device *parent,
  86. struct led_classdev_flash *fled_cdev,
  87. struct led_init_data *init_data);
  88. /**
  89. * led_classdev_flash_unregister - unregisters an object of led_classdev class
  90. * with support for flash LEDs
  91. * @fled_cdev: the flash LED to unregister
  92. *
  93. * Unregister a previously registered via led_classdev_flash_register object
  94. */
  95. void led_classdev_flash_unregister(struct led_classdev_flash *fled_cdev);
  96. int devm_led_classdev_flash_register_ext(struct device *parent,
  97. struct led_classdev_flash *fled_cdev,
  98. struct led_init_data *init_data);
  99. void devm_led_classdev_flash_unregister(struct device *parent,
  100. struct led_classdev_flash *fled_cdev);
  101. #else
  102. static inline int led_classdev_flash_register_ext(struct device *parent,
  103. struct led_classdev_flash *fled_cdev,
  104. struct led_init_data *init_data)
  105. {
  106. return 0;
  107. }
  108. static inline void led_classdev_flash_unregister(struct led_classdev_flash *fled_cdev) {};
  109. static inline int devm_led_classdev_flash_register_ext(struct device *parent,
  110. struct led_classdev_flash *fled_cdev,
  111. struct led_init_data *init_data)
  112. {
  113. return 0;
  114. }
  115. static inline void devm_led_classdev_flash_unregister(struct device *parent,
  116. struct led_classdev_flash *fled_cdev)
  117. {};
  118. #endif /* IS_ENABLED(CONFIG_LEDS_CLASS_FLASH) */
  119. static inline int led_classdev_flash_register(struct device *parent,
  120. struct led_classdev_flash *fled_cdev)
  121. {
  122. return led_classdev_flash_register_ext(parent, fled_cdev, NULL);
  123. }
  124. static inline int devm_led_classdev_flash_register(struct device *parent,
  125. struct led_classdev_flash *fled_cdev)
  126. {
  127. return devm_led_classdev_flash_register_ext(parent, fled_cdev, NULL);
  128. }
  129. /**
  130. * led_set_flash_strobe - setup flash strobe
  131. * @fled_cdev: the flash LED to set strobe on
  132. * @state: 1 - strobe flash, 0 - stop flash strobe
  133. *
  134. * Strobe the flash LED.
  135. *
  136. * Returns: 0 on success or negative error value on failure
  137. */
  138. static inline int led_set_flash_strobe(struct led_classdev_flash *fled_cdev,
  139. bool state)
  140. {
  141. if (!fled_cdev)
  142. return -EINVAL;
  143. return fled_cdev->ops->strobe_set(fled_cdev, state);
  144. }
  145. /**
  146. * led_get_flash_strobe - get flash strobe status
  147. * @fled_cdev: the flash LED to query
  148. * @state: 1 - flash is strobing, 0 - flash is off
  149. *
  150. * Check whether the flash is strobing at the moment.
  151. *
  152. * Returns: 0 on success or negative error value on failure
  153. */
  154. static inline int led_get_flash_strobe(struct led_classdev_flash *fled_cdev,
  155. bool *state)
  156. {
  157. if (!fled_cdev)
  158. return -EINVAL;
  159. if (fled_cdev->ops->strobe_get)
  160. return fled_cdev->ops->strobe_get(fled_cdev, state);
  161. return -EINVAL;
  162. }
  163. /**
  164. * led_set_flash_brightness - set flash LED brightness
  165. * @fled_cdev: the flash LED to set
  166. * @brightness: the brightness to set it to
  167. *
  168. * Set a flash LED's brightness.
  169. *
  170. * Returns: 0 on success or negative error value on failure
  171. */
  172. int led_set_flash_brightness(struct led_classdev_flash *fled_cdev,
  173. u32 brightness);
  174. /**
  175. * led_update_flash_brightness - update flash LED brightness
  176. * @fled_cdev: the flash LED to query
  177. *
  178. * Get a flash LED's current brightness and update led_flash->brightness
  179. * member with the obtained value.
  180. *
  181. * Returns: 0 on success or negative error value on failure
  182. */
  183. int led_update_flash_brightness(struct led_classdev_flash *fled_cdev);
  184. /**
  185. * led_set_flash_timeout - set flash LED timeout
  186. * @fled_cdev: the flash LED to set
  187. * @timeout: the flash timeout to set it to
  188. *
  189. * Set the flash strobe duration.
  190. *
  191. * Returns: 0 on success or negative error value on failure
  192. */
  193. int led_set_flash_timeout(struct led_classdev_flash *fled_cdev, u32 timeout);
  194. /**
  195. * led_get_flash_fault - get the flash LED fault
  196. * @fled_cdev: the flash LED to query
  197. * @fault: bitmask containing flash faults
  198. *
  199. * Get the flash LED fault.
  200. *
  201. * Returns: 0 on success or negative error value on failure
  202. */
  203. int led_get_flash_fault(struct led_classdev_flash *fled_cdev, u32 *fault);
  204. #endif /* __LINUX_FLASH_LEDS_H_INCLUDED */