Index: sys/arm/allwinner/axp81x.c =================================================================== --- sys/arm/allwinner/axp81x.c +++ sys/arm/allwinner/axp81x.c @@ -746,12 +746,6 @@ return (iicbus_transfer(dev, msg, 2)); } -static int -axp8xx_regnode_init(struct regnode *regnode) -{ - return (0); -} - static int axp8xx_regnode_enable(struct regnode *regnode, bool enable, int *udelay) { @@ -869,7 +863,6 @@ static regnode_method_t axp8xx_regnode_methods[] = { /* Regulator interface */ - REGNODEMETHOD(regnode_init, axp8xx_regnode_init), REGNODEMETHOD(regnode_enable, axp8xx_regnode_enable), REGNODEMETHOD(regnode_set_voltage, axp8xx_regnode_set_voltage), REGNODEMETHOD(regnode_get_voltage, axp8xx_regnode_get_voltage), Index: sys/dev/extres/regulator/regulator.h =================================================================== --- sys/dev/extres/regulator/regulator.h +++ sys/dev/extres/regulator/regulator.h @@ -116,6 +116,7 @@ int regnode_status(struct regnode *regnode, int *status); int regnode_get_voltage(struct regnode *regnode, int *uvolt); int regnode_set_voltage(struct regnode *regnode, int min_uvolt, int max_uvolt); +int regnode_set_constraint(struct regnode *regnode); #ifdef FDT phandle_t regnode_get_ofw_node(struct regnode *regnode); #endif Index: sys/dev/extres/regulator/regulator.c =================================================================== --- sys/dev/extres/regulator/regulator.c +++ sys/dev/extres/regulator/regulator.c @@ -65,18 +65,21 @@ typedef TAILQ_HEAD(regulator_list, regulator) regulator_list_t; /* Default regulator methods. */ +static int regnode_method_init(struct regnode *regnode); static int regnode_method_enable(struct regnode *regnode, bool enable, int *udelay); static int regnode_method_status(struct regnode *regnode, int *status); static int regnode_method_set_voltage(struct regnode *regnode, int min_uvolt, int max_uvolt, int *udelay); static int regnode_method_get_voltage(struct regnode *regnode, int *uvolt); +static void regulator_constraint(void *dummy); static void regulator_shutdown(void *dummy); /* * Regulator controller methods. */ static regnode_method_t regnode_methods[] = { + REGNODEMETHOD(regnode_init, regnode_method_init), REGNODEMETHOD(regnode_enable, regnode_method_enable), REGNODEMETHOD(regnode_status, regnode_method_status), REGNODEMETHOD(regnode_set_voltage, regnode_method_set_voltage), @@ -152,9 +155,27 @@ #define REGNODE_XLOCK(_sc) sx_xlock(&((_sc)->lock)) #define REGNODE_UNLOCK(_sc) sx_unlock(&((_sc)->lock)) +SYSINIT(regulator_constraint, SI_SUB_LAST, SI_ORDER_ANY, regulator_constraint, + NULL); SYSINIT(regulator_shutdown, SI_SUB_LAST, SI_ORDER_ANY, regulator_shutdown, NULL); +static void +regulator_constraint(void *dummy) +{ + struct regnode *entry; + int rv; + + REG_TOPO_SLOCK(); + TAILQ_FOREACH(entry, ®node_list, reglist_link) { + rv = regnode_set_constraint(entry); + if (rv != 0 && bootverbose) + printf("regulator: setting constraint on %s failed (%d)\n", + entry->name, rv); + } + REG_TOPO_UNLOCK(); +} + /* * Disable unused regulator * We run this function at SI_SUB_LAST which mean that every driver that needs @@ -212,6 +233,13 @@ * Default regulator methods for base class. * */ +static int +regnode_method_init(struct regnode *regnode) +{ + + return (0); +} + static int regnode_method_enable(struct regnode *regnode, bool enable, int *udelay) { @@ -771,6 +799,66 @@ return (rv); } +int +regnode_set_constraint(struct regnode *regnode) +{ + int status, rv, uvolt; + + if (regnode->std_param.boot_on != true && + regnode->std_param.always_on != true) + return (0); + + rv = regnode_status(regnode, &status); + if (rv != 0) { + if (bootverbose) + printf("Cannot get regulator status for %s\n", + regnode_get_name(regnode)); + return (rv); + } + + if (status == REGULATOR_STATUS_ENABLED) + return (0); + + rv = regnode_get_voltage(regnode, &uvolt); + if (rv != 0) { + if (bootverbose) + printf("Cannot get regulator voltage for %s\n", + regnode_get_name(regnode)); + return (rv); + } + + if (uvolt < regnode->std_param.min_uvolt || + uvolt > regnode->std_param.max_uvolt) { + if (bootverbose) + printf("Regulator %s current voltage %d is not in the" + " acceptable range : %d<->%d\n", + regnode_get_name(regnode), + uvolt, regnode->std_param.min_uvolt, + regnode->std_param.max_uvolt); + /* + * Return 0 here as we still want the regulator + * to be added to the list so we can get its voltage + * with sysctl in case we are called from the init method + */ + return (0); + } + + rv = regnode_enable(regnode); + if (rv != 0) { + if (bootverbose) + printf("Cannot enable regulator %s\n", + regnode_get_name(regnode)); + /* + * Return 0 here as we still want the regulator + * to be added to the list so we can get its voltage + * with sysctl in case we are called from the init method + */ + return (0); + } + + return (0); +} + #ifdef FDT phandle_t regnode_get_ofw_node(struct regnode *regnode)