xref: /linux-6.15/include/linux/backlight.h (revision d160fd4e)
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 
17 enum backlight_update_reason {
18 	BACKLIGHT_UPDATE_HOTKEY,
19 	BACKLIGHT_UPDATE_SYSFS,
20 };
21 
22 enum backlight_type {
23 	BACKLIGHT_RAW = 1,
24 	BACKLIGHT_PLATFORM,
25 	BACKLIGHT_FIRMWARE,
26 	BACKLIGHT_TYPE_MAX,
27 };
28 
29 enum backlight_notification {
30 	BACKLIGHT_REGISTERED,
31 	BACKLIGHT_UNREGISTERED,
32 };
33 
34 enum backlight_scale {
35 	BACKLIGHT_SCALE_UNKNOWN = 0,
36 	BACKLIGHT_SCALE_LINEAR,
37 	BACKLIGHT_SCALE_NON_LINEAR,
38 };
39 
40 struct backlight_device;
41 struct fb_info;
42 
43 /**
44  * struct backlight_ops - backlight operations
45  *
46  * The backlight operations are specified when the backlight device is registered.
47  */
48 struct backlight_ops {
49 	/**
50 	 * @options: Configure how operations are called from the core.
51 	 *
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.
55 	 */
56 	unsigned int options;
57 
58 #define BL_CORE_SUSPENDRESUME	(1 << 0)
59 
60 	/**
61 	 * @update_status: Operation called when properties have changed.
62 	 *
63 	 * Notify the backlight driver some property has changed.
64 	 * The update_status operation is protected by the update_lock.
65 	 *
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.
69 	 *
70 	 * RETURNS:
71 	 *
72 	 * 0 on success, negative error code if any failure occurred.
73 	 */
74 	int (*update_status)(struct backlight_device *);
75 
76 	/**
77 	 * @get_brightness: Return the current backlight brightness.
78 	 *
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.
82 	 *
83 	 * RETURNS:
84 	 *
85 	 * A brightness value which is 0 or a positive number.
86 	 * On failure a negative error code is returned.
87 	 */
88 	int (*get_brightness)(struct backlight_device *);
89 
90 	/**
91 	 * @check_fb: Check the framebuffer device.
92 	 *
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.
96 	 *
97 	 * RETURNS:
98 	 *
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.
101 	 */
102 	int (*check_fb)(struct backlight_device *bd, struct fb_info *info);
103 };
104 
105 /**
106  * struct backlight_properties - backlight properties
107  *
108  * This structure defines all the properties of a backlight.
109  */
110 struct backlight_properties {
111 	/**
112 	 * @brightness: The current brightness requested by the user.
113 	 *
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.
117 	 *
118 	 * This value can be set in the backlight_properties passed
119 	 * to devm_backlight_device_register() to set a default brightness
120 	 * value.
121 	 */
122 	int brightness;
123 
124 	/**
125 	 * @max_brightness: The maximum brightness value.
126 	 *
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.
130 	 */
131 	int max_brightness;
132 
133 	/**
134 	 * @power: The current power mode.
135 	 *
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.
139 	 *
140 	 * The possible values are: (0: full on, 1 to 3: power saving
141 	 * modes; 4: full off), see FB_BLANK_XXX.
142 	 *
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.
146 	 */
147 	int power;
148 
149 	/**
150 	 * @fb_blank: The power state from the FBIOBLANK ioctl.
151 	 *
152 	 * When the FBIOBLANK ioctl is called @fb_blank is set to the
153 	 * blank parameter and the update_status() operation is called.
154 	 *
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.
158 	 *
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
162 	 * state).
163 	 *
164 	 * fb_blank is deprecated and will be removed.
165 	 */
166 	int fb_blank;
167 
168 	/**
169 	 * @type: The type of backlight supported.
170 	 *
171 	 * The backlight type allows userspace to make appropriate
172 	 * policy decisions based on the backlight type.
173 	 *
174 	 * This value must be set in the backlight_properties
175 	 * passed to devm_backlight_device_register().
176 	 */
177 	enum backlight_type type;
178 
179 	/**
180 	 * @state: The state of the backlight core.
181 	 *
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.
185 	 *
186 	 * backlight drivers are expected to use backlight_is_blank()
187 	 * in their update_status() operation rather than reading the
188 	 * state property.
189 	 *
190 	 * The state is maintained by the core and drivers may not modify it.
191 	 */
192 	unsigned int state;
193 
194 #define BL_CORE_SUSPENDED	(1 << 0)	/* backlight is suspended */
195 #define BL_CORE_FBBLANK		(1 << 1)	/* backlight is under an fb blank event */
196 
197 	/**
198 	 * @scale: The type of the brightness scale.
199 	 */
200 	enum backlight_scale scale;
201 };
202 
203 /**
204  * struct backlight_device - backlight device data
205  *
206  * This structure holds all data required by a backlight device.
207  */
208 struct backlight_device {
209 	/**
210 	 * @props: Backlight properties
211 	 */
212 	struct backlight_properties props;
213 
214 	/**
215 	 * @update_lock: The lock used when calling the update_status() operation.
216 	 *
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.
221 	 */
222 	struct mutex update_lock;
223 
224 	/**
225 	 * @ops_lock: The lock used around everything related to backlight_ops.
226 	 *
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.
230 	 */
231 	struct mutex ops_lock;
232 
233 	/**
234 	 * @ops: Pointer to the backlight operations.
235 	 *
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.
239 	 */
240 	const struct backlight_ops *ops;
241 
242 	/**
243 	 * @fb_notif: The framebuffer notifier block
244 	 */
245 	struct notifier_block fb_notif;
246 
247 	/**
248 	 * @entry: List entry of all registered backlight devices
249 	 */
250 	struct list_head entry;
251 
252 	/**
253 	 * @dev: Parent device.
254 	 */
255 	struct device dev;
256 
257 	/**
258 	 * @fb_bl_on: The state of individual fbdev's.
259 	 *
260 	 * Multiple fbdev's may share one backlight device. The fb_bl_on
261 	 * records the state of the individual fbdev.
262 	 */
263 	bool fb_bl_on[FB_MAX];
264 
265 	/**
266 	 * @use_count: The number of uses of fb_bl_on.
267 	 */
268 	int use_count;
269 };
270 
271 /**
272  * backlight_update_status - force an update of the backlight device status
273  * @bd: the backlight device
274  */
275 static inline int backlight_update_status(struct backlight_device *bd)
276 {
277 	int ret = -ENOENT;
278 
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);
283 
284 	return ret;
285 }
286 
287 /**
288  * backlight_enable - Enable backlight
289  * @bd: the backlight device to enable
290  */
291 static inline int backlight_enable(struct backlight_device *bd)
292 {
293 	if (!bd)
294 		return 0;
295 
296 	bd->props.power = FB_BLANK_UNBLANK;
297 	bd->props.fb_blank = FB_BLANK_UNBLANK;
298 	bd->props.state &= ~BL_CORE_FBBLANK;
299 
300 	return backlight_update_status(bd);
301 }
302 
303 /**
304  * backlight_disable - Disable backlight
305  * @bd: the backlight device to disable
306  */
307 static inline int backlight_disable(struct backlight_device *bd)
308 {
309 	if (!bd)
310 		return 0;
311 
312 	bd->props.power = FB_BLANK_POWERDOWN;
313 	bd->props.fb_blank = FB_BLANK_POWERDOWN;
314 	bd->props.state |= BL_CORE_FBBLANK;
315 
316 	return backlight_update_status(bd);
317 }
318 
319 /**
320  * backlight_put - Drop backlight reference
321  * @bd: the backlight device to put
322  */
323 static inline void backlight_put(struct backlight_device *bd)
324 {
325 	if (bd)
326 		put_device(&bd->dev);
327 }
328 
329 /**
330  * backlight_is_blank - Return true if display is expected to be blank
331  * @bd: the backlight device
332  *
333  * Display is expected to be blank if any of these is true::
334  *
335  *   1) if power in not UNBLANK
336  *   2) if fb_blank is not UNBLANK
337  *   3) if state indicate BLANK or SUSPENDED
338  *
339  * Returns true if display is expected to be blank, false otherwise.
340  */
341 static inline bool backlight_is_blank(const struct backlight_device *bd)
342 {
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);
346 }
347 
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);
365 
366 #define to_backlight_device(obj) container_of(obj, struct backlight_device, dev)
367 
368 /**
369  * bl_get_data - access devdata
370  * @bl_dev: pointer to backlight device
371  *
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
374  * devdata.
375  *
376  * RETURNS:
377  *
378  * pointer to devdata stored while registering the backlight device.
379  */
380 static inline void * bl_get_data(struct backlight_device *bl_dev)
381 {
382 	return dev_get_drvdata(&bl_dev->dev);
383 }
384 
385 struct generic_bl_info {
386 	const char *name;
387 	int max_intensity;
388 	int default_intensity;
389 	int limit_mask;
390 	void (*set_bl_intensity)(int intensity);
391 	void (*kick_battery)(void);
392 };
393 
394 #ifdef CONFIG_OF
395 struct backlight_device *of_find_backlight_by_node(struct device_node *node);
396 #else
397 static inline struct backlight_device *
398 of_find_backlight_by_node(struct device_node *node)
399 {
400 	return NULL;
401 }
402 #endif
403 
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);
407 #else
408 static inline struct backlight_device *of_find_backlight(struct device *dev)
409 {
410 	return NULL;
411 }
412 
413 static inline struct backlight_device *
414 devm_of_find_backlight(struct device *dev)
415 {
416 	return NULL;
417 }
418 #endif
419 
420 #endif
421