xref: /openbsd/sys/arch/powerpc64/dev/opalsens.c (revision d415bd75)
1 /*	$OpenBSD: opalsens.c,v 1.3 2022/04/06 18:59:27 naddy Exp $	*/
2 /*
3  * Copyright (c) 2020 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/device.h>
20 #include <sys/sensors.h>
21 #include <sys/systm.h>
22 
23 #include <machine/fdt.h>
24 #include <machine/opal.h>
25 
26 #include <dev/ofw/openfirm.h>
27 #include <dev/ofw/fdt.h>
28 
29 struct opalsens_softc {
30 	struct device		sc_dev;
31 	uint32_t		sc_data;
32 
33 	struct ksensor		sc_sensor;
34 	struct ksensordev	sc_sensordev;
35 };
36 
37 int	opalsens_match(struct device *, void *, void *);
38 void	opalsens_attach(struct device *, struct device *, void *);
39 
40 const struct cfattach opalsens_ca = {
41 	sizeof (struct opalsens_softc), opalsens_match, opalsens_attach
42 };
43 
44 struct cfdriver opalsens_cd = {
45 	NULL, "opalsens", DV_DULL
46 };
47 
48 void	opalsens_refresh(void *);
49 
50 int
51 opalsens_match(struct device *parent, void *match, void *aux)
52 {
53 	struct fdt_attach_args *faa = aux;
54 
55 	return OF_is_compatible(faa->fa_node, "ibm,opal-sensor");
56 }
57 
58 void
59 opalsens_attach(struct device *parent, struct device *self, void *aux)
60 {
61 	struct opalsens_softc *sc = (struct opalsens_softc *)self;
62 	struct fdt_attach_args *faa = aux;
63 	char name[32], type[32], label[32];
64 
65 	sc->sc_data = OF_getpropint(faa->fa_node, "sensor-data", 0);
66 
67 	name[0] = 0;
68 	OF_getprop(faa->fa_node, "name", name, sizeof(name));
69 	name[sizeof(name) - 1] = 0;
70 
71 	printf(": \"%s\"", name);
72 
73 	type[0] = 0;
74 	OF_getprop(faa->fa_node, "sensor-type", type, sizeof(type));
75 	type[sizeof(type) - 1] = 0;
76 
77 	if (strcmp(type, "curr") == 0)
78 		sc->sc_sensor.type = SENSOR_AMPS;
79 	else if (strcmp(type, "energy") == 0)
80 		sc->sc_sensor.type = SENSOR_ENERGY;
81 	else if (strcmp(type, "in") == 0)
82 		sc->sc_sensor.type = SENSOR_VOLTS_DC;
83 	else if (strcmp(type, "power") == 0)
84 		sc->sc_sensor.type = SENSOR_WATTS;
85 	else if (strcmp(type, "temp") == 0)
86 		sc->sc_sensor.type = SENSOR_TEMP;
87 	else {
88 		printf(", unsupported sensor type \"%s\"\n", type);
89 		return;
90 	}
91 
92 	label[0] = 0;
93 	OF_getprop(faa->fa_node, "label", label, sizeof(label));
94 	label[sizeof(label) - 1] = 0;
95 
96 	strlcpy(sc->sc_sensor.desc, label, sizeof(sc->sc_sensor.desc));
97 
98 	printf("\n");
99 
100 	/* Register sensor. */
101 	strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
102 	    sizeof(sc->sc_sensordev.xname));
103 	sc->sc_sensor.flags = SENSOR_FINVALID;
104 	sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
105 	sensordev_install(&sc->sc_sensordev);
106 	sensor_task_register(sc, opalsens_refresh, 5);
107 }
108 
109 void
110 opalsens_refresh(void *arg)
111 {
112 	struct opalsens_softc *sc = arg;
113 	uint64_t value;
114 	int64_t error;
115 
116 	error = opal_sensor_read_u64(sc->sc_data, 0, opal_phys(&value));
117 	if (error == OPAL_SUCCESS)
118 		sc->sc_sensor.flags &= ~SENSOR_FINVALID;
119 	else
120 		sc->sc_sensor.flags |= SENSOR_FINVALID;
121 
122 	switch (sc->sc_sensor.type) {
123 	case SENSOR_AMPS:
124 	case SENSOR_VOLTS_DC:
125 		sc->sc_sensor.value = value * 1000;
126 		break;
127 	case SENSOR_WATTS:
128 		sc->sc_sensor.value = value * 1000000;
129 		break;
130 	case SENSOR_TEMP:
131 		/* Firmware reports 0 for unpopulated DIMM slots. */
132 		if (value == 0)
133 			sc->sc_sensor.flags |= SENSOR_FINVALID;
134 		else
135 			sc->sc_sensor.value = 273150000 + value * 1000000;
136 		break;
137 	default:
138 		sc->sc_sensor.value = value;
139 		break;
140 	}
141 }
142