printf: Remove unused 'bprintf'
[drm/drm-misc.git] / include / linux / backlight.h
blobf5652e5a9060f69c43a708fec8bbffbd3a29da94
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Backlight Lowlevel Control Abstraction
5 * Copyright (C) 2003,2004 Hewlett-Packard Company
7 */
9 #ifndef _LINUX_BACKLIGHT_H
10 #define _LINUX_BACKLIGHT_H
12 #include <linux/device.h>
13 #include <linux/fb.h>
14 #include <linux/mutex.h>
15 #include <linux/notifier.h>
16 #include <linux/types.h>
18 /**
19 * enum backlight_update_reason - what method was used to update backlight
21 * A driver indicates the method (reason) used for updating the backlight
22 * when calling backlight_force_update().
24 enum backlight_update_reason {
25 /**
26 * @BACKLIGHT_UPDATE_HOTKEY: The backlight was updated using a hot-key.
28 BACKLIGHT_UPDATE_HOTKEY,
30 /**
31 * @BACKLIGHT_UPDATE_SYSFS: The backlight was updated using sysfs.
33 BACKLIGHT_UPDATE_SYSFS,
36 /**
37 * enum backlight_type - the type of backlight control
39 * The type of interface used to control the backlight.
41 enum backlight_type {
42 /**
43 * @BACKLIGHT_RAW:
45 * The backlight is controlled using hardware registers.
47 BACKLIGHT_RAW = 1,
49 /**
50 * @BACKLIGHT_PLATFORM:
52 * The backlight is controlled using a platform-specific interface.
54 BACKLIGHT_PLATFORM,
56 /**
57 * @BACKLIGHT_FIRMWARE:
59 * The backlight is controlled using a standard firmware interface.
61 BACKLIGHT_FIRMWARE,
63 /**
64 * @BACKLIGHT_TYPE_MAX: Number of entries.
66 BACKLIGHT_TYPE_MAX,
69 /** enum backlight_scale - the type of scale used for brightness values
71 * The type of scale used for brightness values.
73 enum backlight_scale {
74 /**
75 * @BACKLIGHT_SCALE_UNKNOWN: The scale is unknown.
77 BACKLIGHT_SCALE_UNKNOWN = 0,
79 /**
80 * @BACKLIGHT_SCALE_LINEAR: The scale is linear.
82 * The linear scale will increase brightness the same for each step.
84 BACKLIGHT_SCALE_LINEAR,
86 /**
87 * @BACKLIGHT_SCALE_NON_LINEAR: The scale is not linear.
89 * This is often used when the brightness values tries to adjust to
90 * the relative perception of the eye demanding a non-linear scale.
92 BACKLIGHT_SCALE_NON_LINEAR,
95 struct backlight_device;
97 /**
98 * struct backlight_ops - backlight operations
100 * The backlight operations are specified when the backlight device is registered.
102 struct backlight_ops {
104 * @options: Configure how operations are called from the core.
106 * The options parameter is used to adjust the behaviour of the core.
107 * Set BL_CORE_SUSPENDRESUME to get the update_status() operation called
108 * upon suspend and resume.
110 unsigned int options;
112 #define BL_CORE_SUSPENDRESUME (1 << 0)
115 * @update_status: Operation called when properties have changed.
117 * Notify the backlight driver some property has changed.
118 * The update_status operation is protected by the update_lock.
120 * The backlight driver is expected to use backlight_is_blank()
121 * to check if the display is blanked and set brightness accordingly.
122 * update_status() is called when any of the properties has changed.
124 * RETURNS:
126 * 0 on success, negative error code if any failure occurred.
128 int (*update_status)(struct backlight_device *);
131 * @get_brightness: Return the current backlight brightness.
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.
137 * RETURNS:
139 * A brightness value which is 0 or a positive number.
140 * On failure a negative error code is returned.
142 int (*get_brightness)(struct backlight_device *);
145 * @controls_device: Check against the display device
147 * Check if the backlight controls the given display device. This
148 * operation is optional and if not implemented it is assumed that
149 * the display is always the one controlled by the backlight.
151 * RETURNS:
153 * If display_dev is NULL or display_dev matches the device controlled by
154 * the backlight, return true. Otherwise return false.
156 bool (*controls_device)(struct backlight_device *bd, struct device *display_dev);
160 * struct backlight_properties - backlight properties
162 * This structure defines all the properties of a backlight.
164 struct backlight_properties {
166 * @brightness: The current brightness requested by the user.
168 * The backlight core makes sure the range is (0 to max_brightness)
169 * when the brightness is set via the sysfs attribute:
170 * /sys/class/backlight/<backlight>/brightness.
172 * This value can be set in the backlight_properties passed
173 * to devm_backlight_device_register() to set a default brightness
174 * value.
176 int brightness;
179 * @max_brightness: The maximum brightness value.
181 * This value must be set in the backlight_properties passed to
182 * devm_backlight_device_register() and shall not be modified by the
183 * driver after registration.
185 int max_brightness;
188 * @power: The current power mode.
190 * User space can configure the power mode using the sysfs
191 * attribute: /sys/class/backlight/<backlight>/bl_power
192 * When the power property is updated update_status() is called.
194 * The possible values are: (0: full on, 4: full off), see
195 * BACKLIGHT_POWER constants.
197 * When the backlight device is enabled, @power is set to
198 * BACKLIGHT_POWER_ON. When the backlight device is disabled,
199 * @power is set to BACKLIGHT_POWER_OFF.
201 int power;
203 #define BACKLIGHT_POWER_ON (0)
204 #define BACKLIGHT_POWER_OFF (4)
205 #define BACKLIGHT_POWER_REDUCED (1) // deprecated; don't use in new code
208 * @type: The type of backlight supported.
210 * The backlight type allows userspace to make appropriate
211 * policy decisions based on the backlight type.
213 * This value must be set in the backlight_properties
214 * passed to devm_backlight_device_register().
216 enum backlight_type type;
219 * @state: The state of the backlight core.
221 * The state is a bitmask. BL_CORE_FBBLANK is set when the display
222 * is expected to be blank. BL_CORE_SUSPENDED is set when the
223 * driver is suspended.
225 * backlight drivers are expected to use backlight_is_blank()
226 * in their update_status() operation rather than reading the
227 * state property.
229 * The state is maintained by the core and drivers may not modify it.
231 unsigned int state;
233 #define BL_CORE_SUSPENDED (1 << 0) /* backlight is suspended */
234 #define BL_CORE_FBBLANK (1 << 1) /* backlight is under an fb blank event */
237 * @scale: The type of the brightness scale.
239 enum backlight_scale scale;
243 * struct backlight_device - backlight device data
245 * This structure holds all data required by a backlight device.
247 struct backlight_device {
249 * @props: Backlight properties
251 struct backlight_properties props;
254 * @update_lock: The lock used when calling the update_status() operation.
256 * update_lock is an internal backlight lock that serialise access
257 * to the update_status() operation. The backlight core holds the update_lock
258 * when calling the update_status() operation. The update_lock shall not
259 * be used by backlight drivers.
261 struct mutex update_lock;
264 * @ops_lock: The lock used around everything related to backlight_ops.
266 * ops_lock is an internal backlight lock that protects the ops pointer
267 * and is used around all accesses to ops and when the operations are
268 * invoked. The ops_lock shall not be used by backlight drivers.
270 struct mutex ops_lock;
273 * @ops: Pointer to the backlight operations.
275 * If ops is NULL, the driver that registered this device has been unloaded,
276 * and if class_get_devdata() points to something in the body of that driver,
277 * it is also invalid.
279 const struct backlight_ops *ops;
282 * @fb_notif: The framebuffer notifier block
284 struct notifier_block fb_notif;
287 * @entry: List entry of all registered backlight devices
289 struct list_head entry;
292 * @dev: Parent device.
294 struct device dev;
297 * @fb_bl_on: The state of individual fbdev's.
299 * Multiple fbdev's may share one backlight device. The fb_bl_on
300 * records the state of the individual fbdev.
302 bool fb_bl_on[FB_MAX];
305 * @use_count: The number of uses of fb_bl_on.
307 int use_count;
311 * backlight_update_status - force an update of the backlight device status
312 * @bd: the backlight device
314 static inline int backlight_update_status(struct backlight_device *bd)
316 int ret = -ENOENT;
318 mutex_lock(&bd->update_lock);
319 if (bd->ops && bd->ops->update_status)
320 ret = bd->ops->update_status(bd);
321 mutex_unlock(&bd->update_lock);
323 return ret;
327 * backlight_enable - Enable backlight
328 * @bd: the backlight device to enable
330 static inline int backlight_enable(struct backlight_device *bd)
332 if (!bd)
333 return 0;
335 bd->props.power = BACKLIGHT_POWER_ON;
336 bd->props.state &= ~BL_CORE_FBBLANK;
338 return backlight_update_status(bd);
342 * backlight_disable - Disable backlight
343 * @bd: the backlight device to disable
345 static inline int backlight_disable(struct backlight_device *bd)
347 if (!bd)
348 return 0;
350 bd->props.power = BACKLIGHT_POWER_OFF;
351 bd->props.state |= BL_CORE_FBBLANK;
353 return backlight_update_status(bd);
357 * backlight_is_blank - Return true if display is expected to be blank
358 * @bd: the backlight device
360 * Display is expected to be blank if any of these is true::
362 * 1) if power in not UNBLANK
363 * 2) if state indicate BLANK or SUSPENDED
365 * Returns true if display is expected to be blank, false otherwise.
367 static inline bool backlight_is_blank(const struct backlight_device *bd)
369 return bd->props.power != BACKLIGHT_POWER_ON ||
370 bd->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK);
374 * backlight_get_brightness - Returns the current brightness value
375 * @bd: the backlight device
377 * Returns the current brightness value, taking in consideration the current
378 * state. If backlight_is_blank() returns true then return 0 as brightness
379 * otherwise return the current brightness property value.
381 * Backlight drivers are expected to use this function in their update_status()
382 * operation to get the brightness value.
384 static inline int backlight_get_brightness(const struct backlight_device *bd)
386 if (backlight_is_blank(bd))
387 return 0;
388 else
389 return bd->props.brightness;
392 struct backlight_device *
393 backlight_device_register(const char *name, struct device *dev, void *devdata,
394 const struct backlight_ops *ops,
395 const struct backlight_properties *props);
396 struct backlight_device *
397 devm_backlight_device_register(struct device *dev, const char *name,
398 struct device *parent, void *devdata,
399 const struct backlight_ops *ops,
400 const struct backlight_properties *props);
401 void backlight_device_unregister(struct backlight_device *bd);
402 void devm_backlight_device_unregister(struct device *dev,
403 struct backlight_device *bd);
404 void backlight_force_update(struct backlight_device *bd,
405 enum backlight_update_reason reason);
406 struct backlight_device *backlight_device_get_by_name(const char *name);
407 struct backlight_device *backlight_device_get_by_type(enum backlight_type type);
408 int backlight_device_set_brightness(struct backlight_device *bd,
409 unsigned long brightness);
411 #define to_backlight_device(obj) container_of(obj, struct backlight_device, dev)
414 * bl_get_data - access devdata
415 * @bl_dev: pointer to backlight device
417 * When a backlight device is registered the driver has the possibility
418 * to supply a void * devdata. bl_get_data() return a pointer to the
419 * devdata.
421 * RETURNS:
423 * pointer to devdata stored while registering the backlight device.
425 static inline void * bl_get_data(struct backlight_device *bl_dev)
427 return dev_get_drvdata(&bl_dev->dev);
430 #ifdef CONFIG_OF
431 struct backlight_device *of_find_backlight_by_node(struct device_node *node);
432 #else
433 static inline struct backlight_device *
434 of_find_backlight_by_node(struct device_node *node)
436 return NULL;
438 #endif
440 #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
441 struct backlight_device *devm_of_find_backlight(struct device *dev);
442 #else
443 static inline struct backlight_device *
444 devm_of_find_backlight(struct device *dev)
446 return NULL;
448 #endif
450 #endif