2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 Andreas Tobler
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/module.h>
36 #include <sys/callout.h>
39 #include <sys/ctype.h>
40 #include <sys/kernel.h>
41 #include <sys/reboot.h>
43 #include <sys/sysctl.h>
44 #include <sys/limits.h>
46 #include <machine/bus.h>
47 #include <machine/md_var.h>
49 #include <dev/iicbus/iicbus.h>
50 #include <dev/iicbus/iiconf.h>
52 #include <dev/ofw/openfirm.h>
53 #include <dev/ofw/ofw_bus.h>
54 #include <powerpc/powermac/powermac_thermal.h>
56 /* CPU A/B sensors, temp and adc: AD7417. */
58 #define AD7417_TEMP 0x00
59 #define AD7417_CONFIG 0x01
60 #define AD7417_ADC 0x04
61 #define AD7417_CONFIG2 0x05
62 #define AD7417_CONFMASK 0xe0
64 uint8_t adc741x_config;
66 struct ad7417_sensor {
67 struct pmac_therm therm;
86 /* Regular bus attachment functions */
87 static int ad7417_probe(device_t);
88 static int ad7417_attach(device_t);
90 /* Utility functions */
91 static int ad7417_sensor_sysctl(SYSCTL_HANDLER_ARGS);
92 static int ad7417_write(device_t dev, uint32_t addr, uint8_t reg,
93 uint8_t *buf, int len);
94 static int ad7417_read_1(device_t dev, uint32_t addr, uint8_t reg,
96 static int ad7417_read_2(device_t dev, uint32_t addr, uint8_t reg,
98 static int ad7417_write_read(device_t dev, uint32_t addr,
99 struct write_data out, struct read_data *in);
100 static int ad7417_diode_read(struct ad7417_sensor *sens);
101 static int ad7417_adc_read(struct ad7417_sensor *sens);
102 static int ad7417_sensor_read(struct ad7417_sensor *sens);
104 struct ad7417_softc {
107 struct ad7417_sensor *sc_sensors;
111 static device_method_t ad7417_methods[] = {
112 /* Device interface */
113 DEVMETHOD(device_probe, ad7417_probe),
114 DEVMETHOD(device_attach, ad7417_attach),
118 static driver_t ad7417_driver = {
121 sizeof(struct ad7417_softc)
124 static devclass_t ad7417_devclass;
126 DRIVER_MODULE(ad7417, iicbus, ad7417_driver, ad7417_devclass, 0, 0);
127 static MALLOC_DEFINE(M_AD7417, "ad7417", "Supply-Monitor AD7417");
131 ad7417_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buff, int len)
133 unsigned char buf[4];
136 struct iic_msg msg[] = {
137 { addr, IIC_M_WR, 0, buf }
140 msg[0].len = len + 1;
142 memcpy(buf + 1, buff, len);
146 if (iicbus_transfer(dev, msg, 1) == 0)
150 device_printf(dev, "iicbus write failed\n");
153 pause("ad7417_write", hz);
158 ad7417_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
163 struct iic_msg msg[2] = {
164 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
165 { addr, IIC_M_RD, 1, buf },
170 err = iicbus_transfer(dev, msg, 2);
174 *data = *((uint8_t*)buf);
178 device_printf(dev, "iicbus read failed\n");
181 pause("ad7417_read_1", hz);
186 ad7417_read_2(device_t dev, uint32_t addr, uint8_t reg, uint16_t *data)
191 struct iic_msg msg[2] = {
192 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
193 { addr, IIC_M_RD, 2, buf },
198 err = iicbus_transfer(dev, msg, 2);
202 *data = *((uint16_t*)buf);
206 device_printf(dev, "iicbus read failed\n");
209 pause("ad7417_read_2", hz);
214 ad7417_write_read(device_t dev, uint32_t addr, struct write_data out,
215 struct read_data *in)
220 /* Do a combined write/read. */
221 struct iic_msg msg[3] = {
222 { addr, IIC_M_WR, 2, buf },
223 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, &in->reg },
224 { addr, IIC_M_RD, 2, buf },
227 /* Prepare the write msg. */
229 buf[1] = out.val & 0xff;
233 err = iicbus_transfer(dev, msg, 3);
237 in->val = *((uint16_t*)buf);
241 device_printf(dev, "iicbus write/read failed\n");
244 pause("ad7417_write_read", hz);
249 ad7417_init_adc(device_t dev, uint32_t addr)
253 struct ad7417_softc *sc;
255 sc = device_get_softc(dev);
261 err = ad7417_write(dev, addr, AD7417_CONFIG2, &buf, 1);
263 /* Read & cache Config1 */
265 err = ad7417_write(dev, addr, AD7417_CONFIG, &buf, 1);
266 err = ad7417_read_1(dev, addr, AD7417_CONFIG, &buf);
267 adc741x_config = (uint8_t)buf;
269 /* Disable shutdown mode */
270 adc741x_config &= 0xfe;
271 buf = adc741x_config;
272 err = ad7417_write(dev, addr, AD7417_CONFIG, &buf, 1);
282 ad7417_probe(device_t dev)
284 const char *name, *compatible;
285 struct ad7417_softc *sc;
287 name = ofw_bus_get_name(dev);
288 compatible = ofw_bus_get_compat(dev);
293 if (strcmp(name, "supply-monitor") != 0 ||
294 strcmp(compatible, "ad7417") != 0)
297 sc = device_get_softc(dev);
299 sc->sc_addr = iicbus_get_addr(dev);
301 device_set_desc(dev, "Supply-Monitor AD7417");
307 * This function returns the number of sensors. If we call it the second time
308 * and we have allocated memory for sc->sc_sensors, we fill in the properties.
311 ad7417_fill_sensor_prop(device_t dev)
314 struct ad7417_softc *sc;
318 int i = 0, j, len = 0, prop_len, prev_len = 0;
320 sc = device_get_softc(dev);
322 child = ofw_bus_get_node(dev);
324 /* Fill the sensor location property. */
325 prop_len = OF_getprop(child, "hwsensor-location", location,
327 while (len < prop_len) {
328 if (sc->sc_sensors != NULL)
329 strcpy(sc->sc_sensors[i].therm.name, location + len);
330 prev_len = strlen(location + len) + 1;
334 if (sc->sc_sensors == NULL)
337 /* Fill the sensor type property. */
341 prop_len = OF_getprop(child, "hwsensor-type", type, sizeof(type));
342 while (len < prop_len) {
343 if (strcmp(type + len, "temperature") == 0)
344 sc->sc_sensors[i].type = ADC7417_TEMP_SENSOR;
346 sc->sc_sensors[i].type = ADC7417_ADC_SENSOR;
347 prev_len = strlen(type + len) + 1;
352 /* Fill the sensor id property. Taken from OF. */
353 prop_len = OF_getprop(child, "hwsensor-id", id, sizeof(id));
354 for (j = 0; j < i; j++)
355 sc->sc_sensors[j].id = id[j];
357 /* Fill the sensor zone property. Taken from OF. */
358 prop_len = OF_getprop(child, "hwsensor-zone", id, sizeof(id));
359 for (j = 0; j < i; j++)
360 sc->sc_sensors[j].therm.zone = id[j];
362 /* Finish setting up sensor properties */
363 for (j = 0; j < i; j++) {
364 sc->sc_sensors[j].dev = dev;
366 /* HACK: Apple wired a random diode to the ADC line */
367 if (strstr(sc->sc_sensors[j].therm.name, "DIODE TEMP")
369 sc->sc_sensors[j].type = ADC7417_TEMP_SENSOR;
370 sc->sc_sensors[j].therm.read =
371 (int (*)(struct pmac_therm *))(ad7417_diode_read);
373 sc->sc_sensors[j].therm.read =
374 (int (*)(struct pmac_therm *))(ad7417_sensor_read);
377 if (sc->sc_sensors[j].type != ADC7417_TEMP_SENSOR)
380 /* Make up some ranges */
381 sc->sc_sensors[j].therm.target_temp = 500 + ZERO_C_TO_K;
382 sc->sc_sensors[j].therm.max_temp = 900 + ZERO_C_TO_K;
384 pmac_thermal_sensor_register(&sc->sc_sensors[j].therm);
391 ad7417_attach(device_t dev)
393 struct ad7417_softc *sc;
394 struct sysctl_oid *oid, *sensroot_oid;
395 struct sysctl_ctx_list *ctx;
396 char sysctl_name[32];
401 sc = device_get_softc(dev);
405 /* Count the actual number of sensors. */
406 sc->sc_nsensors = ad7417_fill_sensor_prop(dev);
408 device_printf(dev, "%d sensors detected.\n", sc->sc_nsensors);
410 if (sc->sc_nsensors == 0)
411 device_printf(dev, "WARNING: No AD7417 sensors detected!\n");
413 sc->sc_sensors = malloc (sc->sc_nsensors * sizeof(struct ad7417_sensor),
414 M_AD7417, M_WAITOK | M_ZERO);
416 ctx = device_get_sysctl_ctx(dev);
417 sensroot_oid = SYSCTL_ADD_NODE(ctx,
418 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor",
419 CTLFLAG_RD, 0, "AD7417 Sensor Information");
421 /* Now we can fill the properties into the allocated struct. */
422 sc->sc_nsensors = ad7417_fill_sensor_prop(dev);
424 /* Add sysctls for the sensors. */
425 for (i = 0; i < sc->sc_nsensors; i++) {
426 for (j = 0; j < strlen(sc->sc_sensors[i].therm.name); j++) {
428 tolower(sc->sc_sensors[i].therm.name[j]);
429 if (isspace(sysctl_name[j]))
430 sysctl_name[j] = '_';
434 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
436 sysctl_name, CTLFLAG_RD, 0,
437 "Sensor Information");
439 if (sc->sc_sensors[i].type == ADC7417_TEMP_SENSOR) {
441 desc = "sensor unit (C)";
444 desc = "sensor unit (mV)";
446 /* I use i to pass the sensor id. */
447 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
448 unit, CTLTYPE_INT | CTLFLAG_RD, dev,
449 i, ad7417_sensor_sysctl,
450 sc->sc_sensors[i].type == ADC7417_TEMP_SENSOR ?
453 /* Dump sensor location, ID & type. */
455 device_printf(dev, "Sensors\n");
456 for (i = 0; i < sc->sc_nsensors; i++) {
457 device_printf(dev, "Location: %s ID: %d type: %d\n",
458 sc->sc_sensors[i].therm.name,
459 sc->sc_sensors[i].id,
460 sc->sc_sensors[i].type);
468 ad7417_get_temp(device_t dev, uint32_t addr, int *temp)
474 err = ad7417_read_2(dev, addr, AD7417_TEMP, buf);
479 read = *((int16_t*)buf);
481 /* The ADC is 10 bit, the resolution is 0.25 C.
482 The temperature is in tenth kelvin.
484 *temp = (((int16_t)(read & 0xffc0)) >> 6) * 25 / 10;
489 ad7417_get_adc(device_t dev, uint32_t addr, unsigned int *value,
494 struct write_data config;
495 struct read_data data;
498 config.reg = AD7417_CONFIG;
499 data.reg = AD7417_ADC;
502 err = ad7417_read_1(dev, addr, AD7417_CONFIG, &config.val);
504 config.val = (config.val & ~AD7417_CONFMASK) | (tmp & AD7417_CONFMASK);
506 err = ad7417_write_read(dev, addr, config, &data);
510 *value = ((uint32_t)data.val) >> 6;
516 ad7417_diode_read(struct ad7417_sensor *sens)
518 static int eeprom_read = 0;
519 static cell_t eeprom[2][40];
520 phandle_t eeprom_node;
521 int rawval, diode_slope, diode_offset;
525 eeprom_node = OF_finddevice("/u3/i2c/cpuid@a0");
526 OF_getprop(eeprom_node, "cpuid", eeprom[0], sizeof(eeprom[0]));
527 eeprom_node = OF_finddevice("/u3/i2c/cpuid@a2");
528 OF_getprop(eeprom_node, "cpuid", eeprom[1], sizeof(eeprom[1]));
532 rawval = ad7417_adc_read(sens);
536 if (strstr(sens->therm.name, "CPU B") != NULL) {
537 diode_slope = eeprom[1][0x11] >> 16;
538 diode_offset = (int16_t)(eeprom[1][0x11] & 0xffff) << 12;
540 diode_slope = eeprom[0][0x11] >> 16;
541 diode_offset = (int16_t)(eeprom[0][0x11] & 0xffff) << 12;
544 temp = (rawval*diode_slope + diode_offset) >> 2;
545 temp = (10*(temp >> 16)) + ((10*(temp & 0xffff)) >> 16);
547 return (temp + ZERO_C_TO_K);
551 ad7417_adc_read(struct ad7417_sensor *sens)
553 struct ad7417_softc *sc;
557 sc = device_get_softc(sens->dev);
580 if (ad7417_get_adc(sc->sc_dev, sc->sc_addr, &temp, chan) < 0)
588 ad7417_sensor_read(struct ad7417_sensor *sens)
590 struct ad7417_softc *sc;
593 sc = device_get_softc(sens->dev);
595 /* Init the ADC if not already done.*/
597 if (ad7417_init_adc(sc->sc_dev, sc->sc_addr) < 0)
600 if (sens->type == ADC7417_TEMP_SENSOR) {
601 if (ad7417_get_temp(sc->sc_dev, sc->sc_addr, &temp) < 0)
605 temp = ad7417_adc_read(sens);
611 ad7417_sensor_sysctl(SYSCTL_HANDLER_ARGS)
614 struct ad7417_softc *sc;
615 struct ad7417_sensor *sens;
620 sc = device_get_softc(dev);
621 sens = &sc->sc_sensors[arg2];
623 value = sens->therm.read(&sens->therm);
627 error = sysctl_handle_int(oidp, &value, 0, req);