Home | History | Annotate | Line # | Download | only in i2c
as3722.c revision 1.14
      1 /* $NetBSD: as3722.c,v 1.14 2018/06/18 17:07:07 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.14 2018/06/18 17:07:07 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 char * as3722_compats[] = {
    226 	"ams,as3722",
    227 	NULL
    228 };
    229 
    230 static const struct device_compatible_entry as3722_compat_data[] = {
    231 	DEVICE_COMPAT_ENTRY(as3722_compats),
    232 	DEVICE_COMPAT_TERMINATOR
    233 };
    234 
    235 static int
    236 as3722_match(device_t parent, cfdata_t match, void *aux)
    237 {
    238 	struct i2c_attach_args *ia = aux;
    239 	uint8_t reg, id1;
    240 	int error, match_result;
    241 
    242 	if (iic_use_direct_match(ia, match, as3722_compat_data, &match_result))
    243 		return match_result;
    244 
    245 	if (ia->ia_addr != AS3722_I2C_ADDR)
    246 		return 0;
    247 
    248 	iic_acquire_bus(ia->ia_tag, I2C_F_POLL);
    249 	reg = AS3722_ASIC_ID1_REG;
    250 	error = iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr,
    251 	    &reg, 1, &id1, 1, I2C_F_POLL);
    252 	iic_release_bus(ia->ia_tag, I2C_F_POLL);
    253 
    254 	if (error == 0 && id1 == 0x0c)
    255 		return I2C_MATCH_ADDRESS_AND_PROBE;
    256 
    257 	return 0;
    258 }
    259 
    260 static void
    261 as3722_attach(device_t parent, device_t self, void *aux)
    262 {
    263 	struct as3722_softc * const sc = device_private(self);
    264 	struct i2c_attach_args *ia = aux;
    265 
    266 	sc->sc_dev = self;
    267 	sc->sc_i2c = ia->ia_tag;
    268 	sc->sc_addr = ia->ia_addr;
    269 	sc->sc_phandle = ia->ia_cookie;
    270 
    271 	aprint_naive("\n");
    272 	aprint_normal(": AMS AS3722\n");
    273 
    274 	as3722_wdt_attach(sc);
    275 	as3722_rtc_attach(sc);
    276 #ifdef FDT
    277 	as3722_regulator_attach(sc);
    278 
    279 	fdtbus_register_power_controller(self, sc->sc_phandle,
    280 	    &as3722power_funcs);
    281 #endif
    282 }
    283 
    284 static void
    285 as3722_wdt_attach(struct as3722_softc *sc)
    286 {
    287 	int error;
    288 
    289 	iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
    290 	error = as3722_write(sc, AS3722_GPIO0_CTRL_REG,
    291 	    __SHIFTIN(AS3722_GPIO0_CTRL_IOSF_GPIO,
    292 		      AS3722_GPIO0_CTRL_IOSF) |
    293 	    __SHIFTIN(AS3722_GPIO0_CTRL_MODE_PULLDOWN,
    294 		      AS3722_GPIO0_CTRL_MODE),
    295 	    I2C_F_POLL);
    296 	error += as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
    297 	    __SHIFTIN(1, AS3722_WATCHDOG_CTRL_MODE), 0, I2C_F_POLL);
    298 	iic_release_bus(sc->sc_i2c, I2C_F_POLL);
    299 
    300 	if (error) {
    301 		aprint_error_dev(sc->sc_dev, "couldn't setup watchdog\n");
    302 		return;
    303 	}
    304 
    305 	sc->sc_smw.smw_name = device_xname(sc->sc_dev);
    306 	sc->sc_smw.smw_cookie = sc;
    307 	sc->sc_smw.smw_setmode = as3722_wdt_setmode;
    308 	sc->sc_smw.smw_tickle = as3722_wdt_tickle;
    309 	sc->sc_smw.smw_period = AS3722_WATCHDOG_DEFAULT_PERIOD;
    310 
    311 	aprint_normal_dev(sc->sc_dev, "default watchdog period is %u seconds\n",
    312 	    sc->sc_smw.smw_period);
    313 
    314 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
    315 		aprint_error_dev(sc->sc_dev, "couldn't register with sysmon\n");
    316 }
    317 
    318 static void
    319 as3722_rtc_attach(struct as3722_softc *sc)
    320 {
    321 	int error;
    322 
    323 	iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
    324 	error = as3722_set_clear(sc, AS3722_RTC_CONTROL_REG,
    325 	    AS3722_RTC_CONTROL_RTC_ON, 0, I2C_F_POLL);
    326 	iic_release_bus(sc->sc_i2c, I2C_F_POLL);
    327 
    328 	if (error) {
    329 		aprint_error_dev(sc->sc_dev, "couldn't setup RTC\n");
    330 		return;
    331 	}
    332 
    333 	sc->sc_todr.todr_gettime_ymdhms = as3722_rtc_gettime;
    334 	sc->sc_todr.todr_settime_ymdhms = as3722_rtc_settime;
    335 	sc->sc_todr.cookie = sc;
    336 #ifdef FDT
    337 	fdtbus_todr_attach(sc->sc_dev, sc->sc_phandle, &sc->sc_todr);
    338 #else
    339 	todr_attach(&sc->sc_todr);
    340 #endif
    341 }
    342 
    343 static int
    344 as3722_read(struct as3722_softc *sc, uint8_t reg, uint8_t *val, int flags)
    345 {
    346 	return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    347 	    &reg, 1, val, 1, flags);
    348 }
    349 
    350 static int
    351 as3722_write(struct as3722_softc *sc, uint8_t reg, uint8_t val, int flags)
    352 {
    353 	uint8_t buf[2] = { reg, val };
    354 	return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    355 	    NULL, 0, buf, 2, flags);
    356 }
    357 
    358 static int
    359 as3722_set_clear(struct as3722_softc *sc, uint8_t reg, uint8_t set,
    360     uint8_t clr, int flags)
    361 {
    362 	uint8_t old, new;
    363 	int error;
    364 
    365 	error = as3722_read(sc, reg, &old, flags);
    366 	if (error) {
    367 		return error;
    368 	}
    369 	new = set | (old & ~clr);
    370 
    371 	return as3722_write(sc, reg, new, flags);
    372 }
    373 
    374 static int
    375 as3722_wdt_setmode(struct sysmon_wdog *smw)
    376 {
    377 	struct as3722_softc * const sc = smw->smw_cookie;
    378 	int error;
    379 
    380 	const int flags = (cold ? I2C_F_POLL : 0);
    381 
    382 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    383 		iic_acquire_bus(sc->sc_i2c, flags);
    384 		error = as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
    385 		    0, AS3722_WATCHDOG_CTRL_ON, flags);
    386 		iic_release_bus(sc->sc_i2c, flags);
    387 		return error;
    388 	}
    389 
    390 	if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
    391 		smw->smw_period = AS3722_WATCHDOG_DEFAULT_PERIOD;
    392 	}
    393 	if (smw->smw_period < 1 || smw->smw_period > 128) {
    394 		return EINVAL;
    395 	}
    396 	sc->sc_smw.smw_period = smw->smw_period;
    397 
    398 	iic_acquire_bus(sc->sc_i2c, flags);
    399 	error = as3722_set_clear(sc, AS3722_WATCHDOG_TIMER_REG,
    400 	    __SHIFTIN(sc->sc_smw.smw_period - 1, AS3722_WATCHDOG_TIMER_TIMER),
    401 	    AS3722_WATCHDOG_TIMER_TIMER, flags);
    402 	if (error == 0) {
    403 		error = as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
    404 		    AS3722_WATCHDOG_CTRL_ON, 0, flags);
    405 	}
    406 	iic_release_bus(sc->sc_i2c, flags);
    407 
    408 	return error;
    409 }
    410 
    411 static int
    412 as3722_wdt_tickle(struct sysmon_wdog *smw)
    413 {
    414 	struct as3722_softc * const sc = smw->smw_cookie;
    415 	int error;
    416 
    417 	const int flags = (cold ? I2C_F_POLL : 0);
    418 
    419 	iic_acquire_bus(sc->sc_i2c, flags);
    420 	error = as3722_set_clear(sc, AS3722_WATCHDOG_SIGNAL_REG,
    421 	    AS3722_WATCHDOG_SIGNAL_SW_SIG, 0, flags);
    422 	iic_release_bus(sc->sc_i2c, flags);
    423 
    424 	return error;
    425 }
    426 
    427 static int
    428 as3722_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    429 {
    430 	struct as3722_softc * const sc = tch->cookie;
    431 	uint8_t buf[6];
    432 	int error = 0;
    433 
    434 	const int flags = (cold ? I2C_F_POLL : 0);
    435 
    436 	iic_acquire_bus(sc->sc_i2c, flags);
    437 	error += as3722_read(sc, AS3722_RTC_SECOND_REG, &buf[0], flags);
    438 	error += as3722_read(sc, AS3722_RTC_MINUTE_REG, &buf[1], flags);
    439 	error += as3722_read(sc, AS3722_RTC_HOUR_REG, &buf[2], flags);
    440 	error += as3722_read(sc, AS3722_RTC_DAY_REG, &buf[3], flags);
    441 	error += as3722_read(sc, AS3722_RTC_MONTH_REG, &buf[4], flags);
    442 	error += as3722_read(sc, AS3722_RTC_YEAR_REG, &buf[5], flags);
    443 	iic_release_bus(sc->sc_i2c, flags);
    444 
    445 	if (error)
    446 		return error;
    447 
    448 	dt->dt_sec = bcdtobin(buf[0] & 0x7f);
    449 	dt->dt_min = bcdtobin(buf[1] & 0x7f);
    450 	dt->dt_hour = bcdtobin(buf[2] & 0x3f);
    451 	dt->dt_day = bcdtobin(buf[3] & 0x3f);
    452 	dt->dt_mon = bcdtobin(buf[4] & 0x1f) - 1;
    453 	dt->dt_year = AS3722_START_YEAR + bcdtobin(buf[5] & 0x7f);
    454 	dt->dt_wday = 0;
    455 
    456 	return 0;
    457 }
    458 
    459 static int
    460 as3722_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    461 {
    462 	struct as3722_softc * const sc = tch->cookie;
    463 	uint8_t buf[6];
    464 	int error = 0;
    465 
    466 	if (dt->dt_year < AS3722_START_YEAR)
    467 		return EINVAL;
    468 
    469 	buf[0] = bintobcd(dt->dt_sec) & 0x7f;
    470 	buf[1] = bintobcd(dt->dt_min) & 0x7f;
    471 	buf[2] = bintobcd(dt->dt_hour) & 0x3f;
    472 	buf[3] = bintobcd(dt->dt_day) & 0x3f;
    473 	buf[4] = bintobcd(dt->dt_mon + 1) & 0x1f;
    474 	buf[5] = bintobcd(dt->dt_year - AS3722_START_YEAR) & 0x7f;
    475 
    476 	const int flags = (cold ? I2C_F_POLL : 0);
    477 
    478 	iic_acquire_bus(sc->sc_i2c, flags);
    479 	error += as3722_write(sc, AS3722_RTC_SECOND_REG, buf[0], flags);
    480 	error += as3722_write(sc, AS3722_RTC_MINUTE_REG, buf[1], flags);
    481 	error += as3722_write(sc, AS3722_RTC_HOUR_REG, buf[2], flags);
    482 	error += as3722_write(sc, AS3722_RTC_DAY_REG, buf[3], flags);
    483 	error += as3722_write(sc, AS3722_RTC_MONTH_REG, buf[4], flags);
    484 	error += as3722_write(sc, AS3722_RTC_YEAR_REG, buf[5], flags);
    485 	iic_release_bus(sc->sc_i2c, flags);
    486 
    487 	return error;
    488 }
    489 
    490 #ifdef FDT
    491 static void
    492 as3722_regulator_attach(struct as3722_softc *sc)
    493 {
    494 	struct as3722reg_attach_args raa;
    495 	int phandle, child;
    496 	int error;
    497 	const int flags = (cold ? I2C_F_POLL : 0);
    498 	uint8_t tmp;
    499 
    500 	iic_acquire_bus(sc->sc_i2c, flags);
    501 	error = as3722_read(sc, AS3722_FUSE7_REG, &tmp, flags);
    502 	iic_release_bus(sc->sc_i2c, flags);
    503 	if (error != 0) {
    504 		aprint_error_dev(sc->sc_dev, "failed to read Fuse7: %d\n", error);
    505 		return;
    506 	}
    507 
    508 	if (tmp & AS3722_FUSE7_SD0_V_MINUS_200MV)
    509 		sc->sc_flags |= AS3722_FLAG_SD0_V_MINUS_200MV;
    510 
    511 	phandle = of_find_firstchild_byname(sc->sc_phandle, "regulators");
    512 	if (phandle <= 0)
    513 		return;
    514 
    515 	for (int i = 0; i < __arraycount(as3722regdefs); i++) {
    516 		const struct as3722regdef *regdef = &as3722regdefs[i];
    517 		child = of_find_firstchild_byname(phandle, regdef->name);
    518 		if (child <= 0)
    519 			continue;
    520 		raa.reg_def = regdef;
    521 		raa.reg_phandle = child;
    522 		config_found(sc->sc_dev, &raa, NULL);
    523 	}
    524 }
    525 
    526 static int
    527 as3722reg_match(device_t parent, cfdata_t match, void *aux)
    528 {
    529 	return 1;
    530 }
    531 
    532 static void
    533 as3722reg_attach(device_t parent, device_t self, void *aux)
    534 {
    535 	struct as3722reg_softc *sc = device_private(self);
    536 	struct as3722reg_attach_args *raa = aux;
    537 	char *name = NULL;
    538 	int len;
    539 
    540 	sc->sc_dev = self;
    541 	sc->sc_phandle = raa->reg_phandle;
    542 	sc->sc_regdef = raa->reg_def;
    543 
    544 	fdtbus_register_regulator_controller(self, sc->sc_phandle,
    545 	    &as3722reg_funcs);
    546 
    547 	len = OF_getproplen(sc->sc_phandle, "regulator-name");
    548 	if (len > 0) {
    549 		name = kmem_zalloc(len, KM_SLEEP);
    550 		OF_getprop(sc->sc_phandle, "regulator-name", name, len);
    551 	}
    552 
    553 	aprint_naive("\n");
    554 	if (name)
    555 		aprint_normal(": %s\n", name);
    556 	else
    557 		aprint_normal("\n");
    558 
    559 	if (name)
    560 		kmem_free(name, len);
    561 }
    562 
    563 static int
    564 as3722reg_acquire(device_t dev)
    565 {
    566 	return 0;
    567 }
    568 
    569 static void
    570 as3722reg_release(device_t dev)
    571 {
    572 }
    573 
    574 static int
    575 as3722reg_enable(device_t dev, bool enable)
    576 {
    577 	struct as3722reg_softc *sc = device_private(dev);
    578 	struct as3722_softc *asc = device_private(device_parent(dev));
    579 	const struct as3722regdef *regdef = sc->sc_regdef;
    580 	const int flags = (cold ? I2C_F_POLL : 0);
    581 	int error;
    582 
    583 	if (!regdef->enable_mask)
    584 		return enable ? 0 : EINVAL;
    585 
    586 	iic_acquire_bus(asc->sc_i2c, flags);
    587 	if (enable)
    588 		error = as3722_set_clear(asc, regdef->enable_reg,
    589 		    regdef->enable_mask, 0, flags);
    590 	else
    591 		error = as3722_set_clear(asc, regdef->enable_reg,
    592 		    0, regdef->enable_mask, flags);
    593 	iic_release_bus(asc->sc_i2c, flags);
    594 
    595 	return error;
    596 }
    597 
    598 static int
    599 as3722reg_set_voltage_ldo(device_t dev, u_int min_uvol, u_int max_uvol)
    600 {
    601 	struct as3722reg_softc *sc = device_private(dev);
    602 	struct as3722_softc *asc = device_private(device_parent(dev));
    603 	const struct as3722regdef *regdef = sc->sc_regdef;
    604 	const int flags = (cold ? I2C_F_POLL : 0);
    605 	uint8_t set_v = 0x00;
    606 	u_int uvol;
    607 	int error;
    608 
    609 	for (uint8_t v = 0x01; v <= 0x24; v++) {
    610 		uvol = 800000 + (v * 25000);
    611 		if (uvol >= min_uvol && uvol <= max_uvol) {
    612 			set_v = v;
    613 			goto done;
    614 		}
    615 	}
    616 	for (uint8_t v = 0x40; v <= 0x7f; v++) {
    617 		uvol = 1725000 + ((v - 0x40) * 25000);
    618 		if (uvol >= min_uvol && uvol <= max_uvol) {
    619 			set_v = v;
    620 			goto done;
    621 		}
    622 	}
    623 	if (set_v == 0)
    624 		return ERANGE;
    625 
    626 done:
    627 	iic_acquire_bus(asc->sc_i2c, flags);
    628 	error = as3722_set_clear(asc, regdef->vsel_reg, set_v,
    629 	    regdef->vsel_mask, flags);
    630 	iic_release_bus(asc->sc_i2c, flags);
    631 
    632 	return error;
    633 }
    634 
    635 static int
    636 as3722reg_get_voltage_ldo(device_t dev, u_int *puvol)
    637 {
    638 	struct as3722reg_softc *sc = device_private(dev);
    639 	struct as3722_softc *asc = device_private(device_parent(dev));
    640 	const struct as3722regdef *regdef = sc->sc_regdef;
    641 	const int flags = (cold ? I2C_F_POLL : 0);
    642 	uint8_t v;
    643 	int error;
    644 
    645 	iic_acquire_bus(asc->sc_i2c, flags);
    646 	error = as3722_read(asc, regdef->vsel_reg, &v, flags);
    647 	iic_release_bus(asc->sc_i2c, flags);
    648 	if (error != 0)
    649 		return error;
    650 
    651 	v &= regdef->vsel_mask;
    652 
    653 	if (v == 0)
    654 		*puvol = 0;	/* LDO off */
    655 	else if (v >= 0x01 && v <= 0x24)
    656 		*puvol = 800000 + (v * 25000);
    657 	else if (v >= 0x40 && v <= 0x7f)
    658 		*puvol = 1725000 + ((v - 0x40) * 25000);
    659 	else
    660 		return EINVAL;
    661 
    662 	return 0;
    663 }
    664 
    665 static int
    666 as3722reg_set_voltage_sd0(device_t dev, u_int min_uvol, u_int max_uvol)
    667 {
    668 	struct as3722reg_softc *sc = device_private(dev);
    669 	struct as3722_softc *asc = device_private(device_parent(dev));
    670 	const struct as3722regdef *regdef = sc->sc_regdef;
    671 	const int flags = (cold ? I2C_F_POLL : 0);
    672 	uint8_t set_v = 0x00;
    673 	u_int uvol;
    674 	int error;
    675 
    676 	if (asc->sc_flags & AS3722_FLAG_SD0_V_MINUS_200MV) {
    677 		for (uint8_t v = 0x01; v <= 0x6e; v++) {
    678 			uvol = 400000 + (v * 10000);
    679 			if (uvol >= min_uvol && uvol <= max_uvol) {
    680 				set_v = v;
    681 				goto done;
    682 			}
    683 		}
    684 	} else {
    685 		for (uint8_t v = 0x01; v <= 0x5a; v++) {
    686 			uvol = 600000 + (v * 10000);
    687 			if (uvol >= min_uvol && uvol <= max_uvol) {
    688 				set_v = v;
    689 				goto done;
    690 			}
    691 		}
    692 	}
    693 	if (set_v == 0)
    694 		return ERANGE;
    695 
    696 done:
    697 	iic_acquire_bus(asc->sc_i2c, flags);
    698 	error = as3722_set_clear(asc, regdef->vsel_reg, set_v,
    699 	    regdef->vsel_mask, flags);
    700 	iic_release_bus(asc->sc_i2c, flags);
    701 
    702 	return error;
    703 }
    704 
    705 static int
    706 as3722reg_get_voltage_sd0(device_t dev, u_int *puvol)
    707 {
    708 	struct as3722reg_softc *sc = device_private(dev);
    709 	struct as3722_softc *asc = device_private(device_parent(dev));
    710 	const struct as3722regdef *regdef = sc->sc_regdef;
    711 	const int flags = (cold ? I2C_F_POLL : 0);
    712 	uint8_t v;
    713 	int error;
    714 
    715 	iic_acquire_bus(asc->sc_i2c, flags);
    716 	error = as3722_read(asc, regdef->vsel_reg, &v, flags);
    717 	iic_release_bus(asc->sc_i2c, flags);
    718 	if (error != 0)
    719 		return error;
    720 
    721 	v &= regdef->vsel_mask;
    722 
    723 	if (v == 0) {
    724 		*puvol = 0;	/* DC/DC powered down */
    725 		return 0;
    726 	}
    727 	if (asc->sc_flags & AS3722_FLAG_SD0_V_MINUS_200MV) {
    728 		if (v >= 0x01 && v <= 0x6e) {
    729 			*puvol = 400000 + (v * 10000);
    730 			return 0;
    731 		}
    732 	} else {
    733 		if (v >= 0x01 && v <= 0x5a) {
    734 			*puvol = 600000 + (v * 10000);
    735 			return 0;
    736 		}
    737 	}
    738 
    739 	return EINVAL;
    740 }
    741 
    742 static int
    743 as3722reg_set_voltage_sd4(device_t dev, u_int min_uvol, u_int max_uvol)
    744 {
    745 	struct as3722reg_softc *sc = device_private(dev);
    746 	struct as3722_softc *asc = device_private(device_parent(dev));
    747 	const struct as3722regdef *regdef = sc->sc_regdef;
    748 	const int flags = (cold ? I2C_F_POLL : 0);
    749 	uint8_t set_v = 0x00;
    750 	u_int uvol;
    751 	int error;
    752 
    753 
    754 	for (uint8_t v = 0x01; v <= 0x40; v++) {
    755 		uvol = 600000 + (v * 12500);
    756 		if (uvol >= min_uvol && uvol <= max_uvol) {
    757 			set_v = v;
    758 			goto done;
    759 		}
    760 	}
    761 	for (uint8_t v = 0x41; v <= 0x70; v++) {
    762 		uvol = 1400000 + ((v - 0x40) * 25000);
    763 		if (uvol >= min_uvol && uvol <= max_uvol) {
    764 			set_v = v;
    765 			goto done;
    766 		}
    767 	}
    768 	for (uint8_t v = 0x71; v <= 0x7f; v++) {
    769 		uvol = 2600000 + ((v - 0x70) * 50000);
    770 		if (uvol >= min_uvol && uvol <= max_uvol) {
    771 			set_v = v;
    772 			goto done;
    773 		}
    774 	}
    775 	if (set_v == 0)
    776 		return ERANGE;
    777 
    778 done:
    779 	iic_acquire_bus(asc->sc_i2c, flags);
    780 	error = as3722_set_clear(asc, regdef->vsel_reg, set_v,
    781 	    regdef->vsel_mask, flags);
    782 	iic_release_bus(asc->sc_i2c, flags);
    783 
    784 	return error;
    785 }
    786 
    787 static int
    788 as3722reg_get_voltage_sd4(device_t dev, u_int *puvol)
    789 {
    790 	struct as3722reg_softc *sc = device_private(dev);
    791 	struct as3722_softc *asc = device_private(device_parent(dev));
    792 	const struct as3722regdef *regdef = sc->sc_regdef;
    793 	const int flags = (cold ? I2C_F_POLL : 0);
    794 	uint8_t v;
    795 	int error;
    796 
    797 	iic_acquire_bus(asc->sc_i2c, flags);
    798 	error = as3722_read(asc, regdef->vsel_reg, &v, flags);
    799 	iic_release_bus(asc->sc_i2c, flags);
    800 	if (error != 0)
    801 		return error;
    802 
    803 	v &= regdef->vsel_mask;
    804 
    805 	if (v == 0)
    806 		*puvol = 0;	/* DC/DC powered down */
    807 	else if (v >= 0x01 && v <= 0x40)
    808 		*puvol = 600000 + (v * 12500);
    809 	else if (v >= 0x41 && v <= 0x70)
    810 		*puvol = 1400000 + (v - 0x40) * 25000;
    811 	else if (v >= 0x71 && v <= 0x7f)
    812 		*puvol = 2600000 + (v - 0x70) * 50000;
    813 	else
    814 		return EINVAL;
    815 
    816 	return 0;
    817 }
    818 
    819 static int
    820 as3722reg_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol)
    821 {
    822 	struct as3722reg_softc *sc = device_private(dev);
    823 	const struct as3722regdef *regdef = sc->sc_regdef;
    824 
    825 	return regdef->set(dev, min_uvol, max_uvol);
    826 }
    827 
    828 static int
    829 as3722reg_get_voltage(device_t dev, u_int *puvol)
    830 {
    831 	struct as3722reg_softc *sc = device_private(dev);
    832 	const struct as3722regdef *regdef = sc->sc_regdef;
    833 
    834 	return regdef->get(dev, puvol);
    835 }
    836 
    837 static void
    838 as3722_power_reset(device_t dev)
    839 {
    840 	delay(1000000);
    841 	as3722_reboot(dev);
    842 }
    843 
    844 static void
    845 as3722_power_poweroff(device_t dev)
    846 {
    847 	delay(1000000);
    848 	as3722_poweroff(dev);
    849 }
    850 #endif
    851 
    852 int
    853 as3722_poweroff(device_t dev)
    854 {
    855 	struct as3722_softc * const sc = device_private(dev);
    856 	int error;
    857 
    858 	const int flags = I2C_F_POLL;
    859 
    860 	iic_acquire_bus(sc->sc_i2c, flags);
    861 	error = as3722_write(sc, AS3722_RESET_CTRL_REG,
    862 	    AS3722_RESET_CTRL_POWER_OFF, flags);
    863 	iic_release_bus(sc->sc_i2c, flags);
    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 	const int flags = I2C_F_POLL;
    875 
    876 	iic_acquire_bus(sc->sc_i2c, flags);
    877 	error = as3722_write(sc, AS3722_RESET_CTRL_REG,
    878 	    AS3722_RESET_CTRL_FORCE_RESET, flags);
    879 	iic_release_bus(sc->sc_i2c, flags);
    880 
    881 	return error;
    882 }
    883