1 /* $OpenBSD: qctsens.c,v 1.1 2023/06/27 22:38:46 patrick Exp $ */ 2 /* 3 * Copyright (c) 2023 Patrick Wildt <patrick@blueri.se> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/param.h> 19 #include <sys/systm.h> 20 #include <sys/malloc.h> 21 #include <sys/device.h> 22 #include <sys/sensors.h> 23 24 #include <machine/intr.h> 25 #include <machine/bus.h> 26 #include <machine/fdt.h> 27 28 #include <dev/ofw/openfirm.h> 29 #include <dev/ofw/ofw_clock.h> 30 #include <dev/ofw/ofw_misc.h> 31 #include <dev/ofw/ofw_thermal.h> 32 #include <dev/ofw/fdt.h> 33 34 /* Registers (sensor block) */ 35 #define TSENS_Sn_STATUS(n) (0xa0 + (4 * (n))) 36 #define TSENS_Sn_VALID (1 << 21) 37 #define TSENS_Sn_TEMP(x) ((x) & 0xfff) 38 39 /* Registers (config block) */ 40 #define TSENS_HW_VER 0x00 41 #define TSENS_CTRL 0x04 42 #define TSENS_CTRL_EN (1 << 0) 43 #define TSENS_CTRL_Sn_EN(x) (1 << ((x) + 3)) 44 45 #define TSENS_NUM_SENSORS 16 46 47 struct qctsens_softc { 48 struct device sc_dev; 49 bus_space_tag_t sc_iot; 50 bus_space_handle_t sc_ioh; 51 bus_space_handle_t sc_ioh_conf; 52 53 int sc_node; 54 55 struct ksensordev sc_sensordev; 56 struct ksensor sc_sensor[TSENS_NUM_SENSORS]; 57 58 struct thermal_sensor sc_ts; 59 }; 60 61 int qctsens_match(struct device *, void *, void *); 62 void qctsens_attach(struct device *, struct device *, void *); 63 64 const struct cfattach qctsens_ca = { 65 sizeof (struct qctsens_softc), qctsens_match, qctsens_attach 66 }; 67 68 struct cfdriver qctsens_cd = { 69 NULL, "qctsens", DV_DULL 70 }; 71 72 void qctsens_refresh_sensors(void *); 73 int32_t qctsens_get_temperature(void *, uint32_t *); 74 void qctsens_attach_sensors(struct qctsens_softc *); 75 76 int 77 qctsens_match(struct device *parent, void *match, void *aux) 78 { 79 struct fdt_attach_args *faa = aux; 80 81 return OF_is_compatible(faa->fa_node, "qcom,tsens-v2"); 82 } 83 84 void 85 qctsens_attach(struct device *parent, struct device *self, void *aux) 86 { 87 struct qctsens_softc *sc = (struct qctsens_softc *)self; 88 struct fdt_attach_args *faa = aux; 89 uint32_t reg; 90 91 if (faa->fa_nreg < 1) { 92 printf(": no registers\n"); 93 return; 94 } 95 96 sc->sc_node = faa->fa_node; 97 sc->sc_iot = faa->fa_iot; 98 if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, 99 faa->fa_reg[0].size, 0, &sc->sc_ioh)) { 100 printf(": can't map registers (sensors)\n"); 101 return; 102 } 103 if (bus_space_map(sc->sc_iot, faa->fa_reg[1].addr, 104 faa->fa_reg[1].size, 0, &sc->sc_ioh_conf)) { 105 printf(": can't map registers (config)\n"); 106 return; 107 } 108 109 printf("\n"); 110 111 reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh_conf, TSENS_CTRL); 112 if ((reg & TSENS_CTRL_EN) == 0) 113 return; 114 115 qctsens_attach_sensors(sc); 116 117 sc->sc_ts.ts_node = sc->sc_node; 118 sc->sc_ts.ts_cookie = sc; 119 sc->sc_ts.ts_get_temperature = qctsens_get_temperature; 120 thermal_sensor_register(&sc->sc_ts); 121 } 122 123 void 124 qctsens_attach_sensors(struct qctsens_softc *sc) 125 { 126 char nodename[32]; 127 uint32_t propdata[4]; 128 uint32_t phandle, reg; 129 int node, len, sidx; 130 131 phandle = OF_getpropint(sc->sc_node, "phandle", 0); 132 if (phandle == 0) { 133 printf("%s: missing phandle on node\n", sc->sc_dev.dv_xname); 134 return; 135 } 136 137 reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh_conf, TSENS_CTRL); 138 node = OF_getnodebyname(0, "thermal-zones"); 139 for (node = OF_child(node); node != 0; node = OF_peer(node)) { 140 len = OF_getpropintarray(node, "thermal-sensors", propdata, 141 sizeof(propdata)); 142 143 if (len != 8 || propdata[0] != phandle || propdata[1] >= 16) 144 continue; 145 146 len = OF_getprop(node, "name", nodename, sizeof(nodename)); 147 len = strlen(nodename); 148 if (strcmp("-thermal", &nodename[len - 8]) != 0) 149 continue; 150 151 nodename[len - 8] = '\0'; 152 sidx = propdata[1]; 153 154 if ((reg & TSENS_CTRL_Sn_EN(sidx)) == 0) 155 continue; 156 157 strlcpy(sc->sc_sensor[sidx].desc, nodename, 158 sizeof(sc->sc_sensor[sidx].desc)); 159 sc->sc_sensor[sidx].type = SENSOR_TEMP; 160 sc->sc_sensor[sidx].flags = SENSOR_FINVALID; 161 sensor_attach(&sc->sc_sensordev, &sc->sc_sensor[sidx]); 162 } 163 164 strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname, 165 sizeof(sc->sc_sensordev.xname)); 166 sensordev_install(&sc->sc_sensordev); 167 sensor_task_register(sc, qctsens_refresh_sensors, 1); 168 } 169 170 void 171 qctsens_refresh_sensors(void *arg) 172 { 173 struct qctsens_softc *sc = arg; 174 int32_t reg, temp; 175 int id; 176 177 for (id = 0; id < TSENS_NUM_SENSORS; id++) { 178 if (sc->sc_sensor[id].type != SENSOR_TEMP) 179 continue; 180 reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 181 TSENS_Sn_STATUS(id)); 182 temp = TSENS_Sn_TEMP(reg); 183 if (reg & TSENS_Sn_VALID) { 184 sc->sc_sensor[id].value = 273150000 + 100000 * temp; 185 sc->sc_sensor[id].flags &= ~SENSOR_FINVALID; 186 } else { 187 sc->sc_sensor[id].flags = SENSOR_FINVALID; 188 } 189 } 190 } 191 192 int32_t 193 qctsens_get_temperature(void *cookie, uint32_t *cells) 194 { 195 struct qctsens_softc *sc = cookie; 196 uint32_t id = cells[0]; 197 int32_t reg, temp; 198 199 reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, TSENS_Sn_STATUS(id)); 200 temp = 273150000 + 100000 * TSENS_Sn_TEMP(reg); 201 202 if (reg & TSENS_Sn_VALID) 203 return temp; 204 205 return THERMAL_SENSOR_MAX; 206 } 207