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