xref: /linux/drivers/power/supply/power_supply_core.c (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Universal power supply monitor class
4  *
5  *  Copyright © 2007  Anton Vorontsov <cbou@mail.ru>
6  *  Copyright © 2004  Szabolcs Gyurko
7  *  Copyright © 2003  Ian Molton <spyro@f2s.com>
8  *
9  *  Modified: 2004, Oct     Szabolcs Gyurko
10  */
11 
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/delay.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/property.h>
23 #include <linux/thermal.h>
24 #include <linux/fixp-arith.h>
25 #include "power_supply.h"
26 
27 /* exported for the APM Power driver, APM emulation */
28 struct class *power_supply_class;
29 EXPORT_SYMBOL_GPL(power_supply_class);
30 
31 ATOMIC_NOTIFIER_HEAD(power_supply_notifier);
32 EXPORT_SYMBOL_GPL(power_supply_notifier);
33 
34 static struct device_type power_supply_dev_type;
35 
36 #define POWER_SUPPLY_DEFERRED_REGISTER_TIME	msecs_to_jiffies(10)
37 
38 static bool __power_supply_is_supplied_by(struct power_supply *supplier,
39 					 struct power_supply *supply)
40 {
41 	int i;
42 
43 	if (!supply->supplied_from && !supplier->supplied_to)
44 		return false;
45 
46 	/* Support both supplied_to and supplied_from modes */
47 	if (supply->supplied_from) {
48 		if (!supplier->desc->name)
49 			return false;
50 		for (i = 0; i < supply->num_supplies; i++)
51 			if (!strcmp(supplier->desc->name, supply->supplied_from[i]))
52 				return true;
53 	} else {
54 		if (!supply->desc->name)
55 			return false;
56 		for (i = 0; i < supplier->num_supplicants; i++)
57 			if (!strcmp(supplier->supplied_to[i], supply->desc->name))
58 				return true;
59 	}
60 
61 	return false;
62 }
63 
64 static int __power_supply_changed_work(struct device *dev, void *data)
65 {
66 	struct power_supply *psy = data;
67 	struct power_supply *pst = dev_get_drvdata(dev);
68 
69 	if (__power_supply_is_supplied_by(psy, pst)) {
70 		if (pst->desc->external_power_changed)
71 			pst->desc->external_power_changed(pst);
72 	}
73 
74 	return 0;
75 }
76 
77 static void power_supply_changed_work(struct work_struct *work)
78 {
79 	unsigned long flags;
80 	struct power_supply *psy = container_of(work, struct power_supply,
81 						changed_work);
82 
83 	dev_dbg(&psy->dev, "%s\n", __func__);
84 
85 	spin_lock_irqsave(&psy->changed_lock, flags);
86 	/*
87 	 * Check 'changed' here to avoid issues due to race between
88 	 * power_supply_changed() and this routine. In worst case
89 	 * power_supply_changed() can be called again just before we take above
90 	 * lock. During the first call of this routine we will mark 'changed' as
91 	 * false and it will stay false for the next call as well.
92 	 */
93 	if (likely(psy->changed)) {
94 		psy->changed = false;
95 		spin_unlock_irqrestore(&psy->changed_lock, flags);
96 		class_for_each_device(power_supply_class, NULL, psy,
97 				      __power_supply_changed_work);
98 		power_supply_update_leds(psy);
99 		atomic_notifier_call_chain(&power_supply_notifier,
100 				PSY_EVENT_PROP_CHANGED, psy);
101 		kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
102 		spin_lock_irqsave(&psy->changed_lock, flags);
103 	}
104 
105 	/*
106 	 * Hold the wakeup_source until all events are processed.
107 	 * power_supply_changed() might have called again and have set 'changed'
108 	 * to true.
109 	 */
110 	if (likely(!psy->changed))
111 		pm_relax(&psy->dev);
112 	spin_unlock_irqrestore(&psy->changed_lock, flags);
113 }
114 
115 void power_supply_changed(struct power_supply *psy)
116 {
117 	unsigned long flags;
118 
119 	dev_dbg(&psy->dev, "%s\n", __func__);
120 
121 	spin_lock_irqsave(&psy->changed_lock, flags);
122 	psy->changed = true;
123 	pm_stay_awake(&psy->dev);
124 	spin_unlock_irqrestore(&psy->changed_lock, flags);
125 	schedule_work(&psy->changed_work);
126 }
127 EXPORT_SYMBOL_GPL(power_supply_changed);
128 
129 /*
130  * Notify that power supply was registered after parent finished the probing.
131  *
132  * Often power supply is registered from driver's probe function. However
133  * calling power_supply_changed() directly from power_supply_register()
134  * would lead to execution of get_property() function provided by the driver
135  * too early - before the probe ends.
136  *
137  * Avoid that by waiting on parent's mutex.
138  */
139 static void power_supply_deferred_register_work(struct work_struct *work)
140 {
141 	struct power_supply *psy = container_of(work, struct power_supply,
142 						deferred_register_work.work);
143 
144 	if (psy->dev.parent) {
145 		while (!mutex_trylock(&psy->dev.parent->mutex)) {
146 			if (psy->removing)
147 				return;
148 			msleep(10);
149 		}
150 	}
151 
152 	power_supply_changed(psy);
153 
154 	if (psy->dev.parent)
155 		mutex_unlock(&psy->dev.parent->mutex);
156 }
157 
158 #ifdef CONFIG_OF
159 static int __power_supply_populate_supplied_from(struct device *dev,
160 						 void *data)
161 {
162 	struct power_supply *psy = data;
163 	struct power_supply *epsy = dev_get_drvdata(dev);
164 	struct device_node *np;
165 	int i = 0;
166 
167 	do {
168 		np = of_parse_phandle(psy->of_node, "power-supplies", i++);
169 		if (!np)
170 			break;
171 
172 		if (np == epsy->of_node) {
173 			dev_dbg(&psy->dev, "%s: Found supply : %s\n",
174 				psy->desc->name, epsy->desc->name);
175 			psy->supplied_from[i-1] = (char *)epsy->desc->name;
176 			psy->num_supplies++;
177 			of_node_put(np);
178 			break;
179 		}
180 		of_node_put(np);
181 	} while (np);
182 
183 	return 0;
184 }
185 
186 static int power_supply_populate_supplied_from(struct power_supply *psy)
187 {
188 	int error;
189 
190 	error = class_for_each_device(power_supply_class, NULL, psy,
191 				      __power_supply_populate_supplied_from);
192 
193 	dev_dbg(&psy->dev, "%s %d\n", __func__, error);
194 
195 	return error;
196 }
197 
198 static int  __power_supply_find_supply_from_node(struct device *dev,
199 						 void *data)
200 {
201 	struct device_node *np = data;
202 	struct power_supply *epsy = dev_get_drvdata(dev);
203 
204 	/* returning non-zero breaks out of class_for_each_device loop */
205 	if (epsy->of_node == np)
206 		return 1;
207 
208 	return 0;
209 }
210 
211 static int power_supply_find_supply_from_node(struct device_node *supply_node)
212 {
213 	int error;
214 
215 	/*
216 	 * class_for_each_device() either returns its own errors or values
217 	 * returned by __power_supply_find_supply_from_node().
218 	 *
219 	 * __power_supply_find_supply_from_node() will return 0 (no match)
220 	 * or 1 (match).
221 	 *
222 	 * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
223 	 * it returned 0, or error as returned by it.
224 	 */
225 	error = class_for_each_device(power_supply_class, NULL, supply_node,
226 				       __power_supply_find_supply_from_node);
227 
228 	return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
229 }
230 
231 static int power_supply_check_supplies(struct power_supply *psy)
232 {
233 	struct device_node *np;
234 	int cnt = 0;
235 
236 	/* If there is already a list honor it */
237 	if (psy->supplied_from && psy->num_supplies > 0)
238 		return 0;
239 
240 	/* No device node found, nothing to do */
241 	if (!psy->of_node)
242 		return 0;
243 
244 	do {
245 		int ret;
246 
247 		np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
248 		if (!np)
249 			break;
250 
251 		ret = power_supply_find_supply_from_node(np);
252 		of_node_put(np);
253 
254 		if (ret) {
255 			dev_dbg(&psy->dev, "Failed to find supply!\n");
256 			return ret;
257 		}
258 	} while (np);
259 
260 	/* Missing valid "power-supplies" entries */
261 	if (cnt == 1)
262 		return 0;
263 
264 	/* All supplies found, allocate char ** array for filling */
265 	psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
266 					  GFP_KERNEL);
267 	if (!psy->supplied_from)
268 		return -ENOMEM;
269 
270 	*psy->supplied_from = devm_kcalloc(&psy->dev,
271 					   cnt - 1, sizeof(char *),
272 					   GFP_KERNEL);
273 	if (!*psy->supplied_from)
274 		return -ENOMEM;
275 
276 	return power_supply_populate_supplied_from(psy);
277 }
278 #else
279 static int power_supply_check_supplies(struct power_supply *psy)
280 {
281 	int nval, ret;
282 
283 	if (!psy->dev.parent)
284 		return 0;
285 
286 	nval = device_property_read_string_array(psy->dev.parent,
287 						 "supplied-from", NULL, 0);
288 	if (nval <= 0)
289 		return 0;
290 
291 	psy->supplied_from = devm_kmalloc_array(&psy->dev, nval,
292 						sizeof(char *), GFP_KERNEL);
293 	if (!psy->supplied_from)
294 		return -ENOMEM;
295 
296 	ret = device_property_read_string_array(psy->dev.parent,
297 		"supplied-from", (const char **)psy->supplied_from, nval);
298 	if (ret < 0)
299 		return ret;
300 
301 	psy->num_supplies = nval;
302 
303 	return 0;
304 }
305 #endif
306 
307 struct psy_am_i_supplied_data {
308 	struct power_supply *psy;
309 	unsigned int count;
310 };
311 
312 static int __power_supply_am_i_supplied(struct device *dev, void *_data)
313 {
314 	union power_supply_propval ret = {0,};
315 	struct power_supply *epsy = dev_get_drvdata(dev);
316 	struct psy_am_i_supplied_data *data = _data;
317 
318 	if (__power_supply_is_supplied_by(epsy, data->psy)) {
319 		data->count++;
320 		if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
321 					&ret))
322 			return ret.intval;
323 	}
324 
325 	return 0;
326 }
327 
328 int power_supply_am_i_supplied(struct power_supply *psy)
329 {
330 	struct psy_am_i_supplied_data data = { psy, 0 };
331 	int error;
332 
333 	error = class_for_each_device(power_supply_class, NULL, &data,
334 				      __power_supply_am_i_supplied);
335 
336 	dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
337 
338 	if (data.count == 0)
339 		return -ENODEV;
340 
341 	return error;
342 }
343 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
344 
345 static int __power_supply_is_system_supplied(struct device *dev, void *data)
346 {
347 	union power_supply_propval ret = {0,};
348 	struct power_supply *psy = dev_get_drvdata(dev);
349 	unsigned int *count = data;
350 
351 	(*count)++;
352 	if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
353 		if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
354 					&ret))
355 			return ret.intval;
356 
357 	return 0;
358 }
359 
360 int power_supply_is_system_supplied(void)
361 {
362 	int error;
363 	unsigned int count = 0;
364 
365 	error = class_for_each_device(power_supply_class, NULL, &count,
366 				      __power_supply_is_system_supplied);
367 
368 	/*
369 	 * If no power class device was found at all, most probably we are
370 	 * running on a desktop system, so assume we are on mains power.
371 	 */
372 	if (count == 0)
373 		return 1;
374 
375 	return error;
376 }
377 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
378 
379 static int __power_supply_get_supplier_max_current(struct device *dev,
380 						   void *data)
381 {
382 	union power_supply_propval ret = {0,};
383 	struct power_supply *epsy = dev_get_drvdata(dev);
384 	struct power_supply *psy = data;
385 
386 	if (__power_supply_is_supplied_by(epsy, psy))
387 		if (!epsy->desc->get_property(epsy,
388 					      POWER_SUPPLY_PROP_CURRENT_MAX,
389 					      &ret))
390 			return ret.intval;
391 
392 	return 0;
393 }
394 
395 int power_supply_set_input_current_limit_from_supplier(struct power_supply *psy)
396 {
397 	union power_supply_propval val = {0,};
398 	int curr;
399 
400 	if (!psy->desc->set_property)
401 		return -EINVAL;
402 
403 	/*
404 	 * This function is not intended for use with a supply with multiple
405 	 * suppliers, we simply pick the first supply to report a non 0
406 	 * max-current.
407 	 */
408 	curr = class_for_each_device(power_supply_class, NULL, psy,
409 				      __power_supply_get_supplier_max_current);
410 	if (curr <= 0)
411 		return (curr == 0) ? -ENODEV : curr;
412 
413 	val.intval = curr;
414 
415 	return psy->desc->set_property(psy,
416 				POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
417 }
418 EXPORT_SYMBOL_GPL(power_supply_set_input_current_limit_from_supplier);
419 
420 int power_supply_set_battery_charged(struct power_supply *psy)
421 {
422 	if (atomic_read(&psy->use_cnt) >= 0 &&
423 			psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
424 			psy->desc->set_charged) {
425 		psy->desc->set_charged(psy);
426 		return 0;
427 	}
428 
429 	return -EINVAL;
430 }
431 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
432 
433 static int power_supply_match_device_by_name(struct device *dev, const void *data)
434 {
435 	const char *name = data;
436 	struct power_supply *psy = dev_get_drvdata(dev);
437 
438 	return strcmp(psy->desc->name, name) == 0;
439 }
440 
441 /**
442  * power_supply_get_by_name() - Search for a power supply and returns its ref
443  * @name: Power supply name to fetch
444  *
445  * If power supply was found, it increases reference count for the
446  * internal power supply's device. The user should power_supply_put()
447  * after usage.
448  *
449  * Return: On success returns a reference to a power supply with
450  * matching name equals to @name, a NULL otherwise.
451  */
452 struct power_supply *power_supply_get_by_name(const char *name)
453 {
454 	struct power_supply *psy = NULL;
455 	struct device *dev = class_find_device(power_supply_class, NULL, name,
456 					power_supply_match_device_by_name);
457 
458 	if (dev) {
459 		psy = dev_get_drvdata(dev);
460 		atomic_inc(&psy->use_cnt);
461 	}
462 
463 	return psy;
464 }
465 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
466 
467 /**
468  * power_supply_put() - Drop reference obtained with power_supply_get_by_name
469  * @psy: Reference to put
470  *
471  * The reference to power supply should be put before unregistering
472  * the power supply.
473  */
474 void power_supply_put(struct power_supply *psy)
475 {
476 	might_sleep();
477 
478 	atomic_dec(&psy->use_cnt);
479 	put_device(&psy->dev);
480 }
481 EXPORT_SYMBOL_GPL(power_supply_put);
482 
483 #ifdef CONFIG_OF
484 static int power_supply_match_device_node(struct device *dev, const void *data)
485 {
486 	return dev->parent && dev->parent->of_node == data;
487 }
488 
489 /**
490  * power_supply_get_by_phandle() - Search for a power supply and returns its ref
491  * @np: Pointer to device node holding phandle property
492  * @property: Name of property holding a power supply name
493  *
494  * If power supply was found, it increases reference count for the
495  * internal power supply's device. The user should power_supply_put()
496  * after usage.
497  *
498  * Return: On success returns a reference to a power supply with
499  * matching name equals to value under @property, NULL or ERR_PTR otherwise.
500  */
501 struct power_supply *power_supply_get_by_phandle(struct device_node *np,
502 							const char *property)
503 {
504 	struct device_node *power_supply_np;
505 	struct power_supply *psy = NULL;
506 	struct device *dev;
507 
508 	power_supply_np = of_parse_phandle(np, property, 0);
509 	if (!power_supply_np)
510 		return ERR_PTR(-ENODEV);
511 
512 	dev = class_find_device(power_supply_class, NULL, power_supply_np,
513 						power_supply_match_device_node);
514 
515 	of_node_put(power_supply_np);
516 
517 	if (dev) {
518 		psy = dev_get_drvdata(dev);
519 		atomic_inc(&psy->use_cnt);
520 	}
521 
522 	return psy;
523 }
524 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
525 
526 static void devm_power_supply_put(struct device *dev, void *res)
527 {
528 	struct power_supply **psy = res;
529 
530 	power_supply_put(*psy);
531 }
532 
533 /**
534  * devm_power_supply_get_by_phandle() - Resource managed version of
535  *  power_supply_get_by_phandle()
536  * @dev: Pointer to device holding phandle property
537  * @property: Name of property holding a power supply phandle
538  *
539  * Return: On success returns a reference to a power supply with
540  * matching name equals to value under @property, NULL or ERR_PTR otherwise.
541  */
542 struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
543 						      const char *property)
544 {
545 	struct power_supply **ptr, *psy;
546 
547 	if (!dev->of_node)
548 		return ERR_PTR(-ENODEV);
549 
550 	ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
551 	if (!ptr)
552 		return ERR_PTR(-ENOMEM);
553 
554 	psy = power_supply_get_by_phandle(dev->of_node, property);
555 	if (IS_ERR_OR_NULL(psy)) {
556 		devres_free(ptr);
557 	} else {
558 		*ptr = psy;
559 		devres_add(dev, ptr);
560 	}
561 	return psy;
562 }
563 EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
564 #endif /* CONFIG_OF */
565 
566 int power_supply_get_battery_info(struct power_supply *psy,
567 				  struct power_supply_battery_info **info_out)
568 {
569 	struct power_supply_resistance_temp_table *resist_table;
570 	struct power_supply_battery_info *info;
571 	struct device_node *battery_np;
572 	const char *value;
573 	int err, len, index;
574 	const __be32 *list;
575 
576 	info = devm_kmalloc(&psy->dev, sizeof(*info), GFP_KERNEL);
577 	if (!info)
578 		return -ENOMEM;
579 
580 	info->technology                     = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
581 	info->energy_full_design_uwh         = -EINVAL;
582 	info->charge_full_design_uah         = -EINVAL;
583 	info->voltage_min_design_uv          = -EINVAL;
584 	info->voltage_max_design_uv          = -EINVAL;
585 	info->precharge_current_ua           = -EINVAL;
586 	info->charge_term_current_ua         = -EINVAL;
587 	info->constant_charge_current_max_ua = -EINVAL;
588 	info->constant_charge_voltage_max_uv = -EINVAL;
589 	info->tricklecharge_current_ua       = -EINVAL;
590 	info->precharge_voltage_max_uv       = -EINVAL;
591 	info->charge_restart_voltage_uv      = -EINVAL;
592 	info->overvoltage_limit_uv           = -EINVAL;
593 	info->temp_ambient_alert_min         = INT_MIN;
594 	info->temp_ambient_alert_max         = INT_MAX;
595 	info->temp_alert_min                 = INT_MIN;
596 	info->temp_alert_max                 = INT_MAX;
597 	info->temp_min                       = INT_MIN;
598 	info->temp_max                       = INT_MAX;
599 	info->factory_internal_resistance_uohm  = -EINVAL;
600 	info->resist_table = NULL;
601 
602 	for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) {
603 		info->ocv_table[index]       = NULL;
604 		info->ocv_temp[index]        = -EINVAL;
605 		info->ocv_table_size[index]  = -EINVAL;
606 	}
607 
608 	if (!psy->of_node) {
609 		dev_warn(&psy->dev, "%s currently only supports devicetree\n",
610 			 __func__);
611 		return -ENXIO;
612 	}
613 
614 	battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);
615 	if (!battery_np)
616 		return -ENODEV;
617 
618 	err = of_property_read_string(battery_np, "compatible", &value);
619 	if (err)
620 		goto out_put_node;
621 
622 	if (strcmp("simple-battery", value)) {
623 		err = -ENODEV;
624 		goto out_put_node;
625 	}
626 
627 	/* The property and field names below must correspond to elements
628 	 * in enum power_supply_property. For reasoning, see
629 	 * Documentation/power/power_supply_class.rst.
630 	 */
631 
632 	if (!of_property_read_string(battery_np, "device-chemistry", &value)) {
633 		if (!strcmp("nickel-cadmium", value))
634 			info->technology = POWER_SUPPLY_TECHNOLOGY_NiCd;
635 		else if (!strcmp("nickel-metal-hydride", value))
636 			info->technology = POWER_SUPPLY_TECHNOLOGY_NiMH;
637 		else if (!strcmp("lithium-ion", value))
638 			/* Imprecise lithium-ion type */
639 			info->technology = POWER_SUPPLY_TECHNOLOGY_LION;
640 		else if (!strcmp("lithium-ion-polymer", value))
641 			info->technology = POWER_SUPPLY_TECHNOLOGY_LIPO;
642 		else if (!strcmp("lithium-ion-iron-phosphate", value))
643 			info->technology = POWER_SUPPLY_TECHNOLOGY_LiFe;
644 		else if (!strcmp("lithium-ion-manganese-oxide", value))
645 			info->technology = POWER_SUPPLY_TECHNOLOGY_LiMn;
646 		else
647 			dev_warn(&psy->dev, "%s unknown battery type\n", value);
648 	}
649 
650 	of_property_read_u32(battery_np, "energy-full-design-microwatt-hours",
651 			     &info->energy_full_design_uwh);
652 	of_property_read_u32(battery_np, "charge-full-design-microamp-hours",
653 			     &info->charge_full_design_uah);
654 	of_property_read_u32(battery_np, "voltage-min-design-microvolt",
655 			     &info->voltage_min_design_uv);
656 	of_property_read_u32(battery_np, "voltage-max-design-microvolt",
657 			     &info->voltage_max_design_uv);
658 	of_property_read_u32(battery_np, "trickle-charge-current-microamp",
659 			     &info->tricklecharge_current_ua);
660 	of_property_read_u32(battery_np, "precharge-current-microamp",
661 			     &info->precharge_current_ua);
662 	of_property_read_u32(battery_np, "precharge-upper-limit-microvolt",
663 			     &info->precharge_voltage_max_uv);
664 	of_property_read_u32(battery_np, "charge-term-current-microamp",
665 			     &info->charge_term_current_ua);
666 	of_property_read_u32(battery_np, "re-charge-voltage-microvolt",
667 			     &info->charge_restart_voltage_uv);
668 	of_property_read_u32(battery_np, "over-voltage-threshold-microvolt",
669 			     &info->overvoltage_limit_uv);
670 	of_property_read_u32(battery_np, "constant-charge-current-max-microamp",
671 			     &info->constant_charge_current_max_ua);
672 	of_property_read_u32(battery_np, "constant-charge-voltage-max-microvolt",
673 			     &info->constant_charge_voltage_max_uv);
674 	of_property_read_u32(battery_np, "factory-internal-resistance-micro-ohms",
675 			     &info->factory_internal_resistance_uohm);
676 
677 	of_property_read_u32_index(battery_np, "ambient-celsius",
678 				   0, &info->temp_ambient_alert_min);
679 	of_property_read_u32_index(battery_np, "ambient-celsius",
680 				   1, &info->temp_ambient_alert_max);
681 	of_property_read_u32_index(battery_np, "alert-celsius",
682 				   0, &info->temp_alert_min);
683 	of_property_read_u32_index(battery_np, "alert-celsius",
684 				   1, &info->temp_alert_max);
685 	of_property_read_u32_index(battery_np, "operating-range-celsius",
686 				   0, &info->temp_min);
687 	of_property_read_u32_index(battery_np, "operating-range-celsius",
688 				   1, &info->temp_max);
689 
690 	len = of_property_count_u32_elems(battery_np, "ocv-capacity-celsius");
691 	if (len < 0 && len != -EINVAL) {
692 		err = len;
693 		goto out_put_node;
694 	} else if (len > POWER_SUPPLY_OCV_TEMP_MAX) {
695 		dev_err(&psy->dev, "Too many temperature values\n");
696 		err = -EINVAL;
697 		goto out_put_node;
698 	} else if (len > 0) {
699 		of_property_read_u32_array(battery_np, "ocv-capacity-celsius",
700 					   info->ocv_temp, len);
701 	}
702 
703 	for (index = 0; index < len; index++) {
704 		struct power_supply_battery_ocv_table *table;
705 		char *propname;
706 		int i, tab_len, size;
707 
708 		propname = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d", index);
709 		list = of_get_property(battery_np, propname, &size);
710 		if (!list || !size) {
711 			dev_err(&psy->dev, "failed to get %s\n", propname);
712 			kfree(propname);
713 			power_supply_put_battery_info(psy, info);
714 			err = -EINVAL;
715 			goto out_put_node;
716 		}
717 
718 		kfree(propname);
719 		tab_len = size / (2 * sizeof(__be32));
720 		info->ocv_table_size[index] = tab_len;
721 
722 		table = info->ocv_table[index] =
723 			devm_kcalloc(&psy->dev, tab_len, sizeof(*table), GFP_KERNEL);
724 		if (!info->ocv_table[index]) {
725 			power_supply_put_battery_info(psy, info);
726 			err = -ENOMEM;
727 			goto out_put_node;
728 		}
729 
730 		for (i = 0; i < tab_len; i++) {
731 			table[i].ocv = be32_to_cpu(*list);
732 			list++;
733 			table[i].capacity = be32_to_cpu(*list);
734 			list++;
735 		}
736 	}
737 
738 	list = of_get_property(battery_np, "resistance-temp-table", &len);
739 	if (!list || !len)
740 		goto out_ret_pointer;
741 
742 	info->resist_table_size = len / (2 * sizeof(__be32));
743 	resist_table = info->resist_table = devm_kcalloc(&psy->dev,
744 							 info->resist_table_size,
745 							 sizeof(*resist_table),
746 							 GFP_KERNEL);
747 	if (!info->resist_table) {
748 		power_supply_put_battery_info(psy, info);
749 		err = -ENOMEM;
750 		goto out_put_node;
751 	}
752 
753 	for (index = 0; index < info->resist_table_size; index++) {
754 		resist_table[index].temp = be32_to_cpu(*list++);
755 		resist_table[index].resistance = be32_to_cpu(*list++);
756 	}
757 
758 out_ret_pointer:
759 	/* Finally return the whole thing */
760 	*info_out = info;
761 
762 out_put_node:
763 	of_node_put(battery_np);
764 	return err;
765 }
766 EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
767 
768 void power_supply_put_battery_info(struct power_supply *psy,
769 				   struct power_supply_battery_info *info)
770 {
771 	int i;
772 
773 	for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
774 		if (info->ocv_table[i])
775 			devm_kfree(&psy->dev, info->ocv_table[i]);
776 	}
777 
778 	if (info->resist_table)
779 		devm_kfree(&psy->dev, info->resist_table);
780 
781 	devm_kfree(&psy->dev, info);
782 }
783 EXPORT_SYMBOL_GPL(power_supply_put_battery_info);
784 
785 /**
786  * power_supply_temp2resist_simple() - find the battery internal resistance
787  * percent
788  * @table: Pointer to battery resistance temperature table
789  * @table_len: The table length
790  * @temp: Current temperature
791  *
792  * This helper function is used to look up battery internal resistance percent
793  * according to current temperature value from the resistance temperature table,
794  * and the table must be ordered descending. Then the actual battery internal
795  * resistance = the ideal battery internal resistance * percent / 100.
796  *
797  * Return: the battery internal resistance percent
798  */
799 int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table,
800 				    int table_len, int temp)
801 {
802 	int i, high, low;
803 
804 	/* Break loop at table_len - 1 because that is the highest index */
805 	for (i = 0; i < table_len - 1; i++)
806 		if (temp > table[i].temp)
807 			break;
808 
809 	/* The library function will deal with high == low */
810 	if ((i == 0) || (i == (table_len - 1)))
811 		high = i;
812 	else
813 		high = i - 1;
814 	low = i;
815 
816 	return fixp_linear_interpolate(table[low].temp,
817 				       table[low].resistance,
818 				       table[high].temp,
819 				       table[high].resistance,
820 				       temp);
821 }
822 EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple);
823 
824 /**
825  * power_supply_ocv2cap_simple() - find the battery capacity
826  * @table: Pointer to battery OCV lookup table
827  * @table_len: OCV table length
828  * @ocv: Current OCV value
829  *
830  * This helper function is used to look up battery capacity according to
831  * current OCV value from one OCV table, and the OCV table must be ordered
832  * descending.
833  *
834  * Return: the battery capacity.
835  */
836 int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table,
837 				int table_len, int ocv)
838 {
839 	int i, high, low;
840 
841 	/* Break loop at table_len - 1 because that is the highest index */
842 	for (i = 0; i < table_len - 1; i++)
843 		if (ocv > table[i].ocv)
844 			break;
845 
846 	/* The library function will deal with high == low */
847 	if ((i == 0) || (i == (table_len - 1)))
848 		high = i - 1;
849 	else
850 		high = i; /* i.e. i == 0 */
851 	low = i;
852 
853 	return fixp_linear_interpolate(table[low].ocv,
854 				       table[low].capacity,
855 				       table[high].ocv,
856 				       table[high].capacity,
857 				       ocv);
858 }
859 EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple);
860 
861 struct power_supply_battery_ocv_table *
862 power_supply_find_ocv2cap_table(struct power_supply_battery_info *info,
863 				int temp, int *table_len)
864 {
865 	int best_temp_diff = INT_MAX, temp_diff;
866 	u8 i, best_index = 0;
867 
868 	if (!info->ocv_table[0])
869 		return NULL;
870 
871 	for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
872 		/* Out of capacity tables */
873 		if (!info->ocv_table[i])
874 			break;
875 
876 		temp_diff = abs(info->ocv_temp[i] - temp);
877 
878 		if (temp_diff < best_temp_diff) {
879 			best_temp_diff = temp_diff;
880 			best_index = i;
881 		}
882 	}
883 
884 	*table_len = info->ocv_table_size[best_index];
885 	return info->ocv_table[best_index];
886 }
887 EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table);
888 
889 int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info,
890 				 int ocv, int temp)
891 {
892 	struct power_supply_battery_ocv_table *table;
893 	int table_len;
894 
895 	table = power_supply_find_ocv2cap_table(info, temp, &table_len);
896 	if (!table)
897 		return -EINVAL;
898 
899 	return power_supply_ocv2cap_simple(table, table_len, ocv);
900 }
901 EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap);
902 
903 int power_supply_get_property(struct power_supply *psy,
904 			    enum power_supply_property psp,
905 			    union power_supply_propval *val)
906 {
907 	if (atomic_read(&psy->use_cnt) <= 0) {
908 		if (!psy->initialized)
909 			return -EAGAIN;
910 		return -ENODEV;
911 	}
912 
913 	return psy->desc->get_property(psy, psp, val);
914 }
915 EXPORT_SYMBOL_GPL(power_supply_get_property);
916 
917 int power_supply_set_property(struct power_supply *psy,
918 			    enum power_supply_property psp,
919 			    const union power_supply_propval *val)
920 {
921 	if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
922 		return -ENODEV;
923 
924 	return psy->desc->set_property(psy, psp, val);
925 }
926 EXPORT_SYMBOL_GPL(power_supply_set_property);
927 
928 int power_supply_property_is_writeable(struct power_supply *psy,
929 					enum power_supply_property psp)
930 {
931 	if (atomic_read(&psy->use_cnt) <= 0 ||
932 			!psy->desc->property_is_writeable)
933 		return -ENODEV;
934 
935 	return psy->desc->property_is_writeable(psy, psp);
936 }
937 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
938 
939 void power_supply_external_power_changed(struct power_supply *psy)
940 {
941 	if (atomic_read(&psy->use_cnt) <= 0 ||
942 			!psy->desc->external_power_changed)
943 		return;
944 
945 	psy->desc->external_power_changed(psy);
946 }
947 EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
948 
949 int power_supply_powers(struct power_supply *psy, struct device *dev)
950 {
951 	return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
952 }
953 EXPORT_SYMBOL_GPL(power_supply_powers);
954 
955 static void power_supply_dev_release(struct device *dev)
956 {
957 	struct power_supply *psy = to_power_supply(dev);
958 	dev_dbg(dev, "%s\n", __func__);
959 	kfree(psy);
960 }
961 
962 int power_supply_reg_notifier(struct notifier_block *nb)
963 {
964 	return atomic_notifier_chain_register(&power_supply_notifier, nb);
965 }
966 EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
967 
968 void power_supply_unreg_notifier(struct notifier_block *nb)
969 {
970 	atomic_notifier_chain_unregister(&power_supply_notifier, nb);
971 }
972 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
973 
974 static bool psy_has_property(const struct power_supply_desc *psy_desc,
975 			     enum power_supply_property psp)
976 {
977 	bool found = false;
978 	int i;
979 
980 	for (i = 0; i < psy_desc->num_properties; i++) {
981 		if (psy_desc->properties[i] == psp) {
982 			found = true;
983 			break;
984 		}
985 	}
986 
987 	return found;
988 }
989 
990 #ifdef CONFIG_THERMAL
991 static int power_supply_read_temp(struct thermal_zone_device *tzd,
992 		int *temp)
993 {
994 	struct power_supply *psy;
995 	union power_supply_propval val;
996 	int ret;
997 
998 	WARN_ON(tzd == NULL);
999 	psy = tzd->devdata;
1000 	ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
1001 	if (ret)
1002 		return ret;
1003 
1004 	/* Convert tenths of degree Celsius to milli degree Celsius. */
1005 	*temp = val.intval * 100;
1006 
1007 	return ret;
1008 }
1009 
1010 static struct thermal_zone_device_ops psy_tzd_ops = {
1011 	.get_temp = power_supply_read_temp,
1012 };
1013 
1014 static int psy_register_thermal(struct power_supply *psy)
1015 {
1016 	int ret;
1017 
1018 	if (psy->desc->no_thermal)
1019 		return 0;
1020 
1021 	/* Register battery zone device psy reports temperature */
1022 	if (psy_has_property(psy->desc, POWER_SUPPLY_PROP_TEMP)) {
1023 		psy->tzd = thermal_zone_device_register(psy->desc->name,
1024 				0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
1025 		if (IS_ERR(psy->tzd))
1026 			return PTR_ERR(psy->tzd);
1027 		ret = thermal_zone_device_enable(psy->tzd);
1028 		if (ret)
1029 			thermal_zone_device_unregister(psy->tzd);
1030 		return ret;
1031 	}
1032 
1033 	return 0;
1034 }
1035 
1036 static void psy_unregister_thermal(struct power_supply *psy)
1037 {
1038 	if (IS_ERR_OR_NULL(psy->tzd))
1039 		return;
1040 	thermal_zone_device_unregister(psy->tzd);
1041 }
1042 
1043 /* thermal cooling device callbacks */
1044 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
1045 					unsigned long *state)
1046 {
1047 	struct power_supply *psy;
1048 	union power_supply_propval val;
1049 	int ret;
1050 
1051 	psy = tcd->devdata;
1052 	ret = power_supply_get_property(psy,
1053 			POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
1054 	if (ret)
1055 		return ret;
1056 
1057 	*state = val.intval;
1058 
1059 	return ret;
1060 }
1061 
1062 static int ps_get_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
1063 					unsigned long *state)
1064 {
1065 	struct power_supply *psy;
1066 	union power_supply_propval val;
1067 	int ret;
1068 
1069 	psy = tcd->devdata;
1070 	ret = power_supply_get_property(psy,
1071 			POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
1072 	if (ret)
1073 		return ret;
1074 
1075 	*state = val.intval;
1076 
1077 	return ret;
1078 }
1079 
1080 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
1081 					unsigned long state)
1082 {
1083 	struct power_supply *psy;
1084 	union power_supply_propval val;
1085 	int ret;
1086 
1087 	psy = tcd->devdata;
1088 	val.intval = state;
1089 	ret = psy->desc->set_property(psy,
1090 		POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
1091 
1092 	return ret;
1093 }
1094 
1095 static const struct thermal_cooling_device_ops psy_tcd_ops = {
1096 	.get_max_state = ps_get_max_charge_cntl_limit,
1097 	.get_cur_state = ps_get_cur_charge_cntl_limit,
1098 	.set_cur_state = ps_set_cur_charge_cntl_limit,
1099 };
1100 
1101 static int psy_register_cooler(struct power_supply *psy)
1102 {
1103 	/* Register for cooling device if psy can control charging */
1104 	if (psy_has_property(psy->desc, POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT)) {
1105 		psy->tcd = thermal_cooling_device_register(
1106 			(char *)psy->desc->name,
1107 			psy, &psy_tcd_ops);
1108 		return PTR_ERR_OR_ZERO(psy->tcd);
1109 	}
1110 
1111 	return 0;
1112 }
1113 
1114 static void psy_unregister_cooler(struct power_supply *psy)
1115 {
1116 	if (IS_ERR_OR_NULL(psy->tcd))
1117 		return;
1118 	thermal_cooling_device_unregister(psy->tcd);
1119 }
1120 #else
1121 static int psy_register_thermal(struct power_supply *psy)
1122 {
1123 	return 0;
1124 }
1125 
1126 static void psy_unregister_thermal(struct power_supply *psy)
1127 {
1128 }
1129 
1130 static int psy_register_cooler(struct power_supply *psy)
1131 {
1132 	return 0;
1133 }
1134 
1135 static void psy_unregister_cooler(struct power_supply *psy)
1136 {
1137 }
1138 #endif
1139 
1140 static struct power_supply *__must_check
1141 __power_supply_register(struct device *parent,
1142 				   const struct power_supply_desc *desc,
1143 				   const struct power_supply_config *cfg,
1144 				   bool ws)
1145 {
1146 	struct device *dev;
1147 	struct power_supply *psy;
1148 	int rc;
1149 
1150 	if (!parent)
1151 		pr_warn("%s: Expected proper parent device for '%s'\n",
1152 			__func__, desc->name);
1153 
1154 	if (!desc || !desc->name || !desc->properties || !desc->num_properties)
1155 		return ERR_PTR(-EINVAL);
1156 
1157 	if (psy_has_property(desc, POWER_SUPPLY_PROP_USB_TYPE) &&
1158 	    (!desc->usb_types || !desc->num_usb_types))
1159 		return ERR_PTR(-EINVAL);
1160 
1161 	psy = kzalloc(sizeof(*psy), GFP_KERNEL);
1162 	if (!psy)
1163 		return ERR_PTR(-ENOMEM);
1164 
1165 	dev = &psy->dev;
1166 
1167 	device_initialize(dev);
1168 
1169 	dev->class = power_supply_class;
1170 	dev->type = &power_supply_dev_type;
1171 	dev->parent = parent;
1172 	dev->release = power_supply_dev_release;
1173 	dev_set_drvdata(dev, psy);
1174 	psy->desc = desc;
1175 	if (cfg) {
1176 		dev->groups = cfg->attr_grp;
1177 		psy->drv_data = cfg->drv_data;
1178 		psy->of_node =
1179 			cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node;
1180 		psy->supplied_to = cfg->supplied_to;
1181 		psy->num_supplicants = cfg->num_supplicants;
1182 	}
1183 
1184 	rc = dev_set_name(dev, "%s", desc->name);
1185 	if (rc)
1186 		goto dev_set_name_failed;
1187 
1188 	INIT_WORK(&psy->changed_work, power_supply_changed_work);
1189 	INIT_DELAYED_WORK(&psy->deferred_register_work,
1190 			  power_supply_deferred_register_work);
1191 
1192 	rc = power_supply_check_supplies(psy);
1193 	if (rc) {
1194 		dev_dbg(dev, "Not all required supplies found, defer probe\n");
1195 		goto check_supplies_failed;
1196 	}
1197 
1198 	spin_lock_init(&psy->changed_lock);
1199 	rc = device_add(dev);
1200 	if (rc)
1201 		goto device_add_failed;
1202 
1203 	rc = device_init_wakeup(dev, ws);
1204 	if (rc)
1205 		goto wakeup_init_failed;
1206 
1207 	rc = psy_register_thermal(psy);
1208 	if (rc)
1209 		goto register_thermal_failed;
1210 
1211 	rc = psy_register_cooler(psy);
1212 	if (rc)
1213 		goto register_cooler_failed;
1214 
1215 	rc = power_supply_create_triggers(psy);
1216 	if (rc)
1217 		goto create_triggers_failed;
1218 
1219 	rc = power_supply_add_hwmon_sysfs(psy);
1220 	if (rc)
1221 		goto add_hwmon_sysfs_failed;
1222 
1223 	/*
1224 	 * Update use_cnt after any uevents (most notably from device_add()).
1225 	 * We are here still during driver's probe but
1226 	 * the power_supply_uevent() calls back driver's get_property
1227 	 * method so:
1228 	 * 1. Driver did not assigned the returned struct power_supply,
1229 	 * 2. Driver could not finish initialization (anything in its probe
1230 	 *    after calling power_supply_register()).
1231 	 */
1232 	atomic_inc(&psy->use_cnt);
1233 	psy->initialized = true;
1234 
1235 	queue_delayed_work(system_power_efficient_wq,
1236 			   &psy->deferred_register_work,
1237 			   POWER_SUPPLY_DEFERRED_REGISTER_TIME);
1238 
1239 	return psy;
1240 
1241 add_hwmon_sysfs_failed:
1242 	power_supply_remove_triggers(psy);
1243 create_triggers_failed:
1244 	psy_unregister_cooler(psy);
1245 register_cooler_failed:
1246 	psy_unregister_thermal(psy);
1247 register_thermal_failed:
1248 	device_del(dev);
1249 wakeup_init_failed:
1250 device_add_failed:
1251 check_supplies_failed:
1252 dev_set_name_failed:
1253 	put_device(dev);
1254 	return ERR_PTR(rc);
1255 }
1256 
1257 /**
1258  * power_supply_register() - Register new power supply
1259  * @parent:	Device to be a parent of power supply's device, usually
1260  *		the device which probe function calls this
1261  * @desc:	Description of power supply, must be valid through whole
1262  *		lifetime of this power supply
1263  * @cfg:	Run-time specific configuration accessed during registering,
1264  *		may be NULL
1265  *
1266  * Return: A pointer to newly allocated power_supply on success
1267  * or ERR_PTR otherwise.
1268  * Use power_supply_unregister() on returned power_supply pointer to release
1269  * resources.
1270  */
1271 struct power_supply *__must_check power_supply_register(struct device *parent,
1272 		const struct power_supply_desc *desc,
1273 		const struct power_supply_config *cfg)
1274 {
1275 	return __power_supply_register(parent, desc, cfg, true);
1276 }
1277 EXPORT_SYMBOL_GPL(power_supply_register);
1278 
1279 /**
1280  * power_supply_register_no_ws() - Register new non-waking-source power supply
1281  * @parent:	Device to be a parent of power supply's device, usually
1282  *		the device which probe function calls this
1283  * @desc:	Description of power supply, must be valid through whole
1284  *		lifetime of this power supply
1285  * @cfg:	Run-time specific configuration accessed during registering,
1286  *		may be NULL
1287  *
1288  * Return: A pointer to newly allocated power_supply on success
1289  * or ERR_PTR otherwise.
1290  * Use power_supply_unregister() on returned power_supply pointer to release
1291  * resources.
1292  */
1293 struct power_supply *__must_check
1294 power_supply_register_no_ws(struct device *parent,
1295 		const struct power_supply_desc *desc,
1296 		const struct power_supply_config *cfg)
1297 {
1298 	return __power_supply_register(parent, desc, cfg, false);
1299 }
1300 EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
1301 
1302 static void devm_power_supply_release(struct device *dev, void *res)
1303 {
1304 	struct power_supply **psy = res;
1305 
1306 	power_supply_unregister(*psy);
1307 }
1308 
1309 /**
1310  * devm_power_supply_register() - Register managed power supply
1311  * @parent:	Device to be a parent of power supply's device, usually
1312  *		the device which probe function calls this
1313  * @desc:	Description of power supply, must be valid through whole
1314  *		lifetime of this power supply
1315  * @cfg:	Run-time specific configuration accessed during registering,
1316  *		may be NULL
1317  *
1318  * Return: A pointer to newly allocated power_supply on success
1319  * or ERR_PTR otherwise.
1320  * The returned power_supply pointer will be automatically unregistered
1321  * on driver detach.
1322  */
1323 struct power_supply *__must_check
1324 devm_power_supply_register(struct device *parent,
1325 		const struct power_supply_desc *desc,
1326 		const struct power_supply_config *cfg)
1327 {
1328 	struct power_supply **ptr, *psy;
1329 
1330 	ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1331 
1332 	if (!ptr)
1333 		return ERR_PTR(-ENOMEM);
1334 	psy = __power_supply_register(parent, desc, cfg, true);
1335 	if (IS_ERR(psy)) {
1336 		devres_free(ptr);
1337 	} else {
1338 		*ptr = psy;
1339 		devres_add(parent, ptr);
1340 	}
1341 	return psy;
1342 }
1343 EXPORT_SYMBOL_GPL(devm_power_supply_register);
1344 
1345 /**
1346  * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
1347  * @parent:	Device to be a parent of power supply's device, usually
1348  *		the device which probe function calls this
1349  * @desc:	Description of power supply, must be valid through whole
1350  *		lifetime of this power supply
1351  * @cfg:	Run-time specific configuration accessed during registering,
1352  *		may be NULL
1353  *
1354  * Return: A pointer to newly allocated power_supply on success
1355  * or ERR_PTR otherwise.
1356  * The returned power_supply pointer will be automatically unregistered
1357  * on driver detach.
1358  */
1359 struct power_supply *__must_check
1360 devm_power_supply_register_no_ws(struct device *parent,
1361 		const struct power_supply_desc *desc,
1362 		const struct power_supply_config *cfg)
1363 {
1364 	struct power_supply **ptr, *psy;
1365 
1366 	ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1367 
1368 	if (!ptr)
1369 		return ERR_PTR(-ENOMEM);
1370 	psy = __power_supply_register(parent, desc, cfg, false);
1371 	if (IS_ERR(psy)) {
1372 		devres_free(ptr);
1373 	} else {
1374 		*ptr = psy;
1375 		devres_add(parent, ptr);
1376 	}
1377 	return psy;
1378 }
1379 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
1380 
1381 /**
1382  * power_supply_unregister() - Remove this power supply from system
1383  * @psy:	Pointer to power supply to unregister
1384  *
1385  * Remove this power supply from the system. The resources of power supply
1386  * will be freed here or on last power_supply_put() call.
1387  */
1388 void power_supply_unregister(struct power_supply *psy)
1389 {
1390 	WARN_ON(atomic_dec_return(&psy->use_cnt));
1391 	psy->removing = true;
1392 	cancel_work_sync(&psy->changed_work);
1393 	cancel_delayed_work_sync(&psy->deferred_register_work);
1394 	sysfs_remove_link(&psy->dev.kobj, "powers");
1395 	power_supply_remove_hwmon_sysfs(psy);
1396 	power_supply_remove_triggers(psy);
1397 	psy_unregister_cooler(psy);
1398 	psy_unregister_thermal(psy);
1399 	device_init_wakeup(&psy->dev, false);
1400 	device_unregister(&psy->dev);
1401 }
1402 EXPORT_SYMBOL_GPL(power_supply_unregister);
1403 
1404 void *power_supply_get_drvdata(struct power_supply *psy)
1405 {
1406 	return psy->drv_data;
1407 }
1408 EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
1409 
1410 static int __init power_supply_class_init(void)
1411 {
1412 	power_supply_class = class_create(THIS_MODULE, "power_supply");
1413 
1414 	if (IS_ERR(power_supply_class))
1415 		return PTR_ERR(power_supply_class);
1416 
1417 	power_supply_class->dev_uevent = power_supply_uevent;
1418 	power_supply_init_attrs(&power_supply_dev_type);
1419 
1420 	return 0;
1421 }
1422 
1423 static void __exit power_supply_class_exit(void)
1424 {
1425 	class_destroy(power_supply_class);
1426 }
1427 
1428 subsys_initcall(power_supply_class_init);
1429 module_exit(power_supply_class_exit);
1430 
1431 MODULE_DESCRIPTION("Universal power supply monitor class");
1432 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
1433 	      "Szabolcs Gyurko, "
1434 	      "Anton Vorontsov <cbou@mail.ru>");
1435 MODULE_LICENSE("GPL");
1436