WIP FPC-III support
[linux/fpc-iii.git] / drivers / power / supply / power_supply_core.c
blob38e3aa642131dad1355dded02b4e736d546b8f92
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/module.h>
13 #include <linux/types.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/notifier.h>
19 #include <linux/err.h>
20 #include <linux/of.h>
21 #include <linux/power_supply.h>
22 #include <linux/property.h>
23 #include <linux/thermal.h>
24 #include "power_supply.h"
26 /* exported for the APM Power driver, APM emulation */
27 struct class *power_supply_class;
28 EXPORT_SYMBOL_GPL(power_supply_class);
30 ATOMIC_NOTIFIER_HEAD(power_supply_notifier);
31 EXPORT_SYMBOL_GPL(power_supply_notifier);
33 static struct device_type power_supply_dev_type;
35 #define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10)
37 static bool __power_supply_is_supplied_by(struct power_supply *supplier,
38 struct power_supply *supply)
40 int i;
42 if (!supply->supplied_from && !supplier->supplied_to)
43 return false;
45 /* Support both supplied_to and supplied_from modes */
46 if (supply->supplied_from) {
47 if (!supplier->desc->name)
48 return false;
49 for (i = 0; i < supply->num_supplies; i++)
50 if (!strcmp(supplier->desc->name, supply->supplied_from[i]))
51 return true;
52 } else {
53 if (!supply->desc->name)
54 return false;
55 for (i = 0; i < supplier->num_supplicants; i++)
56 if (!strcmp(supplier->supplied_to[i], supply->desc->name))
57 return true;
60 return false;
63 static int __power_supply_changed_work(struct device *dev, void *data)
65 struct power_supply *psy = data;
66 struct power_supply *pst = dev_get_drvdata(dev);
68 if (__power_supply_is_supplied_by(psy, pst)) {
69 if (pst->desc->external_power_changed)
70 pst->desc->external_power_changed(pst);
73 return 0;
76 static void power_supply_changed_work(struct work_struct *work)
78 unsigned long flags;
79 struct power_supply *psy = container_of(work, struct power_supply,
80 changed_work);
82 dev_dbg(&psy->dev, "%s\n", __func__);
84 spin_lock_irqsave(&psy->changed_lock, flags);
86 * Check 'changed' here to avoid issues due to race between
87 * power_supply_changed() and this routine. In worst case
88 * power_supply_changed() can be called again just before we take above
89 * lock. During the first call of this routine we will mark 'changed' as
90 * false and it will stay false for the next call as well.
92 if (likely(psy->changed)) {
93 psy->changed = false;
94 spin_unlock_irqrestore(&psy->changed_lock, flags);
95 class_for_each_device(power_supply_class, NULL, psy,
96 __power_supply_changed_work);
97 power_supply_update_leds(psy);
98 atomic_notifier_call_chain(&power_supply_notifier,
99 PSY_EVENT_PROP_CHANGED, psy);
100 kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
101 spin_lock_irqsave(&psy->changed_lock, flags);
105 * Hold the wakeup_source until all events are processed.
106 * power_supply_changed() might have called again and have set 'changed'
107 * to true.
109 if (likely(!psy->changed))
110 pm_relax(&psy->dev);
111 spin_unlock_irqrestore(&psy->changed_lock, flags);
114 void power_supply_changed(struct power_supply *psy)
116 unsigned long flags;
118 dev_dbg(&psy->dev, "%s\n", __func__);
120 spin_lock_irqsave(&psy->changed_lock, flags);
121 psy->changed = true;
122 pm_stay_awake(&psy->dev);
123 spin_unlock_irqrestore(&psy->changed_lock, flags);
124 schedule_work(&psy->changed_work);
126 EXPORT_SYMBOL_GPL(power_supply_changed);
129 * Notify that power supply was registered after parent finished the probing.
131 * Often power supply is registered from driver's probe function. However
132 * calling power_supply_changed() directly from power_supply_register()
133 * would lead to execution of get_property() function provided by the driver
134 * too early - before the probe ends.
136 * Avoid that by waiting on parent's mutex.
138 static void power_supply_deferred_register_work(struct work_struct *work)
140 struct power_supply *psy = container_of(work, struct power_supply,
141 deferred_register_work.work);
143 if (psy->dev.parent) {
144 while (!mutex_trylock(&psy->dev.parent->mutex)) {
145 if (psy->removing)
146 return;
147 msleep(10);
151 power_supply_changed(psy);
153 if (psy->dev.parent)
154 mutex_unlock(&psy->dev.parent->mutex);
157 #ifdef CONFIG_OF
158 static int __power_supply_populate_supplied_from(struct device *dev,
159 void *data)
161 struct power_supply *psy = data;
162 struct power_supply *epsy = dev_get_drvdata(dev);
163 struct device_node *np;
164 int i = 0;
166 do {
167 np = of_parse_phandle(psy->of_node, "power-supplies", i++);
168 if (!np)
169 break;
171 if (np == epsy->of_node) {
172 dev_info(&psy->dev, "%s: Found supply : %s\n",
173 psy->desc->name, epsy->desc->name);
174 psy->supplied_from[i-1] = (char *)epsy->desc->name;
175 psy->num_supplies++;
176 of_node_put(np);
177 break;
179 of_node_put(np);
180 } while (np);
182 return 0;
185 static int power_supply_populate_supplied_from(struct power_supply *psy)
187 int error;
189 error = class_for_each_device(power_supply_class, NULL, psy,
190 __power_supply_populate_supplied_from);
192 dev_dbg(&psy->dev, "%s %d\n", __func__, error);
194 return error;
197 static int __power_supply_find_supply_from_node(struct device *dev,
198 void *data)
200 struct device_node *np = data;
201 struct power_supply *epsy = dev_get_drvdata(dev);
203 /* returning non-zero breaks out of class_for_each_device loop */
204 if (epsy->of_node == np)
205 return 1;
207 return 0;
210 static int power_supply_find_supply_from_node(struct device_node *supply_node)
212 int error;
215 * class_for_each_device() either returns its own errors or values
216 * returned by __power_supply_find_supply_from_node().
218 * __power_supply_find_supply_from_node() will return 0 (no match)
219 * or 1 (match).
221 * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
222 * it returned 0, or error as returned by it.
224 error = class_for_each_device(power_supply_class, NULL, supply_node,
225 __power_supply_find_supply_from_node);
227 return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
230 static int power_supply_check_supplies(struct power_supply *psy)
232 struct device_node *np;
233 int cnt = 0;
235 /* If there is already a list honor it */
236 if (psy->supplied_from && psy->num_supplies > 0)
237 return 0;
239 /* No device node found, nothing to do */
240 if (!psy->of_node)
241 return 0;
243 do {
244 int ret;
246 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
247 if (!np)
248 break;
250 ret = power_supply_find_supply_from_node(np);
251 of_node_put(np);
253 if (ret) {
254 dev_dbg(&psy->dev, "Failed to find supply!\n");
255 return ret;
257 } while (np);
259 /* Missing valid "power-supplies" entries */
260 if (cnt == 1)
261 return 0;
263 /* All supplies found, allocate char ** array for filling */
264 psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
265 GFP_KERNEL);
266 if (!psy->supplied_from)
267 return -ENOMEM;
269 *psy->supplied_from = devm_kcalloc(&psy->dev,
270 cnt - 1, sizeof(char *),
271 GFP_KERNEL);
272 if (!*psy->supplied_from)
273 return -ENOMEM;
275 return power_supply_populate_supplied_from(psy);
277 #else
278 static int power_supply_check_supplies(struct power_supply *psy)
280 int nval, ret;
282 if (!psy->dev.parent)
283 return 0;
285 nval = device_property_read_string_array(psy->dev.parent,
286 "supplied-from", NULL, 0);
287 if (nval <= 0)
288 return 0;
290 psy->supplied_from = devm_kmalloc_array(&psy->dev, nval,
291 sizeof(char *), GFP_KERNEL);
292 if (!psy->supplied_from)
293 return -ENOMEM;
295 ret = device_property_read_string_array(psy->dev.parent,
296 "supplied-from", (const char **)psy->supplied_from, nval);
297 if (ret < 0)
298 return ret;
300 psy->num_supplies = nval;
302 return 0;
304 #endif
306 struct psy_am_i_supplied_data {
307 struct power_supply *psy;
308 unsigned int count;
311 static int __power_supply_am_i_supplied(struct device *dev, void *_data)
313 union power_supply_propval ret = {0,};
314 struct power_supply *epsy = dev_get_drvdata(dev);
315 struct psy_am_i_supplied_data *data = _data;
317 if (__power_supply_is_supplied_by(epsy, data->psy)) {
318 data->count++;
319 if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
320 &ret))
321 return ret.intval;
324 return 0;
327 int power_supply_am_i_supplied(struct power_supply *psy)
329 struct psy_am_i_supplied_data data = { psy, 0 };
330 int error;
332 error = class_for_each_device(power_supply_class, NULL, &data,
333 __power_supply_am_i_supplied);
335 dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
337 if (data.count == 0)
338 return -ENODEV;
340 return error;
342 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
344 static int __power_supply_is_system_supplied(struct device *dev, void *data)
346 union power_supply_propval ret = {0,};
347 struct power_supply *psy = dev_get_drvdata(dev);
348 unsigned int *count = data;
350 (*count)++;
351 if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
352 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
353 &ret))
354 return ret.intval;
356 return 0;
359 int power_supply_is_system_supplied(void)
361 int error;
362 unsigned int count = 0;
364 error = class_for_each_device(power_supply_class, NULL, &count,
365 __power_supply_is_system_supplied);
368 * If no power class device was found at all, most probably we are
369 * running on a desktop system, so assume we are on mains power.
371 if (count == 0)
372 return 1;
374 return error;
376 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
378 static int __power_supply_get_supplier_max_current(struct device *dev,
379 void *data)
381 union power_supply_propval ret = {0,};
382 struct power_supply *epsy = dev_get_drvdata(dev);
383 struct power_supply *psy = data;
385 if (__power_supply_is_supplied_by(epsy, psy))
386 if (!epsy->desc->get_property(epsy,
387 POWER_SUPPLY_PROP_CURRENT_MAX,
388 &ret))
389 return ret.intval;
391 return 0;
394 int power_supply_set_input_current_limit_from_supplier(struct power_supply *psy)
396 union power_supply_propval val = {0,};
397 int curr;
399 if (!psy->desc->set_property)
400 return -EINVAL;
403 * This function is not intended for use with a supply with multiple
404 * suppliers, we simply pick the first supply to report a non 0
405 * max-current.
407 curr = class_for_each_device(power_supply_class, NULL, psy,
408 __power_supply_get_supplier_max_current);
409 if (curr <= 0)
410 return (curr == 0) ? -ENODEV : curr;
412 val.intval = curr;
414 return psy->desc->set_property(psy,
415 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
417 EXPORT_SYMBOL_GPL(power_supply_set_input_current_limit_from_supplier);
419 int power_supply_set_battery_charged(struct power_supply *psy)
421 if (atomic_read(&psy->use_cnt) >= 0 &&
422 psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
423 psy->desc->set_charged) {
424 psy->desc->set_charged(psy);
425 return 0;
428 return -EINVAL;
430 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
432 static int power_supply_match_device_by_name(struct device *dev, const void *data)
434 const char *name = data;
435 struct power_supply *psy = dev_get_drvdata(dev);
437 return strcmp(psy->desc->name, name) == 0;
441 * power_supply_get_by_name() - Search for a power supply and returns its ref
442 * @name: Power supply name to fetch
444 * If power supply was found, it increases reference count for the
445 * internal power supply's device. The user should power_supply_put()
446 * after usage.
448 * Return: On success returns a reference to a power supply with
449 * matching name equals to @name, a NULL otherwise.
451 struct power_supply *power_supply_get_by_name(const char *name)
453 struct power_supply *psy = NULL;
454 struct device *dev = class_find_device(power_supply_class, NULL, name,
455 power_supply_match_device_by_name);
457 if (dev) {
458 psy = dev_get_drvdata(dev);
459 atomic_inc(&psy->use_cnt);
462 return psy;
464 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
467 * power_supply_put() - Drop reference obtained with power_supply_get_by_name
468 * @psy: Reference to put
470 * The reference to power supply should be put before unregistering
471 * the power supply.
473 void power_supply_put(struct power_supply *psy)
475 might_sleep();
477 atomic_dec(&psy->use_cnt);
478 put_device(&psy->dev);
480 EXPORT_SYMBOL_GPL(power_supply_put);
482 #ifdef CONFIG_OF
483 static int power_supply_match_device_node(struct device *dev, const void *data)
485 return dev->parent && dev->parent->of_node == data;
489 * power_supply_get_by_phandle() - Search for a power supply and returns its ref
490 * @np: Pointer to device node holding phandle property
491 * @property: Name of property holding a power supply name
493 * If power supply was found, it increases reference count for the
494 * internal power supply's device. The user should power_supply_put()
495 * after usage.
497 * Return: On success returns a reference to a power supply with
498 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
500 struct power_supply *power_supply_get_by_phandle(struct device_node *np,
501 const char *property)
503 struct device_node *power_supply_np;
504 struct power_supply *psy = NULL;
505 struct device *dev;
507 power_supply_np = of_parse_phandle(np, property, 0);
508 if (!power_supply_np)
509 return ERR_PTR(-ENODEV);
511 dev = class_find_device(power_supply_class, NULL, power_supply_np,
512 power_supply_match_device_node);
514 of_node_put(power_supply_np);
516 if (dev) {
517 psy = dev_get_drvdata(dev);
518 atomic_inc(&psy->use_cnt);
521 return psy;
523 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
525 static void devm_power_supply_put(struct device *dev, void *res)
527 struct power_supply **psy = res;
529 power_supply_put(*psy);
533 * devm_power_supply_get_by_phandle() - Resource managed version of
534 * power_supply_get_by_phandle()
535 * @dev: Pointer to device holding phandle property
536 * @property: Name of property holding a power supply phandle
538 * Return: On success returns a reference to a power supply with
539 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
541 struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
542 const char *property)
544 struct power_supply **ptr, *psy;
546 if (!dev->of_node)
547 return ERR_PTR(-ENODEV);
549 ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
550 if (!ptr)
551 return ERR_PTR(-ENOMEM);
553 psy = power_supply_get_by_phandle(dev->of_node, property);
554 if (IS_ERR_OR_NULL(psy)) {
555 devres_free(ptr);
556 } else {
557 *ptr = psy;
558 devres_add(dev, ptr);
560 return psy;
562 EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
563 #endif /* CONFIG_OF */
565 int power_supply_get_battery_info(struct power_supply *psy,
566 struct power_supply_battery_info *info)
568 struct power_supply_resistance_temp_table *resist_table;
569 struct device_node *battery_np;
570 const char *value;
571 int err, len, index;
572 const __be32 *list;
574 info->energy_full_design_uwh = -EINVAL;
575 info->charge_full_design_uah = -EINVAL;
576 info->voltage_min_design_uv = -EINVAL;
577 info->voltage_max_design_uv = -EINVAL;
578 info->precharge_current_ua = -EINVAL;
579 info->charge_term_current_ua = -EINVAL;
580 info->constant_charge_current_max_ua = -EINVAL;
581 info->constant_charge_voltage_max_uv = -EINVAL;
582 info->temp_ambient_alert_min = INT_MIN;
583 info->temp_ambient_alert_max = INT_MAX;
584 info->temp_alert_min = INT_MIN;
585 info->temp_alert_max = INT_MAX;
586 info->temp_min = INT_MIN;
587 info->temp_max = INT_MAX;
588 info->factory_internal_resistance_uohm = -EINVAL;
589 info->resist_table = NULL;
591 for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) {
592 info->ocv_table[index] = NULL;
593 info->ocv_temp[index] = -EINVAL;
594 info->ocv_table_size[index] = -EINVAL;
597 if (!psy->of_node) {
598 dev_warn(&psy->dev, "%s currently only supports devicetree\n",
599 __func__);
600 return -ENXIO;
603 battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);
604 if (!battery_np)
605 return -ENODEV;
607 err = of_property_read_string(battery_np, "compatible", &value);
608 if (err)
609 goto out_put_node;
611 if (strcmp("simple-battery", value)) {
612 err = -ENODEV;
613 goto out_put_node;
616 /* The property and field names below must correspond to elements
617 * in enum power_supply_property. For reasoning, see
618 * Documentation/power/power_supply_class.rst.
621 of_property_read_u32(battery_np, "energy-full-design-microwatt-hours",
622 &info->energy_full_design_uwh);
623 of_property_read_u32(battery_np, "charge-full-design-microamp-hours",
624 &info->charge_full_design_uah);
625 of_property_read_u32(battery_np, "voltage-min-design-microvolt",
626 &info->voltage_min_design_uv);
627 of_property_read_u32(battery_np, "voltage-max-design-microvolt",
628 &info->voltage_max_design_uv);
629 of_property_read_u32(battery_np, "trickle-charge-current-microamp",
630 &info->tricklecharge_current_ua);
631 of_property_read_u32(battery_np, "precharge-current-microamp",
632 &info->precharge_current_ua);
633 of_property_read_u32(battery_np, "precharge-upper-limit-microvolt",
634 &info->precharge_voltage_max_uv);
635 of_property_read_u32(battery_np, "charge-term-current-microamp",
636 &info->charge_term_current_ua);
637 of_property_read_u32(battery_np, "re-charge-voltage-microvolt",
638 &info->charge_restart_voltage_uv);
639 of_property_read_u32(battery_np, "over-voltage-threshold-microvolt",
640 &info->overvoltage_limit_uv);
641 of_property_read_u32(battery_np, "constant-charge-current-max-microamp",
642 &info->constant_charge_current_max_ua);
643 of_property_read_u32(battery_np, "constant-charge-voltage-max-microvolt",
644 &info->constant_charge_voltage_max_uv);
645 of_property_read_u32(battery_np, "factory-internal-resistance-micro-ohms",
646 &info->factory_internal_resistance_uohm);
648 of_property_read_u32_index(battery_np, "ambient-celsius",
649 0, &info->temp_ambient_alert_min);
650 of_property_read_u32_index(battery_np, "ambient-celsius",
651 1, &info->temp_ambient_alert_max);
652 of_property_read_u32_index(battery_np, "alert-celsius",
653 0, &info->temp_alert_min);
654 of_property_read_u32_index(battery_np, "alert-celsius",
655 1, &info->temp_alert_max);
656 of_property_read_u32_index(battery_np, "operating-range-celsius",
657 0, &info->temp_min);
658 of_property_read_u32_index(battery_np, "operating-range-celsius",
659 1, &info->temp_max);
661 len = of_property_count_u32_elems(battery_np, "ocv-capacity-celsius");
662 if (len < 0 && len != -EINVAL) {
663 err = len;
664 goto out_put_node;
665 } else if (len > POWER_SUPPLY_OCV_TEMP_MAX) {
666 dev_err(&psy->dev, "Too many temperature values\n");
667 err = -EINVAL;
668 goto out_put_node;
669 } else if (len > 0) {
670 of_property_read_u32_array(battery_np, "ocv-capacity-celsius",
671 info->ocv_temp, len);
674 for (index = 0; index < len; index++) {
675 struct power_supply_battery_ocv_table *table;
676 char *propname;
677 int i, tab_len, size;
679 propname = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d", index);
680 list = of_get_property(battery_np, propname, &size);
681 if (!list || !size) {
682 dev_err(&psy->dev, "failed to get %s\n", propname);
683 kfree(propname);
684 power_supply_put_battery_info(psy, info);
685 err = -EINVAL;
686 goto out_put_node;
689 kfree(propname);
690 tab_len = size / (2 * sizeof(__be32));
691 info->ocv_table_size[index] = tab_len;
693 table = info->ocv_table[index] =
694 devm_kcalloc(&psy->dev, tab_len, sizeof(*table), GFP_KERNEL);
695 if (!info->ocv_table[index]) {
696 power_supply_put_battery_info(psy, info);
697 err = -ENOMEM;
698 goto out_put_node;
701 for (i = 0; i < tab_len; i++) {
702 table[i].ocv = be32_to_cpu(*list);
703 list++;
704 table[i].capacity = be32_to_cpu(*list);
705 list++;
709 list = of_get_property(battery_np, "resistance-temp-table", &len);
710 if (!list || !len)
711 goto out_put_node;
713 info->resist_table_size = len / (2 * sizeof(__be32));
714 resist_table = info->resist_table = devm_kcalloc(&psy->dev,
715 info->resist_table_size,
716 sizeof(*resist_table),
717 GFP_KERNEL);
718 if (!info->resist_table) {
719 power_supply_put_battery_info(psy, info);
720 err = -ENOMEM;
721 goto out_put_node;
724 for (index = 0; index < info->resist_table_size; index++) {
725 resist_table[index].temp = be32_to_cpu(*list++);
726 resist_table[index].resistance = be32_to_cpu(*list++);
729 out_put_node:
730 of_node_put(battery_np);
731 return err;
733 EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
735 void power_supply_put_battery_info(struct power_supply *psy,
736 struct power_supply_battery_info *info)
738 int i;
740 for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
741 if (info->ocv_table[i])
742 devm_kfree(&psy->dev, info->ocv_table[i]);
745 if (info->resist_table)
746 devm_kfree(&psy->dev, info->resist_table);
748 EXPORT_SYMBOL_GPL(power_supply_put_battery_info);
751 * power_supply_temp2resist_simple() - find the battery internal resistance
752 * percent
753 * @table: Pointer to battery resistance temperature table
754 * @table_len: The table length
755 * @temp: Current temperature
757 * This helper function is used to look up battery internal resistance percent
758 * according to current temperature value from the resistance temperature table,
759 * and the table must be ordered descending. Then the actual battery internal
760 * resistance = the ideal battery internal resistance * percent / 100.
762 * Return: the battery internal resistance percent
764 int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table,
765 int table_len, int temp)
767 int i, resist;
769 for (i = 0; i < table_len; i++)
770 if (temp > table[i].temp)
771 break;
773 if (i > 0 && i < table_len) {
774 int tmp;
776 tmp = (table[i - 1].resistance - table[i].resistance) *
777 (temp - table[i].temp);
778 tmp /= table[i - 1].temp - table[i].temp;
779 resist = tmp + table[i].resistance;
780 } else if (i == 0) {
781 resist = table[0].resistance;
782 } else {
783 resist = table[table_len - 1].resistance;
786 return resist;
788 EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple);
791 * power_supply_ocv2cap_simple() - find the battery capacity
792 * @table: Pointer to battery OCV lookup table
793 * @table_len: OCV table length
794 * @ocv: Current OCV value
796 * This helper function is used to look up battery capacity according to
797 * current OCV value from one OCV table, and the OCV table must be ordered
798 * descending.
800 * Return: the battery capacity.
802 int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table,
803 int table_len, int ocv)
805 int i, cap, tmp;
807 for (i = 0; i < table_len; i++)
808 if (ocv > table[i].ocv)
809 break;
811 if (i > 0 && i < table_len) {
812 tmp = (table[i - 1].capacity - table[i].capacity) *
813 (ocv - table[i].ocv);
814 tmp /= table[i - 1].ocv - table[i].ocv;
815 cap = tmp + table[i].capacity;
816 } else if (i == 0) {
817 cap = table[0].capacity;
818 } else {
819 cap = table[table_len - 1].capacity;
822 return cap;
824 EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple);
826 struct power_supply_battery_ocv_table *
827 power_supply_find_ocv2cap_table(struct power_supply_battery_info *info,
828 int temp, int *table_len)
830 int best_temp_diff = INT_MAX, temp_diff;
831 u8 i, best_index = 0;
833 if (!info->ocv_table[0])
834 return NULL;
836 for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
837 temp_diff = abs(info->ocv_temp[i] - temp);
839 if (temp_diff < best_temp_diff) {
840 best_temp_diff = temp_diff;
841 best_index = i;
845 *table_len = info->ocv_table_size[best_index];
846 return info->ocv_table[best_index];
848 EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table);
850 int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info,
851 int ocv, int temp)
853 struct power_supply_battery_ocv_table *table;
854 int table_len;
856 table = power_supply_find_ocv2cap_table(info, temp, &table_len);
857 if (!table)
858 return -EINVAL;
860 return power_supply_ocv2cap_simple(table, table_len, ocv);
862 EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap);
864 int power_supply_get_property(struct power_supply *psy,
865 enum power_supply_property psp,
866 union power_supply_propval *val)
868 if (atomic_read(&psy->use_cnt) <= 0) {
869 if (!psy->initialized)
870 return -EAGAIN;
871 return -ENODEV;
874 return psy->desc->get_property(psy, psp, val);
876 EXPORT_SYMBOL_GPL(power_supply_get_property);
878 int power_supply_set_property(struct power_supply *psy,
879 enum power_supply_property psp,
880 const union power_supply_propval *val)
882 if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
883 return -ENODEV;
885 return psy->desc->set_property(psy, psp, val);
887 EXPORT_SYMBOL_GPL(power_supply_set_property);
889 int power_supply_property_is_writeable(struct power_supply *psy,
890 enum power_supply_property psp)
892 if (atomic_read(&psy->use_cnt) <= 0 ||
893 !psy->desc->property_is_writeable)
894 return -ENODEV;
896 return psy->desc->property_is_writeable(psy, psp);
898 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
900 void power_supply_external_power_changed(struct power_supply *psy)
902 if (atomic_read(&psy->use_cnt) <= 0 ||
903 !psy->desc->external_power_changed)
904 return;
906 psy->desc->external_power_changed(psy);
908 EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
910 int power_supply_powers(struct power_supply *psy, struct device *dev)
912 return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
914 EXPORT_SYMBOL_GPL(power_supply_powers);
916 static void power_supply_dev_release(struct device *dev)
918 struct power_supply *psy = to_power_supply(dev);
919 dev_dbg(dev, "%s\n", __func__);
920 kfree(psy);
923 int power_supply_reg_notifier(struct notifier_block *nb)
925 return atomic_notifier_chain_register(&power_supply_notifier, nb);
927 EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
929 void power_supply_unreg_notifier(struct notifier_block *nb)
931 atomic_notifier_chain_unregister(&power_supply_notifier, nb);
933 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
935 #ifdef CONFIG_THERMAL
936 static int power_supply_read_temp(struct thermal_zone_device *tzd,
937 int *temp)
939 struct power_supply *psy;
940 union power_supply_propval val;
941 int ret;
943 WARN_ON(tzd == NULL);
944 psy = tzd->devdata;
945 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
946 if (ret)
947 return ret;
949 /* Convert tenths of degree Celsius to milli degree Celsius. */
950 *temp = val.intval * 100;
952 return ret;
955 static struct thermal_zone_device_ops psy_tzd_ops = {
956 .get_temp = power_supply_read_temp,
959 static int psy_register_thermal(struct power_supply *psy)
961 int i, ret;
963 if (psy->desc->no_thermal)
964 return 0;
966 /* Register battery zone device psy reports temperature */
967 for (i = 0; i < psy->desc->num_properties; i++) {
968 if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
969 psy->tzd = thermal_zone_device_register(psy->desc->name,
970 0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
971 if (IS_ERR(psy->tzd))
972 return PTR_ERR(psy->tzd);
973 ret = thermal_zone_device_enable(psy->tzd);
974 if (ret)
975 thermal_zone_device_unregister(psy->tzd);
976 return ret;
979 return 0;
982 static void psy_unregister_thermal(struct power_supply *psy)
984 if (IS_ERR_OR_NULL(psy->tzd))
985 return;
986 thermal_zone_device_unregister(psy->tzd);
989 /* thermal cooling device callbacks */
990 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
991 unsigned long *state)
993 struct power_supply *psy;
994 union power_supply_propval val;
995 int ret;
997 psy = tcd->devdata;
998 ret = power_supply_get_property(psy,
999 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
1000 if (ret)
1001 return ret;
1003 *state = val.intval;
1005 return ret;
1008 static int ps_get_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
1009 unsigned long *state)
1011 struct power_supply *psy;
1012 union power_supply_propval val;
1013 int ret;
1015 psy = tcd->devdata;
1016 ret = power_supply_get_property(psy,
1017 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
1018 if (ret)
1019 return ret;
1021 *state = val.intval;
1023 return ret;
1026 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
1027 unsigned long state)
1029 struct power_supply *psy;
1030 union power_supply_propval val;
1031 int ret;
1033 psy = tcd->devdata;
1034 val.intval = state;
1035 ret = psy->desc->set_property(psy,
1036 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
1038 return ret;
1041 static const struct thermal_cooling_device_ops psy_tcd_ops = {
1042 .get_max_state = ps_get_max_charge_cntl_limit,
1043 .get_cur_state = ps_get_cur_charge_cntl_limit,
1044 .set_cur_state = ps_set_cur_charge_cntl_limit,
1047 static int psy_register_cooler(struct power_supply *psy)
1049 int i;
1051 /* Register for cooling device if psy can control charging */
1052 for (i = 0; i < psy->desc->num_properties; i++) {
1053 if (psy->desc->properties[i] ==
1054 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
1055 psy->tcd = thermal_cooling_device_register(
1056 (char *)psy->desc->name,
1057 psy, &psy_tcd_ops);
1058 return PTR_ERR_OR_ZERO(psy->tcd);
1061 return 0;
1064 static void psy_unregister_cooler(struct power_supply *psy)
1066 if (IS_ERR_OR_NULL(psy->tcd))
1067 return;
1068 thermal_cooling_device_unregister(psy->tcd);
1070 #else
1071 static int psy_register_thermal(struct power_supply *psy)
1073 return 0;
1076 static void psy_unregister_thermal(struct power_supply *psy)
1080 static int psy_register_cooler(struct power_supply *psy)
1082 return 0;
1085 static void psy_unregister_cooler(struct power_supply *psy)
1088 #endif
1090 static struct power_supply *__must_check
1091 __power_supply_register(struct device *parent,
1092 const struct power_supply_desc *desc,
1093 const struct power_supply_config *cfg,
1094 bool ws)
1096 struct device *dev;
1097 struct power_supply *psy;
1098 int i, rc;
1100 if (!parent)
1101 pr_warn("%s: Expected proper parent device for '%s'\n",
1102 __func__, desc->name);
1104 if (!desc || !desc->name || !desc->properties || !desc->num_properties)
1105 return ERR_PTR(-EINVAL);
1107 for (i = 0; i < desc->num_properties; ++i) {
1108 if ((desc->properties[i] == POWER_SUPPLY_PROP_USB_TYPE) &&
1109 (!desc->usb_types || !desc->num_usb_types))
1110 return ERR_PTR(-EINVAL);
1113 psy = kzalloc(sizeof(*psy), GFP_KERNEL);
1114 if (!psy)
1115 return ERR_PTR(-ENOMEM);
1117 dev = &psy->dev;
1119 device_initialize(dev);
1121 dev->class = power_supply_class;
1122 dev->type = &power_supply_dev_type;
1123 dev->parent = parent;
1124 dev->release = power_supply_dev_release;
1125 dev_set_drvdata(dev, psy);
1126 psy->desc = desc;
1127 if (cfg) {
1128 dev->groups = cfg->attr_grp;
1129 psy->drv_data = cfg->drv_data;
1130 psy->of_node =
1131 cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node;
1132 psy->supplied_to = cfg->supplied_to;
1133 psy->num_supplicants = cfg->num_supplicants;
1136 rc = dev_set_name(dev, "%s", desc->name);
1137 if (rc)
1138 goto dev_set_name_failed;
1140 INIT_WORK(&psy->changed_work, power_supply_changed_work);
1141 INIT_DELAYED_WORK(&psy->deferred_register_work,
1142 power_supply_deferred_register_work);
1144 rc = power_supply_check_supplies(psy);
1145 if (rc) {
1146 dev_info(dev, "Not all required supplies found, defer probe\n");
1147 goto check_supplies_failed;
1150 spin_lock_init(&psy->changed_lock);
1151 rc = device_add(dev);
1152 if (rc)
1153 goto device_add_failed;
1155 rc = device_init_wakeup(dev, ws);
1156 if (rc)
1157 goto wakeup_init_failed;
1159 rc = psy_register_thermal(psy);
1160 if (rc)
1161 goto register_thermal_failed;
1163 rc = psy_register_cooler(psy);
1164 if (rc)
1165 goto register_cooler_failed;
1167 rc = power_supply_create_triggers(psy);
1168 if (rc)
1169 goto create_triggers_failed;
1171 rc = power_supply_add_hwmon_sysfs(psy);
1172 if (rc)
1173 goto add_hwmon_sysfs_failed;
1176 * Update use_cnt after any uevents (most notably from device_add()).
1177 * We are here still during driver's probe but
1178 * the power_supply_uevent() calls back driver's get_property
1179 * method so:
1180 * 1. Driver did not assigned the returned struct power_supply,
1181 * 2. Driver could not finish initialization (anything in its probe
1182 * after calling power_supply_register()).
1184 atomic_inc(&psy->use_cnt);
1185 psy->initialized = true;
1187 queue_delayed_work(system_power_efficient_wq,
1188 &psy->deferred_register_work,
1189 POWER_SUPPLY_DEFERRED_REGISTER_TIME);
1191 return psy;
1193 add_hwmon_sysfs_failed:
1194 power_supply_remove_triggers(psy);
1195 create_triggers_failed:
1196 psy_unregister_cooler(psy);
1197 register_cooler_failed:
1198 psy_unregister_thermal(psy);
1199 register_thermal_failed:
1200 device_del(dev);
1201 wakeup_init_failed:
1202 device_add_failed:
1203 check_supplies_failed:
1204 dev_set_name_failed:
1205 put_device(dev);
1206 return ERR_PTR(rc);
1210 * power_supply_register() - Register new power supply
1211 * @parent: Device to be a parent of power supply's device, usually
1212 * the device which probe function calls this
1213 * @desc: Description of power supply, must be valid through whole
1214 * lifetime of this power supply
1215 * @cfg: Run-time specific configuration accessed during registering,
1216 * may be NULL
1218 * Return: A pointer to newly allocated power_supply on success
1219 * or ERR_PTR otherwise.
1220 * Use power_supply_unregister() on returned power_supply pointer to release
1221 * resources.
1223 struct power_supply *__must_check power_supply_register(struct device *parent,
1224 const struct power_supply_desc *desc,
1225 const struct power_supply_config *cfg)
1227 return __power_supply_register(parent, desc, cfg, true);
1229 EXPORT_SYMBOL_GPL(power_supply_register);
1232 * power_supply_register_no_ws() - Register new non-waking-source power supply
1233 * @parent: Device to be a parent of power supply's device, usually
1234 * the device which probe function calls this
1235 * @desc: Description of power supply, must be valid through whole
1236 * lifetime of this power supply
1237 * @cfg: Run-time specific configuration accessed during registering,
1238 * may be NULL
1240 * Return: A pointer to newly allocated power_supply on success
1241 * or ERR_PTR otherwise.
1242 * Use power_supply_unregister() on returned power_supply pointer to release
1243 * resources.
1245 struct power_supply *__must_check
1246 power_supply_register_no_ws(struct device *parent,
1247 const struct power_supply_desc *desc,
1248 const struct power_supply_config *cfg)
1250 return __power_supply_register(parent, desc, cfg, false);
1252 EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
1254 static void devm_power_supply_release(struct device *dev, void *res)
1256 struct power_supply **psy = res;
1258 power_supply_unregister(*psy);
1262 * devm_power_supply_register() - Register managed power supply
1263 * @parent: Device to be a parent of power supply's device, usually
1264 * the device which probe function calls this
1265 * @desc: Description of power supply, must be valid through whole
1266 * lifetime of this power supply
1267 * @cfg: Run-time specific configuration accessed during registering,
1268 * may be NULL
1270 * Return: A pointer to newly allocated power_supply on success
1271 * or ERR_PTR otherwise.
1272 * The returned power_supply pointer will be automatically unregistered
1273 * on driver detach.
1275 struct power_supply *__must_check
1276 devm_power_supply_register(struct device *parent,
1277 const struct power_supply_desc *desc,
1278 const struct power_supply_config *cfg)
1280 struct power_supply **ptr, *psy;
1282 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1284 if (!ptr)
1285 return ERR_PTR(-ENOMEM);
1286 psy = __power_supply_register(parent, desc, cfg, true);
1287 if (IS_ERR(psy)) {
1288 devres_free(ptr);
1289 } else {
1290 *ptr = psy;
1291 devres_add(parent, ptr);
1293 return psy;
1295 EXPORT_SYMBOL_GPL(devm_power_supply_register);
1298 * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
1299 * @parent: Device to be a parent of power supply's device, usually
1300 * the device which probe function calls this
1301 * @desc: Description of power supply, must be valid through whole
1302 * lifetime of this power supply
1303 * @cfg: Run-time specific configuration accessed during registering,
1304 * may be NULL
1306 * Return: A pointer to newly allocated power_supply on success
1307 * or ERR_PTR otherwise.
1308 * The returned power_supply pointer will be automatically unregistered
1309 * on driver detach.
1311 struct power_supply *__must_check
1312 devm_power_supply_register_no_ws(struct device *parent,
1313 const struct power_supply_desc *desc,
1314 const struct power_supply_config *cfg)
1316 struct power_supply **ptr, *psy;
1318 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1320 if (!ptr)
1321 return ERR_PTR(-ENOMEM);
1322 psy = __power_supply_register(parent, desc, cfg, false);
1323 if (IS_ERR(psy)) {
1324 devres_free(ptr);
1325 } else {
1326 *ptr = psy;
1327 devres_add(parent, ptr);
1329 return psy;
1331 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
1334 * power_supply_unregister() - Remove this power supply from system
1335 * @psy: Pointer to power supply to unregister
1337 * Remove this power supply from the system. The resources of power supply
1338 * will be freed here or on last power_supply_put() call.
1340 void power_supply_unregister(struct power_supply *psy)
1342 WARN_ON(atomic_dec_return(&psy->use_cnt));
1343 psy->removing = true;
1344 cancel_work_sync(&psy->changed_work);
1345 cancel_delayed_work_sync(&psy->deferred_register_work);
1346 sysfs_remove_link(&psy->dev.kobj, "powers");
1347 power_supply_remove_hwmon_sysfs(psy);
1348 power_supply_remove_triggers(psy);
1349 psy_unregister_cooler(psy);
1350 psy_unregister_thermal(psy);
1351 device_init_wakeup(&psy->dev, false);
1352 device_unregister(&psy->dev);
1354 EXPORT_SYMBOL_GPL(power_supply_unregister);
1356 void *power_supply_get_drvdata(struct power_supply *psy)
1358 return psy->drv_data;
1360 EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
1362 static int __init power_supply_class_init(void)
1364 power_supply_class = class_create(THIS_MODULE, "power_supply");
1366 if (IS_ERR(power_supply_class))
1367 return PTR_ERR(power_supply_class);
1369 power_supply_class->dev_uevent = power_supply_uevent;
1370 power_supply_init_attrs(&power_supply_dev_type);
1372 return 0;
1375 static void __exit power_supply_class_exit(void)
1377 class_destroy(power_supply_class);
1380 subsys_initcall(power_supply_class_init);
1381 module_exit(power_supply_class_exit);
1383 MODULE_DESCRIPTION("Universal power supply monitor class");
1384 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
1385 "Szabolcs Gyurko, "
1386 "Anton Vorontsov <cbou@mail.ru>");
1387 MODULE_LICENSE("GPL");