Skip to content

Commit

Permalink
drivers: regulator: npm1300: Set voltage checks existing setting
Browse files Browse the repository at this point in the history
Regulator voltage setting is not applied if the device output
is already configured for the requested voltage.
This change is needed to ensure correct device behaviour.

Signed-off-by: Andy Sinclair <[email protected]>
  • Loading branch information
aasinclair authored and carlescufi committed Sep 13, 2023
1 parent 49ef9be commit dd52908
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions drivers/regulator/regulator_npm1300.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,33 @@ static int retention_set_voltage(const struct device *dev, int32_t retention_uv)
idx);
}

static int buck_get_voltage_index(const struct device *dev, uint8_t chan, uint8_t *idx)
{
const struct regulator_npm1300_config *config = dev->config;
uint8_t sel;
int ret;

ret = mfd_npm1300_reg_read(config->mfd, BUCK_BASE, BUCK_OFFSET_SW_CTRL, &sel);

if (ret < 0) {
return ret;
}

if ((sel >> chan) & 1U) {
/* SW control */
return mfd_npm1300_reg_read(config->mfd, BUCK_BASE,
BUCK_OFFSET_VOUT_NORM + (chan * 2U), idx);
}

/* VSET pin control */
return mfd_npm1300_reg_read(config->mfd, BUCK_BASE, BUCK_OFFSET_VOUT_STAT + chan, idx);
}

static int buck_set_voltage(const struct device *dev, uint8_t chan, int32_t min_uv, int32_t max_uv)
{
const struct regulator_npm1300_config *config = dev->config;
uint8_t mask;
uint8_t curr_idx;
uint16_t idx;
int ret;

Expand All @@ -154,6 +177,13 @@ static int buck_set_voltage(const struct device *dev, uint8_t chan, int32_t min_
return ret;
}

/* Get current setting, and return if current and new index match */
ret = buck_get_voltage_index(dev, chan, &curr_idx);

if ((ret < 0) || (idx == curr_idx)) {
return ret;
}

ret = mfd_npm1300_reg_write(config->mfd, BUCK_BASE, BUCK_OFFSET_VOUT_NORM + (chan * 2U),
idx);

Expand Down Expand Up @@ -201,26 +231,10 @@ int regulator_npm1300_set_voltage(const struct device *dev, int32_t min_uv, int3

static int buck_get_voltage(const struct device *dev, uint8_t chan, int32_t *volt_uv)
{
const struct regulator_npm1300_config *config = dev->config;
uint8_t sel;
uint8_t idx;
int ret;

ret = mfd_npm1300_reg_read(config->mfd, BUCK_BASE, BUCK_OFFSET_SW_CTRL, &sel);

if (ret < 0) {
return ret;
}

if ((sel >> chan) & 1U) {
/* SW control */
ret = mfd_npm1300_reg_read(config->mfd, BUCK_BASE,
BUCK_OFFSET_VOUT_NORM + (chan * 2U), &idx);
} else {
/* VSET pin control */
ret = mfd_npm1300_reg_read(config->mfd, BUCK_BASE, BUCK_OFFSET_VOUT_STAT + chan,
&idx);
}
ret = buck_get_voltage_index(dev, chan, &idx);

if (ret < 0) {
return ret;
Expand Down

0 comments on commit dd52908

Please sign in to comment.