From 65e82a3f49ab5077021e8dd16e1816d71f3d96cc Mon Sep 17 00:00:00 2001 From: Rob Barnes Date: Fri, 6 Aug 2021 14:15:56 -0600 Subject: tmp112: Support reading TMP112 in millikelvin TMP112 supports .0625 degrees of resolution. Retain this resolution and support reading the temp in degrees millikelvin. BUG=b:176994331 TEST=Build and run on guybrush BRANCH=None Change-Id: I2802016b1edb08678953238e7f01acdd320c37cf Signed-off-by: Rob Barnes Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3001391 Commit-Queue: Diana Z Reviewed-by: Diana Z --- driver/temp_sensor/tmp112.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'driver/temp_sensor/tmp112.c') diff --git a/driver/temp_sensor/tmp112.c b/driver/temp_sensor/tmp112.c index 064bc70301..4da5c4e0e8 100644 --- a/driver/temp_sensor/tmp112.c +++ b/driver/temp_sensor/tmp112.c @@ -10,6 +10,7 @@ #include "tmp112.h" #include "i2c.h" #include "hooks.h" +#include "math_util.h" #include "util.h" #define TMP112_RESOLUTION 12 @@ -18,7 +19,7 @@ #define CPRINTS(format, args...) cprints(CC_THERMAL, format, ## args) -static int temp_val_local[TMP112_COUNT]; +static int temp_mk_local[TMP112_COUNT]; static int raw_read16(int sensor, const int offset, int *data_ptr) { @@ -48,7 +49,7 @@ static int raw_write16(int sensor, const int offset, int data) offset, data); } -static int get_temp(int sensor, int *temp_ptr) +static int get_reg_temp(int sensor, int *temp_ptr) { int rv; int temp_raw = 0; @@ -61,32 +62,41 @@ static int get_temp(int sensor, int *temp_ptr) return EC_SUCCESS; } -static inline int tmp112_reg_to_c(int16_t reg) +static inline int tmp112_reg_to_mk(int16_t reg) { - int tmp; + int temp_mc; - tmp = (((reg >> TMP112_SHIFT1) * 1000 ) >> TMP112_SHIFT2); + temp_mc = (((reg >> TMP112_SHIFT1) * 1000) >> TMP112_SHIFT2); - return tmp / 1000; + return MILLI_CELSIUS_TO_MILLI_KELVIN(temp_mc); } -int tmp112_get_val(int idx, int *temp_ptr) +int tmp112_get_val_k(int idx, int *temp_k_ptr) { if (idx >= TMP112_COUNT) return EC_ERROR_INVAL; - *temp_ptr = temp_val_local[idx]; + *temp_k_ptr = MILLI_KELVIN_TO_KELVIN(temp_mk_local[idx]); + return EC_SUCCESS; +} + +int tmp112_get_val_mk(int idx, int *temp_mk_ptr) +{ + if (idx >= TMP112_COUNT) + return EC_ERROR_INVAL; + + *temp_mk_ptr = temp_mk_local[idx]; return EC_SUCCESS; } static void tmp112_poll(void) { int s; - int temp_c = 0; + int temp_reg = 0; for (s = 0; s < TMP112_COUNT; s++) { - if (get_temp(s, &temp_c) == EC_SUCCESS) - temp_val_local[s] = C_TO_K(tmp112_reg_to_c(temp_c)); + if (get_reg_temp(s, &temp_reg) == EC_SUCCESS) + temp_mk_local[s] = tmp112_reg_to_mk(temp_reg); } } DECLARE_HOOK(HOOK_SECOND, tmp112_poll, HOOK_PRIO_TEMP_SENSOR); -- cgit v1.2.1