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