backlight.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. /**
  15. * enum backlight_update_reason - what method was used to update backlight
  16. *
  17. * A driver indicates the method (reason) used for updating the backlight
  18. * when calling backlight_force_update().
  19. */
  20. enum backlight_update_reason {
  21. /**
  22. * @BACKLIGHT_UPDATE_HOTKEY: The backlight was updated using a hot-key.
  23. */
  24. BACKLIGHT_UPDATE_HOTKEY,
  25. /**
  26. * @BACKLIGHT_UPDATE_SYSFS: The backlight was updated using sysfs.
  27. */
  28. BACKLIGHT_UPDATE_SYSFS,
  29. };
  30. /**
  31. * enum backlight_type - the type of backlight control
  32. *
  33. * The type of interface used to control the backlight.
  34. */
  35. enum backlight_type {
  36. /**
  37. * @BACKLIGHT_RAW:
  38. *
  39. * The backlight is controlled using hardware registers.
  40. */
  41. BACKLIGHT_RAW = 1,
  42. /**
  43. * @BACKLIGHT_PLATFORM:
  44. *
  45. * The backlight is controlled using a platform-specific interface.
  46. */
  47. BACKLIGHT_PLATFORM,
  48. /**
  49. * @BACKLIGHT_FIRMWARE:
  50. *
  51. * The backlight is controlled using a standard firmware interface.
  52. */
  53. BACKLIGHT_FIRMWARE,
  54. /**
  55. * @BACKLIGHT_TYPE_MAX: Number of entries.
  56. */
  57. BACKLIGHT_TYPE_MAX,
  58. };
  59. /**
  60. * enum backlight_notification - the type of notification
  61. *
  62. * The notifications that is used for notification sent to the receiver
  63. * that registered notifications using backlight_register_notifier().
  64. */
  65. enum backlight_notification {
  66. /**
  67. * @BACKLIGHT_REGISTERED: The backlight device is registered.
  68. */
  69. BACKLIGHT_REGISTERED,
  70. /**
  71. * @BACKLIGHT_UNREGISTERED: The backlight revice is unregistered.
  72. */
  73. BACKLIGHT_UNREGISTERED,
  74. };
  75. /** enum backlight_scale - the type of scale used for brightness values
  76. *
  77. * The type of scale used for brightness values.
  78. */
  79. enum backlight_scale {
  80. /**
  81. * @BACKLIGHT_SCALE_UNKNOWN: The scale is unknown.
  82. */
  83. BACKLIGHT_SCALE_UNKNOWN = 0,
  84. /**
  85. * @BACKLIGHT_SCALE_LINEAR: The scale is linear.
  86. *
  87. * The linear scale will increase brightness the same for each step.
  88. */
  89. BACKLIGHT_SCALE_LINEAR,
  90. /**
  91. * @BACKLIGHT_SCALE_NON_LINEAR: The scale is not linear.
  92. *
  93. * This is often used when the brightness values tries to adjust to
  94. * the relative perception of the eye demanding a non-linear scale.
  95. */
  96. BACKLIGHT_SCALE_NON_LINEAR,
  97. };
  98. struct backlight_device;
  99. struct fb_info;
  100. /**
  101. * struct backlight_ops - backlight operations
  102. *
  103. * The backlight operations are specified when the backlight device is registered.
  104. */
  105. struct backlight_ops {
  106. /**
  107. * @options: Configure how operations are called from the core.
  108. *
  109. * The options parameter is used to adjust the behaviour of the core.
  110. * Set BL_CORE_SUSPENDRESUME to get the update_status() operation called
  111. * upon suspend and resume.
  112. */
  113. unsigned int options;
  114. #define BL_CORE_SUSPENDRESUME (1 << 0)
  115. /**
  116. * @update_status: Operation called when properties have changed.
  117. *
  118. * Notify the backlight driver some property has changed.
  119. * The update_status operation is protected by the update_lock.
  120. *
  121. * The backlight driver is expected to use backlight_is_blank()
  122. * to check if the display is blanked and set brightness accordingly.
  123. * update_status() is called when any of the properties has changed.
  124. *
  125. * RETURNS:
  126. *
  127. * 0 on success, negative error code if any failure occurred.
  128. */
  129. int (*update_status)(struct backlight_device *);
  130. /**
  131. * @get_brightness: Return the current backlight brightness.
  132. *
  133. * The driver may implement this as a readback from the HW.
  134. * This operation is optional and if not present then the current
  135. * brightness property value is used.
  136. *
  137. * RETURNS:
  138. *
  139. * A brightness value which is 0 or a positive number.
  140. * On failure a negative error code is returned.
  141. */
  142. int (*get_brightness)(struct backlight_device *);
  143. /**
  144. * @check_fb: Check the framebuffer device.
  145. *
  146. * Check if given framebuffer device is the one bound to this backlight.
  147. * This operation is optional and if not implemented it is assumed that the
  148. * fbdev is always the one bound to the backlight.
  149. *
  150. * RETURNS:
  151. *
  152. * If info is NULL or the info matches the fbdev bound to the backlight return true.
  153. * If info does not match the fbdev bound to the backlight return false.
  154. */
  155. int (*check_fb)(struct backlight_device *bd, struct fb_info *info);
  156. };
  157. /**
  158. * struct backlight_properties - backlight properties
  159. *
  160. * This structure defines all the properties of a backlight.
  161. */
  162. struct backlight_properties {
  163. /**
  164. * @brightness: The current brightness requested by the user.
  165. *
  166. * The backlight core makes sure the range is (0 to max_brightness)
  167. * when the brightness is set via the sysfs attribute:
  168. * /sys/class/backlight/<backlight>/brightness.
  169. *
  170. * This value can be set in the backlight_properties passed
  171. * to devm_backlight_device_register() to set a default brightness
  172. * value.
  173. */
  174. int brightness;
  175. /**
  176. * @max_brightness: The maximum brightness value.
  177. *
  178. * This value must be set in the backlight_properties passed to
  179. * devm_backlight_device_register() and shall not be modified by the
  180. * driver after registration.
  181. */
  182. int max_brightness;
  183. /**
  184. * @power: The current power mode.
  185. *
  186. * User space can configure the power mode using the sysfs
  187. * attribute: /sys/class/backlight/<backlight>/bl_power
  188. * When the power property is updated update_status() is called.
  189. *
  190. * The possible values are: (0: full on, 1 to 3: power saving
  191. * modes; 4: full off), see FB_BLANK_XXX.
  192. *
  193. * When the backlight device is enabled @power is set
  194. * to FB_BLANK_UNBLANK. When the backlight device is disabled
  195. * @power is set to FB_BLANK_POWERDOWN.
  196. */
  197. int power;
  198. /**
  199. * @fb_blank: The power state from the FBIOBLANK ioctl.
  200. *
  201. * When the FBIOBLANK ioctl is called @fb_blank is set to the
  202. * blank parameter and the update_status() operation is called.
  203. *
  204. * When the backlight device is enabled @fb_blank is set
  205. * to FB_BLANK_UNBLANK. When the backlight device is disabled
  206. * @fb_blank is set to FB_BLANK_POWERDOWN.
  207. *
  208. * Backlight drivers should avoid using this property. It has been
  209. * replaced by state & BL_CORE_FBLANK (although most drivers should
  210. * use backlight_is_blank() as the preferred means to get the blank
  211. * state).
  212. *
  213. * fb_blank is deprecated and will be removed.
  214. */
  215. int fb_blank;
  216. /**
  217. * @type: The type of backlight supported.
  218. *
  219. * The backlight type allows userspace to make appropriate
  220. * policy decisions based on the backlight type.
  221. *
  222. * This value must be set in the backlight_properties
  223. * passed to devm_backlight_device_register().
  224. */
  225. enum backlight_type type;
  226. /**
  227. * @state: The state of the backlight core.
  228. *
  229. * The state is a bitmask. BL_CORE_FBBLANK is set when the display
  230. * is expected to be blank. BL_CORE_SUSPENDED is set when the
  231. * driver is suspended.
  232. *
  233. * backlight drivers are expected to use backlight_is_blank()
  234. * in their update_status() operation rather than reading the
  235. * state property.
  236. *
  237. * The state is maintained by the core and drivers may not modify it.
  238. */
  239. unsigned int state;
  240. #define BL_CORE_SUSPENDED (1 << 0) /* backlight is suspended */
  241. #define BL_CORE_FBBLANK (1 << 1) /* backlight is under an fb blank event */
  242. /**
  243. * @scale: The type of the brightness scale.
  244. */
  245. enum backlight_scale scale;
  246. };
  247. /**
  248. * struct backlight_device - backlight device data
  249. *
  250. * This structure holds all data required by a backlight device.
  251. */
  252. struct backlight_device {
  253. /**
  254. * @props: Backlight properties
  255. */
  256. struct backlight_properties props;
  257. /**
  258. * @update_lock: The lock used when calling the update_status() operation.
  259. *
  260. * update_lock is an internal backlight lock that serialise access
  261. * to the update_status() operation. The backlight core holds the update_lock
  262. * when calling the update_status() operation. The update_lock shall not
  263. * be used by backlight drivers.
  264. */
  265. struct mutex update_lock;
  266. /**
  267. * @ops_lock: The lock used around everything related to backlight_ops.
  268. *
  269. * ops_lock is an internal backlight lock that protects the ops pointer
  270. * and is used around all accesses to ops and when the operations are
  271. * invoked. The ops_lock shall not be used by backlight drivers.
  272. */
  273. struct mutex ops_lock;
  274. /**
  275. * @ops: Pointer to the backlight operations.
  276. *
  277. * If ops is NULL, the driver that registered this device has been unloaded,
  278. * and if class_get_devdata() points to something in the body of that driver,
  279. * it is also invalid.
  280. */
  281. const struct backlight_ops *ops;
  282. /**
  283. * @fb_notif: The framebuffer notifier block
  284. */
  285. struct notifier_block fb_notif;
  286. /**
  287. * @entry: List entry of all registered backlight devices
  288. */
  289. struct list_head entry;
  290. /**
  291. * @dev: Parent device.
  292. */
  293. struct device dev;
  294. /**
  295. * @fb_bl_on: The state of individual fbdev's.
  296. *
  297. * Multiple fbdev's may share one backlight device. The fb_bl_on
  298. * records the state of the individual fbdev.
  299. */
  300. bool fb_bl_on[FB_MAX];
  301. /**
  302. * @use_count: The number of uses of fb_bl_on.
  303. */
  304. int use_count;
  305. };
  306. /**
  307. * backlight_update_status - force an update of the backlight device status
  308. * @bd: the backlight device
  309. */
  310. static inline int backlight_update_status(struct backlight_device *bd)
  311. {
  312. int ret = -ENOENT;
  313. mutex_lock(&bd->update_lock);
  314. if (bd->ops && bd->ops->update_status)
  315. ret = bd->ops->update_status(bd);
  316. mutex_unlock(&bd->update_lock);
  317. return ret;
  318. }
  319. /**
  320. * backlight_enable - Enable backlight
  321. * @bd: the backlight device to enable
  322. */
  323. static inline int backlight_enable(struct backlight_device *bd)
  324. {
  325. if (!bd)
  326. return 0;
  327. bd->props.power = FB_BLANK_UNBLANK;
  328. bd->props.fb_blank = FB_BLANK_UNBLANK;
  329. bd->props.state &= ~BL_CORE_FBBLANK;
  330. return backlight_update_status(bd);
  331. }
  332. /**
  333. * backlight_disable - Disable backlight
  334. * @bd: the backlight device to disable
  335. */
  336. static inline int backlight_disable(struct backlight_device *bd)
  337. {
  338. if (!bd)
  339. return 0;
  340. bd->props.power = FB_BLANK_POWERDOWN;
  341. bd->props.fb_blank = FB_BLANK_POWERDOWN;
  342. bd->props.state |= BL_CORE_FBBLANK;
  343. return backlight_update_status(bd);
  344. }
  345. /**
  346. * backlight_is_blank - Return true if display is expected to be blank
  347. * @bd: the backlight device
  348. *
  349. * Display is expected to be blank if any of these is true::
  350. *
  351. * 1) if power in not UNBLANK
  352. * 2) if fb_blank is not UNBLANK
  353. * 3) if state indicate BLANK or SUSPENDED
  354. *
  355. * Returns true if display is expected to be blank, false otherwise.
  356. */
  357. static inline bool backlight_is_blank(const struct backlight_device *bd)
  358. {
  359. return bd->props.power != FB_BLANK_UNBLANK ||
  360. bd->props.fb_blank != FB_BLANK_UNBLANK ||
  361. bd->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK);
  362. }
  363. /**
  364. * backlight_get_brightness - Returns the current brightness value
  365. * @bd: the backlight device
  366. *
  367. * Returns the current brightness value, taking in consideration the current
  368. * state. If backlight_is_blank() returns true then return 0 as brightness
  369. * otherwise return the current brightness property value.
  370. *
  371. * Backlight drivers are expected to use this function in their update_status()
  372. * operation to get the brightness value.
  373. */
  374. static inline int backlight_get_brightness(const struct backlight_device *bd)
  375. {
  376. if (backlight_is_blank(bd))
  377. return 0;
  378. else
  379. return bd->props.brightness;
  380. }
  381. struct backlight_device *
  382. backlight_device_register(const char *name, struct device *dev, void *devdata,
  383. const struct backlight_ops *ops,
  384. const struct backlight_properties *props);
  385. struct backlight_device *
  386. devm_backlight_device_register(struct device *dev, const char *name,
  387. struct device *parent, void *devdata,
  388. const struct backlight_ops *ops,
  389. const struct backlight_properties *props);
  390. void backlight_device_unregister(struct backlight_device *bd);
  391. void devm_backlight_device_unregister(struct device *dev,
  392. struct backlight_device *bd);
  393. void backlight_force_update(struct backlight_device *bd,
  394. enum backlight_update_reason reason);
  395. int backlight_register_notifier(struct notifier_block *nb);
  396. int backlight_unregister_notifier(struct notifier_block *nb);
  397. struct backlight_device *backlight_device_get_by_name(const char *name);
  398. struct backlight_device *backlight_device_get_by_type(enum backlight_type type);
  399. int backlight_device_set_brightness(struct backlight_device *bd,
  400. unsigned long brightness);
  401. #define to_backlight_device(obj) container_of(obj, struct backlight_device, dev)
  402. /**
  403. * bl_get_data - access devdata
  404. * @bl_dev: pointer to backlight device
  405. *
  406. * When a backlight device is registered the driver has the possibility
  407. * to supply a void * devdata. bl_get_data() return a pointer to the
  408. * devdata.
  409. *
  410. * RETURNS:
  411. *
  412. * pointer to devdata stored while registering the backlight device.
  413. */
  414. static inline void * bl_get_data(struct backlight_device *bl_dev)
  415. {
  416. return dev_get_drvdata(&bl_dev->dev);
  417. }
  418. #ifdef CONFIG_OF
  419. struct backlight_device *of_find_backlight_by_node(struct device_node *node);
  420. #else
  421. static inline struct backlight_device *
  422. of_find_backlight_by_node(struct device_node *node)
  423. {
  424. return NULL;
  425. }
  426. #endif
  427. #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
  428. struct backlight_device *devm_of_find_backlight(struct device *dev);
  429. #else
  430. static inline struct backlight_device *
  431. devm_of_find_backlight(struct device *dev)
  432. {
  433. return NULL;
  434. }
  435. #endif
  436. #endif