xref: /freebsd-13-stable/sys/dev/iicbus/adt746x.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
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 static devclass_t adt746x_devclass;
145 
146 DRIVER_MODULE(adt746x, iicbus, adt746x_driver, adt746x_devclass, 0, 0);
147 static MALLOC_DEFINE(M_ADT746X, "adt746x", "ADT Sensor Information");
148 
149 
150 /* i2c read/write functions. */
151 
152 static int
adt746x_write(device_t dev,uint32_t addr,uint8_t reg,uint8_t * buff)153 adt746x_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buff)
154 {
155 	uint8_t buf[4];
156 	int try = 0;
157 
158 	struct iic_msg msg[] = {
159 		{addr, IIC_M_WR, 2, buf }
160 	};
161 
162 	/* Prepare the write msg. */
163 	buf[0] = reg;
164 	memcpy(buf + 1, buff, 1);
165 
166 	for (;;)
167 	{
168 		if (iicbus_transfer(dev, msg, 1) == 0)
169 			return (0);
170 		if (++try > 5) {
171 			device_printf(dev, "iicbus write failed\n");
172 			return (-1);
173 		}
174 		pause("adt746x_write", hz);
175 	}
176 	return (0);
177 }
178 
179 static int
adt746x_read(device_t dev,uint32_t addr,uint8_t reg,uint8_t * data)180 adt746x_read(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
181 {
182 	uint8_t buf[4];
183 	int err, try = 0;
184 
185 	struct iic_msg msg[2] = {
186 		{addr, IIC_M_WR | IIC_M_NOSTOP, 1, &reg},
187 		{addr, IIC_M_RD, 1, buf},
188 	};
189 
190 	for (;;)
191 	{
192 		err = iicbus_transfer(dev, msg, 2);
193 		if (err != 0)
194 			goto retry;
195 
196 		*data = *((uint8_t*)buf);
197 		return (0);
198 	retry:
199 		if (++try > 5) {
200 			device_printf(dev, "iicbus read failed\n");
201 			return (-1);
202 		}
203 		pause("adt746x_read", hz);
204 	}
205 }
206 
207 static int
adt746x_probe(device_t dev)208 adt746x_probe(device_t dev)
209 {
210 	const char  *name, *compatible;
211 	struct adt746x_softc *sc;
212 
213 	name = ofw_bus_get_name(dev);
214 	compatible = ofw_bus_get_compat(dev);
215 
216 	if (!name)
217 		return (ENXIO);
218 
219 	if (strcmp(name, "fan") != 0 ||
220 	    (strcmp(compatible, "adt7460") != 0 &&
221 	     strcmp(compatible, "adt7467") != 0))
222 		return (ENXIO);
223 
224 	sc = device_get_softc(dev);
225 	sc->sc_dev = dev;
226 	sc->sc_addr = iicbus_get_addr(dev);
227 
228 	device_set_desc(dev, "Apple Thermostat Unit ADT746X");
229 
230 	return (0);
231 }
232 
233 static int
adt746x_attach(device_t dev)234 adt746x_attach(device_t dev)
235 {
236 	struct adt746x_softc *sc;
237 
238 	sc = device_get_softc(dev);
239 
240 	sc->enum_hook.ich_func = adt746x_start;
241 	sc->enum_hook.ich_arg = dev;
242 
243 	/* We have to wait until interrupts are enabled. I2C read and write
244 	 * only works if the interrupts are available.
245 	 * The unin/i2c is controlled by the htpic on unin. But this is not
246 	 * the master. The openpic on mac-io is controlling the htpic.
247 	 * This one gets attached after the mac-io probing and then the
248 	 * interrupts will be available.
249 	 */
250 
251 	if (config_intrhook_establish(&sc->enum_hook) != 0)
252 		return (ENOMEM);
253 
254 	return (0);
255 }
256 
257 static void
adt746x_start(void * xdev)258 adt746x_start(void *xdev)
259 {
260 	uint8_t did, cid, rev, conf;
261 
262 	struct adt746x_softc *sc;
263 
264 	device_t dev = (device_t)xdev;
265 
266 	sc = device_get_softc(dev);
267 
268 	adt746x_read(sc->sc_dev, sc->sc_addr, ADT746X_DEVICE_ID, &did);
269 	adt746x_read(sc->sc_dev, sc->sc_addr, ADT746X_COMPANY_ID, &cid);
270 	adt746x_read(sc->sc_dev, sc->sc_addr, ADT746X_REV_ID, &rev);
271 	adt746x_read(sc->sc_dev, sc->sc_addr, ADT746X_CONFIG, &conf);
272 
273 	device_printf(dev, "Dev ID %#x, Company ID %#x, Rev ID %#x CNF: %#x\n",
274 		      did, cid, rev, conf);
275 
276 	/* We can get the device id either from 'of' properties or from the chip
277 	   itself. This method makes sure we can read the chip, otherwise
278 	   we return.  */
279 
280 	sc->device_id = did;
281 
282 	conf = 1;
283 	/* Start the ADT7460.  */
284 	if (sc->device_id == ADT7460_DEV_ID)
285 		adt746x_write(sc->sc_dev, sc->sc_addr, ADT746X_CONFIG, &conf);
286 
287 	/* Detect and attach child devices.  */
288 	adt746x_attach_fans(dev);
289 	adt746x_attach_sensors(dev);
290 	config_intrhook_disestablish(&sc->enum_hook);
291 }
292 
293 /*
294  * Sensor and fan management
295  */
296 static int
adt746x_fan_set_pwm(struct adt746x_fan * fan,int pwm)297 adt746x_fan_set_pwm(struct adt746x_fan *fan, int pwm)
298 {
299 	uint8_t reg = 0, manual, mode = 0;
300 	struct adt746x_softc *sc;
301 	uint8_t buf;
302 
303 	sc = device_get_softc(fan->dev);
304 
305 	/* Clamp to allowed range */
306 	pwm = max(fan->fan.min_rpm, pwm);
307 	pwm = min(fan->fan.max_rpm, pwm);
308 
309 	reg = fan->pwm_reg;
310 	mode = fan->conf_reg;
311 
312 	/* From the 7460 datasheet:
313 	   PWM dutycycle can be programmed from 0% (0x00) to 100% (0xFF)
314 	   in steps of 0.39% (256 steps).
315 	 */
316 	buf = (pwm * 100 / 39) - (pwm ? 1 : 0);
317 	fan->setpoint = buf;
318 
319 	/* Manual mode.  */
320 	adt746x_read(sc->sc_dev, sc->sc_addr, mode, &manual);
321 	manual |= ADT746X_MANUAL_MASK;
322 	adt746x_write(sc->sc_dev, sc->sc_addr, mode, &manual);
323 
324 	/* Write speed.  */
325 	adt746x_write(sc->sc_dev, sc->sc_addr, reg, &buf);
326 
327 	return (0);
328 }
329 
330 static int
adt746x_fan_get_pwm(struct adt746x_fan * fan)331 adt746x_fan_get_pwm(struct adt746x_fan *fan)
332 {
333 	uint8_t buf, reg;
334 	uint16_t pwm;
335 	struct adt746x_softc *sc;
336 
337 	sc = device_get_softc(fan->dev);
338 
339 	reg = fan->pwm_reg;
340 
341 	adt746x_read(sc->sc_dev, sc->sc_addr, reg, &buf);
342 
343 	pwm = (buf * 39 / 100) + (buf ? 1 : 0);
344 	return (pwm);
345 }
346 
347 static int
adt746x_fill_fan_prop(device_t dev)348 adt746x_fill_fan_prop(device_t dev)
349 {
350 	phandle_t child;
351 	struct adt746x_softc *sc;
352 	u_int *id;
353 	char *location;
354 	int i, id_len, len = 0, location_len, prev_len = 0;
355 
356 	sc = device_get_softc(dev);
357 
358 	child = ofw_bus_get_node(dev);
359 
360 	/* Fill the fan location property. */
361 	location_len = OF_getprop_alloc(child, "hwctrl-location", (void **)&location);
362 	id_len = OF_getprop_alloc_multi(child, "hwctrl-id", sizeof(cell_t), (void **)&id);
363 	if (location_len == -1 || id_len == -1) {
364 		OF_prop_free(location);
365 		OF_prop_free(id);
366 		return 0;
367 	}
368 
369 	/* Fill in all the properties for each fan. */
370 	for (i = 0; i < id_len; i++) {
371 		strlcpy(sc->sc_fans[i].fan.name, location + len, 32);
372 		prev_len = strlen(location + len) + 1;
373 		len += prev_len;
374 		sc->sc_fans[i].id = id[i];
375 		if (id[i] == 6) {
376 			sc->sc_fans[i].pwm_reg = ADT746X_PWM1;
377 			sc->sc_fans[i].conf_reg = ADT746X_PWM1_CONF;
378 		} else if (id[i] == 7) {
379 			sc->sc_fans[i].pwm_reg = ADT746X_PWM2;
380 			sc->sc_fans[i].conf_reg = ADT746X_PWM2_CONF;
381 		} else {
382 			sc->sc_fans[i].pwm_reg = ADT746X_PWM1 + i;
383 			sc->sc_fans[i].conf_reg = ADT746X_PWM1_CONF + i;
384 		}
385 		sc->sc_fans[i].dev = sc->sc_dev;
386 		sc->sc_fans[i].fan.min_rpm = 5;	/* Percent */
387 		sc->sc_fans[i].fan.max_rpm = 100;
388 		sc->sc_fans[i].fan.read = NULL;
389 		sc->sc_fans[i].fan.set =
390 			(int (*)(struct pmac_fan *, int))(adt746x_fan_set_pwm);
391 		sc->sc_fans[i].fan.default_rpm = sc->sc_fans[i].fan.max_rpm;
392 	}
393 	OF_prop_free(location);
394 	OF_prop_free(id);
395 
396 	return (i);
397 }
398 
399 static int
adt746x_fill_sensor_prop(device_t dev)400 adt746x_fill_sensor_prop(device_t dev)
401 {
402 	phandle_t child, node;
403 	struct adt746x_softc *sc;
404 	char sens_type[32];
405 	int i = 0, reg, sensid;
406 
407 	sc = device_get_softc(dev);
408 
409 	child = ofw_bus_get_node(dev);
410 
411 	/* Fill in the sensor properties for each child. */
412 	for (node = OF_child(child); node != 0; node = OF_peer(node)) {
413 		if (OF_getprop(node, "sensor-id", &sensid, sizeof(sensid)) == -1)
414 		    continue;
415 		OF_getprop(node, "location", sc->sc_sensors[i].therm.name, 32);
416 		OF_getprop(node, "device_type", sens_type, sizeof(sens_type));
417 		if (strcmp(sens_type, "temperature") == 0)
418 			sc->sc_sensors[i].type = ADT746X_SENSOR_TEMP;
419 		else if (strcmp(sens_type, "voltage") == 0)
420 			sc->sc_sensors[i].type = ADT746X_SENSOR_VOLT;
421 		else
422 			sc->sc_sensors[i].type = ADT746X_SENSOR_SPEED;
423 		OF_getprop(node, "reg", &reg, sizeof(reg));
424 		OF_getprop(node, "sensor-id", &sensid,
425 			sizeof(sensid));
426 		/* This is the i2c register of the sensor.  */
427 		sc->sc_sensors[i].reg = reg;
428 		sc->sc_sensors[i].id = sensid;
429 		OF_getprop(node, "zone", &sc->sc_sensors[i].therm.zone,
430 			sizeof(sc->sc_sensors[i].therm.zone));
431 		sc->sc_sensors[i].dev = dev;
432 		sc->sc_sensors[i].therm.read =
433 		    (int (*)(struct pmac_therm *))adt746x_sensor_read;
434 		if (sc->sc_sensors[i].type == ADT746X_SENSOR_TEMP) {
435 		    /* Make up some ranges */
436 		    sc->sc_sensors[i].therm.target_temp = 500 + ZERO_C_TO_K;
437 		    sc->sc_sensors[i].therm.max_temp = 800 + ZERO_C_TO_K;
438 
439 		    pmac_thermal_sensor_register(&sc->sc_sensors[i].therm);
440 		}
441 		i++;
442 	}
443 
444 	return (i);
445 }
446 
447 static int
adt746x_fanrpm_sysctl(SYSCTL_HANDLER_ARGS)448 adt746x_fanrpm_sysctl(SYSCTL_HANDLER_ARGS)
449 {
450 	device_t adt;
451 	struct adt746x_softc *sc;
452 	struct adt746x_fan *fan;
453 	int pwm = 0, error;
454 
455 	adt = arg1;
456 	sc = device_get_softc(adt);
457 	fan = &sc->sc_fans[arg2];
458 	pwm = adt746x_fan_get_pwm(fan);
459 	error = sysctl_handle_int(oidp, &pwm, 0, req);
460 
461 	if (error || !req->newptr)
462 		return (error);
463 
464 	return (adt746x_fan_set_pwm(fan, pwm));
465 }
466 
467 static void
adt746x_attach_fans(device_t dev)468 adt746x_attach_fans(device_t dev)
469 {
470 	struct adt746x_softc *sc;
471 	struct sysctl_oid *oid, *fanroot_oid;
472 	struct sysctl_ctx_list *ctx;
473 	phandle_t child;
474 	char sysctl_name[32];
475 	int i, j;
476 
477 	sc = device_get_softc(dev);
478 
479 	sc->sc_nfans = 0;
480 
481 	child = ofw_bus_get_node(dev);
482 
483 	/* Count the actual number of fans. */
484 	sc->sc_nfans = adt746x_fill_fan_prop(dev);
485 
486 	device_printf(dev, "%d fans detected!\n", sc->sc_nfans);
487 
488 	if (sc->sc_nfans == 0) {
489 		device_printf(dev, "WARNING: No fans detected!\n");
490 		return;
491 	}
492 
493 	ctx = device_get_sysctl_ctx(dev);
494 	fanroot_oid = SYSCTL_ADD_NODE(ctx,
495 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "fans",
496 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "ADT Fan Information");
497 
498 	/* Now we can fill the properties into the allocated struct. */
499 	sc->sc_nfans = adt746x_fill_fan_prop(dev);
500 
501 	/* Register fans with pmac_thermal */
502 	for (i = 0; i < sc->sc_nfans; i++)
503 		pmac_thermal_fan_register(&sc->sc_fans[i].fan);
504 
505 	/* Add sysctls for the fans. */
506 	for (i = 0; i < sc->sc_nfans; i++) {
507 		for (j = 0; j < strlen(sc->sc_fans[i].fan.name); j++) {
508 			sysctl_name[j] = tolower(sc->sc_fans[i].fan.name[j]);
509 			if (isspace(sysctl_name[j]))
510 				sysctl_name[j] = '_';
511 		}
512 		sysctl_name[j] = 0;
513 
514 		sc->sc_fans[i].setpoint =
515 			adt746x_fan_get_pwm(&sc->sc_fans[i]);
516 
517 		oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(fanroot_oid),
518 		    OID_AUTO, sysctl_name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
519 		    "Fan Information");
520 
521 		/* I use i to pass the fan id. */
522 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
523 		    "pwm", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, dev,
524 		    i, adt746x_fanrpm_sysctl, "I", "Fan PWM in %");
525 	}
526 
527 	/* Dump fan location & type. */
528 	if (bootverbose) {
529 		for (i = 0; i < sc->sc_nfans; i++) {
530 			device_printf(dev, "Fan location: %s",
531 				      sc->sc_fans[i].fan.name);
532 			device_printf(dev, " id: %d RPM: %d\n",
533 				      sc->sc_fans[i].id,
534 				      sc->sc_fans[i].setpoint);
535 		}
536 	}
537 }
538 
539 static int
adt746x_sensor_read(struct adt746x_sensor * sens)540 adt746x_sensor_read(struct adt746x_sensor *sens)
541 {
542 	struct adt746x_softc *sc;
543 	int tmp = 0;
544 	uint16_t val;
545 	uint8_t data[1], data1[1];
546 	int8_t temp;
547 
548 	sc = device_get_softc(sens->dev);
549 	if (sens->type != ADT746X_SENSOR_SPEED) {
550 		if (adt746x_read(sc->sc_dev, sc->sc_addr, sens->reg,
551 				 &temp) < 0)
552 			return (-1);
553 		if (sens->type == ADT746X_SENSOR_TEMP)
554 			tmp = 10 * temp + ZERO_C_TO_K;
555 		else
556 			tmp = temp;
557 	} else {
558 		if (adt746x_read(sc->sc_dev, sc->sc_addr, sens->reg,
559 				 data) < 0)
560 			return (-1);
561 		if (adt746x_read(sc->sc_dev, sc->sc_addr, sens->reg + 1,
562 				 data1) < 0)
563 			return (-1);
564 		val = data[0] + (data1[0] << 8);
565 		/* A value of 0xffff means the fan is stopped.  */
566 		if (val == 0 || val == 0xffff)
567 			tmp = 0;
568 		else
569 			tmp = (90000 * 60) / val;
570 	}
571 	return (tmp);
572 }
573 
574 static int
adt746x_sensor_sysctl(SYSCTL_HANDLER_ARGS)575 adt746x_sensor_sysctl(SYSCTL_HANDLER_ARGS)
576 {
577 	device_t dev;
578 	struct adt746x_softc *sc;
579 	struct adt746x_sensor *sens;
580 	int value, error;
581 
582 	dev = arg1;
583 	sc = device_get_softc(dev);
584 	sens = &sc->sc_sensors[arg2];
585 
586 	value = sens->therm.read(&sens->therm);
587 	if (value < 0)
588 		return (ENXIO);
589 
590 	error = sysctl_handle_int(oidp, &value, 0, req);
591 
592 	return (error);
593 }
594 
595 static void
adt746x_attach_sensors(device_t dev)596 adt746x_attach_sensors(device_t dev)
597 {
598 	struct adt746x_softc *sc;
599 	struct sysctl_oid *oid, *sensroot_oid;
600 	struct sysctl_ctx_list *ctx;
601 	phandle_t child;
602 	char sysctl_name[40];
603 	const char *unit;
604 	const char *desc;
605 	int i, j;
606 
607 
608 	sc = device_get_softc(dev);
609 	sc->sc_nsensors = 0;
610 	child = ofw_bus_get_node(dev);
611 
612 	/* Count the actual number of sensors. */
613 	sc->sc_nsensors = adt746x_fill_sensor_prop(dev);
614 	device_printf(dev, "%d sensors detected!\n", sc->sc_nsensors);
615 	if (sc->sc_nsensors == 0) {
616 		device_printf(dev, "WARNING: No sensors detected!\n");
617 		return;
618 	}
619 
620 	ctx = device_get_sysctl_ctx(dev);
621 	sensroot_oid = SYSCTL_ADD_NODE(ctx,
622 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensors",
623 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "ADT Sensor Information");
624 
625 	/* Add the sysctl for the sensors. */
626 	for (i = 0; i < sc->sc_nsensors; i++) {
627 		for (j = 0; j < strlen(sc->sc_sensors[i].therm.name); j++) {
628 			sysctl_name[j] = tolower(sc->sc_sensors[i].therm.name[j]);
629 			if (isspace(sysctl_name[j]))
630 				sysctl_name[j] = '_';
631 		}
632 		sysctl_name[j] = 0;
633 		oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
634 		    OID_AUTO, sysctl_name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
635 		    "Sensor Information");
636 		if (sc->sc_sensors[i].type == ADT746X_SENSOR_TEMP) {
637 			unit = "temp";
638 			desc = "sensor unit (C)";
639 		} else if (sc->sc_sensors[i].type == ADT746X_SENSOR_VOLT) {
640 			unit = "volt";
641 			desc = "sensor unit (mV)";
642 		} else {
643 			unit = "rpm";
644 			desc = "sensor unit (RPM)";
645 		}
646 		/* I use i to pass the sensor id. */
647 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
648 		    unit, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, dev, i,
649 		    adt746x_sensor_sysctl,
650 		    sc->sc_sensors[i].type == ADT746X_SENSOR_TEMP ?
651 		    "IK" : "I", desc);
652 	}
653 
654 	/* Dump sensor location & type. */
655 	if (bootverbose) {
656 		for (i = 0; i < sc->sc_nsensors; i++) {
657 			device_printf(dev, "Sensor location: %s",
658 				      sc->sc_sensors[i].therm.name);
659 			device_printf(dev, " type: %d id: %d reg: 0x%x\n",
660 				      sc->sc_sensors[i].type,
661 				      sc->sc_sensors[i].id,
662 				      sc->sc_sensors[i].reg);
663 		}
664 	}
665 }
666