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