2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Andreas Tobler
5 * Copyright (c) 2014 Justin Hibbits
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/module.h>
37 #include <sys/callout.h>
40 #include <sys/ctype.h>
41 #include <sys/kernel.h>
42 #include <sys/reboot.h>
44 #include <sys/sysctl.h>
45 #include <sys/limits.h>
47 #include <machine/bus.h>
48 #include <machine/md_var.h>
50 #include <dev/iicbus/iicbus.h>
51 #include <dev/iicbus/iiconf.h>
53 #include <dev/ofw/openfirm.h>
54 #include <dev/ofw/ofw_bus.h>
55 #include <powerpc/powermac/powermac_thermal.h>
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
72 #define ADT7460_DEV_ID 0x27
73 #define ADT7467_DEV_ID 0x68
84 struct adt746x_sensor {
85 struct pmac_therm therm;
96 struct adt746x_softc {
98 struct intr_config_hook enum_hook;
100 /* The 7467 supports up to 4 fans, 2 voltage and 3 temperature sensors. */
101 struct adt746x_fan sc_fans[4];
103 struct adt746x_sensor sc_sensors[9];
110 /* Regular bus attachment functions */
112 static int adt746x_probe(device_t);
113 static int adt746x_attach(device_t);
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);
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);
127 /* i2c read/write functions. */
128 static int adt746x_write(device_t dev, uint32_t addr, uint8_t reg,
130 static int adt746x_read(device_t dev, uint32_t addr, uint8_t reg,
133 static device_method_t adt746x_methods[] = {
134 /* Device interface */
135 DEVMETHOD(device_probe, adt746x_probe),
136 DEVMETHOD(device_attach, adt746x_attach),
140 static driver_t adt746x_driver = {
143 sizeof(struct adt746x_softc)
146 static devclass_t adt746x_devclass;
148 DRIVER_MODULE(adt746x, iicbus, adt746x_driver, adt746x_devclass, 0, 0);
149 static MALLOC_DEFINE(M_ADT746X, "adt746x", "ADT Sensor Information");
152 /* i2c read/write functions. */
155 adt746x_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buff)
160 struct iic_msg msg[] = {
161 {addr, IIC_M_WR, 2, buf }
164 /* Prepare the write msg. */
166 memcpy(buf + 1, buff, 1);
170 if (iicbus_transfer(dev, msg, 1) == 0)
173 device_printf(dev, "iicbus write failed\n");
176 pause("adt746x_write", hz);
182 adt746x_read(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
187 struct iic_msg msg[2] = {
188 {addr, IIC_M_WR | IIC_M_NOSTOP, 1, ®},
189 {addr, IIC_M_RD, 1, buf},
194 err = iicbus_transfer(dev, msg, 2);
198 *data = *((uint8_t*)buf);
202 device_printf(dev, "iicbus read failed\n");
205 pause("adt746x_read", hz);
210 adt746x_probe(device_t dev)
212 const char *name, *compatible;
213 struct adt746x_softc *sc;
215 name = ofw_bus_get_name(dev);
216 compatible = ofw_bus_get_compat(dev);
221 if (strcmp(name, "fan") != 0 ||
222 (strcmp(compatible, "adt7460") != 0 &&
223 strcmp(compatible, "adt7467") != 0))
226 sc = device_get_softc(dev);
228 sc->sc_addr = iicbus_get_addr(dev);
230 device_set_desc(dev, "Apple Thermostat Unit ADT746X");
236 adt746x_attach(device_t dev)
238 struct adt746x_softc *sc;
240 sc = device_get_softc(dev);
242 sc->enum_hook.ich_func = adt746x_start;
243 sc->enum_hook.ich_arg = dev;
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.
253 if (config_intrhook_establish(&sc->enum_hook) != 0)
260 adt746x_start(void *xdev)
262 uint8_t did, cid, rev, conf;
264 struct adt746x_softc *sc;
266 device_t dev = (device_t)xdev;
268 sc = device_get_softc(dev);
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);
275 device_printf(dev, "Dev ID %#x, Company ID %#x, Rev ID %#x CNF: %#x\n",
276 did, cid, rev, conf);
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
285 /* Start the ADT7460. */
286 if (sc->device_id == ADT7460_DEV_ID)
287 adt746x_write(sc->sc_dev, sc->sc_addr, ADT746X_CONFIG, &conf);
289 /* Detect and attach child devices. */
290 adt746x_attach_fans(dev);
291 adt746x_attach_sensors(dev);
292 config_intrhook_disestablish(&sc->enum_hook);
296 * Sensor and fan management
299 adt746x_fan_set_pwm(struct adt746x_fan *fan, int pwm)
301 uint8_t reg = 0, manual, mode = 0;
302 struct adt746x_softc *sc;
305 sc = device_get_softc(fan->dev);
307 /* Clamp to allowed range */
308 pwm = max(fan->fan.min_rpm, pwm);
309 pwm = min(fan->fan.max_rpm, pwm);
312 mode = fan->conf_reg;
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).
318 buf = (pwm * 100 / 39) - (pwm ? 1 : 0);
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);
327 adt746x_write(sc->sc_dev, sc->sc_addr, reg, &buf);
333 adt746x_fan_get_pwm(struct adt746x_fan *fan)
337 struct adt746x_softc *sc;
339 sc = device_get_softc(fan->dev);
343 adt746x_read(sc->sc_dev, sc->sc_addr, reg, &buf);
345 pwm = (buf * 39 / 100) + (buf ? 1 : 0);
350 adt746x_fill_fan_prop(device_t dev)
353 struct adt746x_softc *sc;
356 int i, id_len, len = 0, location_len, prev_len = 0;
358 sc = device_get_softc(dev);
360 child = ofw_bus_get_node(dev);
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);
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;
376 sc->sc_fans[i].id = id[i];
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;
384 sc->sc_fans[i].pwm_reg = ADT746X_PWM1 + i;
385 sc->sc_fans[i].conf_reg = ADT746X_PWM1_CONF + i;
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;
395 OF_prop_free(location);
402 adt746x_fill_sensor_prop(device_t dev)
404 phandle_t child, node;
405 struct adt746x_softc *sc;
407 int i = 0, reg, sensid;
409 sc = device_get_softc(dev);
411 child = ofw_bus_get_node(dev);
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)
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;
424 sc->sc_sensors[i].type = ADT746X_SENSOR_SPEED;
425 OF_getprop(node, "reg", ®, sizeof(reg));
426 OF_getprop(node, "sensor-id", &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;
441 pmac_thermal_sensor_register(&sc->sc_sensors[i].therm);
450 adt746x_fanrpm_sysctl(SYSCTL_HANDLER_ARGS)
453 struct adt746x_softc *sc;
454 struct adt746x_fan *fan;
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);
463 if (error || !req->newptr)
466 return (adt746x_fan_set_pwm(fan, pwm));
470 adt746x_attach_fans(device_t dev)
472 struct adt746x_softc *sc;
473 struct sysctl_oid *oid, *fanroot_oid;
474 struct sysctl_ctx_list *ctx;
476 char sysctl_name[32];
479 sc = device_get_softc(dev);
483 child = ofw_bus_get_node(dev);
485 /* Count the actual number of fans. */
486 sc->sc_nfans = adt746x_fill_fan_prop(dev);
488 device_printf(dev, "%d fans detected!\n", sc->sc_nfans);
490 if (sc->sc_nfans == 0) {
491 device_printf(dev, "WARNING: No fans detected!\n");
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");
500 /* Now we can fill the properties into the allocated struct. */
501 sc->sc_nfans = adt746x_fill_fan_prop(dev);
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);
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] = '_';
516 sc->sc_fans[i].setpoint =
517 adt746x_fan_get_pwm(&sc->sc_fans[i]);
519 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(fanroot_oid),
520 OID_AUTO, sysctl_name, CTLFLAG_RD, 0, "Fan Information");
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 %");
528 /* Dump fan location & type. */
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",
535 sc->sc_fans[i].setpoint);
541 adt746x_sensor_read(struct adt746x_sensor *sens)
543 struct adt746x_softc *sc;
546 uint8_t data[1], data1[1];
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,
554 if (sens->type == ADT746X_SENSOR_TEMP)
555 tmp = 10 * temp + ZERO_C_TO_K;
559 if (adt746x_read(sc->sc_dev, sc->sc_addr, sens->reg,
562 if (adt746x_read(sc->sc_dev, sc->sc_addr, sens->reg + 1,
565 val = data[0] + (data1[0] << 8);
566 /* A value of 0xffff means the fan is stopped. */
567 if (val == 0 || val == 0xffff)
570 tmp = (90000 * 60) / val;
576 adt746x_sensor_sysctl(SYSCTL_HANDLER_ARGS)
579 struct adt746x_softc *sc;
580 struct adt746x_sensor *sens;
584 sc = device_get_softc(dev);
585 sens = &sc->sc_sensors[arg2];
587 value = sens->therm.read(&sens->therm);
591 error = sysctl_handle_int(oidp, &value, 0, req);
597 adt746x_attach_sensors(device_t dev)
599 struct adt746x_softc *sc;
600 struct sysctl_oid *oid, *sensroot_oid;
601 struct sysctl_ctx_list *ctx;
603 char sysctl_name[40];
609 sc = device_get_softc(dev);
611 child = ofw_bus_get_node(dev);
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");
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");
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] = '_';
634 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
636 sysctl_name, CTLFLAG_RD, 0,
637 "Sensor Information");
638 if (sc->sc_sensors[i].type == ADT746X_SENSOR_TEMP) {
640 desc = "sensor unit (C)";
641 } else if (sc->sc_sensors[i].type == ADT746X_SENSOR_VOLT) {
643 desc = "sensor unit (mV)";
646 desc = "sensor unit (RPM)";
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 ?
656 /* Dump sensor location & type. */
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);