1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Backlight Lowlevel Control Abstraction
5 * Copyright (C) 2003,2004 Hewlett-Packard Company
9 #ifndef _LINUX_BACKLIGHT_H
10 #define _LINUX_BACKLIGHT_H
12 #include <linux/device.h>
14 #include <linux/mutex.h>
15 #include <linux/notifier.h>
17 enum backlight_update_reason {
18 BACKLIGHT_UPDATE_HOTKEY,
19 BACKLIGHT_UPDATE_SYSFS,
29 enum backlight_notification {
31 BACKLIGHT_UNREGISTERED,
34 enum backlight_scale {
35 BACKLIGHT_SCALE_UNKNOWN = 0,
36 BACKLIGHT_SCALE_LINEAR,
37 BACKLIGHT_SCALE_NON_LINEAR,
40 struct backlight_device;
44 * struct backlight_ops - backlight operations
46 * The backlight operations are specified when the backlight device is registered.
48 struct backlight_ops {
50 * @options: Configure how operations are called from the core.
52 * The options parameter is used to adjust the behaviour of the core.
53 * Set BL_CORE_SUSPENDRESUME to get the update_status() operation called
54 * upon suspend and resume.
58 #define BL_CORE_SUSPENDRESUME (1 << 0)
61 * @update_status: Operation called when properties have changed.
63 * Notify the backlight driver some property has changed.
64 * The update_status operation is protected by the update_lock.
66 * The backlight driver is expected to use backlight_is_blank()
67 * to check if the display is blanked and set brightness accordingly.
68 * update_status() is called when any of the properties has changed.
72 * 0 on success, negative error code if any failure occurred.
74 int (*update_status)(struct backlight_device *);
77 * @get_brightness: Return the current backlight brightness.
79 * The driver may implement this as a readback from the HW.
80 * This operation is optional and if not present then the current
81 * brightness property value is used.
85 * A brightness value which is 0 or a positive number.
86 * On failure a negative error code is returned.
88 int (*get_brightness)(struct backlight_device *);
91 * @check_fb: Check the framebuffer device.
93 * Check if given framebuffer device is the one bound to this backlight.
94 * This operation is optional and if not implemented it is assumed that the
95 * fbdev is always the one bound to the backlight.
99 * If info is NULL or the info matches the fbdev bound to the backlight return true.
100 * If info does not match the fbdev bound to the backlight return false.
102 int (*check_fb)(struct backlight_device *bd, struct fb_info *info);
106 * struct backlight_properties - backlight properties
108 * This structure defines all the properties of a backlight.
110 struct backlight_properties {
112 * @brightness: The current brightness requested by the user.
114 * The backlight core makes sure the range is (0 to max_brightness)
115 * when the brightness is set via the sysfs attribute:
116 * /sys/class/backlight/<backlight>/brightness.
118 * This value can be set in the backlight_properties passed
119 * to devm_backlight_device_register() to set a default brightness
125 * @max_brightness: The maximum brightness value.
127 * This value must be set in the backlight_properties passed to
128 * devm_backlight_device_register() and shall not be modified by the
129 * driver after registration.
134 * @power: The current power mode.
136 * User space can configure the power mode using the sysfs
137 * attribute: /sys/class/backlight/<backlight>/bl_power
138 * When the power property is updated update_status() is called.
140 * The possible values are: (0: full on, 1 to 3: power saving
141 * modes; 4: full off), see FB_BLANK_XXX.
143 * When the backlight device is enabled @power is set
144 * to FB_BLANK_UNBLANK. When the backlight device is disabled
145 * @power is set to FB_BLANK_POWERDOWN.
150 * @fb_blank: The power state from the FBIOBLANK ioctl.
152 * When the FBIOBLANK ioctl is called @fb_blank is set to the
153 * blank parameter and the update_status() operation is called.
155 * When the backlight device is enabled @fb_blank is set
156 * to FB_BLANK_UNBLANK. When the backlight device is disabled
157 * @fb_blank is set to FB_BLANK_POWERDOWN.
159 * Backlight drivers should avoid using this property. It has been
160 * replaced by state & BL_CORE_FBLANK (although most drivers should
161 * use backlight_is_blank() as the preferred means to get the blank
164 * fb_blank is deprecated and will be removed.
169 * @type: The type of backlight supported.
171 * The backlight type allows userspace to make appropriate
172 * policy decisions based on the backlight type.
174 * This value must be set in the backlight_properties
175 * passed to devm_backlight_device_register().
177 enum backlight_type type;
180 * @state: The state of the backlight core.
182 * The state is a bitmask. BL_CORE_FBBLANK is set when the display
183 * is expected to be blank. BL_CORE_SUSPENDED is set when the
184 * driver is suspended.
186 * backlight drivers are expected to use backlight_is_blank()
187 * in their update_status() operation rather than reading the
190 * The state is maintained by the core and drivers may not modify it.
194 #define BL_CORE_SUSPENDED (1 << 0) /* backlight is suspended */
195 #define BL_CORE_FBBLANK (1 << 1) /* backlight is under an fb blank event */
198 * @scale: The type of the brightness scale.
200 enum backlight_scale scale;
204 * struct backlight_device - backlight device data
206 * This structure holds all data required by a backlight device.
208 struct backlight_device {
210 * @props: Backlight properties
212 struct backlight_properties props;
215 * @update_lock: The lock used when calling the update_status() operation.
217 * update_lock is an internal backlight lock that serialise access
218 * to the update_status() operation. The backlight core holds the update_lock
219 * when calling the update_status() operation. The update_lock shall not
220 * be used by backlight drivers.
222 struct mutex update_lock;
225 * @ops_lock: The lock used around everything related to backlight_ops.
227 * ops_lock is an internal backlight lock that protects the ops pointer
228 * and is used around all accesses to ops and when the operations are
229 * invoked. The ops_lock shall not be used by backlight drivers.
231 struct mutex ops_lock;
234 * @ops: Pointer to the backlight operations.
236 * If ops is NULL, the driver that registered this device has been unloaded,
237 * and if class_get_devdata() points to something in the body of that driver,
238 * it is also invalid.
240 const struct backlight_ops *ops;
243 * @fb_notif: The framebuffer notifier block
245 struct notifier_block fb_notif;
248 * @entry: List entry of all registered backlight devices
250 struct list_head entry;
253 * @dev: Parent device.
258 * @fb_bl_on: The state of individual fbdev's.
260 * Multiple fbdev's may share one backlight device. The fb_bl_on
261 * records the state of the individual fbdev.
263 bool fb_bl_on[FB_MAX];
266 * @use_count: The number of uses of fb_bl_on.
272 * backlight_update_status - force an update of the backlight device status
273 * @bd: the backlight device
275 static inline int backlight_update_status(struct backlight_device *bd)
279 mutex_lock(&bd->update_lock);
280 if (bd->ops && bd->ops->update_status)
281 ret = bd->ops->update_status(bd);
282 mutex_unlock(&bd->update_lock);
288 * backlight_enable - Enable backlight
289 * @bd: the backlight device to enable
291 static inline int backlight_enable(struct backlight_device *bd)
296 bd->props.power = FB_BLANK_UNBLANK;
297 bd->props.fb_blank = FB_BLANK_UNBLANK;
298 bd->props.state &= ~BL_CORE_FBBLANK;
300 return backlight_update_status(bd);
304 * backlight_disable - Disable backlight
305 * @bd: the backlight device to disable
307 static inline int backlight_disable(struct backlight_device *bd)
312 bd->props.power = FB_BLANK_POWERDOWN;
313 bd->props.fb_blank = FB_BLANK_POWERDOWN;
314 bd->props.state |= BL_CORE_FBBLANK;
316 return backlight_update_status(bd);
320 * backlight_put - Drop backlight reference
321 * @bd: the backlight device to put
323 static inline void backlight_put(struct backlight_device *bd)
326 put_device(&bd->dev);
330 * backlight_is_blank - Return true if display is expected to be blank
331 * @bd: the backlight device
333 * Display is expected to be blank if any of these is true::
335 * 1) if power in not UNBLANK
336 * 2) if fb_blank is not UNBLANK
337 * 3) if state indicate BLANK or SUSPENDED
339 * Returns true if display is expected to be blank, false otherwise.
341 static inline bool backlight_is_blank(const struct backlight_device *bd)
343 return bd->props.power != FB_BLANK_UNBLANK ||
344 bd->props.fb_blank != FB_BLANK_UNBLANK ||
345 bd->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK);
348 extern struct backlight_device *backlight_device_register(const char *name,
349 struct device *dev, void *devdata, const struct backlight_ops *ops,
350 const struct backlight_properties *props);
351 extern struct backlight_device *devm_backlight_device_register(
352 struct device *dev, const char *name, struct device *parent,
353 void *devdata, const struct backlight_ops *ops,
354 const struct backlight_properties *props);
355 extern void backlight_device_unregister(struct backlight_device *bd);
356 extern void devm_backlight_device_unregister(struct device *dev,
357 struct backlight_device *bd);
358 extern void backlight_force_update(struct backlight_device *bd,
359 enum backlight_update_reason reason);
360 extern int backlight_register_notifier(struct notifier_block *nb);
361 extern int backlight_unregister_notifier(struct notifier_block *nb);
362 extern struct backlight_device *backlight_device_get_by_type(enum backlight_type type);
363 struct backlight_device *backlight_device_get_by_name(const char *name);
364 extern int backlight_device_set_brightness(struct backlight_device *bd, unsigned long brightness);
366 #define to_backlight_device(obj) container_of(obj, struct backlight_device, dev)
369 * bl_get_data - access devdata
370 * @bl_dev: pointer to backlight device
372 * When a backlight device is registered the driver has the possibility
373 * to supply a void * devdata. bl_get_data() return a pointer to the
378 * pointer to devdata stored while registering the backlight device.
380 static inline void * bl_get_data(struct backlight_device *bl_dev)
382 return dev_get_drvdata(&bl_dev->dev);
385 struct generic_bl_info {
388 int default_intensity;
390 void (*set_bl_intensity)(int intensity);
391 void (*kick_battery)(void);
395 struct backlight_device *of_find_backlight_by_node(struct device_node *node);
397 static inline struct backlight_device *
398 of_find_backlight_by_node(struct device_node *node)
404 #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
405 struct backlight_device *of_find_backlight(struct device *dev);
406 struct backlight_device *devm_of_find_backlight(struct device *dev);
408 static inline struct backlight_device *of_find_backlight(struct device *dev)
413 static inline struct backlight_device *
414 devm_of_find_backlight(struct device *dev)