Home | History | Annotate | Line # | Download | only in i2c
sdtemp.c revision 1.23.6.2
      1  1.23.6.2     skrll /*      $NetBSD: sdtemp.c,v 1.23.6.2 2015/06/06 14:40:07 skrll 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.23.6.2     skrll __KERNEL_RCSID(0, "$NetBSD: sdtemp.c,v 1.23.6.2 2015/06/06 14:40:07 skrll 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.19  jmcneill #include <sys/module.h>
     42       1.1  pgoyette 
     43       1.1  pgoyette #include <dev/sysmon/sysmonvar.h>
     44       1.1  pgoyette 
     45       1.1  pgoyette #include <dev/i2c/i2cvar.h>
     46       1.1  pgoyette #include <dev/i2c/sdtemp_reg.h>
     47       1.1  pgoyette 
     48       1.1  pgoyette struct sdtemp_softc {
     49       1.1  pgoyette 	device_t sc_dev;
     50       1.1  pgoyette 	i2c_tag_t sc_tag;
     51       1.1  pgoyette 	int sc_address;
     52       1.1  pgoyette 
     53       1.1  pgoyette 	struct sysmon_envsys *sc_sme;
     54       1.1  pgoyette 	envsys_data_t *sc_sensor;
     55      1.13  pgoyette 	sysmon_envsys_lim_t sc_deflims;
     56      1.13  pgoyette 	uint32_t sc_defprops;
     57       1.1  pgoyette 	int sc_resolution;
     58       1.1  pgoyette 	uint16_t sc_capability;
     59       1.1  pgoyette };
     60       1.1  pgoyette 
     61       1.1  pgoyette static int  sdtemp_match(device_t, cfdata_t, void *);
     62       1.1  pgoyette static void sdtemp_attach(device_t, device_t, void *);
     63      1.19  jmcneill static int  sdtemp_detach(device_t, int);
     64       1.1  pgoyette 
     65       1.1  pgoyette CFATTACH_DECL_NEW(sdtemp, sizeof(struct sdtemp_softc),
     66      1.19  jmcneill 	sdtemp_match, sdtemp_attach, sdtemp_detach, NULL);
     67       1.1  pgoyette 
     68       1.1  pgoyette static void	sdtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
     69       1.6  pgoyette static void	sdtemp_get_limits(struct sysmon_envsys *, envsys_data_t *,
     70      1.10  pgoyette 				  sysmon_envsys_lim_t *, uint32_t *);
     71       1.6  pgoyette static void	sdtemp_set_limits(struct sysmon_envsys *, envsys_data_t *,
     72      1.10  pgoyette 				  sysmon_envsys_lim_t *, uint32_t *);
     73       1.1  pgoyette #ifdef NOT_YET
     74       1.1  pgoyette static int	sdtemp_read_8(struct sdtemp_softc *, uint8_t, uint8_t *);
     75       1.1  pgoyette static int	sdtemp_write_8(struct sdtemp_softc *, uint8_t, uint8_t);
     76       1.1  pgoyette #endif /* NOT YET */
     77       1.1  pgoyette static int	sdtemp_read_16(struct sdtemp_softc *, uint8_t, uint16_t *);
     78       1.1  pgoyette static int	sdtemp_write_16(struct sdtemp_softc *, uint8_t, uint16_t);
     79       1.1  pgoyette static uint32_t	sdtemp_decode_temp(struct sdtemp_softc *, uint16_t);
     80      1.11    dyoung static bool	sdtemp_pmf_suspend(device_t, const pmf_qual_t *);
     81      1.11    dyoung static bool	sdtemp_pmf_resume(device_t, const pmf_qual_t *);
     82       1.1  pgoyette 
     83       1.1  pgoyette struct sdtemp_dev_entry {
     84       1.1  pgoyette 	const uint16_t sdtemp_mfg_id;
     85      1.14  pgoyette 	const uint16_t  sdtemp_devrev;
     86      1.14  pgoyette 	const uint16_t  sdtemp_mask;
     87       1.1  pgoyette 	const uint8_t  sdtemp_resolution;
     88       1.1  pgoyette 	const char    *sdtemp_desc;
     89       1.1  pgoyette };
     90       1.1  pgoyette 
     91       1.4  pgoyette /* Convert sysmon_envsys uKelvin value to simple degC */
     92       1.4  pgoyette 
     93       1.4  pgoyette #define	__UK2C(uk) (((uk) - 273150000) / 1000000)
     94       1.1  pgoyette 
     95       1.1  pgoyette /*
     96       1.1  pgoyette  * List of devices known to conform to JEDEC JC42.4
     97       1.1  pgoyette  *
     98       1.1  pgoyette  * NOTE: A non-negative value for resolution indicates that the sensor
     99       1.1  pgoyette  * resolution is fixed at that number of fractional bits;  a negative
    100       1.1  pgoyette  * value indicates that the sensor needs to be configured.  In either
    101       1.1  pgoyette  * case, trip-point registers are fixed at two-bit (0.25C) resolution.
    102       1.1  pgoyette  */
    103       1.1  pgoyette static const struct sdtemp_dev_entry
    104       1.1  pgoyette sdtemp_dev_table[] = {
    105      1.14  pgoyette     { MAXIM_MANUFACTURER_ID, MAX_6604_DEVICE_ID,    MAX_6604_MASK,   3,
    106      1.16  pgoyette 	"Maxim MAX6604" },
    107      1.14  pgoyette     { MCP_MANUFACTURER_ID,   MCP_9805_DEVICE_ID,    MCP_9805_MASK,   2,
    108      1.14  pgoyette 	"Microchip Tech MCP9805/MCP9843" },
    109      1.14  pgoyette     { MCP_MANUFACTURER_ID,   MCP_98243_DEVICE_ID,   MCP_98243_MASK, -4,
    110      1.14  pgoyette 	"Microchip Tech MCP98243" },
    111      1.14  pgoyette     { MCP_MANUFACTURER_ID,   MCP_98242_DEVICE_ID,   MCP_98242_MASK, -4,
    112       1.1  pgoyette 	"Microchip Tech MCP98242" },
    113      1.14  pgoyette     { ADT_MANUFACTURER_ID,   ADT_7408_DEVICE_ID,    ADT_7408_MASK,   4,
    114       1.1  pgoyette 	"Analog Devices ADT7408" },
    115      1.14  pgoyette     { NXP_MANUFACTURER_ID,   NXP_SE98_DEVICE_ID,    NXP_SE98_MASK,   3,
    116      1.14  pgoyette 	"NXP Semiconductors SE97B/SE98" },
    117      1.14  pgoyette     { NXP_MANUFACTURER_ID,   NXP_SE97_DEVICE_ID,    NXP_SE97_MASK,   3,
    118      1.14  pgoyette 	"NXP Semiconductors SE97" },
    119      1.14  pgoyette     { STTS_MANUFACTURER_ID,  STTS_424E_DEVICE_ID,   STTS_424E_MASK,  2,
    120  1.23.6.2     skrll 	"STmicroelectronics STTS424E" },
    121      1.14  pgoyette     { STTS_MANUFACTURER_ID,  STTS_424_DEVICE_ID,    STTS_424_MASK,   2,
    122  1.23.6.2     skrll 	"STmicroelectronics STTS424" },
    123      1.23   mlelstv     { STTS_MANUFACTURER_ID,  STTS_2002_DEVICE_ID,    STTS_2002_MASK,   2,
    124  1.23.6.2     skrll 	"STmicroelectronics STTS2002" },
    125      1.23   mlelstv     { STTS_MANUFACTURER_ID,  STTS_2004_DEVICE_ID,    STTS_2004_MASK,   2,
    126  1.23.6.2     skrll 	"STmicroelectronics STTS2002" },
    127      1.23   mlelstv     { STTS_MANUFACTURER_ID,  STTS_3000_DEVICE_ID,    STTS_3000_MASK,   2,
    128  1.23.6.2     skrll 	"STmicroelectronics STTS3000" },
    129      1.14  pgoyette     { CAT_MANUFACTURER_ID,   CAT_34TS02_DEVICE_ID,  CAT_34TS02_MASK, 4,
    130       1.1  pgoyette 	"Catalyst CAT34TS02/CAT6095" },
    131  1.23.6.2     skrll     { CAT_MANUFACTURER_ID,   CAT_34TS02C_DEVICE_ID,  CAT_34TS02C_MASK, 4,
    132  1.23.6.2     skrll 	"Catalyst CAT34TS02C" },
    133      1.15  pgoyette     { IDT_MANUFACTURER_ID,   IDT_TS3000B3_DEVICE_ID, IDT_TS3000B3_MASK, 4,
    134      1.15  pgoyette 	"Integrated Device Technology TS3000B3/TSE2002B3" },
    135       1.1  pgoyette     { 0, 0, 0, 2, "Unknown" }
    136       1.1  pgoyette };
    137       1.1  pgoyette 
    138       1.1  pgoyette static int
    139      1.14  pgoyette sdtemp_lookup(uint16_t mfg, uint16_t devrev)
    140       1.1  pgoyette {
    141       1.1  pgoyette 	int i;
    142       1.1  pgoyette 
    143      1.14  pgoyette 	for (i = 0; sdtemp_dev_table[i].sdtemp_mfg_id; i++) {
    144      1.14  pgoyette 		if (mfg != sdtemp_dev_table[i].sdtemp_mfg_id)
    145      1.14  pgoyette 			continue;
    146      1.14  pgoyette 		if ((devrev & sdtemp_dev_table[i].sdtemp_mask) ==
    147      1.14  pgoyette 		    sdtemp_dev_table[i].sdtemp_devrev)
    148       1.1  pgoyette 			break;
    149      1.14  pgoyette 	}
    150       1.1  pgoyette 
    151       1.1  pgoyette 	return i;
    152       1.1  pgoyette }
    153       1.1  pgoyette 
    154       1.1  pgoyette static int
    155       1.1  pgoyette sdtemp_match(device_t parent, cfdata_t cf, void *aux)
    156       1.1  pgoyette {
    157       1.1  pgoyette 	struct i2c_attach_args *ia = aux;
    158       1.1  pgoyette 	uint16_t mfgid, devid;
    159       1.1  pgoyette 	struct sdtemp_softc sc;
    160       1.1  pgoyette 	int i, error;
    161       1.1  pgoyette 
    162       1.1  pgoyette 	sc.sc_tag = ia->ia_tag;
    163       1.1  pgoyette 	sc.sc_address = ia->ia_addr;
    164       1.1  pgoyette 
    165       1.1  pgoyette 	if ((ia->ia_addr & SDTEMP_ADDRMASK) != SDTEMP_ADDR)
    166       1.1  pgoyette 		return 0;
    167       1.1  pgoyette 
    168       1.1  pgoyette 	/* Verify that we can read the manufacturer ID  & Device ID */
    169       1.1  pgoyette 	iic_acquire_bus(sc.sc_tag, 0);
    170       1.1  pgoyette 	error = sdtemp_read_16(&sc, SDTEMP_REG_MFG_ID,  &mfgid) |
    171       1.1  pgoyette 		sdtemp_read_16(&sc, SDTEMP_REG_DEV_REV, &devid);
    172       1.1  pgoyette 	iic_release_bus(sc.sc_tag, 0);
    173       1.1  pgoyette 
    174       1.1  pgoyette 	if (error)
    175       1.1  pgoyette 		return 0;
    176       1.1  pgoyette 
    177      1.14  pgoyette 	i = sdtemp_lookup(mfgid, devid);
    178       1.1  pgoyette 	if (sdtemp_dev_table[i].sdtemp_mfg_id == 0) {
    179       1.1  pgoyette 		aprint_debug("sdtemp: No match for mfg 0x%04x dev 0x%02x "
    180       1.1  pgoyette 		    "rev 0x%02x at address 0x%02x\n", mfgid, devid >> 8,
    181       1.1  pgoyette 		    devid & 0xff, sc.sc_address);
    182       1.1  pgoyette 		return 0;
    183       1.1  pgoyette 	}
    184       1.1  pgoyette 
    185       1.1  pgoyette 	return 1;
    186       1.1  pgoyette }
    187       1.1  pgoyette 
    188       1.1  pgoyette static void
    189       1.1  pgoyette sdtemp_attach(device_t parent, device_t self, void *aux)
    190       1.1  pgoyette {
    191       1.1  pgoyette 	struct sdtemp_softc *sc = device_private(self);
    192       1.1  pgoyette 	struct i2c_attach_args *ia = aux;
    193       1.1  pgoyette 	uint16_t mfgid, devid;
    194       1.1  pgoyette 	int i, error;
    195       1.1  pgoyette 
    196       1.1  pgoyette 	sc->sc_tag = ia->ia_tag;
    197       1.1  pgoyette 	sc->sc_address = ia->ia_addr;
    198       1.1  pgoyette 	sc->sc_dev = self;
    199       1.1  pgoyette 
    200       1.1  pgoyette 	iic_acquire_bus(sc->sc_tag, 0);
    201       1.1  pgoyette 	if ((error = sdtemp_read_16(sc, SDTEMP_REG_MFG_ID,  &mfgid)) != 0 ||
    202       1.1  pgoyette 	    (error = sdtemp_read_16(sc, SDTEMP_REG_DEV_REV, &devid)) != 0) {
    203       1.5  pgoyette 		iic_release_bus(sc->sc_tag, 0);
    204       1.1  pgoyette 		aprint_error(": attach error %d\n", error);
    205       1.1  pgoyette 		return;
    206       1.1  pgoyette 	}
    207      1.14  pgoyette 	i = sdtemp_lookup(mfgid, devid);
    208       1.1  pgoyette 	sc->sc_resolution =
    209       1.1  pgoyette 	    sdtemp_dev_table[i].sdtemp_resolution;
    210       1.1  pgoyette 
    211       1.1  pgoyette 	aprint_naive(": Temp Sensor\n");
    212       1.1  pgoyette 	aprint_normal(": %s Temp Sensor\n", sdtemp_dev_table[i].sdtemp_desc);
    213       1.1  pgoyette 
    214       1.1  pgoyette 	if (sdtemp_dev_table[i].sdtemp_mfg_id == 0)
    215       1.1  pgoyette 		aprint_debug_dev(self,
    216       1.1  pgoyette 		    "mfg 0x%04x dev 0x%02x rev 0x%02x at addr 0x%02x\n",
    217       1.1  pgoyette 		    mfgid, devid >> 8, devid & 0xff, ia->ia_addr);
    218       1.1  pgoyette 
    219       1.1  pgoyette 	/*
    220       1.1  pgoyette 	 * Alarm capability is required;  if not present, this is likely
    221       1.1  pgoyette 	 * not a real sdtemp device.
    222       1.1  pgoyette 	 */
    223       1.1  pgoyette 	error = sdtemp_read_16(sc, SDTEMP_REG_CAPABILITY, &sc->sc_capability);
    224       1.1  pgoyette 	if (error != 0 || (sc->sc_capability & SDTEMP_CAP_HAS_ALARM) == 0) {
    225       1.1  pgoyette 		iic_release_bus(sc->sc_tag, 0);
    226       1.1  pgoyette 		aprint_error_dev(self,
    227       1.1  pgoyette 		    "required alarm capability not present!\n");
    228       1.1  pgoyette 		return;
    229       1.1  pgoyette 	}
    230       1.1  pgoyette 	/* Set the configuration to defaults. */
    231       1.1  pgoyette 	error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, 0);
    232       1.1  pgoyette 	if (error != 0) {
    233       1.1  pgoyette 		iic_release_bus(sc->sc_tag, 0);
    234       1.1  pgoyette 		aprint_error_dev(self, "error %d writing config register\n",
    235       1.1  pgoyette 		    error);
    236       1.1  pgoyette 		return;
    237       1.1  pgoyette 	}
    238       1.1  pgoyette 	/* If variable resolution, set to max */
    239       1.1  pgoyette 	if (sc->sc_resolution < 0) {
    240       1.1  pgoyette 		sc->sc_resolution = ~sc->sc_resolution;
    241       1.1  pgoyette 		error = sdtemp_write_16(sc, SDTEMP_REG_RESOLUTION,
    242       1.1  pgoyette 					sc->sc_resolution & 0x3);
    243       1.1  pgoyette 		if (error != 0) {
    244       1.1  pgoyette 			iic_release_bus(sc->sc_tag, 0);
    245       1.1  pgoyette 			aprint_error_dev(self,
    246       1.1  pgoyette 			    "error %d writing resolution register\n", error);
    247       1.1  pgoyette 			return;
    248       1.1  pgoyette 		} else
    249       1.1  pgoyette 			sc->sc_resolution++;
    250       1.1  pgoyette 	}
    251       1.1  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    252       1.1  pgoyette 
    253       1.1  pgoyette 	/* Hook us into the sysmon_envsys subsystem */
    254       1.1  pgoyette 	sc->sc_sme = sysmon_envsys_create();
    255       1.4  pgoyette 	sc->sc_sme->sme_name = device_xname(self);
    256       1.4  pgoyette 	sc->sc_sme->sme_cookie = sc;
    257       1.4  pgoyette 	sc->sc_sme->sme_refresh = sdtemp_refresh;
    258       1.4  pgoyette 	sc->sc_sme->sme_get_limits = sdtemp_get_limits;
    259       1.4  pgoyette 	sc->sc_sme->sme_set_limits = sdtemp_set_limits;
    260       1.4  pgoyette 
    261       1.1  pgoyette 	sc->sc_sensor = kmem_zalloc(sizeof(envsys_data_t), KM_NOSLEEP);
    262       1.1  pgoyette 	if (!sc->sc_sensor) {
    263       1.1  pgoyette 		aprint_error_dev(self, "unable to allocate sc_sensor\n");
    264       1.1  pgoyette 		goto bad2;
    265       1.1  pgoyette 	}
    266       1.1  pgoyette 
    267       1.1  pgoyette 	/* Initialize sensor data. */
    268       1.1  pgoyette 	sc->sc_sensor->units =  ENVSYS_STEMP;
    269       1.1  pgoyette 	sc->sc_sensor->state = ENVSYS_SINVALID;
    270       1.1  pgoyette 	sc->sc_sensor->flags |= ENVSYS_FMONLIMITS;
    271       1.1  pgoyette 	(void)strlcpy(sc->sc_sensor->desc, device_xname(self),
    272       1.1  pgoyette 	    sizeof(sc->sc_sensor->desc));
    273      1.22     soren 	snprintf(sc->sc_sensor->desc, sizeof(sc->sc_sensor->desc),
    274      1.22     soren 	    "DIMM %d temperature", sc->sc_address - SDTEMP_ADDR);
    275       1.1  pgoyette 
    276       1.1  pgoyette 	/* Now attach the sensor */
    277       1.1  pgoyette 	if (sysmon_envsys_sensor_attach(sc->sc_sme, sc->sc_sensor)) {
    278       1.1  pgoyette 		aprint_error_dev(self, "unable to attach sensor\n");
    279       1.1  pgoyette 		goto bad;
    280       1.1  pgoyette 	}
    281       1.1  pgoyette 
    282       1.1  pgoyette 	/* Register the device */
    283       1.1  pgoyette 	error = sysmon_envsys_register(sc->sc_sme);
    284       1.1  pgoyette 	if (error) {
    285       1.1  pgoyette 		aprint_error_dev(self, "error %d registering with sysmon\n",
    286       1.1  pgoyette 		    error);
    287       1.1  pgoyette 		goto bad;
    288       1.1  pgoyette 	}
    289       1.1  pgoyette 
    290       1.1  pgoyette 	if (!pmf_device_register(self, sdtemp_pmf_suspend, sdtemp_pmf_resume))
    291       1.1  pgoyette 		aprint_error_dev(self, "couldn't establish power handler\n");
    292       1.1  pgoyette 
    293       1.1  pgoyette 	/* Retrieve and display hardware monitor limits */
    294      1.17     njoly 	sdtemp_get_limits(sc->sc_sme, sc->sc_sensor, &sc->sc_deflims,
    295      1.17     njoly 	    &sc->sc_defprops);
    296      1.21  pgoyette 	aprint_normal_dev(self, "Hardware limits: ");
    297       1.1  pgoyette 	i = 0;
    298      1.17     njoly 	if (sc->sc_defprops & PROP_WARNMIN) {
    299      1.21  pgoyette 		aprint_normal("low %dC",
    300      1.17     njoly 		              __UK2C(sc->sc_deflims.sel_warnmin));
    301       1.1  pgoyette 		i++;
    302       1.1  pgoyette 	}
    303      1.17     njoly 	if (sc->sc_defprops & PROP_WARNMAX) {
    304      1.21  pgoyette 		aprint_normal("%shigh %dC ", (i)?", ":"",
    305      1.17     njoly 			      __UK2C(sc->sc_deflims.sel_warnmax));
    306       1.1  pgoyette 		i++;
    307       1.1  pgoyette 	}
    308      1.17     njoly 	if (sc->sc_defprops & PROP_CRITMAX) {
    309      1.21  pgoyette 		aprint_normal("%scritical %dC ", (i)?", ":"",
    310      1.17     njoly 			      __UK2C(sc->sc_deflims.sel_critmax));
    311       1.1  pgoyette 		i++;
    312       1.1  pgoyette 	}
    313      1.21  pgoyette 	aprint_normal("%s\n", (i)?"":"none set");
    314       1.1  pgoyette 
    315       1.1  pgoyette 	return;
    316       1.1  pgoyette 
    317       1.1  pgoyette bad:
    318       1.1  pgoyette 	kmem_free(sc->sc_sensor, sizeof(envsys_data_t));
    319       1.1  pgoyette bad2:
    320       1.1  pgoyette 	sysmon_envsys_destroy(sc->sc_sme);
    321       1.1  pgoyette }
    322       1.1  pgoyette 
    323      1.19  jmcneill static int
    324      1.19  jmcneill sdtemp_detach(device_t self, int flags)
    325      1.19  jmcneill {
    326      1.19  jmcneill 	struct sdtemp_softc *sc = device_private(self);
    327      1.19  jmcneill 
    328      1.19  jmcneill 	pmf_device_deregister(self);
    329      1.19  jmcneill 
    330      1.19  jmcneill 	if (sc->sc_sme)
    331      1.19  jmcneill 		sysmon_envsys_unregister(sc->sc_sme);
    332      1.19  jmcneill 	if (sc->sc_sensor)
    333      1.19  jmcneill 		kmem_free(sc->sc_sensor, sizeof(envsys_data_t));
    334      1.19  jmcneill 
    335      1.19  jmcneill 	return 0;
    336      1.19  jmcneill }
    337      1.19  jmcneill 
    338       1.4  pgoyette /* Retrieve current limits from device, and encode in uKelvins */
    339       1.1  pgoyette static void
    340       1.6  pgoyette sdtemp_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    341      1.10  pgoyette 		  sysmon_envsys_lim_t *limits, uint32_t *props)
    342       1.1  pgoyette {
    343       1.4  pgoyette 	struct sdtemp_softc *sc = sme->sme_cookie;
    344       1.4  pgoyette 	uint16_t lim;
    345       1.1  pgoyette 
    346      1.10  pgoyette 	*props = 0;
    347       1.4  pgoyette 	iic_acquire_bus(sc->sc_tag, 0);
    348       1.4  pgoyette 	if (sdtemp_read_16(sc, SDTEMP_REG_LOWER_LIM, &lim) == 0 && lim != 0) {
    349       1.4  pgoyette 		limits->sel_warnmin = sdtemp_decode_temp(sc, lim);
    350      1.10  pgoyette 		*props |= PROP_WARNMIN;
    351       1.4  pgoyette 	}
    352       1.4  pgoyette 	if (sdtemp_read_16(sc, SDTEMP_REG_UPPER_LIM, &lim) == 0 && lim != 0) {
    353       1.4  pgoyette 		limits->sel_warnmax = sdtemp_decode_temp(sc, lim);
    354      1.10  pgoyette 		*props |= PROP_WARNMAX;
    355       1.4  pgoyette 	}
    356       1.4  pgoyette 	if (sdtemp_read_16(sc, SDTEMP_REG_CRIT_LIM, &lim) == 0 && lim != 0) {
    357       1.4  pgoyette 		limits->sel_critmax = sdtemp_decode_temp(sc, lim);
    358      1.10  pgoyette 		*props |= PROP_CRITMAX;
    359       1.1  pgoyette 	}
    360       1.4  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    361      1.10  pgoyette 	if (*props != 0)
    362      1.10  pgoyette 		*props |= PROP_DRIVER_LIMITS;
    363       1.4  pgoyette }
    364       1.4  pgoyette 
    365       1.4  pgoyette /* Send current limit values to the device */
    366       1.4  pgoyette static void
    367       1.6  pgoyette sdtemp_set_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    368      1.10  pgoyette 		  sysmon_envsys_lim_t *limits, uint32_t *props)
    369       1.4  pgoyette {
    370       1.4  pgoyette 	uint16_t val;
    371       1.4  pgoyette 	struct sdtemp_softc *sc = sme->sme_cookie;
    372       1.1  pgoyette 
    373      1.13  pgoyette 	if (limits == NULL) {
    374      1.13  pgoyette 		limits = &sc->sc_deflims;
    375      1.13  pgoyette 		props  = &sc->sc_defprops;
    376      1.13  pgoyette 	}
    377       1.1  pgoyette 	iic_acquire_bus(sc->sc_tag, 0);
    378      1.10  pgoyette 	if (*props & PROP_WARNMIN) {
    379       1.4  pgoyette 		val = __UK2C(limits->sel_warnmin);
    380       1.4  pgoyette 		(void)sdtemp_write_16(sc, SDTEMP_REG_LOWER_LIM,
    381       1.4  pgoyette 					(val << 4) & SDTEMP_TEMP_MASK);
    382       1.4  pgoyette 	}
    383      1.10  pgoyette 	if (*props & PROP_WARNMAX) {
    384       1.4  pgoyette 		val = __UK2C(limits->sel_warnmax);
    385       1.4  pgoyette 		(void)sdtemp_write_16(sc, SDTEMP_REG_UPPER_LIM,
    386       1.4  pgoyette 					(val << 4) & SDTEMP_TEMP_MASK);
    387       1.4  pgoyette 	}
    388      1.10  pgoyette 	if (*props & PROP_CRITMAX) {
    389       1.4  pgoyette 		val = __UK2C(limits->sel_critmax);
    390       1.4  pgoyette 		(void)sdtemp_write_16(sc, SDTEMP_REG_CRIT_LIM,
    391       1.4  pgoyette 					(val << 4) & SDTEMP_TEMP_MASK);
    392       1.4  pgoyette 	}
    393       1.1  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    394       1.1  pgoyette 
    395       1.4  pgoyette 	/*
    396       1.4  pgoyette 	 * If at least one limit is set that we can handle, and no
    397       1.4  pgoyette 	 * limits are set that we cannot handle, tell sysmon that
    398       1.4  pgoyette 	 * the driver will take care of monitoring the limits!
    399       1.4  pgoyette 	 */
    400      1.10  pgoyette 	if (*props & (PROP_CRITMIN | PROP_BATTCAP | PROP_BATTWARN))
    401      1.10  pgoyette 		*props &= ~PROP_DRIVER_LIMITS;
    402      1.10  pgoyette 	else if (*props & PROP_LIMITS)
    403      1.10  pgoyette 		*props |= PROP_DRIVER_LIMITS;
    404       1.4  pgoyette 	else
    405      1.10  pgoyette 		*props &= ~PROP_DRIVER_LIMITS;
    406       1.1  pgoyette }
    407       1.1  pgoyette 
    408       1.1  pgoyette #ifdef NOT_YET	/* All registers on these sensors are 16-bits */
    409       1.1  pgoyette 
    410       1.1  pgoyette /* Read a 8-bit value from a register */
    411       1.1  pgoyette static int
    412       1.1  pgoyette sdtemp_read_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t *valp)
    413       1.1  pgoyette {
    414       1.1  pgoyette 	int error;
    415       1.1  pgoyette 
    416       1.1  pgoyette 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    417       1.1  pgoyette 	    sc->sc_address, &reg, 1, valp, sizeof(*valp), 0);
    418       1.1  pgoyette 
    419       1.1  pgoyette 	return error;
    420       1.1  pgoyette }
    421       1.1  pgoyette 
    422       1.1  pgoyette static int
    423       1.1  pgoyette sdtemp_write_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t val)
    424       1.1  pgoyette {
    425       1.1  pgoyette 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    426       1.1  pgoyette 	    sc->sc_address, &reg, 1, &val, sizeof(val), 0);
    427       1.1  pgoyette }
    428       1.1  pgoyette #endif /* NOT_YET */
    429       1.1  pgoyette 
    430       1.1  pgoyette /* Read a 16-bit value from a register */
    431       1.1  pgoyette static int
    432       1.1  pgoyette sdtemp_read_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t *valp)
    433       1.1  pgoyette {
    434       1.1  pgoyette 	int error;
    435       1.1  pgoyette 
    436       1.1  pgoyette 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    437       1.1  pgoyette 	    sc->sc_address, &reg, 1, valp, sizeof(*valp), 0);
    438       1.1  pgoyette 	if (error)
    439       1.1  pgoyette 		return error;
    440       1.1  pgoyette 
    441       1.1  pgoyette 	*valp = be16toh(*valp);
    442       1.1  pgoyette 
    443       1.1  pgoyette 	return 0;
    444       1.1  pgoyette }
    445       1.1  pgoyette 
    446       1.1  pgoyette static int
    447       1.1  pgoyette sdtemp_write_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t val)
    448       1.1  pgoyette {
    449       1.1  pgoyette 	uint16_t temp;
    450       1.1  pgoyette 
    451       1.1  pgoyette 	temp = htobe16(val);
    452       1.1  pgoyette 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    453       1.1  pgoyette 	    sc->sc_address, &reg, 1, &temp, sizeof(temp), 0);
    454       1.1  pgoyette }
    455       1.1  pgoyette 
    456       1.1  pgoyette static uint32_t
    457       1.1  pgoyette sdtemp_decode_temp(struct sdtemp_softc *sc, uint16_t temp)
    458       1.1  pgoyette {
    459       1.1  pgoyette 	uint32_t val;
    460       1.1  pgoyette 	int32_t stemp;
    461       1.1  pgoyette 
    462       1.1  pgoyette 	/* Get only the temperature bits */
    463       1.1  pgoyette 	temp &= SDTEMP_TEMP_MASK;
    464       1.1  pgoyette 
    465       1.1  pgoyette 	/* If necessary, extend the sign bit */
    466       1.1  pgoyette 	if ((sc->sc_capability & SDTEMP_CAP_WIDER_RANGE) &&
    467       1.1  pgoyette 	    (temp & SDTEMP_TEMP_NEGATIVE))
    468       1.1  pgoyette 		temp |= SDTEMP_TEMP_SIGN_EXT;
    469       1.1  pgoyette 
    470       1.1  pgoyette 	/* Mask off only bits valid within current resolution */
    471       1.1  pgoyette 	temp &= ~(0xf >> sc->sc_resolution);
    472       1.1  pgoyette 
    473       1.1  pgoyette 	/* Treat as signed and extend to 32-bits */
    474       1.1  pgoyette 	stemp = (int16_t)temp;
    475       1.1  pgoyette 
    476       1.1  pgoyette 	/* Now convert from 0.0625 (1/16) deg C increments to microKelvins */
    477       1.1  pgoyette 	val = (stemp * 62500) + 273150000;
    478       1.1  pgoyette 
    479       1.1  pgoyette 	return val;
    480       1.1  pgoyette }
    481       1.1  pgoyette 
    482       1.1  pgoyette static void
    483       1.1  pgoyette sdtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    484       1.1  pgoyette {
    485       1.1  pgoyette 	struct sdtemp_softc *sc = sme->sme_cookie;
    486       1.1  pgoyette 	uint16_t val;
    487       1.1  pgoyette 	int error;
    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_AMBIENT_TEMP, &val);
    491       1.1  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    492       1.1  pgoyette 
    493       1.1  pgoyette 	if (error) {
    494       1.1  pgoyette 		edata->state = ENVSYS_SINVALID;
    495       1.1  pgoyette 		return;
    496       1.1  pgoyette 	}
    497       1.1  pgoyette 
    498       1.1  pgoyette 	edata->value_cur = sdtemp_decode_temp(sc, val);
    499       1.1  pgoyette 
    500       1.1  pgoyette 	/* Now check for limits */
    501       1.4  pgoyette 	if ((edata->upropset & PROP_DRIVER_LIMITS) == 0)
    502       1.4  pgoyette 		edata->state = ENVSYS_SVALID;
    503      1.18  pgoyette 	else if ((val & SDTEMP_ABOVE_CRIT) &&
    504      1.18  pgoyette 		    (edata->upropset & PROP_CRITMAX))
    505       1.1  pgoyette 		edata->state = ENVSYS_SCRITOVER;
    506      1.18  pgoyette 	else if ((val & SDTEMP_ABOVE_UPPER) &&
    507      1.18  pgoyette 		    (edata->upropset & PROP_WARNMAX))
    508       1.1  pgoyette 		edata->state = ENVSYS_SWARNOVER;
    509      1.18  pgoyette 	else if ((val & SDTEMP_BELOW_LOWER) &&
    510      1.18  pgoyette 		    (edata->upropset & PROP_WARNMIN))
    511       1.1  pgoyette 		edata->state = ENVSYS_SWARNUNDER;
    512       1.1  pgoyette 	else
    513       1.1  pgoyette 		edata->state = ENVSYS_SVALID;
    514       1.1  pgoyette }
    515       1.1  pgoyette 
    516       1.1  pgoyette /*
    517       1.1  pgoyette  * power management functions
    518       1.1  pgoyette  *
    519       1.1  pgoyette  * We go into "shutdown" mode at suspend time, and return to normal
    520       1.1  pgoyette  * mode upon resume.  This reduces power consumption by disabling
    521       1.1  pgoyette  * the A/D converter.
    522       1.1  pgoyette  */
    523       1.1  pgoyette 
    524       1.1  pgoyette static bool
    525      1.11    dyoung sdtemp_pmf_suspend(device_t dev, const pmf_qual_t *qual)
    526       1.1  pgoyette {
    527       1.1  pgoyette 	struct sdtemp_softc *sc = device_private(dev);
    528       1.1  pgoyette 	int error;
    529       1.1  pgoyette 	uint16_t config;
    530       1.1  pgoyette 
    531       1.1  pgoyette 	iic_acquire_bus(sc->sc_tag, 0);
    532       1.1  pgoyette 	error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
    533       1.1  pgoyette 	if (error == 0) {
    534       1.1  pgoyette 		config |= SDTEMP_CONFIG_SHUTDOWN_MODE;
    535       1.1  pgoyette 		error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
    536       1.1  pgoyette 	}
    537       1.1  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    538       1.1  pgoyette 	return (error == 0);
    539       1.1  pgoyette }
    540       1.1  pgoyette 
    541       1.1  pgoyette static bool
    542      1.11    dyoung sdtemp_pmf_resume(device_t dev, const pmf_qual_t *qual)
    543       1.1  pgoyette {
    544       1.1  pgoyette 	struct sdtemp_softc *sc = device_private(dev);
    545       1.1  pgoyette 	int error;
    546       1.1  pgoyette 	uint16_t config;
    547       1.1  pgoyette 
    548       1.1  pgoyette 	iic_acquire_bus(sc->sc_tag, 0);
    549       1.1  pgoyette 	error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
    550       1.1  pgoyette 	if (error == 0) {
    551       1.1  pgoyette 		config &= ~SDTEMP_CONFIG_SHUTDOWN_MODE;
    552       1.1  pgoyette 		error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
    553       1.1  pgoyette 	}
    554       1.1  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    555       1.1  pgoyette 	return (error == 0);
    556       1.1  pgoyette }
    557      1.19  jmcneill 
    558  1.23.6.2     skrll MODULE(MODULE_CLASS_DRIVER, sdtemp, "i2cexec,sysmon_envsys");
    559      1.19  jmcneill 
    560      1.19  jmcneill #ifdef _MODULE
    561      1.19  jmcneill #include "ioconf.c"
    562      1.19  jmcneill #endif
    563      1.19  jmcneill 
    564      1.19  jmcneill static int
    565      1.19  jmcneill sdtemp_modcmd(modcmd_t cmd, void *opaque)
    566      1.19  jmcneill {
    567      1.19  jmcneill 	int error = 0;
    568      1.19  jmcneill 
    569      1.19  jmcneill 	switch (cmd) {
    570      1.19  jmcneill 	case MODULE_CMD_INIT:
    571      1.19  jmcneill #ifdef _MODULE
    572      1.19  jmcneill 		error = config_init_component(cfdriver_ioconf_sdtemp,
    573      1.19  jmcneill 		    cfattach_ioconf_sdtemp, cfdata_ioconf_sdtemp);
    574      1.19  jmcneill #endif
    575      1.19  jmcneill 		return error;
    576      1.19  jmcneill 	case MODULE_CMD_FINI:
    577      1.19  jmcneill #ifdef _MODULE
    578      1.19  jmcneill 		error = config_fini_component(cfdriver_ioconf_sdtemp,
    579      1.19  jmcneill 		    cfattach_ioconf_sdtemp, cfdata_ioconf_sdtemp);
    580      1.19  jmcneill #endif
    581      1.19  jmcneill 		return error;
    582      1.19  jmcneill 	default:
    583      1.19  jmcneill 		return ENOTTY;
    584      1.19  jmcneill 	}
    585      1.19  jmcneill }
    586