1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/math.h>
7 #define MILLIWATT_PER_WATT 1000L
8 #define MICROWATT_PER_MILLIWATT 1000L
9 #define MICROWATT_PER_WATT 1000000L
11 #define ABSOLUTE_ZERO_MILLICELSIUS -273150
13 static inline long milli_kelvin_to_millicelsius(long t)
15 return t + ABSOLUTE_ZERO_MILLICELSIUS;
18 static inline long millicelsius_to_milli_kelvin(long t)
20 return t - ABSOLUTE_ZERO_MILLICELSIUS;
23 #define MILLIDEGREE_PER_DEGREE 1000
24 #define MILLIDEGREE_PER_DECIDEGREE 100
26 static inline long kelvin_to_millicelsius(long t)
28 return milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DEGREE);
31 static inline long millicelsius_to_kelvin(long t)
33 t = millicelsius_to_milli_kelvin(t);
35 return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DEGREE);
38 static inline long deci_kelvin_to_celsius(long t)
40 t = milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DECIDEGREE);
42 return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DEGREE);
45 static inline long celsius_to_deci_kelvin(long t)
47 t = millicelsius_to_milli_kelvin(t * MILLIDEGREE_PER_DEGREE);
49 return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DECIDEGREE);
53 * deci_kelvin_to_millicelsius_with_offset - convert Kelvin to Celsius
54 * @t: temperature value in decidegrees Kelvin
55 * @offset: difference between Kelvin and Celsius in millidegrees
57 * Return: temperature value in millidegrees Celsius
59 static inline long deci_kelvin_to_millicelsius_with_offset(long t, long offset)
61 return t * MILLIDEGREE_PER_DECIDEGREE - offset;
64 static inline long deci_kelvin_to_millicelsius(long t)
66 return milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DECIDEGREE);
69 static inline long millicelsius_to_deci_kelvin(long t)
71 t = millicelsius_to_milli_kelvin(t);
73 return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DECIDEGREE);
76 static inline long kelvin_to_celsius(long t)
78 return t + DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS,
79 MILLIDEGREE_PER_DEGREE);
82 static inline long celsius_to_kelvin(long t)
84 return t - DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS,
85 MILLIDEGREE_PER_DEGREE);
88 #endif /* _LINUX_UNITS_H */