Home | History | Annotate | Line # | Download | only in i2c
cwfg.c revision 1.4.4.1
      1  1.4.4.1   thorpej /* $NetBSD: cwfg.c,v 1.4.4.1 2021/05/08 16:46:43 thorpej Exp $ */
      2      1.1  jmcneill 
      3      1.1  jmcneill /*-
      4      1.1  jmcneill  * Copyright (c) 2020 Jared 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.4.4.1   thorpej __KERNEL_RCSID(0, "$NetBSD: cwfg.c,v 1.4.4.1 2021/05/08 16:46:43 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/kernel.h>
     35      1.1  jmcneill #include <sys/device.h>
     36      1.1  jmcneill #include <sys/conf.h>
     37      1.1  jmcneill #include <sys/bus.h>
     38      1.1  jmcneill #include <sys/kmem.h>
     39      1.1  jmcneill 
     40      1.1  jmcneill #include <dev/i2c/i2cvar.h>
     41      1.1  jmcneill 
     42      1.1  jmcneill #include <dev/sysmon/sysmonvar.h>
     43      1.1  jmcneill #include <dev/sysmon/sysmon_taskq.h>
     44      1.1  jmcneill 
     45      1.1  jmcneill #include <dev/fdt/fdtvar.h>
     46      1.1  jmcneill 
     47      1.1  jmcneill #define	VERSION_REG	0x00
     48      1.1  jmcneill #define	VCELL_HI_REG	0x02
     49      1.1  jmcneill #define	 VCELL_HI	__BITS(5,0)
     50      1.1  jmcneill #define	VCELL_LO_REG	0x03
     51      1.1  jmcneill #define	 VCELL_LO	__BITS(7,0)
     52      1.1  jmcneill #define	SOC_HI_REG	0x04
     53      1.1  jmcneill #define	SOC_LO_REG	0x05
     54      1.1  jmcneill #define	RTT_ALRT_HI_REG	0x06
     55      1.1  jmcneill #define	 RTT_ALRT	__BIT(7)
     56      1.1  jmcneill #define	 RTT_HI		__BITS(4,0)
     57      1.1  jmcneill #define	RTT_ALRT_LO_REG	0x07
     58      1.1  jmcneill #define	 RTT_LO		__BITS(7,0)
     59      1.1  jmcneill #define	CONFIG_REG	0x08
     60      1.1  jmcneill #define	 CONFIG_ATHD	__BITS(7,3)
     61      1.1  jmcneill #define	 CONFIG_UFG	__BIT(1)
     62      1.1  jmcneill #define	MODE_REG	0x0a
     63      1.1  jmcneill #define	 MODE_SLEEP	__BITS(7,6)
     64      1.1  jmcneill #define	  MODE_SLEEP_WAKE	0x0
     65      1.1  jmcneill #define	  MODE_SLEEP_SLEEP	0x3
     66      1.1  jmcneill #define	 MODE_QSTRT	__BITS(5,4)
     67      1.1  jmcneill #define	 MODE_POR	__BITS(3,0)
     68      1.1  jmcneill #define	BATINFO_REG(n)	(0x10 + (n))
     69      1.1  jmcneill 
     70      1.1  jmcneill #define	VCELL_STEP	312
     71      1.1  jmcneill #define	VCELL_DIV	1024
     72      1.1  jmcneill #define	BATINFO_SIZE	64
     73      1.1  jmcneill #define	RESET_COUNT	30
     74      1.1  jmcneill #define	RESET_DELAY	100000
     75      1.1  jmcneill 
     76      1.1  jmcneill enum cwfg_sensor {
     77      1.1  jmcneill 	CWFG_SENSOR_VCELL,
     78      1.1  jmcneill 	CWFG_SENSOR_SOC,
     79      1.1  jmcneill 	CWFG_SENSOR_RTT,
     80      1.1  jmcneill 	CWFG_NSENSORS
     81      1.1  jmcneill };
     82      1.1  jmcneill 
     83      1.1  jmcneill struct cwfg_softc {
     84      1.1  jmcneill 	device_t	sc_dev;
     85      1.1  jmcneill 	i2c_tag_t	sc_i2c;
     86      1.1  jmcneill 	i2c_addr_t	sc_addr;
     87      1.1  jmcneill 	int		sc_phandle;
     88      1.1  jmcneill 
     89      1.1  jmcneill 	uint8_t		sc_batinfo[BATINFO_SIZE];
     90      1.1  jmcneill 
     91      1.1  jmcneill 	u_int		sc_alert_level;
     92      1.1  jmcneill 	u_int		sc_monitor_interval;
     93      1.1  jmcneill 	u_int		sc_design_capacity;
     94      1.1  jmcneill 
     95      1.1  jmcneill 	struct sysmon_envsys *sc_sme;
     96      1.1  jmcneill 
     97      1.1  jmcneill 	envsys_data_t	sc_sensor[CWFG_NSENSORS];
     98      1.1  jmcneill };
     99      1.1  jmcneill 
    100      1.1  jmcneill #define	CWFG_MONITOR_INTERVAL_DEFAULT	8
    101      1.1  jmcneill #define	CWFG_DESIGN_CAPACITY_DEFAULT	2000
    102      1.1  jmcneill #define	CWFG_ALERT_LEVEL_DEFAULT	0
    103      1.1  jmcneill 
    104      1.1  jmcneill static const struct device_compatible_entry compat_data[] = {
    105      1.2   thorpej 	{ .compat = "cellwise,cw201x" },
    106      1.4   thorpej 	DEVICE_COMPAT_EOL
    107      1.1  jmcneill };
    108      1.1  jmcneill 
    109      1.1  jmcneill static int
    110      1.1  jmcneill cwfg_lock(struct cwfg_softc *sc)
    111      1.1  jmcneill {
    112      1.1  jmcneill 	return iic_acquire_bus(sc->sc_i2c, 0);
    113      1.1  jmcneill }
    114      1.1  jmcneill 
    115      1.1  jmcneill static void
    116      1.1  jmcneill cwfg_unlock(struct cwfg_softc *sc)
    117      1.1  jmcneill {
    118      1.1  jmcneill 	iic_release_bus(sc->sc_i2c, 0);
    119      1.1  jmcneill }
    120      1.1  jmcneill 
    121      1.1  jmcneill static int
    122      1.1  jmcneill cwfg_read(struct cwfg_softc *sc, uint8_t reg, uint8_t *val)
    123      1.1  jmcneill {
    124      1.1  jmcneill 	return iic_smbus_read_byte(sc->sc_i2c, sc->sc_addr, reg, val, 0);
    125      1.1  jmcneill }
    126      1.1  jmcneill 
    127      1.1  jmcneill static int
    128      1.1  jmcneill cwfg_write(struct cwfg_softc *sc, uint8_t reg, uint8_t val)
    129      1.1  jmcneill {
    130      1.1  jmcneill 	return iic_smbus_write_byte(sc->sc_i2c, sc->sc_addr, reg, val, 0);
    131      1.1  jmcneill }
    132      1.1  jmcneill 
    133      1.1  jmcneill static void
    134      1.1  jmcneill cwfg_sensor_refresh(struct sysmon_envsys *sme, envsys_data_t *e)
    135      1.1  jmcneill {
    136      1.1  jmcneill 	struct cwfg_softc *sc = sme->sme_cookie;
    137      1.1  jmcneill 	u_int vcell, rtt, tmp;
    138      1.1  jmcneill 	uint8_t val;
    139      1.1  jmcneill 	int error, n;
    140      1.1  jmcneill 
    141      1.1  jmcneill 	e->state = ENVSYS_SINVALID;
    142      1.1  jmcneill 
    143      1.1  jmcneill 	if ((error = cwfg_lock(sc)) != 0)
    144      1.1  jmcneill 		return;
    145      1.1  jmcneill 
    146      1.1  jmcneill 	switch (e->private) {
    147      1.1  jmcneill 	case CWFG_SENSOR_VCELL:
    148      1.1  jmcneill 		/* Take the average of three readings */
    149      1.1  jmcneill 		vcell = 0;
    150      1.1  jmcneill 		for (n = 0; n < 3; n++) {
    151      1.1  jmcneill 			if ((error = cwfg_read(sc, VCELL_HI_REG, &val)) != 0)
    152      1.1  jmcneill 				goto done;
    153      1.1  jmcneill 			tmp = __SHIFTOUT(val, VCELL_HI) << 8;
    154      1.1  jmcneill 			if ((error = cwfg_read(sc, VCELL_LO_REG, &val)) != 0)
    155      1.1  jmcneill 				goto done;
    156      1.1  jmcneill 			tmp |= __SHIFTOUT(val, VCELL_LO);
    157      1.1  jmcneill 			vcell += tmp;
    158      1.1  jmcneill 		}
    159      1.1  jmcneill 		vcell /= 3;
    160      1.1  jmcneill 
    161      1.1  jmcneill 		e->state = ENVSYS_SVALID;
    162      1.1  jmcneill 		e->value_cur = ((vcell * VCELL_STEP) / VCELL_DIV) * 1000;
    163      1.1  jmcneill 		break;
    164      1.1  jmcneill 
    165      1.1  jmcneill 	case CWFG_SENSOR_SOC:
    166      1.1  jmcneill 		if ((error = cwfg_read(sc, SOC_HI_REG, &val)) != 0)
    167      1.1  jmcneill 			goto done;
    168      1.1  jmcneill 
    169      1.1  jmcneill 		if (val != 0xff) {
    170      1.1  jmcneill 			e->state = ENVSYS_SVALID;
    171      1.1  jmcneill 			e->value_cur = val;			/* batt % */
    172      1.1  jmcneill 		}
    173      1.1  jmcneill 		break;
    174      1.1  jmcneill 
    175      1.1  jmcneill 	case CWFG_SENSOR_RTT:
    176      1.1  jmcneill 		if ((error = cwfg_read(sc, RTT_ALRT_HI_REG, &val)) != 0)
    177      1.1  jmcneill 			goto done;
    178      1.1  jmcneill 		rtt = __SHIFTOUT(val, RTT_HI) << 8;
    179      1.1  jmcneill 		if ((error = cwfg_read(sc, RTT_ALRT_LO_REG, &val)) != 0)
    180      1.1  jmcneill 			goto done;
    181      1.1  jmcneill 		rtt |= __SHIFTOUT(val, RTT_LO);
    182      1.1  jmcneill 
    183      1.1  jmcneill 		if (rtt != 0x1fff) {
    184      1.1  jmcneill 			e->state = ENVSYS_SVALID;
    185      1.1  jmcneill 			e->value_cur = rtt;			/* minutes */
    186      1.1  jmcneill 		}
    187      1.1  jmcneill 		break;
    188      1.1  jmcneill 	}
    189      1.1  jmcneill 
    190      1.1  jmcneill done:
    191      1.1  jmcneill 	cwfg_unlock(sc);
    192      1.1  jmcneill }
    193      1.1  jmcneill 
    194      1.1  jmcneill static void
    195      1.1  jmcneill cwfg_attach_battery(struct cwfg_softc *sc)
    196      1.1  jmcneill {
    197      1.1  jmcneill 	envsys_data_t *e;
    198      1.1  jmcneill 
    199      1.1  jmcneill 	/* Cell voltage */
    200      1.1  jmcneill 	e = &sc->sc_sensor[CWFG_SENSOR_VCELL];
    201      1.1  jmcneill 	e->private = CWFG_SENSOR_VCELL;
    202      1.1  jmcneill 	e->units = ENVSYS_SVOLTS_DC;
    203      1.1  jmcneill 	e->state = ENVSYS_SINVALID;
    204      1.1  jmcneill 	strlcpy(e->desc, "battery voltage", sizeof(e->desc));
    205      1.1  jmcneill 	sysmon_envsys_sensor_attach(sc->sc_sme, e);
    206      1.1  jmcneill 
    207      1.1  jmcneill 	/* State of charge */
    208      1.1  jmcneill 	e = &sc->sc_sensor[CWFG_SENSOR_SOC];
    209      1.1  jmcneill 	e->private = CWFG_SENSOR_SOC;
    210      1.1  jmcneill 	e->units = ENVSYS_INTEGER;
    211      1.1  jmcneill 	e->state = ENVSYS_SINVALID;
    212      1.1  jmcneill 	e->flags = ENVSYS_FPERCENT;
    213      1.1  jmcneill 	strlcpy(e->desc, "battery percent", sizeof(e->desc));
    214      1.1  jmcneill 	sysmon_envsys_sensor_attach(sc->sc_sme, e);
    215      1.1  jmcneill 
    216      1.1  jmcneill 	/* Remaining run time */
    217      1.1  jmcneill 	e = &sc->sc_sensor[CWFG_SENSOR_RTT];
    218      1.1  jmcneill 	e->private = CWFG_SENSOR_RTT;
    219      1.1  jmcneill 	e->units = ENVSYS_INTEGER;
    220      1.1  jmcneill 	e->state = ENVSYS_SINVALID;
    221      1.1  jmcneill 	strlcpy(e->desc, "battery remaining minutes", sizeof(e->desc));
    222      1.1  jmcneill 	sysmon_envsys_sensor_attach(sc->sc_sme, e);
    223      1.1  jmcneill }
    224      1.1  jmcneill 
    225      1.1  jmcneill static void
    226      1.1  jmcneill cwfg_attach_sensors(struct cwfg_softc *sc)
    227      1.1  jmcneill {
    228      1.1  jmcneill 	sc->sc_sme = sysmon_envsys_create();
    229      1.1  jmcneill 	sc->sc_sme->sme_name = device_xname(sc->sc_dev);
    230      1.1  jmcneill 	sc->sc_sme->sme_cookie = sc;
    231      1.1  jmcneill 	sc->sc_sme->sme_refresh = cwfg_sensor_refresh;
    232      1.1  jmcneill 	sc->sc_sme->sme_events_timeout = sc->sc_monitor_interval;
    233      1.1  jmcneill 	sc->sc_sme->sme_class = SME_CLASS_BATTERY;
    234      1.1  jmcneill 	sc->sc_sme->sme_flags = SME_INIT_REFRESH;
    235      1.1  jmcneill 
    236      1.1  jmcneill 	cwfg_attach_battery(sc);
    237      1.1  jmcneill 
    238      1.1  jmcneill 	sysmon_envsys_register(sc->sc_sme);
    239      1.1  jmcneill }
    240      1.1  jmcneill 
    241      1.1  jmcneill static int
    242      1.1  jmcneill cwfg_set_config(struct cwfg_softc *sc)
    243      1.1  jmcneill {
    244      1.1  jmcneill 	u_int alert_level;
    245      1.1  jmcneill 	bool need_update;
    246      1.1  jmcneill 	uint8_t config, mode, val;
    247      1.1  jmcneill 	int error, n;
    248      1.1  jmcneill 
    249      1.1  jmcneill 	/* Read current config */
    250      1.1  jmcneill 	if ((error = cwfg_read(sc, CONFIG_REG, &config)) != 0)
    251      1.1  jmcneill 		return error;
    252      1.1  jmcneill 
    253      1.1  jmcneill 	/* Update alert level, if necessary */
    254      1.1  jmcneill 	alert_level = __SHIFTOUT(config, CONFIG_ATHD);
    255      1.1  jmcneill 	if (alert_level != sc->sc_alert_level) {
    256      1.1  jmcneill 		config &= ~CONFIG_ATHD;
    257      1.1  jmcneill 		config |= __SHIFTIN(sc->sc_alert_level, CONFIG_ATHD);
    258      1.1  jmcneill 		if ((error = cwfg_write(sc, CONFIG_REG, config)) != 0)
    259      1.1  jmcneill 			return error;
    260      1.1  jmcneill 	}
    261      1.1  jmcneill 
    262      1.1  jmcneill 	/* Re-read current config */
    263      1.1  jmcneill 	if ((error = cwfg_read(sc, CONFIG_REG, &config)) != 0)
    264      1.1  jmcneill 		return error;
    265      1.1  jmcneill 
    266      1.1  jmcneill 	/*
    267      1.1  jmcneill 	 * We need to upload a battery profile if either the UFG flag
    268      1.1  jmcneill 	 * is unset, or the current battery profile differs from the
    269      1.1  jmcneill 	 * one in the DT.
    270      1.1  jmcneill 	 */
    271      1.1  jmcneill 	need_update = (config & CONFIG_UFG) == 0;
    272      1.1  jmcneill 	if (need_update == false) {
    273      1.1  jmcneill 		for (n = 0; n < BATINFO_SIZE; n++) {
    274      1.1  jmcneill 			if ((error = cwfg_read(sc, BATINFO_REG(n), &val)) != 0)
    275      1.1  jmcneill 				return error;
    276      1.1  jmcneill 			if (sc->sc_batinfo[n] != val) {
    277      1.1  jmcneill 				need_update = true;
    278      1.1  jmcneill 				break;
    279      1.1  jmcneill 			}
    280      1.1  jmcneill 		}
    281      1.1  jmcneill 	}
    282      1.1  jmcneill 	if (need_update == false)
    283      1.1  jmcneill 		return 0;
    284      1.1  jmcneill 
    285      1.1  jmcneill 	aprint_verbose_dev(sc->sc_dev, "updating battery profile\n");
    286      1.1  jmcneill 
    287      1.1  jmcneill 	/* Update battery profile */
    288      1.1  jmcneill 	for (n = 0; n < BATINFO_SIZE; n++) {
    289      1.1  jmcneill 		val = sc->sc_batinfo[n];
    290      1.1  jmcneill 		if ((error = cwfg_write(sc, BATINFO_REG(n), val)) != 0)
    291      1.1  jmcneill 			return error;
    292      1.1  jmcneill 	}
    293      1.1  jmcneill 
    294      1.1  jmcneill 	/* Set UFG flag to switch to new profile */
    295      1.1  jmcneill 	if ((error = cwfg_read(sc, CONFIG_REG, &config)) != 0)
    296      1.1  jmcneill 		return error;
    297      1.1  jmcneill 	config |= CONFIG_UFG;
    298      1.1  jmcneill 	if ((error = cwfg_write(sc, CONFIG_REG, config)) != 0)
    299      1.1  jmcneill 		return error;
    300      1.1  jmcneill 
    301      1.1  jmcneill 	/* Restart the IC with new profile */
    302      1.1  jmcneill 	if ((error = cwfg_read(sc, MODE_REG, &mode)) != 0)
    303      1.1  jmcneill 		return error;
    304      1.1  jmcneill 	mode |= MODE_POR;
    305      1.1  jmcneill 	if ((error = cwfg_write(sc, MODE_REG, mode)) != 0)
    306      1.1  jmcneill 		return error;
    307      1.1  jmcneill 	delay(20000);
    308      1.1  jmcneill 	mode &= ~MODE_POR;
    309      1.1  jmcneill 	if ((error = cwfg_write(sc, MODE_REG, mode)) != 0)
    310      1.1  jmcneill 		return error;
    311      1.1  jmcneill 
    312      1.1  jmcneill 	return error;
    313      1.1  jmcneill }
    314      1.1  jmcneill 
    315      1.1  jmcneill static int
    316      1.1  jmcneill cwfg_init(struct cwfg_softc *sc)
    317      1.1  jmcneill {
    318      1.1  jmcneill 	uint8_t mode, soc;
    319      1.1  jmcneill 	int error, retry;
    320      1.1  jmcneill 
    321      1.1  jmcneill 	cwfg_lock(sc);
    322      1.1  jmcneill 
    323      1.1  jmcneill 	/* If the device is in sleep mode, wake it up */
    324      1.1  jmcneill 	if ((error = cwfg_read(sc, MODE_REG, &mode)) != 0)
    325      1.1  jmcneill 		goto done;
    326      1.1  jmcneill 	if (__SHIFTOUT(mode, MODE_SLEEP) == MODE_SLEEP_SLEEP) {
    327      1.1  jmcneill 		mode &= ~MODE_SLEEP;
    328      1.1  jmcneill 		mode |= __SHIFTIN(MODE_SLEEP_WAKE, MODE_SLEEP);
    329      1.1  jmcneill 		if ((error = cwfg_write(sc, MODE_REG, mode)) != 0)
    330      1.1  jmcneill 			goto done;
    331      1.1  jmcneill 	}
    332      1.1  jmcneill 
    333      1.1  jmcneill 	/* Load battery profile */
    334      1.1  jmcneill 	if ((error = cwfg_set_config(sc)) != 0)
    335      1.1  jmcneill 		goto done;
    336      1.1  jmcneill 
    337      1.1  jmcneill 	/* Wait for chip to become ready */
    338      1.1  jmcneill 	for (retry = RESET_COUNT; retry > 0; retry--) {
    339      1.1  jmcneill 		if ((error = cwfg_read(sc, SOC_HI_REG, &soc)) != 0)
    340      1.1  jmcneill 			goto done;
    341      1.1  jmcneill 		if (soc != 0xff)
    342      1.1  jmcneill 			break;
    343      1.1  jmcneill 		delay(RESET_DELAY);
    344      1.1  jmcneill 	}
    345      1.1  jmcneill 	if (retry == 0) {
    346      1.1  jmcneill 		aprint_error_dev(sc->sc_dev,
    347      1.1  jmcneill 		    "WARNING: timeout waiting for chip ready\n");
    348      1.1  jmcneill 	}
    349      1.1  jmcneill 
    350      1.1  jmcneill done:
    351      1.1  jmcneill 	cwfg_unlock(sc);
    352      1.1  jmcneill 
    353      1.1  jmcneill 	return error;
    354      1.1  jmcneill }
    355      1.1  jmcneill 
    356      1.1  jmcneill static int
    357      1.1  jmcneill cwfg_parse_resources(struct cwfg_softc *sc)
    358      1.1  jmcneill {
    359      1.1  jmcneill 	const u_int *batinfo;
    360      1.1  jmcneill 	int len = 0, n;
    361      1.1  jmcneill 
    362      1.1  jmcneill 	batinfo = fdtbus_get_prop(sc->sc_phandle,
    363      1.1  jmcneill 	    "cellwise,bat-config-info", &len);
    364      1.1  jmcneill 	switch (len) {
    365      1.1  jmcneill 	case BATINFO_SIZE:
    366      1.1  jmcneill 		memcpy(sc->sc_batinfo, batinfo, BATINFO_SIZE);
    367      1.1  jmcneill 		break;
    368      1.1  jmcneill 	case BATINFO_SIZE * 4:
    369      1.1  jmcneill 		for (n = 0; n < BATINFO_SIZE; n++)
    370      1.1  jmcneill 			sc->sc_batinfo[n] = be32toh(batinfo[n]);
    371      1.1  jmcneill 		break;
    372      1.1  jmcneill 	default:
    373      1.1  jmcneill 		aprint_error_dev(sc->sc_dev,
    374      1.1  jmcneill 		    "missing or invalid battery info\n");
    375      1.1  jmcneill 		return EINVAL;
    376      1.1  jmcneill 	}
    377      1.1  jmcneill 
    378      1.1  jmcneill 	if (of_getprop_uint32(sc->sc_phandle,
    379      1.1  jmcneill 	    "cellwise,monitor-interval", &sc->sc_monitor_interval) != 0) {
    380      1.1  jmcneill 		sc->sc_monitor_interval = CWFG_MONITOR_INTERVAL_DEFAULT;
    381      1.1  jmcneill 	}
    382      1.1  jmcneill 
    383      1.1  jmcneill 	if (of_getprop_uint32(sc->sc_phandle,
    384      1.1  jmcneill 	    "cellwise,design-capacity", &sc->sc_design_capacity) != 0) {
    385      1.1  jmcneill 		sc->sc_design_capacity = CWFG_DESIGN_CAPACITY_DEFAULT;
    386      1.1  jmcneill 	}
    387      1.1  jmcneill 
    388      1.1  jmcneill 	if (of_getprop_uint32(sc->sc_phandle,
    389      1.1  jmcneill 	    "cellwise,alert-level", &sc->sc_alert_level) != 0) {
    390      1.1  jmcneill 		sc->sc_alert_level = CWFG_ALERT_LEVEL_DEFAULT;
    391      1.1  jmcneill 	}
    392      1.1  jmcneill 
    393      1.1  jmcneill 	return 0;
    394      1.1  jmcneill }
    395      1.1  jmcneill 
    396      1.1  jmcneill static int
    397      1.1  jmcneill cwfg_match(device_t parent, cfdata_t match, void *aux)
    398      1.1  jmcneill {
    399      1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    400      1.1  jmcneill 	int match_result;
    401      1.1  jmcneill 
    402      1.1  jmcneill 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    403      1.1  jmcneill 		return match_result;
    404      1.1  jmcneill 
    405      1.1  jmcneill 	/* This device is direct-config only. */
    406      1.1  jmcneill 
    407      1.1  jmcneill 	return 0;
    408      1.1  jmcneill }
    409      1.1  jmcneill 
    410      1.1  jmcneill static void
    411      1.1  jmcneill cwfg_attach(device_t parent, device_t self, void *aux)
    412      1.1  jmcneill {
    413      1.1  jmcneill 	struct cwfg_softc *sc = device_private(self);
    414      1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    415      1.1  jmcneill 	uint8_t ver;
    416      1.1  jmcneill 	int error;
    417      1.1  jmcneill 
    418      1.1  jmcneill 	sc->sc_dev = self;
    419      1.1  jmcneill 	sc->sc_i2c = ia->ia_tag;
    420      1.1  jmcneill 	sc->sc_addr = ia->ia_addr;
    421  1.4.4.1   thorpej 	sc->sc_phandle = devhandle_to_of(device_handle(self));
    422      1.1  jmcneill 
    423      1.1  jmcneill 	cwfg_lock(sc);
    424      1.1  jmcneill 	error = cwfg_read(sc, VERSION_REG, &ver);
    425      1.1  jmcneill 	cwfg_unlock(sc);
    426      1.1  jmcneill 
    427      1.1  jmcneill 	if (error != 0) {
    428      1.1  jmcneill 		aprint_error(": device not responding, error = %d\n", error);
    429      1.1  jmcneill 		return;
    430      1.1  jmcneill 	}
    431      1.1  jmcneill 
    432      1.1  jmcneill 	aprint_naive("\n");
    433      1.1  jmcneill 	aprint_normal(": CellWise CW2015 Fuel Gauge IC (ver. 0x%02x)\n", ver);
    434      1.1  jmcneill 
    435      1.1  jmcneill 	if (cwfg_parse_resources(sc) != 0) {
    436      1.1  jmcneill 		aprint_error_dev(self, "failed to parse resources\n");
    437      1.1  jmcneill 		return;
    438      1.1  jmcneill 	}
    439      1.1  jmcneill 
    440      1.1  jmcneill 	if (cwfg_init(sc) != 0) {
    441      1.1  jmcneill 		aprint_error_dev(self, "failed to initialize device\n");
    442      1.1  jmcneill 		return;
    443      1.1  jmcneill 	}
    444      1.1  jmcneill 
    445      1.1  jmcneill 	cwfg_attach_sensors(sc);
    446      1.1  jmcneill }
    447      1.1  jmcneill 
    448      1.1  jmcneill CFATTACH_DECL_NEW(cwfg, sizeof(struct cwfg_softc),
    449      1.1  jmcneill     cwfg_match, cwfg_attach, NULL, NULL);
    450