xref: /linux/drivers/power/supply/power_supply_core.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
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 #define POWER_SUPPLY_DEFERRED_REGISTER_TIME	msecs_to_jiffies(10)
34 
35 static 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 
61 static 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 
74 static 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 
112 void 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 }
124 EXPORT_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  */
136 static 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 
153 static 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 
180 static 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 
192 static 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 
205 static 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 
225 static 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
277 static inline int power_supply_check_supplies(struct power_supply *psy)
278 {
279 	return 0;
280 }
281 #endif
282 
283 struct psy_am_i_supplied_data {
284 	struct power_supply *psy;
285 	unsigned int count;
286 };
287 
288 static int __power_supply_am_i_supplied(struct device *dev, void *_data)
289 {
290 	union power_supply_propval ret = {0,};
291 	struct power_supply *epsy = dev_get_drvdata(dev);
292 	struct psy_am_i_supplied_data *data = _data;
293 
294 	data->count++;
295 	if (__power_supply_is_supplied_by(epsy, data->psy))
296 		if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
297 					&ret))
298 			return ret.intval;
299 
300 	return 0;
301 }
302 
303 int power_supply_am_i_supplied(struct power_supply *psy)
304 {
305 	struct psy_am_i_supplied_data data = { psy, 0 };
306 	int error;
307 
308 	error = class_for_each_device(power_supply_class, NULL, &data,
309 				      __power_supply_am_i_supplied);
310 
311 	dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
312 
313 	if (data.count == 0)
314 		return -ENODEV;
315 
316 	return error;
317 }
318 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
319 
320 static int __power_supply_is_system_supplied(struct device *dev, void *data)
321 {
322 	union power_supply_propval ret = {0,};
323 	struct power_supply *psy = dev_get_drvdata(dev);
324 	unsigned int *count = data;
325 
326 	(*count)++;
327 	if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
328 		if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
329 					&ret))
330 			return ret.intval;
331 
332 	return 0;
333 }
334 
335 int power_supply_is_system_supplied(void)
336 {
337 	int error;
338 	unsigned int count = 0;
339 
340 	error = class_for_each_device(power_supply_class, NULL, &count,
341 				      __power_supply_is_system_supplied);
342 
343 	/*
344 	 * If no power class device was found at all, most probably we are
345 	 * running on a desktop system, so assume we are on mains power.
346 	 */
347 	if (count == 0)
348 		return 1;
349 
350 	return error;
351 }
352 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
353 
354 int power_supply_set_battery_charged(struct power_supply *psy)
355 {
356 	if (atomic_read(&psy->use_cnt) >= 0 &&
357 			psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
358 			psy->desc->set_charged) {
359 		psy->desc->set_charged(psy);
360 		return 0;
361 	}
362 
363 	return -EINVAL;
364 }
365 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
366 
367 static int power_supply_match_device_by_name(struct device *dev, const void *data)
368 {
369 	const char *name = data;
370 	struct power_supply *psy = dev_get_drvdata(dev);
371 
372 	return strcmp(psy->desc->name, name) == 0;
373 }
374 
375 /**
376  * power_supply_get_by_name() - Search for a power supply and returns its ref
377  * @name: Power supply name to fetch
378  *
379  * If power supply was found, it increases reference count for the
380  * internal power supply's device. The user should power_supply_put()
381  * after usage.
382  *
383  * Return: On success returns a reference to a power supply with
384  * matching name equals to @name, a NULL otherwise.
385  */
386 struct power_supply *power_supply_get_by_name(const char *name)
387 {
388 	struct power_supply *psy = NULL;
389 	struct device *dev = class_find_device(power_supply_class, NULL, name,
390 					power_supply_match_device_by_name);
391 
392 	if (dev) {
393 		psy = dev_get_drvdata(dev);
394 		atomic_inc(&psy->use_cnt);
395 	}
396 
397 	return psy;
398 }
399 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
400 
401 /**
402  * power_supply_put() - Drop reference obtained with power_supply_get_by_name
403  * @psy: Reference to put
404  *
405  * The reference to power supply should be put before unregistering
406  * the power supply.
407  */
408 void power_supply_put(struct power_supply *psy)
409 {
410 	might_sleep();
411 
412 	atomic_dec(&psy->use_cnt);
413 	put_device(&psy->dev);
414 }
415 EXPORT_SYMBOL_GPL(power_supply_put);
416 
417 #ifdef CONFIG_OF
418 static int power_supply_match_device_node(struct device *dev, const void *data)
419 {
420 	return dev->parent && dev->parent->of_node == data;
421 }
422 
423 /**
424  * power_supply_get_by_phandle() - Search for a power supply and returns its ref
425  * @np: Pointer to device node holding phandle property
426  * @property: Name of property holding a power supply name
427  *
428  * If power supply was found, it increases reference count for the
429  * internal power supply's device. The user should power_supply_put()
430  * after usage.
431  *
432  * Return: On success returns a reference to a power supply with
433  * matching name equals to value under @property, NULL or ERR_PTR otherwise.
434  */
435 struct power_supply *power_supply_get_by_phandle(struct device_node *np,
436 							const char *property)
437 {
438 	struct device_node *power_supply_np;
439 	struct power_supply *psy = NULL;
440 	struct device *dev;
441 
442 	power_supply_np = of_parse_phandle(np, property, 0);
443 	if (!power_supply_np)
444 		return ERR_PTR(-ENODEV);
445 
446 	dev = class_find_device(power_supply_class, NULL, power_supply_np,
447 						power_supply_match_device_node);
448 
449 	of_node_put(power_supply_np);
450 
451 	if (dev) {
452 		psy = dev_get_drvdata(dev);
453 		atomic_inc(&psy->use_cnt);
454 	}
455 
456 	return psy;
457 }
458 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
459 
460 static void devm_power_supply_put(struct device *dev, void *res)
461 {
462 	struct power_supply **psy = res;
463 
464 	power_supply_put(*psy);
465 }
466 
467 /**
468  * devm_power_supply_get_by_phandle() - Resource managed version of
469  *  power_supply_get_by_phandle()
470  * @dev: Pointer to device holding phandle property
471  * @property: Name of property holding a power supply phandle
472  *
473  * Return: On success returns a reference to a power supply with
474  * matching name equals to value under @property, NULL or ERR_PTR otherwise.
475  */
476 struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
477 						      const char *property)
478 {
479 	struct power_supply **ptr, *psy;
480 
481 	if (!dev->of_node)
482 		return ERR_PTR(-ENODEV);
483 
484 	ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
485 	if (!ptr)
486 		return ERR_PTR(-ENOMEM);
487 
488 	psy = power_supply_get_by_phandle(dev->of_node, property);
489 	if (IS_ERR_OR_NULL(psy)) {
490 		devres_free(ptr);
491 	} else {
492 		*ptr = psy;
493 		devres_add(dev, ptr);
494 	}
495 	return psy;
496 }
497 EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
498 #endif /* CONFIG_OF */
499 
500 int power_supply_get_property(struct power_supply *psy,
501 			    enum power_supply_property psp,
502 			    union power_supply_propval *val)
503 {
504 	if (atomic_read(&psy->use_cnt) <= 0) {
505 		if (!psy->initialized)
506 			return -EAGAIN;
507 		return -ENODEV;
508 	}
509 
510 	return psy->desc->get_property(psy, psp, val);
511 }
512 EXPORT_SYMBOL_GPL(power_supply_get_property);
513 
514 int power_supply_set_property(struct power_supply *psy,
515 			    enum power_supply_property psp,
516 			    const union power_supply_propval *val)
517 {
518 	if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
519 		return -ENODEV;
520 
521 	return psy->desc->set_property(psy, psp, val);
522 }
523 EXPORT_SYMBOL_GPL(power_supply_set_property);
524 
525 int power_supply_property_is_writeable(struct power_supply *psy,
526 					enum power_supply_property psp)
527 {
528 	if (atomic_read(&psy->use_cnt) <= 0 ||
529 			!psy->desc->property_is_writeable)
530 		return -ENODEV;
531 
532 	return psy->desc->property_is_writeable(psy, psp);
533 }
534 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
535 
536 void power_supply_external_power_changed(struct power_supply *psy)
537 {
538 	if (atomic_read(&psy->use_cnt) <= 0 ||
539 			!psy->desc->external_power_changed)
540 		return;
541 
542 	psy->desc->external_power_changed(psy);
543 }
544 EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
545 
546 int power_supply_powers(struct power_supply *psy, struct device *dev)
547 {
548 	return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
549 }
550 EXPORT_SYMBOL_GPL(power_supply_powers);
551 
552 static void power_supply_dev_release(struct device *dev)
553 {
554 	struct power_supply *psy = container_of(dev, struct power_supply, dev);
555 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
556 	kfree(psy);
557 }
558 
559 int power_supply_reg_notifier(struct notifier_block *nb)
560 {
561 	return atomic_notifier_chain_register(&power_supply_notifier, nb);
562 }
563 EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
564 
565 void power_supply_unreg_notifier(struct notifier_block *nb)
566 {
567 	atomic_notifier_chain_unregister(&power_supply_notifier, nb);
568 }
569 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
570 
571 #ifdef CONFIG_THERMAL
572 static int power_supply_read_temp(struct thermal_zone_device *tzd,
573 		int *temp)
574 {
575 	struct power_supply *psy;
576 	union power_supply_propval val;
577 	int ret;
578 
579 	WARN_ON(tzd == NULL);
580 	psy = tzd->devdata;
581 	ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
582 	if (ret)
583 		return ret;
584 
585 	/* Convert tenths of degree Celsius to milli degree Celsius. */
586 	*temp = val.intval * 100;
587 
588 	return ret;
589 }
590 
591 static struct thermal_zone_device_ops psy_tzd_ops = {
592 	.get_temp = power_supply_read_temp,
593 };
594 
595 static int psy_register_thermal(struct power_supply *psy)
596 {
597 	int i;
598 
599 	if (psy->desc->no_thermal)
600 		return 0;
601 
602 	/* Register battery zone device psy reports temperature */
603 	for (i = 0; i < psy->desc->num_properties; i++) {
604 		if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
605 			psy->tzd = thermal_zone_device_register(psy->desc->name,
606 					0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
607 			return PTR_ERR_OR_ZERO(psy->tzd);
608 		}
609 	}
610 	return 0;
611 }
612 
613 static void psy_unregister_thermal(struct power_supply *psy)
614 {
615 	if (IS_ERR_OR_NULL(psy->tzd))
616 		return;
617 	thermal_zone_device_unregister(psy->tzd);
618 }
619 
620 /* thermal cooling device callbacks */
621 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
622 					unsigned long *state)
623 {
624 	struct power_supply *psy;
625 	union power_supply_propval val;
626 	int ret;
627 
628 	psy = tcd->devdata;
629 	ret = power_supply_get_property(psy,
630 			POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
631 	if (ret)
632 		return ret;
633 
634 	*state = val.intval;
635 
636 	return ret;
637 }
638 
639 static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
640 					unsigned long *state)
641 {
642 	struct power_supply *psy;
643 	union power_supply_propval val;
644 	int ret;
645 
646 	psy = tcd->devdata;
647 	ret = power_supply_get_property(psy,
648 			POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
649 	if (ret)
650 		return ret;
651 
652 	*state = val.intval;
653 
654 	return ret;
655 }
656 
657 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
658 					unsigned long state)
659 {
660 	struct power_supply *psy;
661 	union power_supply_propval val;
662 	int ret;
663 
664 	psy = tcd->devdata;
665 	val.intval = state;
666 	ret = psy->desc->set_property(psy,
667 		POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
668 
669 	return ret;
670 }
671 
672 static struct thermal_cooling_device_ops psy_tcd_ops = {
673 	.get_max_state = ps_get_max_charge_cntl_limit,
674 	.get_cur_state = ps_get_cur_chrage_cntl_limit,
675 	.set_cur_state = ps_set_cur_charge_cntl_limit,
676 };
677 
678 static int psy_register_cooler(struct power_supply *psy)
679 {
680 	int i;
681 
682 	/* Register for cooling device if psy can control charging */
683 	for (i = 0; i < psy->desc->num_properties; i++) {
684 		if (psy->desc->properties[i] ==
685 				POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
686 			psy->tcd = thermal_cooling_device_register(
687 							(char *)psy->desc->name,
688 							psy, &psy_tcd_ops);
689 			return PTR_ERR_OR_ZERO(psy->tcd);
690 		}
691 	}
692 	return 0;
693 }
694 
695 static void psy_unregister_cooler(struct power_supply *psy)
696 {
697 	if (IS_ERR_OR_NULL(psy->tcd))
698 		return;
699 	thermal_cooling_device_unregister(psy->tcd);
700 }
701 #else
702 static int psy_register_thermal(struct power_supply *psy)
703 {
704 	return 0;
705 }
706 
707 static void psy_unregister_thermal(struct power_supply *psy)
708 {
709 }
710 
711 static int psy_register_cooler(struct power_supply *psy)
712 {
713 	return 0;
714 }
715 
716 static void psy_unregister_cooler(struct power_supply *psy)
717 {
718 }
719 #endif
720 
721 static struct power_supply *__must_check
722 __power_supply_register(struct device *parent,
723 				   const struct power_supply_desc *desc,
724 				   const struct power_supply_config *cfg,
725 				   bool ws)
726 {
727 	struct device *dev;
728 	struct power_supply *psy;
729 	int rc;
730 
731 	if (!parent)
732 		pr_warn("%s: Expected proper parent device for '%s'\n",
733 			__func__, desc->name);
734 
735 	psy = kzalloc(sizeof(*psy), GFP_KERNEL);
736 	if (!psy)
737 		return ERR_PTR(-ENOMEM);
738 
739 	dev = &psy->dev;
740 
741 	device_initialize(dev);
742 
743 	dev->class = power_supply_class;
744 	dev->type = &power_supply_dev_type;
745 	dev->parent = parent;
746 	dev->release = power_supply_dev_release;
747 	dev_set_drvdata(dev, psy);
748 	psy->desc = desc;
749 	if (cfg) {
750 		psy->drv_data = cfg->drv_data;
751 		psy->of_node = cfg->of_node;
752 		psy->supplied_to = cfg->supplied_to;
753 		psy->num_supplicants = cfg->num_supplicants;
754 	}
755 
756 	rc = dev_set_name(dev, "%s", desc->name);
757 	if (rc)
758 		goto dev_set_name_failed;
759 
760 	INIT_WORK(&psy->changed_work, power_supply_changed_work);
761 	INIT_DELAYED_WORK(&psy->deferred_register_work,
762 			  power_supply_deferred_register_work);
763 
764 	rc = power_supply_check_supplies(psy);
765 	if (rc) {
766 		dev_info(dev, "Not all required supplies found, defer probe\n");
767 		goto check_supplies_failed;
768 	}
769 
770 	spin_lock_init(&psy->changed_lock);
771 	rc = device_init_wakeup(dev, ws);
772 	if (rc)
773 		goto wakeup_init_failed;
774 
775 	rc = device_add(dev);
776 	if (rc)
777 		goto device_add_failed;
778 
779 	rc = psy_register_thermal(psy);
780 	if (rc)
781 		goto register_thermal_failed;
782 
783 	rc = psy_register_cooler(psy);
784 	if (rc)
785 		goto register_cooler_failed;
786 
787 	rc = power_supply_create_triggers(psy);
788 	if (rc)
789 		goto create_triggers_failed;
790 
791 	/*
792 	 * Update use_cnt after any uevents (most notably from device_add()).
793 	 * We are here still during driver's probe but
794 	 * the power_supply_uevent() calls back driver's get_property
795 	 * method so:
796 	 * 1. Driver did not assigned the returned struct power_supply,
797 	 * 2. Driver could not finish initialization (anything in its probe
798 	 *    after calling power_supply_register()).
799 	 */
800 	atomic_inc(&psy->use_cnt);
801 	psy->initialized = true;
802 
803 	queue_delayed_work(system_power_efficient_wq,
804 			   &psy->deferred_register_work,
805 			   POWER_SUPPLY_DEFERRED_REGISTER_TIME);
806 
807 	return psy;
808 
809 create_triggers_failed:
810 	psy_unregister_cooler(psy);
811 register_cooler_failed:
812 	psy_unregister_thermal(psy);
813 register_thermal_failed:
814 	device_del(dev);
815 device_add_failed:
816 wakeup_init_failed:
817 check_supplies_failed:
818 dev_set_name_failed:
819 	put_device(dev);
820 	return ERR_PTR(rc);
821 }
822 
823 /**
824  * power_supply_register() - Register new power supply
825  * @parent:	Device to be a parent of power supply's device, usually
826  *		the device which probe function calls this
827  * @desc:	Description of power supply, must be valid through whole
828  *		lifetime of this power supply
829  * @cfg:	Run-time specific configuration accessed during registering,
830  *		may be NULL
831  *
832  * Return: A pointer to newly allocated power_supply on success
833  * or ERR_PTR otherwise.
834  * Use power_supply_unregister() on returned power_supply pointer to release
835  * resources.
836  */
837 struct power_supply *__must_check power_supply_register(struct device *parent,
838 		const struct power_supply_desc *desc,
839 		const struct power_supply_config *cfg)
840 {
841 	return __power_supply_register(parent, desc, cfg, true);
842 }
843 EXPORT_SYMBOL_GPL(power_supply_register);
844 
845 /**
846  * power_supply_register_no_ws() - Register new non-waking-source power supply
847  * @parent:	Device to be a parent of power supply's device, usually
848  *		the device which probe function calls this
849  * @desc:	Description of power supply, must be valid through whole
850  *		lifetime of this power supply
851  * @cfg:	Run-time specific configuration accessed during registering,
852  *		may be NULL
853  *
854  * Return: A pointer to newly allocated power_supply on success
855  * or ERR_PTR otherwise.
856  * Use power_supply_unregister() on returned power_supply pointer to release
857  * resources.
858  */
859 struct power_supply *__must_check
860 power_supply_register_no_ws(struct device *parent,
861 		const struct power_supply_desc *desc,
862 		const struct power_supply_config *cfg)
863 {
864 	return __power_supply_register(parent, desc, cfg, false);
865 }
866 EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
867 
868 static void devm_power_supply_release(struct device *dev, void *res)
869 {
870 	struct power_supply **psy = res;
871 
872 	power_supply_unregister(*psy);
873 }
874 
875 /**
876  * devm_power_supply_register() - Register managed power supply
877  * @parent:	Device to be a parent of power supply's device, usually
878  *		the device which probe function calls this
879  * @desc:	Description of power supply, must be valid through whole
880  *		lifetime of this power supply
881  * @cfg:	Run-time specific configuration accessed during registering,
882  *		may be NULL
883  *
884  * Return: A pointer to newly allocated power_supply on success
885  * or ERR_PTR otherwise.
886  * The returned power_supply pointer will be automatically unregistered
887  * on driver detach.
888  */
889 struct power_supply *__must_check
890 devm_power_supply_register(struct device *parent,
891 		const struct power_supply_desc *desc,
892 		const struct power_supply_config *cfg)
893 {
894 	struct power_supply **ptr, *psy;
895 
896 	ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
897 
898 	if (!ptr)
899 		return ERR_PTR(-ENOMEM);
900 	psy = __power_supply_register(parent, desc, cfg, true);
901 	if (IS_ERR(psy)) {
902 		devres_free(ptr);
903 	} else {
904 		*ptr = psy;
905 		devres_add(parent, ptr);
906 	}
907 	return psy;
908 }
909 EXPORT_SYMBOL_GPL(devm_power_supply_register);
910 
911 /**
912  * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
913  * @parent:	Device to be a parent of power supply's device, usually
914  *		the device which probe function calls this
915  * @desc:	Description of power supply, must be valid through whole
916  *		lifetime of this power supply
917  * @cfg:	Run-time specific configuration accessed during registering,
918  *		may be NULL
919  *
920  * Return: A pointer to newly allocated power_supply on success
921  * or ERR_PTR otherwise.
922  * The returned power_supply pointer will be automatically unregistered
923  * on driver detach.
924  */
925 struct power_supply *__must_check
926 devm_power_supply_register_no_ws(struct device *parent,
927 		const struct power_supply_desc *desc,
928 		const struct power_supply_config *cfg)
929 {
930 	struct power_supply **ptr, *psy;
931 
932 	ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
933 
934 	if (!ptr)
935 		return ERR_PTR(-ENOMEM);
936 	psy = __power_supply_register(parent, desc, cfg, false);
937 	if (IS_ERR(psy)) {
938 		devres_free(ptr);
939 	} else {
940 		*ptr = psy;
941 		devres_add(parent, ptr);
942 	}
943 	return psy;
944 }
945 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
946 
947 /**
948  * power_supply_unregister() - Remove this power supply from system
949  * @psy:	Pointer to power supply to unregister
950  *
951  * Remove this power supply from the system. The resources of power supply
952  * will be freed here or on last power_supply_put() call.
953  */
954 void power_supply_unregister(struct power_supply *psy)
955 {
956 	WARN_ON(atomic_dec_return(&psy->use_cnt));
957 	cancel_work_sync(&psy->changed_work);
958 	cancel_delayed_work_sync(&psy->deferred_register_work);
959 	sysfs_remove_link(&psy->dev.kobj, "powers");
960 	power_supply_remove_triggers(psy);
961 	psy_unregister_cooler(psy);
962 	psy_unregister_thermal(psy);
963 	device_init_wakeup(&psy->dev, false);
964 	device_unregister(&psy->dev);
965 }
966 EXPORT_SYMBOL_GPL(power_supply_unregister);
967 
968 void *power_supply_get_drvdata(struct power_supply *psy)
969 {
970 	return psy->drv_data;
971 }
972 EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
973 
974 static int __init power_supply_class_init(void)
975 {
976 	power_supply_class = class_create(THIS_MODULE, "power_supply");
977 
978 	if (IS_ERR(power_supply_class))
979 		return PTR_ERR(power_supply_class);
980 
981 	power_supply_class->dev_uevent = power_supply_uevent;
982 	power_supply_init_attrs(&power_supply_dev_type);
983 
984 	return 0;
985 }
986 
987 static void __exit power_supply_class_exit(void)
988 {
989 	class_destroy(power_supply_class);
990 }
991 
992 subsys_initcall(power_supply_class_init);
993 module_exit(power_supply_class_exit);
994 
995 MODULE_DESCRIPTION("Universal power supply monitor class");
996 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
997 	      "Szabolcs Gyurko, "
998 	      "Anton Vorontsov <cbou@mail.ru>");
999 MODULE_LICENSE("GPL");
1000