lmu.c revision 1.9.6.1 1 /* $NetBSD: lmu.c,v 1.9.6.1 2021/08/09 00:30:08 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.9.6.1 2021/08/09 00:30:08 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 #include "opt_lmu.h"
49
50 #ifdef LMU_DEBUG
51 #define DPRINTF printf
52 #else
53 #define DPRINTF if (0) printf
54 #endif
55
56 struct lmu_softc {
57 device_t sc_dev;
58 i2c_tag_t sc_i2c;
59 i2c_addr_t sc_addr;
60
61 struct sysmon_envsys *sc_sme;
62 envsys_data_t sc_sensors[2];
63 callout_t sc_adjust;
64 int sc_thresh, sc_hyst, sc_level, sc_target, sc_current;
65 int sc_lux[2];
66 time_t sc_last;
67 int sc_lid_state, sc_video_state;
68 };
69
70 static int lmu_match(device_t, cfdata_t, void *);
71 static void lmu_attach(device_t, device_t, void *);
72
73 static void lmu_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
74 static void lmu_set_brightness(struct lmu_softc *, int);
75 static int lmu_get_brightness(struct lmu_softc *, int);
76 static void lmu_adjust(void *);
77 static int lmu_sysctl(SYSCTLFN_ARGS);
78 static int lmu_sysctl_thresh(SYSCTLFN_ARGS);
79
80 CFATTACH_DECL_NEW(lmu, sizeof(struct lmu_softc),
81 lmu_match, lmu_attach, NULL, NULL);
82
83 static const struct device_compatible_entry compat_data[] = {
84 { .compat = "lmu-micro" },
85 { .compat = "lmu-controller" },
86 DEVICE_COMPAT_EOL
87 };
88
89 /* time between polling the light sensors */
90 #define LMU_POLL (hz * 2)
91 /* time between updates to keyboard brightness */
92 #define LMU_FADE (hz / 16)
93
94 static void
95 lmu_lid_open(device_t dev)
96 {
97 struct lmu_softc * const sc = device_private(dev);
98
99 sc->sc_lid_state = true;
100 }
101
102 static void
103 lmu_lid_close(device_t dev)
104 {
105 struct lmu_softc * const sc = device_private(dev);
106
107 sc->sc_lid_state = false;
108 }
109
110 static void
111 lmu_video_on(device_t dev)
112 {
113 struct lmu_softc * const sc = device_private(dev);
114
115 sc->sc_video_state = true;
116 }
117
118 static void
119 lmu_video_off(device_t dev)
120 {
121 struct lmu_softc * const sc = device_private(dev);
122
123 sc->sc_video_state = false;
124 }
125
126 static void
127 lmu_kbd_brightness_up(device_t dev)
128 {
129 struct lmu_softc * const sc = device_private(dev);
130
131 sc->sc_level = __MIN(16, sc->sc_level + 2);
132 sc->sc_target = sc->sc_level;
133 callout_schedule(&sc->sc_adjust, LMU_FADE);
134 }
135
136 static void
137 lmu_kbd_brightness_down(device_t dev)
138 {
139 struct lmu_softc * const sc = device_private(dev);
140
141 sc->sc_level = __MAX(0, sc->sc_level - 2);
142 sc->sc_target = sc->sc_level;
143 callout_schedule(&sc->sc_adjust, LMU_FADE);
144 }
145
146 static int
147 lmu_match(device_t parent, cfdata_t match, void *aux)
148 {
149 struct i2c_attach_args *ia = aux;
150 int match_result;
151
152 if (iic_use_direct_match(ia, match, compat_data, &match_result))
153 return match_result;
154
155 return 0;
156 }
157
158 static void
159 lmu_attach(device_t parent, device_t self, void *aux)
160 {
161 struct lmu_softc *sc = device_private(self);
162 struct i2c_attach_args *ia = aux;
163 envsys_data_t *s;
164 const struct sysctlnode *me;
165
166 sc->sc_dev = self;
167 sc->sc_i2c = ia->ia_tag;
168 sc->sc_addr = ia->ia_addr;
169 sc->sc_last = 0;
170
171 aprint_naive("\n");
172 aprint_normal(": ambient light sensor\n");
173
174 sc->sc_lid_state = true;
175 pmf_event_register(sc->sc_dev, PMFE_CHASSIS_LID_OPEN,
176 lmu_lid_open, true);
177 pmf_event_register(sc->sc_dev, PMFE_CHASSIS_LID_CLOSE,
178 lmu_lid_close, true);
179 sc->sc_video_state = true;
180 pmf_event_register(sc->sc_dev, PMFE_DISPLAY_ON,
181 lmu_video_on, true);
182 pmf_event_register(sc->sc_dev, PMFE_DISPLAY_OFF,
183 lmu_video_off, true);
184 pmf_event_register(sc->sc_dev, PMFE_KEYBOARD_BRIGHTNESS_UP,
185 lmu_kbd_brightness_up, true);
186 pmf_event_register(sc->sc_dev, PMFE_KEYBOARD_BRIGHTNESS_DOWN,
187 lmu_kbd_brightness_down, true);
188
189 sc->sc_sme = sysmon_envsys_create();
190 sc->sc_sme->sme_name = device_xname(self);
191 sc->sc_sme->sme_cookie = sc;
192 sc->sc_sme->sme_refresh = lmu_sensors_refresh;
193
194 s = &sc->sc_sensors[0];
195 s->state = ENVSYS_SINVALID;
196 s->units = ENVSYS_LUX;
197 strcpy(s->desc, "right");
198 s->private = 0;
199 sysmon_envsys_sensor_attach(sc->sc_sme, s);
200
201 s = &sc->sc_sensors[1];
202 s->state = ENVSYS_SINVALID;
203 s->units = ENVSYS_LUX;
204 strcpy(s->desc, "left");
205 s->private = 1;
206 sysmon_envsys_sensor_attach(sc->sc_sme, s);
207
208 sysmon_envsys_register(sc->sc_sme);
209
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