2 * Copyright (c) 2012 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 /* Sensor: Maxim DS1631 */
56 #define DS1631_STOP 0x22
57 #define DS1631_START 0x51
58 #define DS1631_RESET 0x54
59 #define DS1631_TEMP 0xAA
60 #define DS1631_CONTROL 0xAC
61 #define DS1631_CONTROL_1SHOT 0x01
62 #define DS1631_CONTROL_9BIT 0x00
63 #define DS1631_CONTROL_10BIT 0x04
64 #define DS1631_CONTROL_11BIT 0x08
65 #define DS1631_CONTROL_12BIT 0x0C
69 /* Regular bus attachment functions */
70 static int ds1631_probe(device_t);
71 static int ds1631_attach(device_t);
74 struct pmac_therm sc_sensor;
76 struct intr_config_hook enum_hook;
91 /* Utility functions */
92 static int ds1631_sensor_read(struct ds1631_softc *sc);
93 static int ds1631_sensor_sysctl(SYSCTL_HANDLER_ARGS);
94 static void ds1631_start(void *xdev);
95 static int ds1631_read_1(device_t dev, uint32_t addr, uint8_t reg,
97 static int ds1631_read_2(device_t dev, uint32_t addr, uint8_t reg,
99 static int ds1631_write(device_t dev, uint32_t addr, uint8_t reg,
100 uint8_t *buff, int len);
102 static device_method_t ds1631_methods[] = {
103 /* Device interface */
104 DEVMETHOD(device_probe, ds1631_probe),
105 DEVMETHOD(device_attach, ds1631_attach),
109 static driver_t ds1631_driver = {
112 sizeof(struct ds1631_softc)
115 static devclass_t ds1631_devclass;
117 DRIVER_MODULE(ds1631, iicbus, ds1631_driver, ds1631_devclass, 0, 0);
120 ds1631_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buff, int len)
125 struct iic_msg msg[] = {
126 { addr, IIC_M_WR, 0, buf }
129 /* Prepare the write msg. */
130 msg[0].len = len + 1;
132 memcpy(buf + 1, buff, len);
136 if (iicbus_transfer(dev, msg, 1) == 0)
139 device_printf(dev, "iicbus write failed\n");
142 pause("ds1631_write", hz);
147 ds1631_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
152 struct iic_msg msg[2] = {
153 { addr, IIC_M_WR, 1, ® },
154 { addr, IIC_M_RD, 1, buf },
159 err = iicbus_transfer(dev, msg, 2);
163 *data = *((uint8_t*)buf);
167 device_printf(dev, "iicbus read failed\n");
170 pause("ds1631_read_1", hz);
175 ds1631_read_2(device_t dev, uint32_t addr, uint8_t reg, uint16_t *data)
180 struct iic_msg msg[2] = {
181 { addr, IIC_M_WR, 1, ® },
182 { addr, IIC_M_RD, 2, buf },
187 err = iicbus_transfer(dev, msg, 2);
191 *data = *((uint16_t*)buf);
195 device_printf(dev, "iicbus read failed\n");
198 pause("ds1631_read_2", hz);
203 ds1631_probe(device_t dev)
205 const char *name, *compatible;
206 struct ds1631_softc *sc;
208 name = ofw_bus_get_name(dev);
209 compatible = ofw_bus_get_compat(dev);
214 if (strcmp(name, "temp-monitor") != 0 ||
215 strcmp(compatible, "ds1631") != 0 )
218 sc = device_get_softc(dev);
220 sc->sc_addr = iicbus_get_addr(dev);
222 device_set_desc(dev, "Temp-Monitor DS1631");
228 ds1631_attach(device_t dev)
230 struct ds1631_softc *sc;
232 sc = device_get_softc(dev);
234 sc->enum_hook.ich_func = ds1631_start;
235 sc->enum_hook.ich_arg = dev;
238 * We have to wait until interrupts are enabled. I2C read and write
239 * only works if the interrupts are available.
240 * The unin/i2c is controlled by the htpic on unin. But this is not
241 * the master. The openpic on mac-io is controlling the htpic.
242 * This one gets attached after the mac-io probing and then the
243 * interrupts will be available.
246 if (config_intrhook_establish(&sc->enum_hook) != 0)
252 ds1631_init(device_t dev, uint32_t addr)
256 struct ds1631_softc *sc;
258 sc = device_get_softc(dev);
260 err = ds1631_read_1(dev, addr, DS1631_CONTROL, &conf);
262 device_printf(dev, "ds1631 read config failed: %x\n", err);
266 /* Stop the conversion if not in 1SHOT mode. */
267 if (conf & ~DS1631_CONTROL_1SHOT)
268 err = ds1631_write(dev, addr, DS1631_STOP, &conf, 0);
271 * Setup the resolution, 10-bit is enough. Each bit increase in
272 * resolution doubles the conversion time.
274 conf = DS1631_CONTROL_10BIT;
276 err = ds1631_write(dev, addr, DS1631_CONTROL, &conf, 1);
278 device_printf(dev, "ds1631 write config failed: %x\n", err);
282 /* And now start....*/
283 err = ds1631_write(dev, addr, DS1631_START, &conf, 0);
286 device_printf(dev, "ds1631 write start failed: %x\n", err);
296 ds1631_start(void *xdev)
298 phandle_t child, node;
299 struct ds1631_softc *sc;
300 struct sysctl_oid *oid, *sensroot_oid;
301 struct sysctl_ctx_list *ctx;
304 char sysctl_desc[40], sysctl_name[40];
306 device_t dev = (device_t)xdev;
308 sc = device_get_softc(dev);
310 child = ofw_bus_get_node(dev);
312 ctx = device_get_sysctl_ctx(dev);
313 sensroot_oid = SYSCTL_ADD_NODE(ctx,
314 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor",
315 CTLFLAG_RD, 0, "DS1631 Sensor Information");
317 if (OF_getprop(child, "hwsensor-zone", &sc->sc_sensor.zone,
319 sc->sc_sensor.zone = 0;
321 plen = OF_getprop(child, "hwsensor-location", sc->sc_sensor.name,
322 sizeof(sc->sc_sensor.name));
325 * Ok, no hwsensor-location property, so let's look for a
326 * location property on a sub node.
328 for (node = OF_child(child); node; node = OF_peer(node))
329 plen = OF_getprop(node, "location", sc->sc_sensor.name,
330 sizeof(sc->sc_sensor.name));
334 strcpy(sysctl_name, "sensor");
336 for (i = 0; i < strlen(sc->sc_sensor.name); i++) {
337 sysctl_name[i] = tolower(sc->sc_sensor.name[i]);
338 if (isspace(sysctl_name[i]))
339 sysctl_name[i] = '_';
344 /* Make up target temperatures. These are low, for the drive bay. */
345 if (sc->sc_sensor.zone == 0) {
346 sc->sc_sensor.target_temp = 400 + ZERO_C_TO_K;
347 sc->sc_sensor.max_temp = 500 + ZERO_C_TO_K;
349 sc->sc_sensor.target_temp = 300 + ZERO_C_TO_K;
350 sc->sc_sensor.max_temp = 500 + ZERO_C_TO_K;
354 (int (*)(struct pmac_therm *sc))(ds1631_sensor_read);
355 pmac_thermal_sensor_register(&sc->sc_sensor);
357 sprintf(sysctl_desc,"%s %s", sc->sc_sensor.name, "(C)");
358 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
359 OID_AUTO, sysctl_name, CTLFLAG_RD, 0,
360 "Sensor Information");
361 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "temp",
362 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev,
363 0, ds1631_sensor_sysctl, "IK", sysctl_desc);
365 config_intrhook_disestablish(&sc->enum_hook);
369 ds1631_sensor_read(struct ds1631_softc *sc)
376 ds1631_init(sc->sc_dev, sc->sc_addr);
378 err = ds1631_read_2(sc->sc_dev, sc->sc_addr, DS1631_TEMP, buf);
380 device_printf(sc->sc_dev, "ds1631 read TEMP failed: %x\n", err);
384 read = *((int16_t *)buf);
387 * The default mode of the ADC is 12-bit, the resolution is 0.0625 C
388 * per bit. The temperature is in tenth kelvin.
389 * We use 10-bit resolution which seems enough, resolution is 0.25 C.
392 return (((int16_t)(read) >> 6) * 25 / 10 + ZERO_C_TO_K);
396 ds1631_sensor_sysctl(SYSCTL_HANDLER_ARGS)
399 struct ds1631_softc *sc;
404 sc = device_get_softc(dev);
406 temp = ds1631_sensor_read(sc);
410 error = sysctl_handle_int(oidp, &temp, 0, req);