xref: /freebsd/sys/compat/linuxkpi/common/include/linux/backlight.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
12b68c973SEmmanuel Vadot /*-
22b68c973SEmmanuel Vadot  * Copyright (c) 2020 The FreeBSD Foundation
32b68c973SEmmanuel Vadot  *
42b68c973SEmmanuel Vadot  * This software was developed by Emmanuel Vadot under sponsorship
52b68c973SEmmanuel Vadot  * from the FreeBSD Foundation.
62b68c973SEmmanuel Vadot  *
72b68c973SEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
82b68c973SEmmanuel Vadot  * modification, are permitted provided that the following conditions
92b68c973SEmmanuel Vadot  * are met:
102b68c973SEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
112b68c973SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
122b68c973SEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
132b68c973SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
142b68c973SEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
152b68c973SEmmanuel Vadot  *
162b68c973SEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
172b68c973SEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182b68c973SEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192b68c973SEmmanuel Vadot  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
202b68c973SEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212b68c973SEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222b68c973SEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232b68c973SEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242b68c973SEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252b68c973SEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262b68c973SEmmanuel Vadot  * SUCH DAMAGE.
272b68c973SEmmanuel Vadot  */
282b68c973SEmmanuel Vadot 
29307f78f3SVladimir Kondratyev #ifndef _LINUXKPI_LINUX_BACKLIGHT_H_
30307f78f3SVladimir Kondratyev #define _LINUXKPI_LINUX_BACKLIGHT_H_
312b68c973SEmmanuel Vadot 
322b68c973SEmmanuel Vadot #include <linux/notifier.h>
332b68c973SEmmanuel Vadot 
342b68c973SEmmanuel Vadot struct backlight_device;
352b68c973SEmmanuel Vadot 
362b68c973SEmmanuel Vadot enum backlight_type {
372b68c973SEmmanuel Vadot 	BACKLIGHT_RAW = 0,
382b68c973SEmmanuel Vadot };
392b68c973SEmmanuel Vadot 
402b68c973SEmmanuel Vadot struct backlight_properties {
412b68c973SEmmanuel Vadot 	int type;
422b68c973SEmmanuel Vadot 	int max_brightness;
432b68c973SEmmanuel Vadot 	int brightness;
442b68c973SEmmanuel Vadot 	int power;
452b68c973SEmmanuel Vadot };
462b68c973SEmmanuel Vadot 
472b68c973SEmmanuel Vadot enum backlight_notification {
482b68c973SEmmanuel Vadot 	BACKLIGHT_REGISTERED,
492b68c973SEmmanuel Vadot 	BACKLIGHT_UNREGISTERED,
502b68c973SEmmanuel Vadot };
512b68c973SEmmanuel Vadot 
522b68c973SEmmanuel Vadot enum backlight_update_reason {
532b68c973SEmmanuel Vadot 	BACKLIGHT_UPDATE_HOTKEY = 0
542b68c973SEmmanuel Vadot };
552b68c973SEmmanuel Vadot 
562b68c973SEmmanuel Vadot struct backlight_ops {
572b68c973SEmmanuel Vadot 	int options;
582b68c973SEmmanuel Vadot #define	BL_CORE_SUSPENDRESUME   1
592b68c973SEmmanuel Vadot 	int (*update_status)(struct backlight_device *);
602b68c973SEmmanuel Vadot 	int (*get_brightness)(struct backlight_device *);
612b68c973SEmmanuel Vadot };
622b68c973SEmmanuel Vadot 
632b68c973SEmmanuel Vadot struct backlight_device {
642b68c973SEmmanuel Vadot 	const struct backlight_ops *ops;
652b68c973SEmmanuel Vadot 	struct backlight_properties props;
662b68c973SEmmanuel Vadot 	void *data;
672b68c973SEmmanuel Vadot 	struct device *dev;
682b68c973SEmmanuel Vadot 	char *name;
692b68c973SEmmanuel Vadot };
702b68c973SEmmanuel Vadot 
712b68c973SEmmanuel Vadot #define bl_get_data(bd) (bd)->data
722b68c973SEmmanuel Vadot 
732b68c973SEmmanuel Vadot struct backlight_device *linux_backlight_device_register(const char *name,
742b68c973SEmmanuel Vadot     struct device *dev, void *data, const struct backlight_ops *ops, struct backlight_properties *props);
752b68c973SEmmanuel Vadot void linux_backlight_device_unregister(struct backlight_device *bd);
762b68c973SEmmanuel Vadot #define	backlight_device_register(name, dev, data, ops, props)	\
772b68c973SEmmanuel Vadot 	linux_backlight_device_register(name, dev, data, ops, props)
782b68c973SEmmanuel Vadot #define	backlight_device_unregister(bd)	linux_backlight_device_unregister(bd)
792b68c973SEmmanuel Vadot 
80b52e3638SVladimir Kondratyev static inline int
backlight_update_status(struct backlight_device * bd)812b68c973SEmmanuel Vadot backlight_update_status(struct backlight_device *bd)
822b68c973SEmmanuel Vadot {
83b52e3638SVladimir Kondratyev 	return (bd->ops->update_status(bd));
842b68c973SEmmanuel Vadot }
852b68c973SEmmanuel Vadot 
862b68c973SEmmanuel Vadot static inline void
backlight_force_update(struct backlight_device * bd,int reason)872b68c973SEmmanuel Vadot backlight_force_update(struct backlight_device *bd, int reason)
882b68c973SEmmanuel Vadot {
892b68c973SEmmanuel Vadot 	bd->props.brightness = bd->ops->get_brightness(bd);
902b68c973SEmmanuel Vadot }
912b68c973SEmmanuel Vadot 
92b52e3638SVladimir Kondratyev static inline int
backlight_get_brightness(struct backlight_device * bd)93*a82a8a5eSJean-Sébastien Pédron backlight_get_brightness(struct backlight_device *bd)
94*a82a8a5eSJean-Sébastien Pédron {
95*a82a8a5eSJean-Sébastien Pédron 
96*a82a8a5eSJean-Sébastien Pédron 	return (bd->props.brightness);
97*a82a8a5eSJean-Sébastien Pédron }
98*a82a8a5eSJean-Sébastien Pédron 
99*a82a8a5eSJean-Sébastien Pédron static inline int
backlight_device_set_brightness(struct backlight_device * bd,int brightness)1002cc3af6eSEmmanuel Vadot backlight_device_set_brightness(struct backlight_device *bd, int brightness)
1012cc3af6eSEmmanuel Vadot {
1022cc3af6eSEmmanuel Vadot 
1032cc3af6eSEmmanuel Vadot 	if (brightness > bd->props.max_brightness)
1042cc3af6eSEmmanuel Vadot 		return (EINVAL);
1052cc3af6eSEmmanuel Vadot 	bd->props.brightness = brightness;
1062cc3af6eSEmmanuel Vadot 	return (bd->ops->update_status(bd));
1072cc3af6eSEmmanuel Vadot }
1082cc3af6eSEmmanuel Vadot 
1092cc3af6eSEmmanuel Vadot static inline int
backlight_enable(struct backlight_device * bd)110b52e3638SVladimir Kondratyev backlight_enable(struct backlight_device *bd)
111b52e3638SVladimir Kondratyev {
112b52e3638SVladimir Kondratyev 	if (bd == NULL)
113b52e3638SVladimir Kondratyev 		return (0);
114b52e3638SVladimir Kondratyev 	bd->props.power = 0/* FB_BLANK_UNBLANK */;
115b52e3638SVladimir Kondratyev 	return (backlight_update_status(bd));
116b52e3638SVladimir Kondratyev }
117b52e3638SVladimir Kondratyev 
118b52e3638SVladimir Kondratyev static inline int
backlight_disable(struct backlight_device * bd)119b52e3638SVladimir Kondratyev backlight_disable(struct backlight_device *bd)
120b52e3638SVladimir Kondratyev {
121b52e3638SVladimir Kondratyev 	if (bd == NULL)
122b52e3638SVladimir Kondratyev 		return (0);
123b52e3638SVladimir Kondratyev 	bd->props.power = 4/* FB_BLANK_POWERDOWN */;
124b52e3638SVladimir Kondratyev 	return (backlight_update_status(bd));
125b52e3638SVladimir Kondratyev }
126b52e3638SVladimir Kondratyev 
127*a82a8a5eSJean-Sébastien Pédron static inline bool
backlight_is_blank(struct backlight_device * bd)128*a82a8a5eSJean-Sébastien Pédron backlight_is_blank(struct backlight_device *bd)
129*a82a8a5eSJean-Sébastien Pédron {
130*a82a8a5eSJean-Sébastien Pédron 
131*a82a8a5eSJean-Sébastien Pédron 	return (bd->props.power != 0/* FB_BLANK_UNBLANK */);
132*a82a8a5eSJean-Sébastien Pédron }
133*a82a8a5eSJean-Sébastien Pédron 
134307f78f3SVladimir Kondratyev #endif	/* _LINUXKPI_LINUX_BACKLIGHT_H_ */
135