1 /* $OpenBSD: bcm2835_temp.c,v 1.2 2021/10/24 17:52:26 mpi Exp $ */ 2 /* 3 * Copyright (c) 2018 Mark Kettenis <kettenis@openbsd.org> 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/device.h> 21 #include <sys/sensors.h> 22 23 #include <machine/bus.h> 24 #include <machine/fdt.h> 25 26 #include <dev/ofw/openfirm.h> 27 #include <dev/ofw/fdt.h> 28 29 /* Registers */ 30 #define TS_TSENSCTL 0x0000 31 #define TS_TSENSSTAT 0x0004 32 #define TS_TSENSSTAT_VALID (1 << 10) 33 #define TS_TSENSSTAT_DATA(x) ((x) & 0x3ff) 34 35 #define HREAD4(sc, reg) \ 36 (bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))) 37 #define HWRITE4(sc, reg, val) \ 38 bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val)) 39 40 struct bcmtemp_softc { 41 struct device sc_dev; 42 bus_space_tag_t sc_iot; 43 bus_space_handle_t sc_ioh; 44 45 int32_t sc_slope; 46 int32_t sc_offset; 47 48 struct ksensor sc_sensor; 49 struct ksensordev sc_sensordev; 50 }; 51 52 int bcmtemp_match(struct device *, void *, void *); 53 void bcmtemp_attach(struct device *, struct device *, void *); 54 55 const struct cfattach bcmtemp_ca = { 56 sizeof (struct bcmtemp_softc), bcmtemp_match, bcmtemp_attach 57 }; 58 59 struct cfdriver bcmtemp_cd = { 60 NULL, "bcmtemp", DV_DULL 61 }; 62 63 void bcmtemp_refresh_sensors(void *); 64 65 int 66 bcmtemp_match(struct device *parent, void *match, void *aux) 67 { 68 struct fdt_attach_args *faa = aux; 69 70 return (OF_is_compatible(faa->fa_node, "brcm,bcm2836-thermal") || 71 OF_is_compatible(faa->fa_node, "brcm,bcm2837-thermal")); 72 } 73 74 void 75 bcmtemp_attach(struct device *parent, struct device *self, void *aux) 76 { 77 struct bcmtemp_softc *sc = (struct bcmtemp_softc *)self; 78 struct fdt_attach_args *faa = aux; 79 80 if (faa->fa_nreg < 1) { 81 printf(": no registers\n"); 82 return; 83 } 84 85 sc->sc_iot = faa->fa_iot; 86 if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, 87 faa->fa_reg[0].size, 0, &sc->sc_ioh)) { 88 printf(": can't map registers\n"); 89 return; 90 } 91 92 printf("\n"); 93 94 /* XXX fetch these from /thermal-zones. */ 95 sc->sc_slope = -538; 96 if (OF_is_compatible(faa->fa_node, "brcm,bcm2836-thermal")) 97 sc->sc_offset = 407000; 98 else 99 sc->sc_offset = 412000; 100 101 /* Register sensors. */ 102 strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname, 103 sizeof(sc->sc_sensordev.xname)); 104 sc->sc_sensor.type = SENSOR_TEMP; 105 sc->sc_sensor.flags = SENSOR_FINVALID; 106 sensor_attach(&sc->sc_sensordev, &sc->sc_sensor); 107 sensordev_install(&sc->sc_sensordev); 108 sensor_task_register(sc, bcmtemp_refresh_sensors, 5); 109 } 110 111 void 112 bcmtemp_refresh_sensors(void *arg) 113 { 114 struct bcmtemp_softc *sc = arg; 115 int32_t code, temp; 116 117 code = HREAD4(sc, TS_TSENSSTAT); 118 temp = sc->sc_offset + TS_TSENSSTAT_DATA(code) * sc->sc_slope; 119 sc->sc_sensor.value = 273150000 + 1000 * temp; 120 if (code & TS_TSENSSTAT_VALID) 121 sc->sc_sensor.flags &= ~SENSOR_FINVALID; 122 else 123 sc->sc_sensor.flags |= SENSOR_FINVALID; 124 } 125