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