2 * Copyright (C) 2018 Justin Hibbits
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
18 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
19 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
21 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
28 #include <sys/param.h>
29 #include <sys/kernel.h>
31 #include <sys/module.h>
32 #include <sys/mutex.h>
34 #include <sys/sysctl.h>
35 #include <sys/systm.h>
40 #include <machine/bus.h>
42 #include <dev/ofw/openfirm.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
48 struct opal_sensor_softc {
52 uint32_t sc_min_handle;
53 uint32_t sc_max_handle;
58 #define SENSOR_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
59 #define SENSOR_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
60 #define SENSOR_LOCK_INIT(_sc) \
61 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
62 "opal-sensor", MTX_DEF)
65 * A bit confusing, maybe. There are two types of nodes with compatible strings
66 * of "ibm,opal-sensor". One hangs off /ibm,opal/, named "sensors", the other
67 * hangs off of this node. For newbus attachments, we have one node (opalsens)
68 * attach from opal0, and the others (opal_sensor) attach from opalsens. These
69 * are the real sensors.
71 enum opal_sensor_type {
72 OPAL_SENSOR_TEMP = 0, /* From OPAL: degC */
73 OPAL_SENSOR_FAN = 1, /* From OPAL: RPM */
74 OPAL_SENSOR_POWER = 2, /* From OPAL: W */
75 OPAL_SENSOR_IN = 3, /* From OPAL: mV */
76 OPAL_SENSOR_ENERGY = 4, /* From OPAL: uJ */
77 OPAL_SENSOR_CURR = 5, /* From OPAL: mA */
81 /* This must be kept sorted with the enum above. */
82 const char *opal_sensor_types[] = {
92 * Retrieve the raw value from OPAL. This will be cooked by the sysctl handler.
95 opal_sensor_get_val(struct opal_sensor_softc *sc, uint32_t key, uint64_t *val)
101 token = opal_alloc_async_token();
103 rv = opal_call(OPAL_SENSOR_READ, key, token, vtophys(&val32));
105 if (rv == OPAL_ASYNC_COMPLETION) {
106 /* Sleep a little to let things settle. */
108 bzero(&msg, sizeof(msg));
109 rv = opal_wait_completion(&msg, sizeof(msg), token);
111 if (rv == OPAL_SUCCESS)
112 val32 = msg.params[0];
116 if (rv == OPAL_SUCCESS)
121 opal_free_async_token(token);
126 opal_sensor_sysctl(SYSCTL_HANDLER_ARGS)
128 struct opal_sensor_softc *sc;
136 error = opal_sensor_get_val(sc, sensor, &sensval);
143 switch (sc->sc_type) {
144 case OPAL_SENSOR_TEMP:
145 result = result * 10 + 2731; /* Convert to K */
147 case OPAL_SENSOR_POWER:
148 result = result * 1000; /* Convert to mW */
152 error = sysctl_handle_int(oidp, &result, 0, req);
158 opal_sensor_probe(device_t dev)
160 if (!ofw_bus_is_compatible(dev, "ibm,opal-sensor"))
163 device_set_desc(dev, "OPAL sensor");
164 return (BUS_PROBE_GENERIC);
168 opal_sensor_attach(device_t dev)
170 struct opal_sensor_softc *sc;
171 struct sysctl_ctx_list *ctx;
172 struct sysctl_oid *tree;
178 sc = device_get_softc(dev);
181 node = ofw_bus_get_node(dev);
183 if (OF_getencprop(node, "sensor-data", &sensor_id, sizeof(sensor_id)) < 0) {
184 device_printf(dev, "Missing sensor ID\n");
187 if (OF_getprop(node, "sensor-type", type, sizeof(type)) < 0) {
188 device_printf(dev, "Missing sensor type\n");
193 for (i = 0; i < OPAL_SENSOR_MAX; i++) {
194 if (strcmp(type, opal_sensor_types[i]) == 0) {
199 if (sc->sc_type == -1) {
200 device_printf(dev, "Unknown sensor type '%s'\n", type);
204 ctx = device_get_sysctl_ctx(dev);
205 tree = device_get_sysctl_tree(dev);
207 sc->sc_handle = sensor_id;
208 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
209 "sensor", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
210 sensor_id, opal_sensor_sysctl,
211 (sc->sc_type == OPAL_SENSOR_TEMP) ? "IK" : "I", "current value");
213 SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "type",
214 CTLFLAG_RD, __DECONST(char *, opal_sensor_types[sc->sc_type]),
217 OF_getprop_alloc(node, "label", (void **)&sc->sc_label);
218 SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "label",
219 CTLFLAG_RD, sc->sc_label, 0, "");
221 if (OF_getprop(node, "sensor-data-min",
222 &sensor_id, sizeof(sensor_id)) > 0) {
223 sc->sc_min_handle = sensor_id;
224 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
225 "sensor_min", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
226 sc, sensor_id, opal_sensor_sysctl,
227 (sc->sc_type == OPAL_SENSOR_TEMP) ? "IK" : "I",
231 if (OF_getprop(node, "sensor-data-max",
232 &sensor_id, sizeof(sensor_id)) > 0) {
233 sc->sc_max_handle = sensor_id;
234 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
235 "sensor_max", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
236 sc, sensor_id, opal_sensor_sysctl,
237 (sc->sc_type == OPAL_SENSOR_TEMP) ? "IK" : "I",
241 SENSOR_LOCK_INIT(sc);
246 static device_method_t opal_sensor_methods[] = {
247 DEVMETHOD(device_probe, opal_sensor_probe),
248 DEVMETHOD(device_attach, opal_sensor_attach),
251 static driver_t opal_sensor_driver = {
254 sizeof(struct opal_sensor_softc)
257 static devclass_t opal_sensor_devclass;
259 DRIVER_MODULE(opal_sensor, opalsens, opal_sensor_driver, opal_sensor_devclass,
263 opalsens_probe(device_t dev)
266 if (!ofw_bus_is_compatible(dev, "ibm,opal-sensor"))
269 device_set_desc(dev, "OPAL Sensors");
270 return (BUS_PROBE_GENERIC);
274 opalsens_attach(device_t dev)
278 struct ofw_bus_devinfo *dinfo;
280 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
281 child = OF_peer(child)) {
282 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
283 if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
284 free(dinfo, M_DEVBUF);
287 cdev = device_add_child(dev, NULL, -1);
289 device_printf(dev, "<%s>: device_add_child failed\n",
291 ofw_bus_gen_destroy_devinfo(dinfo);
292 free(dinfo, M_DEVBUF);
295 device_set_ivars(cdev, dinfo);
298 return (bus_generic_attach(dev));
301 static const struct ofw_bus_devinfo *
302 opalsens_get_devinfo(device_t dev, device_t child)
304 return (device_get_ivars(child));
307 static device_method_t opalsens_methods[] = {
308 /* Device interface */
309 DEVMETHOD(device_probe, opalsens_probe),
310 DEVMETHOD(device_attach, opalsens_attach),
312 /* ofw_bus interface */
313 DEVMETHOD(ofw_bus_get_devinfo, opalsens_get_devinfo),
314 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
315 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
316 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
317 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
318 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
323 static driver_t opalsens_driver = {
329 static devclass_t opalsens_devclass;
331 DRIVER_MODULE(opalsens, opal, opalsens_driver, opalsens_devclass, NULL, NULL);