1 /*-
2  * Copyright (c) 2020 The FreeBSD Foundation
3  *
4  * This software was developed by Emmanuel Vadot under sponsorship
5  * from the FreeBSD Foundation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _LINUXKPI_LINUX_BACKLIGHT_H_
30 #define _LINUXKPI_LINUX_BACKLIGHT_H_
31 
32 #include <linux/notifier.h>
33 
34 struct backlight_device;
35 
36 enum backlight_type {
37 	BACKLIGHT_RAW = 0,
38 };
39 
40 struct backlight_properties {
41 	int type;
42 	int max_brightness;
43 	int brightness;
44 	int power;
45 };
46 
47 enum backlight_notification {
48 	BACKLIGHT_REGISTERED,
49 	BACKLIGHT_UNREGISTERED,
50 };
51 
52 enum backlight_update_reason {
53 	BACKLIGHT_UPDATE_HOTKEY = 0
54 };
55 
56 struct backlight_ops {
57 	int options;
58 #define	BL_CORE_SUSPENDRESUME   1
59 	int (*update_status)(struct backlight_device *);
60 	int (*get_brightness)(struct backlight_device *);
61 };
62 
63 struct backlight_device {
64 	const struct backlight_ops *ops;
65 	struct backlight_properties props;
66 	void *data;
67 	struct device *dev;
68 	char *name;
69 };
70 
71 #define bl_get_data(bd) (bd)->data
72 
73 struct backlight_device *linux_backlight_device_register(const char *name,
74     struct device *dev, void *data, const struct backlight_ops *ops, struct backlight_properties *props);
75 void linux_backlight_device_unregister(struct backlight_device *bd);
76 #define	backlight_device_register(name, dev, data, ops, props)	\
77 	linux_backlight_device_register(name, dev, data, ops, props)
78 #define	backlight_device_unregister(bd)	linux_backlight_device_unregister(bd)
79 
80 static inline int
backlight_update_status(struct backlight_device * bd)81 backlight_update_status(struct backlight_device *bd)
82 {
83 	return (bd->ops->update_status(bd));
84 }
85 
86 static inline void
backlight_force_update(struct backlight_device * bd,int reason)87 backlight_force_update(struct backlight_device *bd, int reason)
88 {
89 	bd->props.brightness = bd->ops->get_brightness(bd);
90 }
91 
92 static inline int
backlight_get_brightness(struct backlight_device * bd)93 backlight_get_brightness(struct backlight_device *bd)
94 {
95 
96 	return (bd->props.brightness);
97 }
98 
99 static inline int
backlight_device_set_brightness(struct backlight_device * bd,int brightness)100 backlight_device_set_brightness(struct backlight_device *bd, int brightness)
101 {
102 
103 	if (brightness > bd->props.max_brightness)
104 		return (EINVAL);
105 	bd->props.brightness = brightness;
106 	return (bd->ops->update_status(bd));
107 }
108 
109 static inline int
backlight_enable(struct backlight_device * bd)110 backlight_enable(struct backlight_device *bd)
111 {
112 	if (bd == NULL)
113 		return (0);
114 	bd->props.power = 0/* FB_BLANK_UNBLANK */;
115 	return (backlight_update_status(bd));
116 }
117 
118 static inline int
backlight_disable(struct backlight_device * bd)119 backlight_disable(struct backlight_device *bd)
120 {
121 	if (bd == NULL)
122 		return (0);
123 	bd->props.power = 4/* FB_BLANK_POWERDOWN */;
124 	return (backlight_update_status(bd));
125 }
126 
127 static inline bool
backlight_is_blank(struct backlight_device * bd)128 backlight_is_blank(struct backlight_device *bd)
129 {
130 
131 	return (bd->props.power != 0/* FB_BLANK_UNBLANK */);
132 }
133 
134 #endif	/* _LINUXKPI_LINUX_BACKLIGHT_H_ */
135