2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 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 /* Sensor: Maxim DS1631 */
58 #define DS1631_STOP 0x22
59 #define DS1631_START 0x51
60 #define DS1631_RESET 0x54
61 #define DS1631_TEMP 0xAA
62 #define DS1631_CONTROL 0xAC
63 #define DS1631_CONTROL_1SHOT 0x01
64 #define DS1631_CONTROL_9BIT 0x00
65 #define DS1631_CONTROL_10BIT 0x04
66 #define DS1631_CONTROL_11BIT 0x08
67 #define DS1631_CONTROL_12BIT 0x0C
71 /* Regular bus attachment functions */
72 static int ds1631_probe(device_t);
73 static int ds1631_attach(device_t);
76 struct pmac_therm sc_sensor;
78 struct intr_config_hook enum_hook;
93 /* Utility functions */
94 static int ds1631_sensor_read(struct ds1631_softc *sc);
95 static int ds1631_sensor_sysctl(SYSCTL_HANDLER_ARGS);
96 static void ds1631_start(void *xdev);
97 static int ds1631_read_1(device_t dev, uint32_t addr, uint8_t reg,
99 static int ds1631_read_2(device_t dev, uint32_t addr, uint8_t reg,
101 static int ds1631_write(device_t dev, uint32_t addr, uint8_t reg,
102 uint8_t *buff, int len);
104 static device_method_t ds1631_methods[] = {
105 /* Device interface */
106 DEVMETHOD(device_probe, ds1631_probe),
107 DEVMETHOD(device_attach, ds1631_attach),
111 static driver_t ds1631_driver = {
114 sizeof(struct ds1631_softc)
117 static devclass_t ds1631_devclass;
119 DRIVER_MODULE(ds1631, iicbus, ds1631_driver, ds1631_devclass, 0, 0);
122 ds1631_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buff, int len)
127 struct iic_msg msg[] = {
128 { addr, IIC_M_WR, 0, buf }
131 /* Prepare the write msg. */
132 msg[0].len = len + 1;
134 memcpy(buf + 1, buff, len);
138 if (iicbus_transfer(dev, msg, 1) == 0)
141 device_printf(dev, "iicbus write failed\n");
144 pause("ds1631_write", hz);
149 ds1631_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
154 struct iic_msg msg[2] = {
155 { addr, IIC_M_WR, 1, ® },
156 { addr, IIC_M_RD, 1, buf },
161 err = iicbus_transfer(dev, msg, 2);
165 *data = *((uint8_t*)buf);
169 device_printf(dev, "iicbus read failed\n");
172 pause("ds1631_read_1", hz);
177 ds1631_read_2(device_t dev, uint32_t addr, uint8_t reg, uint16_t *data)
182 struct iic_msg msg[2] = {
183 { addr, IIC_M_WR, 1, ® },
184 { addr, IIC_M_RD, 2, buf },
189 err = iicbus_transfer(dev, msg, 2);
193 *data = *((uint16_t*)buf);
197 device_printf(dev, "iicbus read failed\n");
200 pause("ds1631_read_2", hz);
205 ds1631_probe(device_t dev)
207 const char *name, *compatible;
208 struct ds1631_softc *sc;
210 name = ofw_bus_get_name(dev);
211 compatible = ofw_bus_get_compat(dev);
216 if (strcmp(name, "temp-monitor") != 0 ||
217 strcmp(compatible, "ds1631") != 0 )
220 sc = device_get_softc(dev);
222 sc->sc_addr = iicbus_get_addr(dev);
224 device_set_desc(dev, "Temp-Monitor DS1631");
230 ds1631_attach(device_t dev)
232 struct ds1631_softc *sc;
234 sc = device_get_softc(dev);
236 sc->enum_hook.ich_func = ds1631_start;
237 sc->enum_hook.ich_arg = dev;
240 * We have to wait until interrupts are enabled. I2C read and write
241 * only works if the interrupts are available.
242 * The unin/i2c is controlled by the htpic on unin. But this is not
243 * the master. The openpic on mac-io is controlling the htpic.
244 * This one gets attached after the mac-io probing and then the
245 * interrupts will be available.
248 if (config_intrhook_establish(&sc->enum_hook) != 0)
254 ds1631_init(device_t dev, uint32_t addr)
258 struct ds1631_softc *sc;
260 sc = device_get_softc(dev);
262 err = ds1631_read_1(dev, addr, DS1631_CONTROL, &conf);
264 device_printf(dev, "ds1631 read config failed: %x\n", err);
268 /* Stop the conversion if not in 1SHOT mode. */
269 if (conf & ~DS1631_CONTROL_1SHOT)
270 err = ds1631_write(dev, addr, DS1631_STOP, &conf, 0);
273 * Setup the resolution, 10-bit is enough. Each bit increase in
274 * resolution doubles the conversion time.
276 conf = DS1631_CONTROL_10BIT;
278 err = ds1631_write(dev, addr, DS1631_CONTROL, &conf, 1);
280 device_printf(dev, "ds1631 write config failed: %x\n", err);
284 /* And now start....*/
285 err = ds1631_write(dev, addr, DS1631_START, &conf, 0);
288 device_printf(dev, "ds1631 write start failed: %x\n", err);
298 ds1631_start(void *xdev)
300 phandle_t child, node;
301 struct ds1631_softc *sc;
302 struct sysctl_oid *oid, *sensroot_oid;
303 struct sysctl_ctx_list *ctx;
306 char sysctl_desc[40], sysctl_name[40];
308 device_t dev = (device_t)xdev;
310 sc = device_get_softc(dev);
312 child = ofw_bus_get_node(dev);
314 ctx = device_get_sysctl_ctx(dev);
315 sensroot_oid = SYSCTL_ADD_NODE(ctx,
316 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor",
317 CTLFLAG_RD, 0, "DS1631 Sensor Information");
319 if (OF_getprop(child, "hwsensor-zone", &sc->sc_sensor.zone,
321 sc->sc_sensor.zone = 0;
323 plen = OF_getprop(child, "hwsensor-location", sc->sc_sensor.name,
324 sizeof(sc->sc_sensor.name));
327 * Ok, no hwsensor-location property, so let's look for a
328 * location property on a sub node.
330 for (node = OF_child(child); node; node = OF_peer(node))
331 plen = OF_getprop(node, "location", sc->sc_sensor.name,
332 sizeof(sc->sc_sensor.name));
336 strcpy(sysctl_name, "sensor");
338 for (i = 0; i < strlen(sc->sc_sensor.name); i++) {
339 sysctl_name[i] = tolower(sc->sc_sensor.name[i]);
340 if (isspace(sysctl_name[i]))
341 sysctl_name[i] = '_';
346 /* Make up target temperatures. These are low, for the drive bay. */
347 if (sc->sc_sensor.zone == 0) {
348 sc->sc_sensor.target_temp = 400 + ZERO_C_TO_K;
349 sc->sc_sensor.max_temp = 500 + ZERO_C_TO_K;
351 sc->sc_sensor.target_temp = 300 + ZERO_C_TO_K;
352 sc->sc_sensor.max_temp = 500 + ZERO_C_TO_K;
356 (int (*)(struct pmac_therm *sc))(ds1631_sensor_read);
357 pmac_thermal_sensor_register(&sc->sc_sensor);
359 sprintf(sysctl_desc,"%s %s", sc->sc_sensor.name, "(C)");
360 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
361 OID_AUTO, sysctl_name, CTLFLAG_RD, 0,
362 "Sensor Information");
363 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "temp",
364 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev,
365 0, ds1631_sensor_sysctl, "IK", sysctl_desc);
367 config_intrhook_disestablish(&sc->enum_hook);
371 ds1631_sensor_read(struct ds1631_softc *sc)
378 ds1631_init(sc->sc_dev, sc->sc_addr);
380 err = ds1631_read_2(sc->sc_dev, sc->sc_addr, DS1631_TEMP, buf);
382 device_printf(sc->sc_dev, "ds1631 read TEMP failed: %x\n", err);
386 read = *((int16_t *)buf);
389 * The default mode of the ADC is 12-bit, the resolution is 0.0625 C
390 * per bit. The temperature is in tenth kelvin.
391 * We use 10-bit resolution which seems enough, resolution is 0.25 C.
394 return (((int16_t)(read) >> 6) * 25 / 10 + ZERO_C_TO_K);
398 ds1631_sensor_sysctl(SYSCTL_HANDLER_ARGS)
401 struct ds1631_softc *sc;
406 sc = device_get_softc(dev);
408 temp = ds1631_sensor_read(sc);
412 error = sysctl_handle_int(oidp, &temp, 0, req);