xref: /linux/include/linux/backlight.h (revision 2fb7eb3d7e8c5c0375c726c7d5c443e6f7e53741)
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/notifier.h>
16 #include <linux/types.h>
17 
18 /**
19  * enum backlight_update_reason - what method was used to update backlight
20  *
21  * A driver indicates the method (reason) used for updating the backlight
22  * when calling backlight_force_update().
23  */
24 enum backlight_update_reason {
25 	/**
26 	 * @BACKLIGHT_UPDATE_HOTKEY: The backlight was updated using a hot-key.
27 	 */
28 	BACKLIGHT_UPDATE_HOTKEY,
29 
30 	/**
31 	 * @BACKLIGHT_UPDATE_SYSFS: The backlight was updated using sysfs.
32 	 */
33 	BACKLIGHT_UPDATE_SYSFS,
34 };
35 
36 /**
37  * enum backlight_type - the type of backlight control
38  *
39  * The type of interface used to control the backlight.
40  */
41 enum backlight_type {
42 	/**
43 	 * @BACKLIGHT_RAW:
44 	 *
45 	 * The backlight is controlled using hardware registers.
46 	 */
47 	BACKLIGHT_RAW = 1,
48 
49 	/**
50 	 * @BACKLIGHT_PLATFORM:
51 	 *
52 	 * The backlight is controlled using a platform-specific interface.
53 	 */
54 	BACKLIGHT_PLATFORM,
55 
56 	/**
57 	 * @BACKLIGHT_FIRMWARE:
58 	 *
59 	 * The backlight is controlled using a standard firmware interface.
60 	 */
61 	BACKLIGHT_FIRMWARE,
62 
63 	/**
64 	 * @BACKLIGHT_TYPE_MAX: Number of entries.
65 	 */
66 	BACKLIGHT_TYPE_MAX,
67 };
68 
69 /** enum backlight_scale - the type of scale used for brightness values
70  *
71  * The type of scale used for brightness values.
72  */
73 enum backlight_scale {
74 	/**
75 	 * @BACKLIGHT_SCALE_UNKNOWN: The scale is unknown.
76 	 */
77 	BACKLIGHT_SCALE_UNKNOWN = 0,
78 
79 	/**
80 	 * @BACKLIGHT_SCALE_LINEAR: The scale is linear.
81 	 *
82 	 * The linear scale will increase brightness the same for each step.
83 	 */
84 	BACKLIGHT_SCALE_LINEAR,
85 
86 	/**
87 	 * @BACKLIGHT_SCALE_NON_LINEAR: The scale is not linear.
88 	 *
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.
91 	 */
92 	BACKLIGHT_SCALE_NON_LINEAR,
93 };
94 
95 struct backlight_device;
96 
97 /**
98  * struct backlight_ops - backlight operations
99  *
100  * The backlight operations are specified when the backlight device is registered.
101  */
102 struct backlight_ops {
103 	/**
104 	 * @options: Configure how operations are called from the core.
105 	 *
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.
109 	 */
110 	unsigned int options;
111 
112 #define BL_CORE_SUSPENDRESUME	(1 << 0)
113 
114 	/**
115 	 * @update_status: Operation called when properties have changed.
116 	 *
117 	 * Notify the backlight driver some property has changed.
118 	 * The update_status operation is protected by the update_lock.
119 	 *
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.
123 	 *
124 	 * RETURNS:
125 	 *
126 	 * 0 on success, negative error code if any failure occurred.
127 	 */
128 	int (*update_status)(struct backlight_device *);
129 
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 	/**
145 	 * @controls_device: Check against the display device
146 	 *
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.
150 	 *
151 	 * RETURNS:
152 	 *
153 	 * If display_dev is NULL or display_dev matches the device controlled by
154 	 * the backlight, return true. Otherwise return false.
155 	 */
156 	bool (*controls_device)(struct backlight_device *bd, struct device *display_dev);
157 };
158 
159 /**
160  * struct backlight_properties - backlight properties
161  *
162  * This structure defines all the properties of a backlight.
163  */
164 struct backlight_properties {
165 	/**
166 	 * @brightness: The current brightness requested by the user.
167 	 *
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.
171 	 *
172 	 * This value can be set in the backlight_properties passed
173 	 * to devm_backlight_device_register() to set a default brightness
174 	 * value.
175 	 */
176 	int brightness;
177 
178 	/**
179 	 * @max_brightness: The maximum brightness value.
180 	 *
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.
184 	 */
185 	int max_brightness;
186 
187 	/**
188 	 * @power: The current power mode.
189 	 *
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.
193 	 *
194 	 * The possible values are: (0: full on, 4: full off), see
195 	 * BACKLIGHT_POWER constants.
196 	 *
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.
200 	 */
201 	int power;
202 
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
206 
207 	/**
208 	 * @type: The type of backlight supported.
209 	 *
210 	 * The backlight type allows userspace to make appropriate
211 	 * policy decisions based on the backlight type.
212 	 *
213 	 * This value must be set in the backlight_properties
214 	 * passed to devm_backlight_device_register().
215 	 */
216 	enum backlight_type type;
217 
218 	/**
219 	 * @state: The state of the backlight core.
220 	 *
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.
224 	 *
225 	 * backlight drivers are expected to use backlight_is_blank()
226 	 * in their update_status() operation rather than reading the
227 	 * state property.
228 	 *
229 	 * The state is maintained by the core and drivers may not modify it.
230 	 */
231 	unsigned int state;
232 
233 #define BL_CORE_SUSPENDED	(1 << 0)	/* backlight is suspended */
234 #define BL_CORE_FBBLANK		(1 << 1)	/* backlight is under an fb blank event */
235 
236 	/**
237 	 * @scale: The type of the brightness scale.
238 	 */
239 	enum backlight_scale scale;
240 };
241 
242 /**
243  * struct backlight_device - backlight device data
244  *
245  * This structure holds all data required by a backlight device.
246  */
247 struct backlight_device {
248 	/**
249 	 * @props: Backlight properties
250 	 */
251 	struct backlight_properties props;
252 
253 	/**
254 	 * @update_lock: The lock used when calling the update_status() operation.
255 	 *
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.
260 	 */
261 	struct mutex update_lock;
262 
263 	/**
264 	 * @ops_lock: The lock used around everything related to backlight_ops.
265 	 *
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.
269 	 */
270 	struct mutex ops_lock;
271 
272 	/**
273 	 * @ops: Pointer to the backlight operations.
274 	 *
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.
278 	 */
279 	const struct backlight_ops *ops;
280 
281 	/**
282 	 * @fb_notif: The framebuffer notifier block
283 	 */
284 	struct notifier_block fb_notif;
285 
286 	/**
287 	 * @entry: List entry of all registered backlight devices
288 	 */
289 	struct list_head entry;
290 
291 	/**
292 	 * @dev: Parent device.
293 	 */
294 	struct device dev;
295 
296 	/**
297 	 * @fb_bl_on: The state of individual fbdev's.
298 	 *
299 	 * Multiple fbdev's may share one backlight device. The fb_bl_on
300 	 * records the state of the individual fbdev.
301 	 */
302 	bool fb_bl_on[FB_MAX];
303 
304 	/**
305 	 * @use_count: The number of uses of fb_bl_on.
306 	 */
307 	int use_count;
308 };
309 
310 /**
311  * backlight_update_status - force an update of the backlight device status
312  * @bd: the backlight device
313  */
backlight_update_status(struct backlight_device * bd)314 static inline int backlight_update_status(struct backlight_device *bd)
315 {
316 	int ret = -ENOENT;
317 
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);
322 
323 	return ret;
324 }
325 
326 /**
327  * backlight_enable - Enable backlight
328  * @bd: the backlight device to enable
329  */
backlight_enable(struct backlight_device * bd)330 static inline int backlight_enable(struct backlight_device *bd)
331 {
332 	if (!bd)
333 		return 0;
334 
335 	bd->props.power = BACKLIGHT_POWER_ON;
336 	bd->props.state &= ~BL_CORE_FBBLANK;
337 
338 	return backlight_update_status(bd);
339 }
340 
341 /**
342  * backlight_disable - Disable backlight
343  * @bd: the backlight device to disable
344  */
backlight_disable(struct backlight_device * bd)345 static inline int backlight_disable(struct backlight_device *bd)
346 {
347 	if (!bd)
348 		return 0;
349 
350 	bd->props.power = BACKLIGHT_POWER_OFF;
351 	bd->props.state |= BL_CORE_FBBLANK;
352 
353 	return backlight_update_status(bd);
354 }
355 
356 /**
357  * backlight_is_blank - Return true if display is expected to be blank
358  * @bd: the backlight device
359  *
360  * Display is expected to be blank if any of these is true::
361  *
362  *   1) if power in not UNBLANK
363  *   2) if state indicate BLANK or SUSPENDED
364  *
365  * Returns true if display is expected to be blank, false otherwise.
366  */
backlight_is_blank(const struct backlight_device * bd)367 static inline bool backlight_is_blank(const struct backlight_device *bd)
368 {
369 	return bd->props.power != BACKLIGHT_POWER_ON ||
370 	       bd->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK);
371 }
372 
373 /**
374  * backlight_get_brightness - Returns the current brightness value
375  * @bd: the backlight device
376  *
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.
380  *
381  * Backlight drivers are expected to use this function in their update_status()
382  * operation to get the brightness value.
383  */
backlight_get_brightness(const struct backlight_device * bd)384 static inline int backlight_get_brightness(const struct backlight_device *bd)
385 {
386 	if (backlight_is_blank(bd))
387 		return 0;
388 	else
389 		return bd->props.brightness;
390 }
391 
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);
410 
411 #define to_backlight_device(obj) container_of(obj, struct backlight_device, dev)
412 
413 /**
414  * bl_get_data - access devdata
415  * @bl_dev: pointer to backlight device
416  *
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.
420  *
421  * RETURNS:
422  *
423  * pointer to devdata stored while registering the backlight device.
424  */
bl_get_data(struct backlight_device * bl_dev)425 static inline void * bl_get_data(struct backlight_device *bl_dev)
426 {
427 	return dev_get_drvdata(&bl_dev->dev);
428 }
429 
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 *
of_find_backlight_by_node(struct device_node * node)434 of_find_backlight_by_node(struct device_node *node)
435 {
436 	return NULL;
437 }
438 #endif
439 
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 *
devm_of_find_backlight(struct device * dev)444 devm_of_find_backlight(struct device *dev)
445 {
446 	return NULL;
447 }
448 #endif
449 
450 #endif
451