2 * Universal power supply monitor class
4 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
5 * Copyright © 2004 Szabolcs Gyurko
6 * Copyright © 2003 Ian Molton <spyro@f2s.com>
8 * Modified: 2004, Oct Szabolcs Gyurko
10 * You may use this code as per GPL version 2
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/notifier.h>
20 #include <linux/err.h>
22 #include <linux/power_supply.h>
23 #include <linux/property.h>
24 #include <linux/thermal.h>
25 #include "power_supply.h"
27 /* exported for the APM Power driver, APM emulation */
28 struct class *power_supply_class;
29 EXPORT_SYMBOL_GPL(power_supply_class);
31 ATOMIC_NOTIFIER_HEAD(power_supply_notifier);
32 EXPORT_SYMBOL_GPL(power_supply_notifier);
34 static struct device_type power_supply_dev_type;
36 #define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10)
38 static bool __power_supply_is_supplied_by(struct power_supply *supplier,
39 struct power_supply *supply)
43 if (!supply->supplied_from && !supplier->supplied_to)
46 /* Support both supplied_to and supplied_from modes */
47 if (supply->supplied_from) {
48 if (!supplier->desc->name)
50 for (i = 0; i < supply->num_supplies; i++)
51 if (!strcmp(supplier->desc->name, supply->supplied_from[i]))
54 if (!supply->desc->name)
56 for (i = 0; i < supplier->num_supplicants; i++)
57 if (!strcmp(supplier->supplied_to[i], supply->desc->name))
64 static int __power_supply_changed_work(struct device *dev, void *data)
66 struct power_supply *psy = data;
67 struct power_supply *pst = dev_get_drvdata(dev);
69 if (__power_supply_is_supplied_by(psy, pst)) {
70 if (pst->desc->external_power_changed)
71 pst->desc->external_power_changed(pst);
77 static void power_supply_changed_work(struct work_struct *work)
80 struct power_supply *psy = container_of(work, struct power_supply,
83 dev_dbg(&psy->dev, "%s\n", __func__);
85 spin_lock_irqsave(&psy->changed_lock, flags);
87 * Check 'changed' here to avoid issues due to race between
88 * power_supply_changed() and this routine. In worst case
89 * power_supply_changed() can be called again just before we take above
90 * lock. During the first call of this routine we will mark 'changed' as
91 * false and it will stay false for the next call as well.
93 if (likely(psy->changed)) {
95 spin_unlock_irqrestore(&psy->changed_lock, flags);
96 class_for_each_device(power_supply_class, NULL, psy,
97 __power_supply_changed_work);
98 power_supply_update_leds(psy);
99 atomic_notifier_call_chain(&power_supply_notifier,
100 PSY_EVENT_PROP_CHANGED, psy);
101 kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
102 spin_lock_irqsave(&psy->changed_lock, flags);
106 * Hold the wakeup_source until all events are processed.
107 * power_supply_changed() might have called again and have set 'changed'
110 if (likely(!psy->changed))
112 spin_unlock_irqrestore(&psy->changed_lock, flags);
115 void power_supply_changed(struct power_supply *psy)
119 dev_dbg(&psy->dev, "%s\n", __func__);
121 spin_lock_irqsave(&psy->changed_lock, flags);
123 pm_stay_awake(&psy->dev);
124 spin_unlock_irqrestore(&psy->changed_lock, flags);
125 schedule_work(&psy->changed_work);
127 EXPORT_SYMBOL_GPL(power_supply_changed);
130 * Notify that power supply was registered after parent finished the probing.
132 * Often power supply is registered from driver's probe function. However
133 * calling power_supply_changed() directly from power_supply_register()
134 * would lead to execution of get_property() function provided by the driver
135 * too early - before the probe ends.
137 * Avoid that by waiting on parent's mutex.
139 static void power_supply_deferred_register_work(struct work_struct *work)
141 struct power_supply *psy = container_of(work, struct power_supply,
142 deferred_register_work.work);
144 if (psy->dev.parent) {
145 while (!mutex_trylock(&psy->dev.parent->mutex)) {
152 power_supply_changed(psy);
155 mutex_unlock(&psy->dev.parent->mutex);
159 static int __power_supply_populate_supplied_from(struct device *dev,
162 struct power_supply *psy = data;
163 struct power_supply *epsy = dev_get_drvdata(dev);
164 struct device_node *np;
168 np = of_parse_phandle(psy->of_node, "power-supplies", i++);
172 if (np == epsy->of_node) {
173 dev_info(&psy->dev, "%s: Found supply : %s\n",
174 psy->desc->name, epsy->desc->name);
175 psy->supplied_from[i-1] = (char *)epsy->desc->name;
186 static int power_supply_populate_supplied_from(struct power_supply *psy)
190 error = class_for_each_device(power_supply_class, NULL, psy,
191 __power_supply_populate_supplied_from);
193 dev_dbg(&psy->dev, "%s %d\n", __func__, error);
198 static int __power_supply_find_supply_from_node(struct device *dev,
201 struct device_node *np = data;
202 struct power_supply *epsy = dev_get_drvdata(dev);
204 /* returning non-zero breaks out of class_for_each_device loop */
205 if (epsy->of_node == np)
211 static int power_supply_find_supply_from_node(struct device_node *supply_node)
216 * class_for_each_device() either returns its own errors or values
217 * returned by __power_supply_find_supply_from_node().
219 * __power_supply_find_supply_from_node() will return 0 (no match)
222 * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
223 * it returned 0, or error as returned by it.
225 error = class_for_each_device(power_supply_class, NULL, supply_node,
226 __power_supply_find_supply_from_node);
228 return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
231 static int power_supply_check_supplies(struct power_supply *psy)
233 struct device_node *np;
236 /* If there is already a list honor it */
237 if (psy->supplied_from && psy->num_supplies > 0)
240 /* No device node found, nothing to do */
247 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
251 ret = power_supply_find_supply_from_node(np);
255 dev_dbg(&psy->dev, "Failed to find supply!\n");
260 /* Missing valid "power-supplies" entries */
264 /* All supplies found, allocate char ** array for filling */
265 psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
267 if (!psy->supplied_from)
270 *psy->supplied_from = devm_kcalloc(&psy->dev,
271 cnt - 1, sizeof(char *),
273 if (!*psy->supplied_from)
276 return power_supply_populate_supplied_from(psy);
279 static int power_supply_check_supplies(struct power_supply *psy)
283 if (!psy->dev.parent)
286 nval = device_property_read_string_array(psy->dev.parent,
287 "supplied-from", NULL, 0);
291 psy->supplied_from = devm_kmalloc_array(&psy->dev, nval,
292 sizeof(char *), GFP_KERNEL);
293 if (!psy->supplied_from)
296 ret = device_property_read_string_array(psy->dev.parent,
297 "supplied-from", (const char **)psy->supplied_from, nval);
301 psy->num_supplies = nval;
307 struct psy_am_i_supplied_data {
308 struct power_supply *psy;
312 static int __power_supply_am_i_supplied(struct device *dev, void *_data)
314 union power_supply_propval ret = {0,};
315 struct power_supply *epsy = dev_get_drvdata(dev);
316 struct psy_am_i_supplied_data *data = _data;
318 if (__power_supply_is_supplied_by(epsy, data->psy)) {
320 if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
328 int power_supply_am_i_supplied(struct power_supply *psy)
330 struct psy_am_i_supplied_data data = { psy, 0 };
333 error = class_for_each_device(power_supply_class, NULL, &data,
334 __power_supply_am_i_supplied);
336 dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
343 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
345 static int __power_supply_is_system_supplied(struct device *dev, void *data)
347 union power_supply_propval ret = {0,};
348 struct power_supply *psy = dev_get_drvdata(dev);
349 unsigned int *count = data;
352 if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
353 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
360 int power_supply_is_system_supplied(void)
363 unsigned int count = 0;
365 error = class_for_each_device(power_supply_class, NULL, &count,
366 __power_supply_is_system_supplied);
369 * If no power class device was found at all, most probably we are
370 * running on a desktop system, so assume we are on mains power.
377 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
379 static int __power_supply_get_supplier_max_current(struct device *dev,
382 union power_supply_propval ret = {0,};
383 struct power_supply *epsy = dev_get_drvdata(dev);
384 struct power_supply *psy = data;
386 if (__power_supply_is_supplied_by(epsy, psy))
387 if (!epsy->desc->get_property(epsy,
388 POWER_SUPPLY_PROP_CURRENT_MAX,
395 int power_supply_set_input_current_limit_from_supplier(struct power_supply *psy)
397 union power_supply_propval val = {0,};
400 if (!psy->desc->set_property)
404 * This function is not intended for use with a supply with multiple
405 * suppliers, we simply pick the first supply to report a non 0
408 curr = class_for_each_device(power_supply_class, NULL, psy,
409 __power_supply_get_supplier_max_current);
411 return (curr == 0) ? -ENODEV : curr;
415 return psy->desc->set_property(psy,
416 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
418 EXPORT_SYMBOL_GPL(power_supply_set_input_current_limit_from_supplier);
420 int power_supply_set_battery_charged(struct power_supply *psy)
422 if (atomic_read(&psy->use_cnt) >= 0 &&
423 psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
424 psy->desc->set_charged) {
425 psy->desc->set_charged(psy);
431 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
433 static int power_supply_match_device_by_name(struct device *dev, const void *data)
435 const char *name = data;
436 struct power_supply *psy = dev_get_drvdata(dev);
438 return strcmp(psy->desc->name, name) == 0;
442 * power_supply_get_by_name() - Search for a power supply and returns its ref
443 * @name: Power supply name to fetch
445 * If power supply was found, it increases reference count for the
446 * internal power supply's device. The user should power_supply_put()
449 * Return: On success returns a reference to a power supply with
450 * matching name equals to @name, a NULL otherwise.
452 struct power_supply *power_supply_get_by_name(const char *name)
454 struct power_supply *psy = NULL;
455 struct device *dev = class_find_device(power_supply_class, NULL, name,
456 power_supply_match_device_by_name);
459 psy = dev_get_drvdata(dev);
460 atomic_inc(&psy->use_cnt);
465 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
468 * power_supply_put() - Drop reference obtained with power_supply_get_by_name
469 * @psy: Reference to put
471 * The reference to power supply should be put before unregistering
474 void power_supply_put(struct power_supply *psy)
478 atomic_dec(&psy->use_cnt);
479 put_device(&psy->dev);
481 EXPORT_SYMBOL_GPL(power_supply_put);
484 static int power_supply_match_device_node(struct device *dev, const void *data)
486 return dev->parent && dev->parent->of_node == data;
490 * power_supply_get_by_phandle() - Search for a power supply and returns its ref
491 * @np: Pointer to device node holding phandle property
492 * @property: Name of property holding a power supply name
494 * If power supply was found, it increases reference count for the
495 * internal power supply's device. The user should power_supply_put()
498 * Return: On success returns a reference to a power supply with
499 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
501 struct power_supply *power_supply_get_by_phandle(struct device_node *np,
502 const char *property)
504 struct device_node *power_supply_np;
505 struct power_supply *psy = NULL;
508 power_supply_np = of_parse_phandle(np, property, 0);
509 if (!power_supply_np)
510 return ERR_PTR(-ENODEV);
512 dev = class_find_device(power_supply_class, NULL, power_supply_np,
513 power_supply_match_device_node);
515 of_node_put(power_supply_np);
518 psy = dev_get_drvdata(dev);
519 atomic_inc(&psy->use_cnt);
524 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
526 static void devm_power_supply_put(struct device *dev, void *res)
528 struct power_supply **psy = res;
530 power_supply_put(*psy);
534 * devm_power_supply_get_by_phandle() - Resource managed version of
535 * power_supply_get_by_phandle()
536 * @dev: Pointer to device holding phandle property
537 * @property: Name of property holding a power supply phandle
539 * Return: On success returns a reference to a power supply with
540 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
542 struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
543 const char *property)
545 struct power_supply **ptr, *psy;
548 return ERR_PTR(-ENODEV);
550 ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
552 return ERR_PTR(-ENOMEM);
554 psy = power_supply_get_by_phandle(dev->of_node, property);
555 if (IS_ERR_OR_NULL(psy)) {
559 devres_add(dev, ptr);
563 EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
564 #endif /* CONFIG_OF */
566 int power_supply_get_battery_info(struct power_supply *psy,
567 struct power_supply_battery_info *info)
569 struct device_node *battery_np;
573 info->energy_full_design_uwh = -EINVAL;
574 info->charge_full_design_uah = -EINVAL;
575 info->voltage_min_design_uv = -EINVAL;
576 info->voltage_max_design_uv = -EINVAL;
577 info->precharge_current_ua = -EINVAL;
578 info->charge_term_current_ua = -EINVAL;
579 info->constant_charge_current_max_ua = -EINVAL;
580 info->constant_charge_voltage_max_uv = -EINVAL;
581 info->factory_internal_resistance_uohm = -EINVAL;
583 for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) {
584 info->ocv_table[index] = NULL;
585 info->ocv_temp[index] = -EINVAL;
586 info->ocv_table_size[index] = -EINVAL;
590 dev_warn(&psy->dev, "%s currently only supports devicetree\n",
595 battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);
599 err = of_property_read_string(battery_np, "compatible", &value);
603 if (strcmp("simple-battery", value)) {
608 /* The property and field names below must correspond to elements
609 * in enum power_supply_property. For reasoning, see
610 * Documentation/power/power_supply_class.txt.
613 of_property_read_u32(battery_np, "energy-full-design-microwatt-hours",
614 &info->energy_full_design_uwh);
615 of_property_read_u32(battery_np, "charge-full-design-microamp-hours",
616 &info->charge_full_design_uah);
617 of_property_read_u32(battery_np, "voltage-min-design-microvolt",
618 &info->voltage_min_design_uv);
619 of_property_read_u32(battery_np, "voltage-max-design-microvolt",
620 &info->voltage_max_design_uv);
621 of_property_read_u32(battery_np, "precharge-current-microamp",
622 &info->precharge_current_ua);
623 of_property_read_u32(battery_np, "charge-term-current-microamp",
624 &info->charge_term_current_ua);
625 of_property_read_u32(battery_np, "constant-charge-current-max-microamp",
626 &info->constant_charge_current_max_ua);
627 of_property_read_u32(battery_np, "constant-charge-voltage-max-microvolt",
628 &info->constant_charge_voltage_max_uv);
629 of_property_read_u32(battery_np, "factory-internal-resistance-micro-ohms",
630 &info->factory_internal_resistance_uohm);
632 len = of_property_count_u32_elems(battery_np, "ocv-capacity-celsius");
633 if (len < 0 && len != -EINVAL) {
636 } else if (len > POWER_SUPPLY_OCV_TEMP_MAX) {
637 dev_err(&psy->dev, "Too many temperature values\n");
640 } else if (len > 0) {
641 of_property_read_u32_array(battery_np, "ocv-capacity-celsius",
642 info->ocv_temp, len);
645 for (index = 0; index < len; index++) {
646 struct power_supply_battery_ocv_table *table;
649 int i, tab_len, size;
651 propname = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d", index);
652 list = of_get_property(battery_np, propname, &size);
653 if (!list || !size) {
654 dev_err(&psy->dev, "failed to get %s\n", propname);
656 power_supply_put_battery_info(psy, info);
662 tab_len = size / (2 * sizeof(__be32));
663 info->ocv_table_size[index] = tab_len;
665 table = info->ocv_table[index] =
666 devm_kcalloc(&psy->dev, tab_len, sizeof(*table), GFP_KERNEL);
667 if (!info->ocv_table[index]) {
668 power_supply_put_battery_info(psy, info);
673 for (i = 0; i < tab_len; i++) {
674 table[i].ocv = be32_to_cpu(*list);
676 table[i].capacity = be32_to_cpu(*list);
682 of_node_put(battery_np);
685 EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
687 void power_supply_put_battery_info(struct power_supply *psy,
688 struct power_supply_battery_info *info)
692 for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
693 if (info->ocv_table[i])
694 devm_kfree(&psy->dev, info->ocv_table[i]);
697 EXPORT_SYMBOL_GPL(power_supply_put_battery_info);
700 * power_supply_ocv2cap_simple() - find the battery capacity
701 * @table: Pointer to battery OCV lookup table
702 * @table_len: OCV table length
703 * @ocv: Current OCV value
705 * This helper function is used to look up battery capacity according to
706 * current OCV value from one OCV table, and the OCV table must be ordered
709 * Return: the battery capacity.
711 int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table,
712 int table_len, int ocv)
716 for (i = 0; i < table_len; i++)
717 if (ocv > table[i].ocv)
720 if (i > 0 && i < table_len) {
721 tmp = (table[i - 1].capacity - table[i].capacity) *
722 (ocv - table[i].ocv);
723 tmp /= table[i - 1].ocv - table[i].ocv;
724 cap = tmp + table[i].capacity;
726 cap = table[0].capacity;
728 cap = table[table_len - 1].capacity;
733 EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple);
735 struct power_supply_battery_ocv_table *
736 power_supply_find_ocv2cap_table(struct power_supply_battery_info *info,
737 int temp, int *table_len)
739 int best_temp_diff = INT_MAX, temp_diff;
740 u8 i, best_index = 0;
742 if (!info->ocv_table[0])
745 for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
746 temp_diff = abs(info->ocv_temp[i] - temp);
748 if (temp_diff < best_temp_diff) {
749 best_temp_diff = temp_diff;
754 *table_len = info->ocv_table_size[best_index];
755 return info->ocv_table[best_index];
757 EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table);
759 int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info,
762 struct power_supply_battery_ocv_table *table;
765 table = power_supply_find_ocv2cap_table(info, temp, &table_len);
769 return power_supply_ocv2cap_simple(table, table_len, ocv);
771 EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap);
773 int power_supply_get_property(struct power_supply *psy,
774 enum power_supply_property psp,
775 union power_supply_propval *val)
777 if (atomic_read(&psy->use_cnt) <= 0) {
778 if (!psy->initialized)
783 return psy->desc->get_property(psy, psp, val);
785 EXPORT_SYMBOL_GPL(power_supply_get_property);
787 int power_supply_set_property(struct power_supply *psy,
788 enum power_supply_property psp,
789 const union power_supply_propval *val)
791 if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
794 return psy->desc->set_property(psy, psp, val);
796 EXPORT_SYMBOL_GPL(power_supply_set_property);
798 int power_supply_property_is_writeable(struct power_supply *psy,
799 enum power_supply_property psp)
801 if (atomic_read(&psy->use_cnt) <= 0 ||
802 !psy->desc->property_is_writeable)
805 return psy->desc->property_is_writeable(psy, psp);
807 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
809 void power_supply_external_power_changed(struct power_supply *psy)
811 if (atomic_read(&psy->use_cnt) <= 0 ||
812 !psy->desc->external_power_changed)
815 psy->desc->external_power_changed(psy);
817 EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
819 int power_supply_powers(struct power_supply *psy, struct device *dev)
821 return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
823 EXPORT_SYMBOL_GPL(power_supply_powers);
825 static void power_supply_dev_release(struct device *dev)
827 struct power_supply *psy = to_power_supply(dev);
828 dev_dbg(dev, "%s\n", __func__);
832 int power_supply_reg_notifier(struct notifier_block *nb)
834 return atomic_notifier_chain_register(&power_supply_notifier, nb);
836 EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
838 void power_supply_unreg_notifier(struct notifier_block *nb)
840 atomic_notifier_chain_unregister(&power_supply_notifier, nb);
842 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
844 #ifdef CONFIG_THERMAL
845 static int power_supply_read_temp(struct thermal_zone_device *tzd,
848 struct power_supply *psy;
849 union power_supply_propval val;
852 WARN_ON(tzd == NULL);
854 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
858 /* Convert tenths of degree Celsius to milli degree Celsius. */
859 *temp = val.intval * 100;
864 static struct thermal_zone_device_ops psy_tzd_ops = {
865 .get_temp = power_supply_read_temp,
868 static int psy_register_thermal(struct power_supply *psy)
872 if (psy->desc->no_thermal)
875 /* Register battery zone device psy reports temperature */
876 for (i = 0; i < psy->desc->num_properties; i++) {
877 if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
878 psy->tzd = thermal_zone_device_register(psy->desc->name,
879 0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
880 return PTR_ERR_OR_ZERO(psy->tzd);
886 static void psy_unregister_thermal(struct power_supply *psy)
888 if (IS_ERR_OR_NULL(psy->tzd))
890 thermal_zone_device_unregister(psy->tzd);
893 /* thermal cooling device callbacks */
894 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
895 unsigned long *state)
897 struct power_supply *psy;
898 union power_supply_propval val;
902 ret = power_supply_get_property(psy,
903 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
912 static int ps_get_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
913 unsigned long *state)
915 struct power_supply *psy;
916 union power_supply_propval val;
920 ret = power_supply_get_property(psy,
921 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
930 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
933 struct power_supply *psy;
934 union power_supply_propval val;
939 ret = psy->desc->set_property(psy,
940 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
945 static const struct thermal_cooling_device_ops psy_tcd_ops = {
946 .get_max_state = ps_get_max_charge_cntl_limit,
947 .get_cur_state = ps_get_cur_charge_cntl_limit,
948 .set_cur_state = ps_set_cur_charge_cntl_limit,
951 static int psy_register_cooler(struct power_supply *psy)
955 /* Register for cooling device if psy can control charging */
956 for (i = 0; i < psy->desc->num_properties; i++) {
957 if (psy->desc->properties[i] ==
958 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
959 psy->tcd = thermal_cooling_device_register(
960 (char *)psy->desc->name,
962 return PTR_ERR_OR_ZERO(psy->tcd);
968 static void psy_unregister_cooler(struct power_supply *psy)
970 if (IS_ERR_OR_NULL(psy->tcd))
972 thermal_cooling_device_unregister(psy->tcd);
975 static int psy_register_thermal(struct power_supply *psy)
980 static void psy_unregister_thermal(struct power_supply *psy)
984 static int psy_register_cooler(struct power_supply *psy)
989 static void psy_unregister_cooler(struct power_supply *psy)
994 static struct power_supply *__must_check
995 __power_supply_register(struct device *parent,
996 const struct power_supply_desc *desc,
997 const struct power_supply_config *cfg,
1001 struct power_supply *psy;
1005 pr_warn("%s: Expected proper parent device for '%s'\n",
1006 __func__, desc->name);
1008 if (!desc || !desc->name || !desc->properties || !desc->num_properties)
1009 return ERR_PTR(-EINVAL);
1011 for (i = 0; i < desc->num_properties; ++i) {
1012 if ((desc->properties[i] == POWER_SUPPLY_PROP_USB_TYPE) &&
1013 (!desc->usb_types || !desc->num_usb_types))
1014 return ERR_PTR(-EINVAL);
1017 psy = kzalloc(sizeof(*psy), GFP_KERNEL);
1019 return ERR_PTR(-ENOMEM);
1023 device_initialize(dev);
1025 dev->class = power_supply_class;
1026 dev->type = &power_supply_dev_type;
1027 dev->parent = parent;
1028 dev->release = power_supply_dev_release;
1029 dev_set_drvdata(dev, psy);
1032 dev->groups = cfg->attr_grp;
1033 psy->drv_data = cfg->drv_data;
1035 cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node;
1036 psy->supplied_to = cfg->supplied_to;
1037 psy->num_supplicants = cfg->num_supplicants;
1040 rc = dev_set_name(dev, "%s", desc->name);
1042 goto dev_set_name_failed;
1044 INIT_WORK(&psy->changed_work, power_supply_changed_work);
1045 INIT_DELAYED_WORK(&psy->deferred_register_work,
1046 power_supply_deferred_register_work);
1048 rc = power_supply_check_supplies(psy);
1050 dev_info(dev, "Not all required supplies found, defer probe\n");
1051 goto check_supplies_failed;
1054 spin_lock_init(&psy->changed_lock);
1055 rc = device_init_wakeup(dev, ws);
1057 goto wakeup_init_failed;
1059 rc = device_add(dev);
1061 goto device_add_failed;
1063 rc = psy_register_thermal(psy);
1065 goto register_thermal_failed;
1067 rc = psy_register_cooler(psy);
1069 goto register_cooler_failed;
1071 rc = power_supply_create_triggers(psy);
1073 goto create_triggers_failed;
1076 * Update use_cnt after any uevents (most notably from device_add()).
1077 * We are here still during driver's probe but
1078 * the power_supply_uevent() calls back driver's get_property
1080 * 1. Driver did not assigned the returned struct power_supply,
1081 * 2. Driver could not finish initialization (anything in its probe
1082 * after calling power_supply_register()).
1084 atomic_inc(&psy->use_cnt);
1085 psy->initialized = true;
1087 queue_delayed_work(system_power_efficient_wq,
1088 &psy->deferred_register_work,
1089 POWER_SUPPLY_DEFERRED_REGISTER_TIME);
1093 create_triggers_failed:
1094 psy_unregister_cooler(psy);
1095 register_cooler_failed:
1096 psy_unregister_thermal(psy);
1097 register_thermal_failed:
1101 check_supplies_failed:
1102 dev_set_name_failed:
1108 * power_supply_register() - Register new power supply
1109 * @parent: Device to be a parent of power supply's device, usually
1110 * the device which probe function calls this
1111 * @desc: Description of power supply, must be valid through whole
1112 * lifetime of this power supply
1113 * @cfg: Run-time specific configuration accessed during registering,
1116 * Return: A pointer to newly allocated power_supply on success
1117 * or ERR_PTR otherwise.
1118 * Use power_supply_unregister() on returned power_supply pointer to release
1121 struct power_supply *__must_check power_supply_register(struct device *parent,
1122 const struct power_supply_desc *desc,
1123 const struct power_supply_config *cfg)
1125 return __power_supply_register(parent, desc, cfg, true);
1127 EXPORT_SYMBOL_GPL(power_supply_register);
1130 * power_supply_register_no_ws() - Register new non-waking-source power supply
1131 * @parent: Device to be a parent of power supply's device, usually
1132 * the device which probe function calls this
1133 * @desc: Description of power supply, must be valid through whole
1134 * lifetime of this power supply
1135 * @cfg: Run-time specific configuration accessed during registering,
1138 * Return: A pointer to newly allocated power_supply on success
1139 * or ERR_PTR otherwise.
1140 * Use power_supply_unregister() on returned power_supply pointer to release
1143 struct power_supply *__must_check
1144 power_supply_register_no_ws(struct device *parent,
1145 const struct power_supply_desc *desc,
1146 const struct power_supply_config *cfg)
1148 return __power_supply_register(parent, desc, cfg, false);
1150 EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
1152 static void devm_power_supply_release(struct device *dev, void *res)
1154 struct power_supply **psy = res;
1156 power_supply_unregister(*psy);
1160 * devm_power_supply_register() - Register managed power supply
1161 * @parent: Device to be a parent of power supply's device, usually
1162 * the device which probe function calls this
1163 * @desc: Description of power supply, must be valid through whole
1164 * lifetime of this power supply
1165 * @cfg: Run-time specific configuration accessed during registering,
1168 * Return: A pointer to newly allocated power_supply on success
1169 * or ERR_PTR otherwise.
1170 * The returned power_supply pointer will be automatically unregistered
1173 struct power_supply *__must_check
1174 devm_power_supply_register(struct device *parent,
1175 const struct power_supply_desc *desc,
1176 const struct power_supply_config *cfg)
1178 struct power_supply **ptr, *psy;
1180 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1183 return ERR_PTR(-ENOMEM);
1184 psy = __power_supply_register(parent, desc, cfg, true);
1189 devres_add(parent, ptr);
1193 EXPORT_SYMBOL_GPL(devm_power_supply_register);
1196 * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
1197 * @parent: Device to be a parent of power supply's device, usually
1198 * the device which probe function calls this
1199 * @desc: Description of power supply, must be valid through whole
1200 * lifetime of this power supply
1201 * @cfg: Run-time specific configuration accessed during registering,
1204 * Return: A pointer to newly allocated power_supply on success
1205 * or ERR_PTR otherwise.
1206 * The returned power_supply pointer will be automatically unregistered
1209 struct power_supply *__must_check
1210 devm_power_supply_register_no_ws(struct device *parent,
1211 const struct power_supply_desc *desc,
1212 const struct power_supply_config *cfg)
1214 struct power_supply **ptr, *psy;
1216 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1219 return ERR_PTR(-ENOMEM);
1220 psy = __power_supply_register(parent, desc, cfg, false);
1225 devres_add(parent, ptr);
1229 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
1232 * power_supply_unregister() - Remove this power supply from system
1233 * @psy: Pointer to power supply to unregister
1235 * Remove this power supply from the system. The resources of power supply
1236 * will be freed here or on last power_supply_put() call.
1238 void power_supply_unregister(struct power_supply *psy)
1240 WARN_ON(atomic_dec_return(&psy->use_cnt));
1241 psy->removing = true;
1242 cancel_work_sync(&psy->changed_work);
1243 cancel_delayed_work_sync(&psy->deferred_register_work);
1244 sysfs_remove_link(&psy->dev.kobj, "powers");
1245 power_supply_remove_triggers(psy);
1246 psy_unregister_cooler(psy);
1247 psy_unregister_thermal(psy);
1248 device_init_wakeup(&psy->dev, false);
1249 device_unregister(&psy->dev);
1251 EXPORT_SYMBOL_GPL(power_supply_unregister);
1253 void *power_supply_get_drvdata(struct power_supply *psy)
1255 return psy->drv_data;
1257 EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
1259 static int __init power_supply_class_init(void)
1261 power_supply_class = class_create(THIS_MODULE, "power_supply");
1263 if (IS_ERR(power_supply_class))
1264 return PTR_ERR(power_supply_class);
1266 power_supply_class->dev_uevent = power_supply_uevent;
1267 power_supply_init_attrs(&power_supply_dev_type);
1272 static void __exit power_supply_class_exit(void)
1274 class_destroy(power_supply_class);
1277 subsys_initcall(power_supply_class_init);
1278 module_exit(power_supply_class_exit);
1280 MODULE_DESCRIPTION("Universal power supply monitor class");
1281 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
1283 "Anton Vorontsov <cbou@mail.ru>");
1284 MODULE_LICENSE("GPL");