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