From bc2fa955a56d69c7e6fbb5d6b60bc92cb40b9c65 Mon Sep 17 00:00:00 2001 From: jeffrey Date: Fri, 26 Aug 2022 09:48:05 +0800 Subject: RT9490 : Use ADC thermistor on RT9490 Use thermistor RT9490 for charger temperature. BUG=b:242267801 TEST=ectool temps all to check it can read thermistor temperature. BRANCH=None Signed-off-by: jeffrey Change-Id: I91a01adafdc6aca7fcd2afb14c4e6a099826e46e Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3853779 Reviewed-by: Ting Shen Reviewed-by: Jeffrey Lin Commit-Queue: Jeffrey Lin Tested-by: Jeffrey Lin --- driver/charger/rt9490.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ driver/charger/rt9490.h | 12 ++++++++++ 2 files changed, 76 insertions(+) (limited to 'driver') diff --git a/driver/charger/rt9490.c b/driver/charger/rt9490.c index 204949abde..74b2f42ce3 100644 --- a/driver/charger/rt9490.c +++ b/driver/charger/rt9490.c @@ -7,6 +7,7 @@ #include "battery.h" #include "battery_smart.h" +#include "builtin/assert.h" #include "builtin/endian.h" #include "charger.h" #include "charge_manager.h" @@ -20,6 +21,7 @@ #include "usb_charge.h" #include "usb_pd.h" #include "util.h" +#include "temp_sensor/temp_sensor.h" /* Console output macros */ #define CPRINTF(format, args...) cprintf(CC_CHARGER, format, ##args) @@ -734,3 +736,65 @@ 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) +{ + uint16_t mv; + + if (idx != 0) + return EC_ERROR_PARAM1; + rt9490_read16(idx, RT9490_REG_TS_ADC, &mv); + *temp_ptr = thermistor_linear(mv, &charger_thermistor_info); + *temp_ptr = C_TO_K(*temp_ptr); + return EC_SUCCESS; +} diff --git a/driver/charger/rt9490.h b/driver/charger/rt9490.h index 80afb65274..7c718b9028 100644 --- a/driver/charger/rt9490.h +++ b/driver/charger/rt9490.h @@ -252,3 +252,15 @@ int rt9490_enable_adc(int chgnum, bool en); 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 */ +}; + +struct charger_thermistor_info { + uint8_t num_pairs; + const struct charger_thermistor_data_pair *data; +}; -- cgit v1.2.1