Home | History | Annotate | Line # | Download | only in i2c
as3722.c revision 1.7
      1 /* $NetBSD: as3722.c,v 1.7 2017/04/22 21:48:56 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "opt_fdt.h"
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: as3722.c,v 1.7 2017/04/22 21:48:56 jmcneill Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/kernel.h>
     37 #include <sys/device.h>
     38 #include <sys/conf.h>
     39 #include <sys/bus.h>
     40 #include <sys/kmem.h>
     41 #include <sys/wdog.h>
     42 
     43 #include <dev/clock_subr.h>
     44 
     45 #include <dev/sysmon/sysmonvar.h>
     46 
     47 #include <dev/i2c/i2cvar.h>
     48 #include <dev/i2c/as3722.h>
     49 
     50 #ifdef FDT
     51 #include <dev/fdt/fdtvar.h>
     52 #endif
     53 
     54 #define AS3722_START_YEAR		2000
     55 
     56 #define AS3722_GPIO0_CTRL_REG		0x08
     57 #define AS3722_GPIO0_CTRL_INVERT	__BIT(7)
     58 #define AS3722_GPIO0_CTRL_IOSF		__BITS(6,3)
     59 #define AS3722_GPIO0_CTRL_IOSF_GPIO	0
     60 #define AS3722_GPIO0_CTRL_IOSF_WATCHDOG	9
     61 #define AS3722_GPIO0_CTRL_MODE		__BITS(2,0)
     62 #define AS3722_GPIO0_CTRL_MODE_PULLDOWN	5
     63 
     64 #define AS3722_LDO6_VOLTAGE_REG		0x16
     65 
     66 #define AS3722_RESET_CTRL_REG		0x36
     67 #define AS3722_RESET_CTRL_POWER_OFF	__BIT(1)
     68 #define AS3722_RESET_CTRL_FORCE_RESET	__BIT(0)
     69 
     70 #define AS3722_WATCHDOG_CTRL_REG	0x38
     71 #define AS3722_WATCHDOG_CTRL_MODE	__BITS(2,1)
     72 #define AS3722_WATCHDOG_CTRL_ON		__BIT(0)
     73 
     74 #define AS3722_WATCHDOG_TIMER_REG	0x46
     75 #define AS3722_WATCHDOG_TIMER_TIMER	__BITS(6,0)
     76 
     77 #define AS3722_WATCHDOG_SIGNAL_REG	0x48
     78 #define AS3722_WATCHDOG_SIGNAL_PWM_DIV	__BITS(7,6)
     79 #define AS3722_WATCHDOG_SIGNAL_SW_SIG	__BIT(0)
     80 
     81 #define AS3722_LDOCONTROL0_REG		0x4e
     82 
     83 #define AS3722_RTC_CONTROL_REG		0x60
     84 #define AS3722_RTC_CONTROL_RTC_ON	__BIT(2)
     85 
     86 #define AS3722_RTC_SECOND_REG		0x61
     87 #define AS3722_RTC_MINUTE_REG		0x62
     88 #define AS3722_RTC_HOUR_REG		0x63
     89 #define AS3722_RTC_DAY_REG		0x64
     90 #define AS3722_RTC_MONTH_REG		0x65
     91 #define AS3722_RTC_YEAR_REG		0x66
     92 #define AS3722_RTC_ACCESS_REG		0x6f
     93 
     94 #define AS3722_ASIC_ID1_REG		0x90
     95 #define AS3722_ASIC_ID2_REG		0x91
     96 
     97 struct as3722_softc {
     98 	device_t	sc_dev;
     99 	i2c_tag_t	sc_i2c;
    100 	i2c_addr_t	sc_addr;
    101 	int		sc_phandle;
    102 
    103 	struct sysmon_wdog sc_smw;
    104 	struct todr_chip_handle sc_todr;
    105 };
    106 
    107 #ifdef FDT
    108 static const struct as3722regdef {
    109 	const char	*name;
    110 	u_int		vsel_reg;
    111 	u_int		vsel_mask;
    112 	u_int		enable_reg;
    113 	u_int		enable_mask;
    114 	u_int		n_voltages;
    115 } as3722regdefs[] = {
    116 	{ .name = "ldo6",
    117 	  .vsel_reg = AS3722_LDO6_VOLTAGE_REG,
    118 	  .vsel_mask = 0x7f,
    119 	  .enable_reg = AS3722_LDOCONTROL0_REG,
    120 	  .enable_mask = 0x40,
    121 	  .n_voltages = 0x80 },
    122 };
    123 
    124 struct as3722reg_softc {
    125 	device_t	sc_dev;
    126 	int		sc_phandle;
    127 	const struct as3722regdef *sc_regdef;
    128 };
    129 
    130 struct as3722reg_attach_args {
    131 	const struct as3722regdef *reg_def;
    132 	int		reg_phandle;
    133 };
    134 #endif
    135 
    136 #define AS3722_WATCHDOG_DEFAULT_PERIOD	10
    137 
    138 static int	as3722_match(device_t, cfdata_t, void *);
    139 static void	as3722_attach(device_t, device_t, void *);
    140 
    141 static void	as3722_wdt_attach(struct as3722_softc *);
    142 static int	as3722_wdt_setmode(struct sysmon_wdog *);
    143 static int	as3722_wdt_tickle(struct sysmon_wdog *);
    144 
    145 static void	as3722_rtc_attach(struct as3722_softc *);
    146 static int	as3722_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
    147 static int	as3722_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
    148 
    149 #ifdef FDT
    150 static void	as3722_regulator_attach(struct as3722_softc *);
    151 static int	as3722reg_match(device_t, cfdata_t, void *);
    152 static void	as3722reg_attach(device_t, device_t, void *);
    153 
    154 static int	as3722reg_acquire(device_t);
    155 static void	as3722reg_release(device_t);
    156 static int	as3722reg_enable(device_t, bool);
    157 static int	as3722reg_set_voltage(device_t, u_int, u_int);
    158 static int	as3722reg_get_voltage(device_t, u_int *);
    159 
    160 static struct fdtbus_regulator_controller_func as3722reg_funcs = {
    161 	.acquire = as3722reg_acquire,
    162 	.release = as3722reg_release,
    163 	.enable = as3722reg_enable,
    164 	.set_voltage = as3722reg_set_voltage,
    165 	.get_voltage = as3722reg_get_voltage,
    166 };
    167 #endif
    168 
    169 static int	as3722_read(struct as3722_softc *, uint8_t, uint8_t *, int);
    170 static int	as3722_write(struct as3722_softc *, uint8_t, uint8_t, int);
    171 static int	as3722_set_clear(struct as3722_softc *, uint8_t, uint8_t,
    172 				 uint8_t, int);
    173 
    174 CFATTACH_DECL_NEW(as3722pmic, sizeof(struct as3722_softc),
    175     as3722_match, as3722_attach, NULL, NULL);
    176 
    177 #ifdef FDT
    178 CFATTACH_DECL_NEW(as3722reg, sizeof(struct as3722reg_softc),
    179     as3722reg_match, as3722reg_attach, NULL, NULL);
    180 #endif
    181 
    182 static const char * as3722_compats[] = {
    183 	"ams,as3722",
    184 	NULL
    185 };
    186 
    187 static int
    188 as3722_match(device_t parent, cfdata_t match, void *aux)
    189 {
    190 	struct i2c_attach_args *ia = aux;
    191 	uint8_t reg, id1;
    192 	int error;
    193 
    194 	if (ia->ia_name == NULL) {
    195 		iic_acquire_bus(ia->ia_tag, I2C_F_POLL);
    196 		reg = AS3722_ASIC_ID1_REG;
    197 		error = iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr,
    198 		    &reg, 1, &id1, 1, I2C_F_POLL);
    199 		iic_release_bus(ia->ia_tag, I2C_F_POLL);
    200 
    201 		if (error == 0 && id1 == 0x0c)
    202 			return 1;
    203 
    204 		return 0;
    205 	} else {
    206 		return iic_compat_match(ia, as3722_compats);
    207 	}
    208 }
    209 
    210 static void
    211 as3722_attach(device_t parent, device_t self, void *aux)
    212 {
    213 	struct as3722_softc * const sc = device_private(self);
    214 	struct i2c_attach_args *ia = aux;
    215 
    216 	sc->sc_dev = self;
    217 	sc->sc_i2c = ia->ia_tag;
    218 	sc->sc_addr = ia->ia_addr;
    219 	sc->sc_phandle = ia->ia_cookie;
    220 
    221 	aprint_naive("\n");
    222 	aprint_normal(": AMS AS3722\n");
    223 
    224 	as3722_wdt_attach(sc);
    225 	as3722_rtc_attach(sc);
    226 #ifdef FDT
    227 	as3722_regulator_attach(sc);
    228 #endif
    229 }
    230 
    231 static void
    232 as3722_wdt_attach(struct as3722_softc *sc)
    233 {
    234 	int error;
    235 
    236 	iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
    237 	error = as3722_write(sc, AS3722_GPIO0_CTRL_REG,
    238 	    __SHIFTIN(AS3722_GPIO0_CTRL_IOSF_GPIO,
    239 		      AS3722_GPIO0_CTRL_IOSF) |
    240 	    __SHIFTIN(AS3722_GPIO0_CTRL_MODE_PULLDOWN,
    241 		      AS3722_GPIO0_CTRL_MODE),
    242 	    I2C_F_POLL);
    243 	error += as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
    244 	    __SHIFTIN(1, AS3722_WATCHDOG_CTRL_MODE), 0, I2C_F_POLL);
    245 	iic_release_bus(sc->sc_i2c, I2C_F_POLL);
    246 
    247 	if (error) {
    248 		aprint_error_dev(sc->sc_dev, "couldn't setup watchdog\n");
    249 		return;
    250 	}
    251 
    252 	sc->sc_smw.smw_name = device_xname(sc->sc_dev);
    253 	sc->sc_smw.smw_cookie = sc;
    254 	sc->sc_smw.smw_setmode = as3722_wdt_setmode;
    255 	sc->sc_smw.smw_tickle = as3722_wdt_tickle;
    256 	sc->sc_smw.smw_period = AS3722_WATCHDOG_DEFAULT_PERIOD;
    257 
    258 	aprint_normal_dev(sc->sc_dev, "default watchdog period is %u seconds\n",
    259 	    sc->sc_smw.smw_period);
    260 
    261 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
    262 		aprint_error_dev(sc->sc_dev, "couldn't register with sysmon\n");
    263 }
    264 
    265 static void
    266 as3722_rtc_attach(struct as3722_softc *sc)
    267 {
    268 	int error;
    269 
    270 	iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
    271 	error = as3722_set_clear(sc, AS3722_RTC_CONTROL_REG,
    272 	    AS3722_RTC_CONTROL_RTC_ON, 0, I2C_F_POLL);
    273 	iic_release_bus(sc->sc_i2c, I2C_F_POLL);
    274 
    275 	if (error) {
    276 		aprint_error_dev(sc->sc_dev, "couldn't setup RTC\n");
    277 		return;
    278 	}
    279 
    280 	sc->sc_todr.todr_gettime_ymdhms = as3722_rtc_gettime;
    281 	sc->sc_todr.todr_settime_ymdhms = as3722_rtc_settime;
    282 	sc->sc_todr.cookie = sc;
    283 #ifdef FDT
    284 	fdtbus_todr_attach(sc->sc_dev, sc->sc_phandle, &sc->sc_todr);
    285 #else
    286 	todr_attach(&sc->sc_todr);
    287 #endif
    288 }
    289 
    290 static int
    291 as3722_read(struct as3722_softc *sc, uint8_t reg, uint8_t *val, int flags)
    292 {
    293 	return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    294 	    &reg, 1, val, 1, flags);
    295 }
    296 
    297 static int
    298 as3722_write(struct as3722_softc *sc, uint8_t reg, uint8_t val, int flags)
    299 {
    300 	uint8_t buf[2] = { reg, val };
    301 	return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    302 	    NULL, 0, buf, 2, flags);
    303 }
    304 
    305 static int
    306 as3722_set_clear(struct as3722_softc *sc, uint8_t reg, uint8_t set,
    307     uint8_t clr, int flags)
    308 {
    309 	uint8_t old, new;
    310 	int error;
    311 
    312 	error = as3722_read(sc, reg, &old, flags);
    313 	if (error) {
    314 		return error;
    315 	}
    316 	new = set | (old & ~clr);
    317 
    318 	return as3722_write(sc, reg, new, flags);
    319 }
    320 
    321 static int
    322 as3722_wdt_setmode(struct sysmon_wdog *smw)
    323 {
    324 	struct as3722_softc * const sc = smw->smw_cookie;
    325 	int error;
    326 
    327 	const int flags = (cold ? I2C_F_POLL : 0);
    328 
    329 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    330 		iic_acquire_bus(sc->sc_i2c, flags);
    331 		error = as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
    332 		    0, AS3722_WATCHDOG_CTRL_ON, flags);
    333 		iic_release_bus(sc->sc_i2c, flags);
    334 		return error;
    335 	}
    336 
    337 	if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
    338 		smw->smw_period = AS3722_WATCHDOG_DEFAULT_PERIOD;
    339 	}
    340 	if (smw->smw_period < 1 || smw->smw_period > 128) {
    341 		return EINVAL;
    342 	}
    343 	sc->sc_smw.smw_period = smw->smw_period;
    344 
    345 	iic_acquire_bus(sc->sc_i2c, flags);
    346 	error = as3722_set_clear(sc, AS3722_WATCHDOG_TIMER_REG,
    347 	    __SHIFTIN(sc->sc_smw.smw_period - 1, AS3722_WATCHDOG_TIMER_TIMER),
    348 	    AS3722_WATCHDOG_TIMER_TIMER, flags);
    349 	if (error == 0) {
    350 		error = as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
    351 		    AS3722_WATCHDOG_CTRL_ON, 0, flags);
    352 	}
    353 	iic_release_bus(sc->sc_i2c, flags);
    354 
    355 	return error;
    356 }
    357 
    358 static int
    359 as3722_wdt_tickle(struct sysmon_wdog *smw)
    360 {
    361 	struct as3722_softc * const sc = smw->smw_cookie;
    362 	int error;
    363 
    364 	const int flags = (cold ? I2C_F_POLL : 0);
    365 
    366 	iic_acquire_bus(sc->sc_i2c, flags);
    367 	error = as3722_set_clear(sc, AS3722_WATCHDOG_SIGNAL_REG,
    368 	    AS3722_WATCHDOG_SIGNAL_SW_SIG, 0, flags);
    369 	iic_release_bus(sc->sc_i2c, flags);
    370 
    371 	return error;
    372 }
    373 
    374 static int
    375 as3722_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    376 {
    377 	struct as3722_softc * const sc = tch->cookie;
    378 	uint8_t buf[6];
    379 	int error = 0;
    380 
    381 	const int flags = (cold ? I2C_F_POLL : 0);
    382 
    383 	iic_acquire_bus(sc->sc_i2c, flags);
    384 	error += as3722_read(sc, AS3722_RTC_SECOND_REG, &buf[0], flags);
    385 	error += as3722_read(sc, AS3722_RTC_MINUTE_REG, &buf[1], flags);
    386 	error += as3722_read(sc, AS3722_RTC_HOUR_REG, &buf[2], flags);
    387 	error += as3722_read(sc, AS3722_RTC_DAY_REG, &buf[3], flags);
    388 	error += as3722_read(sc, AS3722_RTC_MONTH_REG, &buf[4], flags);
    389 	error += as3722_read(sc, AS3722_RTC_YEAR_REG, &buf[5], flags);
    390 	iic_release_bus(sc->sc_i2c, flags);
    391 
    392 	if (error)
    393 		return error;
    394 
    395 	dt->dt_sec = bcdtobin(buf[0] & 0x7f);
    396 	dt->dt_min = bcdtobin(buf[1] & 0x7f);
    397 	dt->dt_hour = bcdtobin(buf[2] & 0x3f);
    398 	dt->dt_day = bcdtobin(buf[3] & 0x3f);
    399 	dt->dt_mon = bcdtobin(buf[4] & 0x1f) - 1;
    400 	dt->dt_year = AS3722_START_YEAR + bcdtobin(buf[5] & 0x7f);
    401 	dt->dt_wday = 0;
    402 
    403 	return 0;
    404 }
    405 
    406 static int
    407 as3722_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    408 {
    409 	struct as3722_softc * const sc = tch->cookie;
    410 	uint8_t buf[6];
    411 	int error = 0;
    412 
    413 	if (dt->dt_year < AS3722_START_YEAR)
    414 		return EINVAL;
    415 
    416 	buf[0] = bintobcd(dt->dt_sec) & 0x7f;
    417 	buf[1] = bintobcd(dt->dt_min) & 0x7f;
    418 	buf[2] = bintobcd(dt->dt_hour) & 0x3f;
    419 	buf[3] = bintobcd(dt->dt_day) & 0x3f;
    420 	buf[4] = bintobcd(dt->dt_mon + 1) & 0x1f;
    421 	buf[5] = bintobcd(dt->dt_year - AS3722_START_YEAR) & 0x7f;
    422 
    423 	const int flags = (cold ? I2C_F_POLL : 0);
    424 
    425 	iic_acquire_bus(sc->sc_i2c, flags);
    426 	error += as3722_write(sc, AS3722_RTC_SECOND_REG, buf[0], flags);
    427 	error += as3722_write(sc, AS3722_RTC_MINUTE_REG, buf[1], flags);
    428 	error += as3722_write(sc, AS3722_RTC_HOUR_REG, buf[2], flags);
    429 	error += as3722_write(sc, AS3722_RTC_DAY_REG, buf[3], flags);
    430 	error += as3722_write(sc, AS3722_RTC_MONTH_REG, buf[4], flags);
    431 	error += as3722_write(sc, AS3722_RTC_YEAR_REG, buf[5], flags);
    432 	iic_release_bus(sc->sc_i2c, flags);
    433 
    434 	return error;
    435 }
    436 
    437 #ifdef FDT
    438 static void
    439 as3722_regulator_attach(struct as3722_softc *sc)
    440 {
    441 	struct as3722reg_attach_args raa;
    442 	int phandle, child;
    443 
    444 	phandle = of_find_firstchild_byname(sc->sc_phandle, "regulators");
    445 	if (phandle <= 0)
    446 		return;
    447 
    448 	for (int i = 0; i < __arraycount(as3722regdefs); i++) {
    449 		const struct as3722regdef *regdef = &as3722regdefs[i];
    450 		child = of_find_firstchild_byname(phandle, regdef->name);
    451 		if (child <= 0)
    452 			continue;
    453 		raa.reg_def = regdef;
    454 		raa.reg_phandle = child;
    455 		config_found(sc->sc_dev, &raa, NULL);
    456 	}
    457 }
    458 
    459 static int
    460 as3722reg_match(device_t parent, cfdata_t match, void *aux)
    461 {
    462 	return 1;
    463 }
    464 
    465 static void
    466 as3722reg_attach(device_t parent, device_t self, void *aux)
    467 {
    468 	struct as3722reg_softc *sc = device_private(self);
    469 	struct as3722reg_attach_args *raa = aux;
    470 	char *name = NULL;
    471 	int len;
    472 
    473 	sc->sc_dev = self;
    474 	sc->sc_phandle = raa->reg_phandle;
    475 	sc->sc_regdef = raa->reg_def;
    476 
    477 	fdtbus_register_regulator_controller(self, sc->sc_phandle,
    478 	    &as3722reg_funcs);
    479 
    480 	len = OF_getproplen(sc->sc_phandle, "regulator-name");
    481 	if (len > 0) {
    482 		name = kmem_zalloc(len, KM_SLEEP);
    483 		OF_getprop(sc->sc_phandle, "regulator-name", name, len);
    484 	}
    485 
    486 	aprint_naive("\n");
    487 	if (name)
    488 		aprint_normal(": %s\n", name);
    489 	else
    490 		aprint_normal("\n");
    491 
    492 	if (name)
    493 		kmem_free(name, len);
    494 }
    495 
    496 static int
    497 as3722reg_acquire(device_t dev)
    498 {
    499 	return 0;
    500 }
    501 
    502 static void
    503 as3722reg_release(device_t dev)
    504 {
    505 }
    506 
    507 static int
    508 as3722reg_enable(device_t dev, bool enable)
    509 {
    510 	struct as3722reg_softc *sc = device_private(dev);
    511 	struct as3722_softc *asc = device_private(device_parent(dev));
    512 	const struct as3722regdef *regdef = sc->sc_regdef;
    513 	const int flags = (cold ? I2C_F_POLL : 0);
    514 	int error;
    515 
    516 	iic_acquire_bus(asc->sc_i2c, flags);
    517 	if (enable)
    518 		error = as3722_set_clear(asc, regdef->enable_reg,
    519 		    regdef->enable_mask, 0, flags);
    520 	else
    521 		error = as3722_set_clear(asc, regdef->enable_reg,
    522 		    0, regdef->enable_mask, flags);
    523 	iic_release_bus(asc->sc_i2c, flags);
    524 
    525 	return error;
    526 }
    527 
    528 static int
    529 as3722reg_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol)
    530 {
    531 	struct as3722reg_softc *sc = device_private(dev);
    532 	struct as3722_softc *asc = device_private(device_parent(dev));
    533 	const struct as3722regdef *regdef = sc->sc_regdef;
    534 	const int flags = (cold ? I2C_F_POLL : 0);
    535 	uint8_t set_v = 0x00;
    536 	u_int uvol;
    537 	int error;
    538 
    539 	for (uint8_t v = 0x01; v <= 0x24; v++) {
    540 		uvol = 800000 + (v * 25000);
    541 		if (uvol >= min_uvol && uvol <= max_uvol) {
    542 			set_v = v;
    543 			goto done;
    544 		}
    545 	}
    546 	for (uint8_t v = 0x40; v <= 0x7f; v++) {
    547 		uvol = 1725000 + ((v - 0x40) * 25000);
    548 		if (uvol >= min_uvol && uvol <= max_uvol) {
    549 			set_v = v;
    550 			goto done;
    551 		}
    552 	}
    553 	if (set_v == 0)
    554 		return ERANGE;
    555 
    556 done:
    557 	iic_acquire_bus(asc->sc_i2c, flags);
    558 	error = as3722_set_clear(asc, regdef->vsel_reg, set_v,
    559 	    regdef->vsel_mask, flags);
    560 	iic_release_bus(asc->sc_i2c, flags);
    561 
    562 	return error;
    563 }
    564 
    565 static int
    566 as3722reg_get_voltage(device_t dev, u_int *puvol)
    567 {
    568 	struct as3722reg_softc *sc = device_private(dev);
    569 	struct as3722_softc *asc = device_private(device_parent(dev));
    570 	const struct as3722regdef *regdef = sc->sc_regdef;
    571 	const int flags = (cold ? I2C_F_POLL : 0);
    572 	uint8_t v;
    573 	int error;
    574 
    575 	iic_acquire_bus(asc->sc_i2c, flags);
    576 	error = as3722_read(asc, regdef->vsel_reg, &v, flags);
    577 	iic_release_bus(asc->sc_i2c, flags);
    578 	if (error != 0)
    579 		return error;
    580 
    581 	v &= regdef->vsel_mask;
    582 
    583 	if (v == 0)
    584 		*puvol = 0;	/* LDO off */
    585 	else if (v >= 0x01 && v <= 0x24)
    586 		*puvol = 800000 + (v * 25000);
    587 	else if (v >= 0x40 && v <= 0x7f)
    588 		*puvol = 1725000 + ((v - 0x40) * 25000);
    589 	else
    590 		return EINVAL;
    591 
    592 	return 0;
    593 }
    594 #endif
    595 
    596 int
    597 as3722_poweroff(device_t dev)
    598 {
    599 	struct as3722_softc * const sc = device_private(dev);
    600 	int error;
    601 
    602 	const int flags = I2C_F_POLL;
    603 
    604 	iic_acquire_bus(sc->sc_i2c, flags);
    605 	error = as3722_write(sc, AS3722_RESET_CTRL_REG,
    606 	    AS3722_RESET_CTRL_POWER_OFF, flags);
    607 	iic_release_bus(sc->sc_i2c, flags);
    608 
    609 	return error;
    610 }
    611 
    612 int
    613 as3722_reboot(device_t dev)
    614 {
    615 	struct as3722_softc * const sc = device_private(dev);
    616 	int error;
    617 
    618 	const int flags = I2C_F_POLL;
    619 
    620 	iic_acquire_bus(sc->sc_i2c, flags);
    621 	error = as3722_write(sc, AS3722_RESET_CTRL_REG,
    622 	    AS3722_RESET_CTRL_FORCE_RESET, flags);
    623 	iic_release_bus(sc->sc_i2c, flags);
    624 
    625 	return error;
    626 }
    627