Home | History | Annotate | Line # | Download | only in i2c
sdtemp.c revision 1.35.4.2
      1  1.35.4.2    martin /*      $NetBSD: sdtemp.c,v 1.35.4.2 2021/12/06 19:33:07 martin 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.35.4.2    martin __KERNEL_RCSID(0, "$NetBSD: sdtemp.c,v 1.35.4.2 2021/12/06 19:33:07 martin 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.28   msaitoh 	uint16_t sc_mfgid;
     59      1.28   msaitoh 	uint16_t sc_devid;
     60      1.28   msaitoh 	uint16_t sc_devid_masked;
     61       1.1  pgoyette 	uint16_t sc_capability;
     62       1.1  pgoyette };
     63       1.1  pgoyette 
     64       1.1  pgoyette static int  sdtemp_match(device_t, cfdata_t, void *);
     65       1.1  pgoyette static void sdtemp_attach(device_t, device_t, void *);
     66      1.19  jmcneill static int  sdtemp_detach(device_t, int);
     67       1.1  pgoyette 
     68       1.1  pgoyette CFATTACH_DECL_NEW(sdtemp, sizeof(struct sdtemp_softc),
     69      1.19  jmcneill 	sdtemp_match, sdtemp_attach, sdtemp_detach, NULL);
     70       1.1  pgoyette 
     71       1.1  pgoyette static void	sdtemp_refresh(struct sysmon_envsys *, envsys_data_t *);
     72       1.6  pgoyette static void	sdtemp_get_limits(struct sysmon_envsys *, envsys_data_t *,
     73      1.10  pgoyette 				  sysmon_envsys_lim_t *, uint32_t *);
     74       1.6  pgoyette static void	sdtemp_set_limits(struct sysmon_envsys *, envsys_data_t *,
     75      1.10  pgoyette 				  sysmon_envsys_lim_t *, uint32_t *);
     76       1.1  pgoyette #ifdef NOT_YET
     77       1.1  pgoyette static int	sdtemp_read_8(struct sdtemp_softc *, uint8_t, uint8_t *);
     78       1.1  pgoyette static int	sdtemp_write_8(struct sdtemp_softc *, uint8_t, uint8_t);
     79       1.1  pgoyette #endif /* NOT YET */
     80       1.1  pgoyette static int	sdtemp_read_16(struct sdtemp_softc *, uint8_t, uint16_t *);
     81       1.1  pgoyette static int	sdtemp_write_16(struct sdtemp_softc *, uint8_t, uint16_t);
     82       1.1  pgoyette static uint32_t	sdtemp_decode_temp(struct sdtemp_softc *, uint16_t);
     83      1.11    dyoung static bool	sdtemp_pmf_suspend(device_t, const pmf_qual_t *);
     84      1.11    dyoung static bool	sdtemp_pmf_resume(device_t, const pmf_qual_t *);
     85      1.28   msaitoh /* Device dependent config functions */
     86      1.28   msaitoh static void	sdtemp_config_mcp(struct sdtemp_softc *);
     87      1.28   msaitoh static void	sdtemp_config_idt(struct sdtemp_softc *);
     88       1.1  pgoyette 
     89       1.1  pgoyette struct sdtemp_dev_entry {
     90       1.1  pgoyette 	const uint16_t sdtemp_mfg_id;
     91      1.28   msaitoh 	const uint16_t sdtemp_devrev;
     92      1.28   msaitoh 	const uint16_t sdtemp_mask;
     93      1.28   msaitoh 	void         (*sdtemp_config)(struct sdtemp_softc *);
     94       1.1  pgoyette 	const char    *sdtemp_desc;
     95       1.1  pgoyette };
     96       1.1  pgoyette 
     97       1.4  pgoyette /* Convert sysmon_envsys uKelvin value to simple degC */
     98       1.4  pgoyette 
     99       1.4  pgoyette #define	__UK2C(uk) (((uk) - 273150000) / 1000000)
    100       1.1  pgoyette 
    101      1.28   msaitoh /* List of devices known to conform to JEDEC JC42.4 */
    102      1.28   msaitoh 
    103      1.28   msaitoh #define	CMCP sdtemp_config_mcp
    104      1.28   msaitoh #define	CIDT sdtemp_config_idt
    105      1.28   msaitoh 
    106       1.1  pgoyette static const struct sdtemp_dev_entry
    107       1.1  pgoyette sdtemp_dev_table[] = {
    108      1.28   msaitoh     { AT_MANUFACTURER_ID,   AT_30TS00_DEVICE_ID,     AT_30TS00_MASK,	  NULL,
    109      1.28   msaitoh 	"Atmel AT30TS00" },
    110      1.28   msaitoh     { AT2_MANUFACTURER_ID,  AT2_30TSE004_DEVICE_ID,  AT2_30TSE004_MASK,	  NULL,
    111      1.28   msaitoh 	"Atmel AT30TSE004" },
    112      1.28   msaitoh     { GT_MANUFACTURER_ID,   GT_30TS00_DEVICE_ID,     GT_30TS00_MASK,	  NULL,
    113      1.28   msaitoh 	"Giantec GT30TS00" },
    114      1.28   msaitoh     { GT2_MANUFACTURER_ID,  GT2_34TS02_DEVICE_ID,    GT2_34TS02_MASK,	  NULL,
    115      1.28   msaitoh 	"Giantec GT34TS02" },
    116      1.28   msaitoh     { MAXIM_MANUFACTURER_ID, MAX_6604_DEVICE_ID,     MAX_6604_MASK,	  NULL,
    117      1.16  pgoyette 	"Maxim MAX6604" },
    118      1.33   msaitoh     { MAXIM_MANUFACTURER_ID, MAX_6604_2_DEVICE_ID,   MAX_6604_MASK,	  NULL,
    119      1.33   msaitoh 	"Maxim MAX6604" },
    120      1.28   msaitoh     { MCP_MANUFACTURER_ID,  MCP_9804_DEVICE_ID,	     MCP_9804_MASK,	  CMCP,
    121      1.28   msaitoh 	"Microchip Tech MCP9804" },
    122      1.28   msaitoh     { MCP_MANUFACTURER_ID,  MCP_9805_DEVICE_ID,	     MCP_9805_MASK,	  NULL,
    123      1.14  pgoyette 	"Microchip Tech MCP9805/MCP9843" },
    124      1.28   msaitoh     { MCP_MANUFACTURER_ID,  MCP_98242_DEVICE_ID,     MCP_98242_MASK,	  CMCP,
    125      1.28   msaitoh 	"Microchip Tech MCP98242" },
    126      1.28   msaitoh     { MCP_MANUFACTURER_ID,  MCP_98243_DEVICE_ID,     MCP_98243_MASK,	  CMCP,
    127      1.14  pgoyette 	"Microchip Tech MCP98243" },
    128      1.28   msaitoh     { MCP_MANUFACTURER_ID,  MCP_98244_DEVICE_ID,     MCP_98244_MASK,	  CMCP,
    129      1.28   msaitoh 	"Microchip Tech MCP98244" },
    130      1.33   msaitoh     { MCP2_MANUFACTURER_ID, MCP2_EMC1501_DEVICE_ID,  MCP2_EMC1501_MASK,	  NULL,
    131      1.33   msaitoh 	"Microchip Tech EMC1501" },
    132      1.28   msaitoh     { ADT_MANUFACTURER_ID,  ADT_7408_DEVICE_ID,	     ADT_7408_MASK,	  NULL,
    133       1.1  pgoyette 	"Analog Devices ADT7408" },
    134      1.28   msaitoh     { NXP_MANUFACTURER_ID,  NXP_SE98_DEVICE_ID,	     NXP_SE98_MASK,	  NULL,
    135      1.14  pgoyette 	"NXP Semiconductors SE97B/SE98" },
    136      1.28   msaitoh     { NXP_MANUFACTURER_ID,  NXP_SE97_DEVICE_ID,	     NXP_SE97_MASK,	  NULL,
    137      1.14  pgoyette 	"NXP Semiconductors SE97" },
    138      1.28   msaitoh     { STTS_MANUFACTURER_ID, STTS_424E_DEVICE_ID,     STTS_424E_MASK,	  NULL,
    139      1.28   msaitoh 	"STmicroelectronics STTS424E" },
    140      1.28   msaitoh     { STTS_MANUFACTURER_ID, STTS_424_DEVICE_ID,	     STTS_424_MASK,	  NULL,
    141      1.28   msaitoh 	"STmicroelectronics STTS424" },
    142      1.28   msaitoh     { STTS_MANUFACTURER_ID, STTS_2002_DEVICE_ID,     STTS_2002_MASK,	  NULL,
    143      1.28   msaitoh 	"STmicroelectronics STTS2002" },
    144      1.28   msaitoh     { STTS_MANUFACTURER_ID, STTS_2004_DEVICE_ID,     STTS_2004_MASK,	  NULL,
    145      1.28   msaitoh 	"STmicroelectronics STTS2004" },
    146      1.28   msaitoh     { STTS_MANUFACTURER_ID, STTS_3000_DEVICE_ID,     STTS_3000_MASK,	  NULL,
    147      1.28   msaitoh 	"STmicroelectronics STTS3000" },
    148      1.28   msaitoh     { CAT_MANUFACTURER_ID,  CAT_34TS02_DEVICE_ID,    CAT_34TS02_MASK,	  NULL,
    149       1.1  pgoyette 	"Catalyst CAT34TS02/CAT6095" },
    150      1.28   msaitoh     { CAT_MANUFACTURER_ID,  CAT_34TS02C_DEVICE_ID,   CAT_34TS02C_MASK,	  NULL,
    151      1.26   msaitoh 	"Catalyst CAT34TS02C" },
    152      1.28   msaitoh     { CAT_MANUFACTURER_ID,  CAT_34TS04_DEVICE_ID,    CAT_34TS04_MASK,	  NULL,
    153      1.28   msaitoh 	"Catalyst CAT34TS04" },
    154      1.28   msaitoh     { IDT_MANUFACTURER_ID,  IDT_TSE2004GB2_DEVICE_ID,IDT_TSE2004GB2_MASK, NULL,
    155      1.28   msaitoh 	"Integrated Device Technology TSE2004GB2" },
    156      1.28   msaitoh     { IDT_MANUFACTURER_ID,  IDT_TS3000B3_DEVICE_ID,  IDT_TS3000B3_MASK,	  CIDT,
    157      1.15  pgoyette 	"Integrated Device Technology TS3000B3/TSE2002B3" },
    158      1.28   msaitoh     { IDT_MANUFACTURER_ID,  IDT_TS3000GB0_DEVICE_ID, IDT_TS3000GB0_MASK,  CIDT,
    159      1.28   msaitoh 	"Integrated Device Technology TS3000GB0" },
    160      1.28   msaitoh     { IDT_MANUFACTURER_ID,  IDT_TS3000GB2_DEVICE_ID, IDT_TS3000GB2_MASK,  CIDT,
    161      1.28   msaitoh 	"Integrated Device Technology TS3000GB2" },
    162      1.28   msaitoh     { IDT_MANUFACTURER_ID,  IDT_TS3001GB2_DEVICE_ID, IDT_TS3001GB2_MASK,  CIDT,
    163      1.28   msaitoh 	"Integrated Device Technology TS3001GB2" },
    164      1.31   msaitoh     /*
    165      1.31   msaitoh      * Don't change the location of the following two entries. Device specific
    166      1.31   msaitoh      * entry must be located at above.
    167      1.31   msaitoh      */
    168      1.31   msaitoh     { 0,		    TSE2004AV_ID,	     TSE2004AV_MASK,	  NULL,
    169      1.31   msaitoh 	"TSE2004av compliant device (generic driver)" },
    170      1.28   msaitoh     { 0, 0, 0, NULL, "Unknown" }
    171      1.28   msaitoh };
    172      1.28   msaitoh 
    173      1.28   msaitoh #undef CMCP
    174      1.28   msaitoh #undef CIDT
    175      1.28   msaitoh 
    176      1.28   msaitoh static const char *temp_resl[] = {
    177      1.28   msaitoh 	"0.5C",
    178      1.28   msaitoh 	"0.25C",
    179      1.28   msaitoh 	"0.125C",
    180      1.28   msaitoh 	"0.0625C"
    181       1.1  pgoyette };
    182       1.1  pgoyette 
    183       1.1  pgoyette static int
    184      1.14  pgoyette sdtemp_lookup(uint16_t mfg, uint16_t devrev)
    185       1.1  pgoyette {
    186       1.1  pgoyette 	int i;
    187       1.1  pgoyette 
    188      1.14  pgoyette 	for (i = 0; sdtemp_dev_table[i].sdtemp_mfg_id; i++) {
    189      1.14  pgoyette 		if (mfg != sdtemp_dev_table[i].sdtemp_mfg_id)
    190      1.14  pgoyette 			continue;
    191      1.14  pgoyette 		if ((devrev & sdtemp_dev_table[i].sdtemp_mask) ==
    192      1.14  pgoyette 		    sdtemp_dev_table[i].sdtemp_devrev)
    193       1.1  pgoyette 			break;
    194      1.14  pgoyette 	}
    195      1.31   msaitoh 	/* Check TSE2004av */
    196      1.31   msaitoh 	if ((sdtemp_dev_table[i].sdtemp_mfg_id == 0)
    197      1.31   msaitoh 	    && (SDTEMP_IS_TSE2004AV(devrev) == 0))
    198      1.31   msaitoh 			i++; /* Unknown */
    199       1.1  pgoyette 
    200       1.1  pgoyette 	return i;
    201       1.1  pgoyette }
    202       1.1  pgoyette 
    203       1.1  pgoyette static int
    204       1.1  pgoyette sdtemp_match(device_t parent, cfdata_t cf, void *aux)
    205       1.1  pgoyette {
    206       1.1  pgoyette 	struct i2c_attach_args *ia = aux;
    207      1.31   msaitoh 	uint16_t mfgid, devid, cap;
    208       1.1  pgoyette 	struct sdtemp_softc sc;
    209       1.1  pgoyette 	int i, error;
    210       1.1  pgoyette 
    211       1.1  pgoyette 	sc.sc_tag = ia->ia_tag;
    212       1.1  pgoyette 	sc.sc_address = ia->ia_addr;
    213       1.1  pgoyette 
    214       1.1  pgoyette 	if ((ia->ia_addr & SDTEMP_ADDRMASK) != SDTEMP_ADDR)
    215       1.1  pgoyette 		return 0;
    216       1.1  pgoyette 
    217  1.35.4.1    martin 	/*
    218  1.35.4.1    martin 	 * Verify that we can read the manufacturer ID, Device ID and the
    219  1.35.4.1    martin 	 * capability
    220  1.35.4.1    martin 	 */
    221  1.35.4.1    martin 	error = iic_acquire_bus(sc.sc_tag, 0);
    222  1.35.4.1    martin 	if (error)
    223  1.35.4.1    martin 		return 0;
    224       1.1  pgoyette 	error = sdtemp_read_16(&sc, SDTEMP_REG_MFG_ID,  &mfgid) |
    225      1.31   msaitoh 		sdtemp_read_16(&sc, SDTEMP_REG_DEV_REV, &devid) |
    226      1.31   msaitoh 		sdtemp_read_16(&sc, SDTEMP_REG_CAPABILITY, &cap);
    227       1.1  pgoyette 	iic_release_bus(sc.sc_tag, 0);
    228       1.1  pgoyette 
    229       1.1  pgoyette 	if (error)
    230       1.1  pgoyette 		return 0;
    231       1.1  pgoyette 
    232      1.14  pgoyette 	i = sdtemp_lookup(mfgid, devid);
    233      1.31   msaitoh 	if ((sdtemp_dev_table[i].sdtemp_mfg_id == 0) &&
    234      1.31   msaitoh 	    (sdtemp_dev_table[i].sdtemp_devrev == 0)) {
    235      1.30   msaitoh 		aprint_debug("sdtemp: No match for mfg 0x%04x dev 0x%02x "
    236      1.30   msaitoh 		    "rev 0x%02x at address 0x%02x\n", mfgid, devid >> 8,
    237       1.1  pgoyette 		    devid & 0xff, sc.sc_address);
    238       1.1  pgoyette 		return 0;
    239       1.1  pgoyette 	}
    240       1.1  pgoyette 
    241      1.31   msaitoh 	/*
    242  1.35.4.1    martin 	 * Check by SDTEMP_IS_TSE2004AV() might not be enough, so check the
    243  1.35.4.1    martin 	 * alarm capability, too.
    244      1.31   msaitoh 	 */
    245      1.31   msaitoh 	if ((cap & SDTEMP_CAP_HAS_ALARM) == 0)
    246      1.31   msaitoh 		return 0;
    247      1.31   msaitoh 
    248      1.34   thorpej 	return I2C_MATCH_ADDRESS_AND_PROBE;
    249       1.1  pgoyette }
    250       1.1  pgoyette 
    251       1.1  pgoyette static void
    252       1.1  pgoyette sdtemp_attach(device_t parent, device_t self, void *aux)
    253       1.1  pgoyette {
    254       1.1  pgoyette 	struct sdtemp_softc *sc = device_private(self);
    255       1.1  pgoyette 	struct i2c_attach_args *ia = aux;
    256       1.1  pgoyette 	uint16_t mfgid, devid;
    257       1.1  pgoyette 	int i, error;
    258       1.1  pgoyette 
    259       1.1  pgoyette 	sc->sc_tag = ia->ia_tag;
    260       1.1  pgoyette 	sc->sc_address = ia->ia_addr;
    261       1.1  pgoyette 	sc->sc_dev = self;
    262       1.1  pgoyette 
    263  1.35.4.1    martin 	error = iic_acquire_bus(sc->sc_tag, 0);
    264  1.35.4.1    martin 	if (error)
    265  1.35.4.1    martin 		return;
    266  1.35.4.1    martin 
    267       1.1  pgoyette 	if ((error = sdtemp_read_16(sc, SDTEMP_REG_MFG_ID,  &mfgid)) != 0 ||
    268       1.1  pgoyette 	    (error = sdtemp_read_16(sc, SDTEMP_REG_DEV_REV, &devid)) != 0) {
    269       1.5  pgoyette 		iic_release_bus(sc->sc_tag, 0);
    270       1.1  pgoyette 		aprint_error(": attach error %d\n", error);
    271       1.1  pgoyette 		return;
    272       1.1  pgoyette 	}
    273      1.28   msaitoh 	sc->sc_mfgid = mfgid;
    274      1.28   msaitoh 	sc->sc_devid = devid;
    275      1.14  pgoyette 	i = sdtemp_lookup(mfgid, devid);
    276      1.28   msaitoh 	sc->sc_devid_masked = devid & sdtemp_dev_table[i].sdtemp_mask;
    277       1.1  pgoyette 
    278       1.1  pgoyette 	aprint_naive(": Temp Sensor\n");
    279       1.1  pgoyette 	aprint_normal(": %s Temp Sensor\n", sdtemp_dev_table[i].sdtemp_desc);
    280       1.1  pgoyette 
    281      1.31   msaitoh 	if (sdtemp_dev_table[i].sdtemp_mfg_id == 0) {
    282      1.31   msaitoh 		if (SDTEMP_IS_TSE2004AV(devid))
    283      1.31   msaitoh 			aprint_normal_dev(self, "TSE2004av compliant. "
    284      1.31   msaitoh 			    "Manufacturer ID 0x%04hx, Device revision 0x%02x\n",
    285      1.31   msaitoh 			    mfgid, devid & TSE2004AV_REV);
    286      1.31   msaitoh 		else {
    287      1.31   msaitoh 			aprint_error_dev(self,
    288      1.31   msaitoh 			    "mfg 0x%04x dev 0x%02x rev 0x%02x at addr 0x%02x\n",
    289      1.31   msaitoh 			    mfgid, devid >> 8, devid & 0xff, ia->ia_addr);
    290      1.31   msaitoh 			iic_release_bus(sc->sc_tag, 0);
    291      1.31   msaitoh 			aprint_error_dev(self, "It should no happen. "
    292      1.31   msaitoh 			    "Why attach() found me?\n");
    293      1.31   msaitoh 			return;
    294      1.31   msaitoh 		}
    295      1.31   msaitoh 	}
    296       1.1  pgoyette 
    297      1.28   msaitoh 	error = sdtemp_read_16(sc, SDTEMP_REG_CAPABILITY, &sc->sc_capability);
    298      1.28   msaitoh 	aprint_debug_dev(self, "capability reg = %04x\n", sc->sc_capability);
    299      1.28   msaitoh 	sc->sc_resolution
    300      1.28   msaitoh 	    = __SHIFTOUT(sc->sc_capability, SDTEMP_CAP_RESOLUTION);
    301      1.28   msaitoh 	/*
    302      1.28   msaitoh 	 * Call device dependent function here. Currently, it's used for
    303      1.28   msaitoh 	 * the resolution.
    304      1.28   msaitoh 	 *
    305      1.28   msaitoh 	 * IDT's devices and some Microchip's devices have the resolution
    306      1.28   msaitoh 	 * register in the vendor specific registers area. The devices'
    307      1.28   msaitoh 	 * resolution bits in the capability register are not the maximum
    308      1.35   khorben 	 * resolution but the current value of the setting.
    309      1.28   msaitoh 	 */
    310      1.28   msaitoh 	if (sdtemp_dev_table[i].sdtemp_config != NULL)
    311      1.28   msaitoh 		sdtemp_dev_table[i].sdtemp_config(sc);
    312      1.28   msaitoh 
    313      1.28   msaitoh 	aprint_normal_dev(self, "%s accuracy",
    314      1.28   msaitoh 	    (sc->sc_capability & SDTEMP_CAP_ACCURACY_1C) ? "high" : "default");
    315      1.28   msaitoh 	if ((sc->sc_capability & SDTEMP_CAP_WIDER_RANGE) != 0)
    316      1.28   msaitoh 		aprint_normal(", wider range");
    317      1.28   msaitoh 	aprint_normal(", %s resolution", temp_resl[sc->sc_resolution]);
    318      1.28   msaitoh 	if ((sc->sc_capability & SDTEMP_CAP_VHV) != 0)
    319      1.28   msaitoh 		aprint_debug(", high voltage standoff");
    320      1.28   msaitoh 	aprint_debug(", %s timeout",
    321      1.28   msaitoh 	    (sc->sc_capability & SDTEMP_CAP_TMOUT) ? "25-35ms" : "10-60ms");
    322      1.28   msaitoh 	if ((sc->sc_capability & SDTEMP_CAP_EVSD) != 0)
    323      1.28   msaitoh 		aprint_normal(", event with shutdown");
    324      1.28   msaitoh 	aprint_normal("\n");
    325       1.1  pgoyette 	/*
    326       1.1  pgoyette 	 * Alarm capability is required;  if not present, this is likely
    327       1.1  pgoyette 	 * not a real sdtemp device.
    328       1.1  pgoyette 	 */
    329       1.1  pgoyette 	if (error != 0 || (sc->sc_capability & SDTEMP_CAP_HAS_ALARM) == 0) {
    330       1.1  pgoyette 		iic_release_bus(sc->sc_tag, 0);
    331       1.1  pgoyette 		aprint_error_dev(self,
    332       1.1  pgoyette 		    "required alarm capability not present!\n");
    333       1.1  pgoyette 		return;
    334       1.1  pgoyette 	}
    335       1.1  pgoyette 	/* Set the configuration to defaults. */
    336       1.1  pgoyette 	error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, 0);
    337       1.1  pgoyette 	if (error != 0) {
    338       1.1  pgoyette 		iic_release_bus(sc->sc_tag, 0);
    339       1.1  pgoyette 		aprint_error_dev(self, "error %d writing config register\n",
    340       1.1  pgoyette 		    error);
    341       1.1  pgoyette 		return;
    342       1.1  pgoyette 	}
    343       1.1  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    344       1.1  pgoyette 
    345       1.1  pgoyette 	/* Hook us into the sysmon_envsys subsystem */
    346       1.1  pgoyette 	sc->sc_sme = sysmon_envsys_create();
    347       1.4  pgoyette 	sc->sc_sme->sme_name = device_xname(self);
    348       1.4  pgoyette 	sc->sc_sme->sme_cookie = sc;
    349       1.4  pgoyette 	sc->sc_sme->sme_refresh = sdtemp_refresh;
    350       1.4  pgoyette 	sc->sc_sme->sme_get_limits = sdtemp_get_limits;
    351       1.4  pgoyette 	sc->sc_sme->sme_set_limits = sdtemp_set_limits;
    352       1.4  pgoyette 
    353       1.1  pgoyette 	sc->sc_sensor = kmem_zalloc(sizeof(envsys_data_t), KM_NOSLEEP);
    354       1.1  pgoyette 	if (!sc->sc_sensor) {
    355       1.1  pgoyette 		aprint_error_dev(self, "unable to allocate sc_sensor\n");
    356       1.1  pgoyette 		goto bad2;
    357       1.1  pgoyette 	}
    358       1.1  pgoyette 
    359       1.1  pgoyette 	/* Initialize sensor data. */
    360       1.1  pgoyette 	sc->sc_sensor->units =  ENVSYS_STEMP;
    361       1.1  pgoyette 	sc->sc_sensor->state = ENVSYS_SINVALID;
    362       1.1  pgoyette 	sc->sc_sensor->flags |= ENVSYS_FMONLIMITS;
    363       1.1  pgoyette 	(void)strlcpy(sc->sc_sensor->desc, device_xname(self),
    364       1.1  pgoyette 	    sizeof(sc->sc_sensor->desc));
    365      1.22     soren 	snprintf(sc->sc_sensor->desc, sizeof(sc->sc_sensor->desc),
    366      1.22     soren 	    "DIMM %d temperature", sc->sc_address - SDTEMP_ADDR);
    367       1.1  pgoyette 
    368       1.1  pgoyette 	/* Now attach the sensor */
    369       1.1  pgoyette 	if (sysmon_envsys_sensor_attach(sc->sc_sme, sc->sc_sensor)) {
    370       1.1  pgoyette 		aprint_error_dev(self, "unable to attach sensor\n");
    371       1.1  pgoyette 		goto bad;
    372       1.1  pgoyette 	}
    373       1.1  pgoyette 
    374       1.1  pgoyette 	/* Register the device */
    375       1.1  pgoyette 	error = sysmon_envsys_register(sc->sc_sme);
    376       1.1  pgoyette 	if (error) {
    377       1.1  pgoyette 		aprint_error_dev(self, "error %d registering with sysmon\n",
    378       1.1  pgoyette 		    error);
    379       1.1  pgoyette 		goto bad;
    380       1.1  pgoyette 	}
    381       1.1  pgoyette 
    382       1.1  pgoyette 	if (!pmf_device_register(self, sdtemp_pmf_suspend, sdtemp_pmf_resume))
    383       1.1  pgoyette 		aprint_error_dev(self, "couldn't establish power handler\n");
    384       1.1  pgoyette 
    385       1.1  pgoyette 	/* Retrieve and display hardware monitor limits */
    386      1.17     njoly 	sdtemp_get_limits(sc->sc_sme, sc->sc_sensor, &sc->sc_deflims,
    387      1.17     njoly 	    &sc->sc_defprops);
    388      1.21  pgoyette 	aprint_normal_dev(self, "Hardware limits: ");
    389       1.1  pgoyette 	i = 0;
    390      1.17     njoly 	if (sc->sc_defprops & PROP_WARNMIN) {
    391      1.21  pgoyette 		aprint_normal("low %dC",
    392      1.17     njoly 		              __UK2C(sc->sc_deflims.sel_warnmin));
    393       1.1  pgoyette 		i++;
    394       1.1  pgoyette 	}
    395      1.17     njoly 	if (sc->sc_defprops & PROP_WARNMAX) {
    396      1.21  pgoyette 		aprint_normal("%shigh %dC ", (i)?", ":"",
    397      1.17     njoly 			      __UK2C(sc->sc_deflims.sel_warnmax));
    398       1.1  pgoyette 		i++;
    399       1.1  pgoyette 	}
    400      1.17     njoly 	if (sc->sc_defprops & PROP_CRITMAX) {
    401      1.21  pgoyette 		aprint_normal("%scritical %dC ", (i)?", ":"",
    402      1.17     njoly 			      __UK2C(sc->sc_deflims.sel_critmax));
    403       1.1  pgoyette 		i++;
    404       1.1  pgoyette 	}
    405      1.21  pgoyette 	aprint_normal("%s\n", (i)?"":"none set");
    406       1.1  pgoyette 
    407       1.1  pgoyette 	return;
    408       1.1  pgoyette 
    409       1.1  pgoyette bad:
    410       1.1  pgoyette 	kmem_free(sc->sc_sensor, sizeof(envsys_data_t));
    411       1.1  pgoyette bad2:
    412       1.1  pgoyette 	sysmon_envsys_destroy(sc->sc_sme);
    413       1.1  pgoyette }
    414       1.1  pgoyette 
    415      1.19  jmcneill static int
    416      1.19  jmcneill sdtemp_detach(device_t self, int flags)
    417      1.19  jmcneill {
    418      1.19  jmcneill 	struct sdtemp_softc *sc = device_private(self);
    419      1.19  jmcneill 
    420      1.19  jmcneill 	pmf_device_deregister(self);
    421      1.19  jmcneill 
    422      1.19  jmcneill 	if (sc->sc_sme)
    423      1.19  jmcneill 		sysmon_envsys_unregister(sc->sc_sme);
    424      1.19  jmcneill 	if (sc->sc_sensor)
    425      1.19  jmcneill 		kmem_free(sc->sc_sensor, sizeof(envsys_data_t));
    426      1.19  jmcneill 
    427      1.19  jmcneill 	return 0;
    428      1.19  jmcneill }
    429      1.19  jmcneill 
    430       1.4  pgoyette /* Retrieve current limits from device, and encode in uKelvins */
    431       1.1  pgoyette static void
    432       1.6  pgoyette sdtemp_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    433      1.10  pgoyette 		  sysmon_envsys_lim_t *limits, uint32_t *props)
    434       1.1  pgoyette {
    435       1.4  pgoyette 	struct sdtemp_softc *sc = sme->sme_cookie;
    436       1.4  pgoyette 	uint16_t lim;
    437       1.1  pgoyette 
    438      1.10  pgoyette 	*props = 0;
    439  1.35.4.1    martin 	if (iic_acquire_bus(sc->sc_tag, 0) != 0)
    440  1.35.4.1    martin 		return;
    441  1.35.4.1    martin 
    442       1.4  pgoyette 	if (sdtemp_read_16(sc, SDTEMP_REG_LOWER_LIM, &lim) == 0 && lim != 0) {
    443       1.4  pgoyette 		limits->sel_warnmin = sdtemp_decode_temp(sc, lim);
    444      1.10  pgoyette 		*props |= PROP_WARNMIN;
    445       1.4  pgoyette 	}
    446       1.4  pgoyette 	if (sdtemp_read_16(sc, SDTEMP_REG_UPPER_LIM, &lim) == 0 && lim != 0) {
    447       1.4  pgoyette 		limits->sel_warnmax = sdtemp_decode_temp(sc, lim);
    448      1.10  pgoyette 		*props |= PROP_WARNMAX;
    449       1.4  pgoyette 	}
    450       1.4  pgoyette 	if (sdtemp_read_16(sc, SDTEMP_REG_CRIT_LIM, &lim) == 0 && lim != 0) {
    451       1.4  pgoyette 		limits->sel_critmax = sdtemp_decode_temp(sc, lim);
    452      1.10  pgoyette 		*props |= PROP_CRITMAX;
    453       1.1  pgoyette 	}
    454       1.4  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    455      1.10  pgoyette 	if (*props != 0)
    456      1.10  pgoyette 		*props |= PROP_DRIVER_LIMITS;
    457       1.4  pgoyette }
    458       1.4  pgoyette 
    459       1.4  pgoyette /* Send current limit values to the device */
    460       1.4  pgoyette static void
    461       1.6  pgoyette sdtemp_set_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    462      1.10  pgoyette 		  sysmon_envsys_lim_t *limits, uint32_t *props)
    463       1.4  pgoyette {
    464       1.4  pgoyette 	uint16_t val;
    465       1.4  pgoyette 	struct sdtemp_softc *sc = sme->sme_cookie;
    466       1.1  pgoyette 
    467      1.13  pgoyette 	if (limits == NULL) {
    468      1.13  pgoyette 		limits = &sc->sc_deflims;
    469      1.13  pgoyette 		props  = &sc->sc_defprops;
    470      1.13  pgoyette 	}
    471  1.35.4.1    martin 	if (iic_acquire_bus(sc->sc_tag, 0) != 0)
    472  1.35.4.1    martin 		return;
    473  1.35.4.1    martin 
    474      1.10  pgoyette 	if (*props & PROP_WARNMIN) {
    475       1.4  pgoyette 		val = __UK2C(limits->sel_warnmin);
    476       1.4  pgoyette 		(void)sdtemp_write_16(sc, SDTEMP_REG_LOWER_LIM,
    477       1.4  pgoyette 					(val << 4) & SDTEMP_TEMP_MASK);
    478       1.4  pgoyette 	}
    479      1.10  pgoyette 	if (*props & PROP_WARNMAX) {
    480       1.4  pgoyette 		val = __UK2C(limits->sel_warnmax);
    481       1.4  pgoyette 		(void)sdtemp_write_16(sc, SDTEMP_REG_UPPER_LIM,
    482       1.4  pgoyette 					(val << 4) & SDTEMP_TEMP_MASK);
    483       1.4  pgoyette 	}
    484      1.10  pgoyette 	if (*props & PROP_CRITMAX) {
    485       1.4  pgoyette 		val = __UK2C(limits->sel_critmax);
    486       1.4  pgoyette 		(void)sdtemp_write_16(sc, SDTEMP_REG_CRIT_LIM,
    487       1.4  pgoyette 					(val << 4) & SDTEMP_TEMP_MASK);
    488       1.4  pgoyette 	}
    489       1.1  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    490       1.1  pgoyette 
    491       1.4  pgoyette 	/*
    492       1.4  pgoyette 	 * If at least one limit is set that we can handle, and no
    493       1.4  pgoyette 	 * limits are set that we cannot handle, tell sysmon that
    494       1.4  pgoyette 	 * the driver will take care of monitoring the limits!
    495       1.4  pgoyette 	 */
    496      1.10  pgoyette 	if (*props & (PROP_CRITMIN | PROP_BATTCAP | PROP_BATTWARN))
    497      1.10  pgoyette 		*props &= ~PROP_DRIVER_LIMITS;
    498      1.10  pgoyette 	else if (*props & PROP_LIMITS)
    499      1.10  pgoyette 		*props |= PROP_DRIVER_LIMITS;
    500       1.4  pgoyette 	else
    501      1.10  pgoyette 		*props &= ~PROP_DRIVER_LIMITS;
    502       1.1  pgoyette }
    503       1.1  pgoyette 
    504       1.1  pgoyette #ifdef NOT_YET	/* All registers on these sensors are 16-bits */
    505       1.1  pgoyette 
    506       1.1  pgoyette /* Read a 8-bit value from a register */
    507       1.1  pgoyette static int
    508       1.1  pgoyette sdtemp_read_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t *valp)
    509       1.1  pgoyette {
    510       1.1  pgoyette 	int error;
    511       1.1  pgoyette 
    512       1.1  pgoyette 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    513       1.1  pgoyette 	    sc->sc_address, &reg, 1, valp, sizeof(*valp), 0);
    514       1.1  pgoyette 
    515       1.1  pgoyette 	return error;
    516       1.1  pgoyette }
    517       1.1  pgoyette 
    518       1.1  pgoyette static int
    519       1.1  pgoyette sdtemp_write_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t val)
    520       1.1  pgoyette {
    521       1.1  pgoyette 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    522       1.1  pgoyette 	    sc->sc_address, &reg, 1, &val, sizeof(val), 0);
    523       1.1  pgoyette }
    524       1.1  pgoyette #endif /* NOT_YET */
    525       1.1  pgoyette 
    526       1.1  pgoyette /* Read a 16-bit value from a register */
    527       1.1  pgoyette static int
    528       1.1  pgoyette sdtemp_read_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t *valp)
    529       1.1  pgoyette {
    530       1.1  pgoyette 	int error;
    531       1.1  pgoyette 
    532       1.1  pgoyette 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    533       1.1  pgoyette 	    sc->sc_address, &reg, 1, valp, sizeof(*valp), 0);
    534       1.1  pgoyette 	if (error)
    535       1.1  pgoyette 		return error;
    536       1.1  pgoyette 
    537       1.1  pgoyette 	*valp = be16toh(*valp);
    538       1.1  pgoyette 
    539       1.1  pgoyette 	return 0;
    540       1.1  pgoyette }
    541       1.1  pgoyette 
    542       1.1  pgoyette static int
    543       1.1  pgoyette sdtemp_write_16(struct sdtemp_softc *sc, uint8_t reg, uint16_t val)
    544       1.1  pgoyette {
    545       1.1  pgoyette 	uint16_t temp;
    546       1.1  pgoyette 
    547       1.1  pgoyette 	temp = htobe16(val);
    548       1.1  pgoyette 	return iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    549       1.1  pgoyette 	    sc->sc_address, &reg, 1, &temp, sizeof(temp), 0);
    550       1.1  pgoyette }
    551       1.1  pgoyette 
    552       1.1  pgoyette static uint32_t
    553       1.1  pgoyette sdtemp_decode_temp(struct sdtemp_softc *sc, uint16_t temp)
    554       1.1  pgoyette {
    555       1.1  pgoyette 	uint32_t val;
    556       1.1  pgoyette 	int32_t stemp;
    557       1.1  pgoyette 
    558       1.1  pgoyette 	/* Get only the temperature bits */
    559       1.1  pgoyette 	temp &= SDTEMP_TEMP_MASK;
    560       1.1  pgoyette 
    561       1.1  pgoyette 	/* If necessary, extend the sign bit */
    562       1.1  pgoyette 	if ((sc->sc_capability & SDTEMP_CAP_WIDER_RANGE) &&
    563       1.1  pgoyette 	    (temp & SDTEMP_TEMP_NEGATIVE))
    564       1.1  pgoyette 		temp |= SDTEMP_TEMP_SIGN_EXT;
    565       1.1  pgoyette 
    566       1.1  pgoyette 	/* Mask off only bits valid within current resolution */
    567      1.28   msaitoh 	temp &= ~(0x7 >> sc->sc_resolution);
    568       1.1  pgoyette 
    569       1.1  pgoyette 	/* Treat as signed and extend to 32-bits */
    570       1.1  pgoyette 	stemp = (int16_t)temp;
    571       1.1  pgoyette 
    572       1.1  pgoyette 	/* Now convert from 0.0625 (1/16) deg C increments to microKelvins */
    573       1.1  pgoyette 	val = (stemp * 62500) + 273150000;
    574       1.1  pgoyette 
    575       1.1  pgoyette 	return val;
    576       1.1  pgoyette }
    577       1.1  pgoyette 
    578       1.1  pgoyette static void
    579       1.1  pgoyette sdtemp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    580       1.1  pgoyette {
    581       1.1  pgoyette 	struct sdtemp_softc *sc = sme->sme_cookie;
    582       1.1  pgoyette 	uint16_t val;
    583       1.1  pgoyette 	int error;
    584       1.1  pgoyette 
    585  1.35.4.1    martin 	error = iic_acquire_bus(sc->sc_tag, 0);
    586  1.35.4.1    martin 	if (error) {
    587  1.35.4.1    martin 		edata->state = ENVSYS_SINVALID;
    588  1.35.4.1    martin 		return;
    589  1.35.4.1    martin 	}
    590  1.35.4.1    martin 
    591       1.1  pgoyette 	error = sdtemp_read_16(sc, SDTEMP_REG_AMBIENT_TEMP, &val);
    592       1.1  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    593       1.1  pgoyette 
    594       1.1  pgoyette 	if (error) {
    595       1.1  pgoyette 		edata->state = ENVSYS_SINVALID;
    596       1.1  pgoyette 		return;
    597       1.1  pgoyette 	}
    598       1.1  pgoyette 
    599       1.1  pgoyette 	edata->value_cur = sdtemp_decode_temp(sc, val);
    600       1.1  pgoyette 
    601       1.1  pgoyette 	/* Now check for limits */
    602       1.4  pgoyette 	if ((edata->upropset & PROP_DRIVER_LIMITS) == 0)
    603       1.4  pgoyette 		edata->state = ENVSYS_SVALID;
    604      1.18  pgoyette 	else if ((val & SDTEMP_ABOVE_CRIT) &&
    605      1.18  pgoyette 		    (edata->upropset & PROP_CRITMAX))
    606       1.1  pgoyette 		edata->state = ENVSYS_SCRITOVER;
    607      1.18  pgoyette 	else if ((val & SDTEMP_ABOVE_UPPER) &&
    608      1.18  pgoyette 		    (edata->upropset & PROP_WARNMAX))
    609       1.1  pgoyette 		edata->state = ENVSYS_SWARNOVER;
    610      1.18  pgoyette 	else if ((val & SDTEMP_BELOW_LOWER) &&
    611      1.18  pgoyette 		    (edata->upropset & PROP_WARNMIN))
    612       1.1  pgoyette 		edata->state = ENVSYS_SWARNUNDER;
    613       1.1  pgoyette 	else
    614       1.1  pgoyette 		edata->state = ENVSYS_SVALID;
    615       1.1  pgoyette }
    616       1.1  pgoyette 
    617       1.1  pgoyette /*
    618  1.35.4.1    martin  * Power management functions
    619       1.1  pgoyette  *
    620       1.1  pgoyette  * We go into "shutdown" mode at suspend time, and return to normal
    621       1.1  pgoyette  * mode upon resume.  This reduces power consumption by disabling
    622       1.1  pgoyette  * the A/D converter.
    623       1.1  pgoyette  */
    624       1.1  pgoyette 
    625       1.1  pgoyette static bool
    626      1.11    dyoung sdtemp_pmf_suspend(device_t dev, const pmf_qual_t *qual)
    627       1.1  pgoyette {
    628       1.1  pgoyette 	struct sdtemp_softc *sc = device_private(dev);
    629       1.1  pgoyette 	int error;
    630       1.1  pgoyette 	uint16_t config;
    631       1.1  pgoyette 
    632  1.35.4.1    martin 	error = iic_acquire_bus(sc->sc_tag, 0);
    633  1.35.4.1    martin 	if (error != 0)
    634  1.35.4.1    martin 		return false;
    635  1.35.4.1    martin 
    636       1.1  pgoyette 	error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
    637       1.1  pgoyette 	if (error == 0) {
    638       1.1  pgoyette 		config |= SDTEMP_CONFIG_SHUTDOWN_MODE;
    639       1.1  pgoyette 		error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
    640       1.1  pgoyette 	}
    641       1.1  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    642       1.1  pgoyette 	return (error == 0);
    643       1.1  pgoyette }
    644       1.1  pgoyette 
    645       1.1  pgoyette static bool
    646      1.11    dyoung sdtemp_pmf_resume(device_t dev, const pmf_qual_t *qual)
    647       1.1  pgoyette {
    648       1.1  pgoyette 	struct sdtemp_softc *sc = device_private(dev);
    649       1.1  pgoyette 	int error;
    650       1.1  pgoyette 	uint16_t config;
    651       1.1  pgoyette 
    652  1.35.4.1    martin 	error = iic_acquire_bus(sc->sc_tag, 0);
    653  1.35.4.1    martin 	if (error != 0)
    654  1.35.4.1    martin 		return false;
    655  1.35.4.1    martin 
    656       1.1  pgoyette 	error = sdtemp_read_16(sc, SDTEMP_REG_CONFIG, &config);
    657       1.1  pgoyette 	if (error == 0) {
    658       1.1  pgoyette 		config &= ~SDTEMP_CONFIG_SHUTDOWN_MODE;
    659       1.1  pgoyette 		error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, config);
    660       1.1  pgoyette 	}
    661       1.1  pgoyette 	iic_release_bus(sc->sc_tag, 0);
    662       1.1  pgoyette 	return (error == 0);
    663       1.1  pgoyette }
    664      1.19  jmcneill 
    665      1.28   msaitoh /* Device dependent config functions */
    666      1.28   msaitoh 
    667      1.28   msaitoh static void
    668      1.28   msaitoh sdtemp_config_mcp(struct sdtemp_softc *sc)
    669      1.28   msaitoh {
    670      1.28   msaitoh 	int rv;
    671      1.28   msaitoh 	uint8_t resolreg;
    672      1.28   msaitoh 
    673      1.28   msaitoh 	/* Note that MCP9805 has no resolution register */
    674      1.28   msaitoh 	switch (sc->sc_devid_masked) {
    675      1.28   msaitoh 	case MCP_9804_DEVICE_ID:
    676      1.28   msaitoh 	case MCP_98242_DEVICE_ID:
    677      1.28   msaitoh 	case MCP_98243_DEVICE_ID:
    678      1.28   msaitoh 		resolreg = SDTEMP_REG_MCP_RESOLUTION_9804;
    679      1.28   msaitoh 		break;
    680      1.28   msaitoh 	case MCP_98244_DEVICE_ID:
    681      1.28   msaitoh 		resolreg = SDTEMP_REG_MCP_RESOLUTION_98244;
    682      1.28   msaitoh 		break;
    683      1.28   msaitoh 	default:
    684      1.28   msaitoh 		aprint_error("%s: %s: unknown device ID (%04hx)\n",
    685      1.28   msaitoh 		    device_xname(sc->sc_dev), __func__, sc->sc_devid_masked);
    686      1.28   msaitoh 		return;
    687      1.28   msaitoh 	}
    688      1.28   msaitoh 
    689      1.28   msaitoh 	/*
    690      1.28   msaitoh 	 * Set resolution to the max.
    691      1.28   msaitoh 	 *
    692      1.28   msaitoh 	 * Even if it fails, the resolution will be the default. It's not a
    693      1.28   msaitoh 	 * fatal error.
    694      1.28   msaitoh 	 */
    695      1.28   msaitoh 	rv = sdtemp_write_16(sc, resolreg, SDTEMP_CAP_RESOLUTION_MAX);
    696      1.28   msaitoh 	if (rv == 0)
    697      1.28   msaitoh 		sc->sc_resolution = SDTEMP_CAP_RESOLUTION_MAX;
    698      1.28   msaitoh 	else
    699  1.35.4.2    martin 		aprint_debug_dev(sc->sc_dev,
    700  1.35.4.2    martin 		    "error %d writing resolution register\n", rv);
    701      1.28   msaitoh }
    702      1.28   msaitoh 
    703      1.28   msaitoh static void
    704      1.28   msaitoh sdtemp_config_idt(struct sdtemp_softc *sc)
    705      1.28   msaitoh {
    706      1.28   msaitoh 	int rv;
    707      1.28   msaitoh 
    708      1.28   msaitoh 	/*
    709      1.28   msaitoh 	 * Set resolution to the max.
    710      1.28   msaitoh 	 *
    711      1.28   msaitoh 	 * Even if it fails, the resolution will be the default. It's not a
    712      1.28   msaitoh 	 * fatal error.
    713      1.28   msaitoh 	 */
    714      1.28   msaitoh 	rv = sdtemp_write_16(sc, SDTEMP_REG_IDT_RESOLUTION,
    715      1.28   msaitoh 	    __SHIFTIN(SDTEMP_CAP_RESOLUTION_MAX, SDTEMP_CAP_RESOLUTION));
    716      1.28   msaitoh 	if (rv == 0)
    717      1.28   msaitoh 		sc->sc_resolution = SDTEMP_CAP_RESOLUTION_MAX;
    718      1.28   msaitoh 	else
    719  1.35.4.2    martin 		aprint_debug_dev(sc->sc_dev,
    720  1.35.4.2    martin 		    "error %d writing resolution register\n", rv);
    721      1.28   msaitoh }
    722      1.29   msaitoh 
    723      1.29   msaitoh MODULE(MODULE_CLASS_DRIVER, sdtemp, "i2cexec,sysmon_envsys");
    724      1.29   msaitoh 
    725      1.29   msaitoh #ifdef _MODULE
    726      1.29   msaitoh #include "ioconf.c"
    727      1.29   msaitoh #endif
    728      1.29   msaitoh 
    729      1.29   msaitoh static int
    730      1.29   msaitoh sdtemp_modcmd(modcmd_t cmd, void *opaque)
    731      1.29   msaitoh {
    732      1.29   msaitoh 	int error = 0;
    733      1.29   msaitoh 
    734      1.29   msaitoh 	switch (cmd) {
    735      1.29   msaitoh 	case MODULE_CMD_INIT:
    736      1.29   msaitoh #ifdef _MODULE
    737      1.29   msaitoh 		error = config_init_component(cfdriver_ioconf_sdtemp,
    738      1.29   msaitoh 		    cfattach_ioconf_sdtemp, cfdata_ioconf_sdtemp);
    739      1.29   msaitoh #endif
    740      1.29   msaitoh 		return error;
    741      1.29   msaitoh 	case MODULE_CMD_FINI:
    742      1.29   msaitoh #ifdef _MODULE
    743      1.29   msaitoh 		error = config_fini_component(cfdriver_ioconf_sdtemp,
    744      1.29   msaitoh 		    cfattach_ioconf_sdtemp, cfdata_ioconf_sdtemp);
    745      1.29   msaitoh #endif
    746      1.29   msaitoh 		return error;
    747      1.29   msaitoh 	default:
    748      1.29   msaitoh 		return ENOTTY;
    749      1.29   msaitoh 	}
    750      1.29   msaitoh }
    751