backlight.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. /* Upper 4 bits are reserved for driver internal use */
  72. unsigned int state;
  73. #define BL_CORE_SUSPENDED (1 << 0) /* backlight is suspended */
  74. #define BL_CORE_FBBLANK (1 << 1) /* backlight is under an fb blank event */
  75. #define BL_CORE_DRIVER4 (1 << 28) /* reserved for driver specific use */
  76. #define BL_CORE_DRIVER3 (1 << 29) /* reserved for driver specific use */
  77. #define BL_CORE_DRIVER2 (1 << 30) /* reserved for driver specific use */
  78. #define BL_CORE_DRIVER1 (1 << 31) /* reserved for driver specific use */
  79. };
  80. struct backlight_device {
  81. /* Backlight properties */
  82. struct backlight_properties props;
  83. /* Serialise access to update_status method */
  84. struct mutex update_lock;
  85. /* This protects the 'ops' field. If 'ops' is NULL, the driver that
  86. registered this device has been unloaded, and if class_get_devdata()
  87. points to something in the body of that driver, it is also invalid. */
  88. struct mutex ops_lock;
  89. const struct backlight_ops *ops;
  90. /* The framebuffer notifier block */
  91. struct notifier_block fb_notif;
  92. /* list entry of all registered backlight devices */
  93. struct list_head entry;
  94. struct device dev;
  95. /* Multiple framebuffers may share one backlight device */
  96. bool fb_bl_on[FB_MAX];
  97. int use_count;
  98. };
  99. static inline int backlight_update_status(struct backlight_device *bd)
  100. {
  101. int ret = -ENOENT;
  102. mutex_lock(&bd->update_lock);
  103. if (bd->ops && bd->ops->update_status)
  104. ret = bd->ops->update_status(bd);
  105. mutex_unlock(&bd->update_lock);
  106. return ret;
  107. }
  108. /**
  109. * backlight_enable - Enable backlight
  110. * @bd: the backlight device to enable
  111. */
  112. static inline int backlight_enable(struct backlight_device *bd)
  113. {
  114. if (!bd)
  115. return 0;
  116. bd->props.power = FB_BLANK_UNBLANK;
  117. bd->props.fb_blank = FB_BLANK_UNBLANK;
  118. bd->props.state &= ~BL_CORE_FBBLANK;
  119. return backlight_update_status(bd);
  120. }
  121. /**
  122. * backlight_disable - Disable backlight
  123. * @bd: the backlight device to disable
  124. */
  125. static inline int backlight_disable(struct backlight_device *bd)
  126. {
  127. if (!bd)
  128. return 0;
  129. bd->props.power = FB_BLANK_POWERDOWN;
  130. bd->props.fb_blank = FB_BLANK_POWERDOWN;
  131. bd->props.state |= BL_CORE_FBBLANK;
  132. return backlight_update_status(bd);
  133. }
  134. /**
  135. * backlight_put - Drop backlight reference
  136. * @bd: the backlight device to put
  137. */
  138. static inline void backlight_put(struct backlight_device *bd)
  139. {
  140. if (bd)
  141. put_device(&bd->dev);
  142. }
  143. extern struct backlight_device *backlight_device_register(const char *name,
  144. struct device *dev, void *devdata, const struct backlight_ops *ops,
  145. const struct backlight_properties *props);
  146. extern struct backlight_device *devm_backlight_device_register(
  147. struct device *dev, const char *name, struct device *parent,
  148. void *devdata, const struct backlight_ops *ops,
  149. const struct backlight_properties *props);
  150. extern void backlight_device_unregister(struct backlight_device *bd);
  151. extern void devm_backlight_device_unregister(struct device *dev,
  152. struct backlight_device *bd);
  153. extern void backlight_force_update(struct backlight_device *bd,
  154. enum backlight_update_reason reason);
  155. extern int backlight_register_notifier(struct notifier_block *nb);
  156. extern int backlight_unregister_notifier(struct notifier_block *nb);
  157. extern struct backlight_device *backlight_device_get_by_type(enum backlight_type type);
  158. extern int backlight_device_set_brightness(struct backlight_device *bd, unsigned long brightness);
  159. #define to_backlight_device(obj) container_of(obj, struct backlight_device, dev)
  160. static inline void * bl_get_data(struct backlight_device *bl_dev)
  161. {
  162. return dev_get_drvdata(&bl_dev->dev);
  163. }
  164. struct generic_bl_info {
  165. const char *name;
  166. int max_intensity;
  167. int default_intensity;
  168. int limit_mask;
  169. void (*set_bl_intensity)(int intensity);
  170. void (*kick_battery)(void);
  171. };
  172. #ifdef CONFIG_OF
  173. struct backlight_device *of_find_backlight_by_node(struct device_node *node);
  174. #else
  175. static inline struct backlight_device *
  176. of_find_backlight_by_node(struct device_node *node)
  177. {
  178. return NULL;
  179. }
  180. #endif
  181. #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
  182. struct backlight_device *of_find_backlight(struct device *dev);
  183. #else
  184. static inline struct backlight_device *of_find_backlight(struct device *dev)
  185. {
  186. return NULL;
  187. }
  188. #endif
  189. #endif