1 /* $NetBSD: pcf8591_envctrl.c,v 1.3 2010/02/28 15:30:22 martin 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 | ENVSYS_FMONNOTSUPP; 140 strlcpy(sensor->desc, desc, sizeof(sensor->desc)); 141 142 if (strncmp(desc, "CPU", 3) == 0) 143 sc->sc_channels[sc->sc_nchan].chan_xlate = 144 sc->sc_cpu_xlate; 145 else if (strncmp(desc, "PS", 2) == 0) 146 sc->sc_channels[sc->sc_nchan].chan_xlate = 147 sc->sc_ps_xlate; 148 else 149 sc->sc_channels[sc->sc_nchan].chan_factor = 150 (1000000 * num) / den; 151 sc->sc_channels[sc->sc_nchan].chan_min = 152 273150000 + 1000000 * minv; 153 sc->sc_channels[sc->sc_nchan].chan_warn = 154 273150000 + 1000000 * warnv; 155 sc->sc_channels[sc->sc_nchan].chan_crit = 156 273150000 + 1000000 * crit; 157 sc->sc_nchan++; 158 } 159 160 sc->sc_tag = ia->ia_tag; 161 sc->sc_addr = ia->ia_addr; 162 163 iic_acquire_bus(sc->sc_tag, 0); 164 165 /* Try a read now, so we can fail if it doesn't work */ 166 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, 167 NULL, 0, junk, sc->sc_nchan + 1, 0)) { 168 aprint_error(": read failed\n"); 169 iic_release_bus(sc->sc_tag, 0); 170 return; 171 } 172 173 iic_release_bus(sc->sc_tag, 0); 174 175 /* Hook us into the sysmon_envsys subsystem */ 176 sc->sc_sme = sysmon_envsys_create(); 177 sc->sc_sme->sme_name = device_xname(self); 178 sc->sc_sme->sme_cookie = sc; 179 sc->sc_sme->sme_refresh = ecadc_refresh; 180 sc->sc_sme->sme_get_limits = ecadc_get_limits; 181 182 /* Initialize sensor data. */ 183 for (i = 0; i < sc->sc_nchan; i++) 184 sysmon_envsys_sensor_attach(sc->sc_sme, 185 &sc->sc_channels[i].chan_sensor); 186 187 error = sysmon_envsys_register(sc->sc_sme); 188 if (error) { 189 aprint_error_dev(self, "error %d registering with sysmon\n", 190 error); 191 sysmon_envsys_destroy(sc->sc_sme); 192 return; 193 } 194 195 aprint_naive(": Temp Sensors\n"); 196 aprint_normal(": %s Temp Sensors\n", ia->ia_name); 197 } 198 199 static void 200 ecadc_refresh(struct sysmon_envsys *sme, envsys_data_t *sensor) 201 { 202 struct ecadc_softc *sc = sme->sme_cookie; 203 u_int i; 204 u_int8_t data[PCF8591_CHANNELS + 1]; 205 u_int8_t ctrl = PCF8591_CTRL_CH0 | PCF8591_CTRL_AUTOINC | 206 PCF8591_CTRL_OSCILLATOR; 207 208 iic_acquire_bus(sc->sc_tag, 0); 209 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, 210 &ctrl, 1, NULL, 0, 0)) { 211 iic_release_bus(sc->sc_tag, 0); 212 return; 213 } 214 /* NB: first byte out is stale, so read num_channels + 1 */ 215 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, 216 NULL, 0, data, PCF8591_CHANNELS + 1, 0)) { 217 iic_release_bus(sc->sc_tag, 0); 218 return; 219 } 220 iic_release_bus(sc->sc_tag, 0); 221 222 /* We only support temperature channels. */ 223 for (i = 0; i < sc->sc_nchan; i++) { 224 struct ecadc_channel *chp = &sc->sc_channels[i]; 225 226 if (chp->chan_xlate) 227 chp->chan_sensor.value_cur = 273150000 + 1000000 * 228 chp->chan_xlate[data[1 + chp->chan_num]]; 229 else 230 chp->chan_sensor.value_cur = 273150000 + 231 chp->chan_factor * data[1 + chp->chan_num]; 232 233 chp->chan_sensor.state = ENVSYS_SVALID; 234 chp->chan_sensor.flags &= ~ENVSYS_FNEED_REFRESH; 235 chp->chan_sensor.flags |= ENVSYS_FMONLIMITS; 236 } 237 } 238 239 static void 240 ecadc_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata, 241 sysmon_envsys_lim_t *limits, uint32_t *props) 242 { 243 struct ecadc_softc *sc = sme->sme_cookie; 244 int i; 245 246 for (i = 0; i < sc->sc_nchan; i++) { 247 if (edata != &sc->sc_channels[i].chan_sensor) 248 continue; 249 *props |= PROP_WARNMIN|PROP_WARNMAX|PROP_CRITMAX; 250 limits->sel_warnmin = sc->sc_channels[i].chan_min; 251 limits->sel_warnmax = sc->sc_channels[i].chan_warn; 252 limits->sel_critmax = sc->sc_channels[i].chan_crit; 253 return; 254 } 255 } 256