1 /*
2 * Copyright (c) 2020 Alastair Poole <netstar@gmail.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <sys/param.h>
18 #include <sys/systm.h>
19 #include <sys/device.h>
20 #include <sys/sensors.h>
21
22 #include <machine/bus.h>
23 #include <machine/fdt.h>
24
25 #include <dev/ofw/openfirm.h>
26 #include <dev/ofw/fdt.h>
27
28 /* Registers */
29 #define BCMTMON_TSENSSTAT 0x200
30 #define BCMTMON_TSENSSTAT_VALID (1 << 10)
31 #define BCMTMON_TSENSSTAT_DATA(x) ((x) & 0x3ff)
32
33 #define HREAD4(sc, reg) \
34 (bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
35
36 struct bcmtmon_softc {
37 struct device sc_dev;
38 bus_space_tag_t sc_iot;
39 bus_space_handle_t sc_ioh;
40 bus_size_t sc_tsensstat;
41
42 int32_t sc_slope;
43 int32_t sc_offset;
44
45 struct ksensor sc_sensor;
46 struct ksensordev sc_sensordev;
47 };
48
49 int bcmtmon_match(struct device *, void *, void *);
50 void bcmtmon_attach(struct device *, struct device *, void *);
51
52 const struct cfattach bcmtmon_ca = {
53 sizeof (struct bcmtmon_softc), bcmtmon_match, bcmtmon_attach
54 };
55
56 struct cfdriver bcmtmon_cd = {
57 NULL, "bcmtmon", DV_DULL
58 };
59
60 void bcmtmon_refresh_sensors(void *);
61
62 int
bcmtmon_match(struct device * parent,void * match,void * aux)63 bcmtmon_match(struct device *parent, void *match, void *aux)
64 {
65 struct fdt_attach_args *faa = aux;
66
67 if (OF_is_compatible(faa->fa_node, "brcm,bcm2711-avs-monitor") ||
68 OF_is_compatible(faa->fa_node, "brcm,avs-tmon-bcm2711") ||
69 OF_is_compatible(faa->fa_node, "brcm,avs-tmon-bcm2838"))
70 return 10; /* Must beat syscon(4). */
71
72 return 0;
73 }
74
75 void
bcmtmon_attach(struct device * parent,struct device * self,void * aux)76 bcmtmon_attach(struct device *parent, struct device *self, void *aux)
77 {
78 struct bcmtmon_softc *sc = (struct bcmtmon_softc *)self;
79 struct fdt_attach_args *faa = aux;
80
81 if (faa->fa_nreg < 1) {
82 printf(": no registers\n");
83 return;
84 }
85
86 sc->sc_iot = faa->fa_iot;
87
88 if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
89 faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
90 printf(": can't map registers\n");
91 return;
92 }
93
94 printf("\n");
95
96 if (OF_is_compatible(faa->fa_node, "brcm,bcm2711-avs-monitor"))
97 sc->sc_tsensstat = BCMTMON_TSENSSTAT;
98
99 sc->sc_slope = -487;
100 sc->sc_offset = 410040;
101
102 /* Register sensors. */
103 strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
104 sizeof(sc->sc_sensordev.xname));
105 sc->sc_sensor.type = SENSOR_TEMP;
106 sc->sc_sensor.flags = SENSOR_FINVALID;
107 sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
108 sensordev_install(&sc->sc_sensordev);
109 sensor_task_register(sc, bcmtmon_refresh_sensors, 5);
110 }
111
112 void
bcmtmon_refresh_sensors(void * arg)113 bcmtmon_refresh_sensors(void *arg)
114 {
115 struct bcmtmon_softc *sc = arg;
116 int32_t code, temp;
117
118 code = HREAD4(sc, sc->sc_tsensstat);
119 temp = sc->sc_slope * BCMTMON_TSENSSTAT_DATA(code) + sc->sc_offset;
120
121 sc->sc_sensor.value = 273150000 + 1000 * temp;
122
123 if (code & BCMTMON_TSENSSTAT_VALID)
124 sc->sc_sensor.flags &= ~SENSOR_FINVALID;
125 else
126 sc->sc_sensor.flags |= SENSOR_FINVALID;
127 }
128