2 * Copyright (c) 2010 Andreas Tobler.
3 * Copyright (c) 2013-2014 Luiz Otavio O Souza <loos@freebsd.org>
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 "opt_platform.h"
33 #include <sys/param.h>
35 #include <sys/endian.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/sysctl.h>
39 #include <sys/systm.h>
41 #include <machine/bus.h>
43 #include <dev/iicbus/iicbus.h>
44 #include <dev/iicbus/iiconf.h>
47 #include <dev/ofw/openfirm.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
54 #define LM75_TEMP_MASK 0xff80
55 #define LM75A_TEMP_MASK 0xffe0
57 #define LM75_CONF_FSHIFT 3
58 #define LM75_CONF_FAULT 0x18
59 #define LM75_CONF_POL 0x04
60 #define LM75_CONF_MODE 0x02
61 #define LM75_CONF_SHUTD 0x01
62 #define LM75_CONF_MASK 0x1f
63 #define LM75_THYST 0x2
67 #define LM75_TEST_PATTERN 0xa
68 #define LM75_MIN_TEMP -55
69 #define LM75_MAX_TEMP 125
70 #define LM75_0500C 0x80
71 #define LM75_0250C 0x40
72 #define LM75_0125C 0x20
73 #define LM75_MSB 0x8000
74 #define LM75_NEG_BIT LM75_MSB
77 /* LM75 supported models. */
79 #define HWTYPE_LM75A 2
81 /* Regular bus attachment functions */
82 static int lm75_probe(device_t);
83 static int lm75_attach(device_t);
87 struct intr_config_hook enum_hook;
93 /* Utility functions */
94 static int lm75_conf_read(struct lm75_softc *);
95 static int lm75_conf_write(struct lm75_softc *);
96 static int lm75_temp_read(struct lm75_softc *, uint8_t, int *);
97 static int lm75_temp_write(struct lm75_softc *, uint8_t, int);
98 static void lm75_start(void *);
99 static int lm75_read(device_t, uint32_t, uint8_t, uint8_t *, size_t);
100 static int lm75_write(device_t, uint32_t, uint8_t *, size_t);
101 static int lm75_str_mode(char *);
102 static int lm75_str_pol(char *);
103 static int lm75_temp_sysctl(SYSCTL_HANDLER_ARGS);
104 static int lm75_faults_sysctl(SYSCTL_HANDLER_ARGS);
105 static int lm75_mode_sysctl(SYSCTL_HANDLER_ARGS);
106 static int lm75_pol_sysctl(SYSCTL_HANDLER_ARGS);
107 static int lm75_shutdown_sysctl(SYSCTL_HANDLER_ARGS);
109 static device_method_t lm75_methods[] = {
110 /* Device interface */
111 DEVMETHOD(device_probe, lm75_probe),
112 DEVMETHOD(device_attach, lm75_attach),
117 static driver_t lm75_driver = {
120 sizeof(struct lm75_softc)
123 static devclass_t lm75_devclass;
125 DRIVER_MODULE(lm75, iicbus, lm75_driver, lm75_devclass, 0, 0);
128 lm75_read(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data, size_t len)
130 struct iic_msg msg[2] = {
131 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
132 { addr, IIC_M_RD, len, data },
135 if (iicbus_transfer(dev, msg, nitems(msg)) != 0)
142 lm75_write(device_t dev, uint32_t addr, uint8_t *data, size_t len)
144 struct iic_msg msg[1] = {
145 { addr, IIC_M_WR, len, data },
148 if (iicbus_transfer(dev, msg, nitems(msg)) != 0)
155 lm75_probe(device_t dev)
157 struct lm75_softc *sc;
159 sc = device_get_softc(dev);
160 sc->sc_hwtype = HWTYPE_LM75;
162 if (!ofw_bus_is_compatible(dev, "national,lm75"))
165 device_set_desc(dev, "LM75 temperature sensor");
167 return (BUS_PROBE_GENERIC);
171 lm75_attach(device_t dev)
173 struct lm75_softc *sc;
175 sc = device_get_softc(dev);
177 sc->sc_addr = iicbus_get_addr(dev);
179 sc->enum_hook.ich_func = lm75_start;
180 sc->enum_hook.ich_arg = dev;
183 * We have to wait until interrupts are enabled. Usually I2C read
184 * and write only works when the interrupts are available.
186 if (config_intrhook_establish(&sc->enum_hook) != 0)
193 lm75_type_detect(struct lm75_softc *sc)
199 /* Save the contents of the configuration register. */
200 if (lm75_conf_read(sc) != 0)
205 * Just write some pattern at configuration register so we can later
206 * verify. The test pattern should be pretty harmless.
208 sc->sc_conf = LM75_TEST_PATTERN;
209 if (lm75_conf_write(sc) != 0)
213 * Read the configuration register again and check for our test
216 if (lm75_conf_read(sc) != 0)
218 if (sc->sc_conf != LM75_TEST_PATTERN)
222 * Read from nonexistent registers (0x4 ~ 0x6).
223 * LM75A always return 0xff for nonexistent registers.
224 * LM75 will return the last read value - our test pattern written to
225 * configuration register.
228 for (i = 4; i <= 6; i++) {
229 if (lm75_read(sc->sc_dev, sc->sc_addr, i,
230 &buf8, sizeof(buf8)) < 0)
232 if (buf8 != LM75_TEST_PATTERN && buf8 != 0xff)
238 sc->sc_hwtype = HWTYPE_LM75A;
240 /* Restore the configuration register. */
242 if (lm75_conf_write(sc) != 0)
249 lm75_start(void *xdev)
252 struct lm75_softc *sc;
253 struct sysctl_ctx_list *ctx;
254 struct sysctl_oid *tree_node;
255 struct sysctl_oid_list *tree;
257 dev = (device_t)xdev;
258 sc = device_get_softc(dev);
259 ctx = device_get_sysctl_ctx(dev);
260 tree_node = device_get_sysctl_tree(dev);
261 tree = SYSCTL_CHILDREN(tree_node);
263 config_intrhook_disestablish(&sc->enum_hook);
266 * Detect the kind of chip we are attaching to.
267 * This may not work for LM75 clones.
269 if (lm75_type_detect(sc) != 0) {
270 device_printf(dev, "cannot read from sensor.\n");
273 if (sc->sc_hwtype == HWTYPE_LM75A)
275 "LM75A type sensor detected (11bits resolution).\n");
278 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "temperature",
279 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, LM75_TEMP,
280 lm75_temp_sysctl, "IK", "Current temperature");
281 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "thyst",
282 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev, LM75_THYST,
283 lm75_temp_sysctl, "IK", "Hysteresis temperature");
284 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "tos",
285 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev, LM75_TOS,
286 lm75_temp_sysctl, "IK", "Overtemperature");
288 /* Configuration parameters. */
289 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "faults",
290 CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_MPSAFE, dev, 0,
291 lm75_faults_sysctl, "IU", "LM75 fault queue");
292 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "mode",
293 CTLFLAG_RW | CTLTYPE_STRING | CTLFLAG_MPSAFE, dev, 0,
294 lm75_mode_sysctl, "A", "LM75 mode");
295 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "polarity",
296 CTLFLAG_RW | CTLTYPE_STRING | CTLFLAG_MPSAFE, dev, 0,
297 lm75_pol_sysctl, "A", "LM75 OS polarity");
298 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "shutdown",
299 CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_MPSAFE, dev, 0,
300 lm75_shutdown_sysctl, "IU", "LM75 shutdown");
304 lm75_conf_read(struct lm75_softc *sc)
308 if (lm75_read(sc->sc_dev, sc->sc_addr, LM75_CONF,
309 &buf8, sizeof(buf8)) < 0)
311 sc->sc_conf = (uint32_t)buf8;
317 lm75_conf_write(struct lm75_softc *sc)
322 buf8[1] = (uint8_t)sc->sc_conf & LM75_CONF_MASK;
323 if (lm75_write(sc->sc_dev, sc->sc_addr, buf8, sizeof(buf8)) < 0)
330 lm75_temp_read(struct lm75_softc *sc, uint8_t reg, int *temp)
336 if (lm75_read(sc->sc_dev, sc->sc_addr, reg, buf8, sizeof(buf8)) < 0)
338 buf = (uint16_t)((buf8[0] << 8) | (buf8[1] & 0xff));
340 * LM75 has a 9 bit ADC with resolution of 0.5 C per bit.
341 * LM75A has an 11 bit ADC with resolution of 0.125 C per bit.
342 * Temperature is stored with two's complement.
345 if (buf & LM75_NEG_BIT) {
346 if (sc->sc_hwtype == HWTYPE_LM75A)
347 buf = ~(buf & LM75A_TEMP_MASK) + 1;
349 buf = ~(buf & LM75_TEMP_MASK) + 1;
352 *temp = ((int16_t)buf >> 8) * 10;
354 if (sc->sc_hwtype == HWTYPE_LM75A) {
355 if (buf & LM75_0125C)
357 if (buf & LM75_0250C)
360 if (buf & LM75_0500C)
372 lm75_temp_write(struct lm75_softc *sc, uint8_t reg, int temp)
377 temp = (temp - TZ_ZEROC) / 10;
378 if (temp > LM75_MAX_TEMP)
379 temp = LM75_MAX_TEMP;
380 if (temp < LM75_MIN_TEMP)
381 temp = LM75_MIN_TEMP;
383 buf = (uint16_t)temp;
388 buf8[2] = buf & 0xff;
389 if (lm75_write(sc->sc_dev, sc->sc_addr, buf8, sizeof(buf8)) < 0)
396 lm75_str_mode(char *buf)
402 if (len > 2 && strncasecmp("interrupt", buf, len) == 0)
404 else if (len > 2 && strncasecmp("comparator", buf, len) == 0)
411 lm75_str_pol(char *buf)
417 if (len > 1 && strncasecmp("high", buf, len) == 0)
419 else if (len > 1 && strncasecmp("low", buf, len) == 0)
421 else if (len > 8 && strncasecmp("active-high", buf, len) == 0)
423 else if (len > 8 && strncasecmp("active-low", buf, len) == 0)
430 lm75_temp_sysctl(SYSCTL_HANDLER_ARGS)
434 struct lm75_softc *sc;
437 dev = (device_t)arg1;
439 sc = device_get_softc(dev);
441 if (lm75_temp_read(sc, reg, &temp) != 0)
444 error = sysctl_handle_int(oidp, &temp, 0, req);
445 if (error != 0 || req->newptr == NULL)
448 if (lm75_temp_write(sc, reg, temp) != 0)
455 lm75_faults_sysctl(SYSCTL_HANDLER_ARGS)
458 int lm75_faults[] = { 1, 2, 4, 6 };
459 int error, faults, i, newf, tmp;
460 struct lm75_softc *sc;
462 dev = (device_t)arg1;
463 sc = device_get_softc(dev);
464 tmp = (sc->sc_conf & LM75_CONF_FAULT) >> LM75_CONF_FSHIFT;
465 if (tmp >= nitems(lm75_faults))
466 tmp = nitems(lm75_faults) - 1;
467 faults = lm75_faults[tmp];
469 error = sysctl_handle_int(oidp, &faults, 0, req);
470 if (error != 0 || req->newptr == NULL)
473 if (faults != lm75_faults[tmp]) {
475 for (i = 0; i < nitems(lm75_faults); i++)
476 if (faults >= lm75_faults[i])
478 sc->sc_conf &= ~LM75_CONF_FAULT;
479 sc->sc_conf |= newf << LM75_CONF_FSHIFT;
480 if (lm75_conf_write(sc) != 0)
488 lm75_mode_sysctl(SYSCTL_HANDLER_ARGS)
492 int error, mode, newm;
493 struct lm75_softc *sc;
495 dev = (device_t)arg1;
496 sc = device_get_softc(dev);
497 if (sc->sc_conf & LM75_CONF_MODE) {
499 strlcpy(buf, "interrupt", sizeof(buf));
502 strlcpy(buf, "comparator", sizeof(buf));
505 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
506 if (error != 0 || req->newptr == NULL)
509 newm = lm75_str_mode(buf);
510 if (newm != -1 && mode != newm) {
511 sc->sc_conf &= ~LM75_CONF_MODE;
513 sc->sc_conf |= LM75_CONF_MODE;
514 if (lm75_conf_write(sc) != 0)
522 lm75_pol_sysctl(SYSCTL_HANDLER_ARGS)
526 int error, newp, pol;
527 struct lm75_softc *sc;
529 dev = (device_t)arg1;
530 sc = device_get_softc(dev);
531 if (sc->sc_conf & LM75_CONF_POL) {
533 strlcpy(buf, "active-high", sizeof(buf));
536 strlcpy(buf, "active-low", sizeof(buf));
539 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
540 if (error != 0 || req->newptr == NULL)
543 newp = lm75_str_pol(buf);
544 if (newp != -1 && pol != newp) {
545 sc->sc_conf &= ~LM75_CONF_POL;
547 sc->sc_conf |= LM75_CONF_POL;
548 if (lm75_conf_write(sc) != 0)
556 lm75_shutdown_sysctl(SYSCTL_HANDLER_ARGS)
559 int error, shutdown, tmp;
560 struct lm75_softc *sc;
562 dev = (device_t)arg1;
563 sc = device_get_softc(dev);
564 tmp = shutdown = (sc->sc_conf & LM75_CONF_SHUTD) ? 1 : 0;
566 error = sysctl_handle_int(oidp, &shutdown, 0, req);
567 if (error != 0 || req->newptr == NULL)
570 if (shutdown != tmp) {
571 sc->sc_conf &= ~LM75_CONF_SHUTD;
573 sc->sc_conf |= LM75_CONF_SHUTD;
574 if (lm75_conf_write(sc) != 0)