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 | |
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>
-rw-r--r-- | driver/charger/rt9490.c | 63 | ||||
-rw-r--r-- | driver/charger/rt9490.h | 19 | ||||
-rw-r--r-- | zephyr/dts/bindings/temp/cros-ec,temp-sensor-rt9490.yaml | 4 | ||||
-rw-r--r-- | zephyr/projects/corsola/BUILD.py | 1 | ||||
-rw-r--r-- | zephyr/projects/corsola/adc_tentacruel.dts | 1 | ||||
-rw-r--r-- | zephyr/projects/corsola/thermistor_tentacruel.dts | 140 | ||||
-rw-r--r-- | zephyr/shim/src/temp_sensors.c | 17 |
7 files changed, 170 insertions, 75 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); diff --git a/zephyr/dts/bindings/temp/cros-ec,temp-sensor-rt9490.yaml b/zephyr/dts/bindings/temp/cros-ec,temp-sensor-rt9490.yaml index ae9da7fb62..260dcbf0c7 100644 --- a/zephyr/dts/bindings/temp/cros-ec,temp-sensor-rt9490.yaml +++ b/zephyr/dts/bindings/temp/cros-ec,temp-sensor-rt9490.yaml @@ -14,3 +14,7 @@ properties: required: true type: phandle description: phandle to the named i2c port + + thermistor: + type: phandle + description: Underlying thermistor device diff --git a/zephyr/projects/corsola/BUILD.py b/zephyr/projects/corsola/BUILD.py index ce16e4ff86..94959e4d9f 100644 --- a/zephyr/projects/corsola/BUILD.py +++ b/zephyr/projects/corsola/BUILD.py @@ -110,6 +110,7 @@ register_corsola_project( here / "led_tentacruel.dts", here / "motionsense_tentacruel.dts", here / "usbc_tentacruel.dts", + here / "thermistor_tentacruel.dts", ], extra_kconfig_files=[ here / "prj_it81202_base.conf", diff --git a/zephyr/projects/corsola/adc_tentacruel.dts b/zephyr/projects/corsola/adc_tentacruel.dts index 8a4a22f6c0..86acd14399 100644 --- a/zephyr/projects/corsola/adc_tentacruel.dts +++ b/zephyr/projects/corsola/adc_tentacruel.dts @@ -55,6 +55,7 @@ charger { compatible = "cros-ec,temp-sensor-rt9490", "cros-ec,temp-sensor"; + thermistor = <&thermistor_rt9490>; enum-name = "TEMP_SENSOR_CHARGER"; port = <&i2c_charger>; }; diff --git a/zephyr/projects/corsola/thermistor_tentacruel.dts b/zephyr/projects/corsola/thermistor_tentacruel.dts new file mode 100644 index 0000000000..9ac1b5cf58 --- /dev/null +++ b/zephyr/projects/corsola/thermistor_tentacruel.dts @@ -0,0 +1,140 @@ +/* Copyright 2022 The ChromiumOS Authors. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/ { + thermistor_rt9490: thermistor-rt9490 { + status = "okay"; + compatible = "cros-ec,thermistor"; + scaling-factor = <3>; + num-pairs = <21>; + steinhart-reference-mv = <4900>; + steinhart-reference-res = <10000>; + + sample-datum-0 { + milivolt = <(731 / 3)>; + temp = <0>; + sample-index = <0>; + }; + + sample-datum-1 { + milivolt = <(708 / 3)>; + temp = <5>; + sample-index = <1>; + }; + + sample-datum-2 { + milivolt = <(682 / 3)>; + temp = <10>; + sample-index = <2>; + }; + + sample-datum-3 { + milivolt = <(653 / 3)>; + temp = <15>; + sample-index = <3>; + }; + + sample-datum-4 { + milivolt = <(622 / 3)>; + temp = <20>; + sample-index = <4>; + }; + + sample-datum-5 { + milivolt = <(589 / 3)>; + temp = <25>; + sample-index = <5>; + }; + + sample-datum-6 { + milivolt = <(554 / 3)>; + temp = <30>; + sample-index = <6>; + }; + + sample-datum-7 { + milivolt = <(519 / 3)>; + temp = <35>; + sample-index = <7>; + }; + + sample-datum-8 { + milivolt = <(483 / 3)>; + temp = <40>; + sample-index = <8>; + }; + + sample-datum-9 { + milivolt = <(446 / 3)>; + temp = <45>; + sample-index = <9>; + }; + + sample-datum-10 { + milivolt = <(411 / 3)>; + temp = <50>; + sample-index = <10>; + }; + sample-datum-11 { + milivolt = <(376 / 3)>; + temp = <55>; + sample-index = <11>; + }; + + sample-datum-12 { + milivolt = <(343 / 3)>; + temp = <60>; + sample-index = <12>; + }; + + sample-datum-13 { + milivolt = <(312 / 3)>; + temp = <65>; + sample-index = <13>; + }; + + sample-datum-14 { + milivolt = <(284 / 3)>; + temp = <70>; + sample-index = <14>; + }; + + sample-datum-15 { + milivolt = <(257 / 3)>; + temp = <75>; + sample-index = <15>; + }; + + sample-datum-16 { + milivolt = <(232 / 3)>; + temp = <80>; + sample-index = <16>; + }; + + sample-datum-17 { + milivolt = <(209 / 3)>; + temp = <85>; + sample-index = <17>; + }; + + sample-datum-18 { + milivolt = <(188 / 3)>; + temp = <90>; + sample-index = <18>; + }; + + sample-datum-19 { + milivolt = <(169 / 3)>; + temp = <95>; + sample-index = <19>; + }; + + sample-datum-20 { + milivolt = <(152 / 3)>; + temp = <100>; + sample-index = <20>; + }; + }; +}; diff --git a/zephyr/shim/src/temp_sensors.c b/zephyr/shim/src/temp_sensors.c index 46a4c1fd8f..28d012047c 100644 --- a/zephyr/shim/src/temp_sensors.c +++ b/zephyr/shim/src/temp_sensors.c @@ -160,23 +160,16 @@ const struct tmp112_sensor_t tmp112_sensors[TMP112_COUNT] = { DT_FOREACH_STATUS_OKAY(cros_ec_temp_sensor_tmp112, DEFINE_TMP112_DATA) }; -#if DT_HAS_COMPAT_STATUS_OKAY(cros_ec_temp_sensor_rt9490) -static int rt9490_get_temp(const struct temp_sensor_t *sensor, int *temp_ptr) -{ - return rt9490_get_thermistor_val(sensor->idx, temp_ptr); -} - /* There can be only one thermistor on RT9490 with current driver */ #if DT_NUM_INST_STATUS_OKAY(cros_ec_temp_sensor_rt9490) > 1 #error "Unsupported number of thermistor on RT9490" #endif -#endif /* cros_ec_temp_sensor_rt9490 */ - -#define GET_ZEPHYR_TEMP_SENSOR_RT9490(node_id) \ - (&(struct zephyr_temp_sensor){ \ - .read = &rt9490_get_temp, \ - .thermistor = NULL, \ +#define GET_ZEPHYR_TEMP_SENSOR_RT9490(node_id) \ + (&(struct zephyr_temp_sensor){ \ + .read = &rt9490_get_thermistor_val, \ + .thermistor = \ + GET_THERMISTOR_INFO(DT_PHANDLE(node_id, thermistor)), \ }) #define TEMP_RT9490(node_id) \ |