Index: rktemp.c =================================================================== RCS file: /cvs/src/sys/dev/fdt/rktemp.c,v retrieving revision 1.12 diff -u -p -r1.12 rktemp.c --- rktemp.c 5 Mar 2023 09:57:32 -0000 1.12 +++ rktemp.c 12 Nov 2023 15:14:26 -0000 @@ -15,10 +15,13 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include "kstat.h" + #include #include #include -#include +#include +#include #include #include @@ -256,12 +259,10 @@ struct rktemp_softc { const struct rktemp_entry *sc_temps; int sc_ntemps; - - struct ksensor sc_sensors[3]; int sc_nsensors; - struct ksensordev sc_sensordev; struct thermal_sensor sc_ts; + struct kstat *sc_kstat; }; int rktemp_match(struct device *, void *, void *); @@ -279,9 +280,12 @@ void rktemp_rk3568_init(struct rktemp_so int32_t rktemp_calc_code(struct rktemp_softc *, int32_t); int32_t rktemp_calc_temp(struct rktemp_softc *, int32_t); int rktemp_valid(struct rktemp_softc *, int32_t); -void rktemp_refresh_sensors(void *); int32_t rktemp_get_temperature(void *, uint32_t *); +#if NKSTAT > 0 +void rktemp_attach_kstat(struct rktemp_softc *, const char * const []); +#endif + int rktemp_match(struct device *parent, void *match, void *aux) { @@ -424,17 +428,9 @@ rktemp_attach(struct device *parent, str HWRITE4(sc, TSADC_AUTO_CON, auto_con); /* Register sensors. */ - strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname, - sizeof(sc->sc_sensordev.xname)); - for (i = 0; i < sc->sc_nsensors; i++) { - strlcpy(sc->sc_sensors[i].desc, names[i], - sizeof(sc->sc_sensors[i].desc)); - sc->sc_sensors[i].type = SENSOR_TEMP; - sc->sc_sensors[i].flags = SENSOR_FINVALID; - sensor_attach(&sc->sc_sensordev, &sc->sc_sensors[i]); - } - sensordev_install(&sc->sc_sensordev); - sensor_task_register(sc, rktemp_refresh_sensors, 5); +#if NKSTAT > 0 + rktemp_attach_kstat(sc, names); +#endif sc->sc_ts.ts_node = sc->sc_node; sc->sc_ts.ts_cookie = sc; @@ -549,24 +545,6 @@ rktemp_valid(struct rktemp_softc *sc, in return 1; } -void -rktemp_refresh_sensors(void *arg) -{ - struct rktemp_softc *sc = arg; - int32_t code, temp; - int i; - - for (i = 0; i < sc->sc_nsensors; i++) { - code = HREAD4(sc, TSADC_DATA0 + i * 4); - temp = rktemp_calc_temp(sc, code); - sc->sc_sensors[i].value = 273150000 + 1000 * temp; - if (rktemp_valid(sc, code)) - sc->sc_sensors[i].flags &= ~SENSOR_FINVALID; - else - sc->sc_sensors[i].flags |= SENSOR_FINVALID; - } -} - int32_t rktemp_get_temperature(void *cookie, uint32_t *cells) { @@ -583,3 +561,73 @@ rktemp_get_temperature(void *cookie, uin else return THERMAL_SENSOR_MAX; } + +#if NKSTAT > 0 + +static int +rktemp_kstat_read(struct kstat *ks) +{ + struct rktemp_softc *sc = ks->ks_softc; + struct kstat_kv *kvs = ks->ks_data; + struct timespec now, diff; + int i; + + getnanouptime(&now); + timespecsub(&now, &ks->ks_updated, &diff); + if (timespeccmp(&diff, &ks->ks_interval, <)) + return (0); + + for (i = 0; i < sc->sc_nsensors; i++) { + struct kstat_kv *kv = &kvs[i]; + int32_t code = HREAD4(sc, TSADC_DATA0 + i * 4); + int32_t temp; + + if (rktemp_valid(sc, code)) { + kv->kv_type = KSTAT_KV_T_TEMP; + temp = rktemp_calc_temp(sc, code); + kstat_kv_temp(kv) = 273150000 + 1000 * temp; + } else + kv->kv_type = KSTAT_KV_T_NULL; + } + + ks->ks_updated = now; + + return (0); +} + +void +rktemp_attach_kstat(struct rktemp_softc *sc, const char * const names[]) +{ + struct kstat *ks; + struct kstat_kv *kvs; + int i; + + ks = kstat_create(sc->sc_dev.dv_xname, 0, "tsadc", 0, + KSTAT_T_KV, 0); + if (ks == NULL) { + printf("%s: unable to attach kstat\n", sc->sc_dev.dv_xname); + return; + } + + kvs = mallocarray(sc->sc_nsensors, sizeof(*kvs), M_DEVBUF, + M_WAITOK|M_ZERO); + + for (i = 0; i < sc->sc_nsensors; i++) { + struct kstat_kv *kv = &kvs[i]; + + kstat_kv_init(kv, names[i], KSTAT_KV_T_TEMP); + } + + ks->ks_softc = sc; + ks->ks_data = kvs; + ks->ks_datalen = sc->sc_nsensors * sizeof(*kvs); + ks->ks_read = rktemp_kstat_read; + ks->ks_interval.tv_sec = 5; + + kstat_install(ks); + + sc->sc_kstat = ks; +} + +#endif /* NKSTAT > 0 */ +