Skip to content

Commit

Permalink
drivers: regulator: npm1300: Add boot state detection
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
aasinclair authored and fabiobaltieri committed Nov 7, 2023
1 parent ec03102 commit ec52722
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion drivers/regulator/regulator_npm1300.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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];
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit ec52722

Please sign in to comment.