lmu.c revision 1.8.4.2 1 /* $NetBSD: lmu.c,v 1.8.4.2 2021/05/13 00:59:26 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2020 Michael Lorenz
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * ambient light controller found in PowerBook5,6
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: lmu.c,v 1.8.4.2 2021/05/13 00:59:26 thorpej Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/conf.h>
40 #include <sys/bus.h>
41 #include <sys/time.h>
42 #include <sys/callout.h>
43 #include <sys/sysctl.h>
44
45 #include <dev/i2c/i2cvar.h>
46
47 #include <dev/sysmon/sysmonvar.h>
48
49 #ifdef LMU_DEBUG
50 #define DPRINTF printf
51 #else
52 #define DPRINTF if (0) printf
53 #endif
54
55 struct lmu_softc {
56 device_t sc_dev;
57 i2c_tag_t sc_i2c;
58 i2c_addr_t sc_addr;
59
60 struct sysmon_envsys *sc_sme;
61 envsys_data_t sc_sensors[2];
62 callout_t sc_adjust;
63 int sc_thresh, sc_hyst, sc_level, sc_target, sc_current;
64 int sc_lux[2];
65 time_t sc_last;
66 int sc_lid_state, sc_video_state;
67 };
68
69 static int lmu_match(device_t, cfdata_t, void *);
70 static void lmu_attach(device_t, device_t, void *);
71
72 static void lmu_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
73 static void lmu_set_brightness(struct lmu_softc *, int);
74 static int lmu_get_brightness(struct lmu_softc *, int);
75 static void lmu_adjust(void *);
76 static int lmu_sysctl(SYSCTLFN_ARGS);
77 static int lmu_sysctl_thresh(SYSCTLFN_ARGS);
78
79 CFATTACH_DECL_NEW(lmu, sizeof(struct lmu_softc),
80 lmu_match, lmu_attach, NULL, NULL);
81
82 static const struct device_compatible_entry compat_data[] = {
83 { .compat = "lmu-micro" },
84 { .compat = "lmu-controller" },
85 DEVICE_COMPAT_EOL
86 };
87
88 /* time between polling the light sensors */
89 #define LMU_POLL (hz * 2)
90 /* time between updates to keyboard brightness */
91 #define LMU_FADE (hz / 16)
92
93 static void
94 lmu_lid_open(device_t dev)
95 {
96 struct lmu_softc * const sc = device_private(dev);
97
98 sc->sc_lid_state = true;
99 }
100
101 static void
102 lmu_lid_close(device_t dev)
103 {
104 struct lmu_softc * const sc = device_private(dev);
105
106 sc->sc_lid_state = false;
107 }
108
109 static void
110 lmu_video_on(device_t dev)
111 {
112 struct lmu_softc * const sc = device_private(dev);
113
114 sc->sc_video_state = true;
115 }
116
117 static void
118 lmu_video_off(device_t dev)
119 {
120 struct lmu_softc * const sc = device_private(dev);
121
122 sc->sc_video_state = false;
123 }
124
125 static void
126 lmu_kbd_brightness_up(device_t dev)
127 {
128 struct lmu_softc * const sc = device_private(dev);
129
130 sc->sc_level = __MIN(16, sc->sc_level + 2);
131 sc->sc_target = sc->sc_level;
132 callout_schedule(&sc->sc_adjust, LMU_FADE);
133 }
134
135 static void
136 lmu_kbd_brightness_down(device_t dev)
137 {
138 struct lmu_softc * const sc = device_private(dev);
139
140 sc->sc_level = __MAX(0, sc->sc_level - 2);
141 sc->sc_target = sc->sc_level;
142 callout_schedule(&sc->sc_adjust, LMU_FADE);
143 }
144
145 static int
146 lmu_match(device_t parent, cfdata_t match, void *aux)
147 {
148 struct i2c_attach_args *ia = aux;
149 int match_result;
150
151 if (iic_use_direct_match(ia, match, compat_data, &match_result))
152 return match_result;
153
154 return 0;
155 }
156
157 static void
158 lmu_attach(device_t parent, device_t self, void *aux)
159 {
160 struct lmu_softc *sc = device_private(self);
161 struct i2c_attach_args *ia = aux;
162 envsys_data_t *s;
163 const struct sysctlnode *me;
164
165 sc->sc_dev = self;
166 sc->sc_i2c = ia->ia_tag;
167 sc->sc_addr = ia->ia_addr;
168 sc->sc_last = 0;
169
170 aprint_naive("\n");
171 aprint_normal(": ambient light sensor\n");
172
173 sc->sc_lid_state = true;
174 pmf_event_register(sc->sc_dev, PMFE_CHASSIS_LID_OPEN,
175 lmu_lid_open, true);
176 pmf_event_register(sc->sc_dev, PMFE_CHASSIS_LID_CLOSE,
177 lmu_lid_close, true);
178 sc->sc_video_state = true;
179 pmf_event_register(sc->sc_dev, PMFE_DISPLAY_ON,
180 lmu_video_on, true);
181 pmf_event_register(sc->sc_dev, PMFE_DISPLAY_OFF,
182 lmu_video_off, true);
183 pmf_event_register(sc->sc_dev, PMFE_KEYBOARD_BRIGHTNESS_UP,
184 lmu_kbd_brightness_up, true);
185 pmf_event_register(sc->sc_dev, PMFE_KEYBOARD_BRIGHTNESS_DOWN,
186 lmu_kbd_brightness_down, true);
187
188 sc->sc_sme = sysmon_envsys_create();
189 sc->sc_sme->sme_name = device_xname(self);
190 sc->sc_sme->sme_cookie = sc;
191 sc->sc_sme->sme_refresh = lmu_sensors_refresh;
192
193 s = &sc->sc_sensors[0];
194 s->state = ENVSYS_SINVALID;
195 s->units = ENVSYS_LUX;
196 strcpy(s->desc, "right");
197 s->private = 0;
198 sysmon_envsys_sensor_attach(sc->sc_sme, s);
199
200 s = &sc->sc_sensors[1];
201 s->state = ENVSYS_SINVALID;
202 s->units = ENVSYS_LUX;
203 strcpy(s->desc, "left");
204 s->private = 1;
205 sysmon_envsys_sensor_attach(sc->sc_sme, s);
206
207 sysmon_envsys_register(sc->sc_sme);
208
209 /* TODO: make this adjustable via sysctl */
210 sc->sc_thresh = 300;
211 sc->sc_hyst = 30;
212 sc->sc_level = 16;
213 sc->sc_target = 0;
214 sc->sc_current = 0;
215
216 sysctl_createv(NULL, 0, NULL, &me,
217 CTLFLAG_READWRITE,
218 CTLTYPE_NODE, "lmu",
219 SYSCTL_DESCR("LMU driver"),
220 NULL, 0, NULL, 0,
221 CTL_HW, CTL_CREATE, CTL_EOL);
222
223 sysctl_createv(NULL, 0, NULL, NULL,
224 CTLFLAG_READWRITE,
225 CTLTYPE_INT, "level",
226 SYSCTL_DESCR("keyboard brightness"),
227 lmu_sysctl, 0, (void *)sc, 0,
228 CTL_HW, me->sysctl_num, CTL_CREATE, CTL_EOL);
229
230 sysctl_createv(NULL, 0, NULL, NULL,
231 CTLFLAG_READWRITE,
232 CTLTYPE_INT, "threshold",
233 SYSCTL_DESCR("environmental light threshold"),
234 lmu_sysctl_thresh, 1, (void *)sc, 0,
235 CTL_HW, me->sysctl_num, CTL_CREATE, CTL_EOL);
236
237 callout_init(&sc->sc_adjust, 0);
238 callout_setfunc(&sc->sc_adjust, lmu_adjust, sc);
239 callout_schedule(&sc->sc_adjust, 0);
240 }
241
242 static void
243 lmu_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
244 {
245 struct lmu_softc *sc = sme->sme_cookie;
246 int ret;
247
248 if ( edata->private < 3) {
249 ret = lmu_get_brightness(sc, edata->private);
250 if (ret == -1) return;
251 edata->value_cur = ret;
252 }
253 edata->state = ENVSYS_SVALID;
254 }
255
256 static int
257 lmu_get_brightness(struct lmu_softc *sc, int reg)
258 {
259 int error, i;
260 uint16_t buf[2];
261 uint8_t cmd = 0;
262
263 if (reg > 1) return -1;
264 if (time_second == sc->sc_last)
265 return sc->sc_lux[reg];
266
267 iic_acquire_bus(sc->sc_i2c, 0);
268 error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
269 sc->sc_addr, &cmd, 1, buf, 4, 0);
270 iic_release_bus(sc->sc_i2c, 0);
271 if (error) return -1;
272 sc->sc_last = time_second;
273
274 for (i = 0; i < 2; i++)
275 sc->sc_lux[i] = be16toh(buf[i]);
276
277 DPRINTF("<%d %04x %04x>", reg, buf[0], buf[1]);
278
279 return (sc->sc_lux[reg]);
280 }
281
282 static void
283 lmu_set_brightness(struct lmu_softc *sc, int b)
284 {
285 uint8_t cmd[3];
286
287 cmd[0] = 1;
288
289 cmd[1] = (b & 0xff);
290 cmd[2] = (b & 0xff) >> 8;
291
292 iic_acquire_bus(sc->sc_i2c, 0);
293 iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
294 sc->sc_addr, &cmd, 3, NULL, 0, 0);
295 iic_release_bus(sc->sc_i2c, 0);
296 }
297
298 static void
299 lmu_adjust(void *cookie)
300 {
301 struct lmu_softc *sc = cookie;
302 int left, right, b, offset;
303
304 left = lmu_get_brightness(sc, 1);
305 right = lmu_get_brightness(sc, 0);
306 b = left > right ? left : right;
307
308 if ((b > (sc->sc_thresh + sc->sc_hyst)) ||
309 !(sc->sc_lid_state && sc->sc_video_state)) {
310 sc->sc_target = 0;
311 } else if (b < sc->sc_thresh) {
312 sc->sc_target = sc->sc_level;
313 }
314
315 if (sc->sc_target == sc->sc_current) {
316 /* no update needed, check again later */
317 callout_schedule(&sc->sc_adjust, LMU_POLL);
318 return;
319 }
320
321
322 offset = ((sc->sc_target - sc->sc_current) > 0) ? 2 : -2;
323 sc->sc_current += offset;
324 if (sc->sc_current > sc->sc_level) sc->sc_current = sc->sc_level;
325 if (sc->sc_current < 0) sc->sc_current = 0;
326
327 DPRINTF("[%d]", sc->sc_current);
328
329 lmu_set_brightness(sc, sc->sc_current);
330
331 if (sc->sc_target == sc->sc_current) {
332 /* no update needed, check again later */
333 callout_schedule(&sc->sc_adjust, LMU_POLL);
334 return;
335 }
336
337 /* more updates upcoming */
338 callout_schedule(&sc->sc_adjust, LMU_FADE);
339 }
340
341 static int
342 lmu_sysctl(SYSCTLFN_ARGS)
343 {
344 struct sysctlnode node = *rnode;
345 struct lmu_softc *sc = node.sysctl_data;
346 int target;
347
348 target = sc->sc_level;
349 node.sysctl_data = ⌖
350 if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
351 int new_reg;
352
353 new_reg = *(int *)node.sysctl_data;
354 if (new_reg != sc->sc_target) {
355 sc->sc_level = target;
356 sc->sc_target = target;
357
358 }
359 return 0;
360 }
361 return EINVAL;
362 }
363
364 static int
365 lmu_sysctl_thresh(SYSCTLFN_ARGS)
366 {
367 struct sysctlnode node = *rnode;
368 struct lmu_softc *sc = node.sysctl_data;
369 int thresh;
370
371 thresh = sc->sc_thresh;
372 node.sysctl_data = &thresh;
373 if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
374 int new_reg;
375
376 new_reg = *(int *)node.sysctl_data;
377 if (new_reg != sc->sc_thresh && new_reg > 0) {
378 sc->sc_thresh = new_reg;
379 }
380 return 0;
381 }
382 return EINVAL;
383 }
384