1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/kernel.h>
7 #define ABSOLUTE_ZERO_MILLICELSIUS -273150
9 static inline long milli_kelvin_to_millicelsius(long t
)
11 return t
+ ABSOLUTE_ZERO_MILLICELSIUS
;
14 static inline long millicelsius_to_milli_kelvin(long t
)
16 return t
- ABSOLUTE_ZERO_MILLICELSIUS
;
19 #define MILLIDEGREE_PER_DEGREE 1000
20 #define MILLIDEGREE_PER_DECIDEGREE 100
22 static inline long kelvin_to_millicelsius(long t
)
24 return milli_kelvin_to_millicelsius(t
* MILLIDEGREE_PER_DEGREE
);
27 static inline long millicelsius_to_kelvin(long t
)
29 t
= millicelsius_to_milli_kelvin(t
);
31 return DIV_ROUND_CLOSEST(t
, MILLIDEGREE_PER_DEGREE
);
34 static inline long deci_kelvin_to_celsius(long t
)
36 t
= milli_kelvin_to_millicelsius(t
* MILLIDEGREE_PER_DECIDEGREE
);
38 return DIV_ROUND_CLOSEST(t
, MILLIDEGREE_PER_DEGREE
);
41 static inline long celsius_to_deci_kelvin(long t
)
43 t
= millicelsius_to_milli_kelvin(t
* MILLIDEGREE_PER_DEGREE
);
45 return DIV_ROUND_CLOSEST(t
, MILLIDEGREE_PER_DECIDEGREE
);
49 * deci_kelvin_to_millicelsius_with_offset - convert Kelvin to Celsius
50 * @t: temperature value in decidegrees Kelvin
51 * @offset: difference between Kelvin and Celsius in millidegrees
53 * Return: temperature value in millidegrees Celsius
55 static inline long deci_kelvin_to_millicelsius_with_offset(long t
, long offset
)
57 return t
* MILLIDEGREE_PER_DECIDEGREE
- offset
;
60 static inline long deci_kelvin_to_millicelsius(long t
)
62 return milli_kelvin_to_millicelsius(t
* MILLIDEGREE_PER_DECIDEGREE
);
65 static inline long millicelsius_to_deci_kelvin(long t
)
67 t
= millicelsius_to_milli_kelvin(t
);
69 return DIV_ROUND_CLOSEST(t
, MILLIDEGREE_PER_DECIDEGREE
);
72 static inline long kelvin_to_celsius(long t
)
74 return t
+ DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS
,
75 MILLIDEGREE_PER_DEGREE
);
78 static inline long celsius_to_kelvin(long t
)
80 return t
- DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS
,
81 MILLIDEGREE_PER_DEGREE
);
84 #endif /* _LINUX_UNITS_H */