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