Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys/dev/i2c Add a driver for the LM81, LM87, and compatible ...
details: https://anonhg.NetBSD.org/src/rev/05a580a127ae
branches: trunk
changeset: 790507:05a580a127ae
user: jdc <jdc%NetBSD.org@localhost>
date: Tue Oct 15 19:12:41 2013 +0000
description:
Add a driver for the LM81, LM87, and compatible iic hardware monitors
(lmenv). Ported from OpenBSD's lm87.c, revision 1.20.
diffstat:
sys/dev/i2c/lm87.c | 394 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 394 insertions(+), 0 deletions(-)
diffs (truncated from 398 to 300 lines):
diff -r 2e6e26a581cd -r 05a580a127ae sys/dev/i2c/lm87.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/i2c/lm87.c Tue Oct 15 19:12:41 2013 +0000
@@ -0,0 +1,394 @@
+/* $NetBSD: lm87.c,v 1.1 2013/10/15 19:12:41 jdc Exp $ */
+/* $OpenBSD: lm87.c,v 1.20 2008/11/10 05:19:48 cnst Exp $ */
+
+/*
+ * Copyright (c) 2005 Mark Kettenis
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: lm87.c,v 1.1 2013/10/15 19:12:41 jdc Exp $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/device.h>
+#include <dev/sysmon/sysmonvar.h>
+
+#include <dev/i2c/i2cvar.h>
+
+/* LM87 registers */
+#define LM87_2_5V 0x20
+#define LM87_VCCP1 0x21
+#define LM87_VCC 0x22
+#define LM87_5V 0x23
+#define LM87_12V 0x24
+#define LM87_VCCP2 0x25
+#define LM87_EXT_TEMP 0x26
+#define LM87_INT_TEMP 0x27
+#define LM87_FAN1 0x28
+#define LM87_FAN2 0x29
+#define LM87_COMPANY_ID 0x3e
+#define LM87_REVISION 0x3f
+#define LM87_CONFIG1 0x40
+#define LM87_CONFIG1_START 0x01
+#define LM87_CONFIG1_INTCLR 0x08
+#define LM87_CHANNEL 0x16
+#define LM87_CHANNEL_AIN1 0x01
+#define LM87_CHANNEL_AIN2 0x02
+#define LM87_FANDIV 0x47
+
+struct lmenv_id {
+ u_int8_t id, family;
+ const char *name;
+};
+
+static const struct lmenv_id lmenv_ids[] = {
+ { 0x01, 81, "LM81" },
+ { 0x02, 87, "LM87" }, /* LM87 or LM87CIMT */
+ { 0x23, 81, "ADM9240" },
+ { 0xda, 81, "DSL780" },
+ { 0x00, 0, NULL }
+};
+
+/* Sensors */
+#define LMENV_2_5V 0
+#define LMENV_VCCP1 1
+#define LMENV_VCC 2
+#define LMENV_5V 3
+#define LMENV_12V 4
+#define LMENV_VCCP2 5
+#define LMENV_EXT_TEMP 6
+#define LMENV_INT_TEMP 7
+#define LMENV_FAN1 8
+#define LMENV_FAN2 9
+#define LMENV_NUM_SENSORS 10
+
+struct lmenv_softc {
+ i2c_tag_t sc_tag;
+ i2c_addr_t sc_addr;
+
+ int sc_fan1_div, sc_fan2_div;
+ int sc_family;
+
+ struct sysmon_envsys *sc_sme;
+ envsys_data_t sc_sensor[LMENV_NUM_SENSORS];
+};
+
+int lmenv_match(device_t, cfdata_t, void *);
+void lmenv_attach(device_t, device_t, void *);
+
+void lmenv_refresh(struct sysmon_envsys *, envsys_data_t *);
+
+CFATTACH_DECL_NEW(lmenv, sizeof(struct lmenv_softc),
+ lmenv_match, lmenv_attach, NULL, NULL);
+
+static const char * lmenv_compats[] = {
+ "lm87",
+ "lm87cimt",
+ "adm9240",
+ "lm81",
+ "ds1780",
+ NULL
+};
+
+int
+lmenv_match(device_t parent, cfdata_t match, void *aux)
+{
+ struct i2c_attach_args *ia = aux;
+ u_int8_t cmd, val;
+ int error, i;
+
+ if (ia->ia_name == NULL) {
+ /*
+ * Indirect config - not much we can do!
+ * Check typical addresses and read the Company ID register
+ */
+ if ((ia->ia_addr < 0x2c) || (ia->ia_addr > 0x2f))
+ return 0;
+
+ cmd = LM87_COMPANY_ID;
+ iic_acquire_bus(ia->ia_tag, 0);
+ error = iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr,
+ &cmd, 1, &val, 1, I2C_F_POLL);
+ iic_release_bus(ia->ia_tag, 0);
+
+ if (error)
+ return 0;
+
+ for (i = 0; lmenv_ids[i].id != 0; i++)
+ if (lmenv_ids[i].id == val)
+ return 1;
+ } else {
+ /*
+ * Direct config - match via the list of compatible
+ * hardware.
+ */
+ if (iic_compat_match(ia, lmenv_compats))
+ return 1;
+ }
+
+ return 0;
+}
+
+void
+lmenv_attach(device_t parent, device_t self, void *aux)
+{
+ struct lmenv_softc *sc = device_private(self);
+ struct i2c_attach_args *ia = aux;
+ u_int8_t cmd, data, data2, channel;
+ int i;
+
+ sc->sc_tag = ia->ia_tag;
+ sc->sc_addr = ia->ia_addr;
+
+ iic_acquire_bus(sc->sc_tag, 0);
+
+ cmd = LM87_COMPANY_ID;
+ if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
+ sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0)) {
+ iic_release_bus(sc->sc_tag, 0);
+ printf(": cannot read ID register\n");
+ return;
+ }
+ for (i = 0; lmenv_ids[i].id != 0; i++)
+ if (lmenv_ids[i].id == data)
+ break;
+
+ cmd = LM87_REVISION;
+ if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
+ sc->sc_addr, &cmd, sizeof cmd, &data2, sizeof data, 0)) {
+ iic_release_bus(sc->sc_tag, 0);
+ printf(": cannot read revision register\n");
+ return;
+ }
+ printf(": %s rev %x\n", lmenv_ids[i].name, data2);
+ sc->sc_family = lmenv_ids[i].family;
+
+ cmd = LM87_FANDIV;
+ if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
+ sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0)) {
+ iic_release_bus(sc->sc_tag, 0);
+ printf(", cannot read Fan Divisor register\n");
+ return;
+ }
+ sc->sc_fan1_div = 1 << ((data >> 4) & 0x03);
+ sc->sc_fan2_div = 1 << ((data >> 6) & 0x03);
+
+ if (sc->sc_family == 87) {
+ cmd = LM87_CHANNEL;
+ if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
+ sc->sc_addr, &cmd, sizeof cmd, &channel,
+ sizeof channel, 0)) {
+ iic_release_bus(sc->sc_tag, 0);
+ printf(", cannot read Channel register\n");
+ return;
+ }
+ } else
+ channel = 0;
+
+ cmd = LM87_CONFIG1;
+ if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
+ sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0)) {
+ iic_release_bus(sc->sc_tag, 0);
+ printf(", cannot read Configuration Register 1\n");
+ return;
+ }
+
+ /*
+ * if chip is not running, try to start it.
+ * if it is stalled doing an interrupt, unstall it
+ */
+ data2 = (data | LM87_CONFIG1_START);
+ data2 = data2 & ~LM87_CONFIG1_INTCLR;
+
+ if (data != data2) {
+ if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
+ sc->sc_addr, &cmd, sizeof cmd, &data2, sizeof data2, 0)) {
+ iic_release_bus(sc->sc_tag, 0);
+ printf(", cannot write Configuration Register 1\n");
+ return;
+ }
+ printf(", starting scan");
+ }
+ iic_release_bus(sc->sc_tag, 0);
+
+ /* Initialize sensor data. */
+ sc->sc_sensor[LMENV_2_5V].state = ENVSYS_SINVALID;
+ sc->sc_sensor[LMENV_2_5V].units = ENVSYS_SVOLTS_DC;
+ strlcpy(sc->sc_sensor[LMENV_2_5V].desc, "+2.5Vin",
+ sizeof(sc->sc_sensor[LMENV_2_5V].desc));
+
+ sc->sc_sensor[LMENV_VCCP1].state = ENVSYS_SINVALID;
+ sc->sc_sensor[LMENV_VCCP1].units = ENVSYS_SVOLTS_DC;
+ strlcpy(sc->sc_sensor[LMENV_VCCP1].desc, "Vccp1",
+ sizeof(sc->sc_sensor[LMENV_VCCP1].desc));
+
+ sc->sc_sensor[LMENV_VCC].state = ENVSYS_SINVALID;
+ sc->sc_sensor[LMENV_VCC].units = ENVSYS_SVOLTS_DC;
+ strlcpy(sc->sc_sensor[LMENV_VCC].desc, "+Vcc",
+ sizeof(sc->sc_sensor[LMENV_VCC].desc));
+
+ sc->sc_sensor[LMENV_5V].state = ENVSYS_SINVALID;
+ sc->sc_sensor[LMENV_5V].units = ENVSYS_SVOLTS_DC;
+ strlcpy(sc->sc_sensor[LMENV_5V].desc, "+5Vin/Vcc",
+ sizeof(sc->sc_sensor[LMENV_5V].desc));
+
+ sc->sc_sensor[LMENV_12V].state = ENVSYS_SINVALID;
+ sc->sc_sensor[LMENV_12V].units = ENVSYS_SVOLTS_DC;
+ strlcpy(sc->sc_sensor[LMENV_12V].desc, "+12Vin",
+ sizeof(sc->sc_sensor[LMENV_12V].desc));
+
+ sc->sc_sensor[LMENV_VCCP2].state = ENVSYS_SINVALID;
+ sc->sc_sensor[LMENV_VCCP2].units = ENVSYS_SVOLTS_DC;
+ strlcpy(sc->sc_sensor[LMENV_VCCP2].desc, "Vccp2",
+ sizeof(sc->sc_sensor[LMENV_VCCP2].desc));
+
+ sc->sc_sensor[LMENV_EXT_TEMP].state = ENVSYS_SINVALID;
+ sc->sc_sensor[LMENV_EXT_TEMP].units = ENVSYS_STEMP;
+ strlcpy(sc->sc_sensor[LMENV_EXT_TEMP].desc, "External",
+ sizeof(sc->sc_sensor[LMENV_EXT_TEMP].desc));
+ if (sc->sc_family == 81)
+ sc->sc_sensor[LMENV_EXT_TEMP].state = ENVSYS_SINVALID;
+
+ sc->sc_sensor[LMENV_INT_TEMP].state = ENVSYS_SINVALID;
+ sc->sc_sensor[LMENV_INT_TEMP].units = ENVSYS_STEMP;
+ strlcpy(sc->sc_sensor[LMENV_INT_TEMP].desc, "Internal",
+ sizeof(sc->sc_sensor[LMENV_INT_TEMP].desc));
+
+ sc->sc_sensor[LMENV_FAN1].state = ENVSYS_SINVALID;
+ if (channel & LM87_CHANNEL_AIN1) {
+ sc->sc_sensor[LMENV_FAN1].units = ENVSYS_SVOLTS_DC;
+ strlcpy(sc->sc_sensor[LMENV_FAN1].desc, "AIN1",
+ sizeof(sc->sc_sensor[LMENV_FAN1].desc));
+ } else {
+ sc->sc_sensor[LMENV_FAN1].units = ENVSYS_SFANRPM;
+ strlcpy(sc->sc_sensor[LMENV_FAN1].desc, "FAN1",
+ sizeof(sc->sc_sensor[LMENV_FAN1].desc));
+ }
+
+ sc->sc_sensor[LMENV_FAN2].state = ENVSYS_SINVALID;
+ if (channel & LM87_CHANNEL_AIN2) {
+ sc->sc_sensor[LMENV_FAN2].units = ENVSYS_SVOLTS_DC;
+ strlcpy(sc->sc_sensor[LMENV_FAN2].desc, "AIN2",
+ sizeof(sc->sc_sensor[LMENV_FAN2].desc));
+ } else {
+ sc->sc_sensor[LMENV_FAN2].units = ENVSYS_SFANRPM;
+ strlcpy(sc->sc_sensor[LMENV_FAN2].desc, "FAN2",
+ sizeof(sc->sc_sensor[LMENV_FAN2].desc));
+ }
+
+ sc->sc_sme = sysmon_envsys_create();
+ for (i = 0; i < LMENV_NUM_SENSORS; i++)
+ if (sysmon_envsys_sensor_attach(sc->sc_sme,
+ &sc->sc_sensor[i])) {
+ sysmon_envsys_destroy(sc->sc_sme);
+ aprint_error_dev(self,
Home |
Main Index |
Thread Index |
Old Index