backlight.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Backlight Lowlevel Control Abstraction
  4. *
  5. * Copyright (C) 2003,2004 Hewlett-Packard Company
  6. *
  7. */
  8. #ifndef _LINUX_BACKLIGHT_H
  9. #define _LINUX_BACKLIGHT_H
  10. #include <linux/device.h>
  11. #include <linux/fb.h>
  12. #include <linux/mutex.h>
  13. #include <linux/notifier.h>
  14. /* Notes on locking:
  15. *
  16. * backlight_device->ops_lock is an internal backlight lock protecting the
  17. * ops pointer and no code outside the core should need to touch it.
  18. *
  19. * Access to update_status() is serialised by the update_lock mutex since
  20. * most drivers seem to need this and historically get it wrong.
  21. *
  22. * Most drivers don't need locking on their get_brightness() method.
  23. * If yours does, you need to implement it in the driver. You can use the
  24. * update_lock mutex if appropriate.
  25. *
  26. * Any other use of the locks below is probably wrong.
  27. */
  28. enum backlight_update_reason {
  29. BACKLIGHT_UPDATE_HOTKEY,
  30. BACKLIGHT_UPDATE_SYSFS,
  31. };
  32. enum backlight_type {
  33. BACKLIGHT_RAW = 1,
  34. BACKLIGHT_PLATFORM,
  35. BACKLIGHT_FIRMWARE,
  36. BACKLIGHT_TYPE_MAX,
  37. };
  38. enum backlight_notification {
  39. BACKLIGHT_REGISTERED,
  40. BACKLIGHT_UNREGISTERED,
  41. };
  42. struct backlight_device;
  43. struct fb_info;
  44. struct backlight_ops {
  45. unsigned int options;
  46. #define BL_CORE_SUSPENDRESUME (1 << 0)
  47. /* Notify the backlight driver some property has changed */
  48. int (*update_status)(struct backlight_device *);
  49. /* Return the current backlight brightness (accounting for power,
  50. fb_blank etc.) */
  51. int (*get_brightness)(struct backlight_device *);
  52. /* Check if given framebuffer device is the one bound to this backlight;
  53. return 0 if not, !=0 if it is. If NULL, backlight always matches the fb. */
  54. int (*check_fb)(struct backlight_device *, struct fb_info *);
  55. };
  56. /* This structure defines all the properties of a backlight */
  57. struct backlight_properties {
  58. /* Current User requested brightness (0 - max_brightness) */
  59. int brightness;
  60. /* Maximal value for brightness (read-only) */
  61. int max_brightness;
  62. /* Current FB Power mode (0: full on, 1..3: power saving
  63. modes; 4: full off), see FB_BLANK_XXX */
  64. int power;
  65. /* FB Blanking active? (values as for power) */
  66. /* Due to be removed, please use (state & BL_CORE_FBBLANK) */
  67. int fb_blank;
  68. /* Backlight type */
  69. enum backlight_type type;
  70. /* Flags used to signal drivers of state changes */
  71. unsigned int state;
  72. #define BL_CORE_SUSPENDED (1 << 0) /* backlight is suspended */
  73. #define BL_CORE_FBBLANK (1 << 1) /* backlight is under an fb blank event */
  74. };
  75. struct backlight_device {
  76. /* Backlight properties */
  77. struct backlight_properties props;
  78. /* Serialise access to update_status method */
  79. struct mutex update_lock;
  80. /* This protects the 'ops' field. If 'ops' is NULL, the driver that
  81. registered this device has been unloaded, and if class_get_devdata()
  82. points to something in the body of that driver, it is also invalid. */
  83. struct mutex ops_lock;
  84. const struct backlight_ops *ops;
  85. /* The framebuffer notifier block */
  86. struct notifier_block fb_notif;
  87. /* list entry of all registered backlight devices */
  88. struct list_head entry;
  89. struct device dev;
  90. /* Multiple framebuffers may share one backlight device */
  91. bool fb_bl_on[FB_MAX];
  92. int use_count;
  93. };
  94. static inline int backlight_update_status(struct backlight_device *bd)
  95. {
  96. int ret = -ENOENT;
  97. mutex_lock(&bd->update_lock);
  98. if (bd->ops && bd->ops->update_status)
  99. ret = bd->ops->update_status(bd);
  100. mutex_unlock(&bd->update_lock);
  101. return ret;
  102. }
  103. /**
  104. * backlight_enable - Enable backlight
  105. * @bd: the backlight device to enable
  106. */
  107. static inline int backlight_enable(struct backlight_device *bd)
  108. {
  109. if (!bd)
  110. return 0;
  111. bd->props.power = FB_BLANK_UNBLANK;
  112. bd->props.fb_blank = FB_BLANK_UNBLANK;
  113. bd->props.state &= ~BL_CORE_FBBLANK;
  114. return backlight_update_status(bd);
  115. }
  116. /**
  117. * backlight_disable - Disable backlight
  118. * @bd: the backlight device to disable
  119. */
  120. static inline int backlight_disable(struct backlight_device *bd)
  121. {
  122. if (!bd)
  123. return 0;
  124. bd->props.power = FB_BLANK_POWERDOWN;
  125. bd->props.fb_blank = FB_BLANK_POWERDOWN;
  126. bd->props.state |= BL_CORE_FBBLANK;
  127. return backlight_update_status(bd);
  128. }
  129. /**
  130. * backlight_put - Drop backlight reference
  131. * @bd: the backlight device to put
  132. */
  133. static inline void backlight_put(struct backlight_device *bd)
  134. {
  135. if (bd)
  136. put_device(&bd->dev);
  137. }
  138. extern struct backlight_device *backlight_device_register(const char *name,
  139. struct device *dev, void *devdata, const struct backlight_ops *ops,
  140. const struct backlight_properties *props);
  141. extern struct backlight_device *devm_backlight_device_register(
  142. struct device *dev, const char *name, struct device *parent,
  143. void *devdata, const struct backlight_ops *ops,
  144. const struct backlight_properties *props);
  145. extern void backlight_device_unregister(struct backlight_device *bd);
  146. extern void devm_backlight_device_unregister(struct device *dev,
  147. struct backlight_device *bd);
  148. extern void backlight_force_update(struct backlight_device *bd,
  149. enum backlight_update_reason reason);
  150. extern int backlight_register_notifier(struct notifier_block *nb);
  151. extern int backlight_unregister_notifier(struct notifier_block *nb);
  152. extern struct backlight_device *backlight_device_get_by_type(enum backlight_type type);
  153. extern int backlight_device_set_brightness(struct backlight_device *bd, unsigned long brightness);
  154. #define to_backlight_device(obj) container_of(obj, struct backlight_device, dev)
  155. static inline void * bl_get_data(struct backlight_device *bl_dev)
  156. {
  157. return dev_get_drvdata(&bl_dev->dev);
  158. }
  159. struct generic_bl_info {
  160. const char *name;
  161. int max_intensity;
  162. int default_intensity;
  163. int limit_mask;
  164. void (*set_bl_intensity)(int intensity);
  165. void (*kick_battery)(void);
  166. };
  167. #ifdef CONFIG_OF
  168. struct backlight_device *of_find_backlight_by_node(struct device_node *node);
  169. #else
  170. static inline struct backlight_device *
  171. of_find_backlight_by_node(struct device_node *node)
  172. {
  173. return NULL;
  174. }
  175. #endif
  176. #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
  177. struct backlight_device *of_find_backlight(struct device *dev);
  178. struct backlight_device *devm_of_find_backlight(struct device *dev);
  179. #else
  180. static inline struct backlight_device *of_find_backlight(struct device *dev)
  181. {
  182. return NULL;
  183. }
  184. static inline struct backlight_device *
  185. devm_of_find_backlight(struct device *dev)
  186. {
  187. return NULL;
  188. }
  189. #endif
  190. #endif