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