2 * Copyright (c) 2016 Michal Meloun <mmel@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/sysctl.h>
40 #include <machine/bus.h>
41 #include <machine/cpu.h>
43 #include <dev/extres/clk/clk.h>
44 #include <dev/ofw/ofw_bus_subr.h>
46 #include "tegra_soctherm_if.h"
55 struct tegra124_coretemp_softc {
65 coretemp_get_val_sysctl(SYSCTL_HANDLER_ARGS)
69 struct tegra124_coretemp_softc *sc;
73 dev = (device_t) arg1;
74 sc = device_get_softc(dev);
77 rv = TEGRA_SOCTHERM_GET_TEMPERATURE(sc->tsens_dev, sc->dev,
80 device_printf(sc->dev,
81 "Cannot read temperature sensor %d: %d\n",
92 val = (sc->core_max_temp - temp) / 1000;
94 case CORETEMP_RESOLUTION:
98 val = sc->core_max_temp / 100;
103 if ((temp > sc->core_max_temp) && !sc->overheat_log) {
104 sc->overheat_log = 1;
107 * Check for Critical Temperature Status and Critical
108 * Temperature Log. It doesn't really matter if the
109 * current temperature is invalid because the "Critical
110 * Temperature Log" bit will tell us if the Critical
111 * Temperature has * been reached in past. It's not
112 * directly related to the current temperature.
114 * If we reach a critical level, allow devctl(4)
115 * to catch this and shutdown the system.
117 device_printf(dev, "critical temperature detected, "
118 "suggest system shutdown\n");
119 snprintf(stemp, sizeof(stemp), "%d", val);
120 devctl_notify("coretemp", "Thermal", stemp,
123 sc->overheat_log = 0;
126 return (sysctl_handle_int(oidp, 0, val, req));
130 tegra124_coretemp_ofw_parse(struct tegra124_coretemp_softc *sc)
133 phandle_t node, xnode;
137 node = ofw_bus_find_child(node, "thermal-zones");
139 device_printf(sc->dev, "Cannot find 'thermal-zones'.\n");
143 node = ofw_bus_find_child(node, "cpu");
145 device_printf(sc->dev, "Cannot find 'cpu'\n");
148 rv = ofw_bus_parse_xref_list_alloc(node, "thermal-sensors",
149 "#thermal-sensor-cells", 0, &xnode, &ncells, &cells);
151 device_printf(sc->dev,
152 "Cannot parse 'thermal-sensors' property.\n");
156 device_printf(sc->dev,
157 "Invalid format of 'thermal-sensors' property(%d).\n",
162 sc->tsens_id = 0x100 + sc->cpu_id; //cells[0];
165 sc->tsens_dev = OF_device_from_xref(xnode);
166 if (sc->tsens_dev == NULL) {
167 device_printf(sc->dev,
168 "Cannot find thermal sensors device.");
175 tegra124_coretemp_identify(driver_t *driver, device_t parent)
179 root = OF_finddevice("/");
180 if (!ofw_bus_node_is_compatible(root, "nvidia,tegra124"))
182 if (device_find_child(parent, "tegra124_coretemp", -1) != NULL)
184 if (BUS_ADD_CHILD(parent, 0, "tegra124_coretemp", -1) == NULL)
185 device_printf(parent, "add child failed\n");
189 tegra124_coretemp_probe(device_t dev)
192 device_set_desc(dev, "CPU Thermal Sensor");
197 tegra124_coretemp_attach(device_t dev)
199 struct tegra124_coretemp_softc *sc;
201 struct sysctl_oid *oid;
202 struct sysctl_ctx_list *ctx;
205 sc = device_get_softc(dev);
207 sc->cpu_id = device_get_unit(dev);
208 sc->core_max_temp = 102000;
209 pdev = device_get_parent(dev);
211 rv = tegra124_coretemp_ofw_parse(sc);
215 ctx = device_get_sysctl_ctx(dev);
217 oid = SYSCTL_ADD_NODE(ctx,
218 SYSCTL_CHILDREN(device_get_sysctl_tree(pdev)), OID_AUTO,
219 "coretemp", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
220 "Per-CPU thermal information");
223 * Add the MIBs to dev.cpu.N and dev.cpu.N.coretemp.
225 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(pdev)),
226 OID_AUTO, "temperature", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
227 dev, CORETEMP_TEMP, coretemp_get_val_sysctl, "IK",
228 "Current temperature");
229 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "delta",
230 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, CORETEMP_DELTA,
231 coretemp_get_val_sysctl, "I",
232 "Delta between TCC activation and current temperature");
233 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "resolution",
234 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, CORETEMP_RESOLUTION,
235 coretemp_get_val_sysctl, "I",
236 "Resolution of CPU thermal sensor");
237 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "tjmax",
238 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, CORETEMP_TJMAX,
239 coretemp_get_val_sysctl, "IK",
240 "TCC activation temperature");
246 tegra124_coretemp_detach(device_t dev)
251 static device_method_t tegra124_coretemp_methods[] = {
252 /* Device interface */
253 DEVMETHOD(device_identify, tegra124_coretemp_identify),
254 DEVMETHOD(device_probe, tegra124_coretemp_probe),
255 DEVMETHOD(device_attach, tegra124_coretemp_attach),
256 DEVMETHOD(device_detach, tegra124_coretemp_detach),
261 static DEFINE_CLASS_0(tegra124_coretemp, tegra124_coretemp_driver,
262 tegra124_coretemp_methods, sizeof(struct tegra124_coretemp_softc));
263 DRIVER_MODULE(tegra124_coretemp, cpu, tegra124_coretemp_driver, NULL, NULL);