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