2 * Copyright (c) 2010 Andreas Tobler
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
34 #include <sys/callout.h>
37 #include <sys/ctype.h>
38 #include <sys/kernel.h>
39 #include <sys/reboot.h>
41 #include <sys/sysctl.h>
42 #include <sys/limits.h>
44 #include <machine/bus.h>
45 #include <machine/md_var.h>
47 #include <dev/iicbus/iicbus.h>
48 #include <dev/iicbus/iiconf.h>
50 #include <dev/ofw/openfirm.h>
51 #include <dev/ofw/ofw_bus.h>
52 #include <powerpc/powermac/powermac_thermal.h>
54 /* CPU A/B sensors, temp and adc: AD7417. */
56 #define AD7417_TEMP 0x00
57 #define AD7417_CONFIG 0x01
58 #define AD7417_ADC 0x04
59 #define AD7417_CONFIG2 0x05
60 #define AD7417_CONFMASK 0xe0
62 uint8_t adc741x_config;
64 struct ad7417_sensor {
65 struct pmac_therm therm;
84 /* Regular bus attachment functions */
85 static int ad7417_probe(device_t);
86 static int ad7417_attach(device_t);
88 /* Utility functions */
89 static int ad7417_sensor_sysctl(SYSCTL_HANDLER_ARGS);
90 static int ad7417_write(device_t dev, uint32_t addr, uint8_t reg,
91 uint8_t *buf, int len);
92 static int ad7417_read_1(device_t dev, uint32_t addr, uint8_t reg,
94 static int ad7417_read_2(device_t dev, uint32_t addr, uint8_t reg,
96 static int ad7417_write_read(device_t dev, uint32_t addr,
97 struct write_data out, struct read_data *in);
98 static int ad7417_diode_read(struct ad7417_sensor *sens);
99 static int ad7417_adc_read(struct ad7417_sensor *sens);
100 static int ad7417_sensor_read(struct ad7417_sensor *sens);
102 struct ad7417_softc {
105 struct ad7417_sensor *sc_sensors;
109 static device_method_t ad7417_methods[] = {
110 /* Device interface */
111 DEVMETHOD(device_probe, ad7417_probe),
112 DEVMETHOD(device_attach, ad7417_attach),
116 static driver_t ad7417_driver = {
119 sizeof(struct ad7417_softc)
122 static devclass_t ad7417_devclass;
124 DRIVER_MODULE(ad7417, iicbus, ad7417_driver, ad7417_devclass, 0, 0);
125 static MALLOC_DEFINE(M_AD7417, "ad7417", "Supply-Monitor AD7417");
129 ad7417_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buff, int len)
131 unsigned char buf[4];
134 struct iic_msg msg[] = {
135 { addr, IIC_M_WR, 0, buf }
138 msg[0].len = len + 1;
140 memcpy(buf + 1, buff, len);
144 if (iicbus_transfer(dev, msg, 1) == 0)
148 device_printf(dev, "iicbus write failed\n");
151 pause("ad7417_write", hz);
156 ad7417_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
161 struct iic_msg msg[2] = {
162 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
163 { addr, IIC_M_RD, 1, buf },
168 err = iicbus_transfer(dev, msg, 2);
172 *data = *((uint8_t*)buf);
176 device_printf(dev, "iicbus read failed\n");
179 pause("ad7417_read_1", hz);
184 ad7417_read_2(device_t dev, uint32_t addr, uint8_t reg, uint16_t *data)
189 struct iic_msg msg[2] = {
190 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
191 { addr, IIC_M_RD, 2, buf },
196 err = iicbus_transfer(dev, msg, 2);
200 *data = *((uint16_t*)buf);
204 device_printf(dev, "iicbus read failed\n");
207 pause("ad7417_read_2", hz);
212 ad7417_write_read(device_t dev, uint32_t addr, struct write_data out,
213 struct read_data *in)
218 /* Do a combined write/read. */
219 struct iic_msg msg[3] = {
220 { addr, IIC_M_WR, 2, buf },
221 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, &in->reg },
222 { addr, IIC_M_RD, 2, buf },
225 /* Prepare the write msg. */
227 buf[1] = out.val & 0xff;
231 err = iicbus_transfer(dev, msg, 3);
235 in->val = *((uint16_t*)buf);
239 device_printf(dev, "iicbus write/read failed\n");
242 pause("ad7417_write_read", hz);
247 ad7417_init_adc(device_t dev, uint32_t addr)
251 struct ad7417_softc *sc;
253 sc = device_get_softc(dev);
259 err = ad7417_write(dev, addr, AD7417_CONFIG2, &buf, 1);
261 /* Read & cache Config1 */
263 err = ad7417_write(dev, addr, AD7417_CONFIG, &buf, 1);
264 err = ad7417_read_1(dev, addr, AD7417_CONFIG, &buf);
265 adc741x_config = (uint8_t)buf;
267 /* Disable shutdown mode */
268 adc741x_config &= 0xfe;
269 buf = adc741x_config;
270 err = ad7417_write(dev, addr, AD7417_CONFIG, &buf, 1);
280 ad7417_probe(device_t dev)
282 const char *name, *compatible;
283 struct ad7417_softc *sc;
285 name = ofw_bus_get_name(dev);
286 compatible = ofw_bus_get_compat(dev);
291 if (strcmp(name, "supply-monitor") != 0 ||
292 strcmp(compatible, "ad7417") != 0)
295 sc = device_get_softc(dev);
297 sc->sc_addr = iicbus_get_addr(dev);
299 device_set_desc(dev, "Supply-Monitor AD7417");
305 * This function returns the number of sensors. If we call it the second time
306 * and we have allocated memory for sc->sc_sensors, we fill in the properties.
309 ad7417_fill_sensor_prop(device_t dev)
312 struct ad7417_softc *sc;
316 int i = 0, j, len = 0, prop_len, prev_len = 0;
318 sc = device_get_softc(dev);
320 child = ofw_bus_get_node(dev);
322 /* Fill the sensor location property. */
323 prop_len = OF_getprop(child, "hwsensor-location", location,
325 while (len < prop_len) {
326 if (sc->sc_sensors != NULL)
327 strcpy(sc->sc_sensors[i].therm.name, location + len);
328 prev_len = strlen(location + len) + 1;
332 if (sc->sc_sensors == NULL)
335 /* Fill the sensor type property. */
339 prop_len = OF_getprop(child, "hwsensor-type", type, sizeof(type));
340 while (len < prop_len) {
341 if (strcmp(type + len, "temperature") == 0)
342 sc->sc_sensors[i].type = ADC7417_TEMP_SENSOR;
344 sc->sc_sensors[i].type = ADC7417_ADC_SENSOR;
345 prev_len = strlen(type + len) + 1;
350 /* Fill the sensor id property. Taken from OF. */
351 prop_len = OF_getprop(child, "hwsensor-id", id, sizeof(id));
352 for (j = 0; j < i; j++)
353 sc->sc_sensors[j].id = id[j];
355 /* Fill the sensor zone property. Taken from OF. */
356 prop_len = OF_getprop(child, "hwsensor-zone", id, sizeof(id));
357 for (j = 0; j < i; j++)
358 sc->sc_sensors[j].therm.zone = id[j];
360 /* Finish setting up sensor properties */
361 for (j = 0; j < i; j++) {
362 sc->sc_sensors[j].dev = dev;
364 /* HACK: Apple wired a random diode to the ADC line */
365 if (strstr(sc->sc_sensors[j].therm.name, "DIODE TEMP")
367 sc->sc_sensors[j].type = ADC7417_TEMP_SENSOR;
368 sc->sc_sensors[j].therm.read =
369 (int (*)(struct pmac_therm *))(ad7417_diode_read);
371 sc->sc_sensors[j].therm.read =
372 (int (*)(struct pmac_therm *))(ad7417_sensor_read);
375 if (sc->sc_sensors[j].type != ADC7417_TEMP_SENSOR)
378 /* Make up some ranges */
379 sc->sc_sensors[j].therm.target_temp = 500 + ZERO_C_TO_K;
380 sc->sc_sensors[j].therm.max_temp = 900 + ZERO_C_TO_K;
382 pmac_thermal_sensor_register(&sc->sc_sensors[j].therm);
389 ad7417_attach(device_t dev)
391 struct ad7417_softc *sc;
392 struct sysctl_oid *oid, *sensroot_oid;
393 struct sysctl_ctx_list *ctx;
394 char sysctl_name[32];
399 sc = device_get_softc(dev);
403 /* Count the actual number of sensors. */
404 sc->sc_nsensors = ad7417_fill_sensor_prop(dev);
406 device_printf(dev, "%d sensors detected.\n", sc->sc_nsensors);
408 if (sc->sc_nsensors == 0)
409 device_printf(dev, "WARNING: No AD7417 sensors detected!\n");
411 sc->sc_sensors = malloc (sc->sc_nsensors * sizeof(struct ad7417_sensor),
412 M_AD7417, M_WAITOK | M_ZERO);
414 ctx = device_get_sysctl_ctx(dev);
415 sensroot_oid = SYSCTL_ADD_NODE(ctx,
416 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor",
417 CTLFLAG_RD, 0, "AD7417 Sensor Information");
419 /* Now we can fill the properties into the allocated struct. */
420 sc->sc_nsensors = ad7417_fill_sensor_prop(dev);
422 /* Add sysctls for the sensors. */
423 for (i = 0; i < sc->sc_nsensors; i++) {
424 for (j = 0; j < strlen(sc->sc_sensors[i].therm.name); j++) {
426 tolower(sc->sc_sensors[i].therm.name[j]);
427 if (isspace(sysctl_name[j]))
428 sysctl_name[j] = '_';
432 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
434 sysctl_name, CTLFLAG_RD, 0,
435 "Sensor Information");
437 if (sc->sc_sensors[i].type == ADC7417_TEMP_SENSOR) {
439 desc = "sensor unit (C)";
442 desc = "sensor unit (mV)";
444 /* I use i to pass the sensor id. */
445 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
446 unit, CTLTYPE_INT | CTLFLAG_RD, dev,
447 i, ad7417_sensor_sysctl,
448 sc->sc_sensors[i].type == ADC7417_TEMP_SENSOR ?
451 /* Dump sensor location, ID & type. */
453 device_printf(dev, "Sensors\n");
454 for (i = 0; i < sc->sc_nsensors; i++) {
455 device_printf(dev, "Location: %s ID: %d type: %d\n",
456 sc->sc_sensors[i].therm.name,
457 sc->sc_sensors[i].id,
458 sc->sc_sensors[i].type);
466 ad7417_get_temp(device_t dev, uint32_t addr, int *temp)
472 err = ad7417_read_2(dev, addr, AD7417_TEMP, buf);
477 read = *((int16_t*)buf);
479 /* The ADC is 10 bit, the resolution is 0.25 C.
480 The temperature is in tenth kelvin.
482 *temp = (((int16_t)(read & 0xffc0)) >> 6) * 25 / 10;
487 ad7417_get_adc(device_t dev, uint32_t addr, unsigned int *value,
492 struct write_data config;
493 struct read_data data;
496 config.reg = AD7417_CONFIG;
497 data.reg = AD7417_ADC;
500 err = ad7417_read_1(dev, addr, AD7417_CONFIG, &config.val);
502 config.val = (config.val & ~AD7417_CONFMASK) | (tmp & AD7417_CONFMASK);
504 err = ad7417_write_read(dev, addr, config, &data);
508 *value = ((uint32_t)data.val) >> 6;
514 ad7417_diode_read(struct ad7417_sensor *sens)
516 static int eeprom_read = 0;
517 static cell_t eeprom[2][40];
518 phandle_t eeprom_node;
519 int rawval, diode_slope, diode_offset;
523 eeprom_node = OF_finddevice("/u3/i2c/cpuid@a0");
524 OF_getprop(eeprom_node, "cpuid", eeprom[0], sizeof(eeprom[0]));
525 eeprom_node = OF_finddevice("/u3/i2c/cpuid@a2");
526 OF_getprop(eeprom_node, "cpuid", eeprom[1], sizeof(eeprom[1]));
530 rawval = ad7417_adc_read(sens);
534 if (strstr(sens->therm.name, "CPU B") != NULL) {
535 diode_slope = eeprom[1][0x11] >> 16;
536 diode_offset = (int16_t)(eeprom[1][0x11] & 0xffff) << 12;
538 diode_slope = eeprom[0][0x11] >> 16;
539 diode_offset = (int16_t)(eeprom[0][0x11] & 0xffff) << 12;
542 temp = (rawval*diode_slope + diode_offset) >> 2;
543 temp = (10*(temp >> 16)) + ((10*(temp & 0xffff)) >> 16);
545 return (temp + ZERO_C_TO_K);
549 ad7417_adc_read(struct ad7417_sensor *sens)
551 struct ad7417_softc *sc;
555 sc = device_get_softc(sens->dev);
578 if (ad7417_get_adc(sc->sc_dev, sc->sc_addr, &temp, chan) < 0)
586 ad7417_sensor_read(struct ad7417_sensor *sens)
588 struct ad7417_softc *sc;
591 sc = device_get_softc(sens->dev);
593 /* Init the ADC if not already done.*/
595 if (ad7417_init_adc(sc->sc_dev, sc->sc_addr) < 0)
598 if (sens->type == ADC7417_TEMP_SENSOR) {
599 if (ad7417_get_temp(sc->sc_dev, sc->sc_addr, &temp) < 0)
603 temp = ad7417_adc_read(sens);
609 ad7417_sensor_sysctl(SYSCTL_HANDLER_ARGS)
612 struct ad7417_softc *sc;
613 struct ad7417_sensor *sens;
618 sc = device_get_softc(dev);
619 sens = &sc->sc_sensors[arg2];
621 value = sens->therm.read(&sens->therm);
625 error = sysctl_handle_int(oidp, &value, 0, req);