Skip to contentSkip to conversation

power_supply: Register battery as a thermal zone

PATCH In review Jenny TC 2 participants 5 messages May 9, 2012 – Jun 18, 2012 Power supply

lore
Share & tools
lore

Jenny TC

author
#0 lore raw

PATCH v2 1/1 power_supply: Register battery as a thermal zone

Battery and charger contribute to Thermals in most of the embedded devices. So, it makes sense to identify them as Thermal zones in a particular platform.

This patch registers a thermal zone if the power supply is reporting a temperature property. The thermal zone will be used by platform's thermal management solution.

---
v1
* Made initial code changes
v2
* Removed unnecessary WARN_ON's
* Consolidated all #ifdef CONFIG_THERMAL in one place

 drivers/power/power_supply_core.c |   59 +++++++++++++++++++++++++++++++++++++
 include/linux/power_supply.h      |    3 ++
 2 files changed, 62 insertions(+), 0 deletions(-)
drivers/power/power_supply_core.c +59 -0
index 6ad6127..8e01432 100644
@@ -17,6 +17,7 @@
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/power_supply.h>
+#include <linux/thermal.h>
 #include "power_supply.h"
 
 /* exported for the APM Power driver, APM emulation */
@@ -169,6 +170,53 @@ static void power_supply_dev_release(struct device *dev)
 	kfree(dev);
 }
 
+#ifdef CONFIG_THERMAL
+static int power_supply_read_temp(struct thermal_zone_device *tzd,
+		unsigned long *temp)
+{
+	struct power_supply *psy;
+	union power_supply_propval val;
+	int ret;
+
+	WARN_ON(tzd == NULL);
+	psy = tzd->devdata;
+	ret = psy->get_property(psy,
+				  POWER_SUPPLY_PROP_TEMP, &val);
+
+	/* Convert tenths of degree Celsius to milli degree Celsius*/
+	if (!ret)
+		*temp = val.intval * 100;
+
+	return ret;
+}
+
+static struct thermal_zone_device_ops psy_tzd_ops = {
+	.get_temp = power_supply_read_temp,
+};
+
+static int register_thermal(struct power_supply *psy)
+{
+	int i;
+	/* Register battery zone device psy reports temperature */
+	for (i = 0; i < psy->num_properties; i++) {
+		if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) {
+			psy->tzd = thermal_zone_device_register(
+					(char *)psy->name, 0, psy,
+					&psy_tzd_ops, 0, 0, 0, 0);
+			if (IS_ERR(psy->tzd))
+				return PTR_ERR(psy->tzd);
+			break;
+		}
+	}
+	return 0;
+}
+#else
+static int register_thermal(struct power_supply *psy)
+{
+	return 0;
+}
+#endif
+
 int power_supply_register(struct device *parent, struct power_supply *psy)
 {
 	struct device *dev;
@@ -197,6 +245,10 @@ int power_supply_register(struct device *parent, struct power_supply *psy)
 	if (rc)
 		goto device_add_failed;
 
+	rc = register_thermal(psy);
+	if (rc)
+		goto therm_zone_reg_failed;
+
 	rc = power_supply_create_triggers(psy);
 	if (rc)
 		goto create_triggers_failed;
@@ -206,6 +258,9 @@ int power_supply_register(struct device *parent, struct power_supply *psy)
 	goto success;
 
 create_triggers_failed:
+	if (psy->tzd)
+		thermal_zone_device_unregister(psy->tzd);
+therm_zone_reg_failed:
 	device_del(dev);
 kobject_set_name_failed:
 device_add_failed:
@@ -220,6 +275,10 @@ void power_supply_unregister(struct power_supply *psy)
 	cancel_work_sync(&psy->changed_work);
 	sysfs_remove_link(&psy->dev->kobj, "powers");
 	power_supply_remove_triggers(psy);
+#ifdef CONFIG_THERMAL
+	if (psy->tzd)
+		thermal_zone_device_unregister(psy->tzd);
+#endif
 	device_unregister(psy->dev);
 }
 EXPORT_SYMBOL_GPL(power_supply_unregister);
include/linux/power_supply.h +3 -0
index c38c13d..1f58435 100644
@@ -172,6 +172,9 @@ struct power_supply {
 	/* private */
 	struct device *dev;
 	struct work_struct changed_work;
+#ifdef CONFIG_THERMAL
+	struct thermal_zone_device *tzd;
+#endif
 
 #ifdef CONFIG_LEDS_TRIGGERS
 	struct led_trigger *charging_full_trig;
Signature · 1 line
1.7.1
#1 lore raw
Quoted 10 lines · Jenny TC

Battery and charger contribute to Thermals in most of the embedded devices. So, it makes sense to identify them as Thermal zones in a particular platform.

This patch registers a thermal zone if the power supply is reporting a temperature property. The thermal zone will be used by platform's thermal management solution.

---

Once again, thanks for the patch! It's a nice feature.

[...]

Review int power_supply_register(struct device *parent, struct power_supply *psy) · line 262

Quoted 6 lines
(quoted diff) +2 -0
@@ -206,6 +258,9 @@ int power_supply_register(struct device *parent, struct power_supply *psy)
 	goto success;
 
 create_triggers_failed:
+	if (psy->tzd)
+		thermal_zone_device_unregister(psy->tzd);

This causes errors:

CC drivers/power/pda_power.o
drivers/power/power_supply_core.c: In function ‘power_supply_register’:
drivers/power/power_supply_core.c:261:9: error: ‘struct power_supply’ has no member named ‘tzd’ drivers/power/power_supply_core.c:262:37: error: ‘struct power_supply’ has no member named ‘tzd’ make[2]: *** [drivers/power/power_supply_core.o] Error 1

I think we should just introduce unregister_thermal(). So, that's what I've applied:

commit 3be330bf8860dc6079da5acc81295787a04cf4c9

Author: Jenny TC <[email protected]>
Date:   Wed May 9 20:36:47 2012 +0530

power_supply: Register battery as a thermal zone

Battery and charger contribute to Thermals in most of the embedded devices. So, it makes sense to identify them as Thermal zones in a particular platform.

This patch registers a thermal zone if the power supply is reporting a temperature property. The thermal zone will be used by platform's thermal management solution.

drivers/power/power_supply_core.c +65 -0
index 6ad6127..ff990d2 100644
@@ -17,6 +17,7 @@
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/power_supply.h>
+#include <linux/thermal.h>
 #include "power_supply.h"
 
 /* exported for the APM Power driver, APM emulation */
@@ -169,6 +170,63 @@ static void power_supply_dev_release(struct device *dev)
 	kfree(dev);
 }
 
+#ifdef CONFIG_THERMAL
+static int power_supply_read_temp(struct thermal_zone_device *tzd,
+		unsigned long *temp)
+{
+	struct power_supply *psy;
+	union power_supply_propval val;
+	int ret;
+
+	WARN_ON(tzd == NULL);
+	psy = tzd->devdata;
+	ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
+
+	/* Convert tenths of degree Celsius to milli degree Celsius. */
+	if (!ret)
+		*temp = val.intval * 100;
+
+	return ret;
+}
+
+static struct thermal_zone_device_ops psy_tzd_ops = {
+	.get_temp = power_supply_read_temp,
+};
+
+static int psy_register_thermal(struct power_supply *psy)
+{
+	int i;
+
+	/* Register battery zone device psy reports temperature */
+	for (i = 0; i < psy->num_properties; i++) {
+		if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) {
+			psy->tzd = thermal_zone_device_register(psy->name, 0,
+					psy, &psy_tzd_ops, 0, 0, 0, 0);
+			if (IS_ERR(psy->tzd))
+				return PTR_ERR(psy->tzd);
+			break;
+		}
+	}
+	return 0;
+}
+
+static void psy_unregister_thermal(struct power_supply *psy)
+{
+	if (IS_ERR_OR_NULL(psy->tzd))
+		return;
+	thermal_zone_device_unregister(psy->tzd);
+}
+#else
+static int psy_register_thermal(struct power_supply *psy)
+{
+	return 0;
+}
+
+static void psy_unregister_thermal(struct power_supply *psy)
+{
+}
+#endif
+
 int power_supply_register(struct device *parent, struct power_supply *psy)
 {
 	struct device *dev;
@@ -197,6 +255,10 @@ int power_supply_register(struct device *parent, struct power_supply *psy)
 	if (rc)
 		goto device_add_failed;
 
+	rc = psy_register_thermal(psy);
+	if (rc)
+		goto register_thermal_failed;
+
 	rc = power_supply_create_triggers(psy);
 	if (rc)
 		goto create_triggers_failed;
@@ -206,6 +268,8 @@ int power_supply_register(struct device *parent, struct power_supply *psy)
 	goto success;
 
 create_triggers_failed:
+	psy_unregister_thermal(psy);
+register_thermal_failed:
 	device_del(dev);
 kobject_set_name_failed:
 device_add_failed:
@@ -220,6 +284,7 @@ void power_supply_unregister(struct power_supply *psy)
 	cancel_work_sync(&psy->changed_work);
 	sysfs_remove_link(&psy->dev->kobj, "powers");
 	power_supply_remove_triggers(psy);
+	psy_unregister_thermal(psy);
 	device_unregister(psy->dev);
 }
 EXPORT_SYMBOL_GPL(power_supply_unregister);
include/linux/power_supply.h +3 -0
index 3b912be..59ed2dd 100644
@@ -173,6 +173,9 @@ struct power_supply {
 	/* private */
 	struct device *dev;
 	struct work_struct changed_work;
+#ifdef CONFIG_THERMAL
+	struct thermal_zone_device *tzd;
+#endif
 
 #ifdef CONFIG_LEDS_TRIGGERS
 	struct led_trigger *charging_full_trig;
author
#2 lore raw

Hi Anton,

Quoted 40 lines

-----Original Message-----
From: Anton Vorontsov [mailto:[email protected]]
Sent: Monday, June 18, 2012 9:59 AM
To: Tc, Jenny
Cc: [email protected]; R, Durgadoss
Subject: Re: [PATCHv2] power_supply: Register battery as a thermal zone

Quoted 10 lines · Jenny TC

Battery and charger contribute to Thermals in most of the embedded devices. So, it makes sense to identify them as Thermal zones in a particular platform.

This patch registers a thermal zone if the power supply is reporting a temperature property. The thermal zone will be used by platform's thermal management solution.

---

Once again, thanks for the patch! It's a nice feature.

[...]

Review int power_supply_register(struct device *parent,

Quoted 1 line
(quoted diff) +0 -0
@@ -206,6 +258,9 @@ int power_supply_register(struct device *parent,

struct power_supply *psy)

drivers/power/power_supply_core.c int power_supply_register(struct device *parent, struct power_supply *psy)

Quoted 5 lines
drivers/power/power_supply_core.c +2 -0
 	goto success;
 
 create_triggers_failed:
+	if (psy->tzd)
+		thermal_zone_device_unregister(psy->tzd);

This causes errors:

CC drivers/power/pda_power.o
drivers/power/power_supply_core.c: In function ‘power_supply_register’:
drivers/power/power_supply_core.c:261:9: error: ‘struct power_supply’ has no member named ‘tzd’ drivers/power/power_supply_core.c:262:37: error: ‘struct power_supply’ has no member named ‘tzd’
make[2]: *** [drivers/power/power_supply_core.o] Error 1

Quoted 2 lines

I think we should just introduce unregister_thermal(). So, that's what I've applied:

Sorry I missed it. Thanks for adding it. Hope you will merge it

-jtc ���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

#3 lore raw
In reply to Anton Vorontsov

On Sun, Jun 17, 2012 at 09:29:28PM -0700, Anton Vorontsov wrote:
[...]

drivers/power/power_supply_core.c static void power_supply_dev_release(struct device *dev) · line 203

Quoted 8 lines
drivers/power/power_supply_core.c +8 -0
+static int psy_register_thermal(struct power_supply *psy)
+{
+	int i;
+
+	/* Register battery zone device psy reports temperature */
+	for (i = 0; i < psy->num_properties; i++) {
+		if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) {
+			psy->tzd = thermal_zone_device_register(psy->name, 0,

Oh, and just FYI, there was a (char *) cast for psy->name, but I removed it. The cast should be not needed w/ this patch:

http://lkml.org/lkml/2012/6/18/28

Thanks,

Signature · 2 lines
Anton Vorontsov
Email: [email protected]
author
#4 lore raw
Quoted 21 lines

-----Original Message-----
From: Anton Vorontsov [mailto:[email protected]]
Sent: Monday, June 18, 2012 11:49 AM
To: Tc, Jenny
Cc: [email protected]; R, Durgadoss
Subject: Re: [PATCHv2] power_supply: Register battery as a thermal zone

On Sun, Jun 17, 2012 at 09:29:28PM -0700, Anton Vorontsov wrote:
[...]

Quoted 8 lines
drivers/power/power_supply_core.c +7 -0
+static int psy_register_thermal(struct power_supply *psy) {
+	int i;
+
+	/* Register battery zone device psy reports temperature */
+	for (i = 0; i < psy->num_properties; i++) {
+		if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) {
+			psy->tzd = thermal_zone_device_register(psy-

name, 0,

Oh, and just FYI, there was a (char *) cast for psy->name, but I removed it. The cast should be not needed w/ this patch:

Quoted 1 line

http://lkml.org/lkml/2012/6/18/28

Thanks. I applied it locally when I noticed the warning.

Quoted 6 lines

Thanks,

--
Anton Vorontsov
Email: [email protected]

���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�