Home | History | Annotate | Line # | Download | only in i2c
as3722.c revision 1.8
      1  1.8  jmcneill /* $NetBSD: as3722.c,v 1.8 2017/04/22 23:46:29 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.8  jmcneill __KERNEL_RCSID(0, "$NetBSD: as3722.c,v 1.8 2017/04/22 23:46:29 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.8  jmcneill #define AS3722_SD0_VOLTAGE_REG		0x00
     57  1.8  jmcneill 
     58  1.2  jmcneill #define AS3722_GPIO0_CTRL_REG		0x08
     59  1.2  jmcneill #define AS3722_GPIO0_CTRL_INVERT	__BIT(7)
     60  1.2  jmcneill #define AS3722_GPIO0_CTRL_IOSF		__BITS(6,3)
     61  1.2  jmcneill #define AS3722_GPIO0_CTRL_IOSF_GPIO	0
     62  1.2  jmcneill #define AS3722_GPIO0_CTRL_IOSF_WATCHDOG	9
     63  1.2  jmcneill #define AS3722_GPIO0_CTRL_MODE		__BITS(2,0)
     64  1.2  jmcneill #define AS3722_GPIO0_CTRL_MODE_PULLDOWN	5
     65  1.2  jmcneill 
     66  1.7  jmcneill #define AS3722_LDO6_VOLTAGE_REG		0x16
     67  1.7  jmcneill 
     68  1.1  jmcneill #define AS3722_RESET_CTRL_REG		0x36
     69  1.1  jmcneill #define AS3722_RESET_CTRL_POWER_OFF	__BIT(1)
     70  1.3  jmcneill #define AS3722_RESET_CTRL_FORCE_RESET	__BIT(0)
     71  1.1  jmcneill 
     72  1.2  jmcneill #define AS3722_WATCHDOG_CTRL_REG	0x38
     73  1.2  jmcneill #define AS3722_WATCHDOG_CTRL_MODE	__BITS(2,1)
     74  1.2  jmcneill #define AS3722_WATCHDOG_CTRL_ON		__BIT(0)
     75  1.2  jmcneill 
     76  1.2  jmcneill #define AS3722_WATCHDOG_TIMER_REG	0x46
     77  1.2  jmcneill #define AS3722_WATCHDOG_TIMER_TIMER	__BITS(6,0)
     78  1.2  jmcneill 
     79  1.2  jmcneill #define AS3722_WATCHDOG_SIGNAL_REG	0x48
     80  1.2  jmcneill #define AS3722_WATCHDOG_SIGNAL_PWM_DIV	__BITS(7,6)
     81  1.2  jmcneill #define AS3722_WATCHDOG_SIGNAL_SW_SIG	__BIT(0)
     82  1.2  jmcneill 
     83  1.7  jmcneill #define AS3722_LDOCONTROL0_REG		0x4e
     84  1.7  jmcneill 
     85  1.6  jmcneill #define AS3722_RTC_CONTROL_REG		0x60
     86  1.6  jmcneill #define AS3722_RTC_CONTROL_RTC_ON	__BIT(2)
     87  1.6  jmcneill 
     88  1.6  jmcneill #define AS3722_RTC_SECOND_REG		0x61
     89  1.6  jmcneill #define AS3722_RTC_MINUTE_REG		0x62
     90  1.6  jmcneill #define AS3722_RTC_HOUR_REG		0x63
     91  1.6  jmcneill #define AS3722_RTC_DAY_REG		0x64
     92  1.6  jmcneill #define AS3722_RTC_MONTH_REG		0x65
     93  1.6  jmcneill #define AS3722_RTC_YEAR_REG		0x66
     94  1.6  jmcneill #define AS3722_RTC_ACCESS_REG		0x6f
     95  1.6  jmcneill 
     96  1.1  jmcneill #define AS3722_ASIC_ID1_REG		0x90
     97  1.1  jmcneill #define AS3722_ASIC_ID2_REG		0x91
     98  1.1  jmcneill 
     99  1.1  jmcneill struct as3722_softc {
    100  1.1  jmcneill 	device_t	sc_dev;
    101  1.1  jmcneill 	i2c_tag_t	sc_i2c;
    102  1.1  jmcneill 	i2c_addr_t	sc_addr;
    103  1.6  jmcneill 	int		sc_phandle;
    104  1.2  jmcneill 
    105  1.2  jmcneill 	struct sysmon_wdog sc_smw;
    106  1.6  jmcneill 	struct todr_chip_handle sc_todr;
    107  1.8  jmcneill 
    108  1.8  jmcneill 	uint8_t		sc_fuse[8];
    109  1.1  jmcneill };
    110  1.1  jmcneill 
    111  1.7  jmcneill #ifdef FDT
    112  1.8  jmcneill static int	as3722reg_set_voltage_sd0(device_t, u_int, u_int);
    113  1.8  jmcneill static int	as3722reg_get_voltage_sd0(device_t, u_int *);
    114  1.8  jmcneill static int	as3722reg_set_voltage_ldo(device_t, u_int, u_int);
    115  1.8  jmcneill static int	as3722reg_get_voltage_ldo(device_t, u_int *);
    116  1.8  jmcneill 
    117  1.7  jmcneill static const struct as3722regdef {
    118  1.7  jmcneill 	const char	*name;
    119  1.7  jmcneill 	u_int		vsel_reg;
    120  1.7  jmcneill 	u_int		vsel_mask;
    121  1.7  jmcneill 	u_int		enable_reg;
    122  1.7  jmcneill 	u_int		enable_mask;
    123  1.8  jmcneill 	int		(*set)(device_t, u_int, u_int);
    124  1.8  jmcneill 	int		(*get)(device_t, u_int *);
    125  1.7  jmcneill } as3722regdefs[] = {
    126  1.8  jmcneill 	{ .name = "sd0",
    127  1.8  jmcneill 	  .vsel_reg = AS3722_SD0_VOLTAGE_REG,
    128  1.8  jmcneill 	  .vsel_mask = 0x7f,
    129  1.8  jmcneill 	  .set = as3722reg_set_voltage_sd0,
    130  1.8  jmcneill 	  .get = as3722reg_get_voltage_sd0 },
    131  1.7  jmcneill 	{ .name = "ldo6",
    132  1.7  jmcneill 	  .vsel_reg = AS3722_LDO6_VOLTAGE_REG,
    133  1.7  jmcneill 	  .vsel_mask = 0x7f,
    134  1.7  jmcneill 	  .enable_reg = AS3722_LDOCONTROL0_REG,
    135  1.7  jmcneill 	  .enable_mask = 0x40,
    136  1.8  jmcneill 	  .set = as3722reg_set_voltage_ldo,
    137  1.8  jmcneill 	  .get = as3722reg_get_voltage_ldo },
    138  1.7  jmcneill };
    139  1.7  jmcneill 
    140  1.7  jmcneill struct as3722reg_softc {
    141  1.7  jmcneill 	device_t	sc_dev;
    142  1.7  jmcneill 	int		sc_phandle;
    143  1.7  jmcneill 	const struct as3722regdef *sc_regdef;
    144  1.7  jmcneill };
    145  1.7  jmcneill 
    146  1.7  jmcneill struct as3722reg_attach_args {
    147  1.7  jmcneill 	const struct as3722regdef *reg_def;
    148  1.7  jmcneill 	int		reg_phandle;
    149  1.7  jmcneill };
    150  1.7  jmcneill #endif
    151  1.7  jmcneill 
    152  1.2  jmcneill #define AS3722_WATCHDOG_DEFAULT_PERIOD	10
    153  1.2  jmcneill 
    154  1.1  jmcneill static int	as3722_match(device_t, cfdata_t, void *);
    155  1.1  jmcneill static void	as3722_attach(device_t, device_t, void *);
    156  1.1  jmcneill 
    157  1.6  jmcneill static void	as3722_wdt_attach(struct as3722_softc *);
    158  1.2  jmcneill static int	as3722_wdt_setmode(struct sysmon_wdog *);
    159  1.2  jmcneill static int	as3722_wdt_tickle(struct sysmon_wdog *);
    160  1.2  jmcneill 
    161  1.6  jmcneill static void	as3722_rtc_attach(struct as3722_softc *);
    162  1.6  jmcneill static int	as3722_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
    163  1.6  jmcneill static int	as3722_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
    164  1.6  jmcneill 
    165  1.7  jmcneill #ifdef FDT
    166  1.7  jmcneill static void	as3722_regulator_attach(struct as3722_softc *);
    167  1.7  jmcneill static int	as3722reg_match(device_t, cfdata_t, void *);
    168  1.7  jmcneill static void	as3722reg_attach(device_t, device_t, void *);
    169  1.7  jmcneill 
    170  1.7  jmcneill static int	as3722reg_acquire(device_t);
    171  1.7  jmcneill static void	as3722reg_release(device_t);
    172  1.7  jmcneill static int	as3722reg_enable(device_t, bool);
    173  1.7  jmcneill static int	as3722reg_set_voltage(device_t, u_int, u_int);
    174  1.7  jmcneill static int	as3722reg_get_voltage(device_t, u_int *);
    175  1.7  jmcneill 
    176  1.7  jmcneill static struct fdtbus_regulator_controller_func as3722reg_funcs = {
    177  1.7  jmcneill 	.acquire = as3722reg_acquire,
    178  1.7  jmcneill 	.release = as3722reg_release,
    179  1.7  jmcneill 	.enable = as3722reg_enable,
    180  1.7  jmcneill 	.set_voltage = as3722reg_set_voltage,
    181  1.7  jmcneill 	.get_voltage = as3722reg_get_voltage,
    182  1.7  jmcneill };
    183  1.7  jmcneill #endif
    184  1.7  jmcneill 
    185  1.1  jmcneill static int	as3722_read(struct as3722_softc *, uint8_t, uint8_t *, int);
    186  1.1  jmcneill static int	as3722_write(struct as3722_softc *, uint8_t, uint8_t, int);
    187  1.2  jmcneill static int	as3722_set_clear(struct as3722_softc *, uint8_t, uint8_t,
    188  1.2  jmcneill 				 uint8_t, int);
    189  1.1  jmcneill 
    190  1.1  jmcneill CFATTACH_DECL_NEW(as3722pmic, sizeof(struct as3722_softc),
    191  1.1  jmcneill     as3722_match, as3722_attach, NULL, NULL);
    192  1.1  jmcneill 
    193  1.7  jmcneill #ifdef FDT
    194  1.7  jmcneill CFATTACH_DECL_NEW(as3722reg, sizeof(struct as3722reg_softc),
    195  1.7  jmcneill     as3722reg_match, as3722reg_attach, NULL, NULL);
    196  1.7  jmcneill #endif
    197  1.7  jmcneill 
    198  1.4  jmcneill static const char * as3722_compats[] = {
    199  1.4  jmcneill 	"ams,as3722",
    200  1.4  jmcneill 	NULL
    201  1.4  jmcneill };
    202  1.4  jmcneill 
    203  1.1  jmcneill static int
    204  1.1  jmcneill as3722_match(device_t parent, cfdata_t match, void *aux)
    205  1.1  jmcneill {
    206  1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    207  1.1  jmcneill 	uint8_t reg, id1;
    208  1.1  jmcneill 	int error;
    209  1.1  jmcneill 
    210  1.4  jmcneill 	if (ia->ia_name == NULL) {
    211  1.4  jmcneill 		iic_acquire_bus(ia->ia_tag, I2C_F_POLL);
    212  1.4  jmcneill 		reg = AS3722_ASIC_ID1_REG;
    213  1.4  jmcneill 		error = iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr,
    214  1.4  jmcneill 		    &reg, 1, &id1, 1, I2C_F_POLL);
    215  1.4  jmcneill 		iic_release_bus(ia->ia_tag, I2C_F_POLL);
    216  1.4  jmcneill 
    217  1.4  jmcneill 		if (error == 0 && id1 == 0x0c)
    218  1.4  jmcneill 			return 1;
    219  1.4  jmcneill 
    220  1.4  jmcneill 		return 0;
    221  1.4  jmcneill 	} else {
    222  1.4  jmcneill 		return iic_compat_match(ia, as3722_compats);
    223  1.4  jmcneill 	}
    224  1.1  jmcneill }
    225  1.1  jmcneill 
    226  1.1  jmcneill static void
    227  1.1  jmcneill as3722_attach(device_t parent, device_t self, void *aux)
    228  1.1  jmcneill {
    229  1.1  jmcneill 	struct as3722_softc * const sc = device_private(self);
    230  1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    231  1.1  jmcneill 
    232  1.1  jmcneill 	sc->sc_dev = self;
    233  1.1  jmcneill 	sc->sc_i2c = ia->ia_tag;
    234  1.1  jmcneill 	sc->sc_addr = ia->ia_addr;
    235  1.6  jmcneill 	sc->sc_phandle = ia->ia_cookie;
    236  1.1  jmcneill 
    237  1.1  jmcneill 	aprint_naive("\n");
    238  1.5  jakllsch 	aprint_normal(": AMS AS3722\n");
    239  1.2  jmcneill 
    240  1.6  jmcneill 	as3722_wdt_attach(sc);
    241  1.6  jmcneill 	as3722_rtc_attach(sc);
    242  1.7  jmcneill #ifdef FDT
    243  1.7  jmcneill 	as3722_regulator_attach(sc);
    244  1.7  jmcneill #endif
    245  1.6  jmcneill }
    246  1.6  jmcneill 
    247  1.6  jmcneill static void
    248  1.6  jmcneill as3722_wdt_attach(struct as3722_softc *sc)
    249  1.6  jmcneill {
    250  1.6  jmcneill 	int error;
    251  1.6  jmcneill 
    252  1.2  jmcneill 	iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
    253  1.2  jmcneill 	error = as3722_write(sc, AS3722_GPIO0_CTRL_REG,
    254  1.2  jmcneill 	    __SHIFTIN(AS3722_GPIO0_CTRL_IOSF_GPIO,
    255  1.2  jmcneill 		      AS3722_GPIO0_CTRL_IOSF) |
    256  1.2  jmcneill 	    __SHIFTIN(AS3722_GPIO0_CTRL_MODE_PULLDOWN,
    257  1.2  jmcneill 		      AS3722_GPIO0_CTRL_MODE),
    258  1.2  jmcneill 	    I2C_F_POLL);
    259  1.2  jmcneill 	error += as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
    260  1.2  jmcneill 	    __SHIFTIN(1, AS3722_WATCHDOG_CTRL_MODE), 0, I2C_F_POLL);
    261  1.2  jmcneill 	iic_release_bus(sc->sc_i2c, I2C_F_POLL);
    262  1.2  jmcneill 
    263  1.6  jmcneill 	if (error) {
    264  1.6  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't setup watchdog\n");
    265  1.6  jmcneill 		return;
    266  1.6  jmcneill 	}
    267  1.2  jmcneill 
    268  1.6  jmcneill 	sc->sc_smw.smw_name = device_xname(sc->sc_dev);
    269  1.2  jmcneill 	sc->sc_smw.smw_cookie = sc;
    270  1.2  jmcneill 	sc->sc_smw.smw_setmode = as3722_wdt_setmode;
    271  1.2  jmcneill 	sc->sc_smw.smw_tickle = as3722_wdt_tickle;
    272  1.2  jmcneill 	sc->sc_smw.smw_period = AS3722_WATCHDOG_DEFAULT_PERIOD;
    273  1.2  jmcneill 
    274  1.6  jmcneill 	aprint_normal_dev(sc->sc_dev, "default watchdog period is %u seconds\n",
    275  1.2  jmcneill 	    sc->sc_smw.smw_period);
    276  1.2  jmcneill 
    277  1.2  jmcneill 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
    278  1.6  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't register with sysmon\n");
    279  1.6  jmcneill }
    280  1.6  jmcneill 
    281  1.6  jmcneill static void
    282  1.6  jmcneill as3722_rtc_attach(struct as3722_softc *sc)
    283  1.6  jmcneill {
    284  1.6  jmcneill 	int error;
    285  1.6  jmcneill 
    286  1.6  jmcneill 	iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
    287  1.6  jmcneill 	error = as3722_set_clear(sc, AS3722_RTC_CONTROL_REG,
    288  1.6  jmcneill 	    AS3722_RTC_CONTROL_RTC_ON, 0, I2C_F_POLL);
    289  1.6  jmcneill 	iic_release_bus(sc->sc_i2c, I2C_F_POLL);
    290  1.6  jmcneill 
    291  1.6  jmcneill 	if (error) {
    292  1.6  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't setup RTC\n");
    293  1.6  jmcneill 		return;
    294  1.6  jmcneill 	}
    295  1.6  jmcneill 
    296  1.6  jmcneill 	sc->sc_todr.todr_gettime_ymdhms = as3722_rtc_gettime;
    297  1.6  jmcneill 	sc->sc_todr.todr_settime_ymdhms = as3722_rtc_settime;
    298  1.6  jmcneill 	sc->sc_todr.cookie = sc;
    299  1.6  jmcneill #ifdef FDT
    300  1.6  jmcneill 	fdtbus_todr_attach(sc->sc_dev, sc->sc_phandle, &sc->sc_todr);
    301  1.6  jmcneill #else
    302  1.6  jmcneill 	todr_attach(&sc->sc_todr);
    303  1.6  jmcneill #endif
    304  1.1  jmcneill }
    305  1.1  jmcneill 
    306  1.1  jmcneill static int
    307  1.1  jmcneill as3722_read(struct as3722_softc *sc, uint8_t reg, uint8_t *val, int flags)
    308  1.1  jmcneill {
    309  1.1  jmcneill 	return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    310  1.1  jmcneill 	    &reg, 1, val, 1, flags);
    311  1.1  jmcneill }
    312  1.1  jmcneill 
    313  1.1  jmcneill static int
    314  1.1  jmcneill as3722_write(struct as3722_softc *sc, uint8_t reg, uint8_t val, int flags)
    315  1.1  jmcneill {
    316  1.1  jmcneill 	uint8_t buf[2] = { reg, val };
    317  1.1  jmcneill 	return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    318  1.1  jmcneill 	    NULL, 0, buf, 2, flags);
    319  1.1  jmcneill }
    320  1.1  jmcneill 
    321  1.2  jmcneill static int
    322  1.2  jmcneill as3722_set_clear(struct as3722_softc *sc, uint8_t reg, uint8_t set,
    323  1.2  jmcneill     uint8_t clr, int flags)
    324  1.2  jmcneill {
    325  1.2  jmcneill 	uint8_t old, new;
    326  1.2  jmcneill 	int error;
    327  1.2  jmcneill 
    328  1.2  jmcneill 	error = as3722_read(sc, reg, &old, flags);
    329  1.2  jmcneill 	if (error) {
    330  1.2  jmcneill 		return error;
    331  1.2  jmcneill 	}
    332  1.2  jmcneill 	new = set | (old & ~clr);
    333  1.2  jmcneill 
    334  1.2  jmcneill 	return as3722_write(sc, reg, new, flags);
    335  1.2  jmcneill }
    336  1.2  jmcneill 
    337  1.2  jmcneill static int
    338  1.2  jmcneill as3722_wdt_setmode(struct sysmon_wdog *smw)
    339  1.2  jmcneill {
    340  1.2  jmcneill 	struct as3722_softc * const sc = smw->smw_cookie;
    341  1.2  jmcneill 	int error;
    342  1.2  jmcneill 
    343  1.2  jmcneill 	const int flags = (cold ? I2C_F_POLL : 0);
    344  1.2  jmcneill 
    345  1.2  jmcneill 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    346  1.2  jmcneill 		iic_acquire_bus(sc->sc_i2c, flags);
    347  1.2  jmcneill 		error = as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
    348  1.2  jmcneill 		    0, AS3722_WATCHDOG_CTRL_ON, flags);
    349  1.2  jmcneill 		iic_release_bus(sc->sc_i2c, flags);
    350  1.2  jmcneill 		return error;
    351  1.2  jmcneill 	}
    352  1.2  jmcneill 
    353  1.2  jmcneill 	if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
    354  1.2  jmcneill 		smw->smw_period = AS3722_WATCHDOG_DEFAULT_PERIOD;
    355  1.2  jmcneill 	}
    356  1.2  jmcneill 	if (smw->smw_period < 1 || smw->smw_period > 128) {
    357  1.2  jmcneill 		return EINVAL;
    358  1.2  jmcneill 	}
    359  1.2  jmcneill 	sc->sc_smw.smw_period = smw->smw_period;
    360  1.2  jmcneill 
    361  1.2  jmcneill 	iic_acquire_bus(sc->sc_i2c, flags);
    362  1.2  jmcneill 	error = as3722_set_clear(sc, AS3722_WATCHDOG_TIMER_REG,
    363  1.2  jmcneill 	    __SHIFTIN(sc->sc_smw.smw_period - 1, AS3722_WATCHDOG_TIMER_TIMER),
    364  1.2  jmcneill 	    AS3722_WATCHDOG_TIMER_TIMER, flags);
    365  1.2  jmcneill 	if (error == 0) {
    366  1.2  jmcneill 		error = as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
    367  1.2  jmcneill 		    AS3722_WATCHDOG_CTRL_ON, 0, flags);
    368  1.2  jmcneill 	}
    369  1.2  jmcneill 	iic_release_bus(sc->sc_i2c, flags);
    370  1.2  jmcneill 
    371  1.2  jmcneill 	return error;
    372  1.2  jmcneill }
    373  1.2  jmcneill 
    374  1.2  jmcneill static int
    375  1.2  jmcneill as3722_wdt_tickle(struct sysmon_wdog *smw)
    376  1.2  jmcneill {
    377  1.2  jmcneill 	struct as3722_softc * const sc = smw->smw_cookie;
    378  1.2  jmcneill 	int error;
    379  1.2  jmcneill 
    380  1.2  jmcneill 	const int flags = (cold ? I2C_F_POLL : 0);
    381  1.2  jmcneill 
    382  1.2  jmcneill 	iic_acquire_bus(sc->sc_i2c, flags);
    383  1.2  jmcneill 	error = as3722_set_clear(sc, AS3722_WATCHDOG_SIGNAL_REG,
    384  1.2  jmcneill 	    AS3722_WATCHDOG_SIGNAL_SW_SIG, 0, flags);
    385  1.2  jmcneill 	iic_release_bus(sc->sc_i2c, flags);
    386  1.2  jmcneill 
    387  1.2  jmcneill 	return error;
    388  1.2  jmcneill }
    389  1.2  jmcneill 
    390  1.6  jmcneill static int
    391  1.6  jmcneill as3722_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    392  1.6  jmcneill {
    393  1.6  jmcneill 	struct as3722_softc * const sc = tch->cookie;
    394  1.6  jmcneill 	uint8_t buf[6];
    395  1.6  jmcneill 	int error = 0;
    396  1.6  jmcneill 
    397  1.6  jmcneill 	const int flags = (cold ? I2C_F_POLL : 0);
    398  1.6  jmcneill 
    399  1.6  jmcneill 	iic_acquire_bus(sc->sc_i2c, flags);
    400  1.6  jmcneill 	error += as3722_read(sc, AS3722_RTC_SECOND_REG, &buf[0], flags);
    401  1.6  jmcneill 	error += as3722_read(sc, AS3722_RTC_MINUTE_REG, &buf[1], flags);
    402  1.6  jmcneill 	error += as3722_read(sc, AS3722_RTC_HOUR_REG, &buf[2], flags);
    403  1.6  jmcneill 	error += as3722_read(sc, AS3722_RTC_DAY_REG, &buf[3], flags);
    404  1.6  jmcneill 	error += as3722_read(sc, AS3722_RTC_MONTH_REG, &buf[4], flags);
    405  1.6  jmcneill 	error += as3722_read(sc, AS3722_RTC_YEAR_REG, &buf[5], flags);
    406  1.6  jmcneill 	iic_release_bus(sc->sc_i2c, flags);
    407  1.6  jmcneill 
    408  1.6  jmcneill 	if (error)
    409  1.6  jmcneill 		return error;
    410  1.6  jmcneill 
    411  1.6  jmcneill 	dt->dt_sec = bcdtobin(buf[0] & 0x7f);
    412  1.6  jmcneill 	dt->dt_min = bcdtobin(buf[1] & 0x7f);
    413  1.6  jmcneill 	dt->dt_hour = bcdtobin(buf[2] & 0x3f);
    414  1.6  jmcneill 	dt->dt_day = bcdtobin(buf[3] & 0x3f);
    415  1.6  jmcneill 	dt->dt_mon = bcdtobin(buf[4] & 0x1f) - 1;
    416  1.6  jmcneill 	dt->dt_year = AS3722_START_YEAR + bcdtobin(buf[5] & 0x7f);
    417  1.6  jmcneill 	dt->dt_wday = 0;
    418  1.6  jmcneill 
    419  1.6  jmcneill 	return 0;
    420  1.6  jmcneill }
    421  1.6  jmcneill 
    422  1.6  jmcneill static int
    423  1.6  jmcneill as3722_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    424  1.6  jmcneill {
    425  1.6  jmcneill 	struct as3722_softc * const sc = tch->cookie;
    426  1.6  jmcneill 	uint8_t buf[6];
    427  1.6  jmcneill 	int error = 0;
    428  1.6  jmcneill 
    429  1.6  jmcneill 	if (dt->dt_year < AS3722_START_YEAR)
    430  1.6  jmcneill 		return EINVAL;
    431  1.6  jmcneill 
    432  1.6  jmcneill 	buf[0] = bintobcd(dt->dt_sec) & 0x7f;
    433  1.6  jmcneill 	buf[1] = bintobcd(dt->dt_min) & 0x7f;
    434  1.6  jmcneill 	buf[2] = bintobcd(dt->dt_hour) & 0x3f;
    435  1.6  jmcneill 	buf[3] = bintobcd(dt->dt_day) & 0x3f;
    436  1.6  jmcneill 	buf[4] = bintobcd(dt->dt_mon + 1) & 0x1f;
    437  1.6  jmcneill 	buf[5] = bintobcd(dt->dt_year - AS3722_START_YEAR) & 0x7f;
    438  1.6  jmcneill 
    439  1.6  jmcneill 	const int flags = (cold ? I2C_F_POLL : 0);
    440  1.6  jmcneill 
    441  1.6  jmcneill 	iic_acquire_bus(sc->sc_i2c, flags);
    442  1.6  jmcneill 	error += as3722_write(sc, AS3722_RTC_SECOND_REG, buf[0], flags);
    443  1.6  jmcneill 	error += as3722_write(sc, AS3722_RTC_MINUTE_REG, buf[1], flags);
    444  1.6  jmcneill 	error += as3722_write(sc, AS3722_RTC_HOUR_REG, buf[2], flags);
    445  1.6  jmcneill 	error += as3722_write(sc, AS3722_RTC_DAY_REG, buf[3], flags);
    446  1.6  jmcneill 	error += as3722_write(sc, AS3722_RTC_MONTH_REG, buf[4], flags);
    447  1.6  jmcneill 	error += as3722_write(sc, AS3722_RTC_YEAR_REG, buf[5], flags);
    448  1.6  jmcneill 	iic_release_bus(sc->sc_i2c, flags);
    449  1.6  jmcneill 
    450  1.6  jmcneill 	return error;
    451  1.6  jmcneill }
    452  1.6  jmcneill 
    453  1.7  jmcneill #ifdef FDT
    454  1.7  jmcneill static void
    455  1.7  jmcneill as3722_regulator_attach(struct as3722_softc *sc)
    456  1.7  jmcneill {
    457  1.7  jmcneill 	struct as3722reg_attach_args raa;
    458  1.7  jmcneill 	int phandle, child;
    459  1.7  jmcneill 
    460  1.7  jmcneill 	phandle = of_find_firstchild_byname(sc->sc_phandle, "regulators");
    461  1.7  jmcneill 	if (phandle <= 0)
    462  1.7  jmcneill 		return;
    463  1.7  jmcneill 
    464  1.7  jmcneill 	for (int i = 0; i < __arraycount(as3722regdefs); i++) {
    465  1.7  jmcneill 		const struct as3722regdef *regdef = &as3722regdefs[i];
    466  1.7  jmcneill 		child = of_find_firstchild_byname(phandle, regdef->name);
    467  1.7  jmcneill 		if (child <= 0)
    468  1.7  jmcneill 			continue;
    469  1.7  jmcneill 		raa.reg_def = regdef;
    470  1.7  jmcneill 		raa.reg_phandle = child;
    471  1.7  jmcneill 		config_found(sc->sc_dev, &raa, NULL);
    472  1.7  jmcneill 	}
    473  1.7  jmcneill }
    474  1.7  jmcneill 
    475  1.7  jmcneill static int
    476  1.7  jmcneill as3722reg_match(device_t parent, cfdata_t match, void *aux)
    477  1.7  jmcneill {
    478  1.7  jmcneill 	return 1;
    479  1.7  jmcneill }
    480  1.7  jmcneill 
    481  1.7  jmcneill static void
    482  1.7  jmcneill as3722reg_attach(device_t parent, device_t self, void *aux)
    483  1.7  jmcneill {
    484  1.7  jmcneill 	struct as3722reg_softc *sc = device_private(self);
    485  1.7  jmcneill 	struct as3722reg_attach_args *raa = aux;
    486  1.7  jmcneill 	char *name = NULL;
    487  1.7  jmcneill 	int len;
    488  1.7  jmcneill 
    489  1.7  jmcneill 	sc->sc_dev = self;
    490  1.7  jmcneill 	sc->sc_phandle = raa->reg_phandle;
    491  1.7  jmcneill 	sc->sc_regdef = raa->reg_def;
    492  1.7  jmcneill 
    493  1.7  jmcneill 	fdtbus_register_regulator_controller(self, sc->sc_phandle,
    494  1.7  jmcneill 	    &as3722reg_funcs);
    495  1.7  jmcneill 
    496  1.7  jmcneill 	len = OF_getproplen(sc->sc_phandle, "regulator-name");
    497  1.7  jmcneill 	if (len > 0) {
    498  1.7  jmcneill 		name = kmem_zalloc(len, KM_SLEEP);
    499  1.7  jmcneill 		OF_getprop(sc->sc_phandle, "regulator-name", name, len);
    500  1.7  jmcneill 	}
    501  1.7  jmcneill 
    502  1.7  jmcneill 	aprint_naive("\n");
    503  1.7  jmcneill 	if (name)
    504  1.7  jmcneill 		aprint_normal(": %s\n", name);
    505  1.7  jmcneill 	else
    506  1.7  jmcneill 		aprint_normal("\n");
    507  1.7  jmcneill 
    508  1.7  jmcneill 	if (name)
    509  1.7  jmcneill 		kmem_free(name, len);
    510  1.7  jmcneill }
    511  1.7  jmcneill 
    512  1.7  jmcneill static int
    513  1.7  jmcneill as3722reg_acquire(device_t dev)
    514  1.7  jmcneill {
    515  1.7  jmcneill 	return 0;
    516  1.7  jmcneill }
    517  1.7  jmcneill 
    518  1.7  jmcneill static void
    519  1.7  jmcneill as3722reg_release(device_t dev)
    520  1.7  jmcneill {
    521  1.7  jmcneill }
    522  1.7  jmcneill 
    523  1.7  jmcneill static int
    524  1.7  jmcneill as3722reg_enable(device_t dev, bool enable)
    525  1.7  jmcneill {
    526  1.7  jmcneill 	struct as3722reg_softc *sc = device_private(dev);
    527  1.7  jmcneill 	struct as3722_softc *asc = device_private(device_parent(dev));
    528  1.7  jmcneill 	const struct as3722regdef *regdef = sc->sc_regdef;
    529  1.7  jmcneill 	const int flags = (cold ? I2C_F_POLL : 0);
    530  1.7  jmcneill 	int error;
    531  1.7  jmcneill 
    532  1.8  jmcneill 	if (!regdef->enable_mask)
    533  1.8  jmcneill 		return enable ? 0 : EINVAL;
    534  1.8  jmcneill 
    535  1.7  jmcneill 	iic_acquire_bus(asc->sc_i2c, flags);
    536  1.7  jmcneill 	if (enable)
    537  1.7  jmcneill 		error = as3722_set_clear(asc, regdef->enable_reg,
    538  1.7  jmcneill 		    regdef->enable_mask, 0, flags);
    539  1.7  jmcneill 	else
    540  1.7  jmcneill 		error = as3722_set_clear(asc, regdef->enable_reg,
    541  1.7  jmcneill 		    0, regdef->enable_mask, flags);
    542  1.7  jmcneill 	iic_release_bus(asc->sc_i2c, flags);
    543  1.7  jmcneill 
    544  1.7  jmcneill 	return error;
    545  1.7  jmcneill }
    546  1.7  jmcneill 
    547  1.7  jmcneill static int
    548  1.8  jmcneill as3722reg_set_voltage_ldo(device_t dev, u_int min_uvol, u_int max_uvol)
    549  1.7  jmcneill {
    550  1.7  jmcneill 	struct as3722reg_softc *sc = device_private(dev);
    551  1.7  jmcneill 	struct as3722_softc *asc = device_private(device_parent(dev));
    552  1.7  jmcneill 	const struct as3722regdef *regdef = sc->sc_regdef;
    553  1.7  jmcneill 	const int flags = (cold ? I2C_F_POLL : 0);
    554  1.7  jmcneill 	uint8_t set_v = 0x00;
    555  1.7  jmcneill 	u_int uvol;
    556  1.7  jmcneill 	int error;
    557  1.7  jmcneill 
    558  1.7  jmcneill 	for (uint8_t v = 0x01; v <= 0x24; v++) {
    559  1.7  jmcneill 		uvol = 800000 + (v * 25000);
    560  1.7  jmcneill 		if (uvol >= min_uvol && uvol <= max_uvol) {
    561  1.7  jmcneill 			set_v = v;
    562  1.7  jmcneill 			goto done;
    563  1.7  jmcneill 		}
    564  1.7  jmcneill 	}
    565  1.7  jmcneill 	for (uint8_t v = 0x40; v <= 0x7f; v++) {
    566  1.7  jmcneill 		uvol = 1725000 + ((v - 0x40) * 25000);
    567  1.7  jmcneill 		if (uvol >= min_uvol && uvol <= max_uvol) {
    568  1.7  jmcneill 			set_v = v;
    569  1.7  jmcneill 			goto done;
    570  1.7  jmcneill 		}
    571  1.7  jmcneill 	}
    572  1.7  jmcneill 	if (set_v == 0)
    573  1.7  jmcneill 		return ERANGE;
    574  1.7  jmcneill 
    575  1.7  jmcneill done:
    576  1.7  jmcneill 	iic_acquire_bus(asc->sc_i2c, flags);
    577  1.7  jmcneill 	error = as3722_set_clear(asc, regdef->vsel_reg, set_v,
    578  1.7  jmcneill 	    regdef->vsel_mask, flags);
    579  1.7  jmcneill 	iic_release_bus(asc->sc_i2c, flags);
    580  1.7  jmcneill 
    581  1.7  jmcneill 	return error;
    582  1.7  jmcneill }
    583  1.7  jmcneill 
    584  1.7  jmcneill static int
    585  1.8  jmcneill as3722reg_get_voltage_ldo(device_t dev, u_int *puvol)
    586  1.7  jmcneill {
    587  1.7  jmcneill 	struct as3722reg_softc *sc = device_private(dev);
    588  1.7  jmcneill 	struct as3722_softc *asc = device_private(device_parent(dev));
    589  1.7  jmcneill 	const struct as3722regdef *regdef = sc->sc_regdef;
    590  1.7  jmcneill 	const int flags = (cold ? I2C_F_POLL : 0);
    591  1.7  jmcneill 	uint8_t v;
    592  1.7  jmcneill 	int error;
    593  1.7  jmcneill 
    594  1.7  jmcneill 	iic_acquire_bus(asc->sc_i2c, flags);
    595  1.7  jmcneill 	error = as3722_read(asc, regdef->vsel_reg, &v, flags);
    596  1.7  jmcneill 	iic_release_bus(asc->sc_i2c, flags);
    597  1.7  jmcneill 	if (error != 0)
    598  1.7  jmcneill 		return error;
    599  1.7  jmcneill 
    600  1.7  jmcneill 	v &= regdef->vsel_mask;
    601  1.7  jmcneill 
    602  1.7  jmcneill 	if (v == 0)
    603  1.7  jmcneill 		*puvol = 0;	/* LDO off */
    604  1.7  jmcneill 	else if (v >= 0x01 && v <= 0x24)
    605  1.7  jmcneill 		*puvol = 800000 + (v * 25000);
    606  1.7  jmcneill 	else if (v >= 0x40 && v <= 0x7f)
    607  1.7  jmcneill 		*puvol = 1725000 + ((v - 0x40) * 25000);
    608  1.7  jmcneill 	else
    609  1.7  jmcneill 		return EINVAL;
    610  1.7  jmcneill 
    611  1.7  jmcneill 	return 0;
    612  1.7  jmcneill }
    613  1.8  jmcneill 
    614  1.8  jmcneill static int
    615  1.8  jmcneill as3722reg_set_voltage_sd0(device_t dev, u_int min_uvol, u_int max_uvol)
    616  1.8  jmcneill {
    617  1.8  jmcneill 	struct as3722reg_softc *sc = device_private(dev);
    618  1.8  jmcneill 	struct as3722_softc *asc = device_private(device_parent(dev));
    619  1.8  jmcneill 	const struct as3722regdef *regdef = sc->sc_regdef;
    620  1.8  jmcneill 	const int flags = (cold ? I2C_F_POLL : 0);
    621  1.8  jmcneill 	uint8_t set_v = 0x00;
    622  1.8  jmcneill 	u_int uvol;
    623  1.8  jmcneill 	int error;
    624  1.8  jmcneill 
    625  1.8  jmcneill 	for (uint8_t v = 0x01; v <= 0x5a; v++) {
    626  1.8  jmcneill 		uvol = 600000 + (v * 10000);
    627  1.8  jmcneill 		if (uvol >= min_uvol && uvol <= max_uvol) {
    628  1.8  jmcneill 			set_v = v;
    629  1.8  jmcneill 			goto done;
    630  1.8  jmcneill 		}
    631  1.8  jmcneill 	}
    632  1.8  jmcneill 	if (set_v == 0)
    633  1.8  jmcneill 		return ERANGE;
    634  1.8  jmcneill 
    635  1.8  jmcneill done:
    636  1.8  jmcneill 	iic_acquire_bus(asc->sc_i2c, flags);
    637  1.8  jmcneill 	error = as3722_set_clear(asc, regdef->vsel_reg, set_v,
    638  1.8  jmcneill 	    regdef->vsel_mask, flags);
    639  1.8  jmcneill 	iic_release_bus(asc->sc_i2c, flags);
    640  1.8  jmcneill 
    641  1.8  jmcneill 	return error;
    642  1.8  jmcneill }
    643  1.8  jmcneill 
    644  1.8  jmcneill static int
    645  1.8  jmcneill as3722reg_get_voltage_sd0(device_t dev, u_int *puvol)
    646  1.8  jmcneill {
    647  1.8  jmcneill 	struct as3722reg_softc *sc = device_private(dev);
    648  1.8  jmcneill 	struct as3722_softc *asc = device_private(device_parent(dev));
    649  1.8  jmcneill 	const struct as3722regdef *regdef = sc->sc_regdef;
    650  1.8  jmcneill 	const int flags = (cold ? I2C_F_POLL : 0);
    651  1.8  jmcneill 	uint8_t v;
    652  1.8  jmcneill 	int error;
    653  1.8  jmcneill 
    654  1.8  jmcneill 	iic_acquire_bus(asc->sc_i2c, flags);
    655  1.8  jmcneill 	error = as3722_read(asc, regdef->vsel_reg, &v, flags);
    656  1.8  jmcneill 	iic_release_bus(asc->sc_i2c, flags);
    657  1.8  jmcneill 	if (error != 0)
    658  1.8  jmcneill 		return error;
    659  1.8  jmcneill 
    660  1.8  jmcneill 	v &= regdef->vsel_mask;
    661  1.8  jmcneill 
    662  1.8  jmcneill 	if (v == 0)
    663  1.8  jmcneill 		*puvol = 0;	/* DC/DC powered down */
    664  1.8  jmcneill 	else if (v >= 0x01 && v <= 0x5a)
    665  1.8  jmcneill 		*puvol = 600000 + (v * 10000);
    666  1.8  jmcneill 	else
    667  1.8  jmcneill 		return EINVAL;
    668  1.8  jmcneill 
    669  1.8  jmcneill 	return 0;
    670  1.8  jmcneill }
    671  1.8  jmcneill 
    672  1.8  jmcneill static int
    673  1.8  jmcneill as3722reg_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol)
    674  1.8  jmcneill {
    675  1.8  jmcneill 	struct as3722reg_softc *sc = device_private(dev);
    676  1.8  jmcneill 	const struct as3722regdef *regdef = sc->sc_regdef;
    677  1.8  jmcneill 
    678  1.8  jmcneill 	return regdef->set(dev, min_uvol, max_uvol);
    679  1.8  jmcneill }
    680  1.8  jmcneill 
    681  1.8  jmcneill static int
    682  1.8  jmcneill as3722reg_get_voltage(device_t dev, u_int *puvol)
    683  1.8  jmcneill {
    684  1.8  jmcneill 	struct as3722reg_softc *sc = device_private(dev);
    685  1.8  jmcneill 	const struct as3722regdef *regdef = sc->sc_regdef;
    686  1.8  jmcneill 
    687  1.8  jmcneill 	return regdef->get(dev, puvol);
    688  1.8  jmcneill }
    689  1.7  jmcneill #endif
    690  1.7  jmcneill 
    691  1.1  jmcneill int
    692  1.1  jmcneill as3722_poweroff(device_t dev)
    693  1.1  jmcneill {
    694  1.1  jmcneill 	struct as3722_softc * const sc = device_private(dev);
    695  1.1  jmcneill 	int error;
    696  1.1  jmcneill 
    697  1.1  jmcneill 	const int flags = I2C_F_POLL;
    698  1.1  jmcneill 
    699  1.1  jmcneill 	iic_acquire_bus(sc->sc_i2c, flags);
    700  1.1  jmcneill 	error = as3722_write(sc, AS3722_RESET_CTRL_REG,
    701  1.1  jmcneill 	    AS3722_RESET_CTRL_POWER_OFF, flags);
    702  1.1  jmcneill 	iic_release_bus(sc->sc_i2c, flags);
    703  1.1  jmcneill 
    704  1.1  jmcneill 	return error;
    705  1.1  jmcneill }
    706  1.3  jmcneill 
    707  1.3  jmcneill int
    708  1.3  jmcneill as3722_reboot(device_t dev)
    709  1.3  jmcneill {
    710  1.3  jmcneill 	struct as3722_softc * const sc = device_private(dev);
    711  1.3  jmcneill 	int error;
    712  1.3  jmcneill 
    713  1.3  jmcneill 	const int flags = I2C_F_POLL;
    714  1.3  jmcneill 
    715  1.3  jmcneill 	iic_acquire_bus(sc->sc_i2c, flags);
    716  1.3  jmcneill 	error = as3722_write(sc, AS3722_RESET_CTRL_REG,
    717  1.3  jmcneill 	    AS3722_RESET_CTRL_FORCE_RESET, flags);
    718  1.3  jmcneill 	iic_release_bus(sc->sc_i2c, flags);
    719  1.3  jmcneill 
    720  1.3  jmcneill 	return error;
    721  1.3  jmcneill }
    722