Home | History | Annotate | Line # | Download | only in i2c
axp22x.c revision 1.5
      1  1.5   thorpej /* $NetBSD: axp22x.c,v 1.5 2018/06/18 17:07:07 thorpej Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2014 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.5   thorpej __KERNEL_RCSID(0, "$NetBSD: axp22x.c,v 1.5 2018/06/18 17:07:07 thorpej Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/systm.h>
     34  1.1  jmcneill #include <sys/device.h>
     35  1.1  jmcneill #include <sys/conf.h>
     36  1.1  jmcneill #include <sys/bus.h>
     37  1.1  jmcneill #include <sys/kmem.h>
     38  1.1  jmcneill 
     39  1.1  jmcneill #include <dev/i2c/i2cvar.h>
     40  1.1  jmcneill 
     41  1.1  jmcneill #include <dev/sysmon/sysmonvar.h>
     42  1.1  jmcneill 
     43  1.3  jmcneill static const char *compatible[] = {
     44  1.3  jmcneill 	"x-powers,axp221",
     45  1.3  jmcneill 	NULL
     46  1.3  jmcneill };
     47  1.3  jmcneill 
     48  1.5   thorpej static const struct device_compatible_entry axp22x_compat_data[] = {
     49  1.5   thorpej 	DEVICE_COMPAT_ENTRY(compatible),
     50  1.5   thorpej 	DEVICE_COMPAT_TERMINATOR
     51  1.5   thorpej };
     52  1.5   thorpej 
     53  1.1  jmcneill #define AXP_TEMP_MON_REG	0x56	/* 2 bytes */
     54  1.1  jmcneill 
     55  1.1  jmcneill struct axp22x_softc {
     56  1.1  jmcneill 	device_t	sc_dev;
     57  1.1  jmcneill 	i2c_tag_t	sc_i2c;
     58  1.1  jmcneill 	i2c_addr_t	sc_addr;
     59  1.1  jmcneill 
     60  1.1  jmcneill 	struct sysmon_envsys *sc_sme;
     61  1.1  jmcneill 	envsys_data_t	sc_sensor_temp;
     62  1.1  jmcneill };
     63  1.1  jmcneill 
     64  1.1  jmcneill static int	axp22x_match(device_t, cfdata_t, void *);
     65  1.1  jmcneill static void	axp22x_attach(device_t, device_t, void *);
     66  1.1  jmcneill 
     67  1.1  jmcneill static void	axp22x_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
     68  1.1  jmcneill static int	axp22x_read(struct axp22x_softc *, uint8_t, uint8_t *, size_t);
     69  1.1  jmcneill 
     70  1.1  jmcneill CFATTACH_DECL_NEW(axp22x, sizeof(struct axp22x_softc),
     71  1.1  jmcneill     axp22x_match, axp22x_attach, NULL, NULL);
     72  1.1  jmcneill 
     73  1.1  jmcneill static int
     74  1.1  jmcneill axp22x_match(device_t parent, cfdata_t match, void *aux)
     75  1.1  jmcneill {
     76  1.3  jmcneill 	struct i2c_attach_args *ia = aux;
     77  1.4   thorpej 	int match_result;
     78  1.3  jmcneill 
     79  1.5   thorpej 	if (iic_use_direct_match(ia, match, axp22x_compat_data, &match_result))
     80  1.4   thorpej 		return match_result;
     81  1.3  jmcneill 
     82  1.4   thorpej 	/* This device is direct-config only. */
     83  1.4   thorpej 
     84  1.4   thorpej 	return 0;
     85  1.1  jmcneill }
     86  1.1  jmcneill 
     87  1.1  jmcneill static void
     88  1.1  jmcneill axp22x_attach(device_t parent, device_t self, void *aux)
     89  1.1  jmcneill {
     90  1.1  jmcneill 	struct axp22x_softc *sc = device_private(self);
     91  1.1  jmcneill 	struct i2c_attach_args *ia = aux;
     92  1.1  jmcneill 
     93  1.1  jmcneill 	sc->sc_dev = self;
     94  1.1  jmcneill 	sc->sc_i2c = ia->ia_tag;
     95  1.1  jmcneill 	sc->sc_addr = ia->ia_addr;
     96  1.1  jmcneill 
     97  1.1  jmcneill 	aprint_naive("\n");
     98  1.1  jmcneill 	aprint_normal("\n");
     99  1.1  jmcneill 
    100  1.1  jmcneill 	sc->sc_sme = sysmon_envsys_create();
    101  1.1  jmcneill 	sc->sc_sme->sme_name = device_xname(self);
    102  1.1  jmcneill 	sc->sc_sme->sme_cookie = sc;
    103  1.1  jmcneill 	sc->sc_sme->sme_refresh = axp22x_sensors_refresh;
    104  1.1  jmcneill 
    105  1.1  jmcneill 	sc->sc_sensor_temp.units = ENVSYS_STEMP;
    106  1.1  jmcneill 	sc->sc_sensor_temp.state = ENVSYS_SINVALID;
    107  1.1  jmcneill 	sc->sc_sensor_temp.flags = ENVSYS_FHAS_ENTROPY;
    108  1.1  jmcneill 	snprintf(sc->sc_sensor_temp.desc, sizeof(sc->sc_sensor_temp.desc),
    109  1.1  jmcneill 	    "internal temperature");
    110  1.1  jmcneill 	sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor_temp);
    111  1.1  jmcneill 
    112  1.1  jmcneill 	sysmon_envsys_register(sc->sc_sme);
    113  1.1  jmcneill }
    114  1.1  jmcneill 
    115  1.1  jmcneill static void
    116  1.1  jmcneill axp22x_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    117  1.1  jmcneill {
    118  1.1  jmcneill 	struct axp22x_softc *sc = sme->sme_cookie;
    119  1.1  jmcneill 	uint8_t buf[2];
    120  1.1  jmcneill 	int error;
    121  1.1  jmcneill 
    122  1.1  jmcneill 	iic_acquire_bus(sc->sc_i2c, 0);
    123  1.1  jmcneill 	error = axp22x_read(sc, AXP_TEMP_MON_REG, buf, sizeof(buf));
    124  1.1  jmcneill 	iic_release_bus(sc->sc_i2c, 0);
    125  1.1  jmcneill 
    126  1.1  jmcneill 	if (error) {
    127  1.1  jmcneill 		edata->state = ENVSYS_SINVALID;
    128  1.1  jmcneill 	} else {
    129  1.2  jmcneill 		/* between -243.7C and 165.8C, step +0.1C */
    130  1.2  jmcneill 		edata->value_cur = (((buf[0] << 4) | (buf[1] & 0xf)) - 2437)
    131  1.1  jmcneill 				   * 100000 + 273150000;
    132  1.1  jmcneill 		edata->state = ENVSYS_SVALID;
    133  1.1  jmcneill 	}
    134  1.1  jmcneill }
    135  1.1  jmcneill 
    136  1.1  jmcneill static int
    137  1.1  jmcneill axp22x_read(struct axp22x_softc *sc, uint8_t reg, uint8_t *val, size_t len)
    138  1.1  jmcneill {
    139  1.1  jmcneill 	size_t i;
    140  1.1  jmcneill 	int error;
    141  1.1  jmcneill 
    142  1.1  jmcneill 	for (i = 0; i < len; i++) {
    143  1.1  jmcneill 		error = iic_smbus_read_byte(sc->sc_i2c, sc->sc_addr,
    144  1.1  jmcneill 		    reg + i, &val[i], 0);
    145  1.1  jmcneill 		if (error)
    146  1.1  jmcneill 			return error;
    147  1.1  jmcneill 	}
    148  1.1  jmcneill 
    149  1.1  jmcneill 	return 0;
    150  1.1  jmcneill }
    151