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