drivers: regulator: pca9420: improve instantiation code

- Constify configuration and curr. limits/voltage range/modes arrays
- Use common argument names: node_id, inst to make code more readable
- Improve macro names to make things clear

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2022-11-14 12:37:55 +01:00 committed by Carles Cufí
commit af590c4500

View file

@ -47,9 +47,9 @@ struct regulator_config {
uint8_t ilim_mask; uint8_t ilim_mask;
struct i2c_dt_spec i2c; struct i2c_dt_spec i2c;
uint16_t initial_mode; uint16_t initial_mode;
uint32_t *voltage_array; const uint32_t *voltage_array;
uint32_t *current_array; const uint32_t *current_array;
uint16_t *allowed_modes; const uint16_t *allowed_modes;
uint8_t modesel_offset; uint8_t modesel_offset;
uint8_t modesel_reg; uint8_t modesel_reg;
uint8_t modesel_mask; uint8_t modesel_mask;
@ -547,60 +547,55 @@ static const struct regulator_driver_api api = {
.mode_enable = regulator_pca9420_mode_enable, .mode_enable = regulator_pca9420_mode_enable,
}; };
/* #define REGULATOR_PCA9420_DEFINE(node_id, ord) \
* Each regulator output will be initialized as a separate device struct, static const uint32_t curr_limits_##ord[] = \
* and implement the regulator API. Since the DT binding is defined for the DT_PROP_OR(node_id, current_levels, {}); \
* entire regulator, this macro will be called for each child node of the static const uint32_t vol_range_##ord[] = \
* regulator device. This allows the regulator to have common DTS properties DT_PROP(node_id, voltage_range); \
* shared between each regulator output static const uint16_t allowed_modes_##ord[] = \
*/ DT_PROP_OR(DT_PARENT(node_id), regulator_allowed_modes, {}); \
#define CONFIGURE_REGULATOR_OUTPUT(node, ord) \ \
static uint32_t pmic_reg_##ord##_cur_limits[] = \ static struct regulator_data data_##ord; \
DT_PROP_OR(node, current_levels, {}); \ \
static uint32_t pmic_reg_##ord##_vol_range[] = \ static const struct regulator_config config_##ord = { \
DT_PROP(node, voltage_range); \ .vsel_mask = DT_PROP(node_id, vsel_mask), \
static uint16_t pmic_reg_##ord##_allowed_modes[] = \ .vsel_reg = DT_PROP(node_id, vsel_reg), \
DT_PROP_OR(DT_PARENT(node), regulator_allowed_modes, {}); \ .num_voltages = DT_PROP(node_id, num_voltages), \
static struct regulator_data pmic_reg_##ord##_data; \ .num_current_levels = DT_PROP(node_id, num_current_levels), \
static struct regulator_config pmic_reg_##ord##_cfg = { \ .enable_reg = DT_PROP(node_id, enable_reg), \
.vsel_mask = DT_PROP(node, vsel_mask), \ .enable_mask = DT_PROP(node_id, enable_mask), \
.vsel_reg = DT_PROP(node, vsel_reg), \ .enable_val = DT_PROP(node_id, enable_val), \
.num_voltages = DT_PROP(node, num_voltages), \ .min_uv = DT_PROP(node_id, min_uv), \
.num_current_levels = DT_PROP(node, num_current_levels), \ .max_uv = DT_PROP(node_id, max_uv), \
.enable_reg = DT_PROP(node, enable_reg), \ .ilim_reg = DT_PROP_OR(node_id, ilim_reg, 0), \
.enable_mask = DT_PROP(node, enable_mask), \ .ilim_mask = DT_PROP_OR(node_id, ilim_mask, 0), \
.enable_val = DT_PROP(node, enable_val), \ .enable_inverted = DT_PROP(node_id, enable_inverted), \
.min_uv = DT_PROP(node, min_uv), \ .boot_on = DT_PROP_OR(node_id, regulator_boot_on, false), \
.max_uv = DT_PROP(node, max_uv), \ .num_modes = ARRAY_SIZE(allowed_modes_##ord), \
.ilim_reg = DT_PROP_OR(node, ilim_reg, 0), \ .initial_mode = DT_PROP_OR(DT_PARENT(node_id), \
.ilim_mask = DT_PROP_OR(node, ilim_mask, 0), \ regulator_initial_mode, 0), \
.enable_inverted = DT_PROP(node, enable_inverted), \ .i2c = I2C_DT_SPEC_GET(DT_PARENT(node_id)), \
.boot_on = DT_PROP_OR(node, regulator_boot_on, false), \ .voltage_array = vol_range_##ord, \
.num_modes = ARRAY_SIZE(pmic_reg_##ord##_allowed_modes), \ .current_array = curr_limits_##ord, \
.initial_mode = DT_PROP_OR(DT_PARENT(node), regulator_initial_mode, 0), \ .allowed_modes = allowed_modes_##ord, \
.i2c = I2C_DT_SPEC_GET(DT_PARENT(node)), \ .modesel_offset = \
.voltage_array = pmic_reg_##ord##_vol_range, \ DT_PROP_OR(DT_PARENT(node_id), modesel_offset, 0), \
.current_array = pmic_reg_##ord##_cur_limits, \ .modesel_reg = DT_PROP_OR(DT_PARENT(node_id), modesel_reg, 0), \
.allowed_modes = pmic_reg_##ord##_allowed_modes, \ .modesel_mask = \
.modesel_offset = DT_PROP_OR(DT_PARENT(node), modesel_offset, 0), \ DT_PROP_OR(DT_PARENT(node_id), modesel_mask, 0), \
.modesel_reg = DT_PROP_OR(DT_PARENT(node), modesel_reg, 0), \ }; \
.modesel_mask = DT_PROP_OR(DT_PARENT(node), modesel_mask, 0), \ \
}; \ DEVICE_DT_DEFINE(node_id, pmic_reg_init, NULL, &data_##ord, \
DEVICE_DT_DEFINE(node, pmic_reg_init, NULL, \ &config_##ord, POST_KERNEL, \
&pmic_reg_##ord##_data, \ CONFIG_REGULATOR_PCA9420_INIT_PRIORITY, &api);
&pmic_reg_##ord##_cfg, \
POST_KERNEL, CONFIG_REGULATOR_PCA9420_INIT_PRIORITY, \
&api); \
/* Intermediate macros to extract DT node ordinal
* (used as a unique token for variable names)
*/
#define _CONFIGURE_REGULATOR_OUTPUT(node, ord) \
CONFIGURE_REGULATOR_OUTPUT(node, ord)
#define __CONFIGURE_REGULATOR_OUTPUT(node) \ #define _REGULATOR_PCA9420_DEFINE_ALL(node_id, ord) \
_CONFIGURE_REGULATOR_OUTPUT(node, DT_DEP_ORD(node)) REGULATOR_PCA9420_DEFINE(node_id, ord)
#define CONFIGURE_REGULATOR(id) \ #define __REGULATOR_PCA9420_DEFINE_ALL(node_id) \
DT_INST_FOREACH_CHILD(id, __CONFIGURE_REGULATOR_OUTPUT) _REGULATOR_PCA9420_DEFINE_ALL(node_id, DT_DEP_ORD(node_id))
DT_INST_FOREACH_STATUS_OKAY(CONFIGURE_REGULATOR) #define REGULATOR_PCA9420_DEFINE_ALL(inst) \
DT_INST_FOREACH_CHILD(inst, __REGULATOR_PCA9420_DEFINE_ALL)
DT_INST_FOREACH_STATUS_OKAY(REGULATOR_PCA9420_DEFINE_ALL)