1
2
3
4
5
6
7
8
9
10
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/device.h>
18#include <linux/notifier.h>
19#include <linux/err.h>
20#include <linux/of.h>
21#include <linux/power_supply.h>
22#include <linux/thermal.h>
23#include "power_supply.h"
24
25
26struct class *power_supply_class;
27EXPORT_SYMBOL_GPL(power_supply_class);
28
29ATOMIC_NOTIFIER_HEAD(power_supply_notifier);
30EXPORT_SYMBOL_GPL(power_supply_notifier);
31
32static struct device_type power_supply_dev_type;
33
34#define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10)
35
36static 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
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
62static 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
75static 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
86
87
88
89
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
105
106
107
108 if (likely(!psy->changed))
109 pm_relax(&psy->dev);
110 spin_unlock_irqrestore(&psy->changed_lock, flags);
111}
112
113void 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}
125EXPORT_SYMBOL_GPL(power_supply_changed);
126
127
128
129
130
131
132
133
134
135
136
137static 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 mutex_lock(&psy->dev.parent->mutex);
144
145 power_supply_changed(psy);
146
147 if (psy->dev.parent)
148 mutex_unlock(&psy->dev.parent->mutex);
149}
150
151#ifdef CONFIG_OF
152#include <linux/of.h>
153
154static int __power_supply_populate_supplied_from(struct device *dev,
155 void *data)
156{
157 struct power_supply *psy = data;
158 struct power_supply *epsy = dev_get_drvdata(dev);
159 struct device_node *np;
160 int i = 0;
161
162 do {
163 np = of_parse_phandle(psy->of_node, "power-supplies", i++);
164 if (!np)
165 break;
166
167 if (np == epsy->of_node) {
168 dev_info(&psy->dev, "%s: Found supply : %s\n",
169 psy->desc->name, epsy->desc->name);
170 psy->supplied_from[i-1] = (char *)epsy->desc->name;
171 psy->num_supplies++;
172 of_node_put(np);
173 break;
174 }
175 of_node_put(np);
176 } while (np);
177
178 return 0;
179}
180
181static int power_supply_populate_supplied_from(struct power_supply *psy)
182{
183 int error;
184
185 error = class_for_each_device(power_supply_class, NULL, psy,
186 __power_supply_populate_supplied_from);
187
188 dev_dbg(&psy->dev, "%s %d\n", __func__, error);
189
190 return error;
191}
192
193static int __power_supply_find_supply_from_node(struct device *dev,
194 void *data)
195{
196 struct device_node *np = data;
197 struct power_supply *epsy = dev_get_drvdata(dev);
198
199
200 if (epsy->of_node == np)
201 return 1;
202
203 return 0;
204}
205
206static int power_supply_find_supply_from_node(struct device_node *supply_node)
207{
208 int error;
209
210
211
212
213
214
215
216
217
218
219
220 error = class_for_each_device(power_supply_class, NULL, supply_node,
221 __power_supply_find_supply_from_node);
222
223 return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
224}
225
226static int power_supply_check_supplies(struct power_supply *psy)
227{
228 struct device_node *np;
229 int cnt = 0;
230
231
232 if (psy->supplied_from && psy->num_supplies > 0)
233 return 0;
234
235
236 if (!psy->of_node)
237 return 0;
238
239 do {
240 int ret;
241
242 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
243 if (!np)
244 break;
245
246 ret = power_supply_find_supply_from_node(np);
247 of_node_put(np);
248
249 if (ret) {
250 dev_dbg(&psy->dev, "Failed to find supply!\n");
251 return ret;
252 }
253 } while (np);
254
255
256 if (cnt == 1)
257 return 0;
258
259
260 psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
261 GFP_KERNEL);
262 if (!psy->supplied_from)
263 return -ENOMEM;
264
265 *psy->supplied_from = devm_kzalloc(&psy->dev,
266 sizeof(char *) * (cnt - 1),
267 GFP_KERNEL);
268 if (!*psy->supplied_from)
269 return -ENOMEM;
270
271 return power_supply_populate_supplied_from(psy);
272}
273#else
274static int power_supply_check_supplies(struct power_supply *psy)
275{
276 int nval, ret;
277
278 if (!psy->dev.parent)
279 return 0;
280
281 nval = device_property_read_string_array(psy->dev.parent,
282 "supplied-from", NULL, 0);
283 if (nval <= 0)
284 return 0;
285
286 psy->supplied_from = devm_kmalloc_array(&psy->dev, nval,
287 sizeof(char *), GFP_KERNEL);
288 if (!psy->supplied_from)
289 return -ENOMEM;
290
291 ret = device_property_read_string_array(psy->dev.parent,
292 "supplied-from", (const char **)psy->supplied_from, nval);
293 if (ret < 0)
294 return ret;
295
296 psy->num_supplies = nval;
297
298 return 0;
299}
300#endif
301
302struct psy_am_i_supplied_data {
303 struct power_supply *psy;
304 unsigned int count;
305};
306
307static int __power_supply_am_i_supplied(struct device *dev, void *_data)
308{
309 union power_supply_propval ret = {0,};
310 struct power_supply *epsy = dev_get_drvdata(dev);
311 struct psy_am_i_supplied_data *data = _data;
312
313 if (__power_supply_is_supplied_by(epsy, data->psy)) {
314 data->count++;
315 if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
316 &ret))
317 return ret.intval;
318 }
319
320 return 0;
321}
322
323int power_supply_am_i_supplied(struct power_supply *psy)
324{
325 struct psy_am_i_supplied_data data = { psy, 0 };
326 int error;
327
328 error = class_for_each_device(power_supply_class, NULL, &data,
329 __power_supply_am_i_supplied);
330
331 dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
332
333 if (data.count == 0)
334 return -ENODEV;
335
336 return error;
337}
338EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
339
340static int __power_supply_is_system_supplied(struct device *dev, void *data)
341{
342 union power_supply_propval ret = {0,};
343 struct power_supply *psy = dev_get_drvdata(dev);
344 unsigned int *count = data;
345
346 (*count)++;
347 if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
348 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
349 &ret))
350 return ret.intval;
351
352 return 0;
353}
354
355int power_supply_is_system_supplied(void)
356{
357 int error;
358 unsigned int count = 0;
359
360 error = class_for_each_device(power_supply_class, NULL, &count,
361 __power_supply_is_system_supplied);
362
363
364
365
366
367 if (count == 0)
368 return 1;
369
370 return error;
371}
372EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
373
374static int __power_supply_get_supplier_max_current(struct device *dev,
375 void *data)
376{
377 union power_supply_propval ret = {0,};
378 struct power_supply *epsy = dev_get_drvdata(dev);
379 struct power_supply *psy = data;
380
381 if (__power_supply_is_supplied_by(epsy, psy))
382 if (!epsy->desc->get_property(epsy,
383 POWER_SUPPLY_PROP_CURRENT_MAX,
384 &ret))
385 return ret.intval;
386
387 return 0;
388}
389
390int power_supply_set_input_current_limit_from_supplier(struct power_supply *psy)
391{
392 union power_supply_propval val = {0,};
393 int curr;
394
395 if (!psy->desc->set_property)
396 return -EINVAL;
397
398
399
400
401
402
403 curr = class_for_each_device(power_supply_class, NULL, psy,
404 __power_supply_get_supplier_max_current);
405 if (curr <= 0)
406 return (curr == 0) ? -ENODEV : curr;
407
408 val.intval = curr;
409
410 return psy->desc->set_property(psy,
411 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
412}
413EXPORT_SYMBOL_GPL(power_supply_set_input_current_limit_from_supplier);
414
415int power_supply_set_battery_charged(struct power_supply *psy)
416{
417 if (atomic_read(&psy->use_cnt) >= 0 &&
418 psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
419 psy->desc->set_charged) {
420 psy->desc->set_charged(psy);
421 return 0;
422 }
423
424 return -EINVAL;
425}
426EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
427
428static int power_supply_match_device_by_name(struct device *dev, const void *data)
429{
430 const char *name = data;
431 struct power_supply *psy = dev_get_drvdata(dev);
432
433 return strcmp(psy->desc->name, name) == 0;
434}
435
436
437
438
439
440
441
442
443
444
445
446
447struct power_supply *power_supply_get_by_name(const char *name)
448{
449 struct power_supply *psy = NULL;
450 struct device *dev = class_find_device(power_supply_class, NULL, name,
451 power_supply_match_device_by_name);
452
453 if (dev) {
454 psy = dev_get_drvdata(dev);
455 atomic_inc(&psy->use_cnt);
456 }
457
458 return psy;
459}
460EXPORT_SYMBOL_GPL(power_supply_get_by_name);
461
462
463
464
465
466
467
468
469void power_supply_put(struct power_supply *psy)
470{
471 might_sleep();
472
473 atomic_dec(&psy->use_cnt);
474 put_device(&psy->dev);
475}
476EXPORT_SYMBOL_GPL(power_supply_put);
477
478#ifdef CONFIG_OF
479static int power_supply_match_device_node(struct device *dev, const void *data)
480{
481 return dev->parent && dev->parent->of_node == data;
482}
483
484
485
486
487
488
489
490
491
492
493
494
495
496struct power_supply *power_supply_get_by_phandle(struct device_node *np,
497 const char *property)
498{
499 struct device_node *power_supply_np;
500 struct power_supply *psy = NULL;
501 struct device *dev;
502
503 power_supply_np = of_parse_phandle(np, property, 0);
504 if (!power_supply_np)
505 return ERR_PTR(-ENODEV);
506
507 dev = class_find_device(power_supply_class, NULL, power_supply_np,
508 power_supply_match_device_node);
509
510 of_node_put(power_supply_np);
511
512 if (dev) {
513 psy = dev_get_drvdata(dev);
514 atomic_inc(&psy->use_cnt);
515 }
516
517 return psy;
518}
519EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
520
521static void devm_power_supply_put(struct device *dev, void *res)
522{
523 struct power_supply **psy = res;
524
525 power_supply_put(*psy);
526}
527
528
529
530
531
532
533
534
535
536
537struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
538 const char *property)
539{
540 struct power_supply **ptr, *psy;
541
542 if (!dev->of_node)
543 return ERR_PTR(-ENODEV);
544
545 ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
546 if (!ptr)
547 return ERR_PTR(-ENOMEM);
548
549 psy = power_supply_get_by_phandle(dev->of_node, property);
550 if (IS_ERR_OR_NULL(psy)) {
551 devres_free(ptr);
552 } else {
553 *ptr = psy;
554 devres_add(dev, ptr);
555 }
556 return psy;
557}
558EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
559#endif
560
561int power_supply_get_battery_info(struct power_supply *psy,
562 struct power_supply_battery_info *info)
563{
564 struct device_node *battery_np;
565 const char *value;
566 int err;
567
568 info->energy_full_design_uwh = -EINVAL;
569 info->charge_full_design_uah = -EINVAL;
570 info->voltage_min_design_uv = -EINVAL;
571 info->precharge_current_ua = -EINVAL;
572 info->charge_term_current_ua = -EINVAL;
573 info->constant_charge_current_max_ua = -EINVAL;
574 info->constant_charge_voltage_max_uv = -EINVAL;
575
576 if (!psy->of_node) {
577 dev_warn(&psy->dev, "%s currently only supports devicetree\n",
578 __func__);
579 return -ENXIO;
580 }
581
582 battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);
583 if (!battery_np)
584 return -ENODEV;
585
586 err = of_property_read_string(battery_np, "compatible", &value);
587 if (err)
588 return err;
589
590 if (strcmp("simple-battery", value))
591 return -ENODEV;
592
593
594
595
596
597
598 of_property_read_u32(battery_np, "energy-full-design-microwatt-hours",
599 &info->energy_full_design_uwh);
600 of_property_read_u32(battery_np, "charge-full-design-microamp-hours",
601 &info->charge_full_design_uah);
602 of_property_read_u32(battery_np, "voltage-min-design-microvolt",
603 &info->voltage_min_design_uv);
604 of_property_read_u32(battery_np, "precharge-current-microamp",
605 &info->precharge_current_ua);
606 of_property_read_u32(battery_np, "charge-term-current-microamp",
607 &info->charge_term_current_ua);
608 of_property_read_u32(battery_np, "constant_charge_current_max_microamp",
609 &info->constant_charge_current_max_ua);
610 of_property_read_u32(battery_np, "constant_charge_voltage_max_microvolt",
611 &info->constant_charge_voltage_max_uv);
612
613 return 0;
614}
615EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
616
617int power_supply_get_property(struct power_supply *psy,
618 enum power_supply_property psp,
619 union power_supply_propval *val)
620{
621 if (atomic_read(&psy->use_cnt) <= 0) {
622 if (!psy->initialized)
623 return -EAGAIN;
624 return -ENODEV;
625 }
626
627 return psy->desc->get_property(psy, psp, val);
628}
629EXPORT_SYMBOL_GPL(power_supply_get_property);
630
631int power_supply_set_property(struct power_supply *psy,
632 enum power_supply_property psp,
633 const union power_supply_propval *val)
634{
635 if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
636 return -ENODEV;
637
638 return psy->desc->set_property(psy, psp, val);
639}
640EXPORT_SYMBOL_GPL(power_supply_set_property);
641
642int power_supply_property_is_writeable(struct power_supply *psy,
643 enum power_supply_property psp)
644{
645 if (atomic_read(&psy->use_cnt) <= 0 ||
646 !psy->desc->property_is_writeable)
647 return -ENODEV;
648
649 return psy->desc->property_is_writeable(psy, psp);
650}
651EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
652
653void power_supply_external_power_changed(struct power_supply *psy)
654{
655 if (atomic_read(&psy->use_cnt) <= 0 ||
656 !psy->desc->external_power_changed)
657 return;
658
659 psy->desc->external_power_changed(psy);
660}
661EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
662
663int power_supply_powers(struct power_supply *psy, struct device *dev)
664{
665 return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
666}
667EXPORT_SYMBOL_GPL(power_supply_powers);
668
669static void power_supply_dev_release(struct device *dev)
670{
671 struct power_supply *psy = container_of(dev, struct power_supply, dev);
672 dev_dbg(dev, "%s\n", __func__);
673 kfree(psy);
674}
675
676int power_supply_reg_notifier(struct notifier_block *nb)
677{
678 return atomic_notifier_chain_register(&power_supply_notifier, nb);
679}
680EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
681
682void power_supply_unreg_notifier(struct notifier_block *nb)
683{
684 atomic_notifier_chain_unregister(&power_supply_notifier, nb);
685}
686EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
687
688#ifdef CONFIG_THERMAL
689static int power_supply_read_temp(struct thermal_zone_device *tzd,
690 int *temp)
691{
692 struct power_supply *psy;
693 union power_supply_propval val;
694 int ret;
695
696 WARN_ON(tzd == NULL);
697 psy = tzd->devdata;
698 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
699 if (ret)
700 return ret;
701
702
703 *temp = val.intval * 100;
704
705 return ret;
706}
707
708static struct thermal_zone_device_ops psy_tzd_ops = {
709 .get_temp = power_supply_read_temp,
710};
711
712static int psy_register_thermal(struct power_supply *psy)
713{
714 int i;
715
716 if (psy->desc->no_thermal)
717 return 0;
718
719
720 for (i = 0; i < psy->desc->num_properties; i++) {
721 if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
722 psy->tzd = thermal_zone_device_register(psy->desc->name,
723 0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
724 return PTR_ERR_OR_ZERO(psy->tzd);
725 }
726 }
727 return 0;
728}
729
730static void psy_unregister_thermal(struct power_supply *psy)
731{
732 if (IS_ERR_OR_NULL(psy->tzd))
733 return;
734 thermal_zone_device_unregister(psy->tzd);
735}
736
737
738static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
739 unsigned long *state)
740{
741 struct power_supply *psy;
742 union power_supply_propval val;
743 int ret;
744
745 psy = tcd->devdata;
746 ret = power_supply_get_property(psy,
747 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
748 if (ret)
749 return ret;
750
751 *state = val.intval;
752
753 return ret;
754}
755
756static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
757 unsigned long *state)
758{
759 struct power_supply *psy;
760 union power_supply_propval val;
761 int ret;
762
763 psy = tcd->devdata;
764 ret = power_supply_get_property(psy,
765 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
766 if (ret)
767 return ret;
768
769 *state = val.intval;
770
771 return ret;
772}
773
774static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
775 unsigned long state)
776{
777 struct power_supply *psy;
778 union power_supply_propval val;
779 int ret;
780
781 psy = tcd->devdata;
782 val.intval = state;
783 ret = psy->desc->set_property(psy,
784 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
785
786 return ret;
787}
788
789static const struct thermal_cooling_device_ops psy_tcd_ops = {
790 .get_max_state = ps_get_max_charge_cntl_limit,
791 .get_cur_state = ps_get_cur_chrage_cntl_limit,
792 .set_cur_state = ps_set_cur_charge_cntl_limit,
793};
794
795static int psy_register_cooler(struct power_supply *psy)
796{
797 int i;
798
799
800 for (i = 0; i < psy->desc->num_properties; i++) {
801 if (psy->desc->properties[i] ==
802 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
803 psy->tcd = thermal_cooling_device_register(
804 (char *)psy->desc->name,
805 psy, &psy_tcd_ops);
806 return PTR_ERR_OR_ZERO(psy->tcd);
807 }
808 }
809 return 0;
810}
811
812static void psy_unregister_cooler(struct power_supply *psy)
813{
814 if (IS_ERR_OR_NULL(psy->tcd))
815 return;
816 thermal_cooling_device_unregister(psy->tcd);
817}
818#else
819static int psy_register_thermal(struct power_supply *psy)
820{
821 return 0;
822}
823
824static void psy_unregister_thermal(struct power_supply *psy)
825{
826}
827
828static int psy_register_cooler(struct power_supply *psy)
829{
830 return 0;
831}
832
833static void psy_unregister_cooler(struct power_supply *psy)
834{
835}
836#endif
837
838static struct power_supply *__must_check
839__power_supply_register(struct device *parent,
840 const struct power_supply_desc *desc,
841 const struct power_supply_config *cfg,
842 bool ws)
843{
844 struct device *dev;
845 struct power_supply *psy;
846 int rc;
847
848 if (!parent)
849 pr_warn("%s: Expected proper parent device for '%s'\n",
850 __func__, desc->name);
851
852 psy = kzalloc(sizeof(*psy), GFP_KERNEL);
853 if (!psy)
854 return ERR_PTR(-ENOMEM);
855
856 dev = &psy->dev;
857
858 device_initialize(dev);
859
860 dev->class = power_supply_class;
861 dev->type = &power_supply_dev_type;
862 dev->parent = parent;
863 dev->release = power_supply_dev_release;
864 dev_set_drvdata(dev, psy);
865 psy->desc = desc;
866 if (cfg) {
867 psy->drv_data = cfg->drv_data;
868 psy->of_node = cfg->of_node;
869 psy->supplied_to = cfg->supplied_to;
870 psy->num_supplicants = cfg->num_supplicants;
871 }
872
873 rc = dev_set_name(dev, "%s", desc->name);
874 if (rc)
875 goto dev_set_name_failed;
876
877 INIT_WORK(&psy->changed_work, power_supply_changed_work);
878 INIT_DELAYED_WORK(&psy->deferred_register_work,
879 power_supply_deferred_register_work);
880
881 rc = power_supply_check_supplies(psy);
882 if (rc) {
883 dev_info(dev, "Not all required supplies found, defer probe\n");
884 goto check_supplies_failed;
885 }
886
887 spin_lock_init(&psy->changed_lock);
888 rc = device_init_wakeup(dev, ws);
889 if (rc)
890 goto wakeup_init_failed;
891
892 rc = device_add(dev);
893 if (rc)
894 goto device_add_failed;
895
896 rc = psy_register_thermal(psy);
897 if (rc)
898 goto register_thermal_failed;
899
900 rc = psy_register_cooler(psy);
901 if (rc)
902 goto register_cooler_failed;
903
904 rc = power_supply_create_triggers(psy);
905 if (rc)
906 goto create_triggers_failed;
907
908
909
910
911
912
913
914
915
916
917 atomic_inc(&psy->use_cnt);
918 psy->initialized = true;
919
920 queue_delayed_work(system_power_efficient_wq,
921 &psy->deferred_register_work,
922 POWER_SUPPLY_DEFERRED_REGISTER_TIME);
923
924 return psy;
925
926create_triggers_failed:
927 psy_unregister_cooler(psy);
928register_cooler_failed:
929 psy_unregister_thermal(psy);
930register_thermal_failed:
931 device_del(dev);
932device_add_failed:
933wakeup_init_failed:
934check_supplies_failed:
935dev_set_name_failed:
936 put_device(dev);
937 return ERR_PTR(rc);
938}
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954struct power_supply *__must_check power_supply_register(struct device *parent,
955 const struct power_supply_desc *desc,
956 const struct power_supply_config *cfg)
957{
958 return __power_supply_register(parent, desc, cfg, true);
959}
960EXPORT_SYMBOL_GPL(power_supply_register);
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976struct power_supply *__must_check
977power_supply_register_no_ws(struct device *parent,
978 const struct power_supply_desc *desc,
979 const struct power_supply_config *cfg)
980{
981 return __power_supply_register(parent, desc, cfg, false);
982}
983EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
984
985static void devm_power_supply_release(struct device *dev, void *res)
986{
987 struct power_supply **psy = res;
988
989 power_supply_unregister(*psy);
990}
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006struct power_supply *__must_check
1007devm_power_supply_register(struct device *parent,
1008 const struct power_supply_desc *desc,
1009 const struct power_supply_config *cfg)
1010{
1011 struct power_supply **ptr, *psy;
1012
1013 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1014
1015 if (!ptr)
1016 return ERR_PTR(-ENOMEM);
1017 psy = __power_supply_register(parent, desc, cfg, true);
1018 if (IS_ERR(psy)) {
1019 devres_free(ptr);
1020 } else {
1021 *ptr = psy;
1022 devres_add(parent, ptr);
1023 }
1024 return psy;
1025}
1026EXPORT_SYMBOL_GPL(devm_power_supply_register);
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042struct power_supply *__must_check
1043devm_power_supply_register_no_ws(struct device *parent,
1044 const struct power_supply_desc *desc,
1045 const struct power_supply_config *cfg)
1046{
1047 struct power_supply **ptr, *psy;
1048
1049 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1050
1051 if (!ptr)
1052 return ERR_PTR(-ENOMEM);
1053 psy = __power_supply_register(parent, desc, cfg, false);
1054 if (IS_ERR(psy)) {
1055 devres_free(ptr);
1056 } else {
1057 *ptr = psy;
1058 devres_add(parent, ptr);
1059 }
1060 return psy;
1061}
1062EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
1063
1064
1065
1066
1067
1068
1069
1070
1071void power_supply_unregister(struct power_supply *psy)
1072{
1073 WARN_ON(atomic_dec_return(&psy->use_cnt));
1074 cancel_work_sync(&psy->changed_work);
1075 cancel_delayed_work_sync(&psy->deferred_register_work);
1076 sysfs_remove_link(&psy->dev.kobj, "powers");
1077 power_supply_remove_triggers(psy);
1078 psy_unregister_cooler(psy);
1079 psy_unregister_thermal(psy);
1080 device_init_wakeup(&psy->dev, false);
1081 device_unregister(&psy->dev);
1082}
1083EXPORT_SYMBOL_GPL(power_supply_unregister);
1084
1085void *power_supply_get_drvdata(struct power_supply *psy)
1086{
1087 return psy->drv_data;
1088}
1089EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
1090
1091static int __init power_supply_class_init(void)
1092{
1093 power_supply_class = class_create(THIS_MODULE, "power_supply");
1094
1095 if (IS_ERR(power_supply_class))
1096 return PTR_ERR(power_supply_class);
1097
1098 power_supply_class->dev_uevent = power_supply_uevent;
1099 power_supply_init_attrs(&power_supply_dev_type);
1100
1101 return 0;
1102}
1103
1104static void __exit power_supply_class_exit(void)
1105{
1106 class_destroy(power_supply_class);
1107}
1108
1109subsys_initcall(power_supply_class_init);
1110module_exit(power_supply_class_exit);
1111
1112MODULE_DESCRIPTION("Universal power supply monitor class");
1113MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
1114 "Szabolcs Gyurko, "
1115 "Anton Vorontsov <cbou@mail.ru>");
1116MODULE_LICENSE("GPL");
1117