Home | History | Annotate | Line # | Download | only in i2c
as3722.c revision 1.6
      1  1.6  jmcneill /* $NetBSD: as3722.c,v 1.6 2017/04/22 13:26:05 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2015 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.6  jmcneill #include "opt_fdt.h"
     30  1.6  jmcneill 
     31  1.1  jmcneill #include <sys/cdefs.h>
     32  1.6  jmcneill __KERNEL_RCSID(0, "$NetBSD: as3722.c,v 1.6 2017/04/22 13:26:05 jmcneill Exp $");
     33  1.1  jmcneill 
     34  1.1  jmcneill #include <sys/param.h>
     35  1.1  jmcneill #include <sys/systm.h>
     36  1.1  jmcneill #include <sys/kernel.h>
     37  1.1  jmcneill #include <sys/device.h>
     38  1.1  jmcneill #include <sys/conf.h>
     39  1.1  jmcneill #include <sys/bus.h>
     40  1.1  jmcneill #include <sys/kmem.h>
     41  1.2  jmcneill #include <sys/wdog.h>
     42  1.2  jmcneill 
     43  1.6  jmcneill #include <dev/clock_subr.h>
     44  1.6  jmcneill 
     45  1.2  jmcneill #include <dev/sysmon/sysmonvar.h>
     46  1.1  jmcneill 
     47  1.1  jmcneill #include <dev/i2c/i2cvar.h>
     48  1.1  jmcneill #include <dev/i2c/as3722.h>
     49  1.1  jmcneill 
     50  1.6  jmcneill #ifdef FDT
     51  1.6  jmcneill #include <dev/fdt/fdtvar.h>
     52  1.6  jmcneill #endif
     53  1.6  jmcneill 
     54  1.6  jmcneill #define AS3722_START_YEAR		2000
     55  1.6  jmcneill 
     56  1.2  jmcneill #define AS3722_GPIO0_CTRL_REG		0x08
     57  1.2  jmcneill #define AS3722_GPIO0_CTRL_INVERT	__BIT(7)
     58  1.2  jmcneill #define AS3722_GPIO0_CTRL_IOSF		__BITS(6,3)
     59  1.2  jmcneill #define AS3722_GPIO0_CTRL_IOSF_GPIO	0
     60  1.2  jmcneill #define AS3722_GPIO0_CTRL_IOSF_WATCHDOG	9
     61  1.2  jmcneill #define AS3722_GPIO0_CTRL_MODE		__BITS(2,0)
     62  1.2  jmcneill #define AS3722_GPIO0_CTRL_MODE_PULLDOWN	5
     63  1.2  jmcneill 
     64  1.1  jmcneill #define AS3722_RESET_CTRL_REG		0x36
     65  1.1  jmcneill #define AS3722_RESET_CTRL_POWER_OFF	__BIT(1)
     66  1.3  jmcneill #define AS3722_RESET_CTRL_FORCE_RESET	__BIT(0)
     67  1.1  jmcneill 
     68  1.2  jmcneill #define AS3722_WATCHDOG_CTRL_REG	0x38
     69  1.2  jmcneill #define AS3722_WATCHDOG_CTRL_MODE	__BITS(2,1)
     70  1.2  jmcneill #define AS3722_WATCHDOG_CTRL_ON		__BIT(0)
     71  1.2  jmcneill 
     72  1.2  jmcneill #define AS3722_WATCHDOG_TIMER_REG	0x46
     73  1.2  jmcneill #define AS3722_WATCHDOG_TIMER_TIMER	__BITS(6,0)
     74  1.2  jmcneill 
     75  1.2  jmcneill #define AS3722_WATCHDOG_SIGNAL_REG	0x48
     76  1.2  jmcneill #define AS3722_WATCHDOG_SIGNAL_PWM_DIV	__BITS(7,6)
     77  1.2  jmcneill #define AS3722_WATCHDOG_SIGNAL_SW_SIG	__BIT(0)
     78  1.2  jmcneill 
     79  1.6  jmcneill #define AS3722_RTC_CONTROL_REG		0x60
     80  1.6  jmcneill #define AS3722_RTC_CONTROL_RTC_ON	__BIT(2)
     81  1.6  jmcneill 
     82  1.6  jmcneill #define AS3722_RTC_SECOND_REG		0x61
     83  1.6  jmcneill #define AS3722_RTC_MINUTE_REG		0x62
     84  1.6  jmcneill #define AS3722_RTC_HOUR_REG		0x63
     85  1.6  jmcneill #define AS3722_RTC_DAY_REG		0x64
     86  1.6  jmcneill #define AS3722_RTC_MONTH_REG		0x65
     87  1.6  jmcneill #define AS3722_RTC_YEAR_REG		0x66
     88  1.6  jmcneill #define AS3722_RTC_ACCESS_REG		0x6f
     89  1.6  jmcneill 
     90  1.1  jmcneill #define AS3722_ASIC_ID1_REG		0x90
     91  1.1  jmcneill #define AS3722_ASIC_ID2_REG		0x91
     92  1.1  jmcneill 
     93  1.1  jmcneill struct as3722_softc {
     94  1.1  jmcneill 	device_t	sc_dev;
     95  1.1  jmcneill 	i2c_tag_t	sc_i2c;
     96  1.1  jmcneill 	i2c_addr_t	sc_addr;
     97  1.6  jmcneill 	int		sc_phandle;
     98  1.2  jmcneill 
     99  1.2  jmcneill 	struct sysmon_wdog sc_smw;
    100  1.6  jmcneill 	struct todr_chip_handle sc_todr;
    101  1.1  jmcneill };
    102  1.1  jmcneill 
    103  1.2  jmcneill #define AS3722_WATCHDOG_DEFAULT_PERIOD	10
    104  1.2  jmcneill 
    105  1.1  jmcneill static int	as3722_match(device_t, cfdata_t, void *);
    106  1.1  jmcneill static void	as3722_attach(device_t, device_t, void *);
    107  1.1  jmcneill 
    108  1.6  jmcneill static void	as3722_wdt_attach(struct as3722_softc *);
    109  1.2  jmcneill static int	as3722_wdt_setmode(struct sysmon_wdog *);
    110  1.2  jmcneill static int	as3722_wdt_tickle(struct sysmon_wdog *);
    111  1.2  jmcneill 
    112  1.6  jmcneill static void	as3722_rtc_attach(struct as3722_softc *);
    113  1.6  jmcneill static int	as3722_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
    114  1.6  jmcneill static int	as3722_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
    115  1.6  jmcneill 
    116  1.1  jmcneill static int	as3722_read(struct as3722_softc *, uint8_t, uint8_t *, int);
    117  1.1  jmcneill static int	as3722_write(struct as3722_softc *, uint8_t, uint8_t, int);
    118  1.2  jmcneill static int	as3722_set_clear(struct as3722_softc *, uint8_t, uint8_t,
    119  1.2  jmcneill 				 uint8_t, int);
    120  1.1  jmcneill 
    121  1.1  jmcneill CFATTACH_DECL_NEW(as3722pmic, sizeof(struct as3722_softc),
    122  1.1  jmcneill     as3722_match, as3722_attach, NULL, NULL);
    123  1.1  jmcneill 
    124  1.4  jmcneill static const char * as3722_compats[] = {
    125  1.4  jmcneill 	"ams,as3722",
    126  1.4  jmcneill 	NULL
    127  1.4  jmcneill };
    128  1.4  jmcneill 
    129  1.1  jmcneill static int
    130  1.1  jmcneill as3722_match(device_t parent, cfdata_t match, void *aux)
    131  1.1  jmcneill {
    132  1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    133  1.1  jmcneill 	uint8_t reg, id1;
    134  1.1  jmcneill 	int error;
    135  1.1  jmcneill 
    136  1.4  jmcneill 	if (ia->ia_name == NULL) {
    137  1.4  jmcneill 		iic_acquire_bus(ia->ia_tag, I2C_F_POLL);
    138  1.4  jmcneill 		reg = AS3722_ASIC_ID1_REG;
    139  1.4  jmcneill 		error = iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr,
    140  1.4  jmcneill 		    &reg, 1, &id1, 1, I2C_F_POLL);
    141  1.4  jmcneill 		iic_release_bus(ia->ia_tag, I2C_F_POLL);
    142  1.4  jmcneill 
    143  1.4  jmcneill 		if (error == 0 && id1 == 0x0c)
    144  1.4  jmcneill 			return 1;
    145  1.4  jmcneill 
    146  1.4  jmcneill 		return 0;
    147  1.4  jmcneill 	} else {
    148  1.4  jmcneill 		return iic_compat_match(ia, as3722_compats);
    149  1.4  jmcneill 	}
    150  1.1  jmcneill }
    151  1.1  jmcneill 
    152  1.1  jmcneill static void
    153  1.1  jmcneill as3722_attach(device_t parent, device_t self, void *aux)
    154  1.1  jmcneill {
    155  1.1  jmcneill 	struct as3722_softc * const sc = device_private(self);
    156  1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    157  1.1  jmcneill 
    158  1.1  jmcneill 	sc->sc_dev = self;
    159  1.1  jmcneill 	sc->sc_i2c = ia->ia_tag;
    160  1.1  jmcneill 	sc->sc_addr = ia->ia_addr;
    161  1.6  jmcneill 	sc->sc_phandle = ia->ia_cookie;
    162  1.1  jmcneill 
    163  1.1  jmcneill 	aprint_naive("\n");
    164  1.5  jakllsch 	aprint_normal(": AMS AS3722\n");
    165  1.2  jmcneill 
    166  1.6  jmcneill 	as3722_wdt_attach(sc);
    167  1.6  jmcneill 	as3722_rtc_attach(sc);
    168  1.6  jmcneill }
    169  1.6  jmcneill 
    170  1.6  jmcneill static void
    171  1.6  jmcneill as3722_wdt_attach(struct as3722_softc *sc)
    172  1.6  jmcneill {
    173  1.6  jmcneill 	int error;
    174  1.6  jmcneill 
    175  1.2  jmcneill 	iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
    176  1.2  jmcneill 	error = as3722_write(sc, AS3722_GPIO0_CTRL_REG,
    177  1.2  jmcneill 	    __SHIFTIN(AS3722_GPIO0_CTRL_IOSF_GPIO,
    178  1.2  jmcneill 		      AS3722_GPIO0_CTRL_IOSF) |
    179  1.2  jmcneill 	    __SHIFTIN(AS3722_GPIO0_CTRL_MODE_PULLDOWN,
    180  1.2  jmcneill 		      AS3722_GPIO0_CTRL_MODE),
    181  1.2  jmcneill 	    I2C_F_POLL);
    182  1.2  jmcneill 	error += as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
    183  1.2  jmcneill 	    __SHIFTIN(1, AS3722_WATCHDOG_CTRL_MODE), 0, I2C_F_POLL);
    184  1.2  jmcneill 	iic_release_bus(sc->sc_i2c, I2C_F_POLL);
    185  1.2  jmcneill 
    186  1.6  jmcneill 	if (error) {
    187  1.6  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't setup watchdog\n");
    188  1.6  jmcneill 		return;
    189  1.6  jmcneill 	}
    190  1.2  jmcneill 
    191  1.6  jmcneill 	sc->sc_smw.smw_name = device_xname(sc->sc_dev);
    192  1.2  jmcneill 	sc->sc_smw.smw_cookie = sc;
    193  1.2  jmcneill 	sc->sc_smw.smw_setmode = as3722_wdt_setmode;
    194  1.2  jmcneill 	sc->sc_smw.smw_tickle = as3722_wdt_tickle;
    195  1.2  jmcneill 	sc->sc_smw.smw_period = AS3722_WATCHDOG_DEFAULT_PERIOD;
    196  1.2  jmcneill 
    197  1.6  jmcneill 	aprint_normal_dev(sc->sc_dev, "default watchdog period is %u seconds\n",
    198  1.2  jmcneill 	    sc->sc_smw.smw_period);
    199  1.2  jmcneill 
    200  1.2  jmcneill 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
    201  1.6  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't register with sysmon\n");
    202  1.6  jmcneill }
    203  1.6  jmcneill 
    204  1.6  jmcneill static void
    205  1.6  jmcneill as3722_rtc_attach(struct as3722_softc *sc)
    206  1.6  jmcneill {
    207  1.6  jmcneill 	int error;
    208  1.6  jmcneill 
    209  1.6  jmcneill 	iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
    210  1.6  jmcneill 	error = as3722_set_clear(sc, AS3722_RTC_CONTROL_REG,
    211  1.6  jmcneill 	    AS3722_RTC_CONTROL_RTC_ON, 0, I2C_F_POLL);
    212  1.6  jmcneill 	iic_release_bus(sc->sc_i2c, I2C_F_POLL);
    213  1.6  jmcneill 
    214  1.6  jmcneill 	if (error) {
    215  1.6  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't setup RTC\n");
    216  1.6  jmcneill 		return;
    217  1.6  jmcneill 	}
    218  1.6  jmcneill 
    219  1.6  jmcneill 	sc->sc_todr.todr_gettime_ymdhms = as3722_rtc_gettime;
    220  1.6  jmcneill 	sc->sc_todr.todr_settime_ymdhms = as3722_rtc_settime;
    221  1.6  jmcneill 	sc->sc_todr.cookie = sc;
    222  1.6  jmcneill #ifdef FDT
    223  1.6  jmcneill 	fdtbus_todr_attach(sc->sc_dev, sc->sc_phandle, &sc->sc_todr);
    224  1.6  jmcneill #else
    225  1.6  jmcneill 	todr_attach(&sc->sc_todr);
    226  1.6  jmcneill #endif
    227  1.1  jmcneill }
    228  1.1  jmcneill 
    229  1.1  jmcneill static int
    230  1.1  jmcneill as3722_read(struct as3722_softc *sc, uint8_t reg, uint8_t *val, int flags)
    231  1.1  jmcneill {
    232  1.1  jmcneill 	return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    233  1.1  jmcneill 	    &reg, 1, val, 1, flags);
    234  1.1  jmcneill }
    235  1.1  jmcneill 
    236  1.1  jmcneill static int
    237  1.1  jmcneill as3722_write(struct as3722_softc *sc, uint8_t reg, uint8_t val, int flags)
    238  1.1  jmcneill {
    239  1.1  jmcneill 	uint8_t buf[2] = { reg, val };
    240  1.1  jmcneill 	return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    241  1.1  jmcneill 	    NULL, 0, buf, 2, flags);
    242  1.1  jmcneill }
    243  1.1  jmcneill 
    244  1.2  jmcneill static int
    245  1.2  jmcneill as3722_set_clear(struct as3722_softc *sc, uint8_t reg, uint8_t set,
    246  1.2  jmcneill     uint8_t clr, int flags)
    247  1.2  jmcneill {
    248  1.2  jmcneill 	uint8_t old, new;
    249  1.2  jmcneill 	int error;
    250  1.2  jmcneill 
    251  1.2  jmcneill 	error = as3722_read(sc, reg, &old, flags);
    252  1.2  jmcneill 	if (error) {
    253  1.2  jmcneill 		return error;
    254  1.2  jmcneill 	}
    255  1.2  jmcneill 	new = set | (old & ~clr);
    256  1.2  jmcneill 
    257  1.2  jmcneill 	return as3722_write(sc, reg, new, flags);
    258  1.2  jmcneill }
    259  1.2  jmcneill 
    260  1.2  jmcneill static int
    261  1.2  jmcneill as3722_wdt_setmode(struct sysmon_wdog *smw)
    262  1.2  jmcneill {
    263  1.2  jmcneill 	struct as3722_softc * const sc = smw->smw_cookie;
    264  1.2  jmcneill 	int error;
    265  1.2  jmcneill 
    266  1.2  jmcneill 	const int flags = (cold ? I2C_F_POLL : 0);
    267  1.2  jmcneill 
    268  1.2  jmcneill 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    269  1.2  jmcneill 		iic_acquire_bus(sc->sc_i2c, flags);
    270  1.2  jmcneill 		error = as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
    271  1.2  jmcneill 		    0, AS3722_WATCHDOG_CTRL_ON, flags);
    272  1.2  jmcneill 		iic_release_bus(sc->sc_i2c, flags);
    273  1.2  jmcneill 		return error;
    274  1.2  jmcneill 	}
    275  1.2  jmcneill 
    276  1.2  jmcneill 	if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
    277  1.2  jmcneill 		smw->smw_period = AS3722_WATCHDOG_DEFAULT_PERIOD;
    278  1.2  jmcneill 	}
    279  1.2  jmcneill 	if (smw->smw_period < 1 || smw->smw_period > 128) {
    280  1.2  jmcneill 		return EINVAL;
    281  1.2  jmcneill 	}
    282  1.2  jmcneill 	sc->sc_smw.smw_period = smw->smw_period;
    283  1.2  jmcneill 
    284  1.2  jmcneill 	iic_acquire_bus(sc->sc_i2c, flags);
    285  1.2  jmcneill 	error = as3722_set_clear(sc, AS3722_WATCHDOG_TIMER_REG,
    286  1.2  jmcneill 	    __SHIFTIN(sc->sc_smw.smw_period - 1, AS3722_WATCHDOG_TIMER_TIMER),
    287  1.2  jmcneill 	    AS3722_WATCHDOG_TIMER_TIMER, flags);
    288  1.2  jmcneill 	if (error == 0) {
    289  1.2  jmcneill 		error = as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
    290  1.2  jmcneill 		    AS3722_WATCHDOG_CTRL_ON, 0, flags);
    291  1.2  jmcneill 	}
    292  1.2  jmcneill 	iic_release_bus(sc->sc_i2c, flags);
    293  1.2  jmcneill 
    294  1.2  jmcneill 	return error;
    295  1.2  jmcneill }
    296  1.2  jmcneill 
    297  1.2  jmcneill static int
    298  1.2  jmcneill as3722_wdt_tickle(struct sysmon_wdog *smw)
    299  1.2  jmcneill {
    300  1.2  jmcneill 	struct as3722_softc * const sc = smw->smw_cookie;
    301  1.2  jmcneill 	int error;
    302  1.2  jmcneill 
    303  1.2  jmcneill 	const int flags = (cold ? I2C_F_POLL : 0);
    304  1.2  jmcneill 
    305  1.2  jmcneill 	iic_acquire_bus(sc->sc_i2c, flags);
    306  1.2  jmcneill 	error = as3722_set_clear(sc, AS3722_WATCHDOG_SIGNAL_REG,
    307  1.2  jmcneill 	    AS3722_WATCHDOG_SIGNAL_SW_SIG, 0, flags);
    308  1.2  jmcneill 	iic_release_bus(sc->sc_i2c, flags);
    309  1.2  jmcneill 
    310  1.2  jmcneill 	return error;
    311  1.2  jmcneill }
    312  1.2  jmcneill 
    313  1.6  jmcneill static int
    314  1.6  jmcneill as3722_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    315  1.6  jmcneill {
    316  1.6  jmcneill 	struct as3722_softc * const sc = tch->cookie;
    317  1.6  jmcneill 	uint8_t buf[6];
    318  1.6  jmcneill 	int error = 0;
    319  1.6  jmcneill 
    320  1.6  jmcneill 	const int flags = (cold ? I2C_F_POLL : 0);
    321  1.6  jmcneill 
    322  1.6  jmcneill 	iic_acquire_bus(sc->sc_i2c, flags);
    323  1.6  jmcneill 	error += as3722_read(sc, AS3722_RTC_SECOND_REG, &buf[0], flags);
    324  1.6  jmcneill 	error += as3722_read(sc, AS3722_RTC_MINUTE_REG, &buf[1], flags);
    325  1.6  jmcneill 	error += as3722_read(sc, AS3722_RTC_HOUR_REG, &buf[2], flags);
    326  1.6  jmcneill 	error += as3722_read(sc, AS3722_RTC_DAY_REG, &buf[3], flags);
    327  1.6  jmcneill 	error += as3722_read(sc, AS3722_RTC_MONTH_REG, &buf[4], flags);
    328  1.6  jmcneill 	error += as3722_read(sc, AS3722_RTC_YEAR_REG, &buf[5], flags);
    329  1.6  jmcneill 	iic_release_bus(sc->sc_i2c, flags);
    330  1.6  jmcneill 
    331  1.6  jmcneill 	if (error)
    332  1.6  jmcneill 		return error;
    333  1.6  jmcneill 
    334  1.6  jmcneill 	dt->dt_sec = bcdtobin(buf[0] & 0x7f);
    335  1.6  jmcneill 	dt->dt_min = bcdtobin(buf[1] & 0x7f);
    336  1.6  jmcneill 	dt->dt_hour = bcdtobin(buf[2] & 0x3f);
    337  1.6  jmcneill 	dt->dt_day = bcdtobin(buf[3] & 0x3f);
    338  1.6  jmcneill 	dt->dt_mon = bcdtobin(buf[4] & 0x1f) - 1;
    339  1.6  jmcneill 	dt->dt_year = AS3722_START_YEAR + bcdtobin(buf[5] & 0x7f);
    340  1.6  jmcneill 	dt->dt_wday = 0;
    341  1.6  jmcneill 
    342  1.6  jmcneill 	return 0;
    343  1.6  jmcneill }
    344  1.6  jmcneill 
    345  1.6  jmcneill static int
    346  1.6  jmcneill as3722_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    347  1.6  jmcneill {
    348  1.6  jmcneill 	struct as3722_softc * const sc = tch->cookie;
    349  1.6  jmcneill 	uint8_t buf[6];
    350  1.6  jmcneill 	int error = 0;
    351  1.6  jmcneill 
    352  1.6  jmcneill 	if (dt->dt_year < AS3722_START_YEAR)
    353  1.6  jmcneill 		return EINVAL;
    354  1.6  jmcneill 
    355  1.6  jmcneill 	buf[0] = bintobcd(dt->dt_sec) & 0x7f;
    356  1.6  jmcneill 	buf[1] = bintobcd(dt->dt_min) & 0x7f;
    357  1.6  jmcneill 	buf[2] = bintobcd(dt->dt_hour) & 0x3f;
    358  1.6  jmcneill 	buf[3] = bintobcd(dt->dt_day) & 0x3f;
    359  1.6  jmcneill 	buf[4] = bintobcd(dt->dt_mon + 1) & 0x1f;
    360  1.6  jmcneill 	buf[5] = bintobcd(dt->dt_year - AS3722_START_YEAR) & 0x7f;
    361  1.6  jmcneill 
    362  1.6  jmcneill 	const int flags = (cold ? I2C_F_POLL : 0);
    363  1.6  jmcneill 
    364  1.6  jmcneill 	iic_acquire_bus(sc->sc_i2c, flags);
    365  1.6  jmcneill 	error += as3722_write(sc, AS3722_RTC_SECOND_REG, buf[0], flags);
    366  1.6  jmcneill 	error += as3722_write(sc, AS3722_RTC_MINUTE_REG, buf[1], flags);
    367  1.6  jmcneill 	error += as3722_write(sc, AS3722_RTC_HOUR_REG, buf[2], flags);
    368  1.6  jmcneill 	error += as3722_write(sc, AS3722_RTC_DAY_REG, buf[3], flags);
    369  1.6  jmcneill 	error += as3722_write(sc, AS3722_RTC_MONTH_REG, buf[4], flags);
    370  1.6  jmcneill 	error += as3722_write(sc, AS3722_RTC_YEAR_REG, buf[5], flags);
    371  1.6  jmcneill 	iic_release_bus(sc->sc_i2c, flags);
    372  1.6  jmcneill 
    373  1.6  jmcneill 	return error;
    374  1.6  jmcneill }
    375  1.6  jmcneill 
    376  1.1  jmcneill int
    377  1.1  jmcneill as3722_poweroff(device_t dev)
    378  1.1  jmcneill {
    379  1.1  jmcneill 	struct as3722_softc * const sc = device_private(dev);
    380  1.1  jmcneill 	int error;
    381  1.1  jmcneill 
    382  1.1  jmcneill 	const int flags = I2C_F_POLL;
    383  1.1  jmcneill 
    384  1.1  jmcneill 	iic_acquire_bus(sc->sc_i2c, flags);
    385  1.1  jmcneill 	error = as3722_write(sc, AS3722_RESET_CTRL_REG,
    386  1.1  jmcneill 	    AS3722_RESET_CTRL_POWER_OFF, flags);
    387  1.1  jmcneill 	iic_release_bus(sc->sc_i2c, flags);
    388  1.1  jmcneill 
    389  1.1  jmcneill 	return error;
    390  1.1  jmcneill }
    391  1.3  jmcneill 
    392  1.3  jmcneill int
    393  1.3  jmcneill as3722_reboot(device_t dev)
    394  1.3  jmcneill {
    395  1.3  jmcneill 	struct as3722_softc * const sc = device_private(dev);
    396  1.3  jmcneill 	int error;
    397  1.3  jmcneill 
    398  1.3  jmcneill 	const int flags = I2C_F_POLL;
    399  1.3  jmcneill 
    400  1.3  jmcneill 	iic_acquire_bus(sc->sc_i2c, flags);
    401  1.3  jmcneill 	error = as3722_write(sc, AS3722_RESET_CTRL_REG,
    402  1.3  jmcneill 	    AS3722_RESET_CTRL_FORCE_RESET, flags);
    403  1.3  jmcneill 	iic_release_bus(sc->sc_i2c, flags);
    404  1.3  jmcneill 
    405  1.3  jmcneill 	return error;
    406  1.3  jmcneill }
    407