1 /* $NetBSD: pcf8591_envctrl.c,v 1.5 2011/06/20 17:01:45 pgoyette Exp $ */ 2 /* $OpenBSD: pcf8591_envctrl.c,v 1.6 2007/10/25 21:17:20 kettenis Exp $ */ 3 4 /* 5 * Copyright (c) 2006 Damien Miller <djm@openbsd.org> 6 * Copyright (c) 2007 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/device.h> 24 25 #include <dev/sysmon/sysmonvar.h> 26 27 #include <dev/ofw/openfirm.h> 28 #include <dev/i2c/i2cvar.h> 29 30 #define PCF8591_CHANNELS 4 31 32 #define PCF8591_CTRL_CH0 0x00 33 #define PCF8591_CTRL_CH1 0x01 34 #define PCF8591_CTRL_CH2 0x02 35 #define PCF8591_CTRL_CH3 0x03 36 #define PCF8591_CTRL_AUTOINC 0x04 37 #define PCF8591_CTRL_OSCILLATOR 0x40 38 39 struct ecadc_channel { 40 u_int chan_num; 41 envsys_data_t chan_sensor; 42 u_char *chan_xlate; 43 int64_t chan_factor; 44 int64_t chan_min; 45 int64_t chan_warn; 46 int64_t chan_crit; 47 }; 48 49 struct ecadc_softc { 50 device_t sc_dev; 51 i2c_tag_t sc_tag; 52 i2c_addr_t sc_addr; 53 u_char sc_ps_xlate[256]; 54 u_char sc_cpu_xlate[256]; 55 u_int sc_nchan; 56 struct ecadc_channel sc_channels[PCF8591_CHANNELS]; 57 struct sysmon_envsys *sc_sme; 58 }; 59 60 static int ecadc_match(device_t, cfdata_t, void *); 61 static void ecadc_attach(device_t, device_t, void *); 62 static void ecadc_refresh(struct sysmon_envsys *, envsys_data_t *); 63 static void ecadc_get_limits(struct sysmon_envsys *, envsys_data_t *, 64 sysmon_envsys_lim_t *, uint32_t *); 65 66 CFATTACH_DECL_NEW(ecadc, sizeof(struct ecadc_softc), 67 ecadc_match, ecadc_attach, NULL, NULL); 68 69 static const char * ecadc_compats[] = { 70 "ecadc", 71 NULL 72 }; 73 74 static int 75 ecadc_match(device_t parent, cfdata_t cf, void *aux) 76 { 77 struct i2c_attach_args *ia = aux; 78 79 if (iic_compat_match(ia, ecadc_compats)) 80 return 1; 81 82 return 0; 83 } 84 85 static void 86 ecadc_attach(device_t parent, device_t self, void *aux) 87 { 88 struct i2c_attach_args *ia = aux; 89 struct ecadc_softc *sc = device_private(self); 90 u_char term[256]; 91 u_char *cp, *desc; 92 int64_t minv, warnv, crit, num, den; 93 u_int8_t junk[PCF8591_CHANNELS + 1]; 94 envsys_data_t *sensor; 95 int len, error, addr, chan, node = (int)ia->ia_cookie; 96 u_int i; 97 98 sc->sc_dev = self; 99 if ((len = OF_getprop(node, "thermisters", term, 100 sizeof(term))) < 0) { 101 aprint_error(": couldn't find \"thermisters\" property\n"); 102 return; 103 } 104 105 if (OF_getprop(node, "cpu-temp-factors", &sc->sc_cpu_xlate[2], 106 sizeof(sc->sc_cpu_xlate) - 2) < 0) { 107 aprint_error(": couldn't find \"cpu-temp-factors\" property\n"); 108 return; 109 } 110 sc->sc_cpu_xlate[0] = sc->sc_cpu_xlate[1] = sc->sc_cpu_xlate[2]; 111 112 /* Only the Sun Enterprise 450 has these. */ 113 OF_getprop(node, "ps-temp-factors", &sc->sc_ps_xlate[2], 114 sizeof(sc->sc_ps_xlate) - 2); 115 sc->sc_ps_xlate[0] = sc->sc_ps_xlate[1] = sc->sc_ps_xlate[2]; 116 117 cp = term; 118 while (cp < term + len) { 119 addr = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4; 120 chan = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4; 121 minv = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4; 122 warnv = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4; 123 crit = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4; 124 num = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4; 125 den = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4; 126 desc = cp; 127 while (cp < term + len && *cp++); 128 129 if (addr != (ia->ia_addr << 1)) 130 continue; 131 132 if (num == 0 || den == 0) 133 num = den = 1; 134 135 sc->sc_channels[sc->sc_nchan].chan_num = chan; 136 137 sensor = &sc->sc_channels[sc->sc_nchan].chan_sensor; 138 sensor->units = ENVSYS_STEMP; 139 sensor->flags |= ENVSYS_FMONLIMITS; 140 sensor->state = ENVSYS_SINVALID; 141 strlcpy(sensor->desc, desc, sizeof(sensor->desc)); 142 143 if (strncmp(desc, "CPU", 3) == 0) 144 sc->sc_channels[sc->sc_nchan].chan_xlate = 145 sc->sc_cpu_xlate; 146 else if (strncmp(desc, "PS", 2) == 0) 147 sc->sc_channels[sc->sc_nchan].chan_xlate = 148 sc->sc_ps_xlate; 149 else 150 sc->sc_channels[sc->sc_nchan].chan_factor = 151 (1000000 * num) / den; 152 sc->sc_channels[sc->sc_nchan].chan_min = 153 273150000 + 1000000 * minv; 154 sc->sc_channels[sc->sc_nchan].chan_warn = 155 273150000 + 1000000 * warnv; 156 sc->sc_channels[sc->sc_nchan].chan_crit = 157 273150000 + 1000000 * crit; 158 sc->sc_nchan++; 159 } 160 161 sc->sc_tag = ia->ia_tag; 162 sc->sc_addr = ia->ia_addr; 163 164 iic_acquire_bus(sc->sc_tag, 0); 165 166 /* Try a read now, so we can fail if it doesn't work */ 167 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, 168 NULL, 0, junk, sc->sc_nchan + 1, 0)) { 169 aprint_error(": read failed\n"); 170 iic_release_bus(sc->sc_tag, 0); 171 return; 172 } 173 174 iic_release_bus(sc->sc_tag, 0); 175 176 /* Hook us into the sysmon_envsys subsystem */ 177 sc->sc_sme = sysmon_envsys_create(); 178 sc->sc_sme->sme_name = device_xname(self); 179 sc->sc_sme->sme_cookie = sc; 180 sc->sc_sme->sme_refresh = ecadc_refresh; 181 sc->sc_sme->sme_get_limits = ecadc_get_limits; 182 183 /* Initialize sensor data. */ 184 for (i = 0; i < sc->sc_nchan; i++) 185 sysmon_envsys_sensor_attach(sc->sc_sme, 186 &sc->sc_channels[i].chan_sensor); 187 188 error = sysmon_envsys_register(sc->sc_sme); 189 if (error) { 190 aprint_error_dev(self, "error %d registering with sysmon\n", 191 error); 192 sysmon_envsys_destroy(sc->sc_sme); 193 return; 194 } 195 196 aprint_naive(": Temp Sensors\n"); 197 aprint_normal(": %s Temp Sensors\n", ia->ia_name); 198 } 199 200 static void 201 ecadc_refresh(struct sysmon_envsys *sme, envsys_data_t *sensor) 202 { 203 struct ecadc_softc *sc = sme->sme_cookie; 204 u_int i; 205 u_int8_t data[PCF8591_CHANNELS + 1]; 206 u_int8_t ctrl = PCF8591_CTRL_CH0 | PCF8591_CTRL_AUTOINC | 207 PCF8591_CTRL_OSCILLATOR; 208 209 iic_acquire_bus(sc->sc_tag, 0); 210 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, 211 &ctrl, 1, NULL, 0, 0)) { 212 iic_release_bus(sc->sc_tag, 0); 213 return; 214 } 215 /* NB: first byte out is stale, so read num_channels + 1 */ 216 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, 217 NULL, 0, data, PCF8591_CHANNELS + 1, 0)) { 218 iic_release_bus(sc->sc_tag, 0); 219 return; 220 } 221 iic_release_bus(sc->sc_tag, 0); 222 223 /* We only support temperature channels. */ 224 for (i = 0; i < sc->sc_nchan; i++) { 225 struct ecadc_channel *chp = &sc->sc_channels[i]; 226 227 if (chp->chan_xlate) 228 chp->chan_sensor.value_cur = 273150000 + 1000000 * 229 chp->chan_xlate[data[1 + chp->chan_num]]; 230 else 231 chp->chan_sensor.value_cur = 273150000 + 232 chp->chan_factor * data[1 + chp->chan_num]; 233 234 chp->chan_sensor.state = ENVSYS_SVALID; 235 chp->chan_sensor.flags &= ~ENVSYS_FNEED_REFRESH; 236 chp->chan_sensor.flags |= ENVSYS_FMONLIMITS; 237 } 238 } 239 240 static void 241 ecadc_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata, 242 sysmon_envsys_lim_t *limits, uint32_t *props) 243 { 244 struct ecadc_softc *sc = sme->sme_cookie; 245 int i; 246 247 for (i = 0; i < sc->sc_nchan; i++) { 248 if (edata != &sc->sc_channels[i].chan_sensor) 249 continue; 250 *props |= PROP_WARNMIN|PROP_WARNMAX|PROP_CRITMAX; 251 limits->sel_warnmin = sc->sc_channels[i].chan_min; 252 limits->sel_warnmax = sc->sc_channels[i].chan_warn; 253 limits->sel_critmax = sc->sc_channels[i].chan_crit; 254 return; 255 } 256 } 257