aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLiam Breck <kernel@networkimprov.net>2017-06-07 14:37:51 -0400
committerSebastian Reichel <sebastian.reichel@collabora.co.uk>2017-06-08 10:29:28 -0400
commitc08b1f45d7d193b3e6dcbbf30d403cb49b667b8c (patch)
tree9f7389a87a3d8ff991f61d70d8224adee4f1e3f8
parent230670479a6c54dcae6387119bb7d4441d7870b2 (diff)
power: supply: core: Add power_supply_battery_info and API
power_supply_get_battery_info() reads battery data from devicetree. struct power_supply_battery_info provides battery data to drivers. Its fields correspond to elements in enum power_supply_property. Drivers may surface battery data in sysfs via corresponding POWER_SUPPLY_PROP_* fields. Signed-off-by: Matt Ranostay <matt@ranostay.consulting> Signed-off-by: Liam Breck <kernel@networkimprov.net> Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.co.uk>
-rw-r--r--Documentation/power/power_supply_class.txt12
-rw-r--r--drivers/power/supply/power_supply_core.c57
-rw-r--r--include/linux/power_supply.h22
3 files changed, 91 insertions, 0 deletions
diff --git a/Documentation/power/power_supply_class.txt b/Documentation/power/power_supply_class.txt
index 0c72588bd967..01f007591070 100644
--- a/Documentation/power/power_supply_class.txt
+++ b/Documentation/power/power_supply_class.txt
@@ -174,6 +174,18 @@ issued by external power supply will notify supplicants via
174external_power_changed callback. 174external_power_changed callback.
175 175
176 176
177Devicetree battery characteristics
178~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
179Drivers should call power_supply_get_battery_info() to obtain battery
180characteristics from a devicetree battery node, defined in
181Documentation/devicetree/bindings/power/supply/battery.txt. This is
182implemented in drivers/power/supply/bq27xxx_battery.c.
183
184Properties in struct power_supply_battery_info and their counterparts in the
185battery node have names corresponding to elements in enum power_supply_property,
186for naming consistency between sysfs attributes and battery node properties.
187
188
177QA 189QA
178~~ 190~~
179Q: Where is POWER_SUPPLY_PROP_XYZ attribute? 191Q: Where is POWER_SUPPLY_PROP_XYZ attribute?
diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
index 0c09144193a6..a4303ed66144 100644
--- a/drivers/power/supply/power_supply_core.c
+++ b/drivers/power/supply/power_supply_core.c
@@ -17,6 +17,7 @@
17#include <linux/device.h> 17#include <linux/device.h>
18#include <linux/notifier.h> 18#include <linux/notifier.h>
19#include <linux/err.h> 19#include <linux/err.h>
20#include <linux/of.h>
20#include <linux/power_supply.h> 21#include <linux/power_supply.h>
21#include <linux/thermal.h> 22#include <linux/thermal.h>
22#include "power_supply.h" 23#include "power_supply.h"
@@ -519,6 +520,62 @@ struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
519EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle); 520EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
520#endif /* CONFIG_OF */ 521#endif /* CONFIG_OF */
521 522
523int power_supply_get_battery_info(struct power_supply *psy,
524 struct power_supply_battery_info *info)
525{
526 struct device_node *battery_np;
527 const char *value;
528 int err;
529
530 info->energy_full_design_uwh = -EINVAL;
531 info->charge_full_design_uah = -EINVAL;
532 info->voltage_min_design_uv = -EINVAL;
533 info->precharge_current_ua = -EINVAL;
534 info->charge_term_current_ua = -EINVAL;
535 info->constant_charge_current_max_ua = -EINVAL;
536 info->constant_charge_voltage_max_uv = -EINVAL;
537
538 if (!psy->of_node) {
539 dev_warn(&psy->dev, "%s currently only supports devicetree\n",
540 __func__);
541 return -ENXIO;
542 }
543
544 battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);
545 if (!battery_np)
546 return -ENODEV;
547
548 err = of_property_read_string(battery_np, "compatible", &value);
549 if (err)
550 return err;
551
552 if (strcmp("simple-battery", value))
553 return -ENODEV;
554
555 /* The property and field names below must correspond to elements
556 * in enum power_supply_property. For reasoning, see
557 * Documentation/power/power_supply_class.txt.
558 */
559
560 of_property_read_u32(battery_np, "energy-full-design-microwatt-hours",
561 &info->energy_full_design_uwh);
562 of_property_read_u32(battery_np, "charge-full-design-microamp-hours",
563 &info->charge_full_design_uah);
564 of_property_read_u32(battery_np, "voltage-min-design-microvolt",
565 &info->voltage_min_design_uv);
566 of_property_read_u32(battery_np, "precharge-current-microamp",
567 &info->precharge_current_ua);
568 of_property_read_u32(battery_np, "charge-term-current-microamp",
569 &info->charge_term_current_ua);
570 of_property_read_u32(battery_np, "constant_charge_current_max_microamp",
571 &info->constant_charge_current_max_ua);
572 of_property_read_u32(battery_np, "constant_charge_voltage_max_microvolt",
573 &info->constant_charge_voltage_max_uv);
574
575 return 0;
576}
577EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
578
522int power_supply_get_property(struct power_supply *psy, 579int power_supply_get_property(struct power_supply *psy,
523 enum power_supply_property psp, 580 enum power_supply_property psp,
524 union power_supply_propval *val) 581 union power_supply_propval *val)
diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h
index 4bd34051995e..34345d716286 100644
--- a/include/linux/power_supply.h
+++ b/include/linux/power_supply.h
@@ -289,6 +289,25 @@ struct power_supply_info {
289 int use_for_apm; 289 int use_for_apm;
290}; 290};
291 291
292/*
293 * This is the recommended struct to manage static battery parameters,
294 * populated by power_supply_get_battery_info(). Most platform drivers should
295 * use these for consistency.
296 * Its field names must correspond to elements in enum power_supply_property.
297 * The default field value is -EINVAL.
298 * Power supply class itself doesn't use this.
299 */
300
301struct power_supply_battery_info {
302 int energy_full_design_uwh; /* microWatt-hours */
303 int charge_full_design_uah; /* microAmp-hours */
304 int voltage_min_design_uv; /* microVolts */
305 int precharge_current_ua; /* microAmps */
306 int charge_term_current_ua; /* microAmps */
307 int constant_charge_current_max_ua; /* microAmps */
308 int constant_charge_voltage_max_uv; /* microVolts */
309};
310
292extern struct atomic_notifier_head power_supply_notifier; 311extern struct atomic_notifier_head power_supply_notifier;
293extern int power_supply_reg_notifier(struct notifier_block *nb); 312extern int power_supply_reg_notifier(struct notifier_block *nb);
294extern void power_supply_unreg_notifier(struct notifier_block *nb); 313extern void power_supply_unreg_notifier(struct notifier_block *nb);
@@ -307,6 +326,9 @@ static inline struct power_supply *
307devm_power_supply_get_by_phandle(struct device *dev, const char *property) 326devm_power_supply_get_by_phandle(struct device *dev, const char *property)
308{ return NULL; } 327{ return NULL; }
309#endif /* CONFIG_OF */ 328#endif /* CONFIG_OF */
329
330extern int power_supply_get_battery_info(struct power_supply *psy,
331 struct power_supply_battery_info *info);
310extern void power_supply_changed(struct power_supply *psy); 332extern void power_supply_changed(struct power_supply *psy);
311extern int power_supply_am_i_supplied(struct power_supply *psy); 333extern int power_supply_am_i_supplied(struct power_supply *psy);
312extern int power_supply_set_battery_charged(struct power_supply *psy); 334extern int power_supply_set_battery_charged(struct power_supply *psy);