Home | History | Annotate | Line # | Download | only in dev
fcu.c revision 1.4
      1  1.4  macallan /* $NetBSD: fcu.c,v 1.4 2021/07/30 22:07:14 macallan Exp $ */
      2  1.1  macallan 
      3  1.1  macallan /*-
      4  1.1  macallan  * Copyright (c) 2018 Michael Lorenz
      5  1.1  macallan  * All rights reserved.
      6  1.1  macallan  *
      7  1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8  1.1  macallan  * modification, are permitted provided that the following conditions
      9  1.1  macallan  * are met:
     10  1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11  1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12  1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15  1.1  macallan  *
     16  1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  macallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  macallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  macallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  macallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  macallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  macallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  macallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  macallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  macallan  */
     28  1.1  macallan 
     29  1.1  macallan #include <sys/cdefs.h>
     30  1.4  macallan __KERNEL_RCSID(0, "$NetBSD: fcu.c,v 1.4 2021/07/30 22:07:14 macallan Exp $");
     31  1.1  macallan 
     32  1.1  macallan #include <sys/param.h>
     33  1.1  macallan #include <sys/systm.h>
     34  1.1  macallan #include <sys/device.h>
     35  1.1  macallan #include <sys/conf.h>
     36  1.1  macallan #include <sys/bus.h>
     37  1.1  macallan #include <sys/kthread.h>
     38  1.4  macallan #include <sys/sysctl.h>
     39  1.1  macallan 
     40  1.1  macallan #include <dev/i2c/i2cvar.h>
     41  1.1  macallan 
     42  1.1  macallan #include <dev/sysmon/sysmonvar.h>
     43  1.1  macallan 
     44  1.1  macallan #include <dev/ofw/openfirm.h>
     45  1.1  macallan 
     46  1.2  macallan #include <macppc/dev/fancontrolvar.h>
     47  1.2  macallan 
     48  1.1  macallan //#define FCU_DEBUG
     49  1.1  macallan #ifdef FCU_DEBUG
     50  1.1  macallan #define DPRINTF printf
     51  1.1  macallan #else
     52  1.1  macallan #define DPRINTF if (0) printf
     53  1.1  macallan #endif
     54  1.1  macallan 
     55  1.1  macallan /* FCU registers, from OpenBSD's fcu.c */
     56  1.1  macallan #define FCU_FAN_FAIL	0x0b		/* fans states in bits 0<1-6>7 */
     57  1.1  macallan #define FCU_FAN_ACTIVE	0x0d
     58  1.1  macallan #define FCU_FANREAD(x)	0x11 + (x)*2
     59  1.1  macallan #define FCU_FANSET(x)	0x10 + (x)*2
     60  1.1  macallan #define FCU_PWM_FAIL	0x2b
     61  1.1  macallan #define FCU_PWM_ACTIVE	0x2d
     62  1.1  macallan #define FCU_PWMREAD(x)	0x30 + (x)*2
     63  1.1  macallan 
     64  1.1  macallan 
     65  1.1  macallan typedef struct _fcu_fan {
     66  1.1  macallan 	int target;
     67  1.1  macallan 	int reg;
     68  1.1  macallan 	int base_rpm, max_rpm;
     69  1.1  macallan 	int step;
     70  1.1  macallan 	int duty;	/* for pwm fans */
     71  1.1  macallan } fcu_fan_t;
     72  1.1  macallan 
     73  1.1  macallan #define FCU_ZONE_CPU		0
     74  1.1  macallan #define FCU_ZONE_CASE		1
     75  1.1  macallan #define FCU_ZONE_DRIVEBAY	2
     76  1.1  macallan #define FCU_ZONE_COUNT		3
     77  1.1  macallan 
     78  1.1  macallan struct fcu_softc {
     79  1.1  macallan 	device_t	sc_dev;
     80  1.1  macallan 	i2c_tag_t	sc_i2c;
     81  1.1  macallan 	i2c_addr_t	sc_addr;
     82  1.4  macallan 	struct sysctlnode 	*sc_sysctl_me;
     83  1.4  macallan 	struct sysmon_envsys	*sc_sme;
     84  1.2  macallan 	envsys_data_t		sc_sensors[32];
     85  1.2  macallan 	int			sc_nsensors;
     86  1.2  macallan 	fancontrol_zone_t	sc_zones[FCU_ZONE_COUNT];
     87  1.2  macallan 	fcu_fan_t		sc_fans[FANCONTROL_MAX_FANS];
     88  1.2  macallan 	int			sc_nfans;
     89  1.2  macallan 	lwp_t			*sc_thread;
     90  1.2  macallan 	bool			sc_dying, sc_pwm;
     91  1.2  macallan 	uint8_t			sc_eeprom0[160];
     92  1.2  macallan 	uint8_t			sc_eeprom1[160];
     93  1.1  macallan };
     94  1.1  macallan 
     95  1.1  macallan static int	fcu_match(device_t, cfdata_t, void *);
     96  1.1  macallan static void	fcu_attach(device_t, device_t, void *);
     97  1.1  macallan 
     98  1.1  macallan static void	fcu_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
     99  1.1  macallan 
    100  1.1  macallan static bool is_cpu(const envsys_data_t *);
    101  1.1  macallan static bool is_case(const envsys_data_t *);
    102  1.1  macallan static bool is_drive(const envsys_data_t *);
    103  1.1  macallan 
    104  1.2  macallan static int fcu_set_rpm(void *, int, int);
    105  1.2  macallan static int fcu_get_rpm(void *, int);
    106  1.1  macallan static void fcu_adjust(void *);
    107  1.1  macallan 
    108  1.1  macallan CFATTACH_DECL_NEW(fcu, sizeof(struct fcu_softc),
    109  1.1  macallan     fcu_match, fcu_attach, NULL, NULL);
    110  1.1  macallan 
    111  1.1  macallan static const struct device_compatible_entry compat_data[] = {
    112  1.1  macallan 	{ .compat = "fcu" },
    113  1.1  macallan 	DEVICE_COMPAT_EOL
    114  1.1  macallan };
    115  1.1  macallan 
    116  1.1  macallan static int
    117  1.1  macallan fcu_match(device_t parent, cfdata_t match, void *aux)
    118  1.1  macallan {
    119  1.1  macallan 	struct i2c_attach_args *ia = aux;
    120  1.1  macallan 	int match_result;
    121  1.1  macallan 
    122  1.1  macallan 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    123  1.1  macallan 		return match_result;
    124  1.1  macallan 
    125  1.1  macallan 	if (ia->ia_addr == 0x2f)
    126  1.1  macallan 		return I2C_MATCH_ADDRESS_ONLY;
    127  1.1  macallan 
    128  1.1  macallan 	return 0;
    129  1.1  macallan }
    130  1.1  macallan 
    131  1.1  macallan static void
    132  1.1  macallan fcu_attach(device_t parent, device_t self, void *aux)
    133  1.1  macallan {
    134  1.1  macallan 	struct fcu_softc *sc = device_private(self);
    135  1.1  macallan 	struct i2c_attach_args *ia = aux;
    136  1.4  macallan 	int have_eeprom1 = 1, i;
    137  1.1  macallan 
    138  1.1  macallan 	sc->sc_dev = self;
    139  1.1  macallan 	sc->sc_i2c = ia->ia_tag;
    140  1.1  macallan 	sc->sc_addr = ia->ia_addr;
    141  1.1  macallan 
    142  1.1  macallan 	aprint_naive("\n");
    143  1.1  macallan 	aprint_normal(": Fan Control Unit\n");
    144  1.1  macallan 
    145  1.4  macallan 	sysctl_createv(NULL, 0, NULL, (void *) &sc->sc_sysctl_me,
    146  1.4  macallan 	    CTLFLAG_READWRITE,
    147  1.4  macallan 	    CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
    148  1.4  macallan 	    NULL, 0, NULL, 0,
    149  1.4  macallan 	    CTL_MACHDEP, CTL_CREATE, CTL_EOL);
    150  1.4  macallan 
    151  1.1  macallan 	if (get_cpuid(0, sc->sc_eeprom0) < 160) {
    152  1.1  macallan 		/*
    153  1.1  macallan 		 * XXX this should never happen, we depend on the EEPROM for
    154  1.1  macallan 		 * calibration data to make sense of temperature and voltage
    155  1.1  macallan 		 * sensors elsewhere, and fan parameters here.
    156  1.1  macallan 		 */
    157  1.1  macallan 		aprint_error_dev(self, "no EEPROM data for CPU 0\n");
    158  1.1  macallan 		return;
    159  1.1  macallan 	}
    160  1.1  macallan 	if (get_cpuid(1, sc->sc_eeprom1) < 160)
    161  1.1  macallan 		have_eeprom1 = 0;
    162  1.1  macallan 
    163  1.1  macallan 	/* init zones */
    164  1.2  macallan 	sc->sc_zones[FCU_ZONE_CPU].name = "CPUs";
    165  1.1  macallan 	sc->sc_zones[FCU_ZONE_CPU].filter = is_cpu;
    166  1.2  macallan 	sc->sc_zones[FCU_ZONE_CPU].cookie = sc;
    167  1.2  macallan 	sc->sc_zones[FCU_ZONE_CPU].get_rpm = fcu_get_rpm;
    168  1.2  macallan 	sc->sc_zones[FCU_ZONE_CPU].set_rpm = fcu_set_rpm;
    169  1.2  macallan 	sc->sc_zones[FCU_ZONE_CPU].Tmin = 50;
    170  1.2  macallan 	sc->sc_zones[FCU_ZONE_CPU].Tmax = 85;
    171  1.1  macallan 	sc->sc_zones[FCU_ZONE_CPU].nfans = 0;
    172  1.2  macallan 	sc->sc_zones[FCU_ZONE_CASE].name = "Slots";
    173  1.1  macallan 	sc->sc_zones[FCU_ZONE_CASE].filter = is_case;
    174  1.4  macallan 	sc->sc_zones[FCU_ZONE_CASE].cookie = sc;
    175  1.2  macallan 	sc->sc_zones[FCU_ZONE_CASE].Tmin = 50;
    176  1.4  macallan 	sc->sc_zones[FCU_ZONE_CASE].Tmax = 75;
    177  1.4  macallan 	sc->sc_zones[FCU_ZONE_CASE].nfans = 0;
    178  1.2  macallan 	sc->sc_zones[FCU_ZONE_CASE].get_rpm = fcu_get_rpm;
    179  1.2  macallan 	sc->sc_zones[FCU_ZONE_CASE].set_rpm = fcu_set_rpm;
    180  1.4  macallan 	sc->sc_zones[FCU_ZONE_DRIVEBAY].name = "Drivebays";
    181  1.1  macallan 	sc->sc_zones[FCU_ZONE_DRIVEBAY].filter = is_drive;
    182  1.2  macallan 	sc->sc_zones[FCU_ZONE_DRIVEBAY].cookie = sc;
    183  1.2  macallan 	sc->sc_zones[FCU_ZONE_DRIVEBAY].get_rpm = fcu_get_rpm;
    184  1.2  macallan 	sc->sc_zones[FCU_ZONE_DRIVEBAY].set_rpm = fcu_set_rpm;
    185  1.2  macallan 	sc->sc_zones[FCU_ZONE_DRIVEBAY].Tmin = 30;
    186  1.4  macallan 	sc->sc_zones[FCU_ZONE_DRIVEBAY].Tmax = 50;
    187  1.1  macallan 	sc->sc_zones[FCU_ZONE_DRIVEBAY].nfans = 0;
    188  1.1  macallan 
    189  1.1  macallan 	sc->sc_sme = sysmon_envsys_create();
    190  1.1  macallan 	sc->sc_sme->sme_name = device_xname(self);
    191  1.1  macallan 	sc->sc_sme->sme_cookie = sc;
    192  1.1  macallan 	sc->sc_sme->sme_refresh = fcu_sensors_refresh;
    193  1.1  macallan 
    194  1.1  macallan 	sc->sc_sensors[0].units = ENVSYS_SFANRPM;
    195  1.1  macallan 	sc->sc_sensors[1].state = ENVSYS_SINVALID;
    196  1.1  macallan 	sc->sc_nfans = 0;
    197  1.1  macallan 
    198  1.1  macallan 	/* round up sensors */
    199  1.1  macallan 	int ch;
    200  1.1  macallan 
    201  1.1  macallan 	sc->sc_nsensors = 0;
    202  1.1  macallan 	ch = OF_child(ia->ia_cookie);
    203  1.1  macallan 	while (ch != 0) {
    204  1.1  macallan 		char type[32], descr[32];
    205  1.1  macallan 		uint32_t reg;
    206  1.1  macallan 
    207  1.1  macallan 		envsys_data_t *s = &sc->sc_sensors[sc->sc_nsensors];
    208  1.1  macallan 
    209  1.1  macallan 		s->state = ENVSYS_SINVALID;
    210  1.1  macallan 
    211  1.1  macallan 		if (OF_getprop(ch, "device_type", type, 32) <= 0)
    212  1.1  macallan 			goto next;
    213  1.1  macallan 
    214  1.1  macallan 		if (strcmp(type, "fan-rpm-control") == 0) {
    215  1.1  macallan 			s->units = ENVSYS_SFANRPM;
    216  1.1  macallan 		} else if (strcmp(type, "fan-pwm-control") == 0) {
    217  1.1  macallan 			/* XXX we get the type from the register number */
    218  1.1  macallan 			s->units = ENVSYS_SFANRPM;
    219  1.1  macallan /* skip those for now since we don't really know how to interpret them */
    220  1.1  macallan #if 0
    221  1.1  macallan 		} else if (strcmp(type, "power-sensor") == 0) {
    222  1.1  macallan 			s->units = ENVSYS_SVOLTS_DC;
    223  1.1  macallan #endif
    224  1.1  macallan 		} else if (strcmp(type, "gpi-sensor") == 0) {
    225  1.1  macallan 			s->units = ENVSYS_INDICATOR;
    226  1.1  macallan 		} else {
    227  1.1  macallan 			/* ignore other types for now */
    228  1.1  macallan 			goto next;
    229  1.1  macallan 		}
    230  1.1  macallan 
    231  1.1  macallan 		if (OF_getprop(ch, "reg", &reg, sizeof(reg)) <= 0)
    232  1.1  macallan 			goto next;
    233  1.1  macallan 		s->private = reg;
    234  1.1  macallan 
    235  1.1  macallan 		if (OF_getprop(ch, "location", descr, 32) <= 0)
    236  1.1  macallan 			goto next;
    237  1.1  macallan 		strcpy(s->desc, descr);
    238  1.1  macallan 
    239  1.1  macallan 		if (s->units == ENVSYS_SFANRPM) {
    240  1.1  macallan 			fcu_fan_t *fan = &sc->sc_fans[sc->sc_nfans];
    241  1.1  macallan 			uint8_t *eeprom = NULL;
    242  1.1  macallan 			uint16_t rmin, rmax;
    243  1.1  macallan 
    244  1.1  macallan 			if (strstr(descr, "CPU A") != NULL)
    245  1.1  macallan 				eeprom = sc->sc_eeprom0;
    246  1.1  macallan 			if (strstr(descr, "CPU B") != NULL) {
    247  1.1  macallan 				/*
    248  1.1  macallan 				 * XXX
    249  1.1  macallan 				 * this should never happen
    250  1.1  macallan 				 */
    251  1.1  macallan 				if (have_eeprom1 == 0) {
    252  1.1  macallan 					eeprom = sc->sc_eeprom0;
    253  1.1  macallan 				} else
    254  1.1  macallan 					eeprom = sc->sc_eeprom1;
    255  1.1  macallan 			}
    256  1.1  macallan 
    257  1.1  macallan 			fan->reg = reg;
    258  1.1  macallan 			fan->target = 0;
    259  1.1  macallan 			fan->duty = 0x80;
    260  1.1  macallan 
    261  1.1  macallan 			/* speed settings from EEPROM */
    262  1.1  macallan 			if (strstr(descr, "PUMP") != NULL) {
    263  1.1  macallan 				KASSERT(eeprom != NULL);
    264  1.1  macallan 				memcpy(&rmin, &eeprom[0x54], 2);
    265  1.1  macallan 				memcpy(&rmax, &eeprom[0x56], 2);
    266  1.1  macallan 				fan->base_rpm = rmin;
    267  1.1  macallan 				fan->max_rpm = rmax;
    268  1.1  macallan 				fan->step = (rmax - rmin) / 30;
    269  1.1  macallan 			} else if (strstr(descr, "INTAKE") != NULL) {
    270  1.1  macallan 				KASSERT(eeprom != NULL);
    271  1.1  macallan 				memcpy(&rmin, &eeprom[0x4c], 2);
    272  1.1  macallan 				memcpy(&rmax, &eeprom[0x4e], 2);
    273  1.1  macallan 				fan->base_rpm = rmin;
    274  1.1  macallan 				fan->max_rpm = rmax;
    275  1.1  macallan 				fan->step = (rmax - rmin) / 30;
    276  1.1  macallan 			} else if (strstr(descr, "EXHAUST") != NULL) {
    277  1.1  macallan 				KASSERT(eeprom != NULL);
    278  1.1  macallan 				memcpy(&rmin, &eeprom[0x50], 2);
    279  1.1  macallan 				memcpy(&rmax, &eeprom[0x52], 2);
    280  1.1  macallan 				fan->base_rpm = rmin;
    281  1.1  macallan 				fan->max_rpm = rmax;
    282  1.1  macallan 				fan->step = (rmax - rmin) / 30;
    283  1.1  macallan 			} else if (strstr(descr, "DRIVE") != NULL ) {
    284  1.1  macallan 				fan->base_rpm = 1000;
    285  1.1  macallan 				fan->max_rpm = 3000;
    286  1.1  macallan 				fan->step = 100;
    287  1.1  macallan 			} else {
    288  1.1  macallan 				fan->base_rpm = 1000;
    289  1.1  macallan 				fan->max_rpm = 3000;
    290  1.1  macallan 				fan->step = 100;
    291  1.1  macallan 			}
    292  1.1  macallan 			DPRINTF("fan %s: %d - %d rpm, step %d\n",
    293  1.1  macallan 			   descr, fan->base_rpm, fan->max_rpm, fan->step);
    294  1.1  macallan 
    295  1.1  macallan 			/* now stuff them into zones */
    296  1.1  macallan 			if (strstr(descr, "CPU") != NULL) {
    297  1.2  macallan 				fancontrol_zone_t *z = &sc->sc_zones[FCU_ZONE_CPU];
    298  1.2  macallan 				z->fans[z->nfans].num = sc->sc_nfans;
    299  1.2  macallan 				z->fans[z->nfans].min_rpm = fan->base_rpm;
    300  1.2  macallan 				z->fans[z->nfans].max_rpm = fan->max_rpm;
    301  1.2  macallan 				z->fans[z->nfans].name = s->desc;
    302  1.1  macallan 				z->nfans++;
    303  1.1  macallan 			} else if ((strstr(descr, "BACKSIDE") != NULL) ||
    304  1.1  macallan 				   (strstr(descr, "SLOT") != NULL))  {
    305  1.2  macallan 				fancontrol_zone_t *z = &sc->sc_zones[FCU_ZONE_CASE];
    306  1.2  macallan 				z->fans[z->nfans].num = sc->sc_nfans;
    307  1.2  macallan 				z->fans[z->nfans].min_rpm = fan->base_rpm;
    308  1.2  macallan 				z->fans[z->nfans].max_rpm = fan->max_rpm;
    309  1.2  macallan 				z->fans[z->nfans].name = s->desc;
    310  1.1  macallan 				z->nfans++;
    311  1.1  macallan 			} else if (strstr(descr, "DRIVE") != NULL) {
    312  1.2  macallan 				fancontrol_zone_t *z = &sc->sc_zones[FCU_ZONE_DRIVEBAY];
    313  1.2  macallan 				z->fans[z->nfans].num = sc->sc_nfans;
    314  1.2  macallan 				z->fans[z->nfans].min_rpm = fan->base_rpm;
    315  1.2  macallan 				z->fans[z->nfans].max_rpm = fan->max_rpm;
    316  1.2  macallan 				z->fans[z->nfans].name = s->desc;
    317  1.1  macallan 				z->nfans++;
    318  1.1  macallan 			}
    319  1.1  macallan 			sc->sc_nfans++;
    320  1.1  macallan 		}
    321  1.1  macallan 		sysmon_envsys_sensor_attach(sc->sc_sme, s);
    322  1.1  macallan 		sc->sc_nsensors++;
    323  1.1  macallan next:
    324  1.1  macallan 		ch = OF_peer(ch);
    325  1.1  macallan 	}
    326  1.1  macallan 	sysmon_envsys_register(sc->sc_sme);
    327  1.1  macallan 
    328  1.4  macallan 	/* setup sysctls for our zones etc. */
    329  1.4  macallan 	for (i = 0; i < FCU_ZONE_COUNT; i++) {
    330  1.4  macallan 		fancontrol_init_zone(&sc->sc_zones[i], sc->sc_sysctl_me);
    331  1.4  macallan 	}
    332  1.4  macallan 
    333  1.1  macallan 	sc->sc_dying = FALSE;
    334  1.1  macallan 	kthread_create(PRI_NONE, 0, curcpu(), fcu_adjust, sc, &sc->sc_thread,
    335  1.1  macallan 	    "fan control");
    336  1.1  macallan }
    337  1.1  macallan 
    338  1.1  macallan static void
    339  1.1  macallan fcu_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    340  1.1  macallan {
    341  1.1  macallan 	struct fcu_softc *sc = sme->sme_cookie;
    342  1.1  macallan 	uint8_t cmd;
    343  1.1  macallan 	uint16_t data = -1;
    344  1.1  macallan 	int error;
    345  1.1  macallan 
    346  1.1  macallan 	if (edata->units == ENVSYS_SFANRPM) {
    347  1.1  macallan 	    	cmd = edata->private + 1;
    348  1.1  macallan 	} else
    349  1.1  macallan 		cmd = edata->private;
    350  1.1  macallan 
    351  1.1  macallan 	/* fcu is a macppc only thing so we can safely assume big endian */
    352  1.1  macallan 	iic_acquire_bus(sc->sc_i2c, 0);
    353  1.1  macallan 	error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    354  1.1  macallan 	    sc->sc_addr, &cmd, 1, &data, 2, 0);
    355  1.1  macallan 	iic_release_bus(sc->sc_i2c, 0);
    356  1.1  macallan 
    357  1.1  macallan 	if (error) {
    358  1.1  macallan 		edata->state = ENVSYS_SINVALID;
    359  1.1  macallan 		return;
    360  1.1  macallan 	}
    361  1.1  macallan 
    362  1.1  macallan 	edata->state = ENVSYS_SVALID;
    363  1.1  macallan 
    364  1.1  macallan 	switch (edata->units) {
    365  1.1  macallan 		case ENVSYS_SFANRPM:
    366  1.1  macallan 			edata->value_cur = data >> 3;
    367  1.1  macallan 			break;
    368  1.1  macallan 		case ENVSYS_SVOLTS_DC:
    369  1.1  macallan 			/* XXX this reads bogus */
    370  1.1  macallan 			edata->value_cur = data * 1000;
    371  1.1  macallan 			break;
    372  1.1  macallan 		case ENVSYS_INDICATOR:
    373  1.1  macallan 			/* guesswork for now */
    374  1.1  macallan 			edata->value_cur = data >> 8;
    375  1.1  macallan 			break;
    376  1.1  macallan 		default:
    377  1.1  macallan 			edata->state = ENVSYS_SINVALID;
    378  1.1  macallan 	}
    379  1.1  macallan }
    380  1.1  macallan 
    381  1.1  macallan static bool
    382  1.1  macallan is_cpu(const envsys_data_t *edata)
    383  1.1  macallan {
    384  1.1  macallan 	if (edata->units != ENVSYS_STEMP)
    385  1.1  macallan 		return false;
    386  1.1  macallan 	if (strstr(edata->desc, "CPU") != NULL)
    387  1.1  macallan 		return TRUE;
    388  1.1  macallan 	return false;
    389  1.1  macallan }
    390  1.1  macallan 
    391  1.1  macallan static bool
    392  1.1  macallan is_case(const envsys_data_t *edata)
    393  1.1  macallan {
    394  1.1  macallan 	if (edata->units != ENVSYS_STEMP)
    395  1.1  macallan 		return false;
    396  1.1  macallan 	if ((strstr(edata->desc, "MLB") != NULL) ||
    397  1.1  macallan 	    (strstr(edata->desc, "BACKSIDE") != NULL) ||
    398  1.1  macallan 	    (strstr(edata->desc, "U3") != NULL))
    399  1.1  macallan 		return TRUE;
    400  1.1  macallan 	return false;
    401  1.1  macallan }
    402  1.1  macallan 
    403  1.1  macallan static bool
    404  1.1  macallan is_drive(const envsys_data_t *edata)
    405  1.1  macallan {
    406  1.1  macallan 	if (edata->units != ENVSYS_STEMP)
    407  1.1  macallan 		return false;
    408  1.1  macallan 	if (strstr(edata->desc, "DRIVE") != NULL)
    409  1.1  macallan 		return TRUE;
    410  1.1  macallan 	return false;
    411  1.1  macallan }
    412  1.1  macallan 
    413  1.2  macallan static int
    414  1.2  macallan fcu_get_rpm(void *cookie, int which)
    415  1.1  macallan {
    416  1.2  macallan 	struct fcu_softc *sc = cookie;
    417  1.2  macallan 	fcu_fan_t *f = &sc->sc_fans[which];
    418  1.1  macallan 	int error;
    419  1.2  macallan 	uint16_t data;
    420  1.2  macallan 	uint8_t cmd;
    421  1.2  macallan 
    422  1.2  macallan 	iic_acquire_bus(sc->sc_i2c, 0);
    423  1.2  macallan 	cmd = f->reg + 1;
    424  1.2  macallan 	error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    425  1.2  macallan 	    sc->sc_addr, &cmd, 1, &data, 2, 0);
    426  1.2  macallan 	iic_release_bus(sc->sc_i2c, 0);
    427  1.2  macallan 	if (error != 0) return -1;
    428  1.2  macallan 	data = data >> 3;
    429  1.2  macallan 	return data;
    430  1.2  macallan }
    431  1.2  macallan 
    432  1.2  macallan static int
    433  1.2  macallan fcu_set_rpm(void *cookie, int which, int speed)
    434  1.2  macallan {
    435  1.2  macallan 	struct fcu_softc *sc = cookie;
    436  1.2  macallan 	fcu_fan_t *f = &sc->sc_fans[which];
    437  1.2  macallan 	int error = 0;
    438  1.1  macallan 	uint8_t cmd;
    439  1.1  macallan 
    440  1.1  macallan 	if (speed > f->max_rpm) speed = f->max_rpm;
    441  1.1  macallan 	if (speed < f->base_rpm) speed = f->base_rpm;
    442  1.1  macallan 
    443  1.1  macallan 	if (f->reg < 0x30) {
    444  1.1  macallan 		uint16_t data;
    445  1.1  macallan 		/* simple rpm fan, just poke the register */
    446  1.1  macallan 
    447  1.2  macallan 		if (f->target == speed) return 0;
    448  1.1  macallan 		iic_acquire_bus(sc->sc_i2c, 0);
    449  1.1  macallan 		cmd = f->reg;
    450  1.1  macallan 		data = (speed << 3);
    451  1.1  macallan 		error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
    452  1.1  macallan 		    sc->sc_addr, &cmd, 1, &data, 2, 0);
    453  1.1  macallan 		iic_release_bus(sc->sc_i2c, 0);
    454  1.1  macallan 	} else {
    455  1.1  macallan 		int diff;
    456  1.1  macallan 		int nduty = f->duty;
    457  1.2  macallan 		int current_speed;
    458  1.1  macallan 		/* pwm fan, measure speed, then adjust duty cycle */
    459  1.1  macallan 		DPRINTF("pwm fan ");
    460  1.2  macallan 		current_speed = fcu_get_rpm(sc, which);
    461  1.2  macallan 		diff = current_speed - speed;
    462  1.2  macallan 		DPRINTF("d %d s %d t %d diff %d ", f->duty, current_speed, speed, diff);
    463  1.1  macallan 		if (diff > 100) {
    464  1.1  macallan 			nduty = uimax(20, nduty - 1);
    465  1.1  macallan 		}
    466  1.1  macallan 		if (diff < -100) {
    467  1.1  macallan 			nduty = uimin(0xd0, nduty + 1);
    468  1.1  macallan 		}
    469  1.1  macallan 		cmd = f->reg;
    470  1.1  macallan 		DPRINTF("%s nduty %d", __func__, nduty);
    471  1.1  macallan 		if (nduty != f->duty) {
    472  1.1  macallan 			uint8_t arg = nduty;
    473  1.2  macallan 			iic_acquire_bus(sc->sc_i2c, 0);
    474  1.1  macallan 			error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
    475  1.1  macallan 			    sc->sc_addr, &cmd, 1, &arg, 1, 0);
    476  1.2  macallan 			iic_release_bus(sc->sc_i2c, 0);
    477  1.1  macallan 			f->duty = nduty;
    478  1.1  macallan 			sc->sc_pwm = TRUE;
    479  1.1  macallan 
    480  1.1  macallan 		}
    481  1.1  macallan 		DPRINTF("ok\n");
    482  1.1  macallan 	}
    483  1.1  macallan 	if (error) printf("boo\n");
    484  1.1  macallan 	f->target = speed;
    485  1.2  macallan 	return 0;
    486  1.1  macallan }
    487  1.1  macallan 
    488  1.1  macallan static void
    489  1.1  macallan fcu_adjust(void *cookie)
    490  1.1  macallan {
    491  1.1  macallan 	struct fcu_softc *sc = cookie;
    492  1.1  macallan 	int i;
    493  1.1  macallan 	uint8_t cmd, data;
    494  1.1  macallan 
    495  1.1  macallan 	while (!sc->sc_dying) {
    496  1.1  macallan 		/* poke the FCU so we don't go 747 */
    497  1.1  macallan 		iic_acquire_bus(sc->sc_i2c, 0);
    498  1.1  macallan 		cmd = FCU_FAN_ACTIVE;
    499  1.1  macallan 		iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    500  1.1  macallan 		    sc->sc_addr, &cmd, 1, &data, 1, 0);
    501  1.1  macallan 		iic_release_bus(sc->sc_i2c, 0);
    502  1.1  macallan 		sc->sc_pwm = FALSE;
    503  1.1  macallan 		for (i = 0; i < FCU_ZONE_COUNT; i++)
    504  1.2  macallan 			fancontrol_adjust_zone(&sc->sc_zones[i]);
    505  1.3  macallan 		/*
    506  1.3  macallan 		 * take a shorter nap if we're in the proccess of adjusting a
    507  1.3  macallan 		 * PWM fan, which relies on measuring speed and then changing
    508  1.3  macallan 		 * its duty cycle until we're reasonable close to the target
    509  1.3  macallan 		 * speed
    510  1.3  macallan 		 */
    511  1.3  macallan 		kpause("fanctrl", true, mstohz(sc->sc_pwm ? 1000 : 2000), NULL);
    512  1.1  macallan 	}
    513  1.1  macallan 	kthread_exit(0);
    514  1.1  macallan }
    515