xref: /linux-6.15/include/linux/backlight.h (revision 57e148b6)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Backlight Lowlevel Control Abstraction
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2003,2004 Hewlett-Packard Company
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #ifndef _LINUX_BACKLIGHT_H
91da177e4SLinus Torvalds #define _LINUX_BACKLIGHT_H
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/device.h>
1228ee086dSRichard Purdie #include <linux/mutex.h>
131da177e4SLinus Torvalds #include <linux/notifier.h>
141da177e4SLinus Torvalds 
1528ee086dSRichard Purdie /* Notes on locking:
1628ee086dSRichard Purdie  *
17599a52d1SRichard Purdie  * backlight_device->ops_lock is an internal backlight lock protecting the
18599a52d1SRichard Purdie  * ops pointer and no code outside the core should need to touch it.
1928ee086dSRichard Purdie  *
2028ee086dSRichard Purdie  * Access to update_status() is serialised by the update_lock mutex since
2128ee086dSRichard Purdie  * most drivers seem to need this and historically get it wrong.
2228ee086dSRichard Purdie  *
2328ee086dSRichard Purdie  * Most drivers don't need locking on their get_brightness() method.
2428ee086dSRichard Purdie  * If yours does, you need to implement it in the driver. You can use the
2528ee086dSRichard Purdie  * update_lock mutex if appropriate.
2628ee086dSRichard Purdie  *
2728ee086dSRichard Purdie  * Any other use of the locks below is probably wrong.
2828ee086dSRichard Purdie  */
2928ee086dSRichard Purdie 
30325253a6SMatthew Garrett enum backlight_update_reason {
31325253a6SMatthew Garrett 	BACKLIGHT_UPDATE_HOTKEY,
32325253a6SMatthew Garrett 	BACKLIGHT_UPDATE_SYSFS,
33325253a6SMatthew Garrett };
34325253a6SMatthew Garrett 
351da177e4SLinus Torvalds struct backlight_device;
361da177e4SLinus Torvalds struct fb_info;
371da177e4SLinus Torvalds 
38599a52d1SRichard Purdie struct backlight_ops {
39b4144e4fSRichard Purdie 	unsigned int options;
40c835ee7fSRichard Purdie 
41c835ee7fSRichard Purdie #define BL_CORE_SUSPENDRESUME	(1 << 0)
42c835ee7fSRichard Purdie 
436ca01765SRichard Purdie 	/* Notify the backlight driver some property has changed */
44b4144e4fSRichard Purdie 	int (*update_status)(struct backlight_device *);
456ca01765SRichard Purdie 	/* Return the current backlight brightness (accounting for power,
466ca01765SRichard Purdie 	   fb_blank etc.) */
47b4144e4fSRichard Purdie 	int (*get_brightness)(struct backlight_device *);
481da177e4SLinus Torvalds 	/* Check if given framebuffer device is the one bound to this backlight;
491da177e4SLinus Torvalds 	   return 0 if not, !=0 if it is. If NULL, backlight always matches the fb. */
50*57e148b6SBruno Prémont 	int (*check_fb)(struct backlight_device *, struct fb_info *);
51599a52d1SRichard Purdie };
526ca01765SRichard Purdie 
53599a52d1SRichard Purdie /* This structure defines all the properties of a backlight */
54599a52d1SRichard Purdie struct backlight_properties {
556ca01765SRichard Purdie 	/* Current User requested brightness (0 - max_brightness) */
566ca01765SRichard Purdie 	int brightness;
576ca01765SRichard Purdie 	/* Maximal value for brightness (read-only) */
586ca01765SRichard Purdie 	int max_brightness;
596ca01765SRichard Purdie 	/* Current FB Power mode (0: full on, 1..3: power saving
606ca01765SRichard Purdie 	   modes; 4: full off), see FB_BLANK_XXX */
616ca01765SRichard Purdie 	int power;
626ca01765SRichard Purdie 	/* FB Blanking active? (values as for power) */
63c835ee7fSRichard Purdie 	/* Due to be removed, please use (state & BL_CORE_FBBLANK) */
646ca01765SRichard Purdie 	int fb_blank;
65c835ee7fSRichard Purdie 	/* Flags used to signal drivers of state changes */
66c835ee7fSRichard Purdie 	/* Upper 4 bits are reserved for driver internal use */
67c835ee7fSRichard Purdie 	unsigned int state;
68c835ee7fSRichard Purdie 
69c835ee7fSRichard Purdie #define BL_CORE_SUSPENDED	(1 << 0)	/* backlight is suspended */
70c835ee7fSRichard Purdie #define BL_CORE_FBBLANK		(1 << 1)	/* backlight is under an fb blank event */
71c835ee7fSRichard Purdie #define BL_CORE_DRIVER4		(1 << 28)	/* reserved for driver specific use */
72c835ee7fSRichard Purdie #define BL_CORE_DRIVER3		(1 << 29)	/* reserved for driver specific use */
73c835ee7fSRichard Purdie #define BL_CORE_DRIVER2		(1 << 30)	/* reserved for driver specific use */
74c835ee7fSRichard Purdie #define BL_CORE_DRIVER1		(1 << 31)	/* reserved for driver specific use */
75c835ee7fSRichard Purdie 
761da177e4SLinus Torvalds };
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds struct backlight_device {
79599a52d1SRichard Purdie 	/* Backlight properties */
80599a52d1SRichard Purdie 	struct backlight_properties props;
81599a52d1SRichard Purdie 
8228ee086dSRichard Purdie 	/* Serialise access to update_status method */
8328ee086dSRichard Purdie 	struct mutex update_lock;
84599a52d1SRichard Purdie 
85599a52d1SRichard Purdie 	/* This protects the 'ops' field. If 'ops' is NULL, the driver that
86599a52d1SRichard Purdie 	   registered this device has been unloaded, and if class_get_devdata()
87599a52d1SRichard Purdie 	   points to something in the body of that driver, it is also invalid. */
88599a52d1SRichard Purdie 	struct mutex ops_lock;
899905a43bSEmese Revfy 	const struct backlight_ops *ops;
90599a52d1SRichard Purdie 
911da177e4SLinus Torvalds 	/* The framebuffer notifier block */
921da177e4SLinus Torvalds 	struct notifier_block fb_notif;
93655bfd7aSRichard Purdie 
94655bfd7aSRichard Purdie 	struct device dev;
951da177e4SLinus Torvalds };
961da177e4SLinus Torvalds 
9728ee086dSRichard Purdie static inline void backlight_update_status(struct backlight_device *bd)
9828ee086dSRichard Purdie {
9928ee086dSRichard Purdie 	mutex_lock(&bd->update_lock);
100599a52d1SRichard Purdie 	if (bd->ops && bd->ops->update_status)
101599a52d1SRichard Purdie 		bd->ops->update_status(bd);
10228ee086dSRichard Purdie 	mutex_unlock(&bd->update_lock);
10328ee086dSRichard Purdie }
10428ee086dSRichard Purdie 
1051da177e4SLinus Torvalds extern struct backlight_device *backlight_device_register(const char *name,
1069905a43bSEmese Revfy 	struct device *dev, void *devdata, const struct backlight_ops *ops);
1071da177e4SLinus Torvalds extern void backlight_device_unregister(struct backlight_device *bd);
108325253a6SMatthew Garrett extern void backlight_force_update(struct backlight_device *bd,
109325253a6SMatthew Garrett 				   enum backlight_update_reason reason);
1101da177e4SLinus Torvalds 
111655bfd7aSRichard Purdie #define to_backlight_device(obj) container_of(obj, struct backlight_device, dev)
112655bfd7aSRichard Purdie 
113655bfd7aSRichard Purdie static inline void * bl_get_data(struct backlight_device *bl_dev)
114655bfd7aSRichard Purdie {
115655bfd7aSRichard Purdie 	return dev_get_drvdata(&bl_dev->dev);
116655bfd7aSRichard Purdie }
1171da177e4SLinus Torvalds 
118c3f8f650SRichard Purdie struct generic_bl_info {
119c3f8f650SRichard Purdie 	const char *name;
120c3f8f650SRichard Purdie 	int max_intensity;
121c3f8f650SRichard Purdie 	int default_intensity;
122c3f8f650SRichard Purdie 	int limit_mask;
123c3f8f650SRichard Purdie 	void (*set_bl_intensity)(int intensity);
124c3f8f650SRichard Purdie 	void (*kick_battery)(void);
125c3f8f650SRichard Purdie };
126c3f8f650SRichard Purdie 
1271da177e4SLinus Torvalds #endif
128