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