Skip to contentSkip to conversation

power-supply: Add function to return system-wide power state

PATCH In review Matthew Garrett 2 participants 5 messages Aug 26, 2008 – Aug 31, 2008 1 message not in the archive Power supply

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/1 power-supply: Add function to return system-wide power state

(resend, I screwed up the Cc)

Certain drivers benefit from knowing whether the system is on ac or battery, for instance when determining which backlight registers to read. This adds a simple call to determine whether there's an online power supply other than any batteries.

---

The name is a bit odd, but seems to fit in with the existing naming scheme.

drivers/power/power_supply_core.c +26 -0
index cb1ccb4..6a687da 100644
@@ -87,6 +87,31 @@ int power_supply_am_i_supplied(struct power_supply *psy)
 	return error;
 }
 
+static int __power_supply_is_system_supplied(struct device *dev, void *data)
+{
+	union power_supply_propval ret = {0,};
+	struct power_supply *psy = dev_get_drvdata(dev);
+
+	if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
+		if (psy->get_property(psy,
+				       POWER_SUPPLY_PROP_ONLINE, &ret))
+			return 0;
+		if (ret.intval)
+			return ret.intval;
+	}
+	return 0;
+}
+
+int power_supply_is_system_supplied()
+{
+	int error;
+
+	error = class_for_each_device(power_supply_class, NULL, NULL,
+				      __power_supply_is_system_supplied);
+
+	return error;
+}
+
 int power_supply_register(struct device *parent, struct power_supply *psy)
 {
 	int rc = 0;
@@ -148,6 +173,7 @@ static void __exit power_supply_class_exit(void)
 
 EXPORT_SYMBOL_GPL(power_supply_changed);
 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
+EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
 EXPORT_SYMBOL_GPL(power_supply_register);
 EXPORT_SYMBOL_GPL(power_supply_unregister);
 
include/linux/power_supply.h +1 -0
index ea96ead..3e67d0c 100644
@@ -164,6 +164,7 @@ struct power_supply_info {
 
 extern void power_supply_changed(struct power_supply *psy);
 extern int power_supply_am_i_supplied(struct power_supply *psy);
+extern int power_supply_is_system_supplied(void);
 
 extern int power_supply_register(struct device *parent,
 				 struct power_supply *psy);
Signature · 1 line
Matthew Garrett | [email protected]
#2 lore raw
Quoted 6 lines · Matthew Garrett

(resend, I screwed up the Cc)

Certain drivers benefit from knowing whether the system is on ac or battery, for instance when determining which backlight registers to read. This adds a simple call to determine whether there's an online power supply other than any batteries.

I like the patch, much thanks!

One comment below.

Quoted 56 lines

---

The name is a bit odd, but seems to fit in with the existing naming scheme.

drivers/power/power_supply_core.c +26 -0
index cb1ccb4..6a687da 100644
@@ -87,6 +87,31 @@ int power_supply_am_i_supplied(struct power_supply *psy)
 	return error;
 }
 
+static int __power_supply_is_system_supplied(struct device *dev, void *data)
+{
+	union power_supply_propval ret = {0,};
+	struct power_supply *psy = dev_get_drvdata(dev);
+
+	if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
+		if (psy->get_property(psy,
+				       POWER_SUPPLY_PROP_ONLINE, &ret))
+			return 0;
+		if (ret.intval)
+			return ret.intval;
+	}
+	return 0;
+}
+
+int power_supply_is_system_supplied()
+{
+	int error;
+
+	error = class_for_each_device(power_supply_class, NULL, NULL,
+				      __power_supply_is_system_supplied);
+
+	return error;
+}
+
 int power_supply_register(struct device *parent, struct power_supply *psy)
 {
 	int rc = 0;
@@ -148,6 +173,7 @@ static void __exit power_supply_class_exit(void)
 
 EXPORT_SYMBOL_GPL(power_supply_changed);
 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
+EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
 EXPORT_SYMBOL_GPL(power_supply_register);
 EXPORT_SYMBOL_GPL(power_supply_unregister);
 
include/linux/power_supply.h +0 -0
index ea96ead..3e67d0c 100644

include/linux/power_supply.h struct power_supply_info { · line 167

Quoted 4 lines
include/linux/power_supply.h +1 -0
@@ -164,6 +164,7 @@ struct power_supply_info {
 
 extern void power_supply_changed(struct power_supply *psy);
 extern int power_supply_am_i_supplied(struct power_supply *psy);
+extern int power_supply_is_system_supplied(void);

May I suggest adding
static int power_supply_is_system_supplied(void) { return -ENOSYS; } in case of !CONFIG_POWER_SUPPLY? This way drivers may not strictly depend on power supply class.

And "if (power_supply_is_system_supplied())" will assume that we're always supplied, which good assumption, I think.

Thanks,

Signature · 3 lines
Anton Vorontsov
email: [email protected]
irc://irc.freenode.net/bd2
#3 lore raw

Something like this?

drivers/power/power_supply_core.c +26 -0
index cb1ccb4..6a687da 100644
@@ -87,6 +87,31 @@ int power_supply_am_i_supplied(struct power_supply *psy)
 	return error;
 }
 
+static int __power_supply_is_system_supplied(struct device *dev, void *data)
+{
+	union power_supply_propval ret = {0,};
+	struct power_supply *psy = dev_get_drvdata(dev);
+
+	if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
+		if (psy->get_property(psy,
+				       POWER_SUPPLY_PROP_ONLINE, &ret))
+			return 0;
+		if (ret.intval)
+			return ret.intval;
+	}
+	return 0;
+}
+
+int power_supply_is_system_supplied()
+{
+	int error;
+
+	error = class_for_each_device(power_supply_class, NULL, NULL,
+				      __power_supply_is_system_supplied);
+
+	return error;
+}
+
 int power_supply_register(struct device *parent, struct power_supply *psy)
 {
 	int rc = 0;
@@ -148,6 +173,7 @@ static void __exit power_supply_class_exit(void)
 
 EXPORT_SYMBOL_GPL(power_supply_changed);
 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
+EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
 EXPORT_SYMBOL_GPL(power_supply_register);
 EXPORT_SYMBOL_GPL(power_supply_unregister);
 
include/linux/power_supply.h +6 -1
index ea96ead..304df22 100644
@@ -164,11 +164,16 @@ struct power_supply_info {
 
 extern void power_supply_changed(struct power_supply *psy);
 extern int power_supply_am_i_supplied(struct power_supply *psy);
-
 extern int power_supply_register(struct device *parent,
 				 struct power_supply *psy);
 extern void power_supply_unregister(struct power_supply *psy);
 
+#ifdef CONFIG_POWER_SUPPLY
+extern int power_supply_is_system_supplied(void);
+#else
+static int power_supply_is_system_supplied(void) { return -ENOSYS; }
+#endif
+
 /* For APM emulation, think legacy userspace. */
 extern struct class *power_supply_class;
 
Signature · 1 line
Matthew Garrett | [email protected]
#4 lore raw
Quoted 1 line · Matthew Garrett

Something like this?

Yup, except that I forgot 'inline' in my example. ;-) I'll add it before applying, thanks!

Quoted 65 lines
drivers/power/power_supply_core.c +26 -0
index cb1ccb4..6a687da 100644
@@ -87,6 +87,31 @@ int power_supply_am_i_supplied(struct power_supply *psy)
 	return error;
 }
 
+static int __power_supply_is_system_supplied(struct device *dev, void *data)
+{
+	union power_supply_propval ret = {0,};
+	struct power_supply *psy = dev_get_drvdata(dev);
+
+	if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
+		if (psy->get_property(psy,
+				       POWER_SUPPLY_PROP_ONLINE, &ret))
+			return 0;
+		if (ret.intval)
+			return ret.intval;
+	}
+	return 0;
+}
+
+int power_supply_is_system_supplied()
+{
+	int error;
+
+	error = class_for_each_device(power_supply_class, NULL, NULL,
+				      __power_supply_is_system_supplied);
+
+	return error;
+}
+
 int power_supply_register(struct device *parent, struct power_supply *psy)
 {
 	int rc = 0;
@@ -148,6 +173,7 @@ static void __exit power_supply_class_exit(void)
 
 EXPORT_SYMBOL_GPL(power_supply_changed);
 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
+EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
 EXPORT_SYMBOL_GPL(power_supply_register);
 EXPORT_SYMBOL_GPL(power_supply_unregister);
 
include/linux/power_supply.h +6 -1
index ea96ead..304df22 100644
@@ -164,11 +164,16 @@ struct power_supply_info {
 
 extern void power_supply_changed(struct power_supply *psy);
 extern int power_supply_am_i_supplied(struct power_supply *psy);
-
 extern int power_supply_register(struct device *parent,
 				 struct power_supply *psy);
 extern void power_supply_unregister(struct power_supply *psy);
 
+#ifdef CONFIG_POWER_SUPPLY
+extern int power_supply_is_system_supplied(void);
+#else
+static int power_supply_is_system_supplied(void) { return -ENOSYS; }
+#endif
+
 /* For APM emulation, think legacy userspace. */
 extern struct class *power_supply_class;
Signature · 3 lines
Anton Vorontsov
email: [email protected]
irc://irc.freenode.net/bd2
#5 lore raw
In reply to Matthew Garrett
Quoted 1 line · Matthew Garrett

Something like this?

I just applied it to battery-2.6.git with the following changes:

[...]

Quoted 1 line

+int power_supply_is_system_supplied()

-()
+(void)

[...]

Quoted 1 line

+#ifdef CONFIG_POWER_SUPPLY

Added || defined(CONFIG_POWER_SUPPLY_MODULE)

include/linux/power_supply.h struct power_supply_info { · line 174

Quoted 3 lines
include/linux/power_supply.h +3 -0
+extern int power_supply_is_system_supplied(void);
+#else
+static int power_supply_is_system_supplied(void) { return -ENOSYS; }

Added inline keyword, otherwise gcc might warn about defined but not used function.

Please test battery-2.6.git tree (or simply next -next tree ;-) if everything is okay.

Thanks again!

Signature · 3 lines
Anton Vorontsov
email: [email protected]
irc://irc.freenode.net/bd2