xref: /OK3568_Linux_fs/kernel/drivers/power/supply/power_supply_core.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 	(*count)++;
362 	if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
363 		if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
364 					&ret))
365 			return ret.intval;
366 
367 	return 0;
368 }
369 
power_supply_is_system_supplied(void)370 int power_supply_is_system_supplied(void)
371 {
372 	int error;
373 	unsigned int count = 0;
374 
375 	error = class_for_each_device(power_supply_class, NULL, &count,
376 				      __power_supply_is_system_supplied);
377 
378 	/*
379 	 * If no power class device was found at all, most probably we are
380 	 * running on a desktop system, so assume we are on mains power.
381 	 */
382 	if (count == 0)
383 		return 1;
384 
385 	return error;
386 }
387 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
388 
__power_supply_get_supplier_max_current(struct device * dev,void * data)389 static int __power_supply_get_supplier_max_current(struct device *dev,
390 						   void *data)
391 {
392 	union power_supply_propval ret = {0,};
393 	struct power_supply *epsy = dev_get_drvdata(dev);
394 	struct power_supply *psy = data;
395 
396 	if (__power_supply_is_supplied_by(epsy, psy))
397 		if (!epsy->desc->get_property(epsy,
398 					      POWER_SUPPLY_PROP_CURRENT_MAX,
399 					      &ret))
400 			return ret.intval;
401 
402 	return 0;
403 }
404 
power_supply_set_input_current_limit_from_supplier(struct power_supply * psy)405 int power_supply_set_input_current_limit_from_supplier(struct power_supply *psy)
406 {
407 	union power_supply_propval val = {0,};
408 	int curr;
409 
410 	if (!psy->desc->set_property)
411 		return -EINVAL;
412 
413 	/*
414 	 * This function is not intended for use with a supply with multiple
415 	 * suppliers, we simply pick the first supply to report a non 0
416 	 * max-current.
417 	 */
418 	curr = class_for_each_device(power_supply_class, NULL, psy,
419 				      __power_supply_get_supplier_max_current);
420 	if (curr <= 0)
421 		return (curr == 0) ? -ENODEV : curr;
422 
423 	val.intval = curr;
424 
425 	return psy->desc->set_property(psy,
426 				POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
427 }
428 EXPORT_SYMBOL_GPL(power_supply_set_input_current_limit_from_supplier);
429 
power_supply_set_battery_charged(struct power_supply * psy)430 int power_supply_set_battery_charged(struct power_supply *psy)
431 {
432 	if (atomic_read(&psy->use_cnt) >= 0 &&
433 			psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
434 			psy->desc->set_charged) {
435 		psy->desc->set_charged(psy);
436 		return 0;
437 	}
438 
439 	return -EINVAL;
440 }
441 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
442 
power_supply_match_device_by_name(struct device * dev,const void * data)443 static int power_supply_match_device_by_name(struct device *dev, const void *data)
444 {
445 	const char *name = data;
446 	struct power_supply *psy = dev_get_drvdata(dev);
447 
448 	return strcmp(psy->desc->name, name) == 0;
449 }
450 
451 /**
452  * power_supply_get_by_name() - Search for a power supply and returns its ref
453  * @name: Power supply name to fetch
454  *
455  * If power supply was found, it increases reference count for the
456  * internal power supply's device. The user should power_supply_put()
457  * after usage.
458  *
459  * Return: On success returns a reference to a power supply with
460  * matching name equals to @name, a NULL otherwise.
461  */
power_supply_get_by_name(const char * name)462 struct power_supply *power_supply_get_by_name(const char *name)
463 {
464 	struct power_supply *psy = NULL;
465 	struct device *dev = class_find_device(power_supply_class, NULL, name,
466 					power_supply_match_device_by_name);
467 
468 	if (dev) {
469 		psy = dev_get_drvdata(dev);
470 		atomic_inc(&psy->use_cnt);
471 	}
472 
473 	return psy;
474 }
475 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
476 
477 /**
478  * power_supply_put() - Drop reference obtained with power_supply_get_by_name
479  * @psy: Reference to put
480  *
481  * The reference to power supply should be put before unregistering
482  * the power supply.
483  */
power_supply_put(struct power_supply * psy)484 void power_supply_put(struct power_supply *psy)
485 {
486 	might_sleep();
487 
488 	atomic_dec(&psy->use_cnt);
489 	put_device(&psy->dev);
490 }
491 EXPORT_SYMBOL_GPL(power_supply_put);
492 
493 #ifdef CONFIG_OF
power_supply_match_device_node(struct device * dev,const void * data)494 static int power_supply_match_device_node(struct device *dev, const void *data)
495 {
496 	return dev->parent && dev->parent->of_node == data;
497 }
498 
499 /**
500  * power_supply_get_by_phandle() - Search for a power supply and returns its ref
501  * @np: Pointer to device node holding phandle property
502  * @property: Name of property holding a power supply name
503  *
504  * If power supply was found, it increases reference count for the
505  * internal power supply's device. The user should power_supply_put()
506  * after usage.
507  *
508  * Return: On success returns a reference to a power supply with
509  * matching name equals to value under @property, NULL or ERR_PTR otherwise.
510  */
power_supply_get_by_phandle(struct device_node * np,const char * property)511 struct power_supply *power_supply_get_by_phandle(struct device_node *np,
512 							const char *property)
513 {
514 	struct device_node *power_supply_np;
515 	struct power_supply *psy = NULL;
516 	struct device *dev;
517 
518 	power_supply_np = of_parse_phandle(np, property, 0);
519 	if (!power_supply_np)
520 		return ERR_PTR(-ENODEV);
521 
522 	dev = class_find_device(power_supply_class, NULL, power_supply_np,
523 						power_supply_match_device_node);
524 
525 	of_node_put(power_supply_np);
526 
527 	if (dev) {
528 		psy = dev_get_drvdata(dev);
529 		atomic_inc(&psy->use_cnt);
530 	}
531 
532 	return psy;
533 }
534 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
535 
power_supply_match_device_node_array(struct device * dev,void * data)536 static int power_supply_match_device_node_array(struct device *dev,
537 						void *data)
538 {
539 	struct match_device_node_array_param *param =
540 		(struct match_device_node_array_param *)data;
541 	struct power_supply **psy = param->psy;
542 	ssize_t size = param->psy_size;
543 	ssize_t *count = &param->psy_count;
544 
545 	if (!dev->parent || dev->parent->of_node != param->parent_of_node)
546 		return 0;
547 
548 	if (*count >= size)
549 		return -EOVERFLOW;
550 
551 	psy[*count] = dev_get_drvdata(dev);
552 	atomic_inc(&psy[*count]->use_cnt);
553 	(*count)++;
554 
555 	return 0;
556 }
557 
558 /**
559  * power_supply_get_by_phandle_array() - Similar to
560  * power_supply_get_by_phandle but returns an array of power supply
561  * objects which are associated with the phandle.
562  * @np: Pointer to device node holding phandle property.
563  * @property: Name of property holding a power supply name.
564  * @psy: Array of power_supply pointers provided by the client which is
565  * filled by power_supply_get_by_phandle_array.
566  * @size: size of power_supply pointer array.
567  *
568  * If power supply was found, it increases reference count for the
569  * internal power supply's device. The user should power_supply_put()
570  * after usage.
571  *
572  * Return: On success returns the number of power supply objects filled
573  * in the @psy array.
574  * -EOVERFLOW when size of @psy array is not suffice.
575  * -EINVAL when @psy is NULL or @size is 0.
576  * -ENODEV when matching device_node is not found.
577  */
power_supply_get_by_phandle_array(struct device_node * np,const char * property,struct power_supply ** psy,ssize_t size)578 int power_supply_get_by_phandle_array(struct device_node *np,
579 				      const char *property,
580 				      struct power_supply **psy,
581 				      ssize_t size)
582 {
583 	struct device_node *power_supply_np;
584 	int ret;
585 	struct match_device_node_array_param param;
586 
587 	if (!psy || !size)
588 		return -EINVAL;
589 
590 	power_supply_np = of_parse_phandle(np, property, 0);
591 	if (!power_supply_np)
592 		return -ENODEV;
593 
594 	param.parent_of_node = power_supply_np;
595 	param.psy = psy;
596 	param.psy_size = size;
597 	param.psy_count = 0;
598 	ret = class_for_each_device(power_supply_class, NULL, &param,
599 				    power_supply_match_device_node_array);
600 
601 	of_node_put(power_supply_np);
602 
603 	return param.psy_count;
604 }
605 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle_array);
606 
devm_power_supply_put(struct device * dev,void * res)607 static void devm_power_supply_put(struct device *dev, void *res)
608 {
609 	struct power_supply **psy = res;
610 
611 	power_supply_put(*psy);
612 }
613 
614 /**
615  * devm_power_supply_get_by_phandle() - Resource managed version of
616  *  power_supply_get_by_phandle()
617  * @dev: Pointer to device holding phandle property
618  * @property: Name of property holding a power supply phandle
619  *
620  * Return: On success returns a reference to a power supply with
621  * matching name equals to value under @property, NULL or ERR_PTR otherwise.
622  */
devm_power_supply_get_by_phandle(struct device * dev,const char * property)623 struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
624 						      const char *property)
625 {
626 	struct power_supply **ptr, *psy;
627 
628 	if (!dev->of_node)
629 		return ERR_PTR(-ENODEV);
630 
631 	ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
632 	if (!ptr)
633 		return ERR_PTR(-ENOMEM);
634 
635 	psy = power_supply_get_by_phandle(dev->of_node, property);
636 	if (IS_ERR_OR_NULL(psy)) {
637 		devres_free(ptr);
638 	} else {
639 		*ptr = psy;
640 		devres_add(dev, ptr);
641 	}
642 	return psy;
643 }
644 EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
645 #endif /* CONFIG_OF */
646 
power_supply_get_battery_info(struct power_supply * psy,struct power_supply_battery_info * info)647 int power_supply_get_battery_info(struct power_supply *psy,
648 				  struct power_supply_battery_info *info)
649 {
650 	struct power_supply_resistance_temp_table *resist_table;
651 	struct device_node *battery_np;
652 	const char *value;
653 	int err, len, index;
654 	const __be32 *list;
655 
656 	info->energy_full_design_uwh         = -EINVAL;
657 	info->charge_full_design_uah         = -EINVAL;
658 	info->voltage_min_design_uv          = -EINVAL;
659 	info->voltage_max_design_uv          = -EINVAL;
660 	info->precharge_current_ua           = -EINVAL;
661 	info->charge_term_current_ua         = -EINVAL;
662 	info->constant_charge_current_max_ua = -EINVAL;
663 	info->constant_charge_voltage_max_uv = -EINVAL;
664 	info->temp_ambient_alert_min         = INT_MIN;
665 	info->temp_ambient_alert_max         = INT_MAX;
666 	info->temp_alert_min                 = INT_MIN;
667 	info->temp_alert_max                 = INT_MAX;
668 	info->temp_min                       = INT_MIN;
669 	info->temp_max                       = INT_MAX;
670 	info->factory_internal_resistance_uohm  = -EINVAL;
671 	info->resist_table = NULL;
672 
673 	for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) {
674 		info->ocv_table[index]       = NULL;
675 		info->ocv_temp[index]        = -EINVAL;
676 		info->ocv_table_size[index]  = -EINVAL;
677 	}
678 
679 	if (!psy->of_node) {
680 		dev_warn(&psy->dev, "%s currently only supports devicetree\n",
681 			 __func__);
682 		return -ENXIO;
683 	}
684 
685 	battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);
686 	if (!battery_np)
687 		return -ENODEV;
688 
689 	err = of_property_read_string(battery_np, "compatible", &value);
690 	if (err)
691 		goto out_put_node;
692 
693 	if (strcmp("simple-battery", value)) {
694 		err = -ENODEV;
695 		goto out_put_node;
696 	}
697 
698 	/* The property and field names below must correspond to elements
699 	 * in enum power_supply_property. For reasoning, see
700 	 * Documentation/power/power_supply_class.rst.
701 	 */
702 
703 	of_property_read_u32(battery_np, "energy-full-design-microwatt-hours",
704 			     &info->energy_full_design_uwh);
705 	of_property_read_u32(battery_np, "charge-full-design-microamp-hours",
706 			     &info->charge_full_design_uah);
707 	of_property_read_u32(battery_np, "voltage-min-design-microvolt",
708 			     &info->voltage_min_design_uv);
709 	of_property_read_u32(battery_np, "voltage-max-design-microvolt",
710 			     &info->voltage_max_design_uv);
711 	of_property_read_u32(battery_np, "trickle-charge-current-microamp",
712 			     &info->tricklecharge_current_ua);
713 	of_property_read_u32(battery_np, "precharge-current-microamp",
714 			     &info->precharge_current_ua);
715 	of_property_read_u32(battery_np, "precharge-upper-limit-microvolt",
716 			     &info->precharge_voltage_max_uv);
717 	of_property_read_u32(battery_np, "charge-term-current-microamp",
718 			     &info->charge_term_current_ua);
719 	of_property_read_u32(battery_np, "re-charge-voltage-microvolt",
720 			     &info->charge_restart_voltage_uv);
721 	of_property_read_u32(battery_np, "over-voltage-threshold-microvolt",
722 			     &info->overvoltage_limit_uv);
723 	of_property_read_u32(battery_np, "constant-charge-current-max-microamp",
724 			     &info->constant_charge_current_max_ua);
725 	of_property_read_u32(battery_np, "constant-charge-voltage-max-microvolt",
726 			     &info->constant_charge_voltage_max_uv);
727 	of_property_read_u32(battery_np, "factory-internal-resistance-micro-ohms",
728 			     &info->factory_internal_resistance_uohm);
729 
730 	of_property_read_u32_index(battery_np, "ambient-celsius",
731 				   0, &info->temp_ambient_alert_min);
732 	of_property_read_u32_index(battery_np, "ambient-celsius",
733 				   1, &info->temp_ambient_alert_max);
734 	of_property_read_u32_index(battery_np, "alert-celsius",
735 				   0, &info->temp_alert_min);
736 	of_property_read_u32_index(battery_np, "alert-celsius",
737 				   1, &info->temp_alert_max);
738 	of_property_read_u32_index(battery_np, "operating-range-celsius",
739 				   0, &info->temp_min);
740 	of_property_read_u32_index(battery_np, "operating-range-celsius",
741 				   1, &info->temp_max);
742 
743 	len = of_property_count_u32_elems(battery_np, "ocv-capacity-celsius");
744 	if (len < 0 && len != -EINVAL) {
745 		err = len;
746 		goto out_put_node;
747 	} else if (len > POWER_SUPPLY_OCV_TEMP_MAX) {
748 		dev_err(&psy->dev, "Too many temperature values\n");
749 		err = -EINVAL;
750 		goto out_put_node;
751 	} else if (len > 0) {
752 		of_property_read_u32_array(battery_np, "ocv-capacity-celsius",
753 					   info->ocv_temp, len);
754 	}
755 
756 	for (index = 0; index < len; index++) {
757 		struct power_supply_battery_ocv_table *table;
758 		char *propname;
759 		int i, tab_len, size;
760 
761 		propname = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d", index);
762 		list = of_get_property(battery_np, propname, &size);
763 		if (!list || !size) {
764 			dev_err(&psy->dev, "failed to get %s\n", propname);
765 			kfree(propname);
766 			power_supply_put_battery_info(psy, info);
767 			err = -EINVAL;
768 			goto out_put_node;
769 		}
770 
771 		kfree(propname);
772 		tab_len = size / (2 * sizeof(__be32));
773 		info->ocv_table_size[index] = tab_len;
774 
775 		table = info->ocv_table[index] =
776 			devm_kcalloc(&psy->dev, tab_len, sizeof(*table), GFP_KERNEL);
777 		if (!info->ocv_table[index]) {
778 			power_supply_put_battery_info(psy, info);
779 			err = -ENOMEM;
780 			goto out_put_node;
781 		}
782 
783 		for (i = 0; i < tab_len; i++) {
784 			table[i].ocv = be32_to_cpu(*list);
785 			list++;
786 			table[i].capacity = be32_to_cpu(*list);
787 			list++;
788 		}
789 	}
790 
791 	list = of_get_property(battery_np, "resistance-temp-table", &len);
792 	if (!list || !len)
793 		goto out_put_node;
794 
795 	info->resist_table_size = len / (2 * sizeof(__be32));
796 	resist_table = info->resist_table = devm_kcalloc(&psy->dev,
797 							 info->resist_table_size,
798 							 sizeof(*resist_table),
799 							 GFP_KERNEL);
800 	if (!info->resist_table) {
801 		power_supply_put_battery_info(psy, info);
802 		err = -ENOMEM;
803 		goto out_put_node;
804 	}
805 
806 	for (index = 0; index < info->resist_table_size; index++) {
807 		resist_table[index].temp = be32_to_cpu(*list++);
808 		resist_table[index].resistance = be32_to_cpu(*list++);
809 	}
810 
811 out_put_node:
812 	of_node_put(battery_np);
813 	return err;
814 }
815 EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
816 
power_supply_put_battery_info(struct power_supply * psy,struct power_supply_battery_info * info)817 void power_supply_put_battery_info(struct power_supply *psy,
818 				   struct power_supply_battery_info *info)
819 {
820 	int i;
821 
822 	for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
823 		if (info->ocv_table[i])
824 			devm_kfree(&psy->dev, info->ocv_table[i]);
825 	}
826 
827 	if (info->resist_table)
828 		devm_kfree(&psy->dev, info->resist_table);
829 }
830 EXPORT_SYMBOL_GPL(power_supply_put_battery_info);
831 
832 /**
833  * power_supply_temp2resist_simple() - find the battery internal resistance
834  * percent
835  * @table: Pointer to battery resistance temperature table
836  * @table_len: The table length
837  * @temp: Current temperature
838  *
839  * This helper function is used to look up battery internal resistance percent
840  * according to current temperature value from the resistance temperature table,
841  * and the table must be ordered descending. Then the actual battery internal
842  * resistance = the ideal battery internal resistance * percent / 100.
843  *
844  * Return: the battery internal resistance percent
845  */
power_supply_temp2resist_simple(struct power_supply_resistance_temp_table * table,int table_len,int temp)846 int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table,
847 				    int table_len, int temp)
848 {
849 	int i, resist;
850 
851 	for (i = 0; i < table_len; i++)
852 		if (temp > table[i].temp)
853 			break;
854 
855 	if (i > 0 && i < table_len) {
856 		int tmp;
857 
858 		tmp = (table[i - 1].resistance - table[i].resistance) *
859 			(temp - table[i].temp);
860 		tmp /= table[i - 1].temp - table[i].temp;
861 		resist = tmp + table[i].resistance;
862 	} else if (i == 0) {
863 		resist = table[0].resistance;
864 	} else {
865 		resist = table[table_len - 1].resistance;
866 	}
867 
868 	return resist;
869 }
870 EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple);
871 
872 /**
873  * power_supply_ocv2cap_simple() - find the battery capacity
874  * @table: Pointer to battery OCV lookup table
875  * @table_len: OCV table length
876  * @ocv: Current OCV value
877  *
878  * This helper function is used to look up battery capacity according to
879  * current OCV value from one OCV table, and the OCV table must be ordered
880  * descending.
881  *
882  * Return: the battery capacity.
883  */
power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table * table,int table_len,int ocv)884 int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table,
885 				int table_len, int ocv)
886 {
887 	int i, cap, tmp;
888 
889 	for (i = 0; i < table_len; i++)
890 		if (ocv > table[i].ocv)
891 			break;
892 
893 	if (i > 0 && i < table_len) {
894 		tmp = (table[i - 1].capacity - table[i].capacity) *
895 			(ocv - table[i].ocv);
896 		tmp /= table[i - 1].ocv - table[i].ocv;
897 		cap = tmp + table[i].capacity;
898 	} else if (i == 0) {
899 		cap = table[0].capacity;
900 	} else {
901 		cap = table[table_len - 1].capacity;
902 	}
903 
904 	return cap;
905 }
906 EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple);
907 
908 struct power_supply_battery_ocv_table *
power_supply_find_ocv2cap_table(struct power_supply_battery_info * info,int temp,int * table_len)909 power_supply_find_ocv2cap_table(struct power_supply_battery_info *info,
910 				int temp, int *table_len)
911 {
912 	int best_temp_diff = INT_MAX, temp_diff;
913 	u8 i, best_index = 0;
914 
915 	if (!info->ocv_table[0])
916 		return NULL;
917 
918 	for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
919 		/* Out of capacity tables */
920 		if (!info->ocv_table[i])
921 			break;
922 
923 		temp_diff = abs(info->ocv_temp[i] - temp);
924 
925 		if (temp_diff < best_temp_diff) {
926 			best_temp_diff = temp_diff;
927 			best_index = i;
928 		}
929 	}
930 
931 	*table_len = info->ocv_table_size[best_index];
932 	return info->ocv_table[best_index];
933 }
934 EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table);
935 
power_supply_batinfo_ocv2cap(struct power_supply_battery_info * info,int ocv,int temp)936 int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info,
937 				 int ocv, int temp)
938 {
939 	struct power_supply_battery_ocv_table *table;
940 	int table_len;
941 
942 	table = power_supply_find_ocv2cap_table(info, temp, &table_len);
943 	if (!table)
944 		return -EINVAL;
945 
946 	return power_supply_ocv2cap_simple(table, table_len, ocv);
947 }
948 EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap);
949 
power_supply_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)950 int power_supply_get_property(struct power_supply *psy,
951 			    enum power_supply_property psp,
952 			    union power_supply_propval *val)
953 {
954 	if (atomic_read(&psy->use_cnt) <= 0) {
955 		if (!psy->initialized)
956 			return -EAGAIN;
957 		return -ENODEV;
958 	}
959 
960 	return psy->desc->get_property(psy, psp, val);
961 }
962 EXPORT_SYMBOL_GPL(power_supply_get_property);
963 
power_supply_set_property(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)964 int power_supply_set_property(struct power_supply *psy,
965 			    enum power_supply_property psp,
966 			    const union power_supply_propval *val)
967 {
968 	if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
969 		return -ENODEV;
970 
971 	return psy->desc->set_property(psy, psp, val);
972 }
973 EXPORT_SYMBOL_GPL(power_supply_set_property);
974 
power_supply_property_is_writeable(struct power_supply * psy,enum power_supply_property psp)975 int power_supply_property_is_writeable(struct power_supply *psy,
976 					enum power_supply_property psp)
977 {
978 	if (atomic_read(&psy->use_cnt) <= 0 ||
979 			!psy->desc->property_is_writeable)
980 		return -ENODEV;
981 
982 	return psy->desc->property_is_writeable(psy, psp);
983 }
984 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
985 
power_supply_external_power_changed(struct power_supply * psy)986 void power_supply_external_power_changed(struct power_supply *psy)
987 {
988 	if (atomic_read(&psy->use_cnt) <= 0 ||
989 			!psy->desc->external_power_changed)
990 		return;
991 
992 	psy->desc->external_power_changed(psy);
993 }
994 EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
995 
power_supply_powers(struct power_supply * psy,struct device * dev)996 int power_supply_powers(struct power_supply *psy, struct device *dev)
997 {
998 	return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
999 }
1000 EXPORT_SYMBOL_GPL(power_supply_powers);
1001 
power_supply_dev_release(struct device * dev)1002 static void power_supply_dev_release(struct device *dev)
1003 {
1004 	struct power_supply *psy = to_power_supply(dev);
1005 	dev_dbg(dev, "%s\n", __func__);
1006 	kfree(psy);
1007 }
1008 
power_supply_reg_notifier(struct notifier_block * nb)1009 int power_supply_reg_notifier(struct notifier_block *nb)
1010 {
1011 	return atomic_notifier_chain_register(&power_supply_notifier, nb);
1012 }
1013 EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
1014 
power_supply_unreg_notifier(struct notifier_block * nb)1015 void power_supply_unreg_notifier(struct notifier_block *nb)
1016 {
1017 	atomic_notifier_chain_unregister(&power_supply_notifier, nb);
1018 }
1019 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
1020 
1021 #ifdef CONFIG_THERMAL
power_supply_read_temp(struct thermal_zone_device * tzd,int * temp)1022 static int power_supply_read_temp(struct thermal_zone_device *tzd,
1023 		int *temp)
1024 {
1025 	struct power_supply *psy;
1026 	union power_supply_propval val;
1027 	int ret;
1028 
1029 	WARN_ON(tzd == NULL);
1030 	psy = tzd->devdata;
1031 	ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
1032 	if (ret)
1033 		return ret;
1034 
1035 	/* Convert tenths of degree Celsius to milli degree Celsius. */
1036 	*temp = val.intval * 100;
1037 
1038 	return ret;
1039 }
1040 
1041 static struct thermal_zone_device_ops psy_tzd_ops = {
1042 	.get_temp = power_supply_read_temp,
1043 };
1044 
psy_register_thermal(struct power_supply * psy)1045 static int psy_register_thermal(struct power_supply *psy)
1046 {
1047 	int i, ret;
1048 
1049 	if (psy->desc->no_thermal)
1050 		return 0;
1051 
1052 	/* Register battery zone device psy reports temperature */
1053 	for (i = 0; i < psy->desc->num_properties; i++) {
1054 		if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
1055 			psy->tzd = thermal_zone_device_register(psy->desc->name,
1056 					0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
1057 			if (IS_ERR(psy->tzd))
1058 				return PTR_ERR(psy->tzd);
1059 			ret = thermal_zone_device_enable(psy->tzd);
1060 			if (ret)
1061 				thermal_zone_device_unregister(psy->tzd);
1062 			return ret;
1063 		}
1064 	}
1065 	return 0;
1066 }
1067 
psy_unregister_thermal(struct power_supply * psy)1068 static void psy_unregister_thermal(struct power_supply *psy)
1069 {
1070 	if (IS_ERR_OR_NULL(psy->tzd))
1071 		return;
1072 	thermal_zone_device_unregister(psy->tzd);
1073 }
1074 
1075 /* thermal cooling device callbacks */
ps_get_max_charge_cntl_limit(struct thermal_cooling_device * tcd,unsigned long * state)1076 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
1077 					unsigned long *state)
1078 {
1079 	struct power_supply *psy;
1080 	union power_supply_propval val;
1081 	int ret;
1082 
1083 	psy = tcd->devdata;
1084 	ret = power_supply_get_property(psy,
1085 			POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
1086 	if (ret)
1087 		return ret;
1088 
1089 	*state = val.intval;
1090 
1091 	return ret;
1092 }
1093 
ps_get_cur_charge_cntl_limit(struct thermal_cooling_device * tcd,unsigned long * state)1094 static int ps_get_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
1095 					unsigned long *state)
1096 {
1097 	struct power_supply *psy;
1098 	union power_supply_propval val;
1099 	int ret;
1100 
1101 	psy = tcd->devdata;
1102 	ret = power_supply_get_property(psy,
1103 			POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
1104 	if (ret)
1105 		return ret;
1106 
1107 	*state = val.intval;
1108 
1109 	return ret;
1110 }
1111 
ps_set_cur_charge_cntl_limit(struct thermal_cooling_device * tcd,unsigned long state)1112 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
1113 					unsigned long state)
1114 {
1115 	struct power_supply *psy;
1116 	union power_supply_propval val;
1117 	int ret;
1118 
1119 	psy = tcd->devdata;
1120 	val.intval = state;
1121 	ret = psy->desc->set_property(psy,
1122 		POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
1123 
1124 	return ret;
1125 }
1126 
1127 static const struct thermal_cooling_device_ops psy_tcd_ops = {
1128 	.get_max_state = ps_get_max_charge_cntl_limit,
1129 	.get_cur_state = ps_get_cur_charge_cntl_limit,
1130 	.set_cur_state = ps_set_cur_charge_cntl_limit,
1131 };
1132 
psy_register_cooler(struct power_supply * psy)1133 static int psy_register_cooler(struct power_supply *psy)
1134 {
1135 	int i;
1136 
1137 	/* Register for cooling device if psy can control charging */
1138 	for (i = 0; i < psy->desc->num_properties; i++) {
1139 		if (psy->desc->properties[i] ==
1140 				POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
1141 			if (psy->dev.parent)
1142 				psy->tcd = thermal_of_cooling_device_register(
1143 						dev_of_node(psy->dev.parent),
1144 						(char *)psy->desc->name,
1145 						psy, &psy_tcd_ops);
1146 			else
1147 				psy->tcd = thermal_cooling_device_register(
1148 						(char *)psy->desc->name,
1149 						psy, &psy_tcd_ops);
1150 			return PTR_ERR_OR_ZERO(psy->tcd);
1151 		}
1152 	}
1153 	return 0;
1154 }
1155 
psy_unregister_cooler(struct power_supply * psy)1156 static void psy_unregister_cooler(struct power_supply *psy)
1157 {
1158 	if (IS_ERR_OR_NULL(psy->tcd))
1159 		return;
1160 	thermal_cooling_device_unregister(psy->tcd);
1161 }
1162 #else
psy_register_thermal(struct power_supply * psy)1163 static int psy_register_thermal(struct power_supply *psy)
1164 {
1165 	return 0;
1166 }
1167 
psy_unregister_thermal(struct power_supply * psy)1168 static void psy_unregister_thermal(struct power_supply *psy)
1169 {
1170 }
1171 
psy_register_cooler(struct power_supply * psy)1172 static int psy_register_cooler(struct power_supply *psy)
1173 {
1174 	return 0;
1175 }
1176 
psy_unregister_cooler(struct power_supply * psy)1177 static void psy_unregister_cooler(struct power_supply *psy)
1178 {
1179 }
1180 #endif
1181 
1182 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)1183 __power_supply_register(struct device *parent,
1184 				   const struct power_supply_desc *desc,
1185 				   const struct power_supply_config *cfg,
1186 				   bool ws)
1187 {
1188 	struct device *dev;
1189 	struct power_supply *psy;
1190 	int i, rc;
1191 
1192 	if (!parent)
1193 		pr_warn("%s: Expected proper parent device for '%s'\n",
1194 			__func__, desc->name);
1195 
1196 	if (!desc || !desc->name || !desc->properties || !desc->num_properties)
1197 		return ERR_PTR(-EINVAL);
1198 
1199 	for (i = 0; i < desc->num_properties; ++i) {
1200 		if ((desc->properties[i] == POWER_SUPPLY_PROP_USB_TYPE) &&
1201 		    (!desc->usb_types || !desc->num_usb_types))
1202 			return ERR_PTR(-EINVAL);
1203 	}
1204 
1205 	psy = kzalloc(sizeof(*psy), GFP_KERNEL);
1206 	if (!psy)
1207 		return ERR_PTR(-ENOMEM);
1208 
1209 	dev = &psy->dev;
1210 
1211 	device_initialize(dev);
1212 
1213 	dev->class = power_supply_class;
1214 	dev->type = &power_supply_dev_type;
1215 	dev->parent = parent;
1216 	dev->release = power_supply_dev_release;
1217 	dev_set_drvdata(dev, psy);
1218 	psy->desc = desc;
1219 	if (cfg) {
1220 		dev->groups = cfg->attr_grp;
1221 		psy->drv_data = cfg->drv_data;
1222 		psy->of_node =
1223 			cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node;
1224 		psy->supplied_to = cfg->supplied_to;
1225 		psy->num_supplicants = cfg->num_supplicants;
1226 	}
1227 
1228 	rc = dev_set_name(dev, "%s", desc->name);
1229 	if (rc)
1230 		goto dev_set_name_failed;
1231 
1232 	INIT_WORK(&psy->changed_work, power_supply_changed_work);
1233 	INIT_DELAYED_WORK(&psy->deferred_register_work,
1234 			  power_supply_deferred_register_work);
1235 
1236 	rc = power_supply_check_supplies(psy);
1237 	if (rc) {
1238 		dev_info(dev, "Not all required supplies found, defer probe\n");
1239 		goto check_supplies_failed;
1240 	}
1241 
1242 	spin_lock_init(&psy->changed_lock);
1243 	rc = device_add(dev);
1244 	if (rc)
1245 		goto device_add_failed;
1246 
1247 	rc = device_init_wakeup(dev, ws);
1248 	if (rc)
1249 		goto wakeup_init_failed;
1250 
1251 	rc = psy_register_thermal(psy);
1252 	if (rc)
1253 		goto register_thermal_failed;
1254 
1255 	rc = power_supply_create_triggers(psy);
1256 	if (rc)
1257 		goto create_triggers_failed;
1258 
1259 	rc = power_supply_add_hwmon_sysfs(psy);
1260 	if (rc)
1261 		goto add_hwmon_sysfs_failed;
1262 
1263 	/*
1264 	 * Update use_cnt after any uevents (most notably from device_add()).
1265 	 * We are here still during driver's probe but
1266 	 * the power_supply_uevent() calls back driver's get_property
1267 	 * method so:
1268 	 * 1. Driver did not assigned the returned struct power_supply,
1269 	 * 2. Driver could not finish initialization (anything in its probe
1270 	 *    after calling power_supply_register()).
1271 	 */
1272 	atomic_inc(&psy->use_cnt);
1273 	psy->initialized = true;
1274 
1275 	queue_delayed_work(system_power_efficient_wq,
1276 			   &psy->deferred_register_work,
1277 			   POWER_SUPPLY_DEFERRED_REGISTER_TIME);
1278 
1279 	return psy;
1280 
1281 add_hwmon_sysfs_failed:
1282 	power_supply_remove_triggers(psy);
1283 create_triggers_failed:
1284 	psy_unregister_thermal(psy);
1285 register_thermal_failed:
1286 	device_del(dev);
1287 wakeup_init_failed:
1288 device_add_failed:
1289 check_supplies_failed:
1290 dev_set_name_failed:
1291 	put_device(dev);
1292 	return ERR_PTR(rc);
1293 }
1294 
1295 /**
1296  * power_supply_register() - Register new power supply
1297  * @parent:	Device to be a parent of power supply's device, usually
1298  *		the device which probe function calls this
1299  * @desc:	Description of power supply, must be valid through whole
1300  *		lifetime of this power supply
1301  * @cfg:	Run-time specific configuration accessed during registering,
1302  *		may be NULL
1303  *
1304  * Return: A pointer to newly allocated power_supply on success
1305  * or ERR_PTR otherwise.
1306  * Use power_supply_unregister() on returned power_supply pointer to release
1307  * resources.
1308  */
power_supply_register(struct device * parent,const struct power_supply_desc * desc,const struct power_supply_config * cfg)1309 struct power_supply *__must_check power_supply_register(struct device *parent,
1310 		const struct power_supply_desc *desc,
1311 		const struct power_supply_config *cfg)
1312 {
1313 	return __power_supply_register(parent, desc, cfg, true);
1314 }
1315 EXPORT_SYMBOL_GPL(power_supply_register);
1316 
1317 /**
1318  * power_supply_register_no_ws() - Register new non-waking-source power supply
1319  * @parent:	Device to be a parent of power supply's device, usually
1320  *		the device which probe function calls this
1321  * @desc:	Description of power supply, must be valid through whole
1322  *		lifetime of this power supply
1323  * @cfg:	Run-time specific configuration accessed during registering,
1324  *		may be NULL
1325  *
1326  * Return: A pointer to newly allocated power_supply on success
1327  * or ERR_PTR otherwise.
1328  * Use power_supply_unregister() on returned power_supply pointer to release
1329  * resources.
1330  */
1331 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)1332 power_supply_register_no_ws(struct device *parent,
1333 		const struct power_supply_desc *desc,
1334 		const struct power_supply_config *cfg)
1335 {
1336 	return __power_supply_register(parent, desc, cfg, false);
1337 }
1338 EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
1339 
devm_power_supply_release(struct device * dev,void * res)1340 static void devm_power_supply_release(struct device *dev, void *res)
1341 {
1342 	struct power_supply **psy = res;
1343 
1344 	power_supply_unregister(*psy);
1345 }
1346 
1347 /**
1348  * devm_power_supply_register() - Register managed power supply
1349  * @parent:	Device to be a parent of power supply's device, usually
1350  *		the device which probe function calls this
1351  * @desc:	Description of power supply, must be valid through whole
1352  *		lifetime of this power supply
1353  * @cfg:	Run-time specific configuration accessed during registering,
1354  *		may be NULL
1355  *
1356  * Return: A pointer to newly allocated power_supply on success
1357  * or ERR_PTR otherwise.
1358  * The returned power_supply pointer will be automatically unregistered
1359  * on driver detach.
1360  */
1361 struct power_supply *__must_check
devm_power_supply_register(struct device * parent,const struct power_supply_desc * desc,const struct power_supply_config * cfg)1362 devm_power_supply_register(struct device *parent,
1363 		const struct power_supply_desc *desc,
1364 		const struct power_supply_config *cfg)
1365 {
1366 	struct power_supply **ptr, *psy;
1367 
1368 	ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1369 
1370 	if (!ptr)
1371 		return ERR_PTR(-ENOMEM);
1372 	psy = __power_supply_register(parent, desc, cfg, true);
1373 	if (IS_ERR(psy)) {
1374 		devres_free(ptr);
1375 	} else {
1376 		*ptr = psy;
1377 		devres_add(parent, ptr);
1378 	}
1379 	return psy;
1380 }
1381 EXPORT_SYMBOL_GPL(devm_power_supply_register);
1382 
1383 /**
1384  * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
1385  * @parent:	Device to be a parent of power supply's device, usually
1386  *		the device which probe function calls this
1387  * @desc:	Description of power supply, must be valid through whole
1388  *		lifetime of this power supply
1389  * @cfg:	Run-time specific configuration accessed during registering,
1390  *		may be NULL
1391  *
1392  * Return: A pointer to newly allocated power_supply on success
1393  * or ERR_PTR otherwise.
1394  * The returned power_supply pointer will be automatically unregistered
1395  * on driver detach.
1396  */
1397 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)1398 devm_power_supply_register_no_ws(struct device *parent,
1399 		const struct power_supply_desc *desc,
1400 		const struct power_supply_config *cfg)
1401 {
1402 	struct power_supply **ptr, *psy;
1403 
1404 	ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1405 
1406 	if (!ptr)
1407 		return ERR_PTR(-ENOMEM);
1408 	psy = __power_supply_register(parent, desc, cfg, false);
1409 	if (IS_ERR(psy)) {
1410 		devres_free(ptr);
1411 	} else {
1412 		*ptr = psy;
1413 		devres_add(parent, ptr);
1414 	}
1415 	return psy;
1416 }
1417 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
1418 
1419 /**
1420  * power_supply_unregister() - Remove this power supply from system
1421  * @psy:	Pointer to power supply to unregister
1422  *
1423  * Remove this power supply from the system. The resources of power supply
1424  * will be freed here or on last power_supply_put() call.
1425  */
power_supply_unregister(struct power_supply * psy)1426 void power_supply_unregister(struct power_supply *psy)
1427 {
1428 	WARN_ON(atomic_dec_return(&psy->use_cnt));
1429 	psy->removing = true;
1430 	cancel_work_sync(&psy->changed_work);
1431 	cancel_delayed_work_sync(&psy->deferred_register_work);
1432 	sysfs_remove_link(&psy->dev.kobj, "powers");
1433 	power_supply_remove_hwmon_sysfs(psy);
1434 	power_supply_remove_triggers(psy);
1435 	psy_unregister_cooler(psy);
1436 	psy_unregister_thermal(psy);
1437 	device_init_wakeup(&psy->dev, false);
1438 	device_unregister(&psy->dev);
1439 }
1440 EXPORT_SYMBOL_GPL(power_supply_unregister);
1441 
power_supply_get_drvdata(struct power_supply * psy)1442 void *power_supply_get_drvdata(struct power_supply *psy)
1443 {
1444 	return psy->drv_data;
1445 }
1446 EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
1447 
power_supply_class_init(void)1448 static int __init power_supply_class_init(void)
1449 {
1450 	power_supply_class = class_create(THIS_MODULE, "power_supply");
1451 
1452 	if (IS_ERR(power_supply_class))
1453 		return PTR_ERR(power_supply_class);
1454 
1455 	power_supply_class->dev_uevent = power_supply_uevent;
1456 	power_supply_init_attrs(&power_supply_dev_type);
1457 
1458 	return 0;
1459 }
1460 
power_supply_class_exit(void)1461 static void __exit power_supply_class_exit(void)
1462 {
1463 	class_destroy(power_supply_class);
1464 }
1465 
1466 subsys_initcall(power_supply_class_init);
1467 module_exit(power_supply_class_exit);
1468 
1469 MODULE_DESCRIPTION("Universal power supply monitor class");
1470 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
1471 	      "Szabolcs Gyurko, "
1472 	      "Anton Vorontsov <cbou@mail.ru>");
1473 MODULE_LICENSE("GPL");
1474