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>
55 #define LM75_CONF_FSHIFT 3
56 #define LM75_CONF_FAULT 0x18
57 #define LM75_CONF_POL 0x04
58 #define LM75_CONF_MODE 0x02
59 #define LM75_CONF_SHUTD 0x01
60 #define LM75_CONF_MASK 0x1f
61 #define LM75_THYST 0x2
65 #define LM75_TEST_PATTERN 0xa
66 #define LM75_MIN_TEMP -55
67 #define LM75_MAX_TEMP 125
68 #define LM75_0500C 0x80
69 #define LM75_0250C 0x40
70 #define LM75_0125C 0x20
71 #define LM75_MSB 0x8000
72 #define LM75_NEG_BIT LM75_MSB
75 /* LM75 supported models. */
77 #define HWTYPE_LM75A 2
79 /* Regular bus attachment functions */
80 static int lm75_probe(device_t);
81 static int lm75_attach(device_t);
85 struct intr_config_hook enum_hook;
91 static int lm75_faults[4] = { 1, 2, 4, 6 };
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, 2) != 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, 1) != 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, &buf8, 1) < 0)
231 if (buf8 != LM75_TEST_PATTERN && buf8 != 0xff)
237 sc->sc_hwtype = HWTYPE_LM75A;
239 /* Restore the configuration register. */
241 if (lm75_conf_write(sc) != 0)
248 lm75_start(void *xdev)
251 struct lm75_softc *sc;
252 struct sysctl_ctx_list *ctx;
253 struct sysctl_oid *tree_node;
254 struct sysctl_oid_list *tree;
256 dev = (device_t)xdev;
257 sc = device_get_softc(dev);
258 ctx = device_get_sysctl_ctx(dev);
259 tree_node = device_get_sysctl_tree(dev);
260 tree = SYSCTL_CHILDREN(tree_node);
262 config_intrhook_disestablish(&sc->enum_hook);
265 * Detect the kind of chip we are attaching to.
266 * This may not work for LM75 clones.
268 if (lm75_type_detect(sc) != 0) {
269 device_printf(dev, "cannot read from sensor.\n");
272 if (sc->sc_hwtype == HWTYPE_LM75A)
274 "LM75A type sensor detected (11bits resolution).\n");
277 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "temperature",
278 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, LM75_TEMP,
279 lm75_temp_sysctl, "IK", "Current temperature");
280 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "thyst",
281 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev, LM75_THYST,
282 lm75_temp_sysctl, "IK", "Hysteresis temperature");
283 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "tos",
284 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev, LM75_TOS,
285 lm75_temp_sysctl, "IK", "Overtemperature");
287 /* Configuration parameters. */
288 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "faults",
289 CTLFLAG_RW | CTLTYPE_UINT, dev, 0,
290 lm75_faults_sysctl, "IU", "LM75 fault queue");
291 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "mode",
292 CTLFLAG_RW | CTLTYPE_STRING, dev, 0,
293 lm75_mode_sysctl, "A", "LM75 mode");
294 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "polarity",
295 CTLFLAG_RW | CTLTYPE_STRING, dev, 0,
296 lm75_pol_sysctl, "A", "LM75 OS polarity");
297 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "shutdown",
298 CTLFLAG_RW | CTLTYPE_UINT, dev, 0,
299 lm75_shutdown_sysctl, "IU", "LM75 shutdown");
303 lm75_conf_read(struct lm75_softc *sc)
307 if (lm75_read(sc->sc_dev, sc->sc_addr, LM75_CONF, &buf8, 1) < 0)
310 sc->sc_conf = (uint32_t)buf8;
316 lm75_conf_write(struct lm75_softc *sc)
321 buf8[1] = (uint8_t)sc->sc_conf & LM75_CONF_MASK;
323 if (lm75_write(sc->sc_dev, sc->sc_addr, buf8, 2) < 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, 2) < 0)
339 buf = (buf8[0] << 8) | (buf8[1] & 0xff);
342 * LM75 has a 9 bit ADC with resolution of 0.5 C per bit.
343 * LM75A has an 11 bit ADC with resolution of 0.125 C per bit.
344 * Temperature is stored with two's complement.
346 if (buf & LM75_NEG_BIT)
348 *temp = ((int16_t)buf >> 8) * 10;
350 if (sc->sc_hwtype == HWTYPE_LM75A) {
351 if (buf & LM75_0125C)
353 if (buf & LM75_0250C)
356 if (buf & LM75_0500C)
360 if (buf & LM75_NEG_BIT)
368 lm75_temp_write(struct lm75_softc *sc, uint8_t reg, int temp)
373 if (temp > LM75_MAX_TEMP)
374 temp = LM75_MAX_TEMP;
375 if (temp < LM75_MIN_TEMP)
376 temp = LM75_MIN_TEMP;
378 buf = (uint16_t)temp;
383 buf8[2] = buf & 0xff;
385 if (lm75_write(sc->sc_dev, sc->sc_addr, buf8, 3) < 0)
392 lm75_str_mode(char *buf)
398 if (len > 2 && strncasecmp("interrupt", buf, len) == 0)
400 else if (len > 2 && strncasecmp("comparator", buf, len) == 0)
407 lm75_str_pol(char *buf)
413 if (len > 1 && strncasecmp("high", buf, len) == 0)
415 else if (len > 1 && strncasecmp("low", buf, len) == 0)
417 else if (len > 8 && strncasecmp("active-high", buf, len) == 0)
419 else if (len > 8 && strncasecmp("active-low", buf, len) == 0)
426 lm75_temp_sysctl(SYSCTL_HANDLER_ARGS)
430 struct lm75_softc *sc;
433 dev = (device_t)arg1;
435 sc = device_get_softc(dev);
437 if (lm75_temp_read(sc, reg, &temp) != 0)
440 error = sysctl_handle_int(oidp, &temp, 0, req);
441 if (error != 0 || req->newptr == NULL)
444 if (lm75_temp_write(sc, reg, temp) != 0)
451 lm75_faults_sysctl(SYSCTL_HANDLER_ARGS)
454 int error, faults, i, newf, tmp;
455 struct lm75_softc *sc;
457 dev = (device_t)arg1;
458 sc = device_get_softc(dev);
459 tmp = (sc->sc_conf & LM75_CONF_FAULT) >> LM75_CONF_FSHIFT;
460 if (tmp > nitems(lm75_faults))
461 tmp = nitems(lm75_faults);
462 faults = lm75_faults[tmp];
464 error = sysctl_handle_int(oidp, &faults, 0, req);
465 if (error != 0 || req->newptr == NULL)
468 if (faults != lm75_faults[tmp]) {
470 for (i = 0; i < nitems(lm75_faults); i++)
471 if (faults >= lm75_faults[i])
473 sc->sc_conf &= ~LM75_CONF_FAULT;
474 sc->sc_conf |= newf << LM75_CONF_FSHIFT;
475 if (lm75_conf_write(sc) != 0)
483 lm75_mode_sysctl(SYSCTL_HANDLER_ARGS)
487 int error, mode, newm;
488 struct lm75_softc *sc;
490 dev = (device_t)arg1;
491 sc = device_get_softc(dev);
492 if (sc->sc_conf & LM75_CONF_MODE) {
494 strlcpy(buf, "interrupt", sizeof(buf));
497 strlcpy(buf, "comparator", sizeof(buf));
500 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
501 if (error != 0 || req->newptr == NULL)
504 newm = lm75_str_mode(buf);
505 if (newm != -1 && mode != newm) {
506 sc->sc_conf &= ~LM75_CONF_MODE;
508 sc->sc_conf |= LM75_CONF_MODE;
509 if (lm75_conf_write(sc) != 0)
517 lm75_pol_sysctl(SYSCTL_HANDLER_ARGS)
521 int error, newp, pol;
522 struct lm75_softc *sc;
524 dev = (device_t)arg1;
525 sc = device_get_softc(dev);
526 if (sc->sc_conf & LM75_CONF_POL) {
528 strlcpy(buf, "active-high", sizeof(buf));
531 strlcpy(buf, "active-low", sizeof(buf));
534 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
535 if (error != 0 || req->newptr == NULL)
538 newp = lm75_str_pol(buf);
539 if (newp != -1 && pol != newp) {
540 sc->sc_conf &= ~LM75_CONF_POL;
542 sc->sc_conf |= LM75_CONF_POL;
543 if (lm75_conf_write(sc) != 0)
551 lm75_shutdown_sysctl(SYSCTL_HANDLER_ARGS)
554 int error, shutdown, tmp;
555 struct lm75_softc *sc;
557 dev = (device_t)arg1;
558 sc = device_get_softc(dev);
559 tmp = shutdown = (sc->sc_conf & LM75_CONF_SHUTD) ? 1 : 0;
561 error = sysctl_handle_int(oidp, &shutdown, 0, req);
562 if (error != 0 || req->newptr == NULL)
565 if (shutdown != tmp) {
566 sc->sc_conf &= ~LM75_CONF_SHUTD;
568 sc->sc_conf |= LM75_CONF_SHUTD;
569 if (lm75_conf_write(sc) != 0)