1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2010 Andreas Tobler 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/param.h> 29 #include <sys/bus.h> 30 #include <sys/systm.h> 31 #include <sys/module.h> 32 #include <sys/callout.h> 33 #include <sys/conf.h> 34 #include <sys/cpu.h> 35 #include <sys/ctype.h> 36 #include <sys/kernel.h> 37 #include <sys/reboot.h> 38 #include <sys/rman.h> 39 #include <sys/sysctl.h> 40 #include <sys/limits.h> 41 42 #include <machine/bus.h> 43 #include <machine/md_var.h> 44 45 #include <dev/iicbus/iicbus.h> 46 #include <dev/iicbus/iiconf.h> 47 48 #include <dev/ofw/openfirm.h> 49 #include <dev/ofw/ofw_bus.h> 50 #include <powerpc/powermac/powermac_thermal.h> 51 52 /* Drivebay sensor: LM75/DS1775. */ 53 #define DS1775_TEMP 0x0 54 55 /* Regular bus attachment functions */ 56 static int ds1775_probe(device_t); 57 static int ds1775_attach(device_t); 58 59 struct ds1775_softc { 60 struct pmac_therm sc_sensor; 61 device_t sc_dev; 62 struct intr_config_hook enum_hook; 63 uint32_t sc_addr; 64 }; 65 66 /* Utility functions */ 67 static int ds1775_sensor_read(struct ds1775_softc *sc); 68 static int ds1775_sensor_sysctl(SYSCTL_HANDLER_ARGS); 69 static void ds1775_start(void *xdev); 70 static int ds1775_read_2(device_t dev, uint32_t addr, uint8_t reg, 71 uint16_t *data); 72 73 static device_method_t ds1775_methods[] = { 74 /* Device interface */ 75 DEVMETHOD(device_probe, ds1775_probe), 76 DEVMETHOD(device_attach, ds1775_attach), 77 { 0, 0 }, 78 }; 79 80 static driver_t ds1775_driver = { 81 "ds1775", 82 ds1775_methods, 83 sizeof(struct ds1775_softc) 84 }; 85 86 DRIVER_MODULE(ds1775, iicbus, ds1775_driver, 0, 0); 87 88 static int 89 ds1775_read_2(device_t dev, uint32_t addr, uint8_t reg, uint16_t *data) 90 { 91 uint8_t buf[4]; 92 int err, try = 0; 93 94 struct iic_msg msg[2] = { 95 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® }, 96 { addr, IIC_M_RD, 2, buf }, 97 }; 98 99 for (;;) 100 { 101 err = iicbus_transfer(dev, msg, nitems(msg)); 102 if (err != 0) 103 goto retry; 104 105 *data = *((uint16_t*)buf); 106 return (0); 107 retry: 108 if (++try > 5) { 109 device_printf(dev, "iicbus read failed\n"); 110 return (-1); 111 } 112 pause("ds1775_read_2", hz); 113 } 114 } 115 116 static int 117 ds1775_probe(device_t dev) 118 { 119 const char *name, *compatible; 120 struct ds1775_softc *sc; 121 122 name = ofw_bus_get_name(dev); 123 compatible = ofw_bus_get_compat(dev); 124 125 if (!name) 126 return (ENXIO); 127 128 if (strcmp(name, "temp-monitor") != 0 || 129 (strcmp(compatible, "ds1775") != 0 && 130 strcmp(compatible, "lm75") != 0)) 131 return (ENXIO); 132 133 sc = device_get_softc(dev); 134 sc->sc_dev = dev; 135 sc->sc_addr = iicbus_get_addr(dev); 136 137 device_set_desc(dev, "Temp-Monitor DS1775"); 138 139 return (0); 140 } 141 142 static int 143 ds1775_attach(device_t dev) 144 { 145 struct ds1775_softc *sc; 146 147 sc = device_get_softc(dev); 148 149 sc->enum_hook.ich_func = ds1775_start; 150 sc->enum_hook.ich_arg = dev; 151 152 /* We have to wait until interrupts are enabled. I2C read and write 153 * only works if the interrupts are available. 154 * The unin/i2c is controlled by the htpic on unin. But this is not 155 * the master. The openpic on mac-io is controlling the htpic. 156 * This one gets attached after the mac-io probing and then the 157 * interrupts will be available. 158 */ 159 160 if (config_intrhook_establish(&sc->enum_hook) != 0) 161 return (ENOMEM); 162 163 return (0); 164 } 165 166 static void 167 ds1775_start(void *xdev) 168 { 169 phandle_t child; 170 struct ds1775_softc *sc; 171 struct sysctl_oid *oid, *sensroot_oid; 172 struct sysctl_ctx_list *ctx; 173 ssize_t plen; 174 int i; 175 char sysctl_name[40], sysctl_desc[40]; 176 177 device_t dev = (device_t)xdev; 178 179 sc = device_get_softc(dev); 180 181 child = ofw_bus_get_node(dev); 182 183 ctx = device_get_sysctl_ctx(dev); 184 sensroot_oid = SYSCTL_ADD_NODE(ctx, 185 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor", 186 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "DS1775 Sensor Information"); 187 188 if (OF_getprop(child, "hwsensor-zone", &sc->sc_sensor.zone, 189 sizeof(int)) < 0) 190 sc->sc_sensor.zone = 0; 191 192 plen = OF_getprop(child, "hwsensor-location", sc->sc_sensor.name, 193 sizeof(sc->sc_sensor.name)); 194 195 if (plen == -1) { 196 strcpy(sysctl_name, "sensor"); 197 } else { 198 for (i = 0; i < strlen(sc->sc_sensor.name); i++) { 199 sysctl_name[i] = tolower(sc->sc_sensor.name[i]); 200 if (isspace(sysctl_name[i])) 201 sysctl_name[i] = '_'; 202 } 203 sysctl_name[i] = 0; 204 } 205 206 /* Make up target temperatures. These are low, for the drive bay. */ 207 if (sc->sc_sensor.zone == 0) { 208 sc->sc_sensor.target_temp = 500 + ZERO_C_TO_K; 209 sc->sc_sensor.max_temp = 600 + ZERO_C_TO_K; 210 } 211 else { 212 sc->sc_sensor.target_temp = 300 + ZERO_C_TO_K; 213 sc->sc_sensor.max_temp = 600 + ZERO_C_TO_K; 214 } 215 216 sc->sc_sensor.read = 217 (int (*)(struct pmac_therm *sc))(ds1775_sensor_read); 218 pmac_thermal_sensor_register(&sc->sc_sensor); 219 220 sprintf(sysctl_desc,"%s %s", sc->sc_sensor.name, "(C)"); 221 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid), 222 OID_AUTO, sysctl_name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 223 "Sensor Information"); 224 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "temp", 225 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, 226 0, ds1775_sensor_sysctl, "IK", sysctl_desc); 227 228 config_intrhook_disestablish(&sc->enum_hook); 229 } 230 231 static int 232 ds1775_sensor_read(struct ds1775_softc *sc) 233 { 234 uint16_t buf[2]; 235 uint16_t read; 236 int err; 237 238 err = ds1775_read_2(sc->sc_dev, sc->sc_addr, DS1775_TEMP, buf); 239 if (err < 0) 240 return (-1); 241 242 read = *((int16_t *)buf); 243 244 /* The default mode of the ADC is 9 bit, the resolution is 0.5 C per 245 bit. The temperature is in tenth kelvin. 246 */ 247 return (((int16_t)(read) >> 7) * 5 + ZERO_C_TO_K); 248 } 249 250 static int 251 ds1775_sensor_sysctl(SYSCTL_HANDLER_ARGS) 252 { 253 device_t dev; 254 struct ds1775_softc *sc; 255 int error; 256 int temp; 257 258 dev = arg1; 259 sc = device_get_softc(dev); 260 261 temp = ds1775_sensor_read(sc); 262 if (temp < 0) 263 return (EIO); 264 265 error = sysctl_handle_int(oidp, &temp, 0, req); 266 267 return (error); 268 } 269