1 /* $NetBSD: tda.c,v 1.3 2010/03/11 04:19:56 mrg Exp $ */ 2 /* $OpenBSD: tda.c,v 1.4 2008/02/27 17:25:00 robert Exp $ */ 3 4 /* 5 * Copyright (c) 2008 Robert Nagy <robert@openbsd.org> 6 * Copyright (c) 2008 Mark Kettenis <kettenis@openbsd.org> 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21 #include <sys/param.h> 22 #include <sys/systm.h> 23 #include <sys/kernel.h> 24 #include <sys/device.h> 25 #include <dev/sysmon/sysmonvar.h> 26 #include <dev/sysmon/sysmon_taskq.h> 27 28 #include <machine/autoconf.h> 29 #include <machine/openfirm.h> 30 31 #include <dev/i2c/i2cvar.h> 32 33 /* fan control registers */ 34 #define TDA_SYSFAN_REG 0xf0 35 #define TDA_CPUFAN_REG 0xf2 36 #define TDA_PSFAN_REG 0xf4 37 38 #define TDA_FANSPEED_MIN 0x0c 39 #define TDA_FANSPEED_MAX 0x3f 40 41 #define TDA_PSFAN_ON 0x1f 42 #define TDA_PSFAN_OFF 0x00 43 44 /* Internal and External temperature senor numbers */ 45 #define SENSOR_TEMP_EXT 0 46 #define SENSOR_TEMP_INT 1 47 48 #define CPU_TEMP_MAX (67 * 1000000 + 273150000) 49 #define CPU_TEMP_MIN (57 * 1000000 + 273150000) 50 #define SYS_TEMP_MAX (30 * 1000000 + 273150000) 51 #define SYS_TEMP_MIN (20 * 1000000 + 273150000) 52 53 struct tda_softc { 54 device_t sc_dev; 55 i2c_tag_t sc_tag; 56 i2c_addr_t sc_addr; 57 58 u_int16_t sc_cfan_speed; /* current CPU fan speed */ 59 u_int16_t sc_sfan_speed; /* current SYS fan speed */ 60 61 callout_t sc_timer; 62 }; 63 64 int tda_match(struct device *, struct cfdata *, void *); 65 void tda_attach(struct device *, struct device *, void *); 66 67 void tda_setspeed(struct tda_softc *); 68 static void tda_adjust(void *); 69 static void tda_timeout(void *); 70 71 72 CFATTACH_DECL_NEW(tda, sizeof(struct tda_softc), 73 tda_match, tda_attach, NULL, NULL); 74 75 int 76 tda_match(struct device *parent, struct cfdata *match, void *aux) 77 { 78 struct i2c_attach_args *ia = aux; 79 char name[32]; 80 81 /* Only attach on the Sun Blade 1000/2000. */ 82 if (OF_getprop(findroot(), "name", name, sizeof(name)) <= 0) 83 return (0); 84 if (strcmp(name, "SUNW,Sun-Blade-1000") != 0) 85 return (0); 86 87 /* 88 * No need for "compatible" matching, we know exactly what 89 * firmware calls us. 90 */ 91 return strcmp(ia->ia_name, "fan-control") == 0; 92 } 93 94 void 95 tda_attach(struct device *parent, struct device *self, void *aux) 96 { 97 struct tda_softc *sc = device_private(self); 98 struct i2c_attach_args *ia = aux; 99 100 sc->sc_dev = self; 101 sc->sc_tag = ia->ia_tag; 102 sc->sc_addr = ia->ia_addr; 103 104 aprint_normal(": %s\n", ia->ia_name); 105 aprint_naive(": Environment sensor\n"); 106 107 /* 108 * Set the fans to maximum speed and save the power levels; 109 * the controller is write-only. 110 */ 111 sc->sc_cfan_speed = sc->sc_sfan_speed = (TDA_FANSPEED_MAX+TDA_FANSPEED_MIN)/2; 112 tda_setspeed(sc); 113 114 callout_init(&sc->sc_timer, CALLOUT_MPSAFE); 115 callout_reset(&sc->sc_timer, hz*20, tda_timeout, sc); 116 } 117 118 static void 119 tda_timeout(void *v) 120 { 121 struct tda_softc *sc = v; 122 123 sysmon_task_queue_sched(0, tda_adjust, sc); 124 callout_reset(&sc->sc_timer, hz*60, tda_timeout, sc); 125 } 126 127 void 128 tda_setspeed(struct tda_softc *sc) 129 { 130 u_int8_t cmd[2]; 131 132 if (sc->sc_cfan_speed < TDA_FANSPEED_MIN) 133 sc->sc_cfan_speed = TDA_FANSPEED_MIN; 134 if (sc->sc_sfan_speed < TDA_FANSPEED_MIN) 135 sc->sc_sfan_speed = TDA_FANSPEED_MIN; 136 if (sc->sc_cfan_speed > TDA_FANSPEED_MAX) 137 sc->sc_cfan_speed = TDA_FANSPEED_MAX; 138 if (sc->sc_sfan_speed > TDA_FANSPEED_MAX) 139 sc->sc_sfan_speed = TDA_FANSPEED_MAX; 140 141 iic_acquire_bus(sc->sc_tag, 0); 142 143 cmd[0] = TDA_CPUFAN_REG; 144 cmd[1] = sc->sc_cfan_speed; 145 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, 146 sc->sc_addr, &cmd, sizeof(cmd), NULL, 0, 0)) { 147 aprint_error_dev(sc->sc_dev, "cannot write cpu-fan register\n"); 148 iic_release_bus(sc->sc_tag, 0); 149 return; 150 } 151 152 cmd[0] = TDA_SYSFAN_REG; 153 cmd[1] = sc->sc_sfan_speed; 154 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, 155 sc->sc_addr, &cmd, sizeof(cmd), NULL, 0, 0)) { 156 aprint_error_dev(sc->sc_dev, "cannot write system-fan register\n"); 157 iic_release_bus(sc->sc_tag, 0); 158 return; 159 } 160 161 iic_release_bus(sc->sc_tag, 0); 162 163 aprint_debug_dev(sc->sc_dev, "changed fan speed to cpu=%d system=%d\n", 164 sc->sc_cfan_speed, sc->sc_sfan_speed); 165 } 166 167 static bool 168 is_cpu_sensor(const envsys_data_t *edata) 169 { 170 if (edata->units != ENVSYS_STEMP) 171 return false; 172 return strcmp(edata->desc, "external") == 0; 173 } 174 175 static bool 176 is_system_sensor(const envsys_data_t *edata) 177 { 178 if (edata->units != ENVSYS_STEMP) 179 return false; 180 return strcmp(edata->desc, "internal") == 0; 181 } 182 183 static void 184 tda_adjust(void *v) 185 { 186 struct tda_softc *sc = v; 187 u_int64_t ctemp, stemp; 188 u_int16_t cspeed, sspeed; 189 190 /* Default to running the fans at maximum speed. */ 191 sspeed = cspeed = TDA_FANSPEED_MAX; 192 193 /* fetch maximum current temperature */ 194 ctemp = sysmon_envsys_get_max_value(is_cpu_sensor, true); 195 stemp = sysmon_envsys_get_max_value(is_system_sensor, true); 196 197 /* the predicates for selecting sensors must have gone wrong */ 198 if (ctemp == 0 || stemp == 0) { 199 aprint_error_dev(sc->sc_dev, "skipping temp adjustment" 200 " - no sensor values\n"); 201 return; 202 } 203 204 aprint_debug_dev(sc->sc_dev, "current temperature: cpu %" PRIu64 205 " system %" PRIu64 "\n", 206 ctemp, stemp); 207 208 if (ctemp < CPU_TEMP_MIN) 209 cspeed = TDA_FANSPEED_MIN; 210 else if (ctemp < CPU_TEMP_MAX) 211 cspeed = TDA_FANSPEED_MIN + 212 (ctemp - CPU_TEMP_MIN) * 213 (TDA_FANSPEED_MAX - TDA_FANSPEED_MIN) / 214 (CPU_TEMP_MAX - CPU_TEMP_MIN); 215 216 if (stemp < SYS_TEMP_MIN) 217 sspeed = TDA_FANSPEED_MIN; 218 else if (stemp < SYS_TEMP_MAX) 219 sc->sc_sfan_speed = TDA_FANSPEED_MIN + 220 (stemp - SYS_TEMP_MIN) * 221 (TDA_FANSPEED_MAX - TDA_FANSPEED_MIN) / 222 (SYS_TEMP_MAX - SYS_TEMP_MIN); 223 224 if (sspeed == sc->sc_sfan_speed && cspeed == sc->sc_cfan_speed) 225 return; 226 227 sc->sc_sfan_speed = sspeed; 228 sc->sc_cfan_speed = cspeed; 229 tda_setspeed(sc); 230 } 231