Home | History | Annotate | Line # | Download | only in i2c
sdtemp.c revision 1.6
      1  1.6  pgoyette /*      $NetBSD: sdtemp.c,v 1.6 2009/06/14 19:44:46 pgoyette Exp $        */
      2  1.1  pgoyette 
      3  1.1  pgoyette /*
      4  1.1  pgoyette  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  1.1  pgoyette  * All rights reserved.
      6  1.1  pgoyette  *
      7  1.1  pgoyette  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  pgoyette  * by Paul Goyette.
      9  1.1  pgoyette  *
     10  1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     11  1.1  pgoyette  * modification, are permitted provided that the following conditions
     12  1.1  pgoyette  * are met:
     13  1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     14  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     15  1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     18  1.1  pgoyette  *
     19  1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  pgoyette  */
     31  1.1  pgoyette 
     32  1.1  pgoyette #include <sys/cdefs.h>
     33  1.6  pgoyette __KERNEL_RCSID(0, "$NetBSD: sdtemp.c,v 1.6 2009/06/14 19:44:46 pgoyette Exp $");
     34  1.1  pgoyette 
     35  1.1  pgoyette #include <sys/param.h>
     36  1.1  pgoyette #include <sys/systm.h>
     37  1.1  pgoyette #include <sys/kmem.h>
     38  1.1  pgoyette #include <sys/device.h>
     39  1.1  pgoyette #include <sys/kernel.h>
     40  1.1  pgoyette #include <sys/endian.h>
     41  1.1  pgoyette 
     42  1.1  pgoyette #include <dev/sysmon/sysmonvar.h>
     43  1.1  pgoyette 
     44  1.1  pgoyette #include <dev/i2c/i2cvar.h>
     45  1.1  pgoyette #include <dev/i2c/sdtemp_reg.h>
     46  1.1  pgoyette 
     47  1.1  pgoyette struct sdtemp_softc {
     48  1.1  pgoyette 	device_t sc_dev;
     49  1.1  pgoyette 	i2c_tag_t sc_tag;
     50  1.1  pgoyette 	int sc_address;
     51  1.1  pgoyette 
     52  1.1  pgoyette 	struct sysmon_envsys *sc_sme;
     53  1.1  pgoyette 	envsys_data_t *sc_sensor;
     54  1.1  pgoyette 	int sc_resolution;
     55  1.1  pgoyette 	uint16_t sc_capability;
     56  1.1  pgoyette };
     57  1.1  pgoyette 
     58  1.1  pgoyette static int  sdtemp_match(device_t, cfdata_t, void *);
     59  1.1  pgoyette static void sdtemp_attach(device_t, device_t, void *);
     60  1.1  pgoyette 
     61  1.1  pgoyette CFATTACH_DECL_NEW(sdtemp, sizeof(struct sdtemp_softc),
     62  1.1  pgoyette 	sdtemp_match, sdtemp_attach, NULL, NULL);
     63  1.1  pgoyette 
     64  1.1  pgoyette static void	sdtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
     65  1.6  pgoyette static void	sdtemp_get_limits(struct sysmon_envsys *, envsys_data_t *,
     66  1.4  pgoyette 				  sysmon_envsys_lim_t *);
     67  1.6  pgoyette static void	sdtemp_set_limits(struct sysmon_envsys *, envsys_data_t *,
     68  1.4  pgoyette 				  sysmon_envsys_lim_t *);
     69  1.1  pgoyette #ifdef NOT_YET
     70  1.1  pgoyette static int	sdtemp_read_8(struct sdtemp_softc *, uint8_t, uint8_t *);
     71  1.1  pgoyette static int	sdtemp_write_8(struct sdtemp_softc *, uint8_t, uint8_t);
     72  1.1  pgoyette #endif /* NOT YET */
     73  1.1  pgoyette static int	sdtemp_read_16(struct sdtemp_softc *, uint8_t, uint16_t *);
     74  1.1  pgoyette static int	sdtemp_write_16(struct sdtemp_softc *, uint8_t, uint16_t);
     75  1.1  pgoyette static uint32_t	sdtemp_decode_temp(struct sdtemp_softc *, uint16_t);
     76  1.1  pgoyette static bool	sdtemp_pmf_suspend(device_t PMF_FN_PROTO);
     77  1.1  pgoyette static bool	sdtemp_pmf_resume(device_t PMF_FN_PROTO);
     78  1.1  pgoyette 
     79  1.1  pgoyette struct sdtemp_dev_entry {
     80  1.1  pgoyette 	const uint16_t sdtemp_mfg_id;
     81  1.1  pgoyette 	const uint8_t  sdtemp_dev_id;
     82  1.1  pgoyette 	const uint8_t  sdtemp_rev_id;
     83  1.1  pgoyette 	const uint8_t  sdtemp_resolution;
     84  1.1  pgoyette 	const char    *sdtemp_desc;
     85  1.1  pgoyette };
     86  1.1  pgoyette 
     87  1.4  pgoyette /* Convert sysmon_envsys uKelvin value to simple degC */
     88  1.4  pgoyette 
     89  1.4  pgoyette #define	__UK2C(uk) (((uk) - 273150000) / 1000000)
     90  1.1  pgoyette 
     91  1.1  pgoyette /*
     92  1.1  pgoyette  * List of devices known to conform to JEDEC JC42.4
     93  1.1  pgoyette  *
     94  1.1  pgoyette  * NOTE: A non-negative value for resolution indicates that the sensor
     95  1.1  pgoyette  * resolution is fixed at that number of fractional bits;  a negative
     96  1.1  pgoyette  * value indicates that the sensor needs to be configured.  In either
     97  1.1  pgoyette  * case, trip-point registers are fixed at two-bit (0.25C) resolution.
     98  1.1  pgoyette  */
     99  1.1  pgoyette static const struct sdtemp_dev_entry
    100  1.1  pgoyette sdtemp_dev_table[] = {
    101  1.1  pgoyette     { MAXIM_MANUFACTURER_ID, MAX_6604_DEVICE_ID,    0xff, 3,
    102  1.1  pgoyette 	"Maxim MAX604" },
    103  1.1  pgoyette     { MCP_MANUFACTURER_ID,   MCP_9805_DEVICE_ID,    0xff, 2,
    104  1.1  pgoyette 	"Microchip Tech MCP9805" },
    105  1.1  pgoyette     { MCP_MANUFACTURER_ID,   MCP_98242_DEVICE_ID,   0xff, -4,
    106  1.1  pgoyette 	"Microchip Tech MCP98242" },
    107  1.1  pgoyette     { ADT_MANUFACTURER_ID,   ADT_7408_DEVICE_ID,    0xff, 4,
    108  1.1  pgoyette 	"Analog Devices ADT7408" },
    109  1.1  pgoyette     { NXP_MANUFACTURER_ID,   NXP_SE97_DEVICE_ID,    0xff, 3,
    110  1.1  pgoyette 	"NXP Semiconductors SE97/SE98" },
    111  1.1  pgoyette     { STTS_MANUFACTURER_ID,  STTS_424E02_DEVICE_ID, 0x00, 2,
    112  1.1  pgoyette 	"STmicroelectronics STTS424E02-DA" },
    113  1.1  pgoyette     { STTS_MANUFACTURER_ID,  STTS_424E02_DEVICE_ID, 0x01, 2,
    114  1.1  pgoyette 	"STmicroelectronics STTS424E02-DN" },
    115  1.1  pgoyette     { CAT_MANUFACTURER_ID,   CAT_34TS02_DEVICE_ID,  0xff, 4,
    116  1.1  pgoyette 	"Catalyst CAT34TS02/CAT6095" },
    117  1.1  pgoyette     { 0, 0, 0, 2, "Unknown" }
    118  1.1  pgoyette };
    119  1.1  pgoyette 
    120  1.1  pgoyette static int
    121  1.1  pgoyette sdtemp_lookup(uint16_t mfg, uint16_t dev, uint16_t rev)
    122  1.1  pgoyette {
    123  1.1  pgoyette 	int i;
    124  1.1  pgoyette 
    125  1.1  pgoyette 	for (i = 0; sdtemp_dev_table[i].sdtemp_mfg_id; i++)
    126  1.1  pgoyette 		if (sdtemp_dev_table[i].sdtemp_mfg_id == mfg &&
    127  1.1  pgoyette 		    sdtemp_dev_table[i].sdtemp_dev_id == dev &&
    128  1.1  pgoyette 		    (sdtemp_dev_table[i].sdtemp_rev_id == 0xff ||
    129  1.1  pgoyette 		     sdtemp_dev_table[i].sdtemp_rev_id == rev))
    130  1.1  pgoyette 			break;
    131  1.1  pgoyette 
    132  1.1  pgoyette 	return i;
    133  1.1  pgoyette }
    134  1.1  pgoyette 
    135  1.1  pgoyette static int
    136  1.1  pgoyette sdtemp_match(device_t parent, cfdata_t cf, void *aux)
    137  1.1  pgoyette {
    138  1.1  pgoyette 	struct i2c_attach_args *ia = aux;
    139  1.1  pgoyette 	uint16_t mfgid, devid;
    140  1.1  pgoyette 	struct sdtemp_softc sc;
    141  1.1  pgoyette 	int i, error;
    142  1.1  pgoyette 
    143  1.1  pgoyette 	sc.sc_tag = ia->ia_tag;
    144  1.1  pgoyette 	sc.sc_address = ia->ia_addr;
    145  1.1  pgoyette 
    146  1.1  pgoyette 	if ((ia->ia_addr & SDTEMP_ADDRMASK) != SDTEMP_ADDR)
    147  1.1  pgoyette 		return 0;
    148  1.1  pgoyette 
    149  1.1  pgoyette 	/* Verify that we can read the manufacturer ID  & Device ID */
    150  1.1  pgoyette 	iic_acquire_bus(sc.sc_tag, 0);
    151  1.1  pgoyette 	error = sdtemp_read_16(&sc, SDTEMP_REG_MFG_ID,  &mfgid) |
    152  1.1  pgoyette 		sdtemp_read_16(&sc, SDTEMP_REG_DEV_REV, &devid);
    153  1.1  pgoyette 	iic_release_bus(sc.sc_tag, 0);
    154  1.1  pgoyette 
    155  1.1  pgoyette 	if (error)
    156  1.1  pgoyette 		return 0;
    157  1.1  pgoyette 
    158  1.1  pgoyette 	i = sdtemp_lookup(mfgid, devid >> 8, devid & 0xff);
    159  1.1  pgoyette 	if (sdtemp_dev_table[i].sdtemp_mfg_id == 0) {
    160  1.1  pgoyette 		aprint_debug("sdtemp: No match for mfg 0x%04x dev 0x%02x "
    161  1.1  pgoyette 		    "rev 0x%02x at address 0x%02x\n", mfgid, devid >> 8,
    162  1.1  pgoyette 		    devid & 0xff, sc.sc_address);
    163  1.1  pgoyette 		return 0;
    164  1.1  pgoyette 	}
    165  1.1  pgoyette 
    166  1.1  pgoyette 	return 1;
    167  1.1  pgoyette }
    168  1.1  pgoyette 
    169  1.1  pgoyette static void
    170  1.1  pgoyette sdtemp_attach(device_t parent, device_t self, void *aux)
    171  1.1  pgoyette {
    172  1.1  pgoyette 	struct sdtemp_softc *sc = device_private(self);
    173  1.1  pgoyette 	struct i2c_attach_args *ia = aux;
    174  1.4  pgoyette 	sysmon_envsys_lim_t limits;
    175  1.1  pgoyette 	uint16_t mfgid, devid;
    176  1.1  pgoyette 	int i, error;
    177  1.1  pgoyette 
    178  1.1  pgoyette 	sc->sc_tag = ia->ia_tag;
    179  1.1  pgoyette 	sc->sc_address = ia->ia_addr;
    180  1.1  pgoyette 	sc->sc_dev = self;
    181  1.1  pgoyette 
    182  1.1  pgoyette 	iic_acquire_bus(sc->sc_tag, 0);
    183  1.1  pgoyette 	if ((error = sdtemp_read_16(sc, SDTEMP_REG_MFG_ID,  &mfgid)) != 0 ||
    184  1.1  pgoyette 	    (error = sdtemp_read_16(sc, SDTEMP_REG_DEV_REV, &devid)) != 0) {
    185  1.5  pgoyette 		iic_release_bus(sc->sc_tag, 0);
    186  1.1  pgoyette 		aprint_error(": attach error %d\n", error);
    187  1.1  pgoyette 		return;
    188  1.1  pgoyette 	}
    189  1.1  pgoyette 	i = sdtemp_lookup(mfgid, devid >> 8, devid & 0xff);
    190  1.1  pgoyette 	sc->sc_resolution =
    191  1.1  pgoyette 	    sdtemp_dev_table[i].sdtemp_resolution;
    192  1.1  pgoyette 
    193  1.1  pgoyette 	aprint_naive(": Temp Sensor\n");
    194  1.1  pgoyette 	aprint_normal(": %s Temp Sensor\n", sdtemp_dev_table[i].sdtemp_desc);
    195  1.1  pgoyette 
    196  1.1  pgoyette 	if (sdtemp_dev_table[i].sdtemp_mfg_id == 0)
    197  1.1  pgoyette 		aprint_debug_dev(self,
    198  1.1  pgoyette 		    "mfg 0x%04x dev 0x%02x rev 0x%02x at addr 0x%02x\n",
    199  1.1  pgoyette 		    mfgid, devid >> 8, devid & 0xff, ia->ia_addr);
    200  1.1  pgoyette 
    201  1.1  pgoyette 	/*
    202  1.1  pgoyette 	 * Alarm capability is required;  if not present, this is likely
    203  1.1  pgoyette 	 * not a real sdtemp device.
    204  1.1  pgoyette 	 */
    205  1.1  pgoyette 	error = sdtemp_read_16(sc, SDTEMP_REG_CAPABILITY, &sc->sc_capability);
    206  1.1  pgoyette 	if (error != 0 || (sc->sc_capability & SDTEMP_CAP_HAS_ALARM) == 0) {
    207  1.1  pgoyette 		iic_release_bus(sc->sc_tag, 0);
    208  1.1  pgoyette 		aprint_error_dev(self,
    209  1.1  pgoyette 		    "required alarm capability not present!\n");
    210  1.1  pgoyette 		return;
    211  1.1  pgoyette 	}
    212  1.1  pgoyette 	/* Set the configuration to defaults. */
    213  1.1  pgoyette 	error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, 0);
    214  1.1  pgoyette 	if (error != 0) {
    215  1.1  pgoyette 		iic_release_bus(sc->sc_tag, 0);
    216  1.1  pgoyette 		aprint_error_dev(self, "error %d writing config register\n",
    217  1.1  pgoyette 		    error);
    218  1.1  pgoyette 		return;
    219  1.1  pgoyette 	}
    220  1.1  pgoyette 	/* If variable resolution, set to max */
    221  1.1  pgoyette 	if (sc->sc_resolution < 0) {
    222  1.1  pgoyette 		sc->sc_resolution = ~sc->sc_resolution;
    223  1.1  pgoyette 		error = sdtemp_write_16(sc, SDTEMP_REG_RESOLUTION,
    224  1.1  pgoyette 					sc->sc_resolution & 0x3);
    225  1.1  pgoyette 		if (error != 0) {
    226  1.1  pgoyette 			iic_release_bus(sc->sc_tag, 0);
    227  1.1  pgoyette 			aprint_error_dev(self,
    228  1.1  pgoyette 			    "error %d writing resolution register\n", error);
    229  1.1  pgoyette 			return;
    230  1.1  pgoyette 		} else
    231  1.1  pgoyette 			sc->sc_resolution++;
    232  1.1  pgoyette 	}
    233  1.1  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    234  1.1  pgoyette 
    235  1.1  pgoyette 	/* Hook us into the sysmon_envsys subsystem */
    236  1.1  pgoyette 	sc->sc_sme = sysmon_envsys_create();
    237  1.4  pgoyette 	sc->sc_sme->sme_name = device_xname(self);
    238  1.4  pgoyette 	sc->sc_sme->sme_cookie = sc;
    239  1.4  pgoyette 	sc->sc_sme->sme_refresh = sdtemp_refresh;
    240  1.4  pgoyette 	sc->sc_sme->sme_get_limits = sdtemp_get_limits;
    241  1.4  pgoyette 	sc->sc_sme->sme_set_limits = sdtemp_set_limits;
    242  1.4  pgoyette 
    243  1.1  pgoyette 	sc->sc_sensor = kmem_zalloc(sizeof(envsys_data_t), KM_NOSLEEP);
    244  1.1  pgoyette 	if (!sc->sc_sensor) {
    245  1.1  pgoyette 		aprint_error_dev(self, "unable to allocate sc_sensor\n");
    246  1.1  pgoyette 		goto bad2;
    247  1.1  pgoyette 	}
    248  1.1  pgoyette 
    249  1.1  pgoyette 	/* Initialize sensor data. */
    250  1.1  pgoyette 	sc->sc_sensor->units =  ENVSYS_STEMP;
    251  1.1  pgoyette 	sc->sc_sensor->state = ENVSYS_SINVALID;
    252  1.1  pgoyette 	sc->sc_sensor->flags |= ENVSYS_FMONLIMITS;
    253  1.4  pgoyette 	sc->sc_sensor->monitor = true;
    254  1.1  pgoyette 	(void)strlcpy(sc->sc_sensor->desc, device_xname(self),
    255  1.1  pgoyette 	    sizeof(sc->sc_sensor->desc));
    256  1.1  pgoyette 
    257  1.1  pgoyette 	/* Now attach the sensor */
    258  1.1  pgoyette 	if (sysmon_envsys_sensor_attach(sc->sc_sme, sc->sc_sensor)) {
    259  1.1  pgoyette 		aprint_error_dev(self, "unable to attach sensor\n");
    260  1.1  pgoyette 		goto bad;
    261  1.1  pgoyette 	}
    262  1.1  pgoyette 
    263  1.1  pgoyette 	/* Register the device */
    264  1.1  pgoyette 	error = sysmon_envsys_register(sc->sc_sme);
    265  1.1  pgoyette 	if (error) {
    266  1.1  pgoyette 		aprint_error_dev(self, "error %d registering with sysmon\n",
    267  1.1  pgoyette 		    error);
    268  1.1  pgoyette 		goto bad;
    269  1.1  pgoyette 	}
    270  1.1  pgoyette 
    271  1.1  pgoyette 	if (!pmf_device_register(self, sdtemp_pmf_suspend, sdtemp_pmf_resume))
    272  1.1  pgoyette 		aprint_error_dev(self, "couldn't establish power handler\n");
    273  1.1  pgoyette 
    274  1.1  pgoyette 	/* Retrieve and display hardware monitor limits */
    275  1.6  pgoyette 	sdtemp_get_limits(sc->sc_sme, sc->sc_sensor, &limits);
    276  1.4  pgoyette 	aprint_normal_dev(self, "");
    277  1.1  pgoyette 	i = 0;
    278  1.4  pgoyette 	if (limits.sel_flags & PROP_WARNMIN) {
    279  1.4  pgoyette 		aprint_normal("low limit %dC", __UK2C(limits.sel_warnmin));
    280  1.1  pgoyette 		i++;
    281  1.1  pgoyette 	}
    282  1.4  pgoyette 	if (limits.sel_flags & PROP_WARNMAX) {
    283  1.4  pgoyette 		aprint_normal("%shigh limit %dC ", (i)?", ":"",
    284  1.4  pgoyette 			      __UK2C(limits.sel_warnmax));
    285  1.1  pgoyette 		i++;
    286  1.1  pgoyette 	}
    287  1.4  pgoyette 	if (limits.sel_flags & PROP_CRITMAX) {
    288  1.4  pgoyette 		aprint_normal("%scritical limit %dC ", (i)?", ":"",
    289  1.4  pgoyette 			      __UK2C(limits.sel_critmax));
    290  1.1  pgoyette 		i++;
    291  1.1  pgoyette 	}
    292  1.1  pgoyette 	if (i == 0)
    293  1.1  pgoyette 		aprint_normal("no hardware limits set\n");
    294  1.1  pgoyette 	else
    295  1.1  pgoyette 		aprint_normal("\n");
    296  1.1  pgoyette 
    297  1.1  pgoyette 	return;
    298  1.1  pgoyette 
    299  1.1  pgoyette bad:
    300  1.1  pgoyette 	kmem_free(sc->sc_sensor, sizeof(envsys_data_t));
    301  1.1  pgoyette bad2:
    302  1.1  pgoyette 	sysmon_envsys_destroy(sc->sc_sme);
    303  1.1  pgoyette }
    304  1.1  pgoyette 
    305  1.4  pgoyette /* Retrieve current limits from device, and encode in uKelvins */
    306  1.1  pgoyette static void
    307  1.6  pgoyette sdtemp_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    308  1.6  pgoyette 		  sysmon_envsys_lim_t *limits)
    309  1.1  pgoyette {
    310  1.4  pgoyette 	struct sdtemp_softc *sc = sme->sme_cookie;
    311  1.4  pgoyette 	uint16_t lim;
    312  1.1  pgoyette 
    313  1.4  pgoyette 	limits->sel_flags = PROP_DRIVER_LIMITS;
    314  1.4  pgoyette 	iic_acquire_bus(sc->sc_tag, 0);
    315  1.4  pgoyette 	if (sdtemp_read_16(sc, SDTEMP_REG_LOWER_LIM, &lim) == 0 && lim != 0) {
    316  1.4  pgoyette 		limits->sel_warnmin = sdtemp_decode_temp(sc, lim);
    317  1.4  pgoyette 		limits->sel_flags |= PROP_WARNMIN;
    318  1.4  pgoyette 	}
    319  1.4  pgoyette 	if (sdtemp_read_16(sc, SDTEMP_REG_UPPER_LIM, &lim) == 0 && lim != 0) {
    320  1.4  pgoyette 		limits->sel_warnmax = sdtemp_decode_temp(sc, lim);
    321  1.4  pgoyette 		limits->sel_flags |= PROP_WARNMAX;
    322  1.4  pgoyette 	}
    323  1.4  pgoyette 	if (sdtemp_read_16(sc, SDTEMP_REG_CRIT_LIM, &lim) == 0 && lim != 0) {
    324  1.4  pgoyette 		limits->sel_critmax = sdtemp_decode_temp(sc, lim);
    325  1.4  pgoyette 		limits->sel_flags |= PROP_CRITMAX;
    326  1.1  pgoyette 	}
    327  1.4  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    328  1.4  pgoyette }
    329  1.4  pgoyette 
    330  1.4  pgoyette /* Send current limit values to the device */
    331  1.4  pgoyette static void
    332  1.6  pgoyette sdtemp_set_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    333  1.6  pgoyette 		  sysmon_envsys_lim_t *limits)
    334  1.4  pgoyette {
    335  1.4  pgoyette 	uint16_t val;
    336  1.4  pgoyette 	struct sdtemp_softc *sc = sme->sme_cookie;
    337  1.1  pgoyette 
    338  1.1  pgoyette 	iic_acquire_bus(sc->sc_tag, 0);
    339  1.4  pgoyette 	if (limits->sel_flags & PROP_WARNMIN) {
    340  1.4  pgoyette 		val = __UK2C(limits->sel_warnmin);
    341  1.4  pgoyette 		(void)sdtemp_write_16(sc, SDTEMP_REG_LOWER_LIM,
    342  1.4  pgoyette 					(val << 4) & SDTEMP_TEMP_MASK);
    343  1.4  pgoyette 	}
    344  1.4  pgoyette 	if (limits->sel_flags & PROP_WARNMAX) {
    345  1.4  pgoyette 		val = __UK2C(limits->sel_warnmax);
    346  1.4  pgoyette 		(void)sdtemp_write_16(sc, SDTEMP_REG_UPPER_LIM,
    347  1.4  pgoyette 					(val << 4) & SDTEMP_TEMP_MASK);
    348  1.4  pgoyette 	}
    349  1.4  pgoyette 	if (limits->sel_flags & PROP_CRITMAX) {
    350  1.4  pgoyette 		val = __UK2C(limits->sel_critmax);
    351  1.4  pgoyette 		(void)sdtemp_write_16(sc, SDTEMP_REG_CRIT_LIM,
    352  1.4  pgoyette 					(val << 4) & SDTEMP_TEMP_MASK);
    353  1.4  pgoyette 	}
    354  1.1  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    355  1.1  pgoyette 
    356  1.4  pgoyette 	/*
    357  1.4  pgoyette 	 * If at least one limit is set that we can handle, and no
    358  1.4  pgoyette 	 * limits are set that we cannot handle, tell sysmon that
    359  1.4  pgoyette 	 * the driver will take care of monitoring the limits!
    360  1.4  pgoyette 	 */
    361  1.4  pgoyette 	if (limits->sel_flags & (PROP_CRITMIN | PROP_BATTCAP | PROP_BATTWARN))
    362  1.4  pgoyette 		limits->sel_flags &= ~PROP_DRIVER_LIMITS;
    363  1.4  pgoyette 	else if (limits->sel_flags & PROP_LIMITS)
    364  1.4  pgoyette 		limits->sel_flags |= PROP_DRIVER_LIMITS;
    365  1.4  pgoyette 	else
    366  1.4  pgoyette 		limits->sel_flags &= ~PROP_DRIVER_LIMITS;
    367  1.1  pgoyette }
    368  1.1  pgoyette 
    369  1.1  pgoyette #ifdef NOT_YET	/* All registers on these sensors are 16-bits */
    370  1.1  pgoyette 
    371  1.1  pgoyette /* Read a 8-bit value from a register */
    372  1.1  pgoyette static int
    373  1.1  pgoyette sdtemp_read_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t *valp)
    374  1.1  pgoyette {
    375  1.1  pgoyette 	int error;
    376  1.1  pgoyette 
    377  1.1  pgoyette 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    378  1.1  pgoyette 	    sc->sc_address, &reg, 1, valp, sizeof(*valp), 0);
    379  1.1  pgoyette 
    380  1.1  pgoyette 	return error;
    381  1.1  pgoyette }
    382  1.1  pgoyette 
    383  1.1  pgoyette static int
    384  1.1  pgoyette sdtemp_write_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t val)
    385  1.1  pgoyette {
    386  1.1  pgoyette 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    387  1.1  pgoyette 	    sc->sc_address, &reg, 1, &val, sizeof(val), 0);
    388  1.1  pgoyette }
    389  1.1  pgoyette #endif /* NOT_YET */
    390  1.1  pgoyette 
    391  1.1  pgoyette /* Read a 16-bit value from a register */
    392  1.1  pgoyette static int
    393  1.1  pgoyette sdtemp_read_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t *valp)
    394  1.1  pgoyette {
    395  1.1  pgoyette 	int error;
    396  1.1  pgoyette 
    397  1.1  pgoyette 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    398  1.1  pgoyette 	    sc->sc_address, &reg, 1, valp, sizeof(*valp), 0);
    399  1.1  pgoyette 	if (error)
    400  1.1  pgoyette 		return error;
    401  1.1  pgoyette 
    402  1.1  pgoyette 	*valp = be16toh(*valp);
    403  1.1  pgoyette 
    404  1.1  pgoyette 	return 0;
    405  1.1  pgoyette }
    406  1.1  pgoyette 
    407  1.1  pgoyette static int
    408  1.1  pgoyette sdtemp_write_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t val)
    409  1.1  pgoyette {
    410  1.1  pgoyette 	uint16_t temp;
    411  1.1  pgoyette 
    412  1.1  pgoyette 	temp = htobe16(val);
    413  1.1  pgoyette 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    414  1.1  pgoyette 	    sc->sc_address, &reg, 1, &temp, sizeof(temp), 0);
    415  1.1  pgoyette }
    416  1.1  pgoyette 
    417  1.1  pgoyette static uint32_t
    418  1.1  pgoyette sdtemp_decode_temp(struct sdtemp_softc *sc, uint16_t temp)
    419  1.1  pgoyette {
    420  1.1  pgoyette 	uint32_t val;
    421  1.1  pgoyette 	int32_t stemp;
    422  1.1  pgoyette 
    423  1.1  pgoyette 	/* Get only the temperature bits */
    424  1.1  pgoyette 	temp &= SDTEMP_TEMP_MASK;
    425  1.1  pgoyette 
    426  1.1  pgoyette 	/* If necessary, extend the sign bit */
    427  1.1  pgoyette 	if ((sc->sc_capability & SDTEMP_CAP_WIDER_RANGE) &&
    428  1.1  pgoyette 	    (temp & SDTEMP_TEMP_NEGATIVE))
    429  1.1  pgoyette 		temp |= SDTEMP_TEMP_SIGN_EXT;
    430  1.1  pgoyette 
    431  1.1  pgoyette 	/* Mask off only bits valid within current resolution */
    432  1.1  pgoyette 	temp &= ~(0xf >> sc->sc_resolution);
    433  1.1  pgoyette 
    434  1.1  pgoyette 	/* Treat as signed and extend to 32-bits */
    435  1.1  pgoyette 	stemp = (int16_t)temp;
    436  1.1  pgoyette 
    437  1.1  pgoyette 	/* Now convert from 0.0625 (1/16) deg C increments to microKelvins */
    438  1.1  pgoyette 	val = (stemp * 62500) + 273150000;
    439  1.1  pgoyette 
    440  1.1  pgoyette 	return val;
    441  1.1  pgoyette }
    442  1.1  pgoyette 
    443  1.1  pgoyette static void
    444  1.1  pgoyette sdtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    445  1.1  pgoyette {
    446  1.1  pgoyette 	struct sdtemp_softc *sc = sme->sme_cookie;
    447  1.1  pgoyette 	uint16_t val;
    448  1.1  pgoyette 	int error;
    449  1.1  pgoyette 
    450  1.1  pgoyette 	iic_acquire_bus(sc->sc_tag, 0);
    451  1.1  pgoyette 	error = sdtemp_read_16(sc, SDTEMP_REG_AMBIENT_TEMP, &val);
    452  1.1  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    453  1.1  pgoyette 
    454  1.1  pgoyette 	if (error) {
    455  1.1  pgoyette 		edata->state = ENVSYS_SINVALID;
    456  1.1  pgoyette 		return;
    457  1.1  pgoyette 	}
    458  1.1  pgoyette 
    459  1.1  pgoyette 	edata->value_cur = sdtemp_decode_temp(sc, val);
    460  1.1  pgoyette 
    461  1.1  pgoyette 	/* Now check for limits */
    462  1.4  pgoyette 	if ((edata->upropset & PROP_DRIVER_LIMITS) == 0)
    463  1.4  pgoyette 		edata->state = ENVSYS_SVALID;
    464  1.4  pgoyette 	else if (val & SDTEMP_ABOVE_CRIT)
    465  1.1  pgoyette 		edata->state = ENVSYS_SCRITOVER;
    466  1.1  pgoyette 	else if (val & SDTEMP_ABOVE_UPPER)
    467  1.1  pgoyette 		edata->state = ENVSYS_SWARNOVER;
    468  1.1  pgoyette 	else if (val & SDTEMP_BELOW_LOWER)
    469  1.1  pgoyette 		edata->state = ENVSYS_SWARNUNDER;
    470  1.1  pgoyette 	else
    471  1.1  pgoyette 		edata->state = ENVSYS_SVALID;
    472  1.1  pgoyette }
    473  1.1  pgoyette 
    474  1.1  pgoyette /*
    475  1.1  pgoyette  * power management functions
    476  1.1  pgoyette  *
    477  1.1  pgoyette  * We go into "shutdown" mode at suspend time, and return to normal
    478  1.1  pgoyette  * mode upon resume.  This reduces power consumption by disabling
    479  1.1  pgoyette  * the A/D converter.
    480  1.1  pgoyette  */
    481  1.1  pgoyette 
    482  1.1  pgoyette static bool
    483  1.1  pgoyette sdtemp_pmf_suspend(device_t dev PMF_FN_ARGS)
    484  1.1  pgoyette {
    485  1.1  pgoyette 	struct sdtemp_softc *sc = device_private(dev);
    486  1.1  pgoyette 	int error;
    487  1.1  pgoyette 	uint16_t config;
    488  1.1  pgoyette 
    489  1.1  pgoyette 	iic_acquire_bus(sc->sc_tag, 0);
    490  1.1  pgoyette 	error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
    491  1.1  pgoyette 	if (error == 0) {
    492  1.1  pgoyette 		config |= SDTEMP_CONFIG_SHUTDOWN_MODE;
    493  1.1  pgoyette 		error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
    494  1.1  pgoyette 	}
    495  1.1  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    496  1.1  pgoyette 	return (error == 0);
    497  1.1  pgoyette }
    498  1.1  pgoyette 
    499  1.1  pgoyette static bool
    500  1.1  pgoyette sdtemp_pmf_resume(device_t dev PMF_FN_ARGS)
    501  1.1  pgoyette {
    502  1.1  pgoyette 	struct sdtemp_softc *sc = device_private(dev);
    503  1.1  pgoyette 	int error;
    504  1.1  pgoyette 	uint16_t config;
    505  1.1  pgoyette 
    506  1.1  pgoyette 	iic_acquire_bus(sc->sc_tag, 0);
    507  1.1  pgoyette 	error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
    508  1.1  pgoyette 	if (error == 0) {
    509  1.1  pgoyette 		config &= ~SDTEMP_CONFIG_SHUTDOWN_MODE;
    510  1.1  pgoyette 		error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
    511  1.1  pgoyette 	}
    512  1.1  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    513  1.1  pgoyette 	return (error == 0);
    514  1.1  pgoyette }
    515