Home | History | Annotate | Line # | Download | only in i2c
dstemp.c revision 1.1.2.2
      1 /* $NetBSD: dstemp.c,v 1.1.2.2 2018/07/28 04:37:44 pgoyette Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2018 Michael Lorenz
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: dstemp.c,v 1.1.2.2 2018/07/28 04:37:44 pgoyette Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/device.h>
     35 #include <sys/conf.h>
     36 #include <sys/bus.h>
     37 
     38 #include <dev/i2c/i2cvar.h>
     39 
     40 #include <dev/sysmon/sysmonvar.h>
     41 
     42 #ifdef macppc
     43 #define HAVE_OF 1
     44 #endif
     45 
     46 #ifdef HAVE_OF
     47 #include <dev/ofw/openfirm.h>
     48 #endif
     49 
     50 #define DSTEMP_CMD_START	0x51	/* command, no data */
     51 #define DSTEMP_CMD_POR		0x54	/* command, no data */
     52 #define DSTEMP_CMD_STOP		0x22	/* command, no data */
     53 #define DSTEMP_TCURRENT		0xaa	/* current temperature, 2 bytes */
     54 #define DSTEMP_THIGH		0xa1	/* high threshold, 2 bytes */
     55 #define DSTEMP_TLOW		0xa2	/* low threshold, 2 bytes */
     56 #define DSTEMP_CONFIG		0xac	/* 1 byte */
     57 
     58 struct dstemp_softc {
     59 	device_t	sc_dev;
     60 	i2c_tag_t	sc_i2c;
     61 	i2c_addr_t	sc_addr;
     62 
     63 	struct sysmon_envsys *sc_sme;
     64 	envsys_data_t	sc_sensor_temp;
     65 };
     66 
     67 static int	dstemp_match(device_t, cfdata_t, void *);
     68 static void	dstemp_attach(device_t, device_t, void *);
     69 
     70 static void	dstemp_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
     71 
     72 CFATTACH_DECL_NEW(dstemp, sizeof(struct dstemp_softc),
     73     dstemp_match, dstemp_attach, NULL, NULL);
     74 
     75 static const struct device_compatible_entry compat_data[] = {
     76 	{ "ds1631",			0 },
     77 	{ NULL,				0 }
     78 };
     79 
     80 static int
     81 dstemp_match(device_t parent, cfdata_t match, void *aux)
     82 {
     83 	struct i2c_attach_args *ia = aux;
     84 	int match_result;
     85 
     86 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
     87 		return match_result;
     88 
     89 	if ((ia->ia_addr & 0xf8) == 0x48)
     90 		return I2C_MATCH_ADDRESS_ONLY;
     91 
     92 	return 0;
     93 }
     94 
     95 static void
     96 dstemp_attach(device_t parent, device_t self, void *aux)
     97 {
     98 	struct dstemp_softc *sc = device_private(self);
     99 	struct i2c_attach_args *ia = aux;
    100 	char name[64] = "temperature";
    101 
    102 	sc->sc_dev = self;
    103 	sc->sc_i2c = ia->ia_tag;
    104 	sc->sc_addr = ia->ia_addr;
    105 
    106 	aprint_naive("\n");
    107 	aprint_normal(": DS1361\n");
    108 
    109 	sc->sc_sme = sysmon_envsys_create();
    110 	sc->sc_sme->sme_name = device_xname(self);
    111 	sc->sc_sme->sme_cookie = sc;
    112 	sc->sc_sme->sme_refresh = dstemp_sensors_refresh;
    113 
    114 	sc->sc_sensor_temp.units = ENVSYS_STEMP;
    115 	sc->sc_sensor_temp.state = ENVSYS_SINVALID;
    116 #ifdef HAVE_OF
    117 	int ch;
    118 	ch = OF_child(ia->ia_cookie);
    119 	if (ch != 0) {
    120 		OF_getprop(ch, "location", name, 64);
    121 	}
    122 #endif
    123 	strncpy(sc->sc_sensor_temp.desc, name, sizeof(sc->sc_sensor_temp.desc));
    124 
    125 	sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor_temp);
    126 
    127 	sysmon_envsys_register(sc->sc_sme);
    128 }
    129 
    130 static void
    131 dstemp_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    132 {
    133 	struct dstemp_softc *sc = sme->sme_cookie;
    134 	uint8_t cmd = DSTEMP_TCURRENT;
    135 	uint16_t data = 0;
    136 	int error;
    137 
    138 
    139 	iic_acquire_bus(sc->sc_i2c, 0);
    140 	error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    141 	    sc->sc_addr, &cmd, 1, &data, 2, 0);
    142 	iic_release_bus(sc->sc_i2c, 0);
    143 
    144 	if (error) {
    145 		edata->state = ENVSYS_SINVALID;
    146 	} else {
    147 		edata->value_cur =
    148 		    ((uint64_t)(data>>4) * 62500) +
    149 		    + 273150000;
    150 		edata->state = ENVSYS_SVALID;
    151 	}
    152 }
    153