2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Andreas Tobler
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/module.h>
35 #include <sys/callout.h>
38 #include <sys/ctype.h>
39 #include <sys/kernel.h>
40 #include <sys/reboot.h>
42 #include <sys/sysctl.h>
43 #include <sys/limits.h>
45 #include <machine/bus.h>
46 #include <machine/md_var.h>
48 #include <dev/iicbus/iicbus.h>
49 #include <dev/iicbus/iiconf.h>
51 #include <dev/ofw/openfirm.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <powerpc/powermac/powermac_thermal.h>
55 /* Sensor: Maxim DS1631 */
57 #define DS1631_STOP 0x22
58 #define DS1631_START 0x51
59 #define DS1631_RESET 0x54
60 #define DS1631_TEMP 0xAA
61 #define DS1631_CONTROL 0xAC
62 #define DS1631_CONTROL_1SHOT 0x01
63 #define DS1631_CONTROL_9BIT 0x00
64 #define DS1631_CONTROL_10BIT 0x04
65 #define DS1631_CONTROL_11BIT 0x08
66 #define DS1631_CONTROL_12BIT 0x0C
70 /* Regular bus attachment functions */
71 static int ds1631_probe(device_t);
72 static int ds1631_attach(device_t);
75 struct pmac_therm sc_sensor;
77 struct intr_config_hook enum_hook;
92 /* Utility functions */
93 static int ds1631_sensor_read(struct ds1631_softc *sc);
94 static int ds1631_sensor_sysctl(SYSCTL_HANDLER_ARGS);
95 static void ds1631_start(void *xdev);
96 static int ds1631_read_1(device_t dev, uint32_t addr, uint8_t reg,
98 static int ds1631_read_2(device_t dev, uint32_t addr, uint8_t reg,
100 static int ds1631_write(device_t dev, uint32_t addr, uint8_t reg,
101 uint8_t *buff, int len);
103 static device_method_t ds1631_methods[] = {
104 /* Device interface */
105 DEVMETHOD(device_probe, ds1631_probe),
106 DEVMETHOD(device_attach, ds1631_attach),
110 static driver_t ds1631_driver = {
113 sizeof(struct ds1631_softc)
116 static devclass_t ds1631_devclass;
118 DRIVER_MODULE(ds1631, iicbus, ds1631_driver, ds1631_devclass, 0, 0);
121 ds1631_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buff, int len)
126 struct iic_msg msg[] = {
127 { addr, IIC_M_WR, 0, buf }
130 /* Prepare the write msg. */
131 msg[0].len = len + 1;
133 memcpy(buf + 1, buff, len);
137 if (iicbus_transfer(dev, msg, nitems(msg)) == 0)
140 device_printf(dev, "iicbus write failed\n");
143 pause("ds1631_write", hz);
148 ds1631_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
153 struct iic_msg msg[2] = {
154 { addr, IIC_M_WR, 1, ® },
155 { addr, IIC_M_RD, 1, buf },
160 err = iicbus_transfer(dev, msg, nitems(msg));
164 *data = *((uint8_t*)buf);
168 device_printf(dev, "iicbus read failed\n");
171 pause("ds1631_read_1", hz);
176 ds1631_read_2(device_t dev, uint32_t addr, uint8_t reg, uint16_t *data)
181 struct iic_msg msg[2] = {
182 { addr, IIC_M_WR, 1, ® },
183 { addr, IIC_M_RD, 2, buf },
188 err = iicbus_transfer(dev, msg, nitems(msg));
192 *data = *((uint16_t*)buf);
196 device_printf(dev, "iicbus read failed\n");
199 pause("ds1631_read_2", hz);
204 ds1631_probe(device_t dev)
206 const char *name, *compatible;
207 struct ds1631_softc *sc;
209 name = ofw_bus_get_name(dev);
210 compatible = ofw_bus_get_compat(dev);
215 if (strcmp(name, "temp-monitor") != 0 ||
216 strcmp(compatible, "ds1631") != 0 )
219 sc = device_get_softc(dev);
221 sc->sc_addr = iicbus_get_addr(dev);
223 device_set_desc(dev, "Temp-Monitor DS1631");
229 ds1631_attach(device_t dev)
231 struct ds1631_softc *sc;
233 sc = device_get_softc(dev);
235 sc->enum_hook.ich_func = ds1631_start;
236 sc->enum_hook.ich_arg = dev;
239 * We have to wait until interrupts are enabled. I2C read and write
240 * only works if the interrupts are available.
241 * The unin/i2c is controlled by the htpic on unin. But this is not
242 * the master. The openpic on mac-io is controlling the htpic.
243 * This one gets attached after the mac-io probing and then the
244 * interrupts will be available.
247 if (config_intrhook_establish(&sc->enum_hook) != 0)
253 ds1631_init(device_t dev, uint32_t addr)
257 struct ds1631_softc *sc;
259 sc = device_get_softc(dev);
261 err = ds1631_read_1(dev, addr, DS1631_CONTROL, &conf);
263 device_printf(dev, "ds1631 read config failed: %x\n", err);
267 /* Stop the conversion if not in 1SHOT mode. */
268 if (conf & ~DS1631_CONTROL_1SHOT)
269 err = ds1631_write(dev, addr, DS1631_STOP, &conf, 0);
272 * Setup the resolution, 10-bit is enough. Each bit increase in
273 * resolution doubles the conversion time.
275 conf = DS1631_CONTROL_10BIT;
277 err = ds1631_write(dev, addr, DS1631_CONTROL, &conf, sizeof(conf));
279 device_printf(dev, "ds1631 write config failed: %x\n", err);
283 /* And now start....*/
284 err = ds1631_write(dev, addr, DS1631_START, &conf, 0);
287 device_printf(dev, "ds1631 write start failed: %x\n", err);
297 ds1631_start(void *xdev)
299 phandle_t child, node;
300 struct ds1631_softc *sc;
301 struct sysctl_oid *oid, *sensroot_oid;
302 struct sysctl_ctx_list *ctx;
305 char sysctl_desc[40], sysctl_name[40];
307 device_t dev = (device_t)xdev;
309 sc = device_get_softc(dev);
311 child = ofw_bus_get_node(dev);
313 ctx = device_get_sysctl_ctx(dev);
314 sensroot_oid = SYSCTL_ADD_NODE(ctx,
315 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor",
316 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "DS1631 Sensor Information");
318 if (OF_getprop(child, "hwsensor-zone", &sc->sc_sensor.zone,
320 sc->sc_sensor.zone = 0;
322 plen = OF_getprop(child, "hwsensor-location", sc->sc_sensor.name,
323 sizeof(sc->sc_sensor.name));
326 * Ok, no hwsensor-location property, so let's look for a
327 * location property on a sub node.
329 for (node = OF_child(child); node; node = OF_peer(node))
330 plen = OF_getprop(node, "location", sc->sc_sensor.name,
331 sizeof(sc->sc_sensor.name));
335 strcpy(sysctl_name, "sensor");
337 for (i = 0; i < strlen(sc->sc_sensor.name); i++) {
338 sysctl_name[i] = tolower(sc->sc_sensor.name[i]);
339 if (isspace(sysctl_name[i]))
340 sysctl_name[i] = '_';
345 /* Make up target temperatures. These are low, for the drive bay. */
346 if (sc->sc_sensor.zone == 0) {
347 sc->sc_sensor.target_temp = 400 + ZERO_C_TO_K;
348 sc->sc_sensor.max_temp = 500 + ZERO_C_TO_K;
350 sc->sc_sensor.target_temp = 300 + ZERO_C_TO_K;
351 sc->sc_sensor.max_temp = 500 + ZERO_C_TO_K;
355 (int (*)(struct pmac_therm *sc))(ds1631_sensor_read);
356 pmac_thermal_sensor_register(&sc->sc_sensor);
358 sprintf(sysctl_desc,"%s %s", sc->sc_sensor.name, "(C)");
359 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
360 OID_AUTO, sysctl_name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
361 "Sensor Information");
362 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "temp",
363 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev,
364 0, ds1631_sensor_sysctl, "IK", sysctl_desc);
366 config_intrhook_disestablish(&sc->enum_hook);
370 ds1631_sensor_read(struct ds1631_softc *sc)
377 ds1631_init(sc->sc_dev, sc->sc_addr);
379 err = ds1631_read_2(sc->sc_dev, sc->sc_addr, DS1631_TEMP, buf);
381 device_printf(sc->sc_dev, "ds1631 read TEMP failed: %x\n", err);
385 read = *((int16_t *)buf);
388 * The default mode of the ADC is 12-bit, the resolution is 0.0625 C
389 * per bit. The temperature is in tenth kelvin.
390 * We use 10-bit resolution which seems enough, resolution is 0.25 C.
393 return (((int16_t)(read) >> 6) * 25 / 10 + ZERO_C_TO_K);
397 ds1631_sensor_sysctl(SYSCTL_HANDLER_ARGS)
400 struct ds1631_softc *sc;
405 sc = device_get_softc(dev);
407 temp = ds1631_sensor_read(sc);
411 error = sysctl_handle_int(oidp, &temp, 0, req);