diff options
author | Ting Shen <phoenixshen@google.com> | 2022-08-30 17:28:54 +0800 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-09-01 07:22:34 +0000 |
commit | 662bdbb65a424139b712021603513f02c0287d0f (patch) | |
tree | a93a16c05d0370d0bd9b87ba93f215e1400d5558 /driver/charger | |
parent | edcf3a266b635d51a6d2a8738b3f4965ce58a26c (diff) | |
download | chrome-ec-662bdbb65a424139b712021603513f02c0287d0f.tar.gz |
temp-sensor-rt9490: migrate to common thermistor code
Reuse existing linear interpolation algorithm and dts structure for
rt9490 temperature calculation.
BUG=none
TEST=`temps` in ec console looks reasonable:
ec:~$ temps
ambient 324 K (= 51 C)
charger 322 K (= 49 C)
BRANCH=none
Signed-off-by: Ting Shen <phoenixshen@google.com>
Change-Id: If69d9332d81a5b23b851c76c2e0539d62cbdfe09
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3865584
Tested-by: Ting Shen <phoenixshen@chromium.org>
Reviewed-by: Eric Yilun Lin <yllin@google.com>
Commit-Queue: Ting Shen <phoenixshen@chromium.org>
Diffstat (limited to 'driver/charger')
-rw-r--r-- | driver/charger/rt9490.c | 63 | ||||
-rw-r--r-- | driver/charger/rt9490.h | 19 |
2 files changed, 19 insertions, 63 deletions
diff --git a/driver/charger/rt9490.c b/driver/charger/rt9490.c index c950150128..e663b8ee79 100644 --- a/driver/charger/rt9490.c +++ b/driver/charger/rt9490.c @@ -22,6 +22,7 @@ #include "usb_pd.h" #include "util.h" #include "temp_sensor/temp_sensor.h" +#include "temp_sensor/thermistor.h" /* Console output macros */ #define CPRINTF(format, args...) cprintf(CC_CHARGER, format, ##args) @@ -737,64 +738,20 @@ struct bc12_config bc12_ports[CHARGE_PORT_COUNT] = { }; #endif /* CONFIG_BC12_SINGLE_DRIVER */ -__overridable struct charger_thermistor_data_pair charger_thermistor_data[] = { - { 731, 0 }, { 708, 5 }, { 682, 10 }, { 653, 15 }, { 622, 20 }, - { 589, 25 }, { 554, 30 }, { 519, 35 }, { 483, 40 }, { 446, 45 }, - { 411, 50 }, { 376, 55 }, { 343, 60 }, { 312, 65 }, { 284, 70 }, - { 257, 75 }, { 232, 80 }, { 209, 85 }, { 188, 90 }, { 169, 95 }, - { 152, 100 }, -}; - -__overridable struct charger_thermistor_info charger_thermistor_info = { - .num_pairs = ARRAY_SIZE(charger_thermistor_data), - .data = charger_thermistor_data, -}; - -int thermistor_linear(uint16_t mv, const struct charger_thermistor_info *info) -{ - const struct charger_thermistor_data_pair *data = info->data; - int v_high = 0, v_low = 0, t_low, t_high, num_steps; - int head, tail, mid = 0; - /* We need at least two points to form a line. */ - ASSERT(info->num_pairs >= 2); - - /* - * If input value is out of bounds return the lowest or highest - * value in the data sets provided. - */ - if (mv > data[0].mv) - return data[0].temp; - else if (mv < data[info->num_pairs - 1].mv) - return data[info->num_pairs - 1].temp; - - head = 0; - tail = info->num_pairs - 1; - while (head != tail) { - mid = (head + tail) / 2; - v_high = data[mid].mv; - v_low = data[mid + 1].mv; - if ((mv <= v_high) && (mv >= v_low)) - break; - else if (mv > v_high) - tail = mid; - else if (mv < v_low) - head = mid + 1; - } - - t_low = data[mid].temp; - t_high = data[mid + 1].temp; - num_steps = ((v_high - mv) * (t_high - t_low)) / (v_high - v_low); - return t_low + num_steps; -} - -int rt9490_get_thermistor_val(int idx, int *temp_ptr) +int rt9490_get_thermistor_val(const struct temp_sensor_t *sensor, int *temp_ptr) { uint16_t mv; + int idx = sensor->idx; +#if IS_ENABLED(CONFIG_ZEPHYR) && IS_ENABLED(CONFIG_TEMP_SENSOR) + const struct thermistor_info *info = sensor->zephyr_info->thermistor; +#else + const struct thermistor_info *info = &rt9490_thermistor_info; +#endif if (idx != 0) return EC_ERROR_PARAM1; - rt9490_read16(idx, RT9490_REG_TS_ADC, &mv); - *temp_ptr = thermistor_linear(mv, &charger_thermistor_info); + RETURN_ERROR(rt9490_read16(idx, RT9490_REG_TS_ADC, &mv)); + *temp_ptr = thermistor_linear_interpolate(mv, info); *temp_ptr = C_TO_K(*temp_ptr); return EC_SUCCESS; } diff --git a/driver/charger/rt9490.h b/driver/charger/rt9490.h index 7c718b9028..1a7abc51d3 100644 --- a/driver/charger/rt9490.h +++ b/driver/charger/rt9490.h @@ -6,6 +6,8 @@ */ #include <stdbool.h> +#include "temp_sensor.h" + #ifndef __CROS_EC_RT9490_H #define __CROS_EC_RT9490_H @@ -253,14 +255,11 @@ int rt9490_enable_pwm_1mhz(int chgnum, bool en); #endif /* __CROS_EC_RT9490_H */ -int rt9490_get_thermistor_val(int idx, int *temp_ptr); - -struct charger_thermistor_data_pair { - uint16_t mv; /* Scaled voltage level at ADC (in mV) */ - uint8_t temp; /* Temperature in Celsius */ -}; +/* + * Required for TS_ADC temperature calculation. + * Non-zephyr devices that using TS_ADC must define this in board layer. + */ +extern const struct thermistor_info rt9490_thermistor_info; -struct charger_thermistor_info { - uint8_t num_pairs; - const struct charger_thermistor_data_pair *data; -}; +int rt9490_get_thermistor_val(const struct temp_sensor_t *sensor, + int *temp_ptr); |