[PATCH 1/5] hwmon: (cros_ec) Implement custom kelvin to celsius conversions
From: Thomas Weißschuh
Date: Fri May 29 2026 - 16:39:03 EST
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 <linux@xxxxxxxxxxxxxx>
---
drivers/hwmon/cros_ec_hwmon.c | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/drivers/hwmon/cros_ec_hwmon.c b/drivers/hwmon/cros_ec_hwmon.c
index 7c308b0a4b9e..f1b6c9c2d2a3 100644
--- a/drivers/hwmon/cros_ec_hwmon.c
+++ b/drivers/hwmon/cros_ec_hwmon.c
@@ -147,9 +147,23 @@ 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_millicelsius_to_kelvin(long t)
+{
+ return DIV_ROUND_CLOSEST(t - CROS_EC_HWMON_ABSOLUTE_ZERO_MILLICELSIUS,
+ MILLIDEGREE_PER_DEGREE);
+}
+
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)
@@ -228,7 +242,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);
}
}
--
2.54.0