From 5fccbcb95ebb0b2e2ea84f3409b401ef7f362df3 Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Wed, 23 Apr 2025 13:56:57 +1000 Subject: [PATCH] sensor: npm1300_charger: use common conversion funcs Use the standard sensor value conversion helpers for constructing `struct sensor_value` outputs. Signed-off-by: Jordan Yates --- .../nordic/npm1300_charger/npm1300_charger.c | 29 ++++++------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/drivers/sensor/nordic/npm1300_charger/npm1300_charger.c b/drivers/sensor/nordic/npm1300_charger/npm1300_charger.c index 838cb2b6734..b95da460d23 100644 --- a/drivers/sensor/nordic/npm1300_charger/npm1300_charger.c +++ b/drivers/sensor/nordic/npm1300_charger/npm1300_charger.c @@ -159,8 +159,7 @@ static void calc_temp(const struct npm1300_charger_config *const config, uint16_ float temp = (1.f / inv_temp_k) - 273.15f; - valp->val1 = (int32_t)temp; - valp->val2 = (int32_t)(fmodf(temp, 1.f) * 1000000.f); + (void)sensor_value_from_float(valp, temp); } static void calc_dietemp(const struct npm1300_charger_config *const config, uint16_t code, @@ -170,8 +169,7 @@ static void calc_dietemp(const struct npm1300_charger_config *const config, uint int32_t temp = DIETEMP_OFFSET_MDEGC - (((int32_t)code * DIETEMP_FACTOR_MUL) / DIETEMP_FACTOR_DIV); - valp->val1 = temp / 1000; - valp->val2 = (temp % 1000) * 1000; + (void)sensor_value_from_milli(valp, temp); } static uint32_t calc_ntc_res(const struct npm1300_charger_config *const config, int32_t temp_mdegc) @@ -214,8 +212,7 @@ static void calc_current(const struct npm1300_charger_config *const config, current = (data->current * full_scale_ma) / 1024; - valp->val1 = current / 1000; - valp->val2 = (current % 1000) * 1000; + (void)sensor_value_from_milli(valp, current); } int npm1300_charger_channel_get(const struct device *dev, enum sensor_channel chan, @@ -223,13 +220,10 @@ int npm1300_charger_channel_get(const struct device *dev, enum sensor_channel ch { const struct npm1300_charger_config *const config = dev->config; struct npm1300_charger_data *const data = dev->data; - int32_t tmp; switch ((uint32_t)chan) { case SENSOR_CHAN_GAUGE_VOLTAGE: - tmp = data->voltage * 5000 / 1024; - valp->val1 = tmp / 1000; - valp->val2 = (tmp % 1000) * 1000; + (void)sensor_value_from_milli(valp, (int32_t)data->voltage * 5000 / 1024); break; case SENSOR_CHAN_GAUGE_TEMP: if (config->thermistor_idx == 0) { @@ -249,12 +243,10 @@ int npm1300_charger_channel_get(const struct device *dev, enum sensor_channel ch valp->val2 = 0; break; case SENSOR_CHAN_GAUGE_DESIRED_CHARGING_CURRENT: - valp->val1 = config->current_microamp / 1000000; - valp->val2 = config->current_microamp % 1000000; + (void)sensor_value_from_micro(valp, config->current_microamp); break; case SENSOR_CHAN_GAUGE_MAX_LOAD_CURRENT: - valp->val1 = config->dischg_limit_microamp / 1000000; - valp->val2 = config->dischg_limit_microamp % 1000000; + (void)sensor_value_from_micro(valp, config->dischg_limit_microamp); break; case SENSOR_CHAN_DIE_TEMP: calc_dietemp(config, data->dietemp, valp); @@ -399,15 +391,12 @@ static int npm1300_charger_attr_get(const struct device *dev, enum sensor_channe if (data == 0U) { /* No charger connected */ - val->val1 = 0; - val->val2 = 0; + (void)sensor_value_from_micro(val, 0); } else if ((data & DETECT_HI_MASK) != 0U) { /* CC1 or CC2 indicate 1.5A or 3A capability */ - val->val1 = DETECT_HI_CURRENT / 1000000; - val->val2 = DETECT_HI_CURRENT % 1000000; + (void)sensor_value_from_micro(val, DETECT_HI_CURRENT); } else { - val->val1 = DETECT_LO_CURRENT / 1000000; - val->val2 = DETECT_LO_CURRENT % 1000000; + (void)sensor_value_from_micro(val, DETECT_LO_CURRENT); } return 0;