lmu.c revision 1.10 1 /* $NetBSD: lmu.c,v 1.10 2025/07/06 23:56:38 macallan 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.10 2025/07/06 23:56:38 macallan 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/kthread.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 int sc_node;
61
62 struct sysmon_envsys *sc_sme;
63 envsys_data_t sc_sensors[2];
64 lwp_t *sc_adjust;
65 kcondvar_t sc_event;
66 kmutex_t sc_evmtx;
67 int sc_thresh, sc_hyst, sc_level, sc_target, sc_current;
68 int sc_lux[2];
69 time_t sc_last;
70 int sc_lid_state, sc_video_state;
71 };
72
73 static int lmu_match(device_t, cfdata_t, void *);
74 static void lmu_attach(device_t, device_t, void *);
75
76 static void lmu_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
77 static void lmu_set_brightness(struct lmu_softc *, int);
78 static int lmu_get_brightness(struct lmu_softc *, int);
79 static void lmu_adjust(void *);
80 static int lmu_sysctl(SYSCTLFN_ARGS);
81 static int lmu_sysctl_thresh(SYSCTLFN_ARGS);
82
83 CFATTACH_DECL_NEW(lmu, sizeof(struct lmu_softc),
84 lmu_match, lmu_attach, NULL, NULL);
85
86 static const struct device_compatible_entry compat_data[] = {
87 { .compat = "lmu-micro" },
88 { .compat = "lmu-controller" },
89 DEVICE_COMPAT_EOL
90 };
91
92 /* time between polling the light sensors */
93 #define LMU_POLL (hz)
94 /* time between updates to keyboard brightness */
95 #define LMU_FADE (hz / 16)
96
97 static void
98 lmu_lid_open(device_t dev)
99 {
100 struct lmu_softc * const sc = device_private(dev);
101
102 sc->sc_lid_state = true;
103 }
104
105 static void
106 lmu_lid_close(device_t dev)
107 {
108 struct lmu_softc * const sc = device_private(dev);
109
110 sc->sc_lid_state = false;
111 }
112
113 static void
114 lmu_video_on(device_t dev)
115 {
116 struct lmu_softc * const sc = device_private(dev);
117
118 sc->sc_video_state = true;
119 }
120
121 static void
122 lmu_video_off(device_t dev)
123 {
124 struct lmu_softc * const sc = device_private(dev);
125
126 sc->sc_video_state = false;
127 }
128
129 static void
130 lmu_kbd_brightness_up(device_t dev)
131 {
132 struct lmu_softc * const sc = device_private(dev);
133
134 sc->sc_level = __MIN(16, sc->sc_level + 2);
135 sc->sc_target = sc->sc_level;
136 cv_signal(&sc->sc_event);
137 }
138
139 static void
140 lmu_kbd_brightness_down(device_t dev)
141 {
142 struct lmu_softc * const sc = device_private(dev);
143
144 sc->sc_level = __MAX(0, sc->sc_level - 2);
145 sc->sc_target = sc->sc_level;
146 cv_signal(&sc->sc_event);
147 }
148
149 static int
150 lmu_match(device_t parent, cfdata_t match, void *aux)
151 {
152 struct i2c_attach_args *ia = aux;
153 int match_result;
154
155 if (iic_use_direct_match(ia, match, compat_data, &match_result))
156 return match_result;
157
158 return 0;
159 }
160
161 static void
162 lmu_attach(device_t parent, device_t self, void *aux)
163 {
164 struct lmu_softc *sc = device_private(self);
165 struct i2c_attach_args *ia = aux;
166 envsys_data_t *s;
167 const struct sysctlnode *me;
168
169 sc->sc_dev = self;
170 sc->sc_i2c = ia->ia_tag;
171 sc->sc_addr = ia->ia_addr;
172 sc->sc_node = ia->ia_cookie;
173 sc->sc_last = 0;
174
175 aprint_naive("\n");
176 aprint_normal(": ambient light sensor\n");
177
178 sc->sc_lid_state = true;
179 pmf_event_register(sc->sc_dev, PMFE_CHASSIS_LID_OPEN,
180 lmu_lid_open, true);
181 pmf_event_register(sc->sc_dev, PMFE_CHASSIS_LID_CLOSE,
182 lmu_lid_close, true);
183 sc->sc_video_state = true;
184 pmf_event_register(sc->sc_dev, PMFE_DISPLAY_ON,
185 lmu_video_on, true);
186 pmf_event_register(sc->sc_dev, PMFE_DISPLAY_OFF,
187 lmu_video_off, true);
188 pmf_event_register(sc->sc_dev, PMFE_KEYBOARD_BRIGHTNESS_UP,
189 lmu_kbd_brightness_up, true);
190 pmf_event_register(sc->sc_dev, PMFE_KEYBOARD_BRIGHTNESS_DOWN,
191 lmu_kbd_brightness_down, true);
192
193 sc->sc_sme = sysmon_envsys_create();
194 sc->sc_sme->sme_name = device_xname(self);
195 sc->sc_sme->sme_cookie = sc;
196 sc->sc_sme->sme_refresh = lmu_sensors_refresh;
197 sc->sc_sme->sme_class = 0;
198 sc->sc_sme->sme_flags = SME_INIT_REFRESH;;
199
200
201 s = &sc->sc_sensors[0];
202 s->state = ENVSYS_SINVALID;
203 s->units = ENVSYS_LUX;
204 s->flags = ENVSYS_FHAS_ENTROPY;
205 strcpy(s->desc, "right");
206 s->private = 0;
207 sysmon_envsys_sensor_attach(sc->sc_sme, s);
208
209 s = &sc->sc_sensors[1];
210 s->state = ENVSYS_SINVALID;
211 s->units = ENVSYS_LUX;
212 s->flags = ENVSYS_FHAS_ENTROPY;
213 strcpy(s->desc, "left");
214 s->private = 1;
215 sysmon_envsys_sensor_attach(sc->sc_sme, s);
216
217 sysmon_envsys_register(sc->sc_sme);
218
219 sc->sc_thresh = 300;
220 sc->sc_hyst = 30;
221 sc->sc_level = 16;
222 sc->sc_target = 0;
223 sc->sc_current = 0;
224
225 sysctl_createv(NULL, 0, NULL, &me,
226 CTLFLAG_READWRITE,
227 CTLTYPE_NODE, "lmu",
228 SYSCTL_DESCR("LMU driver"),
229 NULL, 0, NULL, 0,
230 CTL_HW, CTL_CREATE, CTL_EOL);
231
232 sysctl_createv(NULL, 0, NULL, NULL,
233 CTLFLAG_READWRITE,
234 CTLTYPE_INT, "level",
235 SYSCTL_DESCR("keyboard brightness"),
236 lmu_sysctl, 0, (void *)sc, 0,
237 CTL_HW, me->sysctl_num, CTL_CREATE, CTL_EOL);
238
239 sysctl_createv(NULL, 0, NULL, NULL,
240 CTLFLAG_READWRITE,
241 CTLTYPE_INT, "threshold",
242 SYSCTL_DESCR("environmental light threshold"),
243 lmu_sysctl_thresh, 1, (void *)sc, 0,
244 CTL_HW, me->sysctl_num, CTL_CREATE, CTL_EOL);
245
246 cv_init(&sc->sc_event, "lmu_event");
247 mutex_init(&sc->sc_evmtx, MUTEX_DEFAULT, IPL_NONE);
248
249 if (kthread_create(PRI_NONE, 0, NULL, lmu_adjust, sc, &sc->sc_adjust,
250 "%s", "lmu") != 0) {
251 aprint_error_dev(self, "unable to create event kthread\n");
252 }
253 }
254
255 static void
256 lmu_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
257 {
258 struct lmu_softc *sc = sme->sme_cookie;
259 int ret;
260
261 edata->state = ENVSYS_SINVALID;
262 if ( edata->private < 3) {
263 ret = lmu_get_brightness(sc, edata->private);
264 if (ret == -1) return;
265 edata->value_cur = ret;
266 }
267 edata->state = ENVSYS_SVALID;
268 }
269
270 static int
271 lmu_get_brightness(struct lmu_softc *sc, int reg)
272 {
273 int error, i;
274 uint16_t buf[2];
275 uint8_t cmd = 0;
276
277 if (reg > 1) return -1;
278 if (time_second == sc->sc_last)
279 return sc->sc_lux[reg];
280
281 iic_acquire_bus(sc->sc_i2c, 0);
282 error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
283 sc->sc_addr, &cmd, 1, buf, 4, 0);
284 iic_release_bus(sc->sc_i2c, 0);
285
286 if (error) return -1;
287 sc->sc_last = time_second;
288
289 for (i = 0; i < 2; i++)
290 sc->sc_lux[i] = be16toh(buf[i]);
291
292 DPRINTF("<%d %04x %04x>", reg, buf[0], buf[1]);
293
294 return (sc->sc_lux[reg]);
295 }
296
297 static void
298 lmu_set_brightness(struct lmu_softc *sc, int b)
299 {
300 uint8_t cmd[3];
301
302 cmd[0] = 1;
303
304 cmd[1] = (b & 0xff);
305 cmd[2] = (b & 0xff) >> 8;
306
307 iic_acquire_bus(sc->sc_i2c, 0);
308 iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
309 sc->sc_addr, &cmd, 3, NULL, 0, 0);
310 iic_release_bus(sc->sc_i2c, 0);
311 }
312
313 static void
314 lmu_adjust(void *cookie)
315 {
316 struct lmu_softc *sc = cookie;
317 int left, right, b, offset, ticks;
318
319 while (1) {
320 ticks = LMU_POLL;
321 left = lmu_get_brightness(sc, 1);
322 right = lmu_get_brightness(sc, 0);
323 b = left > right ? left : right;
324
325 if ((b > (sc->sc_thresh + sc->sc_hyst)) ||
326 !(sc->sc_lid_state && sc->sc_video_state)) {
327 sc->sc_target = 0;
328 } else if (b < sc->sc_thresh) {
329 sc->sc_target = sc->sc_level;
330 }
331
332 if (sc->sc_target != sc->sc_current) {
333 offset = ((sc->sc_target - sc->sc_current) > 0) ? 2 : -2;
334 sc->sc_current += offset;
335 if (sc->sc_current > sc->sc_level) sc->sc_current = sc->sc_level;
336 if (sc->sc_current < 0) sc->sc_current = 0;
337
338 DPRINTF("[%d]", sc->sc_current);
339 lmu_set_brightness(sc, sc->sc_current);
340
341 if (sc->sc_target != sc->sc_current) ticks = LMU_FADE;
342 }
343
344 mutex_enter(&sc->sc_evmtx);
345 cv_timedwait(&sc->sc_event, &sc->sc_evmtx, ticks);
346 mutex_exit(&sc->sc_evmtx);
347 }
348 }
349
350 static int
351 lmu_sysctl(SYSCTLFN_ARGS)
352 {
353 struct sysctlnode node = *rnode;
354 struct lmu_softc *sc = node.sysctl_data;
355 int target;
356
357 target = sc->sc_level;
358 node.sysctl_data = ⌖
359 if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
360 int new_reg;
361
362 new_reg = *(int *)node.sysctl_data;
363 if (new_reg != sc->sc_target) {
364 sc->sc_level = target;
365 sc->sc_target = target;
366
367 }
368 return 0;
369 }
370 return EINVAL;
371 }
372
373 static int
374 lmu_sysctl_thresh(SYSCTLFN_ARGS)
375 {
376 struct sysctlnode node = *rnode;
377 struct lmu_softc *sc = node.sysctl_data;
378 int thresh;
379
380 thresh = sc->sc_thresh;
381 node.sysctl_data = &thresh;
382 if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
383 int new_reg;
384
385 new_reg = *(int *)node.sysctl_data;
386 if (new_reg != sc->sc_thresh && new_reg > 0) {
387 sc->sc_thresh = new_reg;
388 }
389 return 0;
390 }
391 return EINVAL;
392 }
393