From ec527228613e2768caaddedaa5cdea04951d3a06 Mon Sep 17 00:00:00 2001 From: Andy Sinclair Date: Wed, 1 Nov 2023 16:29:12 +0000 Subject: [PATCH] drivers: regulator: npm1300: Add boot state detection Added boot state detection, so that the reference count is set correctly and enable/disable work as expected. With this fix it is no longer necessary to set regulator-boot-on when the regulator is enabled in hardware. Signed-off-by: Andy Sinclair --- drivers/regulator/regulator_npm1300.c | 50 ++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/drivers/regulator/regulator_npm1300.c b/drivers/regulator/regulator_npm1300.c index 84ea5b68263c35..b3a9d1aced9afb 100644 --- a/drivers/regulator/regulator_npm1300.c +++ b/drivers/regulator/regulator_npm1300.c @@ -48,6 +48,7 @@ enum npm1300_gpio_type { #define BUCK_OFFSET_SW_CTRL 0x0FU #define BUCK_OFFSET_VOUT_STAT 0x10U #define BUCK_OFFSET_CTRL0 0x15U +#define BUCK_OFFSET_STATUS 0x34U /* nPM1300 ldsw register offsets */ #define LDSW_OFFSET_EN_SET 0x00U @@ -61,6 +62,12 @@ enum npm1300_gpio_type { /* nPM1300 ship register offsets */ #define SHIP_OFFSET_SHIP 0x02U +#define BUCK1_ON_MASK 0x04U +#define BUCK2_ON_MASK 0x40U + +#define LDSW1_ON_MASK 0x03U +#define LDSW2_ON_MASK 0x0CU + struct regulator_npm1300_pconfig { const struct device *mfd; struct gpio_dt_spec dvs_state_pins[5]; @@ -512,16 +519,57 @@ int regulator_npm1300_common_init(const struct device *dev) return 0; } +static int get_enabled_reg(const struct device *dev, uint8_t base, uint8_t offset, uint8_t mask, + bool *enabled) +{ + const struct regulator_npm1300_config *config = dev->config; + uint8_t data; + + int ret = mfd_npm1300_reg_read(config->mfd, base, offset, &data); + + if (ret != 0) { + return ret; + } + + *enabled = (data & mask) != 0U; + + return 0; +} + +static int get_enabled(const struct device *dev, bool *enabled) +{ + const struct regulator_npm1300_config *config = dev->config; + + switch (config->source) { + case NPM1300_SOURCE_BUCK1: + return get_enabled_reg(dev, BUCK_BASE, BUCK_OFFSET_STATUS, BUCK1_ON_MASK, enabled); + case NPM1300_SOURCE_BUCK2: + return get_enabled_reg(dev, BUCK_BASE, BUCK_OFFSET_STATUS, BUCK2_ON_MASK, enabled); + case NPM1300_SOURCE_LDO1: + return get_enabled_reg(dev, LDSW_BASE, LDSW_OFFSET_STATUS, LDSW1_ON_MASK, enabled); + case NPM1300_SOURCE_LDO2: + return get_enabled_reg(dev, LDSW_BASE, LDSW_OFFSET_STATUS, LDSW2_ON_MASK, enabled); + default: + return -ENODEV; + } +} + int regulator_npm1300_init(const struct device *dev) { const struct regulator_npm1300_config *config = dev->config; + bool enabled; int ret = 0; if (!device_is_ready(config->mfd)) { return -ENODEV; } - ret = regulator_common_init(dev, false); + ret = get_enabled(dev, &enabled); + if (ret < 0) { + return ret; + } + + ret = regulator_common_init(dev, enabled); if (ret < 0) { return ret; }