From de0181c95b961a9d65ed5923ec3f4011944a6ef6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Date: Tue, 30 Jun 2026 22:57:51 +0200 Subject: [PATCH] hwmon: (cros_ec) Implement custom kelvin to celsius conversions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The ChromeOS EC APIs use integers representing degrees kelvin for temperatures. The default conversions from linux/units.h will then always convert these integer degrees celsius with a 150 millidegree offset. This is a bit confusing, as it also differs from other CrOS EC tooling. Internally the EC uses a kelvin to celsius offset of a round 273, so the current conversion is also not entirely accurate. Implement a custom conversion which preserves round values. Signed-off-by: Thomas Weißschuh Link: https://lore.kernel.org/r/20260630-cros_ec-hwmon-overflow-v1-1-3d2ecd3eb0f2@weissschuh.net Signed-off-by: Guenter Roeck --- drivers/hwmon/cros_ec_hwmon.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/drivers/hwmon/cros_ec_hwmon.c b/drivers/hwmon/cros_ec_hwmon.c index 03bfcc40bb7c..e91e2103a6c7 100644 --- a/drivers/hwmon/cros_ec_hwmon.c +++ b/drivers/hwmon/cros_ec_hwmon.c @@ -146,9 +146,17 @@ static bool cros_ec_hwmon_is_error_temp(u8 temp) temp == EC_TEMP_SENSOR_NOT_CALIBRATED; } +/* This differs slightly from the variant in units.h to avoid rounding inconsistencies. */ +#define CROS_EC_HWMON_ABSOLUTE_ZERO_MILLICELSIUS (-273000) + +static long cros_ec_hwmon_kelvin_to_millicelsius(long t) +{ + return t * MILLIDEGREE_PER_DEGREE + CROS_EC_HWMON_ABSOLUTE_ZERO_MILLICELSIUS; +} + static long cros_ec_hwmon_temp_to_millicelsius(u8 temp) { - return kelvin_to_millicelsius((((long)temp) + EC_TEMP_SENSOR_OFFSET)); + return cros_ec_hwmon_kelvin_to_millicelsius((((long)temp) + EC_TEMP_SENSOR_OFFSET)); } static bool cros_ec_hwmon_attr_is_temp_threshold(u32 attr) @@ -227,7 +235,7 @@ static int cros_ec_hwmon_read(struct device *dev, enum hwmon_sensor_types type, cros_ec_hwmon_attr_to_thres(attr), &threshold); if (ret == 0) - *val = kelvin_to_millicelsius(threshold); + *val = cros_ec_hwmon_kelvin_to_millicelsius(threshold); } }