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