1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2010 Andreas Tobler
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/systm.h>
35 #include <sys/module.h>
36 #include <sys/callout.h>
37 #include <sys/conf.h>
38 #include <sys/cpu.h>
39 #include <sys/ctype.h>
40 #include <sys/kernel.h>
41 #include <sys/reboot.h>
42 #include <sys/rman.h>
43 #include <sys/sysctl.h>
44 #include <sys/limits.h>
45
46 #include <machine/bus.h>
47 #include <machine/md_var.h>
48
49 #include <dev/iicbus/iicbus.h>
50 #include <dev/iicbus/iiconf.h>
51
52 #include <dev/ofw/openfirm.h>
53 #include <dev/ofw/ofw_bus.h>
54 #include <powerpc/powermac/powermac_thermal.h>
55
56 /* Inlet, Backside, U3 Heatsink sensor: MAX6690. */
57
58 #define MAX6690_INT_TEMP 0x0
59 #define MAX6690_EXT_TEMP 0x1
60 #define MAX6690_RSL_STATUS 0x2
61 #define MAX6690_EEXT_TEMP 0x10
62 #define MAX6690_IEXT_TEMP 0x11
63 #define MAX6690_TEMP_MASK 0xe0
64
65 struct max6690_sensor {
66 struct pmac_therm therm;
67 device_t dev;
68
69 int id;
70 };
71
72 /* Regular bus attachment functions */
73 static int max6690_probe(device_t);
74 static int max6690_attach(device_t);
75
76 /* Utility functions */
77 static int max6690_sensor_read(struct max6690_sensor *sens);
78 static int max6690_sensor_sysctl(SYSCTL_HANDLER_ARGS);
79 static void max6690_start(void *xdev);
80 static int max6690_read(device_t dev, uint32_t addr, uint8_t reg,
81 uint8_t *data);
82
83 struct max6690_softc {
84 device_t sc_dev;
85 struct intr_config_hook enum_hook;
86 uint32_t sc_addr;
87 struct max6690_sensor *sc_sensors;
88 int sc_nsensors;
89 };
90 static device_method_t max6690_methods[] = {
91 /* Device interface */
92 DEVMETHOD(device_probe, max6690_probe),
93 DEVMETHOD(device_attach, max6690_attach),
94 { 0, 0 },
95 };
96
97 static driver_t max6690_driver = {
98 "max6690",
99 max6690_methods,
100 sizeof(struct max6690_softc)
101 };
102
103 static devclass_t max6690_devclass;
104
105 DRIVER_MODULE(max6690, iicbus, max6690_driver, max6690_devclass, 0, 0);
106 static MALLOC_DEFINE(M_MAX6690, "max6690", "Temp-Monitor MAX6690");
107
108 static int
max6690_read(device_t dev,uint32_t addr,uint8_t reg,uint8_t * data)109 max6690_read(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
110 {
111 uint8_t buf[4];
112 uint8_t busy[1], rsl;
113 int err, try = 0;
114
115 /* Busy register RSL. */
116 rsl = MAX6690_RSL_STATUS;
117 /* first read the status register, 0x2. If busy, retry. */
118 struct iic_msg msg[4] = {
119 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, &rsl },
120 { addr, IIC_M_RD, 1, busy },
121 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
122 { addr, IIC_M_RD, 1, buf },
123 };
124
125 for (;;)
126 {
127 err = iicbus_transfer(dev, msg, 4);
128 if (err != 0)
129 goto retry;
130 if (busy[0] & 0x80)
131 goto retry;
132 /* Check for invalid value and retry. */
133 if (buf[0] == 0xff)
134 goto retry;
135
136 *data = *((uint8_t*)buf);
137 return (0);
138
139 retry:
140 if (++try > 5) {
141 device_printf(dev, "iicbus read failed\n");
142 return (-1);
143 }
144 pause("max6690_read", hz);
145 }
146 }
147
148 static int
max6690_probe(device_t dev)149 max6690_probe(device_t dev)
150 {
151 const char *name, *compatible;
152 struct max6690_softc *sc;
153
154 name = ofw_bus_get_name(dev);
155 compatible = ofw_bus_get_compat(dev);
156
157 if (!name)
158 return (ENXIO);
159
160 if (strcmp(name, "temp-monitor") != 0 ||
161 strcmp(compatible, "max6690") != 0)
162 return (ENXIO);
163
164 sc = device_get_softc(dev);
165 sc->sc_dev = dev;
166 sc->sc_addr = iicbus_get_addr(dev);
167
168 device_set_desc(dev, "Temp-Monitor MAX6690");
169
170 return (0);
171 }
172
173 /*
174 * This function returns the number of sensors. If we call it the second time
175 * and we have allocated memory for sc->sc_sensors, we fill in the properties.
176 */
177 static int
max6690_fill_sensor_prop(device_t dev)178 max6690_fill_sensor_prop(device_t dev)
179 {
180 phandle_t child;
181 struct max6690_softc *sc;
182 u_int id[8];
183 char location[96];
184 int i = 0, j, len = 0, prop_len, prev_len = 0;
185
186 sc = device_get_softc(dev);
187
188 child = ofw_bus_get_node(dev);
189
190 /* Fill the sensor location property. */
191 prop_len = OF_getprop(child, "hwsensor-location", location,
192 sizeof(location));
193 while (len < prop_len) {
194 if (sc->sc_sensors != NULL)
195 strcpy(sc->sc_sensors[i].therm.name, location + len);
196 prev_len = strlen(location + len) + 1;
197 len += prev_len;
198 i++;
199 }
200 if (sc->sc_sensors == NULL)
201 return (i);
202
203 /* Fill the sensor id property. */
204 prop_len = OF_getprop(child, "hwsensor-id", id, sizeof(id));
205 for (j = 0; j < i; j++)
206 sc->sc_sensors[j].id = (id[j] & 0xf);
207
208 /* Fill the sensor zone property. */
209 prop_len = OF_getprop(child, "hwsensor-zone", id, sizeof(id));
210 for (j = 0; j < i; j++)
211 sc->sc_sensors[j].therm.zone = id[j];
212
213 /* Set up remaining sensor properties */
214 for (j = 0; j < i; j++) {
215 sc->sc_sensors[j].dev = dev;
216
217 sc->sc_sensors[j].therm.target_temp = 400 + ZERO_C_TO_K;
218 sc->sc_sensors[j].therm.max_temp = 800 + ZERO_C_TO_K;
219
220 sc->sc_sensors[j].therm.read =
221 (int (*)(struct pmac_therm *))(max6690_sensor_read);
222 }
223
224 return (i);
225 }
226 static int
max6690_attach(device_t dev)227 max6690_attach(device_t dev)
228 {
229 struct max6690_softc *sc;
230
231 sc = device_get_softc(dev);
232
233 sc->enum_hook.ich_func = max6690_start;
234 sc->enum_hook.ich_arg = dev;
235
236 /* We have to wait until interrupts are enabled. I2C read and write
237 * only works if the interrupts are available.
238 * The unin/i2c is controlled by the htpic on unin. But this is not
239 * the master. The openpic on mac-io is controlling the htpic.
240 * This one gets attached after the mac-io probing and then the
241 * interrupts will be available.
242 */
243
244 if (config_intrhook_establish(&sc->enum_hook) != 0)
245 return (ENOMEM);
246
247 return (0);
248 }
249
250 static void
max6690_start(void * xdev)251 max6690_start(void *xdev)
252 {
253 struct max6690_softc *sc;
254 struct sysctl_oid *oid, *sensroot_oid;
255 struct sysctl_ctx_list *ctx;
256 char sysctl_desc[40], sysctl_name[32];
257 int i, j;
258
259 device_t dev = (device_t)xdev;
260
261 sc = device_get_softc(dev);
262
263 sc->sc_nsensors = 0;
264
265 /* Count the actual number of sensors. */
266 sc->sc_nsensors = max6690_fill_sensor_prop(dev);
267
268 device_printf(dev, "%d sensors detected.\n", sc->sc_nsensors);
269
270 if (sc->sc_nsensors == 0)
271 device_printf(dev, "WARNING: No MAX6690 sensors detected!\n");
272
273 sc->sc_sensors = malloc (sc->sc_nsensors * sizeof(struct max6690_sensor),
274 M_MAX6690, M_WAITOK | M_ZERO);
275
276 ctx = device_get_sysctl_ctx(dev);
277 sensroot_oid = SYSCTL_ADD_NODE(ctx,
278 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor",
279 CTLFLAG_RD, 0, "MAX6690 Sensor Information");
280
281 /* Now we can fill the properties into the allocated struct. */
282 sc->sc_nsensors = max6690_fill_sensor_prop(dev);
283
284 /* Register with powermac_thermal */
285 for (i = 0; i < sc->sc_nsensors; i++)
286 pmac_thermal_sensor_register(&sc->sc_sensors[i].therm);
287
288 /* Add sysctls for the sensors. */
289 for (i = 0; i < sc->sc_nsensors; i++) {
290 for (j = 0; j < strlen(sc->sc_sensors[i].therm.name); j++) {
291 sysctl_name[j] =
292 tolower(sc->sc_sensors[i].therm.name[j]);
293 if (isspace(sysctl_name[j]))
294 sysctl_name[j] = '_';
295 }
296 sysctl_name[j] = 0;
297
298 sprintf(sysctl_desc,"%s %s", sc->sc_sensors[i].therm.name,
299 "(C)");
300 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
301 OID_AUTO,
302 sysctl_name, CTLFLAG_RD, 0,
303 "Sensor Information");
304 /* I use i to pass the sensor id. */
305 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "temp",
306 CTLTYPE_INT | CTLFLAG_RD, dev, i % 2,
307 max6690_sensor_sysctl, "IK", sysctl_desc);
308
309 }
310 /* Dump sensor location & ID. */
311 if (bootverbose) {
312 device_printf(dev, "Sensors\n");
313 for (i = 0; i < sc->sc_nsensors; i++) {
314 device_printf(dev, "Location : %s ID: %d\n",
315 sc->sc_sensors[i].therm.name,
316 sc->sc_sensors[i].id);
317 }
318 }
319
320 config_intrhook_disestablish(&sc->enum_hook);
321 }
322
323 static int
max6690_sensor_read(struct max6690_sensor * sens)324 max6690_sensor_read(struct max6690_sensor *sens)
325 {
326 uint8_t reg_int = 0, reg_ext = 0;
327 uint8_t integer = 0;
328 uint8_t fraction = 0;
329 int err, temp;
330
331 struct max6690_softc *sc;
332
333 sc = device_get_softc(sens->dev);
334
335 /* The internal sensor id's are even, the external are odd. */
336 if ((sens->id % 2) == 0) {
337 reg_int = MAX6690_INT_TEMP;
338 reg_ext = MAX6690_IEXT_TEMP;
339 } else {
340 reg_int = MAX6690_EXT_TEMP;
341 reg_ext = MAX6690_EEXT_TEMP;
342 }
343
344 err = max6690_read(sc->sc_dev, sc->sc_addr, reg_int, &integer);
345
346 if (err < 0)
347 return (-1);
348
349 err = max6690_read(sc->sc_dev, sc->sc_addr, reg_ext, &fraction);
350
351 if (err < 0)
352 return (-1);
353
354 fraction &= MAX6690_TEMP_MASK;
355
356 /* The temperature is in tenth kelvin, the fractional part resolution
357 is 0.125.
358 */
359 temp = (integer * 10) + (fraction >> 5) * 10 / 8;
360
361 return (temp + ZERO_C_TO_K);
362 }
363
364 static int
max6690_sensor_sysctl(SYSCTL_HANDLER_ARGS)365 max6690_sensor_sysctl(SYSCTL_HANDLER_ARGS)
366 {
367 device_t dev;
368 struct max6690_softc *sc;
369 struct max6690_sensor *sens;
370 int error;
371 int temp;
372
373 dev = arg1;
374 sc = device_get_softc(dev);
375 sens = &sc->sc_sensors[arg2];
376
377 temp = max6690_sensor_read(sens);
378 if (temp < 0)
379 return (EIO);
380
381 error = sysctl_handle_int(oidp, &temp, 0, req);
382
383 return (error);
384 }
385