xref: /freebsd/sys/dev/pwm/pwm_backlight.c (revision f5463265955b829775bbb32e1fd0bc11dafc36ce)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020 Emmanuel Vadot <manu@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/bio.h>
31 #include <sys/bus.h>
32 #include <sys/conf.h>
33 #include <sys/endian.h>
34 #include <sys/fcntl.h>
35 #include <sys/ioccom.h>
36 #include <sys/kernel.h>
37 #include <sys/kthread.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/priv.h>
43 #include <sys/slicer.h>
44 #include <sys/sysctl.h>
45 #include <sys/time.h>
46 
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49 
50 #include <dev/regulator/regulator.h>
51 
52 #include <dev/backlight/backlight.h>
53 
54 #include <dev/pwm/ofw_pwm.h>
55 
56 #include "backlight_if.h"
57 #include "pwmbus_if.h"
58 
59 struct pwm_backlight_softc {
60 	device_t	pwmdev;
61 	struct cdev	*cdev;
62 
63 	pwm_channel_t	channel;
64 	uint32_t	*levels;
65 	ssize_t		nlevels;
66 	int		default_level;
67 	ssize_t		current_level;
68 
69 	regulator_t	power_supply;
70 	uint64_t	period;
71 	uint64_t	duty;
72 	bool		enabled;
73 };
74 
75 static int pwm_backlight_find_level_per_percent(struct pwm_backlight_softc *sc, int percent);
76 
77 static struct ofw_compat_data compat_data[] = {
78 	{ "pwm-backlight",	1 },
79 	{ NULL,			0 }
80 };
81 
82 static int
83 pwm_backlight_probe(device_t dev)
84 {
85 
86 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
87 		return (ENXIO);
88 
89 	device_set_desc(dev, "PWM Backlight");
90 	return (BUS_PROBE_DEFAULT);
91 }
92 
93 static int
94 pwm_backlight_attach(device_t dev)
95 {
96 	struct pwm_backlight_softc *sc;
97 	phandle_t node;
98 	int rv;
99 
100 	sc = device_get_softc(dev);
101 	node = ofw_bus_get_node(dev);
102 
103 	rv = pwm_get_by_ofw_propidx(dev, node, "pwms", 0, &sc->channel);
104 	if (rv != 0) {
105 		device_printf(dev, "Cannot map pwm channel %d\n", rv);
106 		return (ENXIO);
107 	}
108 
109 	if (regulator_get_by_ofw_property(dev, 0, "power-supply",
110 	    &sc->power_supply) != 0) {
111 		device_printf(dev, "No power-supply property\n");
112 		return (ENXIO);
113 	}
114 
115 	if (OF_hasprop(node, "brightness-levels")) {
116 		sc->nlevels = OF_getencprop_alloc(node, "brightness-levels",
117 		    (void **)&sc->levels);
118 		if (sc->nlevels <= 0) {
119 			device_printf(dev, "Cannot parse brightness levels\n");
120 			return (ENXIO);
121 		}
122 		sc->nlevels /= sizeof(uint32_t);
123 
124 		if (OF_getencprop(node, "default-brightness-level",
125 		    &sc->default_level, sizeof(uint32_t)) <= 0) {
126 			device_printf(dev, "No default-brightness-level while brightness-levels is specified\n");
127 			return (ENXIO);
128 		} else {
129 			if (sc->default_level > sc->nlevels) {
130 				device_printf(dev, "default-brightness-level isn't present in brightness-levels range\n");
131 				return (ENXIO);
132 			}
133 			sc->channel->duty = sc->channel->period * sc->levels[sc->default_level] / 100;
134 		}
135 
136 		if (bootverbose) {
137 			device_printf(dev, "Number of levels: %zd\n", sc->nlevels);
138 			device_printf(dev, "Configured period time: %ju\n", (uintmax_t)sc->channel->period);
139 			device_printf(dev, "Default duty cycle: %ju\n", (uintmax_t)sc->channel->duty);
140 		}
141 	} else {
142 		/* Get the current backlight level */
143 		PWMBUS_CHANNEL_GET_CONFIG(sc->channel->dev,
144 		    sc->channel->channel,
145 		    (unsigned int *)&sc->channel->period,
146 		    (unsigned int *)&sc->channel->duty);
147 		if (sc->channel->duty > sc->channel->period)
148 			sc->channel->duty = sc->channel->period;
149 		if (bootverbose) {
150 			device_printf(dev, "Configured period time: %ju\n", (uintmax_t)sc->channel->period);
151 			device_printf(dev, "Default duty cycle: %ju\n", (uintmax_t)sc->channel->duty);
152 		}
153 	}
154 
155 	regulator_enable(sc->power_supply);
156 	sc->channel->enabled = true;
157 	PWMBUS_CHANNEL_CONFIG(sc->channel->dev, sc->channel->channel,
158 	    sc->channel->period, sc->channel->duty);
159 	PWMBUS_CHANNEL_ENABLE(sc->channel->dev, sc->channel->channel,
160 	    sc->channel->enabled);
161 
162 	sc->current_level = pwm_backlight_find_level_per_percent(sc,
163 	    sc->channel->period / sc->channel->duty);
164 	sc->cdev = backlight_register("pwm_backlight", dev);
165 	if (sc->cdev == NULL)
166 		device_printf(dev, "Cannot register as a backlight\n");
167 
168 	return (0);
169 }
170 
171 static int
172 pwm_backlight_detach(device_t dev)
173 {
174 	struct pwm_backlight_softc *sc;
175 
176 	sc = device_get_softc(dev);
177 	if (sc->nlevels > 0)
178 		OF_prop_free(sc->levels);
179 	regulator_disable(sc->power_supply);
180 	backlight_destroy(sc->cdev);
181 	return (0);
182 }
183 
184 static int
185 pwm_backlight_find_level_per_percent(struct pwm_backlight_softc *sc, int percent)
186 {
187 	int i;
188 	int diff;
189 
190 	if (percent < 0 || percent > 100)
191 		return (-1);
192 
193 	for (i = 0, diff = 0; i < sc->nlevels; i++) {
194 		if (sc->levels[i] == percent)
195 			return (i);
196 		else if (sc->levels[i] < percent)
197 			diff = percent - sc->levels[i];
198 		else {
199 			if (diff < abs((percent - sc->levels[i])))
200 				return (i - 1);
201 			else
202 				return (i);
203 		}
204 	}
205 
206 	return (-1);
207 }
208 
209 static int
210 pwm_backlight_update_status(device_t dev, struct backlight_props *props)
211 {
212 	struct pwm_backlight_softc *sc;
213 	int reg_status;
214 	int error;
215 
216 	sc = device_get_softc(dev);
217 
218 	if (sc->nlevels != 0) {
219 		error = pwm_backlight_find_level_per_percent(sc,
220 		    props->brightness);
221 		if (error < 0)
222 			return (ERANGE);
223 		sc->current_level = error;
224 		sc->channel->duty = sc->channel->period *
225 			sc->levels[sc->current_level] / 100;
226 	} else {
227 		if (props->brightness > 100 || props->brightness < 0)
228 			return (ERANGE);
229 		sc->channel->duty = sc->channel->period *
230 			props->brightness / 100;
231 	}
232 	sc->channel->enabled = true;
233 	PWMBUS_CHANNEL_CONFIG(sc->channel->dev, sc->channel->channel,
234 	    sc->channel->period, sc->channel->duty);
235 	PWMBUS_CHANNEL_ENABLE(sc->channel->dev, sc->channel->channel,
236 	    sc->channel->enabled);
237 	error = regulator_status(sc->power_supply, &reg_status);
238 	if (error != 0)
239 		device_printf(dev,
240 		    "Cannot get power-supply status: %d\n", error);
241 	else {
242 		if (props->brightness > 0) {
243 			if (reg_status != REGULATOR_STATUS_ENABLED)
244 				regulator_enable(sc->power_supply);
245 		} else {
246 			if (reg_status == REGULATOR_STATUS_ENABLED)
247 				regulator_disable(sc->power_supply);
248 		}
249 	}
250 
251 	return (0);
252 }
253 
254 static int
255 pwm_backlight_get_status(device_t dev, struct backlight_props *props)
256 {
257 	struct pwm_backlight_softc *sc;
258 	int i;
259 
260 	sc = device_get_softc(dev);
261 
262 	if (sc->nlevels != 0) {
263 		props->brightness = sc->levels[sc->current_level];
264 		props->nlevels = sc->nlevels;
265 		for (i = 0; i < sc->nlevels; i++)
266 			props->levels[i] = sc->levels[i];
267 	} else {
268 		props->brightness = sc->channel->duty * 100 / sc->channel->period;
269 		props->nlevels = 0;
270 	}
271 	return (0);
272 }
273 
274 static int
275 pwm_backlight_get_info(device_t dev, struct backlight_info *info)
276 {
277 
278 	info->type = BACKLIGHT_TYPE_PANEL;
279 	strlcpy(info->name, "pwm-backlight", BACKLIGHTMAXNAMELENGTH);
280 	return (0);
281 }
282 
283 static device_method_t pwm_backlight_methods[] = {
284 	/* device_if */
285 	DEVMETHOD(device_probe, pwm_backlight_probe),
286 	DEVMETHOD(device_attach, pwm_backlight_attach),
287 	DEVMETHOD(device_detach, pwm_backlight_detach),
288 
289 	/* backlight interface */
290 	DEVMETHOD(backlight_update_status, pwm_backlight_update_status),
291 	DEVMETHOD(backlight_get_status, pwm_backlight_get_status),
292 	DEVMETHOD(backlight_get_info, pwm_backlight_get_info),
293 	DEVMETHOD_END
294 };
295 
296 driver_t pwm_backlight_driver = {
297 	"pwm_backlight",
298 	pwm_backlight_methods,
299 	sizeof(struct pwm_backlight_softc),
300 };
301 
302 DRIVER_MODULE(pwm_backlight, simplebus, pwm_backlight_driver, 0, 0);
303 MODULE_DEPEND(pwm_backlight, backlight, 1, 1, 1);
304 OFWBUS_PNP_INFO(compat_data);
305