Home | History | Annotate | Line # | Download | only in i2c
smscmon.c revision 1.1.12.1
      1  1.1.12.1    cherry /* $NetBSD: smscmon.c,v 1.1.12.1 2011/06/23 14:19:59 cherry Exp $ */
      2       1.1  pgoyette 
      3       1.1  pgoyette /*
      4       1.1  pgoyette  * Copyright (c) 2009 Takahiro Hayashi
      5       1.1  pgoyette  * All rights reserved.
      6       1.1  pgoyette  *
      7       1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
      8       1.1  pgoyette  * modification, are permitted provided that the following conditions
      9       1.1  pgoyette  * are met:
     10       1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     11       1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     12       1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     15       1.1  pgoyette  *
     16       1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17       1.1  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18       1.1  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19       1.1  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20       1.1  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21       1.1  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22       1.1  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23       1.1  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24       1.1  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25       1.1  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26       1.1  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     27       1.1  pgoyette  */
     28       1.1  pgoyette 
     29       1.1  pgoyette #include <sys/cdefs.h>
     30  1.1.12.1    cherry __KERNEL_RCSID(0, "$NetBSD: smscmon.c,v 1.1.12.1 2011/06/23 14:19:59 cherry Exp $");
     31       1.1  pgoyette 
     32       1.1  pgoyette #include <sys/param.h>
     33       1.1  pgoyette #include <sys/systm.h>
     34       1.1  pgoyette #include <sys/device.h>
     35       1.1  pgoyette 
     36       1.1  pgoyette #include <dev/i2c/i2cvar.h>
     37       1.1  pgoyette #include <dev/sysmon/sysmonvar.h>
     38       1.1  pgoyette #include <dev/i2c/smscmonvar.h>
     39       1.1  pgoyette 
     40       1.1  pgoyette /*
     41       1.1  pgoyette  * A driver for SMSC LPC47M192 hardware monitor at SMBus.
     42       1.1  pgoyette  * This driver supports 8 Voltage and 3 Temperature sensors.
     43       1.1  pgoyette  * Fan RPM monitoring is not supported in this driver because
     44       1.1  pgoyette  * they are seen on ISA bus.
     45       1.1  pgoyette  *
     46       1.1  pgoyette  * Datasheet available (as of Feb. 20, 2010) at
     47       1.1  pgoyette  * http://pdf1.alldatasheet.com/datasheet-pdf/view/109752/SMSC/LPC47M192-NC.html
     48       1.1  pgoyette  */
     49       1.1  pgoyette 
     50       1.1  pgoyette static int smscmon_match(device_t, cfdata_t, void *);
     51       1.1  pgoyette static void smscmon_attach(device_t, device_t, void *);
     52       1.1  pgoyette static uint8_t smscmon_readreg(struct smscmon_sc *, int);
     53       1.1  pgoyette static void smscmon_writereg(struct smscmon_sc *, int, int);
     54       1.1  pgoyette static void smscmon_sensors_setup(struct smscmon_sc *, struct smscmon_sensor *);
     55       1.1  pgoyette static void smscmon_refresh_volt(struct smscmon_sc *, envsys_data_t *);
     56       1.1  pgoyette static void smscmon_refresh_temp(struct smscmon_sc *, envsys_data_t *);
     57       1.1  pgoyette static void smscmon_refresh(struct sysmon_envsys *, envsys_data_t *);
     58       1.1  pgoyette 
     59       1.1  pgoyette CFATTACH_DECL_NEW(smscmon, sizeof(struct smscmon_sc),
     60       1.1  pgoyette     smscmon_match, smscmon_attach, NULL, NULL);
     61       1.1  pgoyette 
     62       1.1  pgoyette static struct smscmon_sensor smscmon_lpc47m192[] = {
     63       1.1  pgoyette 	{
     64       1.1  pgoyette 		.desc = "+2.5V",
     65       1.1  pgoyette 		.type = ENVSYS_SVOLTS_DC,
     66       1.1  pgoyette 		.reg = 0x20,
     67       1.1  pgoyette 		.refresh = smscmon_refresh_volt,
     68       1.1  pgoyette 		.vmin =   13000,
     69       1.1  pgoyette 		.vmax = 3320000
     70       1.1  pgoyette 	},
     71       1.1  pgoyette 	{
     72       1.1  pgoyette 		.desc = "Vccp",
     73       1.1  pgoyette 		.type = ENVSYS_SVOLTS_DC,
     74       1.1  pgoyette 		.reg = 0x21,
     75       1.1  pgoyette 		.refresh = smscmon_refresh_volt,
     76       1.1  pgoyette 		.vmin =   12000,
     77       1.1  pgoyette 		.vmax = 2988000
     78       1.1  pgoyette 	},
     79       1.1  pgoyette 	{
     80       1.1  pgoyette 		.desc = "+3.3V",
     81       1.1  pgoyette 		.type = ENVSYS_SVOLTS_DC,
     82       1.1  pgoyette 		.reg = 0x22,
     83       1.1  pgoyette 		.refresh = smscmon_refresh_volt,
     84       1.1  pgoyette 		.vmin =   17000,
     85       1.1  pgoyette 		.vmax = 4383000
     86       1.1  pgoyette 	},
     87       1.1  pgoyette 	{
     88       1.1  pgoyette 		.desc = "+5V",
     89       1.1  pgoyette 		.type = ENVSYS_SVOLTS_DC,
     90       1.1  pgoyette 		.reg = 0x23,
     91       1.1  pgoyette 		.refresh = smscmon_refresh_volt,
     92       1.1  pgoyette 		.vmin =   26000,
     93       1.1  pgoyette 		.vmax = 6640000
     94       1.1  pgoyette 	},
     95       1.1  pgoyette 	{
     96       1.1  pgoyette 		.desc = "+12V",
     97       1.1  pgoyette 		.type = ENVSYS_SVOLTS_DC,
     98       1.1  pgoyette 		.reg = 0x24,
     99       1.1  pgoyette 		.refresh = smscmon_refresh_volt,
    100       1.1  pgoyette 		.vmin =    62000,
    101       1.1  pgoyette 		.vmax = 15938000
    102       1.1  pgoyette 	},
    103       1.1  pgoyette 	{
    104       1.1  pgoyette 		.desc = "Vcc",
    105       1.1  pgoyette 		.type = ENVSYS_SVOLTS_DC,
    106       1.1  pgoyette 		.reg = 0x25,
    107       1.1  pgoyette 		.refresh = smscmon_refresh_volt,
    108       1.1  pgoyette 		.vmin =   17000,
    109       1.1  pgoyette 		.vmax = 4383000
    110       1.1  pgoyette 	},
    111       1.1  pgoyette 	{
    112       1.1  pgoyette 		.desc = "+1.5V",
    113       1.1  pgoyette 		.type = ENVSYS_SVOLTS_DC,
    114       1.1  pgoyette 		.reg = 0x50,
    115       1.1  pgoyette 		.refresh = smscmon_refresh_volt,
    116       1.1  pgoyette 		.vmin =    8000,
    117       1.1  pgoyette 		.vmax = 1992000
    118       1.1  pgoyette 	},
    119       1.1  pgoyette 	{
    120       1.1  pgoyette 		.desc = "+1.8V",
    121       1.1  pgoyette 		.type = ENVSYS_SVOLTS_DC,
    122       1.1  pgoyette 		.reg = 0x51,
    123       1.1  pgoyette 		.refresh = smscmon_refresh_volt,
    124       1.1  pgoyette 		.vmin =    9000,
    125       1.1  pgoyette 		.vmax = 2391000
    126       1.1  pgoyette 	},
    127       1.1  pgoyette 	{
    128       1.1  pgoyette 		.desc = "Remote Temp1",
    129       1.1  pgoyette 		.type = ENVSYS_STEMP,
    130       1.1  pgoyette 		.reg = 0x26,
    131       1.1  pgoyette 		.refresh = smscmon_refresh_temp,
    132       1.1  pgoyette 		.vmin = 0,
    133       1.1  pgoyette 		.vmax = 0
    134       1.1  pgoyette 	},
    135       1.1  pgoyette 	{
    136       1.1  pgoyette 		.desc = "Ambient Temp",
    137       1.1  pgoyette 		.type = ENVSYS_STEMP,
    138       1.1  pgoyette 		.reg = 0x27,
    139       1.1  pgoyette 		.refresh = smscmon_refresh_temp,
    140       1.1  pgoyette 		.vmin = 0,
    141       1.1  pgoyette 		.vmax = 0
    142       1.1  pgoyette 	},
    143       1.1  pgoyette 	{
    144       1.1  pgoyette 		.desc = "Remote Temp2",
    145       1.1  pgoyette 		.type = ENVSYS_STEMP,
    146       1.1  pgoyette 		.reg = 0x52,
    147       1.1  pgoyette 		.refresh = smscmon_refresh_temp,
    148       1.1  pgoyette 		.vmax = 0,
    149       1.1  pgoyette 		.vmin = 0
    150       1.1  pgoyette 	},
    151       1.1  pgoyette 
    152       1.1  pgoyette 	{ .desc = NULL }
    153       1.1  pgoyette };
    154       1.1  pgoyette 
    155       1.1  pgoyette static int
    156       1.1  pgoyette smscmon_match(device_t parent, cfdata_t match, void *aux)
    157       1.1  pgoyette {
    158       1.1  pgoyette 	struct i2c_attach_args *ia = aux;
    159       1.1  pgoyette 	uint8_t cmd, cid, rev;
    160       1.1  pgoyette 
    161       1.1  pgoyette 	/* Address is hardwired to 010_110x */
    162       1.1  pgoyette 	if ((ia->ia_addr & SMSCMON_ADDR_MASK) != SMSCMON_ADDR)
    163       1.1  pgoyette 		return 0;
    164       1.1  pgoyette 
    165       1.1  pgoyette 	iic_acquire_bus(ia->ia_tag, 0);
    166       1.1  pgoyette 
    167       1.1  pgoyette 	cmd = SMSCMON_REG_COMPANY;
    168       1.1  pgoyette 	if (iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP,
    169       1.1  pgoyette 	    ia->ia_addr, &cmd, sizeof cmd, &cid, sizeof cid, 0)) {
    170       1.1  pgoyette 		iic_release_bus(ia->ia_tag, 0);
    171       1.1  pgoyette 		return 0;
    172       1.1  pgoyette 	}
    173       1.1  pgoyette 	cmd = SMSCMON_REG_STEPPING;
    174       1.1  pgoyette 	if (iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP,
    175       1.1  pgoyette 	    ia->ia_addr, &cmd, sizeof cmd, &rev, sizeof rev, 0)) {
    176       1.1  pgoyette 		iic_release_bus(ia->ia_tag, 0);
    177       1.1  pgoyette 		return 0;
    178       1.1  pgoyette 	}
    179       1.1  pgoyette 
    180       1.1  pgoyette 	if ( cid != SMSC_CID_47M192 || rev != SMSC_REV_47M192) {
    181       1.1  pgoyette 		iic_release_bus(ia->ia_tag, 0);
    182       1.1  pgoyette 		return 0;
    183       1.1  pgoyette 	}
    184       1.1  pgoyette 
    185       1.1  pgoyette 	iic_release_bus(ia->ia_tag, 0);
    186       1.1  pgoyette 	return 1;
    187       1.1  pgoyette }
    188       1.1  pgoyette 
    189       1.1  pgoyette static void
    190       1.1  pgoyette smscmon_attach(device_t parent, device_t self, void *aux)
    191       1.1  pgoyette {
    192       1.1  pgoyette 	struct smscmon_sc *sc = device_private(self);
    193       1.1  pgoyette 	struct i2c_attach_args *ia = aux;
    194       1.1  pgoyette 	uint8_t cid, rev;
    195       1.1  pgoyette 	int i;
    196       1.1  pgoyette 
    197       1.1  pgoyette 	sc->sc_dev = self;
    198       1.1  pgoyette 	sc->sc_tag = ia->ia_tag;
    199       1.1  pgoyette 	sc->sc_addr = ia->ia_addr;
    200       1.1  pgoyette 	sc->smscmon_readreg = smscmon_readreg;
    201       1.1  pgoyette 	sc->smscmon_writereg = smscmon_writereg;
    202       1.1  pgoyette 	sc->smscmon_sensors = NULL;
    203       1.1  pgoyette 
    204       1.1  pgoyette 	cid = sc->smscmon_readreg(sc, SMSCMON_REG_COMPANY);
    205       1.1  pgoyette 	rev = sc->smscmon_readreg(sc, SMSCMON_REG_STEPPING);
    206       1.1  pgoyette 	switch (cid) {
    207       1.1  pgoyette 	case SMSC_CID_47M192:
    208       1.1  pgoyette 		if (rev == SMSC_REV_47M192) {
    209       1.1  pgoyette 			smscmon_sensors_setup(sc, smscmon_lpc47m192);
    210       1.1  pgoyette 			aprint_normal(": LPC47M192 hardware monitor\n");
    211       1.1  pgoyette 		}
    212       1.1  pgoyette 		break;
    213       1.1  pgoyette 	default:
    214       1.1  pgoyette 		/* unknown chip */
    215       1.1  pgoyette 		break;
    216       1.1  pgoyette 	}
    217       1.1  pgoyette 
    218       1.1  pgoyette 	if (sc->smscmon_sensors == NULL) {
    219       1.1  pgoyette 		aprint_normal(": unknown chip: cid 0x%02x rev 0x%02x\n",
    220       1.1  pgoyette 		    cid, rev);
    221       1.1  pgoyette 		return;
    222       1.1  pgoyette 	}
    223       1.1  pgoyette 
    224       1.1  pgoyette 	if ((sc->sc_sme = sysmon_envsys_create()) == NULL) {
    225       1.1  pgoyette 		aprint_error_dev(sc->sc_dev,
    226       1.1  pgoyette 		    "unable to create sysmon structure\n");
    227       1.1  pgoyette 		return;
    228       1.1  pgoyette 	}
    229       1.1  pgoyette 
    230       1.1  pgoyette 	for (i = 0; i < sc->numsensors; i++) {
    231       1.1  pgoyette 		if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sensors[i])) {
    232       1.1  pgoyette 			aprint_error_dev(sc->sc_dev,
    233       1.1  pgoyette 			    "unable to attach sensor\n");
    234       1.1  pgoyette 			sysmon_envsys_destroy(sc->sc_sme);
    235       1.1  pgoyette 			return;
    236       1.1  pgoyette 		}
    237       1.1  pgoyette 	}
    238       1.1  pgoyette 
    239       1.1  pgoyette 	sc->sc_sme->sme_name = device_xname(sc->sc_dev);
    240       1.1  pgoyette 	sc->sc_sme->sme_cookie = sc;
    241       1.1  pgoyette 	sc->sc_sme->sme_refresh = smscmon_refresh;
    242       1.1  pgoyette 	if (sysmon_envsys_register(sc->sc_sme)) {
    243       1.1  pgoyette 		aprint_error_dev(sc->sc_dev,
    244       1.1  pgoyette 		    "unable to register with sysmon\n");
    245       1.1  pgoyette 		sysmon_envsys_destroy(sc->sc_sme);
    246       1.1  pgoyette 		return;
    247       1.1  pgoyette 	}
    248       1.1  pgoyette }
    249       1.1  pgoyette 
    250       1.1  pgoyette static uint8_t
    251       1.1  pgoyette smscmon_readreg(struct smscmon_sc *sc, int reg)
    252       1.1  pgoyette {
    253       1.1  pgoyette 	uint8_t cmd, data;
    254       1.1  pgoyette 
    255       1.1  pgoyette 	iic_acquire_bus(sc->sc_tag, 0);
    256       1.1  pgoyette 
    257       1.1  pgoyette 	cmd = reg;
    258       1.1  pgoyette 	iic_smbus_read_byte(sc->sc_tag, sc->sc_addr, cmd, &data, 0);
    259       1.1  pgoyette 
    260       1.1  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    261       1.1  pgoyette 
    262       1.1  pgoyette 	return data;
    263       1.1  pgoyette }
    264       1.1  pgoyette 
    265       1.1  pgoyette static void
    266       1.1  pgoyette smscmon_writereg(struct smscmon_sc *sc, int reg, int val)
    267       1.1  pgoyette {
    268       1.1  pgoyette 	uint8_t cmd, data;
    269       1.1  pgoyette 
    270       1.1  pgoyette 	iic_acquire_bus(sc->sc_tag, 0);
    271       1.1  pgoyette 
    272       1.1  pgoyette 	cmd = reg;
    273       1.1  pgoyette 	data = val;
    274       1.1  pgoyette 	iic_smbus_write_byte(sc->sc_tag, sc->sc_addr, cmd, data, 0);
    275       1.1  pgoyette 
    276       1.1  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    277       1.1  pgoyette }
    278       1.1  pgoyette 
    279       1.1  pgoyette static void
    280       1.1  pgoyette smscmon_sensors_setup(struct smscmon_sc *sc, struct smscmon_sensor *sens)
    281       1.1  pgoyette {
    282       1.1  pgoyette 	int i;
    283       1.1  pgoyette 
    284       1.1  pgoyette 	for (i = 0; sens[i].desc; i++) {
    285       1.1  pgoyette 		strlcpy(sc->sensors[i].desc, sens[i].desc,
    286       1.1  pgoyette 		    sizeof(sc->sensors[i].desc));
    287       1.1  pgoyette 		sc->sensors[i].units = sens[i].type;
    288  1.1.12.1    cherry 		sc->sensors[i].state = ENVSYS_SINVALID;
    289       1.1  pgoyette 		sc->numsensors++;
    290       1.1  pgoyette 	}
    291       1.1  pgoyette 	sc->smscmon_sensors = sens;
    292       1.1  pgoyette }
    293       1.1  pgoyette 
    294       1.1  pgoyette static void
    295       1.1  pgoyette smscmon_refresh_volt(struct smscmon_sc *sc, envsys_data_t *edata)
    296       1.1  pgoyette {
    297       1.1  pgoyette 	struct smscmon_sensor *sens = &sc->smscmon_sensors[edata->sensor];
    298       1.1  pgoyette 	int data;
    299       1.1  pgoyette 
    300       1.1  pgoyette 	data = (*sc->smscmon_readreg)(sc, sens->reg);
    301       1.1  pgoyette 	if (data == 0xff) {
    302       1.1  pgoyette 		edata->state = ENVSYS_SINVALID;
    303       1.1  pgoyette 	} else {
    304       1.1  pgoyette 		edata->value_cur =
    305       1.1  pgoyette 		    (sens->vmax - sens->vmin)/255 * data + sens->vmin;
    306       1.1  pgoyette 		edata->state = ENVSYS_SVALID;
    307       1.1  pgoyette 	}
    308       1.1  pgoyette }
    309       1.1  pgoyette 
    310       1.1  pgoyette static void
    311       1.1  pgoyette smscmon_refresh_temp(struct smscmon_sc *sc, envsys_data_t *edata)
    312       1.1  pgoyette {
    313       1.1  pgoyette 	struct smscmon_sensor *sens = &sc->smscmon_sensors[edata->sensor];
    314       1.1  pgoyette 	int data;
    315       1.1  pgoyette 
    316       1.1  pgoyette 	data = (*sc->smscmon_readreg)(sc, sens->reg);
    317       1.1  pgoyette 	if (data == 0xff) {
    318       1.1  pgoyette 		edata->state = ENVSYS_SINVALID;
    319       1.1  pgoyette 	} else {
    320       1.1  pgoyette 		/* convert data(degC) to uK */
    321       1.1  pgoyette 		edata->value_cur = 273150000 + 1000000 * data;
    322       1.1  pgoyette 		edata->state = ENVSYS_SVALID;
    323       1.1  pgoyette 	}
    324       1.1  pgoyette }
    325       1.1  pgoyette 
    326       1.1  pgoyette static void
    327       1.1  pgoyette smscmon_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    328       1.1  pgoyette {
    329       1.1  pgoyette 	struct smscmon_sc *sc = sme->sme_cookie;
    330       1.1  pgoyette 
    331       1.1  pgoyette 	sc->smscmon_sensors[edata->sensor].refresh(sc, edata);
    332       1.1  pgoyette }
    333