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