Home | History | Annotate | Line # | Download | only in i2c
si70xx.c revision 1.6
      1  1.6       jdc /*	$NetBSD: si70xx.c,v 1.6 2020/12/05 14:50:33 jdc Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 2017 Brad Spencer <brad (at) anduin.eldar.org>
      5  1.1  christos  *
      6  1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      7  1.1  christos  * purpose with or without fee is hereby granted, provided that the above
      8  1.1  christos  * copyright notice and this permission notice appear in all copies.
      9  1.1  christos  *
     10  1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  1.1  christos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13  1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15  1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16  1.1  christos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  1.1  christos  */
     18  1.1  christos 
     19  1.1  christos #include <sys/cdefs.h>
     20  1.6       jdc __KERNEL_RCSID(0, "$NetBSD: si70xx.c,v 1.6 2020/12/05 14:50:33 jdc Exp $");
     21  1.1  christos 
     22  1.1  christos /*
     23  1.1  christos   Driver for the Silicon Labs SI7013/SI7020/SI7021
     24  1.1  christos */
     25  1.1  christos 
     26  1.1  christos #include <sys/param.h>
     27  1.1  christos #include <sys/systm.h>
     28  1.1  christos #include <sys/kernel.h>
     29  1.1  christos #include <sys/device.h>
     30  1.1  christos #include <sys/module.h>
     31  1.1  christos #include <sys/sysctl.h>
     32  1.1  christos #include <sys/mutex.h>
     33  1.1  christos 
     34  1.1  christos #include <dev/sysmon/sysmonvar.h>
     35  1.1  christos #include <dev/i2c/i2cvar.h>
     36  1.1  christos #include <dev/i2c/si70xxreg.h>
     37  1.1  christos #include <dev/i2c/si70xxvar.h>
     38  1.1  christos 
     39  1.1  christos 
     40  1.1  christos static uint8_t 	si70xx_crc(uint8_t *, size_t);
     41  1.1  christos static int 	si70xx_poke(i2c_tag_t, i2c_addr_t, bool);
     42  1.1  christos static int 	si70xx_match(device_t, cfdata_t, void *);
     43  1.1  christos static void 	si70xx_attach(device_t, device_t, void *);
     44  1.1  christos static int 	si70xx_detach(device_t, int);
     45  1.1  christos static void 	si70xx_refresh(struct sysmon_envsys *, envsys_data_t *);
     46  1.1  christos static int 	si70xx_update_status(struct si70xx_sc *);
     47  1.1  christos static int 	si70xx_set_heateron(struct si70xx_sc *);
     48  1.1  christos static int 	si70xx_set_resolution(struct si70xx_sc *, size_t);
     49  1.1  christos static int 	si70xx_set_heatervalue(struct si70xx_sc *, size_t);
     50  1.1  christos static int 	si70xx_verify_sysctl(SYSCTLFN_ARGS);
     51  1.1  christos static int 	si70xx_verify_sysctl_resolution(SYSCTLFN_ARGS);
     52  1.1  christos static int 	si70xx_verify_sysctl_heateron(SYSCTLFN_ARGS);
     53  1.1  christos static int 	si70xx_verify_sysctl_heatervalue(SYSCTLFN_ARGS);
     54  1.1  christos 
     55  1.1  christos #define SI70XX_DEBUG
     56  1.1  christos #ifdef SI70XX_DEBUG
     57  1.1  christos #define DPRINTF(s, l, x) \
     58  1.1  christos     do { \
     59  1.1  christos 	if (l <= s->sc_si70xxdebug) \
     60  1.1  christos 	    printf x; \
     61  1.1  christos     } while (/*CONSTCOND*/0)
     62  1.1  christos #else
     63  1.1  christos #define DPRINTF(s, l, x)
     64  1.1  christos #endif
     65  1.1  christos 
     66  1.1  christos CFATTACH_DECL_NEW(si70xxtemp, sizeof(struct si70xx_sc),
     67  1.1  christos     si70xx_match, si70xx_attach, si70xx_detach, NULL);
     68  1.1  christos 
     69  1.1  christos static struct si70xx_sensor si70xx_sensors[] = {
     70  1.1  christos 	{
     71  1.1  christos 		.desc = "humidity",
     72  1.1  christos 		.type = ENVSYS_SRELHUMIDITY,
     73  1.1  christos 	},
     74  1.1  christos 	{
     75  1.1  christos 		.desc = "temperature",
     76  1.1  christos 		.type = ENVSYS_STEMP,
     77  1.1  christos 	}
     78  1.1  christos };
     79  1.1  christos 
     80  1.1  christos static struct si70xx_resolution si70xx_resolutions[] = {
     81  1.1  christos 	{
     82  1.1  christos 		.text = "12bit/14bit",
     83  1.1  christos 		.num = 0x00,
     84  1.1  christos 	},
     85  1.1  christos 	{
     86  1.1  christos 		.text = "8bit/12bit",
     87  1.1  christos 		.num = 0x01,
     88  1.1  christos 	},
     89  1.1  christos 	{
     90  1.1  christos 		.text = "10bit/13bit",
     91  1.1  christos 		.num = 0x80,
     92  1.1  christos 	},
     93  1.1  christos 	{
     94  1.1  christos 		.text = "11bit/11bit",
     95  1.1  christos 		.num = 0x81,
     96  1.1  christos 	}
     97  1.1  christos };
     98  1.1  christos 
     99  1.1  christos static const char si70xx_resolution_names[] =
    100  1.1  christos     "12bit/14bit, 8bit/12bit, 10bit/13bit, 11bit/11bit";
    101  1.1  christos 
    102  1.1  christos static const int si70xx_heatervalues[] = {
    103  1.1  christos     0xdeadbeef, 0x00, 0x01, 0x02, 0x04, 0x08, 0x0f
    104  1.1  christos };
    105  1.1  christos 
    106  1.1  christos int
    107  1.1  christos si70xx_verify_sysctl(SYSCTLFN_ARGS)
    108  1.1  christos {
    109  1.1  christos 	int error, t;
    110  1.1  christos 	struct sysctlnode node;
    111  1.1  christos 
    112  1.1  christos 	node = *rnode;
    113  1.1  christos 	t = *(int *)rnode->sysctl_data;
    114  1.1  christos 	node.sysctl_data = &t;
    115  1.1  christos 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    116  1.1  christos 	if (error || newp == NULL)
    117  1.1  christos 		return error;
    118  1.1  christos 
    119  1.1  christos 	if (t < 0)
    120  1.1  christos 		return EINVAL;
    121  1.1  christos 
    122  1.1  christos 	*(int *)rnode->sysctl_data = t;
    123  1.1  christos 
    124  1.1  christos 	return 0;
    125  1.1  christos }
    126  1.1  christos 
    127  1.1  christos int
    128  1.1  christos si70xx_verify_sysctl_resolution(SYSCTLFN_ARGS)
    129  1.1  christos {
    130  1.1  christos 	char buf[SI70XX_RES_NAME];
    131  1.1  christos 	struct si70xx_sc *sc;
    132  1.1  christos 	struct sysctlnode node;
    133  1.1  christos 	int error = 0;
    134  1.1  christos 	size_t i;
    135  1.1  christos 
    136  1.1  christos 	node = *rnode;
    137  1.1  christos 	sc = node.sysctl_data;
    138  1.1  christos 	(void) memcpy(buf, sc->sc_resolution, SI70XX_RES_NAME);
    139  1.1  christos 	node.sysctl_data = buf;
    140  1.1  christos 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    141  1.1  christos 	if (error || newp == NULL)
    142  1.1  christos 		return error;
    143  1.1  christos 
    144  1.1  christos 	for (i = 0; i < __arraycount(si70xx_resolutions); i++) {
    145  1.1  christos 		if (memcmp(node.sysctl_data, si70xx_resolutions[i].text,
    146  1.1  christos 		    SI70XX_RES_NAME) == 0)
    147  1.1  christos 			break;
    148  1.1  christos 	}
    149  1.1  christos 
    150  1.1  christos 	if (i == __arraycount(si70xx_resolutions))
    151  1.1  christos 		return EINVAL;
    152  1.1  christos 	(void) memcpy(sc->sc_resolution, node.sysctl_data, SI70XX_RES_NAME);
    153  1.1  christos 
    154  1.1  christos 	error = si70xx_set_resolution(sc, i);
    155  1.1  christos 
    156  1.1  christos 	return error;
    157  1.1  christos }
    158  1.1  christos 
    159  1.1  christos int
    160  1.1  christos si70xx_verify_sysctl_heateron(SYSCTLFN_ARGS)
    161  1.1  christos {
    162  1.1  christos 	int 		error;
    163  1.1  christos 	bool 		t;
    164  1.1  christos 	struct si70xx_sc *sc;
    165  1.1  christos 	struct sysctlnode node;
    166  1.1  christos 
    167  1.1  christos 	node = *rnode;
    168  1.1  christos 	sc = node.sysctl_data;
    169  1.1  christos 	t = sc->sc_heateron;
    170  1.1  christos 	node.sysctl_data = &t;
    171  1.1  christos 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    172  1.1  christos 	if (error || newp == NULL)
    173  1.1  christos 		return error;
    174  1.1  christos 
    175  1.1  christos 	sc->sc_heateron = t;
    176  1.1  christos 	error = si70xx_set_heateron(sc);
    177  1.1  christos 
    178  1.1  christos 	return error;
    179  1.1  christos }
    180  1.1  christos 
    181  1.1  christos int
    182  1.1  christos si70xx_verify_sysctl_heatervalue(SYSCTLFN_ARGS)
    183  1.1  christos {
    184  1.1  christos 	int 		error = 0, t;
    185  1.1  christos 	struct si70xx_sc *sc;
    186  1.1  christos 	struct sysctlnode node;
    187  1.1  christos 
    188  1.1  christos 	node = *rnode;
    189  1.1  christos 	sc = node.sysctl_data;
    190  1.1  christos 	t = sc->sc_heaterval;
    191  1.1  christos 	node.sysctl_data = &t;
    192  1.1  christos 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    193  1.1  christos 	if (error || newp == NULL)
    194  1.1  christos 		return (error);
    195  1.1  christos 
    196  1.1  christos 	if (t < 1 || t >= __arraycount(si70xx_heatervalues))
    197  1.1  christos 		return (EINVAL);
    198  1.1  christos 
    199  1.1  christos 	sc->sc_heaterval = t;
    200  1.1  christos 	error = si70xx_set_heatervalue(sc, t);
    201  1.1  christos 
    202  1.1  christos 	return error;
    203  1.1  christos }
    204  1.1  christos 
    205  1.3  christos static uint8_t
    206  1.3  christos si70xx_dir(uint8_t cmd, size_t len)
    207  1.1  christos {
    208  1.3  christos 	switch (cmd) {
    209  1.1  christos 	case SI70XX_READ_USER_REG_1:
    210  1.1  christos 	case SI70XX_READ_HEATER_REG:
    211  1.1  christos 	case SI70XX_READ_ID_PT1A:
    212  1.1  christos 	case SI70XX_READ_ID_PT1B:
    213  1.1  christos 	case SI70XX_READ_ID_PT2A:
    214  1.1  christos 	case SI70XX_READ_ID_PT2B:
    215  1.1  christos 	case SI70XX_READ_FW_VERA:
    216  1.1  christos 	case SI70XX_READ_FW_VERB:
    217  1.3  christos 		return I2C_OP_READ_WITH_STOP;
    218  1.1  christos 	case SI70XX_WRITE_USER_REG_1:
    219  1.1  christos 	case SI70XX_WRITE_HEATER_REG:
    220  1.1  christos 	case SI70XX_RESET:
    221  1.3  christos 		return I2C_OP_WRITE_WITH_STOP;
    222  1.1  christos 	case SI70XX_MEASURE_RH_NOHOLD:
    223  1.1  christos 	case SI70XX_MEASURE_TEMP_NOHOLD:
    224  1.3  christos 		return len == 0 ? I2C_OP_READ : I2C_OP_READ_WITH_STOP;
    225  1.1  christos 	default:
    226  1.3  christos 		panic("%s: bad command %#x\n", __func__, cmd);
    227  1.3  christos 		return 0;
    228  1.1  christos 	}
    229  1.3  christos }
    230  1.3  christos 
    231  1.3  christos static int
    232  1.3  christos si70xx_cmd(i2c_tag_t tag, i2c_addr_t addr, uint8_t *cmd,
    233  1.3  christos     uint8_t clen, uint8_t *buf, size_t blen)
    234  1.3  christos {
    235  1.3  christos 	uint8_t dir;
    236  1.3  christos 	if (clen == 0)
    237  1.3  christos 		dir = blen == 0 ? I2C_OP_READ : I2C_OP_READ_WITH_STOP;
    238  1.3  christos 	else
    239  1.3  christos 		dir = si70xx_dir(cmd[0], blen);
    240  1.3  christos 
    241  1.3  christos 	if (dir == I2C_OP_READ || dir == I2C_OP_READ_WITH_STOP)
    242  1.3  christos 		memset(buf, 0, blen);
    243  1.1  christos 
    244  1.1  christos 	return iic_exec(tag, dir, addr, cmd, clen, buf, blen, 0);
    245  1.1  christos }
    246  1.1  christos 
    247  1.1  christos static int
    248  1.1  christos si70xx_cmd0(struct si70xx_sc *sc, uint8_t *buf, size_t blen)
    249  1.1  christos {
    250  1.1  christos 	return si70xx_cmd(sc->sc_tag, sc->sc_addr, NULL, 0, buf, blen);
    251  1.1  christos }
    252  1.1  christos 
    253  1.1  christos static int
    254  1.1  christos si70xx_cmd1(struct si70xx_sc *sc, uint8_t cmd, uint8_t *buf, size_t blen)
    255  1.1  christos {
    256  1.1  christos 	return si70xx_cmd(sc->sc_tag, sc->sc_addr, &cmd, 1, buf, blen);
    257  1.1  christos }
    258  1.1  christos 
    259  1.1  christos static int
    260  1.1  christos si70xx_cmd2(struct si70xx_sc *sc, uint8_t cmd1, uint8_t cmd2, uint8_t *buf,
    261  1.1  christos     size_t blen)
    262  1.1  christos {
    263  1.1  christos 	uint8_t cmd[] = { cmd1, cmd2 };
    264  1.1  christos 	return si70xx_cmd(sc->sc_tag, sc->sc_addr, cmd, __arraycount(cmd),
    265  1.1  christos 	    buf, blen);
    266  1.1  christos }
    267  1.1  christos 
    268  1.1  christos static int
    269  1.1  christos si70xx_set_heateron(struct si70xx_sc * sc)
    270  1.1  christos {
    271  1.1  christos 	int error;
    272  1.1  christos 	uint8_t userregister;
    273  1.1  christos 
    274  1.1  christos 	error = iic_acquire_bus(sc->sc_tag, 0);
    275  1.1  christos 	if (error) {
    276  1.1  christos 		DPRINTF(sc, 2, ("%s:%s: Failed to acquire bus: %d\n",
    277  1.1  christos 		    device_xname(sc->sc_dev), __func__, error));
    278  1.1  christos 		return error;
    279  1.1  christos 	}
    280  1.1  christos 
    281  1.1  christos 	error = si70xx_cmd1(sc, SI70XX_READ_USER_REG_1, &userregister, 1);
    282  1.1  christos 	if (error) {
    283  1.1  christos 		DPRINTF(sc, 2, ("%s: Failed to read user register 1: %d\n",
    284  1.1  christos 		    device_xname(sc->sc_dev), error));
    285  1.1  christos 		goto out;
    286  1.1  christos 	}
    287  1.1  christos 
    288  1.1  christos 	DPRINTF(sc, 2, ("%s:%s: reg 1 values before: %#x\n",
    289  1.1  christos 	    device_xname(sc->sc_dev), __func__, userregister));
    290  1.1  christos 	if (sc->sc_heateron) {
    291  1.1  christos 		userregister |= SI70XX_HTRE_MASK;
    292  1.1  christos 	} else {
    293  1.1  christos 		userregister &= ~SI70XX_HTRE_MASK;
    294  1.1  christos 	}
    295  1.1  christos 	DPRINTF(sc, 2, ("%s:%s: user reg 1 values after: %#x\n",
    296  1.1  christos 	    device_xname(sc->sc_dev), __func__, userregister));
    297  1.1  christos 
    298  1.1  christos 	error = si70xx_cmd1(sc, SI70XX_WRITE_USER_REG_1, &userregister, 1);
    299  1.1  christos 	if (error) {
    300  1.1  christos 		DPRINTF(sc, 2, ("%s: Failed to write user register 1: %d\n",
    301  1.1  christos 		    device_xname(sc->sc_dev), error));
    302  1.1  christos 	}
    303  1.1  christos out:
    304  1.1  christos 	iic_release_bus(sc->sc_tag, 0);
    305  1.1  christos 	return error;
    306  1.1  christos }
    307  1.1  christos 
    308  1.1  christos static int
    309  1.1  christos si70xx_set_resolution(struct si70xx_sc * sc, size_t index)
    310  1.1  christos {
    311  1.1  christos 	int error;
    312  1.1  christos 	uint8_t userregister;
    313  1.1  christos 
    314  1.1  christos 	error = iic_acquire_bus(sc->sc_tag, 0);
    315  1.1  christos 	if (error) {
    316  1.1  christos 		DPRINTF(sc, 2, ("%s: Failed to acquire bus: %d\n",
    317  1.1  christos 		    device_xname(sc->sc_dev), error));
    318  1.1  christos 		return error;
    319  1.1  christos 	}
    320  1.1  christos 
    321  1.1  christos 	error = si70xx_cmd1(sc, SI70XX_READ_USER_REG_1, &userregister, 1);
    322  1.1  christos 	if (error) {
    323  1.1  christos 		DPRINTF(sc, 2, ("%s: Failed to read user register 1: %d\n",
    324  1.1  christos 		    device_xname(sc->sc_dev), error));
    325  1.1  christos 		goto out;
    326  1.1  christos 	}
    327  1.1  christos 
    328  1.1  christos 	DPRINTF(sc, 2, ("%s:%s: reg 1 values before: %#x\n",
    329  1.1  christos 	    device_xname(sc->sc_dev), __func__, userregister));
    330  1.1  christos 	userregister &= (~SI70XX_RESOLUTION_MASK);
    331  1.1  christos 	userregister |= si70xx_resolutions[index].num;
    332  1.1  christos 	DPRINTF(sc, 2, ("%s:%s: reg 1 values after: %#x\n",
    333  1.1  christos 	    device_xname(sc->sc_dev), __func__, userregister));
    334  1.1  christos 
    335  1.1  christos 	error = si70xx_cmd1(sc, SI70XX_WRITE_USER_REG_1, &userregister, 1);
    336  1.1  christos 	if (error) {
    337  1.1  christos 		DPRINTF(sc, 2, ("%s: Failed to write user register 1: %d\n",
    338  1.1  christos 		    device_xname(sc->sc_dev), error));
    339  1.1  christos 	}
    340  1.1  christos out:
    341  1.1  christos 	iic_release_bus(sc->sc_tag, 0);
    342  1.1  christos 	return error;
    343  1.1  christos }
    344  1.1  christos 
    345  1.1  christos static int
    346  1.1  christos si70xx_set_heatervalue(struct si70xx_sc * sc, size_t index)
    347  1.1  christos {
    348  1.1  christos 	int error;
    349  1.1  christos 	uint8_t heaterregister;
    350  1.1  christos 
    351  1.1  christos 	error = iic_acquire_bus(sc->sc_tag, 0);
    352  1.1  christos 	if (error) {
    353  1.1  christos 		DPRINTF(sc, 2, ("%s: Failed to acquire bus: %d\n",
    354  1.1  christos 		    device_xname(sc->sc_dev), error));
    355  1.1  christos 		return error;
    356  1.1  christos 	}
    357  1.1  christos 	error = si70xx_cmd1(sc, SI70XX_READ_HEATER_REG, &heaterregister, 1);
    358  1.1  christos 	if (error) {
    359  1.1  christos 		DPRINTF(sc, 2, ("%s: Failed to read heater register: %d\n",
    360  1.1  christos 		    device_xname(sc->sc_dev), error));
    361  1.1  christos 		goto out;
    362  1.1  christos 	}
    363  1.1  christos 
    364  1.1  christos 	DPRINTF(sc, 2, ("%s:%s: heater values before: %#x\n",
    365  1.1  christos 	    device_xname(sc->sc_dev), __func__, heaterregister));
    366  1.1  christos 	heaterregister &= ~SI70XX_HEATER_MASK;
    367  1.1  christos 	heaterregister |= si70xx_heatervalues[index];
    368  1.1  christos 	DPRINTF(sc, 2, ("%s:%s: heater values after: %#x\n",
    369  1.1  christos 	    device_xname(sc->sc_dev), __func__, heaterregister));
    370  1.1  christos 
    371  1.3  christos 	error = si70xx_cmd1(sc, SI70XX_WRITE_HEATER_REG, &heaterregister, 1);
    372  1.1  christos 	if (error) {
    373  1.1  christos 		DPRINTF(sc, 2, ("%s: Failed to write heater register: %d\n",
    374  1.1  christos 		    device_xname(sc->sc_dev), error));
    375  1.1  christos 	}
    376  1.1  christos out:
    377  1.1  christos 	iic_release_bus(sc->sc_tag, 0);
    378  1.1  christos 	return error;
    379  1.1  christos }
    380  1.1  christos 
    381  1.1  christos static int
    382  1.1  christos si70xx_update_heater(struct si70xx_sc *sc)
    383  1.1  christos {
    384  1.1  christos 	size_t i;
    385  1.1  christos 	int error;
    386  1.1  christos 	uint8_t heaterregister;
    387  1.1  christos 
    388  1.1  christos 	error = si70xx_cmd1(sc, SI70XX_READ_HEATER_REG, &heaterregister, 1);
    389  1.1  christos 	if (error) {
    390  1.1  christos 		DPRINTF(sc, 2, ("%s: Failed to read heater register: %d\n",
    391  1.1  christos 		    device_xname(sc->sc_dev), error));
    392  1.1  christos 		return error;
    393  1.1  christos 	}
    394  1.1  christos 
    395  1.1  christos 	DPRINTF(sc, 2, ("%s: read heater reg values: %02x\n",
    396  1.1  christos 	    device_xname(sc->sc_dev), heaterregister));
    397  1.1  christos 
    398  1.1  christos 	uint8_t heat = heaterregister & SI70XX_HEATER_MASK;
    399  1.1  christos 	for (i = 0; i < __arraycount(si70xx_heatervalues); i++) {
    400  1.1  christos 		if (si70xx_heatervalues[i] == heat)
    401  1.1  christos 			break;
    402  1.1  christos 	}
    403  1.1  christos 	sc->sc_heaterval = i != __arraycount(si70xx_heatervalues) ? i : 0;
    404  1.1  christos 	return 0;
    405  1.1  christos }
    406  1.1  christos 
    407  1.1  christos static int
    408  1.1  christos si70xx_update_user(struct si70xx_sc *sc)
    409  1.1  christos {
    410  1.1  christos 	size_t i;
    411  1.1  christos 	int error;
    412  1.1  christos 	uint8_t userregister;
    413  1.1  christos 
    414  1.1  christos 	error = si70xx_cmd1(sc, SI70XX_READ_USER_REG_1, &userregister, 1);
    415  1.1  christos 	if (error) {
    416  1.1  christos 		DPRINTF(sc, 2, ("%s: Failed to read user register 1: %d\n",
    417  1.1  christos 		    device_xname(sc->sc_dev), error));
    418  1.1  christos 		return error;
    419  1.1  christos 	}
    420  1.1  christos 	DPRINTF(sc, 2, ("%s: read user reg 1 values: %#x\n",
    421  1.1  christos 	    device_xname(sc->sc_dev), userregister));
    422  1.1  christos 
    423  1.1  christos 	uint8_t res = userregister & SI70XX_RESOLUTION_MASK;
    424  1.1  christos 	for (i = 0; i < __arraycount(si70xx_resolutions); i++) {
    425  1.1  christos 		if (si70xx_resolutions[i].num == res)
    426  1.1  christos 			break;
    427  1.1  christos 	}
    428  1.1  christos 
    429  1.1  christos 	if (i != __arraycount(si70xx_resolutions)) {
    430  1.1  christos 		memcpy(sc->sc_resolution, si70xx_resolutions[i].text,
    431  1.1  christos 		    SI70XX_RES_NAME);
    432  1.1  christos 	} else {
    433  1.1  christos 		snprintf(sc->sc_resolution, SI70XX_RES_NAME, "%02x", res);
    434  1.1  christos 	}
    435  1.1  christos 
    436  1.1  christos 	sc->sc_vddok = (userregister & SI70XX_VDDS_MASK) == 0;
    437  1.1  christos 	sc->sc_heaterval = userregister & SI70XX_HTRE_MASK;
    438  1.1  christos 	return 0;
    439  1.1  christos }
    440  1.1  christos 
    441  1.1  christos static int
    442  1.1  christos si70xx_update_status(struct si70xx_sc *sc)
    443  1.1  christos {
    444  1.1  christos 	int error1 = si70xx_update_user(sc);
    445  1.1  christos 	int error2 = si70xx_update_heater(sc);
    446  1.1  christos 	return error1 ? error1 : error2;
    447  1.1  christos }
    448  1.1  christos 
    449  1.1  christos static	uint8_t
    450  1.1  christos si70xx_crc(uint8_t * data, size_t size)
    451  1.1  christos {
    452  1.1  christos 	uint8_t crc = 0;
    453  1.1  christos 
    454  1.1  christos 	for (size_t i = 0; i < size; i++) {
    455  1.1  christos 		crc ^= data[i];
    456  1.1  christos 		for (size_t j = 8; j > 0; j--) {
    457  1.1  christos 			if (crc & 0x80)
    458  1.1  christos 				crc = (crc << 1) ^ 0x131;
    459  1.1  christos 			else
    460  1.1  christos 				crc <<= 1;
    461  1.1  christos 		}
    462  1.1  christos 	}
    463  1.1  christos 	return crc;
    464  1.1  christos }
    465  1.1  christos 
    466  1.1  christos static int
    467  1.1  christos si70xx_poke(i2c_tag_t tag, i2c_addr_t addr, bool matchdebug)
    468  1.1  christos {
    469  1.1  christos 	uint8_t reg = SI70XX_READ_USER_REG_1;
    470  1.1  christos 	uint8_t buf;
    471  1.1  christos 	int error;
    472  1.1  christos 
    473  1.1  christos 	error = si70xx_cmd(tag, addr, &reg, 1, &buf, 1);
    474  1.1  christos 	if (matchdebug) {
    475  1.1  christos 		printf("poke X 1: %d\n", error);
    476  1.1  christos 	}
    477  1.1  christos 	return error;
    478  1.1  christos }
    479  1.1  christos 
    480  1.1  christos static int
    481  1.1  christos si70xx_sysctl_init(struct si70xx_sc *sc)
    482  1.1  christos {
    483  1.1  christos 	int error;
    484  1.1  christos 	const struct sysctlnode *cnode;
    485  1.1  christos 	int sysctlroot_num;
    486  1.1  christos 
    487  1.1  christos 	if ((error = sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
    488  1.1  christos 	    0, CTLTYPE_NODE, device_xname(sc->sc_dev),
    489  1.1  christos 	    SYSCTL_DESCR("si70xx controls"), NULL, 0, NULL, 0, CTL_HW,
    490  1.1  christos 	    CTL_CREATE, CTL_EOL)) != 0)
    491  1.1  christos 		return error;
    492  1.1  christos 
    493  1.1  christos 	sysctlroot_num = cnode->sysctl_num;
    494  1.1  christos 
    495  1.1  christos #ifdef SI70XX_DEBUG
    496  1.1  christos 	if ((error = sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
    497  1.1  christos 	    CTLFLAG_READWRITE, CTLTYPE_INT, "debug",
    498  1.1  christos 	    SYSCTL_DESCR("Debug level"), si70xx_verify_sysctl, 0,
    499  1.1  christos 	    &sc->sc_si70xxdebug, 0, CTL_HW, sysctlroot_num, CTL_CREATE,
    500  1.1  christos 	    CTL_EOL)) != 0)
    501  1.1  christos 		return error;
    502  1.1  christos 
    503  1.1  christos #endif
    504  1.1  christos 
    505  1.1  christos #ifdef HAVE_I2C_EXECV
    506  1.1  christos 	if ((error = sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
    507  1.1  christos 	    CTLFLAG_READWRITE, CTLTYPE_INT, "clockstretch",
    508  1.1  christos 	    SYSCTL_DESCR("Clockstretch value"), si70xx_verify_sysctl, 0,
    509  1.1  christos 	    &sc->sc_clockstretch, 0, CTL_HW, sysctlroot_num, CTL_CREATE,
    510  1.1  christos 	    CTL_EOL)) != 0)
    511  1.1  christos 		return error;
    512  1.1  christos #endif
    513  1.1  christos 
    514  1.1  christos 
    515  1.1  christos 	if ((error = sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
    516  1.1  christos 	    CTLFLAG_READWRITE, CTLTYPE_INT, "readattempts",
    517  1.1  christos 	    SYSCTL_DESCR("The number of times to attempt to read the values"),
    518  1.1  christos 	    si70xx_verify_sysctl, 0, &sc->sc_readattempts, 0, CTL_HW,
    519  1.1  christos 	    sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
    520  1.1  christos 		return error;
    521  1.1  christos 
    522  1.1  christos 
    523  1.1  christos 	if ((error = sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
    524  1.1  christos 	    CTLFLAG_READONLY, CTLTYPE_STRING, "resolutions",
    525  1.1  christos 	    SYSCTL_DESCR("Valid resolutions"), 0, 0,
    526  1.1  christos 	    __UNCONST(si70xx_resolution_names),
    527  1.1  christos 	    sizeof(si70xx_resolution_names) + 1,
    528  1.1  christos 	    CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
    529  1.1  christos 		return error;
    530  1.1  christos 
    531  1.1  christos 	if ((error = sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
    532  1.1  christos 	    CTLFLAG_READWRITE, CTLTYPE_STRING, "resolution",
    533  1.1  christos 	    SYSCTL_DESCR("Resolution of RH and Temp"),
    534  1.1  christos 	    si70xx_verify_sysctl_resolution, 0, (void *) sc,
    535  1.1  christos 	    SI70XX_RES_NAME, CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
    536  1.1  christos 		return error;
    537  1.1  christos 
    538  1.1  christos 	if ((error = sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
    539  1.1  christos 	    CTLFLAG_READWRITE, CTLTYPE_BOOL, "ignorecrc",
    540  1.1  christos 	    SYSCTL_DESCR("Ignore the CRC byte"), NULL, 0, &sc->sc_ignorecrc,
    541  1.1  christos 	    0, CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
    542  1.1  christos 		return error;
    543  1.1  christos 
    544  1.1  christos 	if ((error = sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
    545  1.1  christos 	    CTLFLAG_READONLY, CTLTYPE_BOOL, "vddok",
    546  1.1  christos 	    SYSCTL_DESCR("Vdd at least 1.9v"), NULL, 0, &sc->sc_vddok, 0,
    547  1.1  christos 	    CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
    548  1.1  christos 		return error;
    549  1.1  christos 
    550  1.1  christos 	if ((error = sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
    551  1.1  christos 	    CTLFLAG_READWRITE, CTLTYPE_BOOL, "heateron",
    552  1.1  christos 	    SYSCTL_DESCR("Heater on"), si70xx_verify_sysctl_heateron, 0,
    553  1.1  christos 	    (void *)sc, 0, CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
    554  1.1  christos 		return error;
    555  1.1  christos 
    556  1.1  christos 	return sysctl_createv(&sc->sc_si70xxlog, 0, NULL, &cnode,
    557  1.1  christos 	    CTLFLAG_READWRITE, CTLTYPE_INT, "heaterstrength",
    558  1.1  christos 	    SYSCTL_DESCR("Heater strength 1 to 6"),
    559  1.1  christos 	    si70xx_verify_sysctl_heatervalue, 0, (void *)sc, 0, CTL_HW,
    560  1.1  christos 	    sysctlroot_num, CTL_CREATE, CTL_EOL);
    561  1.1  christos }
    562  1.1  christos 
    563  1.1  christos static int
    564  1.1  christos si70xx_match(device_t parent, cfdata_t match, void *aux)
    565  1.1  christos {
    566  1.4   thorpej 	struct i2c_attach_args *ia = aux;
    567  1.4   thorpej 	int error, match_result;
    568  1.1  christos 	const bool matchdebug = false;
    569  1.1  christos 
    570  1.4   thorpej 	if (iic_use_direct_match(ia, match, NULL, &match_result))
    571  1.4   thorpej 		return match_result;
    572  1.1  christos 
    573  1.4   thorpej 	/* indirect config - check for configured address */
    574  1.4   thorpej 	if (ia->ia_addr != SI70XX_TYPICAL_ADDR)
    575  1.4   thorpej 		return 0;
    576  1.1  christos 
    577  1.1  christos 	/*
    578  1.1  christos 	 * Check to see if something is really at this i2c address. This will
    579  1.1  christos 	 * keep phantom devices from appearing
    580  1.1  christos 	 */
    581  1.1  christos 	if (iic_acquire_bus(ia->ia_tag, 0) != 0) {
    582  1.1  christos 		if (matchdebug)
    583  1.1  christos 			printf("in match acquire bus failed\n");
    584  1.1  christos 		return 0;
    585  1.1  christos 	}
    586  1.1  christos 
    587  1.1  christos 	error = si70xx_poke(ia->ia_tag, ia->ia_addr, matchdebug);
    588  1.1  christos 	iic_release_bus(ia->ia_tag, 0);
    589  1.1  christos 
    590  1.4   thorpej 	return error == 0 ? I2C_MATCH_ADDRESS_AND_PROBE : 0;
    591  1.1  christos }
    592  1.1  christos 
    593  1.1  christos static void
    594  1.1  christos si70xx_attach(device_t parent, device_t self, void *aux)
    595  1.1  christos {
    596  1.1  christos 	struct si70xx_sc *sc;
    597  1.1  christos 	struct i2c_attach_args *ia;
    598  1.1  christos 	int error, i;
    599  1.1  christos 	int ecount = 0;
    600  1.1  christos 	uint8_t buf[8];
    601  1.1  christos 	uint8_t testcrcpt1[4];
    602  1.1  christos 	uint8_t testcrcpt2[4];
    603  1.1  christos 	uint8_t crc1 = 0, crc2 = 0;
    604  1.1  christos 	uint8_t readcrc1 = 0, readcrc2 = 0;
    605  1.1  christos 	uint8_t fwversion, model;
    606  1.1  christos 
    607  1.1  christos 	ia = aux;
    608  1.1  christos 	sc = device_private(self);
    609  1.1  christos 
    610  1.1  christos 	sc->sc_dev = self;
    611  1.1  christos 	sc->sc_tag = ia->ia_tag;
    612  1.1  christos 	sc->sc_addr = ia->ia_addr;
    613  1.1  christos 	sc->sc_si70xxdebug = 0;
    614  1.1  christos #ifdef HAVE_I2C_EXECV
    615  1.1  christos 	sc->sc_clockstretch = 2048;
    616  1.1  christos #endif
    617  1.3  christos 	sc->sc_readattempts = 25;
    618  1.1  christos 	sc->sc_ignorecrc = false;
    619  1.1  christos 	sc->sc_sme = NULL;
    620  1.1  christos 
    621  1.1  christos 	aprint_normal("\n");
    622  1.1  christos 
    623  1.1  christos 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
    624  1.1  christos 	sc->sc_numsensors = __arraycount(si70xx_sensors);
    625  1.1  christos 
    626  1.1  christos 	if ((sc->sc_sme = sysmon_envsys_create()) == NULL) {
    627  1.1  christos 		aprint_error_dev(self,
    628  1.1  christos 		    "Unable to create sysmon structure\n");
    629  1.1  christos 		sc->sc_sme = NULL;
    630  1.1  christos 		return;
    631  1.1  christos 	}
    632  1.1  christos 	if ((error = si70xx_sysctl_init(sc)) != 0) {
    633  1.1  christos 		aprint_error_dev(self, "Can't setup sysctl tree (%d)\n", error);
    634  1.1  christos 		goto out;
    635  1.1  christos 	}
    636  1.1  christos 
    637  1.1  christos 	error = iic_acquire_bus(sc->sc_tag, 0);
    638  1.1  christos 	if (error) {
    639  1.1  christos 		aprint_error_dev(self, "Could not acquire iic bus: %d\n",
    640  1.1  christos 		    error);
    641  1.1  christos 		goto out;
    642  1.1  christos 	}
    643  1.1  christos 	error = si70xx_cmd1(sc, SI70XX_RESET, NULL, 0);
    644  1.1  christos 	if (error != 0)
    645  1.1  christos 		aprint_error_dev(self, "Reset failed: %d\n", error);
    646  1.1  christos 
    647  1.1  christos 	delay(15000);	/* 15 ms max */
    648  1.1  christos 
    649  1.1  christos 	error = si70xx_cmd2(sc, SI70XX_READ_ID_PT1A, SI70XX_READ_ID_PT1B,
    650  1.1  christos 	    buf, 8);
    651  1.1  christos 	if (error) {
    652  1.1  christos 		aprint_error_dev(self, "Failed to read first part of ID: %d\n",
    653  1.1  christos 		    error);
    654  1.1  christos 		ecount++;
    655  1.1  christos 	}
    656  1.1  christos 	testcrcpt1[0] = buf[0];
    657  1.1  christos 	testcrcpt1[1] = buf[2];
    658  1.1  christos 	testcrcpt1[2] = buf[4];
    659  1.1  christos 	testcrcpt1[3] = buf[6];
    660  1.1  christos 	readcrc1 = buf[7];
    661  1.1  christos 	crc1 = si70xx_crc(testcrcpt1, 4);
    662  1.1  christos 
    663  1.1  christos 	DPRINTF(sc, 2, ("%s: read 1 values: %02x%02x%02x%02x%02x%02x%02x%02x "
    664  1.1  christos 	    "- %02x\n", device_xname(sc->sc_dev), buf[0], buf[1],
    665  1.1  christos 	    buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
    666  1.1  christos 	    crc1));
    667  1.1  christos 
    668  1.1  christos 	error = si70xx_cmd2(sc, SI70XX_READ_ID_PT2A, SI70XX_READ_ID_PT2B,
    669  1.1  christos 	    buf, 8);
    670  1.1  christos 	if (error != 0) {
    671  1.1  christos 		aprint_error_dev(self, "Failed to read second part of ID: %d\n",
    672  1.1  christos 		    error);
    673  1.1  christos 		ecount++;
    674  1.1  christos 	}
    675  1.1  christos 	model = testcrcpt2[0] = buf[0];
    676  1.1  christos 	testcrcpt2[1] = buf[1];
    677  1.1  christos 	testcrcpt2[2] = buf[3];
    678  1.1  christos 	testcrcpt2[3] = buf[4];
    679  1.1  christos 	readcrc2 = buf[5];
    680  1.1  christos 	crc2 = si70xx_crc(testcrcpt2, 4);
    681  1.1  christos 
    682  1.1  christos 	DPRINTF(sc, 2, ("%s: read 2 values: %02x%02x%02x%02x%02x%02x - %02x\n",
    683  1.1  christos 	    device_xname(sc->sc_dev), buf[0], buf[1], buf[2],
    684  1.1  christos 	    buf[3], buf[4], buf[5], crc2));
    685  1.1  christos 
    686  1.1  christos 	error = si70xx_cmd2(sc, SI70XX_READ_FW_VERA, SI70XX_READ_FW_VERB,
    687  1.1  christos 	    buf, 8);
    688  1.1  christos 
    689  1.1  christos 	if (error) {
    690  1.1  christos 		aprint_error_dev(self, "Failed to read firware version: %d\n",
    691  1.1  christos 		    error);
    692  1.1  christos 		ecount++;
    693  1.1  christos 	}
    694  1.1  christos 	fwversion = buf[0];
    695  1.1  christos 	DPRINTF(sc, 2, ("%s: read fw values: %#x\n", device_xname(sc->sc_dev),
    696  1.1  christos 	    fwversion));
    697  1.1  christos 
    698  1.1  christos 	error = si70xx_update_status(sc);
    699  1.1  christos 	iic_release_bus(sc->sc_tag, 0);
    700  1.1  christos 	if (error != 0) {
    701  1.1  christos 		aprint_error_dev(self, "Failed to update status: %x\n", error);
    702  1.1  christos 		aprint_error_dev(self, "Unable to setup device\n");
    703  1.1  christos 		goto out;
    704  1.1  christos 	}
    705  1.1  christos 
    706  1.1  christos 	for (i = 0; i < sc->sc_numsensors; i++) {
    707  1.1  christos 		strlcpy(sc->sc_sensors[i].desc, si70xx_sensors[i].desc,
    708  1.1  christos 		    sizeof(sc->sc_sensors[i].desc));
    709  1.1  christos 
    710  1.1  christos 		sc->sc_sensors[i].units = si70xx_sensors[i].type;
    711  1.1  christos 		sc->sc_sensors[i].state = ENVSYS_SINVALID;
    712  1.1  christos 
    713  1.1  christos 		DPRINTF(sc, 2, ("%s: registering sensor %d (%s)\n", __func__, i,
    714  1.1  christos 		    sc->sc_sensors[i].desc));
    715  1.1  christos 
    716  1.1  christos 		error = sysmon_envsys_sensor_attach(sc->sc_sme,
    717  1.1  christos 		    &sc->sc_sensors[i]);
    718  1.1  christos 		if (error) {
    719  1.1  christos 			aprint_error_dev(self,
    720  1.1  christos 			    "Unable to attach sensor %d: %d\n", i, error);
    721  1.6       jdc 			sc->sc_sme = NULL;
    722  1.1  christos 			goto out;
    723  1.1  christos 		}
    724  1.1  christos 	}
    725  1.1  christos 
    726  1.1  christos 	sc->sc_sme->sme_name = device_xname(sc->sc_dev);
    727  1.1  christos 	sc->sc_sme->sme_cookie = sc;
    728  1.1  christos 	sc->sc_sme->sme_refresh = si70xx_refresh;
    729  1.1  christos 
    730  1.1  christos 	DPRINTF(sc, 2, ("si70xx_attach: registering with envsys\n"));
    731  1.1  christos 
    732  1.1  christos 	if (sysmon_envsys_register(sc->sc_sme)) {
    733  1.1  christos 		aprint_error_dev(self,
    734  1.1  christos 			"unable to register with sysmon\n");
    735  1.1  christos 		sysmon_envsys_destroy(sc->sc_sme);
    736  1.1  christos 		sc->sc_sme = NULL;
    737  1.1  christos 		return;
    738  1.1  christos 	}
    739  1.1  christos 	if (ecount != 0) {
    740  1.1  christos 		aprint_normal_dev(self, "Could not read model, "
    741  1.1  christos 		    "probably an HTU21D\n");
    742  1.1  christos 		return;
    743  1.1  christos 	}
    744  1.1  christos 
    745  1.1  christos 	char modelstr[64];
    746  1.1  christos 	switch (model) {
    747  1.1  christos 	case 0:
    748  1.1  christos 	case 0xff:
    749  1.1  christos 		snprintf(modelstr, sizeof(modelstr), "Engineering Sample");
    750  1.5       mrg 		break;
    751  1.1  christos 	case 13:
    752  1.1  christos 	case 20:
    753  1.1  christos 	case 21:
    754  1.1  christos 		snprintf(modelstr, sizeof(modelstr), "SI70%d", model);
    755  1.1  christos 		break;
    756  1.1  christos 	default:
    757  1.1  christos 		snprintf(modelstr, sizeof(modelstr), "Unknown SI70%d", model);
    758  1.1  christos 		break;
    759  1.1  christos 	}
    760  1.1  christos 
    761  1.1  christos 	const char *fwversionstr;
    762  1.1  christos 	switch (fwversion) {
    763  1.1  christos 	case 0xff:
    764  1.1  christos 		fwversionstr = "1.0";
    765  1.1  christos 		break;
    766  1.1  christos 	case 0x20:
    767  1.1  christos 		fwversionstr = "2.0";
    768  1.1  christos 		break;
    769  1.1  christos 	default:
    770  1.1  christos 		fwversionstr = "unknown";
    771  1.1  christos 		break;
    772  1.1  christos 	}
    773  1.1  christos 
    774  1.1  christos 	aprint_normal_dev(self, "Silicon Labs Model: %s, "
    775  1.1  christos 	    "Firmware version: %s, "
    776  1.1  christos 	    "Serial number: %02x%02x%02x%02x%02x%02x%02x%02x%s",
    777  1.1  christos 	    modelstr, fwversionstr, testcrcpt1[0], testcrcpt1[1],
    778  1.1  christos 	    testcrcpt1[2], testcrcpt1[3], testcrcpt2[0], testcrcpt2[1],
    779  1.1  christos 	    testcrcpt2[2], testcrcpt2[3],
    780  1.1  christos 	    (crc1 == readcrc1 && crc2 == readcrc2) ? "\n" : " (bad crc)\n");
    781  1.1  christos 	return;
    782  1.1  christos out:
    783  1.1  christos 	sysmon_envsys_destroy(sc->sc_sme);
    784  1.1  christos 	sc->sc_sme = NULL;
    785  1.1  christos }
    786  1.1  christos 
    787  1.1  christos static int
    788  1.1  christos si70xx_exec(struct si70xx_sc *sc, uint8_t cmd, envsys_data_t *edata)
    789  1.1  christos {
    790  1.1  christos 	int error;
    791  1.1  christos 	int xdelay;
    792  1.1  christos 	const char *name;
    793  1.1  christos 	int64_t mul, offs;
    794  1.1  christos 	uint8_t buf[3];
    795  1.1  christos 
    796  1.1  christos 	switch (cmd) {
    797  1.1  christos 	case SI70XX_MEASURE_RH_NOHOLD:
    798  1.1  christos 		/*
    799  1.1  christos 		 * The published conversion for RH is: %RH =
    800  1.1  christos 		 * ((125 * RHCODE) / 65536) - 6
    801  1.1  christos 		 *
    802  1.1  christos 		 * The sysmon infrastructure for RH wants %RH *
    803  1.1  christos 		 * 10^6 The result will fit in 32 bits, but
    804  1.1  christos 		 * the intermediate values will not.
    805  1.1  christos 		 */
    806  1.1  christos 		mul = 125000000;
    807  1.1  christos 		offs = -6000000;
    808  1.1  christos 		/*
    809  1.1  christos 		 * Conversion times for %RH in ms
    810  1.1  christos 		 *
    811  1.1  christos 		 *        	Typical Max
    812  1.1  christos 		 * 12-bit	10.0	12.0
    813  1.1  christos 		 * 11-bit	 5.8	 7.0
    814  1.1  christos 		 * 10-bit	 3.7	 4.5
    815  1.1  christos 		 *  8-bit	 2.6	 3.1
    816  1.1  christos 		 *
    817  1.1  christos 		 * A call to read %RH will also read temperature.  The
    818  1.1  christos 		 * conversion time will be the amount of time above
    819  1.1  christos 		 * plus the amount of time for temperature below
    820  1.1  christos 		 */
    821  1.1  christos 		xdelay = 10500;
    822  1.1  christos 		name = "RH";
    823  1.1  christos 		break;
    824  1.1  christos 	case SI70XX_MEASURE_TEMP_NOHOLD:
    825  1.1  christos 		/*
    826  1.1  christos 		 * The published conversion for temp is:
    827  1.1  christos 		 * degree C = ((175.72 * TEMPCODE) / 65536) -
    828  1.1  christos 		 * 46.85
    829  1.1  christos 		 *
    830  1.1  christos 		 * The sysmon infrastructure for temp wants
    831  1.1  christos 		 * microkelvin.  This is simple, as degree C
    832  1.1  christos 		 * converts directly with K with simple
    833  1.1  christos 		 * addition. The result will fit in 32 bits,
    834  1.1  christos 		 * but the intermediate values will not.
    835  1.1  christos 		 */
    836  1.1  christos 		mul = 175720000;
    837  1.1  christos 		offs = 226300000;
    838  1.1  christos 		/*
    839  1.1  christos 		 * Conversion times for temperature in ms
    840  1.1  christos 	 	 *
    841  1.1  christos 		 *		Typical	Max
    842  1.1  christos 		 * 14-bit	7.0	10.8
    843  1.1  christos 		 * 13-bit	4.0	 6.2
    844  1.1  christos 		 * 12-bit	2.4	 3.8
    845  1.1  christos 		 * 11-bit	1.5	 2.4
    846  1.1  christos 		 */
    847  1.1  christos 		xdelay = 4750;
    848  1.1  christos 		name = "TEMP";
    849  1.1  christos 		break;
    850  1.1  christos 	default:
    851  1.1  christos 		return EINVAL;
    852  1.1  christos 	}
    853  1.1  christos 
    854  1.1  christos #if HAVE_I2C_EXECV
    855  1.1  christos 	memset(buf, 0, sizeof(buf));
    856  1.1  christos 	error = iic_execv(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    857  1.1  christos 	    &cmd, 1, buf, sizeof(buf), 0, I2C_ATTR_CLOCKSTRETCH,
    858  1.1  christos 	    sc->sc_clockstretch, I2C_ATTR_EOL);
    859  1.1  christos #else
    860  1.1  christos 	/*
    861  1.1  christos 	 * The lower level driver must support the ability to
    862  1.1  christos 	 * do a zero length read, otherwise this breaks
    863  1.1  christos 	 */
    864  1.1  christos 	error = si70xx_cmd1(sc, cmd, buf, 0);
    865  1.1  christos 	if (error) {
    866  1.1  christos 		DPRINTF(sc, 2, ("%s: Failed to read NO HOLD %s %d %d\n",
    867  1.1  christos 		    device_xname(sc->sc_dev), name, 1, error));
    868  1.1  christos 		return error;
    869  1.1  christos 	}
    870  1.1  christos 
    871  1.1  christos 	/*
    872  1.1  christos 	 * It will probably be at least this long... we would
    873  1.1  christos 	 * not have to do this sort of thing if clock
    874  1.1  christos 	 * stretching worked.  Even this is a problem for the
    875  1.1  christos 	 * RPI without a patch to remove a [apparently] not
    876  1.1  christos 	 * needed KASSERT()
    877  1.1  christos 	 */
    878  1.1  christos 	delay(xdelay);
    879  1.1  christos 
    880  1.1  christos 	for (int aint = 0; aint < sc->sc_readattempts; aint++) {
    881  1.1  christos 		error = si70xx_cmd0(sc, buf, sizeof(buf));
    882  1.1  christos 		if (error == 0)
    883  1.1  christos 			break;
    884  1.1  christos 		DPRINTF(sc, 2, ("%s: Failed to read NO HOLD RH"
    885  1.1  christos 		    " %d %d\n", device_xname(sc->sc_dev), 2, error));
    886  1.3  christos 		delay(1000);
    887  1.1  christos 	}
    888  1.1  christos #endif
    889  1.1  christos 
    890  1.1  christos 	DPRINTF(sc, 2, ("%s: %s values: %02x%02x%02x - %02x\n",
    891  1.1  christos 	    device_xname(sc->sc_dev), name, buf[0], buf[1], buf[2],
    892  1.1  christos 	    si70xx_crc(buf, 2)));
    893  1.1  christos 
    894  1.1  christos 	uint8_t crc;
    895  1.1  christos 	if (sc->sc_ignorecrc) {
    896  1.1  christos 		crc = buf[2];
    897  1.1  christos 	} else {
    898  1.1  christos 		crc = si70xx_crc(buf, 2);
    899  1.1  christos 	}
    900  1.1  christos 
    901  1.1  christos 	if (crc != buf[2]) {
    902  1.1  christos 		DPRINTF(sc, 2, ("%s: Bad CRC for %s: %#x and %#x\n",
    903  1.1  christos 		    device_xname(sc->sc_dev), name, crc, buf[2]));
    904  1.1  christos 		return EINVAL;
    905  1.1  christos 	}
    906  1.1  christos 
    907  1.1  christos 	uint16_t val16 = (buf[0] << 8) | buf[1];
    908  1.1  christos 	uint64_t val64 = ((mul * val16) >> 16) + offs;
    909  1.1  christos 	DPRINTF(sc, 2, ("%s: %s calculated values: %x %#jx\n",
    910  1.1  christos 	    device_xname(sc->sc_dev), name, val16, (uintmax_t)val64));
    911  1.1  christos 	edata->value_cur = (uint32_t) val64;
    912  1.1  christos 	edata->state = ENVSYS_SVALID;
    913  1.1  christos 	return 0;
    914  1.1  christos }
    915  1.1  christos 
    916  1.1  christos static void
    917  1.1  christos si70xx_refresh(struct sysmon_envsys * sme, envsys_data_t * edata)
    918  1.1  christos {
    919  1.1  christos 	struct si70xx_sc *sc;
    920  1.1  christos 	int 		error;
    921  1.1  christos 
    922  1.1  christos 	sc = sme->sme_cookie;
    923  1.1  christos 	edata->state = ENVSYS_SINVALID;
    924  1.1  christos 
    925  1.1  christos 	mutex_enter(&sc->sc_mutex);
    926  1.1  christos 	error = iic_acquire_bus(sc->sc_tag, 0);
    927  1.1  christos 	if (error) {
    928  1.1  christos 		DPRINTF(sc, 2, ("%s: Could not acquire i2c bus: %x\n",
    929  1.1  christos 		    device_xname(sc->sc_dev), error));
    930  1.1  christos 		goto out;
    931  1.1  christos 	}
    932  1.1  christos 	error = si70xx_update_status(sc);
    933  1.1  christos 	if (error) {
    934  1.1  christos 		DPRINTF(sc, 2, ("%s: Failed to update status in refresh %d\n",
    935  1.1  christos 		    device_xname(sc->sc_dev), error));
    936  1.1  christos 		goto out1;
    937  1.1  christos 	}
    938  1.1  christos 	switch (edata->sensor) {
    939  1.1  christos 	case SI70XX_HUMIDITY_SENSOR:
    940  1.3  christos 		error = si70xx_exec(sc, SI70XX_MEASURE_RH_NOHOLD, edata);
    941  1.1  christos 		break;
    942  1.1  christos 
    943  1.1  christos 	case SI70XX_TEMP_SENSOR:
    944  1.3  christos 		error = si70xx_exec(sc, SI70XX_MEASURE_TEMP_NOHOLD, edata);
    945  1.1  christos 		break;
    946  1.1  christos 	default:
    947  1.1  christos 		error = EINVAL;
    948  1.1  christos 		break;
    949  1.1  christos 	}
    950  1.1  christos 
    951  1.1  christos 	if (error) {
    952  1.1  christos 		DPRINTF(sc, 2, ("%s: Failed to get new status in refresh %d\n",
    953  1.1  christos 		    device_xname(sc->sc_dev), error));
    954  1.1  christos 	}
    955  1.1  christos out1:
    956  1.1  christos 	iic_release_bus(sc->sc_tag, 0);
    957  1.1  christos out:
    958  1.1  christos 	mutex_exit(&sc->sc_mutex);
    959  1.1  christos }
    960  1.1  christos 
    961  1.1  christos static int
    962  1.1  christos si70xx_detach(device_t self, int flags)
    963  1.1  christos {
    964  1.1  christos 	struct si70xx_sc *sc;
    965  1.1  christos 
    966  1.1  christos 	sc = device_private(self);
    967  1.1  christos 
    968  1.1  christos 	mutex_enter(&sc->sc_mutex);
    969  1.1  christos 
    970  1.1  christos 	/* Remove the sensors */
    971  1.1  christos 	if (sc->sc_sme != NULL) {
    972  1.1  christos 		sysmon_envsys_unregister(sc->sc_sme);
    973  1.1  christos 		sc->sc_sme = NULL;
    974  1.1  christos 	}
    975  1.1  christos 	mutex_exit(&sc->sc_mutex);
    976  1.1  christos 
    977  1.1  christos 	/* Remove the sysctl tree */
    978  1.1  christos 	sysctl_teardown(&sc->sc_si70xxlog);
    979  1.1  christos 
    980  1.1  christos 	/* Remove the mutex */
    981  1.1  christos 	mutex_destroy(&sc->sc_mutex);
    982  1.1  christos 
    983  1.1  christos 	return 0;
    984  1.1  christos }
    985  1.1  christos 
    986  1.1  christos MODULE(MODULE_CLASS_DRIVER, si70xxtemp, "i2cexec,sysmon_envsys");
    987  1.1  christos 
    988  1.1  christos #ifdef _MODULE
    989  1.1  christos #include "ioconf.c"
    990  1.1  christos #endif
    991  1.1  christos 
    992  1.1  christos static int
    993  1.1  christos si70xxtemp_modcmd(modcmd_t cmd, void *opaque)
    994  1.1  christos {
    995  1.1  christos 
    996  1.1  christos 	switch (cmd) {
    997  1.1  christos 	case MODULE_CMD_INIT:
    998  1.1  christos #ifdef _MODULE
    999  1.1  christos 		return config_init_component(cfdriver_ioconf_si70xxtemp,
   1000  1.1  christos 		    cfattach_ioconf_si70xxtemp, cfdata_ioconf_si70xxtemp);
   1001  1.1  christos #else
   1002  1.1  christos 		return 0;
   1003  1.1  christos #endif
   1004  1.1  christos 	case MODULE_CMD_FINI:
   1005  1.1  christos #ifdef _MODULE
   1006  1.2  christos 		return config_fini_component(cfdriver_ioconf_si70xxtemp,
   1007  1.1  christos 		      cfattach_ioconf_si70xxtemp, cfdata_ioconf_si70xxtemp);
   1008  1.1  christos #else
   1009  1.1  christos 		return 0;
   1010  1.1  christos #endif
   1011  1.1  christos 	default:
   1012  1.1  christos 		return ENOTTY;
   1013  1.1  christos 	}
   1014  1.1  christos }
   1015