linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] power_supply: Added support for power supply attribute sources
@ 2012-07-26 15:17 Ramakrishna Pallala
  2012-07-26 17:04 ` Anton Vorontsov
  0 siblings, 1 reply; 5+ messages in thread
From: Ramakrishna Pallala @ 2012-07-26 15:17 UTC (permalink / raw)
  To: linux-kernel; +Cc: Anton Vorontsov, Anton Vorontsov, Ramakrishna Pallala

On some platforms one driver(or HW chip) may not be able to provide all
the necessary attributes of the power supply connected to the platform or
may provide very limited info which can be used by core/primary drivers.

For example a temperature sensor chip placed near the battery can be used
to report battery ambient temperature but it does not makes sense to register
sensor driver with power supply class. Or even a ADC driver or platform
driver may report power supply properties like voltage/current or charging
status but registering all those driver with power supply class is not a
practical or ideal approach.

This patch adds the generic support to register the drivers as power
supply attribute(properties) sources and adds an interface to read
these attributes from power supply class drivers.

If there are multiple attribute sources of the same type then caller has
to do source selection by passing the source string in the query struct.

Signed-off-by: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
---
 Documentation/power/power_supply_class.txt |   30 ++++++++++
 drivers/power/power_supply_core.c          |   81 ++++++++++++++++++++++++++++
 include/linux/power_supply.h               |   32 +++++++++++
 3 files changed, 143 insertions(+), 0 deletions(-)

diff --git a/Documentation/power/power_supply_class.txt b/Documentation/power/power_supply_class.txt
index c0f62ae..f8ceb45 100644
--- a/Documentation/power/power_supply_class.txt
+++ b/Documentation/power/power_supply_class.txt
@@ -130,6 +130,36 @@ while battery powers a load)
 TIME_TO_FULL - seconds left for battery to be considered full (i.e.
 while battery is charging)
 
+Power supply attribute sources
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+On some platforms one driver(or HW chip) may not be able to provide all
+the necessary attributes of the power supply connected to the platform.
+
+For example a temperature sensor chip placed near the battery can be used
+to report battery ambient temperature but it does not makes sense to register
+sensor driver with power supply class. Or even a ADC driver or platform
+driver may report power supply properties like voltage/current or charging
+status but registering all those driver with power supply class is not a
+practical or ideal approach.
+
+Power supply subsystem provides an interface to register and report about
+these power supply attributes to the primary driver which is registered
+with power supply class.
+
+Power supply attribute source driver can use the following functions to
+register/unregister as attributes source.
+
+int power_supply_attributes_register(struct device *parent,
+				struct power_supply_attr_source *psy_attr);
+
+void power_supply_attributes_unregister(
+			struct power_supply_attr_source *psy_attr);
+
+Power supply class driver(consumer driver) which needs to get
+power supply attributes can call the following function.
+
+int power_supply_get_external_attr(
+				struct power_supply_attr_query *query);
 
 Battery <-> external power supply interaction
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c
index ff990d2..a4b52f4 100644
--- a/drivers/power/power_supply_core.c
+++ b/drivers/power/power_supply_core.c
@@ -14,6 +14,7 @@
 #include <linux/types.h>
 #include <linux/init.h>
 #include <linux/slab.h>
+#include <linux/list.h>
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/power_supply.h>
@@ -26,6 +27,8 @@ EXPORT_SYMBOL_GPL(power_supply_class);
 
 static struct device_type power_supply_dev_type;
 
+static LIST_HEAD(list_head_source_attr);
+
 static int __power_supply_changed_work(struct device *dev, void *data)
 {
 	struct power_supply *psy = (struct power_supply *)data;
@@ -164,6 +167,37 @@ int power_supply_powers(struct power_supply *psy, struct device *dev)
 }
 EXPORT_SYMBOL_GPL(power_supply_powers);
 
+int power_supply_get_external_attr(struct power_supply_attr_query *query)
+{
+	struct list_head *list;
+	struct power_supply_attr_source *psy_attr;
+	int ret = -ENODEV;
+
+	if (!query || list_empty(&list_head_source_attr))
+		return -EINVAL;
+
+	list_for_each(list, &list_head_source_attr) {
+		psy_attr = list_entry(list,
+			struct power_supply_attr_source, attr_pool);
+
+		if (psy_attr->type != query->type)
+			continue;
+		if (query->src_name && strcmp(query->src_name,
+						psy_attr->name))
+			continue;
+
+		ret = psy_attr->get_property(psy_attr,
+				query->property, &query->res);
+		if (ret < 0)
+			continue;
+		else
+			break;
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(power_supply_get_external_attr);
+
 static void power_supply_dev_release(struct device *dev)
 {
 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
@@ -289,6 +323,53 @@ void power_supply_unregister(struct power_supply *psy)
 }
 EXPORT_SYMBOL_GPL(power_supply_unregister);
 
+int power_supply_attributes_register(struct device *parent,
+				struct power_supply_attr_source *psy_attr)
+{
+	struct device *dev;
+	int rc;
+
+	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
+	if (!dev)
+		return -ENOMEM;
+
+	device_initialize(dev);
+
+	dev->parent = parent;
+	dev->release = power_supply_dev_release;
+	dev_set_drvdata(dev, psy_attr);
+	psy_attr->dev = dev;
+
+	rc = kobject_set_name(&dev->kobj, "%s", psy_attr->name);
+	if (rc)
+		goto kobject_set_name_failed;
+
+	rc = device_add(dev);
+	if (rc)
+		goto device_add_failed;
+
+	INIT_LIST_HEAD(&psy_attr->attr_pool);
+	/* add to the list head */
+	list_add(&psy_attr->attr_pool, &list_head_source_attr);
+
+	goto success;
+
+kobject_set_name_failed:
+device_add_failed:
+	put_device(dev);
+success:
+	return rc;
+}
+EXPORT_SYMBOL_GPL(power_supply_attributes_register);
+
+void power_supply_attributes_unregister(struct power_supply_attr_source
+								*psy_attr)
+{
+	list_del(&psy_attr->attr_pool);
+	device_unregister(psy_attr->dev);
+}
+EXPORT_SYMBOL_GPL(power_supply_attributes_unregister);
+
 static int __init power_supply_class_init(void)
 {
 	power_supply_class = class_create(THIS_MODULE, "power_supply");
diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h
index 53f177d..b0f175a 100644
--- a/include/linux/power_supply.h
+++ b/include/linux/power_supply.h
@@ -193,6 +193,29 @@ struct power_supply {
 #endif
 };
 
+struct power_supply_attr_query {
+	char *src_name;
+	enum power_supply_property property;
+	enum power_supply_type type;
+	/* variable to store result */
+	union power_supply_propval res;
+};
+
+struct power_supply_attr_source {
+	const char *name;
+	enum power_supply_type type;
+	int (*get_property)(struct power_supply_attr_source *psy_attr,
+			    enum power_supply_property psp,
+			    union power_supply_propval *val);
+	int (*set_property)(struct power_supply_attr_source *psy_attr,
+			    enum power_supply_property psp,
+			    union power_supply_propval *val);
+
+	/* private */
+	struct device *dev;
+	struct list_head attr_pool;
+};
+
 /*
  * This is recommended structure to specify static power supply parameters.
  * Generic one, parametrizable for different power supplies. Power supply
@@ -216,6 +239,8 @@ extern struct power_supply *power_supply_get_by_name(char *name);
 extern void power_supply_changed(struct power_supply *psy);
 extern int power_supply_am_i_supplied(struct power_supply *psy);
 extern int power_supply_set_battery_charged(struct power_supply *psy);
+extern int power_supply_get_external_attr(
+				struct power_supply_attr_query *query);
 
 #ifdef CONFIG_POWER_SUPPLY
 extern int power_supply_is_system_supplied(void);
@@ -226,6 +251,13 @@ static inline int power_supply_is_system_supplied(void) { return -ENOSYS; }
 extern int power_supply_register(struct device *parent,
 				 struct power_supply *psy);
 extern void power_supply_unregister(struct power_supply *psy);
+
+extern int power_supply_attributes_register(struct device *parent,
+				struct power_supply_attr_source *psy_attr);
+
+extern void power_supply_attributes_unregister(
+			struct power_supply_attr_source *psy_attr);
+
 extern int power_supply_powers(struct power_supply *psy, struct device *dev);
 
 /* For APM emulation, think legacy userspace. */
-- 
1.7.0.4


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

* Re: [PATCH v2] power_supply: Added support for power supply attribute sources
  2012-07-26 15:17 [PATCH v2] power_supply: Added support for power supply attribute sources Ramakrishna Pallala
@ 2012-07-26 17:04 ` Anton Vorontsov
  2012-07-27 18:28   ` Pallala, Ramakrishna
  0 siblings, 1 reply; 5+ messages in thread
From: Anton Vorontsov @ 2012-07-26 17:04 UTC (permalink / raw)
  To: Ramakrishna Pallala; +Cc: linux-kernel

Hello Ramakrishna,

On Thu, Jul 26, 2012 at 08:47:24PM +0530, Ramakrishna Pallala wrote:
> On some platforms one driver(or HW chip) may not be able to provide all
> the necessary attributes of the power supply connected to the platform or
> may provide very limited info which can be used by core/primary drivers.
> 
> For example a temperature sensor chip placed near the battery can be used
> to report battery ambient temperature but it does not makes sense to register
> sensor driver with power supply class. Or even a ADC driver or platform
> driver may report power supply properties like voltage/current or charging
> status but registering all those driver with power supply class is not a
> practical or ideal approach.
> 
> This patch adds the generic support to register the drivers as power
> supply attribute(properties) sources and adds an interface to read
> these attributes from power supply class drivers.
> 
> If there are multiple attribute sources of the same type then caller has
> to do source selection by passing the source string in the query struct.
> 
> Signed-off-by: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
> ---
[...]
> +extern int power_supply_attributes_register(struct device *parent,
> +				struct power_supply_attr_source *psy_attr);

Can you please show some user of the new calls? If I understand
correctly, you're going to call these from sensing (ADC, or some
other) drivers, which would be very very wrong thing to do.

Thanks,

-- 
Anton Vorontsov
Email: cbouatmailru@gmail.com

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

* RE: [PATCH v2] power_supply: Added support for power supply attribute sources
  2012-07-26 17:04 ` Anton Vorontsov
@ 2012-07-27 18:28   ` Pallala, Ramakrishna
  2012-08-06  6:42     ` Pallala, Ramakrishna
  2012-08-07  4:41     ` Anton Vorontsov
  0 siblings, 2 replies; 5+ messages in thread
From: Pallala, Ramakrishna @ 2012-07-27 18:28 UTC (permalink / raw)
  To: Anton Vorontsov; +Cc: linux-kernel

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="utf-8", Size: 2030 bytes --]

Hi Anton,

> On Thu, Jul 26, 2012 at 08:47:24PM +0530, Ramakrishna Pallala wrote:
> > On some platforms one driver(or HW chip) may not be able to provide
> > all the necessary attributes of the power supply connected to the
> > platform or may provide very limited info which can be used by core/primary
> drivers.
> >
> > For example a temperature sensor chip placed near the battery can be
> > used to report battery ambient temperature but it does not makes sense
> > to register sensor driver with power supply class. Or even a ADC
> > driver or platform driver may report power supply properties like
> > voltage/current or charging status but registering all those driver
> > with power supply class is not a practical or ideal approach.
> >
> > This patch adds the generic support to register the drivers as power
> > supply attribute(properties) sources and adds an interface to read
> > these attributes from power supply class drivers.
> >
> > If there are multiple attribute sources of the same type then caller
> > has to do source selection by passing the source string in the query struct.
> >
> > Signed-off-by: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
> > ---
> [...]
> > +extern int power_supply_attributes_register(struct device *parent,
> > +				struct power_supply_attr_source *psy_attr);
> 
> Can you please show some user of the new calls? If I understand correctly,
> you're going to call these from sensing (ADC, or some
> other) drivers, which would be very very wrong thing to do.

I have submitted two patches, one on smb347_charger driver and one on max17042_battery driver
to demonstrate the use of these API's.

[PATCH] smb347_charger: Add support for battery power supply attributes registration
[PATCH] max17042_battery: add support for battery STATUS and CHARGE_TYPE

Please review and let me know your comments.

Thanks,
Ram
ÿôèº{.nÇ+‰·Ÿ®‰­†+%ŠËÿ±éݶ\x17¥Šwÿº{.nÇ+‰·¥Š{±þG«éÿŠ{ayº\x1dʇڙë,j\a­¢f£¢·hšïêÿ‘êçz_è®\x03(­éšŽŠÝ¢j"ú\x1a¶^[m§ÿÿ¾\a«þG«éÿ¢¸?™¨è­Ú&£ø§~á¶iO•æ¬z·švØ^\x14\x04\x1a¶^[m§ÿÿÃ\fÿ¶ìÿ¢¸?–I¥

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

* RE: [PATCH v2] power_supply: Added support for power supply attribute sources
  2012-07-27 18:28   ` Pallala, Ramakrishna
@ 2012-08-06  6:42     ` Pallala, Ramakrishna
  2012-08-07  4:41     ` Anton Vorontsov
  1 sibling, 0 replies; 5+ messages in thread
From: Pallala, Ramakrishna @ 2012-08-06  6:42 UTC (permalink / raw)
  To: Pallala, Ramakrishna, Anton Vorontsov; +Cc: linux-kernel

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="utf-8", Size: 2203 bytes --]

Hi Anton,

> > On Thu, Jul 26, 2012 at 08:47:24PM +0530, Ramakrishna Pallala wrote:
> > > On some platforms one driver(or HW chip) may not be able to provide
> > > all the necessary attributes of the power supply connected to the
> > > platform or may provide very limited info which can be used by
> > > core/primary
> > drivers.
> > >
> > > For example a temperature sensor chip placed near the battery can be
> > > used to report battery ambient temperature but it does not makes
> > > sense to register sensor driver with power supply class. Or even a
> > > ADC driver or platform driver may report power supply properties
> > > like voltage/current or charging status but registering all those
> > > driver with power supply class is not a practical or ideal approach.
> > >
> > > This patch adds the generic support to register the drivers as power
> > > supply attribute(properties) sources and adds an interface to read
> > > these attributes from power supply class drivers.
> > >
> > > If there are multiple attribute sources of the same type then caller
> > > has to do source selection by passing the source string in the query struct.
> > >
> > > Signed-off-by: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
> > > ---
> > [...]
> > > +extern int power_supply_attributes_register(struct device *parent,
> > > +				struct power_supply_attr_source *psy_attr);
> >
> > Can you please show some user of the new calls? If I understand
> > correctly, you're going to call these from sensing (ADC, or some
> > other) drivers, which would be very very wrong thing to do.
> 
> I have submitted two patches, one on smb347_charger driver and one on
> max17042_battery driver to demonstrate the use of these API's.
> 
> [PATCH] smb347_charger: Add support for battery power supply attributes
> registration [PATCH] max17042_battery: add support for battery STATUS and
> CHARGE_TYPE
> 
> Please review and let me know your comments.

I haven't received any inputs from you yet.
Can I assume these changes going to be merged?

Thanks,
Ram
ÿôèº{.nÇ+‰·Ÿ®‰­†+%ŠËÿ±éݶ\x17¥Šwÿº{.nÇ+‰·¥Š{±þG«éÿŠ{ayº\x1dʇڙë,j\a­¢f£¢·hšïêÿ‘êçz_è®\x03(­éšŽŠÝ¢j"ú\x1a¶^[m§ÿÿ¾\a«þG«éÿ¢¸?™¨è­Ú&£ø§~á¶iO•æ¬z·švØ^\x14\x04\x1a¶^[m§ÿÿÃ\fÿ¶ìÿ¢¸?–I¥

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

* Re: [PATCH v2] power_supply: Added support for power supply attribute sources
  2012-07-27 18:28   ` Pallala, Ramakrishna
  2012-08-06  6:42     ` Pallala, Ramakrishna
@ 2012-08-07  4:41     ` Anton Vorontsov
  1 sibling, 0 replies; 5+ messages in thread
From: Anton Vorontsov @ 2012-08-07  4:41 UTC (permalink / raw)
  To: Pallala, Ramakrishna; +Cc: linux-kernel

On Fri, Jul 27, 2012 at 06:28:31PM +0000, Pallala, Ramakrishna wrote:
[...]
> > > Signed-off-by: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
> > > ---
> > [...]
> > > +extern int power_supply_attributes_register(struct device *parent,
> > > +				struct power_supply_attr_source *psy_attr);
> > 
> > Can you please show some user of the new calls? If I understand correctly,
> > you're going to call these from sensing (ADC, or some
> > other) drivers, which would be very very wrong thing to do.
> 
> I have submitted two patches, one on smb347_charger driver and one on max17042_battery driver
> to demonstrate the use of these API's.
> 
> [PATCH] smb347_charger: Add support for battery power supply attributes registration
> [PATCH] max17042_battery: add support for battery STATUS and CHARGE_TYPE

Yeah, I noticed them, I'll review the whole approach tomorrow.

Thanks!

-- 
Anton Vorontsov
Email: cbouatmailru@gmail.com

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

end of thread, other threads:[~2012-08-07  4:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-26 15:17 [PATCH v2] power_supply: Added support for power supply attribute sources Ramakrishna Pallala
2012-07-26 17:04 ` Anton Vorontsov
2012-07-27 18:28   ` Pallala, Ramakrishna
2012-08-06  6:42     ` Pallala, Ramakrishna
2012-08-07  4:41     ` Anton Vorontsov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).