pwm_backlight.c revision 1.1 1 1.1 jmcneill /* $NetBSD: pwm_backlight.c,v 1.1 2018/05/06 10:33:21 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.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: pwm_backlight.c,v 1.1 2018/05/06 10:33:21 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.1 jmcneill }
104 1.1 jmcneill
105 1.1 jmcneill levels = fdtbus_get_prop(phandle, "brightness-levels", &len);
106 1.1 jmcneill if (len < 4) {
107 1.1 jmcneill aprint_error(": couldn't get 'brightness-levels' property\n");
108 1.1 jmcneill return;
109 1.1 jmcneill }
110 1.1 jmcneill sc->sc_levels = kmem_alloc(len, KM_SLEEP);
111 1.1 jmcneill sc->sc_nlevels = len / 4;
112 1.1 jmcneill for (n = 0; n < sc->sc_nlevels; n++)
113 1.1 jmcneill sc->sc_levels[n] = be32toh(levels[n]);
114 1.1 jmcneill
115 1.1 jmcneill aprint_naive("\n");
116 1.1 jmcneill aprint_normal(": PWM Backlight");
117 1.1 jmcneill aprint_verbose(" <");
118 1.1 jmcneill for (n = 0; n < sc->sc_nlevels; n++) {
119 1.1 jmcneill aprint_verbose("%s%u", n ? " " : "", sc->sc_levels[n]);
120 1.1 jmcneill }
121 1.1 jmcneill aprint_verbose(">");
122 1.1 jmcneill aprint_normal("\n");
123 1.1 jmcneill
124 1.1 jmcneill if (of_getprop_uint32(phandle, "default-brightness-level", &default_level) == 0) {
125 1.1 jmcneill /* set the default level now */
126 1.1 jmcneill pwm_backlight_set(sc, default_level);
127 1.1 jmcneill }
128 1.1 jmcneill
129 1.1 jmcneill pwm_backlight_sysctl_init(sc);
130 1.1 jmcneill pwm_backlight_pmf_init(sc);
131 1.1 jmcneill }
132 1.1 jmcneill
133 1.1 jmcneill static void
134 1.1 jmcneill pwm_backlight_set(struct pwm_backlight_softc *sc, u_int index)
135 1.1 jmcneill {
136 1.1 jmcneill struct pwm_config conf;
137 1.1 jmcneill
138 1.1 jmcneill if (index >= sc->sc_nlevels)
139 1.1 jmcneill return;
140 1.1 jmcneill
141 1.1 jmcneill aprint_debug_dev(sc->sc_dev, "set duty cycle to %u%%\n", sc->sc_levels[index]);
142 1.1 jmcneill
143 1.1 jmcneill pwm_disable(sc->sc_pwm);
144 1.1 jmcneill pwm_get_config(sc->sc_pwm, &conf);
145 1.1 jmcneill conf.duty_cycle = (conf.period * sc->sc_levels[index]) / sc->sc_levels[sc->sc_nlevels - 1];
146 1.1 jmcneill pwm_set_config(sc->sc_pwm, &conf);
147 1.1 jmcneill pwm_enable(sc->sc_pwm);
148 1.1 jmcneill }
149 1.1 jmcneill
150 1.1 jmcneill static u_int
151 1.1 jmcneill pwm_backlight_get(struct pwm_backlight_softc *sc)
152 1.1 jmcneill {
153 1.1 jmcneill struct pwm_config conf;
154 1.1 jmcneill u_int raw_val, n;
155 1.1 jmcneill
156 1.1 jmcneill pwm_get_config(sc->sc_pwm, &conf);
157 1.1 jmcneill
158 1.1 jmcneill raw_val = (conf.duty_cycle * sc->sc_levels[sc->sc_nlevels - 1]) / conf.period;
159 1.1 jmcneill
160 1.1 jmcneill /* Return the closest setting to the raw value */
161 1.1 jmcneill for (n = 0; n < sc->sc_nlevels; n++) {
162 1.1 jmcneill if (raw_val <= sc->sc_levels[n])
163 1.1 jmcneill break;
164 1.1 jmcneill }
165 1.1 jmcneill return n;
166 1.1 jmcneill }
167 1.1 jmcneill
168 1.1 jmcneill static int
169 1.1 jmcneill pwm_backlight_sysctl_helper(SYSCTLFN_ARGS)
170 1.1 jmcneill {
171 1.1 jmcneill struct pwm_backlight_softc * const sc = rnode->sysctl_data;
172 1.1 jmcneill struct sysctlnode node;
173 1.1 jmcneill int error;
174 1.1 jmcneill u_int level, n;
175 1.1 jmcneill
176 1.1 jmcneill node = *rnode;
177 1.1 jmcneill node.sysctl_data = &level;
178 1.1 jmcneill
179 1.1 jmcneill n = pwm_backlight_get(sc);
180 1.1 jmcneill level = sc->sc_levels[n];
181 1.1 jmcneill
182 1.1 jmcneill error = sysctl_lookup(SYSCTLFN_CALL(&node));
183 1.1 jmcneill if (error || newp == NULL)
184 1.1 jmcneill return error;
185 1.1 jmcneill
186 1.1 jmcneill for (n = 0; n < sc->sc_nlevels; n++) {
187 1.1 jmcneill if (sc->sc_levels[n] == level) {
188 1.1 jmcneill pwm_backlight_set(sc, n);
189 1.1 jmcneill return 0;
190 1.1 jmcneill }
191 1.1 jmcneill }
192 1.1 jmcneill
193 1.1 jmcneill return EINVAL;
194 1.1 jmcneill }
195 1.1 jmcneill
196 1.1 jmcneill static void
197 1.1 jmcneill pwm_backlight_sysctl_init(struct pwm_backlight_softc *sc)
198 1.1 jmcneill {
199 1.1 jmcneill const struct sysctlnode *node, *pwmnode;
200 1.1 jmcneill struct sysctllog *log = NULL;
201 1.1 jmcneill int error;
202 1.1 jmcneill u_int n;
203 1.1 jmcneill
204 1.1 jmcneill sc->sc_levelstr = kmem_zalloc(strlen("XXXXX ") * sc->sc_nlevels, KM_SLEEP);
205 1.1 jmcneill for (n = 0; n < sc->sc_nlevels; n++) {
206 1.1 jmcneill char buf[7];
207 1.1 jmcneill snprintf(buf, sizeof(buf), n ? " %u" : "%u", sc->sc_levels[n]);
208 1.1 jmcneill strcat(sc->sc_levelstr, buf);
209 1.1 jmcneill }
210 1.1 jmcneill
211 1.1 jmcneill error = sysctl_createv(&log, 0, NULL, &node,
212 1.1 jmcneill CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
213 1.1 jmcneill NULL, 0, NULL, 0, CTL_HW, CTL_EOL);
214 1.1 jmcneill if (error)
215 1.1 jmcneill goto failed;
216 1.1 jmcneill
217 1.1 jmcneill error = sysctl_createv(&log, 0, &node, &pwmnode,
218 1.1 jmcneill 0, CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
219 1.1 jmcneill NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
220 1.1 jmcneill if (error)
221 1.1 jmcneill goto failed;
222 1.1 jmcneill
223 1.1 jmcneill error = sysctl_createv(&log, 0, &pwmnode, NULL,
224 1.1 jmcneill 0, CTLTYPE_STRING, "levels", NULL,
225 1.1 jmcneill NULL, 0, sc->sc_levelstr, NULL,
226 1.1 jmcneill CTL_CREATE, CTL_EOL);
227 1.1 jmcneill if (error)
228 1.1 jmcneill goto failed;
229 1.1 jmcneill
230 1.1 jmcneill error = sysctl_createv(&log, 0, &pwmnode, NULL,
231 1.1 jmcneill CTLFLAG_READWRITE, CTLTYPE_INT, "level", NULL,
232 1.1 jmcneill pwm_backlight_sysctl_helper, 0, (void *)sc, 0,
233 1.1 jmcneill CTL_CREATE, CTL_EOL);
234 1.1 jmcneill if (error)
235 1.1 jmcneill goto failed;
236 1.1 jmcneill
237 1.1 jmcneill return;
238 1.1 jmcneill
239 1.1 jmcneill failed:
240 1.1 jmcneill aprint_error_dev(sc->sc_dev, "couldn't create sysctl nodes: %d\n", error);
241 1.1 jmcneill sysctl_teardown(&log);
242 1.1 jmcneill }
243 1.1 jmcneill
244 1.1 jmcneill static void
245 1.1 jmcneill pwm_backlight_display_on(device_t dev)
246 1.1 jmcneill {
247 1.1 jmcneill struct pwm_backlight_softc * const sc = device_private(dev);
248 1.1 jmcneill
249 1.1 jmcneill pwm_enable(sc->sc_pwm);
250 1.1 jmcneill }
251 1.1 jmcneill
252 1.1 jmcneill static void
253 1.1 jmcneill pwm_backlight_display_off(device_t dev)
254 1.1 jmcneill {
255 1.1 jmcneill struct pwm_backlight_softc * const sc = device_private(dev);
256 1.1 jmcneill
257 1.1 jmcneill pwm_disable(sc->sc_pwm);
258 1.1 jmcneill }
259 1.1 jmcneill
260 1.1 jmcneill static void
261 1.1 jmcneill pwm_backlight_display_brightness_up(device_t dev)
262 1.1 jmcneill {
263 1.1 jmcneill struct pwm_backlight_softc * const sc = device_private(dev);
264 1.1 jmcneill u_int n;
265 1.1 jmcneill
266 1.1 jmcneill n = pwm_backlight_get(sc);
267 1.1 jmcneill if (n < sc->sc_nlevels - 1)
268 1.1 jmcneill pwm_backlight_set(sc, n + 1);
269 1.1 jmcneill }
270 1.1 jmcneill
271 1.1 jmcneill static void
272 1.1 jmcneill pwm_backlight_display_brightness_down(device_t dev)
273 1.1 jmcneill {
274 1.1 jmcneill struct pwm_backlight_softc * const sc = device_private(dev);
275 1.1 jmcneill u_int n;
276 1.1 jmcneill
277 1.1 jmcneill n = pwm_backlight_get(sc);
278 1.1 jmcneill if (n > 0)
279 1.1 jmcneill pwm_backlight_set(sc, n - 1);
280 1.1 jmcneill }
281 1.1 jmcneill
282 1.1 jmcneill static void
283 1.1 jmcneill pwm_backlight_pmf_init(struct pwm_backlight_softc *sc)
284 1.1 jmcneill {
285 1.1 jmcneill pmf_event_register(sc->sc_dev, PMFE_DISPLAY_ON,
286 1.1 jmcneill pwm_backlight_display_on, true);
287 1.1 jmcneill pmf_event_register(sc->sc_dev, PMFE_DISPLAY_OFF,
288 1.1 jmcneill pwm_backlight_display_off, true);
289 1.1 jmcneill pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_UP,
290 1.1 jmcneill pwm_backlight_display_brightness_up, true);
291 1.1 jmcneill pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_DOWN,
292 1.1 jmcneill pwm_backlight_display_brightness_down, true);
293 1.1 jmcneill }
294