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