xref: /linux-6.15/include/linux/backlight.h (revision 6f10cd12)
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 static inline int backlight_update_status(struct backlight_device *bd)
272 {
273 	int ret = -ENOENT;
274 
275 	mutex_lock(&bd->update_lock);
276 	if (bd->ops && bd->ops->update_status)
277 		ret = bd->ops->update_status(bd);
278 	mutex_unlock(&bd->update_lock);
279 
280 	return ret;
281 }
282 
283 /**
284  * backlight_enable - Enable backlight
285  * @bd: the backlight device to enable
286  */
287 static inline int backlight_enable(struct backlight_device *bd)
288 {
289 	if (!bd)
290 		return 0;
291 
292 	bd->props.power = FB_BLANK_UNBLANK;
293 	bd->props.fb_blank = FB_BLANK_UNBLANK;
294 	bd->props.state &= ~BL_CORE_FBBLANK;
295 
296 	return backlight_update_status(bd);
297 }
298 
299 /**
300  * backlight_disable - Disable backlight
301  * @bd: the backlight device to disable
302  */
303 static inline int backlight_disable(struct backlight_device *bd)
304 {
305 	if (!bd)
306 		return 0;
307 
308 	bd->props.power = FB_BLANK_POWERDOWN;
309 	bd->props.fb_blank = FB_BLANK_POWERDOWN;
310 	bd->props.state |= BL_CORE_FBBLANK;
311 
312 	return backlight_update_status(bd);
313 }
314 
315 /**
316  * backlight_put - Drop backlight reference
317  * @bd: the backlight device to put
318  */
319 static inline void backlight_put(struct backlight_device *bd)
320 {
321 	if (bd)
322 		put_device(&bd->dev);
323 }
324 
325 /**
326  * backlight_is_blank - Return true if display is expected to be blank
327  * @bd: the backlight device
328  *
329  * Display is expected to be blank if any of these is true::
330  *
331  *   1) if power in not UNBLANK
332  *   2) if fb_blank is not UNBLANK
333  *   3) if state indicate BLANK or SUSPENDED
334  *
335  * Returns true if display is expected to be blank, false otherwise.
336  */
337 static inline bool backlight_is_blank(const struct backlight_device *bd)
338 {
339 	return bd->props.power != FB_BLANK_UNBLANK ||
340 	       bd->props.fb_blank != FB_BLANK_UNBLANK ||
341 	       bd->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK);
342 }
343 
344 extern struct backlight_device *backlight_device_register(const char *name,
345 	struct device *dev, void *devdata, const struct backlight_ops *ops,
346 	const struct backlight_properties *props);
347 extern struct backlight_device *devm_backlight_device_register(
348 	struct device *dev, const char *name, struct device *parent,
349 	void *devdata, const struct backlight_ops *ops,
350 	const struct backlight_properties *props);
351 extern void backlight_device_unregister(struct backlight_device *bd);
352 extern void devm_backlight_device_unregister(struct device *dev,
353 					struct backlight_device *bd);
354 extern void backlight_force_update(struct backlight_device *bd,
355 				   enum backlight_update_reason reason);
356 extern int backlight_register_notifier(struct notifier_block *nb);
357 extern int backlight_unregister_notifier(struct notifier_block *nb);
358 extern struct backlight_device *backlight_device_get_by_type(enum backlight_type type);
359 struct backlight_device *backlight_device_get_by_name(const char *name);
360 extern int backlight_device_set_brightness(struct backlight_device *bd, unsigned long brightness);
361 
362 #define to_backlight_device(obj) container_of(obj, struct backlight_device, dev)
363 
364 static inline void * bl_get_data(struct backlight_device *bl_dev)
365 {
366 	return dev_get_drvdata(&bl_dev->dev);
367 }
368 
369 struct generic_bl_info {
370 	const char *name;
371 	int max_intensity;
372 	int default_intensity;
373 	int limit_mask;
374 	void (*set_bl_intensity)(int intensity);
375 	void (*kick_battery)(void);
376 };
377 
378 #ifdef CONFIG_OF
379 struct backlight_device *of_find_backlight_by_node(struct device_node *node);
380 #else
381 static inline struct backlight_device *
382 of_find_backlight_by_node(struct device_node *node)
383 {
384 	return NULL;
385 }
386 #endif
387 
388 #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
389 struct backlight_device *of_find_backlight(struct device *dev);
390 struct backlight_device *devm_of_find_backlight(struct device *dev);
391 #else
392 static inline struct backlight_device *of_find_backlight(struct device *dev)
393 {
394 	return NULL;
395 }
396 
397 static inline struct backlight_device *
398 devm_of_find_backlight(struct device *dev)
399 {
400 	return NULL;
401 }
402 #endif
403 
404 #endif
405