1 /* $OpenBSD: pcf8591_envctrl.c,v 1.7 2021/10/24 17:05:03 mpi Exp $ */ 2 3 /* 4 * Copyright (c) 2006 Damien Miller <djm@openbsd.org> 5 * Copyright (c) 2007 Mark Kettenis <kettenis@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/param.h> 21 #include <sys/systm.h> 22 #include <sys/device.h> 23 #include <sys/sensors.h> 24 25 #include <dev/ofw/openfirm.h> 26 #include <dev/i2c/i2cvar.h> 27 28 #define PCF8591_CHANNELS 4 29 30 #define PCF8591_CTRL_CH0 0x00 31 #define PCF8591_CTRL_CH1 0x01 32 #define PCF8591_CTRL_CH2 0x02 33 #define PCF8591_CTRL_CH3 0x03 34 #define PCF8591_CTRL_AUTOINC 0x04 35 #define PCF8591_CTRL_OSCILLATOR 0x40 36 37 struct ecadc_channel { 38 u_int chan_num; 39 struct ksensor chan_sensor; 40 u_char *chan_xlate; 41 int64_t chan_factor; 42 int64_t chan_min; 43 int64_t chan_warn; 44 int64_t chan_crit; 45 }; 46 47 struct ecadc_softc { 48 struct device sc_dev; 49 i2c_tag_t sc_tag; 50 i2c_addr_t sc_addr; 51 u_char sc_ps_xlate[256]; 52 u_char sc_cpu_xlate[256]; 53 u_int sc_nchan; 54 struct ecadc_channel sc_channels[PCF8591_CHANNELS]; 55 struct ksensordev sc_sensordev; 56 }; 57 58 int ecadc_match(struct device *, void *, void *); 59 void ecadc_attach(struct device *, struct device *, void *); 60 void ecadc_refresh(void *); 61 62 const struct cfattach ecadc_ca = { 63 sizeof(struct ecadc_softc), ecadc_match, ecadc_attach 64 }; 65 66 struct cfdriver ecadc_cd = { 67 NULL, "ecadc", DV_DULL 68 }; 69 70 int 71 ecadc_match(struct device *parent, void *match, void *aux) 72 { 73 struct i2c_attach_args *ia = aux; 74 75 if (strcmp(ia->ia_name, "ecadc") != 0) 76 return (0); 77 78 return (1); 79 } 80 81 void 82 ecadc_attach(struct device *parent, struct device *self, void *aux) 83 { 84 struct ecadc_softc *sc = (struct ecadc_softc *)self; 85 u_char term[256]; 86 u_char *cp, *desc; 87 int64_t min, warn, crit, num, den; 88 u_int8_t junk[PCF8591_CHANNELS + 1]; 89 struct i2c_attach_args *ia = aux; 90 struct ksensor *sensor; 91 int len, addr, chan, node = *(int *)ia->ia_cookie; 92 u_int i; 93 94 if ((len = OF_getprop(node, "thermisters", term, 95 sizeof(term))) < 0) { 96 printf(": couldn't find \"thermisters\" property\n"); 97 return; 98 } 99 100 if (OF_getprop(node, "cpu-temp-factors", &sc->sc_cpu_xlate[2], 101 sizeof(sc->sc_cpu_xlate) - 2) < 0) { 102 printf(": couldn't find \"cpu-temp-factors\" property\n"); 103 return; 104 } 105 sc->sc_cpu_xlate[0] = sc->sc_cpu_xlate[1] = sc->sc_cpu_xlate[2]; 106 107 /* Only the Sun Enterprise 450 has these. */ 108 OF_getprop(node, "ps-temp-factors", &sc->sc_ps_xlate[2], 109 sizeof(sc->sc_ps_xlate) - 2); 110 sc->sc_ps_xlate[0] = sc->sc_ps_xlate[1] = sc->sc_ps_xlate[2]; 111 112 cp = term; 113 while (cp < term + len) { 114 addr = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4; 115 chan = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4; 116 min = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4; 117 warn = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4; 118 crit = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4; 119 num = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4; 120 den = cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3]; cp += 4; 121 desc = cp; 122 while (cp < term + len && *cp++); 123 124 if (addr != (ia->ia_addr << 1)) 125 continue; 126 127 if (num == 0 || den == 0) 128 num = den = 1; 129 130 sc->sc_channels[sc->sc_nchan].chan_num = chan; 131 132 sensor = &sc->sc_channels[sc->sc_nchan].chan_sensor; 133 sensor->type = SENSOR_TEMP; 134 strlcpy(sensor->desc, desc, sizeof(sensor->desc)); 135 136 if (strncmp(desc, "CPU", 3) == 0) 137 sc->sc_channels[sc->sc_nchan].chan_xlate = 138 sc->sc_cpu_xlate; 139 else if (strncmp(desc, "PS", 2) == 0) 140 sc->sc_channels[sc->sc_nchan].chan_xlate = 141 sc->sc_ps_xlate; 142 else 143 sc->sc_channels[sc->sc_nchan].chan_factor = 144 (1000000 * num) / den; 145 sc->sc_channels[sc->sc_nchan].chan_min = 146 273150000 + 1000000 * min; 147 sc->sc_channels[sc->sc_nchan].chan_warn = 148 273150000 + 1000000 * warn; 149 sc->sc_channels[sc->sc_nchan].chan_crit = 150 273150000 + 1000000 * crit; 151 sc->sc_nchan++; 152 } 153 154 sc->sc_tag = ia->ia_tag; 155 sc->sc_addr = ia->ia_addr; 156 157 iic_acquire_bus(sc->sc_tag, 0); 158 159 /* Try a read now, so we can fail if it doesn't work */ 160 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, 161 NULL, 0, junk, sc->sc_nchan + 1, 0)) { 162 printf(": read failed\n"); 163 iic_release_bus(sc->sc_tag, 0); 164 return; 165 } 166 167 iic_release_bus(sc->sc_tag, 0); 168 169 /* Initialize sensor data. */ 170 strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname, 171 sizeof(sc->sc_sensordev.xname)); 172 173 for (i = 0; i < sc->sc_nchan; i++) 174 sensor_attach(&sc->sc_sensordev, 175 &sc->sc_channels[i].chan_sensor); 176 177 if (sensor_task_register(sc, ecadc_refresh, 5) == NULL) { 178 printf(": unable to register update task\n"); 179 return; 180 } 181 182 sensordev_install(&sc->sc_sensordev); 183 184 printf("\n"); 185 } 186 187 void 188 ecadc_refresh(void *arg) 189 { 190 struct ecadc_softc *sc = arg; 191 u_int i; 192 u_int8_t data[PCF8591_CHANNELS + 1]; 193 u_int8_t ctrl = PCF8591_CTRL_CH0 | PCF8591_CTRL_AUTOINC | 194 PCF8591_CTRL_OSCILLATOR; 195 196 iic_acquire_bus(sc->sc_tag, 0); 197 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, 198 &ctrl, 1, NULL, 0, 0)) { 199 iic_release_bus(sc->sc_tag, 0); 200 return; 201 } 202 /* NB: first byte out is stale, so read num_channels + 1 */ 203 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, 204 NULL, 0, data, PCF8591_CHANNELS + 1, 0)) { 205 iic_release_bus(sc->sc_tag, 0); 206 return; 207 } 208 iic_release_bus(sc->sc_tag, 0); 209 210 /* We only support temperature channels. */ 211 for (i = 0; i < sc->sc_nchan; i++) { 212 struct ecadc_channel *chp = &sc->sc_channels[i]; 213 214 if (chp->chan_xlate) 215 chp->chan_sensor.value = 273150000 + 1000000 * 216 chp->chan_xlate[data[1 + chp->chan_num]]; 217 else 218 chp->chan_sensor.value = 273150000 + 219 chp->chan_factor * data[1 + chp->chan_num]; 220 221 chp->chan_sensor.status = SENSOR_S_OK; 222 if (chp->chan_sensor.value < chp->chan_min) 223 chp->chan_sensor.status = SENSOR_S_UNKNOWN; 224 if (chp->chan_sensor.value > chp->chan_warn) 225 chp->chan_sensor.status = SENSOR_S_WARN; 226 if (chp->chan_sensor.value > chp->chan_crit) 227 chp->chan_sensor.status = SENSOR_S_CRIT; 228 } 229 } 230