Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / power / supply / power_supply_core.c
blob16085eff008442ecd04016cd12c2854a0230d9c6
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Universal power supply monitor class
5 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
6 * Copyright © 2004 Szabolcs Gyurko
7 * Copyright © 2003 Ian Molton <spyro@f2s.com>
9 * Modified: 2004, Oct Szabolcs Gyurko
12 #include <linux/cleanup.h>
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>
21 #include <linux/of.h>
22 #include <linux/power_supply.h>
23 #include <linux/property.h>
24 #include <linux/thermal.h>
25 #include <linux/fixp-arith.h>
26 #include "power_supply.h"
27 #include "samsung-sdi-battery.h"
29 static const struct class power_supply_class = {
30 .name = "power_supply",
31 .dev_uevent = power_supply_uevent,
34 static BLOCKING_NOTIFIER_HEAD(power_supply_notifier);
36 static const struct device_type power_supply_dev_type = {
37 .name = "power_supply",
38 .groups = power_supply_attr_groups,
41 #define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10)
43 static bool __power_supply_is_supplied_by(struct power_supply *supplier,
44 struct power_supply *supply)
46 int i;
48 if (!supply->supplied_from && !supplier->supplied_to)
49 return false;
51 /* Support both supplied_to and supplied_from modes */
52 if (supply->supplied_from) {
53 if (!supplier->desc->name)
54 return false;
55 for (i = 0; i < supply->num_supplies; i++)
56 if (!strcmp(supplier->desc->name, supply->supplied_from[i]))
57 return true;
58 } else {
59 if (!supply->desc->name)
60 return false;
61 for (i = 0; i < supplier->num_supplicants; i++)
62 if (!strcmp(supplier->supplied_to[i], supply->desc->name))
63 return true;
66 return false;
69 static int __power_supply_changed_work(struct device *dev, void *data)
71 struct power_supply *psy = data;
72 struct power_supply *pst = dev_get_drvdata(dev);
74 if (__power_supply_is_supplied_by(psy, pst)) {
75 if (pst->desc->external_power_changed)
76 pst->desc->external_power_changed(pst);
79 return 0;
82 static void power_supply_changed_work(struct work_struct *work)
84 unsigned long flags;
85 struct power_supply *psy = container_of(work, struct power_supply,
86 changed_work);
88 dev_dbg(&psy->dev, "%s\n", __func__);
90 spin_lock_irqsave(&psy->changed_lock, flags);
92 * Check 'changed' here to avoid issues due to race between
93 * power_supply_changed() and this routine. In worst case
94 * power_supply_changed() can be called again just before we take above
95 * lock. During the first call of this routine we will mark 'changed' as
96 * false and it will stay false for the next call as well.
98 if (likely(psy->changed)) {
99 psy->changed = false;
100 spin_unlock_irqrestore(&psy->changed_lock, flags);
101 power_supply_for_each_device(psy, __power_supply_changed_work);
102 power_supply_update_leds(psy);
103 blocking_notifier_call_chain(&power_supply_notifier,
104 PSY_EVENT_PROP_CHANGED, psy);
105 kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
106 spin_lock_irqsave(&psy->changed_lock, flags);
110 * Hold the wakeup_source until all events are processed.
111 * power_supply_changed() might have called again and have set 'changed'
112 * to true.
114 if (likely(!psy->changed))
115 pm_relax(&psy->dev);
116 spin_unlock_irqrestore(&psy->changed_lock, flags);
119 int power_supply_for_each_device(void *data, int (*fn)(struct device *dev, void *data))
121 return class_for_each_device(&power_supply_class, NULL, data, fn);
123 EXPORT_SYMBOL_GPL(power_supply_for_each_device);
125 void power_supply_changed(struct power_supply *psy)
127 unsigned long flags;
129 dev_dbg(&psy->dev, "%s\n", __func__);
131 spin_lock_irqsave(&psy->changed_lock, flags);
132 psy->changed = true;
133 pm_stay_awake(&psy->dev);
134 spin_unlock_irqrestore(&psy->changed_lock, flags);
135 schedule_work(&psy->changed_work);
137 EXPORT_SYMBOL_GPL(power_supply_changed);
140 * Notify that power supply was registered after parent finished the probing.
142 * Often power supply is registered from driver's probe function. However
143 * calling power_supply_changed() directly from power_supply_register()
144 * would lead to execution of get_property() function provided by the driver
145 * too early - before the probe ends.
147 * Avoid that by waiting on parent's mutex.
149 static void power_supply_deferred_register_work(struct work_struct *work)
151 struct power_supply *psy = container_of(work, struct power_supply,
152 deferred_register_work.work);
154 if (psy->dev.parent) {
155 while (!device_trylock(psy->dev.parent)) {
156 if (psy->removing)
157 return;
158 msleep(10);
162 power_supply_changed(psy);
164 if (psy->dev.parent)
165 device_unlock(psy->dev.parent);
168 #ifdef CONFIG_OF
169 static int __power_supply_populate_supplied_from(struct device *dev,
170 void *data)
172 struct power_supply *psy = data;
173 struct power_supply *epsy = dev_get_drvdata(dev);
174 struct device_node *np;
175 int i = 0;
177 do {
178 np = of_parse_phandle(psy->of_node, "power-supplies", i++);
179 if (!np)
180 break;
182 if (np == epsy->of_node) {
183 dev_dbg(&psy->dev, "%s: Found supply : %s\n",
184 psy->desc->name, epsy->desc->name);
185 psy->supplied_from[i-1] = (char *)epsy->desc->name;
186 psy->num_supplies++;
187 of_node_put(np);
188 break;
190 of_node_put(np);
191 } while (np);
193 return 0;
196 static int power_supply_populate_supplied_from(struct power_supply *psy)
198 int error;
200 error = power_supply_for_each_device(psy, __power_supply_populate_supplied_from);
202 dev_dbg(&psy->dev, "%s %d\n", __func__, error);
204 return error;
207 static int __power_supply_find_supply_from_node(struct device *dev,
208 void *data)
210 struct device_node *np = data;
211 struct power_supply *epsy = dev_get_drvdata(dev);
213 /* returning non-zero breaks out of power_supply_for_each_device loop */
214 if (epsy->of_node == np)
215 return 1;
217 return 0;
220 static int power_supply_find_supply_from_node(struct device_node *supply_node)
222 int error;
225 * power_supply_for_each_device() either returns its own errors or values
226 * returned by __power_supply_find_supply_from_node().
228 * __power_supply_find_supply_from_node() will return 0 (no match)
229 * or 1 (match).
231 * We return 0 if power_supply_for_each_device() returned 1, -EPROBE_DEFER if
232 * it returned 0, or error as returned by it.
234 error = power_supply_for_each_device(supply_node, __power_supply_find_supply_from_node);
236 return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
239 static int power_supply_check_supplies(struct power_supply *psy)
241 struct device_node *np;
242 int cnt = 0;
244 /* If there is already a list honor it */
245 if (psy->supplied_from && psy->num_supplies > 0)
246 return 0;
248 /* No device node found, nothing to do */
249 if (!psy->of_node)
250 return 0;
252 do {
253 int ret;
255 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
256 if (!np)
257 break;
259 ret = power_supply_find_supply_from_node(np);
260 of_node_put(np);
262 if (ret) {
263 dev_dbg(&psy->dev, "Failed to find supply!\n");
264 return ret;
266 } while (np);
268 /* Missing valid "power-supplies" entries */
269 if (cnt == 1)
270 return 0;
272 /* All supplies found, allocate char ** array for filling */
273 psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(*psy->supplied_from),
274 GFP_KERNEL);
275 if (!psy->supplied_from)
276 return -ENOMEM;
278 *psy->supplied_from = devm_kcalloc(&psy->dev,
279 cnt - 1, sizeof(**psy->supplied_from),
280 GFP_KERNEL);
281 if (!*psy->supplied_from)
282 return -ENOMEM;
284 return power_supply_populate_supplied_from(psy);
286 #else
287 static int power_supply_check_supplies(struct power_supply *psy)
289 int nval, ret;
291 if (!psy->dev.parent)
292 return 0;
294 nval = device_property_string_array_count(psy->dev.parent, "supplied-from");
295 if (nval <= 0)
296 return 0;
298 psy->supplied_from = devm_kmalloc_array(&psy->dev, nval,
299 sizeof(char *), GFP_KERNEL);
300 if (!psy->supplied_from)
301 return -ENOMEM;
303 ret = device_property_read_string_array(psy->dev.parent,
304 "supplied-from", (const char **)psy->supplied_from, nval);
305 if (ret < 0)
306 return ret;
308 psy->num_supplies = nval;
310 return 0;
312 #endif
314 struct psy_am_i_supplied_data {
315 struct power_supply *psy;
316 unsigned int count;
319 static int __power_supply_am_i_supplied(struct device *dev, void *_data)
321 union power_supply_propval ret = {0,};
322 struct power_supply *epsy = dev_get_drvdata(dev);
323 struct psy_am_i_supplied_data *data = _data;
325 if (__power_supply_is_supplied_by(epsy, data->psy)) {
326 data->count++;
327 if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
328 &ret))
329 return ret.intval;
332 return 0;
335 int power_supply_am_i_supplied(struct power_supply *psy)
337 struct psy_am_i_supplied_data data = { psy, 0 };
338 int error;
340 error = power_supply_for_each_device(&data, __power_supply_am_i_supplied);
342 dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
344 if (data.count == 0)
345 return -ENODEV;
347 return error;
349 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
351 static int __power_supply_is_system_supplied(struct device *dev, void *data)
353 union power_supply_propval ret = {0,};
354 struct power_supply *psy = dev_get_drvdata(dev);
355 unsigned int *count = data;
357 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_SCOPE, &ret))
358 if (ret.intval == POWER_SUPPLY_SCOPE_DEVICE)
359 return 0;
361 (*count)++;
362 if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
363 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
364 &ret))
365 return ret.intval;
367 return 0;
370 int power_supply_is_system_supplied(void)
372 int error;
373 unsigned int count = 0;
375 error = power_supply_for_each_device(&count, __power_supply_is_system_supplied);
378 * If no system scope power class device was found at all, most probably we
379 * are running on a desktop system, so assume we are on mains power.
381 if (count == 0)
382 return 1;
384 return error;
386 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
388 struct psy_get_supplier_prop_data {
389 struct power_supply *psy;
390 enum power_supply_property psp;
391 union power_supply_propval *val;
394 static int __power_supply_get_supplier_property(struct device *dev, void *_data)
396 struct power_supply *epsy = dev_get_drvdata(dev);
397 struct psy_get_supplier_prop_data *data = _data;
399 if (__power_supply_is_supplied_by(epsy, data->psy))
400 if (!power_supply_get_property(epsy, data->psp, data->val))
401 return 1; /* Success */
403 return 0; /* Continue iterating */
406 int power_supply_get_property_from_supplier(struct power_supply *psy,
407 enum power_supply_property psp,
408 union power_supply_propval *val)
410 struct psy_get_supplier_prop_data data = {
411 .psy = psy,
412 .psp = psp,
413 .val = val,
415 int ret;
418 * This function is not intended for use with a supply with multiple
419 * suppliers, we simply pick the first supply to report the psp.
421 ret = power_supply_for_each_device(&data, __power_supply_get_supplier_property);
422 if (ret < 0)
423 return ret;
424 if (ret == 0)
425 return -ENODEV;
427 return 0;
429 EXPORT_SYMBOL_GPL(power_supply_get_property_from_supplier);
431 int power_supply_set_battery_charged(struct power_supply *psy)
433 if (atomic_read(&psy->use_cnt) >= 0 &&
434 psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
435 psy->desc->set_charged) {
436 psy->desc->set_charged(psy);
437 return 0;
440 return -EINVAL;
442 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
444 static int power_supply_match_device_by_name(struct device *dev, const void *data)
446 const char *name = data;
447 struct power_supply *psy = dev_get_drvdata(dev);
449 return strcmp(psy->desc->name, name) == 0;
453 * power_supply_get_by_name() - Search for a power supply and returns its ref
454 * @name: Power supply name to fetch
456 * If power supply was found, it increases reference count for the
457 * internal power supply's device. The user should power_supply_put()
458 * after usage.
460 * Return: On success returns a reference to a power supply with
461 * matching name equals to @name, a NULL otherwise.
463 struct power_supply *power_supply_get_by_name(const char *name)
465 struct power_supply *psy = NULL;
466 struct device *dev = class_find_device(&power_supply_class, NULL, name,
467 power_supply_match_device_by_name);
469 if (dev) {
470 psy = dev_get_drvdata(dev);
471 atomic_inc(&psy->use_cnt);
474 return psy;
476 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
479 * power_supply_put() - Drop reference obtained with power_supply_get_by_name
480 * @psy: Reference to put
482 * The reference to power supply should be put before unregistering
483 * the power supply.
485 void power_supply_put(struct power_supply *psy)
487 atomic_dec(&psy->use_cnt);
488 put_device(&psy->dev);
490 EXPORT_SYMBOL_GPL(power_supply_put);
492 #ifdef CONFIG_OF
493 static int power_supply_match_device_node(struct device *dev, const void *data)
495 return dev->parent && dev->parent->of_node == data;
499 * power_supply_get_by_phandle() - Search for a power supply and returns its ref
500 * @np: Pointer to device node holding phandle property
501 * @property: Name of property holding a power supply name
503 * If power supply was found, it increases reference count for the
504 * internal power supply's device. The user should power_supply_put()
505 * after usage.
507 * Return: On success returns a reference to a power supply with
508 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
510 struct power_supply *power_supply_get_by_phandle(struct device_node *np,
511 const char *property)
513 struct device_node *power_supply_np;
514 struct power_supply *psy = NULL;
515 struct device *dev;
517 power_supply_np = of_parse_phandle(np, property, 0);
518 if (!power_supply_np)
519 return ERR_PTR(-ENODEV);
521 dev = class_find_device(&power_supply_class, NULL, power_supply_np,
522 power_supply_match_device_node);
524 of_node_put(power_supply_np);
526 if (dev) {
527 psy = dev_get_drvdata(dev);
528 atomic_inc(&psy->use_cnt);
531 return psy;
533 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
535 static void devm_power_supply_put(struct device *dev, void *res)
537 struct power_supply **psy = res;
539 power_supply_put(*psy);
543 * devm_power_supply_get_by_phandle() - Resource managed version of
544 * power_supply_get_by_phandle()
545 * @dev: Pointer to device holding phandle property
546 * @property: Name of property holding a power supply phandle
548 * Return: On success returns a reference to a power supply with
549 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
551 struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
552 const char *property)
554 struct power_supply **ptr, *psy;
556 if (!dev->of_node)
557 return ERR_PTR(-ENODEV);
559 ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
560 if (!ptr)
561 return ERR_PTR(-ENOMEM);
563 psy = power_supply_get_by_phandle(dev->of_node, property);
564 if (IS_ERR_OR_NULL(psy)) {
565 devres_free(ptr);
566 } else {
567 *ptr = psy;
568 devres_add(dev, ptr);
570 return psy;
572 EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
573 #endif /* CONFIG_OF */
575 int power_supply_get_battery_info(struct power_supply *psy,
576 struct power_supply_battery_info **info_out)
578 struct power_supply_resistance_temp_table *resist_table;
579 struct power_supply_battery_info *info;
580 struct device_node *battery_np = NULL;
581 struct fwnode_reference_args args;
582 struct fwnode_handle *fwnode = NULL;
583 const char *value;
584 int err, len, index;
585 const __be32 *list;
586 u32 min_max[2];
588 if (psy->of_node) {
589 battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);
590 if (!battery_np)
591 return -ENODEV;
593 fwnode = fwnode_handle_get(of_fwnode_handle(battery_np));
594 } else if (psy->dev.parent) {
595 err = fwnode_property_get_reference_args(
596 dev_fwnode(psy->dev.parent),
597 "monitored-battery", NULL, 0, 0, &args);
598 if (err)
599 return err;
601 fwnode = args.fwnode;
604 if (!fwnode)
605 return -ENOENT;
607 err = fwnode_property_read_string(fwnode, "compatible", &value);
608 if (err)
609 goto out_put_node;
612 /* Try static batteries first */
613 err = samsung_sdi_battery_get_info(&psy->dev, value, &info);
614 if (!err)
615 goto out_ret_pointer;
616 else if (err == -ENODEV)
618 * Device does not have a static battery.
619 * Proceed to look for a simple battery.
621 err = 0;
623 if (strcmp("simple-battery", value)) {
624 err = -ENODEV;
625 goto out_put_node;
628 info = devm_kzalloc(&psy->dev, sizeof(*info), GFP_KERNEL);
629 if (!info) {
630 err = -ENOMEM;
631 goto out_put_node;
634 info->technology = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
635 info->energy_full_design_uwh = -EINVAL;
636 info->charge_full_design_uah = -EINVAL;
637 info->voltage_min_design_uv = -EINVAL;
638 info->voltage_max_design_uv = -EINVAL;
639 info->precharge_current_ua = -EINVAL;
640 info->charge_term_current_ua = -EINVAL;
641 info->constant_charge_current_max_ua = -EINVAL;
642 info->constant_charge_voltage_max_uv = -EINVAL;
643 info->tricklecharge_current_ua = -EINVAL;
644 info->precharge_voltage_max_uv = -EINVAL;
645 info->charge_restart_voltage_uv = -EINVAL;
646 info->overvoltage_limit_uv = -EINVAL;
647 info->maintenance_charge = NULL;
648 info->alert_low_temp_charge_current_ua = -EINVAL;
649 info->alert_low_temp_charge_voltage_uv = -EINVAL;
650 info->alert_high_temp_charge_current_ua = -EINVAL;
651 info->alert_high_temp_charge_voltage_uv = -EINVAL;
652 info->temp_ambient_alert_min = INT_MIN;
653 info->temp_ambient_alert_max = INT_MAX;
654 info->temp_alert_min = INT_MIN;
655 info->temp_alert_max = INT_MAX;
656 info->temp_min = INT_MIN;
657 info->temp_max = INT_MAX;
658 info->factory_internal_resistance_uohm = -EINVAL;
659 info->resist_table = NULL;
660 info->bti_resistance_ohm = -EINVAL;
661 info->bti_resistance_tolerance = -EINVAL;
663 for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) {
664 info->ocv_table[index] = NULL;
665 info->ocv_temp[index] = -EINVAL;
666 info->ocv_table_size[index] = -EINVAL;
669 /* The property and field names below must correspond to elements
670 * in enum power_supply_property. For reasoning, see
671 * Documentation/power/power_supply_class.rst.
674 if (!fwnode_property_read_string(fwnode, "device-chemistry", &value)) {
675 if (!strcmp("nickel-cadmium", value))
676 info->technology = POWER_SUPPLY_TECHNOLOGY_NiCd;
677 else if (!strcmp("nickel-metal-hydride", value))
678 info->technology = POWER_SUPPLY_TECHNOLOGY_NiMH;
679 else if (!strcmp("lithium-ion", value))
680 /* Imprecise lithium-ion type */
681 info->technology = POWER_SUPPLY_TECHNOLOGY_LION;
682 else if (!strcmp("lithium-ion-polymer", value))
683 info->technology = POWER_SUPPLY_TECHNOLOGY_LIPO;
684 else if (!strcmp("lithium-ion-iron-phosphate", value))
685 info->technology = POWER_SUPPLY_TECHNOLOGY_LiFe;
686 else if (!strcmp("lithium-ion-manganese-oxide", value))
687 info->technology = POWER_SUPPLY_TECHNOLOGY_LiMn;
688 else
689 dev_warn(&psy->dev, "%s unknown battery type\n", value);
692 fwnode_property_read_u32(fwnode, "energy-full-design-microwatt-hours",
693 &info->energy_full_design_uwh);
694 fwnode_property_read_u32(fwnode, "charge-full-design-microamp-hours",
695 &info->charge_full_design_uah);
696 fwnode_property_read_u32(fwnode, "voltage-min-design-microvolt",
697 &info->voltage_min_design_uv);
698 fwnode_property_read_u32(fwnode, "voltage-max-design-microvolt",
699 &info->voltage_max_design_uv);
700 fwnode_property_read_u32(fwnode, "trickle-charge-current-microamp",
701 &info->tricklecharge_current_ua);
702 fwnode_property_read_u32(fwnode, "precharge-current-microamp",
703 &info->precharge_current_ua);
704 fwnode_property_read_u32(fwnode, "precharge-upper-limit-microvolt",
705 &info->precharge_voltage_max_uv);
706 fwnode_property_read_u32(fwnode, "charge-term-current-microamp",
707 &info->charge_term_current_ua);
708 fwnode_property_read_u32(fwnode, "re-charge-voltage-microvolt",
709 &info->charge_restart_voltage_uv);
710 fwnode_property_read_u32(fwnode, "over-voltage-threshold-microvolt",
711 &info->overvoltage_limit_uv);
712 fwnode_property_read_u32(fwnode, "constant-charge-current-max-microamp",
713 &info->constant_charge_current_max_ua);
714 fwnode_property_read_u32(fwnode, "constant-charge-voltage-max-microvolt",
715 &info->constant_charge_voltage_max_uv);
716 fwnode_property_read_u32(fwnode, "factory-internal-resistance-micro-ohms",
717 &info->factory_internal_resistance_uohm);
719 if (!fwnode_property_read_u32_array(fwnode, "ambient-celsius",
720 min_max, ARRAY_SIZE(min_max))) {
721 info->temp_ambient_alert_min = min_max[0];
722 info->temp_ambient_alert_max = min_max[1];
724 if (!fwnode_property_read_u32_array(fwnode, "alert-celsius",
725 min_max, ARRAY_SIZE(min_max))) {
726 info->temp_alert_min = min_max[0];
727 info->temp_alert_max = min_max[1];
729 if (!fwnode_property_read_u32_array(fwnode, "operating-range-celsius",
730 min_max, ARRAY_SIZE(min_max))) {
731 info->temp_min = min_max[0];
732 info->temp_max = min_max[1];
736 * The below code uses raw of-data parsing to parse
737 * /schemas/types.yaml#/definitions/uint32-matrix
738 * data, so for now this is only support with of.
740 if (!battery_np)
741 goto out_ret_pointer;
743 len = of_property_count_u32_elems(battery_np, "ocv-capacity-celsius");
744 if (len < 0 && len != -EINVAL) {
745 err = len;
746 goto out_put_node;
747 } else if (len > POWER_SUPPLY_OCV_TEMP_MAX) {
748 dev_err(&psy->dev, "Too many temperature values\n");
749 err = -EINVAL;
750 goto out_put_node;
751 } else if (len > 0) {
752 of_property_read_u32_array(battery_np, "ocv-capacity-celsius",
753 info->ocv_temp, len);
756 for (index = 0; index < len; index++) {
757 struct power_supply_battery_ocv_table *table;
758 int i, tab_len, size;
760 char *propname __free(kfree) = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d",
761 index);
762 if (!propname) {
763 power_supply_put_battery_info(psy, info);
764 err = -ENOMEM;
765 goto out_put_node;
767 list = of_get_property(battery_np, propname, &size);
768 if (!list || !size) {
769 dev_err(&psy->dev, "failed to get %s\n", propname);
770 power_supply_put_battery_info(psy, info);
771 err = -EINVAL;
772 goto out_put_node;
775 tab_len = size / (2 * sizeof(__be32));
776 info->ocv_table_size[index] = tab_len;
778 info->ocv_table[index] = table =
779 devm_kcalloc(&psy->dev, tab_len, sizeof(*table), GFP_KERNEL);
780 if (!info->ocv_table[index]) {
781 power_supply_put_battery_info(psy, info);
782 err = -ENOMEM;
783 goto out_put_node;
786 for (i = 0; i < tab_len; i++) {
787 table[i].ocv = be32_to_cpu(*list);
788 list++;
789 table[i].capacity = be32_to_cpu(*list);
790 list++;
794 list = of_get_property(battery_np, "resistance-temp-table", &len);
795 if (!list || !len)
796 goto out_ret_pointer;
798 info->resist_table_size = len / (2 * sizeof(__be32));
799 info->resist_table = resist_table = devm_kcalloc(&psy->dev,
800 info->resist_table_size,
801 sizeof(*resist_table),
802 GFP_KERNEL);
803 if (!info->resist_table) {
804 power_supply_put_battery_info(psy, info);
805 err = -ENOMEM;
806 goto out_put_node;
809 for (index = 0; index < info->resist_table_size; index++) {
810 resist_table[index].temp = be32_to_cpu(*list++);
811 resist_table[index].resistance = be32_to_cpu(*list++);
814 out_ret_pointer:
815 /* Finally return the whole thing */
816 *info_out = info;
818 out_put_node:
819 fwnode_handle_put(fwnode);
820 of_node_put(battery_np);
821 return err;
823 EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
825 void power_supply_put_battery_info(struct power_supply *psy,
826 struct power_supply_battery_info *info)
828 int i;
830 for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
831 if (info->ocv_table[i])
832 devm_kfree(&psy->dev, info->ocv_table[i]);
835 if (info->resist_table)
836 devm_kfree(&psy->dev, info->resist_table);
838 devm_kfree(&psy->dev, info);
840 EXPORT_SYMBOL_GPL(power_supply_put_battery_info);
842 const enum power_supply_property power_supply_battery_info_properties[] = {
843 POWER_SUPPLY_PROP_TECHNOLOGY,
844 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
845 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
846 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
847 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
848 POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
849 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
850 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
851 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
852 POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN,
853 POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX,
854 POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
855 POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
856 POWER_SUPPLY_PROP_TEMP_MIN,
857 POWER_SUPPLY_PROP_TEMP_MAX,
859 EXPORT_SYMBOL_GPL(power_supply_battery_info_properties);
861 const size_t power_supply_battery_info_properties_size = ARRAY_SIZE(power_supply_battery_info_properties);
862 EXPORT_SYMBOL_GPL(power_supply_battery_info_properties_size);
864 bool power_supply_battery_info_has_prop(struct power_supply_battery_info *info,
865 enum power_supply_property psp)
867 if (!info)
868 return false;
870 switch (psp) {
871 case POWER_SUPPLY_PROP_TECHNOLOGY:
872 return info->technology != POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
873 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
874 return info->energy_full_design_uwh >= 0;
875 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
876 return info->charge_full_design_uah >= 0;
877 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
878 return info->voltage_min_design_uv >= 0;
879 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
880 return info->voltage_max_design_uv >= 0;
881 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
882 return info->precharge_current_ua >= 0;
883 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
884 return info->charge_term_current_ua >= 0;
885 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
886 return info->constant_charge_current_max_ua >= 0;
887 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
888 return info->constant_charge_voltage_max_uv >= 0;
889 case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN:
890 return info->temp_ambient_alert_min > INT_MIN;
891 case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX:
892 return info->temp_ambient_alert_max < INT_MAX;
893 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
894 return info->temp_alert_min > INT_MIN;
895 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
896 return info->temp_alert_max < INT_MAX;
897 case POWER_SUPPLY_PROP_TEMP_MIN:
898 return info->temp_min > INT_MIN;
899 case POWER_SUPPLY_PROP_TEMP_MAX:
900 return info->temp_max < INT_MAX;
901 default:
902 return false;
905 EXPORT_SYMBOL_GPL(power_supply_battery_info_has_prop);
907 int power_supply_battery_info_get_prop(struct power_supply_battery_info *info,
908 enum power_supply_property psp,
909 union power_supply_propval *val)
911 if (!info)
912 return -EINVAL;
914 if (!power_supply_battery_info_has_prop(info, psp))
915 return -EINVAL;
917 switch (psp) {
918 case POWER_SUPPLY_PROP_TECHNOLOGY:
919 val->intval = info->technology;
920 return 0;
921 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
922 val->intval = info->energy_full_design_uwh;
923 return 0;
924 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
925 val->intval = info->charge_full_design_uah;
926 return 0;
927 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
928 val->intval = info->voltage_min_design_uv;
929 return 0;
930 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
931 val->intval = info->voltage_max_design_uv;
932 return 0;
933 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
934 val->intval = info->precharge_current_ua;
935 return 0;
936 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
937 val->intval = info->charge_term_current_ua;
938 return 0;
939 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
940 val->intval = info->constant_charge_current_max_ua;
941 return 0;
942 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
943 val->intval = info->constant_charge_voltage_max_uv;
944 return 0;
945 case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN:
946 val->intval = info->temp_ambient_alert_min;
947 return 0;
948 case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX:
949 val->intval = info->temp_ambient_alert_max;
950 return 0;
951 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
952 val->intval = info->temp_alert_min;
953 return 0;
954 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
955 val->intval = info->temp_alert_max;
956 return 0;
957 case POWER_SUPPLY_PROP_TEMP_MIN:
958 val->intval = info->temp_min;
959 return 0;
960 case POWER_SUPPLY_PROP_TEMP_MAX:
961 val->intval = info->temp_max;
962 return 0;
963 default:
964 return -EINVAL;
967 EXPORT_SYMBOL_GPL(power_supply_battery_info_get_prop);
970 * power_supply_temp2resist_simple() - find the battery internal resistance
971 * percent from temperature
972 * @table: Pointer to battery resistance temperature table
973 * @table_len: The table length
974 * @temp: Current temperature
976 * This helper function is used to look up battery internal resistance percent
977 * according to current temperature value from the resistance temperature table,
978 * and the table must be ordered descending. Then the actual battery internal
979 * resistance = the ideal battery internal resistance * percent / 100.
981 * Return: the battery internal resistance percent
983 int power_supply_temp2resist_simple(const struct power_supply_resistance_temp_table *table,
984 int table_len, int temp)
986 int i, high, low;
988 for (i = 0; i < table_len; i++)
989 if (temp > table[i].temp)
990 break;
992 /* The library function will deal with high == low */
993 if (i == 0)
994 high = low = i;
995 else if (i == table_len)
996 high = low = i - 1;
997 else
998 high = (low = i) - 1;
1000 return fixp_linear_interpolate(table[low].temp,
1001 table[low].resistance,
1002 table[high].temp,
1003 table[high].resistance,
1004 temp);
1006 EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple);
1009 * power_supply_vbat2ri() - find the battery internal resistance
1010 * from the battery voltage
1011 * @info: The battery information container
1012 * @vbat_uv: The battery voltage in microvolt
1013 * @charging: If we are charging (true) or not (false)
1015 * This helper function is used to look up battery internal resistance
1016 * according to current battery voltage. Depending on whether the battery
1017 * is currently charging or not, different resistance will be returned.
1019 * Returns the internal resistance in microohm or negative error code.
1021 int power_supply_vbat2ri(struct power_supply_battery_info *info,
1022 int vbat_uv, bool charging)
1024 const struct power_supply_vbat_ri_table *vbat2ri;
1025 int table_len;
1026 int i, high, low;
1029 * If we are charging, and the battery supplies a separate table
1030 * for this state, we use that in order to compensate for the
1031 * charging voltage. Otherwise we use the main table.
1033 if (charging && info->vbat2ri_charging) {
1034 vbat2ri = info->vbat2ri_charging;
1035 table_len = info->vbat2ri_charging_size;
1036 } else {
1037 vbat2ri = info->vbat2ri_discharging;
1038 table_len = info->vbat2ri_discharging_size;
1042 * If no tables are specified, or if we are above the highest voltage in
1043 * the voltage table, just return the factory specified internal resistance.
1045 if (!vbat2ri || (table_len <= 0) || (vbat_uv > vbat2ri[0].vbat_uv)) {
1046 if (charging && (info->factory_internal_resistance_charging_uohm > 0))
1047 return info->factory_internal_resistance_charging_uohm;
1048 else
1049 return info->factory_internal_resistance_uohm;
1052 /* Break loop at table_len - 1 because that is the highest index */
1053 for (i = 0; i < table_len - 1; i++)
1054 if (vbat_uv > vbat2ri[i].vbat_uv)
1055 break;
1057 /* The library function will deal with high == low */
1058 if ((i == 0) || (i == (table_len - 1)))
1059 high = i;
1060 else
1061 high = i - 1;
1062 low = i;
1064 return fixp_linear_interpolate(vbat2ri[low].vbat_uv,
1065 vbat2ri[low].ri_uohm,
1066 vbat2ri[high].vbat_uv,
1067 vbat2ri[high].ri_uohm,
1068 vbat_uv);
1070 EXPORT_SYMBOL_GPL(power_supply_vbat2ri);
1072 const struct power_supply_maintenance_charge_table *
1073 power_supply_get_maintenance_charging_setting(struct power_supply_battery_info *info,
1074 int index)
1076 if (index >= info->maintenance_charge_size)
1077 return NULL;
1078 return &info->maintenance_charge[index];
1080 EXPORT_SYMBOL_GPL(power_supply_get_maintenance_charging_setting);
1083 * power_supply_ocv2cap_simple() - find the battery capacity
1084 * @table: Pointer to battery OCV lookup table
1085 * @table_len: OCV table length
1086 * @ocv: Current OCV value
1088 * This helper function is used to look up battery capacity according to
1089 * current OCV value from one OCV table, and the OCV table must be ordered
1090 * descending.
1092 * Return: the battery capacity.
1094 int power_supply_ocv2cap_simple(const struct power_supply_battery_ocv_table *table,
1095 int table_len, int ocv)
1097 int i, high, low;
1099 for (i = 0; i < table_len; i++)
1100 if (ocv > table[i].ocv)
1101 break;
1103 /* The library function will deal with high == low */
1104 if (i == 0)
1105 high = low = i;
1106 else if (i == table_len)
1107 high = low = i - 1;
1108 else
1109 high = (low = i) - 1;
1111 return fixp_linear_interpolate(table[low].ocv,
1112 table[low].capacity,
1113 table[high].ocv,
1114 table[high].capacity,
1115 ocv);
1117 EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple);
1119 const struct power_supply_battery_ocv_table *
1120 power_supply_find_ocv2cap_table(struct power_supply_battery_info *info,
1121 int temp, int *table_len)
1123 int best_temp_diff = INT_MAX, temp_diff;
1124 u8 i, best_index = 0;
1126 if (!info->ocv_table[0])
1127 return NULL;
1129 for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
1130 /* Out of capacity tables */
1131 if (!info->ocv_table[i])
1132 break;
1134 temp_diff = abs(info->ocv_temp[i] - temp);
1136 if (temp_diff < best_temp_diff) {
1137 best_temp_diff = temp_diff;
1138 best_index = i;
1142 *table_len = info->ocv_table_size[best_index];
1143 return info->ocv_table[best_index];
1145 EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table);
1147 int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info,
1148 int ocv, int temp)
1150 const struct power_supply_battery_ocv_table *table;
1151 int table_len;
1153 table = power_supply_find_ocv2cap_table(info, temp, &table_len);
1154 if (!table)
1155 return -EINVAL;
1157 return power_supply_ocv2cap_simple(table, table_len, ocv);
1159 EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap);
1161 bool power_supply_battery_bti_in_range(struct power_supply_battery_info *info,
1162 int resistance)
1164 int low, high;
1166 /* Nothing like this can be checked */
1167 if (info->bti_resistance_ohm <= 0)
1168 return false;
1170 /* This will be extremely strict and unlikely to work */
1171 if (info->bti_resistance_tolerance <= 0)
1172 return (info->bti_resistance_ohm == resistance);
1174 low = info->bti_resistance_ohm -
1175 (info->bti_resistance_ohm * info->bti_resistance_tolerance) / 100;
1176 high = info->bti_resistance_ohm +
1177 (info->bti_resistance_ohm * info->bti_resistance_tolerance) / 100;
1179 return ((resistance >= low) && (resistance <= high));
1181 EXPORT_SYMBOL_GPL(power_supply_battery_bti_in_range);
1183 static bool psy_has_property(const struct power_supply_desc *psy_desc,
1184 enum power_supply_property psp)
1186 bool found = false;
1187 int i;
1189 for (i = 0; i < psy_desc->num_properties; i++) {
1190 if (psy_desc->properties[i] == psp) {
1191 found = true;
1192 break;
1196 return found;
1199 int power_supply_get_property(struct power_supply *psy,
1200 enum power_supply_property psp,
1201 union power_supply_propval *val)
1203 if (atomic_read(&psy->use_cnt) <= 0) {
1204 if (!psy->initialized)
1205 return -EAGAIN;
1206 return -ENODEV;
1209 if (psy_has_property(psy->desc, psp))
1210 return psy->desc->get_property(psy, psp, val);
1211 else if (power_supply_battery_info_has_prop(psy->battery_info, psp))
1212 return power_supply_battery_info_get_prop(psy->battery_info, psp, val);
1213 else
1214 return -EINVAL;
1216 EXPORT_SYMBOL_GPL(power_supply_get_property);
1218 int power_supply_set_property(struct power_supply *psy,
1219 enum power_supply_property psp,
1220 const union power_supply_propval *val)
1222 if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
1223 return -ENODEV;
1225 return psy->desc->set_property(psy, psp, val);
1227 EXPORT_SYMBOL_GPL(power_supply_set_property);
1229 int power_supply_property_is_writeable(struct power_supply *psy,
1230 enum power_supply_property psp)
1232 return psy->desc->property_is_writeable && psy->desc->property_is_writeable(psy, psp);
1235 void power_supply_external_power_changed(struct power_supply *psy)
1237 if (atomic_read(&psy->use_cnt) <= 0 ||
1238 !psy->desc->external_power_changed)
1239 return;
1241 psy->desc->external_power_changed(psy);
1243 EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
1245 int power_supply_powers(struct power_supply *psy, struct device *dev)
1247 return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
1249 EXPORT_SYMBOL_GPL(power_supply_powers);
1251 static void power_supply_dev_release(struct device *dev)
1253 struct power_supply *psy = to_power_supply(dev);
1255 dev_dbg(dev, "%s\n", __func__);
1256 kfree(psy);
1259 int power_supply_reg_notifier(struct notifier_block *nb)
1261 return blocking_notifier_chain_register(&power_supply_notifier, nb);
1263 EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
1265 void power_supply_unreg_notifier(struct notifier_block *nb)
1267 blocking_notifier_chain_unregister(&power_supply_notifier, nb);
1269 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
1271 #ifdef CONFIG_THERMAL
1272 static int power_supply_read_temp(struct thermal_zone_device *tzd,
1273 int *temp)
1275 struct power_supply *psy;
1276 union power_supply_propval val;
1277 int ret;
1279 WARN_ON(tzd == NULL);
1280 psy = thermal_zone_device_priv(tzd);
1281 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
1282 if (ret)
1283 return ret;
1285 /* Convert tenths of degree Celsius to milli degree Celsius. */
1286 *temp = val.intval * 100;
1288 return ret;
1291 static const struct thermal_zone_device_ops psy_tzd_ops = {
1292 .get_temp = power_supply_read_temp,
1295 static int psy_register_thermal(struct power_supply *psy)
1297 int ret;
1299 if (psy->desc->no_thermal)
1300 return 0;
1302 /* Register battery zone device psy reports temperature */
1303 if (psy_has_property(psy->desc, POWER_SUPPLY_PROP_TEMP)) {
1304 /* Prefer our hwmon device and avoid duplicates */
1305 struct thermal_zone_params tzp = {
1306 .no_hwmon = IS_ENABLED(CONFIG_POWER_SUPPLY_HWMON)
1308 psy->tzd = thermal_tripless_zone_device_register(psy->desc->name,
1309 psy, &psy_tzd_ops, &tzp);
1310 if (IS_ERR(psy->tzd))
1311 return PTR_ERR(psy->tzd);
1312 ret = thermal_zone_device_enable(psy->tzd);
1313 if (ret)
1314 thermal_zone_device_unregister(psy->tzd);
1315 return ret;
1318 return 0;
1321 static void psy_unregister_thermal(struct power_supply *psy)
1323 if (IS_ERR_OR_NULL(psy->tzd))
1324 return;
1325 thermal_zone_device_unregister(psy->tzd);
1328 #else
1329 static int psy_register_thermal(struct power_supply *psy)
1331 return 0;
1334 static void psy_unregister_thermal(struct power_supply *psy)
1337 #endif
1339 static struct power_supply *__must_check
1340 __power_supply_register(struct device *parent,
1341 const struct power_supply_desc *desc,
1342 const struct power_supply_config *cfg)
1344 struct device *dev;
1345 struct power_supply *psy;
1346 int rc;
1348 if (!desc || !desc->name || !desc->properties || !desc->num_properties)
1349 return ERR_PTR(-EINVAL);
1351 if (!parent)
1352 pr_warn("%s: Expected proper parent device for '%s'\n",
1353 __func__, desc->name);
1355 psy = kzalloc(sizeof(*psy), GFP_KERNEL);
1356 if (!psy)
1357 return ERR_PTR(-ENOMEM);
1359 dev = &psy->dev;
1361 device_initialize(dev);
1363 dev->class = &power_supply_class;
1364 dev->type = &power_supply_dev_type;
1365 dev->parent = parent;
1366 dev->release = power_supply_dev_release;
1367 dev_set_drvdata(dev, psy);
1368 psy->desc = desc;
1369 if (cfg) {
1370 dev->groups = cfg->attr_grp;
1371 psy->drv_data = cfg->drv_data;
1372 psy->of_node =
1373 cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node;
1374 dev->of_node = psy->of_node;
1375 psy->supplied_to = cfg->supplied_to;
1376 psy->num_supplicants = cfg->num_supplicants;
1379 rc = dev_set_name(dev, "%s", desc->name);
1380 if (rc)
1381 goto dev_set_name_failed;
1383 INIT_WORK(&psy->changed_work, power_supply_changed_work);
1384 INIT_DELAYED_WORK(&psy->deferred_register_work,
1385 power_supply_deferred_register_work);
1387 rc = power_supply_check_supplies(psy);
1388 if (rc) {
1389 dev_dbg(dev, "Not all required supplies found, defer probe\n");
1390 goto check_supplies_failed;
1394 * Expose constant battery info, if it is available. While there are
1395 * some chargers accessing constant battery data, we only want to
1396 * expose battery data to userspace for battery devices.
1398 if (desc->type == POWER_SUPPLY_TYPE_BATTERY) {
1399 rc = power_supply_get_battery_info(psy, &psy->battery_info);
1400 if (rc && rc != -ENODEV && rc != -ENOENT)
1401 goto check_supplies_failed;
1404 spin_lock_init(&psy->changed_lock);
1405 rc = device_add(dev);
1406 if (rc)
1407 goto device_add_failed;
1409 rc = device_init_wakeup(dev, cfg ? !cfg->no_wakeup_source : true);
1410 if (rc)
1411 goto wakeup_init_failed;
1413 rc = psy_register_thermal(psy);
1414 if (rc)
1415 goto register_thermal_failed;
1417 rc = power_supply_create_triggers(psy);
1418 if (rc)
1419 goto create_triggers_failed;
1421 rc = power_supply_add_hwmon_sysfs(psy);
1422 if (rc)
1423 goto add_hwmon_sysfs_failed;
1426 * Update use_cnt after any uevents (most notably from device_add()).
1427 * We are here still during driver's probe but
1428 * the power_supply_uevent() calls back driver's get_property
1429 * method so:
1430 * 1. Driver did not assigned the returned struct power_supply,
1431 * 2. Driver could not finish initialization (anything in its probe
1432 * after calling power_supply_register()).
1434 atomic_inc(&psy->use_cnt);
1435 psy->initialized = true;
1437 queue_delayed_work(system_power_efficient_wq,
1438 &psy->deferred_register_work,
1439 POWER_SUPPLY_DEFERRED_REGISTER_TIME);
1441 return psy;
1443 add_hwmon_sysfs_failed:
1444 power_supply_remove_triggers(psy);
1445 create_triggers_failed:
1446 psy_unregister_thermal(psy);
1447 register_thermal_failed:
1448 wakeup_init_failed:
1449 device_del(dev);
1450 device_add_failed:
1451 check_supplies_failed:
1452 dev_set_name_failed:
1453 put_device(dev);
1454 return ERR_PTR(rc);
1458 * power_supply_register() - Register new power supply
1459 * @parent: Device to be a parent of power supply's device, usually
1460 * the device which probe function calls this
1461 * @desc: Description of power supply, must be valid through whole
1462 * lifetime of this power supply
1463 * @cfg: Run-time specific configuration accessed during registering,
1464 * may be NULL
1466 * Return: A pointer to newly allocated power_supply on success
1467 * or ERR_PTR otherwise.
1468 * Use power_supply_unregister() on returned power_supply pointer to release
1469 * resources.
1471 struct power_supply *__must_check power_supply_register(struct device *parent,
1472 const struct power_supply_desc *desc,
1473 const struct power_supply_config *cfg)
1475 return __power_supply_register(parent, desc, cfg);
1477 EXPORT_SYMBOL_GPL(power_supply_register);
1479 static void devm_power_supply_release(struct device *dev, void *res)
1481 struct power_supply **psy = res;
1483 power_supply_unregister(*psy);
1487 * devm_power_supply_register() - Register managed power supply
1488 * @parent: Device to be a parent of power supply's device, usually
1489 * the device which probe function calls this
1490 * @desc: Description of power supply, must be valid through whole
1491 * lifetime of this power supply
1492 * @cfg: Run-time specific configuration accessed during registering,
1493 * may be NULL
1495 * Return: A pointer to newly allocated power_supply on success
1496 * or ERR_PTR otherwise.
1497 * The returned power_supply pointer will be automatically unregistered
1498 * on driver detach.
1500 struct power_supply *__must_check
1501 devm_power_supply_register(struct device *parent,
1502 const struct power_supply_desc *desc,
1503 const struct power_supply_config *cfg)
1505 struct power_supply **ptr, *psy;
1507 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1509 if (!ptr)
1510 return ERR_PTR(-ENOMEM);
1511 psy = __power_supply_register(parent, desc, cfg);
1512 if (IS_ERR(psy)) {
1513 devres_free(ptr);
1514 } else {
1515 *ptr = psy;
1516 devres_add(parent, ptr);
1518 return psy;
1520 EXPORT_SYMBOL_GPL(devm_power_supply_register);
1523 * power_supply_unregister() - Remove this power supply from system
1524 * @psy: Pointer to power supply to unregister
1526 * Remove this power supply from the system. The resources of power supply
1527 * will be freed here or on last power_supply_put() call.
1529 void power_supply_unregister(struct power_supply *psy)
1531 WARN_ON(atomic_dec_return(&psy->use_cnt));
1532 psy->removing = true;
1533 cancel_work_sync(&psy->changed_work);
1534 cancel_delayed_work_sync(&psy->deferred_register_work);
1535 sysfs_remove_link(&psy->dev.kobj, "powers");
1536 power_supply_remove_hwmon_sysfs(psy);
1537 power_supply_remove_triggers(psy);
1538 psy_unregister_thermal(psy);
1539 device_init_wakeup(&psy->dev, false);
1540 device_unregister(&psy->dev);
1542 EXPORT_SYMBOL_GPL(power_supply_unregister);
1544 void *power_supply_get_drvdata(struct power_supply *psy)
1546 return psy->drv_data;
1548 EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
1550 static int __init power_supply_class_init(void)
1552 power_supply_init_attrs();
1553 return class_register(&power_supply_class);
1556 static void __exit power_supply_class_exit(void)
1558 class_unregister(&power_supply_class);
1561 subsys_initcall(power_supply_class_init);
1562 module_exit(power_supply_class_exit);
1564 MODULE_DESCRIPTION("Universal power supply monitor class");
1565 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
1566 MODULE_AUTHOR("Szabolcs Gyurko");
1567 MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");