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