pwm_backlight.c revision 1.3 1 1.3 jmcneill /* $NetBSD: pwm_backlight.c,v 1.3 2018/05/07 12:58:58 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.3 jmcneill __KERNEL_RCSID(0, "$NetBSD: pwm_backlight.c,v 1.3 2018/05/07 12:58:58 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/systm.h>
36 1.1 jmcneill #include <sys/sysctl.h>
37 1.1 jmcneill #include <sys/kmem.h>
38 1.1 jmcneill #include <sys/gpio.h>
39 1.1 jmcneill
40 1.1 jmcneill #include <dev/pwm/pwmvar.h>
41 1.1 jmcneill
42 1.1 jmcneill #include <dev/fdt/fdtvar.h>
43 1.1 jmcneill
44 1.1 jmcneill struct pwm_backlight_softc {
45 1.1 jmcneill device_t sc_dev;
46 1.1 jmcneill pwm_tag_t sc_pwm;
47 1.1 jmcneill struct fdtbus_gpio_pin *sc_pin;
48 1.1 jmcneill
49 1.1 jmcneill u_int *sc_levels;
50 1.1 jmcneill u_int sc_nlevels;
51 1.1 jmcneill
52 1.1 jmcneill char *sc_levelstr;
53 1.1 jmcneill };
54 1.1 jmcneill
55 1.1 jmcneill static int pwm_backlight_match(device_t, cfdata_t, void *);
56 1.1 jmcneill static void pwm_backlight_attach(device_t, device_t, void *);
57 1.1 jmcneill
58 1.1 jmcneill static void pwm_backlight_sysctl_init(struct pwm_backlight_softc *);
59 1.1 jmcneill static void pwm_backlight_pmf_init(struct pwm_backlight_softc *);
60 1.1 jmcneill static void pwm_backlight_set(struct pwm_backlight_softc *, u_int);
61 1.1 jmcneill static u_int pwm_backlight_get(struct pwm_backlight_softc *);
62 1.1 jmcneill
63 1.1 jmcneill static const char *compatible[] = {
64 1.1 jmcneill "pwm-backlight",
65 1.1 jmcneill NULL
66 1.1 jmcneill };
67 1.1 jmcneill
68 1.1 jmcneill CFATTACH_DECL_NEW(pwmbacklight, sizeof(struct pwm_backlight_softc),
69 1.1 jmcneill pwm_backlight_match, pwm_backlight_attach, NULL, NULL);
70 1.1 jmcneill
71 1.1 jmcneill static int
72 1.1 jmcneill pwm_backlight_match(device_t parent, cfdata_t cf, void *aux)
73 1.1 jmcneill {
74 1.1 jmcneill struct fdt_attach_args * const faa = aux;
75 1.1 jmcneill
76 1.1 jmcneill return of_match_compatible(faa->faa_phandle, compatible);
77 1.1 jmcneill }
78 1.1 jmcneill
79 1.1 jmcneill static void
80 1.1 jmcneill pwm_backlight_attach(device_t parent, device_t self, void *aux)
81 1.1 jmcneill {
82 1.1 jmcneill struct pwm_backlight_softc * const sc = device_private(self);
83 1.1 jmcneill struct fdt_attach_args * const faa = aux;
84 1.1 jmcneill const int phandle = faa->faa_phandle;
85 1.1 jmcneill const u_int *levels;
86 1.1 jmcneill u_int default_level;
87 1.1 jmcneill u_int n;
88 1.1 jmcneill int len;
89 1.1 jmcneill
90 1.1 jmcneill sc->sc_dev = self;
91 1.1 jmcneill sc->sc_pwm = fdtbus_pwm_acquire(phandle, "pwms");
92 1.1 jmcneill if (sc->sc_pwm == NULL) {
93 1.1 jmcneill aprint_error(": couldn't acquire pwm\n");
94 1.1 jmcneill return;
95 1.1 jmcneill }
96 1.1 jmcneill if (of_hasprop(phandle, "enable-gpios")) {
97 1.1 jmcneill sc->sc_pin = fdtbus_gpio_acquire(phandle, "enable-gpios",
98 1.1 jmcneill GPIO_PIN_OUTPUT);
99 1.1 jmcneill if (!sc->sc_pin) {
100 1.1 jmcneill aprint_error(": couldn't acquire enable gpio\n");
101 1.1 jmcneill return;
102 1.1 jmcneill }
103 1.2 jmcneill fdtbus_gpio_write(sc->sc_pin, 1);
104 1.1 jmcneill }
105 1.1 jmcneill
106 1.1 jmcneill levels = fdtbus_get_prop(phandle, "brightness-levels", &len);
107 1.1 jmcneill if (len < 4) {
108 1.1 jmcneill aprint_error(": couldn't get 'brightness-levels' property\n");
109 1.1 jmcneill return;
110 1.1 jmcneill }
111 1.1 jmcneill sc->sc_levels = kmem_alloc(len, KM_SLEEP);
112 1.1 jmcneill sc->sc_nlevels = len / 4;
113 1.1 jmcneill for (n = 0; n < sc->sc_nlevels; n++)
114 1.1 jmcneill sc->sc_levels[n] = be32toh(levels[n]);
115 1.1 jmcneill
116 1.1 jmcneill aprint_naive("\n");
117 1.1 jmcneill aprint_normal(": PWM Backlight");
118 1.1 jmcneill aprint_verbose(" <");
119 1.1 jmcneill for (n = 0; n < sc->sc_nlevels; n++) {
120 1.1 jmcneill aprint_verbose("%s%u", n ? " " : "", sc->sc_levels[n]);
121 1.1 jmcneill }
122 1.1 jmcneill aprint_verbose(">");
123 1.1 jmcneill aprint_normal("\n");
124 1.1 jmcneill
125 1.1 jmcneill if (of_getprop_uint32(phandle, "default-brightness-level", &default_level) == 0) {
126 1.1 jmcneill /* set the default level now */
127 1.1 jmcneill pwm_backlight_set(sc, default_level);
128 1.1 jmcneill }
129 1.1 jmcneill
130 1.1 jmcneill pwm_backlight_sysctl_init(sc);
131 1.1 jmcneill pwm_backlight_pmf_init(sc);
132 1.1 jmcneill }
133 1.1 jmcneill
134 1.1 jmcneill static void
135 1.1 jmcneill pwm_backlight_set(struct pwm_backlight_softc *sc, u_int index)
136 1.1 jmcneill {
137 1.1 jmcneill struct pwm_config conf;
138 1.1 jmcneill
139 1.1 jmcneill if (index >= sc->sc_nlevels)
140 1.1 jmcneill return;
141 1.1 jmcneill
142 1.1 jmcneill aprint_debug_dev(sc->sc_dev, "set duty cycle to %u%%\n", sc->sc_levels[index]);
143 1.1 jmcneill
144 1.1 jmcneill pwm_disable(sc->sc_pwm);
145 1.1 jmcneill pwm_get_config(sc->sc_pwm, &conf);
146 1.1 jmcneill conf.duty_cycle = (conf.period * sc->sc_levels[index]) / sc->sc_levels[sc->sc_nlevels - 1];
147 1.1 jmcneill pwm_set_config(sc->sc_pwm, &conf);
148 1.1 jmcneill pwm_enable(sc->sc_pwm);
149 1.1 jmcneill }
150 1.1 jmcneill
151 1.1 jmcneill static u_int
152 1.1 jmcneill pwm_backlight_get(struct pwm_backlight_softc *sc)
153 1.1 jmcneill {
154 1.1 jmcneill struct pwm_config conf;
155 1.1 jmcneill u_int raw_val, n;
156 1.1 jmcneill
157 1.1 jmcneill pwm_get_config(sc->sc_pwm, &conf);
158 1.1 jmcneill
159 1.1 jmcneill raw_val = (conf.duty_cycle * sc->sc_levels[sc->sc_nlevels - 1]) / conf.period;
160 1.1 jmcneill
161 1.1 jmcneill /* Return the closest setting to the raw value */
162 1.1 jmcneill for (n = 0; n < sc->sc_nlevels; n++) {
163 1.1 jmcneill if (raw_val <= sc->sc_levels[n])
164 1.1 jmcneill break;
165 1.1 jmcneill }
166 1.1 jmcneill return n;
167 1.1 jmcneill }
168 1.1 jmcneill
169 1.1 jmcneill static int
170 1.1 jmcneill pwm_backlight_sysctl_helper(SYSCTLFN_ARGS)
171 1.1 jmcneill {
172 1.1 jmcneill struct pwm_backlight_softc * const sc = rnode->sysctl_data;
173 1.1 jmcneill struct sysctlnode node;
174 1.1 jmcneill int error;
175 1.1 jmcneill u_int level, n;
176 1.1 jmcneill
177 1.1 jmcneill node = *rnode;
178 1.1 jmcneill node.sysctl_data = &level;
179 1.1 jmcneill
180 1.1 jmcneill n = pwm_backlight_get(sc);
181 1.1 jmcneill level = sc->sc_levels[n];
182 1.1 jmcneill
183 1.1 jmcneill error = sysctl_lookup(SYSCTLFN_CALL(&node));
184 1.1 jmcneill if (error || newp == NULL)
185 1.1 jmcneill return error;
186 1.1 jmcneill
187 1.1 jmcneill for (n = 0; n < sc->sc_nlevels; n++) {
188 1.1 jmcneill if (sc->sc_levels[n] == level) {
189 1.1 jmcneill pwm_backlight_set(sc, n);
190 1.1 jmcneill return 0;
191 1.1 jmcneill }
192 1.1 jmcneill }
193 1.1 jmcneill
194 1.1 jmcneill return EINVAL;
195 1.1 jmcneill }
196 1.1 jmcneill
197 1.1 jmcneill static void
198 1.1 jmcneill pwm_backlight_sysctl_init(struct pwm_backlight_softc *sc)
199 1.1 jmcneill {
200 1.1 jmcneill const struct sysctlnode *node, *pwmnode;
201 1.1 jmcneill struct sysctllog *log = NULL;
202 1.1 jmcneill int error;
203 1.1 jmcneill u_int n;
204 1.1 jmcneill
205 1.1 jmcneill sc->sc_levelstr = kmem_zalloc(strlen("XXXXX ") * sc->sc_nlevels, KM_SLEEP);
206 1.1 jmcneill for (n = 0; n < sc->sc_nlevels; n++) {
207 1.1 jmcneill char buf[7];
208 1.1 jmcneill snprintf(buf, sizeof(buf), n ? " %u" : "%u", sc->sc_levels[n]);
209 1.1 jmcneill strcat(sc->sc_levelstr, buf);
210 1.1 jmcneill }
211 1.1 jmcneill
212 1.1 jmcneill error = sysctl_createv(&log, 0, NULL, &node,
213 1.1 jmcneill CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
214 1.1 jmcneill NULL, 0, NULL, 0, CTL_HW, CTL_EOL);
215 1.1 jmcneill if (error)
216 1.1 jmcneill goto failed;
217 1.1 jmcneill
218 1.1 jmcneill error = sysctl_createv(&log, 0, &node, &pwmnode,
219 1.1 jmcneill 0, CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
220 1.1 jmcneill NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
221 1.1 jmcneill if (error)
222 1.1 jmcneill goto failed;
223 1.1 jmcneill
224 1.1 jmcneill error = sysctl_createv(&log, 0, &pwmnode, NULL,
225 1.1 jmcneill 0, CTLTYPE_STRING, "levels", NULL,
226 1.3 jmcneill NULL, 0, sc->sc_levelstr, 0,
227 1.1 jmcneill CTL_CREATE, CTL_EOL);
228 1.1 jmcneill if (error)
229 1.1 jmcneill goto failed;
230 1.1 jmcneill
231 1.1 jmcneill error = sysctl_createv(&log, 0, &pwmnode, NULL,
232 1.1 jmcneill CTLFLAG_READWRITE, CTLTYPE_INT, "level", NULL,
233 1.1 jmcneill pwm_backlight_sysctl_helper, 0, (void *)sc, 0,
234 1.1 jmcneill CTL_CREATE, CTL_EOL);
235 1.1 jmcneill if (error)
236 1.1 jmcneill goto failed;
237 1.1 jmcneill
238 1.1 jmcneill return;
239 1.1 jmcneill
240 1.1 jmcneill failed:
241 1.1 jmcneill aprint_error_dev(sc->sc_dev, "couldn't create sysctl nodes: %d\n", error);
242 1.1 jmcneill sysctl_teardown(&log);
243 1.1 jmcneill }
244 1.1 jmcneill
245 1.1 jmcneill static void
246 1.1 jmcneill pwm_backlight_display_on(device_t dev)
247 1.1 jmcneill {
248 1.1 jmcneill struct pwm_backlight_softc * const sc = device_private(dev);
249 1.1 jmcneill
250 1.2 jmcneill if (sc->sc_pin)
251 1.2 jmcneill fdtbus_gpio_write(sc->sc_pin, 1);
252 1.1 jmcneill }
253 1.1 jmcneill
254 1.1 jmcneill static void
255 1.1 jmcneill pwm_backlight_display_off(device_t dev)
256 1.1 jmcneill {
257 1.1 jmcneill struct pwm_backlight_softc * const sc = device_private(dev);
258 1.1 jmcneill
259 1.2 jmcneill if (sc->sc_pin)
260 1.2 jmcneill fdtbus_gpio_write(sc->sc_pin, 0);
261 1.1 jmcneill }
262 1.1 jmcneill
263 1.1 jmcneill static void
264 1.1 jmcneill pwm_backlight_display_brightness_up(device_t dev)
265 1.1 jmcneill {
266 1.1 jmcneill struct pwm_backlight_softc * const sc = device_private(dev);
267 1.1 jmcneill u_int n;
268 1.1 jmcneill
269 1.1 jmcneill n = pwm_backlight_get(sc);
270 1.1 jmcneill if (n < sc->sc_nlevels - 1)
271 1.1 jmcneill pwm_backlight_set(sc, n + 1);
272 1.1 jmcneill }
273 1.1 jmcneill
274 1.1 jmcneill static void
275 1.1 jmcneill pwm_backlight_display_brightness_down(device_t dev)
276 1.1 jmcneill {
277 1.1 jmcneill struct pwm_backlight_softc * const sc = device_private(dev);
278 1.1 jmcneill u_int n;
279 1.1 jmcneill
280 1.1 jmcneill n = pwm_backlight_get(sc);
281 1.1 jmcneill if (n > 0)
282 1.1 jmcneill pwm_backlight_set(sc, n - 1);
283 1.1 jmcneill }
284 1.1 jmcneill
285 1.1 jmcneill static void
286 1.1 jmcneill pwm_backlight_pmf_init(struct pwm_backlight_softc *sc)
287 1.1 jmcneill {
288 1.1 jmcneill pmf_event_register(sc->sc_dev, PMFE_DISPLAY_ON,
289 1.1 jmcneill pwm_backlight_display_on, true);
290 1.1 jmcneill pmf_event_register(sc->sc_dev, PMFE_DISPLAY_OFF,
291 1.1 jmcneill pwm_backlight_display_off, true);
292 1.1 jmcneill pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_UP,
293 1.1 jmcneill pwm_backlight_display_brightness_up, true);
294 1.1 jmcneill pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_DOWN,
295 1.1 jmcneill pwm_backlight_display_brightness_down, true);
296 1.1 jmcneill }
297