xref: /linux-6.15/include/linux/backlight.h (revision cabf1613)
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 /* Notes on locking:
18  *
19  * backlight_device->ops_lock is an internal backlight lock protecting the
20  * ops pointer and no code outside the core should need to touch it.
21  *
22  * Access to update_status() is serialised by the update_lock mutex since
23  * most drivers seem to need this and historically get it wrong.
24  *
25  * Most drivers don't need locking on their get_brightness() method.
26  * If yours does, you need to implement it in the driver. You can use the
27  * update_lock mutex if appropriate.
28  *
29  * Any other use of the locks below is probably wrong.
30  */
31 
32 enum backlight_update_reason {
33 	BACKLIGHT_UPDATE_HOTKEY,
34 	BACKLIGHT_UPDATE_SYSFS,
35 };
36 
37 enum backlight_type {
38 	BACKLIGHT_RAW = 1,
39 	BACKLIGHT_PLATFORM,
40 	BACKLIGHT_FIRMWARE,
41 	BACKLIGHT_TYPE_MAX,
42 };
43 
44 enum backlight_notification {
45 	BACKLIGHT_REGISTERED,
46 	BACKLIGHT_UNREGISTERED,
47 };
48 
49 enum backlight_scale {
50 	BACKLIGHT_SCALE_UNKNOWN = 0,
51 	BACKLIGHT_SCALE_LINEAR,
52 	BACKLIGHT_SCALE_NON_LINEAR,
53 };
54 
55 struct backlight_device;
56 struct fb_info;
57 
58 /**
59  * struct backlight_ops - backlight operations
60  *
61  * The backlight operations are specified when the backlight device is registered.
62  */
63 struct backlight_ops {
64 	/**
65 	 * @options: Configure how operations are called from the core.
66 	 *
67 	 * The options parameter is used to adjust the behaviour of the core.
68 	 * Set BL_CORE_SUSPENDRESUME to get the update_status() operation called
69 	 * upon suspend and resume.
70 	 */
71 	unsigned int options;
72 
73 #define BL_CORE_SUSPENDRESUME	(1 << 0)
74 
75 	/**
76 	 * @update_status: Operation called when properties have changed.
77 	 *
78 	 * Notify the backlight driver some property has changed.
79 	 * The update_status operation is protected by the update_lock.
80 	 *
81 	 * The backlight driver is expected to use backlight_is_blank()
82 	 * to check if the display is blanked and set brightness accordingly.
83 	 * update_status() is called when any of the properties has changed.
84 	 *
85 	 * RETURNS:
86 	 *
87 	 * 0 on success, negative error code if any failure occurred.
88 	 */
89 	int (*update_status)(struct backlight_device *);
90 
91 	/**
92 	 * @get_brightness: Return the current backlight brightness.
93 	 *
94 	 * The driver may implement this as a readback from the HW.
95 	 * This operation is optional and if not present then the current
96 	 * brightness property value is used.
97 	 *
98 	 * RETURNS:
99 	 *
100 	 * A brightness value which is 0 or a positive number.
101 	 * On failure a negative error code is returned.
102 	 */
103 	int (*get_brightness)(struct backlight_device *);
104 
105 	/**
106 	 * @check_fb: Check the framebuffer device.
107 	 *
108 	 * Check if given framebuffer device is the one bound to this backlight.
109 	 * This operation is optional and if not implemented it is assumed that the
110 	 * fbdev is always the one bound to the backlight.
111 	 *
112 	 * RETURNS:
113 	 *
114 	 * If info is NULL or the info matches the fbdev bound to the backlight return true.
115 	 * If info does not match the fbdev bound to the backlight return false.
116 	 */
117 	int (*check_fb)(struct backlight_device *bd, struct fb_info *info);
118 };
119 
120 /**
121  * struct backlight_properties - backlight properties
122  *
123  * This structure defines all the properties of a backlight.
124  */
125 struct backlight_properties {
126 	/**
127 	 * @brightness: The current brightness requested by the user.
128 	 *
129 	 * The backlight core makes sure the range is (0 to max_brightness)
130 	 * when the brightness is set via the sysfs attribute:
131 	 * /sys/class/backlight/<backlight>/brightness.
132 	 *
133 	 * This value can be set in the backlight_properties passed
134 	 * to devm_backlight_device_register() to set a default brightness
135 	 * value.
136 	 */
137 	int brightness;
138 
139 	/**
140 	 * @max_brightness: The maximum brightness value.
141 	 *
142 	 * This value must be set in the backlight_properties passed to
143 	 * devm_backlight_device_register() and shall not be modified by the
144 	 * driver after registration.
145 	 */
146 	int max_brightness;
147 
148 	/**
149 	 * @power: The current power mode.
150 	 *
151 	 * User space can configure the power mode using the sysfs
152 	 * attribute: /sys/class/backlight/<backlight>/bl_power
153 	 * When the power property is updated update_status() is called.
154 	 *
155 	 * The possible values are: (0: full on, 1 to 3: power saving
156 	 * modes; 4: full off), see FB_BLANK_XXX.
157 	 *
158 	 * When the backlight device is enabled @power is set
159 	 * to FB_BLANK_UNBLANK. When the backlight device is disabled
160 	 * @power is set to FB_BLANK_POWERDOWN.
161 	 */
162 	int power;
163 
164 	/**
165 	 * @fb_blank: The power state from the FBIOBLANK ioctl.
166 	 *
167 	 * When the FBIOBLANK ioctl is called @fb_blank is set to the
168 	 * blank parameter and the update_status() operation is called.
169 	 *
170 	 * When the backlight device is enabled @fb_blank is set
171 	 * to FB_BLANK_UNBLANK. When the backlight device is disabled
172 	 * @fb_blank is set to FB_BLANK_POWERDOWN.
173 	 *
174 	 * Backlight drivers should avoid using this property. It has been
175 	 * replaced by state & BL_CORE_FBLANK (although most drivers should
176 	 * use backlight_is_blank() as the preferred means to get the blank
177 	 * state).
178 	 *
179 	 * fb_blank is deprecated and will be removed.
180 	 */
181 	int fb_blank;
182 
183 	/**
184 	 * @type: The type of backlight supported.
185 	 *
186 	 * The backlight type allows userspace to make appropriate
187 	 * policy decisions based on the backlight type.
188 	 *
189 	 * This value must be set in the backlight_properties
190 	 * passed to devm_backlight_device_register().
191 	 */
192 	enum backlight_type type;
193 
194 	/**
195 	 * @state: The state of the backlight core.
196 	 *
197 	 * The state is a bitmask. BL_CORE_FBBLANK is set when the display
198 	 * is expected to be blank. BL_CORE_SUSPENDED is set when the
199 	 * driver is suspended.
200 	 *
201 	 * backlight drivers are expected to use backlight_is_blank()
202 	 * in their update_status() operation rather than reading the
203 	 * state property.
204 	 *
205 	 * The state is maintained by the core and drivers may not modify it.
206 	 */
207 	unsigned int state;
208 
209 #define BL_CORE_SUSPENDED	(1 << 0)	/* backlight is suspended */
210 #define BL_CORE_FBBLANK		(1 << 1)	/* backlight is under an fb blank event */
211 
212 	/**
213 	 * @scale: The type of the brightness scale.
214 	 */
215 	enum backlight_scale scale;
216 };
217 
218 struct backlight_device {
219 	/* Backlight properties */
220 	struct backlight_properties props;
221 
222 	/* Serialise access to update_status method */
223 	struct mutex update_lock;
224 
225 	/* This protects the 'ops' field. If 'ops' is NULL, the driver that
226 	   registered this device has been unloaded, and if class_get_devdata()
227 	   points to something in the body of that driver, it is also invalid. */
228 	struct mutex ops_lock;
229 	const struct backlight_ops *ops;
230 
231 	/* The framebuffer notifier block */
232 	struct notifier_block fb_notif;
233 
234 	/* list entry of all registered backlight devices */
235 	struct list_head entry;
236 
237 	struct device dev;
238 
239 	/* Multiple framebuffers may share one backlight device */
240 	bool fb_bl_on[FB_MAX];
241 
242 	int use_count;
243 };
244 
245 static inline int backlight_update_status(struct backlight_device *bd)
246 {
247 	int ret = -ENOENT;
248 
249 	mutex_lock(&bd->update_lock);
250 	if (bd->ops && bd->ops->update_status)
251 		ret = bd->ops->update_status(bd);
252 	mutex_unlock(&bd->update_lock);
253 
254 	return ret;
255 }
256 
257 /**
258  * backlight_enable - Enable backlight
259  * @bd: the backlight device to enable
260  */
261 static inline int backlight_enable(struct backlight_device *bd)
262 {
263 	if (!bd)
264 		return 0;
265 
266 	bd->props.power = FB_BLANK_UNBLANK;
267 	bd->props.fb_blank = FB_BLANK_UNBLANK;
268 	bd->props.state &= ~BL_CORE_FBBLANK;
269 
270 	return backlight_update_status(bd);
271 }
272 
273 /**
274  * backlight_disable - Disable backlight
275  * @bd: the backlight device to disable
276  */
277 static inline int backlight_disable(struct backlight_device *bd)
278 {
279 	if (!bd)
280 		return 0;
281 
282 	bd->props.power = FB_BLANK_POWERDOWN;
283 	bd->props.fb_blank = FB_BLANK_POWERDOWN;
284 	bd->props.state |= BL_CORE_FBBLANK;
285 
286 	return backlight_update_status(bd);
287 }
288 
289 /**
290  * backlight_put - Drop backlight reference
291  * @bd: the backlight device to put
292  */
293 static inline void backlight_put(struct backlight_device *bd)
294 {
295 	if (bd)
296 		put_device(&bd->dev);
297 }
298 
299 /**
300  * backlight_is_blank - Return true if display is expected to be blank
301  * @bd: the backlight device
302  *
303  * Display is expected to be blank if any of these is true::
304  *
305  *   1) if power in not UNBLANK
306  *   2) if fb_blank is not UNBLANK
307  *   3) if state indicate BLANK or SUSPENDED
308  *
309  * Returns true if display is expected to be blank, false otherwise.
310  */
311 static inline bool backlight_is_blank(const struct backlight_device *bd)
312 {
313 	return bd->props.power != FB_BLANK_UNBLANK ||
314 	       bd->props.fb_blank != FB_BLANK_UNBLANK ||
315 	       bd->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK);
316 }
317 
318 extern struct backlight_device *backlight_device_register(const char *name,
319 	struct device *dev, void *devdata, const struct backlight_ops *ops,
320 	const struct backlight_properties *props);
321 extern struct backlight_device *devm_backlight_device_register(
322 	struct device *dev, const char *name, struct device *parent,
323 	void *devdata, const struct backlight_ops *ops,
324 	const struct backlight_properties *props);
325 extern void backlight_device_unregister(struct backlight_device *bd);
326 extern void devm_backlight_device_unregister(struct device *dev,
327 					struct backlight_device *bd);
328 extern void backlight_force_update(struct backlight_device *bd,
329 				   enum backlight_update_reason reason);
330 extern int backlight_register_notifier(struct notifier_block *nb);
331 extern int backlight_unregister_notifier(struct notifier_block *nb);
332 extern struct backlight_device *backlight_device_get_by_type(enum backlight_type type);
333 struct backlight_device *backlight_device_get_by_name(const char *name);
334 extern int backlight_device_set_brightness(struct backlight_device *bd, unsigned long brightness);
335 
336 #define to_backlight_device(obj) container_of(obj, struct backlight_device, dev)
337 
338 static inline void * bl_get_data(struct backlight_device *bl_dev)
339 {
340 	return dev_get_drvdata(&bl_dev->dev);
341 }
342 
343 struct generic_bl_info {
344 	const char *name;
345 	int max_intensity;
346 	int default_intensity;
347 	int limit_mask;
348 	void (*set_bl_intensity)(int intensity);
349 	void (*kick_battery)(void);
350 };
351 
352 #ifdef CONFIG_OF
353 struct backlight_device *of_find_backlight_by_node(struct device_node *node);
354 #else
355 static inline struct backlight_device *
356 of_find_backlight_by_node(struct device_node *node)
357 {
358 	return NULL;
359 }
360 #endif
361 
362 #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
363 struct backlight_device *of_find_backlight(struct device *dev);
364 struct backlight_device *devm_of_find_backlight(struct device *dev);
365 #else
366 static inline struct backlight_device *of_find_backlight(struct device *dev)
367 {
368 	return NULL;
369 }
370 
371 static inline struct backlight_device *
372 devm_of_find_backlight(struct device *dev)
373 {
374 	return NULL;
375 }
376 #endif
377 
378 #endif
379