2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright 2020 Michal Meloun <mmel@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/param.h>
29 #include <sys/systm.h>
32 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <sys/sysctl.h>
38 #include <machine/bus.h>
39 #include <machine/cpu.h>
41 #include <dev/clk/clk.h>
42 #include <dev/ofw/ofw_bus_subr.h>
44 #include "tegra_soctherm_if.h"
54 struct tegra210_coretemp_softc {
64 coretemp_get_val_sysctl(SYSCTL_HANDLER_ARGS)
68 struct tegra210_coretemp_softc *sc;
73 dev = (device_t) arg1;
74 sc = device_get_softc(dev);
78 rv = TEGRA_SOCTHERM_GET_TEMPERATURE(sc->tsens_dev, sc->dev,
81 device_printf(sc->dev,
82 "Cannot read temperature sensor %u: %d\n",
83 (unsigned int)sc->tsens_id, rv);
93 val = (sc->core_max_temp - temp) / 1000;
95 case CORETEMP_RESOLUTION:
99 val = sc->core_max_temp / 100;
105 if ((temp > sc->core_max_temp) && !sc->overheat_log) {
106 sc->overheat_log = 1;
109 * Check for Critical Temperature Status and Critical
110 * Temperature Log. It doesn't really matter if the
111 * current temperature is invalid because the "Critical
112 * Temperature Log" bit will tell us if the Critical
113 * Temperature has * been reached in past. It's not
114 * directly related to the current temperature.
116 * If we reach a critical level, allow devctl(4)
117 * to catch this and shutdown the system.
119 device_printf(dev, "critical temperature detected, "
120 "suggest system shutdown\n");
121 snprintf(stemp, sizeof(stemp), "%d", val);
122 devctl_notify("coretemp", "Thermal", stemp,
125 sc->overheat_log = 0;
128 return (sysctl_handle_int(oidp, 0, val, req));
132 tegra210_coretemp_ofw_parse(struct tegra210_coretemp_softc *sc)
135 phandle_t node, xnode;
139 node = ofw_bus_find_child(node, "thermal-zones");
141 device_printf(sc->dev, "Cannot find 'thermal-zones'.\n");
145 node = ofw_bus_find_child(node, "cpu");
147 device_printf(sc->dev, "Cannot find 'cpu'\n");
150 rv = ofw_bus_parse_xref_list_alloc(node, "thermal-sensors",
151 "#thermal-sensor-cells", 0, &xnode, &ncells, &cells);
153 device_printf(sc->dev,
154 "Cannot parse 'thermal-sensors' property.\n");
158 device_printf(sc->dev,
159 "Invalid format of 'thermal-sensors' property(%d).\n",
164 sc->tsens_id = 0x100 + sc->cpu_id;
167 sc->tsens_dev = OF_device_from_xref(xnode);
168 if (sc->tsens_dev == NULL) {
169 device_printf(sc->dev,
170 "Cannot find thermal sensors device.");
177 tegra210_coretemp_identify(driver_t *driver, device_t parent)
181 root = OF_finddevice("/");
182 if (!ofw_bus_node_is_compatible(root, "nvidia,tegra210"))
184 if (device_find_child(parent, "tegra210_coretemp", -1) != NULL)
186 if (BUS_ADD_CHILD(parent, 0, "tegra210_coretemp", -1) == NULL)
187 device_printf(parent, "add child failed\n");
191 tegra210_coretemp_probe(device_t dev)
194 device_set_desc(dev, "CPU Thermal Sensor");
199 tegra210_coretemp_attach(device_t dev)
201 struct tegra210_coretemp_softc *sc;
203 struct sysctl_oid *oid;
204 struct sysctl_ctx_list *ctx;
207 sc = device_get_softc(dev);
209 sc->cpu_id = device_get_unit(dev);
210 sc->core_max_temp = 102000;
211 pdev = device_get_parent(dev);
213 rv = tegra210_coretemp_ofw_parse(sc);
217 ctx = device_get_sysctl_ctx(dev);
219 oid = SYSCTL_ADD_NODE(ctx,
220 SYSCTL_CHILDREN(device_get_sysctl_tree(pdev)), OID_AUTO,
221 "coretemp", CTLFLAG_RD, NULL, "Per-CPU thermal information");
224 * Add the MIBs to dev.cpu.N and dev.cpu.N.coretemp.
226 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(pdev)),
227 OID_AUTO, "temperature", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
228 dev, CORETEMP_TEMP, coretemp_get_val_sysctl, "IK",
229 "Current temperature");
230 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "delta",
231 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, CORETEMP_DELTA,
232 coretemp_get_val_sysctl, "I",
233 "Delta between TCC activation and current temperature");
234 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "resolution",
235 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, CORETEMP_RESOLUTION,
236 coretemp_get_val_sysctl, "I",
237 "Resolution of CPU thermal sensor");
238 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "tjmax",
239 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, CORETEMP_TJMAX,
240 coretemp_get_val_sysctl, "IK",
241 "TCC activation temperature");
247 tegra210_coretemp_detach(device_t dev)
252 static device_method_t tegra210_coretemp_methods[] = {
253 /* Device interface */
254 DEVMETHOD(device_identify, tegra210_coretemp_identify),
255 DEVMETHOD(device_probe, tegra210_coretemp_probe),
256 DEVMETHOD(device_attach, tegra210_coretemp_attach),
257 DEVMETHOD(device_detach, tegra210_coretemp_detach),
262 static DEFINE_CLASS_0(tegra210_coretemp, tegra210_coretemp_driver,
263 tegra210_coretemp_methods, sizeof(struct tegra210_coretemp_softc));
264 DRIVER_MODULE(tegra210_coretemp, cpu, tegra210_coretemp_driver, NULL, NULL);