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