1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Backlight Lowlevel Control Abstraction
5 * Copyright (C) 2003,2004 Hewlett-Packard Company
9 #ifndef _LINUX_BACKLIGHT_H
10 #define _LINUX_BACKLIGHT_H
12 #include <linux/device.h>
14 #include <linux/mutex.h>
15 #include <linux/notifier.h>
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.
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.
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.
29 * Any other use of the locks below is probably wrong.
32 enum backlight_update_reason {
33 BACKLIGHT_UPDATE_HOTKEY,
34 BACKLIGHT_UPDATE_SYSFS,
44 enum backlight_notification {
46 BACKLIGHT_UNREGISTERED,
49 enum backlight_scale {
50 BACKLIGHT_SCALE_UNKNOWN = 0,
51 BACKLIGHT_SCALE_LINEAR,
52 BACKLIGHT_SCALE_NON_LINEAR,
55 struct backlight_device;
58 struct backlight_ops {
61 #define BL_CORE_SUSPENDRESUME (1 << 0)
63 /* Notify the backlight driver some property has changed */
64 int (*update_status)(struct backlight_device *);
65 /* Return the current backlight brightness (accounting for power,
67 int (*get_brightness)(struct backlight_device *);
68 /* Check if given framebuffer device is the one bound to this backlight;
69 return 0 if not, !=0 if it is. If NULL, backlight always matches the fb. */
70 int (*check_fb)(struct backlight_device *, struct fb_info *);
73 /* This structure defines all the properties of a backlight */
74 struct backlight_properties {
75 /* Current User requested brightness (0 - max_brightness) */
77 /* Maximal value for brightness (read-only) */
79 /* Current FB Power mode (0: full on, 1..3: power saving
80 modes; 4: full off), see FB_BLANK_XXX */
82 /* FB Blanking active? (values as for power) */
83 /* Due to be removed, please use (state & BL_CORE_FBBLANK) */
86 enum backlight_type type;
87 /* Flags used to signal drivers of state changes */
89 /* Type of the brightness scale (linear, non-linear, ...) */
90 enum backlight_scale scale;
92 #define BL_CORE_SUSPENDED (1 << 0) /* backlight is suspended */
93 #define BL_CORE_FBBLANK (1 << 1) /* backlight is under an fb blank event */
97 struct backlight_device {
98 /* Backlight properties */
99 struct backlight_properties props;
101 /* Serialise access to update_status method */
102 struct mutex update_lock;
104 /* This protects the 'ops' field. If 'ops' is NULL, the driver that
105 registered this device has been unloaded, and if class_get_devdata()
106 points to something in the body of that driver, it is also invalid. */
107 struct mutex ops_lock;
108 const struct backlight_ops *ops;
110 /* The framebuffer notifier block */
111 struct notifier_block fb_notif;
113 /* list entry of all registered backlight devices */
114 struct list_head entry;
118 /* Multiple framebuffers may share one backlight device */
119 bool fb_bl_on[FB_MAX];
124 static inline int backlight_update_status(struct backlight_device *bd)
128 mutex_lock(&bd->update_lock);
129 if (bd->ops && bd->ops->update_status)
130 ret = bd->ops->update_status(bd);
131 mutex_unlock(&bd->update_lock);
137 * backlight_enable - Enable backlight
138 * @bd: the backlight device to enable
140 static inline int backlight_enable(struct backlight_device *bd)
145 bd->props.power = FB_BLANK_UNBLANK;
146 bd->props.fb_blank = FB_BLANK_UNBLANK;
147 bd->props.state &= ~BL_CORE_FBBLANK;
149 return backlight_update_status(bd);
153 * backlight_disable - Disable backlight
154 * @bd: the backlight device to disable
156 static inline int backlight_disable(struct backlight_device *bd)
161 bd->props.power = FB_BLANK_POWERDOWN;
162 bd->props.fb_blank = FB_BLANK_POWERDOWN;
163 bd->props.state |= BL_CORE_FBBLANK;
165 return backlight_update_status(bd);
169 * backlight_put - Drop backlight reference
170 * @bd: the backlight device to put
172 static inline void backlight_put(struct backlight_device *bd)
175 put_device(&bd->dev);
178 extern struct backlight_device *backlight_device_register(const char *name,
179 struct device *dev, void *devdata, const struct backlight_ops *ops,
180 const struct backlight_properties *props);
181 extern struct backlight_device *devm_backlight_device_register(
182 struct device *dev, const char *name, struct device *parent,
183 void *devdata, const struct backlight_ops *ops,
184 const struct backlight_properties *props);
185 extern void backlight_device_unregister(struct backlight_device *bd);
186 extern void devm_backlight_device_unregister(struct device *dev,
187 struct backlight_device *bd);
188 extern void backlight_force_update(struct backlight_device *bd,
189 enum backlight_update_reason reason);
190 extern int backlight_register_notifier(struct notifier_block *nb);
191 extern int backlight_unregister_notifier(struct notifier_block *nb);
192 extern struct backlight_device *backlight_device_get_by_type(enum backlight_type type);
193 struct backlight_device *backlight_device_get_by_name(const char *name);
194 extern int backlight_device_set_brightness(struct backlight_device *bd, unsigned long brightness);
196 #define to_backlight_device(obj) container_of(obj, struct backlight_device, dev)
198 static inline void * bl_get_data(struct backlight_device *bl_dev)
200 return dev_get_drvdata(&bl_dev->dev);
203 struct generic_bl_info {
206 int default_intensity;
208 void (*set_bl_intensity)(int intensity);
209 void (*kick_battery)(void);
213 struct backlight_device *of_find_backlight_by_node(struct device_node *node);
215 static inline struct backlight_device *
216 of_find_backlight_by_node(struct device_node *node)
222 #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
223 struct backlight_device *of_find_backlight(struct device *dev);
224 struct backlight_device *devm_of_find_backlight(struct device *dev);
226 static inline struct backlight_device *of_find_backlight(struct device *dev)
231 static inline struct backlight_device *
232 devm_of_find_backlight(struct device *dev)