drivers: regulator: Remove non-const usage of config struct

Config struct in pmic regulator driver was being used as a non-constant
value when an array defined at compile time was cast to a struct used at
runtime. Move this pointer to the data struct in pmic driver.

Fixes #41951

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
This commit is contained in:
Daniel DeGrasse 2022-01-25 11:58:43 -06:00 committed by Carles Cufí
commit 3a4594e444

View file

@ -24,10 +24,6 @@
LOG_MODULE_REGISTER(pmic_regulator, CONFIG_REGULATOR_LOG_LEVEL); LOG_MODULE_REGISTER(pmic_regulator, CONFIG_REGULATOR_LOG_LEVEL);
struct regulator_data {
struct onoff_sync_service srv;
};
struct __packed voltage_range { struct __packed voltage_range {
int uV; /* Voltage in uV */ int uV; /* Voltage in uV */
int reg_val; /* Register value for voltage */ int reg_val; /* Register value for voltage */
@ -38,10 +34,14 @@ struct __packed current_range {
int reg_val; /* Register value for current limit */ int reg_val; /* Register value for current limit */
}; };
struct regulator_data {
struct onoff_sync_service srv;
const struct voltage_range *voltages;
const struct current_range *current_levels;
};
struct regulator_config { struct regulator_config {
struct voltage_range *voltages;
int num_voltages; int num_voltages;
struct current_range *current_levels;
int num_current_levels; int num_current_levels;
uint8_t vsel_reg; uint8_t vsel_reg;
uint8_t vsel_mask; uint8_t vsel_mask;
@ -101,11 +101,12 @@ int regulator_count_voltages(const struct device *dev)
int regulator_list_voltages(const struct device *dev, unsigned int selector) int regulator_list_voltages(const struct device *dev, unsigned int selector)
{ {
const struct regulator_config *config = dev->config; const struct regulator_config *config = dev->config;
struct regulator_data *data = dev->data;
if (config->num_voltages <= selector) { if (config->num_voltages <= selector) {
return -ENODEV; return -ENODEV;
} }
return config->voltages[selector].uV; return data->voltages[selector].uV;
} }
/** /**
@ -127,6 +128,7 @@ int regulator_is_supported_voltage(const struct device *dev,
int regulator_set_voltage(const struct device *dev, int min_uV, int max_uV) int regulator_set_voltage(const struct device *dev, int min_uV, int max_uV)
{ {
const struct regulator_config *config = dev->config; const struct regulator_config *config = dev->config;
struct regulator_data *data = dev->data;
int i = 0; int i = 0;
if (!regulator_is_supported_voltage(dev, min_uV, max_uV) || if (!regulator_is_supported_voltage(dev, min_uV, max_uV) ||
@ -134,10 +136,10 @@ int regulator_set_voltage(const struct device *dev, int min_uV, int max_uV)
return -EINVAL; return -EINVAL;
} }
/* Find closest supported voltage */ /* Find closest supported voltage */
while (i < config->num_voltages && min_uV > config->voltages[i].uV) { while (i < config->num_voltages && min_uV > data->voltages[i].uV) {
i++; i++;
} }
if (config->voltages[i].uV > max_uV) { if (data->voltages[i].uV > max_uV) {
LOG_DBG("Regulator could not satisfy voltage range, too narrow"); LOG_DBG("Regulator could not satisfy voltage range, too narrow");
return -EINVAL; return -EINVAL;
} }
@ -147,9 +149,9 @@ int regulator_set_voltage(const struct device *dev, int min_uV, int max_uV)
return -EINVAL; return -EINVAL;
} }
LOG_DBG("Setting regulator %s to %duV", dev->name, LOG_DBG("Setting regulator %s to %duV", dev->name,
config->voltages[i].uV); data->voltages[i].uV);
return regulator_modify_register(config, config->vsel_reg, return regulator_modify_register(config, config->vsel_reg,
config->vsel_mask, config->voltages[i].reg_val); config->vsel_mask, data->voltages[i].reg_val);
} }
/** /**
@ -159,6 +161,7 @@ int regulator_set_voltage(const struct device *dev, int min_uV, int max_uV)
int regulator_get_voltage(const struct device *dev) int regulator_get_voltage(const struct device *dev)
{ {
const struct regulator_config *config = dev->config; const struct regulator_config *config = dev->config;
struct regulator_data *data = dev->data;
int rc, i = 0; int rc, i = 0;
uint8_t raw_reg; uint8_t raw_reg;
@ -170,14 +173,14 @@ int regulator_get_voltage(const struct device *dev)
raw_reg &= config->vsel_mask; raw_reg &= config->vsel_mask;
/* Locate the voltage value in the voltage table */ /* Locate the voltage value in the voltage table */
while (i < config->num_voltages && while (i < config->num_voltages &&
raw_reg != config->voltages[i].reg_val){ raw_reg != data->voltages[i].reg_val){
i++; i++;
} }
if (i == config->num_voltages) { if (i == config->num_voltages) {
LOG_WRN("Regulator vsel reg has unknown value"); LOG_WRN("Regulator vsel reg has unknown value");
return -EIO; return -EIO;
} }
return config->voltages[i].uV; return data->voltages[i].uV;
} }
/** /**
@ -187,6 +190,7 @@ int regulator_get_voltage(const struct device *dev)
int regulator_set_current_limit(const struct device *dev, int min_uA, int max_uA) int regulator_set_current_limit(const struct device *dev, int min_uA, int max_uA)
{ {
const struct regulator_config *config = dev->config; const struct regulator_config *config = dev->config;
struct regulator_data *data = dev->data;
int i = 0; int i = 0;
if (config->num_current_levels == 0) { if (config->num_current_levels == 0) {
@ -195,16 +199,16 @@ int regulator_set_current_limit(const struct device *dev, int min_uA, int max_uA
} }
/* Locate the desired current limit */ /* Locate the desired current limit */
while (i < config->num_current_levels && while (i < config->num_current_levels &&
min_uA > config->current_levels[i].uA) { min_uA > data->current_levels[i].uA) {
i++; i++;
} }
if (i == config->num_current_levels || if (i == config->num_current_levels ||
config->current_levels[i].uA > max_uA) { data->current_levels[i].uA > max_uA) {
return -EINVAL; return -EINVAL;
} }
/* Set the current limit */ /* Set the current limit */
return regulator_modify_register(config, config->ilim_reg, return regulator_modify_register(config, config->ilim_reg,
config->ilim_mask, config->current_levels[i].reg_val); config->ilim_mask, data->current_levels[i].reg_val);
} }
/** /**
@ -214,6 +218,7 @@ int regulator_set_current_limit(const struct device *dev, int min_uA, int max_uA
int regulator_get_current_limit(const struct device *dev) int regulator_get_current_limit(const struct device *dev)
{ {
const struct regulator_config *config = dev->config; const struct regulator_config *config = dev->config;
struct regulator_data *data = dev->data;
int rc, i = 0; int rc, i = 0;
uint8_t raw_reg; uint8_t raw_reg;
@ -227,13 +232,13 @@ int regulator_get_current_limit(const struct device *dev)
} }
raw_reg &= config->ilim_mask; raw_reg &= config->ilim_mask;
while (i < config->num_current_levels && while (i < config->num_current_levels &&
config->current_levels[i].reg_val != raw_reg) { data->current_levels[i].reg_val != raw_reg) {
i++; i++;
} }
if (i == config->num_current_levels) { if (i == config->num_current_levels) {
return -EIO; return -EIO;
} }
return config->current_levels[i].uA; return data->current_levels[i].uA;
} }
@ -286,14 +291,15 @@ static int disable_regulator(const struct device *dev)
static int pmic_reg_init(const struct device *dev) static int pmic_reg_init(const struct device *dev)
{ {
struct regulator_config *config = (struct regulator_config *)dev->config; const struct regulator_config *config = dev->config;
struct regulator_data *data = dev->data;
/* Cast the voltage array set at compile time to the voltage range /* Cast the voltage array set at compile time to the voltage range
* struct * struct
*/ */
config->voltages = (struct voltage_range *)config->voltage_array; data->voltages = (struct voltage_range *)config->voltage_array;
/* Do the same cast for current limit ranges */ /* Do the same cast for current limit ranges */
config->current_levels = (struct current_range *)config->current_array; data->current_levels = (struct current_range *)config->current_array;
/* Check to verify we have a valid I2C device */ /* Check to verify we have a valid I2C device */
if (config->i2c_dev == NULL || !device_is_ready(config->i2c_dev)) { if (config->i2c_dev == NULL || !device_is_ready(config->i2c_dev)) {
return -ENODEV; return -ENODEV;