drivers: adc: Add threshold_count to NPCX adc config structure

Number of supported ADC thresholds is provided by devicetree, this
change will add this property into `adc_npcx_config` structure and
replace macro usage. Driver behavior is not meant to be impacted.

Signed-off-by: Bernardo Perez Priego <bernardo.perez.priego@intel.com>
This commit is contained in:
Bernardo Perez Priego 2022-05-03 13:22:15 -07:00 committed by Carles Cufí
commit aa875b3766

View file

@ -38,9 +38,6 @@ LOG_MODULE_REGISTER(adc_npcx, CONFIG_ADC_LOG_LEVEL);
#define NPCX_ADC_CHN_CONVERSION_MODE 0
#define NPCX_ADC_SCAN_CONVERSION_MODE 1
/* ADC threshold detector number */
#define NPCX_ADC_THRESHOLD_COUNT DT_INST_PROP(0, threshold_count)
#define ADC_NPCX_THRVAL_RESOLUTION 10
#define ADC_NPCX_THRVAL_MAX BIT_MASK(ADC_NPCX_THRVAL_RESOLUTION)
@ -52,6 +49,8 @@ struct adc_npcx_config {
struct npcx_clk_cfg clk_cfg;
/* pinmux configuration */
const struct npcx_alt *alts_list;
/* amount of thresholds supported */
const uint8_t threshold_count;
};
struct adc_npcx_threshold_control {
@ -91,7 +90,8 @@ struct adc_npcx_threshold_data {
*/
uint8_t active_thresholds;
/* This array holds current configuration for each threshold. */
struct adc_npcx_threshold_control control[NPCX_ADC_THRESHOLD_COUNT];
struct adc_npcx_threshold_control
control[DT_INST_PROP(0, threshold_count)];
};
/* Driver data */
@ -177,12 +177,12 @@ static void adc_npcx_isr(const struct device *dev)
}
uint16_t thrcts;
for (uint8_t i = 0; i < NPCX_ADC_THRESHOLD_COUNT; i++) {
for (uint8_t i = 0; i < config->threshold_count; i++) {
if (IS_BIT_SET(inst->THRCTS, i) && IS_BIT_SET(inst->THRCTS,
(NPCX_THRCTS_THR1_IEN + i))) {
/* Avoid clearing other threshold status */
thrcts = inst->THRCTS &
~GENMASK(NPCX_ADC_THRESHOLD_COUNT - 1, 0);
~GENMASK(config->threshold_count - 1, 0);
/* Clear threshold status */
thrcts |= BIT(i);
inst->THRCTS = thrcts;
@ -428,6 +428,7 @@ int adc_npcx_threshold_ctrl_set_param(const struct device *dev,
const struct adc_npcx_threshold_param
*param)
{
const struct adc_npcx_config *config = dev->config;
struct adc_npcx_data *const data = dev->data;
struct adc_npcx_threshold_data *const t_data = data->threshold_data;
struct adc_npcx_threshold_control *const t_ctrl =
@ -438,7 +439,7 @@ int adc_npcx_threshold_ctrl_set_param(const struct device *dev,
return -EOPNOTSUPP;
}
if (!param || th_sel >= NPCX_ADC_THRESHOLD_COUNT) {
if (!param || th_sel >= config->threshold_count) {
return -EINVAL;
}
@ -487,7 +488,7 @@ static int adc_npcx_threshold_ctrl_setup(const struct device *dev,
struct adc_npcx_threshold_control *const t_ctrl =
&t_data->control[th_sel];
if (th_sel >= NPCX_ADC_THRESHOLD_COUNT) {
if (th_sel >= config->threshold_count) {
return -EINVAL;
}
@ -535,7 +536,7 @@ static int adc_npcx_threshold_enable_irq(const struct device *dev,
&t_data->control[th_sel];
uint16_t thrcts;
if (th_sel >= NPCX_ADC_THRESHOLD_COUNT) {
if (th_sel >= config->threshold_count) {
LOG_ERR("Invalid ADC threshold selection! (%d)", th_sel);
return -EINVAL;
}
@ -553,7 +554,7 @@ static int adc_npcx_threshold_enable_irq(const struct device *dev,
t_data->active_thresholds |= BIT(th_sel);
/* avoid clearing other threshold status */
thrcts = inst->THRCTS & ~GENMASK(NPCX_ADC_THRESHOLD_COUNT - 1, 0);
thrcts = inst->THRCTS & ~GENMASK(config->threshold_count - 1, 0);
/* Enable threshold detection */
THRCTL(config->base, (th_sel + 1)) |= BIT(NPCX_THRCTL_THEN);
@ -585,7 +586,7 @@ int adc_npcx_threshold_disable_irq(const struct device *dev,
return -EOPNOTSUPP;
}
if (th_sel >= NPCX_ADC_THRESHOLD_COUNT) {
if (th_sel >= config->threshold_count) {
LOG_ERR("Invalid ADC threshold selection! (%d)", th_sel);
return -EINVAL;
}
@ -597,7 +598,7 @@ int adc_npcx_threshold_disable_irq(const struct device *dev,
return -ENODEV;
}
/* avoid clearing other threshold status */
thrcts = inst->THRCTS & ~GENMASK(NPCX_ADC_THRESHOLD_COUNT - 1, 0);
thrcts = inst->THRCTS & ~GENMASK(config->threshold_count - 1, 0);
/* set enable threshold status */
thrcts &= ~BIT(NPCX_THRCTS_THR1_IEN + th_sel);
@ -672,6 +673,7 @@ static const struct adc_npcx_config adc_npcx_cfg_0 = {
.base = DT_INST_REG_ADDR(0),
.clk_cfg = NPCX_DT_CLK_CFG_ITEM(0),
.alts_list = adc_alts,
.threshold_count = DT_INST_PROP(0, threshold_count),
};
static struct adc_npcx_threshold_data threshold_data_0;