units.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_UNITS_H
  3. #define _LINUX_UNITS_H
  4. #include <linux/kernel.h>
  5. #define MILLIWATT_PER_WATT 1000L
  6. #define MICROWATT_PER_MILLIWATT 1000L
  7. #define MICROWATT_PER_WATT 1000000L
  8. #define ABSOLUTE_ZERO_MILLICELSIUS -273150
  9. static inline long milli_kelvin_to_millicelsius(long t)
  10. {
  11. return t + ABSOLUTE_ZERO_MILLICELSIUS;
  12. }
  13. static inline long millicelsius_to_milli_kelvin(long t)
  14. {
  15. return t - ABSOLUTE_ZERO_MILLICELSIUS;
  16. }
  17. #define MILLIDEGREE_PER_DEGREE 1000
  18. #define MILLIDEGREE_PER_DECIDEGREE 100
  19. static inline long kelvin_to_millicelsius(long t)
  20. {
  21. return milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DEGREE);
  22. }
  23. static inline long millicelsius_to_kelvin(long t)
  24. {
  25. t = millicelsius_to_milli_kelvin(t);
  26. return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DEGREE);
  27. }
  28. static inline long deci_kelvin_to_celsius(long t)
  29. {
  30. t = milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DECIDEGREE);
  31. return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DEGREE);
  32. }
  33. static inline long celsius_to_deci_kelvin(long t)
  34. {
  35. t = millicelsius_to_milli_kelvin(t * MILLIDEGREE_PER_DEGREE);
  36. return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DECIDEGREE);
  37. }
  38. /**
  39. * deci_kelvin_to_millicelsius_with_offset - convert Kelvin to Celsius
  40. * @t: temperature value in decidegrees Kelvin
  41. * @offset: difference between Kelvin and Celsius in millidegrees
  42. *
  43. * Return: temperature value in millidegrees Celsius
  44. */
  45. static inline long deci_kelvin_to_millicelsius_with_offset(long t, long offset)
  46. {
  47. return t * MILLIDEGREE_PER_DECIDEGREE - offset;
  48. }
  49. static inline long deci_kelvin_to_millicelsius(long t)
  50. {
  51. return milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DECIDEGREE);
  52. }
  53. static inline long millicelsius_to_deci_kelvin(long t)
  54. {
  55. t = millicelsius_to_milli_kelvin(t);
  56. return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DECIDEGREE);
  57. }
  58. static inline long kelvin_to_celsius(long t)
  59. {
  60. return t + DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS,
  61. MILLIDEGREE_PER_DEGREE);
  62. }
  63. static inline long celsius_to_kelvin(long t)
  64. {
  65. return t - DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS,
  66. MILLIDEGREE_PER_DEGREE);
  67. }
  68. #endif /* _LINUX_UNITS_H */