ACPI / property: Document RS485 _DSD properties
[linux-2.6-microblaze.git] / drivers / power / supply / power_supply_core.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Universal power supply monitor class
4  *
5  *  Copyright © 2007  Anton Vorontsov <cbou@mail.ru>
6  *  Copyright © 2004  Szabolcs Gyurko
7  *  Copyright © 2003  Ian Molton <spyro@f2s.com>
8  *
9  *  Modified: 2004, Oct     Szabolcs Gyurko
10  */
11
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 <linux/fixp-arith.h>
25 #include "power_supply.h"
26 #include "samsung-sdi-battery.h"
27
28 /* exported for the APM Power driver, APM emulation */
29 struct class *power_supply_class;
30 EXPORT_SYMBOL_GPL(power_supply_class);
31
32 ATOMIC_NOTIFIER_HEAD(power_supply_notifier);
33 EXPORT_SYMBOL_GPL(power_supply_notifier);
34
35 static struct device_type power_supply_dev_type;
36
37 #define POWER_SUPPLY_DEFERRED_REGISTER_TIME     msecs_to_jiffies(10)
38
39 static bool __power_supply_is_supplied_by(struct power_supply *supplier,
40                                          struct power_supply *supply)
41 {
42         int i;
43
44         if (!supply->supplied_from && !supplier->supplied_to)
45                 return false;
46
47         /* Support both supplied_to and supplied_from modes */
48         if (supply->supplied_from) {
49                 if (!supplier->desc->name)
50                         return false;
51                 for (i = 0; i < supply->num_supplies; i++)
52                         if (!strcmp(supplier->desc->name, supply->supplied_from[i]))
53                                 return true;
54         } else {
55                 if (!supply->desc->name)
56                         return false;
57                 for (i = 0; i < supplier->num_supplicants; i++)
58                         if (!strcmp(supplier->supplied_to[i], supply->desc->name))
59                                 return true;
60         }
61
62         return false;
63 }
64
65 static int __power_supply_changed_work(struct device *dev, void *data)
66 {
67         struct power_supply *psy = data;
68         struct power_supply *pst = dev_get_drvdata(dev);
69
70         if (__power_supply_is_supplied_by(psy, pst)) {
71                 if (pst->desc->external_power_changed)
72                         pst->desc->external_power_changed(pst);
73         }
74
75         return 0;
76 }
77
78 static void power_supply_changed_work(struct work_struct *work)
79 {
80         unsigned long flags;
81         struct power_supply *psy = container_of(work, struct power_supply,
82                                                 changed_work);
83
84         dev_dbg(&psy->dev, "%s\n", __func__);
85
86         spin_lock_irqsave(&psy->changed_lock, flags);
87         /*
88          * Check 'changed' here to avoid issues due to race between
89          * power_supply_changed() and this routine. In worst case
90          * power_supply_changed() can be called again just before we take above
91          * lock. During the first call of this routine we will mark 'changed' as
92          * false and it will stay false for the next call as well.
93          */
94         if (likely(psy->changed)) {
95                 psy->changed = false;
96                 spin_unlock_irqrestore(&psy->changed_lock, flags);
97                 class_for_each_device(power_supply_class, NULL, psy,
98                                       __power_supply_changed_work);
99                 power_supply_update_leds(psy);
100                 atomic_notifier_call_chain(&power_supply_notifier,
101                                 PSY_EVENT_PROP_CHANGED, psy);
102                 kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
103                 spin_lock_irqsave(&psy->changed_lock, flags);
104         }
105
106         /*
107          * Hold the wakeup_source until all events are processed.
108          * power_supply_changed() might have called again and have set 'changed'
109          * to true.
110          */
111         if (likely(!psy->changed))
112                 pm_relax(&psy->dev);
113         spin_unlock_irqrestore(&psy->changed_lock, flags);
114 }
115
116 void power_supply_changed(struct power_supply *psy)
117 {
118         unsigned long flags;
119
120         dev_dbg(&psy->dev, "%s\n", __func__);
121
122         spin_lock_irqsave(&psy->changed_lock, flags);
123         psy->changed = true;
124         pm_stay_awake(&psy->dev);
125         spin_unlock_irqrestore(&psy->changed_lock, flags);
126         schedule_work(&psy->changed_work);
127 }
128 EXPORT_SYMBOL_GPL(power_supply_changed);
129
130 /*
131  * Notify that power supply was registered after parent finished the probing.
132  *
133  * Often power supply is registered from driver's probe function. However
134  * calling power_supply_changed() directly from power_supply_register()
135  * would lead to execution of get_property() function provided by the driver
136  * too early - before the probe ends.
137  *
138  * Avoid that by waiting on parent's mutex.
139  */
140 static void power_supply_deferred_register_work(struct work_struct *work)
141 {
142         struct power_supply *psy = container_of(work, struct power_supply,
143                                                 deferred_register_work.work);
144
145         if (psy->dev.parent) {
146                 while (!mutex_trylock(&psy->dev.parent->mutex)) {
147                         if (psy->removing)
148                                 return;
149                         msleep(10);
150                 }
151         }
152
153         power_supply_changed(psy);
154
155         if (psy->dev.parent)
156                 mutex_unlock(&psy->dev.parent->mutex);
157 }
158
159 #ifdef CONFIG_OF
160 static int __power_supply_populate_supplied_from(struct device *dev,
161                                                  void *data)
162 {
163         struct power_supply *psy = data;
164         struct power_supply *epsy = dev_get_drvdata(dev);
165         struct device_node *np;
166         int i = 0;
167
168         do {
169                 np = of_parse_phandle(psy->of_node, "power-supplies", i++);
170                 if (!np)
171                         break;
172
173                 if (np == epsy->of_node) {
174                         dev_dbg(&psy->dev, "%s: Found supply : %s\n",
175                                 psy->desc->name, epsy->desc->name);
176                         psy->supplied_from[i-1] = (char *)epsy->desc->name;
177                         psy->num_supplies++;
178                         of_node_put(np);
179                         break;
180                 }
181                 of_node_put(np);
182         } while (np);
183
184         return 0;
185 }
186
187 static int power_supply_populate_supplied_from(struct power_supply *psy)
188 {
189         int error;
190
191         error = class_for_each_device(power_supply_class, NULL, psy,
192                                       __power_supply_populate_supplied_from);
193
194         dev_dbg(&psy->dev, "%s %d\n", __func__, error);
195
196         return error;
197 }
198
199 static int  __power_supply_find_supply_from_node(struct device *dev,
200                                                  void *data)
201 {
202         struct device_node *np = data;
203         struct power_supply *epsy = dev_get_drvdata(dev);
204
205         /* returning non-zero breaks out of class_for_each_device loop */
206         if (epsy->of_node == np)
207                 return 1;
208
209         return 0;
210 }
211
212 static int power_supply_find_supply_from_node(struct device_node *supply_node)
213 {
214         int error;
215
216         /*
217          * class_for_each_device() either returns its own errors or values
218          * returned by __power_supply_find_supply_from_node().
219          *
220          * __power_supply_find_supply_from_node() will return 0 (no match)
221          * or 1 (match).
222          *
223          * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
224          * it returned 0, or error as returned by it.
225          */
226         error = class_for_each_device(power_supply_class, NULL, supply_node,
227                                        __power_supply_find_supply_from_node);
228
229         return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
230 }
231
232 static int power_supply_check_supplies(struct power_supply *psy)
233 {
234         struct device_node *np;
235         int cnt = 0;
236
237         /* If there is already a list honor it */
238         if (psy->supplied_from && psy->num_supplies > 0)
239                 return 0;
240
241         /* No device node found, nothing to do */
242         if (!psy->of_node)
243                 return 0;
244
245         do {
246                 int ret;
247
248                 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
249                 if (!np)
250                         break;
251
252                 ret = power_supply_find_supply_from_node(np);
253                 of_node_put(np);
254
255                 if (ret) {
256                         dev_dbg(&psy->dev, "Failed to find supply!\n");
257                         return ret;
258                 }
259         } while (np);
260
261         /* Missing valid "power-supplies" entries */
262         if (cnt == 1)
263                 return 0;
264
265         /* All supplies found, allocate char ** array for filling */
266         psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
267                                           GFP_KERNEL);
268         if (!psy->supplied_from)
269                 return -ENOMEM;
270
271         *psy->supplied_from = devm_kcalloc(&psy->dev,
272                                            cnt - 1, sizeof(char *),
273                                            GFP_KERNEL);
274         if (!*psy->supplied_from)
275                 return -ENOMEM;
276
277         return power_supply_populate_supplied_from(psy);
278 }
279 #else
280 static int power_supply_check_supplies(struct power_supply *psy)
281 {
282         int nval, ret;
283
284         if (!psy->dev.parent)
285                 return 0;
286
287         nval = device_property_string_array_count(psy->dev.parent, "supplied-from");
288         if (nval <= 0)
289                 return 0;
290
291         psy->supplied_from = devm_kmalloc_array(&psy->dev, nval,
292                                                 sizeof(char *), GFP_KERNEL);
293         if (!psy->supplied_from)
294                 return -ENOMEM;
295
296         ret = device_property_read_string_array(psy->dev.parent,
297                 "supplied-from", (const char **)psy->supplied_from, nval);
298         if (ret < 0)
299                 return ret;
300
301         psy->num_supplies = nval;
302
303         return 0;
304 }
305 #endif
306
307 struct psy_am_i_supplied_data {
308         struct power_supply *psy;
309         unsigned int count;
310 };
311
312 static int __power_supply_am_i_supplied(struct device *dev, void *_data)
313 {
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;
317
318         if (__power_supply_is_supplied_by(epsy, data->psy)) {
319                 data->count++;
320                 if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
321                                         &ret))
322                         return ret.intval;
323         }
324
325         return 0;
326 }
327
328 int power_supply_am_i_supplied(struct power_supply *psy)
329 {
330         struct psy_am_i_supplied_data data = { psy, 0 };
331         int error;
332
333         error = class_for_each_device(power_supply_class, NULL, &data,
334                                       __power_supply_am_i_supplied);
335
336         dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
337
338         if (data.count == 0)
339                 return -ENODEV;
340
341         return error;
342 }
343 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
344
345 static int __power_supply_is_system_supplied(struct device *dev, void *data)
346 {
347         union power_supply_propval ret = {0,};
348         struct power_supply *psy = dev_get_drvdata(dev);
349         unsigned int *count = data;
350
351         (*count)++;
352         if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
353                 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
354                                         &ret))
355                         return ret.intval;
356
357         return 0;
358 }
359
360 int power_supply_is_system_supplied(void)
361 {
362         int error;
363         unsigned int count = 0;
364
365         error = class_for_each_device(power_supply_class, NULL, &count,
366                                       __power_supply_is_system_supplied);
367
368         /*
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.
371          */
372         if (count == 0)
373                 return 1;
374
375         return error;
376 }
377 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
378
379 struct psy_get_supplier_prop_data {
380         struct power_supply *psy;
381         enum power_supply_property psp;
382         union power_supply_propval *val;
383 };
384
385 static int __power_supply_get_supplier_property(struct device *dev, void *_data)
386 {
387         struct power_supply *epsy = dev_get_drvdata(dev);
388         struct psy_get_supplier_prop_data *data = _data;
389
390         if (__power_supply_is_supplied_by(epsy, data->psy))
391                 if (!epsy->desc->get_property(epsy, data->psp, data->val))
392                         return 1; /* Success */
393
394         return 0; /* Continue iterating */
395 }
396
397 int power_supply_get_property_from_supplier(struct power_supply *psy,
398                                             enum power_supply_property psp,
399                                             union power_supply_propval *val)
400 {
401         struct psy_get_supplier_prop_data data = {
402                 .psy = psy,
403                 .psp = psp,
404                 .val = val,
405         };
406         int ret;
407
408         /*
409          * This function is not intended for use with a supply with multiple
410          * suppliers, we simply pick the first supply to report the psp.
411          */
412         ret = class_for_each_device(power_supply_class, NULL, &data,
413                                     __power_supply_get_supplier_property);
414         if (ret < 0)
415                 return ret;
416         if (ret == 0)
417                 return -ENODEV;
418
419         return 0;
420 }
421 EXPORT_SYMBOL_GPL(power_supply_get_property_from_supplier);
422
423 int power_supply_set_battery_charged(struct power_supply *psy)
424 {
425         if (atomic_read(&psy->use_cnt) >= 0 &&
426                         psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
427                         psy->desc->set_charged) {
428                 psy->desc->set_charged(psy);
429                 return 0;
430         }
431
432         return -EINVAL;
433 }
434 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
435
436 static int power_supply_match_device_by_name(struct device *dev, const void *data)
437 {
438         const char *name = data;
439         struct power_supply *psy = dev_get_drvdata(dev);
440
441         return strcmp(psy->desc->name, name) == 0;
442 }
443
444 /**
445  * power_supply_get_by_name() - Search for a power supply and returns its ref
446  * @name: Power supply name to fetch
447  *
448  * If power supply was found, it increases reference count for the
449  * internal power supply's device. The user should power_supply_put()
450  * after usage.
451  *
452  * Return: On success returns a reference to a power supply with
453  * matching name equals to @name, a NULL otherwise.
454  */
455 struct power_supply *power_supply_get_by_name(const char *name)
456 {
457         struct power_supply *psy = NULL;
458         struct device *dev = class_find_device(power_supply_class, NULL, name,
459                                         power_supply_match_device_by_name);
460
461         if (dev) {
462                 psy = dev_get_drvdata(dev);
463                 atomic_inc(&psy->use_cnt);
464         }
465
466         return psy;
467 }
468 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
469
470 /**
471  * power_supply_put() - Drop reference obtained with power_supply_get_by_name
472  * @psy: Reference to put
473  *
474  * The reference to power supply should be put before unregistering
475  * the power supply.
476  */
477 void power_supply_put(struct power_supply *psy)
478 {
479         might_sleep();
480
481         atomic_dec(&psy->use_cnt);
482         put_device(&psy->dev);
483 }
484 EXPORT_SYMBOL_GPL(power_supply_put);
485
486 #ifdef CONFIG_OF
487 static int power_supply_match_device_node(struct device *dev, const void *data)
488 {
489         return dev->parent && dev->parent->of_node == data;
490 }
491
492 /**
493  * power_supply_get_by_phandle() - Search for a power supply and returns its ref
494  * @np: Pointer to device node holding phandle property
495  * @property: Name of property holding a power supply name
496  *
497  * If power supply was found, it increases reference count for the
498  * internal power supply's device. The user should power_supply_put()
499  * after usage.
500  *
501  * Return: On success returns a reference to a power supply with
502  * matching name equals to value under @property, NULL or ERR_PTR otherwise.
503  */
504 struct power_supply *power_supply_get_by_phandle(struct device_node *np,
505                                                         const char *property)
506 {
507         struct device_node *power_supply_np;
508         struct power_supply *psy = NULL;
509         struct device *dev;
510
511         power_supply_np = of_parse_phandle(np, property, 0);
512         if (!power_supply_np)
513                 return ERR_PTR(-ENODEV);
514
515         dev = class_find_device(power_supply_class, NULL, power_supply_np,
516                                                 power_supply_match_device_node);
517
518         of_node_put(power_supply_np);
519
520         if (dev) {
521                 psy = dev_get_drvdata(dev);
522                 atomic_inc(&psy->use_cnt);
523         }
524
525         return psy;
526 }
527 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
528
529 static void devm_power_supply_put(struct device *dev, void *res)
530 {
531         struct power_supply **psy = res;
532
533         power_supply_put(*psy);
534 }
535
536 /**
537  * devm_power_supply_get_by_phandle() - Resource managed version of
538  *  power_supply_get_by_phandle()
539  * @dev: Pointer to device holding phandle property
540  * @property: Name of property holding a power supply phandle
541  *
542  * Return: On success returns a reference to a power supply with
543  * matching name equals to value under @property, NULL or ERR_PTR otherwise.
544  */
545 struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
546                                                       const char *property)
547 {
548         struct power_supply **ptr, *psy;
549
550         if (!dev->of_node)
551                 return ERR_PTR(-ENODEV);
552
553         ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
554         if (!ptr)
555                 return ERR_PTR(-ENOMEM);
556
557         psy = power_supply_get_by_phandle(dev->of_node, property);
558         if (IS_ERR_OR_NULL(psy)) {
559                 devres_free(ptr);
560         } else {
561                 *ptr = psy;
562                 devres_add(dev, ptr);
563         }
564         return psy;
565 }
566 EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
567 #endif /* CONFIG_OF */
568
569 int power_supply_get_battery_info(struct power_supply *psy,
570                                   struct power_supply_battery_info **info_out)
571 {
572         struct power_supply_resistance_temp_table *resist_table;
573         struct power_supply_battery_info *info;
574         struct device_node *battery_np = NULL;
575         struct fwnode_reference_args args;
576         struct fwnode_handle *fwnode;
577         const char *value;
578         int err, len, index;
579         const __be32 *list;
580         u32 min_max[2];
581
582         if (psy->of_node) {
583                 battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);
584                 if (!battery_np)
585                         return -ENODEV;
586
587                 fwnode = fwnode_handle_get(of_fwnode_handle(battery_np));
588         } else {
589                 err = fwnode_property_get_reference_args(
590                                         dev_fwnode(psy->dev.parent),
591                                         "monitored-battery", NULL, 0, 0, &args);
592                 if (err)
593                         return err;
594
595                 fwnode = args.fwnode;
596         }
597
598         err = fwnode_property_read_string(fwnode, "compatible", &value);
599         if (err)
600                 goto out_put_node;
601
602
603         /* Try static batteries first */
604         err = samsung_sdi_battery_get_info(&psy->dev, value, &info);
605         if (!err)
606                 goto out_ret_pointer;
607
608         if (strcmp("simple-battery", value)) {
609                 err = -ENODEV;
610                 goto out_put_node;
611         }
612
613         info = devm_kmalloc(&psy->dev, sizeof(*info), GFP_KERNEL);
614         if (!info) {
615                 err = -ENOMEM;
616                 goto out_put_node;
617         }
618
619         info->technology                     = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
620         info->energy_full_design_uwh         = -EINVAL;
621         info->charge_full_design_uah         = -EINVAL;
622         info->voltage_min_design_uv          = -EINVAL;
623         info->voltage_max_design_uv          = -EINVAL;
624         info->precharge_current_ua           = -EINVAL;
625         info->charge_term_current_ua         = -EINVAL;
626         info->constant_charge_current_max_ua = -EINVAL;
627         info->constant_charge_voltage_max_uv = -EINVAL;
628         info->tricklecharge_current_ua       = -EINVAL;
629         info->precharge_voltage_max_uv       = -EINVAL;
630         info->charge_restart_voltage_uv      = -EINVAL;
631         info->overvoltage_limit_uv           = -EINVAL;
632         info->maintenance_charge             = NULL;
633         info->alert_low_temp_charge_current_ua = -EINVAL;
634         info->alert_low_temp_charge_voltage_uv = -EINVAL;
635         info->alert_high_temp_charge_current_ua = -EINVAL;
636         info->alert_high_temp_charge_voltage_uv = -EINVAL;
637         info->temp_ambient_alert_min         = INT_MIN;
638         info->temp_ambient_alert_max         = INT_MAX;
639         info->temp_alert_min                 = INT_MIN;
640         info->temp_alert_max                 = INT_MAX;
641         info->temp_min                       = INT_MIN;
642         info->temp_max                       = INT_MAX;
643         info->factory_internal_resistance_uohm  = -EINVAL;
644         info->resist_table                   = NULL;
645         info->bti_resistance_ohm             = -EINVAL;
646         info->bti_resistance_tolerance       = -EINVAL;
647
648         for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) {
649                 info->ocv_table[index]       = NULL;
650                 info->ocv_temp[index]        = -EINVAL;
651                 info->ocv_table_size[index]  = -EINVAL;
652         }
653
654         /* The property and field names below must correspond to elements
655          * in enum power_supply_property. For reasoning, see
656          * Documentation/power/power_supply_class.rst.
657          */
658
659         if (!fwnode_property_read_string(fwnode, "device-chemistry", &value)) {
660                 if (!strcmp("nickel-cadmium", value))
661                         info->technology = POWER_SUPPLY_TECHNOLOGY_NiCd;
662                 else if (!strcmp("nickel-metal-hydride", value))
663                         info->technology = POWER_SUPPLY_TECHNOLOGY_NiMH;
664                 else if (!strcmp("lithium-ion", value))
665                         /* Imprecise lithium-ion type */
666                         info->technology = POWER_SUPPLY_TECHNOLOGY_LION;
667                 else if (!strcmp("lithium-ion-polymer", value))
668                         info->technology = POWER_SUPPLY_TECHNOLOGY_LIPO;
669                 else if (!strcmp("lithium-ion-iron-phosphate", value))
670                         info->technology = POWER_SUPPLY_TECHNOLOGY_LiFe;
671                 else if (!strcmp("lithium-ion-manganese-oxide", value))
672                         info->technology = POWER_SUPPLY_TECHNOLOGY_LiMn;
673                 else
674                         dev_warn(&psy->dev, "%s unknown battery type\n", value);
675         }
676
677         fwnode_property_read_u32(fwnode, "energy-full-design-microwatt-hours",
678                              &info->energy_full_design_uwh);
679         fwnode_property_read_u32(fwnode, "charge-full-design-microamp-hours",
680                              &info->charge_full_design_uah);
681         fwnode_property_read_u32(fwnode, "voltage-min-design-microvolt",
682                              &info->voltage_min_design_uv);
683         fwnode_property_read_u32(fwnode, "voltage-max-design-microvolt",
684                              &info->voltage_max_design_uv);
685         fwnode_property_read_u32(fwnode, "trickle-charge-current-microamp",
686                              &info->tricklecharge_current_ua);
687         fwnode_property_read_u32(fwnode, "precharge-current-microamp",
688                              &info->precharge_current_ua);
689         fwnode_property_read_u32(fwnode, "precharge-upper-limit-microvolt",
690                              &info->precharge_voltage_max_uv);
691         fwnode_property_read_u32(fwnode, "charge-term-current-microamp",
692                              &info->charge_term_current_ua);
693         fwnode_property_read_u32(fwnode, "re-charge-voltage-microvolt",
694                              &info->charge_restart_voltage_uv);
695         fwnode_property_read_u32(fwnode, "over-voltage-threshold-microvolt",
696                              &info->overvoltage_limit_uv);
697         fwnode_property_read_u32(fwnode, "constant-charge-current-max-microamp",
698                              &info->constant_charge_current_max_ua);
699         fwnode_property_read_u32(fwnode, "constant-charge-voltage-max-microvolt",
700                              &info->constant_charge_voltage_max_uv);
701         fwnode_property_read_u32(fwnode, "factory-internal-resistance-micro-ohms",
702                              &info->factory_internal_resistance_uohm);
703
704         if (!fwnode_property_read_u32_array(fwnode, "ambient-celsius",
705                                             min_max, ARRAY_SIZE(min_max))) {
706                 info->temp_ambient_alert_min = min_max[0];
707                 info->temp_ambient_alert_max = min_max[1];
708         }
709         if (!fwnode_property_read_u32_array(fwnode, "alert-celsius",
710                                             min_max, ARRAY_SIZE(min_max))) {
711                 info->temp_alert_min = min_max[0];
712                 info->temp_alert_max = min_max[1];
713         }
714         if (!fwnode_property_read_u32_array(fwnode, "operating-range-celsius",
715                                             min_max, ARRAY_SIZE(min_max))) {
716                 info->temp_min = min_max[0];
717                 info->temp_max = min_max[1];
718         }
719
720         /*
721          * The below code uses raw of-data parsing to parse
722          * /schemas/types.yaml#/definitions/uint32-matrix
723          * data, so for now this is only support with of.
724          */
725         if (!battery_np)
726                 goto out_ret_pointer;
727
728         len = of_property_count_u32_elems(battery_np, "ocv-capacity-celsius");
729         if (len < 0 && len != -EINVAL) {
730                 err = len;
731                 goto out_put_node;
732         } else if (len > POWER_SUPPLY_OCV_TEMP_MAX) {
733                 dev_err(&psy->dev, "Too many temperature values\n");
734                 err = -EINVAL;
735                 goto out_put_node;
736         } else if (len > 0) {
737                 of_property_read_u32_array(battery_np, "ocv-capacity-celsius",
738                                            info->ocv_temp, len);
739         }
740
741         for (index = 0; index < len; index++) {
742                 struct power_supply_battery_ocv_table *table;
743                 char *propname;
744                 int i, tab_len, size;
745
746                 propname = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d", index);
747                 list = of_get_property(battery_np, propname, &size);
748                 if (!list || !size) {
749                         dev_err(&psy->dev, "failed to get %s\n", propname);
750                         kfree(propname);
751                         power_supply_put_battery_info(psy, info);
752                         err = -EINVAL;
753                         goto out_put_node;
754                 }
755
756                 kfree(propname);
757                 tab_len = size / (2 * sizeof(__be32));
758                 info->ocv_table_size[index] = tab_len;
759
760                 table = info->ocv_table[index] =
761                         devm_kcalloc(&psy->dev, tab_len, sizeof(*table), GFP_KERNEL);
762                 if (!info->ocv_table[index]) {
763                         power_supply_put_battery_info(psy, info);
764                         err = -ENOMEM;
765                         goto out_put_node;
766                 }
767
768                 for (i = 0; i < tab_len; i++) {
769                         table[i].ocv = be32_to_cpu(*list);
770                         list++;
771                         table[i].capacity = be32_to_cpu(*list);
772                         list++;
773                 }
774         }
775
776         list = of_get_property(battery_np, "resistance-temp-table", &len);
777         if (!list || !len)
778                 goto out_ret_pointer;
779
780         info->resist_table_size = len / (2 * sizeof(__be32));
781         resist_table = info->resist_table = devm_kcalloc(&psy->dev,
782                                                          info->resist_table_size,
783                                                          sizeof(*resist_table),
784                                                          GFP_KERNEL);
785         if (!info->resist_table) {
786                 power_supply_put_battery_info(psy, info);
787                 err = -ENOMEM;
788                 goto out_put_node;
789         }
790
791         for (index = 0; index < info->resist_table_size; index++) {
792                 resist_table[index].temp = be32_to_cpu(*list++);
793                 resist_table[index].resistance = be32_to_cpu(*list++);
794         }
795
796 out_ret_pointer:
797         /* Finally return the whole thing */
798         *info_out = info;
799
800 out_put_node:
801         fwnode_handle_put(fwnode);
802         of_node_put(battery_np);
803         return err;
804 }
805 EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
806
807 void power_supply_put_battery_info(struct power_supply *psy,
808                                    struct power_supply_battery_info *info)
809 {
810         int i;
811
812         for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
813                 if (info->ocv_table[i])
814                         devm_kfree(&psy->dev, info->ocv_table[i]);
815         }
816
817         if (info->resist_table)
818                 devm_kfree(&psy->dev, info->resist_table);
819
820         devm_kfree(&psy->dev, info);
821 }
822 EXPORT_SYMBOL_GPL(power_supply_put_battery_info);
823
824 /**
825  * power_supply_temp2resist_simple() - find the battery internal resistance
826  * percent from temperature
827  * @table: Pointer to battery resistance temperature table
828  * @table_len: The table length
829  * @temp: Current temperature
830  *
831  * This helper function is used to look up battery internal resistance percent
832  * according to current temperature value from the resistance temperature table,
833  * and the table must be ordered descending. Then the actual battery internal
834  * resistance = the ideal battery internal resistance * percent / 100.
835  *
836  * Return: the battery internal resistance percent
837  */
838 int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table,
839                                     int table_len, int temp)
840 {
841         int i, high, low;
842
843         /* Break loop at table_len - 1 because that is the highest index */
844         for (i = 0; i < table_len - 1; i++)
845                 if (temp > table[i].temp)
846                         break;
847
848         /* The library function will deal with high == low */
849         if ((i == 0) || (i == (table_len - 1)))
850                 high = i;
851         else
852                 high = i - 1;
853         low = i;
854
855         return fixp_linear_interpolate(table[low].temp,
856                                        table[low].resistance,
857                                        table[high].temp,
858                                        table[high].resistance,
859                                        temp);
860 }
861 EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple);
862
863 /**
864  * power_supply_vbat2ri() - find the battery internal resistance
865  * from the battery voltage
866  * @info: The battery information container
867  * @table: Pointer to battery resistance temperature table
868  * @vbat_uv: The battery voltage in microvolt
869  * @charging: If we are charging (true) or not (false)
870  *
871  * This helper function is used to look up battery internal resistance
872  * according to current battery voltage. Depending on whether the battery
873  * is currently charging or not, different resistance will be returned.
874  *
875  * Returns the internal resistance in microohm or negative error code.
876  */
877 int power_supply_vbat2ri(struct power_supply_battery_info *info,
878                          int vbat_uv, bool charging)
879 {
880         struct power_supply_vbat_ri_table *vbat2ri;
881         int table_len;
882         int i, high, low;
883
884         /*
885          * If we are charging, and the battery supplies a separate table
886          * for this state, we use that in order to compensate for the
887          * charging voltage. Otherwise we use the main table.
888          */
889         if (charging && info->vbat2ri_charging) {
890                 vbat2ri = info->vbat2ri_charging;
891                 table_len = info->vbat2ri_charging_size;
892         } else {
893                 vbat2ri = info->vbat2ri_discharging;
894                 table_len = info->vbat2ri_discharging_size;
895         }
896
897         /*
898          * If no tables are specified, or if we are above the highest voltage in
899          * the voltage table, just return the factory specified internal resistance.
900          */
901         if (!vbat2ri || (table_len <= 0) || (vbat_uv > vbat2ri[0].vbat_uv)) {
902                 if (charging && (info->factory_internal_resistance_charging_uohm > 0))
903                         return info->factory_internal_resistance_charging_uohm;
904                 else
905                         return info->factory_internal_resistance_uohm;
906         }
907
908         /* Break loop at table_len - 1 because that is the highest index */
909         for (i = 0; i < table_len - 1; i++)
910                 if (vbat_uv > vbat2ri[i].vbat_uv)
911                         break;
912
913         /* The library function will deal with high == low */
914         if ((i == 0) || (i == (table_len - 1)))
915                 high = i;
916         else
917                 high = i - 1;
918         low = i;
919
920         return fixp_linear_interpolate(vbat2ri[low].vbat_uv,
921                                        vbat2ri[low].ri_uohm,
922                                        vbat2ri[high].vbat_uv,
923                                        vbat2ri[high].ri_uohm,
924                                        vbat_uv);
925 }
926 EXPORT_SYMBOL_GPL(power_supply_vbat2ri);
927
928 struct power_supply_maintenance_charge_table *
929 power_supply_get_maintenance_charging_setting(struct power_supply_battery_info *info,
930                                               int index)
931 {
932         if (index >= info->maintenance_charge_size)
933                 return NULL;
934         return &info->maintenance_charge[index];
935 }
936 EXPORT_SYMBOL_GPL(power_supply_get_maintenance_charging_setting);
937
938 /**
939  * power_supply_ocv2cap_simple() - find the battery capacity
940  * @table: Pointer to battery OCV lookup table
941  * @table_len: OCV table length
942  * @ocv: Current OCV value
943  *
944  * This helper function is used to look up battery capacity according to
945  * current OCV value from one OCV table, and the OCV table must be ordered
946  * descending.
947  *
948  * Return: the battery capacity.
949  */
950 int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table,
951                                 int table_len, int ocv)
952 {
953         int i, high, low;
954
955         /* Break loop at table_len - 1 because that is the highest index */
956         for (i = 0; i < table_len - 1; i++)
957                 if (ocv > table[i].ocv)
958                         break;
959
960         /* The library function will deal with high == low */
961         if ((i == 0) || (i == (table_len - 1)))
962                 high = i - 1;
963         else
964                 high = i; /* i.e. i == 0 */
965         low = i;
966
967         return fixp_linear_interpolate(table[low].ocv,
968                                        table[low].capacity,
969                                        table[high].ocv,
970                                        table[high].capacity,
971                                        ocv);
972 }
973 EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple);
974
975 struct power_supply_battery_ocv_table *
976 power_supply_find_ocv2cap_table(struct power_supply_battery_info *info,
977                                 int temp, int *table_len)
978 {
979         int best_temp_diff = INT_MAX, temp_diff;
980         u8 i, best_index = 0;
981
982         if (!info->ocv_table[0])
983                 return NULL;
984
985         for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
986                 /* Out of capacity tables */
987                 if (!info->ocv_table[i])
988                         break;
989
990                 temp_diff = abs(info->ocv_temp[i] - temp);
991
992                 if (temp_diff < best_temp_diff) {
993                         best_temp_diff = temp_diff;
994                         best_index = i;
995                 }
996         }
997
998         *table_len = info->ocv_table_size[best_index];
999         return info->ocv_table[best_index];
1000 }
1001 EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table);
1002
1003 int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info,
1004                                  int ocv, int temp)
1005 {
1006         struct power_supply_battery_ocv_table *table;
1007         int table_len;
1008
1009         table = power_supply_find_ocv2cap_table(info, temp, &table_len);
1010         if (!table)
1011                 return -EINVAL;
1012
1013         return power_supply_ocv2cap_simple(table, table_len, ocv);
1014 }
1015 EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap);
1016
1017 bool power_supply_battery_bti_in_range(struct power_supply_battery_info *info,
1018                                        int resistance)
1019 {
1020         int low, high;
1021
1022         /* Nothing like this can be checked */
1023         if (info->bti_resistance_ohm <= 0)
1024                 return false;
1025
1026         /* This will be extremely strict and unlikely to work */
1027         if (info->bti_resistance_tolerance <= 0)
1028                 return (info->bti_resistance_ohm == resistance);
1029
1030         low = info->bti_resistance_ohm -
1031                 (info->bti_resistance_ohm * info->bti_resistance_tolerance) / 100;
1032         high = info->bti_resistance_ohm +
1033                 (info->bti_resistance_ohm * info->bti_resistance_tolerance) / 100;
1034
1035         return ((resistance >= low) && (resistance <= high));
1036 }
1037 EXPORT_SYMBOL_GPL(power_supply_battery_bti_in_range);
1038
1039 int power_supply_get_property(struct power_supply *psy,
1040                             enum power_supply_property psp,
1041                             union power_supply_propval *val)
1042 {
1043         if (atomic_read(&psy->use_cnt) <= 0) {
1044                 if (!psy->initialized)
1045                         return -EAGAIN;
1046                 return -ENODEV;
1047         }
1048
1049         return psy->desc->get_property(psy, psp, val);
1050 }
1051 EXPORT_SYMBOL_GPL(power_supply_get_property);
1052
1053 int power_supply_set_property(struct power_supply *psy,
1054                             enum power_supply_property psp,
1055                             const union power_supply_propval *val)
1056 {
1057         if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
1058                 return -ENODEV;
1059
1060         return psy->desc->set_property(psy, psp, val);
1061 }
1062 EXPORT_SYMBOL_GPL(power_supply_set_property);
1063
1064 int power_supply_property_is_writeable(struct power_supply *psy,
1065                                         enum power_supply_property psp)
1066 {
1067         if (atomic_read(&psy->use_cnt) <= 0 ||
1068                         !psy->desc->property_is_writeable)
1069                 return -ENODEV;
1070
1071         return psy->desc->property_is_writeable(psy, psp);
1072 }
1073 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
1074
1075 void power_supply_external_power_changed(struct power_supply *psy)
1076 {
1077         if (atomic_read(&psy->use_cnt) <= 0 ||
1078                         !psy->desc->external_power_changed)
1079                 return;
1080
1081         psy->desc->external_power_changed(psy);
1082 }
1083 EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
1084
1085 int power_supply_powers(struct power_supply *psy, struct device *dev)
1086 {
1087         return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
1088 }
1089 EXPORT_SYMBOL_GPL(power_supply_powers);
1090
1091 static void power_supply_dev_release(struct device *dev)
1092 {
1093         struct power_supply *psy = to_power_supply(dev);
1094         dev_dbg(dev, "%s\n", __func__);
1095         kfree(psy);
1096 }
1097
1098 int power_supply_reg_notifier(struct notifier_block *nb)
1099 {
1100         return atomic_notifier_chain_register(&power_supply_notifier, nb);
1101 }
1102 EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
1103
1104 void power_supply_unreg_notifier(struct notifier_block *nb)
1105 {
1106         atomic_notifier_chain_unregister(&power_supply_notifier, nb);
1107 }
1108 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
1109
1110 static bool psy_has_property(const struct power_supply_desc *psy_desc,
1111                              enum power_supply_property psp)
1112 {
1113         bool found = false;
1114         int i;
1115
1116         for (i = 0; i < psy_desc->num_properties; i++) {
1117                 if (psy_desc->properties[i] == psp) {
1118                         found = true;
1119                         break;
1120                 }
1121         }
1122
1123         return found;
1124 }
1125
1126 #ifdef CONFIG_THERMAL
1127 static int power_supply_read_temp(struct thermal_zone_device *tzd,
1128                 int *temp)
1129 {
1130         struct power_supply *psy;
1131         union power_supply_propval val;
1132         int ret;
1133
1134         WARN_ON(tzd == NULL);
1135         psy = tzd->devdata;
1136         ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
1137         if (ret)
1138                 return ret;
1139
1140         /* Convert tenths of degree Celsius to milli degree Celsius. */
1141         *temp = val.intval * 100;
1142
1143         return ret;
1144 }
1145
1146 static struct thermal_zone_device_ops psy_tzd_ops = {
1147         .get_temp = power_supply_read_temp,
1148 };
1149
1150 static int psy_register_thermal(struct power_supply *psy)
1151 {
1152         int ret;
1153
1154         if (psy->desc->no_thermal)
1155                 return 0;
1156
1157         /* Register battery zone device psy reports temperature */
1158         if (psy_has_property(psy->desc, POWER_SUPPLY_PROP_TEMP)) {
1159                 psy->tzd = thermal_zone_device_register(psy->desc->name,
1160                                 0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
1161                 if (IS_ERR(psy->tzd))
1162                         return PTR_ERR(psy->tzd);
1163                 ret = thermal_zone_device_enable(psy->tzd);
1164                 if (ret)
1165                         thermal_zone_device_unregister(psy->tzd);
1166                 return ret;
1167         }
1168
1169         return 0;
1170 }
1171
1172 static void psy_unregister_thermal(struct power_supply *psy)
1173 {
1174         if (IS_ERR_OR_NULL(psy->tzd))
1175                 return;
1176         thermal_zone_device_unregister(psy->tzd);
1177 }
1178
1179 /* thermal cooling device callbacks */
1180 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
1181                                         unsigned long *state)
1182 {
1183         struct power_supply *psy;
1184         union power_supply_propval val;
1185         int ret;
1186
1187         psy = tcd->devdata;
1188         ret = power_supply_get_property(psy,
1189                         POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
1190         if (ret)
1191                 return ret;
1192
1193         *state = val.intval;
1194
1195         return ret;
1196 }
1197
1198 static int ps_get_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
1199                                         unsigned long *state)
1200 {
1201         struct power_supply *psy;
1202         union power_supply_propval val;
1203         int ret;
1204
1205         psy = tcd->devdata;
1206         ret = power_supply_get_property(psy,
1207                         POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
1208         if (ret)
1209                 return ret;
1210
1211         *state = val.intval;
1212
1213         return ret;
1214 }
1215
1216 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
1217                                         unsigned long state)
1218 {
1219         struct power_supply *psy;
1220         union power_supply_propval val;
1221         int ret;
1222
1223         psy = tcd->devdata;
1224         val.intval = state;
1225         ret = psy->desc->set_property(psy,
1226                 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
1227
1228         return ret;
1229 }
1230
1231 static const struct thermal_cooling_device_ops psy_tcd_ops = {
1232         .get_max_state = ps_get_max_charge_cntl_limit,
1233         .get_cur_state = ps_get_cur_charge_cntl_limit,
1234         .set_cur_state = ps_set_cur_charge_cntl_limit,
1235 };
1236
1237 static int psy_register_cooler(struct power_supply *psy)
1238 {
1239         /* Register for cooling device if psy can control charging */
1240         if (psy_has_property(psy->desc, POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT)) {
1241                 psy->tcd = thermal_cooling_device_register(
1242                         (char *)psy->desc->name,
1243                         psy, &psy_tcd_ops);
1244                 return PTR_ERR_OR_ZERO(psy->tcd);
1245         }
1246
1247         return 0;
1248 }
1249
1250 static void psy_unregister_cooler(struct power_supply *psy)
1251 {
1252         if (IS_ERR_OR_NULL(psy->tcd))
1253                 return;
1254         thermal_cooling_device_unregister(psy->tcd);
1255 }
1256 #else
1257 static int psy_register_thermal(struct power_supply *psy)
1258 {
1259         return 0;
1260 }
1261
1262 static void psy_unregister_thermal(struct power_supply *psy)
1263 {
1264 }
1265
1266 static int psy_register_cooler(struct power_supply *psy)
1267 {
1268         return 0;
1269 }
1270
1271 static void psy_unregister_cooler(struct power_supply *psy)
1272 {
1273 }
1274 #endif
1275
1276 static struct power_supply *__must_check
1277 __power_supply_register(struct device *parent,
1278                                    const struct power_supply_desc *desc,
1279                                    const struct power_supply_config *cfg,
1280                                    bool ws)
1281 {
1282         struct device *dev;
1283         struct power_supply *psy;
1284         int rc;
1285
1286         if (!parent)
1287                 pr_warn("%s: Expected proper parent device for '%s'\n",
1288                         __func__, desc->name);
1289
1290         if (!desc || !desc->name || !desc->properties || !desc->num_properties)
1291                 return ERR_PTR(-EINVAL);
1292
1293         if (psy_has_property(desc, POWER_SUPPLY_PROP_USB_TYPE) &&
1294             (!desc->usb_types || !desc->num_usb_types))
1295                 return ERR_PTR(-EINVAL);
1296
1297         psy = kzalloc(sizeof(*psy), GFP_KERNEL);
1298         if (!psy)
1299                 return ERR_PTR(-ENOMEM);
1300
1301         dev = &psy->dev;
1302
1303         device_initialize(dev);
1304
1305         dev->class = power_supply_class;
1306         dev->type = &power_supply_dev_type;
1307         dev->parent = parent;
1308         dev->release = power_supply_dev_release;
1309         dev_set_drvdata(dev, psy);
1310         psy->desc = desc;
1311         if (cfg) {
1312                 dev->groups = cfg->attr_grp;
1313                 psy->drv_data = cfg->drv_data;
1314                 psy->of_node =
1315                         cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node;
1316                 psy->supplied_to = cfg->supplied_to;
1317                 psy->num_supplicants = cfg->num_supplicants;
1318         }
1319
1320         rc = dev_set_name(dev, "%s", desc->name);
1321         if (rc)
1322                 goto dev_set_name_failed;
1323
1324         INIT_WORK(&psy->changed_work, power_supply_changed_work);
1325         INIT_DELAYED_WORK(&psy->deferred_register_work,
1326                           power_supply_deferred_register_work);
1327
1328         rc = power_supply_check_supplies(psy);
1329         if (rc) {
1330                 dev_dbg(dev, "Not all required supplies found, defer probe\n");
1331                 goto check_supplies_failed;
1332         }
1333
1334         spin_lock_init(&psy->changed_lock);
1335         rc = device_add(dev);
1336         if (rc)
1337                 goto device_add_failed;
1338
1339         rc = device_init_wakeup(dev, ws);
1340         if (rc)
1341                 goto wakeup_init_failed;
1342
1343         rc = psy_register_thermal(psy);
1344         if (rc)
1345                 goto register_thermal_failed;
1346
1347         rc = psy_register_cooler(psy);
1348         if (rc)
1349                 goto register_cooler_failed;
1350
1351         rc = power_supply_create_triggers(psy);
1352         if (rc)
1353                 goto create_triggers_failed;
1354
1355         rc = power_supply_add_hwmon_sysfs(psy);
1356         if (rc)
1357                 goto add_hwmon_sysfs_failed;
1358
1359         /*
1360          * Update use_cnt after any uevents (most notably from device_add()).
1361          * We are here still during driver's probe but
1362          * the power_supply_uevent() calls back driver's get_property
1363          * method so:
1364          * 1. Driver did not assigned the returned struct power_supply,
1365          * 2. Driver could not finish initialization (anything in its probe
1366          *    after calling power_supply_register()).
1367          */
1368         atomic_inc(&psy->use_cnt);
1369         psy->initialized = true;
1370
1371         queue_delayed_work(system_power_efficient_wq,
1372                            &psy->deferred_register_work,
1373                            POWER_SUPPLY_DEFERRED_REGISTER_TIME);
1374
1375         return psy;
1376
1377 add_hwmon_sysfs_failed:
1378         power_supply_remove_triggers(psy);
1379 create_triggers_failed:
1380         psy_unregister_cooler(psy);
1381 register_cooler_failed:
1382         psy_unregister_thermal(psy);
1383 register_thermal_failed:
1384         device_del(dev);
1385 wakeup_init_failed:
1386 device_add_failed:
1387 check_supplies_failed:
1388 dev_set_name_failed:
1389         put_device(dev);
1390         return ERR_PTR(rc);
1391 }
1392
1393 /**
1394  * power_supply_register() - Register new power supply
1395  * @parent:     Device to be a parent of power supply's device, usually
1396  *              the device which probe function calls this
1397  * @desc:       Description of power supply, must be valid through whole
1398  *              lifetime of this power supply
1399  * @cfg:        Run-time specific configuration accessed during registering,
1400  *              may be NULL
1401  *
1402  * Return: A pointer to newly allocated power_supply on success
1403  * or ERR_PTR otherwise.
1404  * Use power_supply_unregister() on returned power_supply pointer to release
1405  * resources.
1406  */
1407 struct power_supply *__must_check power_supply_register(struct device *parent,
1408                 const struct power_supply_desc *desc,
1409                 const struct power_supply_config *cfg)
1410 {
1411         return __power_supply_register(parent, desc, cfg, true);
1412 }
1413 EXPORT_SYMBOL_GPL(power_supply_register);
1414
1415 /**
1416  * power_supply_register_no_ws() - Register new non-waking-source power supply
1417  * @parent:     Device to be a parent of power supply's device, usually
1418  *              the device which probe function calls this
1419  * @desc:       Description of power supply, must be valid through whole
1420  *              lifetime of this power supply
1421  * @cfg:        Run-time specific configuration accessed during registering,
1422  *              may be NULL
1423  *
1424  * Return: A pointer to newly allocated power_supply on success
1425  * or ERR_PTR otherwise.
1426  * Use power_supply_unregister() on returned power_supply pointer to release
1427  * resources.
1428  */
1429 struct power_supply *__must_check
1430 power_supply_register_no_ws(struct device *parent,
1431                 const struct power_supply_desc *desc,
1432                 const struct power_supply_config *cfg)
1433 {
1434         return __power_supply_register(parent, desc, cfg, false);
1435 }
1436 EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
1437
1438 static void devm_power_supply_release(struct device *dev, void *res)
1439 {
1440         struct power_supply **psy = res;
1441
1442         power_supply_unregister(*psy);
1443 }
1444
1445 /**
1446  * devm_power_supply_register() - Register managed power supply
1447  * @parent:     Device to be a parent of power supply's device, usually
1448  *              the device which probe function calls this
1449  * @desc:       Description of power supply, must be valid through whole
1450  *              lifetime of this power supply
1451  * @cfg:        Run-time specific configuration accessed during registering,
1452  *              may be NULL
1453  *
1454  * Return: A pointer to newly allocated power_supply on success
1455  * or ERR_PTR otherwise.
1456  * The returned power_supply pointer will be automatically unregistered
1457  * on driver detach.
1458  */
1459 struct power_supply *__must_check
1460 devm_power_supply_register(struct device *parent,
1461                 const struct power_supply_desc *desc,
1462                 const struct power_supply_config *cfg)
1463 {
1464         struct power_supply **ptr, *psy;
1465
1466         ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1467
1468         if (!ptr)
1469                 return ERR_PTR(-ENOMEM);
1470         psy = __power_supply_register(parent, desc, cfg, true);
1471         if (IS_ERR(psy)) {
1472                 devres_free(ptr);
1473         } else {
1474                 *ptr = psy;
1475                 devres_add(parent, ptr);
1476         }
1477         return psy;
1478 }
1479 EXPORT_SYMBOL_GPL(devm_power_supply_register);
1480
1481 /**
1482  * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
1483  * @parent:     Device to be a parent of power supply's device, usually
1484  *              the device which probe function calls this
1485  * @desc:       Description of power supply, must be valid through whole
1486  *              lifetime of this power supply
1487  * @cfg:        Run-time specific configuration accessed during registering,
1488  *              may be NULL
1489  *
1490  * Return: A pointer to newly allocated power_supply on success
1491  * or ERR_PTR otherwise.
1492  * The returned power_supply pointer will be automatically unregistered
1493  * on driver detach.
1494  */
1495 struct power_supply *__must_check
1496 devm_power_supply_register_no_ws(struct device *parent,
1497                 const struct power_supply_desc *desc,
1498                 const struct power_supply_config *cfg)
1499 {
1500         struct power_supply **ptr, *psy;
1501
1502         ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1503
1504         if (!ptr)
1505                 return ERR_PTR(-ENOMEM);
1506         psy = __power_supply_register(parent, desc, cfg, false);
1507         if (IS_ERR(psy)) {
1508                 devres_free(ptr);
1509         } else {
1510                 *ptr = psy;
1511                 devres_add(parent, ptr);
1512         }
1513         return psy;
1514 }
1515 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
1516
1517 /**
1518  * power_supply_unregister() - Remove this power supply from system
1519  * @psy:        Pointer to power supply to unregister
1520  *
1521  * Remove this power supply from the system. The resources of power supply
1522  * will be freed here or on last power_supply_put() call.
1523  */
1524 void power_supply_unregister(struct power_supply *psy)
1525 {
1526         WARN_ON(atomic_dec_return(&psy->use_cnt));
1527         psy->removing = true;
1528         cancel_work_sync(&psy->changed_work);
1529         cancel_delayed_work_sync(&psy->deferred_register_work);
1530         sysfs_remove_link(&psy->dev.kobj, "powers");
1531         power_supply_remove_hwmon_sysfs(psy);
1532         power_supply_remove_triggers(psy);
1533         psy_unregister_cooler(psy);
1534         psy_unregister_thermal(psy);
1535         device_init_wakeup(&psy->dev, false);
1536         device_unregister(&psy->dev);
1537 }
1538 EXPORT_SYMBOL_GPL(power_supply_unregister);
1539
1540 void *power_supply_get_drvdata(struct power_supply *psy)
1541 {
1542         return psy->drv_data;
1543 }
1544 EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
1545
1546 static int __init power_supply_class_init(void)
1547 {
1548         power_supply_class = class_create(THIS_MODULE, "power_supply");
1549
1550         if (IS_ERR(power_supply_class))
1551                 return PTR_ERR(power_supply_class);
1552
1553         power_supply_class->dev_uevent = power_supply_uevent;
1554         power_supply_init_attrs(&power_supply_dev_type);
1555
1556         return 0;
1557 }
1558
1559 static void __exit power_supply_class_exit(void)
1560 {
1561         class_destroy(power_supply_class);
1562 }
1563
1564 subsys_initcall(power_supply_class_init);
1565 module_exit(power_supply_class_exit);
1566
1567 MODULE_DESCRIPTION("Universal power supply monitor class");
1568 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
1569               "Szabolcs Gyurko, "
1570               "Anton Vorontsov <cbou@mail.ru>");
1571 MODULE_LICENSE("GPL");