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;
108 static device_method_t ad7417_methods[] = {
109 /* Device interface */
110 DEVMETHOD(device_probe, ad7417_probe),
111 DEVMETHOD(device_attach, ad7417_attach),
115 static driver_t ad7417_driver = {
118 sizeof(struct ad7417_softc)
121 static devclass_t ad7417_devclass;
123 DRIVER_MODULE(ad7417, iicbus, ad7417_driver, ad7417_devclass, 0, 0);
124 MALLOC_DEFINE(M_AD7417, "ad7417", "Supply-Monitor AD7417");
128 ad7417_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buff, int len)
130 unsigned char buf[4];
133 struct iic_msg msg[] = {
134 { addr, IIC_M_WR, 0, buf }
137 msg[0].len = len + 1;
139 memcpy(buf + 1, buff, len);
143 if (iicbus_transfer(dev, msg, 1) == 0)
147 device_printf(dev, "iicbus write failed\n");
150 pause("ad7417_write", hz);
155 ad7417_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
160 struct iic_msg msg[2] = {
161 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
162 { addr, IIC_M_RD, 1, buf },
167 err = iicbus_transfer(dev, msg, 2);
171 *data = *((uint8_t*)buf);
175 device_printf(dev, "iicbus read failed\n");
178 pause("ad7417_read_1", hz);
183 ad7417_read_2(device_t dev, uint32_t addr, uint8_t reg, uint16_t *data)
188 struct iic_msg msg[2] = {
189 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
190 { addr, IIC_M_RD, 2, buf },
195 err = iicbus_transfer(dev, msg, 2);
199 *data = *((uint16_t*)buf);
203 device_printf(dev, "iicbus read failed\n");
206 pause("ad7417_read_2", hz);
211 ad7417_write_read(device_t dev, uint32_t addr, struct write_data out,
212 struct read_data *in)
217 /* Do a combined write/read. */
218 struct iic_msg msg[3] = {
219 { addr, IIC_M_WR, 2, buf },
220 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, &in->reg },
221 { addr, IIC_M_RD, 2, buf },
224 /* Prepare the write msg. */
226 buf[1] = out.val & 0xff;
230 err = iicbus_transfer(dev, msg, 3);
234 in->val = *((uint16_t*)buf);
238 device_printf(dev, "iicbus write/read failed\n");
241 pause("ad7417_write_read", hz);
246 ad7417_init_adc(device_t dev, uint32_t addr)
255 err = ad7417_write(dev, addr, AD7417_CONFIG2, &buf, 1);
257 /* Read & cache Config1 */
259 err = ad7417_write(dev, addr, AD7417_CONFIG, &buf, 1);
260 err = ad7417_read_1(dev, addr, AD7417_CONFIG, &buf);
261 adc741x_config = (uint8_t)buf;
263 /* Disable shutdown mode */
264 adc741x_config &= 0xfe;
265 buf = adc741x_config;
266 err = ad7417_write(dev, addr, AD7417_CONFIG, &buf, 1);
274 ad7417_probe(device_t dev)
276 const char *name, *compatible;
277 struct ad7417_softc *sc;
279 name = ofw_bus_get_name(dev);
280 compatible = ofw_bus_get_compat(dev);
285 if (strcmp(name, "supply-monitor") != 0 ||
286 strcmp(compatible, "ad7417") != 0)
289 sc = device_get_softc(dev);
291 sc->sc_addr = iicbus_get_addr(dev);
293 device_set_desc(dev, "Supply-Monitor AD7417");
299 * This function returns the number of sensors. If we call it the second time
300 * and we have allocated memory for sc->sc_sensors, we fill in the properties.
303 ad7417_fill_sensor_prop(device_t dev)
306 struct ad7417_softc *sc;
310 int i = 0, j, len = 0, prop_len, prev_len = 0;
312 sc = device_get_softc(dev);
314 child = ofw_bus_get_node(dev);
316 /* Fill the sensor location property. */
317 prop_len = OF_getprop(child, "hwsensor-location", location,
319 while (len < prop_len) {
320 if (sc->sc_sensors != NULL)
321 strcpy(sc->sc_sensors[i].therm.name, location + len);
322 prev_len = strlen(location + len) + 1;
326 if (sc->sc_sensors == NULL)
329 /* Fill the sensor type property. */
333 prop_len = OF_getprop(child, "hwsensor-type", type, sizeof(type));
334 while (len < prop_len) {
335 if (strcmp(type + len, "temperature") == 0)
336 sc->sc_sensors[i].type = ADC7417_TEMP_SENSOR;
338 sc->sc_sensors[i].type = ADC7417_ADC_SENSOR;
339 prev_len = strlen(type + len) + 1;
344 /* Fill the sensor id property. Taken from OF. */
345 prop_len = OF_getprop(child, "hwsensor-id", id, sizeof(id));
346 for (j = 0; j < i; j++)
347 sc->sc_sensors[j].id = id[j];
349 /* Fill the sensor zone property. Taken from OF. */
350 prop_len = OF_getprop(child, "hwsensor-zone", id, sizeof(id));
351 for (j = 0; j < i; j++)
352 sc->sc_sensors[j].therm.zone = id[j];
354 /* Finish setting up sensor properties */
355 for (j = 0; j < i; j++) {
356 sc->sc_sensors[j].dev = dev;
358 /* HACK: Apple wired a random diode to the ADC line */
359 if (strstr(sc->sc_sensors[j].therm.name, "DIODE TEMP")
361 sc->sc_sensors[j].type = ADC7417_TEMP_SENSOR;
362 sc->sc_sensors[j].therm.read =
363 (int (*)(struct pmac_therm *))(ad7417_diode_read);
365 sc->sc_sensors[j].therm.read =
366 (int (*)(struct pmac_therm *))(ad7417_sensor_read);
369 if (sc->sc_sensors[j].type != ADC7417_TEMP_SENSOR)
372 /* Make up some ranges */
373 sc->sc_sensors[j].therm.target_temp = 500 + ZERO_C_TO_K;
374 sc->sc_sensors[j].therm.max_temp = 900 + ZERO_C_TO_K;
376 pmac_thermal_sensor_register(&sc->sc_sensors[j].therm);
383 ad7417_attach(device_t dev)
385 struct ad7417_softc *sc;
386 struct sysctl_oid *oid, *sensroot_oid;
387 struct sysctl_ctx_list *ctx;
388 char sysctl_name[32];
393 sc = device_get_softc(dev);
397 /* Count the actual number of sensors. */
398 sc->sc_nsensors = ad7417_fill_sensor_prop(dev);
400 device_printf(dev, "%d sensors detected.\n", sc->sc_nsensors);
402 if (sc->sc_nsensors == 0)
403 device_printf(dev, "WARNING: No AD7417 sensors detected!\n");
405 sc->sc_sensors = malloc (sc->sc_nsensors * sizeof(struct ad7417_sensor),
406 M_AD7417, M_WAITOK | M_ZERO);
408 ctx = device_get_sysctl_ctx(dev);
409 sensroot_oid = SYSCTL_ADD_NODE(ctx,
410 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor",
411 CTLFLAG_RD, 0, "AD7417 Sensor Information");
413 /* Now we can fill the properties into the allocated struct. */
414 sc->sc_nsensors = ad7417_fill_sensor_prop(dev);
416 /* Add sysctls for the sensors. */
417 for (i = 0; i < sc->sc_nsensors; i++) {
418 for (j = 0; j < strlen(sc->sc_sensors[i].therm.name); j++) {
420 tolower(sc->sc_sensors[i].therm.name[j]);
421 if (isspace(sysctl_name[j]))
422 sysctl_name[j] = '_';
426 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
428 sysctl_name, CTLFLAG_RD, 0,
429 "Sensor Information");
431 if (sc->sc_sensors[i].type == ADC7417_TEMP_SENSOR) {
433 desc = "Sensor temp in C";
436 desc = "Sensor Volt in V";
438 /* I use i to pass the sensor id. */
439 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
440 unit, CTLTYPE_INT | CTLFLAG_RD, dev,
441 i, ad7417_sensor_sysctl,
442 sc->sc_sensors[i].type == ADC7417_TEMP_SENSOR ?
445 /* Dump sensor location, ID & type. */
447 device_printf(dev, "Sensors\n");
448 for (i = 0; i < sc->sc_nsensors; i++) {
449 device_printf(dev, "Location: %s ID: %d type: %d\n",
450 sc->sc_sensors[i].therm.name,
451 sc->sc_sensors[i].id,
452 sc->sc_sensors[i].type);
460 ad7417_get_temp(device_t dev, uint32_t addr, int *temp)
466 err = ad7417_read_2(dev, addr, AD7417_TEMP, buf);
471 read = *((int16_t*)buf);
473 /* The ADC is 10 bit, the resolution is 0.25 C.
474 The temperature is in tenth kelvin.
476 *temp = (((int16_t)(read & 0xffc0)) >> 6) * 25 / 10;
481 ad7417_get_adc(device_t dev, uint32_t addr, unsigned int *value,
486 struct write_data config;
487 struct read_data data;
490 config.reg = AD7417_CONFIG;
491 data.reg = AD7417_ADC;
494 err = ad7417_read_1(dev, addr, AD7417_CONFIG, &config.val);
496 config.val = (config.val & ~AD7417_CONFMASK) | (tmp & AD7417_CONFMASK);
498 err = ad7417_write_read(dev, addr, config, &data);
502 *value = ((uint32_t)data.val) >> 6;
508 ad7417_diode_read(struct ad7417_sensor *sens)
510 static int eeprom_read = 0;
511 static cell_t eeprom[2][40];
512 phandle_t eeprom_node;
513 int rawval, diode_slope, diode_offset;
517 eeprom_node = OF_finddevice("/u3/i2c/cpuid@a0");
518 OF_getprop(eeprom_node, "cpuid", eeprom[0], sizeof(eeprom[0]));
519 eeprom_node = OF_finddevice("/u3/i2c/cpuid@a2");
520 OF_getprop(eeprom_node, "cpuid", eeprom[1], sizeof(eeprom[1]));
524 rawval = ad7417_adc_read(sens);
528 if (strstr(sens->therm.name, "CPU B") != NULL) {
529 diode_slope = eeprom[1][0x11] >> 16;
530 diode_offset = (int16_t)(eeprom[1][0x11] & 0xffff) << 12;
532 diode_slope = eeprom[0][0x11] >> 16;
533 diode_offset = (int16_t)(eeprom[0][0x11] & 0xffff) << 12;
536 temp = (rawval*diode_slope + diode_offset) >> 2;
537 temp = (10*(temp >> 16)) + ((10*(temp & 0xffff)) >> 16);
539 return (temp + ZERO_C_TO_K);
543 ad7417_adc_read(struct ad7417_sensor *sens)
545 struct ad7417_softc *sc;
549 sc = device_get_softc(sens->dev);
572 if (ad7417_get_adc(sc->sc_dev, sc->sc_addr, &temp, chan) < 0)
580 ad7417_sensor_read(struct ad7417_sensor *sens)
582 struct ad7417_softc *sc;
585 sc = device_get_softc(sens->dev);
588 if (ad7417_init_adc(sc->sc_dev, sc->sc_addr) < 0)
591 if (sens->type == ADC7417_TEMP_SENSOR) {
592 if (ad7417_get_temp(sc->sc_dev, sc->sc_addr, &temp) < 0)
596 temp = ad7417_adc_read(sens);
602 ad7417_sensor_sysctl(SYSCTL_HANDLER_ARGS)
605 struct ad7417_softc *sc;
606 struct ad7417_sensor *sens;
611 sc = device_get_softc(dev);
612 sens = &sc->sc_sensors[arg2];
614 value = sens->therm.read(&sens->therm);
618 error = sysctl_handle_int(oidp, &value, 0, req);