xref: /freebsd/sys/dev/iicbus/pwm/adt746x.c (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 Andreas Tobler
5  * Copyright (c) 2014 Justin Hibbits
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/systm.h>
34 #include <sys/module.h>
35 #include <sys/callout.h>
36 #include <sys/conf.h>
37 #include <sys/cpu.h>
38 #include <sys/ctype.h>
39 #include <sys/kernel.h>
40 #include <sys/reboot.h>
41 #include <sys/rman.h>
42 #include <sys/sysctl.h>
43 #include <sys/limits.h>
44 
45 #include <machine/bus.h>
46 #include <machine/md_var.h>
47 
48 #include <dev/iicbus/iicbus.h>
49 #include <dev/iicbus/iiconf.h>
50 
51 #include <dev/ofw/openfirm.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <powerpc/powermac/powermac_thermal.h>
54 
55 /* ADT746X registers. */
56 #define ADT746X_TACH1LOW          0x28
57 #define ADT746X_TACH1HIGH         0x29
58 #define ADT746X_TACH2LOW          0x2a
59 #define ADT746X_TACH2HIGH         0x2b
60 #define ADT746X_PWM1              0x30
61 #define ADT746X_PWM2              0x31
62 #define ADT746X_DEVICE_ID         0x3d
63 #define ADT746X_COMPANY_ID        0x3e
64 #define ADT746X_REV_ID            0x3f
65 #define ADT746X_CONFIG            0x40
66 #define ADT746X_PWM1_CONF         0x5c
67 #define ADT746X_PWM2_CONF         0x5d
68 #define ADT746X_MANUAL_MASK       0xe0
69 
70 #define ADT7460_DEV_ID            0x27
71 #define ADT7467_DEV_ID            0x68
72 
73 struct adt746x_fan {
74 	struct pmac_fan fan;
75 	device_t        dev;
76 	int             id;
77 	int             setpoint;
78 	int		pwm_reg;
79 	int		conf_reg;
80 };
81 
82 struct adt746x_sensor {
83 	struct pmac_therm therm;
84 	device_t          dev;
85 	int               id;
86 	cell_t	          reg;
87 	enum {
88 		ADT746X_SENSOR_TEMP,
89 		ADT746X_SENSOR_VOLT,
90 		ADT746X_SENSOR_SPEED
91 	} type;
92 };
93 
94 struct adt746x_softc {
95 	device_t		sc_dev;
96 	struct intr_config_hook enum_hook;
97 	uint32_t                sc_addr;
98 	/* The 7467 supports up to 4 fans, 2 voltage and 3 temperature sensors. */
99 	struct adt746x_fan	sc_fans[4];
100 	int			sc_nfans;
101 	struct adt746x_sensor   sc_sensors[9];
102 	int			sc_nsensors;
103 	int                     device_id;
104 
105 };
106 
107 
108 /* Regular bus attachment functions */
109 
110 static int  adt746x_probe(device_t);
111 static int  adt746x_attach(device_t);
112 
113 
114 /* Utility functions */
115 static void adt746x_attach_fans(device_t dev);
116 static void adt746x_attach_sensors(device_t dev);
117 static int  adt746x_fill_fan_prop(device_t dev);
118 static int  adt746x_fill_sensor_prop(device_t dev);
119 
120 static int  adt746x_fan_set_pwm(struct adt746x_fan *fan, int pwm);
121 static int  adt746x_fan_get_pwm(struct adt746x_fan *fan);
122 static int  adt746x_sensor_read(struct adt746x_sensor *sens);
123 static void adt746x_start(void *xdev);
124 
125 /* i2c read/write functions. */
126 static int  adt746x_write(device_t dev, uint32_t addr, uint8_t reg,
127 			  uint8_t *buf);
128 static int  adt746x_read(device_t dev, uint32_t addr, uint8_t reg,
129 			 uint8_t *data);
130 
131 static device_method_t  adt746x_methods[] = {
132 	/* Device interface */
133 	DEVMETHOD(device_probe,	 adt746x_probe),
134 	DEVMETHOD(device_attach, adt746x_attach),
135 	{ 0, 0 },
136 };
137 
138 static driver_t adt746x_driver = {
139 	"adt746x",
140 	adt746x_methods,
141 	sizeof(struct adt746x_softc)
142 };
143 
144 DRIVER_MODULE(adt746x, iicbus, adt746x_driver, 0, 0);
145 static MALLOC_DEFINE(M_ADT746X, "adt746x", "ADT Sensor Information");
146 
147 
148 /* i2c read/write functions. */
149 
150 static int
151 adt746x_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buff)
152 {
153 	uint8_t buf[4];
154 	int try = 0;
155 
156 	struct iic_msg msg[] = {
157 		{addr, IIC_M_WR, 2, buf }
158 	};
159 
160 	/* Prepare the write msg. */
161 	buf[0] = reg;
162 	memcpy(buf + 1, buff, 1);
163 
164 	for (;;)
165 	{
166 		if (iicbus_transfer(dev, msg, 1) == 0)
167 			return (0);
168 		if (++try > 5) {
169 			device_printf(dev, "iicbus write failed\n");
170 			return (-1);
171 		}
172 		pause("adt746x_write", hz);
173 	}
174 	return (0);
175 }
176 
177 static int
178 adt746x_read(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
179 {
180 	uint8_t buf[4];
181 	int err, try = 0;
182 
183 	struct iic_msg msg[2] = {
184 		{addr, IIC_M_WR | IIC_M_NOSTOP, 1, &reg},
185 		{addr, IIC_M_RD, 1, buf},
186 	};
187 
188 	for (;;)
189 	{
190 		err = iicbus_transfer(dev, msg, 2);
191 		if (err != 0)
192 			goto retry;
193 
194 		*data = *((uint8_t*)buf);
195 		return (0);
196 	retry:
197 		if (++try > 5) {
198 			device_printf(dev, "iicbus read failed\n");
199 			return (-1);
200 		}
201 		pause("adt746x_read", hz);
202 	}
203 }
204 
205 static int
206 adt746x_probe(device_t dev)
207 {
208 	const char  *name, *compatible;
209 	struct adt746x_softc *sc;
210 
211 	name = ofw_bus_get_name(dev);
212 	compatible = ofw_bus_get_compat(dev);
213 
214 	if (!name)
215 		return (ENXIO);
216 
217 	if (strcmp(name, "fan") != 0 ||
218 	    (strcmp(compatible, "adt7460") != 0 &&
219 	     strcmp(compatible, "adt7467") != 0))
220 		return (ENXIO);
221 
222 	sc = device_get_softc(dev);
223 	sc->sc_dev = dev;
224 	sc->sc_addr = iicbus_get_addr(dev);
225 
226 	device_set_desc(dev, "Apple Thermostat Unit ADT746X");
227 
228 	return (0);
229 }
230 
231 static int
232 adt746x_attach(device_t dev)
233 {
234 	struct adt746x_softc *sc;
235 
236 	sc = device_get_softc(dev);
237 
238 	sc->enum_hook.ich_func = adt746x_start;
239 	sc->enum_hook.ich_arg = dev;
240 
241 	/* We have to wait until interrupts are enabled. I2C read and write
242 	 * only works if the interrupts are available.
243 	 * The unin/i2c is controlled by the htpic on unin. But this is not
244 	 * the master. The openpic on mac-io is controlling the htpic.
245 	 * This one gets attached after the mac-io probing and then the
246 	 * interrupts will be available.
247 	 */
248 
249 	if (config_intrhook_establish(&sc->enum_hook) != 0)
250 		return (ENOMEM);
251 
252 	return (0);
253 }
254 
255 static void
256 adt746x_start(void *xdev)
257 {
258 	uint8_t did, cid, rev, conf;
259 
260 	struct adt746x_softc *sc;
261 
262 	device_t dev = (device_t)xdev;
263 
264 	sc = device_get_softc(dev);
265 
266 	adt746x_read(sc->sc_dev, sc->sc_addr, ADT746X_DEVICE_ID, &did);
267 	adt746x_read(sc->sc_dev, sc->sc_addr, ADT746X_COMPANY_ID, &cid);
268 	adt746x_read(sc->sc_dev, sc->sc_addr, ADT746X_REV_ID, &rev);
269 	adt746x_read(sc->sc_dev, sc->sc_addr, ADT746X_CONFIG, &conf);
270 
271 	device_printf(dev, "Dev ID %#x, Company ID %#x, Rev ID %#x CNF: %#x\n",
272 		      did, cid, rev, conf);
273 
274 	/* We can get the device id either from 'of' properties or from the chip
275 	   itself. This method makes sure we can read the chip, otherwise
276 	   we return.  */
277 
278 	sc->device_id = did;
279 
280 	conf = 1;
281 	/* Start the ADT7460.  */
282 	if (sc->device_id == ADT7460_DEV_ID)
283 		adt746x_write(sc->sc_dev, sc->sc_addr, ADT746X_CONFIG, &conf);
284 
285 	/* Detect and attach child devices.  */
286 	adt746x_attach_fans(dev);
287 	adt746x_attach_sensors(dev);
288 	config_intrhook_disestablish(&sc->enum_hook);
289 }
290 
291 /*
292  * Sensor and fan management
293  */
294 static int
295 adt746x_fan_set_pwm(struct adt746x_fan *fan, int pwm)
296 {
297 	uint8_t reg = 0, manual, mode = 0;
298 	struct adt746x_softc *sc;
299 	uint8_t buf;
300 
301 	sc = device_get_softc(fan->dev);
302 
303 	/* Clamp to allowed range */
304 	pwm = max(fan->fan.min_rpm, pwm);
305 	pwm = min(fan->fan.max_rpm, pwm);
306 
307 	reg = fan->pwm_reg;
308 	mode = fan->conf_reg;
309 
310 	/* From the 7460 datasheet:
311 	   PWM dutycycle can be programmed from 0% (0x00) to 100% (0xFF)
312 	   in steps of 0.39% (256 steps).
313 	 */
314 	buf = (pwm * 100 / 39) - (pwm ? 1 : 0);
315 	fan->setpoint = buf;
316 
317 	/* Manual mode.  */
318 	adt746x_read(sc->sc_dev, sc->sc_addr, mode, &manual);
319 	manual |= ADT746X_MANUAL_MASK;
320 	adt746x_write(sc->sc_dev, sc->sc_addr, mode, &manual);
321 
322 	/* Write speed.  */
323 	adt746x_write(sc->sc_dev, sc->sc_addr, reg, &buf);
324 
325 	return (0);
326 }
327 
328 static int
329 adt746x_fan_get_pwm(struct adt746x_fan *fan)
330 {
331 	uint8_t buf, reg;
332 	uint16_t pwm;
333 	struct adt746x_softc *sc;
334 
335 	sc = device_get_softc(fan->dev);
336 
337 	reg = fan->pwm_reg;
338 
339 	adt746x_read(sc->sc_dev, sc->sc_addr, reg, &buf);
340 
341 	pwm = (buf * 39 / 100) + (buf ? 1 : 0);
342 	return (pwm);
343 }
344 
345 static int
346 adt746x_fill_fan_prop(device_t dev)
347 {
348 	phandle_t child;
349 	struct adt746x_softc *sc;
350 	u_int *id;
351 	char *location;
352 	int i, id_len, len = 0, location_len, prev_len = 0;
353 
354 	sc = device_get_softc(dev);
355 
356 	child = ofw_bus_get_node(dev);
357 
358 	/* Fill the fan location property. */
359 	location_len = OF_getprop_alloc(child, "hwctrl-location", (void **)&location);
360 	id_len = OF_getprop_alloc_multi(child, "hwctrl-id", sizeof(cell_t), (void **)&id);
361 	if (location_len == -1 || id_len == -1) {
362 		OF_prop_free(location);
363 		OF_prop_free(id);
364 		return 0;
365 	}
366 
367 	/* Fill in all the properties for each fan. */
368 	for (i = 0; i < id_len; i++) {
369 		strlcpy(sc->sc_fans[i].fan.name, location + len, 32);
370 		prev_len = strlen(location + len) + 1;
371 		len += prev_len;
372 		sc->sc_fans[i].id = id[i];
373 		if (id[i] == 6) {
374 			sc->sc_fans[i].pwm_reg = ADT746X_PWM1;
375 			sc->sc_fans[i].conf_reg = ADT746X_PWM1_CONF;
376 		} else if (id[i] == 7) {
377 			sc->sc_fans[i].pwm_reg = ADT746X_PWM2;
378 			sc->sc_fans[i].conf_reg = ADT746X_PWM2_CONF;
379 		} else {
380 			sc->sc_fans[i].pwm_reg = ADT746X_PWM1 + i;
381 			sc->sc_fans[i].conf_reg = ADT746X_PWM1_CONF + i;
382 		}
383 		sc->sc_fans[i].dev = sc->sc_dev;
384 		sc->sc_fans[i].fan.min_rpm = 5;	/* Percent */
385 		sc->sc_fans[i].fan.max_rpm = 100;
386 		sc->sc_fans[i].fan.read = NULL;
387 		sc->sc_fans[i].fan.set =
388 			(int (*)(struct pmac_fan *, int))(adt746x_fan_set_pwm);
389 		sc->sc_fans[i].fan.default_rpm = sc->sc_fans[i].fan.max_rpm;
390 	}
391 	OF_prop_free(location);
392 	OF_prop_free(id);
393 
394 	return (i);
395 }
396 
397 static int
398 adt746x_fill_sensor_prop(device_t dev)
399 {
400 	phandle_t child, node;
401 	struct adt746x_softc *sc;
402 	char sens_type[32];
403 	int i = 0, reg, sensid;
404 
405 	sc = device_get_softc(dev);
406 
407 	child = ofw_bus_get_node(dev);
408 
409 	/* Fill in the sensor properties for each child. */
410 	for (node = OF_child(child); node != 0; node = OF_peer(node)) {
411 		if (OF_getprop(node, "sensor-id", &sensid, sizeof(sensid)) == -1)
412 		    continue;
413 		OF_getprop(node, "location", sc->sc_sensors[i].therm.name, 32);
414 		OF_getprop(node, "device_type", sens_type, sizeof(sens_type));
415 		if (strcmp(sens_type, "temperature") == 0)
416 			sc->sc_sensors[i].type = ADT746X_SENSOR_TEMP;
417 		else if (strcmp(sens_type, "voltage") == 0)
418 			sc->sc_sensors[i].type = ADT746X_SENSOR_VOLT;
419 		else
420 			sc->sc_sensors[i].type = ADT746X_SENSOR_SPEED;
421 		OF_getprop(node, "reg", &reg, sizeof(reg));
422 		OF_getprop(node, "sensor-id", &sensid,
423 			sizeof(sensid));
424 		/* This is the i2c register of the sensor.  */
425 		sc->sc_sensors[i].reg = reg;
426 		sc->sc_sensors[i].id = sensid;
427 		OF_getprop(node, "zone", &sc->sc_sensors[i].therm.zone,
428 			sizeof(sc->sc_sensors[i].therm.zone));
429 		sc->sc_sensors[i].dev = dev;
430 		sc->sc_sensors[i].therm.read =
431 		    (int (*)(struct pmac_therm *))adt746x_sensor_read;
432 		if (sc->sc_sensors[i].type == ADT746X_SENSOR_TEMP) {
433 		    /* Make up some ranges */
434 		    sc->sc_sensors[i].therm.target_temp = 500 + ZERO_C_TO_K;
435 		    sc->sc_sensors[i].therm.max_temp = 800 + ZERO_C_TO_K;
436 
437 		    pmac_thermal_sensor_register(&sc->sc_sensors[i].therm);
438 		}
439 		i++;
440 	}
441 
442 	return (i);
443 }
444 
445 static int
446 adt746x_fanrpm_sysctl(SYSCTL_HANDLER_ARGS)
447 {
448 	device_t adt;
449 	struct adt746x_softc *sc;
450 	struct adt746x_fan *fan;
451 	int pwm = 0, error;
452 
453 	adt = arg1;
454 	sc = device_get_softc(adt);
455 	fan = &sc->sc_fans[arg2];
456 	pwm = adt746x_fan_get_pwm(fan);
457 	error = sysctl_handle_int(oidp, &pwm, 0, req);
458 
459 	if (error || !req->newptr)
460 		return (error);
461 
462 	return (adt746x_fan_set_pwm(fan, pwm));
463 }
464 
465 static void
466 adt746x_attach_fans(device_t dev)
467 {
468 	struct adt746x_softc *sc;
469 	struct sysctl_oid *oid, *fanroot_oid;
470 	struct sysctl_ctx_list *ctx;
471 	char sysctl_name[32];
472 	int i, j;
473 
474 	sc = device_get_softc(dev);
475 
476 	sc->sc_nfans = 0;
477 
478 	/* Count the actual number of fans. */
479 	sc->sc_nfans = adt746x_fill_fan_prop(dev);
480 
481 	device_printf(dev, "%d fans detected!\n", sc->sc_nfans);
482 
483 	if (sc->sc_nfans == 0) {
484 		device_printf(dev, "WARNING: No fans detected!\n");
485 		return;
486 	}
487 
488 	ctx = device_get_sysctl_ctx(dev);
489 	fanroot_oid = SYSCTL_ADD_NODE(ctx,
490 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "fans",
491 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "ADT Fan Information");
492 
493 	/* Now we can fill the properties into the allocated struct. */
494 	sc->sc_nfans = adt746x_fill_fan_prop(dev);
495 
496 	/* Register fans with pmac_thermal */
497 	for (i = 0; i < sc->sc_nfans; i++)
498 		pmac_thermal_fan_register(&sc->sc_fans[i].fan);
499 
500 	/* Add sysctls for the fans. */
501 	for (i = 0; i < sc->sc_nfans; i++) {
502 		for (j = 0; j < strlen(sc->sc_fans[i].fan.name); j++) {
503 			sysctl_name[j] = tolower(sc->sc_fans[i].fan.name[j]);
504 			if (isspace(sysctl_name[j]))
505 				sysctl_name[j] = '_';
506 		}
507 		sysctl_name[j] = 0;
508 
509 		sc->sc_fans[i].setpoint =
510 			adt746x_fan_get_pwm(&sc->sc_fans[i]);
511 
512 		oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(fanroot_oid),
513 		    OID_AUTO, sysctl_name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
514 		    "Fan Information");
515 
516 		/* I use i to pass the fan id. */
517 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
518 		    "pwm", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, dev,
519 		    i, adt746x_fanrpm_sysctl, "I", "Fan PWM in %");
520 	}
521 
522 	/* Dump fan location & type. */
523 	if (bootverbose) {
524 		for (i = 0; i < sc->sc_nfans; i++) {
525 			device_printf(dev, "Fan location: %s",
526 				      sc->sc_fans[i].fan.name);
527 			device_printf(dev, " id: %d RPM: %d\n",
528 				      sc->sc_fans[i].id,
529 				      sc->sc_fans[i].setpoint);
530 		}
531 	}
532 }
533 
534 static int
535 adt746x_sensor_read(struct adt746x_sensor *sens)
536 {
537 	struct adt746x_softc *sc;
538 	int tmp = 0;
539 	uint16_t val;
540 	uint8_t data[1], data1[1];
541 	int8_t temp;
542 
543 	sc = device_get_softc(sens->dev);
544 	if (sens->type != ADT746X_SENSOR_SPEED) {
545 		if (adt746x_read(sc->sc_dev, sc->sc_addr, sens->reg,
546 				 &temp) < 0)
547 			return (-1);
548 		if (sens->type == ADT746X_SENSOR_TEMP)
549 			tmp = 10 * temp + ZERO_C_TO_K;
550 		else
551 			tmp = temp;
552 	} else {
553 		if (adt746x_read(sc->sc_dev, sc->sc_addr, sens->reg,
554 				 data) < 0)
555 			return (-1);
556 		if (adt746x_read(sc->sc_dev, sc->sc_addr, sens->reg + 1,
557 				 data1) < 0)
558 			return (-1);
559 		val = data[0] + (data1[0] << 8);
560 		/* A value of 0xffff means the fan is stopped.  */
561 		if (val == 0 || val == 0xffff)
562 			tmp = 0;
563 		else
564 			tmp = (90000 * 60) / val;
565 	}
566 	return (tmp);
567 }
568 
569 static int
570 adt746x_sensor_sysctl(SYSCTL_HANDLER_ARGS)
571 {
572 	device_t dev;
573 	struct adt746x_softc *sc;
574 	struct adt746x_sensor *sens;
575 	int value, error;
576 
577 	dev = arg1;
578 	sc = device_get_softc(dev);
579 	sens = &sc->sc_sensors[arg2];
580 
581 	value = sens->therm.read(&sens->therm);
582 	if (value < 0)
583 		return (ENXIO);
584 
585 	error = sysctl_handle_int(oidp, &value, 0, req);
586 
587 	return (error);
588 }
589 
590 static void
591 adt746x_attach_sensors(device_t dev)
592 {
593 	struct adt746x_softc *sc;
594 	struct sysctl_oid *oid, *sensroot_oid;
595 	struct sysctl_ctx_list *ctx;
596 	char sysctl_name[40];
597 	const char *unit;
598 	const char *desc;
599 	int i, j;
600 
601 
602 	sc = device_get_softc(dev);
603 	sc->sc_nsensors = 0;
604 
605 	/* Count the actual number of sensors. */
606 	sc->sc_nsensors = adt746x_fill_sensor_prop(dev);
607 	device_printf(dev, "%d sensors detected!\n", sc->sc_nsensors);
608 	if (sc->sc_nsensors == 0) {
609 		device_printf(dev, "WARNING: No sensors detected!\n");
610 		return;
611 	}
612 
613 	ctx = device_get_sysctl_ctx(dev);
614 	sensroot_oid = SYSCTL_ADD_NODE(ctx,
615 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensors",
616 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "ADT Sensor Information");
617 
618 	/* Add the sysctl for the sensors. */
619 	for (i = 0; i < sc->sc_nsensors; i++) {
620 		for (j = 0; j < strlen(sc->sc_sensors[i].therm.name); j++) {
621 			sysctl_name[j] = tolower(sc->sc_sensors[i].therm.name[j]);
622 			if (isspace(sysctl_name[j]))
623 				sysctl_name[j] = '_';
624 		}
625 		sysctl_name[j] = 0;
626 		oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
627 		    OID_AUTO, sysctl_name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
628 		    "Sensor Information");
629 		if (sc->sc_sensors[i].type == ADT746X_SENSOR_TEMP) {
630 			unit = "temp";
631 			desc = "sensor unit (C)";
632 		} else if (sc->sc_sensors[i].type == ADT746X_SENSOR_VOLT) {
633 			unit = "volt";
634 			desc = "sensor unit (mV)";
635 		} else {
636 			unit = "rpm";
637 			desc = "sensor unit (RPM)";
638 		}
639 		/* I use i to pass the sensor id. */
640 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
641 		    unit, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, dev, i,
642 		    adt746x_sensor_sysctl,
643 		    sc->sc_sensors[i].type == ADT746X_SENSOR_TEMP ?
644 		    "IK" : "I", desc);
645 	}
646 
647 	/* Dump sensor location & type. */
648 	if (bootverbose) {
649 		for (i = 0; i < sc->sc_nsensors; i++) {
650 			device_printf(dev, "Sensor location: %s",
651 				      sc->sc_sensors[i].therm.name);
652 			device_printf(dev, " type: %d id: %d reg: 0x%x\n",
653 				      sc->sc_sensors[i].type,
654 				      sc->sc_sensors[i].id,
655 				      sc->sc_sensors[i].reg);
656 		}
657 	}
658 }
659