include/linux/units.h: add helpers for kelvin to/from Celsius conversion
[linux-block.git] / include / linux / units.h
CommitLineData
23331e48
AM
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_UNITS_H
3#define _LINUX_UNITS_H
4
5#include <linux/kernel.h>
6
7#define ABSOLUTE_ZERO_MILLICELSIUS -273150
8
9static inline long milli_kelvin_to_millicelsius(long t)
10{
11 return t + ABSOLUTE_ZERO_MILLICELSIUS;
12}
13
14static 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
22static inline long kelvin_to_millicelsius(long t)
23{
24 return milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DEGREE);
25}
26
27static 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
34static 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
41static 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 */
55static inline long deci_kelvin_to_millicelsius_with_offset(long t, long offset)
56{
57 return t * MILLIDEGREE_PER_DECIDEGREE - offset;
58}
59
60static inline long deci_kelvin_to_millicelsius(long t)
61{
62 return milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DECIDEGREE);
63}
64
65static 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
72static inline long kelvin_to_celsius(long t)
73{
74 return t + DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS,
75 MILLIDEGREE_PER_DEGREE);
76}
77
78static 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 */