diff mbox series

[v2,1/8] add header file for kelvin to/from Celsius conversion helpers

Message ID 1574952879-7200-2-git-send-email-akinobu.mita@gmail.com (mailing list archive)
State Not Applicable
Headers show
Series add header file for kelvin to/from Celsius conversion helpers | expand

Commit Message

Akinobu Mita Nov. 28, 2019, 2:54 p.m. UTC
There are several helper macros to convert kelvin to/from Celsius in
<linux/thermal.h> for thermal drivers.  These are useful for any other
drivers or subsystems, but it's odd to include <linux/thermal.h> just for
the helpers.

This adds a new <linux/temperature.h> that provides the equivalent inline
functions for any drivers or subsystems.  It is intended to replace the
helpers in <linux/thermal.h>.

Cc: Sujith Thomas <sujith.thomas@intel.com>
Cc: Darren Hart <dvhart@infradead.org>
Cc: Andy Shevchenko <andy@infradead.org>
Cc: Zhang Rui <rui.zhang@intel.com>
Cc: Daniel Lezcano <daniel.lezcano@linaro.org>
Cc: Amit Kucheria <amit.kucheria@verdurent.com>
Cc: Jean Delvare <jdelvare@suse.com>
Cc: Guenter Roeck <linux@roeck-us.net>
Cc: Keith Busch <kbusch@kernel.org>
Cc: Jens Axboe <axboe@fb.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Sagi Grimberg <sagi@grimberg.me>
Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com>
---
* v2
- add deci_kelvin_to_millicelsius_with_offset() in linux/temperature.h
- stop including linux/temperature.h from linux/thermal.h

 include/linux/temperature.h | 51 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
 create mode 100644 include/linux/temperature.h

Comments

Daniel Lezcano Dec. 6, 2019, 4:49 p.m. UTC | #1
On 28/11/2019 15:54, Akinobu Mita wrote:
> There are several helper macros to convert kelvin to/from Celsius in
> <linux/thermal.h> for thermal drivers.  These are useful for any other
> drivers or subsystems, but it's odd to include <linux/thermal.h> just for
> the helpers.
> 
> This adds a new <linux/temperature.h> that provides the equivalent inline
> functions for any drivers or subsystems.  It is intended to replace the
> helpers in <linux/thermal.h>.
> 
> Cc: Sujith Thomas <sujith.thomas@intel.com>
> Cc: Darren Hart <dvhart@infradead.org>
> Cc: Andy Shevchenko <andy@infradead.org>
> Cc: Zhang Rui <rui.zhang@intel.com>
> Cc: Daniel Lezcano <daniel.lezcano@linaro.org>
> Cc: Amit Kucheria <amit.kucheria@verdurent.com>
> Cc: Jean Delvare <jdelvare@suse.com>
> Cc: Guenter Roeck <linux@roeck-us.net>
> Cc: Keith Busch <kbusch@kernel.org>
> Cc: Jens Axboe <axboe@fb.com>
> Cc: Christoph Hellwig <hch@lst.de>
> Cc: Sagi Grimberg <sagi@grimberg.me>
> Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com>
> ---
> * v2
> - add deci_kelvin_to_millicelsius_with_offset() in linux/temperature.h
> - stop including linux/temperature.h from linux/thermal.h
> 
>  include/linux/temperature.h | 51 +++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 51 insertions(+)
>  create mode 100644 include/linux/temperature.h
> 
> diff --git a/include/linux/temperature.h b/include/linux/temperature.h
> new file mode 100644
> index 0000000..679e70a
> --- /dev/null
> +++ b/include/linux/temperature.h
> @@ -0,0 +1,51 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +#ifndef _LINUX_TEMPERATURE_H
> +#define _LINUX_TEMPERATURE_H

It sounds strange to create a temperature file just for a few conversion
functions. Why not create an units.h file, so some more conversions
could be added later (not necessarily related to temperature) ?

> +#include <linux/kernel.h>
> +
> +#define ABSOLUTE_ZERO_MILLICELSIUS -273150
> +
> +static inline long kelvin_to_millicelsius(long t)

Why 'long' ?

> +{
> +	return t * 1000 + ABSOLUTE_ZERO_MILLICELSIUS;
> +}
> +
> +static inline long millicelsius_to_kelvin(long t)
> +{
> +	return DIV_ROUND_CLOSEST(t - ABSOLUTE_ZERO_MILLICELSIUS, 1000);

Please don't duplicate these operations, just do the conversion to the
right unit and then call a single function.

Replace the constant by macros like what we find in time64.h.

eg.
#define MILLICELSIUS_PER_CELCIUS 1000

So you can really do the cleanup in all the drivers, like removing:

drivers/iio/adc/qcom-vadc-common.h:#define KELVINMIL_CELSIUSMIL
        273150
drivers/iio/pressure/st_pressure_core.c:#define MCELSIUS_PER_CELSIUS
                1000
drivers/hwmon/xgene-hwmon.c:#define CELSIUS_TO_mCELSIUS(x)
((x) * 1000)
drivers/net/wireless/intel/iwlegacy/common.h:#define
KELVIN_TO_CELSIUS(x) ((x)-273)
drivers/net/wireless/intel/iwlegacy/common.h:#define
CELSIUS_TO_KELVIN(x) ((x)+273)
drivers/net/wireless/intel/iwlwifi/dvm/dev.h:#define
KELVIN_TO_CELSIUS(x) ((x)-273)
drivers/net/wireless/intel/iwlwifi/dvm/dev.h:#define
CELSIUS_TO_KELVIN(x) ((x)+273)
drivers/power/supply/sbs-battery.c:#define TEMP_KELVIN_TO_CELSIUS
        2731
drivers/thermal/armada_thermal.c:#define TO_MCELSIUS(c)
((c) * 1000)
drivers/thermal/rcar_gen3_thermal.c:#define MCELSIUS(temp)      ((temp)
* 1000)
drivers/thermal/rcar_thermal.c:#define MCELSIUS(temp)
((temp) * 1000)
drivers/thermal/samsung/exynos_tmu.c:#define MCELSIUS   1000
drivers/thermal/samsung/exynos_tmu.c:                   temp /= MCELSIUS;
drivers/thermal/samsung/exynos_tmu.c:                   hyst /= MCELSIUS;
drivers/thermal/samsung/exynos_tmu.c:   ref = trips[0].temperature /
MCELSIUS;
drivers/thermal/samsung/exynos_tmu.c:           *temp =
code_to_temp(data, value) * MCELSIUS;
drivers/thermal/samsung/exynos_tmu.c:           temp /= MCELSIUS;


> +}
> +
> +static inline long deci_kelvin_to_celsius(long t)
> +{
> +	return DIV_ROUND_CLOSEST(t * 100 + ABSOLUTE_ZERO_MILLICELSIUS, 1000);
> +}
> +
> +static inline long celsius_to_deci_kelvin(long t)
> +{
> +	return t * 10 - DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS, 100);
> +}
> +
> +/**
> + * deci_kelvin_to_millicelsius_with_offset - convert Kelvin to Celsius
> + * @t: temperature value in decidegrees Kelvin
> + * @offset: difference between Kelvin and Celsius in millidegrees
> + *
> + * Return: temperature value in millidegrees Celsius
> + */
> +static inline long deci_kelvin_to_millicelsius_with_offset(long t, long offset)
> +{
> +	return t * 100 - offset;
> +}
> +
> +static inline long deci_kelvin_to_millicelsius(long t)
> +{
> +	return t * 100 + ABSOLUTE_ZERO_MILLICELSIUS;
> +}
> +
> +static inline long millicelsius_to_deci_kelvin(long t)
> +{
> +	return DIV_ROUND_CLOSEST(t - ABSOLUTE_ZERO_MILLICELSIUS, 100);
> +}
> +
> +#endif /* _LINUX_TEMPERATURE_H */
>
Akinobu Mita Dec. 7, 2019, 3:57 p.m. UTC | #2
2019年12月7日(土) 1:49 Daniel Lezcano <daniel.lezcano@linaro.org>:
>
> On 28/11/2019 15:54, Akinobu Mita wrote:
> > There are several helper macros to convert kelvin to/from Celsius in
> > <linux/thermal.h> for thermal drivers.  These are useful for any other
> > drivers or subsystems, but it's odd to include <linux/thermal.h> just for
> > the helpers.
> >
> > This adds a new <linux/temperature.h> that provides the equivalent inline
> > functions for any drivers or subsystems.  It is intended to replace the
> > helpers in <linux/thermal.h>.
> >
> > Cc: Sujith Thomas <sujith.thomas@intel.com>
> > Cc: Darren Hart <dvhart@infradead.org>
> > Cc: Andy Shevchenko <andy@infradead.org>
> > Cc: Zhang Rui <rui.zhang@intel.com>
> > Cc: Daniel Lezcano <daniel.lezcano@linaro.org>
> > Cc: Amit Kucheria <amit.kucheria@verdurent.com>
> > Cc: Jean Delvare <jdelvare@suse.com>
> > Cc: Guenter Roeck <linux@roeck-us.net>
> > Cc: Keith Busch <kbusch@kernel.org>
> > Cc: Jens Axboe <axboe@fb.com>
> > Cc: Christoph Hellwig <hch@lst.de>
> > Cc: Sagi Grimberg <sagi@grimberg.me>
> > Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com>
> > ---
> > * v2
> > - add deci_kelvin_to_millicelsius_with_offset() in linux/temperature.h
> > - stop including linux/temperature.h from linux/thermal.h
> >
> >  include/linux/temperature.h | 51 +++++++++++++++++++++++++++++++++++++++++++++
> >  1 file changed, 51 insertions(+)
> >  create mode 100644 include/linux/temperature.h
> >
> > diff --git a/include/linux/temperature.h b/include/linux/temperature.h
> > new file mode 100644
> > index 0000000..679e70a
> > --- /dev/null
> > +++ b/include/linux/temperature.h
> > @@ -0,0 +1,51 @@
> > +/* SPDX-License-Identifier: GPL-2.0 */
> > +#ifndef _LINUX_TEMPERATURE_H
> > +#define _LINUX_TEMPERATURE_H
>
> It sounds strange to create a temperature file just for a few conversion
> functions. Why not create an units.h file, so some more conversions
> could be added later (not necessarily related to temperature) ?

Fine with me.

> > +#include <linux/kernel.h>
> > +
> > +#define ABSOLUTE_ZERO_MILLICELSIUS -273150
> > +
> > +static inline long kelvin_to_millicelsius(long t)
>
> Why 'long' ?

There's no specific reason, but the existing DECI_KELVIN_TO_CELSIUS() in
linux/thermal.h converts to 'long'.

Do you prefer changing 'long' to 'int'?

> > +{
> > +     return t * 1000 + ABSOLUTE_ZERO_MILLICELSIUS;
> > +}
> > +
> > +static inline long millicelsius_to_kelvin(long t)
> > +{
> > +     return DIV_ROUND_CLOSEST(t - ABSOLUTE_ZERO_MILLICELSIUS, 1000);
>
> Please don't duplicate these operations, just do the conversion to the
> right unit and then call a single function.
>
> Replace the constant by macros like what we find in time64.h.
>
> eg.
> #define MILLICELSIUS_PER_CELCIUS 1000

How about this?

#define ABSOLUTE_ZERO_MILLICELSIUS -273150

static inline long milli_kelvin_to_millicelsius(long t)
{
        return t + ABSOLUTE_ZERO_MILLICELSIUS;
}

static inline long millicelsius_to_milli_kelvin(long t)
{
        return t - ABSOLUTE_ZERO_MILLICELSIUS;
}

#define MILLIDEGREE_PER_DEGREE 1000
#define MILLIDEGREE_PER_DECIDEGREE 100

static inline long kelvin_to_millicelsius(long t)
{
        return milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DEGREE);
}

static inline long millicelsius_to_kelvin(long t)
{
        t = millicelsius_to_milli_kelvin(t);

        return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DEGREE);
}

static inline long deci_kelvin_to_celsius(long t)
{
        t = milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DECIDEGREE);

        return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DEGREE);
}

static inline long celsius_to_deci_kelvin(long t)
{
        t = millicelsius_to_milli_kelvin(t * MILLIDEGREE_PER_DEGREE);

        return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DECIDEGREE);
}

/**
 * deci_kelvin_to_millicelsius_with_offset - convert Kelvin to Celsius
 * @t: temperature value in decidegrees Kelvin
 * @offset: difference between Kelvin and Celsius in millidegrees
 *
 * Return: temperature value in millidegrees Celsius
 */
static inline long deci_kelvin_to_millicelsius_with_offset(long t, long offset)
{
        return t * MILLIDEGREE_PER_DECIDEGREE - offset;
}

static inline long deci_kelvin_to_millicelsius(long t)
{
        return milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DECIDEGREE);
}

static inline long millicelsius_to_deci_kelvin(long t)
{
        t = millicelsius_to_milli_kelvin(t);

        return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DECIDEGREE);
}

> So you can really do the cleanup in all the drivers, like removing:

OK.  I'll try as much as possible.
diff mbox series

Patch

diff --git a/include/linux/temperature.h b/include/linux/temperature.h
new file mode 100644
index 0000000..679e70a
--- /dev/null
+++ b/include/linux/temperature.h
@@ -0,0 +1,51 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_TEMPERATURE_H
+#define _LINUX_TEMPERATURE_H
+
+#include <linux/kernel.h>
+
+#define ABSOLUTE_ZERO_MILLICELSIUS -273150
+
+static inline long kelvin_to_millicelsius(long t)
+{
+	return t * 1000 + ABSOLUTE_ZERO_MILLICELSIUS;
+}
+
+static inline long millicelsius_to_kelvin(long t)
+{
+	return DIV_ROUND_CLOSEST(t - ABSOLUTE_ZERO_MILLICELSIUS, 1000);
+}
+
+static inline long deci_kelvin_to_celsius(long t)
+{
+	return DIV_ROUND_CLOSEST(t * 100 + ABSOLUTE_ZERO_MILLICELSIUS, 1000);
+}
+
+static inline long celsius_to_deci_kelvin(long t)
+{
+	return t * 10 - DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS, 100);
+}
+
+/**
+ * deci_kelvin_to_millicelsius_with_offset - convert Kelvin to Celsius
+ * @t: temperature value in decidegrees Kelvin
+ * @offset: difference between Kelvin and Celsius in millidegrees
+ *
+ * Return: temperature value in millidegrees Celsius
+ */
+static inline long deci_kelvin_to_millicelsius_with_offset(long t, long offset)
+{
+	return t * 100 - offset;
+}
+
+static inline long deci_kelvin_to_millicelsius(long t)
+{
+	return t * 100 + ABSOLUTE_ZERO_MILLICELSIUS;
+}
+
+static inline long millicelsius_to_deci_kelvin(long t)
+{
+	return DIV_ROUND_CLOSEST(t - ABSOLUTE_ZERO_MILLICELSIUS, 100);
+}
+
+#endif /* _LINUX_TEMPERATURE_H */