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