xref: /openbmc/linux/include/linux/units.h (revision bef7a78d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_UNITS_H
3 #define _LINUX_UNITS_H
4 
5 #include <linux/math.h>
6 
7 #define ABSOLUTE_ZERO_MILLICELSIUS -273150
8 
9 static inline long milli_kelvin_to_millicelsius(long t)
10 {
11 	return t + ABSOLUTE_ZERO_MILLICELSIUS;
12 }
13 
14 static inline long millicelsius_to_milli_kelvin(long t)
15 {
16 	return t - ABSOLUTE_ZERO_MILLICELSIUS;
17 }
18 
19 #define MILLIDEGREE_PER_DEGREE 1000
20 #define MILLIDEGREE_PER_DECIDEGREE 100
21 
22 static inline long kelvin_to_millicelsius(long t)
23 {
24 	return milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DEGREE);
25 }
26 
27 static inline long millicelsius_to_kelvin(long t)
28 {
29 	t = millicelsius_to_milli_kelvin(t);
30 
31 	return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DEGREE);
32 }
33 
34 static inline long deci_kelvin_to_celsius(long t)
35 {
36 	t = milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DECIDEGREE);
37 
38 	return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DEGREE);
39 }
40 
41 static inline long celsius_to_deci_kelvin(long t)
42 {
43 	t = millicelsius_to_milli_kelvin(t * MILLIDEGREE_PER_DEGREE);
44 
45 	return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DECIDEGREE);
46 }
47 
48 /**
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
52  *
53  * Return: temperature value in millidegrees Celsius
54  */
55 static inline long deci_kelvin_to_millicelsius_with_offset(long t, long offset)
56 {
57 	return t * MILLIDEGREE_PER_DECIDEGREE - offset;
58 }
59 
60 static inline long deci_kelvin_to_millicelsius(long t)
61 {
62 	return milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DECIDEGREE);
63 }
64 
65 static inline long millicelsius_to_deci_kelvin(long t)
66 {
67 	t = millicelsius_to_milli_kelvin(t);
68 
69 	return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DECIDEGREE);
70 }
71 
72 static inline long kelvin_to_celsius(long t)
73 {
74 	return t + DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS,
75 				     MILLIDEGREE_PER_DEGREE);
76 }
77 
78 static inline long celsius_to_kelvin(long t)
79 {
80 	return t - DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS,
81 				     MILLIDEGREE_PER_DEGREE);
82 }
83 
84 #endif /* _LINUX_UNITS_H */
85