GNU Linux-libre 4.19.304-gnu1
[releases.git] / drivers / power / supply / power_supply_core.c
1 /*
2  *  Universal power supply monitor class
3  *
4  *  Copyright © 2007  Anton Vorontsov <cbou@mail.ru>
5  *  Copyright © 2004  Szabolcs Gyurko
6  *  Copyright © 2003  Ian Molton <spyro@f2s.com>
7  *
8  *  Modified: 2004, Oct     Szabolcs Gyurko
9  *
10  *  You may use this code as per GPL version 2
11  */
12
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/notifier.h>
20 #include <linux/err.h>
21 #include <linux/of.h>
22 #include <linux/power_supply.h>
23 #include <linux/property.h>
24 #include <linux/thermal.h>
25 #include "power_supply.h"
26
27 /* exported for the APM Power driver, APM emulation */
28 struct class *power_supply_class;
29 EXPORT_SYMBOL_GPL(power_supply_class);
30
31 ATOMIC_NOTIFIER_HEAD(power_supply_notifier);
32 EXPORT_SYMBOL_GPL(power_supply_notifier);
33
34 static struct device_type power_supply_dev_type;
35
36 #define POWER_SUPPLY_DEFERRED_REGISTER_TIME     msecs_to_jiffies(10)
37
38 static bool __power_supply_is_supplied_by(struct power_supply *supplier,
39                                          struct power_supply *supply)
40 {
41         int i;
42
43         if (!supply->supplied_from && !supplier->supplied_to)
44                 return false;
45
46         /* Support both supplied_to and supplied_from modes */
47         if (supply->supplied_from) {
48                 if (!supplier->desc->name)
49                         return false;
50                 for (i = 0; i < supply->num_supplies; i++)
51                         if (!strcmp(supplier->desc->name, supply->supplied_from[i]))
52                                 return true;
53         } else {
54                 if (!supply->desc->name)
55                         return false;
56                 for (i = 0; i < supplier->num_supplicants; i++)
57                         if (!strcmp(supplier->supplied_to[i], supply->desc->name))
58                                 return true;
59         }
60
61         return false;
62 }
63
64 static int __power_supply_changed_work(struct device *dev, void *data)
65 {
66         struct power_supply *psy = data;
67         struct power_supply *pst = dev_get_drvdata(dev);
68
69         if (__power_supply_is_supplied_by(psy, pst)) {
70                 if (pst->desc->external_power_changed)
71                         pst->desc->external_power_changed(pst);
72         }
73
74         return 0;
75 }
76
77 static void power_supply_changed_work(struct work_struct *work)
78 {
79         unsigned long flags;
80         struct power_supply *psy = container_of(work, struct power_supply,
81                                                 changed_work);
82
83         dev_dbg(&psy->dev, "%s\n", __func__);
84
85         spin_lock_irqsave(&psy->changed_lock, flags);
86         /*
87          * Check 'changed' here to avoid issues due to race between
88          * power_supply_changed() and this routine. In worst case
89          * power_supply_changed() can be called again just before we take above
90          * lock. During the first call of this routine we will mark 'changed' as
91          * false and it will stay false for the next call as well.
92          */
93         if (likely(psy->changed)) {
94                 psy->changed = false;
95                 spin_unlock_irqrestore(&psy->changed_lock, flags);
96                 class_for_each_device(power_supply_class, NULL, psy,
97                                       __power_supply_changed_work);
98                 power_supply_update_leds(psy);
99                 atomic_notifier_call_chain(&power_supply_notifier,
100                                 PSY_EVENT_PROP_CHANGED, psy);
101                 kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
102                 spin_lock_irqsave(&psy->changed_lock, flags);
103         }
104
105         /*
106          * Hold the wakeup_source until all events are processed.
107          * power_supply_changed() might have called again and have set 'changed'
108          * to true.
109          */
110         if (likely(!psy->changed))
111                 pm_relax(&psy->dev);
112         spin_unlock_irqrestore(&psy->changed_lock, flags);
113 }
114
115 void power_supply_changed(struct power_supply *psy)
116 {
117         unsigned long flags;
118
119         dev_dbg(&psy->dev, "%s\n", __func__);
120
121         spin_lock_irqsave(&psy->changed_lock, flags);
122         psy->changed = true;
123         pm_stay_awake(&psy->dev);
124         spin_unlock_irqrestore(&psy->changed_lock, flags);
125         schedule_work(&psy->changed_work);
126 }
127 EXPORT_SYMBOL_GPL(power_supply_changed);
128
129 /*
130  * Notify that power supply was registered after parent finished the probing.
131  *
132  * Often power supply is registered from driver's probe function. However
133  * calling power_supply_changed() directly from power_supply_register()
134  * would lead to execution of get_property() function provided by the driver
135  * too early - before the probe ends.
136  *
137  * Avoid that by waiting on parent's mutex.
138  */
139 static void power_supply_deferred_register_work(struct work_struct *work)
140 {
141         struct power_supply *psy = container_of(work, struct power_supply,
142                                                 deferred_register_work.work);
143
144         if (psy->dev.parent) {
145                 while (!mutex_trylock(&psy->dev.parent->mutex)) {
146                         if (psy->removing)
147                                 return;
148                         msleep(10);
149                 }
150         }
151
152         power_supply_changed(psy);
153
154         if (psy->dev.parent)
155                 mutex_unlock(&psy->dev.parent->mutex);
156 }
157
158 #ifdef CONFIG_OF
159 #include <linux/of.h>
160
161 static int __power_supply_populate_supplied_from(struct device *dev,
162                                                  void *data)
163 {
164         struct power_supply *psy = data;
165         struct power_supply *epsy = dev_get_drvdata(dev);
166         struct device_node *np;
167         int i = 0;
168
169         do {
170                 np = of_parse_phandle(psy->of_node, "power-supplies", i++);
171                 if (!np)
172                         break;
173
174                 if (np == epsy->of_node) {
175                         dev_info(&psy->dev, "%s: Found supply : %s\n",
176                                 psy->desc->name, epsy->desc->name);
177                         psy->supplied_from[i-1] = (char *)epsy->desc->name;
178                         psy->num_supplies++;
179                         of_node_put(np);
180                         break;
181                 }
182                 of_node_put(np);
183         } while (np);
184
185         return 0;
186 }
187
188 static int power_supply_populate_supplied_from(struct power_supply *psy)
189 {
190         int error;
191
192         error = class_for_each_device(power_supply_class, NULL, psy,
193                                       __power_supply_populate_supplied_from);
194
195         dev_dbg(&psy->dev, "%s %d\n", __func__, error);
196
197         return error;
198 }
199
200 static int  __power_supply_find_supply_from_node(struct device *dev,
201                                                  void *data)
202 {
203         struct device_node *np = data;
204         struct power_supply *epsy = dev_get_drvdata(dev);
205
206         /* returning non-zero breaks out of class_for_each_device loop */
207         if (epsy->of_node == np)
208                 return 1;
209
210         return 0;
211 }
212
213 static int power_supply_find_supply_from_node(struct device_node *supply_node)
214 {
215         int error;
216
217         /*
218          * class_for_each_device() either returns its own errors or values
219          * returned by __power_supply_find_supply_from_node().
220          *
221          * __power_supply_find_supply_from_node() will return 0 (no match)
222          * or 1 (match).
223          *
224          * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
225          * it returned 0, or error as returned by it.
226          */
227         error = class_for_each_device(power_supply_class, NULL, supply_node,
228                                        __power_supply_find_supply_from_node);
229
230         return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
231 }
232
233 static int power_supply_check_supplies(struct power_supply *psy)
234 {
235         struct device_node *np;
236         int cnt = 0;
237
238         /* If there is already a list honor it */
239         if (psy->supplied_from && psy->num_supplies > 0)
240                 return 0;
241
242         /* No device node found, nothing to do */
243         if (!psy->of_node)
244                 return 0;
245
246         do {
247                 int ret;
248
249                 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
250                 if (!np)
251                         break;
252
253                 ret = power_supply_find_supply_from_node(np);
254                 of_node_put(np);
255
256                 if (ret) {
257                         dev_dbg(&psy->dev, "Failed to find supply!\n");
258                         return ret;
259                 }
260         } while (np);
261
262         /* Missing valid "power-supplies" entries */
263         if (cnt == 1)
264                 return 0;
265
266         /* All supplies found, allocate char ** array for filling */
267         psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
268                                           GFP_KERNEL);
269         if (!psy->supplied_from)
270                 return -ENOMEM;
271
272         *psy->supplied_from = devm_kcalloc(&psy->dev,
273                                            cnt - 1, sizeof(char *),
274                                            GFP_KERNEL);
275         if (!*psy->supplied_from)
276                 return -ENOMEM;
277
278         return power_supply_populate_supplied_from(psy);
279 }
280 #else
281 static int power_supply_check_supplies(struct power_supply *psy)
282 {
283         int nval, ret;
284
285         if (!psy->dev.parent)
286                 return 0;
287
288         nval = device_property_read_string_array(psy->dev.parent,
289                                                  "supplied-from", NULL, 0);
290         if (nval <= 0)
291                 return 0;
292
293         psy->supplied_from = devm_kmalloc_array(&psy->dev, nval,
294                                                 sizeof(char *), GFP_KERNEL);
295         if (!psy->supplied_from)
296                 return -ENOMEM;
297
298         ret = device_property_read_string_array(psy->dev.parent,
299                 "supplied-from", (const char **)psy->supplied_from, nval);
300         if (ret < 0)
301                 return ret;
302
303         psy->num_supplies = nval;
304
305         return 0;
306 }
307 #endif
308
309 struct psy_am_i_supplied_data {
310         struct power_supply *psy;
311         unsigned int count;
312 };
313
314 static int __power_supply_am_i_supplied(struct device *dev, void *_data)
315 {
316         union power_supply_propval ret = {0,};
317         struct power_supply *epsy = dev_get_drvdata(dev);
318         struct psy_am_i_supplied_data *data = _data;
319
320         if (__power_supply_is_supplied_by(epsy, data->psy)) {
321                 data->count++;
322                 if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
323                                         &ret))
324                         return ret.intval;
325         }
326
327         return 0;
328 }
329
330 int power_supply_am_i_supplied(struct power_supply *psy)
331 {
332         struct psy_am_i_supplied_data data = { psy, 0 };
333         int error;
334
335         error = class_for_each_device(power_supply_class, NULL, &data,
336                                       __power_supply_am_i_supplied);
337
338         dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
339
340         if (data.count == 0)
341                 return -ENODEV;
342
343         return error;
344 }
345 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
346
347 static int __power_supply_is_system_supplied(struct device *dev, void *data)
348 {
349         union power_supply_propval ret = {0,};
350         struct power_supply *psy = dev_get_drvdata(dev);
351         unsigned int *count = data;
352
353         if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_SCOPE, &ret))
354                 if (ret.intval == POWER_SUPPLY_SCOPE_DEVICE)
355                         return 0;
356
357         (*count)++;
358         if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
359                 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
360                                         &ret))
361                         return ret.intval;
362
363         return 0;
364 }
365
366 int power_supply_is_system_supplied(void)
367 {
368         int error;
369         unsigned int count = 0;
370
371         error = class_for_each_device(power_supply_class, NULL, &count,
372                                       __power_supply_is_system_supplied);
373
374         /*
375          * If no system scope power class device was found at all, most probably we
376          * are running on a desktop system, so assume we are on mains power.
377          */
378         if (count == 0)
379                 return 1;
380
381         return error;
382 }
383 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
384
385 struct psy_get_supplier_prop_data {
386         struct power_supply *psy;
387         enum power_supply_property psp;
388         union power_supply_propval *val;
389 };
390
391 static int __power_supply_get_supplier_property(struct device *dev, void *_data)
392 {
393         struct power_supply *epsy = dev_get_drvdata(dev);
394         struct psy_get_supplier_prop_data *data = _data;
395
396         if (__power_supply_is_supplied_by(epsy, data->psy))
397                 if (!epsy->desc->get_property(epsy, data->psp, data->val))
398                         return 1; /* Success */
399
400         return 0; /* Continue iterating */
401 }
402
403 int power_supply_get_property_from_supplier(struct power_supply *psy,
404                                             enum power_supply_property psp,
405                                             union power_supply_propval *val)
406 {
407         struct psy_get_supplier_prop_data data = {
408                 .psy = psy,
409                 .psp = psp,
410                 .val = val,
411         };
412         int ret;
413
414         /*
415          * This function is not intended for use with a supply with multiple
416          * suppliers, we simply pick the first supply to report the psp.
417          */
418         ret = class_for_each_device(power_supply_class, NULL, &data,
419                                     __power_supply_get_supplier_property);
420         if (ret < 0)
421                 return ret;
422         if (ret == 0)
423                 return -ENODEV;
424
425         return 0;
426 }
427 EXPORT_SYMBOL_GPL(power_supply_get_property_from_supplier);
428
429 int power_supply_set_battery_charged(struct power_supply *psy)
430 {
431         if (atomic_read(&psy->use_cnt) >= 0 &&
432                         psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
433                         psy->desc->set_charged) {
434                 psy->desc->set_charged(psy);
435                 return 0;
436         }
437
438         return -EINVAL;
439 }
440 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
441
442 static int power_supply_match_device_by_name(struct device *dev, const void *data)
443 {
444         const char *name = data;
445         struct power_supply *psy = dev_get_drvdata(dev);
446
447         return strcmp(psy->desc->name, name) == 0;
448 }
449
450 /**
451  * power_supply_get_by_name() - Search for a power supply and returns its ref
452  * @name: Power supply name to fetch
453  *
454  * If power supply was found, it increases reference count for the
455  * internal power supply's device. The user should power_supply_put()
456  * after usage.
457  *
458  * Return: On success returns a reference to a power supply with
459  * matching name equals to @name, a NULL otherwise.
460  */
461 struct power_supply *power_supply_get_by_name(const char *name)
462 {
463         struct power_supply *psy = NULL;
464         struct device *dev = class_find_device(power_supply_class, NULL, name,
465                                         power_supply_match_device_by_name);
466
467         if (dev) {
468                 psy = dev_get_drvdata(dev);
469                 atomic_inc(&psy->use_cnt);
470         }
471
472         return psy;
473 }
474 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
475
476 /**
477  * power_supply_put() - Drop reference obtained with power_supply_get_by_name
478  * @psy: Reference to put
479  *
480  * The reference to power supply should be put before unregistering
481  * the power supply.
482  */
483 void power_supply_put(struct power_supply *psy)
484 {
485         might_sleep();
486
487         atomic_dec(&psy->use_cnt);
488         put_device(&psy->dev);
489 }
490 EXPORT_SYMBOL_GPL(power_supply_put);
491
492 #ifdef CONFIG_OF
493 static int power_supply_match_device_node(struct device *dev, const void *data)
494 {
495         return dev->parent && dev->parent->of_node == data;
496 }
497
498 /**
499  * power_supply_get_by_phandle() - Search for a power supply and returns its ref
500  * @np: Pointer to device node holding phandle property
501  * @property: Name of property holding a power supply name
502  *
503  * If power supply was found, it increases reference count for the
504  * internal power supply's device. The user should power_supply_put()
505  * after usage.
506  *
507  * Return: On success returns a reference to a power supply with
508  * matching name equals to value under @property, NULL or ERR_PTR otherwise.
509  */
510 struct power_supply *power_supply_get_by_phandle(struct device_node *np,
511                                                         const char *property)
512 {
513         struct device_node *power_supply_np;
514         struct power_supply *psy = NULL;
515         struct device *dev;
516
517         power_supply_np = of_parse_phandle(np, property, 0);
518         if (!power_supply_np)
519                 return ERR_PTR(-ENODEV);
520
521         dev = class_find_device(power_supply_class, NULL, power_supply_np,
522                                                 power_supply_match_device_node);
523
524         of_node_put(power_supply_np);
525
526         if (dev) {
527                 psy = dev_get_drvdata(dev);
528                 atomic_inc(&psy->use_cnt);
529         }
530
531         return psy;
532 }
533 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
534
535 static void devm_power_supply_put(struct device *dev, void *res)
536 {
537         struct power_supply **psy = res;
538
539         power_supply_put(*psy);
540 }
541
542 /**
543  * devm_power_supply_get_by_phandle() - Resource managed version of
544  *  power_supply_get_by_phandle()
545  * @dev: Pointer to device holding phandle property
546  * @property: Name of property holding a power supply phandle
547  *
548  * Return: On success returns a reference to a power supply with
549  * matching name equals to value under @property, NULL or ERR_PTR otherwise.
550  */
551 struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
552                                                       const char *property)
553 {
554         struct power_supply **ptr, *psy;
555
556         if (!dev->of_node)
557                 return ERR_PTR(-ENODEV);
558
559         ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
560         if (!ptr)
561                 return ERR_PTR(-ENOMEM);
562
563         psy = power_supply_get_by_phandle(dev->of_node, property);
564         if (IS_ERR_OR_NULL(psy)) {
565                 devres_free(ptr);
566         } else {
567                 *ptr = psy;
568                 devres_add(dev, ptr);
569         }
570         return psy;
571 }
572 EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
573 #endif /* CONFIG_OF */
574
575 int power_supply_get_battery_info(struct power_supply *psy,
576                                   struct power_supply_battery_info *info)
577 {
578         struct device_node *battery_np;
579         const char *value;
580         int err;
581
582         info->energy_full_design_uwh         = -EINVAL;
583         info->charge_full_design_uah         = -EINVAL;
584         info->voltage_min_design_uv          = -EINVAL;
585         info->precharge_current_ua           = -EINVAL;
586         info->charge_term_current_ua         = -EINVAL;
587         info->constant_charge_current_max_ua = -EINVAL;
588         info->constant_charge_voltage_max_uv = -EINVAL;
589
590         if (!psy->of_node) {
591                 dev_warn(&psy->dev, "%s currently only supports devicetree\n",
592                          __func__);
593                 return -ENXIO;
594         }
595
596         battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);
597         if (!battery_np)
598                 return -ENODEV;
599
600         err = of_property_read_string(battery_np, "compatible", &value);
601         if (err)
602                 return err;
603
604         if (strcmp("simple-battery", value))
605                 return -ENODEV;
606
607         /* The property and field names below must correspond to elements
608          * in enum power_supply_property. For reasoning, see
609          * Documentation/power/power_supply_class.txt.
610          */
611
612         of_property_read_u32(battery_np, "energy-full-design-microwatt-hours",
613                              &info->energy_full_design_uwh);
614         of_property_read_u32(battery_np, "charge-full-design-microamp-hours",
615                              &info->charge_full_design_uah);
616         of_property_read_u32(battery_np, "voltage-min-design-microvolt",
617                              &info->voltage_min_design_uv);
618         of_property_read_u32(battery_np, "precharge-current-microamp",
619                              &info->precharge_current_ua);
620         of_property_read_u32(battery_np, "charge-term-current-microamp",
621                              &info->charge_term_current_ua);
622         of_property_read_u32(battery_np, "constant_charge_current_max_microamp",
623                              &info->constant_charge_current_max_ua);
624         of_property_read_u32(battery_np, "constant_charge_voltage_max_microvolt",
625                              &info->constant_charge_voltage_max_uv);
626
627         return 0;
628 }
629 EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
630
631 int power_supply_get_property(struct power_supply *psy,
632                             enum power_supply_property psp,
633                             union power_supply_propval *val)
634 {
635         if (atomic_read(&psy->use_cnt) <= 0) {
636                 if (!psy->initialized)
637                         return -EAGAIN;
638                 return -ENODEV;
639         }
640
641         return psy->desc->get_property(psy, psp, val);
642 }
643 EXPORT_SYMBOL_GPL(power_supply_get_property);
644
645 int power_supply_set_property(struct power_supply *psy,
646                             enum power_supply_property psp,
647                             const union power_supply_propval *val)
648 {
649         if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
650                 return -ENODEV;
651
652         return psy->desc->set_property(psy, psp, val);
653 }
654 EXPORT_SYMBOL_GPL(power_supply_set_property);
655
656 int power_supply_property_is_writeable(struct power_supply *psy,
657                                         enum power_supply_property psp)
658 {
659         if (atomic_read(&psy->use_cnt) <= 0 ||
660                         !psy->desc->property_is_writeable)
661                 return -ENODEV;
662
663         return psy->desc->property_is_writeable(psy, psp);
664 }
665 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
666
667 void power_supply_external_power_changed(struct power_supply *psy)
668 {
669         if (atomic_read(&psy->use_cnt) <= 0 ||
670                         !psy->desc->external_power_changed)
671                 return;
672
673         psy->desc->external_power_changed(psy);
674 }
675 EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
676
677 int power_supply_powers(struct power_supply *psy, struct device *dev)
678 {
679         return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
680 }
681 EXPORT_SYMBOL_GPL(power_supply_powers);
682
683 static void power_supply_dev_release(struct device *dev)
684 {
685         struct power_supply *psy = to_power_supply(dev);
686         dev_dbg(dev, "%s\n", __func__);
687         kfree(psy);
688 }
689
690 int power_supply_reg_notifier(struct notifier_block *nb)
691 {
692         return atomic_notifier_chain_register(&power_supply_notifier, nb);
693 }
694 EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
695
696 void power_supply_unreg_notifier(struct notifier_block *nb)
697 {
698         atomic_notifier_chain_unregister(&power_supply_notifier, nb);
699 }
700 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
701
702 #ifdef CONFIG_THERMAL
703 static int power_supply_read_temp(struct thermal_zone_device *tzd,
704                 int *temp)
705 {
706         struct power_supply *psy;
707         union power_supply_propval val;
708         int ret;
709
710         WARN_ON(tzd == NULL);
711         psy = tzd->devdata;
712         ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
713         if (ret)
714                 return ret;
715
716         /* Convert tenths of degree Celsius to milli degree Celsius. */
717         *temp = val.intval * 100;
718
719         return ret;
720 }
721
722 static struct thermal_zone_device_ops psy_tzd_ops = {
723         .get_temp = power_supply_read_temp,
724 };
725
726 static int psy_register_thermal(struct power_supply *psy)
727 {
728         int i;
729
730         if (psy->desc->no_thermal)
731                 return 0;
732
733         /* Register battery zone device psy reports temperature */
734         for (i = 0; i < psy->desc->num_properties; i++) {
735                 if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
736                         psy->tzd = thermal_zone_device_register(psy->desc->name,
737                                         0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
738                         return PTR_ERR_OR_ZERO(psy->tzd);
739                 }
740         }
741         return 0;
742 }
743
744 static void psy_unregister_thermal(struct power_supply *psy)
745 {
746         if (IS_ERR_OR_NULL(psy->tzd))
747                 return;
748         thermal_zone_device_unregister(psy->tzd);
749 }
750
751 /* thermal cooling device callbacks */
752 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
753                                         unsigned long *state)
754 {
755         struct power_supply *psy;
756         union power_supply_propval val;
757         int ret;
758
759         psy = tcd->devdata;
760         ret = power_supply_get_property(psy,
761                         POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
762         if (ret)
763                 return ret;
764
765         *state = val.intval;
766
767         return ret;
768 }
769
770 static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
771                                         unsigned long *state)
772 {
773         struct power_supply *psy;
774         union power_supply_propval val;
775         int ret;
776
777         psy = tcd->devdata;
778         ret = power_supply_get_property(psy,
779                         POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
780         if (ret)
781                 return ret;
782
783         *state = val.intval;
784
785         return ret;
786 }
787
788 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
789                                         unsigned long state)
790 {
791         struct power_supply *psy;
792         union power_supply_propval val;
793         int ret;
794
795         psy = tcd->devdata;
796         val.intval = state;
797         ret = psy->desc->set_property(psy,
798                 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
799
800         return ret;
801 }
802
803 static const struct thermal_cooling_device_ops psy_tcd_ops = {
804         .get_max_state = ps_get_max_charge_cntl_limit,
805         .get_cur_state = ps_get_cur_chrage_cntl_limit,
806         .set_cur_state = ps_set_cur_charge_cntl_limit,
807 };
808
809 static int psy_register_cooler(struct power_supply *psy)
810 {
811         int i;
812
813         /* Register for cooling device if psy can control charging */
814         for (i = 0; i < psy->desc->num_properties; i++) {
815                 if (psy->desc->properties[i] ==
816                                 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
817                         psy->tcd = thermal_cooling_device_register(
818                                                         (char *)psy->desc->name,
819                                                         psy, &psy_tcd_ops);
820                         return PTR_ERR_OR_ZERO(psy->tcd);
821                 }
822         }
823         return 0;
824 }
825
826 static void psy_unregister_cooler(struct power_supply *psy)
827 {
828         if (IS_ERR_OR_NULL(psy->tcd))
829                 return;
830         thermal_cooling_device_unregister(psy->tcd);
831 }
832 #else
833 static int psy_register_thermal(struct power_supply *psy)
834 {
835         return 0;
836 }
837
838 static void psy_unregister_thermal(struct power_supply *psy)
839 {
840 }
841
842 static int psy_register_cooler(struct power_supply *psy)
843 {
844         return 0;
845 }
846
847 static void psy_unregister_cooler(struct power_supply *psy)
848 {
849 }
850 #endif
851
852 static struct power_supply *__must_check
853 __power_supply_register(struct device *parent,
854                                    const struct power_supply_desc *desc,
855                                    const struct power_supply_config *cfg,
856                                    bool ws)
857 {
858         struct device *dev;
859         struct power_supply *psy;
860         int i, rc;
861
862         if (!parent)
863                 pr_warn("%s: Expected proper parent device for '%s'\n",
864                         __func__, desc->name);
865
866         if (!desc || !desc->name || !desc->properties || !desc->num_properties)
867                 return ERR_PTR(-EINVAL);
868
869         for (i = 0; i < desc->num_properties; ++i) {
870                 if ((desc->properties[i] == POWER_SUPPLY_PROP_USB_TYPE) &&
871                     (!desc->usb_types || !desc->num_usb_types))
872                         return ERR_PTR(-EINVAL);
873         }
874
875         psy = kzalloc(sizeof(*psy), GFP_KERNEL);
876         if (!psy)
877                 return ERR_PTR(-ENOMEM);
878
879         dev = &psy->dev;
880
881         device_initialize(dev);
882
883         dev->class = power_supply_class;
884         dev->type = &power_supply_dev_type;
885         dev->parent = parent;
886         dev->release = power_supply_dev_release;
887         dev_set_drvdata(dev, psy);
888         psy->desc = desc;
889         if (cfg) {
890                 psy->drv_data = cfg->drv_data;
891                 psy->of_node =
892                         cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node;
893                 psy->supplied_to = cfg->supplied_to;
894                 psy->num_supplicants = cfg->num_supplicants;
895         }
896
897         rc = dev_set_name(dev, "%s", desc->name);
898         if (rc)
899                 goto dev_set_name_failed;
900
901         INIT_WORK(&psy->changed_work, power_supply_changed_work);
902         INIT_DELAYED_WORK(&psy->deferred_register_work,
903                           power_supply_deferred_register_work);
904
905         rc = power_supply_check_supplies(psy);
906         if (rc) {
907                 dev_info(dev, "Not all required supplies found, defer probe\n");
908                 goto check_supplies_failed;
909         }
910
911         spin_lock_init(&psy->changed_lock);
912         rc = device_add(dev);
913         if (rc)
914                 goto device_add_failed;
915
916         rc = device_init_wakeup(dev, ws);
917         if (rc)
918                 goto wakeup_init_failed;
919
920         rc = psy_register_thermal(psy);
921         if (rc)
922                 goto register_thermal_failed;
923
924         rc = psy_register_cooler(psy);
925         if (rc)
926                 goto register_cooler_failed;
927
928         rc = power_supply_create_triggers(psy);
929         if (rc)
930                 goto create_triggers_failed;
931
932         /*
933          * Update use_cnt after any uevents (most notably from device_add()).
934          * We are here still during driver's probe but
935          * the power_supply_uevent() calls back driver's get_property
936          * method so:
937          * 1. Driver did not assigned the returned struct power_supply,
938          * 2. Driver could not finish initialization (anything in its probe
939          *    after calling power_supply_register()).
940          */
941         atomic_inc(&psy->use_cnt);
942         psy->initialized = true;
943
944         queue_delayed_work(system_power_efficient_wq,
945                            &psy->deferred_register_work,
946                            POWER_SUPPLY_DEFERRED_REGISTER_TIME);
947
948         return psy;
949
950 create_triggers_failed:
951         psy_unregister_cooler(psy);
952 register_cooler_failed:
953         psy_unregister_thermal(psy);
954 register_thermal_failed:
955 wakeup_init_failed:
956         device_del(dev);
957 device_add_failed:
958 check_supplies_failed:
959 dev_set_name_failed:
960         put_device(dev);
961         return ERR_PTR(rc);
962 }
963
964 /**
965  * power_supply_register() - Register new power supply
966  * @parent:     Device to be a parent of power supply's device, usually
967  *              the device which probe function calls this
968  * @desc:       Description of power supply, must be valid through whole
969  *              lifetime of this power supply
970  * @cfg:        Run-time specific configuration accessed during registering,
971  *              may be NULL
972  *
973  * Return: A pointer to newly allocated power_supply on success
974  * or ERR_PTR otherwise.
975  * Use power_supply_unregister() on returned power_supply pointer to release
976  * resources.
977  */
978 struct power_supply *__must_check power_supply_register(struct device *parent,
979                 const struct power_supply_desc *desc,
980                 const struct power_supply_config *cfg)
981 {
982         return __power_supply_register(parent, desc, cfg, true);
983 }
984 EXPORT_SYMBOL_GPL(power_supply_register);
985
986 /**
987  * power_supply_register_no_ws() - Register new non-waking-source power supply
988  * @parent:     Device to be a parent of power supply's device, usually
989  *              the device which probe function calls this
990  * @desc:       Description of power supply, must be valid through whole
991  *              lifetime of this power supply
992  * @cfg:        Run-time specific configuration accessed during registering,
993  *              may be NULL
994  *
995  * Return: A pointer to newly allocated power_supply on success
996  * or ERR_PTR otherwise.
997  * Use power_supply_unregister() on returned power_supply pointer to release
998  * resources.
999  */
1000 struct power_supply *__must_check
1001 power_supply_register_no_ws(struct device *parent,
1002                 const struct power_supply_desc *desc,
1003                 const struct power_supply_config *cfg)
1004 {
1005         return __power_supply_register(parent, desc, cfg, false);
1006 }
1007 EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
1008
1009 static void devm_power_supply_release(struct device *dev, void *res)
1010 {
1011         struct power_supply **psy = res;
1012
1013         power_supply_unregister(*psy);
1014 }
1015
1016 /**
1017  * devm_power_supply_register() - Register managed power supply
1018  * @parent:     Device to be a parent of power supply's device, usually
1019  *              the device which probe function calls this
1020  * @desc:       Description of power supply, must be valid through whole
1021  *              lifetime of this power supply
1022  * @cfg:        Run-time specific configuration accessed during registering,
1023  *              may be NULL
1024  *
1025  * Return: A pointer to newly allocated power_supply on success
1026  * or ERR_PTR otherwise.
1027  * The returned power_supply pointer will be automatically unregistered
1028  * on driver detach.
1029  */
1030 struct power_supply *__must_check
1031 devm_power_supply_register(struct device *parent,
1032                 const struct power_supply_desc *desc,
1033                 const struct power_supply_config *cfg)
1034 {
1035         struct power_supply **ptr, *psy;
1036
1037         ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1038
1039         if (!ptr)
1040                 return ERR_PTR(-ENOMEM);
1041         psy = __power_supply_register(parent, desc, cfg, true);
1042         if (IS_ERR(psy)) {
1043                 devres_free(ptr);
1044         } else {
1045                 *ptr = psy;
1046                 devres_add(parent, ptr);
1047         }
1048         return psy;
1049 }
1050 EXPORT_SYMBOL_GPL(devm_power_supply_register);
1051
1052 /**
1053  * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
1054  * @parent:     Device to be a parent of power supply's device, usually
1055  *              the device which probe function calls this
1056  * @desc:       Description of power supply, must be valid through whole
1057  *              lifetime of this power supply
1058  * @cfg:        Run-time specific configuration accessed during registering,
1059  *              may be NULL
1060  *
1061  * Return: A pointer to newly allocated power_supply on success
1062  * or ERR_PTR otherwise.
1063  * The returned power_supply pointer will be automatically unregistered
1064  * on driver detach.
1065  */
1066 struct power_supply *__must_check
1067 devm_power_supply_register_no_ws(struct device *parent,
1068                 const struct power_supply_desc *desc,
1069                 const struct power_supply_config *cfg)
1070 {
1071         struct power_supply **ptr, *psy;
1072
1073         ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1074
1075         if (!ptr)
1076                 return ERR_PTR(-ENOMEM);
1077         psy = __power_supply_register(parent, desc, cfg, false);
1078         if (IS_ERR(psy)) {
1079                 devres_free(ptr);
1080         } else {
1081                 *ptr = psy;
1082                 devres_add(parent, ptr);
1083         }
1084         return psy;
1085 }
1086 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
1087
1088 /**
1089  * power_supply_unregister() - Remove this power supply from system
1090  * @psy:        Pointer to power supply to unregister
1091  *
1092  * Remove this power supply from the system. The resources of power supply
1093  * will be freed here or on last power_supply_put() call.
1094  */
1095 void power_supply_unregister(struct power_supply *psy)
1096 {
1097         WARN_ON(atomic_dec_return(&psy->use_cnt));
1098         psy->removing = true;
1099         cancel_work_sync(&psy->changed_work);
1100         cancel_delayed_work_sync(&psy->deferred_register_work);
1101         sysfs_remove_link(&psy->dev.kobj, "powers");
1102         power_supply_remove_triggers(psy);
1103         psy_unregister_cooler(psy);
1104         psy_unregister_thermal(psy);
1105         device_init_wakeup(&psy->dev, false);
1106         device_unregister(&psy->dev);
1107 }
1108 EXPORT_SYMBOL_GPL(power_supply_unregister);
1109
1110 void *power_supply_get_drvdata(struct power_supply *psy)
1111 {
1112         return psy->drv_data;
1113 }
1114 EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
1115
1116 static int __init power_supply_class_init(void)
1117 {
1118         power_supply_class = class_create(THIS_MODULE, "power_supply");
1119
1120         if (IS_ERR(power_supply_class))
1121                 return PTR_ERR(power_supply_class);
1122
1123         power_supply_class->dev_uevent = power_supply_uevent;
1124         power_supply_init_attrs(&power_supply_dev_type);
1125
1126         return 0;
1127 }
1128
1129 static void __exit power_supply_class_exit(void)
1130 {
1131         class_destroy(power_supply_class);
1132 }
1133
1134 subsys_initcall(power_supply_class_init);
1135 module_exit(power_supply_class_exit);
1136
1137 MODULE_DESCRIPTION("Universal power supply monitor class");
1138 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
1139               "Szabolcs Gyurko, "
1140               "Anton Vorontsov <cbou@mail.ru>");
1141 MODULE_LICENSE("GPL");