Skip to contentSkip to conversation

bq27x00_battery: Fix reporting battery temperature

PATCH Applied Pali Rohár 2 participants 11 messages Feb 2, 2013 – Apr 17, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bq27x00_battery: Fix reporting battery temperature

Reported temperature can be also negative, so cache value in non negative Kelvin degree.

---
 drivers/power/bq27x00_battery.c |   10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/power/bq27x00_battery.c +5 -5
index 36b34ef..4f39dd8 100644
@@ -299,7 +299,7 @@ static int bq27x00_battery_read_energy(struct bq27x00_device_info *di)
 }
 
 /*
- * Return the battery temperature in tenths of degree Celsius
+ * Return the battery temperature in tenths of degree Kelvin
  * Or < 0 if something fails.
  */
 static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
@@ -312,10 +312,8 @@ static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
 		return temp;
 	}
 
-	if (bq27xxx_is_chip_version_higher(di))
-		temp -= 2731;
-	else
-		temp = ((temp * 5) - 5463) / 2;
+	if (!bq27xxx_is_chip_version_higher(di))
+		temp = 5 * temp / 2;
 
 	return temp;
 }
@@ -642,6 +640,8 @@ static int bq27x00_battery_get_property(struct power_supply *psy,
 		break;
 	case POWER_SUPPLY_PROP_TEMP:
 		ret = bq27x00_simple_value(di->cache.temperature, val);
+		if (ret == 0)
+			val->intval -= 2731;
 		break;
 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
 		ret = bq27x00_simple_value(di->cache.time_to_empty, val);
Signature · 1 line
1.7.10.4
#2 lore raw
Quoted 8 lines · On Sunday 03 February 2013 04:44:51 Anton Vorontsov wrote:
Quoted 5 lines · Pali Rohár

Reported temperature can be also negative, so cache value in non negative Kelvin degree.

---

Applied, thanks!

Now I looked at bq27x00_battery and rx51_battery drivers and I see that both drivers reporting temperature in different units. bq27x00_battery in 1/10 °C and rx51_battery in 1/100 °C. What is correct degree for kernel power power supply API? Maybe other kernel drivers have different units too... Note that my above patch did not changed anything units, only fixed reporting (possible) negative temperature.

Signature · 2 lines
Pali Rohár
[email protected]
#3 lore raw
Quoted 10 lines · Pali Rohár
Quoted 8 lines · On Sunday 03 February 2013 04:44:51 Anton Vorontsov wrote:
Quoted 5 lines · Pali Rohár

Reported temperature can be also negative, so cache value in non negative Kelvin degree.

---

Applied, thanks!

Quoted 7 lines

Now I looked at bq27x00_battery and rx51_battery drivers and I see that both drivers reporting temperature in different units. bq27x00_battery in 1/10 °C and rx51_battery in 1/100 °C. What is correct degree for kernel power power supply API? Maybe other kernel drivers have different units too... Note that my above patch did not changed anything units, only fixed reporting (possible) negative temperature.

Per Documentation/power/power_supply_class.txt and power_supply.h:

/*
* All voltages, currents, charges, energies, time and temperatures in uV,
* µA, µAh, µWh, seconds and tenths of degree Celsius unless otherwise
* stated. It's driver's job to convert its raw values to units in which
* this class operates.
*/

Feel free to fix the offending drivers.

Thanks,

Anton

#4 lore raw
Quoted 30 lines · On Sunday 03 February 2013 21:05:57 Anton Vorontsov wrote:
Quoted 10 lines · Pali Rohár
Quoted 8 lines · On Sunday 03 February 2013 04:44:51 Anton Vorontsov wrote:
Quoted 5 lines · Pali Rohár

Reported temperature can be also negative, so cache value in non negative Kelvin degree.

---

Applied, thanks!

Quoted 7 lines

Now I looked at bq27x00_battery and rx51_battery drivers and I see that both drivers reporting temperature in different units. bq27x00_battery in 1/10 °C and rx51_battery in 1/100 °C. What is correct degree for kernel power power supply API? Maybe other kernel drivers have different units too... Note that my above patch did not changed anything units, only fixed reporting (possible) negative temperature.

Per Documentation/power/power_supply_class.txt and power_supply.h:

/*
* All voltages, currents, charges, energies, time and
temperatures in uV, * µA, µAh, µWh, seconds and tenths of degree Celsius unless otherwise * stated. It's driver's job to convert its raw values to units in which * this class operates.
*/

Quoted 5 lines

Feel free to fix the offending drivers.

Thanks,

Anton

Ok, here is small patch which fixing it in bq27x00_battery driver. Tested on Nokia N900 with patched 3.8-rc3 kernel. bq27x00_battery reporting "POWER_SUPPLY_TEMP=2700"
and rx51_battery reporting "POWER_SUPPLY_TEMP=3590"

From 43a99615d6d7375770736303f1e000f3d85d75cb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sun, 3 Feb 2013 22:06:20 +0100
Subject: [PATCH] bq27x00_battery: Report temperature in 1/100 degree Celsius
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Documentation/power/power_supply_class.txt say that temperature must be reported in tenths of degree Celsius

---
 drivers/power/bq27x00_battery.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/power/bq27x00_battery.c +5 -3
index 5b077af..482755f 100644
@@ -312,8 +312,10 @@ static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
 		return temp;
 	}
 
-	if (!bq27xxx_is_chip_version_higher(di))
-		temp = 5 * temp / 2;
+	if (bq27xxx_is_chip_version_higher(di))
+		temp *= 10;
+	else
+		temp *= 25;
 
 	return temp;
 }
@@ -640,7 +642,7 @@ static int bq27x00_battery_get_property(struct power_supply *psy,
 	case POWER_SUPPLY_PROP_TEMP:
 		ret = bq27x00_simple_value(di->cache.temperature, val);
 		if (ret == 0)
-			val->intval -= 2731;
+			val->intval -= 27310;
 		break;
 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
 		ret = bq27x00_simple_value(di->cache.time_to_empty, val);
Signature · 5 lines
1.7.10.4

-- 
Pali Rohár
[email protected]
#5 lore raw

On Wed, Feb 06, 2013 at 06:56:34PM +0100, Pali Rohár wrote:
[...]

Quoted 3 lines
Quoted 3 lines

* All voltages, currents, charges, energies, time and
temperatures in uV, * µA, µAh, µWh, seconds and tenths of degree Celsius unless otherwise * stated. It's driver's job

[...]

Quoted 2 lines

bq27x00_battery reporting "POWER_SUPPLY_TEMP=2700" and rx51_battery reporting "POWER_SUPPLY_TEMP=3590"

[...]

Quoted 1 line

Subject: [PATCH] bq27x00_battery: Report temperature in 1/100 degree Celsius

Hm. The documentation says tenth (1/10) degrees, and you even restate it in the commit message. But the subject, and your example seem to prove that you still report it in 1/100 of Celsius.

Unless your phone was on fire during the time you took the values, I tend to think the patch needs to be fixed. :-)

Thanks,
Anton

Quoted 40 lines

* Documentation/power/power_supply_class.txt say that temperature must be reported in tenths of degree Celsius

---
 drivers/power/bq27x00_battery.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/power/bq27x00_battery.c +5 -3
index 5b077af..482755f 100644
@@ -312,8 +312,10 @@ static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
 		return temp;
 	}
 
-	if (!bq27xxx_is_chip_version_higher(di))
-		temp = 5 * temp / 2;
+	if (bq27xxx_is_chip_version_higher(di))
+		temp *= 10;
+	else
+		temp *= 25;
 
 	return temp;
 }
@@ -640,7 +642,7 @@ static int bq27x00_battery_get_property(struct power_supply *psy,
 	case POWER_SUPPLY_PROP_TEMP:
 		ret = bq27x00_simple_value(di->cache.temperature, val);
 		if (ret == 0)
-			val->intval -= 2731;
+			val->intval -= 27310;
 		break;
 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
 		ret = bq27x00_simple_value(di->cache.time_to_empty, val);
Signature · 5 lines
1.7.10.4

-- 
Pali Rohár
[email protected]
#6 lore raw
Quoted 6 lines · On Saturday 09 February 2013 02:57:18 Anton Vorontsov wrote:

Hm. The documentation says tenth (1/10) degrees, and you even restate it in the commit message. But the subject, and your example seem to prove that you still report it in 1/100 of Celsius.

Quoted 6 lines

Unless your phone was on fire during the time you took the values, I tend to think the patch needs to be fixed. :-)

Thanks,
Anton

Ah, my mistake. I very quicky looked that bq and rx driver reporting different values... But bad conversation is in rx51_battery driver, not in bq. So drop previous bq patch...

Here is patch for rx51_battery. I think now it should report correct temperature. On Nokia N900 it reporting: POWER_SUPPLY_TEMP=350

From 92efbf24ac2fdc7af41abb12afd5e8c319479f6b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sat, 9 Feb 2013 11:06:48 +0100
Subject: [PATCH] =?UTF-8?q?rx51=5Fbattery:=20Fix=20reporting=20temperature=20?=
=?UTF-8?q?(in=201/10=20=C2=B0C)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 drivers/power/rx51_battery.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/power/rx51_battery.c +2 -2
index 8208888..527d256 100644
@@ -119,7 +119,7 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
 
 	/* First check for temperature in first direct table */
 	if (raw < ARRAY_SIZE(rx51_temp_table1))
-		return rx51_temp_table1[raw] * 100;
+		return rx51_temp_table1[raw] * 10;
 
 	/* Binary search RAW value in second inverse table */
 	while (max - min > 1) {
@@ -132,7 +132,7 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
 			break;
 	}
 
-	return (rx51_temp_table2_first - min) * 100;
+	return (rx51_temp_table2_first - min) * 10;
 }
 
 /*
Signature · 5 lines
1.7.10.4

-- 
Pali Rohár
[email protected]
#7 lore raw
Quoted 21 lines · Pali Rohár
Quoted 6 lines · On Saturday 09 February 2013 02:57:18 Anton Vorontsov wrote:

Hm. The documentation says tenth (1/10) degrees, and you even restate it in the commit message. But the subject, and your example seem to prove that you still report it in 1/100 of Celsius.

Quoted 6 lines

Unless your phone was on fire during the time you took the values, I tend to think the patch needs to be fixed. :-)

Thanks,
Anton

Ah, my mistake. I very quicky looked that bq and rx driver reporting different values... But bad conversation is in rx51_battery driver, not in bq. So drop previous bq patch...

Here is patch for rx51_battery. I think now it should report correct temperature. On Nokia N900 it reporting: POWER_SUPPLY_TEMP=350

Quoted 8 lines

From 92efbf24ac2fdc7af41abb12afd5e8c319479f6b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sat, 9 Feb 2013 11:06:48 +0100
Subject: [PATCH] =?UTF-8?q?rx51=5Fbattery:=20Fix=20reporting=20temperature=20?=
=?UTF-8?q?(in=201/10=20=C2=B0C)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The patch is missing signed-off-by tag and commit message. :-/

Thanks,

Quoted 32 lines
---
 drivers/power/rx51_battery.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/power/rx51_battery.c +2 -2
index 8208888..527d256 100644
@@ -119,7 +119,7 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
 
 	/* First check for temperature in first direct table */
 	if (raw < ARRAY_SIZE(rx51_temp_table1))
-		return rx51_temp_table1[raw] * 100;
+		return rx51_temp_table1[raw] * 10;
 
 	/* Binary search RAW value in second inverse table */
 	while (max - min > 1) {
@@ -132,7 +132,7 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
 			break;
 	}
 
-	return (rx51_temp_table2_first - min) * 100;
+	return (rx51_temp_table2_first - min) * 10;
 }
 
 /*
Signature · 5 lines
1.7.10.4

-- 
Pali Rohár
[email protected]
#8 lore raw

PATCH 1/1 power: rx51_battery: Fix reporting temperature

This patch fixing units (1/10 °C) in which is temperature reported.

---
 drivers/power/rx51_battery.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/power/rx51_battery.c +2 -2
index 8208888..527d256 100644
@@ -119,7 +119,7 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
 
 	/* First check for temperature in first direct table */
 	if (raw < ARRAY_SIZE(rx51_temp_table1))
-		return rx51_temp_table1[raw] * 100;
+		return rx51_temp_table1[raw] * 10;
 
 	/* Binary search RAW value in second inverse table */
 	while (max - min > 1) {
@@ -132,7 +132,7 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
 			break;
 	}
 
-	return (rx51_temp_table2_first - min) * 100;
+	return (rx51_temp_table2_first - min) * 10;
 }
 
 /*
Signature · 1 line
1.7.10.4
#9 lore raw

Re: [PATCH] power: rx51_battery: Fix reporting temperature

Quoted 31 lines · On Thursday 28 March 2013 17:42:23 Pali Rohár wrote:

This patch fixing units (1/10 °C) in which is temperature reported.

---
 drivers/power/rx51_battery.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/drivers/power/rx51_battery.c index 8208888..527d256 100644

drivers/power/rx51_battery.c +1 -1
@@ -119,7 +119,7 @@ static int
 rx51_battery_read_temperature(struct rx51_device_info *di)
 
 	/* First check for temperature in first direct table */
 	if (raw < ARRAY_SIZE(rx51_temp_table1))
-		return rx51_temp_table1[raw] * 100;
+		return rx51_temp_table1[raw] * 10;
 
 	/* Binary search RAW value in second inverse table */
drivers/power/rx51_battery.c +1 -1
 	while (max - min > 1) {
@@ -132,7 +132,7 @@ static int
 rx51_battery_read_temperature(struct rx51_device_info *di)
 break;
 	}
 
-	return (rx51_temp_table2_first - min) * 100;
+	return (rx51_temp_table2_first - min) * 10;
 }
 
Quoted 1 line

/*

Hi Anton, please include this patch into your battery tree.

Signature · 2 lines
Pali Rohár
[email protected]
#10 lore raw

Re: [PATCH] power: rx51_battery: Fix reporting temperature

In reply to Pali Rohár
Quoted 4 lines · Pali Rohár

This patch fixing units (1/10 °C) in which is temperature reported.

---

Applied, thanks!

Quoted 27 lines
 drivers/power/rx51_battery.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/power/rx51_battery.c +2 -2
index 8208888..527d256 100644
@@ -119,7 +119,7 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
 
 	/* First check for temperature in first direct table */
 	if (raw < ARRAY_SIZE(rx51_temp_table1))
-		return rx51_temp_table1[raw] * 100;
+		return rx51_temp_table1[raw] * 10;
 
 	/* Binary search RAW value in second inverse table */
 	while (max - min > 1) {
@@ -132,7 +132,7 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
 			break;
 	}
 
-	return (rx51_temp_table2_first - min) * 100;
+	return (rx51_temp_table2_first - min) * 10;
 }
 
 /*
Signature · 1 line
1.7.10.4