1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/math.h>
7 #define HZ_PER_KHZ 1000UL
8 #define KHZ_PER_MHZ 1000UL
9 #define HZ_PER_MHZ 1000000UL
11 #define MILLIWATT_PER_WATT 1000UL
12 #define MICROWATT_PER_MILLIWATT 1000UL
13 #define MICROWATT_PER_WATT 1000000UL
15 #define ABSOLUTE_ZERO_MILLICELSIUS -273150
17 static inline long milli_kelvin_to_millicelsius(long t)
19 return t + ABSOLUTE_ZERO_MILLICELSIUS;
22 static inline long millicelsius_to_milli_kelvin(long t)
24 return t - ABSOLUTE_ZERO_MILLICELSIUS;
27 #define MILLIDEGREE_PER_DEGREE 1000
28 #define MILLIDEGREE_PER_DECIDEGREE 100
30 static inline long kelvin_to_millicelsius(long t)
32 return milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DEGREE);
35 static inline long millicelsius_to_kelvin(long t)
37 t = millicelsius_to_milli_kelvin(t);
39 return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DEGREE);
42 static inline long deci_kelvin_to_celsius(long t)
44 t = milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DECIDEGREE);
46 return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DEGREE);
49 static inline long celsius_to_deci_kelvin(long t)
51 t = millicelsius_to_milli_kelvin(t * MILLIDEGREE_PER_DEGREE);
53 return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DECIDEGREE);
57 * deci_kelvin_to_millicelsius_with_offset - convert Kelvin to Celsius
58 * @t: temperature value in decidegrees Kelvin
59 * @offset: difference between Kelvin and Celsius in millidegrees
61 * Return: temperature value in millidegrees Celsius
63 static inline long deci_kelvin_to_millicelsius_with_offset(long t, long offset)
65 return t * MILLIDEGREE_PER_DECIDEGREE - offset;
68 static inline long deci_kelvin_to_millicelsius(long t)
70 return milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DECIDEGREE);
73 static inline long millicelsius_to_deci_kelvin(long t)
75 t = millicelsius_to_milli_kelvin(t);
77 return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DECIDEGREE);
80 static inline long kelvin_to_celsius(long t)
82 return t + DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS,
83 MILLIDEGREE_PER_DEGREE);
86 static inline long celsius_to_kelvin(long t)
88 return t - DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS,
89 MILLIDEGREE_PER_DEGREE);
92 #endif /* _LINUX_UNITS_H */