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