power-supply: Don't return -EINVAL from __power_supply_find_supply_from_node()
[firefly-linux-kernel-4.4.55.git] / drivers / power / 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/device.h>
18 #include <linux/notifier.h>
19 #include <linux/err.h>
20 #include <linux/power_supply.h>
21 #include <linux/thermal.h>
22 #include "power_supply.h"
23
24 /* exported for the APM Power driver, APM emulation */
25 struct class *power_supply_class;
26 EXPORT_SYMBOL_GPL(power_supply_class);
27
28 ATOMIC_NOTIFIER_HEAD(power_supply_notifier);
29 EXPORT_SYMBOL_GPL(power_supply_notifier);
30
31 static struct device_type power_supply_dev_type;
32
33 static bool __power_supply_is_supplied_by(struct power_supply *supplier,
34                                          struct power_supply *supply)
35 {
36         int i;
37
38         if (!supply->supplied_from && !supplier->supplied_to)
39                 return false;
40
41         /* Support both supplied_to and supplied_from modes */
42         if (supply->supplied_from) {
43                 if (!supplier->name)
44                         return false;
45                 for (i = 0; i < supply->num_supplies; i++)
46                         if (!strcmp(supplier->name, supply->supplied_from[i]))
47                                 return true;
48         } else {
49                 if (!supply->name)
50                         return false;
51                 for (i = 0; i < supplier->num_supplicants; i++)
52                         if (!strcmp(supplier->supplied_to[i], supply->name))
53                                 return true;
54         }
55
56         return false;
57 }
58
59 static int __power_supply_changed_work(struct device *dev, void *data)
60 {
61         struct power_supply *psy = data;
62         struct power_supply *pst = dev_get_drvdata(dev);
63
64         if (__power_supply_is_supplied_by(psy, pst)) {
65                 if (pst->external_power_changed)
66                         pst->external_power_changed(pst);
67         }
68
69         return 0;
70 }
71
72 static void power_supply_changed_work(struct work_struct *work)
73 {
74         unsigned long flags;
75         struct power_supply *psy = container_of(work, struct power_supply,
76                                                 changed_work);
77
78         dev_dbg(psy->dev, "%s\n", __func__);
79
80         spin_lock_irqsave(&psy->changed_lock, flags);
81         if (psy->changed) {
82                 psy->changed = false;
83                 spin_unlock_irqrestore(&psy->changed_lock, flags);
84                 class_for_each_device(power_supply_class, NULL, psy,
85                                       __power_supply_changed_work);
86                 power_supply_update_leds(psy);
87                 atomic_notifier_call_chain(&power_supply_notifier,
88                                 PSY_EVENT_PROP_CHANGED, psy);
89                 kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
90                 spin_lock_irqsave(&psy->changed_lock, flags);
91         }
92         /*
93          * Dependent power supplies (e.g. battery) may have changed state
94          * as a result of this event, so poll again and hold the
95          * wakeup_source until all events are processed.
96          */
97         if (!psy->changed)
98                 pm_relax(psy->dev);
99         spin_unlock_irqrestore(&psy->changed_lock, flags);
100 }
101
102 void power_supply_changed(struct power_supply *psy)
103 {
104         unsigned long flags;
105
106         dev_dbg(psy->dev, "%s\n", __func__);
107
108         spin_lock_irqsave(&psy->changed_lock, flags);
109         psy->changed = true;
110         pm_stay_awake(psy->dev);
111         spin_unlock_irqrestore(&psy->changed_lock, flags);
112         schedule_work(&psy->changed_work);
113 }
114 EXPORT_SYMBOL_GPL(power_supply_changed);
115
116 #ifdef CONFIG_OF
117 #include <linux/of.h>
118
119 static int __power_supply_populate_supplied_from(struct device *dev,
120                                                  void *data)
121 {
122         struct power_supply *psy = data;
123         struct power_supply *epsy = dev_get_drvdata(dev);
124         struct device_node *np;
125         int i = 0;
126
127         do {
128                 np = of_parse_phandle(psy->of_node, "power-supplies", i++);
129                 if (!np)
130                         break;
131
132                 if (np == epsy->of_node) {
133                         dev_info(psy->dev, "%s: Found supply : %s\n",
134                                 psy->name, epsy->name);
135                         psy->supplied_from[i-1] = (char *)epsy->name;
136                         psy->num_supplies++;
137                         of_node_put(np);
138                         break;
139                 }
140                 of_node_put(np);
141         } while (np);
142
143         return 0;
144 }
145
146 static int power_supply_populate_supplied_from(struct power_supply *psy)
147 {
148         int error;
149
150         error = class_for_each_device(power_supply_class, NULL, psy,
151                                       __power_supply_populate_supplied_from);
152
153         dev_dbg(psy->dev, "%s %d\n", __func__, error);
154
155         return error;
156 }
157
158 static int  __power_supply_find_supply_from_node(struct device *dev,
159                                                  void *data)
160 {
161         struct device_node *np = data;
162         struct power_supply *epsy = dev_get_drvdata(dev);
163
164         /* returning non-zero breaks out of class_for_each_device loop */
165         if (epsy->of_node == np)
166                 return 1;
167
168         return 0;
169 }
170
171 static int power_supply_find_supply_from_node(struct device_node *supply_node)
172 {
173         int error;
174         struct device *dev;
175         struct class_dev_iter iter;
176
177         /*
178          * Use iterator to see if any other device is registered.
179          * This is required since class_for_each_device returns 0
180          * if there are no devices registered.
181          */
182         class_dev_iter_init(&iter, power_supply_class, NULL, NULL);
183         dev = class_dev_iter_next(&iter);
184
185         if (!dev)
186                 return -EPROBE_DEFER;
187
188         /*
189          * class_for_each_device() either returns its own errors or values
190          * returned by __power_supply_find_supply_from_node().
191          *
192          * __power_supply_find_supply_from_node() will return 0 (no match)
193          * or 1 (match).
194          *
195          * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
196          * it returned 0, or error as returned by it.
197          */
198         error = class_for_each_device(power_supply_class, NULL, supply_node,
199                                        __power_supply_find_supply_from_node);
200
201         return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
202 }
203
204 static int power_supply_check_supplies(struct power_supply *psy)
205 {
206         struct device_node *np;
207         int cnt = 0;
208
209         /* If there is already a list honor it */
210         if (psy->supplied_from && psy->num_supplies > 0)
211                 return 0;
212
213         /* No device node found, nothing to do */
214         if (!psy->of_node)
215                 return 0;
216
217         do {
218                 int ret;
219
220                 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
221                 if (!np)
222                         break;
223
224                 ret = power_supply_find_supply_from_node(np);
225                 of_node_put(np);
226
227                 if (ret) {
228                         dev_dbg(psy->dev, "Failed to find supply!\n");
229                         return ret;
230                 }
231         } while (np);
232
233         /* Missing valid "power-supplies" entries */
234         if (cnt == 1)
235                 return 0;
236
237         /* All supplies found, allocate char ** array for filling */
238         psy->supplied_from = devm_kzalloc(psy->dev, sizeof(psy->supplied_from),
239                                           GFP_KERNEL);
240         if (!psy->supplied_from) {
241                 dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
242                 return -ENOMEM;
243         }
244
245         *psy->supplied_from = devm_kzalloc(psy->dev, sizeof(char *) * (cnt - 1),
246                                            GFP_KERNEL);
247         if (!*psy->supplied_from) {
248                 dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
249                 return -ENOMEM;
250         }
251
252         return power_supply_populate_supplied_from(psy);
253 }
254 #else
255 static inline int power_supply_check_supplies(struct power_supply *psy)
256 {
257         return 0;
258 }
259 #endif
260
261 static int __power_supply_am_i_supplied(struct device *dev, void *data)
262 {
263         union power_supply_propval ret = {0,};
264         struct power_supply *psy = data;
265         struct power_supply *epsy = dev_get_drvdata(dev);
266
267         if (__power_supply_is_supplied_by(epsy, psy))
268                 if (!epsy->get_property(epsy, POWER_SUPPLY_PROP_ONLINE, &ret)) {
269                         if (ret.intval)
270                                 return ret.intval;
271                 }
272
273         return 0;
274 }
275
276 int power_supply_am_i_supplied(struct power_supply *psy)
277 {
278         int error;
279
280         error = class_for_each_device(power_supply_class, NULL, psy,
281                                       __power_supply_am_i_supplied);
282
283         dev_dbg(psy->dev, "%s %d\n", __func__, error);
284
285         return error;
286 }
287 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
288
289 static int __power_supply_is_system_supplied(struct device *dev, void *data)
290 {
291         union power_supply_propval ret = {0,};
292         struct power_supply *psy = dev_get_drvdata(dev);
293         unsigned int *count = data;
294
295         (*count)++;
296         if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
297                 if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
298                         return 0;
299                 if (ret.intval)
300                         return ret.intval;
301         }
302         return 0;
303 }
304
305 int power_supply_is_system_supplied(void)
306 {
307         int error;
308         unsigned int count = 0;
309
310         error = class_for_each_device(power_supply_class, NULL, &count,
311                                       __power_supply_is_system_supplied);
312
313         /*
314          * If no power class device was found at all, most probably we are
315          * running on a desktop system, so assume we are on mains power.
316          */
317         if (count == 0)
318                 return 1;
319
320         return error;
321 }
322 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
323
324 int power_supply_set_battery_charged(struct power_supply *psy)
325 {
326         if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
327                 psy->set_charged(psy);
328                 return 0;
329         }
330
331         return -EINVAL;
332 }
333 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
334
335 static int power_supply_match_device_by_name(struct device *dev, const void *data)
336 {
337         const char *name = data;
338         struct power_supply *psy = dev_get_drvdata(dev);
339
340         return strcmp(psy->name, name) == 0;
341 }
342
343 struct power_supply *power_supply_get_by_name(const char *name)
344 {
345         struct device *dev = class_find_device(power_supply_class, NULL, name,
346                                         power_supply_match_device_by_name);
347
348         return dev ? dev_get_drvdata(dev) : NULL;
349 }
350 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
351
352 #ifdef CONFIG_OF
353 static int power_supply_match_device_node(struct device *dev, const void *data)
354 {
355         return dev->parent && dev->parent->of_node == data;
356 }
357
358 struct power_supply *power_supply_get_by_phandle(struct device_node *np,
359                                                         const char *property)
360 {
361         struct device_node *power_supply_np;
362         struct device *dev;
363
364         power_supply_np = of_parse_phandle(np, property, 0);
365         if (!power_supply_np)
366                 return ERR_PTR(-ENODEV);
367
368         dev = class_find_device(power_supply_class, NULL, power_supply_np,
369                                                 power_supply_match_device_node);
370
371         of_node_put(power_supply_np);
372
373         return dev ? dev_get_drvdata(dev) : NULL;
374 }
375 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
376 #endif /* CONFIG_OF */
377
378 int power_supply_powers(struct power_supply *psy, struct device *dev)
379 {
380         return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers");
381 }
382 EXPORT_SYMBOL_GPL(power_supply_powers);
383
384 static void power_supply_dev_release(struct device *dev)
385 {
386         pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
387         kfree(dev);
388 }
389
390 int power_supply_reg_notifier(struct notifier_block *nb)
391 {
392         return atomic_notifier_chain_register(&power_supply_notifier, nb);
393 }
394 EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
395
396 void power_supply_unreg_notifier(struct notifier_block *nb)
397 {
398         atomic_notifier_chain_unregister(&power_supply_notifier, nb);
399 }
400 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
401
402 #ifdef CONFIG_THERMAL
403 static int power_supply_read_temp(struct thermal_zone_device *tzd,
404                 unsigned long *temp)
405 {
406         struct power_supply *psy;
407         union power_supply_propval val;
408         int ret;
409
410         WARN_ON(tzd == NULL);
411         psy = tzd->devdata;
412         ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
413
414         /* Convert tenths of degree Celsius to milli degree Celsius. */
415         if (!ret)
416                 *temp = val.intval * 100;
417
418         return ret;
419 }
420
421 static struct thermal_zone_device_ops psy_tzd_ops = {
422         .get_temp = power_supply_read_temp,
423 };
424
425 static int psy_register_thermal(struct power_supply *psy)
426 {
427         int i;
428
429         /* Register battery zone device psy reports temperature */
430         for (i = 0; i < psy->num_properties; i++) {
431                 if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) {
432                         psy->tzd = thermal_zone_device_register(psy->name, 0, 0,
433                                         psy, &psy_tzd_ops, NULL, 0, 0);
434                         if (IS_ERR(psy->tzd))
435                                 return PTR_ERR(psy->tzd);
436                         break;
437                 }
438         }
439         return 0;
440 }
441
442 static void psy_unregister_thermal(struct power_supply *psy)
443 {
444         if (IS_ERR_OR_NULL(psy->tzd))
445                 return;
446         thermal_zone_device_unregister(psy->tzd);
447 }
448
449 /* thermal cooling device callbacks */
450 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
451                                         unsigned long *state)
452 {
453         struct power_supply *psy;
454         union power_supply_propval val;
455         int ret;
456
457         psy = tcd->devdata;
458         ret = psy->get_property(psy,
459                 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
460         if (!ret)
461                 *state = val.intval;
462
463         return ret;
464 }
465
466 static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
467                                         unsigned long *state)
468 {
469         struct power_supply *psy;
470         union power_supply_propval val;
471         int ret;
472
473         psy = tcd->devdata;
474         ret = psy->get_property(psy,
475                 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
476         if (!ret)
477                 *state = val.intval;
478
479         return ret;
480 }
481
482 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
483                                         unsigned long state)
484 {
485         struct power_supply *psy;
486         union power_supply_propval val;
487         int ret;
488
489         psy = tcd->devdata;
490         val.intval = state;
491         ret = psy->set_property(psy,
492                 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
493
494         return ret;
495 }
496
497 static struct thermal_cooling_device_ops psy_tcd_ops = {
498         .get_max_state = ps_get_max_charge_cntl_limit,
499         .get_cur_state = ps_get_cur_chrage_cntl_limit,
500         .set_cur_state = ps_set_cur_charge_cntl_limit,
501 };
502
503 static int psy_register_cooler(struct power_supply *psy)
504 {
505         int i;
506
507         /* Register for cooling device if psy can control charging */
508         for (i = 0; i < psy->num_properties; i++) {
509                 if (psy->properties[i] ==
510                                 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
511                         psy->tcd = thermal_cooling_device_register(
512                                                         (char *)psy->name,
513                                                         psy, &psy_tcd_ops);
514                         if (IS_ERR(psy->tcd))
515                                 return PTR_ERR(psy->tcd);
516                         break;
517                 }
518         }
519         return 0;
520 }
521
522 static void psy_unregister_cooler(struct power_supply *psy)
523 {
524         if (IS_ERR_OR_NULL(psy->tcd))
525                 return;
526         thermal_cooling_device_unregister(psy->tcd);
527 }
528 #else
529 static int psy_register_thermal(struct power_supply *psy)
530 {
531         return 0;
532 }
533
534 static void psy_unregister_thermal(struct power_supply *psy)
535 {
536 }
537
538 static int psy_register_cooler(struct power_supply *psy)
539 {
540         return 0;
541 }
542
543 static void psy_unregister_cooler(struct power_supply *psy)
544 {
545 }
546 #endif
547
548 static int __power_supply_register(struct device *parent,
549                                    struct power_supply *psy, bool ws)
550 {
551         struct device *dev;
552         int rc;
553
554         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
555         if (!dev)
556                 return -ENOMEM;
557
558         device_initialize(dev);
559
560         dev->class = power_supply_class;
561         dev->type = &power_supply_dev_type;
562         dev->parent = parent;
563         dev->release = power_supply_dev_release;
564         dev_set_drvdata(dev, psy);
565         psy->dev = dev;
566
567         rc = dev_set_name(dev, "%s", psy->name);
568         if (rc)
569                 goto dev_set_name_failed;
570
571         INIT_WORK(&psy->changed_work, power_supply_changed_work);
572
573         rc = power_supply_check_supplies(psy);
574         if (rc) {
575                 dev_info(dev, "Not all required supplies found, defer probe\n");
576                 goto check_supplies_failed;
577         }
578
579         spin_lock_init(&psy->changed_lock);
580         rc = device_init_wakeup(dev, ws);
581         if (rc)
582                 goto wakeup_init_failed;
583
584         rc = device_add(dev);
585         if (rc)
586                 goto device_add_failed;
587
588         rc = psy_register_thermal(psy);
589         if (rc)
590                 goto register_thermal_failed;
591
592         rc = psy_register_cooler(psy);
593         if (rc)
594                 goto register_cooler_failed;
595
596         rc = power_supply_create_triggers(psy);
597         if (rc)
598                 goto create_triggers_failed;
599
600         power_supply_changed(psy);
601
602         goto success;
603
604 create_triggers_failed:
605         psy_unregister_cooler(psy);
606 register_cooler_failed:
607         psy_unregister_thermal(psy);
608 register_thermal_failed:
609         device_del(dev);
610 device_add_failed:
611 wakeup_init_failed:
612 check_supplies_failed:
613 dev_set_name_failed:
614         put_device(dev);
615 success:
616         return rc;
617 }
618
619 int power_supply_register(struct device *parent, struct power_supply *psy)
620 {
621         return __power_supply_register(parent, psy, true);
622 }
623 EXPORT_SYMBOL_GPL(power_supply_register);
624
625 int power_supply_register_no_ws(struct device *parent, struct power_supply *psy)
626 {
627         return __power_supply_register(parent, psy, false);
628 }
629 EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
630
631 void power_supply_unregister(struct power_supply *psy)
632 {
633         cancel_work_sync(&psy->changed_work);
634         sysfs_remove_link(&psy->dev->kobj, "powers");
635         power_supply_remove_triggers(psy);
636         psy_unregister_cooler(psy);
637         psy_unregister_thermal(psy);
638         device_init_wakeup(psy->dev, false);
639         device_unregister(psy->dev);
640 }
641 EXPORT_SYMBOL_GPL(power_supply_unregister);
642
643 static int __init power_supply_class_init(void)
644 {
645         power_supply_class = class_create(THIS_MODULE, "power_supply");
646
647         if (IS_ERR(power_supply_class))
648                 return PTR_ERR(power_supply_class);
649
650         power_supply_class->dev_uevent = power_supply_uevent;
651         power_supply_init_attrs(&power_supply_dev_type);
652
653         return 0;
654 }
655
656 static void __exit power_supply_class_exit(void)
657 {
658         class_destroy(power_supply_class);
659 }
660
661 subsys_initcall(power_supply_class_init);
662 module_exit(power_supply_class_exit);
663
664 MODULE_DESCRIPTION("Universal power supply monitor class");
665 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
666               "Szabolcs Gyurko, "
667               "Anton Vorontsov <cbou@mail.ru>");
668 MODULE_LICENSE("GPL");