All of lore.kernel.org
 help / color / mirror / Atom feed
* [U-Boot] [PATCH 1/2] imx: thermal: update imx6 thermal driver according new equation
@ 2017-04-18 12:41 Peng Fan
  2017-04-18 12:41 ` [U-Boot] [PATCH 2/2] thermal: imx: fix calculation Peng Fan
  2017-05-11 11:24 ` [U-Boot] [PATCH 1/2] imx: thermal: update imx6 thermal driver according new equation Stefano Babic
  0 siblings, 2 replies; 4+ messages in thread
From: Peng Fan @ 2017-04-18 12:41 UTC (permalink / raw)
  To: u-boot

From IC guys:
"
After a thorough accuracy study of the Temp sense circuit,
we found that with our current equation, an average part can
read 7 degrees lower than a known forced temperature.
We also found out that the standard variance was around 2C;
which is the tightest distribution that we could create.
We need to change the temp sense equation to center the average
part around the target temperature.
"

New equation:
Tmeas = (Nmeas - n1) / slope + t1 + offset
n1= fused room count
t1= 25
offset=3.580661
slope= 0.4148468 – 0.0015423*n1

According the new equation, update the thermal driver.
c1 and c2 changed to u64 type and update comments.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
---
 drivers/thermal/imx_thermal.c | 38 +++++++++++++++++++++-----------------
 1 file changed, 21 insertions(+), 17 deletions(-)

diff --git a/drivers/thermal/imx_thermal.c b/drivers/thermal/imx_thermal.c
index 0509094..b159464 100644
--- a/drivers/thermal/imx_thermal.c
+++ b/drivers/thermal/imx_thermal.c
@@ -22,8 +22,9 @@
 /* board will busyloop until this many degrees C below CPU max temperature */
 #define TEMPERATURE_HOT_DELTA   5 /* CPU maxT - 5C */
 #define FACTOR0			10000000
-#define FACTOR1			15976
-#define FACTOR2			4297157
+#define FACTOR1			15423
+#define FACTOR2			4148468
+#define OFFSET			3580661
 #define MEASURE_FREQ		327
 #define TEMPERATURE_MIN         -40
 #define TEMPERATURE_HOT         85
@@ -54,39 +55,42 @@ static int read_cpu_temperature(struct udevice *dev)
 	struct thermal_data *priv = dev_get_priv(dev);
 	u32 fuse = priv->fuse;
 	int t1, n1;
-	u32 c1, c2;
+	u64 c1, c2;
 	u64 temp64;
 
 	/*
 	 * Sensor data layout:
 	 *   [31:20] - sensor value @ 25C
 	 * We use universal formula now and only need sensor value @ 25C
-	 * slope = 0.4297157 - (0.0015976 * 25C fuse)
+	 * slope = 0.4445388 - (0.0016549 * 25C fuse)
 	 */
 	n1 = fuse >> 20;
 	t1 = 25; /* t1 always 25C */
 
 	/*
 	 * Derived from linear interpolation:
-	 * slope = 0.4297157 - (0.0015976 * 25C fuse)
+	 * slope = 0.4445388 - (0.0016549 * 25C fuse)
 	 * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0
-	 * (Nmeas - n1) / (Tmeas - t1) = slope
+	 * offset = 3.580661
+	 * offset = OFFSET / 1000000
+	 * (Nmeas - n1) / (Tmeas - t1 - offset) = slope
 	 * We want to reduce this down to the minimum computation necessary
 	 * for each temperature read.  Also, we want Tmeas in millicelsius
 	 * and we don't want to lose precision from integer division. So...
-	 * Tmeas = (Nmeas - n1) / slope + t1
-	 * milli_Tmeas = 1000 * (Nmeas - n1) / slope + 1000 * t1
-	 * milli_Tmeas = -1000 * (n1 - Nmeas) / slope + 1000 * t1
-	 * Let constant c1 = (-1000 / slope)
-	 * milli_Tmeas = (n1 - Nmeas) * c1 + 1000 * t1
-	 * Let constant c2 = n1 *c1 + 1000 * t1
-	 * milli_Tmeas = c2 - Nmeas * c1
+	 * Tmeas = (Nmeas - n1) / slope + t1 + offset
+	 * milli_Tmeas = 1000000 * (Nmeas - n1) / slope + 1000000 * t1 + OFFSET
+	 * milli_Tmeas = -1000000 * (n1 - Nmeas) / slope + 1000000 * t1 + OFFSET
+	 * Let constant c1 = (-1000000 / slope)
+	 * milli_Tmeas = (n1 - Nmeas) * c1 + 1000000 * t1 + OFFSET
+	 * Let constant c2 = n1 *c1 + 1000000 * t1
+	 * milli_Tmeas = (c2 - Nmeas * c1) + OFFSET
+	 * Tmeas = ((c2 - Nmeas * c1) + OFFSET) / 1000000
 	 */
 	temp64 = FACTOR0;
-	temp64 *= 1000;
+	temp64 *= 1000000;
 	do_div(temp64, FACTOR1 * n1 - FACTOR2);
 	c1 = temp64;
-	c2 = n1 * c1 + 1000 * t1;
+	c2 = n1 * c1 + 1000000 * t1;
 
 	/*
 	 * now we only use single measure, every time we read
@@ -118,8 +122,8 @@ static int read_cpu_temperature(struct udevice *dev)
 		>> TEMPSENSE0_TEMP_CNT_SHIFT;
 	writel(TEMPSENSE0_FINISHED, &anatop->tempsense0_clr);
 
-	/* milli_Tmeas = c2 - Nmeas * c1 */
-	temperature = (long)(c2 - n_meas * c1)/1000;
+	/* Tmeas = (c2 - Nmeas * c1 + OFFSET) / 1000000 */
+	temperature = lldiv(c2 - n_meas * c1 + OFFSET, 1000000);
 
 	/* power down anatop thermal sensor */
 	writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_set);
-- 
2.6.2

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [U-Boot] [PATCH 2/2] thermal: imx: fix calculation
  2017-04-18 12:41 [U-Boot] [PATCH 1/2] imx: thermal: update imx6 thermal driver according new equation Peng Fan
@ 2017-04-18 12:41 ` Peng Fan
  2017-05-11 11:24   ` Stefano Babic
  2017-05-11 11:24 ` [U-Boot] [PATCH 1/2] imx: thermal: update imx6 thermal driver according new equation Stefano Babic
  1 sibling, 1 reply; 4+ messages in thread
From: Peng Fan @ 2017-04-18 12:41 UTC (permalink / raw)
  To: u-boot

Fix calculation. do_div can not handle negative values.
Use div_s64_rem to handle the calculation.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
---
 drivers/thermal/imx_thermal.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/thermal/imx_thermal.c b/drivers/thermal/imx_thermal.c
index b159464..d137bfd 100644
--- a/drivers/thermal/imx_thermal.c
+++ b/drivers/thermal/imx_thermal.c
@@ -16,6 +16,7 @@
 #include <dm.h>
 #include <errno.h>
 #include <malloc.h>
+#include <linux/math64.h>
 #include <thermal.h>
 #include <imx_thermal.h>
 
@@ -55,8 +56,9 @@ static int read_cpu_temperature(struct udevice *dev)
 	struct thermal_data *priv = dev_get_priv(dev);
 	u32 fuse = priv->fuse;
 	int t1, n1;
-	u64 c1, c2;
-	u64 temp64;
+	s64 c1, c2;
+	s64 temp64;
+	s32 rem;
 
 	/*
 	 * Sensor data layout:
@@ -88,7 +90,7 @@ static int read_cpu_temperature(struct udevice *dev)
 	 */
 	temp64 = FACTOR0;
 	temp64 *= 1000000;
-	do_div(temp64, FACTOR1 * n1 - FACTOR2);
+	temp64 = div_s64_rem(temp64, FACTOR1 * n1 - FACTOR2, &rem);
 	c1 = temp64;
 	c2 = n1 * c1 + 1000000 * t1;
 
@@ -123,7 +125,7 @@ static int read_cpu_temperature(struct udevice *dev)
 	writel(TEMPSENSE0_FINISHED, &anatop->tempsense0_clr);
 
 	/* Tmeas = (c2 - Nmeas * c1 + OFFSET) / 1000000 */
-	temperature = lldiv(c2 - n_meas * c1 + OFFSET, 1000000);
+	temperature = div_s64_rem(c2 - n_meas * c1 + OFFSET, 1000000, &rem);
 
 	/* power down anatop thermal sensor */
 	writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_set);
-- 
2.6.2

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [U-Boot] [PATCH 1/2] imx: thermal: update imx6 thermal driver according new equation
  2017-04-18 12:41 [U-Boot] [PATCH 1/2] imx: thermal: update imx6 thermal driver according new equation Peng Fan
  2017-04-18 12:41 ` [U-Boot] [PATCH 2/2] thermal: imx: fix calculation Peng Fan
@ 2017-05-11 11:24 ` Stefano Babic
  1 sibling, 0 replies; 4+ messages in thread
From: Stefano Babic @ 2017-05-11 11:24 UTC (permalink / raw)
  To: u-boot

On 18/04/2017 14:41, Peng Fan wrote:
> From IC guys:
> "
> After a thorough accuracy study of the Temp sense circuit,
> we found that with our current equation, an average part can
> read 7 degrees lower than a known forced temperature.
> We also found out that the standard variance was around 2C;
> which is the tightest distribution that we could create.
> We need to change the temp sense equation to center the average
> part around the target temperature.
> "
> 
> New equation:
> Tmeas = (Nmeas - n1) / slope + t1 + offset
> n1= fused room count
> t1= 25
> offset=3.580661
> slope= 0.4148468 – 0.0015423*n1
> 
> According the new equation, update the thermal driver.
> c1 and c2 changed to u64 type and update comments.
> 
> Signed-off-by: Peng Fan <peng.fan@nxp.com>
> Cc: Stefano Babic <sbabic@denx.de>
> ---


Applied to u-boot-imx, thanks !

Best regards,
Stefano Babic




-- 
=====================================================================
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sbabic at denx.de
=====================================================================

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [U-Boot] [PATCH 2/2] thermal: imx: fix calculation
  2017-04-18 12:41 ` [U-Boot] [PATCH 2/2] thermal: imx: fix calculation Peng Fan
@ 2017-05-11 11:24   ` Stefano Babic
  0 siblings, 0 replies; 4+ messages in thread
From: Stefano Babic @ 2017-05-11 11:24 UTC (permalink / raw)
  To: u-boot

On 18/04/2017 14:41, Peng Fan wrote:
> Fix calculation. do_div can not handle negative values.
> Use div_s64_rem to handle the calculation.
> 
> Signed-off-by: Peng Fan <peng.fan@nxp.com>
> Cc: Stefano Babic <sbabic@denx.de>
> ---

Applied to u-boot-imx, thanks !

Best regards,
Stefano Babic




-- 
=====================================================================
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sbabic at denx.de
=====================================================================

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-05-11 11:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-18 12:41 [U-Boot] [PATCH 1/2] imx: thermal: update imx6 thermal driver according new equation Peng Fan
2017-04-18 12:41 ` [U-Boot] [PATCH 2/2] thermal: imx: fix calculation Peng Fan
2017-05-11 11:24   ` Stefano Babic
2017-05-11 11:24 ` [U-Boot] [PATCH 1/2] imx: thermal: update imx6 thermal driver according new equation Stefano Babic

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.